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 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 36 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | --> List of (family, socktype, 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\ |
| 124 | listen(n) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 125 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 126 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 127 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 128 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 130 | sendall(data[, flags]) -- send all data\n\ |
| 131 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 132 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 133 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 134 | setsockopt(level, optname, value) -- set socket options\n\ |
| 135 | settimeout(None | float) -- set or clear the timeout\n\ |
| 136 | shutdown(how) -- shut down traffic in one or both directions\n\ |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 137 | if_nameindex() -- return all network interface indices and names\n\ |
Gregory P. Smith | b6471db | 2011-05-22 22:47:55 -0700 | [diff] [blame] | 138 | if_nametoindex(name) -- return the corresponding interface index\n\ |
| 139 | if_indextoname(index) -- return the corresponding interface name\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 140 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 141 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 142 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 143 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 144 | I hope some day someone can clean this up please... */ |
| 145 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 146 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 147 | script doesn't get this right, so we hardcode some platform checks below. |
| 148 | On the other hand, not all Linux versions agree, so there the settings |
| 149 | computed by the configure script are needed! */ |
| 150 | |
| 151 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 152 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 153 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 154 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 156 | |
Victor Stinner | 710d27e | 2011-08-23 10:57:32 +0200 | [diff] [blame] | 157 | #if defined(__OpenBSD__) |
| 158 | # include <sys/uio.h> |
| 159 | #endif |
| 160 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 161 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 162 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 163 | #endif |
| 164 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 165 | #ifdef HAVE_GETHOSTBYNAME_R |
Jesus Cea | 736e7fc | 2011-03-14 17:36:54 +0100 | [diff] [blame] | 166 | # if defined(_AIX) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 167 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 168 | # elif defined(__sun) || defined(__sgi) |
| 169 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 170 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 171 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 172 | # else |
| 173 | # undef HAVE_GETHOSTBYNAME_R |
| 174 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 177 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 178 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 179 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 180 | #endif |
| 181 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 182 | /* To use __FreeBSD_version */ |
| 183 | #ifdef HAVE_SYS_PARAM_H |
| 184 | #include <sys/param.h> |
| 185 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 186 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Just van Rossum | 09aecd7 | 2003-05-09 08:03:44 +0000 | [diff] [blame] | 187 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 188 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 189 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 190 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 191 | !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 192 | #define USE_GETADDRINFO_LOCK |
| 193 | #endif |
| 194 | |
| 195 | #ifdef USE_GETADDRINFO_LOCK |
| 196 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 197 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 198 | #else |
| 199 | #define ACQUIRE_GETADDRINFO_LOCK |
| 200 | #define RELEASE_GETADDRINFO_LOCK |
| 201 | #endif |
| 202 | |
| 203 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 204 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 205 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 208 | # include <types.h> |
| 209 | # include <io.h> |
| 210 | # include <sys/ioctl.h> |
| 211 | # include <utils.h> |
| 212 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 213 | #endif |
| 214 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 215 | #ifdef __APPLE__ |
| 216 | # include <sys/ioctl.h> |
| 217 | #endif |
| 218 | |
| 219 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 220 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 221 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 222 | functions are declared correctly if compiling with |
| 223 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 224 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 225 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 226 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 227 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 228 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 229 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 230 | #undef _XOPEN_SOURCE |
| 231 | #include <sys/socket.h> |
| 232 | #include <sys/types.h> |
| 233 | #include <netinet/in.h> |
| 234 | #ifdef _SS_ALIGNSIZE |
| 235 | #define HAVE_GETADDRINFO 1 |
| 236 | #define HAVE_GETNAMEINFO 1 |
| 237 | #endif |
| 238 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 239 | #define HAVE_INET_PTON |
| 240 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 243 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 244 | for both GCC and SGI's compiler. I'd say that the SGI headers |
Georg Brandl | dbd8339 | 2006-02-20 09:42:33 +0000 | [diff] [blame] | 245 | are just busted. Same thing for Solaris. */ |
| 246 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 247 | #define INET_ADDRSTRLEN 16 |
| 248 | #endif |
| 249 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 250 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 253 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 254 | |
Gregory P. Smith | 3b1f2c3 | 2011-05-15 12:18:23 -0700 | [diff] [blame] | 255 | #ifdef HAVE_SYS_SOCKET_H |
| 256 | #include <sys/socket.h> |
| 257 | #endif |
| 258 | |
| 259 | #ifdef HAVE_NET_IF_H |
| 260 | #include <net/if.h> |
| 261 | #endif |
| 262 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 263 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 264 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 265 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 266 | |
| 267 | /* Addressing includes */ |
| 268 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 269 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 270 | |
| 271 | /* Non-MS WINDOWS includes */ |
| 272 | # include <netdb.h> |
Nick Coghlan | 4ecf6cf | 2011-08-22 15:45:19 +1000 | [diff] [blame] | 273 | # include <unistd.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 275 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 276 | # include <arpa/inet.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 277 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 278 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 280 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 281 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | # ifdef HAVE_FCNTL_H |
| 284 | # include <fcntl.h> |
| 285 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 286 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 287 | #endif |
| 288 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 289 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 290 | |
| 291 | #ifndef offsetof |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 293 | #endif |
| 294 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 295 | #ifndef O_NONBLOCK |
| 296 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 297 | #endif |
| 298 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 299 | /* include Python's addrinfo.h unless it causes trouble */ |
| 300 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 301 | /* Do not include addinfo.h on some newer IRIX versions. |
| 302 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 303 | * for example, but not by 6.5.10. |
| 304 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 305 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 306 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 307 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 308 | */ |
| 309 | #else |
| 310 | # include "addrinfo.h" |
| 311 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 312 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 313 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 314 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 315 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 316 | 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] | 317 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 318 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 319 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 320 | #ifdef __APPLE__ |
| 321 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 322 | failure, so we must use the emulation instead of the libinfo |
| 323 | implementation. Unfortunately, performing an autoconf test |
| 324 | for this bug would require DNS access for the machine performing |
| 325 | the configuration, which is not acceptable. Therefore, we |
| 326 | determine the bug just by checking for __APPLE__. If this bug |
| 327 | gets ever fixed, perhaps checking for sys/version.h would be |
| 328 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 329 | #ifndef HAVE_GETNAMEINFO |
| 330 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 331 | Find to check for Jaguar is that it has getnameinfo(), which |
| 332 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 333 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 334 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 335 | |
| 336 | #ifdef HAVE_INET_ATON |
| 337 | #define USE_INET_ATON_WEAKLINK |
| 338 | #endif |
| 339 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 340 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 341 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 342 | /* 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] | 343 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 344 | /* avoid clashes with the C library definition of the symbol. */ |
| 345 | #define getaddrinfo fake_getaddrinfo |
| 346 | #define gai_strerror fake_gai_strerror |
| 347 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 348 | #include "getaddrinfo.c" |
| 349 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 350 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 351 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 352 | #include "getnameinfo.c" |
| 353 | #endif |
| 354 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 355 | #ifdef MS_WINDOWS |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 356 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 357 | #endif |
| 358 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 359 | #ifdef MS_WIN32 |
Kristján Valur Jónsson | 310052c | 2012-03-31 13:35:00 +0000 | [diff] [blame] | 360 | #undef EAFNOSUPPORT |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 361 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 362 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 363 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 365 | #ifndef SOCKETCLOSE |
| 366 | #define SOCKETCLOSE close |
| 367 | #endif |
| 368 | |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 369 | #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] | 370 | #define USE_BLUETOOTH 1 |
| 371 | #if defined(__FreeBSD__) |
| 372 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 373 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 374 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 375 | #define SOL_HCI SOL_HCI_RAW |
| 376 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 377 | #define sockaddr_l2 sockaddr_l2cap |
| 378 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 379 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 380 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 381 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 382 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 383 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 384 | #define sockaddr_l2 sockaddr_bt |
| 385 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 386 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 387 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 388 | #define SOL_HCI BTPROTO_HCI |
| 389 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 390 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 391 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 392 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 393 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 394 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 395 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 396 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 397 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 398 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 399 | #endif |
| 400 | #endif |
| 401 | |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 402 | /* Convert "sock_addr_t *" to "struct sockaddr *". */ |
| 403 | #define SAS2SA(x) (&((x)->sa)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 404 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 405 | /* |
| 406 | * Constants for getnameinfo() |
| 407 | */ |
| 408 | #if !defined(NI_MAXHOST) |
| 409 | #define NI_MAXHOST 1025 |
| 410 | #endif |
| 411 | #if !defined(NI_MAXSERV) |
| 412 | #define NI_MAXSERV 32 |
| 413 | #endif |
| 414 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 415 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 416 | #define INVALID_SOCKET (-1) |
| 417 | #endif |
| 418 | |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 419 | #ifndef INADDR_NONE |
| 420 | #define INADDR_NONE (-1) |
| 421 | #endif |
| 422 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 423 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 424 | a Py prefix (or use CapitalizedWords). Later... */ |
| 425 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 426 | /* Global variable holding the exception type for errors detected |
| 427 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 428 | static PyObject *socket_herror; |
| 429 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 430 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 431 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 432 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 433 | The sock_type variable contains pointers to various functions, |
| 434 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 435 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 436 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 437 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 438 | #if defined(HAVE_POLL_H) |
| 439 | #include <poll.h> |
| 440 | #elif defined(HAVE_SYS_POLL_H) |
| 441 | #include <sys/poll.h> |
| 442 | #endif |
| 443 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 444 | /* Largest value to try to store in a socklen_t (used when handling |
| 445 | ancillary data). POSIX requires socklen_t to hold at least |
| 446 | (2**31)-1 and recommends against storing larger values, but |
| 447 | socklen_t was originally int in the BSD interface, so to be on the |
| 448 | safe side we use the smaller of (2**31)-1 and INT_MAX. */ |
| 449 | #if INT_MAX > 0x7fffffff |
| 450 | #define SOCKLEN_T_LIMIT 0x7fffffff |
| 451 | #else |
| 452 | #define SOCKLEN_T_LIMIT INT_MAX |
| 453 | #endif |
| 454 | |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 455 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 456 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 457 | #define IS_SELECTABLE(s) 1 |
| 458 | /* 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] | 459 | #else |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 460 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 461 | * little white lie. */ |
| 462 | #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] | 463 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 464 | |
| 465 | static PyObject* |
| 466 | select_error(void) |
| 467 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 468 | PyErr_SetString(PyExc_OSError, "unable to select on socket"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 472 | #ifdef MS_WINDOWS |
Antoine Pitrou | c8f0196 | 2010-09-28 22:03:27 +0000 | [diff] [blame] | 473 | #ifndef WSAEAGAIN |
| 474 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 475 | #endif |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 476 | #define CHECK_ERRNO(expected) \ |
| 477 | (WSAGetLastError() == WSA ## expected) |
| 478 | #else |
| 479 | #define CHECK_ERRNO(expected) \ |
| 480 | (errno == expected) |
| 481 | #endif |
| 482 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 483 | #ifdef MS_WINDOWS |
| 484 | /* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */ |
| 485 | static int support_wsa_no_inherit = -1; |
| 486 | #endif |
| 487 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 488 | /* Convenience function to raise an error according to errno |
| 489 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 491 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 492 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 493 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 494 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | int err_no = WSAGetLastError(); |
| 496 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 497 | recognizes the error codes used by both GetLastError() and |
| 498 | WSAGetLastError */ |
| 499 | if (err_no) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 500 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 501 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 502 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 503 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 506 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 507 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 508 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 511 | |
| 512 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 514 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 516 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | if (v != NULL) { |
| 518 | PyErr_SetObject(socket_herror, v); |
| 519 | Py_DECREF(v); |
| 520 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
| 526 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 527 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 530 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 531 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 533 | if (error == EAI_SYSTEM) |
| 534 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 535 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 536 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 537 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 539 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 541 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | if (v != NULL) { |
| 543 | PyErr_SetObject(socket_gaierror, v); |
| 544 | Py_DECREF(v); |
| 545 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 550 | /* Function to perform the setting of socket blocking mode |
| 551 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 552 | static int |
| 553 | internal_setblocking(PySocketSockObject *s, int block) |
| 554 | { |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 555 | #if !defined(MS_WINDOWS) \ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 556 | && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 557 | int delay_flag, new_delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 558 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 559 | #ifdef SOCK_NONBLOCK |
| 560 | if (block) |
| 561 | s->sock_type &= (~SOCK_NONBLOCK); |
| 562 | else |
| 563 | s->sock_type |= SOCK_NONBLOCK; |
| 564 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 567 | #ifndef MS_WINDOWS |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 568 | #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | block = !block; |
| 570 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 571 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 573 | if (block) |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 574 | new_delay_flag = delay_flag & (~O_NONBLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | else |
Victor Stinner | 9a95483 | 2013-12-04 00:41:24 +0100 | [diff] [blame] | 576 | new_delay_flag = delay_flag | O_NONBLOCK; |
| 577 | if (new_delay_flag != delay_flag) |
| 578 | fcntl(s->sock_fd, F_SETFL, new_delay_flag); |
| 579 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 580 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | block = !block; |
| 582 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 583 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | /* Since these don't return anything */ |
| 587 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 590 | /* 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] | 591 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 592 | This does not raise an exception; we'll let our caller do that |
| 593 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 594 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 595 | static int |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 596 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 601 | if (s->sock_timeout <= 0.0) |
| 602 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | /* Guard against closed socket */ |
| 605 | if (s->sock_fd < 0) |
| 606 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 608 | /* Handling this condition here simplifies the select loops */ |
| 609 | if (interval < 0.0) |
| 610 | return 1; |
| 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | /* Prefer poll, if available, since you can poll() any fd |
| 613 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 614 | #ifdef HAVE_POLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | { |
| 616 | struct pollfd pollfd; |
| 617 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | pollfd.fd = s->sock_fd; |
| 620 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | /* s->sock_timeout is in seconds, timeout in ms */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 623 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | n = poll(&pollfd, 1, timeout); |
| 625 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 626 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | { |
| 628 | /* Construct the arguments to select */ |
| 629 | fd_set fds; |
| 630 | struct timeval tv; |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 631 | tv.tv_sec = (int)interval; |
| 632 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | FD_ZERO(&fds); |
| 634 | FD_SET(s->sock_fd, &fds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | /* See if the socket is ready */ |
| 637 | if (writing) |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 638 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 639 | NULL, &fds, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | else |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 641 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 642 | &fds, NULL, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 644 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | if (n < 0) |
| 647 | return -1; |
| 648 | if (n == 0) |
| 649 | return 1; |
| 650 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 653 | static int |
| 654 | internal_select(PySocketSockObject *s, int writing) |
| 655 | { |
| 656 | return internal_select_ex(s, writing, s->sock_timeout); |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | Two macros for automatic retry of select() in case of false positives |
| 661 | (for example, select() could indicate a socket is ready for reading |
| 662 | but the data then discarded by the OS because of a wrong checksum). |
| 663 | Here is an example of use: |
| 664 | |
| 665 | BEGIN_SELECT_LOOP(s) |
| 666 | Py_BEGIN_ALLOW_THREADS |
| 667 | timeout = internal_select_ex(s, 0, interval); |
| 668 | if (!timeout) |
| 669 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 670 | Py_END_ALLOW_THREADS |
| 671 | if (timeout == 1) { |
| 672 | PyErr_SetString(socket_timeout, "timed out"); |
| 673 | return -1; |
| 674 | } |
| 675 | END_SELECT_LOOP(s) |
| 676 | */ |
| 677 | |
| 678 | #define BEGIN_SELECT_LOOP(s) \ |
| 679 | { \ |
| 680 | _PyTime_timeval now, deadline = {0, 0}; \ |
| 681 | double interval = s->sock_timeout; \ |
| 682 | int has_timeout = s->sock_timeout > 0.0; \ |
| 683 | if (has_timeout) { \ |
| 684 | _PyTime_gettimeofday(&now); \ |
| 685 | deadline = now; \ |
| 686 | _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \ |
| 687 | } \ |
| 688 | while (1) { \ |
| 689 | errno = 0; \ |
| 690 | |
| 691 | #define END_SELECT_LOOP(s) \ |
| 692 | if (!has_timeout || \ |
| 693 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 694 | break; \ |
| 695 | _PyTime_gettimeofday(&now); \ |
| 696 | interval = _PyTime_INTERVAL(now, deadline); \ |
| 697 | } \ |
| 698 | } \ |
| 699 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 700 | /* Initialize a new socket object. */ |
| 701 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 702 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 703 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 704 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 705 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 707 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | s->sock_fd = fd; |
| 709 | s->sock_family = family; |
| 710 | s->sock_type = type; |
| 711 | s->sock_proto = proto; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | s->errorhandler = &set_error; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 714 | #ifdef SOCK_NONBLOCK |
| 715 | if (type & SOCK_NONBLOCK) |
| 716 | s->sock_timeout = 0.0; |
| 717 | else |
| 718 | #endif |
| 719 | { |
| 720 | s->sock_timeout = defaulttimeout; |
| 721 | if (defaulttimeout >= 0.0) |
| 722 | internal_setblocking(s, 0); |
| 723 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 724 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 728 | /* Create a new socket object. |
| 729 | This just creates the object and initializes it. |
| 730 | If the creation fails, return NULL and set an exception (implicit |
| 731 | in NEWOBJ()). */ |
| 732 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 733 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 734 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | PySocketSockObject *s; |
| 737 | s = (PySocketSockObject *) |
| 738 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 739 | if (s != NULL) |
| 740 | init_sockobject(s, fd, family, type, proto); |
| 741 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 744 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 745 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 746 | thread to be in gethostbyname or getaddrinfo */ |
| 747 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | d182a6c | 2012-08-14 17:29:04 +0200 | [diff] [blame] | 748 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 749 | #endif |
| 750 | |
| 751 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 752 | /* Convert a string specifying a host name or one of a few symbolic |
| 753 | names to a numeric IP address. This usually calls gethostbyname() |
| 754 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 755 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 756 | an error occurred; then an exception is raised. */ |
| 757 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 758 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 759 | 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] | 760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | struct addrinfo hints, *res; |
| 762 | int error; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 765 | if (name[0] == '\0') { |
| 766 | int siz; |
| 767 | memset(&hints, 0, sizeof(hints)); |
| 768 | hints.ai_family = af; |
| 769 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 770 | hints.ai_flags = AI_PASSIVE; |
| 771 | Py_BEGIN_ALLOW_THREADS |
| 772 | ACQUIRE_GETADDRINFO_LOCK |
| 773 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 774 | Py_END_ALLOW_THREADS |
| 775 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 776 | *are* safe regarding their return value, ie. that a |
| 777 | subsequent call to getaddrinfo() does not destroy the |
| 778 | outcome of the first call. */ |
| 779 | RELEASE_GETADDRINFO_LOCK |
| 780 | if (error) { |
| 781 | set_gaierror(error); |
| 782 | return -1; |
| 783 | } |
| 784 | switch (res->ai_family) { |
| 785 | case AF_INET: |
| 786 | siz = 4; |
| 787 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 788 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | case AF_INET6: |
| 790 | siz = 16; |
| 791 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 792 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | default: |
| 794 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 795 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | "unsupported address family"); |
| 797 | return -1; |
| 798 | } |
| 799 | if (res->ai_next) { |
| 800 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 801 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | "wildcard resolved to multiple address"); |
| 803 | return -1; |
| 804 | } |
| 805 | if (res->ai_addrlen < addr_ret_size) |
| 806 | addr_ret_size = res->ai_addrlen; |
| 807 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 808 | freeaddrinfo(res); |
| 809 | return siz; |
| 810 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 811 | /* special-case broadcast - inet_addr() below can return INADDR_NONE for |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 812 | * this */ |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 813 | if (strcmp(name, "255.255.255.255") == 0 || |
| 814 | strcmp(name, "<broadcast>") == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | struct sockaddr_in *sin; |
| 816 | if (af != AF_INET && af != AF_UNSPEC) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 817 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | "address family mismatched"); |
| 819 | return -1; |
| 820 | } |
| 821 | sin = (struct sockaddr_in *)addr_ret; |
| 822 | memset((void *) sin, '\0', sizeof(*sin)); |
| 823 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 824 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 826 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 828 | return sizeof(sin->sin_addr); |
| 829 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 830 | |
| 831 | /* avoid a name resolution in case of numeric address */ |
| 832 | #ifdef HAVE_INET_PTON |
| 833 | /* check for an IPv4 address */ |
| 834 | if (af == AF_UNSPEC || af == AF_INET) { |
| 835 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 836 | memset(sin, 0, sizeof(*sin)); |
| 837 | if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) { |
| 838 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 839 | #ifdef HAVE_SOCKADDR_SA_LEN |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 840 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 841 | #endif |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 842 | return 4; |
| 843 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 845 | #ifdef ENABLE_IPV6 |
| 846 | /* check for an IPv6 address - if the address contains a scope ID, we |
| 847 | * fallback to getaddrinfo(), which can handle translation from interface |
| 848 | * name to interface index */ |
| 849 | if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) { |
| 850 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret; |
| 851 | memset(sin, 0, sizeof(*sin)); |
| 852 | if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) { |
| 853 | sin->sin6_family = AF_INET6; |
| 854 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 855 | sin->sin6_len = sizeof(*sin); |
| 856 | #endif |
| 857 | return 16; |
| 858 | } |
| 859 | } |
| 860 | #endif /* ENABLE_IPV6 */ |
| 861 | #else /* HAVE_INET_PTON */ |
| 862 | /* check for an IPv4 address */ |
| 863 | if (af == AF_INET || af == AF_UNSPEC) { |
| 864 | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
| 865 | memset(sin, 0, sizeof(*sin)); |
| 866 | if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { |
| 867 | sin->sin_family = AF_INET; |
| 868 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 869 | sin->sin_len = sizeof(*sin); |
| 870 | #endif |
| 871 | return 4; |
| 872 | } |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 873 | } |
Charles-François Natali | 0cc8685 | 2013-09-13 19:53:08 +0200 | [diff] [blame] | 874 | #endif /* HAVE_INET_PTON */ |
| 875 | |
| 876 | /* perform a name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | memset(&hints, 0, sizeof(hints)); |
| 878 | hints.ai_family = af; |
| 879 | Py_BEGIN_ALLOW_THREADS |
| 880 | ACQUIRE_GETADDRINFO_LOCK |
| 881 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 882 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 884 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 885 | if no address family is given. Assume IPv4 for now.*/ |
| 886 | hints.ai_family = AF_INET; |
| 887 | error = getaddrinfo(name, NULL, &hints, &res); |
| 888 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 889 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | Py_END_ALLOW_THREADS |
| 891 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 892 | if (error) { |
| 893 | set_gaierror(error); |
| 894 | return -1; |
| 895 | } |
| 896 | if (res->ai_addrlen < addr_ret_size) |
| 897 | addr_ret_size = res->ai_addrlen; |
| 898 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 899 | freeaddrinfo(res); |
| 900 | switch (addr_ret->sa_family) { |
| 901 | case AF_INET: |
| 902 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 903 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | case AF_INET6: |
| 905 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 906 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 908 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | return -1; |
| 910 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 913 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 914 | /* Create a string object representing an IP address. |
| 915 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 916 | size numbers). */ |
| 917 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 918 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 919 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | char buf[NI_MAXHOST]; |
| 922 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 925 | NI_NUMERICHOST); |
| 926 | if (error) { |
| 927 | set_gaierror(error); |
| 928 | return NULL; |
| 929 | } |
| 930 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 934 | #ifdef USE_BLUETOOTH |
| 935 | /* Convert a string representation of a Bluetooth address into a numeric |
| 936 | address. Returns the length (6), or raises an exception and returns -1 if |
| 937 | an error occurred. */ |
| 938 | |
| 939 | static int |
| 940 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | unsigned int b0, b1, b2, b3, b4, b5; |
| 943 | char ch; |
| 944 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 947 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 948 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 949 | bdaddr->b[0] = b0; |
| 950 | bdaddr->b[1] = b1; |
| 951 | bdaddr->b[2] = b2; |
| 952 | bdaddr->b[3] = b3; |
| 953 | bdaddr->b[4] = b4; |
| 954 | bdaddr->b[5] = b5; |
| 955 | return 6; |
| 956 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 957 | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | return -1; |
| 959 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* Create a string representation of the Bluetooth address. This is always a |
| 963 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 964 | value (zero padded if necessary). */ |
| 965 | |
| 966 | static PyObject * |
| 967 | makebdaddr(bdaddr_t *bdaddr) |
| 968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 972 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 973 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 974 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 975 | } |
| 976 | #endif |
| 977 | |
| 978 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 979 | /* Create an object representing the given socket address, |
| 980 | suitable for passing it back to bind(), connect() etc. |
| 981 | The family field of the sockaddr structure is inspected |
| 982 | to determine what kind of address it really is. */ |
| 983 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 984 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 985 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 986 | 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] | 987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | if (addrlen == 0) { |
| 989 | /* No address -- may be recvfrom() from known socket */ |
| 990 | Py_INCREF(Py_None); |
| 991 | return Py_None; |
| 992 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | case AF_INET: |
| 997 | { |
| 998 | struct sockaddr_in *a; |
| 999 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1000 | PyObject *ret = NULL; |
| 1001 | if (addrobj) { |
| 1002 | a = (struct sockaddr_in *)addr; |
| 1003 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1004 | Py_DECREF(addrobj); |
| 1005 | } |
| 1006 | return ret; |
| 1007 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1008 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1009 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | case AF_UNIX: |
| 1011 | { |
| 1012 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1013 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1015 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
Charles-François Natali | b10c71d | 2013-05-21 10:45:46 +0200 | [diff] [blame] | 1016 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | } |
| 1018 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1019 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | { |
| 1021 | /* regular NULL-terminated string */ |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1022 | return PyUnicode_DecodeFSDefault(a->sun_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1025 | #endif /* AF_UNIX */ |
| 1026 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1027 | #if defined(AF_NETLINK) |
| 1028 | case AF_NETLINK: |
| 1029 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1031 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1032 | } |
| 1033 | #endif /* AF_NETLINK */ |
| 1034 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1035 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | case AF_INET6: |
| 1037 | { |
| 1038 | struct sockaddr_in6 *a; |
| 1039 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1040 | PyObject *ret = NULL; |
| 1041 | if (addrobj) { |
| 1042 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1043 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | addrobj, |
| 1045 | ntohs(a->sin6_port), |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1046 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | a->sin6_scope_id); |
| 1048 | Py_DECREF(addrobj); |
| 1049 | } |
| 1050 | return ret; |
| 1051 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1052 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1053 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1054 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | case AF_BLUETOOTH: |
| 1056 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | case BTPROTO_L2CAP: |
| 1059 | { |
| 1060 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1061 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1062 | PyObject *ret = NULL; |
| 1063 | if (addrobj) { |
| 1064 | ret = Py_BuildValue("Oi", |
| 1065 | addrobj, |
| 1066 | _BT_L2_MEMB(a, psm)); |
| 1067 | Py_DECREF(addrobj); |
| 1068 | } |
| 1069 | return ret; |
| 1070 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | case BTPROTO_RFCOMM: |
| 1073 | { |
| 1074 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1075 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1076 | PyObject *ret = NULL; |
| 1077 | if (addrobj) { |
| 1078 | ret = Py_BuildValue("Oi", |
| 1079 | addrobj, |
| 1080 | _BT_RC_MEMB(a, channel)); |
| 1081 | Py_DECREF(addrobj); |
| 1082 | } |
| 1083 | return ret; |
| 1084 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | case BTPROTO_HCI: |
| 1087 | { |
| 1088 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1089 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1090 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1091 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | PyObject *ret = NULL; |
| 1093 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1094 | return ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1095 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1097 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1098 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | case BTPROTO_SCO: |
| 1100 | { |
| 1101 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1102 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1103 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1104 | #endif |
| 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | default: |
| 1107 | PyErr_SetString(PyExc_ValueError, |
| 1108 | "Unknown Bluetooth protocol"); |
| 1109 | return NULL; |
| 1110 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1111 | #endif |
| 1112 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1113 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | case AF_PACKET: |
| 1115 | { |
| 1116 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1117 | char *ifname = ""; |
| 1118 | struct ifreq ifr; |
| 1119 | /* need to look up interface name give index */ |
| 1120 | if (a->sll_ifindex) { |
| 1121 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1122 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1123 | ifname = ifr.ifr_name; |
| 1124 | } |
| 1125 | return Py_BuildValue("shbhy#", |
| 1126 | ifname, |
| 1127 | ntohs(a->sll_protocol), |
| 1128 | a->sll_pkttype, |
| 1129 | a->sll_hatype, |
| 1130 | a->sll_addr, |
| 1131 | a->sll_halen); |
| 1132 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1133 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1134 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1135 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | case AF_TIPC: |
| 1137 | { |
| 1138 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1139 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1140 | return Py_BuildValue("IIIII", |
| 1141 | a->addrtype, |
| 1142 | a->addr.nameseq.type, |
| 1143 | a->addr.nameseq.lower, |
| 1144 | a->addr.nameseq.upper, |
| 1145 | a->scope); |
| 1146 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1147 | return Py_BuildValue("IIIII", |
| 1148 | a->addrtype, |
| 1149 | a->addr.name.name.type, |
| 1150 | a->addr.name.name.instance, |
| 1151 | a->addr.name.name.instance, |
| 1152 | a->scope); |
| 1153 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1154 | return Py_BuildValue("IIIII", |
| 1155 | a->addrtype, |
| 1156 | a->addr.id.node, |
| 1157 | a->addr.id.ref, |
| 1158 | 0, |
| 1159 | a->scope); |
| 1160 | } else { |
| 1161 | PyErr_SetString(PyExc_ValueError, |
| 1162 | "Invalid address type"); |
| 1163 | return NULL; |
| 1164 | } |
| 1165 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1166 | #endif |
| 1167 | |
Charles-François Natali | b4062e8 | 2014-02-08 22:54:11 +0100 | [diff] [blame] | 1168 | #ifdef HAVE_LINUX_CAN_H |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1169 | case AF_CAN: |
| 1170 | { |
| 1171 | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
| 1172 | char *ifname = ""; |
| 1173 | struct ifreq ifr; |
| 1174 | /* need to look up interface name given index */ |
| 1175 | if (a->can_ifindex) { |
| 1176 | ifr.ifr_ifindex = a->can_ifindex; |
| 1177 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1178 | ifname = ifr.ifr_name; |
| 1179 | } |
| 1180 | |
| 1181 | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
| 1182 | ifname, |
| 1183 | a->can_family); |
| 1184 | } |
| 1185 | #endif |
| 1186 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1187 | #ifdef PF_SYSTEM |
| 1188 | case PF_SYSTEM: |
| 1189 | switch(proto) { |
| 1190 | #ifdef SYSPROTO_CONTROL |
| 1191 | case SYSPROTO_CONTROL: |
| 1192 | { |
| 1193 | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
| 1194 | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
| 1195 | } |
| 1196 | #endif |
| 1197 | default: |
| 1198 | PyErr_SetString(PyExc_ValueError, |
| 1199 | "Invalid address type"); |
| 1200 | return 0; |
| 1201 | } |
| 1202 | #endif |
| 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | default: |
| 1207 | /* If we don't know the address family, don't raise an |
| 1208 | exception -- return it as an (int, bytes) tuple. */ |
| 1209 | return Py_BuildValue("iy#", |
| 1210 | addr->sa_family, |
| 1211 | addr->sa_data, |
| 1212 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* Parse a socket address argument according to the socket object's |
| 1219 | address family. Return 1 if the address was in the proper format, |
| 1220 | 0 of not. The address is returned through addr_ret, its length |
| 1221 | through len_ret. */ |
| 1222 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1223 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1224 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1228 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1229 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | case AF_UNIX: |
| 1231 | { |
| 1232 | struct sockaddr_un* addr; |
| 1233 | char *path; |
| 1234 | int len; |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1235 | int retval = 0; |
| 1236 | |
| 1237 | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
| 1238 | allow embedded nulls on Linux. */ |
| 1239 | if (PyUnicode_Check(args)) { |
| 1240 | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
| 1241 | return 0; |
| 1242 | } |
| 1243 | else |
| 1244 | Py_INCREF(args); |
| 1245 | if (!PyArg_Parse(args, "y#", &path, &len)) |
| 1246 | goto unix_out; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1249 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | if (len > 0 && path[0] == 0) { |
| 1251 | /* Linux abstract namespace extension */ |
| 1252 | if (len > sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1253 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1255 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1259 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | { |
| 1261 | /* regular NULL-terminated string */ |
| 1262 | if (len >= sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1263 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1265 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | } |
| 1267 | addr->sun_path[len] = 0; |
| 1268 | } |
| 1269 | addr->sun_family = s->sock_family; |
| 1270 | memcpy(addr->sun_path, path, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1272 | retval = 1; |
| 1273 | unix_out: |
| 1274 | Py_DECREF(args); |
| 1275 | return retval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1277 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1278 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1279 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | case AF_NETLINK: |
| 1281 | { |
| 1282 | struct sockaddr_nl* addr; |
| 1283 | int pid, groups; |
| 1284 | addr = (struct sockaddr_nl *)addr_ret; |
| 1285 | if (!PyTuple_Check(args)) { |
| 1286 | PyErr_Format( |
| 1287 | PyExc_TypeError, |
| 1288 | "getsockaddrarg: " |
| 1289 | "AF_NETLINK address must be tuple, not %.500s", |
| 1290 | Py_TYPE(args)->tp_name); |
| 1291 | return 0; |
| 1292 | } |
| 1293 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1294 | return 0; |
| 1295 | addr->nl_family = AF_NETLINK; |
| 1296 | addr->nl_pid = pid; |
| 1297 | addr->nl_groups = groups; |
| 1298 | *len_ret = sizeof(*addr); |
| 1299 | return 1; |
| 1300 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1301 | #endif |
| 1302 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1303 | #ifdef AF_RDS |
| 1304 | case AF_RDS: |
| 1305 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1306 | #endif |
| 1307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | case AF_INET: |
| 1309 | { |
| 1310 | struct sockaddr_in* addr; |
| 1311 | char *host; |
| 1312 | int port, result; |
| 1313 | if (!PyTuple_Check(args)) { |
| 1314 | PyErr_Format( |
| 1315 | PyExc_TypeError, |
| 1316 | "getsockaddrarg: " |
| 1317 | "AF_INET address must be tuple, not %.500s", |
| 1318 | Py_TYPE(args)->tp_name); |
| 1319 | return 0; |
| 1320 | } |
| 1321 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1322 | "idna", &host, &port)) |
| 1323 | return 0; |
| 1324 | addr=(struct sockaddr_in*)addr_ret; |
| 1325 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1326 | sizeof(*addr), AF_INET); |
| 1327 | PyMem_Free(host); |
| 1328 | if (result < 0) |
| 1329 | return 0; |
| 1330 | if (port < 0 || port > 0xffff) { |
| 1331 | PyErr_SetString( |
| 1332 | PyExc_OverflowError, |
| 1333 | "getsockaddrarg: port must be 0-65535."); |
| 1334 | return 0; |
| 1335 | } |
| 1336 | addr->sin_family = AF_INET; |
| 1337 | addr->sin_port = htons((short)port); |
| 1338 | *len_ret = sizeof *addr; |
| 1339 | return 1; |
| 1340 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1341 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1342 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | case AF_INET6: |
| 1344 | { |
| 1345 | struct sockaddr_in6* addr; |
| 1346 | char *host; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1347 | int port, result; |
| 1348 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | flowinfo = scope_id = 0; |
| 1350 | if (!PyTuple_Check(args)) { |
| 1351 | PyErr_Format( |
| 1352 | PyExc_TypeError, |
| 1353 | "getsockaddrarg: " |
| 1354 | "AF_INET6 address must be tuple, not %.500s", |
| 1355 | Py_TYPE(args)->tp_name); |
| 1356 | return 0; |
| 1357 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1358 | if (!PyArg_ParseTuple(args, "eti|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | "idna", &host, &port, &flowinfo, |
| 1360 | &scope_id)) { |
| 1361 | return 0; |
| 1362 | } |
| 1363 | addr = (struct sockaddr_in6*)addr_ret; |
| 1364 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1365 | sizeof(*addr), AF_INET6); |
| 1366 | PyMem_Free(host); |
| 1367 | if (result < 0) |
| 1368 | return 0; |
| 1369 | if (port < 0 || port > 0xffff) { |
| 1370 | PyErr_SetString( |
| 1371 | PyExc_OverflowError, |
| 1372 | "getsockaddrarg: port must be 0-65535."); |
| 1373 | return 0; |
| 1374 | } |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 1375 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1376 | PyErr_SetString( |
| 1377 | PyExc_OverflowError, |
| 1378 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1379 | return 0; |
| 1380 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | addr->sin6_family = s->sock_family; |
| 1382 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1383 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | addr->sin6_scope_id = scope_id; |
| 1385 | *len_ret = sizeof *addr; |
| 1386 | return 1; |
| 1387 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1388 | #endif |
| 1389 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1390 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | case AF_BLUETOOTH: |
| 1392 | { |
| 1393 | switch (s->sock_proto) { |
| 1394 | case BTPROTO_L2CAP: |
| 1395 | { |
| 1396 | struct sockaddr_l2 *addr; |
| 1397 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1400 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1401 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1402 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1403 | &_BT_L2_MEMB(addr, psm))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1404 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | "wrong format"); |
| 1406 | return 0; |
| 1407 | } |
| 1408 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1409 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | *len_ret = sizeof *addr; |
| 1412 | return 1; |
| 1413 | } |
| 1414 | case BTPROTO_RFCOMM: |
| 1415 | { |
| 1416 | struct sockaddr_rc *addr; |
| 1417 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | addr = (struct sockaddr_rc *)addr_ret; |
| 1420 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1421 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1422 | &_BT_RC_MEMB(addr, channel))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1423 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | "wrong format"); |
| 1425 | return 0; |
| 1426 | } |
| 1427 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1428 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | *len_ret = sizeof *addr; |
| 1431 | return 1; |
| 1432 | } |
| 1433 | case BTPROTO_HCI: |
| 1434 | { |
| 1435 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1436 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1437 | char *straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1438 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1439 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1440 | if (straddr == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1441 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1442 | "wrong format"); |
| 1443 | return 0; |
| 1444 | } |
| 1445 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1446 | return 0; |
| 1447 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1449 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1450 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | "wrong format"); |
| 1452 | return 0; |
| 1453 | } |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1454 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | *len_ret = sizeof *addr; |
| 1456 | return 1; |
| 1457 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1458 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | case BTPROTO_SCO: |
| 1460 | { |
| 1461 | struct sockaddr_sco *addr; |
| 1462 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | addr = (struct sockaddr_sco *)addr_ret; |
| 1465 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1466 | if (!PyBytes_Check(args)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1467 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | "wrong format"); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | straddr = PyBytes_AS_STRING(args); |
| 1472 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1473 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | *len_ret = sizeof *addr; |
| 1476 | return 1; |
| 1477 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1478 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1480 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | return 0; |
| 1482 | } |
| 1483 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1484 | #endif |
| 1485 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1486 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | case AF_PACKET: |
| 1488 | { |
| 1489 | struct sockaddr_ll* addr; |
| 1490 | struct ifreq ifr; |
| 1491 | char *interfaceName; |
| 1492 | int protoNumber; |
| 1493 | int hatype = 0; |
| 1494 | int pkttype = 0; |
| 1495 | char *haddr = NULL; |
| 1496 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | if (!PyTuple_Check(args)) { |
| 1499 | PyErr_Format( |
| 1500 | PyExc_TypeError, |
| 1501 | "getsockaddrarg: " |
| 1502 | "AF_PACKET address must be tuple, not %.500s", |
| 1503 | Py_TYPE(args)->tp_name); |
| 1504 | return 0; |
| 1505 | } |
| 1506 | if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, |
| 1507 | &protoNumber, &pkttype, &hatype, |
| 1508 | &haddr, &halen)) |
| 1509 | return 0; |
| 1510 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1511 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1512 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1513 | s->errorhandler(); |
| 1514 | return 0; |
| 1515 | } |
| 1516 | if (halen > 8) { |
| 1517 | PyErr_SetString(PyExc_ValueError, |
| 1518 | "Hardware address must be 8 bytes or less"); |
| 1519 | return 0; |
| 1520 | } |
| 1521 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1522 | PyErr_SetString( |
| 1523 | PyExc_OverflowError, |
| 1524 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1525 | return 0; |
| 1526 | } |
| 1527 | addr = (struct sockaddr_ll*)addr_ret; |
| 1528 | addr->sll_family = AF_PACKET; |
| 1529 | addr->sll_protocol = htons((short)protoNumber); |
| 1530 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1531 | addr->sll_pkttype = pkttype; |
| 1532 | addr->sll_hatype = hatype; |
| 1533 | if (halen != 0) { |
| 1534 | memcpy(&addr->sll_addr, haddr, halen); |
| 1535 | } |
| 1536 | addr->sll_halen = halen; |
| 1537 | *len_ret = sizeof *addr; |
| 1538 | return 1; |
| 1539 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1540 | #endif |
| 1541 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1542 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | case AF_TIPC: |
| 1544 | { |
| 1545 | unsigned int atype, v1, v2, v3; |
| 1546 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1547 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | if (!PyTuple_Check(args)) { |
| 1550 | PyErr_Format( |
| 1551 | PyExc_TypeError, |
| 1552 | "getsockaddrarg: " |
| 1553 | "AF_TIPC address must be tuple, not %.500s", |
| 1554 | Py_TYPE(args)->tp_name); |
| 1555 | return 0; |
| 1556 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | if (!PyArg_ParseTuple(args, |
| 1559 | "IIII|I;Invalid TIPC address format", |
| 1560 | &atype, &v1, &v2, &v3, &scope)) |
| 1561 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1564 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | addr->family = AF_TIPC; |
| 1567 | addr->scope = scope; |
| 1568 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1571 | addr->addr.nameseq.type = v1; |
| 1572 | addr->addr.nameseq.lower = v2; |
| 1573 | addr->addr.nameseq.upper = v3; |
| 1574 | } else if (atype == TIPC_ADDR_NAME) { |
| 1575 | addr->addr.name.name.type = v1; |
| 1576 | addr->addr.name.name.instance = v2; |
| 1577 | } else if (atype == TIPC_ADDR_ID) { |
| 1578 | addr->addr.id.node = v1; |
| 1579 | addr->addr.id.ref = v2; |
| 1580 | } else { |
| 1581 | /* Shouldn't happen */ |
| 1582 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1583 | return 0; |
| 1584 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | return 1; |
| 1589 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1590 | #endif |
| 1591 | |
Charles-François Natali | b4062e8 | 2014-02-08 22:54:11 +0100 | [diff] [blame] | 1592 | #ifdef HAVE_LINUX_CAN_H |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1593 | case AF_CAN: |
| 1594 | switch (s->sock_proto) { |
| 1595 | case CAN_RAW: |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 1596 | /* fall-through */ |
| 1597 | case CAN_BCM: |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1598 | { |
| 1599 | struct sockaddr_can *addr; |
| 1600 | PyObject *interfaceName; |
| 1601 | struct ifreq ifr; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1602 | Py_ssize_t len; |
| 1603 | |
Benjamin Peterson | 18b7191 | 2013-05-16 15:29:44 -0500 | [diff] [blame] | 1604 | addr = (struct sockaddr_can *)addr_ret; |
| 1605 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1606 | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
| 1607 | &interfaceName)) |
| 1608 | return 0; |
| 1609 | |
| 1610 | len = PyBytes_GET_SIZE(interfaceName); |
| 1611 | |
| 1612 | if (len == 0) { |
| 1613 | ifr.ifr_ifindex = 0; |
| 1614 | } else if (len < sizeof(ifr.ifr_name)) { |
Christian Heimes | 15b6885 | 2012-09-10 01:25:50 +0200 | [diff] [blame] | 1615 | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
| 1616 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1617 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1618 | s->errorhandler(); |
| 1619 | Py_DECREF(interfaceName); |
| 1620 | return 0; |
| 1621 | } |
| 1622 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1623 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1624 | "AF_CAN interface name too long"); |
| 1625 | Py_DECREF(interfaceName); |
| 1626 | return 0; |
| 1627 | } |
| 1628 | |
| 1629 | addr->can_family = AF_CAN; |
| 1630 | addr->can_ifindex = ifr.ifr_ifindex; |
| 1631 | |
| 1632 | *len_ret = sizeof(*addr); |
| 1633 | Py_DECREF(interfaceName); |
| 1634 | return 1; |
| 1635 | } |
| 1636 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1637 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1638 | "getsockaddrarg: unsupported CAN protocol"); |
| 1639 | return 0; |
| 1640 | } |
| 1641 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1642 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1643 | #ifdef PF_SYSTEM |
| 1644 | case PF_SYSTEM: |
| 1645 | switch (s->sock_proto) { |
| 1646 | #ifdef SYSPROTO_CONTROL |
| 1647 | case SYSPROTO_CONTROL: |
| 1648 | { |
| 1649 | struct sockaddr_ctl *addr; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1650 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1651 | addr = (struct sockaddr_ctl *)addr_ret; |
| 1652 | addr->sc_family = AF_SYSTEM; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1653 | addr->ss_sysaddr = AF_SYS_CONTROL; |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1654 | |
| 1655 | if (PyUnicode_Check(args)) { |
| 1656 | struct ctl_info info; |
| 1657 | PyObject *ctl_name; |
| 1658 | |
| 1659 | if (!PyArg_Parse(args, "O&", |
| 1660 | PyUnicode_FSConverter, &ctl_name)) { |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | if (PyBytes_GET_SIZE(ctl_name) > sizeof(info.ctl_name)) { |
| 1665 | PyErr_SetString(PyExc_ValueError, |
| 1666 | "provided string is too long"); |
| 1667 | Py_DECREF(ctl_name); |
| 1668 | return 0; |
| 1669 | } |
| 1670 | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
| 1671 | sizeof(info.ctl_name)); |
| 1672 | Py_DECREF(ctl_name); |
| 1673 | |
| 1674 | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
| 1675 | PyErr_SetString(PyExc_OSError, |
| 1676 | "cannot find kernel control with provided name"); |
| 1677 | return 0; |
| 1678 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1679 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1680 | addr->sc_id = info.ctl_id; |
| 1681 | addr->sc_unit = 0; |
| 1682 | } else if (!PyArg_ParseTuple(args, "II", |
| 1683 | &(addr->sc_id), &(addr->sc_unit))) { |
| 1684 | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
| 1685 | "expected str or tuple of two ints"); |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1686 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1689 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1690 | *len_ret = sizeof(*addr); |
| 1691 | return 1; |
| 1692 | } |
| 1693 | #endif |
| 1694 | default: |
| 1695 | PyErr_SetString(PyExc_OSError, |
| 1696 | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
| 1697 | return 0; |
| 1698 | } |
| 1699 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1704 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1710 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1711 | /* 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] | 1712 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1713 | through len_ret. */ |
| 1714 | |
| 1715 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1716 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1719 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1720 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | case AF_UNIX: |
| 1722 | { |
| 1723 | *len_ret = sizeof (struct sockaddr_un); |
| 1724 | return 1; |
| 1725 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1726 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1727 | #if defined(AF_NETLINK) |
| 1728 | case AF_NETLINK: |
| 1729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | *len_ret = sizeof (struct sockaddr_nl); |
| 1731 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1732 | } |
| 1733 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1734 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1735 | #ifdef AF_RDS |
| 1736 | case AF_RDS: |
| 1737 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1738 | #endif |
| 1739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | case AF_INET: |
| 1741 | { |
| 1742 | *len_ret = sizeof (struct sockaddr_in); |
| 1743 | return 1; |
| 1744 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1745 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1746 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | case AF_INET6: |
| 1748 | { |
| 1749 | *len_ret = sizeof (struct sockaddr_in6); |
| 1750 | return 1; |
| 1751 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1752 | #endif |
| 1753 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1754 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | case AF_BLUETOOTH: |
| 1756 | { |
| 1757 | switch(s->sock_proto) |
| 1758 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | case BTPROTO_L2CAP: |
| 1761 | *len_ret = sizeof (struct sockaddr_l2); |
| 1762 | return 1; |
| 1763 | case BTPROTO_RFCOMM: |
| 1764 | *len_ret = sizeof (struct sockaddr_rc); |
| 1765 | return 1; |
| 1766 | case BTPROTO_HCI: |
| 1767 | *len_ret = sizeof (struct sockaddr_hci); |
| 1768 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1769 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | case BTPROTO_SCO: |
| 1771 | *len_ret = sizeof (struct sockaddr_sco); |
| 1772 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1773 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1775 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | "unknown BT protocol"); |
| 1777 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | } |
| 1780 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1781 | #endif |
| 1782 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1783 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | case AF_PACKET: |
| 1785 | { |
| 1786 | *len_ret = sizeof (struct sockaddr_ll); |
| 1787 | return 1; |
| 1788 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1789 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1790 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1791 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | case AF_TIPC: |
| 1793 | { |
| 1794 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1795 | return 1; |
| 1796 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1797 | #endif |
| 1798 | |
Charles-François Natali | b4062e8 | 2014-02-08 22:54:11 +0100 | [diff] [blame] | 1799 | #ifdef HAVE_LINUX_CAN_H |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1800 | case AF_CAN: |
| 1801 | { |
| 1802 | *len_ret = sizeof (struct sockaddr_can); |
| 1803 | return 1; |
| 1804 | } |
| 1805 | #endif |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 1806 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1807 | #ifdef PF_SYSTEM |
| 1808 | case PF_SYSTEM: |
| 1809 | switch(s->sock_proto) { |
| 1810 | #ifdef SYSPROTO_CONTROL |
| 1811 | case SYSPROTO_CONTROL: |
| 1812 | *len_ret = sizeof (struct sockaddr_ctl); |
| 1813 | return 1; |
| 1814 | #endif |
| 1815 | default: |
| 1816 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
| 1817 | "unknown PF_SYSTEM protocol"); |
| 1818 | return 0; |
| 1819 | } |
| 1820 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1825 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1832 | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
| 1833 | Currently, these methods are only compiled if the RFC 2292/3542 |
| 1834 | CMSG_LEN() macro is available. Older systems seem to have used |
| 1835 | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
| 1836 | it may be possible to define CMSG_LEN() that way if it's not |
| 1837 | provided. Some architectures might need extra padding after the |
| 1838 | cmsghdr, however, and CMSG_LEN() would have to take account of |
| 1839 | this. */ |
| 1840 | #ifdef CMSG_LEN |
| 1841 | /* If length is in range, set *result to CMSG_LEN(length) and return |
| 1842 | true; otherwise, return false. */ |
| 1843 | static int |
| 1844 | get_CMSG_LEN(size_t length, size_t *result) |
| 1845 | { |
| 1846 | size_t tmp; |
| 1847 | |
| 1848 | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
| 1849 | return 0; |
| 1850 | tmp = CMSG_LEN(length); |
| 1851 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1852 | return 0; |
| 1853 | *result = tmp; |
| 1854 | return 1; |
| 1855 | } |
| 1856 | |
| 1857 | #ifdef CMSG_SPACE |
| 1858 | /* If length is in range, set *result to CMSG_SPACE(length) and return |
| 1859 | true; otherwise, return false. */ |
| 1860 | static int |
| 1861 | get_CMSG_SPACE(size_t length, size_t *result) |
| 1862 | { |
| 1863 | size_t tmp; |
| 1864 | |
| 1865 | /* Use CMSG_SPACE(1) here in order to take account of the padding |
| 1866 | necessary before *and* after the data. */ |
| 1867 | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
| 1868 | return 0; |
| 1869 | tmp = CMSG_SPACE(length); |
| 1870 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1871 | return 0; |
| 1872 | *result = tmp; |
| 1873 | return 1; |
| 1874 | } |
| 1875 | #endif |
| 1876 | |
| 1877 | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
| 1878 | pointer in msg->msg_control with at least "space" bytes after it, |
| 1879 | and its cmsg_len member inside the buffer. */ |
| 1880 | static int |
| 1881 | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
| 1882 | { |
| 1883 | size_t cmsg_offset; |
| 1884 | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
| 1885 | sizeof(cmsgh->cmsg_len)); |
| 1886 | |
Charles-François Natali | 466517d | 2011-08-28 18:23:43 +0200 | [diff] [blame] | 1887 | /* Note that POSIX allows msg_controllen to be of signed type. */ |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1888 | if (cmsgh == NULL || msg->msg_control == NULL) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1889 | return 0; |
Brett Cannon | b05cbe6 | 2014-01-07 17:01:01 -0500 | [diff] [blame] | 1890 | /* Note that POSIX allows msg_controllen to be of a signed type. This is |
| 1891 | annoying under OS X as it's unsigned there and so it triggers a |
| 1892 | tautological comparison warning under Clang when compared against 0. |
| 1893 | Since the check is valid on other platforms, silence the warning under |
| 1894 | Clang. */ |
| 1895 | #ifdef __clang__ |
| 1896 | #pragma clang diagnostic push |
| 1897 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 1898 | #endif |
| 1899 | if (msg->msg_controllen < 0) |
| 1900 | return 0; |
| 1901 | #ifdef __clang__ |
| 1902 | #pragma clang diagnostic pop |
| 1903 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1904 | if (space < cmsg_len_end) |
| 1905 | space = cmsg_len_end; |
| 1906 | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
| 1907 | return (cmsg_offset <= (size_t)-1 - space && |
| 1908 | cmsg_offset + space <= msg->msg_controllen); |
| 1909 | } |
| 1910 | |
| 1911 | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
| 1912 | *space to number of bytes following it in the buffer and return |
| 1913 | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
| 1914 | msg->msg_controllen are valid. */ |
| 1915 | static int |
| 1916 | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
| 1917 | { |
| 1918 | size_t data_offset; |
| 1919 | char *data_ptr; |
| 1920 | |
| 1921 | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
| 1922 | return 0; |
| 1923 | data_offset = data_ptr - (char *)msg->msg_control; |
| 1924 | if (data_offset > msg->msg_controllen) |
| 1925 | return 0; |
| 1926 | *space = msg->msg_controllen - data_offset; |
| 1927 | return 1; |
| 1928 | } |
| 1929 | |
| 1930 | /* If cmsgh is invalid or not contained in the buffer pointed to by |
| 1931 | msg->msg_control, return -1. If cmsgh is valid and its associated |
| 1932 | data is entirely contained in the buffer, set *data_len to the |
| 1933 | length of the associated data and return 0. If only part of the |
| 1934 | associated data is contained in the buffer but cmsgh is otherwise |
| 1935 | valid, set *data_len to the length contained in the buffer and |
| 1936 | return 1. */ |
| 1937 | static int |
| 1938 | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
| 1939 | { |
| 1940 | size_t space, cmsg_data_len; |
| 1941 | |
| 1942 | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
| 1943 | cmsgh->cmsg_len < CMSG_LEN(0)) |
| 1944 | return -1; |
| 1945 | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
| 1946 | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
| 1947 | return -1; |
| 1948 | if (space >= cmsg_data_len) { |
| 1949 | *data_len = cmsg_data_len; |
| 1950 | return 0; |
| 1951 | } |
| 1952 | *data_len = space; |
| 1953 | return 1; |
| 1954 | } |
| 1955 | #endif /* CMSG_LEN */ |
| 1956 | |
| 1957 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1958 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1959 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1960 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1961 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1962 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | sock_addr_t addrbuf; |
| 1964 | SOCKET_T newfd = INVALID_SOCKET; |
| 1965 | socklen_t addrlen; |
| 1966 | PyObject *sock = NULL; |
| 1967 | PyObject *addr = NULL; |
| 1968 | PyObject *res = NULL; |
| 1969 | int timeout; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1970 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 1971 | /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ |
| 1972 | static int accept4_works = -1; |
| 1973 | #endif |
| 1974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | if (!getsockaddrlen(s, &addrlen)) |
| 1976 | return NULL; |
| 1977 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | if (!IS_SELECTABLE(s)) |
| 1980 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1982 | BEGIN_SELECT_LOOP(s) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1985 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 1986 | if (!timeout) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1987 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 1988 | if (accept4_works != 0) { |
| 1989 | newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen, |
| 1990 | SOCK_CLOEXEC); |
| 1991 | if (newfd == INVALID_SOCKET && accept4_works == -1) { |
| 1992 | /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ |
| 1993 | accept4_works = (errno != ENOSYS); |
| 1994 | } |
| 1995 | } |
| 1996 | if (accept4_works == 0) |
| 1997 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1998 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2000 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2001 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | if (timeout == 1) { |
| 2005 | PyErr_SetString(socket_timeout, "timed out"); |
| 2006 | return NULL; |
| 2007 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2008 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | if (newfd == INVALID_SOCKET) |
| 2011 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2012 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2013 | #ifdef MS_WINDOWS |
| 2014 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 2015 | PyErr_SetFromWindowsErr(0); |
| 2016 | SOCKETCLOSE(newfd); |
| 2017 | goto finally; |
| 2018 | } |
| 2019 | #else |
| 2020 | |
| 2021 | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
| 2022 | if (!accept4_works) |
| 2023 | #endif |
| 2024 | { |
| 2025 | if (_Py_set_inheritable(newfd, 0, NULL) < 0) { |
| 2026 | SOCKETCLOSE(newfd); |
| 2027 | goto finally; |
| 2028 | } |
| 2029 | } |
| 2030 | #endif |
| 2031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | sock = PyLong_FromSocket_t(newfd); |
| 2033 | if (sock == NULL) { |
| 2034 | SOCKETCLOSE(newfd); |
| 2035 | goto finally; |
| 2036 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2038 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2039 | addrlen, s->sock_proto); |
| 2040 | if (addr == NULL) |
| 2041 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2043 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2044 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2045 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | Py_XDECREF(sock); |
| 2047 | Py_XDECREF(addr); |
| 2048 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2051 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2052 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2053 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2054 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 2055 | representing the connection, and the address of the client.\n\ |
| 2056 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2058 | /* s.setblocking(flag) method. Argument: |
| 2059 | False -- non-blocking mode; same as settimeout(0) |
| 2060 | True -- blocking mode; same as settimeout(None) |
| 2061 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2062 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2063 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2064 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2065 | { |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 2066 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | block = PyLong_AsLong(arg); |
| 2069 | if (block == -1 && PyErr_Occurred()) |
| 2070 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2072 | s->sock_timeout = block ? -1.0 : 0.0; |
| 2073 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | Py_INCREF(Py_None); |
| 2076 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2077 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2079 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2080 | "setblocking(flag)\n\ |
| 2081 | \n\ |
| 2082 | 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] | 2083 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2084 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2085 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2086 | /* s.settimeout(timeout) method. Argument: |
| 2087 | None -- no timeout, blocking mode; same as setblocking(True) |
| 2088 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 2089 | > 0 -- timeout mode; operations time out after timeout seconds |
| 2090 | < 0 -- illegal; raises an exception |
| 2091 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2092 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2093 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | if (arg == Py_None) |
| 2098 | timeout = -1.0; |
| 2099 | else { |
| 2100 | timeout = PyFloat_AsDouble(arg); |
| 2101 | if (timeout < 0.0) { |
| 2102 | if (!PyErr_Occurred()) |
| 2103 | PyErr_SetString(PyExc_ValueError, |
| 2104 | "Timeout value out of range"); |
| 2105 | return NULL; |
| 2106 | } |
| 2107 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | s->sock_timeout = timeout; |
| 2110 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | Py_INCREF(Py_None); |
| 2113 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2116 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2117 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2118 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2119 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 2120 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 2121 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2122 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2123 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2124 | /* s.gettimeout() method. |
| 2125 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2126 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2127 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | if (s->sock_timeout < 0.0) { |
| 2130 | Py_INCREF(Py_None); |
| 2131 | return Py_None; |
| 2132 | } |
| 2133 | else |
| 2134 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2137 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2138 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2139 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 2140 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2141 | 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] | 2142 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2143 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2144 | /* s.setsockopt() method. |
| 2145 | With an integer third argument, sets an integer option. |
| 2146 | With a string third argument, sets an option from a buffer; |
| 2147 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2148 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2149 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2150 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | int level; |
| 2153 | int optname; |
| 2154 | int res; |
| 2155 | char *buf; |
| 2156 | int buflen; |
| 2157 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 2160 | &level, &optname, &flag)) { |
| 2161 | buf = (char *) &flag; |
| 2162 | buflen = sizeof flag; |
| 2163 | } |
| 2164 | else { |
| 2165 | PyErr_Clear(); |
| 2166 | if (!PyArg_ParseTuple(args, "iiy#:setsockopt", |
| 2167 | &level, &optname, &buf, &buflen)) |
| 2168 | return NULL; |
| 2169 | } |
| 2170 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 2171 | if (res < 0) |
| 2172 | return s->errorhandler(); |
| 2173 | Py_INCREF(Py_None); |
| 2174 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2177 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2178 | "setsockopt(level, option, value)\n\ |
| 2179 | \n\ |
| 2180 | 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] | 2181 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2182 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2183 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2184 | /* s.getsockopt() method. |
| 2185 | With two arguments, retrieves an integer option. |
| 2186 | With a third integer argument, retrieves a string buffer of that size; |
| 2187 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2188 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2189 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2190 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2191 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | int level; |
| 2193 | int optname; |
| 2194 | int res; |
| 2195 | PyObject *buf; |
| 2196 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 2199 | &level, &optname, &buflen)) |
| 2200 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | if (buflen == 0) { |
| 2203 | int flag = 0; |
| 2204 | socklen_t flagsize = sizeof flag; |
| 2205 | res = getsockopt(s->sock_fd, level, optname, |
| 2206 | (void *)&flag, &flagsize); |
| 2207 | if (res < 0) |
| 2208 | return s->errorhandler(); |
| 2209 | return PyLong_FromLong(flag); |
| 2210 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | if (buflen <= 0 || buflen > 1024) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2212 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | "getsockopt buflen out of range"); |
| 2214 | return NULL; |
| 2215 | } |
| 2216 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 2217 | if (buf == NULL) |
| 2218 | return NULL; |
| 2219 | res = getsockopt(s->sock_fd, level, optname, |
| 2220 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 2221 | if (res < 0) { |
| 2222 | Py_DECREF(buf); |
| 2223 | return s->errorhandler(); |
| 2224 | } |
| 2225 | _PyBytes_Resize(&buf, buflen); |
| 2226 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2229 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2230 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 2231 | \n\ |
| 2232 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2233 | 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] | 2234 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2235 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2236 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2237 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2238 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2239 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2240 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2241 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | sock_addr_t addrbuf; |
| 2243 | int addrlen; |
| 2244 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2247 | return NULL; |
| 2248 | Py_BEGIN_ALLOW_THREADS |
| 2249 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2250 | Py_END_ALLOW_THREADS |
| 2251 | if (res < 0) |
| 2252 | return s->errorhandler(); |
| 2253 | Py_INCREF(Py_None); |
| 2254 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2257 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2258 | "bind(address)\n\ |
| 2259 | \n\ |
| 2260 | 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] | 2261 | 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] | 2262 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2263 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2264 | |
| 2265 | /* s.close() method. |
| 2266 | Set the file descriptor to -1 so operations tried subsequently |
| 2267 | will surely fail. */ |
| 2268 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2269 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2270 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | if ((fd = s->sock_fd) != -1) { |
| 2275 | s->sock_fd = -1; |
| 2276 | Py_BEGIN_ALLOW_THREADS |
| 2277 | (void) SOCKETCLOSE(fd); |
| 2278 | Py_END_ALLOW_THREADS |
| 2279 | } |
| 2280 | Py_INCREF(Py_None); |
| 2281 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2284 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2285 | "close()\n\ |
| 2286 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2287 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2288 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2289 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2290 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2291 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2292 | SOCKET_T fd = s->sock_fd; |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2293 | s->sock_fd = -1; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2294 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2297 | PyDoc_STRVAR(detach_doc, |
| 2298 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2299 | \n\ |
Guido van Rossum | 638aebd | 2013-12-04 11:50:09 -0800 | [diff] [blame] | 2300 | Close the socket object without closing the underlying file descriptor.\n\ |
| 2301 | 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] | 2302 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2304 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2305 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | timeout = 0; |
| 2311 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2312 | |
| 2313 | #ifdef MS_WINDOWS |
| 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | if (s->sock_timeout > 0.0) { |
| 2316 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2317 | IS_SELECTABLE(s)) { |
| 2318 | /* This is a mess. Best solution: trust select */ |
| 2319 | fd_set fds; |
| 2320 | fd_set fds_exc; |
| 2321 | struct timeval tv; |
| 2322 | tv.tv_sec = (int)s->sock_timeout; |
| 2323 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2324 | FD_ZERO(&fds); |
| 2325 | FD_SET(s->sock_fd, &fds); |
| 2326 | FD_ZERO(&fds_exc); |
| 2327 | FD_SET(s->sock_fd, &fds_exc); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2328 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 2329 | NULL, &fds, &fds_exc, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | if (res == 0) { |
| 2331 | res = WSAEWOULDBLOCK; |
| 2332 | timeout = 1; |
| 2333 | } else if (res > 0) { |
| 2334 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2335 | /* The socket is in the writable set - this |
| 2336 | means connected */ |
| 2337 | res = 0; |
| 2338 | else { |
| 2339 | /* As per MS docs, we need to call getsockopt() |
| 2340 | to get the underlying error */ |
| 2341 | int res_size = sizeof res; |
| 2342 | /* It must be in the exception set */ |
| 2343 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2344 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2345 | (char *)&res, &res_size)) |
| 2346 | /* getsockopt also clears WSAGetLastError, |
| 2347 | so reset it back. */ |
| 2348 | WSASetLastError(res); |
| 2349 | else |
| 2350 | res = WSAGetLastError(); |
| 2351 | } |
| 2352 | } |
| 2353 | /* else if (res < 0) an error occurred */ |
| 2354 | } |
| 2355 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | if (res < 0) |
| 2358 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2359 | |
| 2360 | #else |
| 2361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | if (s->sock_timeout > 0.0) { |
| 2363 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2364 | timeout = internal_select(s, 1); |
| 2365 | if (timeout == 0) { |
| 2366 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2367 | use getsockopt(SO_ERROR) to get the real |
| 2368 | error. */ |
| 2369 | socklen_t res_size = sizeof res; |
| 2370 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2371 | SO_ERROR, &res, &res_size); |
| 2372 | if (res == EISCONN) |
| 2373 | res = 0; |
| 2374 | errno = res; |
| 2375 | } |
| 2376 | else if (timeout == -1) { |
| 2377 | res = errno; /* had error */ |
| 2378 | } |
| 2379 | else |
| 2380 | res = EWOULDBLOCK; /* timed out */ |
| 2381 | } |
| 2382 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2384 | if (res < 0) |
| 2385 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2386 | |
| 2387 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | *timeoutp = timeout; |
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 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2391 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2392 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2393 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2394 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2395 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2396 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | sock_addr_t addrbuf; |
| 2399 | int addrlen; |
| 2400 | int res; |
| 2401 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2404 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | Py_BEGIN_ALLOW_THREADS |
| 2407 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2408 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | if (timeout == 1) { |
| 2411 | PyErr_SetString(socket_timeout, "timed out"); |
| 2412 | return NULL; |
| 2413 | } |
| 2414 | if (res != 0) |
| 2415 | return s->errorhandler(); |
| 2416 | Py_INCREF(Py_None); |
| 2417 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2420 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2421 | "connect(address)\n\ |
| 2422 | \n\ |
| 2423 | 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] | 2424 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2425 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2426 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2427 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2428 | |
| 2429 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2430 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2432 | sock_addr_t addrbuf; |
| 2433 | int addrlen; |
| 2434 | int res; |
| 2435 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2438 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2440 | Py_BEGIN_ALLOW_THREADS |
| 2441 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2442 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2445 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2446 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | if (res == EINTR && PyErr_CheckSignals()) |
| 2448 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2449 | #endif |
| 2450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2454 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2455 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2456 | \n\ |
| 2457 | 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] | 2458 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2459 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2460 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2461 | /* s.fileno() method */ |
| 2462 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2463 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2464 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2469 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2470 | "fileno() -> integer\n\ |
| 2471 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2472 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2473 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2474 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2475 | /* s.getsockname() method */ |
| 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_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | sock_addr_t addrbuf; |
| 2481 | int res; |
| 2482 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 | if (!getsockaddrlen(s, &addrlen)) |
| 2485 | return NULL; |
| 2486 | memset(&addrbuf, 0, addrlen); |
| 2487 | Py_BEGIN_ALLOW_THREADS |
| 2488 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2489 | Py_END_ALLOW_THREADS |
| 2490 | if (res < 0) |
| 2491 | return s->errorhandler(); |
| 2492 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2493 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2496 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2497 | "getsockname() -> address info\n\ |
| 2498 | \n\ |
| 2499 | 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] | 2500 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2501 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2503 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2504 | /* s.getpeername() method */ |
| 2505 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2506 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2507 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | sock_addr_t addrbuf; |
| 2510 | int res; |
| 2511 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2513 | if (!getsockaddrlen(s, &addrlen)) |
| 2514 | return NULL; |
| 2515 | memset(&addrbuf, 0, addrlen); |
| 2516 | Py_BEGIN_ALLOW_THREADS |
| 2517 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2518 | Py_END_ALLOW_THREADS |
| 2519 | if (res < 0) |
| 2520 | return s->errorhandler(); |
| 2521 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2522 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2523 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2524 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2525 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2526 | "getpeername() -> address info\n\ |
| 2527 | \n\ |
| 2528 | 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] | 2529 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2530 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2531 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2532 | |
| 2533 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2534 | /* s.listen(n) method */ |
| 2535 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2536 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2537 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | int backlog; |
| 2540 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2541 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 2542 | backlog = _PyLong_AsInt(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | if (backlog == -1 && PyErr_Occurred()) |
| 2544 | return NULL; |
| 2545 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2546 | /* To avoid problems on systems that don't allow a negative backlog |
| 2547 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2548 | if (backlog < 0) |
| 2549 | backlog = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | res = listen(s->sock_fd, backlog); |
| 2551 | Py_END_ALLOW_THREADS |
| 2552 | if (res < 0) |
| 2553 | return s->errorhandler(); |
| 2554 | Py_INCREF(Py_None); |
| 2555 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2558 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2559 | "listen(backlog)\n\ |
| 2560 | \n\ |
| 2561 | Enable a server to accept connections. The backlog argument must be at\n\ |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2562 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
| 2563 | unaccepted connections that the system will allow before refusing new\n\ |
| 2564 | connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2565 | |
| 2566 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2567 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2568 | * 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] | 2569 | * 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] | 2570 | * 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] | 2571 | * 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] | 2572 | * also possible that we return a number of bytes smaller than the request |
| 2573 | * bytes. |
| 2574 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2575 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2576 | static Py_ssize_t |
| 2577 | 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] | 2578 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2579 | Py_ssize_t outlen = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | int timeout; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 | if (!IS_SELECTABLE(s)) { |
| 2583 | select_error(); |
| 2584 | return -1; |
| 2585 | } |
| 2586 | if (len == 0) { |
| 2587 | /* If 0 bytes were requested, do nothing. */ |
| 2588 | return 0; |
| 2589 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2590 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2591 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2593 | timeout = internal_select_ex(s, 0, interval); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2594 | if (!timeout) { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 2595 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2596 | if (len > INT_MAX) |
| 2597 | len = INT_MAX; |
| 2598 | outlen = recv(s->sock_fd, cbuf, (int)len, flags); |
| 2599 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | outlen = recv(s->sock_fd, cbuf, len, flags); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2601 | #endif |
| 2602 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2603 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | if (timeout == 1) { |
| 2606 | PyErr_SetString(socket_timeout, "timed out"); |
| 2607 | return -1; |
| 2608 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2609 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | if (outlen < 0) { |
| 2611 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2612 | NULL, so ignore its return value */ |
| 2613 | s->errorhandler(); |
| 2614 | return -1; |
| 2615 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2619 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2620 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2621 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2622 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2623 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2624 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2625 | Py_ssize_t recvlen, outlen; |
| 2626 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2628 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2629 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2630 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | if (recvlen < 0) { |
| 2633 | PyErr_SetString(PyExc_ValueError, |
| 2634 | "negative buffersize in recv"); |
| 2635 | return NULL; |
| 2636 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | /* Allocate a new string. */ |
| 2639 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2640 | if (buf == NULL) |
| 2641 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | /* Call the guts */ |
| 2644 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2645 | if (outlen < 0) { |
| 2646 | /* An error occurred, release the string and return an |
| 2647 | error. */ |
| 2648 | Py_DECREF(buf); |
| 2649 | return NULL; |
| 2650 | } |
| 2651 | if (outlen != recvlen) { |
| 2652 | /* We did not read as many bytes as we anticipated, resize the |
| 2653 | string if possible and be successful. */ |
| 2654 | _PyBytes_Resize(&buf, outlen); |
| 2655 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2660 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2661 | "recv(buffersize[, flags]) -> data\n\ |
| 2662 | \n\ |
| 2663 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2664 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2665 | 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] | 2666 | 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] | 2667 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2668 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2669 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2670 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2671 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2672 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2675 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2676 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | Py_buffer pbuf; |
| 2678 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2679 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2682 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | &pbuf, &recvlen, &flags)) |
| 2684 | return NULL; |
| 2685 | buf = pbuf.buf; |
| 2686 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2688 | if (recvlen < 0) { |
| 2689 | PyBuffer_Release(&pbuf); |
| 2690 | PyErr_SetString(PyExc_ValueError, |
| 2691 | "negative buffersize in recv_into"); |
| 2692 | return NULL; |
| 2693 | } |
| 2694 | if (recvlen == 0) { |
| 2695 | /* If nbytes was not specified, use the buffer's length */ |
| 2696 | recvlen = buflen; |
| 2697 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | /* Check if the buffer is large enough */ |
| 2700 | if (buflen < recvlen) { |
| 2701 | PyBuffer_Release(&pbuf); |
| 2702 | PyErr_SetString(PyExc_ValueError, |
| 2703 | "buffer too small for requested bytes"); |
| 2704 | return NULL; |
| 2705 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | /* Call the guts */ |
| 2708 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2709 | if (readlen < 0) { |
| 2710 | /* Return an error. */ |
| 2711 | PyBuffer_Release(&pbuf); |
| 2712 | return NULL; |
| 2713 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | PyBuffer_Release(&pbuf); |
| 2716 | /* Return the number of bytes read. Note that we do not do anything |
| 2717 | special here in the case that readlen < recvlen. */ |
| 2718 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2721 | PyDoc_STRVAR(recv_into_doc, |
| 2722 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2723 | \n\ |
| 2724 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2725 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2726 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2727 | \n\ |
| 2728 | See recv() for documentation about the flags."); |
| 2729 | |
| 2730 | |
| 2731 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2732 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2733 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2734 | * 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] | 2735 | * 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] | 2736 | * that it is also possible that we return a number of bytes smaller than the |
| 2737 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2738 | * |
| 2739 | * 'addr' is a return value for the address object. Note that you must decref |
| 2740 | * it yourself. |
| 2741 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2742 | static Py_ssize_t |
| 2743 | 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] | 2744 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | sock_addr_t addrbuf; |
| 2747 | int timeout; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2748 | Py_ssize_t n = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2749 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2753 | if (!getsockaddrlen(s, &addrlen)) |
| 2754 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | if (!IS_SELECTABLE(s)) { |
| 2757 | select_error(); |
| 2758 | return -1; |
| 2759 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2760 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2761 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2762 | Py_BEGIN_ALLOW_THREADS |
| 2763 | memset(&addrbuf, 0, addrlen); |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2764 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | if (!timeout) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 2766 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 2767 | if (len > INT_MAX) |
| 2768 | len = INT_MAX; |
| 2769 | n = recvfrom(s->sock_fd, cbuf, (int)len, flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2771 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2773 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2774 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | } |
| 2776 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | if (timeout == 1) { |
| 2779 | PyErr_SetString(socket_timeout, "timed out"); |
| 2780 | return -1; |
| 2781 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2782 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | if (n < 0) { |
| 2784 | s->errorhandler(); |
| 2785 | return -1; |
| 2786 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2788 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2789 | addrlen, s->sock_proto))) |
| 2790 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2796 | |
| 2797 | static PyObject * |
| 2798 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | PyObject *buf = NULL; |
| 2801 | PyObject *addr = NULL; |
| 2802 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2803 | int flags = 0; |
| 2804 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2805 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2806 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | if (recvlen < 0) { |
| 2810 | PyErr_SetString(PyExc_ValueError, |
| 2811 | "negative buffersize in recvfrom"); |
| 2812 | return NULL; |
| 2813 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2816 | if (buf == NULL) |
| 2817 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2820 | recvlen, flags, &addr); |
| 2821 | if (outlen < 0) { |
| 2822 | goto finally; |
| 2823 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | if (outlen != recvlen) { |
| 2826 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2827 | string if possible and be successful. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | if (_PyBytes_Resize(&buf, outlen) < 0) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2829 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | goto finally; |
| 2831 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2834 | |
| 2835 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | Py_XDECREF(buf); |
| 2837 | Py_XDECREF(addr); |
| 2838 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2841 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2842 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2843 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2844 | 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] | 2845 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2846 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2847 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2848 | |
| 2849 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2850 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2853 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2854 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | Py_buffer pbuf; |
| 2856 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2857 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2861 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | kwlist, &pbuf, |
| 2863 | &recvlen, &flags)) |
| 2864 | return NULL; |
| 2865 | buf = pbuf.buf; |
| 2866 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | if (recvlen < 0) { |
| 2869 | PyBuffer_Release(&pbuf); |
| 2870 | PyErr_SetString(PyExc_ValueError, |
| 2871 | "negative buffersize in recvfrom_into"); |
| 2872 | return NULL; |
| 2873 | } |
| 2874 | if (recvlen == 0) { |
| 2875 | /* If nbytes was not specified, use the buffer's length */ |
| 2876 | recvlen = buflen; |
Benjamin Peterson | c6b37e2 | 2014-01-13 23:14:42 -0500 | [diff] [blame] | 2877 | } else if (recvlen > buflen) { |
| 2878 | PyBuffer_Release(&pbuf); |
| 2879 | PyErr_SetString(PyExc_ValueError, |
| 2880 | "nbytes is greater than the length of the buffer"); |
| 2881 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2885 | if (readlen < 0) { |
| 2886 | PyBuffer_Release(&pbuf); |
| 2887 | /* Return an error */ |
| 2888 | Py_XDECREF(addr); |
| 2889 | return NULL; |
| 2890 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | PyBuffer_Release(&pbuf); |
| 2893 | /* Return the number of bytes read and the address. Note that we do |
| 2894 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2895 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2898 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2899 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2900 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2901 | 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] | 2902 | |
| 2903 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2904 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 2905 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 2906 | #ifdef CMSG_LEN |
| 2907 | /* |
| 2908 | * Call recvmsg() with the supplied iovec structures, flags, and |
| 2909 | * ancillary data buffer size (controllen). Returns the tuple return |
| 2910 | * value for recvmsg() or recvmsg_into(), with the first item provided |
| 2911 | * by the supplied makeval() function. makeval() will be called with |
| 2912 | * the length read and makeval_data as arguments, and must return a |
| 2913 | * new reference (which will be decrefed if there is a subsequent |
| 2914 | * error). On error, closes any file descriptors received via |
| 2915 | * SCM_RIGHTS. |
| 2916 | */ |
| 2917 | static PyObject * |
| 2918 | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
| 2919 | int flags, Py_ssize_t controllen, |
| 2920 | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
| 2921 | { |
| 2922 | ssize_t bytes_received = -1; |
| 2923 | int timeout; |
| 2924 | sock_addr_t addrbuf; |
| 2925 | socklen_t addrbuflen; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 2926 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2927 | PyObject *cmsg_list = NULL, *retval = NULL; |
| 2928 | void *controlbuf = NULL; |
| 2929 | struct cmsghdr *cmsgh; |
| 2930 | size_t cmsgdatalen = 0; |
| 2931 | int cmsg_status; |
| 2932 | |
| 2933 | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
| 2934 | ignored" when the socket is connected (Linux fills them in |
| 2935 | anyway for AF_UNIX sockets at least). Normally msg_namelen |
| 2936 | seems to be set to 0 if there's no address, but try to |
| 2937 | initialize msg_name to something that won't be mistaken for a |
| 2938 | real address if that doesn't happen. */ |
| 2939 | if (!getsockaddrlen(s, &addrbuflen)) |
| 2940 | return NULL; |
| 2941 | memset(&addrbuf, 0, addrbuflen); |
| 2942 | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
| 2943 | |
| 2944 | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
| 2945 | PyErr_SetString(PyExc_ValueError, |
| 2946 | "invalid ancillary data buffer length"); |
| 2947 | return NULL; |
| 2948 | } |
| 2949 | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
| 2950 | return PyErr_NoMemory(); |
| 2951 | |
| 2952 | /* Make the system call. */ |
| 2953 | if (!IS_SELECTABLE(s)) { |
| 2954 | select_error(); |
| 2955 | goto finally; |
| 2956 | } |
| 2957 | |
| 2958 | BEGIN_SELECT_LOOP(s) |
| 2959 | Py_BEGIN_ALLOW_THREADS; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2960 | msg.msg_name = SAS2SA(&addrbuf); |
| 2961 | msg.msg_namelen = addrbuflen; |
| 2962 | msg.msg_iov = iov; |
| 2963 | msg.msg_iovlen = iovlen; |
| 2964 | msg.msg_control = controlbuf; |
| 2965 | msg.msg_controllen = controllen; |
| 2966 | timeout = internal_select_ex(s, 0, interval); |
| 2967 | if (!timeout) |
| 2968 | bytes_received = recvmsg(s->sock_fd, &msg, flags); |
| 2969 | Py_END_ALLOW_THREADS; |
| 2970 | if (timeout == 1) { |
| 2971 | PyErr_SetString(socket_timeout, "timed out"); |
| 2972 | goto finally; |
| 2973 | } |
| 2974 | END_SELECT_LOOP(s) |
| 2975 | |
| 2976 | if (bytes_received < 0) { |
| 2977 | s->errorhandler(); |
| 2978 | goto finally; |
| 2979 | } |
| 2980 | |
| 2981 | /* Make list of (level, type, data) tuples from control messages. */ |
| 2982 | if ((cmsg_list = PyList_New(0)) == NULL) |
| 2983 | goto err_closefds; |
| 2984 | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
| 2985 | implementations didn't do so. */ |
| 2986 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 2987 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 2988 | PyObject *bytes, *tuple; |
| 2989 | int tmp; |
| 2990 | |
| 2991 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 2992 | if (cmsg_status != 0) { |
| 2993 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 2994 | "received malformed or improperly-truncated " |
| 2995 | "ancillary data", 1) == -1) |
| 2996 | goto err_closefds; |
| 2997 | } |
| 2998 | if (cmsg_status < 0) |
| 2999 | break; |
| 3000 | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3001 | PyErr_SetString(PyExc_OSError, "control message too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3002 | goto err_closefds; |
| 3003 | } |
| 3004 | |
| 3005 | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
| 3006 | cmsgdatalen); |
| 3007 | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
| 3008 | (int)cmsgh->cmsg_type, bytes); |
| 3009 | if (tuple == NULL) |
| 3010 | goto err_closefds; |
| 3011 | tmp = PyList_Append(cmsg_list, tuple); |
| 3012 | Py_DECREF(tuple); |
| 3013 | if (tmp != 0) |
| 3014 | goto err_closefds; |
| 3015 | |
| 3016 | if (cmsg_status != 0) |
| 3017 | break; |
| 3018 | } |
| 3019 | |
| 3020 | retval = Py_BuildValue("NOiN", |
| 3021 | (*makeval)(bytes_received, makeval_data), |
| 3022 | cmsg_list, |
| 3023 | (int)msg.msg_flags, |
| 3024 | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 3025 | ((msg.msg_namelen > addrbuflen) ? |
| 3026 | addrbuflen : msg.msg_namelen), |
| 3027 | s->sock_proto)); |
| 3028 | if (retval == NULL) |
| 3029 | goto err_closefds; |
| 3030 | |
| 3031 | finally: |
| 3032 | Py_XDECREF(cmsg_list); |
| 3033 | PyMem_Free(controlbuf); |
| 3034 | return retval; |
| 3035 | |
| 3036 | err_closefds: |
| 3037 | #ifdef SCM_RIGHTS |
| 3038 | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
| 3039 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3040 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3041 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3042 | if (cmsg_status < 0) |
| 3043 | break; |
| 3044 | if (cmsgh->cmsg_level == SOL_SOCKET && |
| 3045 | cmsgh->cmsg_type == SCM_RIGHTS) { |
| 3046 | size_t numfds; |
| 3047 | int *fdp; |
| 3048 | |
| 3049 | numfds = cmsgdatalen / sizeof(int); |
| 3050 | fdp = (int *)CMSG_DATA(cmsgh); |
| 3051 | while (numfds-- > 0) |
| 3052 | close(*fdp++); |
| 3053 | } |
| 3054 | if (cmsg_status != 0) |
| 3055 | break; |
| 3056 | } |
| 3057 | #endif /* SCM_RIGHTS */ |
| 3058 | goto finally; |
| 3059 | } |
| 3060 | |
| 3061 | |
| 3062 | static PyObject * |
| 3063 | makeval_recvmsg(ssize_t received, void *data) |
| 3064 | { |
| 3065 | PyObject **buf = data; |
| 3066 | |
| 3067 | if (received < PyBytes_GET_SIZE(*buf)) |
| 3068 | _PyBytes_Resize(buf, received); |
| 3069 | Py_XINCREF(*buf); |
| 3070 | return *buf; |
| 3071 | } |
| 3072 | |
| 3073 | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
| 3074 | |
| 3075 | static PyObject * |
| 3076 | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
| 3077 | { |
| 3078 | Py_ssize_t bufsize, ancbufsize = 0; |
| 3079 | int flags = 0; |
| 3080 | struct iovec iov; |
| 3081 | PyObject *buf = NULL, *retval = NULL; |
| 3082 | |
| 3083 | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
| 3084 | return NULL; |
| 3085 | |
| 3086 | if (bufsize < 0) { |
| 3087 | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
| 3088 | return NULL; |
| 3089 | } |
| 3090 | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
| 3091 | return NULL; |
| 3092 | iov.iov_base = PyBytes_AS_STRING(buf); |
| 3093 | iov.iov_len = bufsize; |
| 3094 | |
| 3095 | /* Note that we're passing a pointer to *our pointer* to the bytes |
| 3096 | object here (&buf); makeval_recvmsg() may incref the object, or |
| 3097 | deallocate it and set our pointer to NULL. */ |
| 3098 | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
| 3099 | &makeval_recvmsg, &buf); |
| 3100 | Py_XDECREF(buf); |
| 3101 | return retval; |
| 3102 | } |
| 3103 | |
| 3104 | PyDoc_STRVAR(recvmsg_doc, |
| 3105 | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
| 3106 | \n\ |
| 3107 | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
| 3108 | socket. The ancbufsize argument sets the size in bytes of the\n\ |
| 3109 | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
| 3110 | meaning that no ancillary data will be received. Appropriate buffer\n\ |
| 3111 | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
| 3112 | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3113 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3114 | same meaning as for recv().\n\ |
| 3115 | \n\ |
| 3116 | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
| 3117 | The data item is a bytes object holding the non-ancillary data\n\ |
| 3118 | received. The ancdata item is a list of zero or more tuples\n\ |
| 3119 | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
| 3120 | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
| 3121 | specifying the protocol level and protocol-specific type respectively,\n\ |
| 3122 | and cmsg_data is a bytes object holding the associated data. The\n\ |
| 3123 | msg_flags item is the bitwise OR of various flags indicating\n\ |
| 3124 | conditions on the received message; see your system documentation for\n\ |
| 3125 | details. If the receiving socket is unconnected, address is the\n\ |
| 3126 | address of the sending socket, if available; otherwise, its value is\n\ |
| 3127 | unspecified.\n\ |
| 3128 | \n\ |
| 3129 | If recvmsg() raises an exception after the system call returns, it\n\ |
| 3130 | will first attempt to close any file descriptors received via the\n\ |
| 3131 | SCM_RIGHTS mechanism."); |
| 3132 | |
| 3133 | |
| 3134 | static PyObject * |
| 3135 | makeval_recvmsg_into(ssize_t received, void *data) |
| 3136 | { |
| 3137 | return PyLong_FromSsize_t(received); |
| 3138 | } |
| 3139 | |
| 3140 | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
| 3141 | |
| 3142 | static PyObject * |
| 3143 | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
| 3144 | { |
| 3145 | Py_ssize_t ancbufsize = 0; |
| 3146 | int flags = 0; |
| 3147 | struct iovec *iovs = NULL; |
| 3148 | Py_ssize_t i, nitems, nbufs = 0; |
| 3149 | Py_buffer *bufs = NULL; |
| 3150 | PyObject *buffers_arg, *fast, *retval = NULL; |
| 3151 | |
| 3152 | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
| 3153 | &buffers_arg, &ancbufsize, &flags)) |
| 3154 | return NULL; |
| 3155 | |
| 3156 | if ((fast = PySequence_Fast(buffers_arg, |
| 3157 | "recvmsg_into() argument 1 must be an " |
| 3158 | "iterable")) == NULL) |
| 3159 | return NULL; |
| 3160 | nitems = PySequence_Fast_GET_SIZE(fast); |
| 3161 | if (nitems > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3162 | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3163 | goto finally; |
| 3164 | } |
| 3165 | |
| 3166 | /* Fill in an iovec for each item, and save the Py_buffer |
| 3167 | structs to release afterwards. */ |
| 3168 | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
| 3169 | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
| 3170 | PyErr_NoMemory(); |
| 3171 | goto finally; |
| 3172 | } |
| 3173 | for (; nbufs < nitems; nbufs++) { |
| 3174 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
| 3175 | "w*;recvmsg_into() argument 1 must be an iterable " |
| 3176 | "of single-segment read-write buffers", |
| 3177 | &bufs[nbufs])) |
| 3178 | goto finally; |
| 3179 | iovs[nbufs].iov_base = bufs[nbufs].buf; |
| 3180 | iovs[nbufs].iov_len = bufs[nbufs].len; |
| 3181 | } |
| 3182 | |
| 3183 | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
| 3184 | &makeval_recvmsg_into, NULL); |
| 3185 | finally: |
| 3186 | for (i = 0; i < nbufs; i++) |
| 3187 | PyBuffer_Release(&bufs[i]); |
| 3188 | PyMem_Free(bufs); |
| 3189 | PyMem_Free(iovs); |
| 3190 | Py_DECREF(fast); |
| 3191 | return retval; |
| 3192 | } |
| 3193 | |
| 3194 | PyDoc_STRVAR(recvmsg_into_doc, |
| 3195 | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
| 3196 | \n\ |
| 3197 | Receive normal data and ancillary data from the socket, scattering the\n\ |
| 3198 | non-ancillary data into a series of buffers. The buffers argument\n\ |
| 3199 | must be an iterable of objects that export writable buffers\n\ |
| 3200 | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
| 3201 | of the non-ancillary data until it has all been written or there are\n\ |
| 3202 | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
| 3203 | the internal buffer used to receive the ancillary data; it defaults to\n\ |
| 3204 | 0, meaning that no ancillary data will be received. Appropriate\n\ |
| 3205 | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
| 3206 | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3207 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3208 | same meaning as for recv().\n\ |
| 3209 | \n\ |
| 3210 | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
| 3211 | The nbytes item is the total number of bytes of non-ancillary data\n\ |
| 3212 | written into the buffers. The ancdata item is a list of zero or more\n\ |
| 3213 | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
| 3214 | data (control messages) received: cmsg_level and cmsg_type are\n\ |
| 3215 | integers specifying the protocol level and protocol-specific type\n\ |
| 3216 | respectively, and cmsg_data is a bytes object holding the associated\n\ |
| 3217 | data. The msg_flags item is the bitwise OR of various flags\n\ |
| 3218 | indicating conditions on the received message; see your system\n\ |
| 3219 | documentation for details. If the receiving socket is unconnected,\n\ |
| 3220 | address is the address of the sending socket, if available; otherwise,\n\ |
| 3221 | its value is unspecified.\n\ |
| 3222 | \n\ |
| 3223 | If recvmsg_into() raises an exception after the system call returns,\n\ |
| 3224 | it will first attempt to close any file descriptors received via the\n\ |
| 3225 | SCM_RIGHTS mechanism."); |
| 3226 | #endif /* CMSG_LEN */ |
| 3227 | |
| 3228 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3229 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3230 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3231 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3232 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3235 | Py_ssize_t len, n = -1; |
| 3236 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 3240 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3242 | if (!IS_SELECTABLE(s)) { |
| 3243 | PyBuffer_Release(&pbuf); |
| 3244 | return select_error(); |
| 3245 | } |
| 3246 | buf = pbuf.buf; |
| 3247 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3248 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3249 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3251 | timeout = internal_select_ex(s, 1, interval); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3252 | if (!timeout) { |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3253 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3254 | if (len > INT_MAX) |
| 3255 | len = INT_MAX; |
| 3256 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3257 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3258 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3259 | #endif |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3260 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3262 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3263 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | PyErr_SetString(socket_timeout, "timed out"); |
| 3265 | return NULL; |
| 3266 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3267 | END_SELECT_LOOP(s) |
| 3268 | |
| 3269 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3270 | if (n < 0) |
| 3271 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3272 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3275 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3276 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3277 | \n\ |
| 3278 | 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] | 3279 | 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] | 3280 | 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] | 3281 | |
| 3282 | |
| 3283 | /* s.sendall(data [,flags]) method */ |
| 3284 | |
| 3285 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3286 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3289 | Py_ssize_t len, n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3290 | int flags = 0, timeout, saved_errno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 3294 | return NULL; |
| 3295 | buf = pbuf.buf; |
| 3296 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | if (!IS_SELECTABLE(s)) { |
| 3299 | PyBuffer_Release(&pbuf); |
| 3300 | return select_error(); |
| 3301 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | do { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3304 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | timeout = internal_select(s, 1); |
| 3306 | n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3307 | if (!timeout) { |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 3308 | #ifdef MS_WINDOWS |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3309 | if (len > INT_MAX) |
| 3310 | len = INT_MAX; |
| 3311 | n = send(s->sock_fd, buf, (int)len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3312 | #else |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3313 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3314 | #endif |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3315 | } |
| 3316 | Py_END_ALLOW_THREADS |
| 3317 | if (timeout == 1) { |
| 3318 | PyBuffer_Release(&pbuf); |
| 3319 | PyErr_SetString(socket_timeout, "timed out"); |
| 3320 | return NULL; |
| 3321 | } |
| 3322 | /* PyErr_CheckSignals() might change errno */ |
| 3323 | saved_errno = errno; |
| 3324 | /* We must run our signal handlers before looping again. |
| 3325 | send() can return a successful partial write when it is |
| 3326 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 3327 | if (PyErr_CheckSignals()) { |
| 3328 | PyBuffer_Release(&pbuf); |
| 3329 | return NULL; |
| 3330 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | if (n < 0) { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3332 | /* If interrupted, try again */ |
| 3333 | if (saved_errno == EINTR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | continue; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3335 | else |
| 3336 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | } |
| 3338 | buf += n; |
| 3339 | len -= n; |
| 3340 | } while (len > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | if (n < 0) |
| 3344 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3346 | Py_INCREF(Py_None); |
| 3347 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3350 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3351 | "sendall(data[, flags])\n\ |
| 3352 | \n\ |
| 3353 | Send a data string to the socket. For the optional flags\n\ |
| 3354 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 3355 | 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] | 3356 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3357 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3358 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3359 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3360 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3361 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3362 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3364 | Py_buffer pbuf; |
| 3365 | PyObject *addro; |
| 3366 | char *buf; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3367 | Py_ssize_t len, arglen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | sock_addr_t addrbuf; |
| 3369 | int addrlen, n = -1, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | flags = 0; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3372 | arglen = PyTuple_Size(args); |
| 3373 | switch (arglen) { |
| 3374 | case 2: |
| 3375 | PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); |
| 3376 | break; |
| 3377 | case 3: |
| 3378 | PyArg_ParseTuple(args, "y*iO:sendto", |
| 3379 | &pbuf, &flags, &addro); |
| 3380 | break; |
| 3381 | default: |
| 3382 | PyErr_Format(PyExc_TypeError, |
| 3383 | "sendto() takes 2 or 3 arguments (%d given)", |
| 3384 | arglen); |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 3385 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | } |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3387 | if (PyErr_Occurred()) |
| 3388 | return NULL; |
| 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | buf = pbuf.buf; |
| 3391 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | if (!IS_SELECTABLE(s)) { |
| 3394 | PyBuffer_Release(&pbuf); |
| 3395 | return select_error(); |
| 3396 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 3399 | PyBuffer_Release(&pbuf); |
| 3400 | return NULL; |
| 3401 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3402 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3403 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3405 | timeout = internal_select_ex(s, 1, interval); |
Victor Stinner | 9a644b2 | 2013-06-24 23:47:41 +0200 | [diff] [blame] | 3406 | if (!timeout) { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [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 = sendto(s->sock_fd, buf, (int)len, flags, |
| 3411 | SAS2SA(&addrbuf), addrlen); |
| 3412 | #else |
| 3413 | n = sendto(s->sock_fd, buf, len, flags, |
| 3414 | SAS2SA(&addrbuf), addrlen); |
| 3415 | #endif |
| 3416 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3417 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3420 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3421 | PyErr_SetString(socket_timeout, "timed out"); |
| 3422 | return NULL; |
| 3423 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3424 | END_SELECT_LOOP(s) |
| 3425 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | if (n < 0) |
| 3427 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3428 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3431 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3432 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3433 | \n\ |
| 3434 | 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] | 3435 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3436 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3437 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3438 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3439 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3440 | #ifdef CMSG_LEN |
| 3441 | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
| 3442 | |
| 3443 | static PyObject * |
| 3444 | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
| 3445 | { |
| 3446 | Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
| 3447 | Py_buffer *databufs = NULL; |
| 3448 | struct iovec *iovs = NULL; |
| 3449 | sock_addr_t addrbuf; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3450 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3451 | struct cmsginfo { |
| 3452 | int level; |
| 3453 | int type; |
| 3454 | Py_buffer data; |
| 3455 | } *cmsgs = NULL; |
| 3456 | void *controlbuf = NULL; |
| 3457 | size_t controllen, controllen_last; |
| 3458 | ssize_t bytes_sent = -1; |
| 3459 | int addrlen, timeout, flags = 0; |
| 3460 | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL, |
| 3461 | *cmsg_fast = NULL, *retval = NULL; |
| 3462 | |
| 3463 | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
| 3464 | &data_arg, &cmsg_arg, &flags, &addr_arg)) |
| 3465 | return NULL; |
| 3466 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3467 | /* Parse destination address. */ |
| 3468 | if (addr_arg != NULL && addr_arg != Py_None) { |
| 3469 | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
| 3470 | goto finally; |
| 3471 | msg.msg_name = &addrbuf; |
| 3472 | msg.msg_namelen = addrlen; |
| 3473 | } |
| 3474 | |
| 3475 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 3476 | structs to release afterwards. */ |
| 3477 | if ((data_fast = PySequence_Fast(data_arg, |
| 3478 | "sendmsg() argument 1 must be an " |
| 3479 | "iterable")) == NULL) |
| 3480 | goto finally; |
| 3481 | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
| 3482 | if (ndataparts > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3483 | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3484 | goto finally; |
| 3485 | } |
| 3486 | msg.msg_iovlen = ndataparts; |
| 3487 | if (ndataparts > 0 && |
| 3488 | ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL || |
| 3489 | (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) { |
| 3490 | PyErr_NoMemory(); |
| 3491 | goto finally; |
| 3492 | } |
| 3493 | for (; ndatabufs < ndataparts; ndatabufs++) { |
| 3494 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
| 3495 | "y*;sendmsg() argument 1 must be an iterable of " |
| 3496 | "buffer-compatible objects", |
| 3497 | &databufs[ndatabufs])) |
| 3498 | goto finally; |
| 3499 | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
| 3500 | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
| 3501 | } |
| 3502 | |
| 3503 | if (cmsg_arg == NULL) |
| 3504 | ncmsgs = 0; |
| 3505 | else { |
| 3506 | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
| 3507 | "sendmsg() argument 2 must be an " |
| 3508 | "iterable")) == NULL) |
| 3509 | goto finally; |
| 3510 | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
| 3511 | } |
| 3512 | |
| 3513 | #ifndef CMSG_SPACE |
| 3514 | if (ncmsgs > 1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3515 | PyErr_SetString(PyExc_OSError, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3516 | "sending multiple control messages is not supported " |
| 3517 | "on this system"); |
| 3518 | goto finally; |
| 3519 | } |
| 3520 | #endif |
| 3521 | /* Save level, type and Py_buffer for each control message, |
| 3522 | and calculate total size. */ |
| 3523 | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
| 3524 | PyErr_NoMemory(); |
| 3525 | goto finally; |
| 3526 | } |
| 3527 | controllen = controllen_last = 0; |
| 3528 | while (ncmsgbufs < ncmsgs) { |
| 3529 | size_t bufsize, space; |
| 3530 | |
| 3531 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
| 3532 | "(iiy*):[sendmsg() ancillary data items]", |
| 3533 | &cmsgs[ncmsgbufs].level, |
| 3534 | &cmsgs[ncmsgbufs].type, |
| 3535 | &cmsgs[ncmsgbufs].data)) |
| 3536 | goto finally; |
| 3537 | bufsize = cmsgs[ncmsgbufs++].data.len; |
| 3538 | |
| 3539 | #ifdef CMSG_SPACE |
| 3540 | if (!get_CMSG_SPACE(bufsize, &space)) { |
| 3541 | #else |
| 3542 | if (!get_CMSG_LEN(bufsize, &space)) { |
| 3543 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3544 | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3545 | goto finally; |
| 3546 | } |
| 3547 | controllen += space; |
| 3548 | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3549 | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3550 | goto finally; |
| 3551 | } |
| 3552 | controllen_last = controllen; |
| 3553 | } |
| 3554 | |
| 3555 | /* Construct ancillary data block from control message info. */ |
| 3556 | if (ncmsgbufs > 0) { |
| 3557 | struct cmsghdr *cmsgh = NULL; |
| 3558 | |
| 3559 | if ((msg.msg_control = controlbuf = |
| 3560 | PyMem_Malloc(controllen)) == NULL) { |
| 3561 | PyErr_NoMemory(); |
| 3562 | goto finally; |
| 3563 | } |
| 3564 | msg.msg_controllen = controllen; |
| 3565 | |
| 3566 | /* Need to zero out the buffer as a workaround for glibc's |
| 3567 | CMSG_NXTHDR() implementation. After getting the pointer to |
| 3568 | the next header, it checks its (uninitialized) cmsg_len |
| 3569 | member to see if the "message" fits in the buffer, and |
| 3570 | returns NULL if it doesn't. Zero-filling the buffer |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 3571 | ensures that this doesn't happen. */ |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3572 | memset(controlbuf, 0, controllen); |
| 3573 | |
| 3574 | for (i = 0; i < ncmsgbufs; i++) { |
| 3575 | size_t msg_len, data_len = cmsgs[i].data.len; |
| 3576 | int enough_space = 0; |
| 3577 | |
| 3578 | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
| 3579 | if (cmsgh == NULL) { |
| 3580 | PyErr_Format(PyExc_RuntimeError, |
| 3581 | "unexpected NULL result from %s()", |
| 3582 | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
| 3583 | goto finally; |
| 3584 | } |
| 3585 | if (!get_CMSG_LEN(data_len, &msg_len)) { |
| 3586 | PyErr_SetString(PyExc_RuntimeError, |
| 3587 | "item size out of range for CMSG_LEN()"); |
| 3588 | goto finally; |
| 3589 | } |
| 3590 | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
| 3591 | size_t space; |
| 3592 | |
| 3593 | cmsgh->cmsg_len = msg_len; |
| 3594 | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
| 3595 | enough_space = (space >= data_len); |
| 3596 | } |
| 3597 | if (!enough_space) { |
| 3598 | PyErr_SetString(PyExc_RuntimeError, |
| 3599 | "ancillary data does not fit in calculated " |
| 3600 | "space"); |
| 3601 | goto finally; |
| 3602 | } |
| 3603 | cmsgh->cmsg_level = cmsgs[i].level; |
| 3604 | cmsgh->cmsg_type = cmsgs[i].type; |
| 3605 | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
| 3606 | } |
| 3607 | } |
| 3608 | |
| 3609 | /* Make the system call. */ |
| 3610 | if (!IS_SELECTABLE(s)) { |
| 3611 | select_error(); |
| 3612 | goto finally; |
| 3613 | } |
| 3614 | |
| 3615 | BEGIN_SELECT_LOOP(s) |
| 3616 | Py_BEGIN_ALLOW_THREADS; |
| 3617 | timeout = internal_select_ex(s, 1, interval); |
| 3618 | if (!timeout) |
| 3619 | bytes_sent = sendmsg(s->sock_fd, &msg, flags); |
| 3620 | Py_END_ALLOW_THREADS; |
| 3621 | if (timeout == 1) { |
| 3622 | PyErr_SetString(socket_timeout, "timed out"); |
| 3623 | goto finally; |
| 3624 | } |
| 3625 | END_SELECT_LOOP(s) |
| 3626 | |
| 3627 | if (bytes_sent < 0) { |
| 3628 | s->errorhandler(); |
| 3629 | goto finally; |
| 3630 | } |
| 3631 | retval = PyLong_FromSsize_t(bytes_sent); |
| 3632 | |
| 3633 | finally: |
| 3634 | PyMem_Free(controlbuf); |
| 3635 | for (i = 0; i < ncmsgbufs; i++) |
| 3636 | PyBuffer_Release(&cmsgs[i].data); |
| 3637 | PyMem_Free(cmsgs); |
| 3638 | Py_XDECREF(cmsg_fast); |
| 3639 | for (i = 0; i < ndatabufs; i++) |
| 3640 | PyBuffer_Release(&databufs[i]); |
| 3641 | PyMem_Free(databufs); |
| 3642 | PyMem_Free(iovs); |
| 3643 | Py_XDECREF(data_fast); |
| 3644 | return retval; |
| 3645 | } |
| 3646 | |
| 3647 | PyDoc_STRVAR(sendmsg_doc, |
| 3648 | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
| 3649 | \n\ |
| 3650 | Send normal and ancillary data to the socket, gathering the\n\ |
| 3651 | non-ancillary data from a series of buffers and concatenating it into\n\ |
| 3652 | a single message. The buffers argument specifies the non-ancillary\n\ |
| 3653 | data as an iterable of buffer-compatible objects (e.g. bytes objects).\n\ |
| 3654 | The ancdata argument specifies the ancillary data (control messages)\n\ |
| 3655 | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
| 3656 | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
| 3657 | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
| 3658 | is a buffer-compatible object holding the associated data. The flags\n\ |
| 3659 | argument defaults to 0 and has the same meaning as for send(). If\n\ |
| 3660 | address is supplied and not None, it sets a destination address for\n\ |
| 3661 | the message. The return value is the number of bytes of non-ancillary\n\ |
| 3662 | data sent."); |
| 3663 | #endif /* CMSG_LEN */ |
| 3664 | |
| 3665 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3666 | /* s.shutdown(how) method */ |
| 3667 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3668 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3669 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 | int how; |
| 3672 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 3673 | |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 3674 | how = _PyLong_AsInt(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | if (how == -1 && PyErr_Occurred()) |
| 3676 | return NULL; |
| 3677 | Py_BEGIN_ALLOW_THREADS |
| 3678 | res = shutdown(s->sock_fd, how); |
| 3679 | Py_END_ALLOW_THREADS |
| 3680 | if (res < 0) |
| 3681 | return s->errorhandler(); |
| 3682 | Py_INCREF(Py_None); |
| 3683 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3686 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3687 | "shutdown(flag)\n\ |
| 3688 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3689 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3690 | 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] | 3691 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3692 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3693 | static PyObject* |
| 3694 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3696 | unsigned long cmd = SIO_RCVALL; |
| 3697 | PyObject *argO; |
| 3698 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3700 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3701 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3703 | switch (cmd) { |
| 3704 | case SIO_RCVALL: { |
| 3705 | unsigned int option = RCVALL_ON; |
| 3706 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3707 | return NULL; |
| 3708 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3709 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3710 | return set_error(); |
| 3711 | } |
| 3712 | return PyLong_FromUnsignedLong(recv); } |
| 3713 | case SIO_KEEPALIVE_VALS: { |
| 3714 | struct tcp_keepalive ka; |
| 3715 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3716 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3717 | return NULL; |
| 3718 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3719 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3720 | return set_error(); |
| 3721 | } |
| 3722 | return PyLong_FromUnsignedLong(recv); } |
| 3723 | default: |
| 3724 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3725 | return NULL; |
| 3726 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3727 | } |
| 3728 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3729 | "ioctl(cmd, option) -> long\n\ |
| 3730 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 3731 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3732 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3733 | 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] | 3734 | #endif |
| 3735 | |
| 3736 | #if defined(MS_WINDOWS) |
| 3737 | static PyObject* |
| 3738 | sock_share(PySocketSockObject *s, PyObject *arg) |
| 3739 | { |
| 3740 | WSAPROTOCOL_INFO info; |
| 3741 | DWORD processId; |
| 3742 | int result; |
| 3743 | |
| 3744 | if (!PyArg_ParseTuple(arg, "I", &processId)) |
| 3745 | return NULL; |
| 3746 | |
| 3747 | Py_BEGIN_ALLOW_THREADS |
| 3748 | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
| 3749 | Py_END_ALLOW_THREADS |
| 3750 | if (result == SOCKET_ERROR) |
| 3751 | return set_error(); |
| 3752 | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
| 3753 | } |
| 3754 | PyDoc_STRVAR(sock_share_doc, |
| 3755 | "share(process_id) -> bytes\n\ |
| 3756 | \n\ |
| 3757 | Share the socket with another process. The target process id\n\ |
| 3758 | must be provided and the resulting bytes object passed to the target\n\ |
| 3759 | process. There the shared socket can be instantiated by calling\n\ |
| 3760 | socket.fromshare()."); |
| 3761 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3762 | |
| 3763 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3764 | |
| 3765 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3766 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3767 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3768 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3769 | accept_doc}, |
| 3770 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3771 | bind_doc}, |
| 3772 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3773 | close_doc}, |
| 3774 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3775 | connect_doc}, |
| 3776 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3777 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 3778 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 3779 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3780 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3781 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3782 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3783 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3784 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3785 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3786 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3787 | METH_NOARGS, getsockname_doc}, |
| 3788 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3789 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3790 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3791 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3792 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3793 | #endif |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3794 | #if defined(MS_WINDOWS) |
| 3795 | {"share", (PyCFunction)sock_share, METH_VARARGS, |
| 3796 | sock_share_doc}, |
| 3797 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3798 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 3799 | listen_doc}, |
| 3800 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3801 | recv_doc}, |
| 3802 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3803 | recv_into_doc}, |
| 3804 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3805 | recvfrom_doc}, |
| 3806 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3807 | recvfrom_into_doc}, |
| 3808 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3809 | send_doc}, |
| 3810 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3811 | sendall_doc}, |
| 3812 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3813 | sendto_doc}, |
| 3814 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3815 | setblocking_doc}, |
| 3816 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3817 | settimeout_doc}, |
| 3818 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3819 | gettimeout_doc}, |
| 3820 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3821 | setsockopt_doc}, |
| 3822 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3823 | shutdown_doc}, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3824 | #ifdef CMSG_LEN |
| 3825 | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
| 3826 | recvmsg_doc}, |
| 3827 | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
| 3828 | recvmsg_into_doc,}, |
| 3829 | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
| 3830 | sendmsg_doc}, |
| 3831 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3832 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3833 | }; |
| 3834 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3835 | /* SockObject members */ |
| 3836 | static PyMemberDef sock_memberlist[] = { |
| 3837 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3838 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3839 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3840 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3841 | {0}, |
| 3842 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3843 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3844 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3845 | First close the file description. */ |
| 3846 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3847 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3848 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3849 | { |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3850 | if (s->sock_fd != -1) { |
| 3851 | PyObject *exc, *val, *tb; |
| 3852 | Py_ssize_t old_refcount = Py_REFCNT(s); |
| 3853 | ++Py_REFCNT(s); |
| 3854 | PyErr_Fetch(&exc, &val, &tb); |
| 3855 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, |
| 3856 | "unclosed %R", s)) |
| 3857 | /* Spurious errors can appear at shutdown */ |
| 3858 | if (PyErr_ExceptionMatches(PyExc_Warning)) |
| 3859 | PyErr_WriteUnraisable((PyObject *) s); |
| 3860 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3861 | (void) SOCKETCLOSE(s->sock_fd); |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3862 | Py_REFCNT(s) = old_refcount; |
| 3863 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3864 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3867 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3868 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3869 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3870 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3871 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | if (s->sock_fd > LONG_MAX) { |
| 3873 | /* this can occur on Win64, and actually there is a special |
| 3874 | ugly printf formatter for decimal pointer length integer |
| 3875 | printing, only bother if necessary*/ |
| 3876 | PyErr_SetString(PyExc_OverflowError, |
| 3877 | "no printf formatter to display " |
| 3878 | "the socket descriptor in decimal"); |
| 3879 | return NULL; |
| 3880 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3881 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | return PyUnicode_FromFormat( |
| 3883 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
| 3884 | (long)s->sock_fd, s->sock_family, |
| 3885 | s->sock_type, |
| 3886 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3890 | /* Create a new, uninitialized socket object. */ |
| 3891 | |
| 3892 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3893 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3894 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3895 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3897 | new = type->tp_alloc(type, 0); |
| 3898 | if (new != NULL) { |
| 3899 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3900 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3901 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 3902 | } |
| 3903 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | |
| 3907 | /* Initialize a new socket object. */ |
| 3908 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3909 | #ifdef SOCK_CLOEXEC |
| 3910 | /* socket() and socketpair() fail with EINVAL on Linux kernel older |
| 3911 | * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */ |
| 3912 | static int sock_cloexec_works = -1; |
| 3913 | #endif |
| 3914 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3915 | /*ARGSUSED*/ |
| 3916 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3917 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3919 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3920 | PyObject *fdobj = NULL; |
| 3921 | SOCKET_T fd = INVALID_SOCKET; |
| 3922 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3923 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3924 | #ifndef MS_WINDOWS |
| 3925 | #ifdef SOCK_CLOEXEC |
| 3926 | int *atomic_flag_works = &sock_cloexec_works; |
| 3927 | #else |
| 3928 | int *atomic_flag_works = NULL; |
| 3929 | #endif |
| 3930 | #endif |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3932 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 3933 | "|iiiO:socket", keywords, |
| 3934 | &family, &type, &proto, &fdobj)) |
| 3935 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3937 | if (fdobj != NULL && fdobj != Py_None) { |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3938 | #ifdef MS_WINDOWS |
| 3939 | /* recreate a socket that was duplicated */ |
| 3940 | if (PyBytes_Check(fdobj)) { |
| 3941 | WSAPROTOCOL_INFO info; |
| 3942 | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
| 3943 | PyErr_Format(PyExc_ValueError, |
| 3944 | "socket descriptor string has wrong size, " |
| 3945 | "should be %zu bytes.", sizeof(info)); |
| 3946 | return -1; |
| 3947 | } |
| 3948 | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
| 3949 | Py_BEGIN_ALLOW_THREADS |
| 3950 | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 3951 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
| 3952 | Py_END_ALLOW_THREADS |
| 3953 | if (fd == INVALID_SOCKET) { |
| 3954 | set_error(); |
| 3955 | return -1; |
| 3956 | } |
| 3957 | family = info.iAddressFamily; |
| 3958 | type = info.iSocketType; |
| 3959 | proto = info.iProtocol; |
| 3960 | } |
| 3961 | else |
| 3962 | #endif |
| 3963 | { |
| 3964 | fd = PyLong_AsSocket_t(fdobj); |
| 3965 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 3966 | return -1; |
| 3967 | if (fd == INVALID_SOCKET) { |
| 3968 | PyErr_SetString(PyExc_ValueError, |
| 3969 | "can't use invalid socket value"); |
| 3970 | return -1; |
| 3971 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3972 | } |
| 3973 | } |
| 3974 | else { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3975 | #ifdef MS_WINDOWS |
| 3976 | /* Windows implementation */ |
| 3977 | #ifndef WSA_FLAG_NO_HANDLE_INHERIT |
| 3978 | #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
| 3979 | #endif |
| 3980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3981 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3982 | if (support_wsa_no_inherit) { |
| 3983 | fd = WSASocket(family, type, proto, |
| 3984 | NULL, 0, |
| 3985 | WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); |
| 3986 | if (fd == INVALID_SOCKET) { |
| 3987 | /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */ |
| 3988 | support_wsa_no_inherit = 0; |
| 3989 | fd = socket(family, type, proto); |
| 3990 | } |
| 3991 | } |
| 3992 | else { |
| 3993 | fd = socket(family, type, proto); |
| 3994 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3997 | if (fd == INVALID_SOCKET) { |
| 3998 | set_error(); |
| 3999 | return -1; |
| 4000 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4001 | |
| 4002 | if (!support_wsa_no_inherit) { |
| 4003 | if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { |
| 4004 | closesocket(fd); |
| 4005 | PyErr_SetFromWindowsErr(0); |
| 4006 | return -1; |
| 4007 | } |
| 4008 | } |
| 4009 | #else |
| 4010 | /* UNIX */ |
| 4011 | Py_BEGIN_ALLOW_THREADS |
| 4012 | #ifdef SOCK_CLOEXEC |
| 4013 | if (sock_cloexec_works != 0) { |
| 4014 | fd = socket(family, type | SOCK_CLOEXEC, proto); |
| 4015 | if (sock_cloexec_works == -1) { |
| 4016 | if (fd >= 0) { |
| 4017 | sock_cloexec_works = 1; |
| 4018 | } |
| 4019 | else if (errno == EINVAL) { |
| 4020 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4021 | sock_cloexec_works = 0; |
| 4022 | fd = socket(family, type, proto); |
| 4023 | } |
| 4024 | } |
| 4025 | } |
| 4026 | else |
| 4027 | #endif |
| 4028 | { |
| 4029 | fd = socket(family, type, proto); |
| 4030 | } |
| 4031 | Py_END_ALLOW_THREADS |
| 4032 | |
| 4033 | if (fd == INVALID_SOCKET) { |
| 4034 | set_error(); |
| 4035 | return -1; |
| 4036 | } |
| 4037 | |
| 4038 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 4039 | SOCKETCLOSE(fd); |
| 4040 | return -1; |
| 4041 | } |
| 4042 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | } |
| 4044 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4046 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4047 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4051 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4052 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4053 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 4055 | "_socket.socket", /* tp_name */ |
| 4056 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 4057 | 0, /* tp_itemsize */ |
| 4058 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 4059 | 0, /* tp_print */ |
| 4060 | 0, /* tp_getattr */ |
| 4061 | 0, /* tp_setattr */ |
| 4062 | 0, /* tp_reserved */ |
| 4063 | (reprfunc)sock_repr, /* tp_repr */ |
| 4064 | 0, /* tp_as_number */ |
| 4065 | 0, /* tp_as_sequence */ |
| 4066 | 0, /* tp_as_mapping */ |
| 4067 | 0, /* tp_hash */ |
| 4068 | 0, /* tp_call */ |
| 4069 | 0, /* tp_str */ |
| 4070 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4071 | 0, /* tp_setattro */ |
| 4072 | 0, /* tp_as_buffer */ |
| 4073 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4074 | sock_doc, /* tp_doc */ |
| 4075 | 0, /* tp_traverse */ |
| 4076 | 0, /* tp_clear */ |
| 4077 | 0, /* tp_richcompare */ |
| 4078 | 0, /* tp_weaklistoffset */ |
| 4079 | 0, /* tp_iter */ |
| 4080 | 0, /* tp_iternext */ |
| 4081 | sock_methods, /* tp_methods */ |
| 4082 | sock_memberlist, /* tp_members */ |
| 4083 | 0, /* tp_getset */ |
| 4084 | 0, /* tp_base */ |
| 4085 | 0, /* tp_dict */ |
| 4086 | 0, /* tp_descr_get */ |
| 4087 | 0, /* tp_descr_set */ |
| 4088 | 0, /* tp_dictoffset */ |
| 4089 | sock_initobj, /* tp_init */ |
| 4090 | PyType_GenericAlloc, /* tp_alloc */ |
| 4091 | sock_new, /* tp_new */ |
| 4092 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4093 | }; |
| 4094 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4095 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4096 | /* Python interface to gethostname(). */ |
| 4097 | |
| 4098 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4099 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4100 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4101 | { |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4102 | #ifdef MS_WINDOWS |
| 4103 | /* Don't use winsock's gethostname, as this returns the ANSI |
| 4104 | version of the hostname, whereas we need a Unicode string. |
| 4105 | Otherwise, gethostname apparently also returns the DNS name. */ |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4106 | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4107 | DWORD size = Py_ARRAY_LENGTH(buf); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4108 | wchar_t *name; |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4109 | PyObject *result; |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4110 | |
| 4111 | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4112 | return PyUnicode_FromWideChar(buf, size); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4113 | |
| 4114 | if (GetLastError() != ERROR_MORE_DATA) |
| 4115 | return PyErr_SetFromWindowsErr(0); |
| 4116 | |
| 4117 | if (size == 0) |
| 4118 | return PyUnicode_New(0, 0); |
| 4119 | |
| 4120 | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
| 4121 | names */ |
| 4122 | name = PyMem_Malloc(size * sizeof(wchar_t)); |
| 4123 | if (!name) |
| 4124 | return NULL; |
| 4125 | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
| 4126 | name, |
| 4127 | &size)) |
| 4128 | { |
| 4129 | PyMem_Free(name); |
| 4130 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4131 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4132 | |
| 4133 | result = PyUnicode_FromWideChar(name, size); |
| 4134 | PyMem_Free(name); |
| 4135 | return result; |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4136 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4137 | char buf[1024]; |
| 4138 | int res; |
| 4139 | Py_BEGIN_ALLOW_THREADS |
| 4140 | res = gethostname(buf, (int) sizeof buf - 1); |
| 4141 | Py_END_ALLOW_THREADS |
| 4142 | if (res < 0) |
| 4143 | return set_error(); |
| 4144 | buf[sizeof buf - 1] = '\0'; |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4145 | return PyUnicode_DecodeFSDefault(buf); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4146 | #endif |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4147 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4149 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4150 | "gethostname() -> string\n\ |
| 4151 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4152 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4153 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4154 | #ifdef HAVE_SETHOSTNAME |
| 4155 | PyDoc_STRVAR(sethostname_doc, |
| 4156 | "sethostname(name)\n\n\ |
| 4157 | Sets the hostname to name."); |
| 4158 | |
| 4159 | static PyObject * |
| 4160 | socket_sethostname(PyObject *self, PyObject *args) |
| 4161 | { |
| 4162 | PyObject *hnobj; |
| 4163 | Py_buffer buf; |
| 4164 | int res, flag = 0; |
| 4165 | |
Christian Heimes | d2774c7 | 2013-06-19 02:06:29 +0200 | [diff] [blame] | 4166 | #ifdef _AIX |
| 4167 | /* issue #18259, not declared in any useful header file */ |
| 4168 | extern int sethostname(const char *, size_t); |
| 4169 | #endif |
| 4170 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4171 | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
| 4172 | PyErr_Clear(); |
| 4173 | if (!PyArg_ParseTuple(args, "O&:sethostname", |
| 4174 | PyUnicode_FSConverter, &hnobj)) |
| 4175 | return NULL; |
| 4176 | flag = 1; |
| 4177 | } |
| 4178 | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
| 4179 | if (!res) { |
| 4180 | res = sethostname(buf.buf, buf.len); |
| 4181 | PyBuffer_Release(&buf); |
| 4182 | } |
| 4183 | if (flag) |
| 4184 | Py_DECREF(hnobj); |
| 4185 | if (res) |
| 4186 | return set_error(); |
| 4187 | Py_RETURN_NONE; |
| 4188 | } |
| 4189 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4190 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4191 | /* Python interface to gethostbyname(name). */ |
| 4192 | |
| 4193 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4194 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4195 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4197 | char *name; |
| 4198 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4199 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4200 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4201 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | return NULL; |
| 4203 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4204 | goto finally; |
| 4205 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 4206 | finally: |
| 4207 | PyMem_Free(name); |
| 4208 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4211 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4212 | "gethostbyname(host) -> address\n\ |
| 4213 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4214 | 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] | 4215 | |
| 4216 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4217 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 4218 | |
| 4219 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4220 | gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4222 | char **pch; |
| 4223 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 4224 | PyObject *name_list = (PyObject *)NULL; |
| 4225 | PyObject *addr_list = (PyObject *)NULL; |
| 4226 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4228 | if (h == NULL) { |
| 4229 | /* Let's get real error message to return */ |
| 4230 | set_herror(h_errno); |
| 4231 | return NULL; |
| 4232 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4234 | if (h->h_addrtype != af) { |
| 4235 | /* Let's get real error message to return */ |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 4236 | errno = EAFNOSUPPORT; |
| 4237 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | return NULL; |
| 4239 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4241 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | case AF_INET: |
| 4244 | if (alen < sizeof(struct sockaddr_in)) |
| 4245 | return NULL; |
| 4246 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4247 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4248 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4249 | case AF_INET6: |
| 4250 | if (alen < sizeof(struct sockaddr_in6)) |
| 4251 | return NULL; |
| 4252 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4253 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4257 | if ((name_list = PyList_New(0)) == NULL) |
| 4258 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4260 | if ((addr_list = PyList_New(0)) == NULL) |
| 4261 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | /* SF #1511317: h_aliases can be NULL */ |
| 4264 | if (h->h_aliases) { |
| 4265 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 4266 | int status; |
| 4267 | tmp = PyUnicode_FromString(*pch); |
| 4268 | if (tmp == NULL) |
| 4269 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4271 | status = PyList_Append(name_list, tmp); |
| 4272 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4274 | if (status) |
| 4275 | goto err; |
| 4276 | } |
| 4277 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 4280 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4282 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4284 | case AF_INET: |
| 4285 | { |
| 4286 | struct sockaddr_in sin; |
| 4287 | memset(&sin, 0, sizeof(sin)); |
| 4288 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4289 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4290 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4291 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4292 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 4293 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4295 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 4296 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 4297 | break; |
| 4298 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4299 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4300 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4301 | case AF_INET6: |
| 4302 | { |
| 4303 | struct sockaddr_in6 sin6; |
| 4304 | memset(&sin6, 0, sizeof(sin6)); |
| 4305 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4306 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4308 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4309 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 4310 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 4311 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4313 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 4314 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 4315 | break; |
| 4316 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4317 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | default: /* can't happen */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4320 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4321 | "unsupported address family"); |
| 4322 | return NULL; |
| 4323 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | if (tmp == NULL) |
| 4326 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | status = PyList_Append(addr_list, tmp); |
| 4329 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | if (status) |
| 4332 | goto err; |
| 4333 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4335 | 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] | 4336 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4337 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4338 | Py_XDECREF(name_list); |
| 4339 | Py_XDECREF(addr_list); |
| 4340 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | |
| 4344 | /* Python interface to gethostbyname_ex(name). */ |
| 4345 | |
| 4346 | /*ARGSUSED*/ |
| 4347 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4348 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4350 | char *name; |
| 4351 | struct hostent *h; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4352 | sock_addr_t addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4354 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4355 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4357 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4358 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4359 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4360 | char buf[16384]; |
| 4361 | int buf_len = (sizeof buf) - 1; |
| 4362 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4363 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4364 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4365 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4366 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4367 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4368 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4369 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | return NULL; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4371 | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4372 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4373 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4374 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4375 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4376 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4378 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4380 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4381 | memset((void *) &data, '\0', sizeof(data)); |
| 4382 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 4383 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4384 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4385 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4386 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4388 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4390 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | Py_END_ALLOW_THREADS |
| 4392 | /* Some C libraries would require addr.__ss_family instead of |
| 4393 | addr.ss_family. |
| 4394 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 4395 | access sa_family. */ |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4396 | sa = SAS2SA(&addr); |
| 4397 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4399 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 4401 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4402 | finally: |
| 4403 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4407 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4408 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 4409 | \n\ |
| 4410 | 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] | 4411 | 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] | 4412 | |
| 4413 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4414 | /* Python interface to gethostbyaddr(IP). */ |
| 4415 | |
| 4416 | /*ARGSUSED*/ |
| 4417 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4418 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4419 | { |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4420 | sock_addr_t addr; |
| 4421 | struct sockaddr *sa = SAS2SA(&addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | char *ip_num; |
| 4423 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4424 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4425 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4427 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4428 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4429 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | /* glibcs up to 2.10 assume that the buf argument to |
| 4431 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 4432 | does not ensure. The attribute below instructs the compiler |
| 4433 | to maintain this alignment. */ |
| 4434 | char buf[16384] Py_ALIGNED(8); |
| 4435 | int buf_len = (sizeof buf) - 1; |
| 4436 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4437 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4438 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4440 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4441 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | char *ap; |
| 4443 | int al; |
| 4444 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4445 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4446 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | return NULL; |
| 4448 | af = AF_UNSPEC; |
| 4449 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4450 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | af = sa->sa_family; |
| 4452 | ap = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 4453 | /* al = 0; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | switch (af) { |
| 4455 | case AF_INET: |
| 4456 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 4457 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 4458 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4459 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | case AF_INET6: |
| 4461 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 4462 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 4463 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4464 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4466 | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4467 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | } |
| 4469 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4470 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4471 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4472 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | &hp_allocated, buf, buf_len, |
| 4474 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4475 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4476 | h = gethostbyaddr_r(ap, al, af, |
| 4477 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4478 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | memset((void *) &data, '\0', sizeof(data)); |
| 4480 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 4481 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4482 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4483 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4484 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4486 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4488 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | Py_END_ALLOW_THREADS |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4490 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4491 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4492 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4493 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4494 | finally: |
| 4495 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4499 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4500 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 4501 | \n\ |
| 4502 | 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] | 4503 | 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] | 4504 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4505 | |
| 4506 | /* Python interface to getservbyname(name). |
| 4507 | This only returns the port number, since the other info is already |
| 4508 | known or not useful (like the list of aliases). */ |
| 4509 | |
| 4510 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4511 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4512 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4514 | char *name, *proto=NULL; |
| 4515 | struct servent *sp; |
| 4516 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 4517 | return NULL; |
| 4518 | Py_BEGIN_ALLOW_THREADS |
| 4519 | sp = getservbyname(name, proto); |
| 4520 | Py_END_ALLOW_THREADS |
| 4521 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4522 | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4523 | return NULL; |
| 4524 | } |
| 4525 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4528 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4529 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4530 | \n\ |
| 4531 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4532 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4533 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4534 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4535 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4536 | /* Python interface to getservbyport(port). |
| 4537 | This only returns the service name, since the other info is already |
| 4538 | known or not useful (like the list of aliases). */ |
| 4539 | |
| 4540 | /*ARGSUSED*/ |
| 4541 | static PyObject * |
| 4542 | socket_getservbyport(PyObject *self, PyObject *args) |
| 4543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4544 | int port; |
| 4545 | char *proto=NULL; |
| 4546 | struct servent *sp; |
| 4547 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 4548 | return NULL; |
| 4549 | if (port < 0 || port > 0xffff) { |
| 4550 | PyErr_SetString( |
| 4551 | PyExc_OverflowError, |
| 4552 | "getservbyport: port must be 0-65535."); |
| 4553 | return NULL; |
| 4554 | } |
| 4555 | Py_BEGIN_ALLOW_THREADS |
| 4556 | sp = getservbyport(htons((short)port), proto); |
| 4557 | Py_END_ALLOW_THREADS |
| 4558 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4559 | PyErr_SetString(PyExc_OSError, "port/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4560 | return NULL; |
| 4561 | } |
| 4562 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | PyDoc_STRVAR(getservbyport_doc, |
| 4566 | "getservbyport(port[, protocolname]) -> string\n\ |
| 4567 | \n\ |
| 4568 | Return the service name from a port number and protocol name.\n\ |
| 4569 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4570 | otherwise any protocol will match."); |
| 4571 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4572 | /* Python interface to getprotobyname(name). |
| 4573 | This only returns the protocol number, since the other info is |
| 4574 | already known or not useful (like the list of aliases). */ |
| 4575 | |
| 4576 | /*ARGSUSED*/ |
| 4577 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4578 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | char *name; |
| 4581 | struct protoent *sp; |
| 4582 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 4583 | return NULL; |
| 4584 | Py_BEGIN_ALLOW_THREADS |
| 4585 | sp = getprotobyname(name); |
| 4586 | Py_END_ALLOW_THREADS |
| 4587 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4588 | PyErr_SetString(PyExc_OSError, "protocol not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | return NULL; |
| 4590 | } |
| 4591 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4594 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4595 | "getprotobyname(name) -> integer\n\ |
| 4596 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4597 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4598 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4599 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4600 | #ifndef NO_DUP |
| 4601 | /* dup() function for socket fds */ |
| 4602 | |
| 4603 | static PyObject * |
| 4604 | socket_dup(PyObject *self, PyObject *fdobj) |
| 4605 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | SOCKET_T fd, newfd; |
| 4607 | PyObject *newfdobj; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4608 | #ifdef MS_WINDOWS |
| 4609 | WSAPROTOCOL_INFO info; |
| 4610 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4612 | fd = PyLong_AsSocket_t(fdobj); |
| 4613 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4614 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4615 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4616 | #ifdef MS_WINDOWS |
| 4617 | if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info)) |
| 4618 | return set_error(); |
| 4619 | |
| 4620 | newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 4621 | FROM_PROTOCOL_INFO, |
| 4622 | &info, 0, WSA_FLAG_OVERLAPPED); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4623 | if (newfd == INVALID_SOCKET) |
| 4624 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4625 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4626 | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
| 4627 | closesocket(newfd); |
| 4628 | PyErr_SetFromWindowsErr(0); |
| 4629 | return NULL; |
| 4630 | } |
| 4631 | #else |
| 4632 | /* On UNIX, dup can be used to duplicate the file descriptor of a socket */ |
| 4633 | newfd = _Py_dup(fd); |
| 4634 | if (newfd == INVALID_SOCKET) |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 4635 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4636 | #endif |
| 4637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | newfdobj = PyLong_FromSocket_t(newfd); |
| 4639 | if (newfdobj == NULL) |
| 4640 | SOCKETCLOSE(newfd); |
| 4641 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | PyDoc_STRVAR(dup_doc, |
| 4645 | "dup(integer) -> integer\n\ |
| 4646 | \n\ |
| 4647 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 4648 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 4649 | #endif |
| 4650 | |
| 4651 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4652 | #ifdef HAVE_SOCKETPAIR |
| 4653 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4654 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 4655 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4656 | |
| 4657 | /*ARGSUSED*/ |
| 4658 | static PyObject * |
| 4659 | socket_socketpair(PyObject *self, PyObject *args) |
| 4660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4661 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 4662 | SOCKET_T sv[2]; |
| 4663 | int family, type = SOCK_STREAM, proto = 0; |
| 4664 | PyObject *res = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4665 | #ifdef SOCK_CLOEXEC |
| 4666 | int *atomic_flag_works = &sock_cloexec_works; |
| 4667 | #else |
| 4668 | int *atomic_flag_works = NULL; |
| 4669 | #endif |
| 4670 | int ret; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4671 | |
| 4672 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4674 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4675 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4676 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 4678 | &family, &type, &proto)) |
| 4679 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | /* Create a pair of socket fds */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4682 | Py_BEGIN_ALLOW_THREADS |
| 4683 | #ifdef SOCK_CLOEXEC |
| 4684 | if (sock_cloexec_works != 0) { |
| 4685 | ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv); |
| 4686 | if (sock_cloexec_works == -1) { |
| 4687 | if (ret >= 0) { |
| 4688 | sock_cloexec_works = 1; |
| 4689 | } |
| 4690 | else if (errno == EINVAL) { |
| 4691 | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
| 4692 | sock_cloexec_works = 0; |
| 4693 | ret = socketpair(family, type, proto, sv); |
| 4694 | } |
| 4695 | } |
| 4696 | } |
| 4697 | else |
| 4698 | #endif |
| 4699 | { |
| 4700 | ret = socketpair(family, type, proto, sv); |
| 4701 | } |
| 4702 | Py_END_ALLOW_THREADS |
| 4703 | |
| 4704 | if (ret < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4705 | return set_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4706 | |
| 4707 | if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0) |
| 4708 | goto finally; |
| 4709 | if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0) |
| 4710 | goto finally; |
| 4711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | s0 = new_sockobject(sv[0], family, type, proto); |
| 4713 | if (s0 == NULL) |
| 4714 | goto finally; |
| 4715 | s1 = new_sockobject(sv[1], family, type, proto); |
| 4716 | if (s1 == NULL) |
| 4717 | goto finally; |
| 4718 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4719 | |
| 4720 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | if (res == NULL) { |
| 4722 | if (s0 == NULL) |
| 4723 | SOCKETCLOSE(sv[0]); |
| 4724 | if (s1 == NULL) |
| 4725 | SOCKETCLOSE(sv[1]); |
| 4726 | } |
| 4727 | Py_XDECREF(s0); |
| 4728 | Py_XDECREF(s1); |
| 4729 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | PyDoc_STRVAR(socketpair_doc, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4733 | "socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4734 | \n\ |
| 4735 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 4736 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4737 | 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] | 4738 | 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] | 4739 | |
| 4740 | #endif /* HAVE_SOCKETPAIR */ |
| 4741 | |
| 4742 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4743 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4744 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4748 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 4749 | return NULL; |
| 4750 | } |
| 4751 | if (x1 < 0) { |
| 4752 | PyErr_SetString(PyExc_OverflowError, |
| 4753 | "can't convert negative number to unsigned long"); |
| 4754 | return NULL; |
| 4755 | } |
| 4756 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 4757 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4760 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4761 | "ntohs(integer) -> integer\n\ |
| 4762 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4763 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4764 | |
| 4765 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4766 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4767 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4769 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4771 | if (PyLong_Check(arg)) { |
| 4772 | x = PyLong_AsUnsignedLong(arg); |
| 4773 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4774 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4775 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | { |
| 4777 | unsigned long y; |
| 4778 | /* only want the trailing 32 bits */ |
| 4779 | y = x & 0xFFFFFFFFUL; |
| 4780 | if (y ^ x) |
| 4781 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4782 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4783 | x = y; |
| 4784 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4785 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4786 | } |
| 4787 | else |
| 4788 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4789 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4790 | Py_TYPE(arg)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4791 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4794 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4795 | "ntohl(integer) -> integer\n\ |
| 4796 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4797 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4798 | |
| 4799 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4800 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4801 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4803 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4805 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 4806 | return NULL; |
| 4807 | } |
| 4808 | if (x1 < 0) { |
| 4809 | PyErr_SetString(PyExc_OverflowError, |
| 4810 | "can't convert negative number to unsigned long"); |
| 4811 | return NULL; |
| 4812 | } |
| 4813 | x2 = (unsigned int)htons((unsigned short)x1); |
| 4814 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4817 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4818 | "htons(integer) -> integer\n\ |
| 4819 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4820 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4821 | |
| 4822 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4823 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4824 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4826 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | if (PyLong_Check(arg)) { |
| 4829 | x = PyLong_AsUnsignedLong(arg); |
| 4830 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4831 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4832 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4833 | { |
| 4834 | unsigned long y; |
| 4835 | /* only want the trailing 32 bits */ |
| 4836 | y = x & 0xFFFFFFFFUL; |
| 4837 | if (y ^ x) |
| 4838 | return PyErr_Format(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4839 | "int larger than 32 bits"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4840 | x = y; |
| 4841 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4842 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4843 | } |
| 4844 | else |
| 4845 | return PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4846 | "expected int, %s found", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4847 | Py_TYPE(arg)->tp_name); |
| 4848 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4851 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4852 | "htonl(integer) -> integer\n\ |
| 4853 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4854 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4855 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4856 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4857 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4858 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4859 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4860 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4861 | 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] | 4862 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4863 | |
| 4864 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4865 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4866 | { |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 4867 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4869 | #endif |
| 4870 | |
| 4871 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 4872 | #if (SIZEOF_INT != 4) |
| 4873 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 4874 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4875 | /* Have to use inet_addr() instead */ |
| 4876 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4877 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4880 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 4881 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4882 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4883 | |
| 4884 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4885 | |
| 4886 | #ifdef USE_INET_ATON_WEAKLINK |
| 4887 | if (inet_aton != NULL) { |
| 4888 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | if (inet_aton(ip_addr, &buf)) |
| 4890 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 4891 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4892 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4893 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | "illegal IP address string passed to inet_aton"); |
| 4895 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4896 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4897 | #ifdef USE_INET_ATON_WEAKLINK |
| 4898 | } else { |
| 4899 | #endif |
| 4900 | |
| 4901 | #endif |
| 4902 | |
| 4903 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 4904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4905 | /* special-case this address as inet_addr might return INADDR_NONE |
| 4906 | * for this */ |
| 4907 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
Charles-François Natali | 24aa041 | 2013-08-31 14:48:25 +0200 | [diff] [blame] | 4908 | packed_addr = INADDR_BROADCAST; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4909 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4911 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4914 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | "illegal IP address string passed to inet_aton"); |
| 4916 | return NULL; |
| 4917 | } |
| 4918 | } |
| 4919 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 4920 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4921 | |
| 4922 | #ifdef USE_INET_ATON_WEAKLINK |
| 4923 | } |
| 4924 | #endif |
| 4925 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4926 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4929 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 4930 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4931 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4932 | 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] | 4933 | |
| 4934 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4935 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4937 | char *packed_str; |
| 4938 | int addr_len; |
| 4939 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4941 | if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
| 4942 | return NULL; |
| 4943 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4945 | if (addr_len != sizeof(packed_addr)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4946 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | "packed IP wrong length for inet_ntoa"); |
| 4948 | return NULL; |
| 4949 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4951 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4953 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4954 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4955 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 4956 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4957 | |
| 4958 | PyDoc_STRVAR(inet_pton_doc, |
| 4959 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 4960 | \n\ |
| 4961 | Convert an IP address from string format to a packed string suitable\n\ |
| 4962 | for use with low-level network functions."); |
| 4963 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 4964 | #endif |
| 4965 | |
| 4966 | #ifdef HAVE_INET_PTON |
| 4967 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4968 | static PyObject * |
| 4969 | socket_inet_pton(PyObject *self, PyObject *args) |
| 4970 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4971 | int af; |
| 4972 | char* ip; |
| 4973 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4974 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 4975 | 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] | 4976 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4978 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 4980 | return NULL; |
| 4981 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4982 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 4983 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4984 | if(af == AF_INET6) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4985 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4986 | "can't use AF_INET6, IPv6 is disabled"); |
| 4987 | return NULL; |
| 4988 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4989 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 4990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4991 | retval = inet_pton(af, ip, packed); |
| 4992 | if (retval < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4993 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4994 | return NULL; |
| 4995 | } else if (retval == 0) { |
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_pton"); |
| 4998 | return NULL; |
| 4999 | } else if (af == AF_INET) { |
| 5000 | return PyBytes_FromStringAndSize(packed, |
| 5001 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5002 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5003 | } else if (af == AF_INET6) { |
| 5004 | return PyBytes_FromStringAndSize(packed, |
| 5005 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5006 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5007 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5008 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | return NULL; |
| 5010 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5011 | } |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5012 | #elif defined(MS_WINDOWS) |
| 5013 | |
| 5014 | static PyObject * |
| 5015 | socket_inet_pton(PyObject *self, PyObject *args) |
| 5016 | { |
| 5017 | int af; |
| 5018 | char* ip; |
| 5019 | struct sockaddr_in6 addr; |
| 5020 | INT ret, size; |
| 5021 | |
| 5022 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 5023 | return NULL; |
| 5024 | } |
| 5025 | |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5026 | size = sizeof(addr); |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5027 | ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size); |
| 5028 | |
| 5029 | if (ret) { |
| 5030 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5031 | return NULL; |
| 5032 | } else if(af == AF_INET) { |
| 5033 | struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr; |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5034 | return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5035 | sizeof(addr4->sin_addr)); |
| 5036 | } else if (af == AF_INET6) { |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5037 | return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr), |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5038 | sizeof(addr.sin6_addr)); |
| 5039 | } else { |
| 5040 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
| 5041 | return NULL; |
| 5042 | } |
| 5043 | } |
| 5044 | |
| 5045 | #endif |
| 5046 | |
| 5047 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5048 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5049 | PyDoc_STRVAR(inet_ntop_doc, |
| 5050 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 5051 | \n\ |
| 5052 | Convert a packed IP address of the given family to string format."); |
| 5053 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5054 | #endif |
| 5055 | |
| 5056 | |
| 5057 | #ifdef HAVE_INET_PTON |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5058 | static PyObject * |
| 5059 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5061 | int af; |
| 5062 | char* packed; |
| 5063 | int len; |
| 5064 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5065 | #ifdef ENABLE_IPV6 |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 5066 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5067 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5069 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5071 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5072 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 5075 | return NULL; |
| 5076 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5078 | if (af == AF_INET) { |
| 5079 | if (len != sizeof(struct in_addr)) { |
| 5080 | PyErr_SetString(PyExc_ValueError, |
| 5081 | "invalid length of packed IP address string"); |
| 5082 | return NULL; |
| 5083 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5084 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | } else if (af == AF_INET6) { |
| 5086 | if (len != sizeof(struct in6_addr)) { |
| 5087 | PyErr_SetString(PyExc_ValueError, |
| 5088 | "invalid length of packed IP address string"); |
| 5089 | return NULL; |
| 5090 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 5091 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | } else { |
| 5093 | PyErr_Format(PyExc_ValueError, |
| 5094 | "unknown address family %d", af); |
| 5095 | return NULL; |
| 5096 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 5099 | if (!retval) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5100 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | return NULL; |
| 5102 | } else { |
| 5103 | return PyUnicode_FromString(retval); |
| 5104 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5106 | /* NOTREACHED */ |
| 5107 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 5108 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5111 | #elif defined(MS_WINDOWS) |
| 5112 | |
| 5113 | static PyObject * |
| 5114 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 5115 | { |
| 5116 | int af; |
| 5117 | char* packed; |
| 5118 | int len; |
| 5119 | struct sockaddr_in6 addr; |
| 5120 | DWORD addrlen, ret, retlen; |
Jason R. Coombs | 8ec784c | 2013-11-10 13:43:22 -0500 | [diff] [blame] | 5121 | #ifdef ENABLE_IPV6 |
| 5122 | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
| 5123 | #else |
| 5124 | char ip[INET_ADDRSTRLEN + 1]; |
| 5125 | #endif |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5126 | |
| 5127 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 5128 | memset((void *) &ip[0], '\0', sizeof(ip)); |
| 5129 | |
| 5130 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 5131 | return NULL; |
| 5132 | } |
| 5133 | |
| 5134 | if (af == AF_INET) { |
| 5135 | struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr; |
| 5136 | |
| 5137 | if (len != sizeof(struct in_addr)) { |
| 5138 | PyErr_SetString(PyExc_ValueError, |
| 5139 | "invalid length of packed IP address string"); |
| 5140 | return NULL; |
| 5141 | } |
| 5142 | memset(addr4, 0, sizeof(struct sockaddr_in)); |
| 5143 | addr4->sin_family = AF_INET; |
| 5144 | memcpy(&(addr4->sin_addr), packed, sizeof(addr4->sin_addr)); |
| 5145 | addrlen = sizeof(struct sockaddr_in); |
| 5146 | } else if (af == AF_INET6) { |
| 5147 | if (len != sizeof(struct in6_addr)) { |
| 5148 | PyErr_SetString(PyExc_ValueError, |
| 5149 | "invalid length of packed IP address string"); |
| 5150 | return NULL; |
| 5151 | } |
| 5152 | |
| 5153 | memset(&addr, 0, sizeof(addr)); |
| 5154 | addr.sin6_family = AF_INET6; |
| 5155 | memcpy(&(addr.sin6_addr), packed, sizeof(addr.sin6_addr)); |
| 5156 | addrlen = sizeof(addr); |
| 5157 | } else { |
| 5158 | PyErr_Format(PyExc_ValueError, |
| 5159 | "unknown address family %d", af); |
| 5160 | return NULL; |
| 5161 | } |
| 5162 | |
| 5163 | retlen = sizeof(ip); |
Victor Stinner | e990c6e | 2013-11-16 00:18:58 +0100 | [diff] [blame] | 5164 | ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5165 | ip, &retlen); |
| 5166 | |
| 5167 | if (ret) { |
| 5168 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
| 5169 | return NULL; |
| 5170 | } else { |
| 5171 | return PyUnicode_FromString(ip); |
| 5172 | } |
| 5173 | } |
| 5174 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5175 | #endif /* HAVE_INET_PTON */ |
| 5176 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5177 | /* Python interface to getaddrinfo(host, port). */ |
| 5178 | |
| 5179 | /*ARGSUSED*/ |
| 5180 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5181 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5182 | { |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5183 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5184 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5185 | struct addrinfo hints, *res; |
| 5186 | struct addrinfo *res0 = NULL; |
| 5187 | PyObject *hobj = NULL; |
| 5188 | PyObject *pobj = (PyObject *)NULL; |
| 5189 | char pbuf[30]; |
| 5190 | char *hptr, *pptr; |
| 5191 | int family, socktype, protocol, flags; |
| 5192 | int error; |
| 5193 | PyObject *all = (PyObject *)NULL; |
| 5194 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5195 | |
Georg Brandl | 6083a4b | 2013-10-14 06:51:46 +0200 | [diff] [blame] | 5196 | socktype = protocol = flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5197 | family = AF_UNSPEC; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5198 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5199 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5200 | &protocol, &flags)) { |
| 5201 | return NULL; |
| 5202 | } |
| 5203 | if (hobj == Py_None) { |
| 5204 | hptr = NULL; |
| 5205 | } else if (PyUnicode_Check(hobj)) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5206 | _Py_IDENTIFIER(encode); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5207 | |
| 5208 | idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | if (!idna) |
| 5210 | return NULL; |
| 5211 | assert(PyBytes_Check(idna)); |
| 5212 | hptr = PyBytes_AS_STRING(idna); |
| 5213 | } else if (PyBytes_Check(hobj)) { |
| 5214 | hptr = PyBytes_AsString(hobj); |
| 5215 | } else { |
| 5216 | PyErr_SetString(PyExc_TypeError, |
| 5217 | "getaddrinfo() argument 1 must be string or None"); |
| 5218 | return NULL; |
| 5219 | } |
| 5220 | if (PyLong_CheckExact(pobj)) { |
| 5221 | long value = PyLong_AsLong(pobj); |
| 5222 | if (value == -1 && PyErr_Occurred()) |
| 5223 | goto err; |
| 5224 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 5225 | pptr = pbuf; |
| 5226 | } else if (PyUnicode_Check(pobj)) { |
| 5227 | pptr = _PyUnicode_AsString(pobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5228 | if (pptr == NULL) |
| 5229 | goto err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | } else if (PyBytes_Check(pobj)) { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 5231 | pptr = PyBytes_AS_STRING(pobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | } else if (pobj == Py_None) { |
| 5233 | pptr = (char *)NULL; |
| 5234 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5235 | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5236 | goto err; |
| 5237 | } |
Ronald Oussoren | a822d366 | 2013-06-10 10:36:28 +0200 | [diff] [blame] | 5238 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5239 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 5240 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 5241 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 5242 | * This workaround avoids a segfault in libsystem. |
| 5243 | */ |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 5244 | pptr = "00"; |
| 5245 | } |
| 5246 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | memset(&hints, 0, sizeof(hints)); |
| 5248 | hints.ai_family = family; |
| 5249 | hints.ai_socktype = socktype; |
| 5250 | hints.ai_protocol = protocol; |
| 5251 | hints.ai_flags = flags; |
| 5252 | Py_BEGIN_ALLOW_THREADS |
| 5253 | ACQUIRE_GETADDRINFO_LOCK |
| 5254 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 5255 | Py_END_ALLOW_THREADS |
| 5256 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5257 | if (error) { |
| 5258 | set_gaierror(error); |
| 5259 | goto err; |
| 5260 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | if ((all = PyList_New(0)) == NULL) |
| 5263 | goto err; |
| 5264 | for (res = res0; res; res = res->ai_next) { |
| 5265 | PyObject *single; |
| 5266 | PyObject *addr = |
| 5267 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 5268 | if (addr == NULL) |
| 5269 | goto err; |
| 5270 | single = Py_BuildValue("iiisO", res->ai_family, |
| 5271 | res->ai_socktype, res->ai_protocol, |
| 5272 | res->ai_canonname ? res->ai_canonname : "", |
| 5273 | addr); |
| 5274 | Py_DECREF(addr); |
| 5275 | if (single == NULL) |
| 5276 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5278 | if (PyList_Append(all, single)) |
| 5279 | goto err; |
| 5280 | Py_XDECREF(single); |
| 5281 | } |
| 5282 | Py_XDECREF(idna); |
| 5283 | if (res0) |
| 5284 | freeaddrinfo(res0); |
| 5285 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5286 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | Py_XDECREF(all); |
| 5288 | Py_XDECREF(idna); |
| 5289 | if (res0) |
| 5290 | freeaddrinfo(res0); |
| 5291 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5294 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5295 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 5296 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5297 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5298 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5299 | |
| 5300 | /* Python interface to getnameinfo(sa, flags). */ |
| 5301 | |
| 5302 | /*ARGSUSED*/ |
| 5303 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5304 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5306 | PyObject *sa = (PyObject *)NULL; |
| 5307 | int flags; |
| 5308 | char *hostp; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5309 | int port; |
| 5310 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 5312 | struct addrinfo hints, *res = NULL; |
| 5313 | int error; |
| 5314 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5316 | flags = flowinfo = scope_id = 0; |
| 5317 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 5318 | return NULL; |
| 5319 | if (!PyTuple_Check(sa)) { |
| 5320 | PyErr_SetString(PyExc_TypeError, |
| 5321 | "getnameinfo() argument 1 must be a tuple"); |
| 5322 | return NULL; |
| 5323 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5324 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5325 | &hostp, &port, &flowinfo, &scope_id)) |
| 5326 | return NULL; |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 5327 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5328 | PyErr_SetString(PyExc_OverflowError, |
| 5329 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 5330 | return NULL; |
| 5331 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5332 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 5333 | memset(&hints, 0, sizeof(hints)); |
| 5334 | hints.ai_family = AF_UNSPEC; |
| 5335 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 5336 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5337 | Py_BEGIN_ALLOW_THREADS |
| 5338 | ACQUIRE_GETADDRINFO_LOCK |
| 5339 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 5340 | Py_END_ALLOW_THREADS |
| 5341 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5342 | if (error) { |
| 5343 | set_gaierror(error); |
| 5344 | goto fail; |
| 5345 | } |
| 5346 | if (res->ai_next) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5347 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5348 | "sockaddr resolved to multiple addresses"); |
| 5349 | goto fail; |
| 5350 | } |
| 5351 | switch (res->ai_family) { |
| 5352 | case AF_INET: |
| 5353 | { |
| 5354 | if (PyTuple_GET_SIZE(sa) != 2) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5355 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5356 | "IPv4 sockaddr must be 2 tuple"); |
| 5357 | goto fail; |
| 5358 | } |
| 5359 | break; |
| 5360 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5361 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5362 | case AF_INET6: |
| 5363 | { |
| 5364 | struct sockaddr_in6 *sin6; |
| 5365 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5366 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5367 | sin6->sin6_scope_id = scope_id; |
| 5368 | break; |
| 5369 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5370 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5371 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 5372 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5373 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 5374 | if (error) { |
| 5375 | set_gaierror(error); |
| 5376 | goto fail; |
| 5377 | } |
| 5378 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5379 | |
| 5380 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5381 | if (res) |
| 5382 | freeaddrinfo(res); |
| 5383 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5384 | } |
| 5385 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5386 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5387 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5388 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5389 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5390 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5391 | |
| 5392 | /* Python API to getting and setting the default timeout value. */ |
| 5393 | |
| 5394 | static PyObject * |
| 5395 | socket_getdefaulttimeout(PyObject *self) |
| 5396 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5397 | if (defaulttimeout < 0.0) { |
| 5398 | Py_INCREF(Py_None); |
| 5399 | return Py_None; |
| 5400 | } |
| 5401 | else |
| 5402 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5406 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5407 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5408 | 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] | 5409 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5410 | When the socket module is first imported, the default is None."); |
| 5411 | |
| 5412 | static PyObject * |
| 5413 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 5414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5415 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | if (arg == Py_None) |
| 5418 | timeout = -1.0; |
| 5419 | else { |
| 5420 | timeout = PyFloat_AsDouble(arg); |
| 5421 | if (timeout < 0.0) { |
| 5422 | if (!PyErr_Occurred()) |
| 5423 | PyErr_SetString(PyExc_ValueError, |
| 5424 | "Timeout value out of range"); |
| 5425 | return NULL; |
| 5426 | } |
| 5427 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5429 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5431 | Py_INCREF(Py_None); |
| 5432 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5436 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5437 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5438 | 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] | 5439 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5440 | When the socket module is first imported, the default is None."); |
| 5441 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5442 | #ifdef HAVE_IF_NAMEINDEX |
| 5443 | /* Python API for getting interface indices and names */ |
| 5444 | |
| 5445 | static PyObject * |
| 5446 | socket_if_nameindex(PyObject *self, PyObject *arg) |
| 5447 | { |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5448 | PyObject *list; |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5449 | int i; |
| 5450 | struct if_nameindex *ni; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5451 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5452 | ni = if_nameindex(); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5453 | if (ni == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5454 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5455 | return NULL; |
| 5456 | } |
| 5457 | |
| 5458 | list = PyList_New(0); |
| 5459 | if (list == NULL) { |
| 5460 | if_freenameindex(ni); |
| 5461 | return NULL; |
| 5462 | } |
| 5463 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5464 | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
| 5465 | PyObject *ni_tuple = Py_BuildValue("IO&", |
| 5466 | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5467 | |
| 5468 | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
| 5469 | Py_XDECREF(ni_tuple); |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5470 | Py_DECREF(list); |
| 5471 | if_freenameindex(ni); |
| 5472 | return NULL; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5473 | } |
| 5474 | Py_DECREF(ni_tuple); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | if_freenameindex(ni); |
| 5478 | return list; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | PyDoc_STRVAR(if_nameindex_doc, |
| 5482 | "if_nameindex()\n\ |
| 5483 | \n\ |
| 5484 | Returns a list of network interface information (index, name) tuples."); |
| 5485 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5486 | static PyObject * |
| 5487 | socket_if_nametoindex(PyObject *self, PyObject *args) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5488 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5489 | PyObject *oname; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5490 | unsigned long index; |
| 5491 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5492 | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
| 5493 | PyUnicode_FSConverter, &oname)) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5494 | return NULL; |
| 5495 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5496 | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
| 5497 | Py_DECREF(oname); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5498 | if (index == 0) { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5499 | /* if_nametoindex() doesn't set errno */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5500 | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5501 | return NULL; |
| 5502 | } |
| 5503 | |
| 5504 | return PyLong_FromUnsignedLong(index); |
| 5505 | } |
| 5506 | |
| 5507 | PyDoc_STRVAR(if_nametoindex_doc, |
| 5508 | "if_nametoindex(if_name)\n\ |
| 5509 | \n\ |
| 5510 | Returns the interface index corresponding to the interface name if_name."); |
| 5511 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5512 | static PyObject * |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5513 | socket_if_indextoname(PyObject *self, PyObject *arg) |
| 5514 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5515 | unsigned long index; |
| 5516 | char name[IF_NAMESIZE + 1]; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5517 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5518 | index = PyLong_AsUnsignedLong(arg); |
| 5519 | if (index == (unsigned long) -1) |
| 5520 | return NULL; |
| 5521 | |
| 5522 | if (if_indextoname(index, name) == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5523 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5524 | return NULL; |
| 5525 | } |
| 5526 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5527 | return PyUnicode_DecodeFSDefault(name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | PyDoc_STRVAR(if_indextoname_doc, |
| 5531 | "if_indextoname(if_index)\n\ |
| 5532 | \n\ |
| 5533 | Returns the interface name corresponding to the interface index if_index."); |
| 5534 | |
| 5535 | #endif /* HAVE_IF_NAMEINDEX */ |
| 5536 | |
| 5537 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5538 | #ifdef CMSG_LEN |
| 5539 | /* Python interface to CMSG_LEN(length). */ |
| 5540 | |
| 5541 | static PyObject * |
| 5542 | socket_CMSG_LEN(PyObject *self, PyObject *args) |
| 5543 | { |
| 5544 | Py_ssize_t length; |
| 5545 | size_t result; |
| 5546 | |
| 5547 | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
| 5548 | return NULL; |
| 5549 | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
| 5550 | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
| 5551 | return NULL; |
| 5552 | } |
| 5553 | return PyLong_FromSize_t(result); |
| 5554 | } |
| 5555 | |
| 5556 | PyDoc_STRVAR(CMSG_LEN_doc, |
| 5557 | "CMSG_LEN(length) -> control message length\n\ |
| 5558 | \n\ |
| 5559 | Return the total length, without trailing padding, of an ancillary\n\ |
| 5560 | data item with associated data of the given length. This value can\n\ |
| 5561 | often be used as the buffer size for recvmsg() to receive a single\n\ |
| 5562 | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
| 5563 | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
| 5564 | item will be the last in the buffer. Raises OverflowError if length\n\ |
| 5565 | is outside the permissible range of values."); |
| 5566 | |
| 5567 | |
| 5568 | #ifdef CMSG_SPACE |
| 5569 | /* Python interface to CMSG_SPACE(length). */ |
| 5570 | |
| 5571 | static PyObject * |
| 5572 | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
| 5573 | { |
| 5574 | Py_ssize_t length; |
| 5575 | size_t result; |
| 5576 | |
| 5577 | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
| 5578 | return NULL; |
| 5579 | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
| 5580 | PyErr_SetString(PyExc_OverflowError, |
| 5581 | "CMSG_SPACE() argument out of range"); |
| 5582 | return NULL; |
| 5583 | } |
| 5584 | return PyLong_FromSize_t(result); |
| 5585 | } |
| 5586 | |
| 5587 | PyDoc_STRVAR(CMSG_SPACE_doc, |
| 5588 | "CMSG_SPACE(length) -> buffer size\n\ |
| 5589 | \n\ |
| 5590 | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
| 5591 | data item with associated data of the given length, along with any\n\ |
| 5592 | trailing padding. The buffer space needed to receive multiple items\n\ |
| 5593 | is the sum of the CMSG_SPACE() values for their associated data\n\ |
| 5594 | lengths. Raises OverflowError if length is outside the permissible\n\ |
| 5595 | range of values."); |
| 5596 | #endif /* CMSG_SPACE */ |
| 5597 | #endif /* CMSG_LEN */ |
| 5598 | |
| 5599 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5600 | /* List of functions exported by this module. */ |
| 5601 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5602 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5603 | {"gethostbyname", socket_gethostbyname, |
| 5604 | METH_VARARGS, gethostbyname_doc}, |
| 5605 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 5606 | METH_VARARGS, ghbn_ex_doc}, |
| 5607 | {"gethostbyaddr", socket_gethostbyaddr, |
| 5608 | METH_VARARGS, gethostbyaddr_doc}, |
| 5609 | {"gethostname", socket_gethostname, |
| 5610 | METH_NOARGS, gethostname_doc}, |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 5611 | #ifdef HAVE_SETHOSTNAME |
| 5612 | {"sethostname", socket_sethostname, |
| 5613 | METH_VARARGS, sethostname_doc}, |
| 5614 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5615 | {"getservbyname", socket_getservbyname, |
| 5616 | METH_VARARGS, getservbyname_doc}, |
| 5617 | {"getservbyport", socket_getservbyport, |
| 5618 | METH_VARARGS, getservbyport_doc}, |
| 5619 | {"getprotobyname", socket_getprotobyname, |
| 5620 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5621 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5622 | {"dup", socket_dup, |
| 5623 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5624 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5625 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5626 | {"socketpair", socket_socketpair, |
| 5627 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5628 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5629 | {"ntohs", socket_ntohs, |
| 5630 | METH_VARARGS, ntohs_doc}, |
| 5631 | {"ntohl", socket_ntohl, |
| 5632 | METH_O, ntohl_doc}, |
| 5633 | {"htons", socket_htons, |
| 5634 | METH_VARARGS, htons_doc}, |
| 5635 | {"htonl", socket_htonl, |
| 5636 | METH_O, htonl_doc}, |
| 5637 | {"inet_aton", socket_inet_aton, |
| 5638 | METH_VARARGS, inet_aton_doc}, |
| 5639 | {"inet_ntoa", socket_inet_ntoa, |
| 5640 | METH_VARARGS, inet_ntoa_doc}, |
Atsuo Ishimoto | da0fc14 | 2012-07-16 15:16:54 +0900 | [diff] [blame] | 5641 | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5642 | {"inet_pton", socket_inet_pton, |
| 5643 | METH_VARARGS, inet_pton_doc}, |
| 5644 | {"inet_ntop", socket_inet_ntop, |
| 5645 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5646 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5647 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 5648 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5649 | {"getnameinfo", socket_getnameinfo, |
| 5650 | METH_VARARGS, getnameinfo_doc}, |
| 5651 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 5652 | METH_NOARGS, getdefaulttimeout_doc}, |
| 5653 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 5654 | METH_O, setdefaulttimeout_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5655 | #ifdef HAVE_IF_NAMEINDEX |
| 5656 | {"if_nameindex", socket_if_nameindex, |
| 5657 | METH_NOARGS, if_nameindex_doc}, |
| 5658 | {"if_nametoindex", socket_if_nametoindex, |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5659 | METH_VARARGS, if_nametoindex_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5660 | {"if_indextoname", socket_if_indextoname, |
| 5661 | METH_O, if_indextoname_doc}, |
| 5662 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5663 | #ifdef CMSG_LEN |
| 5664 | {"CMSG_LEN", socket_CMSG_LEN, |
| 5665 | METH_VARARGS, CMSG_LEN_doc}, |
| 5666 | #ifdef CMSG_SPACE |
| 5667 | {"CMSG_SPACE", socket_CMSG_SPACE, |
| 5668 | METH_VARARGS, CMSG_SPACE_doc}, |
| 5669 | #endif |
| 5670 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5671 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5672 | }; |
| 5673 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5674 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5675 | #ifdef MS_WINDOWS |
| 5676 | #define OS_INIT_DEFINED |
| 5677 | |
| 5678 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5679 | |
| 5680 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5681 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5682 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5684 | } |
| 5685 | |
| 5686 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5687 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5689 | WSADATA WSAData; |
| 5690 | int ret; |
| 5691 | ret = WSAStartup(0x0101, &WSAData); |
| 5692 | switch (ret) { |
| 5693 | case 0: /* No error */ |
| 5694 | Py_AtExit(os_cleanup); |
| 5695 | return 1; /* Success */ |
| 5696 | case WSASYSNOTREADY: |
| 5697 | PyErr_SetString(PyExc_ImportError, |
| 5698 | "WSAStartup failed: network not ready"); |
| 5699 | break; |
| 5700 | case WSAVERNOTSUPPORTED: |
| 5701 | case WSAEINVAL: |
| 5702 | PyErr_SetString( |
| 5703 | PyExc_ImportError, |
| 5704 | "WSAStartup failed: requested version not supported"); |
| 5705 | break; |
| 5706 | default: |
| 5707 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 5708 | break; |
| 5709 | } |
| 5710 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 5713 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5714 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5715 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5716 | |
| 5717 | #ifndef OS_INIT_DEFINED |
| 5718 | static int |
| 5719 | os_init(void) |
| 5720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5721 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5722 | } |
| 5723 | #endif |
| 5724 | |
| 5725 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5726 | /* C API table - always add new things to the end for binary |
| 5727 | compatibility. */ |
| 5728 | static |
| 5729 | PySocketModule_APIObject PySocketModuleAPI = |
| 5730 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5731 | &sock_type, |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5732 | NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5733 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5734 | }; |
| 5735 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5736 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5737 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5738 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5739 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5740 | "socket.py" which implements some additional functionality. |
| 5741 | The import of "_socket" may fail with an ImportError exception if |
| 5742 | os-specific initialization fails. On Windows, this does WINSOCK |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 5743 | initialization. When WINSOCK is initialized successfully, a call to |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5744 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5745 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5746 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5747 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5748 | "Implementation module for socket operations.\n\ |
| 5749 | \n\ |
| 5750 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5751 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5752 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | PyModuleDef_HEAD_INIT, |
| 5754 | PySocket_MODULE_NAME, |
| 5755 | socket_doc, |
| 5756 | -1, |
| 5757 | socket_methods, |
| 5758 | NULL, |
| 5759 | NULL, |
| 5760 | NULL, |
| 5761 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5762 | }; |
| 5763 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5764 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5765 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5767 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5769 | if (!os_init()) |
| 5770 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5771 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5772 | #ifdef MS_WINDOWS |
| 5773 | if (support_wsa_no_inherit == -1) { |
| 5774 | DWORD version = GetVersion(); |
| 5775 | DWORD major = (DWORD)LOBYTE(LOWORD(version)); |
| 5776 | DWORD minor = (DWORD)HIBYTE(LOWORD(version)); |
| 5777 | /* need Windows 7 SP1, 2008 R2 SP1 or later */ |
| 5778 | support_wsa_no_inherit = (major >= 6 && minor >= 1); |
| 5779 | } |
| 5780 | #endif |
| 5781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5782 | Py_TYPE(&sock_type) = &PyType_Type; |
| 5783 | m = PyModule_Create(&socketmodule); |
| 5784 | if (m == NULL) |
| 5785 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5786 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5787 | Py_INCREF(PyExc_OSError); |
| 5788 | PySocketModuleAPI.error = PyExc_OSError; |
| 5789 | Py_INCREF(PyExc_OSError); |
| 5790 | PyModule_AddObject(m, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5791 | socket_herror = PyErr_NewException("socket.herror", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5792 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5793 | if (socket_herror == NULL) |
| 5794 | return NULL; |
| 5795 | Py_INCREF(socket_herror); |
| 5796 | PyModule_AddObject(m, "herror", socket_herror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5797 | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5798 | NULL); |
| 5799 | if (socket_gaierror == NULL) |
| 5800 | return NULL; |
| 5801 | Py_INCREF(socket_gaierror); |
| 5802 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 5803 | socket_timeout = PyErr_NewException("socket.timeout", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5804 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5805 | if (socket_timeout == NULL) |
| 5806 | return NULL; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5807 | PySocketModuleAPI.timeout_error = socket_timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | Py_INCREF(socket_timeout); |
| 5809 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 5810 | Py_INCREF((PyObject *)&sock_type); |
| 5811 | if (PyModule_AddObject(m, "SocketType", |
| 5812 | (PyObject *)&sock_type) != 0) |
| 5813 | return NULL; |
| 5814 | Py_INCREF((PyObject *)&sock_type); |
| 5815 | if (PyModule_AddObject(m, "socket", |
| 5816 | (PyObject *)&sock_type) != 0) |
| 5817 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5818 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5819 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5820 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5821 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5823 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | Py_INCREF(has_ipv6); |
| 5825 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5827 | /* Export C API */ |
| 5828 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 5829 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 5830 | ) != 0) |
| 5831 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5833 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5834 | #ifdef AF_UNSPEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5835 | PyModule_AddIntMacro(m, AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5836 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5837 | PyModule_AddIntMacro(m, AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5838 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5839 | PyModule_AddIntMacro(m, AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5840 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 5841 | #if defined(AF_UNIX) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5842 | PyModule_AddIntMacro(m, AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5843 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5844 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5845 | /* Amateur Radio AX.25 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5846 | PyModule_AddIntMacro(m, AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5847 | #endif |
| 5848 | #ifdef AF_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5849 | PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5850 | #endif |
| 5851 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5852 | /* Appletalk DDP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5853 | PyModule_AddIntMacro(m, AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5854 | #endif |
| 5855 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5856 | /* Amateur radio NetROM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5857 | PyModule_AddIntMacro(m, AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5858 | #endif |
| 5859 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5860 | /* Multiprotocol bridge */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5861 | PyModule_AddIntMacro(m, AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5862 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5863 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5864 | /* ATM PVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5865 | PyModule_AddIntMacro(m, AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5866 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5867 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5868 | /* Reserved for Werner's ATM */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5869 | PyModule_AddIntMacro(m, AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5870 | #endif |
| 5871 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5872 | /* Reserved for X.25 project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5873 | PyModule_AddIntMacro(m, AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5874 | #endif |
| 5875 | #ifdef AF_INET6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5876 | PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5877 | #endif |
| 5878 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5879 | /* Amateur Radio X.25 PLP */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5880 | PyModule_AddIntMacro(m, AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5881 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5882 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5883 | /* Reserved for DECnet project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5884 | PyModule_AddIntMacro(m, AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5885 | #endif |
| 5886 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5887 | /* Reserved for 802.2LLC project */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5888 | PyModule_AddIntMacro(m, AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5889 | #endif |
| 5890 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5891 | /* Security callback pseudo AF */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5892 | PyModule_AddIntMacro(m, AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5893 | #endif |
| 5894 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5895 | /* PF_KEY key management API */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5896 | PyModule_AddIntMacro(m, AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5897 | #endif |
| 5898 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5899 | /* */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5900 | PyModule_AddIntMacro(m, AF_NETLINK); |
| 5901 | PyModule_AddIntMacro(m, NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5902 | #ifdef NETLINK_SKIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5903 | PyModule_AddIntMacro(m, NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5904 | #endif |
| 5905 | #ifdef NETLINK_W1 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5906 | PyModule_AddIntMacro(m, NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5907 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5908 | PyModule_AddIntMacro(m, NETLINK_USERSOCK); |
| 5909 | PyModule_AddIntMacro(m, NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5910 | #ifdef NETLINK_TCPDIAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5911 | PyModule_AddIntMacro(m, NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5912 | #endif |
| 5913 | #ifdef NETLINK_NFLOG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5914 | PyModule_AddIntMacro(m, NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5915 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 5916 | #ifdef NETLINK_XFRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5917 | PyModule_AddIntMacro(m, NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 5918 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5919 | #ifdef NETLINK_ARPD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5920 | PyModule_AddIntMacro(m, NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5921 | #endif |
| 5922 | #ifdef NETLINK_ROUTE6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5923 | PyModule_AddIntMacro(m, NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5924 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5925 | PyModule_AddIntMacro(m, NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5926 | #ifdef NETLINK_DNRTMSG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5927 | PyModule_AddIntMacro(m, NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5928 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5929 | #ifdef NETLINK_TAPBASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5930 | PyModule_AddIntMacro(m, NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5931 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5932 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5933 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5934 | /* Alias to emulate 4.4BSD */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5935 | PyModule_AddIntMacro(m, AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5936 | #endif |
Giampaolo Rodola' | 80e1c43 | 2013-05-21 21:02:04 +0200 | [diff] [blame] | 5937 | #ifdef AF_LINK |
| 5938 | PyModule_AddIntMacro(m, AF_LINK); |
| 5939 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5940 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5941 | /* Ash */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5942 | PyModule_AddIntMacro(m, AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5943 | #endif |
| 5944 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5945 | /* Acorn Econet */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5946 | PyModule_AddIntMacro(m, AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5947 | #endif |
| 5948 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5949 | /* ATM SVCs */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5950 | PyModule_AddIntMacro(m, AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5951 | #endif |
| 5952 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5953 | /* Linux SNA Project (nutters!) */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5954 | PyModule_AddIntMacro(m, AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5955 | #endif |
| 5956 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5957 | /* IRDA sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5958 | PyModule_AddIntMacro(m, AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5959 | #endif |
| 5960 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5961 | /* PPPoX sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5962 | PyModule_AddIntMacro(m, AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5963 | #endif |
| 5964 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5965 | /* Wanpipe API Sockets */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5966 | PyModule_AddIntMacro(m, AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5967 | #endif |
| 5968 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | /* Linux LLC */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5970 | PyModule_AddIntMacro(m, AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5971 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 5972 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 5973 | #ifdef USE_BLUETOOTH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5974 | PyModule_AddIntMacro(m, AF_BLUETOOTH); |
| 5975 | PyModule_AddIntMacro(m, BTPROTO_L2CAP); |
| 5976 | PyModule_AddIntMacro(m, BTPROTO_HCI); |
| 5977 | PyModule_AddIntMacro(m, SOL_HCI); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5978 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5979 | PyModule_AddIntMacro(m, HCI_FILTER); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5980 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 5981 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5982 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5983 | PyModule_AddIntMacro(m, HCI_TIME_STAMP); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5984 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5985 | PyModule_AddIntMacro(m, HCI_DATA_DIR); |
| 5986 | PyModule_AddIntMacro(m, BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 5987 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5988 | PyModule_AddIntMacro(m, BTPROTO_RFCOMM); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5989 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 5990 | 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] | 5991 | #endif |
| 5992 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 5993 | #ifdef AF_CAN |
| 5994 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5995 | PyModule_AddIntMacro(m, AF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 5996 | #endif |
| 5997 | #ifdef PF_CAN |
| 5998 | /* Controller Area Network */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 5999 | PyModule_AddIntMacro(m, PF_CAN); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6000 | #endif |
| 6001 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6002 | /* Reliable Datagram Sockets */ |
| 6003 | #ifdef AF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6004 | PyModule_AddIntMacro(m, AF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6005 | #endif |
| 6006 | #ifdef PF_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6007 | PyModule_AddIntMacro(m, PF_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6008 | #endif |
| 6009 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6010 | /* Kernel event messages */ |
| 6011 | #ifdef PF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6012 | PyModule_AddIntMacro(m, PF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6013 | #endif |
| 6014 | #ifdef AF_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6015 | PyModule_AddIntMacro(m, AF_SYSTEM); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6016 | #endif |
| 6017 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 6018 | #ifdef AF_PACKET |
| 6019 | PyModule_AddIntMacro(m, AF_PACKET); |
| 6020 | #endif |
| 6021 | #ifdef PF_PACKET |
| 6022 | PyModule_AddIntMacro(m, PF_PACKET); |
| 6023 | #endif |
| 6024 | #ifdef PACKET_HOST |
| 6025 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 6026 | #endif |
| 6027 | #ifdef PACKET_BROADCAST |
| 6028 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 6029 | #endif |
| 6030 | #ifdef PACKET_MULTICAST |
| 6031 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 6032 | #endif |
| 6033 | #ifdef PACKET_OTHERHOST |
| 6034 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 6035 | #endif |
| 6036 | #ifdef PACKET_OUTGOING |
| 6037 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 6038 | #endif |
| 6039 | #ifdef PACKET_LOOPBACK |
| 6040 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 6041 | #endif |
| 6042 | #ifdef PACKET_FASTROUTE |
| 6043 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 6044 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6045 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6046 | #ifdef HAVE_LINUX_TIPC_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6047 | PyModule_AddIntMacro(m, AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6049 | /* for addresses */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6050 | PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ); |
| 6051 | PyModule_AddIntMacro(m, TIPC_ADDR_NAME); |
| 6052 | PyModule_AddIntMacro(m, TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6053 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6054 | PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE); |
| 6055 | PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE); |
| 6056 | PyModule_AddIntMacro(m, TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6058 | /* for setsockopt() */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6059 | PyModule_AddIntMacro(m, SOL_TIPC); |
| 6060 | PyModule_AddIntMacro(m, TIPC_IMPORTANCE); |
| 6061 | PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE); |
| 6062 | PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE); |
| 6063 | PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6064 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6065 | PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE); |
| 6066 | PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE); |
| 6067 | PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE); |
| 6068 | PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6070 | /* for subscriptions */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6071 | PyModule_AddIntMacro(m, TIPC_SUB_PORTS); |
| 6072 | PyModule_AddIntMacro(m, TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6073 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6074 | /* doesn't seem to be available everywhere */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6075 | PyModule_AddIntMacro(m, TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 6076 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6077 | PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER); |
| 6078 | PyModule_AddIntMacro(m, TIPC_PUBLISHED); |
| 6079 | PyModule_AddIntMacro(m, TIPC_WITHDRAWN); |
| 6080 | PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT); |
| 6081 | PyModule_AddIntMacro(m, TIPC_CFG_SRV); |
| 6082 | PyModule_AddIntMacro(m, TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 6083 | #endif |
| 6084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6085 | /* Socket types */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6086 | PyModule_AddIntMacro(m, SOCK_STREAM); |
| 6087 | PyModule_AddIntMacro(m, SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6088 | /* We have incomplete socket support. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6089 | PyModule_AddIntMacro(m, SOCK_RAW); |
| 6090 | PyModule_AddIntMacro(m, SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 6091 | #if defined(SOCK_RDM) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6092 | PyModule_AddIntMacro(m, SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 6093 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6094 | #ifdef SOCK_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6095 | PyModule_AddIntMacro(m, SOCK_CLOEXEC); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6096 | #endif |
| 6097 | #ifdef SOCK_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6098 | PyModule_AddIntMacro(m, SOCK_NONBLOCK); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 6099 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6101 | #ifdef SO_DEBUG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6102 | PyModule_AddIntMacro(m, SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6103 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6104 | #ifdef SO_ACCEPTCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6105 | PyModule_AddIntMacro(m, SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6106 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6107 | #ifdef SO_REUSEADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6108 | PyModule_AddIntMacro(m, SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6109 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6110 | #ifdef SO_EXCLUSIVEADDRUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6111 | PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 6112 | #endif |
| 6113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6114 | #ifdef SO_KEEPALIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6115 | PyModule_AddIntMacro(m, SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6116 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6117 | #ifdef SO_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6118 | PyModule_AddIntMacro(m, SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6119 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6120 | #ifdef SO_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6121 | PyModule_AddIntMacro(m, SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6122 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6123 | #ifdef SO_USELOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6124 | PyModule_AddIntMacro(m, SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6125 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6126 | #ifdef SO_LINGER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6127 | PyModule_AddIntMacro(m, SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6128 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | #ifdef SO_OOBINLINE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6130 | PyModule_AddIntMacro(m, SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6131 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6132 | #ifdef SO_REUSEPORT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6133 | PyModule_AddIntMacro(m, SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6134 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6135 | #ifdef SO_SNDBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6136 | PyModule_AddIntMacro(m, SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6137 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6138 | #ifdef SO_RCVBUF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6139 | PyModule_AddIntMacro(m, SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6140 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6141 | #ifdef SO_SNDLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6142 | PyModule_AddIntMacro(m, SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6143 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6144 | #ifdef SO_RCVLOWAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6145 | PyModule_AddIntMacro(m, SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6146 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6147 | #ifdef SO_SNDTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6148 | PyModule_AddIntMacro(m, SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6149 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6150 | #ifdef SO_RCVTIMEO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6151 | PyModule_AddIntMacro(m, SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6152 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6153 | #ifdef SO_ERROR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6154 | PyModule_AddIntMacro(m, SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6155 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6156 | #ifdef SO_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6157 | PyModule_AddIntMacro(m, SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6158 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6159 | #ifdef SO_SETFIB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6160 | PyModule_AddIntMacro(m, SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 6161 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6162 | #ifdef SO_PASSCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6163 | PyModule_AddIntMacro(m, SO_PASSCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6164 | #endif |
| 6165 | #ifdef SO_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6166 | PyModule_AddIntMacro(m, SO_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6167 | #endif |
| 6168 | #ifdef LOCAL_PEERCRED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6169 | PyModule_AddIntMacro(m, LOCAL_PEERCRED); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6170 | #endif |
Victor Stinner | cb98bed | 2011-10-25 13:45:48 +0200 | [diff] [blame] | 6171 | #ifdef SO_BINDTODEVICE |
| 6172 | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
| 6173 | #endif |
Benjamin Peterson | 1314ef7 | 2013-11-27 09:18:54 -0600 | [diff] [blame] | 6174 | #ifdef SO_PRIORITY |
| 6175 | PyModule_AddIntMacro(m, SO_PRIORITY); |
| 6176 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6178 | /* Maximum number of connections for "listen" */ |
| 6179 | #ifdef SOMAXCONN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6180 | PyModule_AddIntMacro(m, SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6181 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6182 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6183 | #endif |
| 6184 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6185 | /* Ancilliary message types */ |
| 6186 | #ifdef SCM_RIGHTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6187 | PyModule_AddIntMacro(m, SCM_RIGHTS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6188 | #endif |
| 6189 | #ifdef SCM_CREDENTIALS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6190 | PyModule_AddIntMacro(m, SCM_CREDENTIALS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6191 | #endif |
| 6192 | #ifdef SCM_CREDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6193 | PyModule_AddIntMacro(m, SCM_CREDS); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6194 | #endif |
| 6195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6196 | /* Flags for send, recv */ |
| 6197 | #ifdef MSG_OOB |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6198 | PyModule_AddIntMacro(m, MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6199 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6200 | #ifdef MSG_PEEK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6201 | PyModule_AddIntMacro(m, MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6202 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6203 | #ifdef MSG_DONTROUTE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6204 | PyModule_AddIntMacro(m, MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6205 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6206 | #ifdef MSG_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6207 | PyModule_AddIntMacro(m, MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 6208 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6209 | #ifdef MSG_EOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6210 | PyModule_AddIntMacro(m, MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6211 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6212 | #ifdef MSG_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6213 | PyModule_AddIntMacro(m, MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6214 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6215 | #ifdef MSG_CTRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6216 | PyModule_AddIntMacro(m, MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6217 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6218 | #ifdef MSG_WAITALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6219 | PyModule_AddIntMacro(m, MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6220 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6221 | #ifdef MSG_BTAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6222 | PyModule_AddIntMacro(m, MSG_BTAG); |
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 MSG_ETAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6225 | PyModule_AddIntMacro(m, MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6226 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6227 | #ifdef MSG_NOSIGNAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6228 | PyModule_AddIntMacro(m, MSG_NOSIGNAL); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6229 | #endif |
| 6230 | #ifdef MSG_NOTIFICATION |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6231 | PyModule_AddIntMacro(m, MSG_NOTIFICATION); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6232 | #endif |
| 6233 | #ifdef MSG_CMSG_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6234 | PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6235 | #endif |
| 6236 | #ifdef MSG_ERRQUEUE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6237 | PyModule_AddIntMacro(m, MSG_ERRQUEUE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6238 | #endif |
| 6239 | #ifdef MSG_CONFIRM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6240 | PyModule_AddIntMacro(m, MSG_CONFIRM); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6241 | #endif |
| 6242 | #ifdef MSG_MORE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6243 | PyModule_AddIntMacro(m, MSG_MORE); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6244 | #endif |
| 6245 | #ifdef MSG_EOF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6246 | PyModule_AddIntMacro(m, MSG_EOF); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6247 | #endif |
| 6248 | #ifdef MSG_BCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6249 | PyModule_AddIntMacro(m, MSG_BCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6250 | #endif |
| 6251 | #ifdef MSG_MCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6252 | PyModule_AddIntMacro(m, MSG_MCAST); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6253 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6254 | #ifdef MSG_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6255 | PyModule_AddIntMacro(m, MSG_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6256 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6258 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 6259 | #ifdef SOL_SOCKET |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6260 | PyModule_AddIntMacro(m, SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6261 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6262 | #ifdef SOL_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6263 | PyModule_AddIntMacro(m, SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6264 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6265 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6266 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6267 | #ifdef SOL_IPX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6268 | PyModule_AddIntMacro(m, SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6269 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6270 | #ifdef SOL_AX25 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6271 | PyModule_AddIntMacro(m, SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6272 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6273 | #ifdef SOL_ATALK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6274 | PyModule_AddIntMacro(m, SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6275 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6276 | #ifdef SOL_NETROM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6277 | PyModule_AddIntMacro(m, SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6278 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6279 | #ifdef SOL_ROSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6280 | PyModule_AddIntMacro(m, SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6281 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6282 | #ifdef SOL_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6283 | PyModule_AddIntMacro(m, SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6284 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6285 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6286 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6287 | #ifdef SOL_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6288 | PyModule_AddIntMacro(m, SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6289 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6290 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6291 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6292 | #ifdef SOL_CAN_BASE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6293 | PyModule_AddIntMacro(m, SOL_CAN_BASE); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6294 | #endif |
| 6295 | #ifdef SOL_CAN_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6296 | PyModule_AddIntMacro(m, SOL_CAN_RAW); |
| 6297 | PyModule_AddIntMacro(m, CAN_RAW); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6298 | #endif |
| 6299 | #ifdef HAVE_LINUX_CAN_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6300 | PyModule_AddIntMacro(m, CAN_EFF_FLAG); |
| 6301 | PyModule_AddIntMacro(m, CAN_RTR_FLAG); |
| 6302 | PyModule_AddIntMacro(m, CAN_ERR_FLAG); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6303 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6304 | PyModule_AddIntMacro(m, CAN_SFF_MASK); |
| 6305 | PyModule_AddIntMacro(m, CAN_EFF_MASK); |
| 6306 | PyModule_AddIntMacro(m, CAN_ERR_MASK); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6307 | #endif |
| 6308 | #ifdef HAVE_LINUX_CAN_RAW_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6309 | PyModule_AddIntMacro(m, CAN_RAW_FILTER); |
| 6310 | PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER); |
| 6311 | PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK); |
| 6312 | PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS); |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6313 | #endif |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6314 | #ifdef HAVE_LINUX_CAN_BCM_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6315 | PyModule_AddIntMacro(m, CAN_BCM); |
Charles-François Natali | 773e42d | 2013-02-05 19:42:01 +0100 | [diff] [blame] | 6316 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP); |
| 6317 | PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE); |
| 6318 | PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ); |
| 6319 | PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND); |
| 6320 | PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP); |
| 6321 | PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE); |
| 6322 | PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ); |
| 6323 | PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS); |
| 6324 | PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED); |
| 6325 | PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS); |
| 6326 | PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT); |
| 6327 | PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED); |
| 6328 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6329 | #ifdef SOL_RDS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6330 | PyModule_AddIntMacro(m, SOL_RDS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6331 | #endif |
| 6332 | #ifdef RDS_CANCEL_SENT_TO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6333 | PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6334 | #endif |
| 6335 | #ifdef RDS_GET_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6336 | PyModule_AddIntMacro(m, RDS_GET_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6337 | #endif |
| 6338 | #ifdef RDS_FREE_MR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6339 | PyModule_AddIntMacro(m, RDS_FREE_MR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6340 | #endif |
| 6341 | #ifdef RDS_RECVERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6342 | PyModule_AddIntMacro(m, RDS_RECVERR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6343 | #endif |
| 6344 | #ifdef RDS_CONG_MONITOR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6345 | PyModule_AddIntMacro(m, RDS_CONG_MONITOR); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6346 | #endif |
| 6347 | #ifdef RDS_GET_MR_FOR_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6348 | PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6349 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6350 | #ifdef IPPROTO_IP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6351 | PyModule_AddIntMacro(m, IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6352 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6353 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6354 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6355 | #ifdef IPPROTO_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6356 | PyModule_AddIntMacro(m, IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6357 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6358 | #ifdef IPPROTO_ICMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6359 | PyModule_AddIntMacro(m, IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6360 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6361 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6362 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6363 | #ifdef IPPROTO_IGMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6364 | PyModule_AddIntMacro(m, IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6365 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6366 | #ifdef IPPROTO_GGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6367 | PyModule_AddIntMacro(m, IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6368 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | #ifdef IPPROTO_IPV4 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6370 | PyModule_AddIntMacro(m, IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6371 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6372 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6373 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 6374 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6375 | #ifdef IPPROTO_IPIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6376 | PyModule_AddIntMacro(m, IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6377 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6378 | #ifdef IPPROTO_TCP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6379 | PyModule_AddIntMacro(m, IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6380 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6381 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6382 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6383 | #ifdef IPPROTO_EGP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6384 | PyModule_AddIntMacro(m, IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6385 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | #ifdef IPPROTO_PUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6387 | PyModule_AddIntMacro(m, IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6388 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6389 | #ifdef IPPROTO_UDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6390 | PyModule_AddIntMacro(m, IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6391 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6392 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6393 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6394 | #ifdef IPPROTO_IDP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6395 | PyModule_AddIntMacro(m, IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6396 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6397 | #ifdef IPPROTO_HELLO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6398 | PyModule_AddIntMacro(m, IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6399 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | #ifdef IPPROTO_ND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6401 | PyModule_AddIntMacro(m, IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6402 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6403 | #ifdef IPPROTO_TP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6404 | PyModule_AddIntMacro(m, IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6405 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6406 | #ifdef IPPROTO_IPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6407 | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6408 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6409 | #ifdef IPPROTO_ROUTING |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6410 | PyModule_AddIntMacro(m, IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6411 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6412 | #ifdef IPPROTO_FRAGMENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6413 | PyModule_AddIntMacro(m, IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6414 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6415 | #ifdef IPPROTO_RSVP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6416 | PyModule_AddIntMacro(m, IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6417 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6418 | #ifdef IPPROTO_GRE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6419 | PyModule_AddIntMacro(m, IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6420 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6421 | #ifdef IPPROTO_ESP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6422 | PyModule_AddIntMacro(m, IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6423 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6424 | #ifdef IPPROTO_AH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6425 | PyModule_AddIntMacro(m, IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6426 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6427 | #ifdef IPPROTO_MOBILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6428 | PyModule_AddIntMacro(m, IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6429 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6430 | #ifdef IPPROTO_ICMPV6 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6431 | PyModule_AddIntMacro(m, IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6432 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6433 | #ifdef IPPROTO_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6434 | PyModule_AddIntMacro(m, IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6435 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6436 | #ifdef IPPROTO_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6437 | PyModule_AddIntMacro(m, IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6438 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6439 | #ifdef IPPROTO_XTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6440 | PyModule_AddIntMacro(m, IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6441 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6442 | #ifdef IPPROTO_EON |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6443 | PyModule_AddIntMacro(m, IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6444 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6445 | #ifdef IPPROTO_PIM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6446 | PyModule_AddIntMacro(m, IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6447 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6448 | #ifdef IPPROTO_IPCOMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6449 | PyModule_AddIntMacro(m, IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6450 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6451 | #ifdef IPPROTO_VRRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6452 | PyModule_AddIntMacro(m, IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6453 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6454 | #ifdef IPPROTO_SCTP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6455 | PyModule_AddIntMacro(m, IPPROTO_SCTP); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6456 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6457 | #ifdef IPPROTO_BIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6458 | PyModule_AddIntMacro(m, IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6459 | #endif |
| 6460 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6461 | #ifdef IPPROTO_RAW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6462 | PyModule_AddIntMacro(m, IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6463 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6464 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6465 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6466 | #ifdef IPPROTO_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6467 | PyModule_AddIntMacro(m, IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6468 | #endif |
| 6469 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6470 | #ifdef SYSPROTO_CONTROL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6471 | PyModule_AddIntMacro(m, SYSPROTO_CONTROL); |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6472 | #endif |
| 6473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6474 | /* Some port configuration */ |
| 6475 | #ifdef IPPORT_RESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6476 | PyModule_AddIntMacro(m, IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6477 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6478 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6479 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6480 | #ifdef IPPORT_USERRESERVED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6481 | PyModule_AddIntMacro(m, IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6482 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6483 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6484 | #endif |
| 6485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6486 | /* Some reserved IP v.4 addresses */ |
| 6487 | #ifdef INADDR_ANY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6488 | PyModule_AddIntMacro(m, INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6489 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6490 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6491 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6492 | #ifdef INADDR_BROADCAST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6493 | PyModule_AddIntMacro(m, INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6494 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6495 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6496 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6497 | #ifdef INADDR_LOOPBACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6498 | PyModule_AddIntMacro(m, INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6499 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6500 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6501 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6502 | #ifdef INADDR_UNSPEC_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6503 | PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6504 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6505 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6506 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6507 | #ifdef INADDR_ALLHOSTS_GROUP |
| 6508 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 6509 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6510 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6511 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6512 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6513 | #ifdef INADDR_MAX_LOCAL_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6514 | PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6515 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6516 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6517 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6518 | #ifdef INADDR_NONE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6519 | PyModule_AddIntMacro(m, INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6520 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6521 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6522 | #endif |
| 6523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6524 | /* IPv4 [gs]etsockopt options */ |
| 6525 | #ifdef IP_OPTIONS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6526 | PyModule_AddIntMacro(m, IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6527 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6528 | #ifdef IP_HDRINCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6529 | PyModule_AddIntMacro(m, IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6530 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6531 | #ifdef IP_TOS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6532 | PyModule_AddIntMacro(m, IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6533 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6534 | #ifdef IP_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6535 | PyModule_AddIntMacro(m, IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6536 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6537 | #ifdef IP_RECVOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6538 | PyModule_AddIntMacro(m, IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6539 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6540 | #ifdef IP_RECVRETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6541 | PyModule_AddIntMacro(m, IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6542 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6543 | #ifdef IP_RECVDSTADDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6544 | PyModule_AddIntMacro(m, IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6545 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6546 | #ifdef IP_RETOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6547 | PyModule_AddIntMacro(m, IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6548 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6549 | #ifdef IP_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6550 | PyModule_AddIntMacro(m, IP_MULTICAST_IF); |
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 IP_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6553 | PyModule_AddIntMacro(m, IP_MULTICAST_TTL); |
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 IP_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6556 | PyModule_AddIntMacro(m, IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6557 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6558 | #ifdef IP_ADD_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6559 | PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6560 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6561 | #ifdef IP_DROP_MEMBERSHIP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6562 | PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6563 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6564 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6565 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6566 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6567 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6568 | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6569 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6570 | #ifdef IP_MAX_MEMBERSHIPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6571 | PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6572 | #endif |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6573 | #ifdef IP_TRANSPARENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6574 | PyModule_AddIntMacro(m, IP_TRANSPARENT); |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6575 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6577 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 6578 | #ifdef IPV6_JOIN_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6579 | PyModule_AddIntMacro(m, IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6580 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6581 | #ifdef IPV6_LEAVE_GROUP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6582 | PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6583 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6584 | #ifdef IPV6_MULTICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6585 | PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6586 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6587 | #ifdef IPV6_MULTICAST_IF |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6588 | PyModule_AddIntMacro(m, IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6589 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6590 | #ifdef IPV6_MULTICAST_LOOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6591 | PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6592 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | #ifdef IPV6_UNICAST_HOPS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6594 | PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6595 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6596 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6597 | #ifdef IPV6_V6ONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6598 | PyModule_AddIntMacro(m, IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6599 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6600 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6601 | #ifdef IPV6_CHECKSUM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6602 | PyModule_AddIntMacro(m, IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6603 | #endif |
| 6604 | #ifdef IPV6_DONTFRAG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6605 | PyModule_AddIntMacro(m, IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6606 | #endif |
| 6607 | #ifdef IPV6_DSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6608 | PyModule_AddIntMacro(m, IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6609 | #endif |
| 6610 | #ifdef IPV6_HOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6611 | PyModule_AddIntMacro(m, IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6612 | #endif |
| 6613 | #ifdef IPV6_HOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6614 | PyModule_AddIntMacro(m, IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6615 | #endif |
| 6616 | #ifdef IPV6_NEXTHOP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6617 | PyModule_AddIntMacro(m, IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6618 | #endif |
| 6619 | #ifdef IPV6_PATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6620 | PyModule_AddIntMacro(m, IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6621 | #endif |
| 6622 | #ifdef IPV6_PKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6623 | PyModule_AddIntMacro(m, IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6624 | #endif |
| 6625 | #ifdef IPV6_RECVDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6626 | PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6627 | #endif |
| 6628 | #ifdef IPV6_RECVHOPLIMIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6629 | PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6630 | #endif |
| 6631 | #ifdef IPV6_RECVHOPOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6632 | PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6633 | #endif |
| 6634 | #ifdef IPV6_RECVPKTINFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6635 | PyModule_AddIntMacro(m, IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6636 | #endif |
| 6637 | #ifdef IPV6_RECVRTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6638 | PyModule_AddIntMacro(m, IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6639 | #endif |
| 6640 | #ifdef IPV6_RECVTCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6641 | PyModule_AddIntMacro(m, IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6642 | #endif |
| 6643 | #ifdef IPV6_RTHDR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6644 | PyModule_AddIntMacro(m, IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6645 | #endif |
| 6646 | #ifdef IPV6_RTHDRDSTOPTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6647 | PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6648 | #endif |
| 6649 | #ifdef IPV6_RTHDR_TYPE_0 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6650 | PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6651 | #endif |
| 6652 | #ifdef IPV6_RECVPATHMTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6653 | PyModule_AddIntMacro(m, IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6654 | #endif |
| 6655 | #ifdef IPV6_TCLASS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6656 | PyModule_AddIntMacro(m, IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6657 | #endif |
| 6658 | #ifdef IPV6_USE_MIN_MTU |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6659 | PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6660 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6662 | /* TCP options */ |
| 6663 | #ifdef TCP_NODELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6664 | PyModule_AddIntMacro(m, TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6665 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6666 | #ifdef TCP_MAXSEG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6667 | PyModule_AddIntMacro(m, TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6668 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6669 | #ifdef TCP_CORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6670 | PyModule_AddIntMacro(m, TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6671 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6672 | #ifdef TCP_KEEPIDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6673 | PyModule_AddIntMacro(m, TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6674 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6675 | #ifdef TCP_KEEPINTVL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6676 | PyModule_AddIntMacro(m, TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6677 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6678 | #ifdef TCP_KEEPCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6679 | PyModule_AddIntMacro(m, TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6680 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6681 | #ifdef TCP_SYNCNT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6682 | PyModule_AddIntMacro(m, TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6683 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6684 | #ifdef TCP_LINGER2 |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6685 | PyModule_AddIntMacro(m, TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6686 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6687 | #ifdef TCP_DEFER_ACCEPT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6688 | PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6689 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6690 | #ifdef TCP_WINDOW_CLAMP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6691 | PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6692 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6693 | #ifdef TCP_INFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6694 | PyModule_AddIntMacro(m, TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6695 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6696 | #ifdef TCP_QUICKACK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6697 | PyModule_AddIntMacro(m, TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6698 | #endif |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6699 | #ifdef TCP_FASTOPEN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6700 | PyModule_AddIntMacro(m, TCP_FASTOPEN); |
Benjamin Peterson | 57b6672 | 2012-12-12 22:24:47 -0500 | [diff] [blame] | 6701 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6703 | /* IPX options */ |
| 6704 | #ifdef IPX_TYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6705 | PyModule_AddIntMacro(m, IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6706 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6707 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6708 | /* Reliable Datagram Sockets */ |
| 6709 | #ifdef RDS_CMSG_RDMA_ARGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6710 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6711 | #endif |
| 6712 | #ifdef RDS_CMSG_RDMA_DEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6713 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6714 | #endif |
| 6715 | #ifdef RDS_CMSG_RDMA_MAP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6716 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6717 | #endif |
| 6718 | #ifdef RDS_CMSG_RDMA_STATUS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6719 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6720 | #endif |
| 6721 | #ifdef RDS_CMSG_RDMA_UPDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6722 | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6723 | #endif |
| 6724 | #ifdef RDS_RDMA_READWRITE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6725 | PyModule_AddIntMacro(m, RDS_RDMA_READWRITE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6726 | #endif |
| 6727 | #ifdef RDS_RDMA_FENCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6728 | PyModule_AddIntMacro(m, RDS_RDMA_FENCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6729 | #endif |
| 6730 | #ifdef RDS_RDMA_INVALIDATE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6731 | PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6732 | #endif |
| 6733 | #ifdef RDS_RDMA_USE_ONCE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6734 | PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6735 | #endif |
| 6736 | #ifdef RDS_RDMA_DONTWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6737 | PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6738 | #endif |
| 6739 | #ifdef RDS_RDMA_NOTIFY_ME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6740 | PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6741 | #endif |
| 6742 | #ifdef RDS_RDMA_SILENT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6743 | PyModule_AddIntMacro(m, RDS_RDMA_SILENT); |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6744 | #endif |
| 6745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6746 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6747 | #ifdef EAI_ADDRFAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6748 | PyModule_AddIntMacro(m, EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6749 | #endif |
| 6750 | #ifdef EAI_AGAIN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6751 | PyModule_AddIntMacro(m, EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6752 | #endif |
| 6753 | #ifdef EAI_BADFLAGS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6754 | PyModule_AddIntMacro(m, EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6755 | #endif |
| 6756 | #ifdef EAI_FAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6757 | PyModule_AddIntMacro(m, EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6758 | #endif |
| 6759 | #ifdef EAI_FAMILY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6760 | PyModule_AddIntMacro(m, EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6761 | #endif |
| 6762 | #ifdef EAI_MEMORY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6763 | PyModule_AddIntMacro(m, EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6764 | #endif |
| 6765 | #ifdef EAI_NODATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6766 | PyModule_AddIntMacro(m, EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6767 | #endif |
| 6768 | #ifdef EAI_NONAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6769 | PyModule_AddIntMacro(m, EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6770 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6771 | #ifdef EAI_OVERFLOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6772 | PyModule_AddIntMacro(m, EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6773 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6774 | #ifdef EAI_SERVICE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6775 | PyModule_AddIntMacro(m, EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6776 | #endif |
| 6777 | #ifdef EAI_SOCKTYPE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6778 | PyModule_AddIntMacro(m, EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6779 | #endif |
| 6780 | #ifdef EAI_SYSTEM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6781 | PyModule_AddIntMacro(m, EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6782 | #endif |
| 6783 | #ifdef EAI_BADHINTS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6784 | PyModule_AddIntMacro(m, EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6785 | #endif |
| 6786 | #ifdef EAI_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6787 | PyModule_AddIntMacro(m, EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6788 | #endif |
| 6789 | #ifdef EAI_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6790 | PyModule_AddIntMacro(m, EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6791 | #endif |
| 6792 | #ifdef AI_PASSIVE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6793 | PyModule_AddIntMacro(m, AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6794 | #endif |
| 6795 | #ifdef AI_CANONNAME |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6796 | PyModule_AddIntMacro(m, AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6797 | #endif |
| 6798 | #ifdef AI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6799 | PyModule_AddIntMacro(m, AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6800 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6801 | #ifdef AI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6802 | PyModule_AddIntMacro(m, AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6803 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6804 | #ifdef AI_MASK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6805 | PyModule_AddIntMacro(m, AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6806 | #endif |
| 6807 | #ifdef AI_ALL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6808 | PyModule_AddIntMacro(m, AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6809 | #endif |
| 6810 | #ifdef AI_V4MAPPED_CFG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6811 | PyModule_AddIntMacro(m, AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6812 | #endif |
| 6813 | #ifdef AI_ADDRCONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6814 | PyModule_AddIntMacro(m, AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6815 | #endif |
| 6816 | #ifdef AI_V4MAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6817 | PyModule_AddIntMacro(m, AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6818 | #endif |
| 6819 | #ifdef AI_DEFAULT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6820 | PyModule_AddIntMacro(m, AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6821 | #endif |
| 6822 | #ifdef NI_MAXHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6823 | PyModule_AddIntMacro(m, NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6824 | #endif |
| 6825 | #ifdef NI_MAXSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6826 | PyModule_AddIntMacro(m, NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6827 | #endif |
| 6828 | #ifdef NI_NOFQDN |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6829 | PyModule_AddIntMacro(m, NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6830 | #endif |
| 6831 | #ifdef NI_NUMERICHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6832 | PyModule_AddIntMacro(m, NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6833 | #endif |
| 6834 | #ifdef NI_NAMEREQD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6835 | PyModule_AddIntMacro(m, NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6836 | #endif |
| 6837 | #ifdef NI_NUMERICSERV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6838 | PyModule_AddIntMacro(m, NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6839 | #endif |
| 6840 | #ifdef NI_DGRAM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6841 | PyModule_AddIntMacro(m, NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6842 | #endif |
| 6843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6844 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6845 | #ifdef SHUT_RD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6846 | PyModule_AddIntMacro(m, SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6847 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6848 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6849 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6850 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6851 | #endif |
| 6852 | #ifdef SHUT_WR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6853 | PyModule_AddIntMacro(m, SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6854 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6855 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6856 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6857 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6858 | #endif |
| 6859 | #ifdef SHUT_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6860 | PyModule_AddIntMacro(m, SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6861 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6862 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6863 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6864 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6865 | #endif |
| 6866 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6867 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6868 | { |
| 6869 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 6870 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 6871 | int i; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6872 | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6873 | PyObject *tmp; |
| 6874 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 6875 | if (tmp == NULL) |
| 6876 | return NULL; |
| 6877 | PyModule_AddObject(m, names[i], tmp); |
| 6878 | } |
| 6879 | } |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6880 | PyModule_AddIntMacro(m, RCVALL_OFF); |
| 6881 | PyModule_AddIntMacro(m, RCVALL_ON); |
| 6882 | PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6883 | #ifdef RCVALL_IPLEVEL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6884 | PyModule_AddIntMacro(m, RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6885 | #endif |
| 6886 | #ifdef RCVALL_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 6887 | PyModule_AddIntMacro(m, RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6888 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6889 | #endif /* _MSTCPIP_ */ |
| 6890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6891 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 6892 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6893 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6894 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6895 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 6896 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6897 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6898 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6899 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 6900 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6901 | |
| 6902 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6903 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6904 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 6905 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6906 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6908 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 6909 | #if (SIZEOF_INT != 4) |
| 6910 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 6911 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6912 | unsigned int packed_addr; |
| 6913 | packed_addr = inet_addr(src); |
| 6914 | if (packed_addr == INADDR_NONE) |
| 6915 | return 0; |
| 6916 | memcpy(dst, &packed_addr, 4); |
| 6917 | return 1; |
| 6918 | } |
| 6919 | /* Should set errno to EAFNOSUPPORT */ |
| 6920 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 6923 | const char * |
| 6924 | 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] | 6925 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6926 | if (af == AF_INET) { |
| 6927 | struct in_addr packed_addr; |
| 6928 | if (size < 16) |
| 6929 | /* Should set errno to ENOSPC. */ |
| 6930 | return NULL; |
| 6931 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 6932 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 6933 | } |
| 6934 | /* Should set errno to EAFNOSUPPORT */ |
| 6935 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6936 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6937 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6938 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 6939 | #endif |