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) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 45 | - an Internet socket address is a pair (hostname, port) |
| 46 | where hostname can be anything recognized by gethostbyname() |
| 47 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 48 | - 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] | 49 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 50 | - an AF_PACKET socket address is a tuple containing a string |
| 51 | specifying the ethernet interface and an integer specifying |
| 52 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 53 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 54 | specify packet-type and ha-type/addr. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 55 | - an AF_TIPC socket address is expressed as |
| 56 | (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] | 57 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 58 | and scope can be one of: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 60 | 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] | 61 | if addr_type is TIPC_ADDR_NAME: |
| 62 | v1 is the server type |
| 63 | v2 is the port identifier |
| 64 | v3 is ignored |
| 65 | if addr_type is TIPC_ADDR_NAMESEQ: |
| 66 | v1 is the server type |
| 67 | v2 is the lower port number |
| 68 | v3 is the upper port number |
| 69 | if addr_type is TIPC_ADDR_ID: |
| 70 | v1 is the node |
| 71 | v2 is the ref |
| 72 | v3 is ignored |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 75 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 77 | - names starting with sock_ are socket object methods |
| 78 | - names starting with socket_ are module-level functions |
| 79 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 81 | */ |
| 82 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 83 | #ifdef __APPLE__ |
| 84 | /* |
| 85 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 86 | * that was build on 10.4 or later to work on that release, weak linking |
| 87 | * comes to the rescue. |
| 88 | */ |
| 89 | # pragma weak inet_aton |
| 90 | #endif |
| 91 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 92 | #include "Python.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 95 | #undef MAX |
| 96 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 97 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 98 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 99 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 100 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 101 | \n\ |
| 102 | Open a socket of the given type. The family argument specifies the\n\ |
| 103 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 104 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 105 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 106 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 107 | \n\ |
| 108 | A socket object represents one endpoint of a network connection.\n\ |
| 109 | \n\ |
| 110 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 111 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 112 | _accept() -- accept connection, returning new socket fd and client address\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 113 | bind(addr) -- bind the socket to a local address\n\ |
| 114 | close() -- close the socket\n\ |
| 115 | connect(addr) -- connect the socket to a remote address\n\ |
| 116 | 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] | 117 | _dup() -- return a new socket fd duplicated from fileno()\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 118 | fileno() -- return underlying file descriptor\n\ |
| 119 | getpeername() -- return remote address [*]\n\ |
| 120 | getsockname() -- return local address\n\ |
| 121 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 122 | gettimeout() -- return timeout or None\n\ |
| 123 | listen(n) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 124 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 125 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 126 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 127 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 128 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 129 | sendall(data[, flags]) -- send all data\n\ |
| 130 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 131 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 132 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 133 | setsockopt(level, optname, value) -- set socket options\n\ |
| 134 | settimeout(None | float) -- set or clear the timeout\n\ |
| 135 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 136 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 137 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 138 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 139 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 140 | I hope some day someone can clean this up please... */ |
| 141 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 142 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 143 | script doesn't get this right, so we hardcode some platform checks below. |
| 144 | On the other hand, not all Linux versions agree, so there the settings |
| 145 | computed by the configure script are needed! */ |
| 146 | |
| 147 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 148 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 149 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 150 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 151 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 153 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 154 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 155 | #endif |
| 156 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 157 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 158 | # if defined(_AIX) || defined(__osf__) |
| 159 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 160 | # elif defined(__sun) || defined(__sgi) |
| 161 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 162 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 163 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 164 | # else |
| 165 | # undef HAVE_GETHOSTBYNAME_R |
| 166 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 169 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 170 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 171 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 172 | #endif |
| 173 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 174 | /* To use __FreeBSD_version */ |
| 175 | #ifdef HAVE_SYS_PARAM_H |
| 176 | #include <sys/param.h> |
| 177 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 178 | /* 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] | 179 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 180 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 181 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 182 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 183 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 184 | #define USE_GETADDRINFO_LOCK |
| 185 | #endif |
| 186 | |
| 187 | #ifdef USE_GETADDRINFO_LOCK |
| 188 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 189 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 190 | #else |
| 191 | #define ACQUIRE_GETADDRINFO_LOCK |
| 192 | #define RELEASE_GETADDRINFO_LOCK |
| 193 | #endif |
| 194 | |
| 195 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 196 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 197 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 199 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 200 | # include <types.h> |
| 201 | # include <io.h> |
| 202 | # include <sys/ioctl.h> |
| 203 | # include <utils.h> |
| 204 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 205 | #endif |
| 206 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 207 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 208 | # include <ioctl.h> |
| 209 | #endif |
| 210 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 211 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 212 | # define INCL_DOS |
| 213 | # define INCL_DOSERRORS |
| 214 | # define INCL_NOPMAPI |
| 215 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 216 | #endif |
| 217 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 218 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 219 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 220 | functions are declared correctly if compiling with |
| 221 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 222 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 223 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 224 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 225 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 226 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 227 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 228 | #undef _XOPEN_SOURCE |
| 229 | #include <sys/socket.h> |
| 230 | #include <sys/types.h> |
| 231 | #include <netinet/in.h> |
| 232 | #ifdef _SS_ALIGNSIZE |
| 233 | #define HAVE_GETADDRINFO 1 |
| 234 | #define HAVE_GETNAMEINFO 1 |
| 235 | #endif |
| 236 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 237 | #define HAVE_INET_PTON |
| 238 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 239 | #endif |
| 240 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 241 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 242 | 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] | 243 | are just busted. Same thing for Solaris. */ |
| 244 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 245 | #define INET_ADDRSTRLEN 16 |
| 246 | #endif |
| 247 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 248 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 249 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 252 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 253 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 254 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 255 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 256 | |
| 257 | /* Addressing includes */ |
| 258 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 259 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 260 | |
| 261 | /* Non-MS WINDOWS includes */ |
| 262 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 264 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Skip Montanaro | eb33e5a | 2007-08-17 12:57:41 +0000 | [diff] [blame] | 265 | # if defined(PYOS_OS2) && defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 266 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 267 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 268 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 269 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 270 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 271 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 272 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 274 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 275 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 276 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 277 | # ifdef HAVE_FCNTL_H |
| 278 | # include <fcntl.h> |
| 279 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 280 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 281 | #endif |
| 282 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 283 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 284 | |
| 285 | #ifndef offsetof |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 287 | #endif |
| 288 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 289 | #ifndef O_NONBLOCK |
| 290 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 291 | #endif |
| 292 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 293 | /* include Python's addrinfo.h unless it causes trouble */ |
| 294 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 295 | /* Do not include addinfo.h on some newer IRIX versions. |
| 296 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 297 | * for example, but not by 6.5.10. |
| 298 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 299 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 300 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 301 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 302 | */ |
| 303 | #else |
| 304 | # include "addrinfo.h" |
| 305 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 306 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 307 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 308 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 309 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 310 | 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] | 311 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 312 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 313 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 314 | #ifdef __APPLE__ |
| 315 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 316 | failure, so we must use the emulation instead of the libinfo |
| 317 | implementation. Unfortunately, performing an autoconf test |
| 318 | for this bug would require DNS access for the machine performing |
| 319 | the configuration, which is not acceptable. Therefore, we |
| 320 | determine the bug just by checking for __APPLE__. If this bug |
| 321 | gets ever fixed, perhaps checking for sys/version.h would be |
| 322 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 323 | #ifndef HAVE_GETNAMEINFO |
| 324 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 325 | Find to check for Jaguar is that it has getnameinfo(), which |
| 326 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 327 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 328 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 329 | |
| 330 | #ifdef HAVE_INET_ATON |
| 331 | #define USE_INET_ATON_WEAKLINK |
| 332 | #endif |
| 333 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 334 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 335 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 336 | /* 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] | 337 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 338 | /* avoid clashes with the C library definition of the symbol. */ |
| 339 | #define getaddrinfo fake_getaddrinfo |
| 340 | #define gai_strerror fake_gai_strerror |
| 341 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 342 | #include "getaddrinfo.c" |
| 343 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 344 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 345 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 346 | #include "getnameinfo.c" |
| 347 | #endif |
| 348 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 349 | #ifdef MS_WINDOWS |
| 350 | /* On Windows a socket is really a handle not an fd */ |
| 351 | static SOCKET |
| 352 | dup_socket(SOCKET handle) |
| 353 | { |
Daniel Stutzbach | 6c76528 | 2010-09-03 12:38:33 +0000 | [diff] [blame] | 354 | WSAPROTOCOL_INFO info; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 355 | |
Daniel Stutzbach | 6c76528 | 2010-09-03 12:38:33 +0000 | [diff] [blame] | 356 | if (WSADuplicateSocket(handle, GetCurrentProcessId(), &info)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | return INVALID_SOCKET; |
Daniel Stutzbach | 6c76528 | 2010-09-03 12:38:33 +0000 | [diff] [blame] | 358 | |
| 359 | return WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 360 | FROM_PROTOCOL_INFO, &info, 0, 0); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 361 | } |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 362 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 363 | #else |
| 364 | /* On Unix we can use dup to duplicate the file descriptor of a socket*/ |
| 365 | #define dup_socket(fd) dup(fd) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 366 | #endif |
| 367 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 368 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 369 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 370 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 371 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 372 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 373 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 374 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 375 | #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 376 | #endif |
| 377 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 378 | #ifndef SOCKETCLOSE |
| 379 | #define SOCKETCLOSE close |
| 380 | #endif |
| 381 | |
Jesse Noller | 32d68c2 | 2009-03-31 18:48:42 +0000 | [diff] [blame] | 382 | #if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) && !defined(__NetBSD__) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 383 | #define USE_BLUETOOTH 1 |
| 384 | #if defined(__FreeBSD__) |
| 385 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 386 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 387 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 388 | #define SOL_HCI SOL_HCI_RAW |
| 389 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 390 | #define sockaddr_l2 sockaddr_l2cap |
| 391 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 392 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 393 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 394 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 395 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 396 | #elif defined(__NetBSD__) |
| 397 | #define sockaddr_l2 sockaddr_bt |
| 398 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 399 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 400 | #define sockaddr_sco sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 401 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 402 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 403 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 404 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 405 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 406 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 407 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 408 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 409 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 410 | #endif |
| 411 | #endif |
| 412 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 413 | #ifdef __VMS |
| 414 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 415 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 416 | #endif |
| 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 419 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 420 | /* |
| 421 | * Constants for getnameinfo() |
| 422 | */ |
| 423 | #if !defined(NI_MAXHOST) |
| 424 | #define NI_MAXHOST 1025 |
| 425 | #endif |
| 426 | #if !defined(NI_MAXSERV) |
| 427 | #define NI_MAXSERV 32 |
| 428 | #endif |
| 429 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 430 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 431 | #define INVALID_SOCKET (-1) |
| 432 | #endif |
| 433 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 434 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 435 | a Py prefix (or use CapitalizedWords). Later... */ |
| 436 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 437 | /* Global variable holding the exception type for errors detected |
| 438 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 439 | static PyObject *socket_error; |
| 440 | static PyObject *socket_herror; |
| 441 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 442 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 443 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 444 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 445 | The sock_type variable contains pointers to various functions, |
| 446 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 447 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 448 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 449 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 450 | #if defined(HAVE_POLL_H) |
| 451 | #include <poll.h> |
| 452 | #elif defined(HAVE_SYS_POLL_H) |
| 453 | #include <sys/poll.h> |
| 454 | #endif |
| 455 | |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 456 | #ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
| 457 | /* Platform can select file descriptors beyond FD_SETSIZE */ |
| 458 | #define IS_SELECTABLE(s) 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 459 | #elif defined(HAVE_POLL) |
| 460 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 461 | #define IS_SELECTABLE(s) 1 |
| 462 | /* 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] | 463 | #else |
| 464 | /* POSIX says selecting file descriptors beyond FD_SETSIZE |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 465 | has undefined behaviour. If there's no timeout left, we don't have to |
| 466 | call select, so it's a safe, little white lie. */ |
| 467 | #define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 468 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 469 | |
| 470 | static PyObject* |
| 471 | select_error(void) |
| 472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 474 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 477 | #ifdef MS_WINDOWS |
Antoine Pitrou | c8f0196 | 2010-09-28 22:03:27 +0000 | [diff] [blame] | 478 | #ifndef WSAEAGAIN |
| 479 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 480 | #endif |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 481 | #define CHECK_ERRNO(expected) \ |
| 482 | (WSAGetLastError() == WSA ## expected) |
| 483 | #else |
| 484 | #define CHECK_ERRNO(expected) \ |
| 485 | (errno == expected) |
| 486 | #endif |
| 487 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 488 | /* Convenience function to raise an error according to errno |
| 489 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 491 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 492 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 493 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 494 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | int err_no = WSAGetLastError(); |
| 496 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 497 | recognizes the error codes used by both GetLastError() and |
| 498 | WSAGetLastError */ |
| 499 | if (err_no) |
| 500 | return PyErr_SetExcFromWindowsErr(socket_error, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 501 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 502 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 503 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | if (sock_errno() != NO_ERROR) { |
| 505 | APIRET rc; |
| 506 | ULONG msglen; |
| 507 | char outbuf[100]; |
| 508 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 511 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 512 | myerrorcode - SOCBASEERR + 26, |
| 513 | "mptn.msg", |
| 514 | &msglen); |
| 515 | if (rc == NO_ERROR) { |
| 516 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | /* OS/2 doesn't guarantee a terminator */ |
| 519 | outbuf[msglen] = '\0'; |
| 520 | if (strlen(outbuf) > 0) { |
| 521 | /* If non-empty msg, trim CRLF */ |
| 522 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 523 | while (lastc > outbuf && |
| 524 | isspace(Py_CHARMASK(*lastc))) { |
| 525 | /* Trim trailing whitespace (CRLF) */ |
| 526 | *lastc-- = '\0'; |
| 527 | } |
| 528 | } |
| 529 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 530 | if (v != NULL) { |
| 531 | PyErr_SetObject(socket_error, v); |
| 532 | Py_DECREF(v); |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
| 536 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 537 | #endif |
| 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 542 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 543 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 544 | set_herror(int h_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 | |
| 548 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 550 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 552 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | if (v != NULL) { |
| 554 | PyErr_SetObject(socket_herror, v); |
| 555 | Py_DECREF(v); |
| 556 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
| 562 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 563 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 564 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 566 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 567 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 569 | if (error == EAI_SYSTEM) |
| 570 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 571 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 572 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 573 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 575 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 577 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | if (v != NULL) { |
| 579 | PyErr_SetObject(socket_gaierror, v); |
| 580 | Py_DECREF(v); |
| 581 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 586 | #ifdef __VMS |
| 587 | /* Function to send in segments */ |
| 588 | static int |
| 589 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | int n = 0; |
| 592 | int remaining = len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | while (remaining > 0) { |
| 595 | unsigned int segment; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 598 | n = send(sock_fd, buf, segment, flags); |
| 599 | if (n < 0) { |
| 600 | return n; |
| 601 | } |
| 602 | remaining -= segment; |
| 603 | buf += segment; |
| 604 | } /* end while */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | return len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 607 | } |
| 608 | #endif |
| 609 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 610 | /* Function to perform the setting of socket blocking mode |
| 611 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 612 | static int |
| 613 | internal_setblocking(PySocketSockObject *s, int block) |
| 614 | { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 615 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 617 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 620 | #ifndef MS_WINDOWS |
| 621 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | block = !block; |
| 623 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 624 | #elif defined(__VMS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | block = !block; |
| 626 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 627 | #else /* !PYOS_OS2 && !__VMS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 629 | if (block) |
| 630 | delay_flag &= (~O_NONBLOCK); |
| 631 | else |
| 632 | delay_flag |= O_NONBLOCK; |
| 633 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 634 | #endif /* !PYOS_OS2 */ |
| 635 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | block = !block; |
| 637 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 638 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | /* Since these don't return anything */ |
| 642 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 645 | /* 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] | 646 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 647 | This does not raise an exception; we'll let our caller do that |
| 648 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 649 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 650 | static int |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 651 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 656 | if (s->sock_timeout <= 0.0) |
| 657 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | /* Guard against closed socket */ |
| 660 | if (s->sock_fd < 0) |
| 661 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 663 | /* Handling this condition here simplifies the select loops */ |
| 664 | if (interval < 0.0) |
| 665 | return 1; |
| 666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | /* Prefer poll, if available, since you can poll() any fd |
| 668 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 669 | #ifdef HAVE_POLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | { |
| 671 | struct pollfd pollfd; |
| 672 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | pollfd.fd = s->sock_fd; |
| 675 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | /* s->sock_timeout is in seconds, timeout in ms */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 678 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | n = poll(&pollfd, 1, timeout); |
| 680 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 681 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | { |
| 683 | /* Construct the arguments to select */ |
| 684 | fd_set fds; |
| 685 | struct timeval tv; |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 686 | tv.tv_sec = (int)interval; |
| 687 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | FD_ZERO(&fds); |
| 689 | FD_SET(s->sock_fd, &fds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | /* See if the socket is ready */ |
| 692 | if (writing) |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 693 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 694 | NULL, &fds, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | else |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 696 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 697 | &fds, NULL, NULL, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 699 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | if (n < 0) |
| 702 | return -1; |
| 703 | if (n == 0) |
| 704 | return 1; |
| 705 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 708 | static int |
| 709 | internal_select(PySocketSockObject *s, int writing) |
| 710 | { |
| 711 | return internal_select_ex(s, writing, s->sock_timeout); |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | Two macros for automatic retry of select() in case of false positives |
| 716 | (for example, select() could indicate a socket is ready for reading |
| 717 | but the data then discarded by the OS because of a wrong checksum). |
| 718 | Here is an example of use: |
| 719 | |
| 720 | BEGIN_SELECT_LOOP(s) |
| 721 | Py_BEGIN_ALLOW_THREADS |
| 722 | timeout = internal_select_ex(s, 0, interval); |
| 723 | if (!timeout) |
| 724 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 725 | Py_END_ALLOW_THREADS |
| 726 | if (timeout == 1) { |
| 727 | PyErr_SetString(socket_timeout, "timed out"); |
| 728 | return -1; |
| 729 | } |
| 730 | END_SELECT_LOOP(s) |
| 731 | */ |
| 732 | |
| 733 | #define BEGIN_SELECT_LOOP(s) \ |
| 734 | { \ |
| 735 | _PyTime_timeval now, deadline = {0, 0}; \ |
| 736 | double interval = s->sock_timeout; \ |
| 737 | int has_timeout = s->sock_timeout > 0.0; \ |
| 738 | if (has_timeout) { \ |
| 739 | _PyTime_gettimeofday(&now); \ |
| 740 | deadline = now; \ |
| 741 | _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \ |
| 742 | } \ |
| 743 | while (1) { \ |
| 744 | errno = 0; \ |
| 745 | |
| 746 | #define END_SELECT_LOOP(s) \ |
| 747 | if (!has_timeout || \ |
| 748 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 749 | break; \ |
| 750 | _PyTime_gettimeofday(&now); \ |
| 751 | interval = _PyTime_INTERVAL(now, deadline); \ |
| 752 | } \ |
| 753 | } \ |
| 754 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 755 | /* Initialize a new socket object. */ |
| 756 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 757 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 758 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 759 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 760 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | s->sock_fd = fd; |
| 764 | s->sock_family = family; |
| 765 | s->sock_type = type; |
| 766 | s->sock_proto = proto; |
| 767 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | if (defaulttimeout >= 0.0) |
| 772 | internal_setblocking(s, 0); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 773 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 777 | /* Create a new socket object. |
| 778 | This just creates the object and initializes it. |
| 779 | If the creation fails, return NULL and set an exception (implicit |
| 780 | in NEWOBJ()). */ |
| 781 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 782 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 783 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | PySocketSockObject *s; |
| 786 | s = (PySocketSockObject *) |
| 787 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 788 | if (s != NULL) |
| 789 | init_sockobject(s, fd, family, type, proto); |
| 790 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 793 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 794 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 795 | thread to be in gethostbyname or getaddrinfo */ |
| 796 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 797 | PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 798 | #endif |
| 799 | |
| 800 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 801 | /* Convert a string specifying a host name or one of a few symbolic |
| 802 | names to a numeric IP address. This usually calls gethostbyname() |
| 803 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 804 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 805 | an error occurred; then an exception is raised. */ |
| 806 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 807 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 808 | 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] | 809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | struct addrinfo hints, *res; |
| 811 | int error; |
| 812 | int d1, d2, d3, d4; |
| 813 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 816 | if (name[0] == '\0') { |
| 817 | int siz; |
| 818 | memset(&hints, 0, sizeof(hints)); |
| 819 | hints.ai_family = af; |
| 820 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 821 | hints.ai_flags = AI_PASSIVE; |
| 822 | Py_BEGIN_ALLOW_THREADS |
| 823 | ACQUIRE_GETADDRINFO_LOCK |
| 824 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 825 | Py_END_ALLOW_THREADS |
| 826 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 827 | *are* safe regarding their return value, ie. that a |
| 828 | subsequent call to getaddrinfo() does not destroy the |
| 829 | outcome of the first call. */ |
| 830 | RELEASE_GETADDRINFO_LOCK |
| 831 | if (error) { |
| 832 | set_gaierror(error); |
| 833 | return -1; |
| 834 | } |
| 835 | switch (res->ai_family) { |
| 836 | case AF_INET: |
| 837 | siz = 4; |
| 838 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 839 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | case AF_INET6: |
| 841 | siz = 16; |
| 842 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 843 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | default: |
| 845 | freeaddrinfo(res); |
| 846 | PyErr_SetString(socket_error, |
| 847 | "unsupported address family"); |
| 848 | return -1; |
| 849 | } |
| 850 | if (res->ai_next) { |
| 851 | freeaddrinfo(res); |
| 852 | PyErr_SetString(socket_error, |
| 853 | "wildcard resolved to multiple address"); |
| 854 | return -1; |
| 855 | } |
| 856 | if (res->ai_addrlen < addr_ret_size) |
| 857 | addr_ret_size = res->ai_addrlen; |
| 858 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 859 | freeaddrinfo(res); |
| 860 | return siz; |
| 861 | } |
| 862 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 863 | struct sockaddr_in *sin; |
| 864 | if (af != AF_INET && af != AF_UNSPEC) { |
| 865 | PyErr_SetString(socket_error, |
| 866 | "address family mismatched"); |
| 867 | return -1; |
| 868 | } |
| 869 | sin = (struct sockaddr_in *)addr_ret; |
| 870 | memset((void *) sin, '\0', sizeof(*sin)); |
| 871 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 872 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 874 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 876 | return sizeof(sin->sin_addr); |
| 877 | } |
| 878 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 879 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 880 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 881 | struct sockaddr_in *sin; |
| 882 | sin = (struct sockaddr_in *)addr_ret; |
| 883 | sin->sin_addr.s_addr = htonl( |
| 884 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 885 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 886 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 887 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 889 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | return 4; |
| 891 | } |
| 892 | memset(&hints, 0, sizeof(hints)); |
| 893 | hints.ai_family = af; |
| 894 | Py_BEGIN_ALLOW_THREADS |
| 895 | ACQUIRE_GETADDRINFO_LOCK |
| 896 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 897 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 899 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 900 | if no address family is given. Assume IPv4 for now.*/ |
| 901 | hints.ai_family = AF_INET; |
| 902 | error = getaddrinfo(name, NULL, &hints, &res); |
| 903 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 904 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | Py_END_ALLOW_THREADS |
| 906 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 907 | if (error) { |
| 908 | set_gaierror(error); |
| 909 | return -1; |
| 910 | } |
| 911 | if (res->ai_addrlen < addr_ret_size) |
| 912 | addr_ret_size = res->ai_addrlen; |
| 913 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 914 | freeaddrinfo(res); |
| 915 | switch (addr_ret->sa_family) { |
| 916 | case AF_INET: |
| 917 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 918 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | case AF_INET6: |
| 920 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 921 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | default: |
| 923 | PyErr_SetString(socket_error, "unknown address family"); |
| 924 | return -1; |
| 925 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 928 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 929 | /* Create a string object representing an IP address. |
| 930 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 931 | size numbers). */ |
| 932 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 933 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 934 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 935 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | char buf[NI_MAXHOST]; |
| 937 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 940 | NI_NUMERICHOST); |
| 941 | if (error) { |
| 942 | set_gaierror(error); |
| 943 | return NULL; |
| 944 | } |
| 945 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 949 | #ifdef USE_BLUETOOTH |
| 950 | /* Convert a string representation of a Bluetooth address into a numeric |
| 951 | address. Returns the length (6), or raises an exception and returns -1 if |
| 952 | an error occurred. */ |
| 953 | |
| 954 | static int |
| 955 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | unsigned int b0, b1, b2, b3, b4, b5; |
| 958 | char ch; |
| 959 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 962 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 963 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 964 | bdaddr->b[0] = b0; |
| 965 | bdaddr->b[1] = b1; |
| 966 | bdaddr->b[2] = b2; |
| 967 | bdaddr->b[3] = b3; |
| 968 | bdaddr->b[4] = b4; |
| 969 | bdaddr->b[5] = b5; |
| 970 | return 6; |
| 971 | } else { |
| 972 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 973 | return -1; |
| 974 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* Create a string representation of the Bluetooth address. This is always a |
| 978 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 979 | value (zero padded if necessary). */ |
| 980 | |
| 981 | static PyObject * |
| 982 | makebdaddr(bdaddr_t *bdaddr) |
| 983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 987 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 988 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 989 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 990 | } |
| 991 | #endif |
| 992 | |
| 993 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 994 | /* Create an object representing the given socket address, |
| 995 | suitable for passing it back to bind(), connect() etc. |
| 996 | The family field of the sockaddr structure is inspected |
| 997 | to determine what kind of address it really is. */ |
| 998 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 999 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1000 | static PyObject * |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 1001 | 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] | 1002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | if (addrlen == 0) { |
| 1004 | /* No address -- may be recvfrom() from known socket */ |
| 1005 | Py_INCREF(Py_None); |
| 1006 | return Py_None; |
| 1007 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | case AF_INET: |
| 1012 | { |
| 1013 | struct sockaddr_in *a; |
| 1014 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1015 | PyObject *ret = NULL; |
| 1016 | if (addrobj) { |
| 1017 | a = (struct sockaddr_in *)addr; |
| 1018 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1019 | Py_DECREF(addrobj); |
| 1020 | } |
| 1021 | return ret; |
| 1022 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1023 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1024 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | case AF_UNIX: |
| 1026 | { |
| 1027 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1028 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1030 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 1031 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
| 1032 | } |
| 1033 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1034 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | { |
| 1036 | /* regular NULL-terminated string */ |
| 1037 | return PyUnicode_FromString(a->sun_path); |
| 1038 | } |
| 1039 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1040 | #endif /* AF_UNIX */ |
| 1041 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1042 | #if defined(AF_NETLINK) |
| 1043 | case AF_NETLINK: |
| 1044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1046 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1047 | } |
| 1048 | #endif /* AF_NETLINK */ |
| 1049 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1050 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | case AF_INET6: |
| 1052 | { |
| 1053 | struct sockaddr_in6 *a; |
| 1054 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1055 | PyObject *ret = NULL; |
| 1056 | if (addrobj) { |
| 1057 | a = (struct sockaddr_in6 *)addr; |
| 1058 | ret = Py_BuildValue("Oiii", |
| 1059 | addrobj, |
| 1060 | ntohs(a->sin6_port), |
| 1061 | a->sin6_flowinfo, |
| 1062 | a->sin6_scope_id); |
| 1063 | Py_DECREF(addrobj); |
| 1064 | } |
| 1065 | return ret; |
| 1066 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1067 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1068 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1069 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | case AF_BLUETOOTH: |
| 1071 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | case BTPROTO_L2CAP: |
| 1074 | { |
| 1075 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1076 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1077 | PyObject *ret = NULL; |
| 1078 | if (addrobj) { |
| 1079 | ret = Py_BuildValue("Oi", |
| 1080 | addrobj, |
| 1081 | _BT_L2_MEMB(a, psm)); |
| 1082 | Py_DECREF(addrobj); |
| 1083 | } |
| 1084 | return ret; |
| 1085 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | case BTPROTO_RFCOMM: |
| 1088 | { |
| 1089 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1090 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1091 | PyObject *ret = NULL; |
| 1092 | if (addrobj) { |
| 1093 | ret = Py_BuildValue("Oi", |
| 1094 | addrobj, |
| 1095 | _BT_RC_MEMB(a, channel)); |
| 1096 | Py_DECREF(addrobj); |
| 1097 | } |
| 1098 | return ret; |
| 1099 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | case BTPROTO_HCI: |
| 1102 | { |
| 1103 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
| 1104 | PyObject *ret = NULL; |
| 1105 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1106 | return ret; |
| 1107 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1108 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1109 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | case BTPROTO_SCO: |
| 1111 | { |
| 1112 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1113 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1114 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1115 | #endif |
| 1116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | default: |
| 1118 | PyErr_SetString(PyExc_ValueError, |
| 1119 | "Unknown Bluetooth protocol"); |
| 1120 | return NULL; |
| 1121 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1122 | #endif |
| 1123 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1124 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | case AF_PACKET: |
| 1126 | { |
| 1127 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1128 | char *ifname = ""; |
| 1129 | struct ifreq ifr; |
| 1130 | /* need to look up interface name give index */ |
| 1131 | if (a->sll_ifindex) { |
| 1132 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1133 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1134 | ifname = ifr.ifr_name; |
| 1135 | } |
| 1136 | return Py_BuildValue("shbhy#", |
| 1137 | ifname, |
| 1138 | ntohs(a->sll_protocol), |
| 1139 | a->sll_pkttype, |
| 1140 | a->sll_hatype, |
| 1141 | a->sll_addr, |
| 1142 | a->sll_halen); |
| 1143 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1144 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1145 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1146 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | case AF_TIPC: |
| 1148 | { |
| 1149 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1150 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1151 | return Py_BuildValue("IIIII", |
| 1152 | a->addrtype, |
| 1153 | a->addr.nameseq.type, |
| 1154 | a->addr.nameseq.lower, |
| 1155 | a->addr.nameseq.upper, |
| 1156 | a->scope); |
| 1157 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1158 | return Py_BuildValue("IIIII", |
| 1159 | a->addrtype, |
| 1160 | a->addr.name.name.type, |
| 1161 | a->addr.name.name.instance, |
| 1162 | a->addr.name.name.instance, |
| 1163 | a->scope); |
| 1164 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1165 | return Py_BuildValue("IIIII", |
| 1166 | a->addrtype, |
| 1167 | a->addr.id.node, |
| 1168 | a->addr.id.ref, |
| 1169 | 0, |
| 1170 | a->scope); |
| 1171 | } else { |
| 1172 | PyErr_SetString(PyExc_ValueError, |
| 1173 | "Invalid address type"); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1177 | #endif |
| 1178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | default: |
| 1182 | /* If we don't know the address family, don't raise an |
| 1183 | exception -- return it as an (int, bytes) tuple. */ |
| 1184 | return Py_BuildValue("iy#", |
| 1185 | addr->sa_family, |
| 1186 | addr->sa_data, |
| 1187 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1192 | |
| 1193 | /* Parse a socket address argument according to the socket object's |
| 1194 | address family. Return 1 if the address was in the proper format, |
| 1195 | 0 of not. The address is returned through addr_ret, its length |
| 1196 | through len_ret. */ |
| 1197 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1198 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1199 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1203 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1204 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | case AF_UNIX: |
| 1206 | { |
| 1207 | struct sockaddr_un* addr; |
| 1208 | char *path; |
| 1209 | int len; |
| 1210 | if (!PyArg_Parse(args, "s#", &path, &len)) |
| 1211 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1214 | #ifdef linux |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | if (len > 0 && path[0] == 0) { |
| 1216 | /* Linux abstract namespace extension */ |
| 1217 | if (len > sizeof addr->sun_path) { |
| 1218 | PyErr_SetString(socket_error, |
| 1219 | "AF_UNIX path too long"); |
| 1220 | return 0; |
| 1221 | } |
| 1222 | } |
| 1223 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1224 | #endif /* linux */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | { |
| 1226 | /* regular NULL-terminated string */ |
| 1227 | if (len >= sizeof addr->sun_path) { |
| 1228 | PyErr_SetString(socket_error, |
| 1229 | "AF_UNIX path too long"); |
| 1230 | return 0; |
| 1231 | } |
| 1232 | addr->sun_path[len] = 0; |
| 1233 | } |
| 1234 | addr->sun_family = s->sock_family; |
| 1235 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1236 | #if defined(PYOS_OS2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | *len_ret = sizeof(*addr); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1238 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1240 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | return 1; |
| 1242 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1243 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1244 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1245 | #if defined(AF_NETLINK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | case AF_NETLINK: |
| 1247 | { |
| 1248 | struct sockaddr_nl* addr; |
| 1249 | int pid, groups; |
| 1250 | addr = (struct sockaddr_nl *)addr_ret; |
| 1251 | if (!PyTuple_Check(args)) { |
| 1252 | PyErr_Format( |
| 1253 | PyExc_TypeError, |
| 1254 | "getsockaddrarg: " |
| 1255 | "AF_NETLINK address must be tuple, not %.500s", |
| 1256 | Py_TYPE(args)->tp_name); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1260 | return 0; |
| 1261 | addr->nl_family = AF_NETLINK; |
| 1262 | addr->nl_pid = pid; |
| 1263 | addr->nl_groups = groups; |
| 1264 | *len_ret = sizeof(*addr); |
| 1265 | return 1; |
| 1266 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1267 | #endif |
| 1268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | case AF_INET: |
| 1270 | { |
| 1271 | struct sockaddr_in* addr; |
| 1272 | char *host; |
| 1273 | int port, result; |
| 1274 | if (!PyTuple_Check(args)) { |
| 1275 | PyErr_Format( |
| 1276 | PyExc_TypeError, |
| 1277 | "getsockaddrarg: " |
| 1278 | "AF_INET address must be tuple, not %.500s", |
| 1279 | Py_TYPE(args)->tp_name); |
| 1280 | return 0; |
| 1281 | } |
| 1282 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1283 | "idna", &host, &port)) |
| 1284 | return 0; |
| 1285 | addr=(struct sockaddr_in*)addr_ret; |
| 1286 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1287 | sizeof(*addr), AF_INET); |
| 1288 | PyMem_Free(host); |
| 1289 | if (result < 0) |
| 1290 | return 0; |
| 1291 | if (port < 0 || port > 0xffff) { |
| 1292 | PyErr_SetString( |
| 1293 | PyExc_OverflowError, |
| 1294 | "getsockaddrarg: port must be 0-65535."); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | addr->sin_family = AF_INET; |
| 1298 | addr->sin_port = htons((short)port); |
| 1299 | *len_ret = sizeof *addr; |
| 1300 | return 1; |
| 1301 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1302 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1303 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1304 | case AF_INET6: |
| 1305 | { |
| 1306 | struct sockaddr_in6* addr; |
| 1307 | char *host; |
| 1308 | int port, flowinfo, scope_id, result; |
| 1309 | flowinfo = scope_id = 0; |
| 1310 | if (!PyTuple_Check(args)) { |
| 1311 | PyErr_Format( |
| 1312 | PyExc_TypeError, |
| 1313 | "getsockaddrarg: " |
| 1314 | "AF_INET6 address must be tuple, not %.500s", |
| 1315 | Py_TYPE(args)->tp_name); |
| 1316 | return 0; |
| 1317 | } |
| 1318 | if (!PyArg_ParseTuple(args, "eti|ii", |
| 1319 | "idna", &host, &port, &flowinfo, |
| 1320 | &scope_id)) { |
| 1321 | return 0; |
| 1322 | } |
| 1323 | addr = (struct sockaddr_in6*)addr_ret; |
| 1324 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1325 | sizeof(*addr), AF_INET6); |
| 1326 | PyMem_Free(host); |
| 1327 | if (result < 0) |
| 1328 | return 0; |
| 1329 | if (port < 0 || port > 0xffff) { |
| 1330 | PyErr_SetString( |
| 1331 | PyExc_OverflowError, |
| 1332 | "getsockaddrarg: port must be 0-65535."); |
| 1333 | return 0; |
| 1334 | } |
| 1335 | addr->sin6_family = s->sock_family; |
| 1336 | addr->sin6_port = htons((short)port); |
| 1337 | addr->sin6_flowinfo = flowinfo; |
| 1338 | addr->sin6_scope_id = scope_id; |
| 1339 | *len_ret = sizeof *addr; |
| 1340 | return 1; |
| 1341 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1342 | #endif |
| 1343 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1344 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | case AF_BLUETOOTH: |
| 1346 | { |
| 1347 | switch (s->sock_proto) { |
| 1348 | case BTPROTO_L2CAP: |
| 1349 | { |
| 1350 | struct sockaddr_l2 *addr; |
| 1351 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1354 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1355 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1356 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1357 | &_BT_L2_MEMB(addr, psm))) { |
| 1358 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1359 | "wrong format"); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1363 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | *len_ret = sizeof *addr; |
| 1366 | return 1; |
| 1367 | } |
| 1368 | case BTPROTO_RFCOMM: |
| 1369 | { |
| 1370 | struct sockaddr_rc *addr; |
| 1371 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | addr = (struct sockaddr_rc *)addr_ret; |
| 1374 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1375 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1376 | &_BT_RC_MEMB(addr, channel))) { |
| 1377 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1378 | "wrong format"); |
| 1379 | return 0; |
| 1380 | } |
| 1381 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1382 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | *len_ret = sizeof *addr; |
| 1385 | return 1; |
| 1386 | } |
| 1387 | case BTPROTO_HCI: |
| 1388 | { |
| 1389 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
| 1390 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1391 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1392 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1393 | "wrong format"); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | *len_ret = sizeof *addr; |
| 1397 | return 1; |
| 1398 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1399 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | case BTPROTO_SCO: |
| 1401 | { |
| 1402 | struct sockaddr_sco *addr; |
| 1403 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | addr = (struct sockaddr_sco *)addr_ret; |
| 1406 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1407 | if (!PyBytes_Check(args)) { |
| 1408 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1409 | "wrong format"); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | straddr = PyBytes_AS_STRING(args); |
| 1413 | if (setbdaddr(straddr, &_BT_SCO_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 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1419 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | default: |
| 1421 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1422 | return 0; |
| 1423 | } |
| 1424 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1425 | #endif |
| 1426 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1427 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | case AF_PACKET: |
| 1429 | { |
| 1430 | struct sockaddr_ll* addr; |
| 1431 | struct ifreq ifr; |
| 1432 | char *interfaceName; |
| 1433 | int protoNumber; |
| 1434 | int hatype = 0; |
| 1435 | int pkttype = 0; |
| 1436 | char *haddr = NULL; |
| 1437 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | if (!PyTuple_Check(args)) { |
| 1440 | PyErr_Format( |
| 1441 | PyExc_TypeError, |
| 1442 | "getsockaddrarg: " |
| 1443 | "AF_PACKET address must be tuple, not %.500s", |
| 1444 | Py_TYPE(args)->tp_name); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, |
| 1448 | &protoNumber, &pkttype, &hatype, |
| 1449 | &haddr, &halen)) |
| 1450 | return 0; |
| 1451 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1452 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1453 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1454 | s->errorhandler(); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | if (halen > 8) { |
| 1458 | PyErr_SetString(PyExc_ValueError, |
| 1459 | "Hardware address must be 8 bytes or less"); |
| 1460 | return 0; |
| 1461 | } |
| 1462 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1463 | PyErr_SetString( |
| 1464 | PyExc_OverflowError, |
| 1465 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1466 | return 0; |
| 1467 | } |
| 1468 | addr = (struct sockaddr_ll*)addr_ret; |
| 1469 | addr->sll_family = AF_PACKET; |
| 1470 | addr->sll_protocol = htons((short)protoNumber); |
| 1471 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1472 | addr->sll_pkttype = pkttype; |
| 1473 | addr->sll_hatype = hatype; |
| 1474 | if (halen != 0) { |
| 1475 | memcpy(&addr->sll_addr, haddr, halen); |
| 1476 | } |
| 1477 | addr->sll_halen = halen; |
| 1478 | *len_ret = sizeof *addr; |
| 1479 | return 1; |
| 1480 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1481 | #endif |
| 1482 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1483 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | case AF_TIPC: |
| 1485 | { |
| 1486 | unsigned int atype, v1, v2, v3; |
| 1487 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1488 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | if (!PyTuple_Check(args)) { |
| 1491 | PyErr_Format( |
| 1492 | PyExc_TypeError, |
| 1493 | "getsockaddrarg: " |
| 1494 | "AF_TIPC address must be tuple, not %.500s", |
| 1495 | Py_TYPE(args)->tp_name); |
| 1496 | return 0; |
| 1497 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | if (!PyArg_ParseTuple(args, |
| 1500 | "IIII|I;Invalid TIPC address format", |
| 1501 | &atype, &v1, &v2, &v3, &scope)) |
| 1502 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1505 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | addr->family = AF_TIPC; |
| 1508 | addr->scope = scope; |
| 1509 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1512 | addr->addr.nameseq.type = v1; |
| 1513 | addr->addr.nameseq.lower = v2; |
| 1514 | addr->addr.nameseq.upper = v3; |
| 1515 | } else if (atype == TIPC_ADDR_NAME) { |
| 1516 | addr->addr.name.name.type = v1; |
| 1517 | addr->addr.name.name.instance = v2; |
| 1518 | } else if (atype == TIPC_ADDR_ID) { |
| 1519 | addr->addr.id.node = v1; |
| 1520 | addr->addr.id.ref = v2; |
| 1521 | } else { |
| 1522 | /* Shouldn't happen */ |
| 1523 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1524 | return 0; |
| 1525 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | return 1; |
| 1530 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1531 | #endif |
| 1532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | default: |
| 1536 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1537 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1542 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1543 | /* 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] | 1544 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1545 | through len_ret. */ |
| 1546 | |
| 1547 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1548 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1551 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1552 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1553 | case AF_UNIX: |
| 1554 | { |
| 1555 | *len_ret = sizeof (struct sockaddr_un); |
| 1556 | return 1; |
| 1557 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1558 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1559 | #if defined(AF_NETLINK) |
| 1560 | case AF_NETLINK: |
| 1561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | *len_ret = sizeof (struct sockaddr_nl); |
| 1563 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1564 | } |
| 1565 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | case AF_INET: |
| 1568 | { |
| 1569 | *len_ret = sizeof (struct sockaddr_in); |
| 1570 | return 1; |
| 1571 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1572 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1573 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | case AF_INET6: |
| 1575 | { |
| 1576 | *len_ret = sizeof (struct sockaddr_in6); |
| 1577 | return 1; |
| 1578 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1579 | #endif |
| 1580 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1581 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | case AF_BLUETOOTH: |
| 1583 | { |
| 1584 | switch(s->sock_proto) |
| 1585 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | case BTPROTO_L2CAP: |
| 1588 | *len_ret = sizeof (struct sockaddr_l2); |
| 1589 | return 1; |
| 1590 | case BTPROTO_RFCOMM: |
| 1591 | *len_ret = sizeof (struct sockaddr_rc); |
| 1592 | return 1; |
| 1593 | case BTPROTO_HCI: |
| 1594 | *len_ret = sizeof (struct sockaddr_hci); |
| 1595 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1596 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | case BTPROTO_SCO: |
| 1598 | *len_ret = sizeof (struct sockaddr_sco); |
| 1599 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1600 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | default: |
| 1602 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1603 | "unknown BT protocol"); |
| 1604 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1608 | #endif |
| 1609 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1610 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | case AF_PACKET: |
| 1612 | { |
| 1613 | *len_ret = sizeof (struct sockaddr_ll); |
| 1614 | return 1; |
| 1615 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1616 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1617 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1618 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | case AF_TIPC: |
| 1620 | { |
| 1621 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1622 | return 1; |
| 1623 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1624 | #endif |
| 1625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | default: |
| 1629 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1630 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1636 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1637 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1638 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1639 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | sock_addr_t addrbuf; |
| 1642 | SOCKET_T newfd = INVALID_SOCKET; |
| 1643 | socklen_t addrlen; |
| 1644 | PyObject *sock = NULL; |
| 1645 | PyObject *addr = NULL; |
| 1646 | PyObject *res = NULL; |
| 1647 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | if (!getsockaddrlen(s, &addrlen)) |
| 1650 | return NULL; |
| 1651 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | if (!IS_SELECTABLE(s)) |
| 1654 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1656 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1658 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | if (!timeout) |
| 1660 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1661 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | if (timeout == 1) { |
| 1664 | PyErr_SetString(socket_timeout, "timed out"); |
| 1665 | return NULL; |
| 1666 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 1667 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | if (newfd == INVALID_SOCKET) |
| 1670 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | sock = PyLong_FromSocket_t(newfd); |
| 1673 | if (sock == NULL) { |
| 1674 | SOCKETCLOSE(newfd); |
| 1675 | goto finally; |
| 1676 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1679 | addrlen, s->sock_proto); |
| 1680 | if (addr == NULL) |
| 1681 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1684 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1685 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | Py_XDECREF(sock); |
| 1687 | Py_XDECREF(addr); |
| 1688 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1691 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1692 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1693 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1694 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 1695 | representing the connection, and the address of the client.\n\ |
| 1696 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1697 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1698 | /* s.setblocking(flag) method. Argument: |
| 1699 | False -- non-blocking mode; same as settimeout(0) |
| 1700 | True -- blocking mode; same as settimeout(None) |
| 1701 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1702 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1703 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1704 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1705 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | block = PyLong_AsLong(arg); |
| 1709 | if (block == -1 && PyErr_Occurred()) |
| 1710 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | s->sock_timeout = block ? -1.0 : 0.0; |
| 1713 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | Py_INCREF(Py_None); |
| 1716 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1717 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1718 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1719 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1720 | "setblocking(flag)\n\ |
| 1721 | \n\ |
| 1722 | 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] | 1723 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1724 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1725 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1726 | /* s.settimeout(timeout) method. Argument: |
| 1727 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1728 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1729 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1730 | < 0 -- illegal; raises an exception |
| 1731 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1732 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1733 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | if (arg == Py_None) |
| 1738 | timeout = -1.0; |
| 1739 | else { |
| 1740 | timeout = PyFloat_AsDouble(arg); |
| 1741 | if (timeout < 0.0) { |
| 1742 | if (!PyErr_Occurred()) |
| 1743 | PyErr_SetString(PyExc_ValueError, |
| 1744 | "Timeout value out of range"); |
| 1745 | return NULL; |
| 1746 | } |
| 1747 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | s->sock_timeout = timeout; |
| 1750 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | Py_INCREF(Py_None); |
| 1753 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1756 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1757 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1758 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1759 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1760 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1761 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1762 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1763 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1764 | /* s.gettimeout() method. |
| 1765 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1766 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1767 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | if (s->sock_timeout < 0.0) { |
| 1770 | Py_INCREF(Py_None); |
| 1771 | return Py_None; |
| 1772 | } |
| 1773 | else |
| 1774 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1777 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1778 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1779 | \n\ |
| 1780 | Returns the timeout in floating seconds associated with socket \n\ |
| 1781 | 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] | 1782 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1783 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1784 | /* s.setsockopt() method. |
| 1785 | With an integer third argument, sets an integer option. |
| 1786 | With a string third argument, sets an option from a buffer; |
| 1787 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1788 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1789 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1790 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | int level; |
| 1793 | int optname; |
| 1794 | int res; |
| 1795 | char *buf; |
| 1796 | int buflen; |
| 1797 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1800 | &level, &optname, &flag)) { |
| 1801 | buf = (char *) &flag; |
| 1802 | buflen = sizeof flag; |
| 1803 | } |
| 1804 | else { |
| 1805 | PyErr_Clear(); |
| 1806 | if (!PyArg_ParseTuple(args, "iiy#:setsockopt", |
| 1807 | &level, &optname, &buf, &buflen)) |
| 1808 | return NULL; |
| 1809 | } |
| 1810 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1811 | if (res < 0) |
| 1812 | return s->errorhandler(); |
| 1813 | Py_INCREF(Py_None); |
| 1814 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1817 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1818 | "setsockopt(level, option, value)\n\ |
| 1819 | \n\ |
| 1820 | 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] | 1821 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1822 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1823 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1824 | /* s.getsockopt() method. |
| 1825 | With two arguments, retrieves an integer option. |
| 1826 | With a third integer argument, retrieves a string buffer of that size; |
| 1827 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1828 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1829 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1830 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | int level; |
| 1833 | int optname; |
| 1834 | int res; |
| 1835 | PyObject *buf; |
| 1836 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1839 | &level, &optname, &buflen)) |
| 1840 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | if (buflen == 0) { |
| 1843 | int flag = 0; |
| 1844 | socklen_t flagsize = sizeof flag; |
| 1845 | res = getsockopt(s->sock_fd, level, optname, |
| 1846 | (void *)&flag, &flagsize); |
| 1847 | if (res < 0) |
| 1848 | return s->errorhandler(); |
| 1849 | return PyLong_FromLong(flag); |
| 1850 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1851 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | /* socklen_t is unsigned so no negative test is needed, |
| 1853 | test buflen == 0 is previously done */ |
| 1854 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1855 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1857 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | PyErr_SetString(socket_error, |
| 1859 | "getsockopt buflen out of range"); |
| 1860 | return NULL; |
| 1861 | } |
| 1862 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 1863 | if (buf == NULL) |
| 1864 | return NULL; |
| 1865 | res = getsockopt(s->sock_fd, level, optname, |
| 1866 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 1867 | if (res < 0) { |
| 1868 | Py_DECREF(buf); |
| 1869 | return s->errorhandler(); |
| 1870 | } |
| 1871 | _PyBytes_Resize(&buf, buflen); |
| 1872 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1875 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1876 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1877 | \n\ |
| 1878 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1879 | 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] | 1880 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1881 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1882 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1883 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1884 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1885 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1886 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | sock_addr_t addrbuf; |
| 1889 | int addrlen; |
| 1890 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1893 | return NULL; |
| 1894 | Py_BEGIN_ALLOW_THREADS |
| 1895 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 1896 | Py_END_ALLOW_THREADS |
| 1897 | if (res < 0) |
| 1898 | return s->errorhandler(); |
| 1899 | Py_INCREF(Py_None); |
| 1900 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1903 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1904 | "bind(address)\n\ |
| 1905 | \n\ |
| 1906 | 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] | 1907 | 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] | 1908 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1909 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1910 | |
| 1911 | /* s.close() method. |
| 1912 | Set the file descriptor to -1 so operations tried subsequently |
| 1913 | will surely fail. */ |
| 1914 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1915 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1916 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1917 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | if ((fd = s->sock_fd) != -1) { |
| 1921 | s->sock_fd = -1; |
| 1922 | Py_BEGIN_ALLOW_THREADS |
| 1923 | (void) SOCKETCLOSE(fd); |
| 1924 | Py_END_ALLOW_THREADS |
| 1925 | } |
| 1926 | Py_INCREF(Py_None); |
| 1927 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1930 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1931 | "close()\n\ |
| 1932 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1933 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1934 | |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1935 | static PyObject * |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1936 | sock_detach(PySocketSockObject *s) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1937 | { |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1938 | SOCKET_T fd = s->sock_fd; |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1939 | s->sock_fd = -1; |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1940 | return PyLong_FromSocket_t(fd); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1943 | PyDoc_STRVAR(detach_doc, |
| 1944 | "detach()\n\ |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1945 | \n\ |
| 1946 | Close the socket object without closing the underlying file descriptor.\ |
| 1947 | The object cannot be used after this call, but the file descriptor\ |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1948 | can be reused for other purposes. The file descriptor is returned."); |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1949 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1950 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1951 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | timeout = 0; |
| 1957 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1958 | |
| 1959 | #ifdef MS_WINDOWS |
| 1960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | if (s->sock_timeout > 0.0) { |
| 1962 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 1963 | IS_SELECTABLE(s)) { |
| 1964 | /* This is a mess. Best solution: trust select */ |
| 1965 | fd_set fds; |
| 1966 | fd_set fds_exc; |
| 1967 | struct timeval tv; |
| 1968 | tv.tv_sec = (int)s->sock_timeout; |
| 1969 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1970 | FD_ZERO(&fds); |
| 1971 | FD_SET(s->sock_fd, &fds); |
| 1972 | FD_ZERO(&fds_exc); |
| 1973 | FD_SET(s->sock_fd, &fds_exc); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 1974 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 1975 | NULL, &fds, &fds_exc, &tv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | if (res == 0) { |
| 1977 | res = WSAEWOULDBLOCK; |
| 1978 | timeout = 1; |
| 1979 | } else if (res > 0) { |
| 1980 | if (FD_ISSET(s->sock_fd, &fds)) |
| 1981 | /* The socket is in the writable set - this |
| 1982 | means connected */ |
| 1983 | res = 0; |
| 1984 | else { |
| 1985 | /* As per MS docs, we need to call getsockopt() |
| 1986 | to get the underlying error */ |
| 1987 | int res_size = sizeof res; |
| 1988 | /* It must be in the exception set */ |
| 1989 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 1990 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 1991 | (char *)&res, &res_size)) |
| 1992 | /* getsockopt also clears WSAGetLastError, |
| 1993 | so reset it back. */ |
| 1994 | WSASetLastError(res); |
| 1995 | else |
| 1996 | res = WSAGetLastError(); |
| 1997 | } |
| 1998 | } |
| 1999 | /* else if (res < 0) an error occurred */ |
| 2000 | } |
| 2001 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | if (res < 0) |
| 2004 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2005 | |
| 2006 | #else |
| 2007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | if (s->sock_timeout > 0.0) { |
| 2009 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2010 | timeout = internal_select(s, 1); |
| 2011 | if (timeout == 0) { |
| 2012 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2013 | use getsockopt(SO_ERROR) to get the real |
| 2014 | error. */ |
| 2015 | socklen_t res_size = sizeof res; |
| 2016 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2017 | SO_ERROR, &res, &res_size); |
| 2018 | if (res == EISCONN) |
| 2019 | res = 0; |
| 2020 | errno = res; |
| 2021 | } |
| 2022 | else if (timeout == -1) { |
| 2023 | res = errno; /* had error */ |
| 2024 | } |
| 2025 | else |
| 2026 | res = EWOULDBLOCK; /* timed out */ |
| 2027 | } |
| 2028 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2030 | if (res < 0) |
| 2031 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2032 | |
| 2033 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2037 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2038 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2039 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2040 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2041 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2042 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | sock_addr_t addrbuf; |
| 2045 | int addrlen; |
| 2046 | int res; |
| 2047 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2050 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | Py_BEGIN_ALLOW_THREADS |
| 2053 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2054 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | if (timeout == 1) { |
| 2057 | PyErr_SetString(socket_timeout, "timed out"); |
| 2058 | return NULL; |
| 2059 | } |
| 2060 | if (res != 0) |
| 2061 | return s->errorhandler(); |
| 2062 | Py_INCREF(Py_None); |
| 2063 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2066 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2067 | "connect(address)\n\ |
| 2068 | \n\ |
| 2069 | 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] | 2070 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2071 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2072 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2073 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2074 | |
| 2075 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2076 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2077 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | sock_addr_t addrbuf; |
| 2079 | int addrlen; |
| 2080 | int res; |
| 2081 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2084 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | Py_BEGIN_ALLOW_THREADS |
| 2087 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2088 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2091 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2092 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | if (res == EINTR && PyErr_CheckSignals()) |
| 2094 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2095 | #endif |
| 2096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2100 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2101 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2102 | \n\ |
| 2103 | 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] | 2104 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2105 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2106 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2107 | /* s.fileno() method */ |
| 2108 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2109 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2110 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2115 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2116 | "fileno() -> integer\n\ |
| 2117 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2118 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2119 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2120 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2121 | /* s.getsockname() method */ |
| 2122 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2123 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2124 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | sock_addr_t addrbuf; |
| 2127 | int res; |
| 2128 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | if (!getsockaddrlen(s, &addrlen)) |
| 2131 | return NULL; |
| 2132 | memset(&addrbuf, 0, addrlen); |
| 2133 | Py_BEGIN_ALLOW_THREADS |
| 2134 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2135 | Py_END_ALLOW_THREADS |
| 2136 | if (res < 0) |
| 2137 | return s->errorhandler(); |
| 2138 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2139 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2142 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2143 | "getsockname() -> address info\n\ |
| 2144 | \n\ |
| 2145 | 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] | 2146 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2147 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2150 | /* s.getpeername() method */ |
| 2151 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2152 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2153 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | sock_addr_t addrbuf; |
| 2156 | int res; |
| 2157 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | if (!getsockaddrlen(s, &addrlen)) |
| 2160 | return NULL; |
| 2161 | memset(&addrbuf, 0, addrlen); |
| 2162 | Py_BEGIN_ALLOW_THREADS |
| 2163 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2164 | Py_END_ALLOW_THREADS |
| 2165 | if (res < 0) |
| 2166 | return s->errorhandler(); |
| 2167 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2168 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2169 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2171 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2172 | "getpeername() -> address info\n\ |
| 2173 | \n\ |
| 2174 | 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] | 2175 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2176 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2177 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2178 | |
| 2179 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2180 | /* s.listen(n) method */ |
| 2181 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2182 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2183 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | int backlog; |
| 2186 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2188 | backlog = PyLong_AsLong(arg); |
| 2189 | if (backlog == -1 && PyErr_Occurred()) |
| 2190 | return NULL; |
| 2191 | Py_BEGIN_ALLOW_THREADS |
| 2192 | if (backlog < 1) |
| 2193 | backlog = 1; |
| 2194 | res = listen(s->sock_fd, backlog); |
| 2195 | Py_END_ALLOW_THREADS |
| 2196 | if (res < 0) |
| 2197 | return s->errorhandler(); |
| 2198 | Py_INCREF(Py_None); |
| 2199 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2202 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2203 | "listen(backlog)\n\ |
| 2204 | \n\ |
| 2205 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 2206 | least 1; it specifies the number of unaccepted connection that the system\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2207 | will allow before refusing new connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2208 | |
| 2209 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2210 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2211 | * 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] | 2212 | * 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] | 2213 | * the buffer, do it in the caller. This function returns the number of bytes |
| 2214 | * succesfully read. If there was an error, it returns -1. Note that it is |
| 2215 | * also possible that we return a number of bytes smaller than the request |
| 2216 | * bytes. |
| 2217 | */ |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2218 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2219 | static Py_ssize_t |
| 2220 | 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] | 2221 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2222 | Py_ssize_t outlen = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | int timeout; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2224 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | int remaining; |
| 2226 | char *read_buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2227 | #endif |
| 2228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | if (!IS_SELECTABLE(s)) { |
| 2230 | select_error(); |
| 2231 | return -1; |
| 2232 | } |
| 2233 | if (len == 0) { |
| 2234 | /* If 0 bytes were requested, do nothing. */ |
| 2235 | return 0; |
| 2236 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2237 | |
| 2238 | #ifndef __VMS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2239 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2241 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | if (!timeout) |
| 2243 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2244 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | if (timeout == 1) { |
| 2247 | PyErr_SetString(socket_timeout, "timed out"); |
| 2248 | return -1; |
| 2249 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2250 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | if (outlen < 0) { |
| 2252 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2253 | NULL, so ignore its return value */ |
| 2254 | s->errorhandler(); |
| 2255 | return -1; |
| 2256 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2257 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | read_buf = cbuf; |
| 2259 | remaining = len; |
| 2260 | while (remaining != 0) { |
| 2261 | unsigned int segment; |
| 2262 | int nread = -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | segment = remaining /SEGMENT_SIZE; |
| 2265 | if (segment != 0) { |
| 2266 | segment = SEGMENT_SIZE; |
| 2267 | } |
| 2268 | else { |
| 2269 | segment = remaining; |
| 2270 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2271 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2272 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2273 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2274 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | if (!timeout) |
| 2276 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2277 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | if (timeout == 1) { |
| 2279 | PyErr_SetString(socket_timeout, "timed out"); |
| 2280 | return -1; |
| 2281 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2282 | END_SELECT_LOOP(s) |
| 2283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | if (nread < 0) { |
| 2285 | s->errorhandler(); |
| 2286 | return -1; |
| 2287 | } |
| 2288 | if (nread != remaining) { |
| 2289 | read_buf += nread; |
| 2290 | break; |
| 2291 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | remaining -= segment; |
| 2294 | read_buf += segment; |
| 2295 | } |
| 2296 | outlen = read_buf - cbuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2297 | #endif /* !__VMS */ |
| 2298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2302 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2303 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2304 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2305 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2306 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2307 | { |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2308 | Py_ssize_t recvlen, outlen; |
| 2309 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2311 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2312 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | if (recvlen < 0) { |
| 2316 | PyErr_SetString(PyExc_ValueError, |
| 2317 | "negative buffersize in recv"); |
| 2318 | return NULL; |
| 2319 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | /* Allocate a new string. */ |
| 2322 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2323 | if (buf == NULL) |
| 2324 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | /* Call the guts */ |
| 2327 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2328 | if (outlen < 0) { |
| 2329 | /* An error occurred, release the string and return an |
| 2330 | error. */ |
| 2331 | Py_DECREF(buf); |
| 2332 | return NULL; |
| 2333 | } |
| 2334 | if (outlen != recvlen) { |
| 2335 | /* We did not read as many bytes as we anticipated, resize the |
| 2336 | string if possible and be successful. */ |
| 2337 | _PyBytes_Resize(&buf, outlen); |
| 2338 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2343 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2344 | "recv(buffersize[, flags]) -> data\n\ |
| 2345 | \n\ |
| 2346 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2347 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2348 | 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] | 2349 | 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] | 2350 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2351 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2352 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2353 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2354 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2355 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2358 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2359 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | Py_buffer pbuf; |
| 2361 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2362 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2364 | /* Get the buffer's memory */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2365 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | &pbuf, &recvlen, &flags)) |
| 2367 | return NULL; |
| 2368 | buf = pbuf.buf; |
| 2369 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | if (recvlen < 0) { |
| 2372 | PyBuffer_Release(&pbuf); |
| 2373 | PyErr_SetString(PyExc_ValueError, |
| 2374 | "negative buffersize in recv_into"); |
| 2375 | return NULL; |
| 2376 | } |
| 2377 | if (recvlen == 0) { |
| 2378 | /* If nbytes was not specified, use the buffer's length */ |
| 2379 | recvlen = buflen; |
| 2380 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | /* Check if the buffer is large enough */ |
| 2383 | if (buflen < recvlen) { |
| 2384 | PyBuffer_Release(&pbuf); |
| 2385 | PyErr_SetString(PyExc_ValueError, |
| 2386 | "buffer too small for requested bytes"); |
| 2387 | return NULL; |
| 2388 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | /* Call the guts */ |
| 2391 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2392 | if (readlen < 0) { |
| 2393 | /* Return an error. */ |
| 2394 | PyBuffer_Release(&pbuf); |
| 2395 | return NULL; |
| 2396 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | PyBuffer_Release(&pbuf); |
| 2399 | /* Return the number of bytes read. Note that we do not do anything |
| 2400 | special here in the case that readlen < recvlen. */ |
| 2401 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2404 | PyDoc_STRVAR(recv_into_doc, |
| 2405 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2406 | \n\ |
| 2407 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2408 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2409 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2410 | \n\ |
| 2411 | See recv() for documentation about the flags."); |
| 2412 | |
| 2413 | |
| 2414 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2415 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2416 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2417 | * contain the buffer, do it in the caller. This function returns the number |
| 2418 | * of bytes succesfully read. If there was an error, it returns -1. Note |
| 2419 | * that it is also possible that we return a number of bytes smaller than the |
| 2420 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2421 | * |
| 2422 | * 'addr' is a return value for the address object. Note that you must decref |
| 2423 | * it yourself. |
| 2424 | */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2425 | static Py_ssize_t |
| 2426 | 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] | 2427 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 | sock_addr_t addrbuf; |
| 2430 | int timeout; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2431 | Py_ssize_t n = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2432 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2434 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | if (!getsockaddrlen(s, &addrlen)) |
| 2437 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | if (!IS_SELECTABLE(s)) { |
| 2440 | select_error(); |
| 2441 | return -1; |
| 2442 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2443 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2444 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | Py_BEGIN_ALLOW_THREADS |
| 2446 | memset(&addrbuf, 0, addrlen); |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2447 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2448 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2449 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2450 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2452 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2453 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2454 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2455 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2456 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2457 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2459 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2460 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | } |
| 2462 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | if (timeout == 1) { |
| 2465 | PyErr_SetString(socket_timeout, "timed out"); |
| 2466 | return -1; |
| 2467 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2468 | END_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | if (n < 0) { |
| 2470 | s->errorhandler(); |
| 2471 | return -1; |
| 2472 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2474 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2475 | addrlen, s->sock_proto))) |
| 2476 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2482 | |
| 2483 | static PyObject * |
| 2484 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2486 | PyObject *buf = NULL; |
| 2487 | PyObject *addr = NULL; |
| 2488 | PyObject *ret = NULL; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2489 | int flags = 0; |
| 2490 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2491 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2492 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2495 | if (recvlen < 0) { |
| 2496 | PyErr_SetString(PyExc_ValueError, |
| 2497 | "negative buffersize in recvfrom"); |
| 2498 | return NULL; |
| 2499 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2501 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2502 | if (buf == NULL) |
| 2503 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2505 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2506 | recvlen, flags, &addr); |
| 2507 | if (outlen < 0) { |
| 2508 | goto finally; |
| 2509 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | if (outlen != recvlen) { |
| 2512 | /* We did not read as many bytes as we anticipated, resize the |
| 2513 | string if possible and be succesful. */ |
| 2514 | if (_PyBytes_Resize(&buf, outlen) < 0) |
| 2515 | /* Oopsy, not so succesful after all. */ |
| 2516 | goto finally; |
| 2517 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2520 | |
| 2521 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | Py_XDECREF(buf); |
| 2523 | Py_XDECREF(addr); |
| 2524 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2527 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2528 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2529 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2530 | 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] | 2531 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2532 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2533 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2534 | |
| 2535 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2536 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2540 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | Py_buffer pbuf; |
| 2542 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2543 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2546 | |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2547 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 | kwlist, &pbuf, |
| 2549 | &recvlen, &flags)) |
| 2550 | return NULL; |
| 2551 | buf = pbuf.buf; |
| 2552 | buflen = pbuf.len; |
| 2553 | assert(buf != 0 && buflen > 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | if (recvlen < 0) { |
| 2556 | PyBuffer_Release(&pbuf); |
| 2557 | PyErr_SetString(PyExc_ValueError, |
| 2558 | "negative buffersize in recvfrom_into"); |
| 2559 | return NULL; |
| 2560 | } |
| 2561 | if (recvlen == 0) { |
| 2562 | /* If nbytes was not specified, use the buffer's length */ |
| 2563 | recvlen = buflen; |
| 2564 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2567 | if (readlen < 0) { |
| 2568 | PyBuffer_Release(&pbuf); |
| 2569 | /* Return an error */ |
| 2570 | Py_XDECREF(addr); |
| 2571 | return NULL; |
| 2572 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | PyBuffer_Release(&pbuf); |
| 2575 | /* Return the number of bytes read and the address. Note that we do |
| 2576 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2577 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2580 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2581 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2582 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2583 | 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] | 2584 | |
| 2585 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2586 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2587 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2588 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2589 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2592 | Py_ssize_t len, n = -1; |
| 2593 | int flags = 0, timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 2597 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2599 | if (!IS_SELECTABLE(s)) { |
| 2600 | PyBuffer_Release(&pbuf); |
| 2601 | return select_error(); |
| 2602 | } |
| 2603 | buf = pbuf.buf; |
| 2604 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2605 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2606 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2608 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2609 | if (!timeout) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2610 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2611 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2612 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2614 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2615 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2617 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | PyErr_SetString(socket_timeout, "timed out"); |
| 2619 | return NULL; |
| 2620 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2621 | END_SELECT_LOOP(s) |
| 2622 | |
| 2623 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | if (n < 0) |
| 2625 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2626 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2629 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2630 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2631 | \n\ |
| 2632 | 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] | 2633 | 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] | 2634 | 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] | 2635 | |
| 2636 | |
| 2637 | /* s.sendall(data [,flags]) method */ |
| 2638 | |
| 2639 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2640 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2641 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | char *buf; |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2643 | Py_ssize_t len, n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2644 | int flags = 0, timeout, saved_errno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 2648 | return NULL; |
| 2649 | buf = pbuf.buf; |
| 2650 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | if (!IS_SELECTABLE(s)) { |
| 2653 | PyBuffer_Release(&pbuf); |
| 2654 | return select_error(); |
| 2655 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | do { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2658 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | timeout = internal_select(s, 1); |
| 2660 | n = -1; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2661 | if (!timeout) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2662 | #ifdef __VMS |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2663 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2664 | #else |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2665 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2666 | #endif |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2667 | } |
| 2668 | Py_END_ALLOW_THREADS |
| 2669 | if (timeout == 1) { |
| 2670 | PyBuffer_Release(&pbuf); |
| 2671 | PyErr_SetString(socket_timeout, "timed out"); |
| 2672 | return NULL; |
| 2673 | } |
| 2674 | /* PyErr_CheckSignals() might change errno */ |
| 2675 | saved_errno = errno; |
| 2676 | /* We must run our signal handlers before looping again. |
| 2677 | send() can return a successful partial write when it is |
| 2678 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 2679 | if (PyErr_CheckSignals()) { |
| 2680 | PyBuffer_Release(&pbuf); |
| 2681 | return NULL; |
| 2682 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | if (n < 0) { |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2684 | /* If interrupted, try again */ |
| 2685 | if (saved_errno == EINTR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | continue; |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 2687 | else |
| 2688 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | } |
| 2690 | buf += n; |
| 2691 | len -= n; |
| 2692 | } while (len > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | if (n < 0) |
| 2696 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | Py_INCREF(Py_None); |
| 2699 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2702 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2703 | "sendall(data[, flags])\n\ |
| 2704 | \n\ |
| 2705 | Send a data string to the socket. For the optional flags\n\ |
| 2706 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2707 | 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] | 2708 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2709 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2710 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2711 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2712 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2713 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2714 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | Py_buffer pbuf; |
| 2717 | PyObject *addro; |
| 2718 | char *buf; |
| 2719 | Py_ssize_t len; |
| 2720 | sock_addr_t addrbuf; |
| 2721 | int addrlen, n = -1, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | flags = 0; |
| 2724 | if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) { |
| 2725 | PyErr_Clear(); |
| 2726 | if (!PyArg_ParseTuple(args, "y*iO:sendto", |
| 2727 | &pbuf, &flags, &addro)) |
| 2728 | return NULL; |
| 2729 | } |
| 2730 | buf = pbuf.buf; |
| 2731 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | if (!IS_SELECTABLE(s)) { |
| 2734 | PyBuffer_Release(&pbuf); |
| 2735 | return select_error(); |
| 2736 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2739 | PyBuffer_Release(&pbuf); |
| 2740 | return NULL; |
| 2741 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2743 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2745 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | if (!timeout) |
| 2747 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2748 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | if (timeout == 1) { |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2751 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | PyErr_SetString(socket_timeout, "timed out"); |
| 2753 | return NULL; |
| 2754 | } |
Antoine Pitrou | 3e1fd27 | 2010-09-28 21:23:11 +0000 | [diff] [blame] | 2755 | END_SELECT_LOOP(s) |
| 2756 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | if (n < 0) |
| 2758 | return s->errorhandler(); |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 2759 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2762 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2763 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2764 | \n\ |
| 2765 | 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] | 2766 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2767 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2768 | |
| 2769 | /* s.shutdown(how) method */ |
| 2770 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2771 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2772 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | int how; |
| 2775 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | how = PyLong_AsLong(arg); |
| 2778 | if (how == -1 && PyErr_Occurred()) |
| 2779 | return NULL; |
| 2780 | Py_BEGIN_ALLOW_THREADS |
| 2781 | res = shutdown(s->sock_fd, how); |
| 2782 | Py_END_ALLOW_THREADS |
| 2783 | if (res < 0) |
| 2784 | return s->errorhandler(); |
| 2785 | Py_INCREF(Py_None); |
| 2786 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2789 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2790 | "shutdown(flag)\n\ |
| 2791 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2792 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2793 | 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] | 2794 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 2795 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2796 | static PyObject* |
| 2797 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 2798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | unsigned long cmd = SIO_RCVALL; |
| 2800 | PyObject *argO; |
| 2801 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 2804 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | switch (cmd) { |
| 2807 | case SIO_RCVALL: { |
| 2808 | unsigned int option = RCVALL_ON; |
| 2809 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 2810 | return NULL; |
| 2811 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 2812 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 2813 | return set_error(); |
| 2814 | } |
| 2815 | return PyLong_FromUnsignedLong(recv); } |
| 2816 | case SIO_KEEPALIVE_VALS: { |
| 2817 | struct tcp_keepalive ka; |
| 2818 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 2819 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 2820 | return NULL; |
| 2821 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 2822 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 2823 | return set_error(); |
| 2824 | } |
| 2825 | return PyLong_FromUnsignedLong(recv); } |
| 2826 | default: |
| 2827 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 2828 | return NULL; |
| 2829 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2830 | } |
| 2831 | PyDoc_STRVAR(sock_ioctl_doc, |
| 2832 | "ioctl(cmd, option) -> long\n\ |
| 2833 | \n\ |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 2834 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 2835 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 2836 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."); |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2837 | |
| 2838 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2839 | |
| 2840 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2841 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2842 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 2844 | accept_doc}, |
| 2845 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 2846 | bind_doc}, |
| 2847 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 2848 | close_doc}, |
| 2849 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 2850 | connect_doc}, |
| 2851 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 2852 | connect_ex_doc}, |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 2853 | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
| 2854 | detach_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 2856 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2857 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | {"getpeername", (PyCFunction)sock_getpeername, |
| 2859 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 2860 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | {"getsockname", (PyCFunction)sock_getsockname, |
| 2862 | METH_NOARGS, getsockname_doc}, |
| 2863 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 2864 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 2865 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 2867 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2868 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 2870 | listen_doc}, |
| 2871 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 2872 | recv_doc}, |
| 2873 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 2874 | recv_into_doc}, |
| 2875 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 2876 | recvfrom_doc}, |
| 2877 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 2878 | recvfrom_into_doc}, |
| 2879 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 2880 | send_doc}, |
| 2881 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 2882 | sendall_doc}, |
| 2883 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 2884 | sendto_doc}, |
| 2885 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 2886 | setblocking_doc}, |
| 2887 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 2888 | settimeout_doc}, |
| 2889 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 2890 | gettimeout_doc}, |
| 2891 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 2892 | setsockopt_doc}, |
| 2893 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 2894 | shutdown_doc}, |
| 2895 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2896 | }; |
| 2897 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2898 | /* SockObject members */ |
| 2899 | static PyMemberDef sock_memberlist[] = { |
| 2900 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 2901 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 2902 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 2903 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 2904 | {0}, |
| 2905 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2906 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2907 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2908 | First close the file description. */ |
| 2909 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2910 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2911 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2912 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | if (s->sock_fd != -1) |
| 2914 | (void) SOCKETCLOSE(s->sock_fd); |
| 2915 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2918 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2919 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2920 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2921 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2922 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | if (s->sock_fd > LONG_MAX) { |
| 2924 | /* this can occur on Win64, and actually there is a special |
| 2925 | ugly printf formatter for decimal pointer length integer |
| 2926 | printing, only bother if necessary*/ |
| 2927 | PyErr_SetString(PyExc_OverflowError, |
| 2928 | "no printf formatter to display " |
| 2929 | "the socket descriptor in decimal"); |
| 2930 | return NULL; |
| 2931 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2932 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2933 | return PyUnicode_FromFormat( |
| 2934 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
| 2935 | (long)s->sock_fd, s->sock_family, |
| 2936 | s->sock_type, |
| 2937 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2941 | /* Create a new, uninitialized socket object. */ |
| 2942 | |
| 2943 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2944 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2948 | new = type->tp_alloc(type, 0); |
| 2949 | if (new != NULL) { |
| 2950 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 2951 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 2952 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 2953 | } |
| 2954 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | |
| 2958 | /* Initialize a new socket object. */ |
| 2959 | |
| 2960 | /*ARGSUSED*/ |
| 2961 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2962 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2964 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 2965 | PyObject *fdobj = NULL; |
| 2966 | SOCKET_T fd = INVALID_SOCKET; |
| 2967 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 2968 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2971 | "|iiiO:socket", keywords, |
| 2972 | &family, &type, &proto, &fdobj)) |
| 2973 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | if (fdobj != NULL && fdobj != Py_None) { |
| 2976 | fd = PyLong_AsSocket_t(fdobj); |
| 2977 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 2978 | return -1; |
| 2979 | if (fd == INVALID_SOCKET) { |
| 2980 | PyErr_SetString(PyExc_ValueError, |
| 2981 | "can't use invalid socket value"); |
| 2982 | return -1; |
| 2983 | } |
| 2984 | } |
| 2985 | else { |
| 2986 | Py_BEGIN_ALLOW_THREADS |
| 2987 | fd = socket(family, type, proto); |
| 2988 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | if (fd == INVALID_SOCKET) { |
| 2991 | set_error(); |
| 2992 | return -1; |
| 2993 | } |
| 2994 | } |
| 2995 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2998 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3002 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3003 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3004 | static PyTypeObject sock_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 3006 | "_socket.socket", /* tp_name */ |
| 3007 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 3008 | 0, /* tp_itemsize */ |
| 3009 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 3010 | 0, /* tp_print */ |
| 3011 | 0, /* tp_getattr */ |
| 3012 | 0, /* tp_setattr */ |
| 3013 | 0, /* tp_reserved */ |
| 3014 | (reprfunc)sock_repr, /* tp_repr */ |
| 3015 | 0, /* tp_as_number */ |
| 3016 | 0, /* tp_as_sequence */ |
| 3017 | 0, /* tp_as_mapping */ |
| 3018 | 0, /* tp_hash */ |
| 3019 | 0, /* tp_call */ |
| 3020 | 0, /* tp_str */ |
| 3021 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3022 | 0, /* tp_setattro */ |
| 3023 | 0, /* tp_as_buffer */ |
| 3024 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3025 | sock_doc, /* tp_doc */ |
| 3026 | 0, /* tp_traverse */ |
| 3027 | 0, /* tp_clear */ |
| 3028 | 0, /* tp_richcompare */ |
| 3029 | 0, /* tp_weaklistoffset */ |
| 3030 | 0, /* tp_iter */ |
| 3031 | 0, /* tp_iternext */ |
| 3032 | sock_methods, /* tp_methods */ |
| 3033 | sock_memberlist, /* tp_members */ |
| 3034 | 0, /* tp_getset */ |
| 3035 | 0, /* tp_base */ |
| 3036 | 0, /* tp_dict */ |
| 3037 | 0, /* tp_descr_get */ |
| 3038 | 0, /* tp_descr_set */ |
| 3039 | 0, /* tp_dictoffset */ |
| 3040 | sock_initobj, /* tp_init */ |
| 3041 | PyType_GenericAlloc, /* tp_alloc */ |
| 3042 | sock_new, /* tp_new */ |
| 3043 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3044 | }; |
| 3045 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3046 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3047 | /* Python interface to gethostname(). */ |
| 3048 | |
| 3049 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3050 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 3051 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3052 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3053 | char buf[1024]; |
| 3054 | int res; |
| 3055 | Py_BEGIN_ALLOW_THREADS |
| 3056 | res = gethostname(buf, (int) sizeof buf - 1); |
| 3057 | Py_END_ALLOW_THREADS |
| 3058 | if (res < 0) |
| 3059 | return set_error(); |
| 3060 | buf[sizeof buf - 1] = '\0'; |
| 3061 | return PyUnicode_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3062 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3063 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3064 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3065 | "gethostname() -> string\n\ |
| 3066 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3067 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3068 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3069 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3070 | /* Python interface to gethostbyname(name). */ |
| 3071 | |
| 3072 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3073 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3074 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3075 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | char *name; |
| 3077 | sock_addr_t addrbuf; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3078 | PyObject *ret = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3079 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3080 | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | return NULL; |
| 3082 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3083 | goto finally; |
| 3084 | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 3085 | finally: |
| 3086 | PyMem_Free(name); |
| 3087 | return ret; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3090 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3091 | "gethostbyname(host) -> address\n\ |
| 3092 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3093 | 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] | 3094 | |
| 3095 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3096 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 3097 | |
| 3098 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3099 | 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] | 3100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | char **pch; |
| 3102 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 3103 | PyObject *name_list = (PyObject *)NULL; |
| 3104 | PyObject *addr_list = (PyObject *)NULL; |
| 3105 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | if (h == NULL) { |
| 3108 | /* Let's get real error message to return */ |
| 3109 | set_herror(h_errno); |
| 3110 | return NULL; |
| 3111 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | if (h->h_addrtype != af) { |
| 3114 | /* Let's get real error message to return */ |
| 3115 | PyErr_SetString(socket_error, |
| 3116 | (char *)strerror(EAFNOSUPPORT)); |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 3117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | return NULL; |
| 3119 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3123 | case AF_INET: |
| 3124 | if (alen < sizeof(struct sockaddr_in)) |
| 3125 | return NULL; |
| 3126 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3127 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3128 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | case AF_INET6: |
| 3130 | if (alen < sizeof(struct sockaddr_in6)) |
| 3131 | return NULL; |
| 3132 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3133 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | if ((name_list = PyList_New(0)) == NULL) |
| 3138 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | if ((addr_list = PyList_New(0)) == NULL) |
| 3141 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | /* SF #1511317: h_aliases can be NULL */ |
| 3144 | if (h->h_aliases) { |
| 3145 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3146 | int status; |
| 3147 | tmp = PyUnicode_FromString(*pch); |
| 3148 | if (tmp == NULL) |
| 3149 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | status = PyList_Append(name_list, tmp); |
| 3152 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | if (status) |
| 3155 | goto err; |
| 3156 | } |
| 3157 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3159 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3160 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | case AF_INET: |
| 3165 | { |
| 3166 | struct sockaddr_in sin; |
| 3167 | memset(&sin, 0, sizeof(sin)); |
| 3168 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3169 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3171 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3173 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3176 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3177 | break; |
| 3178 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3179 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3180 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | case AF_INET6: |
| 3182 | { |
| 3183 | struct sockaddr_in6 sin6; |
| 3184 | memset(&sin6, 0, sizeof(sin6)); |
| 3185 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3186 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3188 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3190 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3191 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3194 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3195 | break; |
| 3196 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3197 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | default: /* can't happen */ |
| 3200 | PyErr_SetString(socket_error, |
| 3201 | "unsupported address family"); |
| 3202 | return NULL; |
| 3203 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | if (tmp == NULL) |
| 3206 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | status = PyList_Append(addr_list, tmp); |
| 3209 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | if (status) |
| 3212 | goto err; |
| 3213 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3215 | 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] | 3216 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3217 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 | Py_XDECREF(name_list); |
| 3219 | Py_XDECREF(addr_list); |
| 3220 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | |
| 3224 | /* Python interface to gethostbyname_ex(name). */ |
| 3225 | |
| 3226 | /*ARGSUSED*/ |
| 3227 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3228 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | char *name; |
| 3231 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3232 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | struct sockaddr_storage addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3234 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | struct sockaddr_in addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3236 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | struct sockaddr *sa; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3238 | PyObject *ret = NULL; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3239 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3241 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3242 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3243 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | char buf[16384]; |
| 3245 | int buf_len = (sizeof buf) - 1; |
| 3246 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3247 | #endif |
| 3248 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3250 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3251 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3252 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3253 | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | return NULL; |
| 3255 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3256 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3258 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3259 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3260 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3261 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3262 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3263 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3264 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | memset((void *) &data, '\0', sizeof(data)); |
| 3266 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3267 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3268 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3269 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3270 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3272 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3273 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3274 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | Py_END_ALLOW_THREADS |
| 3276 | /* Some C libraries would require addr.__ss_family instead of |
| 3277 | addr.ss_family. |
| 3278 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3279 | access sa_family. */ |
| 3280 | sa = (struct sockaddr*)&addr; |
| 3281 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3282 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3283 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3284 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3285 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3286 | finally: |
| 3287 | PyMem_Free(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3291 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3292 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3293 | \n\ |
| 3294 | 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] | 3295 | 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] | 3296 | |
| 3297 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3298 | /* Python interface to gethostbyaddr(IP). */ |
| 3299 | |
| 3300 | /*ARGSUSED*/ |
| 3301 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3302 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3303 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3304 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3306 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3307 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3308 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3310 | char *ip_num; |
| 3311 | struct hostent *h; |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3312 | PyObject *ret = NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3313 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3315 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3317 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3318 | /* glibcs up to 2.10 assume that the buf argument to |
| 3319 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3320 | does not ensure. The attribute below instructs the compiler |
| 3321 | to maintain this alignment. */ |
| 3322 | char buf[16384] Py_ALIGNED(8); |
| 3323 | int buf_len = (sizeof buf) - 1; |
| 3324 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3325 | #endif |
| 3326 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3327 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3328 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3329 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | char *ap; |
| 3331 | int al; |
| 3332 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3333 | |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3334 | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3335 | return NULL; |
| 3336 | af = AF_UNSPEC; |
| 3337 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3338 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3339 | af = sa->sa_family; |
| 3340 | ap = NULL; |
| 3341 | al = 0; |
| 3342 | switch (af) { |
| 3343 | case AF_INET: |
| 3344 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3345 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3346 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3347 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | case AF_INET6: |
| 3349 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3350 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3351 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3352 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3353 | default: |
| 3354 | PyErr_SetString(socket_error, "unsupported address family"); |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3355 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | } |
| 3357 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3358 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3359 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | result = gethostbyaddr_r(ap, al, af, |
| 3361 | &hp_allocated, buf, buf_len, |
| 3362 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3363 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3364 | h = gethostbyaddr_r(ap, al, af, |
| 3365 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3366 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | memset((void *) &data, '\0', sizeof(data)); |
| 3368 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3369 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3370 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3371 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3372 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3374 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3376 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3377 | Py_END_ALLOW_THREADS |
| 3378 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3379 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3380 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3381 | #endif |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 3382 | finally: |
| 3383 | PyMem_Free(ip_num); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3384 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3387 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3388 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3389 | \n\ |
| 3390 | 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] | 3391 | 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] | 3392 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3393 | |
| 3394 | /* Python interface to getservbyname(name). |
| 3395 | This only returns the port number, since the other info is already |
| 3396 | known or not useful (like the list of aliases). */ |
| 3397 | |
| 3398 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3399 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3400 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | char *name, *proto=NULL; |
| 3403 | struct servent *sp; |
| 3404 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3405 | return NULL; |
| 3406 | Py_BEGIN_ALLOW_THREADS |
| 3407 | sp = getservbyname(name, proto); |
| 3408 | Py_END_ALLOW_THREADS |
| 3409 | if (sp == NULL) { |
| 3410 | PyErr_SetString(socket_error, "service/proto not found"); |
| 3411 | return NULL; |
| 3412 | } |
| 3413 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3416 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3417 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3418 | \n\ |
| 3419 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3420 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3421 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3422 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3423 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3424 | /* Python interface to getservbyport(port). |
| 3425 | This only returns the service name, since the other info is already |
| 3426 | known or not useful (like the list of aliases). */ |
| 3427 | |
| 3428 | /*ARGSUSED*/ |
| 3429 | static PyObject * |
| 3430 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | int port; |
| 3433 | char *proto=NULL; |
| 3434 | struct servent *sp; |
| 3435 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3436 | return NULL; |
| 3437 | if (port < 0 || port > 0xffff) { |
| 3438 | PyErr_SetString( |
| 3439 | PyExc_OverflowError, |
| 3440 | "getservbyport: port must be 0-65535."); |
| 3441 | return NULL; |
| 3442 | } |
| 3443 | Py_BEGIN_ALLOW_THREADS |
| 3444 | sp = getservbyport(htons((short)port), proto); |
| 3445 | Py_END_ALLOW_THREADS |
| 3446 | if (sp == NULL) { |
| 3447 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3448 | return NULL; |
| 3449 | } |
| 3450 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | PyDoc_STRVAR(getservbyport_doc, |
| 3454 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3455 | \n\ |
| 3456 | Return the service name from a port number and protocol name.\n\ |
| 3457 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3458 | otherwise any protocol will match."); |
| 3459 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3460 | /* Python interface to getprotobyname(name). |
| 3461 | This only returns the protocol number, since the other info is |
| 3462 | already known or not useful (like the list of aliases). */ |
| 3463 | |
| 3464 | /*ARGSUSED*/ |
| 3465 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3466 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | char *name; |
| 3469 | struct protoent *sp; |
| 3470 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3471 | return NULL; |
| 3472 | Py_BEGIN_ALLOW_THREADS |
| 3473 | sp = getprotobyname(name); |
| 3474 | Py_END_ALLOW_THREADS |
| 3475 | if (sp == NULL) { |
| 3476 | PyErr_SetString(socket_error, "protocol not found"); |
| 3477 | return NULL; |
| 3478 | } |
| 3479 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3482 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3483 | "getprotobyname(name) -> integer\n\ |
| 3484 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3485 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3486 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3487 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3488 | #ifndef NO_DUP |
| 3489 | /* dup() function for socket fds */ |
| 3490 | |
| 3491 | static PyObject * |
| 3492 | socket_dup(PyObject *self, PyObject *fdobj) |
| 3493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3494 | SOCKET_T fd, newfd; |
| 3495 | PyObject *newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3496 | |
| 3497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | fd = PyLong_AsSocket_t(fdobj); |
| 3499 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 3500 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3502 | newfd = dup_socket(fd); |
| 3503 | if (newfd == INVALID_SOCKET) |
| 3504 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | newfdobj = PyLong_FromSocket_t(newfd); |
| 3507 | if (newfdobj == NULL) |
| 3508 | SOCKETCLOSE(newfd); |
| 3509 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | PyDoc_STRVAR(dup_doc, |
| 3513 | "dup(integer) -> integer\n\ |
| 3514 | \n\ |
| 3515 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 3516 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 3517 | #endif |
| 3518 | |
| 3519 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3520 | #ifdef HAVE_SOCKETPAIR |
| 3521 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3522 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3523 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3524 | |
| 3525 | /*ARGSUSED*/ |
| 3526 | static PyObject * |
| 3527 | socket_socketpair(PyObject *self, PyObject *args) |
| 3528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3529 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3530 | SOCKET_T sv[2]; |
| 3531 | int family, type = SOCK_STREAM, proto = 0; |
| 3532 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3533 | |
| 3534 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3535 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3536 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3538 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3539 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3540 | &family, &type, &proto)) |
| 3541 | return NULL; |
| 3542 | /* Create a pair of socket fds */ |
| 3543 | if (socketpair(family, type, proto, sv) < 0) |
| 3544 | return set_error(); |
| 3545 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3546 | if (s0 == NULL) |
| 3547 | goto finally; |
| 3548 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3549 | if (s1 == NULL) |
| 3550 | goto finally; |
| 3551 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3552 | |
| 3553 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | if (res == NULL) { |
| 3555 | if (s0 == NULL) |
| 3556 | SOCKETCLOSE(sv[0]); |
| 3557 | if (s1 == NULL) |
| 3558 | SOCKETCLOSE(sv[1]); |
| 3559 | } |
| 3560 | Py_XDECREF(s0); |
| 3561 | Py_XDECREF(s1); |
| 3562 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | PyDoc_STRVAR(socketpair_doc, |
| 3566 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3567 | \n\ |
| 3568 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3569 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3570 | 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] | 3571 | 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] | 3572 | |
| 3573 | #endif /* HAVE_SOCKETPAIR */ |
| 3574 | |
| 3575 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3576 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3577 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3578 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3579 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3582 | return NULL; |
| 3583 | } |
| 3584 | if (x1 < 0) { |
| 3585 | PyErr_SetString(PyExc_OverflowError, |
| 3586 | "can't convert negative number to unsigned long"); |
| 3587 | return NULL; |
| 3588 | } |
| 3589 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3590 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3593 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3594 | "ntohs(integer) -> integer\n\ |
| 3595 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3596 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3597 | |
| 3598 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3599 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3600 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3604 | if (PyLong_Check(arg)) { |
| 3605 | x = PyLong_AsUnsignedLong(arg); |
| 3606 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3607 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3608 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | { |
| 3610 | unsigned long y; |
| 3611 | /* only want the trailing 32 bits */ |
| 3612 | y = x & 0xFFFFFFFFUL; |
| 3613 | if (y ^ x) |
| 3614 | return PyErr_Format(PyExc_OverflowError, |
| 3615 | "long int larger than 32 bits"); |
| 3616 | x = y; |
| 3617 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3618 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3619 | } |
| 3620 | else |
| 3621 | return PyErr_Format(PyExc_TypeError, |
| 3622 | "expected int/long, %s found", |
| 3623 | Py_TYPE(arg)->tp_name); |
| 3624 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3625 | return NULL; |
| 3626 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3629 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3630 | "ntohl(integer) -> integer\n\ |
| 3631 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3632 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3633 | |
| 3634 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3635 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3636 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3641 | return NULL; |
| 3642 | } |
| 3643 | if (x1 < 0) { |
| 3644 | PyErr_SetString(PyExc_OverflowError, |
| 3645 | "can't convert negative number to unsigned long"); |
| 3646 | return NULL; |
| 3647 | } |
| 3648 | x2 = (unsigned int)htons((unsigned short)x1); |
| 3649 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3652 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3653 | "htons(integer) -> integer\n\ |
| 3654 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3655 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3656 | |
| 3657 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3658 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3659 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3661 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3663 | if (PyLong_Check(arg)) { |
| 3664 | x = PyLong_AsUnsignedLong(arg); |
| 3665 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3666 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3667 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | { |
| 3669 | unsigned long y; |
| 3670 | /* only want the trailing 32 bits */ |
| 3671 | y = x & 0xFFFFFFFFUL; |
| 3672 | if (y ^ x) |
| 3673 | return PyErr_Format(PyExc_OverflowError, |
| 3674 | "long int larger than 32 bits"); |
| 3675 | x = y; |
| 3676 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3677 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 | } |
| 3679 | else |
| 3680 | return PyErr_Format(PyExc_TypeError, |
| 3681 | "expected int/long, %s found", |
| 3682 | Py_TYPE(arg)->tp_name); |
| 3683 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3686 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3687 | "htonl(integer) -> integer\n\ |
| 3688 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3689 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3690 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3691 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3693 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 3694 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3695 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3696 | 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] | 3697 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3698 | |
| 3699 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3700 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3701 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3702 | #ifndef INADDR_NONE |
| 3703 | #define INADDR_NONE (-1) |
| 3704 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3705 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3706 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3707 | #endif |
| 3708 | |
| 3709 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 3710 | #if (SIZEOF_INT != 4) |
| 3711 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3712 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3713 | /* Have to use inet_addr() instead */ |
| 3714 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3715 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3716 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3719 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3720 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3721 | |
| 3722 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3723 | |
| 3724 | #ifdef USE_INET_ATON_WEAKLINK |
| 3725 | if (inet_aton != NULL) { |
| 3726 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3727 | if (inet_aton(ip_addr, &buf)) |
| 3728 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 3729 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3731 | PyErr_SetString(socket_error, |
| 3732 | "illegal IP address string passed to inet_aton"); |
| 3733 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3734 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3735 | #ifdef USE_INET_ATON_WEAKLINK |
| 3736 | } else { |
| 3737 | #endif |
| 3738 | |
| 3739 | #endif |
| 3740 | |
| 3741 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3744 | * for this */ |
| 3745 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3746 | packed_addr = 0xFFFFFFFF; |
| 3747 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3751 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3752 | PyErr_SetString(socket_error, |
| 3753 | "illegal IP address string passed to inet_aton"); |
| 3754 | return NULL; |
| 3755 | } |
| 3756 | } |
| 3757 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 3758 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3759 | |
| 3760 | #ifdef USE_INET_ATON_WEAKLINK |
| 3761 | } |
| 3762 | #endif |
| 3763 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3764 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3767 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3768 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3769 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3770 | 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] | 3771 | |
| 3772 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3773 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3775 | char *packed_str; |
| 3776 | int addr_len; |
| 3777 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3779 | if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
| 3780 | return NULL; |
| 3781 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3783 | if (addr_len != sizeof(packed_addr)) { |
| 3784 | PyErr_SetString(socket_error, |
| 3785 | "packed IP wrong length for inet_ntoa"); |
| 3786 | return NULL; |
| 3787 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3789 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3791 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3792 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3793 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3794 | #ifdef HAVE_INET_PTON |
| 3795 | |
| 3796 | PyDoc_STRVAR(inet_pton_doc, |
| 3797 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 3798 | \n\ |
| 3799 | Convert an IP address from string format to a packed string suitable\n\ |
| 3800 | for use with low-level network functions."); |
| 3801 | |
| 3802 | static PyObject * |
| 3803 | socket_inet_pton(PyObject *self, PyObject *args) |
| 3804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | int af; |
| 3806 | char* ip; |
| 3807 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3808 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3809 | 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] | 3810 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3811 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3812 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3814 | return NULL; |
| 3815 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3816 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 3817 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3818 | if(af == AF_INET6) { |
| 3819 | PyErr_SetString(socket_error, |
| 3820 | "can't use AF_INET6, IPv6 is disabled"); |
| 3821 | return NULL; |
| 3822 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3823 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | retval = inet_pton(af, ip, packed); |
| 3826 | if (retval < 0) { |
| 3827 | PyErr_SetFromErrno(socket_error); |
| 3828 | return NULL; |
| 3829 | } else if (retval == 0) { |
| 3830 | PyErr_SetString(socket_error, |
| 3831 | "illegal IP address string passed to inet_pton"); |
| 3832 | return NULL; |
| 3833 | } else if (af == AF_INET) { |
| 3834 | return PyBytes_FromStringAndSize(packed, |
| 3835 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3836 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | } else if (af == AF_INET6) { |
| 3838 | return PyBytes_FromStringAndSize(packed, |
| 3839 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3840 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3841 | } else { |
| 3842 | PyErr_SetString(socket_error, "unknown address family"); |
| 3843 | return NULL; |
| 3844 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3845 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3846 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3847 | PyDoc_STRVAR(inet_ntop_doc, |
| 3848 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3849 | \n\ |
| 3850 | Convert a packed IP address of the given family to string format."); |
| 3851 | |
| 3852 | static PyObject * |
| 3853 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 3854 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3855 | int af; |
| 3856 | char* packed; |
| 3857 | int len; |
| 3858 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3859 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3861 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3862 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3863 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 3866 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 3869 | return NULL; |
| 3870 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | if (af == AF_INET) { |
| 3873 | if (len != sizeof(struct in_addr)) { |
| 3874 | PyErr_SetString(PyExc_ValueError, |
| 3875 | "invalid length of packed IP address string"); |
| 3876 | return NULL; |
| 3877 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3878 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3879 | } else if (af == AF_INET6) { |
| 3880 | if (len != sizeof(struct in6_addr)) { |
| 3881 | PyErr_SetString(PyExc_ValueError, |
| 3882 | "invalid length of packed IP address string"); |
| 3883 | return NULL; |
| 3884 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3885 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3886 | } else { |
| 3887 | PyErr_Format(PyExc_ValueError, |
| 3888 | "unknown address family %d", af); |
| 3889 | return NULL; |
| 3890 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3892 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 3893 | if (!retval) { |
| 3894 | PyErr_SetFromErrno(socket_error); |
| 3895 | return NULL; |
| 3896 | } else { |
| 3897 | return PyUnicode_FromString(retval); |
| 3898 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | /* NOTREACHED */ |
| 3901 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 3902 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | #endif /* HAVE_INET_PTON */ |
| 3906 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3907 | /* Python interface to getaddrinfo(host, port). */ |
| 3908 | |
| 3909 | /*ARGSUSED*/ |
| 3910 | static PyObject * |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 3911 | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3912 | { |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 3913 | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
| 3914 | "flags", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3915 | struct addrinfo hints, *res; |
| 3916 | struct addrinfo *res0 = NULL; |
| 3917 | PyObject *hobj = NULL; |
| 3918 | PyObject *pobj = (PyObject *)NULL; |
| 3919 | char pbuf[30]; |
| 3920 | char *hptr, *pptr; |
| 3921 | int family, socktype, protocol, flags; |
| 3922 | int error; |
| 3923 | PyObject *all = (PyObject *)NULL; |
| 3924 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3926 | family = socktype = protocol = flags = 0; |
| 3927 | family = AF_UNSPEC; |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 3928 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
| 3929 | kwnames, &hobj, &pobj, &family, &socktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3930 | &protocol, &flags)) { |
| 3931 | return NULL; |
| 3932 | } |
| 3933 | if (hobj == Py_None) { |
| 3934 | hptr = NULL; |
| 3935 | } else if (PyUnicode_Check(hobj)) { |
| 3936 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 3937 | if (!idna) |
| 3938 | return NULL; |
| 3939 | assert(PyBytes_Check(idna)); |
| 3940 | hptr = PyBytes_AS_STRING(idna); |
| 3941 | } else if (PyBytes_Check(hobj)) { |
| 3942 | hptr = PyBytes_AsString(hobj); |
| 3943 | } else { |
| 3944 | PyErr_SetString(PyExc_TypeError, |
| 3945 | "getaddrinfo() argument 1 must be string or None"); |
| 3946 | return NULL; |
| 3947 | } |
| 3948 | if (PyLong_CheckExact(pobj)) { |
| 3949 | long value = PyLong_AsLong(pobj); |
| 3950 | if (value == -1 && PyErr_Occurred()) |
| 3951 | goto err; |
| 3952 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 3953 | pptr = pbuf; |
| 3954 | } else if (PyUnicode_Check(pobj)) { |
| 3955 | pptr = _PyUnicode_AsString(pobj); |
| 3956 | } else if (PyBytes_Check(pobj)) { |
| 3957 | pptr = PyBytes_AsString(pobj); |
| 3958 | } else if (pobj == Py_None) { |
| 3959 | pptr = (char *)NULL; |
| 3960 | } else { |
| 3961 | PyErr_SetString(socket_error, "Int or String expected"); |
| 3962 | goto err; |
| 3963 | } |
| 3964 | memset(&hints, 0, sizeof(hints)); |
| 3965 | hints.ai_family = family; |
| 3966 | hints.ai_socktype = socktype; |
| 3967 | hints.ai_protocol = protocol; |
| 3968 | hints.ai_flags = flags; |
| 3969 | Py_BEGIN_ALLOW_THREADS |
| 3970 | ACQUIRE_GETADDRINFO_LOCK |
| 3971 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 3972 | Py_END_ALLOW_THREADS |
| 3973 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 3974 | if (error) { |
| 3975 | set_gaierror(error); |
| 3976 | goto err; |
| 3977 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3979 | if ((all = PyList_New(0)) == NULL) |
| 3980 | goto err; |
| 3981 | for (res = res0; res; res = res->ai_next) { |
| 3982 | PyObject *single; |
| 3983 | PyObject *addr = |
| 3984 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 3985 | if (addr == NULL) |
| 3986 | goto err; |
| 3987 | single = Py_BuildValue("iiisO", res->ai_family, |
| 3988 | res->ai_socktype, res->ai_protocol, |
| 3989 | res->ai_canonname ? res->ai_canonname : "", |
| 3990 | addr); |
| 3991 | Py_DECREF(addr); |
| 3992 | if (single == NULL) |
| 3993 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | if (PyList_Append(all, single)) |
| 3996 | goto err; |
| 3997 | Py_XDECREF(single); |
| 3998 | } |
| 3999 | Py_XDECREF(idna); |
| 4000 | if (res0) |
| 4001 | freeaddrinfo(res0); |
| 4002 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4003 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | Py_XDECREF(all); |
| 4005 | Py_XDECREF(idna); |
| 4006 | if (res0) |
| 4007 | freeaddrinfo(res0); |
| 4008 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4011 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4012 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 4013 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4014 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4015 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4016 | |
| 4017 | /* Python interface to getnameinfo(sa, flags). */ |
| 4018 | |
| 4019 | /*ARGSUSED*/ |
| 4020 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4021 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4022 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4023 | PyObject *sa = (PyObject *)NULL; |
| 4024 | int flags; |
| 4025 | char *hostp; |
| 4026 | int port, flowinfo, scope_id; |
| 4027 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 4028 | struct addrinfo hints, *res = NULL; |
| 4029 | int error; |
| 4030 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | flags = flowinfo = scope_id = 0; |
| 4033 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 4034 | return NULL; |
| 4035 | if (!PyTuple_Check(sa)) { |
| 4036 | PyErr_SetString(PyExc_TypeError, |
| 4037 | "getnameinfo() argument 1 must be a tuple"); |
| 4038 | return NULL; |
| 4039 | } |
| 4040 | if (!PyArg_ParseTuple(sa, "si|ii", |
| 4041 | &hostp, &port, &flowinfo, &scope_id)) |
| 4042 | return NULL; |
| 4043 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 4044 | memset(&hints, 0, sizeof(hints)); |
| 4045 | hints.ai_family = AF_UNSPEC; |
| 4046 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 4047 | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4048 | Py_BEGIN_ALLOW_THREADS |
| 4049 | ACQUIRE_GETADDRINFO_LOCK |
| 4050 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 4051 | Py_END_ALLOW_THREADS |
| 4052 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4053 | if (error) { |
| 4054 | set_gaierror(error); |
| 4055 | goto fail; |
| 4056 | } |
| 4057 | if (res->ai_next) { |
| 4058 | PyErr_SetString(socket_error, |
| 4059 | "sockaddr resolved to multiple addresses"); |
| 4060 | goto fail; |
| 4061 | } |
| 4062 | switch (res->ai_family) { |
| 4063 | case AF_INET: |
| 4064 | { |
| 4065 | if (PyTuple_GET_SIZE(sa) != 2) { |
| 4066 | PyErr_SetString(socket_error, |
| 4067 | "IPv4 sockaddr must be 2 tuple"); |
| 4068 | goto fail; |
| 4069 | } |
| 4070 | break; |
| 4071 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4072 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | case AF_INET6: |
| 4074 | { |
| 4075 | struct sockaddr_in6 *sin6; |
| 4076 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 4077 | sin6->sin6_flowinfo = flowinfo; |
| 4078 | sin6->sin6_scope_id = scope_id; |
| 4079 | break; |
| 4080 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4081 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | } |
Antoine Pitrou | 19467d2 | 2010-08-17 19:33:30 +0000 | [diff] [blame] | 4083 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4084 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 4085 | if (error) { |
| 4086 | set_gaierror(error); |
| 4087 | goto fail; |
| 4088 | } |
| 4089 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4090 | |
| 4091 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | if (res) |
| 4093 | freeaddrinfo(res); |
| 4094 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4097 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4098 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4099 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4100 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4101 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4102 | |
| 4103 | /* Python API to getting and setting the default timeout value. */ |
| 4104 | |
| 4105 | static PyObject * |
| 4106 | socket_getdefaulttimeout(PyObject *self) |
| 4107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4108 | if (defaulttimeout < 0.0) { |
| 4109 | Py_INCREF(Py_None); |
| 4110 | return Py_None; |
| 4111 | } |
| 4112 | else |
| 4113 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4117 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4118 | \n\ |
| 4119 | Returns the default timeout in floating seconds for new socket objects.\n\ |
| 4120 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4121 | When the socket module is first imported, the default is None."); |
| 4122 | |
| 4123 | static PyObject * |
| 4124 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 4125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4126 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4128 | if (arg == Py_None) |
| 4129 | timeout = -1.0; |
| 4130 | else { |
| 4131 | timeout = PyFloat_AsDouble(arg); |
| 4132 | if (timeout < 0.0) { |
| 4133 | if (!PyErr_Occurred()) |
| 4134 | PyErr_SetString(PyExc_ValueError, |
| 4135 | "Timeout value out of range"); |
| 4136 | return NULL; |
| 4137 | } |
| 4138 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4140 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 | Py_INCREF(Py_None); |
| 4143 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4147 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4148 | \n\ |
| 4149 | Set the default timeout in floating seconds for new socket objects.\n\ |
| 4150 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4151 | When the socket module is first imported, the default is None."); |
| 4152 | |
| 4153 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4154 | /* List of functions exported by this module. */ |
| 4155 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4156 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4157 | {"gethostbyname", socket_gethostbyname, |
| 4158 | METH_VARARGS, gethostbyname_doc}, |
| 4159 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4160 | METH_VARARGS, ghbn_ex_doc}, |
| 4161 | {"gethostbyaddr", socket_gethostbyaddr, |
| 4162 | METH_VARARGS, gethostbyaddr_doc}, |
| 4163 | {"gethostname", socket_gethostname, |
| 4164 | METH_NOARGS, gethostname_doc}, |
| 4165 | {"getservbyname", socket_getservbyname, |
| 4166 | METH_VARARGS, getservbyname_doc}, |
| 4167 | {"getservbyport", socket_getservbyport, |
| 4168 | METH_VARARGS, getservbyport_doc}, |
| 4169 | {"getprotobyname", socket_getprotobyname, |
| 4170 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4171 | #ifndef NO_DUP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | {"dup", socket_dup, |
| 4173 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4174 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4175 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4176 | {"socketpair", socket_socketpair, |
| 4177 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4178 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4179 | {"ntohs", socket_ntohs, |
| 4180 | METH_VARARGS, ntohs_doc}, |
| 4181 | {"ntohl", socket_ntohl, |
| 4182 | METH_O, ntohl_doc}, |
| 4183 | {"htons", socket_htons, |
| 4184 | METH_VARARGS, htons_doc}, |
| 4185 | {"htonl", socket_htonl, |
| 4186 | METH_O, htonl_doc}, |
| 4187 | {"inet_aton", socket_inet_aton, |
| 4188 | METH_VARARGS, inet_aton_doc}, |
| 4189 | {"inet_ntoa", socket_inet_ntoa, |
| 4190 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4191 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4192 | {"inet_pton", socket_inet_pton, |
| 4193 | METH_VARARGS, inet_pton_doc}, |
| 4194 | {"inet_ntop", socket_inet_ntop, |
| 4195 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4196 | #endif |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 4197 | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
| 4198 | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | {"getnameinfo", socket_getnameinfo, |
| 4200 | METH_VARARGS, getnameinfo_doc}, |
| 4201 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4202 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4203 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4204 | METH_O, setdefaulttimeout_doc}, |
| 4205 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4206 | }; |
| 4207 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4208 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4209 | #ifdef MS_WINDOWS |
| 4210 | #define OS_INIT_DEFINED |
| 4211 | |
| 4212 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4213 | |
| 4214 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4215 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4217 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4221 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4223 | WSADATA WSAData; |
| 4224 | int ret; |
| 4225 | ret = WSAStartup(0x0101, &WSAData); |
| 4226 | switch (ret) { |
| 4227 | case 0: /* No error */ |
| 4228 | Py_AtExit(os_cleanup); |
| 4229 | return 1; /* Success */ |
| 4230 | case WSASYSNOTREADY: |
| 4231 | PyErr_SetString(PyExc_ImportError, |
| 4232 | "WSAStartup failed: network not ready"); |
| 4233 | break; |
| 4234 | case WSAVERNOTSUPPORTED: |
| 4235 | case WSAEINVAL: |
| 4236 | PyErr_SetString( |
| 4237 | PyExc_ImportError, |
| 4238 | "WSAStartup failed: requested version not supported"); |
| 4239 | break; |
| 4240 | default: |
| 4241 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 4242 | break; |
| 4243 | } |
| 4244 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4247 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4248 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4249 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4250 | #ifdef PYOS_OS2 |
| 4251 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4252 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4253 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4254 | |
| 4255 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4256 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4257 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4258 | #ifndef PYCC_GCC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4259 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4261 | if (rc == 0) { |
| 4262 | return 1; /* Success */ |
| 4263 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4265 | PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno()); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4268 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4269 | /* No need to initialise sockets with GCC/EMX */ |
| 4270 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4271 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | #endif /* PYOS_OS2 */ |
| 4275 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4276 | |
| 4277 | #ifndef OS_INIT_DEFINED |
| 4278 | static int |
| 4279 | os_init(void) |
| 4280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4282 | } |
| 4283 | #endif |
| 4284 | |
| 4285 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4286 | /* C API table - always add new things to the end for binary |
| 4287 | compatibility. */ |
| 4288 | static |
| 4289 | PySocketModule_APIObject PySocketModuleAPI = |
| 4290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4291 | &sock_type, |
| 4292 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4293 | }; |
| 4294 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4295 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4296 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4297 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4298 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4299 | "socket.py" which implements some additional functionality. |
| 4300 | The import of "_socket" may fail with an ImportError exception if |
| 4301 | os-specific initialization fails. On Windows, this does WINSOCK |
| 4302 | initialization. When WINSOCK is initialized succesfully, a call to |
| 4303 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4304 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4306 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4307 | "Implementation module for socket operations.\n\ |
| 4308 | \n\ |
| 4309 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4310 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4311 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4312 | PyModuleDef_HEAD_INIT, |
| 4313 | PySocket_MODULE_NAME, |
| 4314 | socket_doc, |
| 4315 | -1, |
| 4316 | socket_methods, |
| 4317 | NULL, |
| 4318 | NULL, |
| 4319 | NULL, |
| 4320 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4321 | }; |
| 4322 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4323 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4324 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4326 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | if (!os_init()) |
| 4329 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | Py_TYPE(&sock_type) = &PyType_Type; |
| 4332 | m = PyModule_Create(&socketmodule); |
| 4333 | if (m == NULL) |
| 4334 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4336 | socket_error = PyErr_NewException("socket.error", |
| 4337 | PyExc_IOError, NULL); |
| 4338 | if (socket_error == NULL) |
| 4339 | return NULL; |
| 4340 | PySocketModuleAPI.error = socket_error; |
| 4341 | Py_INCREF(socket_error); |
| 4342 | PyModule_AddObject(m, "error", socket_error); |
| 4343 | socket_herror = PyErr_NewException("socket.herror", |
| 4344 | socket_error, NULL); |
| 4345 | if (socket_herror == NULL) |
| 4346 | return NULL; |
| 4347 | Py_INCREF(socket_herror); |
| 4348 | PyModule_AddObject(m, "herror", socket_herror); |
| 4349 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4350 | NULL); |
| 4351 | if (socket_gaierror == NULL) |
| 4352 | return NULL; |
| 4353 | Py_INCREF(socket_gaierror); |
| 4354 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4355 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4356 | socket_error, NULL); |
| 4357 | if (socket_timeout == NULL) |
| 4358 | return NULL; |
| 4359 | Py_INCREF(socket_timeout); |
| 4360 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 4361 | Py_INCREF((PyObject *)&sock_type); |
| 4362 | if (PyModule_AddObject(m, "SocketType", |
| 4363 | (PyObject *)&sock_type) != 0) |
| 4364 | return NULL; |
| 4365 | Py_INCREF((PyObject *)&sock_type); |
| 4366 | if (PyModule_AddObject(m, "socket", |
| 4367 | (PyObject *)&sock_type) != 0) |
| 4368 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4369 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4370 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4371 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4372 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4373 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4374 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | Py_INCREF(has_ipv6); |
| 4376 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | /* Export C API */ |
| 4379 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4380 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4381 | ) != 0) |
| 4382 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4384 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4385 | #ifdef AF_UNSPEC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4386 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4387 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4389 | #ifdef AF_INET6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4390 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4391 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4392 | #if defined(AF_UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4394 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4395 | #ifdef AF_AX25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | /* Amateur Radio AX.25 */ |
| 4397 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4398 | #endif |
| 4399 | #ifdef AF_IPX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4401 | #endif |
| 4402 | #ifdef AF_APPLETALK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | /* Appletalk DDP */ |
| 4404 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4405 | #endif |
| 4406 | #ifdef AF_NETROM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | /* Amateur radio NetROM */ |
| 4408 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4409 | #endif |
| 4410 | #ifdef AF_BRIDGE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | /* Multiprotocol bridge */ |
| 4412 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4413 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4414 | #ifdef AF_ATMPVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | /* ATM PVCs */ |
| 4416 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4417 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4418 | #ifdef AF_AAL5 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4419 | /* Reserved for Werner's ATM */ |
| 4420 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4421 | #endif |
| 4422 | #ifdef AF_X25 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | /* Reserved for X.25 project */ |
| 4424 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4425 | #endif |
| 4426 | #ifdef AF_INET6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4428 | #endif |
| 4429 | #ifdef AF_ROSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | /* Amateur Radio X.25 PLP */ |
| 4431 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4432 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4433 | #ifdef AF_DECnet |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4434 | /* Reserved for DECnet project */ |
| 4435 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4436 | #endif |
| 4437 | #ifdef AF_NETBEUI |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | /* Reserved for 802.2LLC project */ |
| 4439 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4440 | #endif |
| 4441 | #ifdef AF_SECURITY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | /* Security callback pseudo AF */ |
| 4443 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4444 | #endif |
| 4445 | #ifdef AF_KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4446 | /* PF_KEY key management API */ |
| 4447 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4448 | #endif |
| 4449 | #ifdef AF_NETLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4450 | /* */ |
| 4451 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4452 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4453 | #ifdef NETLINK_SKIP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4455 | #endif |
| 4456 | #ifdef NETLINK_W1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4458 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4459 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4460 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4461 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4463 | #endif |
| 4464 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4466 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4467 | #ifdef NETLINK_XFRM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4469 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4470 | #ifdef NETLINK_ARPD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4472 | #endif |
| 4473 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4474 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4475 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4476 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4477 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4479 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4480 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4482 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4483 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4484 | #ifdef AF_ROUTE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | /* Alias to emulate 4.4BSD */ |
| 4486 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4487 | #endif |
| 4488 | #ifdef AF_ASH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | /* Ash */ |
| 4490 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4491 | #endif |
| 4492 | #ifdef AF_ECONET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | /* Acorn Econet */ |
| 4494 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4495 | #endif |
| 4496 | #ifdef AF_ATMSVC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | /* ATM SVCs */ |
| 4498 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4499 | #endif |
| 4500 | #ifdef AF_SNA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | /* Linux SNA Project (nutters!) */ |
| 4502 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4503 | #endif |
| 4504 | #ifdef AF_IRDA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | /* IRDA sockets */ |
| 4506 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4507 | #endif |
| 4508 | #ifdef AF_PPPOX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4509 | /* PPPoX sockets */ |
| 4510 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4511 | #endif |
| 4512 | #ifdef AF_WANPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4513 | /* Wanpipe API Sockets */ |
| 4514 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4515 | #endif |
| 4516 | #ifdef AF_LLC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 | /* Linux LLC */ |
| 4518 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4519 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4520 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4521 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4522 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4523 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4524 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4525 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
| 4526 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4527 | #if !defined(__FreeBSD__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
| 4529 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4530 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4531 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4532 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4533 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4534 | 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] | 4535 | #endif |
| 4536 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 4537 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); |
| 4539 | PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); |
| 4540 | PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); |
| 4541 | PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 4542 | PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 4543 | PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 4544 | PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 4545 | PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 4546 | PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4547 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4548 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4549 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4550 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4552 | /* for addresses */ |
| 4553 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4554 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4555 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4558 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4559 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | /* for setsockopt() */ |
| 4562 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4563 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4564 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4565 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4566 | TIPC_DEST_DROPPABLE); |
| 4567 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4569 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4570 | TIPC_LOW_IMPORTANCE); |
| 4571 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4572 | TIPC_MEDIUM_IMPORTANCE); |
| 4573 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4574 | TIPC_HIGH_IMPORTANCE); |
| 4575 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4576 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | /* for subscriptions */ |
| 4579 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4580 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 4581 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | /* doesn't seem to be available everywhere */ |
| 4583 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 4584 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4586 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4587 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4588 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4589 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4590 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4591 | #endif |
| 4592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4593 | /* Socket types */ |
| 4594 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4595 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4596 | /* We have incomplete socket support. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4597 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4598 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4599 | #if defined(SOCK_RDM) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4600 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4601 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | #ifdef SO_DEBUG |
| 4604 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4605 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | #ifdef SO_ACCEPTCONN |
| 4607 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4608 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4609 | #ifdef SO_REUSEADDR |
| 4610 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4611 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4612 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4613 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4614 | #endif |
| 4615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | #ifdef SO_KEEPALIVE |
| 4617 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4618 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | #ifdef SO_DONTROUTE |
| 4620 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4621 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | #ifdef SO_BROADCAST |
| 4623 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4624 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4625 | #ifdef SO_USELOOPBACK |
| 4626 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4627 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4628 | #ifdef SO_LINGER |
| 4629 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4630 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4631 | #ifdef SO_OOBINLINE |
| 4632 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4633 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | #ifdef SO_REUSEPORT |
| 4635 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4636 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | #ifdef SO_SNDBUF |
| 4638 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4639 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | #ifdef SO_RCVBUF |
| 4641 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4642 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4643 | #ifdef SO_SNDLOWAT |
| 4644 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4645 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | #ifdef SO_RCVLOWAT |
| 4647 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4648 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4649 | #ifdef SO_SNDTIMEO |
| 4650 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4651 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4652 | #ifdef SO_RCVTIMEO |
| 4653 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4654 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4655 | #ifdef SO_ERROR |
| 4656 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4657 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4658 | #ifdef SO_TYPE |
| 4659 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4660 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4661 | #ifdef SO_SETFIB |
| 4662 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB); |
Larry Hastings | f0f3795 | 2010-04-02 11:47:10 +0000 | [diff] [blame] | 4663 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4665 | /* Maximum number of connections for "listen" */ |
| 4666 | #ifdef SOMAXCONN |
| 4667 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4668 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4670 | #endif |
| 4671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | /* Flags for send, recv */ |
| 4673 | #ifdef MSG_OOB |
| 4674 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4675 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | #ifdef MSG_PEEK |
| 4677 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4678 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | #ifdef MSG_DONTROUTE |
| 4680 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4681 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | #ifdef MSG_DONTWAIT |
| 4683 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4684 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4685 | #ifdef MSG_EOR |
| 4686 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4687 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | #ifdef MSG_TRUNC |
| 4689 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4690 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4691 | #ifdef MSG_CTRUNC |
| 4692 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4693 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4694 | #ifdef MSG_WAITALL |
| 4695 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4696 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4697 | #ifdef MSG_BTAG |
| 4698 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4699 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4700 | #ifdef MSG_ETAG |
| 4701 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4702 | #endif |
| 4703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4704 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4705 | #ifdef SOL_SOCKET |
| 4706 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4707 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4708 | #ifdef SOL_IP |
| 4709 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4710 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4711 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4712 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | #ifdef SOL_IPX |
| 4714 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4715 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4716 | #ifdef SOL_AX25 |
| 4717 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4718 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4719 | #ifdef SOL_ATALK |
| 4720 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4721 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4722 | #ifdef SOL_NETROM |
| 4723 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4724 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4725 | #ifdef SOL_ROSE |
| 4726 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4727 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | #ifdef SOL_TCP |
| 4729 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4730 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4731 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4732 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | #ifdef SOL_UDP |
| 4734 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4735 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4736 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4737 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4738 | #ifdef IPPROTO_IP |
| 4739 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4740 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4742 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4743 | #ifdef IPPROTO_HOPOPTS |
| 4744 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4745 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | #ifdef IPPROTO_ICMP |
| 4747 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4748 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4749 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4750 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | #ifdef IPPROTO_IGMP |
| 4752 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4753 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4754 | #ifdef IPPROTO_GGP |
| 4755 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4756 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4757 | #ifdef IPPROTO_IPV4 |
| 4758 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4759 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | #ifdef IPPROTO_IPV6 |
| 4761 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 4762 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4763 | #ifdef IPPROTO_IPIP |
| 4764 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4765 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4766 | #ifdef IPPROTO_TCP |
| 4767 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4768 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4769 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4770 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4771 | #ifdef IPPROTO_EGP |
| 4772 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4773 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4774 | #ifdef IPPROTO_PUP |
| 4775 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4776 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4777 | #ifdef IPPROTO_UDP |
| 4778 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4779 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4781 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4782 | #ifdef IPPROTO_IDP |
| 4783 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4784 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | #ifdef IPPROTO_HELLO |
| 4786 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4787 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4788 | #ifdef IPPROTO_ND |
| 4789 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4790 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4791 | #ifdef IPPROTO_TP |
| 4792 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4793 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4794 | #ifdef IPPROTO_IPV6 |
| 4795 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4796 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4797 | #ifdef IPPROTO_ROUTING |
| 4798 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4799 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | #ifdef IPPROTO_FRAGMENT |
| 4801 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4802 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4803 | #ifdef IPPROTO_RSVP |
| 4804 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4805 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4806 | #ifdef IPPROTO_GRE |
| 4807 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4808 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | #ifdef IPPROTO_ESP |
| 4810 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4811 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4812 | #ifdef IPPROTO_AH |
| 4813 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4814 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | #ifdef IPPROTO_MOBILE |
| 4816 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4817 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4818 | #ifdef IPPROTO_ICMPV6 |
| 4819 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4820 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4821 | #ifdef IPPROTO_NONE |
| 4822 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4823 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4824 | #ifdef IPPROTO_DSTOPTS |
| 4825 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4826 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4827 | #ifdef IPPROTO_XTP |
| 4828 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4829 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4830 | #ifdef IPPROTO_EON |
| 4831 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4832 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4833 | #ifdef IPPROTO_PIM |
| 4834 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4835 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4836 | #ifdef IPPROTO_IPCOMP |
| 4837 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4838 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4839 | #ifdef IPPROTO_VRRP |
| 4840 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4841 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4842 | #ifdef IPPROTO_BIP |
| 4843 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4844 | #endif |
| 4845 | /**/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4846 | #ifdef IPPROTO_RAW |
| 4847 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4848 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4849 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4850 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | #ifdef IPPROTO_MAX |
| 4852 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4853 | #endif |
| 4854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4855 | /* Some port configuration */ |
| 4856 | #ifdef IPPORT_RESERVED |
| 4857 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4858 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4859 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4860 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4861 | #ifdef IPPORT_USERRESERVED |
| 4862 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4863 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4864 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4865 | #endif |
| 4866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4867 | /* Some reserved IP v.4 addresses */ |
| 4868 | #ifdef INADDR_ANY |
| 4869 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4870 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4871 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4872 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4873 | #ifdef INADDR_BROADCAST |
| 4874 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4875 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4876 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4877 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | #ifdef INADDR_LOOPBACK |
| 4879 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4880 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4881 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4882 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4883 | #ifdef INADDR_UNSPEC_GROUP |
| 4884 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4885 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4886 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4887 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4888 | #ifdef INADDR_ALLHOSTS_GROUP |
| 4889 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 4890 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4891 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4892 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4893 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 4895 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 4896 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4897 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4898 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4899 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4900 | #ifdef INADDR_NONE |
| 4901 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4902 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4903 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4904 | #endif |
| 4905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | /* IPv4 [gs]etsockopt options */ |
| 4907 | #ifdef IP_OPTIONS |
| 4908 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4909 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | #ifdef IP_HDRINCL |
| 4911 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4912 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | #ifdef IP_TOS |
| 4914 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4915 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4916 | #ifdef IP_TTL |
| 4917 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4918 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4919 | #ifdef IP_RECVOPTS |
| 4920 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4921 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4922 | #ifdef IP_RECVRETOPTS |
| 4923 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4924 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4925 | #ifdef IP_RECVDSTADDR |
| 4926 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4927 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4928 | #ifdef IP_RETOPTS |
| 4929 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4930 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4931 | #ifdef IP_MULTICAST_IF |
| 4932 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4933 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4934 | #ifdef IP_MULTICAST_TTL |
| 4935 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4936 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4937 | #ifdef IP_MULTICAST_LOOP |
| 4938 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4939 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4940 | #ifdef IP_ADD_MEMBERSHIP |
| 4941 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4942 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | #ifdef IP_DROP_MEMBERSHIP |
| 4944 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4945 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4946 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 4947 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 4948 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4949 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4950 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 4951 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 4952 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4953 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4954 | #ifdef IP_MAX_MEMBERSHIPS |
| 4955 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4956 | #endif |
| 4957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4958 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 4959 | #ifdef IPV6_JOIN_GROUP |
| 4960 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4961 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4962 | #ifdef IPV6_LEAVE_GROUP |
| 4963 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4964 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4965 | #ifdef IPV6_MULTICAST_HOPS |
| 4966 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4967 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4968 | #ifdef IPV6_MULTICAST_IF |
| 4969 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4970 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4971 | #ifdef IPV6_MULTICAST_LOOP |
| 4972 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4973 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4974 | #ifdef IPV6_UNICAST_HOPS |
| 4975 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4976 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4978 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4980 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4981 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4982 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4984 | #endif |
| 4985 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4986 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4987 | #endif |
| 4988 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4990 | #endif |
| 4991 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4993 | #endif |
| 4994 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4995 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4996 | #endif |
| 4997 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4999 | #endif |
| 5000 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5001 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5002 | #endif |
| 5003 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5005 | #endif |
| 5006 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5007 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5008 | #endif |
| 5009 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5011 | #endif |
| 5012 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5014 | #endif |
| 5015 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5017 | #endif |
| 5018 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5019 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5020 | #endif |
| 5021 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5022 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5023 | #endif |
| 5024 | #ifdef IPV6_RTHDR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5025 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5026 | #endif |
| 5027 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5029 | #endif |
| 5030 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5031 | 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] | 5032 | #endif |
| 5033 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5034 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5035 | #endif |
| 5036 | #ifdef IPV6_TCLASS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5037 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5038 | #endif |
| 5039 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5040 | 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] | 5041 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5043 | /* TCP options */ |
| 5044 | #ifdef TCP_NODELAY |
| 5045 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5046 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | #ifdef TCP_MAXSEG |
| 5048 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5049 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | #ifdef TCP_CORK |
| 5051 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5052 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5053 | #ifdef TCP_KEEPIDLE |
| 5054 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5055 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5056 | #ifdef TCP_KEEPINTVL |
| 5057 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5058 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5059 | #ifdef TCP_KEEPCNT |
| 5060 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5061 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5062 | #ifdef TCP_SYNCNT |
| 5063 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5064 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | #ifdef TCP_LINGER2 |
| 5066 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5067 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | #ifdef TCP_DEFER_ACCEPT |
| 5069 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5070 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5071 | #ifdef TCP_WINDOW_CLAMP |
| 5072 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5073 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | #ifdef TCP_INFO |
| 5075 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5076 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5077 | #ifdef TCP_QUICKACK |
| 5078 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5079 | #endif |
| 5080 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | /* IPX options */ |
| 5083 | #ifdef IPX_TYPE |
| 5084 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5085 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5088 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5090 | #endif |
| 5091 | #ifdef EAI_AGAIN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5093 | #endif |
| 5094 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5095 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5096 | #endif |
| 5097 | #ifdef EAI_FAIL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5099 | #endif |
| 5100 | #ifdef EAI_FAMILY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5102 | #endif |
| 5103 | #ifdef EAI_MEMORY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5104 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5105 | #endif |
| 5106 | #ifdef EAI_NODATA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5107 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5108 | #endif |
| 5109 | #ifdef EAI_NONAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5110 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5111 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5112 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5113 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5114 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5115 | #ifdef EAI_SERVICE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5116 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5117 | #endif |
| 5118 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5119 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5120 | #endif |
| 5121 | #ifdef EAI_SYSTEM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5122 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5123 | #endif |
| 5124 | #ifdef EAI_BADHINTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5125 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5126 | #endif |
| 5127 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5128 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5129 | #endif |
| 5130 | #ifdef EAI_MAX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5131 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5132 | #endif |
| 5133 | #ifdef AI_PASSIVE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5135 | #endif |
| 5136 | #ifdef AI_CANONNAME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5138 | #endif |
| 5139 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5140 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5141 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5142 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5143 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5144 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5145 | #ifdef AI_MASK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5146 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5147 | #endif |
| 5148 | #ifdef AI_ALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5149 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5150 | #endif |
| 5151 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5153 | #endif |
| 5154 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5155 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5156 | #endif |
| 5157 | #ifdef AI_V4MAPPED |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5158 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5159 | #endif |
| 5160 | #ifdef AI_DEFAULT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5161 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5162 | #endif |
| 5163 | #ifdef NI_MAXHOST |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5165 | #endif |
| 5166 | #ifdef NI_MAXSERV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5167 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5168 | #endif |
| 5169 | #ifdef NI_NOFQDN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5170 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5171 | #endif |
| 5172 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5173 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5174 | #endif |
| 5175 | #ifdef NI_NAMEREQD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5176 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5177 | #endif |
| 5178 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5180 | #endif |
| 5181 | #ifdef NI_DGRAM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5182 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5183 | #endif |
| 5184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5185 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5186 | #ifdef SHUT_RD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5187 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5188 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5189 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5190 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5191 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5192 | #endif |
| 5193 | #ifdef SHUT_WR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5194 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5195 | #elif defined(SD_SEND) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5196 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5197 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5199 | #endif |
| 5200 | #ifdef SHUT_RDWR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5201 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5202 | #elif defined(SD_BOTH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5204 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5205 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5206 | #endif |
| 5207 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 5208 | #ifdef SIO_RCVALL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | { |
| 5210 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 5211 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 5212 | int i; |
| 5213 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) { |
| 5214 | PyObject *tmp; |
| 5215 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 5216 | if (tmp == NULL) |
| 5217 | return NULL; |
| 5218 | PyModule_AddObject(m, names[i], tmp); |
| 5219 | } |
| 5220 | } |
| 5221 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5222 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5223 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 5224 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5225 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 5226 | #endif |
| 5227 | #ifdef RCVALL_MAX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5228 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 5229 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 5230 | #endif /* _MSTCPIP_ */ |
| 5231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5233 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5234 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5235 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5236 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5237 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5238 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5239 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5240 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 5241 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5242 | |
| 5243 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5244 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5245 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5246 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5247 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5249 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 5250 | #if (SIZEOF_INT != 4) |
| 5251 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5252 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5253 | unsigned int packed_addr; |
| 5254 | packed_addr = inet_addr(src); |
| 5255 | if (packed_addr == INADDR_NONE) |
| 5256 | return 0; |
| 5257 | memcpy(dst, &packed_addr, 4); |
| 5258 | return 1; |
| 5259 | } |
| 5260 | /* Should set errno to EAFNOSUPPORT */ |
| 5261 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5264 | const char * |
| 5265 | 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] | 5266 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | if (af == AF_INET) { |
| 5268 | struct in_addr packed_addr; |
| 5269 | if (size < 16) |
| 5270 | /* Should set errno to ENOSPC. */ |
| 5271 | return NULL; |
| 5272 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5273 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5274 | } |
| 5275 | /* Should set errno to EAFNOSUPPORT */ |
| 5276 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5277 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5278 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5279 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 5280 | #endif |