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 | fb2d25a | 2008-01-07 16:12:44 +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 | c83ea13 | 2010-05-09 14:46:46 +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 | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 22 | a subclass of socket.error |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 23 | - socket.fromfd(fd, family, type[, proto]) --> new socket object (created |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 24 | from an existing file descriptor) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 25 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 26 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 27 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 28 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 29 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 30 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 31 | - socket.socket([family[, type [, proto]]]) --> new socket object |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 32 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 33 | - socket.ntohs(16 bit value) --> new int object |
| 34 | - socket.ntohl(32 bit value) --> new int object |
| 35 | - socket.htons(16 bit value) --> new int object |
| 36 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 37 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 38 | --> List of (family, socktype, proto, canonname, sockaddr) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 39 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 40 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 41 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 42 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 43 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 44 | - socket.getdefaulttimeout() -> None | float |
| 45 | - socket.setdefaulttimeout(None | float) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 46 | - an Internet socket address is a pair (hostname, port) |
| 47 | where hostname can be anything recognized by gethostbyname() |
| 48 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 49 | - 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] | 50 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 51 | - an AF_PACKET socket address is a tuple containing a string |
| 52 | specifying the ethernet interface and an integer specifying |
| 53 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 54 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 55 | specify packet-type and ha-type/addr. |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 56 | - an AF_TIPC socket address is expressed as |
| 57 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 59 | and scope can be one of: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 60 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 61 | The meaning of v1, v2 and v3 depends on the value of addr_type: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 62 | if addr_type is TIPC_ADDR_NAME: |
| 63 | v1 is the server type |
| 64 | v2 is the port identifier |
| 65 | v3 is ignored |
| 66 | if addr_type is TIPC_ADDR_NAMESEQ: |
| 67 | v1 is the server type |
| 68 | v2 is the lower port number |
| 69 | v3 is the upper port number |
| 70 | if addr_type is TIPC_ADDR_ID: |
| 71 | v1 is the node |
| 72 | v2 is the ref |
| 73 | v3 is ignored |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 76 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 78 | - names starting with sock_ are socket object methods |
| 79 | - names starting with socket_ are module-level functions |
| 80 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 84 | #ifdef __APPLE__ |
| 85 | /* |
| 86 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 87 | * that was build on 10.4 or later to work on that release, weak linking |
| 88 | * comes to the rescue. |
| 89 | */ |
| 90 | # pragma weak inet_aton |
| 91 | #endif |
| 92 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 93 | #include "Python.h" |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 94 | #include "structmember.h" |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 95 | #include "timefuncs.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 97 | #undef MAX |
| 98 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 99 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 100 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 102 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 103 | \n\ |
| 104 | Open a socket of the given type. The family argument specifies the\n\ |
| 105 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 106 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 107 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 108 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 109 | \n\ |
| 110 | A socket object represents one endpoint of a network connection.\n\ |
| 111 | \n\ |
| 112 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 113 | \n\ |
| 114 | accept() -- accept a connection, returning new socket and client address\n\ |
| 115 | bind(addr) -- bind the socket to a local address\n\ |
| 116 | close() -- close the socket\n\ |
| 117 | connect(addr) -- connect the socket to a remote address\n\ |
| 118 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
| 119 | dup() -- return a new socket object identical to the current one [*]\n\ |
| 120 | fileno() -- return underlying file descriptor\n\ |
| 121 | getpeername() -- return remote address [*]\n\ |
| 122 | getsockname() -- return local address\n\ |
| 123 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 124 | gettimeout() -- return timeout or None\n\ |
| 125 | listen(n) -- start listening for incoming connections\n\ |
| 126 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\ |
| 127 | recv(buflen[, flags]) -- receive data\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 128 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 129 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 130 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 131 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 132 | sendall(data[, flags]) -- send all data\n\ |
| 133 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 134 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 135 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 136 | setsockopt(level, optname, value) -- set socket options\n\ |
| 137 | settimeout(None | float) -- set or clear the timeout\n\ |
| 138 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 139 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 140 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 141 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 142 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 143 | I hope some day someone can clean this up please... */ |
| 144 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 145 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 146 | script doesn't get this right, so we hardcode some platform checks below. |
| 147 | On the other hand, not all Linux versions agree, so there the settings |
| 148 | computed by the configure script are needed! */ |
| 149 | |
| 150 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 151 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 152 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 153 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 154 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 156 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 157 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 158 | #endif |
| 159 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 160 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 161 | # if defined(_AIX) || defined(__osf__) |
| 162 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 163 | # elif defined(__sun) || defined(__sgi) |
| 164 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 165 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 166 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 167 | # else |
| 168 | # undef HAVE_GETHOSTBYNAME_R |
| 169 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 170 | #endif |
| 171 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 172 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 173 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 174 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 177 | /* To use __FreeBSD_version */ |
| 178 | #ifdef HAVE_SYS_PARAM_H |
| 179 | #include <sys/param.h> |
| 180 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 181 | /* 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] | 182 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 183 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 184 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 185 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 186 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 187 | #define USE_GETADDRINFO_LOCK |
| 188 | #endif |
| 189 | |
| 190 | #ifdef USE_GETADDRINFO_LOCK |
| 191 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 192 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 193 | #else |
| 194 | #define ACQUIRE_GETADDRINFO_LOCK |
| 195 | #define RELEASE_GETADDRINFO_LOCK |
| 196 | #endif |
| 197 | |
| 198 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 199 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 200 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 202 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 203 | # include <types.h> |
| 204 | # include <io.h> |
| 205 | # include <sys/ioctl.h> |
| 206 | # include <utils.h> |
| 207 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 208 | #endif |
| 209 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 210 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 211 | # include <ioctl.h> |
| 212 | #endif |
| 213 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 214 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 215 | # define INCL_DOS |
| 216 | # define INCL_DOSERRORS |
| 217 | # define INCL_NOPMAPI |
| 218 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 221 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 222 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 223 | functions are declared correctly if compiling with |
| 224 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 225 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 226 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 227 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 228 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 229 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 230 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 231 | #undef _XOPEN_SOURCE |
| 232 | #include <sys/socket.h> |
| 233 | #include <sys/types.h> |
| 234 | #include <netinet/in.h> |
| 235 | #ifdef _SS_ALIGNSIZE |
| 236 | #define HAVE_GETADDRINFO 1 |
| 237 | #define HAVE_GETNAMEINFO 1 |
| 238 | #endif |
| 239 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 240 | #define HAVE_INET_PTON |
| 241 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 242 | #endif |
| 243 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 244 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 245 | 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] | 246 | are just busted. Same thing for Solaris. */ |
| 247 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 248 | #define INET_ADDRSTRLEN 16 |
| 249 | #endif |
| 250 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 251 | /* Generic includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 252 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 254 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 255 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 256 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 257 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 258 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 259 | |
| 260 | /* Addressing includes */ |
| 261 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 262 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 263 | |
| 264 | /* Non-MS WINDOWS includes */ |
| 265 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 267 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 268 | # ifdef __BEOS__ |
| 269 | # include <net/netdb.h> |
| 270 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 271 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 272 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 273 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 274 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 275 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 276 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 277 | # ifndef RISCOS |
| 278 | # include <fcntl.h> |
| 279 | # else |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 280 | # include <sys/ioctl.h> |
| 281 | # include <socklib.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 282 | # define NO_DUP |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 283 | int h_errno; /* not used */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 284 | # define INET_ADDRSTRLEN 16 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 285 | # endif |
| 286 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 287 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 288 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 289 | /* MS_WINDOWS includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 290 | # ifdef HAVE_FCNTL_H |
| 291 | # include <fcntl.h> |
| 292 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 293 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 294 | #endif |
| 295 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 296 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 297 | |
| 298 | #ifndef offsetof |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 299 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 300 | #endif |
| 301 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 302 | #ifndef O_NONBLOCK |
| 303 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 304 | #endif |
| 305 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 306 | /* include Python's addrinfo.h unless it causes trouble */ |
| 307 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 308 | /* Do not include addinfo.h on some newer IRIX versions. |
| 309 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 310 | * for example, but not by 6.5.10. |
| 311 | */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 312 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 313 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 314 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 315 | */ |
| 316 | #else |
| 317 | # include "addrinfo.h" |
| 318 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 319 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 320 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 321 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 322 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 323 | 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] | 324 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 325 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 326 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 327 | #ifdef __APPLE__ |
| 328 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 329 | failure, so we must use the emulation instead of the libinfo |
| 330 | implementation. Unfortunately, performing an autoconf test |
| 331 | for this bug would require DNS access for the machine performing |
| 332 | the configuration, which is not acceptable. Therefore, we |
| 333 | determine the bug just by checking for __APPLE__. If this bug |
| 334 | gets ever fixed, perhaps checking for sys/version.h would be |
| 335 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 336 | #ifndef HAVE_GETNAMEINFO |
| 337 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 338 | Find to check for Jaguar is that it has getnameinfo(), which |
| 339 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 340 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 341 | #endif |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 342 | |
| 343 | #ifdef HAVE_INET_ATON |
| 344 | #define USE_INET_ATON_WEAKLINK |
| 345 | #endif |
| 346 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 347 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 348 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 349 | /* 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] | 350 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 351 | /* avoid clashes with the C library definition of the symbol. */ |
| 352 | #define getaddrinfo fake_getaddrinfo |
| 353 | #define gai_strerror fake_gai_strerror |
| 354 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 355 | #include "getaddrinfo.c" |
| 356 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 357 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 358 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 359 | #include "getnameinfo.c" |
| 360 | #endif |
| 361 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 362 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 363 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 364 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 365 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 366 | #define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 367 | #endif |
| 368 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 369 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 370 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 371 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 372 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 373 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 374 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 375 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 376 | #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] | 377 | #endif |
| 378 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 379 | #ifndef SOCKETCLOSE |
| 380 | #define SOCKETCLOSE close |
| 381 | #endif |
| 382 | |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 383 | #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 384 | #define USE_BLUETOOTH 1 |
| 385 | #if defined(__FreeBSD__) |
| 386 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 387 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 388 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 389 | #define SOL_HCI SOL_HCI_RAW |
| 390 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 391 | #define sockaddr_l2 sockaddr_l2cap |
| 392 | #define sockaddr_rc sockaddr_rfcomm |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 393 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 394 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 395 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 396 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 397 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 398 | #define sockaddr_l2 sockaddr_bt |
| 399 | #define sockaddr_rc sockaddr_bt |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 400 | #define sockaddr_hci sockaddr_bt |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 401 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 402 | #define SOL_HCI BTPROTO_HCI |
| 403 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 404 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 405 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 406 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 407 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 408 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 409 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 410 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 411 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 412 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 413 | #endif |
| 414 | #endif |
| 415 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 416 | #ifdef __VMS |
| 417 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 418 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 419 | #endif |
| 420 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 421 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 422 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 423 | /* |
| 424 | * Constants for getnameinfo() |
| 425 | */ |
| 426 | #if !defined(NI_MAXHOST) |
| 427 | #define NI_MAXHOST 1025 |
| 428 | #endif |
| 429 | #if !defined(NI_MAXSERV) |
| 430 | #define NI_MAXSERV 32 |
| 431 | #endif |
| 432 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 433 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 434 | a Py prefix (or use CapitalizedWords). Later... */ |
| 435 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 436 | /* Global variable holding the exception type for errors detected |
| 437 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 438 | static PyObject *socket_error; |
| 439 | static PyObject *socket_herror; |
| 440 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 441 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 442 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 443 | #ifdef RISCOS |
| 444 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 445 | static int taskwindow; |
| 446 | #endif |
| 447 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 448 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 449 | The sock_type variable contains pointers to various functions, |
| 450 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 451 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 452 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 453 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 454 | #if defined(HAVE_POLL_H) |
| 455 | #include <poll.h> |
| 456 | #elif defined(HAVE_SYS_POLL_H) |
| 457 | #include <sys/poll.h> |
| 458 | #endif |
| 459 | |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 460 | #ifdef HAVE_POLL |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 461 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 462 | #define IS_SELECTABLE(s) 1 |
| 463 | /* 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] | 464 | #else |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 465 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 466 | * little white lie. */ |
| 467 | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 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 | c83ea13 | 2010-05-09 14:46:46 +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 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 477 | #ifdef MS_WINDOWS |
| 478 | #ifndef WSAEAGAIN |
| 479 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 480 | #endif |
| 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 | c83ea13 | 2010-05-09 14:46:46 +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 | c83ea13 | 2010-05-09 14:46:46 +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 | c83ea13 | 2010-05-09 14:46:46 +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 | c83ea13 | 2010-05-09 14:46:46 +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 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 539 | #if defined(RISCOS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 540 | if (_inet_error.errnum != NULL) { |
| 541 | PyObject *v; |
| 542 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 543 | if (v != NULL) { |
| 544 | PyErr_SetObject(socket_error, v); |
| 545 | Py_DECREF(v); |
| 546 | } |
| 547 | return NULL; |
| 548 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 549 | #endif |
| 550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 551 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 554 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 555 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 556 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 557 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 558 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 559 | |
| 560 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 561 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 562 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 563 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 564 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 565 | if (v != NULL) { |
| 566 | PyErr_SetObject(socket_herror, v); |
| 567 | Py_DECREF(v); |
| 568 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 569 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 570 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
| 574 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 575 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 576 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 577 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 578 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 579 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 580 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 581 | if (error == EAI_SYSTEM) |
| 582 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 583 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 584 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 585 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 586 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 587 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 588 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 589 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 590 | if (v != NULL) { |
| 591 | PyErr_SetObject(socket_gaierror, v); |
| 592 | Py_DECREF(v); |
| 593 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 595 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 598 | #ifdef __VMS |
| 599 | /* Function to send in segments */ |
| 600 | static int |
| 601 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 602 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 603 | int n = 0; |
| 604 | int remaining = len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 606 | while (remaining > 0) { |
| 607 | unsigned int segment; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 608 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 609 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 610 | n = send(sock_fd, buf, segment, flags); |
| 611 | if (n < 0) { |
| 612 | return n; |
| 613 | } |
| 614 | remaining -= segment; |
| 615 | buf += segment; |
| 616 | } /* end while */ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 618 | return len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 619 | } |
| 620 | #endif |
| 621 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 622 | /* Function to perform the setting of socket blocking mode |
| 623 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 624 | static int |
| 625 | internal_setblocking(PySocketSockObject *s, int block) |
| 626 | { |
| 627 | #ifndef RISCOS |
| 628 | #ifndef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 629 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 630 | #endif |
| 631 | #endif |
| 632 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 633 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 634 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 635 | block = !block; |
| 636 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 637 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 638 | #else |
| 639 | #ifndef RISCOS |
| 640 | #ifndef MS_WINDOWS |
| 641 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 642 | block = !block; |
| 643 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 644 | #elif defined(__VMS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 645 | block = !block; |
| 646 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 647 | #else /* !PYOS_OS2 && !__VMS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 648 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 649 | if (block) |
| 650 | delay_flag &= (~O_NONBLOCK); |
| 651 | else |
| 652 | delay_flag |= O_NONBLOCK; |
| 653 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 654 | #endif /* !PYOS_OS2 */ |
| 655 | #else /* MS_WINDOWS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 656 | block = !block; |
| 657 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 659 | #else /* RISCOS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 660 | block = !block; |
| 661 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 662 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 663 | #endif /* __BEOS__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 664 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 666 | /* Since these don't return anything */ |
| 667 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 670 | /* 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] | 671 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 672 | This does not raise an exception; we'll let our caller do that |
| 673 | after they've reacquired the interpreter lock. |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 674 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 675 | static int |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 676 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 677 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 678 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 680 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 681 | if (s->sock_timeout <= 0.0) |
| 682 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 684 | /* Guard against closed socket */ |
| 685 | if (s->sock_fd < 0) |
| 686 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 687 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 688 | /* Handling this condition here simplifies the select loops */ |
| 689 | if (interval < 0.0) |
| 690 | return 1; |
| 691 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 692 | /* Prefer poll, if available, since you can poll() any fd |
| 693 | * which can't be done with select(). */ |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 694 | #ifdef HAVE_POLL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 695 | { |
| 696 | struct pollfd pollfd; |
| 697 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 699 | pollfd.fd = s->sock_fd; |
| 700 | pollfd.events = writing ? POLLOUT : POLLIN; |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 702 | /* s->sock_timeout is in seconds, timeout in ms */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 703 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 704 | n = poll(&pollfd, 1, timeout); |
| 705 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 706 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 707 | { |
| 708 | /* Construct the arguments to select */ |
| 709 | fd_set fds; |
| 710 | struct timeval tv; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 711 | tv.tv_sec = (int)interval; |
| 712 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 713 | FD_ZERO(&fds); |
| 714 | FD_SET(s->sock_fd, &fds); |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 716 | /* See if the socket is ready */ |
| 717 | if (writing) |
| 718 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 719 | else |
| 720 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 721 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 722 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 723 | |
| 724 | if (n < 0) |
| 725 | return -1; |
| 726 | if (n == 0) |
| 727 | return 1; |
| 728 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 731 | static int |
| 732 | internal_select(PySocketSockObject *s, int writing) |
| 733 | { |
| 734 | return internal_select_ex(s, writing, s->sock_timeout); |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | Two macros for automatic retry of select() in case of false positives |
| 739 | (for example, select() could indicate a socket is ready for reading |
| 740 | but the data then discarded by the OS because of a wrong checksum). |
| 741 | Here is an example of use: |
| 742 | |
| 743 | BEGIN_SELECT_LOOP(s) |
| 744 | Py_BEGIN_ALLOW_THREADS |
| 745 | timeout = internal_select_ex(s, 0, interval); |
| 746 | if (!timeout) |
| 747 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 748 | Py_END_ALLOW_THREADS |
| 749 | if (timeout == 1) { |
| 750 | PyErr_SetString(socket_timeout, "timed out"); |
| 751 | return -1; |
| 752 | } |
| 753 | END_SELECT_LOOP(s) |
| 754 | */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 755 | #define BEGIN_SELECT_LOOP(s) \ |
| 756 | { \ |
| 757 | double deadline, interval = s->sock_timeout; \ |
| 758 | int has_timeout = s->sock_timeout > 0.0; \ |
| 759 | if (has_timeout) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 760 | deadline = _PyTime_FloatTime() + s->sock_timeout; \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 761 | } \ |
| 762 | while (1) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 763 | errno = 0; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 764 | |
| 765 | #define END_SELECT_LOOP(s) \ |
| 766 | if (!has_timeout || \ |
| 767 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 768 | break; \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 769 | interval = deadline - _PyTime_FloatTime(); \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 770 | } \ |
Kristján Valur Jónsson | 620e364 | 2013-03-19 13:01:05 -0700 | [diff] [blame] | 771 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 772 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 773 | /* Initialize a new socket object. */ |
| 774 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 775 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 776 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 777 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 778 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 779 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 780 | { |
| 781 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 782 | int block = 1; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 783 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 784 | s->sock_fd = fd; |
| 785 | s->sock_family = family; |
| 786 | s->sock_type = type; |
| 787 | s->sock_proto = proto; |
| 788 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 789 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 790 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 791 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 792 | if (defaulttimeout >= 0.0) |
| 793 | internal_setblocking(s, 0); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 794 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 795 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 796 | if (taskwindow) |
| 797 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 798 | #endif |
| 799 | } |
| 800 | |
| 801 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 802 | /* Create a new socket object. |
| 803 | This just creates the object and initializes it. |
| 804 | If the creation fails, return NULL and set an exception (implicit |
| 805 | in NEWOBJ()). */ |
| 806 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 807 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 808 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 809 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 810 | PySocketSockObject *s; |
| 811 | s = (PySocketSockObject *) |
| 812 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 813 | if (s != NULL) |
| 814 | init_sockobject(s, fd, family, type, proto); |
| 815 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 818 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 819 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 820 | thread to be in gethostbyname or getaddrinfo */ |
| 821 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | c551776 | 2012-08-14 17:24:47 +0200 | [diff] [blame] | 822 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 823 | #endif |
| 824 | |
| 825 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 826 | /* Convert a string specifying a host name or one of a few symbolic |
| 827 | names to a numeric IP address. This usually calls gethostbyname() |
| 828 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 829 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 830 | an error occurred; then an exception is raised. */ |
| 831 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 832 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 833 | 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] | 834 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 835 | struct addrinfo hints, *res; |
| 836 | int error; |
| 837 | int d1, d2, d3, d4; |
| 838 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 839 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 840 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 841 | if (name[0] == '\0') { |
| 842 | int siz; |
| 843 | memset(&hints, 0, sizeof(hints)); |
| 844 | hints.ai_family = af; |
| 845 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 846 | hints.ai_flags = AI_PASSIVE; |
| 847 | Py_BEGIN_ALLOW_THREADS |
| 848 | ACQUIRE_GETADDRINFO_LOCK |
| 849 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 850 | Py_END_ALLOW_THREADS |
| 851 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 852 | *are* safe regarding their return value, ie. that a |
| 853 | subsequent call to getaddrinfo() does not destroy the |
| 854 | outcome of the first call. */ |
| 855 | RELEASE_GETADDRINFO_LOCK |
| 856 | if (error) { |
| 857 | set_gaierror(error); |
| 858 | return -1; |
| 859 | } |
| 860 | switch (res->ai_family) { |
| 861 | case AF_INET: |
| 862 | siz = 4; |
| 863 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 864 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 865 | case AF_INET6: |
| 866 | siz = 16; |
| 867 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 868 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 869 | default: |
| 870 | freeaddrinfo(res); |
| 871 | PyErr_SetString(socket_error, |
| 872 | "unsupported address family"); |
| 873 | return -1; |
| 874 | } |
| 875 | if (res->ai_next) { |
| 876 | freeaddrinfo(res); |
| 877 | PyErr_SetString(socket_error, |
| 878 | "wildcard resolved to multiple address"); |
| 879 | return -1; |
| 880 | } |
| 881 | if (res->ai_addrlen < addr_ret_size) |
| 882 | addr_ret_size = res->ai_addrlen; |
| 883 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 884 | freeaddrinfo(res); |
| 885 | return siz; |
| 886 | } |
| 887 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 888 | struct sockaddr_in *sin; |
| 889 | if (af != AF_INET && af != AF_UNSPEC) { |
| 890 | PyErr_SetString(socket_error, |
| 891 | "address family mismatched"); |
| 892 | return -1; |
| 893 | } |
| 894 | sin = (struct sockaddr_in *)addr_ret; |
| 895 | memset((void *) sin, '\0', sizeof(*sin)); |
| 896 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 897 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 898 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 899 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 900 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 901 | return sizeof(sin->sin_addr); |
| 902 | } |
| 903 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 904 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 905 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 906 | struct sockaddr_in *sin; |
| 907 | sin = (struct sockaddr_in *)addr_ret; |
| 908 | sin->sin_addr.s_addr = htonl( |
| 909 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 910 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 911 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 912 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 913 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 914 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 915 | return 4; |
| 916 | } |
| 917 | memset(&hints, 0, sizeof(hints)); |
| 918 | hints.ai_family = af; |
| 919 | Py_BEGIN_ALLOW_THREADS |
| 920 | ACQUIRE_GETADDRINFO_LOCK |
| 921 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 922 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 923 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 924 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 925 | if no address family is given. Assume IPv4 for now.*/ |
| 926 | hints.ai_family = AF_INET; |
| 927 | error = getaddrinfo(name, NULL, &hints, &res); |
| 928 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 929 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 930 | Py_END_ALLOW_THREADS |
| 931 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 932 | if (error) { |
| 933 | set_gaierror(error); |
| 934 | return -1; |
| 935 | } |
| 936 | if (res->ai_addrlen < addr_ret_size) |
| 937 | addr_ret_size = res->ai_addrlen; |
| 938 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 939 | freeaddrinfo(res); |
| 940 | switch (addr_ret->sa_family) { |
| 941 | case AF_INET: |
| 942 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 943 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 944 | case AF_INET6: |
| 945 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 946 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 947 | default: |
| 948 | PyErr_SetString(socket_error, "unknown address family"); |
| 949 | return -1; |
| 950 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 953 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 954 | /* Create a string object representing an IP address. |
| 955 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 956 | size numbers). */ |
| 957 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 958 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 959 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 960 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 961 | char buf[NI_MAXHOST]; |
| 962 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 963 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 964 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 965 | NI_NUMERICHOST); |
| 966 | if (error) { |
| 967 | set_gaierror(error); |
| 968 | return NULL; |
| 969 | } |
| 970 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 974 | #ifdef USE_BLUETOOTH |
| 975 | /* Convert a string representation of a Bluetooth address into a numeric |
| 976 | address. Returns the length (6), or raises an exception and returns -1 if |
| 977 | an error occurred. */ |
| 978 | |
| 979 | static int |
| 980 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 981 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 982 | unsigned int b0, b1, b2, b3, b4, b5; |
| 983 | char ch; |
| 984 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 986 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 987 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 988 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 989 | bdaddr->b[0] = b0; |
| 990 | bdaddr->b[1] = b1; |
| 991 | bdaddr->b[2] = b2; |
| 992 | bdaddr->b[3] = b3; |
| 993 | bdaddr->b[4] = b4; |
| 994 | bdaddr->b[5] = b5; |
| 995 | return 6; |
| 996 | } else { |
| 997 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 998 | return -1; |
| 999 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* Create a string representation of the Bluetooth address. This is always a |
| 1003 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 1004 | value (zero padded if necessary). */ |
| 1005 | |
| 1006 | static PyObject * |
| 1007 | makebdaddr(bdaddr_t *bdaddr) |
| 1008 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1009 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1011 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1012 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 1013 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 1014 | return PyString_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1015 | } |
| 1016 | #endif |
| 1017 | |
| 1018 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1019 | /* Create an object representing the given socket address, |
| 1020 | suitable for passing it back to bind(), connect() etc. |
| 1021 | The family field of the sockaddr structure is inspected |
| 1022 | to determine what kind of address it really is. */ |
| 1023 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1024 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1025 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1026 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1027 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1028 | if (addrlen == 0) { |
| 1029 | /* No address -- may be recvfrom() from known socket */ |
| 1030 | Py_INCREF(Py_None); |
| 1031 | return Py_None; |
| 1032 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1033 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1034 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1035 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
| 1036 | addr->sa_family = AF_INET; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1037 | #endif |
| 1038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1039 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1040 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1041 | case AF_INET: |
| 1042 | { |
| 1043 | struct sockaddr_in *a; |
| 1044 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1045 | PyObject *ret = NULL; |
| 1046 | if (addrobj) { |
| 1047 | a = (struct sockaddr_in *)addr; |
| 1048 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1049 | Py_DECREF(addrobj); |
| 1050 | } |
| 1051 | return ret; |
| 1052 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1053 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1054 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1055 | case AF_UNIX: |
| 1056 | { |
| 1057 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1058 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1059 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1060 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 1061 | return PyString_FromStringAndSize(a->sun_path, |
| 1062 | addrlen); |
| 1063 | } |
| 1064 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1065 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1066 | { |
| 1067 | /* regular NULL-terminated string */ |
| 1068 | return PyString_FromString(a->sun_path); |
| 1069 | } |
| 1070 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1071 | #endif /* AF_UNIX */ |
| 1072 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1073 | #if defined(AF_NETLINK) |
| 1074 | case AF_NETLINK: |
| 1075 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1076 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1077 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1078 | } |
| 1079 | #endif /* AF_NETLINK */ |
| 1080 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1081 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1082 | case AF_INET6: |
| 1083 | { |
| 1084 | struct sockaddr_in6 *a; |
| 1085 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1086 | PyObject *ret = NULL; |
| 1087 | if (addrobj) { |
| 1088 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1089 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1090 | addrobj, |
| 1091 | ntohs(a->sin6_port), |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1092 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1093 | a->sin6_scope_id); |
| 1094 | Py_DECREF(addrobj); |
| 1095 | } |
| 1096 | return ret; |
| 1097 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1098 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1099 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1100 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1101 | case AF_BLUETOOTH: |
| 1102 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1103 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1104 | case BTPROTO_L2CAP: |
| 1105 | { |
| 1106 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1107 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1108 | PyObject *ret = NULL; |
| 1109 | if (addrobj) { |
| 1110 | ret = Py_BuildValue("Oi", |
| 1111 | addrobj, |
| 1112 | _BT_L2_MEMB(a, psm)); |
| 1113 | Py_DECREF(addrobj); |
| 1114 | } |
| 1115 | return ret; |
| 1116 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1117 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1118 | case BTPROTO_RFCOMM: |
| 1119 | { |
| 1120 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1121 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1122 | PyObject *ret = NULL; |
| 1123 | if (addrobj) { |
| 1124 | ret = Py_BuildValue("Oi", |
| 1125 | addrobj, |
| 1126 | _BT_RC_MEMB(a, channel)); |
| 1127 | Py_DECREF(addrobj); |
| 1128 | } |
| 1129 | return ret; |
| 1130 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1131 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1132 | case BTPROTO_HCI: |
| 1133 | { |
| 1134 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1135 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1136 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1137 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1138 | PyObject *ret = NULL; |
| 1139 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1140 | return ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1141 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1142 | } |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1143 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1144 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1145 | case BTPROTO_SCO: |
| 1146 | { |
| 1147 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1148 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1149 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1150 | #endif |
| 1151 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1152 | default: |
| 1153 | PyErr_SetString(PyExc_ValueError, |
| 1154 | "Unknown Bluetooth protocol"); |
| 1155 | return NULL; |
| 1156 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1157 | #endif |
| 1158 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1159 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1160 | case AF_PACKET: |
| 1161 | { |
| 1162 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1163 | char *ifname = ""; |
| 1164 | struct ifreq ifr; |
| 1165 | /* need to look up interface name give index */ |
| 1166 | if (a->sll_ifindex) { |
| 1167 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1168 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1169 | ifname = ifr.ifr_name; |
| 1170 | } |
| 1171 | return Py_BuildValue("shbhs#", |
| 1172 | ifname, |
| 1173 | ntohs(a->sll_protocol), |
| 1174 | a->sll_pkttype, |
| 1175 | a->sll_hatype, |
| 1176 | a->sll_addr, |
| 1177 | a->sll_halen); |
| 1178 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1179 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1180 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1181 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1182 | case AF_TIPC: |
| 1183 | { |
| 1184 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1185 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1186 | return Py_BuildValue("IIIII", |
| 1187 | a->addrtype, |
| 1188 | a->addr.nameseq.type, |
| 1189 | a->addr.nameseq.lower, |
| 1190 | a->addr.nameseq.upper, |
| 1191 | a->scope); |
| 1192 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1193 | return Py_BuildValue("IIIII", |
| 1194 | a->addrtype, |
| 1195 | a->addr.name.name.type, |
| 1196 | a->addr.name.name.instance, |
| 1197 | a->addr.name.name.instance, |
| 1198 | a->scope); |
| 1199 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1200 | return Py_BuildValue("IIIII", |
| 1201 | a->addrtype, |
| 1202 | a->addr.id.node, |
| 1203 | a->addr.id.ref, |
| 1204 | 0, |
| 1205 | a->scope); |
| 1206 | } else { |
| 1207 | PyErr_SetString(PyExc_ValueError, |
| 1208 | "Invalid address type"); |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1212 | #endif |
| 1213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1214 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1216 | default: |
| 1217 | /* If we don't know the address family, don't raise an |
| 1218 | exception -- return it as a tuple. */ |
| 1219 | return Py_BuildValue("is#", |
| 1220 | addr->sa_family, |
| 1221 | addr->sa_data, |
| 1222 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1223 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1224 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1227 | |
| 1228 | /* Parse a socket address argument according to the socket object's |
| 1229 | address family. Return 1 if the address was in the proper format, |
| 1230 | 0 of not. The address is returned through addr_ret, its length |
| 1231 | through len_ret. */ |
| 1232 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1233 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1234 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1235 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1236 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1237 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1238 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1239 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1240 | case AF_UNIX: |
| 1241 | { |
| 1242 | struct sockaddr_un* addr; |
| 1243 | char *path; |
| 1244 | int len; |
| 1245 | if (!PyArg_Parse(args, "t#", &path, &len)) |
| 1246 | return 0; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1247 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1248 | addr = (struct sockaddr_un*)addr_ret; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1249 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1250 | if (len > 0 && path[0] == 0) { |
| 1251 | /* Linux abstract namespace extension */ |
| 1252 | if (len > sizeof addr->sun_path) { |
| 1253 | PyErr_SetString(socket_error, |
| 1254 | "AF_UNIX path too long"); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | } |
| 1258 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1259 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1260 | { |
| 1261 | /* regular NULL-terminated string */ |
| 1262 | if (len >= sizeof addr->sun_path) { |
| 1263 | PyErr_SetString(socket_error, |
| 1264 | "AF_UNIX path too long"); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | addr->sun_path[len] = 0; |
| 1268 | } |
| 1269 | addr->sun_family = s->sock_family; |
| 1270 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1271 | #if defined(PYOS_OS2) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1272 | *len_ret = sizeof(*addr); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1273 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1274 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1275 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1276 | return 1; |
| 1277 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1278 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1279 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1280 | #if defined(AF_NETLINK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1281 | case AF_NETLINK: |
| 1282 | { |
| 1283 | struct sockaddr_nl* addr; |
| 1284 | int pid, groups; |
| 1285 | addr = (struct sockaddr_nl *)addr_ret; |
| 1286 | if (!PyTuple_Check(args)) { |
| 1287 | PyErr_Format( |
| 1288 | PyExc_TypeError, |
| 1289 | "getsockaddrarg: " |
| 1290 | "AF_NETLINK address must be tuple, not %.500s", |
| 1291 | Py_TYPE(args)->tp_name); |
| 1292 | return 0; |
| 1293 | } |
| 1294 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1295 | return 0; |
| 1296 | addr->nl_family = AF_NETLINK; |
| 1297 | addr->nl_pid = pid; |
| 1298 | addr->nl_groups = groups; |
| 1299 | *len_ret = sizeof(*addr); |
| 1300 | return 1; |
| 1301 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1302 | #endif |
| 1303 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1304 | case AF_INET: |
| 1305 | { |
| 1306 | struct sockaddr_in* addr; |
| 1307 | char *host; |
| 1308 | int port, result; |
| 1309 | if (!PyTuple_Check(args)) { |
| 1310 | PyErr_Format( |
| 1311 | PyExc_TypeError, |
| 1312 | "getsockaddrarg: " |
| 1313 | "AF_INET address must be tuple, not %.500s", |
| 1314 | Py_TYPE(args)->tp_name); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1318 | "idna", &host, &port)) |
| 1319 | return 0; |
| 1320 | addr=(struct sockaddr_in*)addr_ret; |
| 1321 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1322 | sizeof(*addr), AF_INET); |
| 1323 | PyMem_Free(host); |
| 1324 | if (result < 0) |
| 1325 | return 0; |
| 1326 | if (port < 0 || port > 0xffff) { |
| 1327 | PyErr_SetString( |
| 1328 | PyExc_OverflowError, |
| 1329 | "getsockaddrarg: port must be 0-65535."); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | addr->sin_family = AF_INET; |
| 1333 | addr->sin_port = htons((short)port); |
| 1334 | *len_ret = sizeof *addr; |
| 1335 | return 1; |
| 1336 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1337 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1338 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1339 | case AF_INET6: |
| 1340 | { |
| 1341 | struct sockaddr_in6* addr; |
| 1342 | char *host; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1343 | int port, result; |
| 1344 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1345 | flowinfo = scope_id = 0; |
| 1346 | if (!PyTuple_Check(args)) { |
| 1347 | PyErr_Format( |
| 1348 | PyExc_TypeError, |
| 1349 | "getsockaddrarg: " |
| 1350 | "AF_INET6 address must be tuple, not %.500s", |
| 1351 | Py_TYPE(args)->tp_name); |
| 1352 | return 0; |
| 1353 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1354 | if (!PyArg_ParseTuple(args, "eti|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1355 | "idna", &host, &port, &flowinfo, |
| 1356 | &scope_id)) { |
| 1357 | return 0; |
| 1358 | } |
| 1359 | addr = (struct sockaddr_in6*)addr_ret; |
| 1360 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1361 | sizeof(*addr), AF_INET6); |
| 1362 | PyMem_Free(host); |
| 1363 | if (result < 0) |
| 1364 | return 0; |
| 1365 | if (port < 0 || port > 0xffff) { |
| 1366 | PyErr_SetString( |
| 1367 | PyExc_OverflowError, |
| 1368 | "getsockaddrarg: port must be 0-65535."); |
| 1369 | return 0; |
| 1370 | } |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 1371 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1372 | PyErr_SetString( |
| 1373 | PyExc_OverflowError, |
| 1374 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1375 | return 0; |
| 1376 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1377 | addr->sin6_family = s->sock_family; |
| 1378 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1379 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1380 | addr->sin6_scope_id = scope_id; |
| 1381 | *len_ret = sizeof *addr; |
| 1382 | return 1; |
| 1383 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1384 | #endif |
| 1385 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1386 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1387 | case AF_BLUETOOTH: |
| 1388 | { |
| 1389 | switch (s->sock_proto) { |
| 1390 | case BTPROTO_L2CAP: |
| 1391 | { |
| 1392 | struct sockaddr_l2 *addr; |
| 1393 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1394 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1395 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1396 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1397 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1398 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1399 | &_BT_L2_MEMB(addr, psm))) { |
| 1400 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1401 | "wrong format"); |
| 1402 | return 0; |
| 1403 | } |
| 1404 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1405 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1406 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1407 | *len_ret = sizeof *addr; |
| 1408 | return 1; |
| 1409 | } |
| 1410 | case BTPROTO_RFCOMM: |
| 1411 | { |
| 1412 | struct sockaddr_rc *addr; |
| 1413 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1414 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1415 | addr = (struct sockaddr_rc *)addr_ret; |
| 1416 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1417 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1418 | &_BT_RC_MEMB(addr, channel))) { |
| 1419 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1420 | "wrong format"); |
| 1421 | return 0; |
| 1422 | } |
| 1423 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1424 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1425 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1426 | *len_ret = sizeof *addr; |
| 1427 | return 1; |
| 1428 | } |
| 1429 | case BTPROTO_HCI: |
| 1430 | { |
| 1431 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1432 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1433 | char *straddr = PyBytes_AS_STRING(args); |
| 1434 | |
| 1435 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1436 | if (straddr == NULL) { |
| 1437 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1438 | "wrong format"); |
| 1439 | return 0; |
| 1440 | } |
| 1441 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1442 | return 0; |
| 1443 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1444 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1445 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1446 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1447 | "wrong format"); |
| 1448 | return 0; |
| 1449 | } |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1450 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1451 | *len_ret = sizeof *addr; |
| 1452 | return 1; |
| 1453 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1454 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1455 | case BTPROTO_SCO: |
| 1456 | { |
| 1457 | struct sockaddr_sco *addr; |
| 1458 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1459 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1460 | addr = (struct sockaddr_sco *)addr_ret; |
| 1461 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1462 | straddr = PyString_AsString(args); |
| 1463 | if (straddr == NULL) { |
| 1464 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1465 | "wrong format"); |
| 1466 | return 0; |
| 1467 | } |
| 1468 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1469 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1471 | *len_ret = sizeof *addr; |
| 1472 | return 1; |
| 1473 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1474 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1475 | default: |
| 1476 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1477 | return 0; |
| 1478 | } |
| 1479 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1480 | #endif |
| 1481 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1482 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1483 | case AF_PACKET: |
| 1484 | { |
| 1485 | struct sockaddr_ll* addr; |
| 1486 | struct ifreq ifr; |
| 1487 | char *interfaceName; |
| 1488 | int protoNumber; |
| 1489 | int hatype = 0; |
| 1490 | int pkttype = 0; |
| 1491 | char *haddr = NULL; |
| 1492 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1494 | if (!PyTuple_Check(args)) { |
| 1495 | PyErr_Format( |
| 1496 | PyExc_TypeError, |
| 1497 | "getsockaddrarg: " |
| 1498 | "AF_PACKET address must be tuple, not %.500s", |
| 1499 | Py_TYPE(args)->tp_name); |
| 1500 | return 0; |
| 1501 | } |
| 1502 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1503 | &protoNumber, &pkttype, &hatype, |
| 1504 | &haddr, &halen)) |
| 1505 | return 0; |
| 1506 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1507 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1508 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1509 | s->errorhandler(); |
| 1510 | return 0; |
| 1511 | } |
| 1512 | if (halen > 8) { |
| 1513 | PyErr_SetString(PyExc_ValueError, |
| 1514 | "Hardware address must be 8 bytes or less"); |
| 1515 | return 0; |
| 1516 | } |
| 1517 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1518 | PyErr_SetString( |
| 1519 | PyExc_OverflowError, |
| 1520 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1521 | return 0; |
| 1522 | } |
| 1523 | addr = (struct sockaddr_ll*)addr_ret; |
| 1524 | addr->sll_family = AF_PACKET; |
| 1525 | addr->sll_protocol = htons((short)protoNumber); |
| 1526 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1527 | addr->sll_pkttype = pkttype; |
| 1528 | addr->sll_hatype = hatype; |
| 1529 | if (halen != 0) { |
| 1530 | memcpy(&addr->sll_addr, haddr, halen); |
| 1531 | } |
| 1532 | addr->sll_halen = halen; |
| 1533 | *len_ret = sizeof *addr; |
| 1534 | return 1; |
| 1535 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1536 | #endif |
| 1537 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1538 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1539 | case AF_TIPC: |
| 1540 | { |
| 1541 | unsigned int atype, v1, v2, v3; |
| 1542 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1543 | struct sockaddr_tipc *addr; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1544 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1545 | if (!PyTuple_Check(args)) { |
| 1546 | PyErr_Format( |
| 1547 | PyExc_TypeError, |
| 1548 | "getsockaddrarg: " |
| 1549 | "AF_TIPC address must be tuple, not %.500s", |
| 1550 | Py_TYPE(args)->tp_name); |
| 1551 | return 0; |
| 1552 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1553 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1554 | if (!PyArg_ParseTuple(args, |
| 1555 | "IIII|I;Invalid TIPC address format", |
| 1556 | &atype, &v1, &v2, &v3, &scope)) |
| 1557 | return 0; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1559 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1560 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1561 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1562 | addr->family = AF_TIPC; |
| 1563 | addr->scope = scope; |
| 1564 | addr->addrtype = atype; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1566 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1567 | addr->addr.nameseq.type = v1; |
| 1568 | addr->addr.nameseq.lower = v2; |
| 1569 | addr->addr.nameseq.upper = v3; |
| 1570 | } else if (atype == TIPC_ADDR_NAME) { |
| 1571 | addr->addr.name.name.type = v1; |
| 1572 | addr->addr.name.name.instance = v2; |
| 1573 | } else if (atype == TIPC_ADDR_ID) { |
| 1574 | addr->addr.id.node = v1; |
| 1575 | addr->addr.id.ref = v2; |
| 1576 | } else { |
| 1577 | /* Shouldn't happen */ |
| 1578 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1579 | return 0; |
| 1580 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1582 | *len_ret = sizeof(*addr); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1583 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1584 | return 1; |
| 1585 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1586 | #endif |
| 1587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1588 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1589 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1590 | default: |
| 1591 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1592 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1594 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1597 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1598 | /* 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] | 1599 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1600 | through len_ret. */ |
| 1601 | |
| 1602 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1603 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1604 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1605 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1606 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1607 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1608 | case AF_UNIX: |
| 1609 | { |
| 1610 | *len_ret = sizeof (struct sockaddr_un); |
| 1611 | return 1; |
| 1612 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1613 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1614 | #if defined(AF_NETLINK) |
| 1615 | case AF_NETLINK: |
| 1616 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1617 | *len_ret = sizeof (struct sockaddr_nl); |
| 1618 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1619 | } |
| 1620 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1622 | case AF_INET: |
| 1623 | { |
| 1624 | *len_ret = sizeof (struct sockaddr_in); |
| 1625 | return 1; |
| 1626 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1627 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1628 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1629 | case AF_INET6: |
| 1630 | { |
| 1631 | *len_ret = sizeof (struct sockaddr_in6); |
| 1632 | return 1; |
| 1633 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1634 | #endif |
| 1635 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1636 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1637 | case AF_BLUETOOTH: |
| 1638 | { |
| 1639 | switch(s->sock_proto) |
| 1640 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1641 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1642 | case BTPROTO_L2CAP: |
| 1643 | *len_ret = sizeof (struct sockaddr_l2); |
| 1644 | return 1; |
| 1645 | case BTPROTO_RFCOMM: |
| 1646 | *len_ret = sizeof (struct sockaddr_rc); |
| 1647 | return 1; |
| 1648 | case BTPROTO_HCI: |
| 1649 | *len_ret = sizeof (struct sockaddr_hci); |
| 1650 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1651 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1652 | case BTPROTO_SCO: |
| 1653 | *len_ret = sizeof (struct sockaddr_sco); |
| 1654 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1655 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1656 | default: |
| 1657 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1658 | "unknown BT protocol"); |
| 1659 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1661 | } |
| 1662 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1663 | #endif |
| 1664 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1665 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1666 | case AF_PACKET: |
| 1667 | { |
| 1668 | *len_ret = sizeof (struct sockaddr_ll); |
| 1669 | return 1; |
| 1670 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1671 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1672 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1673 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1674 | case AF_TIPC: |
| 1675 | { |
| 1676 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1677 | return 1; |
| 1678 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1679 | #endif |
| 1680 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1681 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1682 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1683 | default: |
| 1684 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1685 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1687 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1691 | /* s.accept() method */ |
| 1692 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1693 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1694 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1695 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1696 | sock_addr_t addrbuf; |
| 1697 | SOCKET_T newfd; |
| 1698 | socklen_t addrlen; |
| 1699 | PyObject *sock = NULL; |
| 1700 | PyObject *addr = NULL; |
| 1701 | PyObject *res = NULL; |
| 1702 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1704 | if (!getsockaddrlen(s, &addrlen)) |
| 1705 | return NULL; |
| 1706 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1707 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1708 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1709 | newfd = INVALID_SOCKET; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1710 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1711 | newfd = -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1712 | #endif |
| 1713 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1714 | if (!IS_SELECTABLE(s)) |
| 1715 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1716 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1717 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1718 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1719 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1720 | if (!timeout) |
| 1721 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1722 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1723 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1724 | if (timeout == 1) { |
| 1725 | PyErr_SetString(socket_timeout, "timed out"); |
| 1726 | return NULL; |
| 1727 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1728 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1729 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1730 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1731 | if (newfd == INVALID_SOCKET) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1732 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1733 | if (newfd < 0) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1734 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1735 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1737 | /* Create the new object with unspecified family, |
| 1738 | to avoid calls to bind() etc. on it. */ |
| 1739 | sock = (PyObject *) new_sockobject(newfd, |
| 1740 | s->sock_family, |
| 1741 | s->sock_type, |
| 1742 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1743 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1744 | if (sock == NULL) { |
| 1745 | SOCKETCLOSE(newfd); |
| 1746 | goto finally; |
| 1747 | } |
| 1748 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1749 | addrlen, s->sock_proto); |
| 1750 | if (addr == NULL) |
| 1751 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1752 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1753 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1754 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1755 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1756 | Py_XDECREF(sock); |
| 1757 | Py_XDECREF(addr); |
| 1758 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1761 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1762 | "accept() -> (socket object, address info)\n\ |
| 1763 | \n\ |
| 1764 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1765 | connection, and the address of the client. For IP sockets, the address\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1766 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1767 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1768 | /* s.setblocking(flag) method. Argument: |
| 1769 | False -- non-blocking mode; same as settimeout(0) |
| 1770 | True -- blocking mode; same as settimeout(None) |
| 1771 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1772 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1773 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1774 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1775 | { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 1776 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1778 | block = PyInt_AsLong(arg); |
| 1779 | if (block == -1 && PyErr_Occurred()) |
| 1780 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1781 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1782 | s->sock_timeout = block ? -1.0 : 0.0; |
| 1783 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1784 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1785 | Py_INCREF(Py_None); |
| 1786 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1787 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1789 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1790 | "setblocking(flag)\n\ |
| 1791 | \n\ |
| 1792 | 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] | 1793 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1794 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1795 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1796 | /* s.settimeout(timeout) method. Argument: |
| 1797 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1798 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1799 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1800 | < 0 -- illegal; raises an exception |
| 1801 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1802 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1803 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1804 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1805 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1806 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1807 | if (arg == Py_None) |
| 1808 | timeout = -1.0; |
| 1809 | else { |
| 1810 | timeout = PyFloat_AsDouble(arg); |
| 1811 | if (timeout < 0.0) { |
| 1812 | if (!PyErr_Occurred()) |
| 1813 | PyErr_SetString(PyExc_ValueError, |
| 1814 | "Timeout value out of range"); |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1818 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1819 | s->sock_timeout = timeout; |
| 1820 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1821 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1822 | Py_INCREF(Py_None); |
| 1823 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1826 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1827 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1828 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1829 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1830 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1831 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1832 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1833 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1834 | /* s.gettimeout() method. |
| 1835 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1836 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1837 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1838 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1839 | if (s->sock_timeout < 0.0) { |
| 1840 | Py_INCREF(Py_None); |
| 1841 | return Py_None; |
| 1842 | } |
| 1843 | else |
| 1844 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1847 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1848 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1849 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 1850 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1851 | 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] | 1852 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1853 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1854 | #ifdef RISCOS |
| 1855 | /* s.sleeptaskw(1 | 0) method */ |
| 1856 | |
| 1857 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1858 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1859 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1860 | int block; |
| 1861 | block = PyInt_AsLong(arg); |
| 1862 | if (block == -1 && PyErr_Occurred()) |
| 1863 | return NULL; |
| 1864 | Py_BEGIN_ALLOW_THREADS |
| 1865 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1866 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1867 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1868 | Py_INCREF(Py_None); |
| 1869 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1870 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1871 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1872 | "sleeptaskw(flag)\n\ |
| 1873 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1874 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1875 | #endif |
| 1876 | |
| 1877 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1878 | /* s.setsockopt() method. |
| 1879 | With an integer third argument, sets an integer option. |
| 1880 | With a string third argument, sets an option from a buffer; |
| 1881 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1882 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1883 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1884 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1885 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1886 | int level; |
| 1887 | int optname; |
| 1888 | int res; |
| 1889 | char *buf; |
| 1890 | int buflen; |
| 1891 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1892 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1893 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1894 | &level, &optname, &flag)) { |
| 1895 | buf = (char *) &flag; |
| 1896 | buflen = sizeof flag; |
| 1897 | } |
| 1898 | else { |
| 1899 | PyErr_Clear(); |
| 1900 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1901 | &level, &optname, &buf, &buflen)) |
| 1902 | return NULL; |
| 1903 | } |
| 1904 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1905 | if (res < 0) |
| 1906 | return s->errorhandler(); |
| 1907 | Py_INCREF(Py_None); |
| 1908 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1911 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1912 | "setsockopt(level, option, value)\n\ |
| 1913 | \n\ |
| 1914 | 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] | 1915 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1916 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1917 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1918 | /* s.getsockopt() method. |
| 1919 | With two arguments, retrieves an integer option. |
| 1920 | With a third integer argument, retrieves a string buffer of that size; |
| 1921 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1922 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1923 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1924 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1925 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1926 | int level; |
| 1927 | int optname; |
| 1928 | int res; |
| 1929 | PyObject *buf; |
| 1930 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1931 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1932 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1933 | /* We have incomplete socket support. */ |
| 1934 | PyErr_SetString(socket_error, "getsockopt not supported"); |
| 1935 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1936 | #else |
| 1937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1938 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1939 | &level, &optname, &buflen)) |
| 1940 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1941 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1942 | if (buflen == 0) { |
| 1943 | int flag = 0; |
| 1944 | socklen_t flagsize = sizeof flag; |
| 1945 | res = getsockopt(s->sock_fd, level, optname, |
| 1946 | (void *)&flag, &flagsize); |
| 1947 | if (res < 0) |
| 1948 | return s->errorhandler(); |
| 1949 | return PyInt_FromLong(flag); |
| 1950 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1951 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1952 | /* socklen_t is unsigned so no negative test is needed, |
| 1953 | test buflen == 0 is previously done */ |
| 1954 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1955 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1956 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1957 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1958 | PyErr_SetString(socket_error, |
| 1959 | "getsockopt buflen out of range"); |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
| 1963 | if (buf == NULL) |
| 1964 | return NULL; |
| 1965 | res = getsockopt(s->sock_fd, level, optname, |
| 1966 | (void *)PyString_AS_STRING(buf), &buflen); |
| 1967 | if (res < 0) { |
| 1968 | Py_DECREF(buf); |
| 1969 | return s->errorhandler(); |
| 1970 | } |
| 1971 | _PyString_Resize(&buf, buflen); |
| 1972 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1973 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1976 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1977 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1978 | \n\ |
| 1979 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1980 | 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] | 1981 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1982 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1983 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1984 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1985 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1986 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1987 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1988 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1989 | sock_addr_t addrbuf; |
| 1990 | int addrlen; |
| 1991 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1992 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1993 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1994 | return NULL; |
| 1995 | Py_BEGIN_ALLOW_THREADS |
| 1996 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 1997 | Py_END_ALLOW_THREADS |
| 1998 | if (res < 0) |
| 1999 | return s->errorhandler(); |
| 2000 | Py_INCREF(Py_None); |
| 2001 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2004 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2005 | "bind(address)\n\ |
| 2006 | \n\ |
| 2007 | 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] | 2008 | 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] | 2009 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2010 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2011 | |
| 2012 | /* s.close() method. |
| 2013 | Set the file descriptor to -1 so operations tried subsequently |
| 2014 | will surely fail. */ |
| 2015 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2016 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2017 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2018 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2019 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2020 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2021 | if ((fd = s->sock_fd) != -1) { |
| 2022 | s->sock_fd = -1; |
| 2023 | Py_BEGIN_ALLOW_THREADS |
| 2024 | (void) SOCKETCLOSE(fd); |
| 2025 | Py_END_ALLOW_THREADS |
| 2026 | } |
| 2027 | Py_INCREF(Py_None); |
| 2028 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2032 | "close()\n\ |
| 2033 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2034 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2035 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2036 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2037 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2038 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2039 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2040 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2041 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2042 | timeout = 0; |
| 2043 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2044 | |
| 2045 | #ifdef MS_WINDOWS |
| 2046 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2047 | if (s->sock_timeout > 0.0) { |
| 2048 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2049 | IS_SELECTABLE(s)) { |
| 2050 | /* This is a mess. Best solution: trust select */ |
| 2051 | fd_set fds; |
| 2052 | fd_set fds_exc; |
| 2053 | struct timeval tv; |
| 2054 | tv.tv_sec = (int)s->sock_timeout; |
| 2055 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2056 | FD_ZERO(&fds); |
| 2057 | FD_SET(s->sock_fd, &fds); |
| 2058 | FD_ZERO(&fds_exc); |
| 2059 | FD_SET(s->sock_fd, &fds_exc); |
| 2060 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
| 2061 | if (res == 0) { |
| 2062 | res = WSAEWOULDBLOCK; |
| 2063 | timeout = 1; |
| 2064 | } else if (res > 0) { |
| 2065 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2066 | /* The socket is in the writeable set - this |
| 2067 | means connected */ |
| 2068 | res = 0; |
| 2069 | else { |
| 2070 | /* As per MS docs, we need to call getsockopt() |
| 2071 | to get the underlying error */ |
| 2072 | int res_size = sizeof res; |
| 2073 | /* It must be in the exception set */ |
| 2074 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2075 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2076 | (char *)&res, &res_size)) |
| 2077 | /* getsockopt also clears WSAGetLastError, |
| 2078 | so reset it back. */ |
| 2079 | WSASetLastError(res); |
| 2080 | else |
| 2081 | res = WSAGetLastError(); |
| 2082 | } |
| 2083 | } |
| 2084 | /* else if (res < 0) an error occurred */ |
| 2085 | } |
| 2086 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2087 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2088 | if (res < 0) |
| 2089 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2090 | |
| 2091 | #else |
| 2092 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2093 | if (s->sock_timeout > 0.0) { |
| 2094 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2095 | timeout = internal_select(s, 1); |
| 2096 | if (timeout == 0) { |
| 2097 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2098 | use getsockopt(SO_ERROR) to get the real |
| 2099 | error. */ |
| 2100 | socklen_t res_size = sizeof res; |
| 2101 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2102 | SO_ERROR, &res, &res_size); |
| 2103 | if (res == EISCONN) |
| 2104 | res = 0; |
| 2105 | errno = res; |
| 2106 | } |
| 2107 | else if (timeout == -1) { |
| 2108 | res = errno; /* had error */ |
| 2109 | } |
| 2110 | else |
| 2111 | res = EWOULDBLOCK; /* timed out */ |
| 2112 | } |
| 2113 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2115 | if (res < 0) |
| 2116 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2117 | |
| 2118 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2119 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2121 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2122 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2123 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2124 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2125 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2126 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2127 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2128 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2129 | sock_addr_t addrbuf; |
| 2130 | int addrlen; |
| 2131 | int res; |
| 2132 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2133 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2134 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2135 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2137 | Py_BEGIN_ALLOW_THREADS |
| 2138 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2139 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2140 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2141 | if (timeout == 1) { |
| 2142 | PyErr_SetString(socket_timeout, "timed out"); |
| 2143 | return NULL; |
| 2144 | } |
| 2145 | if (res != 0) |
| 2146 | return s->errorhandler(); |
| 2147 | Py_INCREF(Py_None); |
| 2148 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2151 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2152 | "connect(address)\n\ |
| 2153 | \n\ |
| 2154 | 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] | 2155 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2156 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2157 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2158 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2159 | |
| 2160 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2161 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2162 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2163 | sock_addr_t addrbuf; |
| 2164 | int addrlen; |
| 2165 | int res; |
| 2166 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2168 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2169 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2171 | Py_BEGIN_ALLOW_THREADS |
| 2172 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2173 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2175 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2176 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2177 | #ifdef EINTR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2178 | if (res == EINTR && PyErr_CheckSignals()) |
| 2179 | return NULL; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2180 | #endif |
| 2181 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2182 | return PyInt_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2185 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2186 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2187 | \n\ |
| 2188 | 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] | 2189 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2190 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2191 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2192 | /* s.fileno() method */ |
| 2193 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2194 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2195 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2196 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2197 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2198 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2199 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2200 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2201 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2204 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2205 | "fileno() -> integer\n\ |
| 2206 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2207 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2208 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2210 | #ifndef NO_DUP |
| 2211 | /* s.dup() method */ |
| 2212 | |
| 2213 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2214 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2215 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2216 | SOCKET_T newfd; |
| 2217 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2218 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2219 | newfd = dup(s->sock_fd); |
| 2220 | if (newfd < 0) |
| 2221 | return s->errorhandler(); |
| 2222 | sock = (PyObject *) new_sockobject(newfd, |
| 2223 | s->sock_family, |
| 2224 | s->sock_type, |
| 2225 | s->sock_proto); |
| 2226 | if (sock == NULL) |
| 2227 | SOCKETCLOSE(newfd); |
| 2228 | return sock; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2229 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2231 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2232 | "dup() -> socket object\n\ |
| 2233 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2234 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2235 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2236 | #endif |
| 2237 | |
| 2238 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2239 | /* s.getsockname() method */ |
| 2240 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2241 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2242 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2243 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2244 | sock_addr_t addrbuf; |
| 2245 | int res; |
| 2246 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2247 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2248 | if (!getsockaddrlen(s, &addrlen)) |
| 2249 | return NULL; |
| 2250 | memset(&addrbuf, 0, addrlen); |
| 2251 | Py_BEGIN_ALLOW_THREADS |
| 2252 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2253 | Py_END_ALLOW_THREADS |
| 2254 | if (res < 0) |
| 2255 | return s->errorhandler(); |
| 2256 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2257 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2260 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2261 | "getsockname() -> address info\n\ |
| 2262 | \n\ |
| 2263 | 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] | 2264 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2265 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2266 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2267 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2268 | /* s.getpeername() method */ |
| 2269 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2270 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2271 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2272 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2273 | sock_addr_t addrbuf; |
| 2274 | int res; |
| 2275 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2276 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2277 | if (!getsockaddrlen(s, &addrlen)) |
| 2278 | return NULL; |
| 2279 | memset(&addrbuf, 0, addrlen); |
| 2280 | Py_BEGIN_ALLOW_THREADS |
| 2281 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2282 | Py_END_ALLOW_THREADS |
| 2283 | if (res < 0) |
| 2284 | return s->errorhandler(); |
| 2285 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2286 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2287 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2289 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2290 | "getpeername() -> address info\n\ |
| 2291 | \n\ |
| 2292 | 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] | 2293 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2294 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2295 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2296 | |
| 2297 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2298 | /* s.listen(n) method */ |
| 2299 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2300 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2301 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2302 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2303 | int backlog; |
| 2304 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2305 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2306 | backlog = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2307 | if (backlog == -1 && PyErr_Occurred()) |
| 2308 | return NULL; |
| 2309 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2310 | /* To avoid problems on systems that don't allow a negative backlog |
| 2311 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2312 | if (backlog < 0) |
| 2313 | backlog = 0; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2314 | res = listen(s->sock_fd, backlog); |
| 2315 | Py_END_ALLOW_THREADS |
| 2316 | if (res < 0) |
| 2317 | return s->errorhandler(); |
| 2318 | Py_INCREF(Py_None); |
| 2319 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2322 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2323 | "listen(backlog)\n\ |
| 2324 | \n\ |
| 2325 | Enable a server to accept connections. The backlog argument must be at\n\ |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2326 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
| 2327 | unaccepted connections that the system will allow before refusing new\n\ |
| 2328 | connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2329 | |
| 2330 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2331 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2332 | /* s.makefile(mode) method. |
| 2333 | Create a new open file object referring to a dupped version of |
| 2334 | the socket's file descriptor. (The dup() call is necessary so |
| 2335 | that the open file and socket objects may be closed independent |
| 2336 | of each other.) |
| 2337 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 2338 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2339 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2340 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2341 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2342 | extern int fclose(FILE *); |
| 2343 | char *mode = "r"; |
| 2344 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2345 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2346 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2347 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2348 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2349 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2350 | FILE *fp; |
| 2351 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2352 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2353 | char *mode_r = "r"; |
| 2354 | char *mode_w = "w"; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2355 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2356 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2357 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
| 2358 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2359 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2360 | if (strcmp(mode,"rb") == 0) { |
| 2361 | mode = mode_r; |
| 2362 | } |
| 2363 | else { |
| 2364 | if (strcmp(mode,"wb") == 0) { |
| 2365 | mode = mode_w; |
| 2366 | } |
| 2367 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2368 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2369 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2370 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 2371 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2372 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2373 | if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2374 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | { |
| 2376 | if (fd >= 0) |
| 2377 | SOCKETCLOSE(fd); |
| 2378 | return s->errorhandler(); |
| 2379 | } |
| 2380 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 2381 | if (f != NULL) |
| 2382 | PyFile_SetBufSize(f, bufsize); |
| 2383 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2384 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2385 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2386 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2387 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 2388 | \n\ |
| 2389 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2390 | The mode and buffersize arguments are as for the built-in open() function."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2391 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2392 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2393 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2394 | /* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2395 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Andrew M. Kuchling | 8d0baae | 2006-12-19 14:29:04 +0000 | [diff] [blame] | 2396 | * char buffer. If you have any inc/dec ref to do to the objects that contain |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2397 | * the buffer, do it in the caller. This function returns the number of bytes |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2398 | * successfully read. If there was an error, it returns -1. Note that it is |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2399 | * also possible that we return a number of bytes smaller than the request |
| 2400 | * bytes. |
| 2401 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2402 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2403 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2404 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2405 | ssize_t outlen = -1; |
| 2406 | int timeout; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2407 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2408 | int remaining; |
| 2409 | char *read_buf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2410 | #endif |
| 2411 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2412 | if (!IS_SELECTABLE(s)) { |
| 2413 | select_error(); |
| 2414 | return -1; |
| 2415 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2416 | |
| 2417 | #ifndef __VMS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2418 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2419 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2420 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2421 | if (!timeout) |
| 2422 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2423 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2425 | if (timeout == 1) { |
| 2426 | PyErr_SetString(socket_timeout, "timed out"); |
| 2427 | return -1; |
| 2428 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2429 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2430 | if (outlen < 0) { |
| 2431 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2432 | NULL, so ignore its return value */ |
| 2433 | s->errorhandler(); |
| 2434 | return -1; |
| 2435 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2436 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2437 | read_buf = cbuf; |
| 2438 | remaining = len; |
| 2439 | while (remaining != 0) { |
| 2440 | unsigned int segment; |
| 2441 | int nread = -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2442 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2443 | segment = remaining /SEGMENT_SIZE; |
| 2444 | if (segment != 0) { |
| 2445 | segment = SEGMENT_SIZE; |
| 2446 | } |
| 2447 | else { |
| 2448 | segment = remaining; |
| 2449 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2450 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2451 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2452 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2453 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2454 | if (!timeout) |
| 2455 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2456 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2457 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2458 | if (timeout == 1) { |
| 2459 | PyErr_SetString(socket_timeout, "timed out"); |
| 2460 | return -1; |
| 2461 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2462 | END_SELECT_LOOP(s) |
| 2463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2464 | if (nread < 0) { |
| 2465 | s->errorhandler(); |
| 2466 | return -1; |
| 2467 | } |
| 2468 | if (nread != remaining) { |
| 2469 | read_buf += nread; |
| 2470 | break; |
| 2471 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2472 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2473 | remaining -= segment; |
| 2474 | read_buf += segment; |
| 2475 | } |
| 2476 | outlen = read_buf - cbuf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2477 | #endif /* !__VMS */ |
| 2478 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2479 | return outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2482 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2483 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2484 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2485 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2486 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2487 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2488 | int recvlen, flags = 0; |
| 2489 | ssize_t outlen; |
| 2490 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2491 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2492 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
| 2493 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2494 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2495 | if (recvlen < 0) { |
| 2496 | PyErr_SetString(PyExc_ValueError, |
| 2497 | "negative buffersize in recv"); |
| 2498 | return NULL; |
| 2499 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2500 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2501 | /* Allocate a new string. */ |
| 2502 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2503 | if (buf == NULL) |
| 2504 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2506 | /* Call the guts */ |
| 2507 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags); |
| 2508 | if (outlen < 0) { |
| 2509 | /* An error occurred, release the string and return an |
| 2510 | error. */ |
| 2511 | Py_DECREF(buf); |
| 2512 | return NULL; |
| 2513 | } |
| 2514 | if (outlen != recvlen) { |
| 2515 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2516 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2517 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2518 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2519 | return NULL; |
| 2520 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2522 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2525 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2526 | "recv(buffersize[, flags]) -> data\n\ |
| 2527 | \n\ |
| 2528 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2529 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2530 | 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] | 2531 | 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] | 2532 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2533 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2534 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2535 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2536 | static PyObject* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2537 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2538 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2539 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2540 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2541 | int recvlen = 0, flags = 0; |
| 2542 | ssize_t readlen; |
| 2543 | Py_buffer buf; |
| 2544 | Py_ssize_t buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2546 | /* Get the buffer's memory */ |
| 2547 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist, |
| 2548 | &buf, &recvlen, &flags)) |
| 2549 | return NULL; |
| 2550 | buflen = buf.len; |
| 2551 | assert(buf.buf != 0 && buflen > 0); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2552 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2553 | if (recvlen < 0) { |
| 2554 | PyErr_SetString(PyExc_ValueError, |
| 2555 | "negative buffersize in recv_into"); |
| 2556 | goto error; |
| 2557 | } |
| 2558 | if (recvlen == 0) { |
| 2559 | /* If nbytes was not specified, use the buffer's length */ |
| 2560 | recvlen = buflen; |
| 2561 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2562 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2563 | /* Check if the buffer is large enough */ |
| 2564 | if (buflen < recvlen) { |
| 2565 | PyErr_SetString(PyExc_ValueError, |
| 2566 | "buffer too small for requested bytes"); |
| 2567 | goto error; |
| 2568 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2569 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2570 | /* Call the guts */ |
| 2571 | readlen = sock_recv_guts(s, buf.buf, recvlen, flags); |
| 2572 | if (readlen < 0) { |
| 2573 | /* Return an error. */ |
| 2574 | goto error; |
| 2575 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2577 | PyBuffer_Release(&buf); |
| 2578 | /* Return the number of bytes read. Note that we do not do anything |
| 2579 | special here in the case that readlen < recvlen. */ |
| 2580 | return PyInt_FromSsize_t(readlen); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2581 | |
| 2582 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2583 | PyBuffer_Release(&buf); |
| 2584 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2587 | PyDoc_STRVAR(recv_into_doc, |
| 2588 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2589 | \n\ |
| 2590 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2591 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2592 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2593 | \n\ |
| 2594 | See recv() for documentation about the flags."); |
| 2595 | |
| 2596 | |
| 2597 | /* |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2598 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2599 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2600 | * contain the buffer, do it in the caller. This function returns the number |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2601 | * of bytes successfully read. If there was an error, it returns -1. Note |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2602 | * that it is also possible that we return a number of bytes smaller than the |
| 2603 | * request bytes. |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2604 | * |
| 2605 | * 'addr' is a return value for the address object. Note that you must decref |
| 2606 | * it yourself. |
| 2607 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2608 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2609 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2610 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2611 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2612 | sock_addr_t addrbuf; |
| 2613 | int timeout; |
| 2614 | ssize_t n = -1; |
| 2615 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2616 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2617 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2618 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2619 | if (!getsockaddrlen(s, &addrlen)) |
| 2620 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2621 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2622 | if (!IS_SELECTABLE(s)) { |
| 2623 | select_error(); |
| 2624 | return -1; |
| 2625 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2626 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2627 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2628 | Py_BEGIN_ALLOW_THREADS |
| 2629 | memset(&addrbuf, 0, addrlen); |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2630 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2631 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2632 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2633 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2634 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2635 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2636 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2637 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2638 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2639 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2640 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2641 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2642 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2643 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2644 | } |
| 2645 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2646 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2647 | if (timeout == 1) { |
| 2648 | PyErr_SetString(socket_timeout, "timed out"); |
| 2649 | return -1; |
| 2650 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2651 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2652 | if (n < 0) { |
| 2653 | s->errorhandler(); |
| 2654 | return -1; |
| 2655 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2657 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2658 | addrlen, s->sock_proto))) |
| 2659 | return -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2661 | return n; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2665 | |
| 2666 | static PyObject * |
| 2667 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2668 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2669 | PyObject *buf = NULL; |
| 2670 | PyObject *addr = NULL; |
| 2671 | PyObject *ret = NULL; |
| 2672 | int recvlen, flags = 0; |
| 2673 | ssize_t outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2674 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2675 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
| 2676 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2678 | if (recvlen < 0) { |
| 2679 | PyErr_SetString(PyExc_ValueError, |
| 2680 | "negative buffersize in recvfrom"); |
| 2681 | return NULL; |
| 2682 | } |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 2683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2684 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2685 | if (buf == NULL) |
| 2686 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2687 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2688 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf), |
| 2689 | recvlen, flags, &addr); |
| 2690 | if (outlen < 0) { |
| 2691 | goto finally; |
| 2692 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2694 | if (outlen != recvlen) { |
| 2695 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2696 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2697 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2698 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2699 | goto finally; |
| 2700 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2702 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2703 | |
| 2704 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2705 | Py_XDECREF(buf); |
| 2706 | Py_XDECREF(addr); |
| 2707 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2710 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2711 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2712 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2713 | 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] | 2714 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2715 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2716 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2717 | |
| 2718 | static PyObject * |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2719 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2720 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2721 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2722 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2723 | int recvlen = 0, flags = 0; |
| 2724 | ssize_t readlen; |
| 2725 | Py_buffer buf; |
| 2726 | int buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2727 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2728 | PyObject *addr = NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2729 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2730 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into", |
| 2731 | kwlist, &buf, |
| 2732 | &recvlen, &flags)) |
| 2733 | return NULL; |
| 2734 | buflen = buf.len; |
| 2735 | assert(buf.buf != 0 && buflen > 0); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2737 | if (recvlen < 0) { |
| 2738 | PyErr_SetString(PyExc_ValueError, |
| 2739 | "negative buffersize in recvfrom_into"); |
| 2740 | goto error; |
| 2741 | } |
| 2742 | if (recvlen == 0) { |
| 2743 | /* If nbytes was not specified, use the buffer's length */ |
| 2744 | recvlen = buflen; |
| 2745 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2746 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2747 | readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr); |
| 2748 | if (readlen < 0) { |
| 2749 | /* Return an error */ |
| 2750 | goto error; |
| 2751 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2752 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2753 | PyBuffer_Release(&buf); |
| 2754 | /* Return the number of bytes read and the address. Note that we do |
| 2755 | not do anything special here in the case that readlen < recvlen. */ |
| 2756 | return Py_BuildValue("lN", readlen, addr); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2757 | |
| 2758 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2759 | Py_XDECREF(addr); |
| 2760 | PyBuffer_Release(&buf); |
| 2761 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2764 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2765 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2766 | \n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2767 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2768 | |
| 2769 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2770 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2771 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2772 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2773 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2774 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2775 | char *buf; |
| 2776 | int len, n = -1, flags = 0, timeout; |
| 2777 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2778 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2779 | if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags)) |
| 2780 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2781 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2782 | if (!IS_SELECTABLE(s)) { |
| 2783 | PyBuffer_Release(&pbuf); |
| 2784 | return select_error(); |
| 2785 | } |
| 2786 | buf = pbuf.buf; |
| 2787 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2788 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2789 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2790 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2791 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2792 | if (!timeout) |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2793 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2794 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2795 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2796 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2797 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2798 | Py_END_ALLOW_THREADS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2799 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2800 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2801 | PyErr_SetString(socket_timeout, "timed out"); |
| 2802 | return NULL; |
| 2803 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2804 | END_SELECT_LOOP(s) |
| 2805 | |
| 2806 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2807 | if (n < 0) |
| 2808 | return s->errorhandler(); |
| 2809 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2812 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2813 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2814 | \n\ |
| 2815 | 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] | 2816 | 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] | 2817 | 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] | 2818 | |
| 2819 | |
| 2820 | /* s.sendall(data [,flags]) method */ |
| 2821 | |
| 2822 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2823 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2824 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2825 | char *buf; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2826 | int len, n = -1, flags = 0, timeout, saved_errno; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2827 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2828 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2829 | if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags)) |
| 2830 | return NULL; |
| 2831 | buf = pbuf.buf; |
| 2832 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2833 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2834 | if (!IS_SELECTABLE(s)) { |
| 2835 | PyBuffer_Release(&pbuf); |
| 2836 | return select_error(); |
| 2837 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2838 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2839 | do { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2840 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2841 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2842 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2843 | n = -1; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2844 | if (!timeout) { |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2845 | #ifdef __VMS |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2846 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2847 | #else |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2848 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2849 | #endif |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2850 | } |
| 2851 | Py_END_ALLOW_THREADS |
| 2852 | if (timeout == 1) { |
| 2853 | PyBuffer_Release(&pbuf); |
| 2854 | PyErr_SetString(socket_timeout, "timed out"); |
| 2855 | return NULL; |
| 2856 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2857 | END_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2858 | /* PyErr_CheckSignals() might change errno */ |
| 2859 | saved_errno = errno; |
| 2860 | /* We must run our signal handlers before looping again. |
| 2861 | send() can return a successful partial write when it is |
| 2862 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 2863 | if (PyErr_CheckSignals()) { |
| 2864 | PyBuffer_Release(&pbuf); |
| 2865 | return NULL; |
| 2866 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2867 | if (n < 0) { |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2868 | /* If interrupted, try again */ |
| 2869 | if (saved_errno == EINTR) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2870 | continue; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2871 | else |
| 2872 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2873 | } |
| 2874 | buf += n; |
| 2875 | len -= n; |
| 2876 | } while (len > 0); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2877 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2879 | if (n < 0) |
| 2880 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2882 | Py_INCREF(Py_None); |
| 2883 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2886 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2887 | "sendall(data[, flags])\n\ |
| 2888 | \n\ |
| 2889 | Send a data string to the socket. For the optional flags\n\ |
| 2890 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2891 | 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] | 2892 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2893 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2894 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2895 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2896 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2897 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2898 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2899 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2900 | Py_buffer pbuf; |
| 2901 | PyObject *addro; |
| 2902 | char *buf; |
| 2903 | Py_ssize_t len; |
| 2904 | sock_addr_t addrbuf; |
| 2905 | int addrlen, n = -1, flags, timeout; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2906 | int arglen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2907 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2908 | flags = 0; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2909 | arglen = PyTuple_Size(args); |
| 2910 | switch(arglen) { |
| 2911 | case 2: |
| 2912 | PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro); |
| 2913 | break; |
| 2914 | case 3: |
| 2915 | PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro); |
| 2916 | break; |
| 2917 | default: |
| 2918 | PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3" |
| 2919 | " arguments (%d given)", arglen); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2920 | } |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2921 | if (PyErr_Occurred()) |
| 2922 | return NULL; |
| 2923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2924 | buf = pbuf.buf; |
| 2925 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2927 | if (!IS_SELECTABLE(s)) { |
| 2928 | PyBuffer_Release(&pbuf); |
| 2929 | return select_error(); |
| 2930 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2931 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2932 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2933 | PyBuffer_Release(&pbuf); |
| 2934 | return NULL; |
| 2935 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2936 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2937 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2938 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2939 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2940 | if (!timeout) |
| 2941 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2942 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2943 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2944 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2945 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2946 | PyErr_SetString(socket_timeout, "timed out"); |
| 2947 | return NULL; |
| 2948 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2949 | END_SELECT_LOOP(s) |
| 2950 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2951 | if (n < 0) |
| 2952 | return s->errorhandler(); |
| 2953 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2956 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2957 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2958 | \n\ |
| 2959 | 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] | 2960 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2961 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2962 | |
| 2963 | /* s.shutdown(how) method */ |
| 2964 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2965 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2966 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2967 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2968 | int how; |
| 2969 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2970 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2971 | how = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2972 | if (how == -1 && PyErr_Occurred()) |
| 2973 | return NULL; |
| 2974 | Py_BEGIN_ALLOW_THREADS |
| 2975 | res = shutdown(s->sock_fd, how); |
| 2976 | Py_END_ALLOW_THREADS |
| 2977 | if (res < 0) |
| 2978 | return s->errorhandler(); |
| 2979 | Py_INCREF(Py_None); |
| 2980 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2983 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2984 | "shutdown(flag)\n\ |
| 2985 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2986 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2987 | 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] | 2988 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 2989 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2990 | static PyObject* |
| 2991 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 2992 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2993 | unsigned long cmd = SIO_RCVALL; |
| 2994 | PyObject *argO; |
| 2995 | DWORD recv; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2996 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2997 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 2998 | return NULL; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3000 | switch (cmd) { |
| 3001 | case SIO_RCVALL: { |
| 3002 | unsigned int option = RCVALL_ON; |
| 3003 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3004 | return NULL; |
| 3005 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3006 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3007 | return set_error(); |
| 3008 | } |
| 3009 | return PyLong_FromUnsignedLong(recv); } |
| 3010 | case SIO_KEEPALIVE_VALS: { |
| 3011 | struct tcp_keepalive ka; |
| 3012 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3013 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3014 | return NULL; |
| 3015 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3016 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3017 | return set_error(); |
| 3018 | } |
| 3019 | return PyLong_FromUnsignedLong(recv); } |
| 3020 | default: |
| 3021 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3022 | return NULL; |
| 3023 | } |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3024 | } |
| 3025 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3026 | "ioctl(cmd, option) -> long\n\ |
| 3027 | \n\ |
Kristján Valur Jónsson | 2fcd03b | 2009-09-25 15:19:51 +0000 | [diff] [blame] | 3028 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3029 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3030 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."); |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3031 | |
| 3032 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3033 | |
| 3034 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3035 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3036 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3037 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3038 | accept_doc}, |
| 3039 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3040 | bind_doc}, |
| 3041 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3042 | close_doc}, |
| 3043 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3044 | connect_doc}, |
| 3045 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3046 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3047 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3048 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
| 3049 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3050 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3051 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3052 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3053 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3054 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3055 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3056 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3057 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3058 | METH_NOARGS, getsockname_doc}, |
| 3059 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3060 | getsockopt_doc}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 3061 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3062 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3063 | sock_ioctl_doc}, |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3064 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3065 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 3066 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3067 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3068 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
| 3069 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3070 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3071 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3072 | recv_doc}, |
| 3073 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3074 | recv_into_doc}, |
| 3075 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3076 | recvfrom_doc}, |
| 3077 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3078 | recvfrom_into_doc}, |
| 3079 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3080 | send_doc}, |
| 3081 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3082 | sendall_doc}, |
| 3083 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3084 | sendto_doc}, |
| 3085 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3086 | setblocking_doc}, |
| 3087 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3088 | settimeout_doc}, |
| 3089 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3090 | gettimeout_doc}, |
| 3091 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3092 | setsockopt_doc}, |
| 3093 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3094 | shutdown_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3095 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3096 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
| 3097 | sleeptaskw_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3098 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3099 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3100 | }; |
| 3101 | |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 3102 | /* SockObject members */ |
| 3103 | static PyMemberDef sock_memberlist[] = { |
| 3104 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3105 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3106 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3107 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3108 | {0}, |
| 3109 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3110 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3111 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3112 | First close the file description. */ |
| 3113 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3114 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3115 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3116 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3117 | if (s->sock_fd != -1) |
| 3118 | (void) SOCKETCLOSE(s->sock_fd); |
| 3119 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3122 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3123 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3124 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3125 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3126 | char buf[512]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3127 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3128 | if (s->sock_fd > LONG_MAX) { |
| 3129 | /* this can occur on Win64, and actually there is a special |
| 3130 | ugly printf formatter for decimal pointer length integer |
| 3131 | printing, only bother if necessary*/ |
| 3132 | PyErr_SetString(PyExc_OverflowError, |
| 3133 | "no printf formatter to display " |
| 3134 | "the socket descriptor in decimal"); |
| 3135 | return NULL; |
| 3136 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3137 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3138 | PyOS_snprintf( |
| 3139 | buf, sizeof(buf), |
| 3140 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
| 3141 | (long)s->sock_fd, s->sock_family, |
| 3142 | s->sock_type, |
| 3143 | s->sock_proto); |
| 3144 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3148 | /* Create a new, uninitialized socket object. */ |
| 3149 | |
| 3150 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3151 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3152 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3153 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3154 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3155 | new = type->tp_alloc(type, 0); |
| 3156 | if (new != NULL) { |
| 3157 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3158 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3159 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 3160 | } |
| 3161 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | |
| 3165 | /* Initialize a new socket object. */ |
| 3166 | |
| 3167 | /*ARGSUSED*/ |
| 3168 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3169 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3170 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3171 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3172 | SOCKET_T fd; |
| 3173 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3174 | static char *keywords[] = {"family", "type", "proto", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3175 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3176 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 3177 | "|iii:socket", keywords, |
| 3178 | &family, &type, &proto)) |
| 3179 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3180 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3181 | Py_BEGIN_ALLOW_THREADS |
| 3182 | fd = socket(family, type, proto); |
| 3183 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3184 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3185 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3186 | if (fd == INVALID_SOCKET) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3187 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3188 | if (fd < 0) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3189 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3190 | { |
| 3191 | set_error(); |
| 3192 | return -1; |
| 3193 | } |
| 3194 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3196 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3197 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3201 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3202 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3203 | static PyTypeObject sock_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3204 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 3205 | "_socket.socket", /* tp_name */ |
| 3206 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 3207 | 0, /* tp_itemsize */ |
| 3208 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 3209 | 0, /* tp_print */ |
| 3210 | 0, /* tp_getattr */ |
| 3211 | 0, /* tp_setattr */ |
| 3212 | 0, /* tp_compare */ |
| 3213 | (reprfunc)sock_repr, /* tp_repr */ |
| 3214 | 0, /* tp_as_number */ |
| 3215 | 0, /* tp_as_sequence */ |
| 3216 | 0, /* tp_as_mapping */ |
| 3217 | 0, /* tp_hash */ |
| 3218 | 0, /* tp_call */ |
| 3219 | 0, /* tp_str */ |
| 3220 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3221 | 0, /* tp_setattro */ |
| 3222 | 0, /* tp_as_buffer */ |
| 3223 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3224 | sock_doc, /* tp_doc */ |
| 3225 | 0, /* tp_traverse */ |
| 3226 | 0, /* tp_clear */ |
| 3227 | 0, /* tp_richcompare */ |
| 3228 | 0, /* tp_weaklistoffset */ |
| 3229 | 0, /* tp_iter */ |
| 3230 | 0, /* tp_iternext */ |
| 3231 | sock_methods, /* tp_methods */ |
| 3232 | sock_memberlist, /* tp_members */ |
| 3233 | 0, /* tp_getset */ |
| 3234 | 0, /* tp_base */ |
| 3235 | 0, /* tp_dict */ |
| 3236 | 0, /* tp_descr_get */ |
| 3237 | 0, /* tp_descr_set */ |
| 3238 | 0, /* tp_dictoffset */ |
| 3239 | sock_initobj, /* tp_init */ |
| 3240 | PyType_GenericAlloc, /* tp_alloc */ |
| 3241 | sock_new, /* tp_new */ |
| 3242 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3243 | }; |
| 3244 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3245 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3246 | /* Python interface to gethostname(). */ |
| 3247 | |
| 3248 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3249 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3250 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3251 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3252 | char buf[1024]; |
| 3253 | int res; |
| 3254 | Py_BEGIN_ALLOW_THREADS |
| 3255 | res = gethostname(buf, (int) sizeof buf - 1); |
| 3256 | Py_END_ALLOW_THREADS |
| 3257 | if (res < 0) |
| 3258 | return set_error(); |
| 3259 | buf[sizeof buf - 1] = '\0'; |
| 3260 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3261 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3262 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3263 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3264 | "gethostname() -> string\n\ |
| 3265 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3266 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3267 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3268 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3269 | /* Python interface to gethostbyname(name). */ |
| 3270 | |
| 3271 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3272 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3273 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3274 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3275 | char *name; |
| 3276 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3277 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3278 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
| 3279 | return NULL; |
| 3280 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
| 3281 | return NULL; |
| 3282 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3285 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3286 | "gethostbyname(host) -> address\n\ |
| 3287 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3288 | 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] | 3289 | |
| 3290 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3291 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 3292 | |
| 3293 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3294 | 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] | 3295 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3296 | char **pch; |
| 3297 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 3298 | PyObject *name_list = (PyObject *)NULL; |
| 3299 | PyObject *addr_list = (PyObject *)NULL; |
| 3300 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3302 | if (h == NULL) { |
| 3303 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3304 | #ifndef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3305 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3306 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3307 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3308 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3309 | return NULL; |
| 3310 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3311 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3312 | if (h->h_addrtype != af) { |
| 3313 | /* Let's get real error message to return */ |
| 3314 | PyErr_SetString(socket_error, |
| 3315 | (char *)strerror(EAFNOSUPPORT)); |
Brett Cannon | 10ed0f5 | 2008-03-18 15:35:58 +0000 | [diff] [blame] | 3316 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3317 | return NULL; |
| 3318 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3319 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3320 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3322 | case AF_INET: |
| 3323 | if (alen < sizeof(struct sockaddr_in)) |
| 3324 | return NULL; |
| 3325 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3326 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3327 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3328 | case AF_INET6: |
| 3329 | if (alen < sizeof(struct sockaddr_in6)) |
| 3330 | return NULL; |
| 3331 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3332 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3333 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3334 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3336 | if ((name_list = PyList_New(0)) == NULL) |
| 3337 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3338 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3339 | if ((addr_list = PyList_New(0)) == NULL) |
| 3340 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3341 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3342 | /* SF #1511317: h_aliases can be NULL */ |
| 3343 | if (h->h_aliases) { |
| 3344 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3345 | int status; |
| 3346 | tmp = PyString_FromString(*pch); |
| 3347 | if (tmp == NULL) |
| 3348 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3349 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3350 | status = PyList_Append(name_list, tmp); |
| 3351 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3352 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3353 | if (status) |
| 3354 | goto err; |
| 3355 | } |
| 3356 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3358 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3359 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3360 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3361 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3362 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3363 | case AF_INET: |
| 3364 | { |
| 3365 | struct sockaddr_in sin; |
| 3366 | memset(&sin, 0, sizeof(sin)); |
| 3367 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3368 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3369 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3370 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3371 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3372 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3373 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3374 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3375 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3376 | break; |
| 3377 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3378 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3379 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3380 | case AF_INET6: |
| 3381 | { |
| 3382 | struct sockaddr_in6 sin6; |
| 3383 | memset(&sin6, 0, sizeof(sin6)); |
| 3384 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3385 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3386 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3387 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3388 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3389 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3390 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3391 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3392 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3393 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3394 | break; |
| 3395 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3396 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3397 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3398 | default: /* can't happen */ |
| 3399 | PyErr_SetString(socket_error, |
| 3400 | "unsupported address family"); |
| 3401 | return NULL; |
| 3402 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3404 | if (tmp == NULL) |
| 3405 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3406 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3407 | status = PyList_Append(addr_list, tmp); |
| 3408 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3409 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3410 | if (status) |
| 3411 | goto err; |
| 3412 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3413 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3414 | 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] | 3415 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3416 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3417 | Py_XDECREF(name_list); |
| 3418 | Py_XDECREF(addr_list); |
| 3419 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
| 3422 | |
| 3423 | /* Python interface to gethostbyname_ex(name). */ |
| 3424 | |
| 3425 | /*ARGSUSED*/ |
| 3426 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3427 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3428 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3429 | char *name; |
| 3430 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3431 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3432 | struct sockaddr_storage addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3433 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3434 | struct sockaddr_in addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3435 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3436 | struct sockaddr *sa; |
| 3437 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3438 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3439 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3440 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3441 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3442 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3443 | char buf[16384]; |
| 3444 | int buf_len = (sizeof buf) - 1; |
| 3445 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3446 | #endif |
| 3447 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3448 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3449 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3450 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3451 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3452 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
| 3453 | return NULL; |
| 3454 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
| 3455 | return NULL; |
| 3456 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3457 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3458 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3459 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3460 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3461 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3462 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3463 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3464 | memset((void *) &data, '\0', sizeof(data)); |
| 3465 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3466 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3467 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3468 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3469 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3470 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3471 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3472 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3473 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3474 | Py_END_ALLOW_THREADS |
| 3475 | /* Some C libraries would require addr.__ss_family instead of |
| 3476 | addr.ss_family. |
| 3477 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3478 | access sa_family. */ |
| 3479 | sa = (struct sockaddr*)&addr; |
| 3480 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3481 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3482 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3483 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3484 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3485 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3488 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3489 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3490 | \n\ |
| 3491 | 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] | 3492 | 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] | 3493 | |
| 3494 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3495 | /* Python interface to gethostbyaddr(IP). */ |
| 3496 | |
| 3497 | /*ARGSUSED*/ |
| 3498 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3499 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3500 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3501 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3502 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3503 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3504 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3505 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3506 | struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3507 | char *ip_num; |
| 3508 | struct hostent *h; |
| 3509 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3510 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3511 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3512 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3513 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3514 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3515 | /* glibcs up to 2.10 assume that the buf argument to |
| 3516 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3517 | does not ensure. The attribute below instructs the compiler |
| 3518 | to maintain this alignment. */ |
| 3519 | char buf[16384] Py_ALIGNED(8); |
| 3520 | int buf_len = (sizeof buf) - 1; |
| 3521 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3522 | #endif |
| 3523 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3524 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3525 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3526 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3527 | char *ap; |
| 3528 | int al; |
| 3529 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3531 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
| 3532 | return NULL; |
| 3533 | af = AF_UNSPEC; |
| 3534 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
| 3535 | return NULL; |
| 3536 | af = sa->sa_family; |
| 3537 | ap = NULL; |
| 3538 | switch (af) { |
| 3539 | case AF_INET: |
| 3540 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3541 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3542 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3543 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3544 | case AF_INET6: |
| 3545 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3546 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3547 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3548 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3549 | default: |
| 3550 | PyErr_SetString(socket_error, "unsupported address family"); |
| 3551 | return NULL; |
| 3552 | } |
| 3553 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3554 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3555 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3556 | result = gethostbyaddr_r(ap, al, af, |
| 3557 | &hp_allocated, buf, buf_len, |
| 3558 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3559 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3560 | h = gethostbyaddr_r(ap, al, af, |
| 3561 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3562 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3563 | memset((void *) &data, '\0', sizeof(data)); |
| 3564 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3565 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3566 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3567 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3568 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3569 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3570 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3571 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3572 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3573 | Py_END_ALLOW_THREADS |
| 3574 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3575 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3576 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3577 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3578 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3581 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3582 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3583 | \n\ |
| 3584 | 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] | 3585 | 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] | 3586 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3587 | |
| 3588 | /* Python interface to getservbyname(name). |
| 3589 | This only returns the port number, since the other info is already |
| 3590 | known or not useful (like the list of aliases). */ |
| 3591 | |
| 3592 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3593 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3594 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3595 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3596 | char *name, *proto=NULL; |
| 3597 | struct servent *sp; |
| 3598 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3599 | return NULL; |
| 3600 | Py_BEGIN_ALLOW_THREADS |
| 3601 | sp = getservbyname(name, proto); |
| 3602 | Py_END_ALLOW_THREADS |
| 3603 | if (sp == NULL) { |
| 3604 | PyErr_SetString(socket_error, "service/proto not found"); |
| 3605 | return NULL; |
| 3606 | } |
| 3607 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3610 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3611 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3612 | \n\ |
| 3613 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3614 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3615 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3616 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3617 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3618 | /* Python interface to getservbyport(port). |
| 3619 | This only returns the service name, since the other info is already |
| 3620 | known or not useful (like the list of aliases). */ |
| 3621 | |
| 3622 | /*ARGSUSED*/ |
| 3623 | static PyObject * |
| 3624 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3625 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3626 | int port; |
| 3627 | char *proto=NULL; |
| 3628 | struct servent *sp; |
| 3629 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3630 | return NULL; |
| 3631 | if (port < 0 || port > 0xffff) { |
| 3632 | PyErr_SetString( |
| 3633 | PyExc_OverflowError, |
| 3634 | "getservbyport: port must be 0-65535."); |
| 3635 | return NULL; |
| 3636 | } |
| 3637 | Py_BEGIN_ALLOW_THREADS |
| 3638 | sp = getservbyport(htons((short)port), proto); |
| 3639 | Py_END_ALLOW_THREADS |
| 3640 | if (sp == NULL) { |
| 3641 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3642 | return NULL; |
| 3643 | } |
| 3644 | return PyString_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | PyDoc_STRVAR(getservbyport_doc, |
| 3648 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3649 | \n\ |
| 3650 | Return the service name from a port number and protocol name.\n\ |
| 3651 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3652 | otherwise any protocol will match."); |
| 3653 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3654 | /* Python interface to getprotobyname(name). |
| 3655 | This only returns the protocol number, since the other info is |
| 3656 | already known or not useful (like the list of aliases). */ |
| 3657 | |
| 3658 | /*ARGSUSED*/ |
| 3659 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3660 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3661 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3662 | char *name; |
| 3663 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3664 | #ifdef __BEOS__ |
| 3665 | /* Not available in BeOS yet. - [cjh] */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3666 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
| 3667 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3668 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3669 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3670 | return NULL; |
| 3671 | Py_BEGIN_ALLOW_THREADS |
| 3672 | sp = getprotobyname(name); |
| 3673 | Py_END_ALLOW_THREADS |
| 3674 | if (sp == NULL) { |
| 3675 | PyErr_SetString(socket_error, "protocol not found"); |
| 3676 | return NULL; |
| 3677 | } |
| 3678 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3679 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3682 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3683 | "getprotobyname(name) -> integer\n\ |
| 3684 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3685 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3686 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3687 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3688 | #ifdef HAVE_SOCKETPAIR |
| 3689 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3690 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3691 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3692 | |
| 3693 | /*ARGSUSED*/ |
| 3694 | static PyObject * |
| 3695 | socket_socketpair(PyObject *self, PyObject *args) |
| 3696 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3697 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3698 | SOCKET_T sv[2]; |
| 3699 | int family, type = SOCK_STREAM, proto = 0; |
| 3700 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3701 | |
| 3702 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3703 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3704 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3705 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3706 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3707 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3708 | &family, &type, &proto)) |
| 3709 | return NULL; |
| 3710 | /* Create a pair of socket fds */ |
| 3711 | if (socketpair(family, type, proto, sv) < 0) |
| 3712 | return set_error(); |
| 3713 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3714 | if (s0 == NULL) |
| 3715 | goto finally; |
| 3716 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3717 | if (s1 == NULL) |
| 3718 | goto finally; |
| 3719 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3720 | |
| 3721 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3722 | if (res == NULL) { |
| 3723 | if (s0 == NULL) |
| 3724 | SOCKETCLOSE(sv[0]); |
| 3725 | if (s1 == NULL) |
| 3726 | SOCKETCLOSE(sv[1]); |
| 3727 | } |
| 3728 | Py_XDECREF(s0); |
| 3729 | Py_XDECREF(s1); |
| 3730 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | PyDoc_STRVAR(socketpair_doc, |
| 3734 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3735 | \n\ |
| 3736 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3737 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3738 | 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] | 3739 | 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] | 3740 | |
| 3741 | #endif /* HAVE_SOCKETPAIR */ |
| 3742 | |
| 3743 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3744 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3745 | /* Create a socket object from a numeric file description. |
| 3746 | Useful e.g. if stdin is a socket. |
| 3747 | Additional arguments as for socket(). */ |
| 3748 | |
| 3749 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3750 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3751 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3752 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3753 | PySocketSockObject *s; |
| 3754 | SOCKET_T fd; |
| 3755 | int family, type, proto = 0; |
| 3756 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 3757 | &fd, &family, &type, &proto)) |
| 3758 | return NULL; |
| 3759 | /* Dup the fd so it and the socket can be closed independently */ |
| 3760 | fd = dup(fd); |
| 3761 | if (fd < 0) |
| 3762 | return set_error(); |
| 3763 | s = new_sockobject(fd, family, type, proto); |
| 3764 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3765 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3766 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3767 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3768 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 3769 | \n\ |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 3770 | Create a socket object from a duplicate of the given\n\ |
| 3771 | file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3772 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3773 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3774 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3775 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3776 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3777 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3778 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3779 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3780 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3781 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3782 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3783 | return NULL; |
| 3784 | } |
| 3785 | if (x1 < 0) { |
| 3786 | PyErr_SetString(PyExc_OverflowError, |
| 3787 | "can't convert negative number to unsigned long"); |
| 3788 | return NULL; |
| 3789 | } |
| 3790 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3791 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3794 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3795 | "ntohs(integer) -> integer\n\ |
| 3796 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3797 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3798 | |
| 3799 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3800 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3801 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3802 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3803 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3804 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3805 | if (PyInt_Check(arg)) { |
| 3806 | x = PyInt_AS_LONG(arg); |
| 3807 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3808 | return NULL; |
| 3809 | if ((long)x < 0) { |
| 3810 | PyErr_SetString(PyExc_OverflowError, |
| 3811 | "can't convert negative number to unsigned long"); |
| 3812 | return NULL; |
| 3813 | } |
| 3814 | } |
| 3815 | else if (PyLong_Check(arg)) { |
| 3816 | x = PyLong_AsUnsignedLong(arg); |
| 3817 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3818 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3819 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3820 | { |
| 3821 | unsigned long y; |
| 3822 | /* only want the trailing 32 bits */ |
| 3823 | y = x & 0xFFFFFFFFUL; |
| 3824 | if (y ^ x) |
| 3825 | return PyErr_Format(PyExc_OverflowError, |
| 3826 | "long int larger than 32 bits"); |
| 3827 | x = y; |
| 3828 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3829 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3830 | } |
| 3831 | else |
| 3832 | return PyErr_Format(PyExc_TypeError, |
| 3833 | "expected int/long, %s found", |
| 3834 | Py_TYPE(arg)->tp_name); |
| 3835 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3836 | return NULL; |
| 3837 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3840 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3841 | "ntohl(integer) -> integer\n\ |
| 3842 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3843 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3844 | |
| 3845 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3846 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3847 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3848 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3849 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3850 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3851 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3852 | return NULL; |
| 3853 | } |
| 3854 | if (x1 < 0) { |
| 3855 | PyErr_SetString(PyExc_OverflowError, |
| 3856 | "can't convert negative number to unsigned long"); |
| 3857 | return NULL; |
| 3858 | } |
| 3859 | x2 = (unsigned int)htons((unsigned short)x1); |
| 3860 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3863 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3864 | "htons(integer) -> integer\n\ |
| 3865 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3866 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3867 | |
| 3868 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3869 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3870 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3871 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3872 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3874 | if (PyInt_Check(arg)) { |
| 3875 | x = PyInt_AS_LONG(arg); |
| 3876 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3877 | return NULL; |
| 3878 | if ((long)x < 0) { |
| 3879 | PyErr_SetString(PyExc_OverflowError, |
| 3880 | "can't convert negative number to unsigned long"); |
| 3881 | return NULL; |
| 3882 | } |
| 3883 | } |
| 3884 | else if (PyLong_Check(arg)) { |
| 3885 | x = PyLong_AsUnsignedLong(arg); |
| 3886 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3887 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3888 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3889 | { |
| 3890 | unsigned long y; |
| 3891 | /* only want the trailing 32 bits */ |
| 3892 | y = x & 0xFFFFFFFFUL; |
| 3893 | if (y ^ x) |
| 3894 | return PyErr_Format(PyExc_OverflowError, |
| 3895 | "long int larger than 32 bits"); |
| 3896 | x = y; |
| 3897 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3898 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3899 | } |
| 3900 | else |
| 3901 | return PyErr_Format(PyExc_TypeError, |
| 3902 | "expected int/long, %s found", |
| 3903 | Py_TYPE(arg)->tp_name); |
| 3904 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3907 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3908 | "htonl(integer) -> integer\n\ |
| 3909 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3910 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3911 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3912 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3914 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3915 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3916 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3917 | 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] | 3918 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3919 | |
| 3920 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3921 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3922 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3923 | #ifndef INADDR_NONE |
| 3924 | #define INADDR_NONE (-1) |
| 3925 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3926 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3927 | struct in_addr buf; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3928 | #endif |
| 3929 | |
| 3930 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 3931 | #if (SIZEOF_INT != 4) |
| 3932 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3933 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3934 | /* Have to use inet_addr() instead */ |
| 3935 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3936 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3937 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3939 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3940 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3941 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3942 | |
| 3943 | #ifdef HAVE_INET_ATON |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3944 | |
| 3945 | #ifdef USE_INET_ATON_WEAKLINK |
| 3946 | if (inet_aton != NULL) { |
| 3947 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3948 | if (inet_aton(ip_addr, &buf)) |
| 3949 | return PyString_FromStringAndSize((char *)(&buf), |
| 3950 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3951 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3952 | PyErr_SetString(socket_error, |
| 3953 | "illegal IP address string passed to inet_aton"); |
| 3954 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3955 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3956 | #ifdef USE_INET_ATON_WEAKLINK |
| 3957 | } else { |
| 3958 | #endif |
| 3959 | |
| 3960 | #endif |
| 3961 | |
| 3962 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3963 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3964 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3965 | * for this */ |
| 3966 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3967 | packed_addr = 0xFFFFFFFF; |
| 3968 | } else { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3969 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3970 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3972 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3973 | PyErr_SetString(socket_error, |
| 3974 | "illegal IP address string passed to inet_aton"); |
| 3975 | return NULL; |
| 3976 | } |
| 3977 | } |
| 3978 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 3979 | sizeof(packed_addr)); |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3980 | |
| 3981 | #ifdef USE_INET_ATON_WEAKLINK |
| 3982 | } |
| 3983 | #endif |
| 3984 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3985 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3988 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3989 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3990 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3991 | 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] | 3992 | |
| 3993 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3994 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3995 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3996 | char *packed_str; |
| 3997 | int addr_len; |
| 3998 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4000 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
| 4001 | return NULL; |
| 4002 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4003 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4004 | if (addr_len != sizeof(packed_addr)) { |
| 4005 | PyErr_SetString(socket_error, |
| 4006 | "packed IP wrong length for inet_ntoa"); |
| 4007 | return NULL; |
| 4008 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4009 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4010 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4011 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4012 | return PyString_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4013 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4014 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4015 | #ifdef HAVE_INET_PTON |
| 4016 | |
| 4017 | PyDoc_STRVAR(inet_pton_doc, |
| 4018 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 4019 | \n\ |
| 4020 | Convert an IP address from string format to a packed string suitable\n\ |
| 4021 | for use with low-level network functions."); |
| 4022 | |
| 4023 | static PyObject * |
| 4024 | socket_inet_pton(PyObject *self, PyObject *args) |
| 4025 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4026 | int af; |
| 4027 | char* ip; |
| 4028 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4029 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4030 | 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] | 4031 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4032 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4033 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4034 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 4035 | return NULL; |
| 4036 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4037 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 4038 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4039 | if(af == AF_INET6) { |
| 4040 | PyErr_SetString(socket_error, |
| 4041 | "can't use AF_INET6, IPv6 is disabled"); |
| 4042 | return NULL; |
| 4043 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4044 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 4045 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4046 | retval = inet_pton(af, ip, packed); |
| 4047 | if (retval < 0) { |
| 4048 | PyErr_SetFromErrno(socket_error); |
| 4049 | return NULL; |
| 4050 | } else if (retval == 0) { |
| 4051 | PyErr_SetString(socket_error, |
| 4052 | "illegal IP address string passed to inet_pton"); |
| 4053 | return NULL; |
| 4054 | } else if (af == AF_INET) { |
| 4055 | return PyString_FromStringAndSize(packed, |
| 4056 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4057 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4058 | } else if (af == AF_INET6) { |
| 4059 | return PyString_FromStringAndSize(packed, |
| 4060 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4061 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4062 | } else { |
| 4063 | PyErr_SetString(socket_error, "unknown address family"); |
| 4064 | return NULL; |
| 4065 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4066 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4067 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4068 | PyDoc_STRVAR(inet_ntop_doc, |
| 4069 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 4070 | \n\ |
| 4071 | Convert a packed IP address of the given family to string format."); |
| 4072 | |
| 4073 | static PyObject * |
| 4074 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 4075 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4076 | int af; |
| 4077 | char* packed; |
| 4078 | int len; |
| 4079 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4080 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4081 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4082 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4083 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4084 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4085 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4086 | /* Guarantee NUL-termination for PyString_FromString() below */ |
| 4087 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4088 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4089 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 4090 | return NULL; |
| 4091 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4092 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4093 | if (af == AF_INET) { |
| 4094 | if (len != sizeof(struct in_addr)) { |
| 4095 | PyErr_SetString(PyExc_ValueError, |
| 4096 | "invalid length of packed IP address string"); |
| 4097 | return NULL; |
| 4098 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4099 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4100 | } else if (af == AF_INET6) { |
| 4101 | if (len != sizeof(struct in6_addr)) { |
| 4102 | PyErr_SetString(PyExc_ValueError, |
| 4103 | "invalid length of packed IP address string"); |
| 4104 | return NULL; |
| 4105 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4106 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4107 | } else { |
| 4108 | PyErr_Format(PyExc_ValueError, |
| 4109 | "unknown address family %d", af); |
| 4110 | return NULL; |
| 4111 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4112 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4113 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 4114 | if (!retval) { |
| 4115 | PyErr_SetFromErrno(socket_error); |
| 4116 | return NULL; |
| 4117 | } else { |
| 4118 | return PyString_FromString(retval); |
| 4119 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4120 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4121 | /* NOTREACHED */ |
| 4122 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 4123 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | #endif /* HAVE_INET_PTON */ |
| 4127 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4128 | /* Python interface to getaddrinfo(host, port). */ |
| 4129 | |
| 4130 | /*ARGSUSED*/ |
| 4131 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4132 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4133 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4134 | struct addrinfo hints, *res; |
| 4135 | struct addrinfo *res0 = NULL; |
| 4136 | PyObject *hobj = NULL; |
| 4137 | PyObject *pobj = (PyObject *)NULL; |
| 4138 | char pbuf[30]; |
| 4139 | char *hptr, *pptr; |
| 4140 | int family, socktype, protocol, flags; |
| 4141 | int error; |
| 4142 | PyObject *all = (PyObject *)NULL; |
| 4143 | PyObject *single = (PyObject *)NULL; |
| 4144 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4145 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4146 | family = socktype = protocol = flags = 0; |
| 4147 | family = AF_UNSPEC; |
| 4148 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 4149 | &hobj, &pobj, &family, &socktype, |
| 4150 | &protocol, &flags)) { |
| 4151 | return NULL; |
| 4152 | } |
| 4153 | if (hobj == Py_None) { |
| 4154 | hptr = NULL; |
| 4155 | } else if (PyUnicode_Check(hobj)) { |
| 4156 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 4157 | if (!idna) |
| 4158 | return NULL; |
| 4159 | hptr = PyString_AsString(idna); |
| 4160 | } else if (PyString_Check(hobj)) { |
| 4161 | hptr = PyString_AsString(hobj); |
| 4162 | } else { |
| 4163 | PyErr_SetString(PyExc_TypeError, |
| 4164 | "getaddrinfo() argument 1 must be string or None"); |
| 4165 | return NULL; |
| 4166 | } |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4167 | if (PyInt_Check(pobj) || PyLong_Check(pobj)) { |
| 4168 | long value = PyLong_AsLong(pobj); |
| 4169 | if (value == -1 && PyErr_Occurred()) |
| 4170 | return NULL; |
| 4171 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4172 | pptr = pbuf; |
| 4173 | } else if (PyString_Check(pobj)) { |
| 4174 | pptr = PyString_AsString(pobj); |
| 4175 | } else if (pobj == Py_None) { |
| 4176 | pptr = (char *)NULL; |
| 4177 | } else { |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4178 | PyErr_SetString(socket_error, |
| 4179 | "getaddrinfo() argument 2 must be integer or string"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4180 | goto err; |
| 4181 | } |
| 4182 | memset(&hints, 0, sizeof(hints)); |
| 4183 | hints.ai_family = family; |
| 4184 | hints.ai_socktype = socktype; |
| 4185 | hints.ai_protocol = protocol; |
| 4186 | hints.ai_flags = flags; |
| 4187 | Py_BEGIN_ALLOW_THREADS |
| 4188 | ACQUIRE_GETADDRINFO_LOCK |
| 4189 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 4190 | Py_END_ALLOW_THREADS |
| 4191 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4192 | if (error) { |
| 4193 | set_gaierror(error); |
| 4194 | goto err; |
| 4195 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4196 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4197 | if ((all = PyList_New(0)) == NULL) |
| 4198 | goto err; |
| 4199 | for (res = res0; res; res = res->ai_next) { |
| 4200 | PyObject *addr = |
| 4201 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 4202 | if (addr == NULL) |
| 4203 | goto err; |
| 4204 | single = Py_BuildValue("iiisO", res->ai_family, |
| 4205 | res->ai_socktype, res->ai_protocol, |
| 4206 | res->ai_canonname ? res->ai_canonname : "", |
| 4207 | addr); |
| 4208 | Py_DECREF(addr); |
| 4209 | if (single == NULL) |
| 4210 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4211 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4212 | if (PyList_Append(all, single)) |
| 4213 | goto err; |
| 4214 | Py_XDECREF(single); |
| 4215 | } |
| 4216 | Py_XDECREF(idna); |
| 4217 | if (res0) |
| 4218 | freeaddrinfo(res0); |
| 4219 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4220 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4221 | Py_XDECREF(single); |
| 4222 | Py_XDECREF(all); |
| 4223 | Py_XDECREF(idna); |
| 4224 | if (res0) |
| 4225 | freeaddrinfo(res0); |
| 4226 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4229 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4230 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 4231 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4232 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4233 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4234 | |
| 4235 | /* Python interface to getnameinfo(sa, flags). */ |
| 4236 | |
| 4237 | /*ARGSUSED*/ |
| 4238 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4239 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4240 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4241 | PyObject *sa = (PyObject *)NULL; |
| 4242 | int flags; |
| 4243 | char *hostp; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4244 | int port; |
| 4245 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4246 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 4247 | struct addrinfo hints, *res = NULL; |
| 4248 | int error; |
| 4249 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4250 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4251 | flags = flowinfo = scope_id = 0; |
| 4252 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 4253 | return NULL; |
| 4254 | if (!PyTuple_Check(sa)) { |
| 4255 | PyErr_SetString(PyExc_TypeError, |
| 4256 | "getnameinfo() argument 1 must be a tuple"); |
| 4257 | return NULL; |
| 4258 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4259 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4260 | &hostp, &port, &flowinfo, &scope_id)) |
| 4261 | return NULL; |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 4262 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4263 | PyErr_SetString(PyExc_OverflowError, |
| 4264 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 4265 | return NULL; |
| 4266 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4267 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 4268 | memset(&hints, 0, sizeof(hints)); |
| 4269 | hints.ai_family = AF_UNSPEC; |
| 4270 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
| 4271 | Py_BEGIN_ALLOW_THREADS |
| 4272 | ACQUIRE_GETADDRINFO_LOCK |
| 4273 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 4274 | Py_END_ALLOW_THREADS |
| 4275 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4276 | if (error) { |
| 4277 | set_gaierror(error); |
| 4278 | goto fail; |
| 4279 | } |
| 4280 | if (res->ai_next) { |
| 4281 | PyErr_SetString(socket_error, |
| 4282 | "sockaddr resolved to multiple addresses"); |
| 4283 | goto fail; |
| 4284 | } |
| 4285 | switch (res->ai_family) { |
| 4286 | case AF_INET: |
| 4287 | { |
| 4288 | if (PyTuple_GET_SIZE(sa) != 2) { |
| 4289 | PyErr_SetString(socket_error, |
| 4290 | "IPv4 sockaddr must be 2 tuple"); |
| 4291 | goto fail; |
| 4292 | } |
| 4293 | break; |
| 4294 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4295 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4296 | case AF_INET6: |
| 4297 | { |
| 4298 | struct sockaddr_in6 *sin6; |
| 4299 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4300 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4301 | sin6->sin6_scope_id = scope_id; |
| 4302 | break; |
| 4303 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4304 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4305 | } |
| 4306 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 4307 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 4308 | if (error) { |
| 4309 | set_gaierror(error); |
| 4310 | goto fail; |
| 4311 | } |
| 4312 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4313 | |
| 4314 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4315 | if (res) |
| 4316 | freeaddrinfo(res); |
| 4317 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4320 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4321 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4322 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4323 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4324 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4325 | |
| 4326 | /* Python API to getting and setting the default timeout value. */ |
| 4327 | |
| 4328 | static PyObject * |
| 4329 | socket_getdefaulttimeout(PyObject *self) |
| 4330 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4331 | if (defaulttimeout < 0.0) { |
| 4332 | Py_INCREF(Py_None); |
| 4333 | return Py_None; |
| 4334 | } |
| 4335 | else |
| 4336 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4340 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4341 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4342 | Returns the default timeout in seconds (float) for new socket objects.\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4343 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4344 | When the socket module is first imported, the default is None."); |
| 4345 | |
| 4346 | static PyObject * |
| 4347 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 4348 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4349 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4350 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4351 | if (arg == Py_None) |
| 4352 | timeout = -1.0; |
| 4353 | else { |
| 4354 | timeout = PyFloat_AsDouble(arg); |
| 4355 | if (timeout < 0.0) { |
| 4356 | if (!PyErr_Occurred()) |
| 4357 | PyErr_SetString(PyExc_ValueError, |
| 4358 | "Timeout value out of range"); |
| 4359 | return NULL; |
| 4360 | } |
| 4361 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4362 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4363 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4364 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4365 | Py_INCREF(Py_None); |
| 4366 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4370 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4371 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4372 | Set the default timeout in seconds (float) for new socket objects.\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4373 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4374 | When the socket module is first imported, the default is None."); |
| 4375 | |
| 4376 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4377 | /* List of functions exported by this module. */ |
| 4378 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4379 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4380 | {"gethostbyname", socket_gethostbyname, |
| 4381 | METH_VARARGS, gethostbyname_doc}, |
| 4382 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4383 | METH_VARARGS, ghbn_ex_doc}, |
| 4384 | {"gethostbyaddr", socket_gethostbyaddr, |
| 4385 | METH_VARARGS, gethostbyaddr_doc}, |
| 4386 | {"gethostname", socket_gethostname, |
| 4387 | METH_NOARGS, gethostname_doc}, |
| 4388 | {"getservbyname", socket_getservbyname, |
| 4389 | METH_VARARGS, getservbyname_doc}, |
| 4390 | {"getservbyport", socket_getservbyport, |
| 4391 | METH_VARARGS, getservbyport_doc}, |
| 4392 | {"getprotobyname", socket_getprotobyname, |
| 4393 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4394 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4395 | {"fromfd", socket_fromfd, |
| 4396 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4397 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4398 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4399 | {"socketpair", socket_socketpair, |
| 4400 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4401 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4402 | {"ntohs", socket_ntohs, |
| 4403 | METH_VARARGS, ntohs_doc}, |
| 4404 | {"ntohl", socket_ntohl, |
| 4405 | METH_O, ntohl_doc}, |
| 4406 | {"htons", socket_htons, |
| 4407 | METH_VARARGS, htons_doc}, |
| 4408 | {"htonl", socket_htonl, |
| 4409 | METH_O, htonl_doc}, |
| 4410 | {"inet_aton", socket_inet_aton, |
| 4411 | METH_VARARGS, inet_aton_doc}, |
| 4412 | {"inet_ntoa", socket_inet_ntoa, |
| 4413 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4414 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4415 | {"inet_pton", socket_inet_pton, |
| 4416 | METH_VARARGS, inet_pton_doc}, |
| 4417 | {"inet_ntop", socket_inet_ntop, |
| 4418 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4419 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4420 | {"getaddrinfo", socket_getaddrinfo, |
| 4421 | METH_VARARGS, getaddrinfo_doc}, |
| 4422 | {"getnameinfo", socket_getnameinfo, |
| 4423 | METH_VARARGS, getnameinfo_doc}, |
| 4424 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4425 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4426 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4427 | METH_O, setdefaulttimeout_doc}, |
| 4428 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4429 | }; |
| 4430 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4431 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4432 | #ifdef RISCOS |
| 4433 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4434 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4435 | static int |
| 4436 | os_init(void) |
| 4437 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4438 | _kernel_swi_regs r; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4439 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4440 | r.r[0] = 0; |
| 4441 | _kernel_swi(0x43380, &r, &r); |
| 4442 | taskwindow = r.r[0]; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4444 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | #endif /* RISCOS */ |
| 4448 | |
| 4449 | |
| 4450 | #ifdef MS_WINDOWS |
| 4451 | #define OS_INIT_DEFINED |
| 4452 | |
| 4453 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4454 | |
| 4455 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4456 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4457 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4458 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4462 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4463 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4464 | WSADATA WSAData; |
| 4465 | int ret; |
| 4466 | char buf[100]; |
| 4467 | ret = WSAStartup(0x0101, &WSAData); |
| 4468 | switch (ret) { |
| 4469 | case 0: /* No error */ |
| 4470 | Py_AtExit(os_cleanup); |
| 4471 | return 1; /* Success */ |
| 4472 | case WSASYSNOTREADY: |
| 4473 | PyErr_SetString(PyExc_ImportError, |
| 4474 | "WSAStartup failed: network not ready"); |
| 4475 | break; |
| 4476 | case WSAVERNOTSUPPORTED: |
| 4477 | case WSAEINVAL: |
| 4478 | PyErr_SetString( |
| 4479 | PyExc_ImportError, |
| 4480 | "WSAStartup failed: requested version not supported"); |
| 4481 | break; |
| 4482 | default: |
| 4483 | PyOS_snprintf(buf, sizeof(buf), |
| 4484 | "WSAStartup failed: error code %d", ret); |
| 4485 | PyErr_SetString(PyExc_ImportError, buf); |
| 4486 | break; |
| 4487 | } |
| 4488 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4491 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4492 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4493 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4494 | #ifdef PYOS_OS2 |
| 4495 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4496 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4497 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4498 | |
| 4499 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4500 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4501 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4502 | #ifndef PYCC_GCC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4503 | char reason[64]; |
| 4504 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4506 | if (rc == 0) { |
| 4507 | return 1; /* Success */ |
| 4508 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4509 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4510 | PyOS_snprintf(reason, sizeof(reason), |
| 4511 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 4512 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4513 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4514 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4515 | #else |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4516 | /* No need to initialize sockets with GCC/EMX */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4517 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4518 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | #endif /* PYOS_OS2 */ |
| 4522 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4523 | |
| 4524 | #ifndef OS_INIT_DEFINED |
| 4525 | static int |
| 4526 | os_init(void) |
| 4527 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4528 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4529 | } |
| 4530 | #endif |
| 4531 | |
| 4532 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4533 | /* C API table - always add new things to the end for binary |
| 4534 | compatibility. */ |
| 4535 | static |
| 4536 | PySocketModule_APIObject PySocketModuleAPI = |
| 4537 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4538 | &sock_type, |
| 4539 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4540 | }; |
| 4541 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4542 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4543 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4544 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4545 | This module is actually called "_socket", and there's a wrapper |
| 4546 | "socket.py" which implements some additional functionality. On some |
| 4547 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 4548 | wrapper for the socket type that provides missing functionality such |
| 4549 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 4550 | with an ImportError exception if os-specific initialization fails. |
| 4551 | On Windows, this does WINSOCK initialization. When WINSOCK is |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4552 | initialized successfully, a call to WSACleanup() is scheduled to be |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4553 | made at exit time. |
| 4554 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4556 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4557 | "Implementation module for socket operations.\n\ |
| 4558 | \n\ |
| 4559 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4560 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4561 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4562 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4563 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4564 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4565 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4566 | if (!os_init()) |
| 4567 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4568 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4569 | Py_TYPE(&sock_type) = &PyType_Type; |
| 4570 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 4571 | socket_methods, |
| 4572 | socket_doc); |
| 4573 | if (m == NULL) |
| 4574 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4576 | socket_error = PyErr_NewException("socket.error", |
| 4577 | PyExc_IOError, NULL); |
| 4578 | if (socket_error == NULL) |
| 4579 | return; |
| 4580 | PySocketModuleAPI.error = socket_error; |
| 4581 | Py_INCREF(socket_error); |
| 4582 | PyModule_AddObject(m, "error", socket_error); |
| 4583 | socket_herror = PyErr_NewException("socket.herror", |
| 4584 | socket_error, NULL); |
| 4585 | if (socket_herror == NULL) |
| 4586 | return; |
| 4587 | Py_INCREF(socket_herror); |
| 4588 | PyModule_AddObject(m, "herror", socket_herror); |
| 4589 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4590 | NULL); |
| 4591 | if (socket_gaierror == NULL) |
| 4592 | return; |
| 4593 | Py_INCREF(socket_gaierror); |
| 4594 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4595 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4596 | socket_error, NULL); |
| 4597 | if (socket_timeout == NULL) |
| 4598 | return; |
| 4599 | Py_INCREF(socket_timeout); |
| 4600 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 4601 | Py_INCREF((PyObject *)&sock_type); |
| 4602 | if (PyModule_AddObject(m, "SocketType", |
| 4603 | (PyObject *)&sock_type) != 0) |
| 4604 | return; |
| 4605 | Py_INCREF((PyObject *)&sock_type); |
| 4606 | if (PyModule_AddObject(m, "socket", |
| 4607 | (PyObject *)&sock_type) != 0) |
| 4608 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4609 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4610 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4611 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4612 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4613 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4614 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4615 | Py_INCREF(has_ipv6); |
| 4616 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4618 | /* Export C API */ |
| 4619 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4620 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4621 | ) != 0) |
| 4622 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4623 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4624 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4625 | #ifdef AF_UNSPEC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4626 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4627 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4628 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4629 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4630 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4631 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4632 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4633 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4634 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4635 | #ifdef AF_AX25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4636 | /* Amateur Radio AX.25 */ |
| 4637 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4638 | #endif |
| 4639 | #ifdef AF_IPX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4640 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4641 | #endif |
| 4642 | #ifdef AF_APPLETALK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4643 | /* Appletalk DDP */ |
| 4644 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4645 | #endif |
| 4646 | #ifdef AF_NETROM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4647 | /* Amateur radio NetROM */ |
| 4648 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4649 | #endif |
| 4650 | #ifdef AF_BRIDGE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4651 | /* Multiprotocol bridge */ |
| 4652 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4653 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4654 | #ifdef AF_ATMPVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4655 | /* ATM PVCs */ |
| 4656 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4657 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4658 | #ifdef AF_AAL5 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4659 | /* Reserved for Werner's ATM */ |
| 4660 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4661 | #endif |
| 4662 | #ifdef AF_X25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4663 | /* Reserved for X.25 project */ |
| 4664 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4665 | #endif |
| 4666 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4667 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4668 | #endif |
| 4669 | #ifdef AF_ROSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4670 | /* Amateur Radio X.25 PLP */ |
| 4671 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4672 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4673 | #ifdef AF_DECnet |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4674 | /* Reserved for DECnet project */ |
| 4675 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4676 | #endif |
| 4677 | #ifdef AF_NETBEUI |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4678 | /* Reserved for 802.2LLC project */ |
| 4679 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4680 | #endif |
| 4681 | #ifdef AF_SECURITY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4682 | /* Security callback pseudo AF */ |
| 4683 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4684 | #endif |
| 4685 | #ifdef AF_KEY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4686 | /* PF_KEY key management API */ |
| 4687 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4688 | #endif |
| 4689 | #ifdef AF_NETLINK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4690 | /* */ |
| 4691 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4692 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4693 | #ifdef NETLINK_SKIP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4694 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4695 | #endif |
| 4696 | #ifdef NETLINK_W1 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4697 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4698 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4699 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4700 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4701 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4702 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4703 | #endif |
| 4704 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4705 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4706 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4707 | #ifdef NETLINK_XFRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4708 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4709 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4710 | #ifdef NETLINK_ARPD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4711 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4712 | #endif |
| 4713 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4714 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4715 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4716 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Martin v. Löwis | b1cc1d4 | 2007-02-13 12:14:19 +0000 | [diff] [blame] | 4717 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4718 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
| 4719 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4720 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4721 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4722 | #endif |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 4723 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4724 | #ifdef AF_ROUTE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4725 | /* Alias to emulate 4.4BSD */ |
| 4726 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4727 | #endif |
| 4728 | #ifdef AF_ASH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4729 | /* Ash */ |
| 4730 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4731 | #endif |
| 4732 | #ifdef AF_ECONET |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4733 | /* Acorn Econet */ |
| 4734 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4735 | #endif |
| 4736 | #ifdef AF_ATMSVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4737 | /* ATM SVCs */ |
| 4738 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4739 | #endif |
| 4740 | #ifdef AF_SNA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4741 | /* Linux SNA Project (nutters!) */ |
| 4742 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4743 | #endif |
| 4744 | #ifdef AF_IRDA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4745 | /* IRDA sockets */ |
| 4746 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4747 | #endif |
| 4748 | #ifdef AF_PPPOX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4749 | /* PPPoX sockets */ |
| 4750 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4751 | #endif |
| 4752 | #ifdef AF_WANPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4753 | /* Wanpipe API Sockets */ |
| 4754 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4755 | #endif |
| 4756 | #ifdef AF_LLC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4757 | /* Linux LLC */ |
| 4758 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4759 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4760 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4761 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4762 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4763 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4764 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4765 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4766 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4767 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4768 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4769 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4770 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4771 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4772 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4773 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4774 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4775 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4776 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4777 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4778 | 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] | 4779 | #endif |
| 4780 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 4781 | #ifdef AF_PACKET |
| 4782 | PyModule_AddIntMacro(m, AF_PACKET); |
| 4783 | #endif |
| 4784 | #ifdef PF_PACKET |
| 4785 | PyModule_AddIntMacro(m, PF_PACKET); |
| 4786 | #endif |
| 4787 | #ifdef PACKET_HOST |
| 4788 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 4789 | #endif |
| 4790 | #ifdef PACKET_BROADCAST |
| 4791 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 4792 | #endif |
| 4793 | #ifdef PACKET_MULTICAST |
| 4794 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 4795 | #endif |
| 4796 | #ifdef PACKET_OTHERHOST |
| 4797 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 4798 | #endif |
| 4799 | #ifdef PACKET_OUTGOING |
| 4800 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 4801 | #endif |
| 4802 | #ifdef PACKET_LOOPBACK |
| 4803 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 4804 | #endif |
| 4805 | #ifdef PACKET_FASTROUTE |
| 4806 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4807 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4808 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4809 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4810 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4811 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4812 | /* for addresses */ |
| 4813 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4814 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4815 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4816 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4817 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4818 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4819 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4820 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4821 | /* for setsockopt() */ |
| 4822 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4823 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4824 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4825 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4826 | TIPC_DEST_DROPPABLE); |
| 4827 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4828 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4829 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4830 | TIPC_LOW_IMPORTANCE); |
| 4831 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4832 | TIPC_MEDIUM_IMPORTANCE); |
| 4833 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4834 | TIPC_HIGH_IMPORTANCE); |
| 4835 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4836 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4837 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4838 | /* for subscriptions */ |
| 4839 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4840 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4841 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4842 | /* doesn't seem to be available everywhere */ |
| 4843 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4844 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4845 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4846 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4847 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4848 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4849 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4850 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4851 | #endif |
| 4852 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4853 | /* Socket types */ |
| 4854 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4855 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4856 | #ifndef __BEOS__ |
| 4857 | /* We have incomplete socket support. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4858 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4859 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4860 | #if defined(SOCK_RDM) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4861 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4862 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4863 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4864 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4865 | #ifdef SO_DEBUG |
| 4866 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4867 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4868 | #ifdef SO_ACCEPTCONN |
| 4869 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4870 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4871 | #ifdef SO_REUSEADDR |
| 4872 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4873 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4874 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4875 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4876 | #endif |
| 4877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4878 | #ifdef SO_KEEPALIVE |
| 4879 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4880 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4881 | #ifdef SO_DONTROUTE |
| 4882 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4883 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4884 | #ifdef SO_BROADCAST |
| 4885 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4886 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4887 | #ifdef SO_USELOOPBACK |
| 4888 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4889 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4890 | #ifdef SO_LINGER |
| 4891 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4892 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4893 | #ifdef SO_OOBINLINE |
| 4894 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4895 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4896 | #ifdef SO_REUSEPORT |
| 4897 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4898 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4899 | #ifdef SO_SNDBUF |
| 4900 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4901 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4902 | #ifdef SO_RCVBUF |
| 4903 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4904 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4905 | #ifdef SO_SNDLOWAT |
| 4906 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4907 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4908 | #ifdef SO_RCVLOWAT |
| 4909 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4910 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4911 | #ifdef SO_SNDTIMEO |
| 4912 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4913 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4914 | #ifdef SO_RCVTIMEO |
| 4915 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4916 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4917 | #ifdef SO_ERROR |
| 4918 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4919 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4920 | #ifdef SO_TYPE |
| 4921 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4922 | #endif |
Larry Hastings | 3b958e3 | 2010-04-02 11:18:17 +0000 | [diff] [blame] | 4923 | #ifdef SO_SETFIB |
| 4924 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB); |
| 4925 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4927 | /* Maximum number of connections for "listen" */ |
| 4928 | #ifdef SOMAXCONN |
| 4929 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4930 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4931 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4932 | #endif |
| 4933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4934 | /* Flags for send, recv */ |
| 4935 | #ifdef MSG_OOB |
| 4936 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4937 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4938 | #ifdef MSG_PEEK |
| 4939 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4940 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4941 | #ifdef MSG_DONTROUTE |
| 4942 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4943 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4944 | #ifdef MSG_DONTWAIT |
| 4945 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4946 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4947 | #ifdef MSG_EOR |
| 4948 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4949 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4950 | #ifdef MSG_TRUNC |
| 4951 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4952 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4953 | #ifdef MSG_CTRUNC |
| 4954 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4955 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4956 | #ifdef MSG_WAITALL |
| 4957 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4958 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4959 | #ifdef MSG_BTAG |
| 4960 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4961 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4962 | #ifdef MSG_ETAG |
| 4963 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4964 | #endif |
| 4965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4966 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4967 | #ifdef SOL_SOCKET |
| 4968 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4969 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4970 | #ifdef SOL_IP |
| 4971 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4972 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4973 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4974 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4975 | #ifdef SOL_IPX |
| 4976 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4977 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4978 | #ifdef SOL_AX25 |
| 4979 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4980 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4981 | #ifdef SOL_ATALK |
| 4982 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4983 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4984 | #ifdef SOL_NETROM |
| 4985 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4986 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4987 | #ifdef SOL_ROSE |
| 4988 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4989 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4990 | #ifdef SOL_TCP |
| 4991 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4992 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4993 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4994 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4995 | #ifdef SOL_UDP |
| 4996 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4997 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4998 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4999 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5000 | #ifdef IPPROTO_IP |
| 5001 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5002 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5003 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5004 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5005 | #ifdef IPPROTO_HOPOPTS |
| 5006 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5007 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5008 | #ifdef IPPROTO_ICMP |
| 5009 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5010 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5011 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5012 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5013 | #ifdef IPPROTO_IGMP |
| 5014 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5015 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5016 | #ifdef IPPROTO_GGP |
| 5017 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5018 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5019 | #ifdef IPPROTO_IPV4 |
| 5020 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5021 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5022 | #ifdef IPPROTO_IPV6 |
| 5023 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 5024 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5025 | #ifdef IPPROTO_IPIP |
| 5026 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5027 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5028 | #ifdef IPPROTO_TCP |
| 5029 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5030 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5031 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5032 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5033 | #ifdef IPPROTO_EGP |
| 5034 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5035 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5036 | #ifdef IPPROTO_PUP |
| 5037 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5038 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5039 | #ifdef IPPROTO_UDP |
| 5040 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5041 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5042 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5043 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5044 | #ifdef IPPROTO_IDP |
| 5045 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5046 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5047 | #ifdef IPPROTO_HELLO |
| 5048 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5049 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5050 | #ifdef IPPROTO_ND |
| 5051 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5052 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5053 | #ifdef IPPROTO_TP |
| 5054 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5055 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5056 | #ifdef IPPROTO_IPV6 |
| 5057 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5058 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5059 | #ifdef IPPROTO_ROUTING |
| 5060 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5061 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5062 | #ifdef IPPROTO_FRAGMENT |
| 5063 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5064 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5065 | #ifdef IPPROTO_RSVP |
| 5066 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5067 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5068 | #ifdef IPPROTO_GRE |
| 5069 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5070 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5071 | #ifdef IPPROTO_ESP |
| 5072 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5073 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5074 | #ifdef IPPROTO_AH |
| 5075 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5076 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5077 | #ifdef IPPROTO_MOBILE |
| 5078 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5079 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5080 | #ifdef IPPROTO_ICMPV6 |
| 5081 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5082 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5083 | #ifdef IPPROTO_NONE |
| 5084 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5085 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5086 | #ifdef IPPROTO_DSTOPTS |
| 5087 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5088 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5089 | #ifdef IPPROTO_XTP |
| 5090 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5091 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5092 | #ifdef IPPROTO_EON |
| 5093 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5094 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5095 | #ifdef IPPROTO_PIM |
| 5096 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5097 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5098 | #ifdef IPPROTO_IPCOMP |
| 5099 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5100 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5101 | #ifdef IPPROTO_VRRP |
| 5102 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5103 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5104 | #ifdef IPPROTO_BIP |
| 5105 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5106 | #endif |
| 5107 | /**/ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5108 | #ifdef IPPROTO_RAW |
| 5109 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5110 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5111 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5112 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5113 | #ifdef IPPROTO_MAX |
| 5114 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5115 | #endif |
| 5116 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5117 | /* Some port configuration */ |
| 5118 | #ifdef IPPORT_RESERVED |
| 5119 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5120 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5121 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5122 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5123 | #ifdef IPPORT_USERRESERVED |
| 5124 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5125 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5126 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5127 | #endif |
| 5128 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5129 | /* Some reserved IP v.4 addresses */ |
| 5130 | #ifdef INADDR_ANY |
| 5131 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5132 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5133 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5134 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5135 | #ifdef INADDR_BROADCAST |
| 5136 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5137 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5138 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5139 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5140 | #ifdef INADDR_LOOPBACK |
| 5141 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5142 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5143 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5144 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5145 | #ifdef INADDR_UNSPEC_GROUP |
| 5146 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5147 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5148 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5149 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5150 | #ifdef INADDR_ALLHOSTS_GROUP |
| 5151 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 5152 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5153 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5154 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5155 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5156 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 5157 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 5158 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5159 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5160 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5161 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5162 | #ifdef INADDR_NONE |
| 5163 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5164 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5165 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5166 | #endif |
| 5167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5168 | /* IPv4 [gs]etsockopt options */ |
| 5169 | #ifdef IP_OPTIONS |
| 5170 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5171 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5172 | #ifdef IP_HDRINCL |
| 5173 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5174 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5175 | #ifdef IP_TOS |
| 5176 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5177 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5178 | #ifdef IP_TTL |
| 5179 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5180 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5181 | #ifdef IP_RECVOPTS |
| 5182 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5183 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5184 | #ifdef IP_RECVRETOPTS |
| 5185 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5186 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5187 | #ifdef IP_RECVDSTADDR |
| 5188 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5189 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5190 | #ifdef IP_RETOPTS |
| 5191 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5192 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5193 | #ifdef IP_MULTICAST_IF |
| 5194 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5195 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5196 | #ifdef IP_MULTICAST_TTL |
| 5197 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5198 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5199 | #ifdef IP_MULTICAST_LOOP |
| 5200 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5201 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5202 | #ifdef IP_ADD_MEMBERSHIP |
| 5203 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5204 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5205 | #ifdef IP_DROP_MEMBERSHIP |
| 5206 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5207 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5208 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 5209 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 5210 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5211 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5212 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 5213 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 5214 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5215 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5216 | #ifdef IP_MAX_MEMBERSHIPS |
| 5217 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5218 | #endif |
| 5219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5220 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 5221 | #ifdef IPV6_JOIN_GROUP |
| 5222 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5223 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5224 | #ifdef IPV6_LEAVE_GROUP |
| 5225 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5226 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5227 | #ifdef IPV6_MULTICAST_HOPS |
| 5228 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5229 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5230 | #ifdef IPV6_MULTICAST_IF |
| 5231 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5232 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5233 | #ifdef IPV6_MULTICAST_LOOP |
| 5234 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5235 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5236 | #ifdef IPV6_UNICAST_HOPS |
| 5237 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5238 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5239 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5240 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5241 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5242 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5243 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5244 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5245 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5246 | #endif |
| 5247 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5248 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5249 | #endif |
| 5250 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5251 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5252 | #endif |
| 5253 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5254 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5255 | #endif |
| 5256 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5257 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5258 | #endif |
| 5259 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5260 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5261 | #endif |
| 5262 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5263 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5264 | #endif |
| 5265 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5266 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5267 | #endif |
| 5268 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5269 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5270 | #endif |
| 5271 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5272 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5273 | #endif |
| 5274 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5275 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5276 | #endif |
| 5277 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5278 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5279 | #endif |
| 5280 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5281 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5282 | #endif |
| 5283 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5284 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5285 | #endif |
| 5286 | #ifdef IPV6_RTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5287 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5288 | #endif |
| 5289 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5290 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5291 | #endif |
| 5292 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5293 | 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] | 5294 | #endif |
| 5295 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5296 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5297 | #endif |
| 5298 | #ifdef IPV6_TCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5299 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5300 | #endif |
| 5301 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5302 | 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] | 5303 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5305 | /* TCP options */ |
| 5306 | #ifdef TCP_NODELAY |
| 5307 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5308 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5309 | #ifdef TCP_MAXSEG |
| 5310 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5311 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5312 | #ifdef TCP_CORK |
| 5313 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5314 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5315 | #ifdef TCP_KEEPIDLE |
| 5316 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5317 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5318 | #ifdef TCP_KEEPINTVL |
| 5319 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5320 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5321 | #ifdef TCP_KEEPCNT |
| 5322 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5323 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5324 | #ifdef TCP_SYNCNT |
| 5325 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5326 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5327 | #ifdef TCP_LINGER2 |
| 5328 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5329 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5330 | #ifdef TCP_DEFER_ACCEPT |
| 5331 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5332 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5333 | #ifdef TCP_WINDOW_CLAMP |
| 5334 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5335 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5336 | #ifdef TCP_INFO |
| 5337 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5338 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5339 | #ifdef TCP_QUICKACK |
| 5340 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5341 | #endif |
| 5342 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5344 | /* IPX options */ |
| 5345 | #ifdef IPX_TYPE |
| 5346 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5347 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5348 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5349 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5350 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5351 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5352 | #endif |
| 5353 | #ifdef EAI_AGAIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5354 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5355 | #endif |
| 5356 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5357 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5358 | #endif |
| 5359 | #ifdef EAI_FAIL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5360 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5361 | #endif |
| 5362 | #ifdef EAI_FAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5363 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5364 | #endif |
| 5365 | #ifdef EAI_MEMORY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5366 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5367 | #endif |
| 5368 | #ifdef EAI_NODATA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5369 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5370 | #endif |
| 5371 | #ifdef EAI_NONAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5372 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5373 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5374 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5375 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5376 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5377 | #ifdef EAI_SERVICE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5378 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5379 | #endif |
| 5380 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5381 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5382 | #endif |
| 5383 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5384 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5385 | #endif |
| 5386 | #ifdef EAI_BADHINTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5387 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5388 | #endif |
| 5389 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5390 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5391 | #endif |
| 5392 | #ifdef EAI_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5393 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5394 | #endif |
| 5395 | #ifdef AI_PASSIVE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5396 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5397 | #endif |
| 5398 | #ifdef AI_CANONNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5399 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5400 | #endif |
| 5401 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5402 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5403 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5404 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5405 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5406 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5407 | #ifdef AI_MASK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5408 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5409 | #endif |
| 5410 | #ifdef AI_ALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5411 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5412 | #endif |
| 5413 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5414 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5415 | #endif |
| 5416 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5417 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5418 | #endif |
| 5419 | #ifdef AI_V4MAPPED |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5420 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5421 | #endif |
| 5422 | #ifdef AI_DEFAULT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5423 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5424 | #endif |
| 5425 | #ifdef NI_MAXHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5426 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5427 | #endif |
| 5428 | #ifdef NI_MAXSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5429 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5430 | #endif |
| 5431 | #ifdef NI_NOFQDN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5432 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5433 | #endif |
| 5434 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5435 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5436 | #endif |
| 5437 | #ifdef NI_NAMEREQD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5438 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5439 | #endif |
| 5440 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5441 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5442 | #endif |
| 5443 | #ifdef NI_DGRAM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5444 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5445 | #endif |
| 5446 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5447 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5448 | #ifdef SHUT_RD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5449 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5450 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5451 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5452 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5453 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5454 | #endif |
| 5455 | #ifdef SHUT_WR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5456 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5457 | #elif defined(SD_SEND) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5458 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5459 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5460 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5461 | #endif |
| 5462 | #ifdef SHUT_RDWR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5463 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5464 | #elif defined(SD_BOTH) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5465 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5466 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5467 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5468 | #endif |
| 5469 | |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5470 | #ifdef SIO_RCVALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5471 | { |
| 5472 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 5473 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 5474 | int i; |
| 5475 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) { |
| 5476 | PyObject *tmp; |
| 5477 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 5478 | if (tmp == NULL) |
| 5479 | return; |
| 5480 | PyModule_AddObject(m, names[i], tmp); |
| 5481 | } |
| 5482 | } |
| 5483 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5484 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5485 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5486 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5487 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5488 | #endif |
| 5489 | #ifdef RCVALL_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5490 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5491 | #endif |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5492 | #endif /* _MSTCPIP_ */ |
| 5493 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5494 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5495 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5496 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5497 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5498 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5499 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5500 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5501 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5502 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5503 | |
| 5504 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5505 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5506 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5507 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5508 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5509 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5510 | if (af == AF_INET) { |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 5511 | #if (SIZEOF_INT != 4) |
| 5512 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5513 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5514 | unsigned int packed_addr; |
| 5515 | packed_addr = inet_addr(src); |
| 5516 | if (packed_addr == INADDR_NONE) |
| 5517 | return 0; |
| 5518 | memcpy(dst, &packed_addr, 4); |
| 5519 | return 1; |
| 5520 | } |
| 5521 | /* Should set errno to EAFNOSUPPORT */ |
| 5522 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5525 | const char * |
| 5526 | 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] | 5527 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5528 | if (af == AF_INET) { |
| 5529 | struct in_addr packed_addr; |
| 5530 | if (size < 16) |
| 5531 | /* Should set errno to ENOSPC. */ |
| 5532 | return NULL; |
| 5533 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5534 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5535 | } |
| 5536 | /* Should set errno to EAFNOSUPPORT */ |
| 5537 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5538 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5539 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5540 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5541 | #endif |