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 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 18 | - socket.error: exception raised for socket specific errors |
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 | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 98 | #undef MAX |
| 99 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 100 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 101 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 102 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 103 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 104 | \n\ |
| 105 | Open a socket of the given type. The family argument specifies the\n\ |
| 106 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 107 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 108 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 109 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 110 | \n\ |
| 111 | A socket object represents one endpoint of a network connection.\n\ |
| 112 | \n\ |
| 113 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 114 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 115 | _accept() -- accept connection, returning new socket fd and client address\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 116 | bind(addr) -- bind the socket to a local address\n\ |
| 117 | close() -- close the socket\n\ |
| 118 | connect(addr) -- connect the socket to a remote address\n\ |
| 119 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 120 | _dup() -- return a new socket fd duplicated from fileno()\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 121 | fileno() -- return underlying file descriptor\n\ |
| 122 | getpeername() -- return remote address [*]\n\ |
| 123 | getsockname() -- return local address\n\ |
| 124 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 125 | gettimeout() -- return timeout or None\n\ |
| 126 | listen(n) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 127 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 128 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 130 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 131 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 132 | sendall(data[, flags]) -- send all data\n\ |
| 133 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 134 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 135 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 136 | setsockopt(level, optname, value) -- set socket options\n\ |
| 137 | settimeout(None | float) -- set or clear the timeout\n\ |
| 138 | shutdown(how) -- shut down traffic in one or both directions\n\ |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 139 | if_nameindex() -- return all network interface indices and names\n\ |
Gregory P. Smith | b6471db | 2011-05-22 22:47:55 -0700 | [diff] [blame] | 140 | if_nametoindex(name) -- return the corresponding interface index\n\ |
| 141 | if_indextoname(index) -- return the corresponding interface name\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 142 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 143 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 144 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 145 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 146 | I hope some day someone can clean this up please... */ |
| 147 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 148 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 149 | script doesn't get this right, so we hardcode some platform checks below. |
| 150 | On the other hand, not all Linux versions agree, so there the settings |
| 151 | computed by the configure script are needed! */ |
| 152 | |
| 153 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 154 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 155 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 156 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 157 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 158 | |
Victor Stinner | 710d27e | 2011-08-23 10:57:32 +0200 | [diff] [blame] | 159 | #if defined(__OpenBSD__) |
| 160 | # include <sys/uio.h> |
| 161 | #endif |
| 162 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 163 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 164 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 167 | #ifdef HAVE_GETHOSTBYNAME_R |
Jesus Cea | 736e7fc | 2011-03-14 17:36:54 +0100 | [diff] [blame] | 168 | # if defined(_AIX) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 169 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 170 | # elif defined(__sun) || defined(__sgi) |
| 171 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 172 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 173 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 174 | # else |
| 175 | # undef HAVE_GETHOSTBYNAME_R |
| 176 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 177 | #endif |
| 178 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 179 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 180 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 181 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 182 | #endif |
| 183 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 184 | /* To use __FreeBSD_version */ |
| 185 | #ifdef HAVE_SYS_PARAM_H |
| 186 | #include <sys/param.h> |
| 187 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 188 | /* 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] | 189 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 190 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 191 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 192 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 193 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 194 | #define USE_GETADDRINFO_LOCK |
| 195 | #endif |
| 196 | |
| 197 | #ifdef USE_GETADDRINFO_LOCK |
| 198 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 199 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 200 | #else |
| 201 | #define ACQUIRE_GETADDRINFO_LOCK |
| 202 | #define RELEASE_GETADDRINFO_LOCK |
| 203 | #endif |
| 204 | |
| 205 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 206 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 207 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 209 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 210 | # include <types.h> |
| 211 | # include <io.h> |
| 212 | # include <sys/ioctl.h> |
| 213 | # include <utils.h> |
| 214 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 215 | #endif |
| 216 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 217 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 218 | # include <ioctl.h> |
| 219 | #endif |
| 220 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 221 | #ifdef __APPLE__ |
| 222 | # include <sys/ioctl.h> |
| 223 | #endif |
| 224 | |
| 225 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 226 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 227 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 228 | functions are declared correctly if compiling with |
| 229 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 230 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 231 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 232 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 233 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 234 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 235 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 236 | #undef _XOPEN_SOURCE |
| 237 | #include <sys/socket.h> |
| 238 | #include <sys/types.h> |
| 239 | #include <netinet/in.h> |
| 240 | #ifdef _SS_ALIGNSIZE |
| 241 | #define HAVE_GETADDRINFO 1 |
| 242 | #define HAVE_GETNAMEINFO 1 |
| 243 | #endif |
| 244 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 245 | #define HAVE_INET_PTON |
| 246 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 247 | #endif |
| 248 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 249 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 250 | 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] | 251 | are just busted. Same thing for Solaris. */ |
| 252 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 253 | #define INET_ADDRSTRLEN 16 |
| 254 | #endif |
| 255 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 256 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 257 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 259 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 260 | |
Gregory P. Smith | 3b1f2c3 | 2011-05-15 12:18:23 -0700 | [diff] [blame] | 261 | #ifdef HAVE_SYS_SOCKET_H |
| 262 | #include <sys/socket.h> |
| 263 | #endif |
| 264 | |
| 265 | #ifdef HAVE_NET_IF_H |
| 266 | #include <net/if.h> |
| 267 | #endif |
| 268 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 269 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 270 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 271 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 272 | |
| 273 | /* Addressing includes */ |
| 274 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 275 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 276 | |
| 277 | /* Non-MS WINDOWS includes */ |
| 278 | # include <netdb.h> |
Nick Coghlan | 4ecf6cf | 2011-08-22 15:45:19 +1000 | [diff] [blame] | 279 | # include <unistd.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 281 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | # include <arpa/inet.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 283 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 284 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 286 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 287 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 288 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 289 | # ifdef HAVE_FCNTL_H |
| 290 | # include <fcntl.h> |
| 291 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 292 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 293 | #endif |
| 294 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 295 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 296 | |
| 297 | #ifndef offsetof |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 299 | #endif |
| 300 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 301 | #ifndef O_NONBLOCK |
| 302 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 303 | #endif |
| 304 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 305 | /* include Python's addrinfo.h unless it causes trouble */ |
| 306 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 307 | /* Do not include addinfo.h on some newer IRIX versions. |
| 308 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 309 | * for example, but not by 6.5.10. |
| 310 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 311 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 312 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 313 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 314 | */ |
| 315 | #else |
| 316 | # include "addrinfo.h" |
| 317 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 318 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 319 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 320 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 321 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 322 | 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] | 323 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 324 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 325 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 326 | #ifdef __APPLE__ |
| 327 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 328 | failure, so we must use the emulation instead of the libinfo |
| 329 | implementation. Unfortunately, performing an autoconf test |
| 330 | for this bug would require DNS access for the machine performing |
| 331 | the configuration, which is not acceptable. Therefore, we |
| 332 | determine the bug just by checking for __APPLE__. If this bug |
| 333 | gets ever fixed, perhaps checking for sys/version.h would be |
| 334 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 335 | #ifndef HAVE_GETNAMEINFO |
| 336 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 337 | Find to check for Jaguar is that it has getnameinfo(), which |
| 338 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 339 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 340 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 341 | |
| 342 | #ifdef HAVE_INET_ATON |
| 343 | #define USE_INET_ATON_WEAKLINK |
| 344 | #endif |
| 345 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 346 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 347 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 348 | /* 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] | 349 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 350 | /* avoid clashes with the C library definition of the symbol. */ |
| 351 | #define getaddrinfo fake_getaddrinfo |
| 352 | #define gai_strerror fake_gai_strerror |
| 353 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 354 | #include "getaddrinfo.c" |
| 355 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 356 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 357 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 358 | #include "getnameinfo.c" |
| 359 | #endif |
| 360 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 361 | #ifdef MS_WINDOWS |
| 362 | /* On Windows a socket is really a handle not an fd */ |
| 363 | static SOCKET |
| 364 | dup_socket(SOCKET handle) |
| 365 | { |
Daniel Stutzbach | 6c76528 | 2010-09-03 12:38:33 +0000 | [diff] [blame] | 366 | WSAPROTOCOL_INFO info; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 367 | |
Daniel Stutzbach | 6c76528 | 2010-09-03 12:38:33 +0000 | [diff] [blame] | 368 | if (WSADuplicateSocket(handle, GetCurrentProcessId(), &info)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | return INVALID_SOCKET; |
Daniel Stutzbach | 6c76528 | 2010-09-03 12:38:33 +0000 | [diff] [blame] | 370 | |
| 371 | return WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
Antoine Pitrou | 9a54a26 | 2012-04-01 01:14:39 +0200 | [diff] [blame] | 372 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 373 | } |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 374 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 375 | #else |
| 376 | /* On Unix we can use dup to duplicate the file descriptor of a socket*/ |
| 377 | #define dup_socket(fd) dup(fd) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 378 | #endif |
| 379 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 380 | #ifdef MS_WIN32 |
Kristján Valur Jónsson | 310052c | 2012-03-31 13:35:00 +0000 | [diff] [blame] | 381 | #undef EAFNOSUPPORT |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 382 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 383 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 384 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 386 | #ifndef SOCKETCLOSE |
| 387 | #define SOCKETCLOSE close |
| 388 | #endif |
| 389 | |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 390 | #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] | 391 | #define USE_BLUETOOTH 1 |
| 392 | #if defined(__FreeBSD__) |
| 393 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 394 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 395 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 396 | #define SOL_HCI SOL_HCI_RAW |
| 397 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 398 | #define sockaddr_l2 sockaddr_l2cap |
| 399 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 400 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 401 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 402 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 403 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 404 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 405 | #define sockaddr_l2 sockaddr_bt |
| 406 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 407 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 408 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 409 | #define SOL_HCI BTPROTO_HCI |
| 410 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 411 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 412 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 413 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 414 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 415 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 416 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 417 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 418 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 419 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 420 | #endif |
| 421 | #endif |
| 422 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 423 | #ifdef __VMS |
| 424 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 425 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 426 | #endif |
| 427 | |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 428 | /* Convert "sock_addr_t *" to "struct sockaddr *". */ |
| 429 | #define SAS2SA(x) (&((x)->sa)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 430 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 431 | /* |
| 432 | * Constants for getnameinfo() |
| 433 | */ |
| 434 | #if !defined(NI_MAXHOST) |
| 435 | #define NI_MAXHOST 1025 |
| 436 | #endif |
| 437 | #if !defined(NI_MAXSERV) |
| 438 | #define NI_MAXSERV 32 |
| 439 | #endif |
| 440 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 441 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 442 | #define INVALID_SOCKET (-1) |
| 443 | #endif |
| 444 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 445 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 446 | a Py prefix (or use CapitalizedWords). Later... */ |
| 447 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 448 | /* Global variable holding the exception type for errors detected |
| 449 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 450 | static PyObject *socket_herror; |
| 451 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 452 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 453 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 454 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 455 | The sock_type variable contains pointers to various functions, |
| 456 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 457 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 458 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 459 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 460 | #if defined(HAVE_POLL_H) |
| 461 | #include <poll.h> |
| 462 | #elif defined(HAVE_SYS_POLL_H) |
| 463 | #include <sys/poll.h> |
| 464 | #endif |
| 465 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 466 | /* Largest value to try to store in a socklen_t (used when handling |
| 467 | ancillary data). POSIX requires socklen_t to hold at least |
| 468 | (2**31)-1 and recommends against storing larger values, but |
| 469 | socklen_t was originally int in the BSD interface, so to be on the |
| 470 | safe side we use the smaller of (2**31)-1 and INT_MAX. */ |
| 471 | #if INT_MAX > 0x7fffffff |
| 472 | #define SOCKLEN_T_LIMIT 0x7fffffff |
| 473 | #else |
| 474 | #define SOCKLEN_T_LIMIT INT_MAX |
| 475 | #endif |
| 476 | |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 477 | #ifdef HAVE_POLL |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 478 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 479 | #define IS_SELECTABLE(s) 1 |
| 480 | /* 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] | 481 | #else |
Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 482 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 483 | * little white lie. */ |
| 484 | #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] | 485 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 486 | |
| 487 | static PyObject* |
| 488 | select_error(void) |
| 489 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 490 | PyErr_SetString(PyExc_OSError, "unable to select on socket"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 494 | #ifdef MS_WINDOWS |
Antoine Pitrou | c8f0196 | 2010-09-28 22:03:27 +0000 | [diff] [blame] | 495 | #ifndef WSAEAGAIN |
| 496 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 497 | #endif |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 498 | #define CHECK_ERRNO(expected) \ |
| 499 | (WSAGetLastError() == WSA ## expected) |
| 500 | #else |
| 501 | #define CHECK_ERRNO(expected) \ |
| 502 | (errno == expected) |
| 503 | #endif |
| 504 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 505 | /* Convenience function to raise an error according to errno |
| 506 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 507 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 508 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 509 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 510 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 511 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | int err_no = WSAGetLastError(); |
| 513 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 514 | recognizes the error codes used by both GetLastError() and |
| 515 | WSAGetLastError */ |
| 516 | if (err_no) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 517 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 518 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 520 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 523 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 524 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 525 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 526 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 528 | |
| 529 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 531 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 533 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | if (v != NULL) { |
| 535 | PyErr_SetObject(socket_herror, v); |
| 536 | Py_DECREF(v); |
| 537 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
| 543 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 544 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 547 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 548 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 550 | if (error == EAI_SYSTEM) |
| 551 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 552 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 553 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 554 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 556 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 558 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | if (v != NULL) { |
| 560 | PyErr_SetObject(socket_gaierror, v); |
| 561 | Py_DECREF(v); |
| 562 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 567 | #ifdef __VMS |
| 568 | /* Function to send in segments */ |
| 569 | static int |
| 570 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | int n = 0; |
| 573 | int remaining = len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | while (remaining > 0) { |
| 576 | unsigned int segment; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 579 | n = send(sock_fd, buf, segment, flags); |
| 580 | if (n < 0) { |
| 581 | return n; |
| 582 | } |
| 583 | remaining -= segment; |
| 584 | buf += segment; |
| 585 | } /* end while */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | return len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 588 | } |
| 589 | #endif |
| 590 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 591 | /* Function to perform the setting of socket blocking mode |
| 592 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 593 | static int |
| 594 | internal_setblocking(PySocketSockObject *s, int block) |
| 595 | { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 596 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 598 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 599 | #ifdef SOCK_NONBLOCK |
| 600 | if (block) |
| 601 | s->sock_type &= (~SOCK_NONBLOCK); |
| 602 | else |
| 603 | s->sock_type |= SOCK_NONBLOCK; |
| 604 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 607 | #ifndef MS_WINDOWS |
Jesus Cea | 14c81ab | 2012-10-05 02:11:36 +0200 | [diff] [blame] | 608 | #if defined(__VMS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | block = !block; |
| 610 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Jesus Cea | 14c81ab | 2012-10-05 02:11:36 +0200 | [diff] [blame] | 611 | #else /* !__VMS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 613 | if (block) |
| 614 | delay_flag &= (~O_NONBLOCK); |
| 615 | else |
| 616 | delay_flag |= O_NONBLOCK; |
| 617 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Jesus Cea | 14c81ab | 2012-10-05 02:11:36 +0200 | [diff] [blame] | 618 | #endif /* !__VMS */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 619 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | block = !block; |
| 621 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 622 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | /* Since these don't return anything */ |
| 626 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 629 | /* 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] | 630 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 631 | This does not raise an exception; we'll let our caller do that |
| 632 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 633 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 634 | static int |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 635 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 636 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 640 | if (s->sock_timeout <= 0.0) |
| 641 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | /* Guard against closed socket */ |
| 644 | if (s->sock_fd < 0) |
| 645 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 647 | /* Handling this condition here simplifies the select loops */ |
| 648 | if (interval < 0.0) |
| 649 | return 1; |
| 650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | /* Prefer poll, if available, since you can poll() any fd |
| 652 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 653 | #ifdef HAVE_POLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | { |
| 655 | struct pollfd pollfd; |
| 656 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | pollfd.fd = s->sock_fd; |
| 659 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | /* s->sock_timeout is in seconds, timeout in ms */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 662 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | n = poll(&pollfd, 1, timeout); |
| 664 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 665 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | { |
| 667 | /* Construct the arguments to select */ |
| 668 | fd_set fds; |
| 669 | struct timeval tv; |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 670 | tv.tv_sec = (int)interval; |
| 671 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | FD_ZERO(&fds); |
| 673 | FD_SET(s->sock_fd, &fds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | /* See if the socket is ready */ |
| 676 | if (writing) |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 677 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 678 | NULL, &fds, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | else |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 680 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 681 | &fds, NULL, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 683 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | if (n < 0) |
| 686 | return -1; |
| 687 | if (n == 0) |
| 688 | return 1; |
| 689 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 692 | static int |
| 693 | internal_select(PySocketSockObject *s, int writing) |
| 694 | { |
| 695 | return internal_select_ex(s, writing, s->sock_timeout); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | Two macros for automatic retry of select() in case of false positives |
| 700 | (for example, select() could indicate a socket is ready for reading |
| 701 | but the data then discarded by the OS because of a wrong checksum). |
| 702 | Here is an example of use: |
| 703 | |
| 704 | BEGIN_SELECT_LOOP(s) |
| 705 | Py_BEGIN_ALLOW_THREADS |
| 706 | timeout = internal_select_ex(s, 0, interval); |
| 707 | if (!timeout) |
| 708 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 709 | Py_END_ALLOW_THREADS |
| 710 | if (timeout == 1) { |
| 711 | PyErr_SetString(socket_timeout, "timed out"); |
| 712 | return -1; |
| 713 | } |
| 714 | END_SELECT_LOOP(s) |
| 715 | */ |
| 716 | |
| 717 | #define BEGIN_SELECT_LOOP(s) \ |
| 718 | { \ |
| 719 | _PyTime_timeval now, deadline = {0, 0}; \ |
| 720 | double interval = s->sock_timeout; \ |
| 721 | int has_timeout = s->sock_timeout > 0.0; \ |
| 722 | if (has_timeout) { \ |
| 723 | _PyTime_gettimeofday(&now); \ |
| 724 | deadline = now; \ |
| 725 | _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \ |
| 726 | } \ |
| 727 | while (1) { \ |
| 728 | errno = 0; \ |
| 729 | |
| 730 | #define END_SELECT_LOOP(s) \ |
| 731 | if (!has_timeout || \ |
| 732 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 733 | break; \ |
| 734 | _PyTime_gettimeofday(&now); \ |
| 735 | interval = _PyTime_INTERVAL(now, deadline); \ |
| 736 | } \ |
| 737 | } \ |
| 738 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 739 | /* Initialize a new socket object. */ |
| 740 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 741 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 742 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 743 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 744 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 746 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | s->sock_fd = fd; |
| 748 | s->sock_family = family; |
| 749 | s->sock_type = type; |
| 750 | s->sock_proto = proto; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | s->errorhandler = &set_error; |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 753 | #ifdef SOCK_NONBLOCK |
| 754 | if (type & SOCK_NONBLOCK) |
| 755 | s->sock_timeout = 0.0; |
| 756 | else |
| 757 | #endif |
| 758 | { |
| 759 | s->sock_timeout = defaulttimeout; |
| 760 | if (defaulttimeout >= 0.0) |
| 761 | internal_setblocking(s, 0); |
| 762 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 763 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 767 | /* Create a new socket object. |
| 768 | This just creates the object and initializes it. |
| 769 | If the creation fails, return NULL and set an exception (implicit |
| 770 | in NEWOBJ()). */ |
| 771 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 772 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 773 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | PySocketSockObject *s; |
| 776 | s = (PySocketSockObject *) |
| 777 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 778 | if (s != NULL) |
| 779 | init_sockobject(s, fd, family, type, proto); |
| 780 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 784 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 785 | thread to be in gethostbyname or getaddrinfo */ |
| 786 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | d182a6c | 2012-08-14 17:29:04 +0200 | [diff] [blame] | 787 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 788 | #endif |
| 789 | |
| 790 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 791 | /* Convert a string specifying a host name or one of a few symbolic |
| 792 | names to a numeric IP address. This usually calls gethostbyname() |
| 793 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 794 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 795 | an error occurred; then an exception is raised. */ |
| 796 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 797 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 798 | 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] | 799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | struct addrinfo hints, *res; |
| 801 | int error; |
| 802 | int d1, d2, d3, d4; |
| 803 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 806 | if (name[0] == '\0') { |
| 807 | int siz; |
| 808 | memset(&hints, 0, sizeof(hints)); |
| 809 | hints.ai_family = af; |
| 810 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 811 | hints.ai_flags = AI_PASSIVE; |
| 812 | Py_BEGIN_ALLOW_THREADS |
| 813 | ACQUIRE_GETADDRINFO_LOCK |
| 814 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 815 | Py_END_ALLOW_THREADS |
| 816 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 817 | *are* safe regarding their return value, ie. that a |
| 818 | subsequent call to getaddrinfo() does not destroy the |
| 819 | outcome of the first call. */ |
| 820 | RELEASE_GETADDRINFO_LOCK |
| 821 | if (error) { |
| 822 | set_gaierror(error); |
| 823 | return -1; |
| 824 | } |
| 825 | switch (res->ai_family) { |
| 826 | case AF_INET: |
| 827 | siz = 4; |
| 828 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 829 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | case AF_INET6: |
| 831 | siz = 16; |
| 832 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 833 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | default: |
| 835 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 836 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | "unsupported address family"); |
| 838 | return -1; |
| 839 | } |
| 840 | if (res->ai_next) { |
| 841 | freeaddrinfo(res); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 842 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | "wildcard resolved to multiple address"); |
| 844 | return -1; |
| 845 | } |
| 846 | if (res->ai_addrlen < addr_ret_size) |
| 847 | addr_ret_size = res->ai_addrlen; |
| 848 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 849 | freeaddrinfo(res); |
| 850 | return siz; |
| 851 | } |
| 852 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 853 | struct sockaddr_in *sin; |
| 854 | if (af != AF_INET && af != AF_UNSPEC) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 855 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | "address family mismatched"); |
| 857 | return -1; |
| 858 | } |
| 859 | sin = (struct sockaddr_in *)addr_ret; |
| 860 | memset((void *) sin, '\0', sizeof(*sin)); |
| 861 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 862 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 864 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 866 | return sizeof(sin->sin_addr); |
| 867 | } |
| 868 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 869 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 870 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 871 | struct sockaddr_in *sin; |
| 872 | sin = (struct sockaddr_in *)addr_ret; |
| 873 | sin->sin_addr.s_addr = htonl( |
| 874 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 875 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 876 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 877 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 879 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | return 4; |
| 881 | } |
| 882 | memset(&hints, 0, sizeof(hints)); |
| 883 | hints.ai_family = af; |
| 884 | Py_BEGIN_ALLOW_THREADS |
| 885 | ACQUIRE_GETADDRINFO_LOCK |
| 886 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 887 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 889 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 890 | if no address family is given. Assume IPv4 for now.*/ |
| 891 | hints.ai_family = AF_INET; |
| 892 | error = getaddrinfo(name, NULL, &hints, &res); |
| 893 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 894 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | Py_END_ALLOW_THREADS |
| 896 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 897 | if (error) { |
| 898 | set_gaierror(error); |
| 899 | return -1; |
| 900 | } |
| 901 | if (res->ai_addrlen < addr_ret_size) |
| 902 | addr_ret_size = res->ai_addrlen; |
| 903 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 904 | freeaddrinfo(res); |
| 905 | switch (addr_ret->sa_family) { |
| 906 | case AF_INET: |
| 907 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 908 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | case AF_INET6: |
| 910 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 911 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 913 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | return -1; |
| 915 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 918 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 919 | /* Create a string object representing an IP address. |
| 920 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 921 | size numbers). */ |
| 922 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 923 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 924 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 925 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | char buf[NI_MAXHOST]; |
| 927 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 930 | NI_NUMERICHOST); |
| 931 | if (error) { |
| 932 | set_gaierror(error); |
| 933 | return NULL; |
| 934 | } |
| 935 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 939 | #ifdef USE_BLUETOOTH |
| 940 | /* Convert a string representation of a Bluetooth address into a numeric |
| 941 | address. Returns the length (6), or raises an exception and returns -1 if |
| 942 | an error occurred. */ |
| 943 | |
| 944 | static int |
| 945 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | unsigned int b0, b1, b2, b3, b4, b5; |
| 948 | char ch; |
| 949 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 952 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 953 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 954 | bdaddr->b[0] = b0; |
| 955 | bdaddr->b[1] = b1; |
| 956 | bdaddr->b[2] = b2; |
| 957 | bdaddr->b[3] = b3; |
| 958 | bdaddr->b[4] = b4; |
| 959 | bdaddr->b[5] = b5; |
| 960 | return 6; |
| 961 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 962 | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | return -1; |
| 964 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /* Create a string representation of the Bluetooth address. This is always a |
| 968 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 969 | value (zero padded if necessary). */ |
| 970 | |
| 971 | static PyObject * |
| 972 | makebdaddr(bdaddr_t *bdaddr) |
| 973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 977 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 978 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 979 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 980 | } |
| 981 | #endif |
| 982 | |
| 983 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 984 | /* Create an object representing the given socket address, |
| 985 | suitable for passing it back to bind(), connect() etc. |
| 986 | The family field of the sockaddr structure is inspected |
| 987 | to determine what kind of address it really is. */ |
| 988 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 989 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 990 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 991 | 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] | 992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | if (addrlen == 0) { |
| 994 | /* No address -- may be recvfrom() from known socket */ |
| 995 | Py_INCREF(Py_None); |
| 996 | return Py_None; |
| 997 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | case AF_INET: |
| 1002 | { |
| 1003 | struct sockaddr_in *a; |
| 1004 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1005 | PyObject *ret = NULL; |
| 1006 | if (addrobj) { |
| 1007 | a = (struct sockaddr_in *)addr; |
| 1008 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1009 | Py_DECREF(addrobj); |
| 1010 | } |
| 1011 | return ret; |
| 1012 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1013 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1014 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | case AF_UNIX: |
| 1016 | { |
| 1017 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1018 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1020 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 1021 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
| 1022 | } |
| 1023 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1024 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | { |
| 1026 | /* regular NULL-terminated string */ |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1027 | return PyUnicode_DecodeFSDefault(a->sun_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1030 | #endif /* AF_UNIX */ |
| 1031 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1032 | #if defined(AF_NETLINK) |
| 1033 | case AF_NETLINK: |
| 1034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1036 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1037 | } |
| 1038 | #endif /* AF_NETLINK */ |
| 1039 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1040 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | case AF_INET6: |
| 1042 | { |
| 1043 | struct sockaddr_in6 *a; |
| 1044 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1045 | PyObject *ret = NULL; |
| 1046 | if (addrobj) { |
| 1047 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1048 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | addrobj, |
| 1050 | ntohs(a->sin6_port), |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1051 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | a->sin6_scope_id); |
| 1053 | Py_DECREF(addrobj); |
| 1054 | } |
| 1055 | return ret; |
| 1056 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1057 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1058 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1059 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | case AF_BLUETOOTH: |
| 1061 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | case BTPROTO_L2CAP: |
| 1064 | { |
| 1065 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1066 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1067 | PyObject *ret = NULL; |
| 1068 | if (addrobj) { |
| 1069 | ret = Py_BuildValue("Oi", |
| 1070 | addrobj, |
| 1071 | _BT_L2_MEMB(a, psm)); |
| 1072 | Py_DECREF(addrobj); |
| 1073 | } |
| 1074 | return ret; |
| 1075 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | case BTPROTO_RFCOMM: |
| 1078 | { |
| 1079 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1080 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1081 | PyObject *ret = NULL; |
| 1082 | if (addrobj) { |
| 1083 | ret = Py_BuildValue("Oi", |
| 1084 | addrobj, |
| 1085 | _BT_RC_MEMB(a, channel)); |
| 1086 | Py_DECREF(addrobj); |
| 1087 | } |
| 1088 | return ret; |
| 1089 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | case BTPROTO_HCI: |
| 1092 | { |
| 1093 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1094 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1095 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1096 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | PyObject *ret = NULL; |
| 1098 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1099 | return ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1100 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1102 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1103 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | case BTPROTO_SCO: |
| 1105 | { |
| 1106 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1107 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1108 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1109 | #endif |
| 1110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | default: |
| 1112 | PyErr_SetString(PyExc_ValueError, |
| 1113 | "Unknown Bluetooth protocol"); |
| 1114 | return NULL; |
| 1115 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1116 | #endif |
| 1117 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1118 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | case AF_PACKET: |
| 1120 | { |
| 1121 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1122 | char *ifname = ""; |
| 1123 | struct ifreq ifr; |
| 1124 | /* need to look up interface name give index */ |
| 1125 | if (a->sll_ifindex) { |
| 1126 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1127 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1128 | ifname = ifr.ifr_name; |
| 1129 | } |
| 1130 | return Py_BuildValue("shbhy#", |
| 1131 | ifname, |
| 1132 | ntohs(a->sll_protocol), |
| 1133 | a->sll_pkttype, |
| 1134 | a->sll_hatype, |
| 1135 | a->sll_addr, |
| 1136 | a->sll_halen); |
| 1137 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1138 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1139 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1140 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | case AF_TIPC: |
| 1142 | { |
| 1143 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1144 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1145 | return Py_BuildValue("IIIII", |
| 1146 | a->addrtype, |
| 1147 | a->addr.nameseq.type, |
| 1148 | a->addr.nameseq.lower, |
| 1149 | a->addr.nameseq.upper, |
| 1150 | a->scope); |
| 1151 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1152 | return Py_BuildValue("IIIII", |
| 1153 | a->addrtype, |
| 1154 | a->addr.name.name.type, |
| 1155 | a->addr.name.name.instance, |
| 1156 | a->addr.name.name.instance, |
| 1157 | a->scope); |
| 1158 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1159 | return Py_BuildValue("IIIII", |
| 1160 | a->addrtype, |
| 1161 | a->addr.id.node, |
| 1162 | a->addr.id.ref, |
| 1163 | 0, |
| 1164 | a->scope); |
| 1165 | } else { |
| 1166 | PyErr_SetString(PyExc_ValueError, |
| 1167 | "Invalid address type"); |
| 1168 | return NULL; |
| 1169 | } |
| 1170 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1171 | #endif |
| 1172 | |
Charles-François Natali | 30589c9 | 2011-10-07 22:47:08 +0200 | [diff] [blame] | 1173 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1174 | case AF_CAN: |
| 1175 | { |
| 1176 | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
| 1177 | char *ifname = ""; |
| 1178 | struct ifreq ifr; |
| 1179 | /* need to look up interface name given index */ |
| 1180 | if (a->can_ifindex) { |
| 1181 | ifr.ifr_ifindex = a->can_ifindex; |
| 1182 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1183 | ifname = ifr.ifr_name; |
| 1184 | } |
| 1185 | |
| 1186 | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
| 1187 | ifname, |
| 1188 | a->can_family); |
| 1189 | } |
| 1190 | #endif |
| 1191 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1192 | #ifdef PF_SYSTEM |
| 1193 | case PF_SYSTEM: |
| 1194 | switch(proto) { |
| 1195 | #ifdef SYSPROTO_CONTROL |
| 1196 | case SYSPROTO_CONTROL: |
| 1197 | { |
| 1198 | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
| 1199 | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
| 1200 | } |
| 1201 | #endif |
| 1202 | default: |
| 1203 | PyErr_SetString(PyExc_ValueError, |
| 1204 | "Invalid address type"); |
| 1205 | return 0; |
| 1206 | } |
| 1207 | #endif |
| 1208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | default: |
| 1212 | /* If we don't know the address family, don't raise an |
| 1213 | exception -- return it as an (int, bytes) tuple. */ |
| 1214 | return Py_BuildValue("iy#", |
| 1215 | addr->sa_family, |
| 1216 | addr->sa_data, |
| 1217 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1222 | |
| 1223 | /* Parse a socket address argument according to the socket object's |
| 1224 | address family. Return 1 if the address was in the proper format, |
| 1225 | 0 of not. The address is returned through addr_ret, its length |
| 1226 | through len_ret. */ |
| 1227 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1228 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1229 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1233 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1234 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | case AF_UNIX: |
| 1236 | { |
| 1237 | struct sockaddr_un* addr; |
| 1238 | char *path; |
| 1239 | int len; |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1240 | int retval = 0; |
| 1241 | |
| 1242 | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
| 1243 | allow embedded nulls on Linux. */ |
| 1244 | if (PyUnicode_Check(args)) { |
| 1245 | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
| 1246 | return 0; |
| 1247 | } |
| 1248 | else |
| 1249 | Py_INCREF(args); |
| 1250 | if (!PyArg_Parse(args, "y#", &path, &len)) |
| 1251 | goto unix_out; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1254 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | if (len > 0 && path[0] == 0) { |
| 1256 | /* Linux abstract namespace extension */ |
| 1257 | if (len > sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1258 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1260 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1264 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | { |
| 1266 | /* regular NULL-terminated string */ |
| 1267 | if (len >= sizeof addr->sun_path) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1268 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | "AF_UNIX path too long"); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1270 | goto unix_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | } |
| 1272 | addr->sun_path[len] = 0; |
| 1273 | } |
| 1274 | addr->sun_family = s->sock_family; |
| 1275 | memcpy(addr->sun_path, path, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 1277 | retval = 1; |
| 1278 | unix_out: |
| 1279 | Py_DECREF(args); |
| 1280 | return retval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1282 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1283 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1284 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | case AF_NETLINK: |
| 1286 | { |
| 1287 | struct sockaddr_nl* addr; |
| 1288 | int pid, groups; |
| 1289 | addr = (struct sockaddr_nl *)addr_ret; |
| 1290 | if (!PyTuple_Check(args)) { |
| 1291 | PyErr_Format( |
| 1292 | PyExc_TypeError, |
| 1293 | "getsockaddrarg: " |
| 1294 | "AF_NETLINK address must be tuple, not %.500s", |
| 1295 | Py_TYPE(args)->tp_name); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1299 | return 0; |
| 1300 | addr->nl_family = AF_NETLINK; |
| 1301 | addr->nl_pid = pid; |
| 1302 | addr->nl_groups = groups; |
| 1303 | *len_ret = sizeof(*addr); |
| 1304 | return 1; |
| 1305 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1306 | #endif |
| 1307 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1308 | #ifdef AF_RDS |
| 1309 | case AF_RDS: |
| 1310 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1311 | #endif |
| 1312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | case AF_INET: |
| 1314 | { |
| 1315 | struct sockaddr_in* addr; |
| 1316 | char *host; |
| 1317 | int port, result; |
| 1318 | if (!PyTuple_Check(args)) { |
| 1319 | PyErr_Format( |
| 1320 | PyExc_TypeError, |
| 1321 | "getsockaddrarg: " |
| 1322 | "AF_INET address must be tuple, not %.500s", |
| 1323 | Py_TYPE(args)->tp_name); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1327 | "idna", &host, &port)) |
| 1328 | return 0; |
| 1329 | addr=(struct sockaddr_in*)addr_ret; |
| 1330 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1331 | sizeof(*addr), AF_INET); |
| 1332 | PyMem_Free(host); |
| 1333 | if (result < 0) |
| 1334 | return 0; |
| 1335 | if (port < 0 || port > 0xffff) { |
| 1336 | PyErr_SetString( |
| 1337 | PyExc_OverflowError, |
| 1338 | "getsockaddrarg: port must be 0-65535."); |
| 1339 | return 0; |
| 1340 | } |
| 1341 | addr->sin_family = AF_INET; |
| 1342 | addr->sin_port = htons((short)port); |
| 1343 | *len_ret = sizeof *addr; |
| 1344 | return 1; |
| 1345 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1346 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1347 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | case AF_INET6: |
| 1349 | { |
| 1350 | struct sockaddr_in6* addr; |
| 1351 | char *host; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1352 | int port, result; |
| 1353 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | flowinfo = scope_id = 0; |
| 1355 | if (!PyTuple_Check(args)) { |
| 1356 | PyErr_Format( |
| 1357 | PyExc_TypeError, |
| 1358 | "getsockaddrarg: " |
| 1359 | "AF_INET6 address must be tuple, not %.500s", |
| 1360 | Py_TYPE(args)->tp_name); |
| 1361 | return 0; |
| 1362 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1363 | if (!PyArg_ParseTuple(args, "eti|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1364 | "idna", &host, &port, &flowinfo, |
| 1365 | &scope_id)) { |
| 1366 | return 0; |
| 1367 | } |
| 1368 | addr = (struct sockaddr_in6*)addr_ret; |
| 1369 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1370 | sizeof(*addr), AF_INET6); |
| 1371 | PyMem_Free(host); |
| 1372 | if (result < 0) |
| 1373 | return 0; |
| 1374 | if (port < 0 || port > 0xffff) { |
| 1375 | PyErr_SetString( |
| 1376 | PyExc_OverflowError, |
| 1377 | "getsockaddrarg: port must be 0-65535."); |
| 1378 | return 0; |
| 1379 | } |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 1380 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1381 | PyErr_SetString( |
| 1382 | PyExc_OverflowError, |
| 1383 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1384 | return 0; |
| 1385 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | addr->sin6_family = s->sock_family; |
| 1387 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1388 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | addr->sin6_scope_id = scope_id; |
| 1390 | *len_ret = sizeof *addr; |
| 1391 | return 1; |
| 1392 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1393 | #endif |
| 1394 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1395 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | case AF_BLUETOOTH: |
| 1397 | { |
| 1398 | switch (s->sock_proto) { |
| 1399 | case BTPROTO_L2CAP: |
| 1400 | { |
| 1401 | struct sockaddr_l2 *addr; |
| 1402 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1405 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1406 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1407 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1408 | &_BT_L2_MEMB(addr, psm))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1409 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | "wrong format"); |
| 1411 | return 0; |
| 1412 | } |
| 1413 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1414 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | *len_ret = sizeof *addr; |
| 1417 | return 1; |
| 1418 | } |
| 1419 | case BTPROTO_RFCOMM: |
| 1420 | { |
| 1421 | struct sockaddr_rc *addr; |
| 1422 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | addr = (struct sockaddr_rc *)addr_ret; |
| 1425 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1426 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1427 | &_BT_RC_MEMB(addr, channel))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1428 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | "wrong format"); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1433 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1435 | *len_ret = sizeof *addr; |
| 1436 | return 1; |
| 1437 | } |
| 1438 | case BTPROTO_HCI: |
| 1439 | { |
| 1440 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1441 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1442 | char *straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1443 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1444 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1445 | if (straddr == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1446 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1447 | "wrong format"); |
| 1448 | return 0; |
| 1449 | } |
| 1450 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1451 | return 0; |
| 1452 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1454 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1455 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | "wrong format"); |
| 1457 | return 0; |
| 1458 | } |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 1459 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | *len_ret = sizeof *addr; |
| 1461 | return 1; |
| 1462 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1463 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | case BTPROTO_SCO: |
| 1465 | { |
| 1466 | struct sockaddr_sco *addr; |
| 1467 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | addr = (struct sockaddr_sco *)addr_ret; |
| 1470 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1471 | if (!PyBytes_Check(args)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1472 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | "wrong format"); |
| 1474 | return 0; |
| 1475 | } |
| 1476 | straddr = PyBytes_AS_STRING(args); |
| 1477 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1478 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | *len_ret = sizeof *addr; |
| 1481 | return 1; |
| 1482 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1483 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1485 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | return 0; |
| 1487 | } |
| 1488 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1489 | #endif |
| 1490 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 1491 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | case AF_PACKET: |
| 1493 | { |
| 1494 | struct sockaddr_ll* addr; |
| 1495 | struct ifreq ifr; |
| 1496 | char *interfaceName; |
| 1497 | int protoNumber; |
| 1498 | int hatype = 0; |
| 1499 | int pkttype = 0; |
| 1500 | char *haddr = NULL; |
| 1501 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | if (!PyTuple_Check(args)) { |
| 1504 | PyErr_Format( |
| 1505 | PyExc_TypeError, |
| 1506 | "getsockaddrarg: " |
| 1507 | "AF_PACKET address must be tuple, not %.500s", |
| 1508 | Py_TYPE(args)->tp_name); |
| 1509 | return 0; |
| 1510 | } |
| 1511 | if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, |
| 1512 | &protoNumber, &pkttype, &hatype, |
| 1513 | &haddr, &halen)) |
| 1514 | return 0; |
| 1515 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1516 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1517 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1518 | s->errorhandler(); |
| 1519 | return 0; |
| 1520 | } |
| 1521 | if (halen > 8) { |
| 1522 | PyErr_SetString(PyExc_ValueError, |
| 1523 | "Hardware address must be 8 bytes or less"); |
| 1524 | return 0; |
| 1525 | } |
| 1526 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1527 | PyErr_SetString( |
| 1528 | PyExc_OverflowError, |
| 1529 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1530 | return 0; |
| 1531 | } |
| 1532 | addr = (struct sockaddr_ll*)addr_ret; |
| 1533 | addr->sll_family = AF_PACKET; |
| 1534 | addr->sll_protocol = htons((short)protoNumber); |
| 1535 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1536 | addr->sll_pkttype = pkttype; |
| 1537 | addr->sll_hatype = hatype; |
| 1538 | if (halen != 0) { |
| 1539 | memcpy(&addr->sll_addr, haddr, halen); |
| 1540 | } |
| 1541 | addr->sll_halen = halen; |
| 1542 | *len_ret = sizeof *addr; |
| 1543 | return 1; |
| 1544 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1545 | #endif |
| 1546 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1547 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | case AF_TIPC: |
| 1549 | { |
| 1550 | unsigned int atype, v1, v2, v3; |
| 1551 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1552 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1554 | if (!PyTuple_Check(args)) { |
| 1555 | PyErr_Format( |
| 1556 | PyExc_TypeError, |
| 1557 | "getsockaddrarg: " |
| 1558 | "AF_TIPC address must be tuple, not %.500s", |
| 1559 | Py_TYPE(args)->tp_name); |
| 1560 | return 0; |
| 1561 | } |
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 | if (!PyArg_ParseTuple(args, |
| 1564 | "IIII|I;Invalid TIPC address format", |
| 1565 | &atype, &v1, &v2, &v3, &scope)) |
| 1566 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1569 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | addr->family = AF_TIPC; |
| 1572 | addr->scope = scope; |
| 1573 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1576 | addr->addr.nameseq.type = v1; |
| 1577 | addr->addr.nameseq.lower = v2; |
| 1578 | addr->addr.nameseq.upper = v3; |
| 1579 | } else if (atype == TIPC_ADDR_NAME) { |
| 1580 | addr->addr.name.name.type = v1; |
| 1581 | addr->addr.name.name.instance = v2; |
| 1582 | } else if (atype == TIPC_ADDR_ID) { |
| 1583 | addr->addr.id.node = v1; |
| 1584 | addr->addr.id.ref = v2; |
| 1585 | } else { |
| 1586 | /* Shouldn't happen */ |
| 1587 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1588 | return 0; |
| 1589 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | return 1; |
| 1594 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1595 | #endif |
| 1596 | |
Charles-François Natali | 30589c9 | 2011-10-07 22:47:08 +0200 | [diff] [blame] | 1597 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1598 | case AF_CAN: |
| 1599 | switch (s->sock_proto) { |
| 1600 | case CAN_RAW: |
| 1601 | { |
| 1602 | struct sockaddr_can *addr; |
| 1603 | PyObject *interfaceName; |
| 1604 | struct ifreq ifr; |
| 1605 | addr = (struct sockaddr_can *)addr_ret; |
| 1606 | Py_ssize_t len; |
| 1607 | |
| 1608 | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
| 1609 | &interfaceName)) |
| 1610 | return 0; |
| 1611 | |
| 1612 | len = PyBytes_GET_SIZE(interfaceName); |
| 1613 | |
| 1614 | if (len == 0) { |
| 1615 | ifr.ifr_ifindex = 0; |
| 1616 | } else if (len < sizeof(ifr.ifr_name)) { |
Christian Heimes | 15b6885 | 2012-09-10 01:25:50 +0200 | [diff] [blame] | 1617 | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
| 1618 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1619 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1620 | s->errorhandler(); |
| 1621 | Py_DECREF(interfaceName); |
| 1622 | return 0; |
| 1623 | } |
| 1624 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1625 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1626 | "AF_CAN interface name too long"); |
| 1627 | Py_DECREF(interfaceName); |
| 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | addr->can_family = AF_CAN; |
| 1632 | addr->can_ifindex = ifr.ifr_ifindex; |
| 1633 | |
| 1634 | *len_ret = sizeof(*addr); |
| 1635 | Py_DECREF(interfaceName); |
| 1636 | return 1; |
| 1637 | } |
| 1638 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1639 | PyErr_SetString(PyExc_OSError, |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1640 | "getsockaddrarg: unsupported CAN protocol"); |
| 1641 | return 0; |
| 1642 | } |
| 1643 | #endif |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1644 | |
| 1645 | #ifdef PF_SYSTEM |
| 1646 | case PF_SYSTEM: |
| 1647 | switch (s->sock_proto) { |
| 1648 | #ifdef SYSPROTO_CONTROL |
| 1649 | case SYSPROTO_CONTROL: |
| 1650 | { |
| 1651 | struct sockaddr_ctl *addr; |
| 1652 | |
| 1653 | addr = (struct sockaddr_ctl *)addr_ret; |
| 1654 | addr->sc_family = AF_SYSTEM; |
| 1655 | addr->ss_sysaddr = AF_SYS_CONTROL; |
| 1656 | |
| 1657 | if (PyUnicode_Check(args)) { |
| 1658 | struct ctl_info info; |
| 1659 | PyObject *ctl_name; |
| 1660 | |
| 1661 | if (!PyArg_Parse(args, "O&", |
| 1662 | PyUnicode_FSConverter, &ctl_name)) { |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | if (PyBytes_GET_SIZE(ctl_name) > sizeof(info.ctl_name)) { |
| 1667 | PyErr_SetString(PyExc_ValueError, |
| 1668 | "provided string is too long"); |
| 1669 | Py_DECREF(ctl_name); |
| 1670 | return 0; |
| 1671 | } |
| 1672 | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
| 1673 | sizeof(info.ctl_name)); |
| 1674 | Py_DECREF(ctl_name); |
| 1675 | |
| 1676 | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
| 1677 | PyErr_SetString(PyExc_OSError, |
| 1678 | "cannot find kernel control with provided name"); |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | addr->sc_id = info.ctl_id; |
| 1683 | addr->sc_unit = 0; |
| 1684 | } else if (!PyArg_ParseTuple(args, "II", |
| 1685 | &(addr->sc_id), &(addr->sc_unit))) { |
| 1686 | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
| 1687 | "expected str or tuple of two ints"); |
| 1688 | |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | *len_ret = sizeof(*addr); |
| 1693 | return 1; |
| 1694 | } |
| 1695 | #endif |
| 1696 | default: |
| 1697 | PyErr_SetString(PyExc_OSError, |
| 1698 | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
| 1699 | return 0; |
| 1700 | } |
| 1701 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1706 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1712 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1713 | /* 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] | 1714 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1715 | through len_ret. */ |
| 1716 | |
| 1717 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1718 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1721 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1722 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | case AF_UNIX: |
| 1724 | { |
| 1725 | *len_ret = sizeof (struct sockaddr_un); |
| 1726 | return 1; |
| 1727 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1728 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1729 | #if defined(AF_NETLINK) |
| 1730 | case AF_NETLINK: |
| 1731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1732 | *len_ret = sizeof (struct sockaddr_nl); |
| 1733 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1734 | } |
| 1735 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1736 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1737 | #ifdef AF_RDS |
| 1738 | case AF_RDS: |
| 1739 | /* RDS sockets use sockaddr_in: fall-through */ |
| 1740 | #endif |
| 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | case AF_INET: |
| 1743 | { |
| 1744 | *len_ret = sizeof (struct sockaddr_in); |
| 1745 | return 1; |
| 1746 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1747 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1748 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | case AF_INET6: |
| 1750 | { |
| 1751 | *len_ret = sizeof (struct sockaddr_in6); |
| 1752 | return 1; |
| 1753 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1754 | #endif |
| 1755 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1756 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | case AF_BLUETOOTH: |
| 1758 | { |
| 1759 | switch(s->sock_proto) |
| 1760 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | case BTPROTO_L2CAP: |
| 1763 | *len_ret = sizeof (struct sockaddr_l2); |
| 1764 | return 1; |
| 1765 | case BTPROTO_RFCOMM: |
| 1766 | *len_ret = sizeof (struct sockaddr_rc); |
| 1767 | return 1; |
| 1768 | case BTPROTO_HCI: |
| 1769 | *len_ret = sizeof (struct sockaddr_hci); |
| 1770 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1771 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | case BTPROTO_SCO: |
| 1773 | *len_ret = sizeof (struct sockaddr_sco); |
| 1774 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1775 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1777 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | "unknown BT protocol"); |
| 1779 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | } |
| 1782 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1783 | #endif |
| 1784 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1785 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | case AF_PACKET: |
| 1787 | { |
| 1788 | *len_ret = sizeof (struct sockaddr_ll); |
| 1789 | return 1; |
| 1790 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1791 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1792 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1793 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | case AF_TIPC: |
| 1795 | { |
| 1796 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1797 | return 1; |
| 1798 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1799 | #endif |
| 1800 | |
Charles-François Natali | 30589c9 | 2011-10-07 22:47:08 +0200 | [diff] [blame] | 1801 | #ifdef AF_CAN |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1802 | case AF_CAN: |
| 1803 | { |
| 1804 | *len_ret = sizeof (struct sockaddr_can); |
| 1805 | return 1; |
| 1806 | } |
| 1807 | #endif |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 1808 | |
| 1809 | #ifdef PF_SYSTEM |
| 1810 | case PF_SYSTEM: |
| 1811 | switch(s->sock_proto) { |
| 1812 | #ifdef SYSPROTO_CONTROL |
| 1813 | case SYSPROTO_CONTROL: |
| 1814 | *len_ret = sizeof (struct sockaddr_ctl); |
| 1815 | return 1; |
| 1816 | #endif |
| 1817 | default: |
| 1818 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
| 1819 | "unknown PF_SYSTEM protocol"); |
| 1820 | return 0; |
| 1821 | } |
| 1822 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1827 | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1834 | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
| 1835 | Currently, these methods are only compiled if the RFC 2292/3542 |
| 1836 | CMSG_LEN() macro is available. Older systems seem to have used |
| 1837 | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
| 1838 | it may be possible to define CMSG_LEN() that way if it's not |
| 1839 | provided. Some architectures might need extra padding after the |
| 1840 | cmsghdr, however, and CMSG_LEN() would have to take account of |
| 1841 | this. */ |
| 1842 | #ifdef CMSG_LEN |
| 1843 | /* If length is in range, set *result to CMSG_LEN(length) and return |
| 1844 | true; otherwise, return false. */ |
| 1845 | static int |
| 1846 | get_CMSG_LEN(size_t length, size_t *result) |
| 1847 | { |
| 1848 | size_t tmp; |
| 1849 | |
| 1850 | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
| 1851 | return 0; |
| 1852 | tmp = CMSG_LEN(length); |
| 1853 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1854 | return 0; |
| 1855 | *result = tmp; |
| 1856 | return 1; |
| 1857 | } |
| 1858 | |
| 1859 | #ifdef CMSG_SPACE |
| 1860 | /* If length is in range, set *result to CMSG_SPACE(length) and return |
| 1861 | true; otherwise, return false. */ |
| 1862 | static int |
| 1863 | get_CMSG_SPACE(size_t length, size_t *result) |
| 1864 | { |
| 1865 | size_t tmp; |
| 1866 | |
| 1867 | /* Use CMSG_SPACE(1) here in order to take account of the padding |
| 1868 | necessary before *and* after the data. */ |
| 1869 | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
| 1870 | return 0; |
| 1871 | tmp = CMSG_SPACE(length); |
| 1872 | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
| 1873 | return 0; |
| 1874 | *result = tmp; |
| 1875 | return 1; |
| 1876 | } |
| 1877 | #endif |
| 1878 | |
| 1879 | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
| 1880 | pointer in msg->msg_control with at least "space" bytes after it, |
| 1881 | and its cmsg_len member inside the buffer. */ |
| 1882 | static int |
| 1883 | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
| 1884 | { |
| 1885 | size_t cmsg_offset; |
| 1886 | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
| 1887 | sizeof(cmsgh->cmsg_len)); |
| 1888 | |
Charles-François Natali | 466517d | 2011-08-28 18:23:43 +0200 | [diff] [blame] | 1889 | /* Note that POSIX allows msg_controllen to be of signed type. */ |
| 1890 | if (cmsgh == NULL || msg->msg_control == NULL || msg->msg_controllen < 0) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1891 | return 0; |
| 1892 | if (space < cmsg_len_end) |
| 1893 | space = cmsg_len_end; |
| 1894 | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
| 1895 | return (cmsg_offset <= (size_t)-1 - space && |
| 1896 | cmsg_offset + space <= msg->msg_controllen); |
| 1897 | } |
| 1898 | |
| 1899 | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
| 1900 | *space to number of bytes following it in the buffer and return |
| 1901 | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
| 1902 | msg->msg_controllen are valid. */ |
| 1903 | static int |
| 1904 | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
| 1905 | { |
| 1906 | size_t data_offset; |
| 1907 | char *data_ptr; |
| 1908 | |
| 1909 | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
| 1910 | return 0; |
| 1911 | data_offset = data_ptr - (char *)msg->msg_control; |
| 1912 | if (data_offset > msg->msg_controllen) |
| 1913 | return 0; |
| 1914 | *space = msg->msg_controllen - data_offset; |
| 1915 | return 1; |
| 1916 | } |
| 1917 | |
| 1918 | /* If cmsgh is invalid or not contained in the buffer pointed to by |
| 1919 | msg->msg_control, return -1. If cmsgh is valid and its associated |
| 1920 | data is entirely contained in the buffer, set *data_len to the |
| 1921 | length of the associated data and return 0. If only part of the |
| 1922 | associated data is contained in the buffer but cmsgh is otherwise |
| 1923 | valid, set *data_len to the length contained in the buffer and |
| 1924 | return 1. */ |
| 1925 | static int |
| 1926 | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
| 1927 | { |
| 1928 | size_t space, cmsg_data_len; |
| 1929 | |
| 1930 | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
| 1931 | cmsgh->cmsg_len < CMSG_LEN(0)) |
| 1932 | return -1; |
| 1933 | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
| 1934 | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
| 1935 | return -1; |
| 1936 | if (space >= cmsg_data_len) { |
| 1937 | *data_len = cmsg_data_len; |
| 1938 | return 0; |
| 1939 | } |
| 1940 | *data_len = space; |
| 1941 | return 1; |
| 1942 | } |
| 1943 | #endif /* CMSG_LEN */ |
| 1944 | |
| 1945 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1946 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1947 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1948 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1949 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1950 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | sock_addr_t addrbuf; |
| 1952 | SOCKET_T newfd = INVALID_SOCKET; |
| 1953 | socklen_t addrlen; |
| 1954 | PyObject *sock = NULL; |
| 1955 | PyObject *addr = NULL; |
| 1956 | PyObject *res = NULL; |
| 1957 | int timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | if (!getsockaddrlen(s, &addrlen)) |
| 1959 | return NULL; |
| 1960 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | if (!IS_SELECTABLE(s)) |
| 1963 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1964 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1965 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1967 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 1968 | if (!timeout) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 1970 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1973 | if (timeout == 1) { |
| 1974 | PyErr_SetString(socket_timeout, "timed out"); |
| 1975 | return NULL; |
| 1976 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1977 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | if (newfd == INVALID_SOCKET) |
| 1980 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | sock = PyLong_FromSocket_t(newfd); |
| 1983 | if (sock == NULL) { |
| 1984 | SOCKETCLOSE(newfd); |
| 1985 | goto finally; |
| 1986 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1989 | addrlen, s->sock_proto); |
| 1990 | if (addr == NULL) |
| 1991 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1994 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1995 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | Py_XDECREF(sock); |
| 1997 | Py_XDECREF(addr); |
| 1998 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2001 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2002 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2003 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 2004 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 2005 | representing the connection, and the address of the client.\n\ |
| 2006 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2007 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2008 | /* s.setblocking(flag) method. Argument: |
| 2009 | False -- non-blocking mode; same as settimeout(0) |
| 2010 | True -- blocking mode; same as settimeout(None) |
| 2011 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2012 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2013 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2014 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2015 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | block = PyLong_AsLong(arg); |
| 2019 | if (block == -1 && PyErr_Occurred()) |
| 2020 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | s->sock_timeout = block ? -1.0 : 0.0; |
| 2023 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | Py_INCREF(Py_None); |
| 2026 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2027 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2029 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2030 | "setblocking(flag)\n\ |
| 2031 | \n\ |
| 2032 | 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] | 2033 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2034 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2035 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2036 | /* s.settimeout(timeout) method. Argument: |
| 2037 | None -- no timeout, blocking mode; same as setblocking(True) |
| 2038 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 2039 | > 0 -- timeout mode; operations time out after timeout seconds |
| 2040 | < 0 -- illegal; raises an exception |
| 2041 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2042 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2043 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | if (arg == Py_None) |
| 2048 | timeout = -1.0; |
| 2049 | else { |
| 2050 | timeout = PyFloat_AsDouble(arg); |
| 2051 | if (timeout < 0.0) { |
| 2052 | if (!PyErr_Occurred()) |
| 2053 | PyErr_SetString(PyExc_ValueError, |
| 2054 | "Timeout value out of range"); |
| 2055 | return NULL; |
| 2056 | } |
| 2057 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | s->sock_timeout = timeout; |
| 2060 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | Py_INCREF(Py_None); |
| 2063 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2066 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2067 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2068 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2069 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 2070 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 2071 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2072 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2073 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2074 | /* s.gettimeout() method. |
| 2075 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2076 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2077 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2079 | if (s->sock_timeout < 0.0) { |
| 2080 | Py_INCREF(Py_None); |
| 2081 | return Py_None; |
| 2082 | } |
| 2083 | else |
| 2084 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2087 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2088 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2089 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 2090 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2091 | 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] | 2092 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 2093 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2094 | /* s.setsockopt() method. |
| 2095 | With an integer third argument, sets an integer option. |
| 2096 | With a string third argument, sets an option from a buffer; |
| 2097 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2098 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2099 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2100 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | int level; |
| 2103 | int optname; |
| 2104 | int res; |
| 2105 | char *buf; |
| 2106 | int buflen; |
| 2107 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 2110 | &level, &optname, &flag)) { |
| 2111 | buf = (char *) &flag; |
| 2112 | buflen = sizeof flag; |
| 2113 | } |
| 2114 | else { |
| 2115 | PyErr_Clear(); |
| 2116 | if (!PyArg_ParseTuple(args, "iiy#:setsockopt", |
| 2117 | &level, &optname, &buf, &buflen)) |
| 2118 | return NULL; |
| 2119 | } |
| 2120 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 2121 | if (res < 0) |
| 2122 | return s->errorhandler(); |
| 2123 | Py_INCREF(Py_None); |
| 2124 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2127 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2128 | "setsockopt(level, option, value)\n\ |
| 2129 | \n\ |
| 2130 | 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] | 2131 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2132 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 2134 | /* s.getsockopt() method. |
| 2135 | With two arguments, retrieves an integer option. |
| 2136 | With a third integer argument, retrieves a string buffer of that size; |
| 2137 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2138 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2139 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2140 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | int level; |
| 2143 | int optname; |
| 2144 | int res; |
| 2145 | PyObject *buf; |
| 2146 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 2149 | &level, &optname, &buflen)) |
| 2150 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | if (buflen == 0) { |
| 2153 | int flag = 0; |
| 2154 | socklen_t flagsize = sizeof flag; |
| 2155 | res = getsockopt(s->sock_fd, level, optname, |
| 2156 | (void *)&flag, &flagsize); |
| 2157 | if (res < 0) |
| 2158 | return s->errorhandler(); |
| 2159 | return PyLong_FromLong(flag); |
| 2160 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2161 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | /* socklen_t is unsigned so no negative test is needed, |
| 2163 | test buflen == 0 is previously done */ |
| 2164 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2165 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2167 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2168 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | "getsockopt buflen out of range"); |
| 2170 | return NULL; |
| 2171 | } |
| 2172 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 2173 | if (buf == NULL) |
| 2174 | return NULL; |
| 2175 | res = getsockopt(s->sock_fd, level, optname, |
| 2176 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 2177 | if (res < 0) { |
| 2178 | Py_DECREF(buf); |
| 2179 | return s->errorhandler(); |
| 2180 | } |
| 2181 | _PyBytes_Resize(&buf, buflen); |
| 2182 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2185 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2186 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 2187 | \n\ |
| 2188 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2189 | 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] | 2190 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2191 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2192 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2193 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2194 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2195 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2196 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | sock_addr_t addrbuf; |
| 2199 | int addrlen; |
| 2200 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2203 | return NULL; |
| 2204 | Py_BEGIN_ALLOW_THREADS |
| 2205 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2206 | Py_END_ALLOW_THREADS |
| 2207 | if (res < 0) |
| 2208 | return s->errorhandler(); |
| 2209 | Py_INCREF(Py_None); |
| 2210 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2213 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2214 | "bind(address)\n\ |
| 2215 | \n\ |
| 2216 | 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] | 2217 | 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] | 2218 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2219 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2220 | |
| 2221 | /* s.close() method. |
| 2222 | Set the file descriptor to -1 so operations tried subsequently |
| 2223 | will surely fail. */ |
| 2224 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2225 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2226 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | if ((fd = s->sock_fd) != -1) { |
| 2231 | s->sock_fd = -1; |
| 2232 | Py_BEGIN_ALLOW_THREADS |
| 2233 | (void) SOCKETCLOSE(fd); |
| 2234 | Py_END_ALLOW_THREADS |
| 2235 | } |
| 2236 | Py_INCREF(Py_None); |
| 2237 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2240 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2241 | "close()\n\ |
| 2242 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2243 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2244 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2245 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2246 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2247 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2248 | SOCKET_T fd = s->sock_fd; |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2249 | s->sock_fd = -1; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2250 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2253 | PyDoc_STRVAR(detach_doc, |
| 2254 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2255 | \n\ |
| 2256 | Close the socket object without closing the underlying file descriptor.\ |
| 2257 | The object cannot be used after this call, but the file descriptor\ |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2258 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 2259 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2260 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2261 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | timeout = 0; |
| 2267 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2268 | |
| 2269 | #ifdef MS_WINDOWS |
| 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | if (s->sock_timeout > 0.0) { |
| 2272 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2273 | IS_SELECTABLE(s)) { |
| 2274 | /* This is a mess. Best solution: trust select */ |
| 2275 | fd_set fds; |
| 2276 | fd_set fds_exc; |
| 2277 | struct timeval tv; |
| 2278 | tv.tv_sec = (int)s->sock_timeout; |
| 2279 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2280 | FD_ZERO(&fds); |
| 2281 | FD_SET(s->sock_fd, &fds); |
| 2282 | FD_ZERO(&fds_exc); |
| 2283 | FD_SET(s->sock_fd, &fds_exc); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2284 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 2285 | NULL, &fds, &fds_exc, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | if (res == 0) { |
| 2287 | res = WSAEWOULDBLOCK; |
| 2288 | timeout = 1; |
| 2289 | } else if (res > 0) { |
| 2290 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2291 | /* The socket is in the writable set - this |
| 2292 | means connected */ |
| 2293 | res = 0; |
| 2294 | else { |
| 2295 | /* As per MS docs, we need to call getsockopt() |
| 2296 | to get the underlying error */ |
| 2297 | int res_size = sizeof res; |
| 2298 | /* It must be in the exception set */ |
| 2299 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2300 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2301 | (char *)&res, &res_size)) |
| 2302 | /* getsockopt also clears WSAGetLastError, |
| 2303 | so reset it back. */ |
| 2304 | WSASetLastError(res); |
| 2305 | else |
| 2306 | res = WSAGetLastError(); |
| 2307 | } |
| 2308 | } |
| 2309 | /* else if (res < 0) an error occurred */ |
| 2310 | } |
| 2311 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | if (res < 0) |
| 2314 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2315 | |
| 2316 | #else |
| 2317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | if (s->sock_timeout > 0.0) { |
| 2319 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2320 | timeout = internal_select(s, 1); |
| 2321 | if (timeout == 0) { |
| 2322 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2323 | use getsockopt(SO_ERROR) to get the real |
| 2324 | error. */ |
| 2325 | socklen_t res_size = sizeof res; |
| 2326 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2327 | SO_ERROR, &res, &res_size); |
| 2328 | if (res == EISCONN) |
| 2329 | res = 0; |
| 2330 | errno = res; |
| 2331 | } |
| 2332 | else if (timeout == -1) { |
| 2333 | res = errno; /* had error */ |
| 2334 | } |
| 2335 | else |
| 2336 | res = EWOULDBLOCK; /* timed out */ |
| 2337 | } |
| 2338 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | if (res < 0) |
| 2341 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2342 | |
| 2343 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2347 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2348 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2349 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2350 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2351 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2352 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | sock_addr_t addrbuf; |
| 2355 | int addrlen; |
| 2356 | int res; |
| 2357 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2360 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | Py_BEGIN_ALLOW_THREADS |
| 2363 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2364 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | if (timeout == 1) { |
| 2367 | PyErr_SetString(socket_timeout, "timed out"); |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | if (res != 0) |
| 2371 | return s->errorhandler(); |
| 2372 | Py_INCREF(Py_None); |
| 2373 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2376 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2377 | "connect(address)\n\ |
| 2378 | \n\ |
| 2379 | 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] | 2380 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2381 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2382 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2383 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2384 | |
| 2385 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2386 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | sock_addr_t addrbuf; |
| 2389 | int addrlen; |
| 2390 | int res; |
| 2391 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2394 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | Py_BEGIN_ALLOW_THREADS |
| 2397 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2398 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2401 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2402 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | if (res == EINTR && PyErr_CheckSignals()) |
| 2404 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2405 | #endif |
| 2406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2410 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2411 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2412 | \n\ |
| 2413 | 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] | 2414 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2415 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2416 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2417 | /* s.fileno() method */ |
| 2418 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2419 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2420 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2425 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2426 | "fileno() -> integer\n\ |
| 2427 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2428 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2429 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2430 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2431 | /* s.getsockname() method */ |
| 2432 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2433 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2434 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | sock_addr_t addrbuf; |
| 2437 | int res; |
| 2438 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2440 | if (!getsockaddrlen(s, &addrlen)) |
| 2441 | return NULL; |
| 2442 | memset(&addrbuf, 0, addrlen); |
| 2443 | Py_BEGIN_ALLOW_THREADS |
| 2444 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2445 | Py_END_ALLOW_THREADS |
| 2446 | if (res < 0) |
| 2447 | return s->errorhandler(); |
| 2448 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2449 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2452 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2453 | "getsockname() -> address info\n\ |
| 2454 | \n\ |
| 2455 | 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] | 2456 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2457 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2460 | /* s.getpeername() method */ |
| 2461 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2462 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2463 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | sock_addr_t addrbuf; |
| 2466 | int res; |
| 2467 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | if (!getsockaddrlen(s, &addrlen)) |
| 2470 | return NULL; |
| 2471 | memset(&addrbuf, 0, addrlen); |
| 2472 | Py_BEGIN_ALLOW_THREADS |
| 2473 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2474 | Py_END_ALLOW_THREADS |
| 2475 | if (res < 0) |
| 2476 | return s->errorhandler(); |
| 2477 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2478 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2479 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2480 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2481 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2482 | "getpeername() -> address info\n\ |
| 2483 | \n\ |
| 2484 | 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] | 2485 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2486 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2487 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2488 | |
| 2489 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2490 | /* s.listen(n) method */ |
| 2491 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2492 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2493 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2494 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2495 | int backlog; |
| 2496 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | backlog = PyLong_AsLong(arg); |
| 2499 | if (backlog == -1 && PyErr_Occurred()) |
| 2500 | return NULL; |
| 2501 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 2502 | /* To avoid problems on systems that don't allow a negative backlog |
| 2503 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2504 | if (backlog < 0) |
| 2505 | backlog = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2506 | res = listen(s->sock_fd, backlog); |
| 2507 | Py_END_ALLOW_THREADS |
| 2508 | if (res < 0) |
| 2509 | return s->errorhandler(); |
| 2510 | Py_INCREF(Py_None); |
| 2511 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2514 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2515 | "listen(backlog)\n\ |
| 2516 | \n\ |
| 2517 | 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] | 2518 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
| 2519 | unaccepted connections that the system will allow before refusing new\n\ |
| 2520 | connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2521 | |
| 2522 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2523 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2524 | * 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] | 2525 | * 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] | 2526 | * 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] | 2527 | * 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] | 2528 | * also possible that we return a number of bytes smaller than the request |
| 2529 | * bytes. |
| 2530 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2531 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2532 | static Py_ssize_t |
| 2533 | 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] | 2534 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2535 | Py_ssize_t outlen = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2536 | int timeout; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2537 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | int remaining; |
| 2539 | char *read_buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2540 | #endif |
| 2541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2542 | if (!IS_SELECTABLE(s)) { |
| 2543 | select_error(); |
| 2544 | return -1; |
| 2545 | } |
| 2546 | if (len == 0) { |
| 2547 | /* If 0 bytes were requested, do nothing. */ |
| 2548 | return 0; |
| 2549 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2550 | |
| 2551 | #ifndef __VMS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2552 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2554 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | if (!timeout) |
| 2556 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2557 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | if (timeout == 1) { |
| 2560 | PyErr_SetString(socket_timeout, "timed out"); |
| 2561 | return -1; |
| 2562 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2563 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | if (outlen < 0) { |
| 2565 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2566 | NULL, so ignore its return value */ |
| 2567 | s->errorhandler(); |
| 2568 | return -1; |
| 2569 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2570 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2571 | read_buf = cbuf; |
| 2572 | remaining = len; |
| 2573 | while (remaining != 0) { |
| 2574 | unsigned int segment; |
| 2575 | int nread = -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2577 | segment = remaining /SEGMENT_SIZE; |
| 2578 | if (segment != 0) { |
| 2579 | segment = SEGMENT_SIZE; |
| 2580 | } |
| 2581 | else { |
| 2582 | segment = remaining; |
| 2583 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2584 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2585 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2586 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2587 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2588 | if (!timeout) |
| 2589 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2590 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | if (timeout == 1) { |
| 2592 | PyErr_SetString(socket_timeout, "timed out"); |
| 2593 | return -1; |
| 2594 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2595 | END_SELECT_LOOP(s) |
| 2596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | if (nread < 0) { |
| 2598 | s->errorhandler(); |
| 2599 | return -1; |
| 2600 | } |
| 2601 | if (nread != remaining) { |
| 2602 | read_buf += nread; |
| 2603 | break; |
| 2604 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2606 | remaining -= segment; |
| 2607 | read_buf += segment; |
| 2608 | } |
| 2609 | outlen = read_buf - cbuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2610 | #endif /* !__VMS */ |
| 2611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2615 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2616 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2617 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2618 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2619 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2620 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2621 | Py_ssize_t recvlen, outlen; |
| 2622 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2623 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2624 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2625 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2628 | if (recvlen < 0) { |
| 2629 | PyErr_SetString(PyExc_ValueError, |
| 2630 | "negative buffersize in recv"); |
| 2631 | return NULL; |
| 2632 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | /* Allocate a new string. */ |
| 2635 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2636 | if (buf == NULL) |
| 2637 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2639 | /* Call the guts */ |
| 2640 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2641 | if (outlen < 0) { |
| 2642 | /* An error occurred, release the string and return an |
| 2643 | error. */ |
| 2644 | Py_DECREF(buf); |
| 2645 | return NULL; |
| 2646 | } |
| 2647 | if (outlen != recvlen) { |
| 2648 | /* We did not read as many bytes as we anticipated, resize the |
| 2649 | string if possible and be successful. */ |
| 2650 | _PyBytes_Resize(&buf, outlen); |
| 2651 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2656 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2657 | "recv(buffersize[, flags]) -> data\n\ |
| 2658 | \n\ |
| 2659 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2660 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2661 | 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] | 2662 | 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] | 2663 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2664 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2665 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2666 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2667 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2668 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2672 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | Py_buffer pbuf; |
| 2674 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2675 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2678 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | &pbuf, &recvlen, &flags)) |
| 2680 | return NULL; |
| 2681 | buf = pbuf.buf; |
| 2682 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | if (recvlen < 0) { |
| 2685 | PyBuffer_Release(&pbuf); |
| 2686 | PyErr_SetString(PyExc_ValueError, |
| 2687 | "negative buffersize in recv_into"); |
| 2688 | return NULL; |
| 2689 | } |
| 2690 | if (recvlen == 0) { |
| 2691 | /* If nbytes was not specified, use the buffer's length */ |
| 2692 | recvlen = buflen; |
| 2693 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | /* Check if the buffer is large enough */ |
| 2696 | if (buflen < recvlen) { |
| 2697 | PyBuffer_Release(&pbuf); |
| 2698 | PyErr_SetString(PyExc_ValueError, |
| 2699 | "buffer too small for requested bytes"); |
| 2700 | return NULL; |
| 2701 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | /* Call the guts */ |
| 2704 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2705 | if (readlen < 0) { |
| 2706 | /* Return an error. */ |
| 2707 | PyBuffer_Release(&pbuf); |
| 2708 | return NULL; |
| 2709 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | PyBuffer_Release(&pbuf); |
| 2712 | /* Return the number of bytes read. Note that we do not do anything |
| 2713 | special here in the case that readlen < recvlen. */ |
| 2714 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2717 | PyDoc_STRVAR(recv_into_doc, |
| 2718 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2719 | \n\ |
| 2720 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2721 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2722 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2723 | \n\ |
| 2724 | See recv() for documentation about the flags."); |
| 2725 | |
| 2726 | |
| 2727 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2728 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2729 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2730 | * 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] | 2731 | * 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] | 2732 | * that it is also possible that we return a number of bytes smaller than the |
| 2733 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2734 | * |
| 2735 | * 'addr' is a return value for the address object. Note that you must decref |
| 2736 | * it yourself. |
| 2737 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2738 | static Py_ssize_t |
| 2739 | 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] | 2740 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | sock_addr_t addrbuf; |
| 2743 | int timeout; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2744 | Py_ssize_t n = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2747 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2749 | if (!getsockaddrlen(s, &addrlen)) |
| 2750 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | if (!IS_SELECTABLE(s)) { |
| 2753 | select_error(); |
| 2754 | return -1; |
| 2755 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2756 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2757 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | Py_BEGIN_ALLOW_THREADS |
| 2759 | memset(&addrbuf, 0, addrlen); |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2760 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2762 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2764 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2765 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2767 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2768 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | } |
| 2770 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | if (timeout == 1) { |
| 2773 | PyErr_SetString(socket_timeout, "timed out"); |
| 2774 | return -1; |
| 2775 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2776 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | if (n < 0) { |
| 2778 | s->errorhandler(); |
| 2779 | return -1; |
| 2780 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2783 | addrlen, s->sock_proto))) |
| 2784 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2790 | |
| 2791 | static PyObject * |
| 2792 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | PyObject *buf = NULL; |
| 2795 | PyObject *addr = NULL; |
| 2796 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2797 | int flags = 0; |
| 2798 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2799 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2800 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2801 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | if (recvlen < 0) { |
| 2804 | PyErr_SetString(PyExc_ValueError, |
| 2805 | "negative buffersize in recvfrom"); |
| 2806 | return NULL; |
| 2807 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2810 | if (buf == NULL) |
| 2811 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2814 | recvlen, flags, &addr); |
| 2815 | if (outlen < 0) { |
| 2816 | goto finally; |
| 2817 | } |
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 | if (outlen != recvlen) { |
| 2820 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2821 | string if possible and be successful. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | if (_PyBytes_Resize(&buf, outlen) < 0) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2823 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | goto finally; |
| 2825 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2828 | |
| 2829 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | Py_XDECREF(buf); |
| 2831 | Py_XDECREF(addr); |
| 2832 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2835 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2836 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2837 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2838 | 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] | 2839 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2840 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2841 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2842 | |
| 2843 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2844 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2848 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | Py_buffer pbuf; |
| 2850 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2851 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2854 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2855 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | kwlist, &pbuf, |
| 2857 | &recvlen, &flags)) |
| 2858 | return NULL; |
| 2859 | buf = pbuf.buf; |
| 2860 | buflen = pbuf.len; |
| 2861 | assert(buf != 0 && buflen > 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | if (recvlen < 0) { |
| 2864 | PyBuffer_Release(&pbuf); |
| 2865 | PyErr_SetString(PyExc_ValueError, |
| 2866 | "negative buffersize in recvfrom_into"); |
| 2867 | return NULL; |
| 2868 | } |
| 2869 | if (recvlen == 0) { |
| 2870 | /* If nbytes was not specified, use the buffer's length */ |
| 2871 | recvlen = buflen; |
| 2872 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2875 | if (readlen < 0) { |
| 2876 | PyBuffer_Release(&pbuf); |
| 2877 | /* Return an error */ |
| 2878 | Py_XDECREF(addr); |
| 2879 | return NULL; |
| 2880 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | PyBuffer_Release(&pbuf); |
| 2883 | /* Return the number of bytes read and the address. Note that we do |
| 2884 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2885 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2888 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2889 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2890 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2891 | 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] | 2892 | |
| 2893 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2894 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 2895 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 2896 | #ifdef CMSG_LEN |
| 2897 | /* |
| 2898 | * Call recvmsg() with the supplied iovec structures, flags, and |
| 2899 | * ancillary data buffer size (controllen). Returns the tuple return |
| 2900 | * value for recvmsg() or recvmsg_into(), with the first item provided |
| 2901 | * by the supplied makeval() function. makeval() will be called with |
| 2902 | * the length read and makeval_data as arguments, and must return a |
| 2903 | * new reference (which will be decrefed if there is a subsequent |
| 2904 | * error). On error, closes any file descriptors received via |
| 2905 | * SCM_RIGHTS. |
| 2906 | */ |
| 2907 | static PyObject * |
| 2908 | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
| 2909 | int flags, Py_ssize_t controllen, |
| 2910 | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
| 2911 | { |
| 2912 | ssize_t bytes_received = -1; |
| 2913 | int timeout; |
| 2914 | sock_addr_t addrbuf; |
| 2915 | socklen_t addrbuflen; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 2916 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2917 | PyObject *cmsg_list = NULL, *retval = NULL; |
| 2918 | void *controlbuf = NULL; |
| 2919 | struct cmsghdr *cmsgh; |
| 2920 | size_t cmsgdatalen = 0; |
| 2921 | int cmsg_status; |
| 2922 | |
| 2923 | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
| 2924 | ignored" when the socket is connected (Linux fills them in |
| 2925 | anyway for AF_UNIX sockets at least). Normally msg_namelen |
| 2926 | seems to be set to 0 if there's no address, but try to |
| 2927 | initialize msg_name to something that won't be mistaken for a |
| 2928 | real address if that doesn't happen. */ |
| 2929 | if (!getsockaddrlen(s, &addrbuflen)) |
| 2930 | return NULL; |
| 2931 | memset(&addrbuf, 0, addrbuflen); |
| 2932 | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
| 2933 | |
| 2934 | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
| 2935 | PyErr_SetString(PyExc_ValueError, |
| 2936 | "invalid ancillary data buffer length"); |
| 2937 | return NULL; |
| 2938 | } |
| 2939 | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
| 2940 | return PyErr_NoMemory(); |
| 2941 | |
| 2942 | /* Make the system call. */ |
| 2943 | if (!IS_SELECTABLE(s)) { |
| 2944 | select_error(); |
| 2945 | goto finally; |
| 2946 | } |
| 2947 | |
| 2948 | BEGIN_SELECT_LOOP(s) |
| 2949 | Py_BEGIN_ALLOW_THREADS; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2950 | msg.msg_name = SAS2SA(&addrbuf); |
| 2951 | msg.msg_namelen = addrbuflen; |
| 2952 | msg.msg_iov = iov; |
| 2953 | msg.msg_iovlen = iovlen; |
| 2954 | msg.msg_control = controlbuf; |
| 2955 | msg.msg_controllen = controllen; |
| 2956 | timeout = internal_select_ex(s, 0, interval); |
| 2957 | if (!timeout) |
| 2958 | bytes_received = recvmsg(s->sock_fd, &msg, flags); |
| 2959 | Py_END_ALLOW_THREADS; |
| 2960 | if (timeout == 1) { |
| 2961 | PyErr_SetString(socket_timeout, "timed out"); |
| 2962 | goto finally; |
| 2963 | } |
| 2964 | END_SELECT_LOOP(s) |
| 2965 | |
| 2966 | if (bytes_received < 0) { |
| 2967 | s->errorhandler(); |
| 2968 | goto finally; |
| 2969 | } |
| 2970 | |
| 2971 | /* Make list of (level, type, data) tuples from control messages. */ |
| 2972 | if ((cmsg_list = PyList_New(0)) == NULL) |
| 2973 | goto err_closefds; |
| 2974 | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
| 2975 | implementations didn't do so. */ |
| 2976 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 2977 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 2978 | PyObject *bytes, *tuple; |
| 2979 | int tmp; |
| 2980 | |
| 2981 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 2982 | if (cmsg_status != 0) { |
| 2983 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 2984 | "received malformed or improperly-truncated " |
| 2985 | "ancillary data", 1) == -1) |
| 2986 | goto err_closefds; |
| 2987 | } |
| 2988 | if (cmsg_status < 0) |
| 2989 | break; |
| 2990 | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2991 | PyErr_SetString(PyExc_OSError, "control message too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2992 | goto err_closefds; |
| 2993 | } |
| 2994 | |
| 2995 | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
| 2996 | cmsgdatalen); |
| 2997 | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
| 2998 | (int)cmsgh->cmsg_type, bytes); |
| 2999 | if (tuple == NULL) |
| 3000 | goto err_closefds; |
| 3001 | tmp = PyList_Append(cmsg_list, tuple); |
| 3002 | Py_DECREF(tuple); |
| 3003 | if (tmp != 0) |
| 3004 | goto err_closefds; |
| 3005 | |
| 3006 | if (cmsg_status != 0) |
| 3007 | break; |
| 3008 | } |
| 3009 | |
| 3010 | retval = Py_BuildValue("NOiN", |
| 3011 | (*makeval)(bytes_received, makeval_data), |
| 3012 | cmsg_list, |
| 3013 | (int)msg.msg_flags, |
| 3014 | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 3015 | ((msg.msg_namelen > addrbuflen) ? |
| 3016 | addrbuflen : msg.msg_namelen), |
| 3017 | s->sock_proto)); |
| 3018 | if (retval == NULL) |
| 3019 | goto err_closefds; |
| 3020 | |
| 3021 | finally: |
| 3022 | Py_XDECREF(cmsg_list); |
| 3023 | PyMem_Free(controlbuf); |
| 3024 | return retval; |
| 3025 | |
| 3026 | err_closefds: |
| 3027 | #ifdef SCM_RIGHTS |
| 3028 | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
| 3029 | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
| 3030 | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
| 3031 | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
| 3032 | if (cmsg_status < 0) |
| 3033 | break; |
| 3034 | if (cmsgh->cmsg_level == SOL_SOCKET && |
| 3035 | cmsgh->cmsg_type == SCM_RIGHTS) { |
| 3036 | size_t numfds; |
| 3037 | int *fdp; |
| 3038 | |
| 3039 | numfds = cmsgdatalen / sizeof(int); |
| 3040 | fdp = (int *)CMSG_DATA(cmsgh); |
| 3041 | while (numfds-- > 0) |
| 3042 | close(*fdp++); |
| 3043 | } |
| 3044 | if (cmsg_status != 0) |
| 3045 | break; |
| 3046 | } |
| 3047 | #endif /* SCM_RIGHTS */ |
| 3048 | goto finally; |
| 3049 | } |
| 3050 | |
| 3051 | |
| 3052 | static PyObject * |
| 3053 | makeval_recvmsg(ssize_t received, void *data) |
| 3054 | { |
| 3055 | PyObject **buf = data; |
| 3056 | |
| 3057 | if (received < PyBytes_GET_SIZE(*buf)) |
| 3058 | _PyBytes_Resize(buf, received); |
| 3059 | Py_XINCREF(*buf); |
| 3060 | return *buf; |
| 3061 | } |
| 3062 | |
| 3063 | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
| 3064 | |
| 3065 | static PyObject * |
| 3066 | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
| 3067 | { |
| 3068 | Py_ssize_t bufsize, ancbufsize = 0; |
| 3069 | int flags = 0; |
| 3070 | struct iovec iov; |
| 3071 | PyObject *buf = NULL, *retval = NULL; |
| 3072 | |
| 3073 | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
| 3074 | return NULL; |
| 3075 | |
| 3076 | if (bufsize < 0) { |
| 3077 | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
| 3078 | return NULL; |
| 3079 | } |
| 3080 | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
| 3081 | return NULL; |
| 3082 | iov.iov_base = PyBytes_AS_STRING(buf); |
| 3083 | iov.iov_len = bufsize; |
| 3084 | |
| 3085 | /* Note that we're passing a pointer to *our pointer* to the bytes |
| 3086 | object here (&buf); makeval_recvmsg() may incref the object, or |
| 3087 | deallocate it and set our pointer to NULL. */ |
| 3088 | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
| 3089 | &makeval_recvmsg, &buf); |
| 3090 | Py_XDECREF(buf); |
| 3091 | return retval; |
| 3092 | } |
| 3093 | |
| 3094 | PyDoc_STRVAR(recvmsg_doc, |
| 3095 | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
| 3096 | \n\ |
| 3097 | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
| 3098 | socket. The ancbufsize argument sets the size in bytes of the\n\ |
| 3099 | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
| 3100 | meaning that no ancillary data will be received. Appropriate buffer\n\ |
| 3101 | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
| 3102 | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3103 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3104 | same meaning as for recv().\n\ |
| 3105 | \n\ |
| 3106 | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
| 3107 | The data item is a bytes object holding the non-ancillary data\n\ |
| 3108 | received. The ancdata item is a list of zero or more tuples\n\ |
| 3109 | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
| 3110 | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
| 3111 | specifying the protocol level and protocol-specific type respectively,\n\ |
| 3112 | and cmsg_data is a bytes object holding the associated data. The\n\ |
| 3113 | msg_flags item is the bitwise OR of various flags indicating\n\ |
| 3114 | conditions on the received message; see your system documentation for\n\ |
| 3115 | details. If the receiving socket is unconnected, address is the\n\ |
| 3116 | address of the sending socket, if available; otherwise, its value is\n\ |
| 3117 | unspecified.\n\ |
| 3118 | \n\ |
| 3119 | If recvmsg() raises an exception after the system call returns, it\n\ |
| 3120 | will first attempt to close any file descriptors received via the\n\ |
| 3121 | SCM_RIGHTS mechanism."); |
| 3122 | |
| 3123 | |
| 3124 | static PyObject * |
| 3125 | makeval_recvmsg_into(ssize_t received, void *data) |
| 3126 | { |
| 3127 | return PyLong_FromSsize_t(received); |
| 3128 | } |
| 3129 | |
| 3130 | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
| 3131 | |
| 3132 | static PyObject * |
| 3133 | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
| 3134 | { |
| 3135 | Py_ssize_t ancbufsize = 0; |
| 3136 | int flags = 0; |
| 3137 | struct iovec *iovs = NULL; |
| 3138 | Py_ssize_t i, nitems, nbufs = 0; |
| 3139 | Py_buffer *bufs = NULL; |
| 3140 | PyObject *buffers_arg, *fast, *retval = NULL; |
| 3141 | |
| 3142 | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
| 3143 | &buffers_arg, &ancbufsize, &flags)) |
| 3144 | return NULL; |
| 3145 | |
| 3146 | if ((fast = PySequence_Fast(buffers_arg, |
| 3147 | "recvmsg_into() argument 1 must be an " |
| 3148 | "iterable")) == NULL) |
| 3149 | return NULL; |
| 3150 | nitems = PySequence_Fast_GET_SIZE(fast); |
| 3151 | if (nitems > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3152 | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3153 | goto finally; |
| 3154 | } |
| 3155 | |
| 3156 | /* Fill in an iovec for each item, and save the Py_buffer |
| 3157 | structs to release afterwards. */ |
| 3158 | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
| 3159 | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
| 3160 | PyErr_NoMemory(); |
| 3161 | goto finally; |
| 3162 | } |
| 3163 | for (; nbufs < nitems; nbufs++) { |
| 3164 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
| 3165 | "w*;recvmsg_into() argument 1 must be an iterable " |
| 3166 | "of single-segment read-write buffers", |
| 3167 | &bufs[nbufs])) |
| 3168 | goto finally; |
| 3169 | iovs[nbufs].iov_base = bufs[nbufs].buf; |
| 3170 | iovs[nbufs].iov_len = bufs[nbufs].len; |
| 3171 | } |
| 3172 | |
| 3173 | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
| 3174 | &makeval_recvmsg_into, NULL); |
| 3175 | finally: |
| 3176 | for (i = 0; i < nbufs; i++) |
| 3177 | PyBuffer_Release(&bufs[i]); |
| 3178 | PyMem_Free(bufs); |
| 3179 | PyMem_Free(iovs); |
| 3180 | Py_DECREF(fast); |
| 3181 | return retval; |
| 3182 | } |
| 3183 | |
| 3184 | PyDoc_STRVAR(recvmsg_into_doc, |
| 3185 | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
| 3186 | \n\ |
| 3187 | Receive normal data and ancillary data from the socket, scattering the\n\ |
| 3188 | non-ancillary data into a series of buffers. The buffers argument\n\ |
| 3189 | must be an iterable of objects that export writable buffers\n\ |
| 3190 | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
| 3191 | of the non-ancillary data until it has all been written or there are\n\ |
| 3192 | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
| 3193 | the internal buffer used to receive the ancillary data; it defaults to\n\ |
| 3194 | 0, meaning that no ancillary data will be received. Appropriate\n\ |
| 3195 | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
| 3196 | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
| 3197 | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
| 3198 | same meaning as for recv().\n\ |
| 3199 | \n\ |
| 3200 | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
| 3201 | The nbytes item is the total number of bytes of non-ancillary data\n\ |
| 3202 | written into the buffers. The ancdata item is a list of zero or more\n\ |
| 3203 | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
| 3204 | data (control messages) received: cmsg_level and cmsg_type are\n\ |
| 3205 | integers specifying the protocol level and protocol-specific type\n\ |
| 3206 | respectively, and cmsg_data is a bytes object holding the associated\n\ |
| 3207 | data. The msg_flags item is the bitwise OR of various flags\n\ |
| 3208 | indicating conditions on the received message; see your system\n\ |
| 3209 | documentation for details. If the receiving socket is unconnected,\n\ |
| 3210 | address is the address of the sending socket, if available; otherwise,\n\ |
| 3211 | its value is unspecified.\n\ |
| 3212 | \n\ |
| 3213 | If recvmsg_into() raises an exception after the system call returns,\n\ |
| 3214 | it will first attempt to close any file descriptors received via the\n\ |
| 3215 | SCM_RIGHTS mechanism."); |
| 3216 | #endif /* CMSG_LEN */ |
| 3217 | |
| 3218 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3219 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3220 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3221 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3222 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3225 | Py_ssize_t len, n = -1; |
| 3226 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 3230 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | if (!IS_SELECTABLE(s)) { |
| 3233 | PyBuffer_Release(&pbuf); |
| 3234 | return select_error(); |
| 3235 | } |
| 3236 | buf = pbuf.buf; |
| 3237 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3239 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3241 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3242 | if (!timeout) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3243 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3245 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3247 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3250 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3251 | PyErr_SetString(socket_timeout, "timed out"); |
| 3252 | return NULL; |
| 3253 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3254 | END_SELECT_LOOP(s) |
| 3255 | |
| 3256 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | if (n < 0) |
| 3258 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3259 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3262 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3263 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3264 | \n\ |
| 3265 | 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] | 3266 | 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] | 3267 | 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] | 3268 | |
| 3269 | |
| 3270 | /* s.sendall(data [,flags]) method */ |
| 3271 | |
| 3272 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3273 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3276 | Py_ssize_t len, n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3277 | int flags = 0, timeout, saved_errno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3278 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 3281 | return NULL; |
| 3282 | buf = pbuf.buf; |
| 3283 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | if (!IS_SELECTABLE(s)) { |
| 3286 | PyBuffer_Release(&pbuf); |
| 3287 | return select_error(); |
| 3288 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | do { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3291 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3292 | timeout = internal_select(s, 1); |
| 3293 | n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3294 | if (!timeout) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3295 | #ifdef __VMS |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3296 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3297 | #else |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3298 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3299 | #endif |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3300 | } |
| 3301 | Py_END_ALLOW_THREADS |
| 3302 | if (timeout == 1) { |
| 3303 | PyBuffer_Release(&pbuf); |
| 3304 | PyErr_SetString(socket_timeout, "timed out"); |
| 3305 | return NULL; |
| 3306 | } |
| 3307 | /* PyErr_CheckSignals() might change errno */ |
| 3308 | saved_errno = errno; |
| 3309 | /* We must run our signal handlers before looping again. |
| 3310 | send() can return a successful partial write when it is |
| 3311 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 3312 | if (PyErr_CheckSignals()) { |
| 3313 | PyBuffer_Release(&pbuf); |
| 3314 | return NULL; |
| 3315 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | if (n < 0) { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3317 | /* If interrupted, try again */ |
| 3318 | if (saved_errno == EINTR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | continue; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 3320 | else |
| 3321 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | } |
| 3323 | buf += n; |
| 3324 | len -= n; |
| 3325 | } while (len > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | if (n < 0) |
| 3329 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | Py_INCREF(Py_None); |
| 3332 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3335 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 3336 | "sendall(data[, flags])\n\ |
| 3337 | \n\ |
| 3338 | Send a data string to the socket. For the optional flags\n\ |
| 3339 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 3340 | 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] | 3341 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3342 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3343 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 3344 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3345 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3346 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3347 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | Py_buffer pbuf; |
| 3350 | PyObject *addro; |
| 3351 | char *buf; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3352 | Py_ssize_t len, arglen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3353 | sock_addr_t addrbuf; |
| 3354 | int addrlen, n = -1, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | flags = 0; |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3357 | arglen = PyTuple_Size(args); |
| 3358 | switch (arglen) { |
| 3359 | case 2: |
| 3360 | PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); |
| 3361 | break; |
| 3362 | case 3: |
| 3363 | PyArg_ParseTuple(args, "y*iO:sendto", |
| 3364 | &pbuf, &flags, &addro); |
| 3365 | break; |
| 3366 | default: |
| 3367 | PyErr_Format(PyExc_TypeError, |
| 3368 | "sendto() takes 2 or 3 arguments (%d given)", |
| 3369 | arglen); |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 3370 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | } |
Antoine Pitrou | 5e98141 | 2011-03-17 22:38:37 +0100 | [diff] [blame] | 3372 | if (PyErr_Occurred()) |
| 3373 | return NULL; |
| 3374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | buf = pbuf.buf; |
| 3376 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | if (!IS_SELECTABLE(s)) { |
| 3379 | PyBuffer_Release(&pbuf); |
| 3380 | return select_error(); |
| 3381 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 3382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3383 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 3384 | PyBuffer_Release(&pbuf); |
| 3385 | return NULL; |
| 3386 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3387 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3388 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3390 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3391 | if (!timeout) |
| 3392 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 3393 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3396 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | PyErr_SetString(socket_timeout, "timed out"); |
| 3398 | return NULL; |
| 3399 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 3400 | END_SELECT_LOOP(s) |
| 3401 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | if (n < 0) |
| 3403 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 3404 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3407 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3408 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3409 | \n\ |
| 3410 | 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] | 3411 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3412 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3413 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3414 | /* The sendmsg() and recvmsg[_into]() methods require a working |
| 3415 | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
| 3416 | #ifdef CMSG_LEN |
| 3417 | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
| 3418 | |
| 3419 | static PyObject * |
| 3420 | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
| 3421 | { |
| 3422 | Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
| 3423 | Py_buffer *databufs = NULL; |
| 3424 | struct iovec *iovs = NULL; |
| 3425 | sock_addr_t addrbuf; |
Charles-François Natali | e9e95ae | 2011-08-24 21:40:53 +0200 | [diff] [blame] | 3426 | struct msghdr msg = {0}; |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3427 | struct cmsginfo { |
| 3428 | int level; |
| 3429 | int type; |
| 3430 | Py_buffer data; |
| 3431 | } *cmsgs = NULL; |
| 3432 | void *controlbuf = NULL; |
| 3433 | size_t controllen, controllen_last; |
| 3434 | ssize_t bytes_sent = -1; |
| 3435 | int addrlen, timeout, flags = 0; |
| 3436 | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL, |
| 3437 | *cmsg_fast = NULL, *retval = NULL; |
| 3438 | |
| 3439 | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
| 3440 | &data_arg, &cmsg_arg, &flags, &addr_arg)) |
| 3441 | return NULL; |
| 3442 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3443 | /* Parse destination address. */ |
| 3444 | if (addr_arg != NULL && addr_arg != Py_None) { |
| 3445 | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
| 3446 | goto finally; |
| 3447 | msg.msg_name = &addrbuf; |
| 3448 | msg.msg_namelen = addrlen; |
| 3449 | } |
| 3450 | |
| 3451 | /* Fill in an iovec for each message part, and save the Py_buffer |
| 3452 | structs to release afterwards. */ |
| 3453 | if ((data_fast = PySequence_Fast(data_arg, |
| 3454 | "sendmsg() argument 1 must be an " |
| 3455 | "iterable")) == NULL) |
| 3456 | goto finally; |
| 3457 | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
| 3458 | if (ndataparts > INT_MAX) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3459 | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3460 | goto finally; |
| 3461 | } |
| 3462 | msg.msg_iovlen = ndataparts; |
| 3463 | if (ndataparts > 0 && |
| 3464 | ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL || |
| 3465 | (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) { |
| 3466 | PyErr_NoMemory(); |
| 3467 | goto finally; |
| 3468 | } |
| 3469 | for (; ndatabufs < ndataparts; ndatabufs++) { |
| 3470 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
| 3471 | "y*;sendmsg() argument 1 must be an iterable of " |
| 3472 | "buffer-compatible objects", |
| 3473 | &databufs[ndatabufs])) |
| 3474 | goto finally; |
| 3475 | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
| 3476 | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
| 3477 | } |
| 3478 | |
| 3479 | if (cmsg_arg == NULL) |
| 3480 | ncmsgs = 0; |
| 3481 | else { |
| 3482 | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
| 3483 | "sendmsg() argument 2 must be an " |
| 3484 | "iterable")) == NULL) |
| 3485 | goto finally; |
| 3486 | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
| 3487 | } |
| 3488 | |
| 3489 | #ifndef CMSG_SPACE |
| 3490 | if (ncmsgs > 1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3491 | PyErr_SetString(PyExc_OSError, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3492 | "sending multiple control messages is not supported " |
| 3493 | "on this system"); |
| 3494 | goto finally; |
| 3495 | } |
| 3496 | #endif |
| 3497 | /* Save level, type and Py_buffer for each control message, |
| 3498 | and calculate total size. */ |
| 3499 | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
| 3500 | PyErr_NoMemory(); |
| 3501 | goto finally; |
| 3502 | } |
| 3503 | controllen = controllen_last = 0; |
| 3504 | while (ncmsgbufs < ncmsgs) { |
| 3505 | size_t bufsize, space; |
| 3506 | |
| 3507 | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
| 3508 | "(iiy*):[sendmsg() ancillary data items]", |
| 3509 | &cmsgs[ncmsgbufs].level, |
| 3510 | &cmsgs[ncmsgbufs].type, |
| 3511 | &cmsgs[ncmsgbufs].data)) |
| 3512 | goto finally; |
| 3513 | bufsize = cmsgs[ncmsgbufs++].data.len; |
| 3514 | |
| 3515 | #ifdef CMSG_SPACE |
| 3516 | if (!get_CMSG_SPACE(bufsize, &space)) { |
| 3517 | #else |
| 3518 | if (!get_CMSG_LEN(bufsize, &space)) { |
| 3519 | #endif |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3520 | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3521 | goto finally; |
| 3522 | } |
| 3523 | controllen += space; |
| 3524 | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 3525 | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3526 | goto finally; |
| 3527 | } |
| 3528 | controllen_last = controllen; |
| 3529 | } |
| 3530 | |
| 3531 | /* Construct ancillary data block from control message info. */ |
| 3532 | if (ncmsgbufs > 0) { |
| 3533 | struct cmsghdr *cmsgh = NULL; |
| 3534 | |
| 3535 | if ((msg.msg_control = controlbuf = |
| 3536 | PyMem_Malloc(controllen)) == NULL) { |
| 3537 | PyErr_NoMemory(); |
| 3538 | goto finally; |
| 3539 | } |
| 3540 | msg.msg_controllen = controllen; |
| 3541 | |
| 3542 | /* Need to zero out the buffer as a workaround for glibc's |
| 3543 | CMSG_NXTHDR() implementation. After getting the pointer to |
| 3544 | the next header, it checks its (uninitialized) cmsg_len |
| 3545 | member to see if the "message" fits in the buffer, and |
| 3546 | returns NULL if it doesn't. Zero-filling the buffer |
| 3547 | ensures that that doesn't happen. */ |
| 3548 | memset(controlbuf, 0, controllen); |
| 3549 | |
| 3550 | for (i = 0; i < ncmsgbufs; i++) { |
| 3551 | size_t msg_len, data_len = cmsgs[i].data.len; |
| 3552 | int enough_space = 0; |
| 3553 | |
| 3554 | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
| 3555 | if (cmsgh == NULL) { |
| 3556 | PyErr_Format(PyExc_RuntimeError, |
| 3557 | "unexpected NULL result from %s()", |
| 3558 | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
| 3559 | goto finally; |
| 3560 | } |
| 3561 | if (!get_CMSG_LEN(data_len, &msg_len)) { |
| 3562 | PyErr_SetString(PyExc_RuntimeError, |
| 3563 | "item size out of range for CMSG_LEN()"); |
| 3564 | goto finally; |
| 3565 | } |
| 3566 | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
| 3567 | size_t space; |
| 3568 | |
| 3569 | cmsgh->cmsg_len = msg_len; |
| 3570 | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
| 3571 | enough_space = (space >= data_len); |
| 3572 | } |
| 3573 | if (!enough_space) { |
| 3574 | PyErr_SetString(PyExc_RuntimeError, |
| 3575 | "ancillary data does not fit in calculated " |
| 3576 | "space"); |
| 3577 | goto finally; |
| 3578 | } |
| 3579 | cmsgh->cmsg_level = cmsgs[i].level; |
| 3580 | cmsgh->cmsg_type = cmsgs[i].type; |
| 3581 | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | /* Make the system call. */ |
| 3586 | if (!IS_SELECTABLE(s)) { |
| 3587 | select_error(); |
| 3588 | goto finally; |
| 3589 | } |
| 3590 | |
| 3591 | BEGIN_SELECT_LOOP(s) |
| 3592 | Py_BEGIN_ALLOW_THREADS; |
| 3593 | timeout = internal_select_ex(s, 1, interval); |
| 3594 | if (!timeout) |
| 3595 | bytes_sent = sendmsg(s->sock_fd, &msg, flags); |
| 3596 | Py_END_ALLOW_THREADS; |
| 3597 | if (timeout == 1) { |
| 3598 | PyErr_SetString(socket_timeout, "timed out"); |
| 3599 | goto finally; |
| 3600 | } |
| 3601 | END_SELECT_LOOP(s) |
| 3602 | |
| 3603 | if (bytes_sent < 0) { |
| 3604 | s->errorhandler(); |
| 3605 | goto finally; |
| 3606 | } |
| 3607 | retval = PyLong_FromSsize_t(bytes_sent); |
| 3608 | |
| 3609 | finally: |
| 3610 | PyMem_Free(controlbuf); |
| 3611 | for (i = 0; i < ncmsgbufs; i++) |
| 3612 | PyBuffer_Release(&cmsgs[i].data); |
| 3613 | PyMem_Free(cmsgs); |
| 3614 | Py_XDECREF(cmsg_fast); |
| 3615 | for (i = 0; i < ndatabufs; i++) |
| 3616 | PyBuffer_Release(&databufs[i]); |
| 3617 | PyMem_Free(databufs); |
| 3618 | PyMem_Free(iovs); |
| 3619 | Py_XDECREF(data_fast); |
| 3620 | return retval; |
| 3621 | } |
| 3622 | |
| 3623 | PyDoc_STRVAR(sendmsg_doc, |
| 3624 | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
| 3625 | \n\ |
| 3626 | Send normal and ancillary data to the socket, gathering the\n\ |
| 3627 | non-ancillary data from a series of buffers and concatenating it into\n\ |
| 3628 | a single message. The buffers argument specifies the non-ancillary\n\ |
| 3629 | data as an iterable of buffer-compatible objects (e.g. bytes objects).\n\ |
| 3630 | The ancdata argument specifies the ancillary data (control messages)\n\ |
| 3631 | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
| 3632 | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
| 3633 | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
| 3634 | is a buffer-compatible object holding the associated data. The flags\n\ |
| 3635 | argument defaults to 0 and has the same meaning as for send(). If\n\ |
| 3636 | address is supplied and not None, it sets a destination address for\n\ |
| 3637 | the message. The return value is the number of bytes of non-ancillary\n\ |
| 3638 | data sent."); |
| 3639 | #endif /* CMSG_LEN */ |
| 3640 | |
| 3641 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3642 | /* s.shutdown(how) method */ |
| 3643 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3644 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3645 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3647 | int how; |
| 3648 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 3649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3650 | how = PyLong_AsLong(arg); |
| 3651 | if (how == -1 && PyErr_Occurred()) |
| 3652 | return NULL; |
| 3653 | Py_BEGIN_ALLOW_THREADS |
| 3654 | res = shutdown(s->sock_fd, how); |
| 3655 | Py_END_ALLOW_THREADS |
| 3656 | if (res < 0) |
| 3657 | return s->errorhandler(); |
| 3658 | Py_INCREF(Py_None); |
| 3659 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3662 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3663 | "shutdown(flag)\n\ |
| 3664 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3665 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3666 | 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] | 3667 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3668 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3669 | static PyObject* |
| 3670 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3671 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3672 | unsigned long cmd = SIO_RCVALL; |
| 3673 | PyObject *argO; |
| 3674 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3676 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3677 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3679 | switch (cmd) { |
| 3680 | case SIO_RCVALL: { |
| 3681 | unsigned int option = RCVALL_ON; |
| 3682 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3683 | return NULL; |
| 3684 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3685 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3686 | return set_error(); |
| 3687 | } |
| 3688 | return PyLong_FromUnsignedLong(recv); } |
| 3689 | case SIO_KEEPALIVE_VALS: { |
| 3690 | struct tcp_keepalive ka; |
| 3691 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3692 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3693 | return NULL; |
| 3694 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3695 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3696 | return set_error(); |
| 3697 | } |
| 3698 | return PyLong_FromUnsignedLong(recv); } |
| 3699 | default: |
| 3700 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3701 | return NULL; |
| 3702 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3703 | } |
| 3704 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3705 | "ioctl(cmd, option) -> long\n\ |
| 3706 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 3707 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3708 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3709 | 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] | 3710 | #endif |
| 3711 | |
| 3712 | #if defined(MS_WINDOWS) |
| 3713 | static PyObject* |
| 3714 | sock_share(PySocketSockObject *s, PyObject *arg) |
| 3715 | { |
| 3716 | WSAPROTOCOL_INFO info; |
| 3717 | DWORD processId; |
| 3718 | int result; |
| 3719 | |
| 3720 | if (!PyArg_ParseTuple(arg, "I", &processId)) |
| 3721 | return NULL; |
| 3722 | |
| 3723 | Py_BEGIN_ALLOW_THREADS |
| 3724 | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
| 3725 | Py_END_ALLOW_THREADS |
| 3726 | if (result == SOCKET_ERROR) |
| 3727 | return set_error(); |
| 3728 | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
| 3729 | } |
| 3730 | PyDoc_STRVAR(sock_share_doc, |
| 3731 | "share(process_id) -> bytes\n\ |
| 3732 | \n\ |
| 3733 | Share the socket with another process. The target process id\n\ |
| 3734 | must be provided and the resulting bytes object passed to the target\n\ |
| 3735 | process. There the shared socket can be instantiated by calling\n\ |
| 3736 | socket.fromshare()."); |
| 3737 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3738 | |
| 3739 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3740 | |
| 3741 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3742 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3743 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3744 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3745 | accept_doc}, |
| 3746 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3747 | bind_doc}, |
| 3748 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3749 | close_doc}, |
| 3750 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3751 | connect_doc}, |
| 3752 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3753 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 3754 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 3755 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3756 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3757 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3758 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3760 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3761 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3762 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3763 | METH_NOARGS, getsockname_doc}, |
| 3764 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3765 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 3766 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3768 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3769 | #endif |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3770 | #if defined(MS_WINDOWS) |
| 3771 | {"share", (PyCFunction)sock_share, METH_VARARGS, |
| 3772 | sock_share_doc}, |
| 3773 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 3775 | listen_doc}, |
| 3776 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3777 | recv_doc}, |
| 3778 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3779 | recv_into_doc}, |
| 3780 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3781 | recvfrom_doc}, |
| 3782 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3783 | recvfrom_into_doc}, |
| 3784 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3785 | send_doc}, |
| 3786 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3787 | sendall_doc}, |
| 3788 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3789 | sendto_doc}, |
| 3790 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3791 | setblocking_doc}, |
| 3792 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3793 | settimeout_doc}, |
| 3794 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3795 | gettimeout_doc}, |
| 3796 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3797 | setsockopt_doc}, |
| 3798 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3799 | shutdown_doc}, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3800 | #ifdef CMSG_LEN |
| 3801 | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
| 3802 | recvmsg_doc}, |
| 3803 | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
| 3804 | recvmsg_into_doc,}, |
| 3805 | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
| 3806 | sendmsg_doc}, |
| 3807 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3809 | }; |
| 3810 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3811 | /* SockObject members */ |
| 3812 | static PyMemberDef sock_memberlist[] = { |
| 3813 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3814 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3815 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3816 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3817 | {0}, |
| 3818 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3819 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3820 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3821 | First close the file description. */ |
| 3822 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3823 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3824 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3825 | { |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3826 | if (s->sock_fd != -1) { |
| 3827 | PyObject *exc, *val, *tb; |
| 3828 | Py_ssize_t old_refcount = Py_REFCNT(s); |
| 3829 | ++Py_REFCNT(s); |
| 3830 | PyErr_Fetch(&exc, &val, &tb); |
| 3831 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 1, |
| 3832 | "unclosed %R", s)) |
| 3833 | /* Spurious errors can appear at shutdown */ |
| 3834 | if (PyErr_ExceptionMatches(PyExc_Warning)) |
| 3835 | PyErr_WriteUnraisable((PyObject *) s); |
| 3836 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | (void) SOCKETCLOSE(s->sock_fd); |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 3838 | Py_REFCNT(s) = old_refcount; |
| 3839 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3840 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3843 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3844 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3845 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3846 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3847 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3848 | if (s->sock_fd > LONG_MAX) { |
| 3849 | /* this can occur on Win64, and actually there is a special |
| 3850 | ugly printf formatter for decimal pointer length integer |
| 3851 | printing, only bother if necessary*/ |
| 3852 | PyErr_SetString(PyExc_OverflowError, |
| 3853 | "no printf formatter to display " |
| 3854 | "the socket descriptor in decimal"); |
| 3855 | return NULL; |
| 3856 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3857 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | return PyUnicode_FromFormat( |
| 3859 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
| 3860 | (long)s->sock_fd, s->sock_family, |
| 3861 | s->sock_type, |
| 3862 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3866 | /* Create a new, uninitialized socket object. */ |
| 3867 | |
| 3868 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3869 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3871 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 | new = type->tp_alloc(type, 0); |
| 3874 | if (new != NULL) { |
| 3875 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3876 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3877 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 3878 | } |
| 3879 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | |
| 3883 | /* Initialize a new socket object. */ |
| 3884 | |
| 3885 | /*ARGSUSED*/ |
| 3886 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3887 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3889 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3890 | PyObject *fdobj = NULL; |
| 3891 | SOCKET_T fd = INVALID_SOCKET; |
| 3892 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3893 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
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 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 3896 | "|iiiO:socket", keywords, |
| 3897 | &family, &type, &proto, &fdobj)) |
| 3898 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | if (fdobj != NULL && fdobj != Py_None) { |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 3901 | #ifdef MS_WINDOWS |
| 3902 | /* recreate a socket that was duplicated */ |
| 3903 | if (PyBytes_Check(fdobj)) { |
| 3904 | WSAPROTOCOL_INFO info; |
| 3905 | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
| 3906 | PyErr_Format(PyExc_ValueError, |
| 3907 | "socket descriptor string has wrong size, " |
| 3908 | "should be %zu bytes.", sizeof(info)); |
| 3909 | return -1; |
| 3910 | } |
| 3911 | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
| 3912 | Py_BEGIN_ALLOW_THREADS |
| 3913 | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 3914 | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
| 3915 | Py_END_ALLOW_THREADS |
| 3916 | if (fd == INVALID_SOCKET) { |
| 3917 | set_error(); |
| 3918 | return -1; |
| 3919 | } |
| 3920 | family = info.iAddressFamily; |
| 3921 | type = info.iSocketType; |
| 3922 | proto = info.iProtocol; |
| 3923 | } |
| 3924 | else |
| 3925 | #endif |
| 3926 | { |
| 3927 | fd = PyLong_AsSocket_t(fdobj); |
| 3928 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 3929 | return -1; |
| 3930 | if (fd == INVALID_SOCKET) { |
| 3931 | PyErr_SetString(PyExc_ValueError, |
| 3932 | "can't use invalid socket value"); |
| 3933 | return -1; |
| 3934 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | else { |
| 3938 | Py_BEGIN_ALLOW_THREADS |
| 3939 | fd = socket(family, type, proto); |
| 3940 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3942 | if (fd == INVALID_SOCKET) { |
| 3943 | set_error(); |
| 3944 | return -1; |
| 3945 | } |
| 3946 | } |
| 3947 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3949 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3950 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3954 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3955 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3956 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 3958 | "_socket.socket", /* tp_name */ |
| 3959 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 3960 | 0, /* tp_itemsize */ |
| 3961 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 3962 | 0, /* tp_print */ |
| 3963 | 0, /* tp_getattr */ |
| 3964 | 0, /* tp_setattr */ |
| 3965 | 0, /* tp_reserved */ |
| 3966 | (reprfunc)sock_repr, /* tp_repr */ |
| 3967 | 0, /* tp_as_number */ |
| 3968 | 0, /* tp_as_sequence */ |
| 3969 | 0, /* tp_as_mapping */ |
| 3970 | 0, /* tp_hash */ |
| 3971 | 0, /* tp_call */ |
| 3972 | 0, /* tp_str */ |
| 3973 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3974 | 0, /* tp_setattro */ |
| 3975 | 0, /* tp_as_buffer */ |
| 3976 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3977 | sock_doc, /* tp_doc */ |
| 3978 | 0, /* tp_traverse */ |
| 3979 | 0, /* tp_clear */ |
| 3980 | 0, /* tp_richcompare */ |
| 3981 | 0, /* tp_weaklistoffset */ |
| 3982 | 0, /* tp_iter */ |
| 3983 | 0, /* tp_iternext */ |
| 3984 | sock_methods, /* tp_methods */ |
| 3985 | sock_memberlist, /* tp_members */ |
| 3986 | 0, /* tp_getset */ |
| 3987 | 0, /* tp_base */ |
| 3988 | 0, /* tp_dict */ |
| 3989 | 0, /* tp_descr_get */ |
| 3990 | 0, /* tp_descr_set */ |
| 3991 | 0, /* tp_dictoffset */ |
| 3992 | sock_initobj, /* tp_init */ |
| 3993 | PyType_GenericAlloc, /* tp_alloc */ |
| 3994 | sock_new, /* tp_new */ |
| 3995 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3996 | }; |
| 3997 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3998 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3999 | /* Python interface to gethostname(). */ |
| 4000 | |
| 4001 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4002 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4003 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4004 | { |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4005 | #ifdef MS_WINDOWS |
| 4006 | /* Don't use winsock's gethostname, as this returns the ANSI |
| 4007 | version of the hostname, whereas we need a Unicode string. |
| 4008 | Otherwise, gethostname apparently also returns the DNS name. */ |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4009 | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4010 | DWORD size = Py_ARRAY_LENGTH(buf); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4011 | wchar_t *name; |
Hirokazu Yamamoto | 09fff7a | 2010-11-05 17:24:13 +0000 | [diff] [blame] | 4012 | PyObject *result; |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4013 | |
| 4014 | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4015 | return PyUnicode_FromWideChar(buf, size); |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4016 | |
| 4017 | if (GetLastError() != ERROR_MORE_DATA) |
| 4018 | return PyErr_SetFromWindowsErr(0); |
| 4019 | |
| 4020 | if (size == 0) |
| 4021 | return PyUnicode_New(0, 0); |
| 4022 | |
| 4023 | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
| 4024 | names */ |
| 4025 | name = PyMem_Malloc(size * sizeof(wchar_t)); |
| 4026 | if (!name) |
| 4027 | return NULL; |
| 4028 | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
| 4029 | name, |
| 4030 | &size)) |
| 4031 | { |
| 4032 | PyMem_Free(name); |
| 4033 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4034 | } |
Victor Stinner | 7416897 | 2011-11-17 01:11:36 +0100 | [diff] [blame] | 4035 | |
| 4036 | result = PyUnicode_FromWideChar(name, size); |
| 4037 | PyMem_Free(name); |
| 4038 | return result; |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4039 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4040 | char buf[1024]; |
| 4041 | int res; |
| 4042 | Py_BEGIN_ALLOW_THREADS |
| 4043 | res = gethostname(buf, (int) sizeof buf - 1); |
| 4044 | Py_END_ALLOW_THREADS |
| 4045 | if (res < 0) |
| 4046 | return set_error(); |
| 4047 | buf[sizeof buf - 1] = '\0'; |
| 4048 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 72f4842 | 2010-10-29 18:20:08 +0000 | [diff] [blame] | 4049 | #endif |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 4050 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4052 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4053 | "gethostname() -> string\n\ |
| 4054 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4055 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4056 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 4057 | #ifdef HAVE_SETHOSTNAME |
| 4058 | PyDoc_STRVAR(sethostname_doc, |
| 4059 | "sethostname(name)\n\n\ |
| 4060 | Sets the hostname to name."); |
| 4061 | |
| 4062 | static PyObject * |
| 4063 | socket_sethostname(PyObject *self, PyObject *args) |
| 4064 | { |
| 4065 | PyObject *hnobj; |
| 4066 | Py_buffer buf; |
| 4067 | int res, flag = 0; |
| 4068 | |
| 4069 | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
| 4070 | PyErr_Clear(); |
| 4071 | if (!PyArg_ParseTuple(args, "O&:sethostname", |
| 4072 | PyUnicode_FSConverter, &hnobj)) |
| 4073 | return NULL; |
| 4074 | flag = 1; |
| 4075 | } |
| 4076 | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
| 4077 | if (!res) { |
| 4078 | res = sethostname(buf.buf, buf.len); |
| 4079 | PyBuffer_Release(&buf); |
| 4080 | } |
| 4081 | if (flag) |
| 4082 | Py_DECREF(hnobj); |
| 4083 | if (res) |
| 4084 | return set_error(); |
| 4085 | Py_RETURN_NONE; |
| 4086 | } |
| 4087 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 4088 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4089 | /* Python interface to gethostbyname(name). */ |
| 4090 | |
| 4091 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4092 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4093 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | char *name; |
| 4096 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4097 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4098 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4099 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4100 | return NULL; |
| 4101 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4102 | goto finally; |
| 4103 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 4104 | finally: |
| 4105 | PyMem_Free(name); |
| 4106 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4109 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4110 | "gethostbyname(host) -> address\n\ |
| 4111 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4112 | 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] | 4113 | |
| 4114 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4115 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 4116 | |
| 4117 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4118 | 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] | 4119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | char **pch; |
| 4121 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 4122 | PyObject *name_list = (PyObject *)NULL; |
| 4123 | PyObject *addr_list = (PyObject *)NULL; |
| 4124 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | if (h == NULL) { |
| 4127 | /* Let's get real error message to return */ |
| 4128 | set_herror(h_errno); |
| 4129 | return NULL; |
| 4130 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4132 | if (h->h_addrtype != af) { |
| 4133 | /* Let's get real error message to return */ |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 4134 | errno = EAFNOSUPPORT; |
| 4135 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4136 | return NULL; |
| 4137 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4139 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | case AF_INET: |
| 4142 | if (alen < sizeof(struct sockaddr_in)) |
| 4143 | return NULL; |
| 4144 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4145 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4146 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | case AF_INET6: |
| 4148 | if (alen < sizeof(struct sockaddr_in6)) |
| 4149 | return NULL; |
| 4150 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4151 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4153 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | if ((name_list = PyList_New(0)) == NULL) |
| 4156 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | if ((addr_list = PyList_New(0)) == NULL) |
| 4159 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4161 | /* SF #1511317: h_aliases can be NULL */ |
| 4162 | if (h->h_aliases) { |
| 4163 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 4164 | int status; |
| 4165 | tmp = PyUnicode_FromString(*pch); |
| 4166 | if (tmp == NULL) |
| 4167 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4169 | status = PyList_Append(name_list, tmp); |
| 4170 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | if (status) |
| 4173 | goto err; |
| 4174 | } |
| 4175 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4177 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 4178 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4180 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4182 | case AF_INET: |
| 4183 | { |
| 4184 | struct sockaddr_in sin; |
| 4185 | memset(&sin, 0, sizeof(sin)); |
| 4186 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4187 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4188 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4189 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4190 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 4191 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4193 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 4194 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 4195 | break; |
| 4196 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4197 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4198 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | case AF_INET6: |
| 4200 | { |
| 4201 | struct sockaddr_in6 sin6; |
| 4202 | memset(&sin6, 0, sizeof(sin6)); |
| 4203 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4204 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4206 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4207 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 4208 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 4209 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 4212 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 4213 | break; |
| 4214 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4215 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4217 | default: /* can't happen */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4218 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | "unsupported address family"); |
| 4220 | return NULL; |
| 4221 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4223 | if (tmp == NULL) |
| 4224 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | status = PyList_Append(addr_list, tmp); |
| 4227 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4229 | if (status) |
| 4230 | goto err; |
| 4231 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 4232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4233 | 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] | 4234 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4235 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4236 | Py_XDECREF(name_list); |
| 4237 | Py_XDECREF(addr_list); |
| 4238 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | |
| 4242 | /* Python interface to gethostbyname_ex(name). */ |
| 4243 | |
| 4244 | /*ARGSUSED*/ |
| 4245 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4246 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | char *name; |
| 4249 | struct hostent *h; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4250 | sock_addr_t addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4252 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4253 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4254 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4255 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4256 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4257 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | char buf[16384]; |
| 4259 | int buf_len = (sizeof buf) - 1; |
| 4260 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4261 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4262 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4263 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4264 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4265 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4266 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4267 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4268 | return NULL; |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4269 | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4270 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4271 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4272 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4273 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4274 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4275 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4276 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4277 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4278 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4279 | memset((void *) &data, '\0', sizeof(data)); |
| 4280 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 4281 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4282 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4283 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4284 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4286 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4287 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4288 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4289 | Py_END_ALLOW_THREADS |
| 4290 | /* Some C libraries would require addr.__ss_family instead of |
| 4291 | addr.ss_family. |
| 4292 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 4293 | access sa_family. */ |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4294 | sa = SAS2SA(&addr); |
| 4295 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4296 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4297 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 4299 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4300 | finally: |
| 4301 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4305 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 4306 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 4307 | \n\ |
| 4308 | 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] | 4309 | 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] | 4310 | |
| 4311 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4312 | /* Python interface to gethostbyaddr(IP). */ |
| 4313 | |
| 4314 | /*ARGSUSED*/ |
| 4315 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4316 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4317 | { |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4318 | sock_addr_t addr; |
| 4319 | struct sockaddr *sa = SAS2SA(&addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | char *ip_num; |
| 4321 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4322 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4323 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4324 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4325 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4326 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4327 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | /* glibcs up to 2.10 assume that the buf argument to |
| 4329 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 4330 | does not ensure. The attribute below instructs the compiler |
| 4331 | to maintain this alignment. */ |
| 4332 | char buf[16384] Py_ALIGNED(8); |
| 4333 | int buf_len = (sizeof buf) - 1; |
| 4334 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4335 | #endif |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4336 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4337 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4338 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4339 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4340 | char *ap; |
| 4341 | int al; |
| 4342 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4343 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4344 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 | return NULL; |
| 4346 | af = AF_UNSPEC; |
| 4347 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4348 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | af = sa->sa_family; |
| 4350 | ap = NULL; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 4351 | /* al = 0; */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 | switch (af) { |
| 4353 | case AF_INET: |
| 4354 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 4355 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 4356 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4357 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4358 | case AF_INET6: |
| 4359 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 4360 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 4361 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4362 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | default: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4364 | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4365 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | } |
| 4367 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4368 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4369 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Ross Lagerwall | 8c15976 | 2012-03-06 21:36:18 +0200 | [diff] [blame] | 4370 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4371 | &hp_allocated, buf, buf_len, |
| 4372 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4373 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | h = gethostbyaddr_r(ap, al, af, |
| 4375 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 4376 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4377 | memset((void *) &data, '\0', sizeof(data)); |
| 4378 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 4379 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 4380 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4381 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4382 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4383 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4384 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4386 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4387 | Py_END_ALLOW_THREADS |
Charles-François Natali | 8b75965 | 2011-12-23 16:44:51 +0100 | [diff] [blame] | 4388 | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4389 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4390 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 4391 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 4392 | finally: |
| 4393 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4394 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4397 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4398 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 4399 | \n\ |
| 4400 | 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] | 4401 | 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] | 4402 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4403 | |
| 4404 | /* Python interface to getservbyname(name). |
| 4405 | This only returns the port number, since the other info is already |
| 4406 | known or not useful (like the list of aliases). */ |
| 4407 | |
| 4408 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 4409 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4410 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | char *name, *proto=NULL; |
| 4413 | struct servent *sp; |
| 4414 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 4415 | return NULL; |
| 4416 | Py_BEGIN_ALLOW_THREADS |
| 4417 | sp = getservbyname(name, proto); |
| 4418 | Py_END_ALLOW_THREADS |
| 4419 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4420 | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4421 | return NULL; |
| 4422 | } |
| 4423 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4426 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4427 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4428 | \n\ |
| 4429 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4430 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4431 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4432 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4433 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4434 | /* Python interface to getservbyport(port). |
| 4435 | This only returns the service name, since the other info is already |
| 4436 | known or not useful (like the list of aliases). */ |
| 4437 | |
| 4438 | /*ARGSUSED*/ |
| 4439 | static PyObject * |
| 4440 | socket_getservbyport(PyObject *self, PyObject *args) |
| 4441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | int port; |
| 4443 | char *proto=NULL; |
| 4444 | struct servent *sp; |
| 4445 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 4446 | return NULL; |
| 4447 | if (port < 0 || port > 0xffff) { |
| 4448 | PyErr_SetString( |
| 4449 | PyExc_OverflowError, |
| 4450 | "getservbyport: port must be 0-65535."); |
| 4451 | return NULL; |
| 4452 | } |
| 4453 | Py_BEGIN_ALLOW_THREADS |
| 4454 | sp = getservbyport(htons((short)port), proto); |
| 4455 | Py_END_ALLOW_THREADS |
| 4456 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4457 | PyErr_SetString(PyExc_OSError, "port/proto not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4458 | return NULL; |
| 4459 | } |
| 4460 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | PyDoc_STRVAR(getservbyport_doc, |
| 4464 | "getservbyport(port[, protocolname]) -> string\n\ |
| 4465 | \n\ |
| 4466 | Return the service name from a port number and protocol name.\n\ |
| 4467 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 4468 | otherwise any protocol will match."); |
| 4469 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4470 | /* Python interface to getprotobyname(name). |
| 4471 | This only returns the protocol number, since the other info is |
| 4472 | already known or not useful (like the list of aliases). */ |
| 4473 | |
| 4474 | /*ARGSUSED*/ |
| 4475 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4476 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 | char *name; |
| 4479 | struct protoent *sp; |
| 4480 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 4481 | return NULL; |
| 4482 | Py_BEGIN_ALLOW_THREADS |
| 4483 | sp = getprotobyname(name); |
| 4484 | Py_END_ALLOW_THREADS |
| 4485 | if (sp == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4486 | PyErr_SetString(PyExc_OSError, "protocol not found"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | return NULL; |
| 4488 | } |
| 4489 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4492 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4493 | "getprotobyname(name) -> integer\n\ |
| 4494 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4495 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4496 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 4497 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4498 | #ifndef NO_DUP |
| 4499 | /* dup() function for socket fds */ |
| 4500 | |
| 4501 | static PyObject * |
| 4502 | socket_dup(PyObject *self, PyObject *fdobj) |
| 4503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | SOCKET_T fd, newfd; |
| 4505 | PyObject *newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4506 | |
| 4507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | fd = PyLong_AsSocket_t(fdobj); |
| 4509 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 4510 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | newfd = dup_socket(fd); |
| 4513 | if (newfd == INVALID_SOCKET) |
| 4514 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | newfdobj = PyLong_FromSocket_t(newfd); |
| 4517 | if (newfdobj == NULL) |
| 4518 | SOCKETCLOSE(newfd); |
| 4519 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | PyDoc_STRVAR(dup_doc, |
| 4523 | "dup(integer) -> integer\n\ |
| 4524 | \n\ |
| 4525 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 4526 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 4527 | #endif |
| 4528 | |
| 4529 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4530 | #ifdef HAVE_SOCKETPAIR |
| 4531 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4532 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 4533 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4534 | |
| 4535 | /*ARGSUSED*/ |
| 4536 | static PyObject * |
| 4537 | socket_socketpair(PyObject *self, PyObject *args) |
| 4538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4539 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 4540 | SOCKET_T sv[2]; |
| 4541 | int family, type = SOCK_STREAM, proto = 0; |
| 4542 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4543 | |
| 4544 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4546 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4548 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 4550 | &family, &type, &proto)) |
| 4551 | return NULL; |
| 4552 | /* Create a pair of socket fds */ |
| 4553 | if (socketpair(family, type, proto, sv) < 0) |
| 4554 | return set_error(); |
| 4555 | s0 = new_sockobject(sv[0], family, type, proto); |
| 4556 | if (s0 == NULL) |
| 4557 | goto finally; |
| 4558 | s1 = new_sockobject(sv[1], family, type, proto); |
| 4559 | if (s1 == NULL) |
| 4560 | goto finally; |
| 4561 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4562 | |
| 4563 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 | if (res == NULL) { |
| 4565 | if (s0 == NULL) |
| 4566 | SOCKETCLOSE(sv[0]); |
| 4567 | if (s1 == NULL) |
| 4568 | SOCKETCLOSE(sv[1]); |
| 4569 | } |
| 4570 | Py_XDECREF(s0); |
| 4571 | Py_XDECREF(s1); |
| 4572 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | PyDoc_STRVAR(socketpair_doc, |
| 4576 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 4577 | \n\ |
| 4578 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 4579 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 4580 | 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] | 4581 | 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] | 4582 | |
| 4583 | #endif /* HAVE_SOCKETPAIR */ |
| 4584 | |
| 4585 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4586 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4587 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 4592 | return NULL; |
| 4593 | } |
| 4594 | if (x1 < 0) { |
| 4595 | PyErr_SetString(PyExc_OverflowError, |
| 4596 | "can't convert negative number to unsigned long"); |
| 4597 | return NULL; |
| 4598 | } |
| 4599 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 4600 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4603 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4604 | "ntohs(integer) -> integer\n\ |
| 4605 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4606 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4607 | |
| 4608 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4609 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4610 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4612 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | if (PyLong_Check(arg)) { |
| 4615 | x = PyLong_AsUnsignedLong(arg); |
| 4616 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4617 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4618 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | { |
| 4620 | unsigned long y; |
| 4621 | /* only want the trailing 32 bits */ |
| 4622 | y = x & 0xFFFFFFFFUL; |
| 4623 | if (y ^ x) |
| 4624 | return PyErr_Format(PyExc_OverflowError, |
| 4625 | "long int larger than 32 bits"); |
| 4626 | x = y; |
| 4627 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4628 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | } |
| 4630 | else |
| 4631 | return PyErr_Format(PyExc_TypeError, |
| 4632 | "expected int/long, %s found", |
| 4633 | Py_TYPE(arg)->tp_name); |
| 4634 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4635 | return NULL; |
| 4636 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4639 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4640 | "ntohl(integer) -> integer\n\ |
| 4641 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4642 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4643 | |
| 4644 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4645 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4646 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4648 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 4651 | return NULL; |
| 4652 | } |
| 4653 | if (x1 < 0) { |
| 4654 | PyErr_SetString(PyExc_OverflowError, |
| 4655 | "can't convert negative number to unsigned long"); |
| 4656 | return NULL; |
| 4657 | } |
| 4658 | x2 = (unsigned int)htons((unsigned short)x1); |
| 4659 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4662 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4663 | "htons(integer) -> integer\n\ |
| 4664 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4665 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4666 | |
| 4667 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4668 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4669 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4671 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | if (PyLong_Check(arg)) { |
| 4674 | x = PyLong_AsUnsignedLong(arg); |
| 4675 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 4676 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4677 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | { |
| 4679 | unsigned long y; |
| 4680 | /* only want the trailing 32 bits */ |
| 4681 | y = x & 0xFFFFFFFFUL; |
| 4682 | if (y ^ x) |
| 4683 | return PyErr_Format(PyExc_OverflowError, |
| 4684 | "long int larger than 32 bits"); |
| 4685 | x = y; |
| 4686 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4687 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | } |
| 4689 | else |
| 4690 | return PyErr_Format(PyExc_TypeError, |
| 4691 | "expected int/long, %s found", |
| 4692 | Py_TYPE(arg)->tp_name); |
| 4693 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4696 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4697 | "htonl(integer) -> integer\n\ |
| 4698 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4699 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4700 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4701 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4702 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4703 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4704 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4705 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4706 | 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] | 4707 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4708 | |
| 4709 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4710 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4711 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 4712 | #ifndef INADDR_NONE |
| 4713 | #define INADDR_NONE (-1) |
| 4714 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 4715 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4716 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4717 | #endif |
| 4718 | |
| 4719 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 4720 | #if (SIZEOF_INT != 4) |
| 4721 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 4722 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4723 | /* Have to use inet_addr() instead */ |
| 4724 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4725 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4726 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 4729 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4730 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 4731 | |
| 4732 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4733 | |
| 4734 | #ifdef USE_INET_ATON_WEAKLINK |
| 4735 | if (inet_aton != NULL) { |
| 4736 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4737 | if (inet_aton(ip_addr, &buf)) |
| 4738 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 4739 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4740 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4741 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4742 | "illegal IP address string passed to inet_aton"); |
| 4743 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4744 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4745 | #ifdef USE_INET_ATON_WEAKLINK |
| 4746 | } else { |
| 4747 | #endif |
| 4748 | |
| 4749 | #endif |
| 4750 | |
| 4751 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 4752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4753 | /* special-case this address as inet_addr might return INADDR_NONE |
| 4754 | * for this */ |
| 4755 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 4756 | packed_addr = 0xFFFFFFFF; |
| 4757 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4761 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4762 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4763 | "illegal IP address string passed to inet_aton"); |
| 4764 | return NULL; |
| 4765 | } |
| 4766 | } |
| 4767 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 4768 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4769 | |
| 4770 | #ifdef USE_INET_ATON_WEAKLINK |
| 4771 | } |
| 4772 | #endif |
| 4773 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4774 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4777 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 4778 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4779 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4780 | 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] | 4781 | |
| 4782 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4783 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | char *packed_str; |
| 4786 | int addr_len; |
| 4787 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4789 | if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
| 4790 | return NULL; |
| 4791 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | if (addr_len != sizeof(packed_addr)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4794 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4795 | "packed IP wrong length for inet_ntoa"); |
| 4796 | return NULL; |
| 4797 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4799 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4801 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4802 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4803 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4804 | #ifdef HAVE_INET_PTON |
| 4805 | |
| 4806 | PyDoc_STRVAR(inet_pton_doc, |
| 4807 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 4808 | \n\ |
| 4809 | Convert an IP address from string format to a packed string suitable\n\ |
| 4810 | for use with low-level network functions."); |
| 4811 | |
| 4812 | static PyObject * |
| 4813 | socket_inet_pton(PyObject *self, PyObject *args) |
| 4814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | int af; |
| 4816 | char* ip; |
| 4817 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4818 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4819 | char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4820 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4821 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4822 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4823 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 4824 | return NULL; |
| 4825 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4826 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 4827 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | if(af == AF_INET6) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4829 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4830 | "can't use AF_INET6, IPv6 is disabled"); |
| 4831 | return NULL; |
| 4832 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4833 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 4834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4835 | retval = inet_pton(af, ip, packed); |
| 4836 | if (retval < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4837 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | return NULL; |
| 4839 | } else if (retval == 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4840 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4841 | "illegal IP address string passed to inet_pton"); |
| 4842 | return NULL; |
| 4843 | } else if (af == AF_INET) { |
| 4844 | return PyBytes_FromStringAndSize(packed, |
| 4845 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4846 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4847 | } else if (af == AF_INET6) { |
| 4848 | return PyBytes_FromStringAndSize(packed, |
| 4849 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4850 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4852 | PyErr_SetString(PyExc_OSError, "unknown address family"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4853 | return NULL; |
| 4854 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4855 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4856 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4857 | PyDoc_STRVAR(inet_ntop_doc, |
| 4858 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 4859 | \n\ |
| 4860 | Convert a packed IP address of the given family to string format."); |
| 4861 | |
| 4862 | static PyObject * |
| 4863 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 4864 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4865 | int af; |
| 4866 | char* packed; |
| 4867 | int len; |
| 4868 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4869 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4871 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4872 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4873 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4875 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 4876 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 4879 | return NULL; |
| 4880 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4882 | if (af == AF_INET) { |
| 4883 | if (len != sizeof(struct in_addr)) { |
| 4884 | PyErr_SetString(PyExc_ValueError, |
| 4885 | "invalid length of packed IP address string"); |
| 4886 | return NULL; |
| 4887 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4888 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4889 | } else if (af == AF_INET6) { |
| 4890 | if (len != sizeof(struct in6_addr)) { |
| 4891 | PyErr_SetString(PyExc_ValueError, |
| 4892 | "invalid length of packed IP address string"); |
| 4893 | return NULL; |
| 4894 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4895 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4896 | } else { |
| 4897 | PyErr_Format(PyExc_ValueError, |
| 4898 | "unknown address family %d", af); |
| 4899 | return NULL; |
| 4900 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4902 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 4903 | if (!retval) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4904 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4905 | return NULL; |
| 4906 | } else { |
| 4907 | return PyUnicode_FromString(retval); |
| 4908 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | /* NOTREACHED */ |
| 4911 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 4912 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | #endif /* HAVE_INET_PTON */ |
| 4916 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4917 | /* Python interface to getaddrinfo(host, port). */ |
| 4918 | |
| 4919 | /*ARGSUSED*/ |
| 4920 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 4921 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4922 | { |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 4923 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 4924 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4925 | struct addrinfo hints, *res; |
| 4926 | struct addrinfo *res0 = NULL; |
| 4927 | PyObject *hobj = NULL; |
| 4928 | PyObject *pobj = (PyObject *)NULL; |
| 4929 | char pbuf[30]; |
| 4930 | char *hptr, *pptr; |
| 4931 | int family, socktype, protocol, flags; |
| 4932 | int error; |
| 4933 | PyObject *all = (PyObject *)NULL; |
| 4934 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4936 | family = socktype = protocol = flags = 0; |
| 4937 | family = AF_UNSPEC; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 4938 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 4939 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4940 | &protocol, &flags)) { |
| 4941 | return NULL; |
| 4942 | } |
| 4943 | if (hobj == Py_None) { |
| 4944 | hptr = NULL; |
| 4945 | } else if (PyUnicode_Check(hobj)) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4946 | _Py_IDENTIFIER(encode); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4947 | |
| 4948 | idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4949 | if (!idna) |
| 4950 | return NULL; |
| 4951 | assert(PyBytes_Check(idna)); |
| 4952 | hptr = PyBytes_AS_STRING(idna); |
| 4953 | } else if (PyBytes_Check(hobj)) { |
| 4954 | hptr = PyBytes_AsString(hobj); |
| 4955 | } else { |
| 4956 | PyErr_SetString(PyExc_TypeError, |
| 4957 | "getaddrinfo() argument 1 must be string or None"); |
| 4958 | return NULL; |
| 4959 | } |
| 4960 | if (PyLong_CheckExact(pobj)) { |
| 4961 | long value = PyLong_AsLong(pobj); |
| 4962 | if (value == -1 && PyErr_Occurred()) |
| 4963 | goto err; |
| 4964 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 4965 | pptr = pbuf; |
| 4966 | } else if (PyUnicode_Check(pobj)) { |
| 4967 | pptr = _PyUnicode_AsString(pobj); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 4968 | if (pptr == NULL) |
| 4969 | goto err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4970 | } else if (PyBytes_Check(pobj)) { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 4971 | pptr = PyBytes_AS_STRING(pobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4972 | } else if (pobj == Py_None) { |
| 4973 | pptr = (char *)NULL; |
| 4974 | } else { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 4975 | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4976 | goto err; |
| 4977 | } |
| 4978 | memset(&hints, 0, sizeof(hints)); |
| 4979 | hints.ai_family = family; |
| 4980 | hints.ai_socktype = socktype; |
| 4981 | hints.ai_protocol = protocol; |
| 4982 | hints.ai_flags = flags; |
| 4983 | Py_BEGIN_ALLOW_THREADS |
| 4984 | ACQUIRE_GETADDRINFO_LOCK |
| 4985 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 4986 | Py_END_ALLOW_THREADS |
| 4987 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4988 | if (error) { |
| 4989 | set_gaierror(error); |
| 4990 | goto err; |
| 4991 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | if ((all = PyList_New(0)) == NULL) |
| 4994 | goto err; |
| 4995 | for (res = res0; res; res = res->ai_next) { |
| 4996 | PyObject *single; |
| 4997 | PyObject *addr = |
| 4998 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 4999 | if (addr == NULL) |
| 5000 | goto err; |
| 5001 | single = Py_BuildValue("iiisO", res->ai_family, |
| 5002 | res->ai_socktype, res->ai_protocol, |
| 5003 | res->ai_canonname ? res->ai_canonname : "", |
| 5004 | addr); |
| 5005 | Py_DECREF(addr); |
| 5006 | if (single == NULL) |
| 5007 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | if (PyList_Append(all, single)) |
| 5010 | goto err; |
| 5011 | Py_XDECREF(single); |
| 5012 | } |
| 5013 | Py_XDECREF(idna); |
| 5014 | if (res0) |
| 5015 | freeaddrinfo(res0); |
| 5016 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5017 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5018 | Py_XDECREF(all); |
| 5019 | Py_XDECREF(idna); |
| 5020 | if (res0) |
| 5021 | freeaddrinfo(res0); |
| 5022 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5025 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5026 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 5027 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5028 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5029 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5030 | |
| 5031 | /* Python interface to getnameinfo(sa, flags). */ |
| 5032 | |
| 5033 | /*ARGSUSED*/ |
| 5034 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5035 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5037 | PyObject *sa = (PyObject *)NULL; |
| 5038 | int flags; |
| 5039 | char *hostp; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5040 | int port; |
| 5041 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5042 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 5043 | struct addrinfo hints, *res = NULL; |
| 5044 | int error; |
| 5045 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | flags = flowinfo = scope_id = 0; |
| 5048 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 5049 | return NULL; |
| 5050 | if (!PyTuple_Check(sa)) { |
| 5051 | PyErr_SetString(PyExc_TypeError, |
| 5052 | "getnameinfo() argument 1 must be a tuple"); |
| 5053 | return NULL; |
| 5054 | } |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5055 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5056 | &hostp, &port, &flowinfo, &scope_id)) |
| 5057 | return NULL; |
Charles-François Natali | 9b0c006 | 2012-06-23 10:17:05 +0200 | [diff] [blame] | 5058 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5059 | PyErr_SetString(PyExc_OverflowError, |
| 5060 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 5061 | return NULL; |
| 5062 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 5064 | memset(&hints, 0, sizeof(hints)); |
| 5065 | hints.ai_family = AF_UNSPEC; |
| 5066 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 5067 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | Py_BEGIN_ALLOW_THREADS |
| 5069 | ACQUIRE_GETADDRINFO_LOCK |
| 5070 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 5071 | Py_END_ALLOW_THREADS |
| 5072 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 5073 | if (error) { |
| 5074 | set_gaierror(error); |
| 5075 | goto fail; |
| 5076 | } |
| 5077 | if (res->ai_next) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5078 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5079 | "sockaddr resolved to multiple addresses"); |
| 5080 | goto fail; |
| 5081 | } |
| 5082 | switch (res->ai_family) { |
| 5083 | case AF_INET: |
| 5084 | { |
| 5085 | if (PyTuple_GET_SIZE(sa) != 2) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5086 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | "IPv4 sockaddr must be 2 tuple"); |
| 5088 | goto fail; |
| 5089 | } |
| 5090 | break; |
| 5091 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 5092 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | case AF_INET6: |
| 5094 | { |
| 5095 | struct sockaddr_in6 *sin6; |
| 5096 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 5097 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | sin6->sin6_scope_id = scope_id; |
| 5099 | break; |
| 5100 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5101 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5102 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 5103 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5104 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 5105 | if (error) { |
| 5106 | set_gaierror(error); |
| 5107 | goto fail; |
| 5108 | } |
| 5109 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5110 | |
| 5111 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5112 | if (res) |
| 5113 | freeaddrinfo(res); |
| 5114 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5117 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5118 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5119 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5120 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5121 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5122 | |
| 5123 | /* Python API to getting and setting the default timeout value. */ |
| 5124 | |
| 5125 | static PyObject * |
| 5126 | socket_getdefaulttimeout(PyObject *self) |
| 5127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5128 | if (defaulttimeout < 0.0) { |
| 5129 | Py_INCREF(Py_None); |
| 5130 | return Py_None; |
| 5131 | } |
| 5132 | else |
| 5133 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5137 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5138 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5139 | 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] | 5140 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5141 | When the socket module is first imported, the default is None."); |
| 5142 | |
| 5143 | static PyObject * |
| 5144 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 5145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5146 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5148 | if (arg == Py_None) |
| 5149 | timeout = -1.0; |
| 5150 | else { |
| 5151 | timeout = PyFloat_AsDouble(arg); |
| 5152 | if (timeout < 0.0) { |
| 5153 | if (!PyErr_Occurred()) |
| 5154 | PyErr_SetString(PyExc_ValueError, |
| 5155 | "Timeout value out of range"); |
| 5156 | return NULL; |
| 5157 | } |
| 5158 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5160 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5162 | Py_INCREF(Py_None); |
| 5163 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5167 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 5168 | \n\ |
Ezio Melotti | 388c945 | 2011-08-14 08:28:57 +0300 | [diff] [blame] | 5169 | 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] | 5170 | A value of None indicates that new socket objects have no timeout.\n\ |
| 5171 | When the socket module is first imported, the default is None."); |
| 5172 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5173 | #ifdef HAVE_IF_NAMEINDEX |
| 5174 | /* Python API for getting interface indices and names */ |
| 5175 | |
| 5176 | static PyObject * |
| 5177 | socket_if_nameindex(PyObject *self, PyObject *arg) |
| 5178 | { |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5179 | PyObject *list; |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5180 | int i; |
| 5181 | struct if_nameindex *ni; |
Victor Stinner | 77af172 | 2011-05-26 14:05:59 +0200 | [diff] [blame] | 5182 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5183 | ni = if_nameindex(); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5184 | if (ni == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5185 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5186 | return NULL; |
| 5187 | } |
| 5188 | |
| 5189 | list = PyList_New(0); |
| 5190 | if (list == NULL) { |
| 5191 | if_freenameindex(ni); |
| 5192 | return NULL; |
| 5193 | } |
| 5194 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5195 | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
| 5196 | PyObject *ni_tuple = Py_BuildValue("IO&", |
| 5197 | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5198 | |
| 5199 | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
| 5200 | Py_XDECREF(ni_tuple); |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5201 | Py_DECREF(list); |
| 5202 | if_freenameindex(ni); |
| 5203 | return NULL; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5204 | } |
| 5205 | Py_DECREF(ni_tuple); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | if_freenameindex(ni); |
| 5209 | return list; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | PyDoc_STRVAR(if_nameindex_doc, |
| 5213 | "if_nameindex()\n\ |
| 5214 | \n\ |
| 5215 | Returns a list of network interface information (index, name) tuples."); |
| 5216 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5217 | static PyObject * |
| 5218 | socket_if_nametoindex(PyObject *self, PyObject *args) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5219 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5220 | PyObject *oname; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5221 | unsigned long index; |
| 5222 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5223 | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
| 5224 | PyUnicode_FSConverter, &oname)) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5225 | return NULL; |
| 5226 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5227 | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
| 5228 | Py_DECREF(oname); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5229 | if (index == 0) { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5230 | /* if_nametoindex() doesn't set errno */ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5231 | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5232 | return NULL; |
| 5233 | } |
| 5234 | |
| 5235 | return PyLong_FromUnsignedLong(index); |
| 5236 | } |
| 5237 | |
| 5238 | PyDoc_STRVAR(if_nametoindex_doc, |
| 5239 | "if_nametoindex(if_name)\n\ |
| 5240 | \n\ |
| 5241 | Returns the interface index corresponding to the interface name if_name."); |
| 5242 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5243 | static PyObject * |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5244 | socket_if_indextoname(PyObject *self, PyObject *arg) |
| 5245 | { |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5246 | unsigned long index; |
| 5247 | char name[IF_NAMESIZE + 1]; |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5248 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5249 | index = PyLong_AsUnsignedLong(arg); |
| 5250 | if (index == (unsigned long) -1) |
| 5251 | return NULL; |
| 5252 | |
| 5253 | if (if_indextoname(index, name) == NULL) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5254 | PyErr_SetFromErrno(PyExc_OSError); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5255 | return NULL; |
| 5256 | } |
| 5257 | |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5258 | return PyUnicode_DecodeFSDefault(name); |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | PyDoc_STRVAR(if_indextoname_doc, |
| 5262 | "if_indextoname(if_index)\n\ |
| 5263 | \n\ |
| 5264 | Returns the interface name corresponding to the interface index if_index."); |
| 5265 | |
| 5266 | #endif /* HAVE_IF_NAMEINDEX */ |
| 5267 | |
| 5268 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5269 | #ifdef CMSG_LEN |
| 5270 | /* Python interface to CMSG_LEN(length). */ |
| 5271 | |
| 5272 | static PyObject * |
| 5273 | socket_CMSG_LEN(PyObject *self, PyObject *args) |
| 5274 | { |
| 5275 | Py_ssize_t length; |
| 5276 | size_t result; |
| 5277 | |
| 5278 | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
| 5279 | return NULL; |
| 5280 | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
| 5281 | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
| 5282 | return NULL; |
| 5283 | } |
| 5284 | return PyLong_FromSize_t(result); |
| 5285 | } |
| 5286 | |
| 5287 | PyDoc_STRVAR(CMSG_LEN_doc, |
| 5288 | "CMSG_LEN(length) -> control message length\n\ |
| 5289 | \n\ |
| 5290 | Return the total length, without trailing padding, of an ancillary\n\ |
| 5291 | data item with associated data of the given length. This value can\n\ |
| 5292 | often be used as the buffer size for recvmsg() to receive a single\n\ |
| 5293 | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
| 5294 | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
| 5295 | item will be the last in the buffer. Raises OverflowError if length\n\ |
| 5296 | is outside the permissible range of values."); |
| 5297 | |
| 5298 | |
| 5299 | #ifdef CMSG_SPACE |
| 5300 | /* Python interface to CMSG_SPACE(length). */ |
| 5301 | |
| 5302 | static PyObject * |
| 5303 | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
| 5304 | { |
| 5305 | Py_ssize_t length; |
| 5306 | size_t result; |
| 5307 | |
| 5308 | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
| 5309 | return NULL; |
| 5310 | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
| 5311 | PyErr_SetString(PyExc_OverflowError, |
| 5312 | "CMSG_SPACE() argument out of range"); |
| 5313 | return NULL; |
| 5314 | } |
| 5315 | return PyLong_FromSize_t(result); |
| 5316 | } |
| 5317 | |
| 5318 | PyDoc_STRVAR(CMSG_SPACE_doc, |
| 5319 | "CMSG_SPACE(length) -> buffer size\n\ |
| 5320 | \n\ |
| 5321 | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
| 5322 | data item with associated data of the given length, along with any\n\ |
| 5323 | trailing padding. The buffer space needed to receive multiple items\n\ |
| 5324 | is the sum of the CMSG_SPACE() values for their associated data\n\ |
| 5325 | lengths. Raises OverflowError if length is outside the permissible\n\ |
| 5326 | range of values."); |
| 5327 | #endif /* CMSG_SPACE */ |
| 5328 | #endif /* CMSG_LEN */ |
| 5329 | |
| 5330 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5331 | /* List of functions exported by this module. */ |
| 5332 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5333 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5334 | {"gethostbyname", socket_gethostbyname, |
| 5335 | METH_VARARGS, gethostbyname_doc}, |
| 5336 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 5337 | METH_VARARGS, ghbn_ex_doc}, |
| 5338 | {"gethostbyaddr", socket_gethostbyaddr, |
| 5339 | METH_VARARGS, gethostbyaddr_doc}, |
| 5340 | {"gethostname", socket_gethostname, |
| 5341 | METH_NOARGS, gethostname_doc}, |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 5342 | #ifdef HAVE_SETHOSTNAME |
| 5343 | {"sethostname", socket_sethostname, |
| 5344 | METH_VARARGS, sethostname_doc}, |
| 5345 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5346 | {"getservbyname", socket_getservbyname, |
| 5347 | METH_VARARGS, getservbyname_doc}, |
| 5348 | {"getservbyport", socket_getservbyport, |
| 5349 | METH_VARARGS, getservbyport_doc}, |
| 5350 | {"getprotobyname", socket_getprotobyname, |
| 5351 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5352 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5353 | {"dup", socket_dup, |
| 5354 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5355 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5356 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5357 | {"socketpair", socket_socketpair, |
| 5358 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 5359 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5360 | {"ntohs", socket_ntohs, |
| 5361 | METH_VARARGS, ntohs_doc}, |
| 5362 | {"ntohl", socket_ntohl, |
| 5363 | METH_O, ntohl_doc}, |
| 5364 | {"htons", socket_htons, |
| 5365 | METH_VARARGS, htons_doc}, |
| 5366 | {"htonl", socket_htonl, |
| 5367 | METH_O, htonl_doc}, |
| 5368 | {"inet_aton", socket_inet_aton, |
| 5369 | METH_VARARGS, inet_aton_doc}, |
| 5370 | {"inet_ntoa", socket_inet_ntoa, |
| 5371 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5372 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5373 | {"inet_pton", socket_inet_pton, |
| 5374 | METH_VARARGS, inet_pton_doc}, |
| 5375 | {"inet_ntop", socket_inet_ntop, |
| 5376 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5377 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 5378 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 5379 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | {"getnameinfo", socket_getnameinfo, |
| 5381 | METH_VARARGS, getnameinfo_doc}, |
| 5382 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 5383 | METH_NOARGS, getdefaulttimeout_doc}, |
| 5384 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 5385 | METH_O, setdefaulttimeout_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5386 | #ifdef HAVE_IF_NAMEINDEX |
| 5387 | {"if_nameindex", socket_if_nameindex, |
| 5388 | METH_NOARGS, if_nameindex_doc}, |
| 5389 | {"if_nametoindex", socket_if_nametoindex, |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 5390 | METH_VARARGS, if_nametoindex_doc}, |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 5391 | {"if_indextoname", socket_if_indextoname, |
| 5392 | METH_O, if_indextoname_doc}, |
| 5393 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5394 | #ifdef CMSG_LEN |
| 5395 | {"CMSG_LEN", socket_CMSG_LEN, |
| 5396 | METH_VARARGS, CMSG_LEN_doc}, |
| 5397 | #ifdef CMSG_SPACE |
| 5398 | {"CMSG_SPACE", socket_CMSG_SPACE, |
| 5399 | METH_VARARGS, CMSG_SPACE_doc}, |
| 5400 | #endif |
| 5401 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5402 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5403 | }; |
| 5404 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 5405 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5406 | #ifdef MS_WINDOWS |
| 5407 | #define OS_INIT_DEFINED |
| 5408 | |
| 5409 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5410 | |
| 5411 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5412 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5418 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | WSADATA WSAData; |
| 5421 | int ret; |
| 5422 | ret = WSAStartup(0x0101, &WSAData); |
| 5423 | switch (ret) { |
| 5424 | case 0: /* No error */ |
| 5425 | Py_AtExit(os_cleanup); |
| 5426 | return 1; /* Success */ |
| 5427 | case WSASYSNOTREADY: |
| 5428 | PyErr_SetString(PyExc_ImportError, |
| 5429 | "WSAStartup failed: network not ready"); |
| 5430 | break; |
| 5431 | case WSAVERNOTSUPPORTED: |
| 5432 | case WSAEINVAL: |
| 5433 | PyErr_SetString( |
| 5434 | PyExc_ImportError, |
| 5435 | "WSAStartup failed: requested version not supported"); |
| 5436 | break; |
| 5437 | default: |
| 5438 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 5439 | break; |
| 5440 | } |
| 5441 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 5444 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 5445 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 5446 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5447 | |
| 5448 | #ifndef OS_INIT_DEFINED |
| 5449 | static int |
| 5450 | os_init(void) |
| 5451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5452 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5453 | } |
| 5454 | #endif |
| 5455 | |
| 5456 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5457 | /* C API table - always add new things to the end for binary |
| 5458 | compatibility. */ |
| 5459 | static |
| 5460 | PySocketModule_APIObject PySocketModuleAPI = |
| 5461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | &sock_type, |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5463 | NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5464 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5465 | }; |
| 5466 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5467 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5468 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5469 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5470 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5471 | "socket.py" which implements some additional functionality. |
| 5472 | The import of "_socket" may fail with an ImportError exception if |
| 5473 | os-specific initialization fails. On Windows, this does WINSOCK |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 5474 | initialization. When WINSOCK is initialized successfully, a call to |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 5475 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5476 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5478 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 5479 | "Implementation module for socket operations.\n\ |
| 5480 | \n\ |
| 5481 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 5482 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5483 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5484 | PyModuleDef_HEAD_INIT, |
| 5485 | PySocket_MODULE_NAME, |
| 5486 | socket_doc, |
| 5487 | -1, |
| 5488 | socket_methods, |
| 5489 | NULL, |
| 5490 | NULL, |
| 5491 | NULL, |
| 5492 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5493 | }; |
| 5494 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 5495 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5496 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5498 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5500 | if (!os_init()) |
| 5501 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | Py_TYPE(&sock_type) = &PyType_Type; |
| 5504 | m = PyModule_Create(&socketmodule); |
| 5505 | if (m == NULL) |
| 5506 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5507 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5508 | Py_INCREF(PyExc_OSError); |
| 5509 | PySocketModuleAPI.error = PyExc_OSError; |
| 5510 | Py_INCREF(PyExc_OSError); |
| 5511 | PyModule_AddObject(m, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | socket_herror = PyErr_NewException("socket.herror", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5513 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5514 | if (socket_herror == NULL) |
| 5515 | return NULL; |
| 5516 | Py_INCREF(socket_herror); |
| 5517 | PyModule_AddObject(m, "herror", socket_herror); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5518 | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5519 | NULL); |
| 5520 | if (socket_gaierror == NULL) |
| 5521 | return NULL; |
| 5522 | Py_INCREF(socket_gaierror); |
| 5523 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 5524 | socket_timeout = PyErr_NewException("socket.timeout", |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 5525 | PyExc_OSError, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5526 | if (socket_timeout == NULL) |
| 5527 | return NULL; |
Antoine Pitrou | c4df784 | 2010-12-03 19:59:41 +0000 | [diff] [blame] | 5528 | PySocketModuleAPI.timeout_error = socket_timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5529 | Py_INCREF(socket_timeout); |
| 5530 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 5531 | Py_INCREF((PyObject *)&sock_type); |
| 5532 | if (PyModule_AddObject(m, "SocketType", |
| 5533 | (PyObject *)&sock_type) != 0) |
| 5534 | return NULL; |
| 5535 | Py_INCREF((PyObject *)&sock_type); |
| 5536 | if (PyModule_AddObject(m, "socket", |
| 5537 | (PyObject *)&sock_type) != 0) |
| 5538 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5539 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5540 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5542 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5543 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5544 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | Py_INCREF(has_ipv6); |
| 5546 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | /* Export C API */ |
| 5549 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 5550 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 5551 | ) != 0) |
| 5552 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 5553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5554 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5555 | #ifdef AF_UNSPEC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5557 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5558 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5559 | #ifdef AF_INET6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5560 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5561 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 5562 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5563 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5564 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5565 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | /* Amateur Radio AX.25 */ |
| 5567 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5568 | #endif |
| 5569 | #ifdef AF_IPX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5570 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5571 | #endif |
| 5572 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5573 | /* Appletalk DDP */ |
| 5574 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5575 | #endif |
| 5576 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5577 | /* Amateur radio NetROM */ |
| 5578 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5579 | #endif |
| 5580 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5581 | /* Multiprotocol bridge */ |
| 5582 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5583 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5584 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | /* ATM PVCs */ |
| 5586 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5587 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5588 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5589 | /* Reserved for Werner's ATM */ |
| 5590 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5591 | #endif |
| 5592 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5593 | /* Reserved for X.25 project */ |
| 5594 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5595 | #endif |
| 5596 | #ifdef AF_INET6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5597 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5598 | #endif |
| 5599 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5600 | /* Amateur Radio X.25 PLP */ |
| 5601 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5602 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5603 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5604 | /* Reserved for DECnet project */ |
| 5605 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5606 | #endif |
| 5607 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5608 | /* Reserved for 802.2LLC project */ |
| 5609 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5610 | #endif |
| 5611 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | /* Security callback pseudo AF */ |
| 5613 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5614 | #endif |
| 5615 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5616 | /* PF_KEY key management API */ |
| 5617 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5618 | #endif |
| 5619 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5620 | /* */ |
| 5621 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 5622 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5623 | #ifdef NETLINK_SKIP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5624 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5625 | #endif |
| 5626 | #ifdef NETLINK_W1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5627 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5628 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5629 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 5630 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5631 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5632 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5633 | #endif |
| 5634 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5635 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 5636 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 5637 | #ifdef NETLINK_XFRM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5638 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 5639 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5640 | #ifdef NETLINK_ARPD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5641 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5642 | #endif |
| 5643 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5644 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5645 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5646 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 5647 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5648 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 5649 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5650 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5652 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5653 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5654 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5655 | /* Alias to emulate 4.4BSD */ |
| 5656 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5657 | #endif |
| 5658 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5659 | /* Ash */ |
| 5660 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5661 | #endif |
| 5662 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | /* Acorn Econet */ |
| 5664 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5665 | #endif |
| 5666 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5667 | /* ATM SVCs */ |
| 5668 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5669 | #endif |
| 5670 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5671 | /* Linux SNA Project (nutters!) */ |
| 5672 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5673 | #endif |
| 5674 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5675 | /* IRDA sockets */ |
| 5676 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5677 | #endif |
| 5678 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5679 | /* PPPoX sockets */ |
| 5680 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5681 | #endif |
| 5682 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | /* Wanpipe API Sockets */ |
| 5684 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5685 | #endif |
| 5686 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5687 | /* Linux LLC */ |
| 5688 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 5689 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 5690 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 5691 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 5693 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 5694 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 5695 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5696 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5697 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5698 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 5699 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5700 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5701 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
Gregory P. Smith | 3e85dfd | 2010-10-17 00:43:10 +0000 | [diff] [blame] | 5702 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5703 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 5704 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 5705 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 5707 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 5708 | 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] | 5709 | #endif |
| 5710 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 5711 | #ifdef AF_CAN |
| 5712 | /* Controller Area Network */ |
| 5713 | PyModule_AddIntConstant(m, "AF_CAN", AF_CAN); |
| 5714 | #endif |
| 5715 | #ifdef PF_CAN |
| 5716 | /* Controller Area Network */ |
| 5717 | PyModule_AddIntConstant(m, "PF_CAN", PF_CAN); |
| 5718 | #endif |
| 5719 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 5720 | /* Reliable Datagram Sockets */ |
| 5721 | #ifdef AF_RDS |
| 5722 | PyModule_AddIntConstant(m, "AF_RDS", AF_RDS); |
| 5723 | #endif |
| 5724 | #ifdef PF_RDS |
| 5725 | PyModule_AddIntConstant(m, "PF_RDS", PF_RDS); |
| 5726 | #endif |
| 5727 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 5728 | /* Kernel event messages */ |
| 5729 | #ifdef PF_SYSTEM |
| 5730 | PyModule_AddIntConstant(m, "PF_SYSTEM", PF_SYSTEM); |
| 5731 | #endif |
| 5732 | #ifdef AF_SYSTEM |
| 5733 | PyModule_AddIntConstant(m, "AF_SYSTEM", AF_SYSTEM); |
| 5734 | #endif |
| 5735 | |
Antoine Pitrou | b156a46 | 2010-10-27 20:13:57 +0000 | [diff] [blame] | 5736 | #ifdef AF_PACKET |
| 5737 | PyModule_AddIntMacro(m, AF_PACKET); |
| 5738 | #endif |
| 5739 | #ifdef PF_PACKET |
| 5740 | PyModule_AddIntMacro(m, PF_PACKET); |
| 5741 | #endif |
| 5742 | #ifdef PACKET_HOST |
| 5743 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 5744 | #endif |
| 5745 | #ifdef PACKET_BROADCAST |
| 5746 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 5747 | #endif |
| 5748 | #ifdef PACKET_MULTICAST |
| 5749 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 5750 | #endif |
| 5751 | #ifdef PACKET_OTHERHOST |
| 5752 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 5753 | #endif |
| 5754 | #ifdef PACKET_OUTGOING |
| 5755 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 5756 | #endif |
| 5757 | #ifdef PACKET_LOOPBACK |
| 5758 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 5759 | #endif |
| 5760 | #ifdef PACKET_FASTROUTE |
| 5761 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 5762 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5763 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 5764 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5765 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 5766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5767 | /* for addresses */ |
| 5768 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 5769 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 5770 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 5771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5772 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 5773 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 5774 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 5775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5776 | /* for setsockopt() */ |
| 5777 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 5778 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 5779 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 5780 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 5781 | TIPC_DEST_DROPPABLE); |
| 5782 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 5783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5784 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 5785 | TIPC_LOW_IMPORTANCE); |
| 5786 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 5787 | TIPC_MEDIUM_IMPORTANCE); |
| 5788 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 5789 | TIPC_HIGH_IMPORTANCE); |
| 5790 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 5791 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 5792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5793 | /* for subscriptions */ |
| 5794 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 5795 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 5796 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 | /* doesn't seem to be available everywhere */ |
| 5798 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 5799 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5800 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 5801 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 5802 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 5803 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 5804 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 5805 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 5806 | #endif |
| 5807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | /* Socket types */ |
| 5809 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 5810 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 5811 | /* We have incomplete socket support. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 5813 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 5814 | #if defined(SOCK_RDM) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5815 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 5816 | #endif |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 5817 | #ifdef SOCK_CLOEXEC |
| 5818 | PyModule_AddIntConstant(m, "SOCK_CLOEXEC", SOCK_CLOEXEC); |
| 5819 | #endif |
| 5820 | #ifdef SOCK_NONBLOCK |
| 5821 | PyModule_AddIntConstant(m, "SOCK_NONBLOCK", SOCK_NONBLOCK); |
| 5822 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | #ifdef SO_DEBUG |
| 5825 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5826 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5827 | #ifdef SO_ACCEPTCONN |
| 5828 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5829 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5830 | #ifdef SO_REUSEADDR |
| 5831 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5832 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 5833 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5834 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 5835 | #endif |
| 5836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5837 | #ifdef SO_KEEPALIVE |
| 5838 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5839 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5840 | #ifdef SO_DONTROUTE |
| 5841 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5842 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5843 | #ifdef SO_BROADCAST |
| 5844 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5845 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5846 | #ifdef SO_USELOOPBACK |
| 5847 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5848 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5849 | #ifdef SO_LINGER |
| 5850 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5851 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5852 | #ifdef SO_OOBINLINE |
| 5853 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5854 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5855 | #ifdef SO_REUSEPORT |
| 5856 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5857 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5858 | #ifdef SO_SNDBUF |
| 5859 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5860 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5861 | #ifdef SO_RCVBUF |
| 5862 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5863 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5864 | #ifdef SO_SNDLOWAT |
| 5865 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5866 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5867 | #ifdef SO_RCVLOWAT |
| 5868 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5869 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5870 | #ifdef SO_SNDTIMEO |
| 5871 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5872 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5873 | #ifdef SO_RCVTIMEO |
| 5874 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5875 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5876 | #ifdef SO_ERROR |
| 5877 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5878 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5879 | #ifdef SO_TYPE |
| 5880 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5881 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | #ifdef SO_SETFIB |
| 5883 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 5884 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5885 | #ifdef SO_PASSCRED |
| 5886 | PyModule_AddIntConstant(m, "SO_PASSCRED", SO_PASSCRED); |
| 5887 | #endif |
| 5888 | #ifdef SO_PEERCRED |
| 5889 | PyModule_AddIntConstant(m, "SO_PEERCRED", SO_PEERCRED); |
| 5890 | #endif |
| 5891 | #ifdef LOCAL_PEERCRED |
| 5892 | PyModule_AddIntConstant(m, "LOCAL_PEERCRED", LOCAL_PEERCRED); |
| 5893 | #endif |
Victor Stinner | cb98bed | 2011-10-25 13:45:48 +0200 | [diff] [blame] | 5894 | #ifdef SO_BINDTODEVICE |
| 5895 | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
| 5896 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5898 | /* Maximum number of connections for "listen" */ |
| 5899 | #ifdef SOMAXCONN |
| 5900 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5901 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5902 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5903 | #endif |
| 5904 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5905 | /* Ancilliary message types */ |
| 5906 | #ifdef SCM_RIGHTS |
| 5907 | PyModule_AddIntConstant(m, "SCM_RIGHTS", SCM_RIGHTS); |
| 5908 | #endif |
| 5909 | #ifdef SCM_CREDENTIALS |
| 5910 | PyModule_AddIntConstant(m, "SCM_CREDENTIALS", SCM_CREDENTIALS); |
| 5911 | #endif |
| 5912 | #ifdef SCM_CREDS |
| 5913 | PyModule_AddIntConstant(m, "SCM_CREDS", SCM_CREDS); |
| 5914 | #endif |
| 5915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5916 | /* Flags for send, recv */ |
| 5917 | #ifdef MSG_OOB |
| 5918 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5919 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5920 | #ifdef MSG_PEEK |
| 5921 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5922 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5923 | #ifdef MSG_DONTROUTE |
| 5924 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5925 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5926 | #ifdef MSG_DONTWAIT |
| 5927 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 5928 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5929 | #ifdef MSG_EOR |
| 5930 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5931 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5932 | #ifdef MSG_TRUNC |
| 5933 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5934 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5935 | #ifdef MSG_CTRUNC |
| 5936 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5937 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5938 | #ifdef MSG_WAITALL |
| 5939 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5940 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5941 | #ifdef MSG_BTAG |
| 5942 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5943 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5944 | #ifdef MSG_ETAG |
| 5945 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5946 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 5947 | #ifdef MSG_NOSIGNAL |
| 5948 | PyModule_AddIntConstant(m, "MSG_NOSIGNAL", MSG_NOSIGNAL); |
| 5949 | #endif |
| 5950 | #ifdef MSG_NOTIFICATION |
| 5951 | PyModule_AddIntConstant(m, "MSG_NOTIFICATION", MSG_NOTIFICATION); |
| 5952 | #endif |
| 5953 | #ifdef MSG_CMSG_CLOEXEC |
| 5954 | PyModule_AddIntConstant(m, "MSG_CMSG_CLOEXEC", MSG_CMSG_CLOEXEC); |
| 5955 | #endif |
| 5956 | #ifdef MSG_ERRQUEUE |
| 5957 | PyModule_AddIntConstant(m, "MSG_ERRQUEUE", MSG_ERRQUEUE); |
| 5958 | #endif |
| 5959 | #ifdef MSG_CONFIRM |
| 5960 | PyModule_AddIntConstant(m, "MSG_CONFIRM", MSG_CONFIRM); |
| 5961 | #endif |
| 5962 | #ifdef MSG_MORE |
| 5963 | PyModule_AddIntConstant(m, "MSG_MORE", MSG_MORE); |
| 5964 | #endif |
| 5965 | #ifdef MSG_EOF |
| 5966 | PyModule_AddIntConstant(m, "MSG_EOF", MSG_EOF); |
| 5967 | #endif |
| 5968 | #ifdef MSG_BCAST |
| 5969 | PyModule_AddIntConstant(m, "MSG_BCAST", MSG_BCAST); |
| 5970 | #endif |
| 5971 | #ifdef MSG_MCAST |
| 5972 | PyModule_AddIntConstant(m, "MSG_MCAST", MSG_MCAST); |
| 5973 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5975 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 5976 | #ifdef SOL_SOCKET |
| 5977 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5978 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5979 | #ifdef SOL_IP |
| 5980 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5981 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5982 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5983 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5984 | #ifdef SOL_IPX |
| 5985 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5986 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5987 | #ifdef SOL_AX25 |
| 5988 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5989 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5990 | #ifdef SOL_ATALK |
| 5991 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5992 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5993 | #ifdef SOL_NETROM |
| 5994 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5995 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5996 | #ifdef SOL_ROSE |
| 5997 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5998 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5999 | #ifdef SOL_TCP |
| 6000 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6001 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6002 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6003 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6004 | #ifdef SOL_UDP |
| 6005 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6006 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6007 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6008 | #endif |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 6009 | #ifdef SOL_CAN_BASE |
| 6010 | PyModule_AddIntConstant(m, "SOL_CAN_BASE", SOL_CAN_BASE); |
| 6011 | #endif |
| 6012 | #ifdef SOL_CAN_RAW |
| 6013 | PyModule_AddIntConstant(m, "SOL_CAN_RAW", SOL_CAN_RAW); |
| 6014 | PyModule_AddIntConstant(m, "CAN_RAW", CAN_RAW); |
| 6015 | #endif |
| 6016 | #ifdef HAVE_LINUX_CAN_H |
| 6017 | PyModule_AddIntConstant(m, "CAN_EFF_FLAG", CAN_EFF_FLAG); |
| 6018 | PyModule_AddIntConstant(m, "CAN_RTR_FLAG", CAN_RTR_FLAG); |
| 6019 | PyModule_AddIntConstant(m, "CAN_ERR_FLAG", CAN_ERR_FLAG); |
| 6020 | |
| 6021 | PyModule_AddIntConstant(m, "CAN_SFF_MASK", CAN_SFF_MASK); |
| 6022 | PyModule_AddIntConstant(m, "CAN_EFF_MASK", CAN_EFF_MASK); |
| 6023 | PyModule_AddIntConstant(m, "CAN_ERR_MASK", CAN_ERR_MASK); |
| 6024 | #endif |
| 6025 | #ifdef HAVE_LINUX_CAN_RAW_H |
| 6026 | PyModule_AddIntConstant(m, "CAN_RAW_FILTER", CAN_RAW_FILTER); |
| 6027 | PyModule_AddIntConstant(m, "CAN_RAW_ERR_FILTER", CAN_RAW_ERR_FILTER); |
| 6028 | PyModule_AddIntConstant(m, "CAN_RAW_LOOPBACK", CAN_RAW_LOOPBACK); |
| 6029 | PyModule_AddIntConstant(m, "CAN_RAW_RECV_OWN_MSGS", CAN_RAW_RECV_OWN_MSGS); |
| 6030 | #endif |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6031 | #ifdef SOL_RDS |
| 6032 | PyModule_AddIntConstant(m, "SOL_RDS", SOL_RDS); |
| 6033 | #endif |
| 6034 | #ifdef RDS_CANCEL_SENT_TO |
| 6035 | PyModule_AddIntConstant(m, "RDS_CANCEL_SENT_TO", RDS_CANCEL_SENT_TO); |
| 6036 | #endif |
| 6037 | #ifdef RDS_GET_MR |
| 6038 | PyModule_AddIntConstant(m, "RDS_GET_MR", RDS_GET_MR); |
| 6039 | #endif |
| 6040 | #ifdef RDS_FREE_MR |
| 6041 | PyModule_AddIntConstant(m, "RDS_FREE_MR", RDS_FREE_MR); |
| 6042 | #endif |
| 6043 | #ifdef RDS_RECVERR |
| 6044 | PyModule_AddIntConstant(m, "RDS_RECVERR", RDS_RECVERR); |
| 6045 | #endif |
| 6046 | #ifdef RDS_CONG_MONITOR |
| 6047 | PyModule_AddIntConstant(m, "RDS_CONG_MONITOR", RDS_CONG_MONITOR); |
| 6048 | #endif |
| 6049 | #ifdef RDS_GET_MR_FOR_DEST |
| 6050 | PyModule_AddIntConstant(m, "RDS_GET_MR_FOR_DEST", RDS_GET_MR_FOR_DEST); |
| 6051 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6052 | #ifdef IPPROTO_IP |
| 6053 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6054 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6055 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6056 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6057 | #ifdef IPPROTO_HOPOPTS |
| 6058 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6059 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6060 | #ifdef IPPROTO_ICMP |
| 6061 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6062 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6063 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6064 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6065 | #ifdef IPPROTO_IGMP |
| 6066 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6067 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6068 | #ifdef IPPROTO_GGP |
| 6069 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6070 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6071 | #ifdef IPPROTO_IPV4 |
| 6072 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6073 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6074 | #ifdef IPPROTO_IPV6 |
| 6075 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 6076 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6077 | #ifdef IPPROTO_IPIP |
| 6078 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6079 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6080 | #ifdef IPPROTO_TCP |
| 6081 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6082 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6083 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6084 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6085 | #ifdef IPPROTO_EGP |
| 6086 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6087 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6088 | #ifdef IPPROTO_PUP |
| 6089 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6090 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6091 | #ifdef IPPROTO_UDP |
| 6092 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6093 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6094 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6095 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6096 | #ifdef IPPROTO_IDP |
| 6097 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6098 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6099 | #ifdef IPPROTO_HELLO |
| 6100 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6101 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6102 | #ifdef IPPROTO_ND |
| 6103 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6104 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6105 | #ifdef IPPROTO_TP |
| 6106 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6107 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6108 | #ifdef IPPROTO_IPV6 |
| 6109 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6110 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6111 | #ifdef IPPROTO_ROUTING |
| 6112 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6113 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6114 | #ifdef IPPROTO_FRAGMENT |
| 6115 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6116 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6117 | #ifdef IPPROTO_RSVP |
| 6118 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6119 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6120 | #ifdef IPPROTO_GRE |
| 6121 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6122 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6123 | #ifdef IPPROTO_ESP |
| 6124 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6125 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6126 | #ifdef IPPROTO_AH |
| 6127 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6128 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | #ifdef IPPROTO_MOBILE |
| 6130 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6131 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6132 | #ifdef IPPROTO_ICMPV6 |
| 6133 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6134 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6135 | #ifdef IPPROTO_NONE |
| 6136 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6137 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6138 | #ifdef IPPROTO_DSTOPTS |
| 6139 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6140 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6141 | #ifdef IPPROTO_XTP |
| 6142 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
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 IPPROTO_EON |
| 6145 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
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 IPPROTO_PIM |
| 6148 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6149 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6150 | #ifdef IPPROTO_IPCOMP |
| 6151 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6152 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6153 | #ifdef IPPROTO_VRRP |
| 6154 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6155 | #endif |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 6156 | #ifdef IPPROTO_SCTP |
| 6157 | PyModule_AddIntConstant(m, "IPPROTO_SCTP", IPPROTO_SCTP); |
| 6158 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6159 | #ifdef IPPROTO_BIP |
| 6160 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6161 | #endif |
| 6162 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6163 | #ifdef IPPROTO_RAW |
| 6164 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 6165 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6166 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6167 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6168 | #ifdef IPPROTO_MAX |
| 6169 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6170 | #endif |
| 6171 | |
Martin v. Löwis | 9d6c669 | 2012-02-03 17:44:58 +0100 | [diff] [blame] | 6172 | #ifdef SYSPROTO_CONTROL |
| 6173 | PyModule_AddIntConstant(m, "SYSPROTO_CONTROL", SYSPROTO_CONTROL); |
| 6174 | #endif |
| 6175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6176 | /* Some port configuration */ |
| 6177 | #ifdef IPPORT_RESERVED |
| 6178 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6179 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6180 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6181 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6182 | #ifdef IPPORT_USERRESERVED |
| 6183 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6184 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6185 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6186 | #endif |
| 6187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6188 | /* Some reserved IP v.4 addresses */ |
| 6189 | #ifdef INADDR_ANY |
| 6190 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6191 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6192 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6193 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6194 | #ifdef INADDR_BROADCAST |
| 6195 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6196 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6197 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6198 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6199 | #ifdef INADDR_LOOPBACK |
| 6200 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6201 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6202 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6203 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6204 | #ifdef INADDR_UNSPEC_GROUP |
| 6205 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6206 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6207 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6208 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6209 | #ifdef INADDR_ALLHOSTS_GROUP |
| 6210 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 6211 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6212 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6213 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6214 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6215 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 6216 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 6217 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6218 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6219 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6220 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6221 | #ifdef INADDR_NONE |
| 6222 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6223 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6224 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 6225 | #endif |
| 6226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6227 | /* IPv4 [gs]etsockopt options */ |
| 6228 | #ifdef IP_OPTIONS |
| 6229 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6230 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6231 | #ifdef IP_HDRINCL |
| 6232 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6233 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6234 | #ifdef IP_TOS |
| 6235 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6236 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6237 | #ifdef IP_TTL |
| 6238 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6239 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6240 | #ifdef IP_RECVOPTS |
| 6241 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6242 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6243 | #ifdef IP_RECVRETOPTS |
| 6244 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6245 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6246 | #ifdef IP_RECVDSTADDR |
| 6247 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6248 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6249 | #ifdef IP_RETOPTS |
| 6250 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6251 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6252 | #ifdef IP_MULTICAST_IF |
| 6253 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6254 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6255 | #ifdef IP_MULTICAST_TTL |
| 6256 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6257 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6258 | #ifdef IP_MULTICAST_LOOP |
| 6259 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6260 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6261 | #ifdef IP_ADD_MEMBERSHIP |
| 6262 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6263 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6264 | #ifdef IP_DROP_MEMBERSHIP |
| 6265 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 6266 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6267 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 6268 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 6269 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6270 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6271 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 6272 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 6273 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6274 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6275 | #ifdef IP_MAX_MEMBERSHIPS |
| 6276 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6277 | #endif |
Charles-François Natali | 564a42c | 2011-12-17 14:59:56 +0100 | [diff] [blame] | 6278 | #ifdef IP_TRANSPARENT |
| 6279 | PyModule_AddIntConstant(m, "IP_TRANSPARENT", IP_TRANSPARENT); |
| 6280 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6282 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 6283 | #ifdef IPV6_JOIN_GROUP |
| 6284 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6285 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6286 | #ifdef IPV6_LEAVE_GROUP |
| 6287 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6288 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6289 | #ifdef IPV6_MULTICAST_HOPS |
| 6290 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6291 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6292 | #ifdef IPV6_MULTICAST_IF |
| 6293 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6294 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6295 | #ifdef IPV6_MULTICAST_LOOP |
| 6296 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6297 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6298 | #ifdef IPV6_UNICAST_HOPS |
| 6299 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6300 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6301 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6302 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6303 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6304 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6305 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6306 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6307 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6308 | #endif |
| 6309 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6310 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6311 | #endif |
| 6312 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6313 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6314 | #endif |
| 6315 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6316 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6317 | #endif |
| 6318 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6319 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6320 | #endif |
| 6321 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6322 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6323 | #endif |
| 6324 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6325 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6326 | #endif |
| 6327 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6328 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6329 | #endif |
| 6330 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6331 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6332 | #endif |
| 6333 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6334 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6335 | #endif |
| 6336 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6337 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6338 | #endif |
| 6339 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6340 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6341 | #endif |
| 6342 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6343 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6344 | #endif |
| 6345 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6346 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6347 | #endif |
| 6348 | #ifdef IPV6_RTHDR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6349 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6350 | #endif |
| 6351 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6352 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6353 | #endif |
| 6354 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6355 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6356 | #endif |
| 6357 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6358 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6359 | #endif |
| 6360 | #ifdef IPV6_TCLASS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6361 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6362 | #endif |
| 6363 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6364 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6365 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 6366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6367 | /* TCP options */ |
| 6368 | #ifdef TCP_NODELAY |
| 6369 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6370 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6371 | #ifdef TCP_MAXSEG |
| 6372 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6373 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6374 | #ifdef TCP_CORK |
| 6375 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6376 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6377 | #ifdef TCP_KEEPIDLE |
| 6378 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6379 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6380 | #ifdef TCP_KEEPINTVL |
| 6381 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6382 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6383 | #ifdef TCP_KEEPCNT |
| 6384 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6385 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | #ifdef TCP_SYNCNT |
| 6387 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6388 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6389 | #ifdef TCP_LINGER2 |
| 6390 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6391 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6392 | #ifdef TCP_DEFER_ACCEPT |
| 6393 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6394 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6395 | #ifdef TCP_WINDOW_CLAMP |
| 6396 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6397 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6398 | #ifdef TCP_INFO |
| 6399 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6400 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6401 | #ifdef TCP_QUICKACK |
| 6402 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 6403 | #endif |
| 6404 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6406 | /* IPX options */ |
| 6407 | #ifdef IPX_TYPE |
| 6408 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 6409 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6410 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 6411 | /* Reliable Datagram Sockets */ |
| 6412 | #ifdef RDS_CMSG_RDMA_ARGS |
| 6413 | PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_ARGS", RDS_CMSG_RDMA_ARGS); |
| 6414 | #endif |
| 6415 | #ifdef RDS_CMSG_RDMA_DEST |
| 6416 | PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_DEST", RDS_CMSG_RDMA_DEST); |
| 6417 | #endif |
| 6418 | #ifdef RDS_CMSG_RDMA_MAP |
| 6419 | PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_MAP", RDS_CMSG_RDMA_MAP); |
| 6420 | #endif |
| 6421 | #ifdef RDS_CMSG_RDMA_STATUS |
| 6422 | PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_STATUS", RDS_CMSG_RDMA_STATUS); |
| 6423 | #endif |
| 6424 | #ifdef RDS_CMSG_RDMA_UPDATE |
| 6425 | PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_UPDATE", RDS_CMSG_RDMA_UPDATE); |
| 6426 | #endif |
| 6427 | #ifdef RDS_RDMA_READWRITE |
| 6428 | PyModule_AddIntConstant(m, "RDS_RDMA_READWRITE", RDS_RDMA_READWRITE); |
| 6429 | #endif |
| 6430 | #ifdef RDS_RDMA_FENCE |
| 6431 | PyModule_AddIntConstant(m, "RDS_RDMA_FENCE", RDS_RDMA_FENCE); |
| 6432 | #endif |
| 6433 | #ifdef RDS_RDMA_INVALIDATE |
| 6434 | PyModule_AddIntConstant(m, "RDS_RDMA_INVALIDATE", RDS_RDMA_INVALIDATE); |
| 6435 | #endif |
| 6436 | #ifdef RDS_RDMA_USE_ONCE |
| 6437 | PyModule_AddIntConstant(m, "RDS_RDMA_USE_ONCE", RDS_RDMA_USE_ONCE); |
| 6438 | #endif |
| 6439 | #ifdef RDS_RDMA_DONTWAIT |
| 6440 | PyModule_AddIntConstant(m, "RDS_RDMA_DONTWAIT", RDS_RDMA_DONTWAIT); |
| 6441 | #endif |
| 6442 | #ifdef RDS_RDMA_NOTIFY_ME |
| 6443 | PyModule_AddIntConstant(m, "RDS_RDMA_NOTIFY_ME", RDS_RDMA_NOTIFY_ME); |
| 6444 | #endif |
| 6445 | #ifdef RDS_RDMA_SILENT |
| 6446 | PyModule_AddIntConstant(m, "RDS_RDMA_SILENT", RDS_RDMA_SILENT); |
| 6447 | #endif |
| 6448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6449 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6450 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6451 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6452 | #endif |
| 6453 | #ifdef EAI_AGAIN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6454 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6455 | #endif |
| 6456 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6457 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6458 | #endif |
| 6459 | #ifdef EAI_FAIL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6460 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6461 | #endif |
| 6462 | #ifdef EAI_FAMILY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6463 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6464 | #endif |
| 6465 | #ifdef EAI_MEMORY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6466 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6467 | #endif |
| 6468 | #ifdef EAI_NODATA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6469 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6470 | #endif |
| 6471 | #ifdef EAI_NONAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6472 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6473 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6474 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6475 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6476 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6477 | #ifdef EAI_SERVICE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6478 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6479 | #endif |
| 6480 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6481 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6482 | #endif |
| 6483 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6484 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6485 | #endif |
| 6486 | #ifdef EAI_BADHINTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6487 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6488 | #endif |
| 6489 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6490 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6491 | #endif |
| 6492 | #ifdef EAI_MAX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6493 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6494 | #endif |
| 6495 | #ifdef AI_PASSIVE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6496 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6497 | #endif |
| 6498 | #ifdef AI_CANONNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6499 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6500 | #endif |
| 6501 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6502 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6503 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6504 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6505 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 6506 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6507 | #ifdef AI_MASK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6508 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6509 | #endif |
| 6510 | #ifdef AI_ALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6511 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6512 | #endif |
| 6513 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6514 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6515 | #endif |
| 6516 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6517 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6518 | #endif |
| 6519 | #ifdef AI_V4MAPPED |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6520 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6521 | #endif |
| 6522 | #ifdef AI_DEFAULT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6523 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6524 | #endif |
| 6525 | #ifdef NI_MAXHOST |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6526 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6527 | #endif |
| 6528 | #ifdef NI_MAXSERV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6529 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6530 | #endif |
| 6531 | #ifdef NI_NOFQDN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6532 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6533 | #endif |
| 6534 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6535 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6536 | #endif |
| 6537 | #ifdef NI_NAMEREQD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6538 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6539 | #endif |
| 6540 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6541 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6542 | #endif |
| 6543 | #ifdef NI_DGRAM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6544 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 6545 | #endif |
| 6546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6547 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6548 | #ifdef SHUT_RD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6549 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6550 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6551 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6552 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6553 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6554 | #endif |
| 6555 | #ifdef SHUT_WR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6556 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6557 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6558 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6559 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6560 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6561 | #endif |
| 6562 | #ifdef SHUT_RDWR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6563 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6564 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6565 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6566 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6567 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 6568 | #endif |
| 6569 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6570 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6571 | { |
| 6572 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 6573 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 6574 | int i; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 6575 | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6576 | PyObject *tmp; |
| 6577 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 6578 | if (tmp == NULL) |
| 6579 | return NULL; |
| 6580 | PyModule_AddObject(m, names[i], tmp); |
| 6581 | } |
| 6582 | } |
| 6583 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 6584 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 6585 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6586 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6587 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6588 | #endif |
| 6589 | #ifdef RCVALL_MAX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6590 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 6591 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 6592 | #endif /* _MSTCPIP_ */ |
| 6593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6594 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 6595 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6596 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 6597 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6598 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 6599 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6600 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6601 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6602 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 6603 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6604 | |
| 6605 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 6606 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6607 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 6608 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6609 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 6612 | #if (SIZEOF_INT != 4) |
| 6613 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 6614 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6615 | unsigned int packed_addr; |
| 6616 | packed_addr = inet_addr(src); |
| 6617 | if (packed_addr == INADDR_NONE) |
| 6618 | return 0; |
| 6619 | memcpy(dst, &packed_addr, 4); |
| 6620 | return 1; |
| 6621 | } |
| 6622 | /* Should set errno to EAFNOSUPPORT */ |
| 6623 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 6626 | const char * |
| 6627 | 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] | 6628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6629 | if (af == AF_INET) { |
| 6630 | struct in_addr packed_addr; |
| 6631 | if (size < 16) |
| 6632 | /* Should set errno to ENOSPC. */ |
| 6633 | return NULL; |
| 6634 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 6635 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 6636 | } |
| 6637 | /* Should set errno to EAFNOSUPPORT */ |
| 6638 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6639 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 6640 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 6641 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 6642 | #endif |