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__ |
Ned Deily | 1c2a7b5 | 2016-02-15 16:51:24 +1100 | [diff] [blame] | 85 | #include <AvailabilityMacros.h> |
| 86 | /* for getaddrinfo thread safety test on old versions of OS X */ |
| 87 | #ifndef MAC_OS_X_VERSION_10_5 |
| 88 | #define MAC_OS_X_VERSION_10_5 1050 |
| 89 | #endif |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 90 | /* |
| 91 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 92 | * that was build on 10.4 or later to work on that release, weak linking |
| 93 | * comes to the rescue. |
| 94 | */ |
| 95 | # pragma weak inet_aton |
| 96 | #endif |
| 97 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 98 | #include "Python.h" |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 99 | #include "structmember.h" |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 100 | #include "timefuncs.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | |
Victor Stinner | 465db3c | 2014-07-26 14:47:56 +0200 | [diff] [blame] | 102 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 103 | #define INVALID_SOCKET (-1) |
| 104 | #endif |
| 105 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 106 | #undef MAX |
| 107 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 108 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 109 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 110 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 111 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 112 | \n\ |
| 113 | Open a socket of the given type. The family argument specifies the\n\ |
| 114 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 115 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 116 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 117 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 118 | \n\ |
| 119 | A socket object represents one endpoint of a network connection.\n\ |
| 120 | \n\ |
| 121 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 122 | \n\ |
| 123 | accept() -- accept a connection, returning new socket and client address\n\ |
| 124 | bind(addr) -- bind the socket to a local address\n\ |
| 125 | close() -- close the socket\n\ |
| 126 | connect(addr) -- connect the socket to a remote address\n\ |
| 127 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
| 128 | dup() -- return a new socket object identical to the current one [*]\n\ |
| 129 | fileno() -- return underlying file descriptor\n\ |
| 130 | getpeername() -- return remote address [*]\n\ |
| 131 | getsockname() -- return local address\n\ |
| 132 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 133 | gettimeout() -- return timeout or None\n\ |
| 134 | listen(n) -- start listening for incoming connections\n\ |
| 135 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\ |
| 136 | recv(buflen[, flags]) -- receive data\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 137 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 138 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 139 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 140 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 141 | sendall(data[, flags]) -- send all data\n\ |
| 142 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 143 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 144 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 145 | setsockopt(level, optname, value) -- set socket options\n\ |
| 146 | settimeout(None | float) -- set or clear the timeout\n\ |
| 147 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 148 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 149 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 150 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 151 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 152 | I hope some day someone can clean this up please... */ |
| 153 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 154 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 155 | script doesn't get this right, so we hardcode some platform checks below. |
| 156 | On the other hand, not all Linux versions agree, so there the settings |
| 157 | computed by the configure script are needed! */ |
| 158 | |
| 159 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 160 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 161 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 162 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 163 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 165 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 166 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 169 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 170 | # if defined(_AIX) || defined(__osf__) |
| 171 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 172 | # elif defined(__sun) || defined(__sgi) |
| 173 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 174 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 175 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 176 | # else |
| 177 | # undef HAVE_GETHOSTBYNAME_R |
| 178 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 181 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 182 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 183 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 184 | #endif |
| 185 | |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 186 | /* To use __FreeBSD_version, __OpenBSD__, and __NetBSD_Version__ */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 187 | #ifdef HAVE_SYS_PARAM_H |
| 188 | #include <sys/param.h> |
| 189 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 190 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Ned Deily | 1c2a7b5 | 2016-02-15 16:51:24 +1100 | [diff] [blame] | 191 | (this includes the getaddrinfo emulation) protect access with a lock. |
| 192 | |
| 193 | getaddrinfo is thread-safe on Mac OS X 10.5 and later. Originally it was |
| 194 | a mix of code including an unsafe implementation from an old BSD's |
| 195 | libresolv. In 10.5 Apple reimplemented it as a safe IPC call to the |
| 196 | mDNSResponder process. 10.5 is the first be UNIX '03 certified, which |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 197 | includes the requirement that getaddrinfo be thread-safe. See issue #25924. |
Ned Deily | 1c2a7b5 | 2016-02-15 16:51:24 +1100 | [diff] [blame] | 198 | |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 199 | It's thread-safe in OpenBSD starting with 5.4, released Nov 2013: |
| 200 | http://www.openbsd.org/plus54.html |
| 201 | |
| 202 | It's thread-safe in NetBSD starting with 4.0, released Dec 2007: |
| 203 | |
| 204 | http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getaddrinfo.c.diff?r1=1.82&r2=1.83 |
| 205 | */ |
Ned Deily | 1c2a7b5 | 2016-02-15 16:51:24 +1100 | [diff] [blame] | 206 | #if defined(WITH_THREAD) && ( \ |
| 207 | (defined(__APPLE__) && \ |
| 208 | MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) || \ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 209 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 210 | (defined(__OpenBSD__) && OpenBSD+0 < 201311) || \ |
| 211 | (defined(__NetBSD__) && __NetBSD_Version__+0 < 400000000) || \ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 212 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 213 | #define USE_GETADDRINFO_LOCK |
| 214 | #endif |
| 215 | |
| 216 | #ifdef USE_GETADDRINFO_LOCK |
| 217 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 218 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 219 | #else |
| 220 | #define ACQUIRE_GETADDRINFO_LOCK |
| 221 | #define RELEASE_GETADDRINFO_LOCK |
| 222 | #endif |
| 223 | |
| 224 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 225 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 226 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 228 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 229 | # include <types.h> |
| 230 | # include <io.h> |
| 231 | # include <sys/ioctl.h> |
| 232 | # include <utils.h> |
| 233 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 236 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 237 | # include <ioctl.h> |
| 238 | #endif |
| 239 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 240 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 241 | # define INCL_DOS |
| 242 | # define INCL_DOSERRORS |
| 243 | # define INCL_NOPMAPI |
| 244 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 245 | #endif |
| 246 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 247 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 248 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 249 | functions are declared correctly if compiling with |
| 250 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 251 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 252 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 253 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 254 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 255 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 256 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 257 | #undef _XOPEN_SOURCE |
| 258 | #include <sys/socket.h> |
| 259 | #include <sys/types.h> |
| 260 | #include <netinet/in.h> |
| 261 | #ifdef _SS_ALIGNSIZE |
| 262 | #define HAVE_GETADDRINFO 1 |
| 263 | #define HAVE_GETNAMEINFO 1 |
| 264 | #endif |
| 265 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 266 | #define HAVE_INET_PTON |
| 267 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 268 | #endif |
| 269 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 270 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 271 | 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] | 272 | are just busted. Same thing for Solaris. */ |
| 273 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 274 | #define INET_ADDRSTRLEN 16 |
| 275 | #endif |
| 276 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 277 | /* Generic includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 278 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 280 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 281 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 282 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 283 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 284 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 285 | |
| 286 | /* Addressing includes */ |
| 287 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 288 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 289 | |
| 290 | /* Non-MS WINDOWS includes */ |
| 291 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 293 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 294 | # ifdef __BEOS__ |
| 295 | # include <net/netdb.h> |
| 296 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 297 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 298 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 299 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 300 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 301 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 302 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 303 | # ifndef RISCOS |
| 304 | # include <fcntl.h> |
| 305 | # else |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 306 | # include <sys/ioctl.h> |
| 307 | # include <socklib.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 308 | # define NO_DUP |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 309 | int h_errno; /* not used */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 310 | # define INET_ADDRSTRLEN 16 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 311 | # endif |
| 312 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 313 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 314 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 315 | /* MS_WINDOWS includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 316 | # ifdef HAVE_FCNTL_H |
| 317 | # include <fcntl.h> |
| 318 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 319 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 320 | #endif |
| 321 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 322 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 323 | |
| 324 | #ifndef offsetof |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 325 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 326 | #endif |
| 327 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 328 | #ifndef O_NONBLOCK |
| 329 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 330 | #endif |
| 331 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 332 | /* include Python's addrinfo.h unless it causes trouble */ |
| 333 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 334 | /* Do not include addinfo.h on some newer IRIX versions. |
| 335 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 336 | * for example, but not by 6.5.10. |
| 337 | */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 338 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 339 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 340 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 341 | */ |
| 342 | #else |
| 343 | # include "addrinfo.h" |
| 344 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 345 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 346 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 347 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 348 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 349 | 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] | 350 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 351 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 352 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 353 | #ifdef __APPLE__ |
| 354 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 355 | failure, so we must use the emulation instead of the libinfo |
| 356 | implementation. Unfortunately, performing an autoconf test |
| 357 | for this bug would require DNS access for the machine performing |
| 358 | the configuration, which is not acceptable. Therefore, we |
| 359 | determine the bug just by checking for __APPLE__. If this bug |
| 360 | gets ever fixed, perhaps checking for sys/version.h would be |
| 361 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 362 | #ifndef HAVE_GETNAMEINFO |
| 363 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 364 | Find to check for Jaguar is that it has getnameinfo(), which |
| 365 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 366 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 367 | #endif |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 368 | |
| 369 | #ifdef HAVE_INET_ATON |
| 370 | #define USE_INET_ATON_WEAKLINK |
| 371 | #endif |
| 372 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 373 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 374 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 375 | /* 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] | 376 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 377 | /* avoid clashes with the C library definition of the symbol. */ |
| 378 | #define getaddrinfo fake_getaddrinfo |
| 379 | #define gai_strerror fake_gai_strerror |
| 380 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 381 | #include "getaddrinfo.c" |
| 382 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 383 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 384 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 385 | #include "getnameinfo.c" |
| 386 | #endif |
| 387 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 388 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 389 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 390 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 391 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 392 | #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] | 393 | #endif |
| 394 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 395 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 396 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 397 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 398 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 399 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 400 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 401 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 402 | #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] | 403 | #endif |
| 404 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 405 | #ifndef SOCKETCLOSE |
| 406 | #define SOCKETCLOSE close |
| 407 | #endif |
| 408 | |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 409 | #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] | 410 | #define USE_BLUETOOTH 1 |
| 411 | #if defined(__FreeBSD__) |
| 412 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 413 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 414 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 415 | #define SOL_HCI SOL_HCI_RAW |
| 416 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 417 | #define sockaddr_l2 sockaddr_l2cap |
| 418 | #define sockaddr_rc sockaddr_rfcomm |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 419 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 420 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 421 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 422 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 423 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 424 | #define sockaddr_l2 sockaddr_bt |
| 425 | #define sockaddr_rc sockaddr_bt |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 426 | #define sockaddr_hci sockaddr_bt |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 427 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 428 | #define SOL_HCI BTPROTO_HCI |
| 429 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 430 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 431 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 432 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 433 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 434 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 435 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 436 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 437 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 438 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 439 | #endif |
| 440 | #endif |
| 441 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 442 | #ifdef __VMS |
| 443 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 444 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 445 | #endif |
| 446 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 447 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 448 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 449 | /* |
| 450 | * Constants for getnameinfo() |
| 451 | */ |
| 452 | #if !defined(NI_MAXHOST) |
| 453 | #define NI_MAXHOST 1025 |
| 454 | #endif |
| 455 | #if !defined(NI_MAXSERV) |
| 456 | #define NI_MAXSERV 32 |
| 457 | #endif |
| 458 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 459 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 460 | a Py prefix (or use CapitalizedWords). Later... */ |
| 461 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 462 | /* Global variable holding the exception type for errors detected |
| 463 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 464 | static PyObject *socket_error; |
| 465 | static PyObject *socket_herror; |
| 466 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 467 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 468 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 469 | #ifdef RISCOS |
| 470 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 471 | static int taskwindow; |
| 472 | #endif |
| 473 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 474 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 475 | The sock_type variable contains pointers to various functions, |
| 476 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 477 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 478 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 479 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 480 | #if defined(HAVE_POLL_H) |
| 481 | #include <poll.h> |
| 482 | #elif defined(HAVE_SYS_POLL_H) |
| 483 | #include <sys/poll.h> |
| 484 | #endif |
| 485 | |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 486 | #ifdef HAVE_POLL |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 487 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 488 | #define IS_SELECTABLE(s) 1 |
| 489 | /* 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] | 490 | #else |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 491 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 492 | * little white lie. */ |
| 493 | #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] | 494 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 495 | |
| 496 | static PyObject* |
| 497 | select_error(void) |
| 498 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 499 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 500 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 503 | #ifdef MS_WINDOWS |
| 504 | #ifndef WSAEAGAIN |
| 505 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 506 | #endif |
| 507 | #define CHECK_ERRNO(expected) \ |
| 508 | (WSAGetLastError() == WSA ## expected) |
| 509 | #else |
| 510 | #define CHECK_ERRNO(expected) \ |
| 511 | (errno == expected) |
| 512 | #endif |
| 513 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 514 | /* Convenience function to raise an error according to errno |
| 515 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 516 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 517 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 518 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 519 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 520 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 521 | int err_no = WSAGetLastError(); |
| 522 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 523 | recognizes the error codes used by both GetLastError() and |
| 524 | WSAGetLastError */ |
| 525 | if (err_no) |
| 526 | return PyErr_SetExcFromWindowsErr(socket_error, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 527 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 528 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 529 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | if (sock_errno() != NO_ERROR) { |
| 531 | APIRET rc; |
| 532 | ULONG msglen; |
| 533 | char outbuf[100]; |
| 534 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 536 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 537 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 538 | myerrorcode - SOCBASEERR + 26, |
| 539 | "mptn.msg", |
| 540 | &msglen); |
| 541 | if (rc == NO_ERROR) { |
| 542 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 543 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 544 | /* OS/2 doesn't guarantee a terminator */ |
| 545 | outbuf[msglen] = '\0'; |
| 546 | if (strlen(outbuf) > 0) { |
| 547 | /* If non-empty msg, trim CRLF */ |
| 548 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 549 | while (lastc > outbuf && |
| 550 | isspace(Py_CHARMASK(*lastc))) { |
| 551 | /* Trim trailing whitespace (CRLF) */ |
| 552 | *lastc-- = '\0'; |
| 553 | } |
| 554 | } |
| 555 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 556 | if (v != NULL) { |
| 557 | PyErr_SetObject(socket_error, v); |
| 558 | Py_DECREF(v); |
| 559 | } |
| 560 | return NULL; |
| 561 | } |
| 562 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 563 | #endif |
| 564 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 565 | #if defined(RISCOS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 566 | if (_inet_error.errnum != NULL) { |
| 567 | PyObject *v; |
| 568 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 569 | if (v != NULL) { |
| 570 | PyErr_SetObject(socket_error, v); |
| 571 | Py_DECREF(v); |
| 572 | } |
| 573 | return NULL; |
| 574 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 575 | #endif |
| 576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 577 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 580 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 581 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 582 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 583 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 584 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 585 | |
| 586 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 587 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 588 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 589 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 590 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 591 | if (v != NULL) { |
| 592 | PyErr_SetObject(socket_herror, v); |
| 593 | Py_DECREF(v); |
| 594 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 596 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
| 600 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 601 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 602 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 603 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 604 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 605 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 606 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 607 | if (error == EAI_SYSTEM) |
| 608 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 609 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 610 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 611 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 612 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 613 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 614 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 615 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 616 | if (v != NULL) { |
| 617 | PyErr_SetObject(socket_gaierror, v); |
| 618 | Py_DECREF(v); |
| 619 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 621 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 624 | #ifdef __VMS |
| 625 | /* Function to send in segments */ |
| 626 | static int |
| 627 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 628 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 629 | int n = 0; |
| 630 | int remaining = len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 632 | while (remaining > 0) { |
| 633 | unsigned int segment; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 635 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 636 | n = send(sock_fd, buf, segment, flags); |
| 637 | if (n < 0) { |
| 638 | return n; |
| 639 | } |
| 640 | remaining -= segment; |
| 641 | buf += segment; |
| 642 | } /* end while */ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 644 | return len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 645 | } |
| 646 | #endif |
| 647 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 648 | /* Function to perform the setting of socket blocking mode |
| 649 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 650 | static int |
| 651 | internal_setblocking(PySocketSockObject *s, int block) |
| 652 | { |
| 653 | #ifndef RISCOS |
| 654 | #ifndef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 655 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 656 | #endif |
| 657 | #endif |
| 658 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 659 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 660 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 661 | block = !block; |
| 662 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 663 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 664 | #else |
| 665 | #ifndef RISCOS |
| 666 | #ifndef MS_WINDOWS |
| 667 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 668 | block = !block; |
| 669 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 670 | #elif defined(__VMS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 671 | block = !block; |
| 672 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 673 | #else /* !PYOS_OS2 && !__VMS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 674 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 675 | if (block) |
| 676 | delay_flag &= (~O_NONBLOCK); |
| 677 | else |
| 678 | delay_flag |= O_NONBLOCK; |
| 679 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 680 | #endif /* !PYOS_OS2 */ |
| 681 | #else /* MS_WINDOWS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 682 | block = !block; |
| 683 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 684 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 685 | #else /* RISCOS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 686 | block = !block; |
| 687 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 688 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 689 | #endif /* __BEOS__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 690 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 691 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 692 | /* Since these don't return anything */ |
| 693 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 696 | /* 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] | 697 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 698 | This does not raise an exception; we'll let our caller do that |
| 699 | after they've reacquired the interpreter lock. |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 700 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 701 | static int |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 702 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 703 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 704 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 706 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 707 | if (s->sock_timeout <= 0.0) |
| 708 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 709 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 710 | /* Guard against closed socket */ |
| 711 | if (s->sock_fd < 0) |
| 712 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 713 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 714 | /* Handling this condition here simplifies the select loops */ |
| 715 | if (interval < 0.0) |
| 716 | return 1; |
| 717 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 718 | /* Prefer poll, if available, since you can poll() any fd |
| 719 | * which can't be done with select(). */ |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 720 | #ifdef HAVE_POLL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 721 | { |
| 722 | struct pollfd pollfd; |
| 723 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 725 | pollfd.fd = s->sock_fd; |
| 726 | pollfd.events = writing ? POLLOUT : POLLIN; |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 727 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 728 | /* s->sock_timeout is in seconds, timeout in ms */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 729 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 730 | n = poll(&pollfd, 1, timeout); |
| 731 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 732 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 733 | { |
| 734 | /* Construct the arguments to select */ |
| 735 | fd_set fds; |
| 736 | struct timeval tv; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 737 | tv.tv_sec = (int)interval; |
| 738 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 739 | FD_ZERO(&fds); |
| 740 | FD_SET(s->sock_fd, &fds); |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 741 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 742 | /* See if the socket is ready */ |
| 743 | if (writing) |
| 744 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 745 | else |
| 746 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 747 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 748 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 749 | |
| 750 | if (n < 0) |
| 751 | return -1; |
| 752 | if (n == 0) |
| 753 | return 1; |
| 754 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 757 | static int |
| 758 | internal_select(PySocketSockObject *s, int writing) |
| 759 | { |
| 760 | return internal_select_ex(s, writing, s->sock_timeout); |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | Two macros for automatic retry of select() in case of false positives |
| 765 | (for example, select() could indicate a socket is ready for reading |
| 766 | but the data then discarded by the OS because of a wrong checksum). |
| 767 | Here is an example of use: |
| 768 | |
| 769 | BEGIN_SELECT_LOOP(s) |
| 770 | Py_BEGIN_ALLOW_THREADS |
| 771 | timeout = internal_select_ex(s, 0, interval); |
| 772 | if (!timeout) |
| 773 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 774 | Py_END_ALLOW_THREADS |
| 775 | if (timeout == 1) { |
| 776 | PyErr_SetString(socket_timeout, "timed out"); |
| 777 | return -1; |
| 778 | } |
| 779 | END_SELECT_LOOP(s) |
| 780 | */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 781 | #define BEGIN_SELECT_LOOP(s) \ |
| 782 | { \ |
Benjamin Peterson | e5fa8b0 | 2016-07-07 00:24:26 -0700 | [diff] [blame] | 783 | double deadline = 0, interval = s->sock_timeout; \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 784 | int has_timeout = s->sock_timeout > 0.0; \ |
| 785 | if (has_timeout) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 786 | deadline = _PyTime_FloatTime() + s->sock_timeout; \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 787 | } \ |
| 788 | while (1) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 789 | errno = 0; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 790 | |
| 791 | #define END_SELECT_LOOP(s) \ |
| 792 | if (!has_timeout || \ |
| 793 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 794 | break; \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 795 | interval = deadline - _PyTime_FloatTime(); \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 796 | } \ |
Kristján Valur Jónsson | 620e364 | 2013-03-19 13:01:05 -0700 | [diff] [blame] | 797 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 798 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 799 | /* Initialize a new socket object. */ |
| 800 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 801 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 802 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 803 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 804 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 805 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 806 | { |
| 807 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 808 | int block = 1; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 809 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 810 | s->sock_fd = fd; |
| 811 | s->sock_family = family; |
| 812 | s->sock_type = type; |
| 813 | s->sock_proto = proto; |
| 814 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 815 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 816 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 818 | if (defaulttimeout >= 0.0) |
| 819 | internal_setblocking(s, 0); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 820 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 821 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 822 | if (taskwindow) |
| 823 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 824 | #endif |
| 825 | } |
| 826 | |
| 827 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 828 | /* Create a new socket object. |
| 829 | This just creates the object and initializes it. |
| 830 | If the creation fails, return NULL and set an exception (implicit |
| 831 | in NEWOBJ()). */ |
| 832 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 833 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 834 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 835 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 836 | PySocketSockObject *s; |
| 837 | s = (PySocketSockObject *) |
| 838 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 839 | if (s != NULL) |
| 840 | init_sockobject(s, fd, family, type, proto); |
| 841 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 845 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 846 | thread to be in gethostbyname or getaddrinfo */ |
| 847 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | c551776 | 2012-08-14 17:24:47 +0200 | [diff] [blame] | 848 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 849 | #endif |
| 850 | |
| 851 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 852 | /* Convert a string specifying a host name or one of a few symbolic |
| 853 | names to a numeric IP address. This usually calls gethostbyname() |
| 854 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 855 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 856 | an error occurred; then an exception is raised. */ |
| 857 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 858 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 859 | 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] | 860 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 861 | struct addrinfo hints, *res; |
| 862 | int error; |
| 863 | int d1, d2, d3, d4; |
| 864 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 866 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 867 | if (name[0] == '\0') { |
| 868 | int siz; |
| 869 | memset(&hints, 0, sizeof(hints)); |
| 870 | hints.ai_family = af; |
| 871 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 872 | hints.ai_flags = AI_PASSIVE; |
| 873 | Py_BEGIN_ALLOW_THREADS |
| 874 | ACQUIRE_GETADDRINFO_LOCK |
| 875 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 876 | Py_END_ALLOW_THREADS |
| 877 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 878 | *are* safe regarding their return value, ie. that a |
| 879 | subsequent call to getaddrinfo() does not destroy the |
| 880 | outcome of the first call. */ |
| 881 | RELEASE_GETADDRINFO_LOCK |
| 882 | if (error) { |
| 883 | set_gaierror(error); |
| 884 | return -1; |
| 885 | } |
| 886 | switch (res->ai_family) { |
| 887 | case AF_INET: |
| 888 | siz = 4; |
| 889 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 890 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 891 | case AF_INET6: |
| 892 | siz = 16; |
| 893 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 894 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 895 | default: |
| 896 | freeaddrinfo(res); |
| 897 | PyErr_SetString(socket_error, |
| 898 | "unsupported address family"); |
| 899 | return -1; |
| 900 | } |
| 901 | if (res->ai_next) { |
| 902 | freeaddrinfo(res); |
| 903 | PyErr_SetString(socket_error, |
| 904 | "wildcard resolved to multiple address"); |
| 905 | return -1; |
| 906 | } |
| 907 | if (res->ai_addrlen < addr_ret_size) |
| 908 | addr_ret_size = res->ai_addrlen; |
| 909 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 910 | freeaddrinfo(res); |
| 911 | return siz; |
| 912 | } |
| 913 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 914 | struct sockaddr_in *sin; |
| 915 | if (af != AF_INET && af != AF_UNSPEC) { |
| 916 | PyErr_SetString(socket_error, |
| 917 | "address family mismatched"); |
| 918 | return -1; |
| 919 | } |
| 920 | sin = (struct sockaddr_in *)addr_ret; |
| 921 | memset((void *) sin, '\0', sizeof(*sin)); |
| 922 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 923 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 924 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 925 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 926 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 927 | return sizeof(sin->sin_addr); |
| 928 | } |
| 929 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 930 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 931 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 932 | struct sockaddr_in *sin; |
| 933 | sin = (struct sockaddr_in *)addr_ret; |
| 934 | sin->sin_addr.s_addr = htonl( |
| 935 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 936 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 937 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 938 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 939 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 940 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 941 | return 4; |
| 942 | } |
| 943 | memset(&hints, 0, sizeof(hints)); |
| 944 | hints.ai_family = af; |
| 945 | Py_BEGIN_ALLOW_THREADS |
| 946 | ACQUIRE_GETADDRINFO_LOCK |
| 947 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 948 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 949 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 950 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 951 | if no address family is given. Assume IPv4 for now.*/ |
| 952 | hints.ai_family = AF_INET; |
| 953 | error = getaddrinfo(name, NULL, &hints, &res); |
| 954 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 955 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 956 | Py_END_ALLOW_THREADS |
| 957 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 958 | if (error) { |
| 959 | set_gaierror(error); |
| 960 | return -1; |
| 961 | } |
| 962 | if (res->ai_addrlen < addr_ret_size) |
| 963 | addr_ret_size = res->ai_addrlen; |
| 964 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 965 | freeaddrinfo(res); |
| 966 | switch (addr_ret->sa_family) { |
| 967 | case AF_INET: |
| 968 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 969 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 970 | case AF_INET6: |
| 971 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 972 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 973 | default: |
| 974 | PyErr_SetString(socket_error, "unknown address family"); |
| 975 | return -1; |
| 976 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 979 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 980 | /* Create a string object representing an IP address. |
| 981 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 982 | size numbers). */ |
| 983 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 984 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 985 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 986 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 987 | char buf[NI_MAXHOST]; |
| 988 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 990 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 991 | NI_NUMERICHOST); |
| 992 | if (error) { |
| 993 | set_gaierror(error); |
| 994 | return NULL; |
| 995 | } |
| 996 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1000 | #ifdef USE_BLUETOOTH |
| 1001 | /* Convert a string representation of a Bluetooth address into a numeric |
| 1002 | address. Returns the length (6), or raises an exception and returns -1 if |
| 1003 | an error occurred. */ |
| 1004 | |
| 1005 | static int |
| 1006 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 1007 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1008 | unsigned int b0, b1, b2, b3, b4, b5; |
| 1009 | char ch; |
| 1010 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1011 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1012 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 1013 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 1014 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 1015 | bdaddr->b[0] = b0; |
| 1016 | bdaddr->b[1] = b1; |
| 1017 | bdaddr->b[2] = b2; |
| 1018 | bdaddr->b[3] = b3; |
| 1019 | bdaddr->b[4] = b4; |
| 1020 | bdaddr->b[5] = b5; |
| 1021 | return 6; |
| 1022 | } else { |
| 1023 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 1024 | return -1; |
| 1025 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* Create a string representation of the Bluetooth address. This is always a |
| 1029 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 1030 | value (zero padded if necessary). */ |
| 1031 | |
| 1032 | static PyObject * |
| 1033 | makebdaddr(bdaddr_t *bdaddr) |
| 1034 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1035 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1036 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1037 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1038 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 1039 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 1040 | return PyString_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1041 | } |
| 1042 | #endif |
| 1043 | |
| 1044 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1045 | /* Create an object representing the given socket address, |
| 1046 | suitable for passing it back to bind(), connect() etc. |
| 1047 | The family field of the sockaddr structure is inspected |
| 1048 | to determine what kind of address it really is. */ |
| 1049 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1050 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1051 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1052 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1053 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1054 | if (addrlen == 0) { |
| 1055 | /* No address -- may be recvfrom() from known socket */ |
| 1056 | Py_INCREF(Py_None); |
| 1057 | return Py_None; |
| 1058 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1059 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1060 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1061 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
| 1062 | addr->sa_family = AF_INET; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1063 | #endif |
| 1064 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1065 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1067 | case AF_INET: |
| 1068 | { |
| 1069 | struct sockaddr_in *a; |
| 1070 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1071 | PyObject *ret = NULL; |
| 1072 | if (addrobj) { |
| 1073 | a = (struct sockaddr_in *)addr; |
| 1074 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1075 | Py_DECREF(addrobj); |
| 1076 | } |
| 1077 | return ret; |
| 1078 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1079 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1080 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1081 | case AF_UNIX: |
| 1082 | { |
| 1083 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1084 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1085 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1086 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 1087 | return PyString_FromStringAndSize(a->sun_path, |
| 1088 | addrlen); |
| 1089 | } |
| 1090 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1091 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1092 | { |
| 1093 | /* regular NULL-terminated string */ |
| 1094 | return PyString_FromString(a->sun_path); |
| 1095 | } |
| 1096 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1097 | #endif /* AF_UNIX */ |
| 1098 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1099 | #if defined(AF_NETLINK) |
| 1100 | case AF_NETLINK: |
| 1101 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1102 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1103 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1104 | } |
| 1105 | #endif /* AF_NETLINK */ |
| 1106 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1107 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1108 | case AF_INET6: |
| 1109 | { |
| 1110 | struct sockaddr_in6 *a; |
| 1111 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1112 | PyObject *ret = NULL; |
| 1113 | if (addrobj) { |
| 1114 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1115 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1116 | addrobj, |
| 1117 | ntohs(a->sin6_port), |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1118 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1119 | a->sin6_scope_id); |
| 1120 | Py_DECREF(addrobj); |
| 1121 | } |
| 1122 | return ret; |
| 1123 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1124 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1125 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1126 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1127 | case AF_BLUETOOTH: |
| 1128 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1130 | case BTPROTO_L2CAP: |
| 1131 | { |
| 1132 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1133 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1134 | PyObject *ret = NULL; |
| 1135 | if (addrobj) { |
| 1136 | ret = Py_BuildValue("Oi", |
| 1137 | addrobj, |
| 1138 | _BT_L2_MEMB(a, psm)); |
| 1139 | Py_DECREF(addrobj); |
| 1140 | } |
| 1141 | return ret; |
| 1142 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1143 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1144 | case BTPROTO_RFCOMM: |
| 1145 | { |
| 1146 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1147 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1148 | PyObject *ret = NULL; |
| 1149 | if (addrobj) { |
| 1150 | ret = Py_BuildValue("Oi", |
| 1151 | addrobj, |
| 1152 | _BT_RC_MEMB(a, channel)); |
| 1153 | Py_DECREF(addrobj); |
| 1154 | } |
| 1155 | return ret; |
| 1156 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1157 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1158 | case BTPROTO_HCI: |
| 1159 | { |
| 1160 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1161 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1162 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1163 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1164 | PyObject *ret = NULL; |
| 1165 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1166 | return ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1167 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1168 | } |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1169 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1170 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1171 | case BTPROTO_SCO: |
| 1172 | { |
| 1173 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1174 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1175 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1176 | #endif |
| 1177 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1178 | default: |
| 1179 | PyErr_SetString(PyExc_ValueError, |
| 1180 | "Unknown Bluetooth protocol"); |
| 1181 | return NULL; |
| 1182 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1183 | #endif |
| 1184 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1185 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1186 | case AF_PACKET: |
| 1187 | { |
| 1188 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1189 | char *ifname = ""; |
| 1190 | struct ifreq ifr; |
| 1191 | /* need to look up interface name give index */ |
| 1192 | if (a->sll_ifindex) { |
| 1193 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1194 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1195 | ifname = ifr.ifr_name; |
| 1196 | } |
| 1197 | return Py_BuildValue("shbhs#", |
| 1198 | ifname, |
| 1199 | ntohs(a->sll_protocol), |
| 1200 | a->sll_pkttype, |
| 1201 | a->sll_hatype, |
| 1202 | a->sll_addr, |
| 1203 | a->sll_halen); |
| 1204 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1205 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1206 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1207 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1208 | case AF_TIPC: |
| 1209 | { |
| 1210 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1211 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1212 | return Py_BuildValue("IIIII", |
| 1213 | a->addrtype, |
| 1214 | a->addr.nameseq.type, |
| 1215 | a->addr.nameseq.lower, |
| 1216 | a->addr.nameseq.upper, |
| 1217 | a->scope); |
| 1218 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1219 | return Py_BuildValue("IIIII", |
| 1220 | a->addrtype, |
| 1221 | a->addr.name.name.type, |
| 1222 | a->addr.name.name.instance, |
| 1223 | a->addr.name.name.instance, |
| 1224 | a->scope); |
| 1225 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1226 | return Py_BuildValue("IIIII", |
| 1227 | a->addrtype, |
| 1228 | a->addr.id.node, |
| 1229 | a->addr.id.ref, |
| 1230 | 0, |
| 1231 | a->scope); |
| 1232 | } else { |
| 1233 | PyErr_SetString(PyExc_ValueError, |
| 1234 | "Invalid address type"); |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1238 | #endif |
| 1239 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1240 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1242 | default: |
| 1243 | /* If we don't know the address family, don't raise an |
| 1244 | exception -- return it as a tuple. */ |
| 1245 | return Py_BuildValue("is#", |
| 1246 | addr->sa_family, |
| 1247 | addr->sa_data, |
| 1248 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1250 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1253 | |
| 1254 | /* Parse a socket address argument according to the socket object's |
| 1255 | address family. Return 1 if the address was in the proper format, |
| 1256 | 0 of not. The address is returned through addr_ret, its length |
| 1257 | through len_ret. */ |
| 1258 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1259 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1260 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1261 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1262 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1263 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1264 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1265 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1266 | case AF_UNIX: |
| 1267 | { |
| 1268 | struct sockaddr_un* addr; |
| 1269 | char *path; |
| 1270 | int len; |
| 1271 | if (!PyArg_Parse(args, "t#", &path, &len)) |
| 1272 | return 0; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1273 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1274 | addr = (struct sockaddr_un*)addr_ret; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1275 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1276 | if (len > 0 && path[0] == 0) { |
| 1277 | /* Linux abstract namespace extension */ |
| 1278 | if (len > sizeof addr->sun_path) { |
| 1279 | PyErr_SetString(socket_error, |
| 1280 | "AF_UNIX path too long"); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | } |
| 1284 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1285 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1286 | { |
| 1287 | /* regular NULL-terminated string */ |
| 1288 | if (len >= sizeof addr->sun_path) { |
| 1289 | PyErr_SetString(socket_error, |
| 1290 | "AF_UNIX path too long"); |
| 1291 | return 0; |
| 1292 | } |
| 1293 | addr->sun_path[len] = 0; |
| 1294 | } |
| 1295 | addr->sun_family = s->sock_family; |
| 1296 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1297 | #if defined(PYOS_OS2) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1298 | *len_ret = sizeof(*addr); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1299 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1300 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1301 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1302 | return 1; |
| 1303 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1304 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1305 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1306 | #if defined(AF_NETLINK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1307 | case AF_NETLINK: |
| 1308 | { |
| 1309 | struct sockaddr_nl* addr; |
| 1310 | int pid, groups; |
| 1311 | addr = (struct sockaddr_nl *)addr_ret; |
| 1312 | if (!PyTuple_Check(args)) { |
| 1313 | PyErr_Format( |
| 1314 | PyExc_TypeError, |
| 1315 | "getsockaddrarg: " |
| 1316 | "AF_NETLINK address must be tuple, not %.500s", |
| 1317 | Py_TYPE(args)->tp_name); |
| 1318 | return 0; |
| 1319 | } |
| 1320 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1321 | return 0; |
| 1322 | addr->nl_family = AF_NETLINK; |
| 1323 | addr->nl_pid = pid; |
| 1324 | addr->nl_groups = groups; |
| 1325 | *len_ret = sizeof(*addr); |
| 1326 | return 1; |
| 1327 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1328 | #endif |
| 1329 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1330 | case AF_INET: |
| 1331 | { |
| 1332 | struct sockaddr_in* addr; |
| 1333 | char *host; |
| 1334 | int port, result; |
| 1335 | if (!PyTuple_Check(args)) { |
| 1336 | PyErr_Format( |
| 1337 | PyExc_TypeError, |
| 1338 | "getsockaddrarg: " |
| 1339 | "AF_INET address must be tuple, not %.500s", |
| 1340 | Py_TYPE(args)->tp_name); |
| 1341 | return 0; |
| 1342 | } |
| 1343 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1344 | "idna", &host, &port)) |
| 1345 | return 0; |
| 1346 | addr=(struct sockaddr_in*)addr_ret; |
| 1347 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1348 | sizeof(*addr), AF_INET); |
| 1349 | PyMem_Free(host); |
| 1350 | if (result < 0) |
| 1351 | return 0; |
| 1352 | if (port < 0 || port > 0xffff) { |
| 1353 | PyErr_SetString( |
| 1354 | PyExc_OverflowError, |
| 1355 | "getsockaddrarg: port must be 0-65535."); |
| 1356 | return 0; |
| 1357 | } |
| 1358 | addr->sin_family = AF_INET; |
| 1359 | addr->sin_port = htons((short)port); |
| 1360 | *len_ret = sizeof *addr; |
| 1361 | return 1; |
| 1362 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1363 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1364 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1365 | case AF_INET6: |
| 1366 | { |
| 1367 | struct sockaddr_in6* addr; |
| 1368 | char *host; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1369 | int port, result; |
| 1370 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1371 | flowinfo = scope_id = 0; |
| 1372 | if (!PyTuple_Check(args)) { |
| 1373 | PyErr_Format( |
| 1374 | PyExc_TypeError, |
| 1375 | "getsockaddrarg: " |
| 1376 | "AF_INET6 address must be tuple, not %.500s", |
| 1377 | Py_TYPE(args)->tp_name); |
| 1378 | return 0; |
| 1379 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1380 | if (!PyArg_ParseTuple(args, "eti|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1381 | "idna", &host, &port, &flowinfo, |
| 1382 | &scope_id)) { |
| 1383 | return 0; |
| 1384 | } |
| 1385 | addr = (struct sockaddr_in6*)addr_ret; |
| 1386 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1387 | sizeof(*addr), AF_INET6); |
| 1388 | PyMem_Free(host); |
| 1389 | if (result < 0) |
| 1390 | return 0; |
| 1391 | if (port < 0 || port > 0xffff) { |
| 1392 | PyErr_SetString( |
| 1393 | PyExc_OverflowError, |
| 1394 | "getsockaddrarg: port must be 0-65535."); |
| 1395 | return 0; |
| 1396 | } |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 1397 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1398 | PyErr_SetString( |
| 1399 | PyExc_OverflowError, |
| 1400 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1401 | return 0; |
| 1402 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1403 | addr->sin6_family = s->sock_family; |
| 1404 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1405 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1406 | addr->sin6_scope_id = scope_id; |
| 1407 | *len_ret = sizeof *addr; |
| 1408 | return 1; |
| 1409 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1410 | #endif |
| 1411 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1412 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1413 | case AF_BLUETOOTH: |
| 1414 | { |
| 1415 | switch (s->sock_proto) { |
| 1416 | case BTPROTO_L2CAP: |
| 1417 | { |
| 1418 | struct sockaddr_l2 *addr; |
| 1419 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1421 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1422 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1423 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1424 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1425 | &_BT_L2_MEMB(addr, psm))) { |
| 1426 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1427 | "wrong format"); |
| 1428 | return 0; |
| 1429 | } |
| 1430 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1431 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1432 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1433 | *len_ret = sizeof *addr; |
| 1434 | return 1; |
| 1435 | } |
| 1436 | case BTPROTO_RFCOMM: |
| 1437 | { |
| 1438 | struct sockaddr_rc *addr; |
| 1439 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1441 | addr = (struct sockaddr_rc *)addr_ret; |
| 1442 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1443 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1444 | &_BT_RC_MEMB(addr, channel))) { |
| 1445 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1446 | "wrong format"); |
| 1447 | return 0; |
| 1448 | } |
| 1449 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1450 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1451 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1452 | *len_ret = sizeof *addr; |
| 1453 | return 1; |
| 1454 | } |
| 1455 | case BTPROTO_HCI: |
| 1456 | { |
| 1457 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1458 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1459 | char *straddr = PyBytes_AS_STRING(args); |
| 1460 | |
| 1461 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1462 | if (straddr == NULL) { |
| 1463 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1464 | "wrong format"); |
| 1465 | return 0; |
| 1466 | } |
| 1467 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1468 | return 0; |
| 1469 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1470 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1471 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1472 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1473 | "wrong format"); |
| 1474 | return 0; |
| 1475 | } |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1476 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1477 | *len_ret = sizeof *addr; |
| 1478 | return 1; |
| 1479 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1480 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1481 | case BTPROTO_SCO: |
| 1482 | { |
| 1483 | struct sockaddr_sco *addr; |
| 1484 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1485 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1486 | addr = (struct sockaddr_sco *)addr_ret; |
| 1487 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1488 | straddr = PyString_AsString(args); |
| 1489 | if (straddr == NULL) { |
| 1490 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1491 | "wrong format"); |
| 1492 | return 0; |
| 1493 | } |
| 1494 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1495 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1496 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1497 | *len_ret = sizeof *addr; |
| 1498 | return 1; |
| 1499 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1500 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1501 | default: |
| 1502 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1503 | return 0; |
| 1504 | } |
| 1505 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1506 | #endif |
| 1507 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1508 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1509 | case AF_PACKET: |
| 1510 | { |
| 1511 | struct sockaddr_ll* addr; |
| 1512 | struct ifreq ifr; |
| 1513 | char *interfaceName; |
| 1514 | int protoNumber; |
| 1515 | int hatype = 0; |
| 1516 | int pkttype = 0; |
| 1517 | char *haddr = NULL; |
| 1518 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1519 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1520 | if (!PyTuple_Check(args)) { |
| 1521 | PyErr_Format( |
| 1522 | PyExc_TypeError, |
| 1523 | "getsockaddrarg: " |
| 1524 | "AF_PACKET address must be tuple, not %.500s", |
| 1525 | Py_TYPE(args)->tp_name); |
| 1526 | return 0; |
| 1527 | } |
| 1528 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1529 | &protoNumber, &pkttype, &hatype, |
| 1530 | &haddr, &halen)) |
| 1531 | return 0; |
| 1532 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1533 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1534 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1535 | s->errorhandler(); |
| 1536 | return 0; |
| 1537 | } |
| 1538 | if (halen > 8) { |
| 1539 | PyErr_SetString(PyExc_ValueError, |
| 1540 | "Hardware address must be 8 bytes or less"); |
| 1541 | return 0; |
| 1542 | } |
| 1543 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1544 | PyErr_SetString( |
| 1545 | PyExc_OverflowError, |
| 1546 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1547 | return 0; |
| 1548 | } |
| 1549 | addr = (struct sockaddr_ll*)addr_ret; |
| 1550 | addr->sll_family = AF_PACKET; |
| 1551 | addr->sll_protocol = htons((short)protoNumber); |
| 1552 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1553 | addr->sll_pkttype = pkttype; |
| 1554 | addr->sll_hatype = hatype; |
| 1555 | if (halen != 0) { |
| 1556 | memcpy(&addr->sll_addr, haddr, halen); |
| 1557 | } |
| 1558 | addr->sll_halen = halen; |
| 1559 | *len_ret = sizeof *addr; |
| 1560 | return 1; |
| 1561 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1562 | #endif |
| 1563 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1564 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1565 | case AF_TIPC: |
| 1566 | { |
| 1567 | unsigned int atype, v1, v2, v3; |
| 1568 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1569 | struct sockaddr_tipc *addr; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1571 | if (!PyTuple_Check(args)) { |
| 1572 | PyErr_Format( |
| 1573 | PyExc_TypeError, |
| 1574 | "getsockaddrarg: " |
| 1575 | "AF_TIPC address must be tuple, not %.500s", |
| 1576 | Py_TYPE(args)->tp_name); |
| 1577 | return 0; |
| 1578 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1579 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1580 | if (!PyArg_ParseTuple(args, |
| 1581 | "IIII|I;Invalid TIPC address format", |
| 1582 | &atype, &v1, &v2, &v3, &scope)) |
| 1583 | return 0; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1585 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1586 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1588 | addr->family = AF_TIPC; |
| 1589 | addr->scope = scope; |
| 1590 | addr->addrtype = atype; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1591 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1592 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1593 | addr->addr.nameseq.type = v1; |
| 1594 | addr->addr.nameseq.lower = v2; |
| 1595 | addr->addr.nameseq.upper = v3; |
| 1596 | } else if (atype == TIPC_ADDR_NAME) { |
| 1597 | addr->addr.name.name.type = v1; |
| 1598 | addr->addr.name.name.instance = v2; |
| 1599 | } else if (atype == TIPC_ADDR_ID) { |
| 1600 | addr->addr.id.node = v1; |
| 1601 | addr->addr.id.ref = v2; |
| 1602 | } else { |
| 1603 | /* Shouldn't happen */ |
| 1604 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1605 | return 0; |
| 1606 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1608 | *len_ret = sizeof(*addr); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1609 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1610 | return 1; |
| 1611 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1612 | #endif |
| 1613 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1614 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1615 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1616 | default: |
| 1617 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1618 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1619 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1620 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1623 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1624 | /* 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] | 1625 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1626 | through len_ret. */ |
| 1627 | |
| 1628 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1629 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1630 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1631 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1632 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1633 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1634 | case AF_UNIX: |
| 1635 | { |
| 1636 | *len_ret = sizeof (struct sockaddr_un); |
| 1637 | return 1; |
| 1638 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1639 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1640 | #if defined(AF_NETLINK) |
| 1641 | case AF_NETLINK: |
| 1642 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1643 | *len_ret = sizeof (struct sockaddr_nl); |
| 1644 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1645 | } |
| 1646 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1648 | case AF_INET: |
| 1649 | { |
| 1650 | *len_ret = sizeof (struct sockaddr_in); |
| 1651 | return 1; |
| 1652 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1653 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1654 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1655 | case AF_INET6: |
| 1656 | { |
| 1657 | *len_ret = sizeof (struct sockaddr_in6); |
| 1658 | return 1; |
| 1659 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1660 | #endif |
| 1661 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1662 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1663 | case AF_BLUETOOTH: |
| 1664 | { |
| 1665 | switch(s->sock_proto) |
| 1666 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1667 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1668 | case BTPROTO_L2CAP: |
| 1669 | *len_ret = sizeof (struct sockaddr_l2); |
| 1670 | return 1; |
| 1671 | case BTPROTO_RFCOMM: |
| 1672 | *len_ret = sizeof (struct sockaddr_rc); |
| 1673 | return 1; |
| 1674 | case BTPROTO_HCI: |
| 1675 | *len_ret = sizeof (struct sockaddr_hci); |
| 1676 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1677 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1678 | case BTPROTO_SCO: |
| 1679 | *len_ret = sizeof (struct sockaddr_sco); |
| 1680 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1681 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1682 | default: |
| 1683 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1684 | "unknown BT protocol"); |
| 1685 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1687 | } |
| 1688 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1689 | #endif |
| 1690 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1691 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1692 | case AF_PACKET: |
| 1693 | { |
| 1694 | *len_ret = sizeof (struct sockaddr_ll); |
| 1695 | return 1; |
| 1696 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1697 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1698 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1699 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1700 | case AF_TIPC: |
| 1701 | { |
| 1702 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1703 | return 1; |
| 1704 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1705 | #endif |
| 1706 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1707 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1708 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1709 | default: |
| 1710 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1711 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1712 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1713 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1717 | /* s.accept() method */ |
| 1718 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1719 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1720 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1721 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1722 | sock_addr_t addrbuf; |
| 1723 | SOCKET_T newfd; |
| 1724 | socklen_t addrlen; |
| 1725 | PyObject *sock = NULL; |
| 1726 | PyObject *addr = NULL; |
| 1727 | PyObject *res = NULL; |
| 1728 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1730 | if (!getsockaddrlen(s, &addrlen)) |
| 1731 | return NULL; |
| 1732 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1734 | newfd = INVALID_SOCKET; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1735 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1736 | if (!IS_SELECTABLE(s)) |
| 1737 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1738 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1739 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1740 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1741 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1742 | if (!timeout) |
| 1743 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1744 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1745 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1746 | if (timeout == 1) { |
| 1747 | PyErr_SetString(socket_timeout, "timed out"); |
| 1748 | return NULL; |
| 1749 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1750 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1752 | if (newfd == INVALID_SOCKET) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1753 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1755 | /* Create the new object with unspecified family, |
| 1756 | to avoid calls to bind() etc. on it. */ |
| 1757 | sock = (PyObject *) new_sockobject(newfd, |
| 1758 | s->sock_family, |
| 1759 | s->sock_type, |
| 1760 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1762 | if (sock == NULL) { |
| 1763 | SOCKETCLOSE(newfd); |
| 1764 | goto finally; |
| 1765 | } |
| 1766 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1767 | addrlen, s->sock_proto); |
| 1768 | if (addr == NULL) |
| 1769 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1771 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1772 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1773 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1774 | Py_XDECREF(sock); |
| 1775 | Py_XDECREF(addr); |
| 1776 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1779 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1780 | "accept() -> (socket object, address info)\n\ |
| 1781 | \n\ |
| 1782 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1783 | 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] | 1784 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1785 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1786 | /* s.setblocking(flag) method. Argument: |
| 1787 | False -- non-blocking mode; same as settimeout(0) |
| 1788 | True -- blocking mode; same as settimeout(None) |
| 1789 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1790 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1791 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1792 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1793 | { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 1794 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1795 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1796 | block = PyInt_AsLong(arg); |
| 1797 | if (block == -1 && PyErr_Occurred()) |
| 1798 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1800 | s->sock_timeout = block ? -1.0 : 0.0; |
| 1801 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1803 | Py_INCREF(Py_None); |
| 1804 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1805 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1807 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1808 | "setblocking(flag)\n\ |
| 1809 | \n\ |
| 1810 | 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] | 1811 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1812 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1813 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1814 | /* s.settimeout(timeout) method. Argument: |
| 1815 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1816 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1817 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1818 | < 0 -- illegal; raises an exception |
| 1819 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1820 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1821 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1822 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1823 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1824 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1825 | if (arg == Py_None) |
| 1826 | timeout = -1.0; |
| 1827 | else { |
| 1828 | timeout = PyFloat_AsDouble(arg); |
| 1829 | if (timeout < 0.0) { |
| 1830 | if (!PyErr_Occurred()) |
| 1831 | PyErr_SetString(PyExc_ValueError, |
| 1832 | "Timeout value out of range"); |
| 1833 | return NULL; |
| 1834 | } |
| 1835 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1836 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1837 | s->sock_timeout = timeout; |
| 1838 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1839 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1840 | Py_INCREF(Py_None); |
| 1841 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1844 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1845 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1846 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1847 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1848 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1849 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1850 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1851 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1852 | /* s.gettimeout() method. |
| 1853 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1854 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1855 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1856 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1857 | if (s->sock_timeout < 0.0) { |
| 1858 | Py_INCREF(Py_None); |
| 1859 | return Py_None; |
| 1860 | } |
| 1861 | else |
| 1862 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1865 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1866 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1867 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 1868 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1869 | 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] | 1870 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1871 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1872 | #ifdef RISCOS |
| 1873 | /* s.sleeptaskw(1 | 0) method */ |
| 1874 | |
| 1875 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1876 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1877 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1878 | int block; |
| 1879 | block = PyInt_AsLong(arg); |
| 1880 | if (block == -1 && PyErr_Occurred()) |
| 1881 | return NULL; |
| 1882 | Py_BEGIN_ALLOW_THREADS |
| 1883 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1884 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1885 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1886 | Py_INCREF(Py_None); |
| 1887 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1888 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1889 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1890 | "sleeptaskw(flag)\n\ |
| 1891 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1892 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1893 | #endif |
| 1894 | |
| 1895 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1896 | /* s.setsockopt() method. |
| 1897 | With an integer third argument, sets an integer option. |
| 1898 | With a string third argument, sets an option from a buffer; |
| 1899 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1900 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1901 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1902 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1903 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1904 | int level; |
| 1905 | int optname; |
| 1906 | int res; |
| 1907 | char *buf; |
| 1908 | int buflen; |
| 1909 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1910 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1911 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1912 | &level, &optname, &flag)) { |
| 1913 | buf = (char *) &flag; |
| 1914 | buflen = sizeof flag; |
| 1915 | } |
| 1916 | else { |
| 1917 | PyErr_Clear(); |
| 1918 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1919 | &level, &optname, &buf, &buflen)) |
| 1920 | return NULL; |
| 1921 | } |
| 1922 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1923 | if (res < 0) |
| 1924 | return s->errorhandler(); |
| 1925 | Py_INCREF(Py_None); |
| 1926 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1929 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1930 | "setsockopt(level, option, value)\n\ |
| 1931 | \n\ |
| 1932 | 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] | 1933 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1934 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1935 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1936 | /* s.getsockopt() method. |
| 1937 | With two arguments, retrieves an integer option. |
| 1938 | With a third integer argument, retrieves a string buffer of that size; |
| 1939 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1940 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1941 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1942 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1943 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1944 | int level; |
| 1945 | int optname; |
| 1946 | int res; |
| 1947 | PyObject *buf; |
| 1948 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1949 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1950 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1951 | /* We have incomplete socket support. */ |
| 1952 | PyErr_SetString(socket_error, "getsockopt not supported"); |
| 1953 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1954 | #else |
| 1955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1956 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1957 | &level, &optname, &buflen)) |
| 1958 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1960 | if (buflen == 0) { |
| 1961 | int flag = 0; |
| 1962 | socklen_t flagsize = sizeof flag; |
| 1963 | res = getsockopt(s->sock_fd, level, optname, |
| 1964 | (void *)&flag, &flagsize); |
| 1965 | if (res < 0) |
| 1966 | return s->errorhandler(); |
| 1967 | return PyInt_FromLong(flag); |
| 1968 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1969 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1970 | /* socklen_t is unsigned so no negative test is needed, |
| 1971 | test buflen == 0 is previously done */ |
| 1972 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1973 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1974 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1975 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1976 | PyErr_SetString(socket_error, |
| 1977 | "getsockopt buflen out of range"); |
| 1978 | return NULL; |
| 1979 | } |
| 1980 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
| 1981 | if (buf == NULL) |
| 1982 | return NULL; |
| 1983 | res = getsockopt(s->sock_fd, level, optname, |
| 1984 | (void *)PyString_AS_STRING(buf), &buflen); |
| 1985 | if (res < 0) { |
| 1986 | Py_DECREF(buf); |
| 1987 | return s->errorhandler(); |
| 1988 | } |
| 1989 | _PyString_Resize(&buf, buflen); |
| 1990 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1991 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1994 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1995 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1996 | \n\ |
| 1997 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1998 | 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] | 1999 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2000 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2001 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2002 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2003 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2004 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2005 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2006 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2007 | sock_addr_t addrbuf; |
| 2008 | int addrlen; |
| 2009 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2010 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2011 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2012 | return NULL; |
| 2013 | Py_BEGIN_ALLOW_THREADS |
| 2014 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2015 | Py_END_ALLOW_THREADS |
| 2016 | if (res < 0) |
| 2017 | return s->errorhandler(); |
| 2018 | Py_INCREF(Py_None); |
| 2019 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2022 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2023 | "bind(address)\n\ |
| 2024 | \n\ |
| 2025 | 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] | 2026 | 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] | 2027 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2029 | |
| 2030 | /* s.close() method. |
| 2031 | Set the file descriptor to -1 so operations tried subsequently |
| 2032 | will surely fail. */ |
| 2033 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2034 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2035 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2036 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2037 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2039 | if ((fd = s->sock_fd) != -1) { |
| 2040 | s->sock_fd = -1; |
| 2041 | Py_BEGIN_ALLOW_THREADS |
| 2042 | (void) SOCKETCLOSE(fd); |
| 2043 | Py_END_ALLOW_THREADS |
| 2044 | } |
| 2045 | Py_INCREF(Py_None); |
| 2046 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2049 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2050 | "close()\n\ |
| 2051 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2052 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2053 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2054 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2055 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2056 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2057 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2058 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2059 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2060 | timeout = 0; |
| 2061 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2062 | |
| 2063 | #ifdef MS_WINDOWS |
| 2064 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2065 | if (s->sock_timeout > 0.0) { |
| 2066 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2067 | IS_SELECTABLE(s)) { |
| 2068 | /* This is a mess. Best solution: trust select */ |
| 2069 | fd_set fds; |
| 2070 | fd_set fds_exc; |
| 2071 | struct timeval tv; |
| 2072 | tv.tv_sec = (int)s->sock_timeout; |
| 2073 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2074 | FD_ZERO(&fds); |
| 2075 | FD_SET(s->sock_fd, &fds); |
| 2076 | FD_ZERO(&fds_exc); |
| 2077 | FD_SET(s->sock_fd, &fds_exc); |
| 2078 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
| 2079 | if (res == 0) { |
| 2080 | res = WSAEWOULDBLOCK; |
| 2081 | timeout = 1; |
| 2082 | } else if (res > 0) { |
| 2083 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2084 | /* The socket is in the writeable set - this |
| 2085 | means connected */ |
| 2086 | res = 0; |
| 2087 | else { |
| 2088 | /* As per MS docs, we need to call getsockopt() |
| 2089 | to get the underlying error */ |
| 2090 | int res_size = sizeof res; |
| 2091 | /* It must be in the exception set */ |
| 2092 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2093 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2094 | (char *)&res, &res_size)) |
| 2095 | /* getsockopt also clears WSAGetLastError, |
| 2096 | so reset it back. */ |
| 2097 | WSASetLastError(res); |
| 2098 | else |
| 2099 | res = WSAGetLastError(); |
| 2100 | } |
| 2101 | } |
| 2102 | /* else if (res < 0) an error occurred */ |
| 2103 | } |
| 2104 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2105 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2106 | if (res < 0) |
| 2107 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2108 | |
| 2109 | #else |
| 2110 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2111 | if (s->sock_timeout > 0.0) { |
| 2112 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2113 | timeout = internal_select(s, 1); |
| 2114 | if (timeout == 0) { |
| 2115 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2116 | use getsockopt(SO_ERROR) to get the real |
| 2117 | error. */ |
| 2118 | socklen_t res_size = sizeof res; |
| 2119 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2120 | SO_ERROR, &res, &res_size); |
| 2121 | if (res == EISCONN) |
| 2122 | res = 0; |
| 2123 | errno = res; |
| 2124 | } |
| 2125 | else if (timeout == -1) { |
| 2126 | res = errno; /* had error */ |
| 2127 | } |
| 2128 | else |
| 2129 | res = EWOULDBLOCK; /* timed out */ |
| 2130 | } |
| 2131 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2133 | if (res < 0) |
| 2134 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2135 | |
| 2136 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2137 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2138 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2139 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2140 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2141 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2142 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2143 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2144 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2145 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2146 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2147 | sock_addr_t addrbuf; |
| 2148 | int addrlen; |
| 2149 | int res; |
| 2150 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2151 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2152 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2153 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2154 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2155 | Py_BEGIN_ALLOW_THREADS |
| 2156 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2157 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2159 | if (timeout == 1) { |
| 2160 | PyErr_SetString(socket_timeout, "timed out"); |
| 2161 | return NULL; |
| 2162 | } |
| 2163 | if (res != 0) |
| 2164 | return s->errorhandler(); |
| 2165 | Py_INCREF(Py_None); |
| 2166 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2169 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2170 | "connect(address)\n\ |
| 2171 | \n\ |
| 2172 | 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] | 2173 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2174 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2175 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2176 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2177 | |
| 2178 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2179 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2180 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2181 | sock_addr_t addrbuf; |
| 2182 | int addrlen; |
| 2183 | int res; |
| 2184 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2186 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2187 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2188 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2189 | Py_BEGIN_ALLOW_THREADS |
| 2190 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2191 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2192 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2193 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2194 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2195 | #ifdef EINTR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2196 | if (res == EINTR && PyErr_CheckSignals()) |
| 2197 | return NULL; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2198 | #endif |
| 2199 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2200 | return PyInt_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2203 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2204 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2205 | \n\ |
| 2206 | 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] | 2207 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2208 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2210 | /* s.fileno() method */ |
| 2211 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2212 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2213 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2214 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2215 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2216 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2217 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2218 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2219 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2222 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2223 | "fileno() -> integer\n\ |
| 2224 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2225 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2226 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2227 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2228 | #ifndef NO_DUP |
| 2229 | /* s.dup() method */ |
| 2230 | |
| 2231 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2232 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2233 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2234 | SOCKET_T newfd; |
| 2235 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2236 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2237 | newfd = dup(s->sock_fd); |
| 2238 | if (newfd < 0) |
| 2239 | return s->errorhandler(); |
| 2240 | sock = (PyObject *) new_sockobject(newfd, |
| 2241 | s->sock_family, |
| 2242 | s->sock_type, |
| 2243 | s->sock_proto); |
| 2244 | if (sock == NULL) |
| 2245 | SOCKETCLOSE(newfd); |
| 2246 | return sock; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2247 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2249 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2250 | "dup() -> socket object\n\ |
| 2251 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2252 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2253 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2254 | #endif |
| 2255 | |
| 2256 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2257 | /* s.getsockname() method */ |
| 2258 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2259 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2260 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2261 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2262 | sock_addr_t addrbuf; |
| 2263 | int res; |
| 2264 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2266 | if (!getsockaddrlen(s, &addrlen)) |
| 2267 | return NULL; |
| 2268 | memset(&addrbuf, 0, addrlen); |
| 2269 | Py_BEGIN_ALLOW_THREADS |
| 2270 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2271 | Py_END_ALLOW_THREADS |
| 2272 | if (res < 0) |
| 2273 | return s->errorhandler(); |
| 2274 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2275 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2278 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2279 | "getsockname() -> address info\n\ |
| 2280 | \n\ |
| 2281 | 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] | 2282 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2283 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2284 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2285 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2286 | /* s.getpeername() method */ |
| 2287 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2288 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2289 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2290 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2291 | sock_addr_t addrbuf; |
| 2292 | int res; |
| 2293 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2295 | if (!getsockaddrlen(s, &addrlen)) |
| 2296 | return NULL; |
| 2297 | memset(&addrbuf, 0, addrlen); |
| 2298 | Py_BEGIN_ALLOW_THREADS |
| 2299 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2300 | Py_END_ALLOW_THREADS |
| 2301 | if (res < 0) |
| 2302 | return s->errorhandler(); |
| 2303 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2304 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2305 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2307 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2308 | "getpeername() -> address info\n\ |
| 2309 | \n\ |
| 2310 | 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] | 2311 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2313 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2314 | |
| 2315 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2316 | /* s.listen(n) method */ |
| 2317 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2318 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2319 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2320 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2321 | int backlog; |
| 2322 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2323 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2324 | backlog = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2325 | if (backlog == -1 && PyErr_Occurred()) |
| 2326 | return NULL; |
| 2327 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2328 | /* To avoid problems on systems that don't allow a negative backlog |
| 2329 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2330 | if (backlog < 0) |
| 2331 | backlog = 0; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2332 | res = listen(s->sock_fd, backlog); |
| 2333 | Py_END_ALLOW_THREADS |
| 2334 | if (res < 0) |
| 2335 | return s->errorhandler(); |
| 2336 | Py_INCREF(Py_None); |
| 2337 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2340 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2341 | "listen(backlog)\n\ |
| 2342 | \n\ |
| 2343 | 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] | 2344 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
| 2345 | unaccepted connections that the system will allow before refusing new\n\ |
| 2346 | connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2347 | |
| 2348 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2349 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2350 | /* s.makefile(mode) method. |
| 2351 | Create a new open file object referring to a dupped version of |
| 2352 | the socket's file descriptor. (The dup() call is necessary so |
| 2353 | that the open file and socket objects may be closed independent |
| 2354 | of each other.) |
| 2355 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 2356 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2357 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2358 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2359 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2360 | extern int fclose(FILE *); |
| 2361 | char *mode = "r"; |
| 2362 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2363 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2364 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2365 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2366 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2367 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2368 | FILE *fp; |
| 2369 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2370 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2371 | char *mode_r = "r"; |
| 2372 | char *mode_w = "w"; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2373 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
| 2376 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2377 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2378 | if (strcmp(mode,"rb") == 0) { |
| 2379 | mode = mode_r; |
| 2380 | } |
| 2381 | else { |
| 2382 | if (strcmp(mode,"wb") == 0) { |
| 2383 | mode = mode_w; |
| 2384 | } |
| 2385 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2386 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2387 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2388 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 2389 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2390 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2391 | 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] | 2392 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2393 | { |
| 2394 | if (fd >= 0) |
| 2395 | SOCKETCLOSE(fd); |
| 2396 | return s->errorhandler(); |
| 2397 | } |
| 2398 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 2399 | if (f != NULL) |
| 2400 | PyFile_SetBufSize(f, bufsize); |
| 2401 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2402 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2404 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2405 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 2406 | \n\ |
| 2407 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2408 | 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] | 2409 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2410 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2411 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2412 | /* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2413 | * 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] | 2414 | * 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] | 2415 | * 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] | 2416 | * 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] | 2417 | * also possible that we return a number of bytes smaller than the request |
| 2418 | * bytes. |
| 2419 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2420 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2421 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2422 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2423 | ssize_t outlen = -1; |
| 2424 | int timeout; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2425 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2426 | int remaining; |
| 2427 | char *read_buf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2428 | #endif |
| 2429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2430 | if (!IS_SELECTABLE(s)) { |
| 2431 | select_error(); |
| 2432 | return -1; |
| 2433 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2434 | |
| 2435 | #ifndef __VMS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2436 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2437 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2438 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2439 | if (!timeout) |
| 2440 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2441 | Py_END_ALLOW_THREADS |
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 | if (timeout == 1) { |
| 2444 | PyErr_SetString(socket_timeout, "timed out"); |
| 2445 | return -1; |
| 2446 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2447 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2448 | if (outlen < 0) { |
| 2449 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2450 | NULL, so ignore its return value */ |
| 2451 | s->errorhandler(); |
| 2452 | return -1; |
| 2453 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2454 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2455 | read_buf = cbuf; |
| 2456 | remaining = len; |
| 2457 | while (remaining != 0) { |
| 2458 | unsigned int segment; |
| 2459 | int nread = -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2460 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2461 | segment = remaining /SEGMENT_SIZE; |
| 2462 | if (segment != 0) { |
| 2463 | segment = SEGMENT_SIZE; |
| 2464 | } |
| 2465 | else { |
| 2466 | segment = remaining; |
| 2467 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2468 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2469 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2470 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2471 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2472 | if (!timeout) |
| 2473 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2474 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2475 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2476 | if (timeout == 1) { |
| 2477 | PyErr_SetString(socket_timeout, "timed out"); |
| 2478 | return -1; |
| 2479 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2480 | END_SELECT_LOOP(s) |
| 2481 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2482 | if (nread < 0) { |
| 2483 | s->errorhandler(); |
| 2484 | return -1; |
| 2485 | } |
| 2486 | if (nread != remaining) { |
| 2487 | read_buf += nread; |
| 2488 | break; |
| 2489 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2490 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2491 | remaining -= segment; |
| 2492 | read_buf += segment; |
| 2493 | } |
| 2494 | outlen = read_buf - cbuf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2495 | #endif /* !__VMS */ |
| 2496 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2497 | return outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2500 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2501 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2502 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2503 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2504 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2505 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2506 | int recvlen, flags = 0; |
| 2507 | ssize_t outlen; |
| 2508 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2509 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2510 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
| 2511 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2512 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2513 | if (recvlen < 0) { |
| 2514 | PyErr_SetString(PyExc_ValueError, |
| 2515 | "negative buffersize in recv"); |
| 2516 | return NULL; |
| 2517 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2519 | /* Allocate a new string. */ |
| 2520 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2521 | if (buf == NULL) |
| 2522 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2523 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2524 | /* Call the guts */ |
| 2525 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags); |
| 2526 | if (outlen < 0) { |
| 2527 | /* An error occurred, release the string and return an |
| 2528 | error. */ |
| 2529 | Py_DECREF(buf); |
| 2530 | return NULL; |
| 2531 | } |
| 2532 | if (outlen != recvlen) { |
| 2533 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2534 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2535 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2536 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2537 | return NULL; |
| 2538 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2540 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2543 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2544 | "recv(buffersize[, flags]) -> data\n\ |
| 2545 | \n\ |
| 2546 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2547 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2548 | 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] | 2549 | 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] | 2550 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2551 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2552 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2553 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2554 | static PyObject* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2555 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2556 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2557 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2558 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2559 | int recvlen = 0, flags = 0; |
| 2560 | ssize_t readlen; |
| 2561 | Py_buffer buf; |
| 2562 | Py_ssize_t buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2564 | /* Get the buffer's memory */ |
| 2565 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist, |
| 2566 | &buf, &recvlen, &flags)) |
| 2567 | return NULL; |
| 2568 | buflen = buf.len; |
| 2569 | assert(buf.buf != 0 && buflen > 0); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2571 | if (recvlen < 0) { |
| 2572 | PyErr_SetString(PyExc_ValueError, |
| 2573 | "negative buffersize in recv_into"); |
| 2574 | goto error; |
| 2575 | } |
| 2576 | if (recvlen == 0) { |
| 2577 | /* If nbytes was not specified, use the buffer's length */ |
| 2578 | recvlen = buflen; |
| 2579 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2581 | /* Check if the buffer is large enough */ |
| 2582 | if (buflen < recvlen) { |
| 2583 | PyErr_SetString(PyExc_ValueError, |
| 2584 | "buffer too small for requested bytes"); |
| 2585 | goto error; |
| 2586 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2588 | /* Call the guts */ |
| 2589 | readlen = sock_recv_guts(s, buf.buf, recvlen, flags); |
| 2590 | if (readlen < 0) { |
| 2591 | /* Return an error. */ |
| 2592 | goto error; |
| 2593 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2594 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2595 | PyBuffer_Release(&buf); |
| 2596 | /* Return the number of bytes read. Note that we do not do anything |
| 2597 | special here in the case that readlen < recvlen. */ |
| 2598 | return PyInt_FromSsize_t(readlen); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2599 | |
| 2600 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2601 | PyBuffer_Release(&buf); |
| 2602 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2605 | PyDoc_STRVAR(recv_into_doc, |
| 2606 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2607 | \n\ |
| 2608 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2609 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2610 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2611 | \n\ |
| 2612 | See recv() for documentation about the flags."); |
| 2613 | |
| 2614 | |
| 2615 | /* |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2616 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2617 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2618 | * 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] | 2619 | * 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] | 2620 | * that it is also possible that we return a number of bytes smaller than the |
| 2621 | * request bytes. |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2622 | * |
| 2623 | * 'addr' is a return value for the address object. Note that you must decref |
| 2624 | * it yourself. |
| 2625 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2626 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2627 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2628 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2629 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2630 | sock_addr_t addrbuf; |
| 2631 | int timeout; |
| 2632 | ssize_t n = -1; |
| 2633 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2634 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2635 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2636 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2637 | if (!getsockaddrlen(s, &addrlen)) |
| 2638 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2639 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2640 | if (!IS_SELECTABLE(s)) { |
| 2641 | select_error(); |
| 2642 | return -1; |
| 2643 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2644 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2645 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2646 | Py_BEGIN_ALLOW_THREADS |
| 2647 | memset(&addrbuf, 0, addrlen); |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2648 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2649 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2650 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2651 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2652 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2653 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2654 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2655 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2656 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2657 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2658 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2659 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2660 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2661 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2662 | } |
| 2663 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2665 | if (timeout == 1) { |
| 2666 | PyErr_SetString(socket_timeout, "timed out"); |
| 2667 | return -1; |
| 2668 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2669 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2670 | if (n < 0) { |
| 2671 | s->errorhandler(); |
| 2672 | return -1; |
| 2673 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2674 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2675 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2676 | addrlen, s->sock_proto))) |
| 2677 | return -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2679 | return n; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2683 | |
| 2684 | static PyObject * |
| 2685 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2686 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2687 | PyObject *buf = NULL; |
| 2688 | PyObject *addr = NULL; |
| 2689 | PyObject *ret = NULL; |
| 2690 | int recvlen, flags = 0; |
| 2691 | ssize_t outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2693 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
| 2694 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2696 | if (recvlen < 0) { |
| 2697 | PyErr_SetString(PyExc_ValueError, |
| 2698 | "negative buffersize in recvfrom"); |
| 2699 | return NULL; |
| 2700 | } |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 2701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2702 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2703 | if (buf == NULL) |
| 2704 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2706 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf), |
| 2707 | recvlen, flags, &addr); |
| 2708 | if (outlen < 0) { |
| 2709 | goto finally; |
| 2710 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2712 | if (outlen != recvlen) { |
| 2713 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2714 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2715 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2716 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2717 | goto finally; |
| 2718 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2720 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2721 | |
| 2722 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2723 | Py_XDECREF(buf); |
| 2724 | Py_XDECREF(addr); |
| 2725 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2728 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2729 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2730 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2731 | 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] | 2732 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2733 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2734 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2735 | |
| 2736 | static PyObject * |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2737 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2738 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2739 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2740 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2741 | int recvlen = 0, flags = 0; |
| 2742 | ssize_t readlen; |
| 2743 | Py_buffer buf; |
| 2744 | int buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2745 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2746 | PyObject *addr = NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2747 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2748 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into", |
| 2749 | kwlist, &buf, |
| 2750 | &recvlen, &flags)) |
| 2751 | return NULL; |
| 2752 | buflen = buf.len; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2753 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2754 | if (recvlen < 0) { |
| 2755 | PyErr_SetString(PyExc_ValueError, |
| 2756 | "negative buffersize in recvfrom_into"); |
| 2757 | goto error; |
| 2758 | } |
| 2759 | if (recvlen == 0) { |
| 2760 | /* If nbytes was not specified, use the buffer's length */ |
| 2761 | recvlen = buflen; |
Benjamin Peterson | 28cf368 | 2014-01-13 22:59:38 -0500 | [diff] [blame] | 2762 | } else if (recvlen > buflen) { |
| 2763 | PyErr_SetString(PyExc_ValueError, |
| 2764 | "nbytes is greater than the length of the buffer"); |
| 2765 | goto error; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2766 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2767 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2768 | readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr); |
| 2769 | if (readlen < 0) { |
| 2770 | /* Return an error */ |
| 2771 | goto error; |
| 2772 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2773 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2774 | PyBuffer_Release(&buf); |
| 2775 | /* Return the number of bytes read and the address. Note that we do |
| 2776 | not do anything special here in the case that readlen < recvlen. */ |
| 2777 | return Py_BuildValue("lN", readlen, addr); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2778 | |
| 2779 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2780 | Py_XDECREF(addr); |
| 2781 | PyBuffer_Release(&buf); |
| 2782 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2785 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2786 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2787 | \n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2788 | 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] | 2789 | |
| 2790 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2791 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2792 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2793 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2794 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2795 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2796 | char *buf; |
| 2797 | int len, n = -1, flags = 0, timeout; |
| 2798 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2800 | if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags)) |
| 2801 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2803 | if (!IS_SELECTABLE(s)) { |
| 2804 | PyBuffer_Release(&pbuf); |
| 2805 | return select_error(); |
| 2806 | } |
| 2807 | buf = pbuf.buf; |
| 2808 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2809 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2810 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2811 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2812 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2813 | if (!timeout) |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2814 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2815 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2816 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2817 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2818 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2819 | Py_END_ALLOW_THREADS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2820 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2821 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2822 | PyErr_SetString(socket_timeout, "timed out"); |
| 2823 | return NULL; |
| 2824 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2825 | END_SELECT_LOOP(s) |
| 2826 | |
| 2827 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2828 | if (n < 0) |
| 2829 | return s->errorhandler(); |
| 2830 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2833 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2834 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2835 | \n\ |
| 2836 | 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] | 2837 | 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] | 2838 | 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] | 2839 | |
| 2840 | |
| 2841 | /* s.sendall(data [,flags]) method */ |
| 2842 | |
| 2843 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2844 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2845 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2846 | char *buf; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2847 | int len, n = -1, flags = 0, timeout, saved_errno; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2848 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2849 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2850 | if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags)) |
| 2851 | return NULL; |
| 2852 | buf = pbuf.buf; |
| 2853 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2854 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2855 | if (!IS_SELECTABLE(s)) { |
| 2856 | PyBuffer_Release(&pbuf); |
| 2857 | return select_error(); |
| 2858 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2859 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2860 | do { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2861 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2862 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2863 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2864 | n = -1; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2865 | if (!timeout) { |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2866 | #ifdef __VMS |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2867 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2868 | #else |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2869 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2870 | #endif |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2871 | } |
| 2872 | Py_END_ALLOW_THREADS |
| 2873 | if (timeout == 1) { |
| 2874 | PyBuffer_Release(&pbuf); |
| 2875 | PyErr_SetString(socket_timeout, "timed out"); |
| 2876 | return NULL; |
| 2877 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2878 | END_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2879 | /* PyErr_CheckSignals() might change errno */ |
| 2880 | saved_errno = errno; |
| 2881 | /* We must run our signal handlers before looping again. |
| 2882 | send() can return a successful partial write when it is |
| 2883 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 2884 | if (PyErr_CheckSignals()) { |
| 2885 | PyBuffer_Release(&pbuf); |
| 2886 | return NULL; |
| 2887 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2888 | if (n < 0) { |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2889 | /* If interrupted, try again */ |
| 2890 | if (saved_errno == EINTR) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2891 | continue; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2892 | else |
| 2893 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2894 | } |
| 2895 | buf += n; |
| 2896 | len -= n; |
| 2897 | } while (len > 0); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2898 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2900 | if (n < 0) |
| 2901 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2903 | Py_INCREF(Py_None); |
| 2904 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2907 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2908 | "sendall(data[, flags])\n\ |
| 2909 | \n\ |
| 2910 | Send a data string to the socket. For the optional flags\n\ |
| 2911 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2912 | 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] | 2913 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2914 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2915 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2916 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2917 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2918 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2919 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2920 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2921 | Py_buffer pbuf; |
| 2922 | PyObject *addro; |
| 2923 | char *buf; |
| 2924 | Py_ssize_t len; |
| 2925 | sock_addr_t addrbuf; |
Victor Stinner | 31c7e4f | 2015-04-02 17:19:17 +0200 | [diff] [blame] | 2926 | int addrlen, flags, timeout; |
| 2927 | long n = -1; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2928 | int arglen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2930 | flags = 0; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2931 | arglen = PyTuple_Size(args); |
| 2932 | switch(arglen) { |
| 2933 | case 2: |
| 2934 | PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro); |
| 2935 | break; |
| 2936 | case 3: |
| 2937 | PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro); |
| 2938 | break; |
| 2939 | default: |
| 2940 | PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3" |
| 2941 | " arguments (%d given)", arglen); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2942 | } |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2943 | if (PyErr_Occurred()) |
| 2944 | return NULL; |
| 2945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2946 | buf = pbuf.buf; |
| 2947 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2948 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2949 | if (!IS_SELECTABLE(s)) { |
| 2950 | PyBuffer_Release(&pbuf); |
| 2951 | return select_error(); |
| 2952 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2953 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2954 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2955 | PyBuffer_Release(&pbuf); |
| 2956 | return NULL; |
| 2957 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2958 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2959 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2960 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2961 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2962 | if (!timeout) |
| 2963 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2964 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2966 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2967 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2968 | PyErr_SetString(socket_timeout, "timed out"); |
| 2969 | return NULL; |
| 2970 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2971 | END_SELECT_LOOP(s) |
| 2972 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2973 | if (n < 0) |
| 2974 | return s->errorhandler(); |
| 2975 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2978 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2979 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2980 | \n\ |
| 2981 | 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] | 2982 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2983 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2984 | |
| 2985 | /* s.shutdown(how) method */ |
| 2986 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2987 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2988 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2989 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2990 | int how; |
| 2991 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2992 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2993 | how = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2994 | if (how == -1 && PyErr_Occurred()) |
| 2995 | return NULL; |
| 2996 | Py_BEGIN_ALLOW_THREADS |
| 2997 | res = shutdown(s->sock_fd, how); |
| 2998 | Py_END_ALLOW_THREADS |
| 2999 | if (res < 0) |
| 3000 | return s->errorhandler(); |
| 3001 | Py_INCREF(Py_None); |
| 3002 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3005 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3006 | "shutdown(flag)\n\ |
| 3007 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3008 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3009 | 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] | 3010 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 3011 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3012 | static PyObject* |
| 3013 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3014 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3015 | unsigned long cmd = SIO_RCVALL; |
| 3016 | PyObject *argO; |
| 3017 | DWORD recv; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3018 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3019 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3020 | return NULL; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3022 | switch (cmd) { |
| 3023 | case SIO_RCVALL: { |
| 3024 | unsigned int option = RCVALL_ON; |
| 3025 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3026 | return NULL; |
| 3027 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3028 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3029 | return set_error(); |
| 3030 | } |
| 3031 | return PyLong_FromUnsignedLong(recv); } |
| 3032 | case SIO_KEEPALIVE_VALS: { |
| 3033 | struct tcp_keepalive ka; |
| 3034 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3035 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3036 | return NULL; |
| 3037 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3038 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3039 | return set_error(); |
| 3040 | } |
| 3041 | return PyLong_FromUnsignedLong(recv); } |
| 3042 | default: |
| 3043 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3044 | return NULL; |
| 3045 | } |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3046 | } |
| 3047 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3048 | "ioctl(cmd, option) -> long\n\ |
| 3049 | \n\ |
Kristján Valur Jónsson | 2fcd03b | 2009-09-25 15:19:51 +0000 | [diff] [blame] | 3050 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3051 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3052 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."); |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3053 | |
| 3054 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3055 | |
| 3056 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3057 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3058 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3059 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3060 | accept_doc}, |
| 3061 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3062 | bind_doc}, |
| 3063 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3064 | close_doc}, |
| 3065 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3066 | connect_doc}, |
| 3067 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3068 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3069 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3070 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
| 3071 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3072 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3073 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3074 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3075 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3076 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3077 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3078 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3079 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3080 | METH_NOARGS, getsockname_doc}, |
| 3081 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3082 | getsockopt_doc}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 3083 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3084 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3085 | sock_ioctl_doc}, |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3086 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3087 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 3088 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3089 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3090 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
| 3091 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3092 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3093 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3094 | recv_doc}, |
| 3095 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3096 | recv_into_doc}, |
| 3097 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3098 | recvfrom_doc}, |
| 3099 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3100 | recvfrom_into_doc}, |
| 3101 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3102 | send_doc}, |
| 3103 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3104 | sendall_doc}, |
| 3105 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3106 | sendto_doc}, |
| 3107 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3108 | setblocking_doc}, |
| 3109 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3110 | settimeout_doc}, |
| 3111 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3112 | gettimeout_doc}, |
| 3113 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3114 | setsockopt_doc}, |
| 3115 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3116 | shutdown_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3117 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3118 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
| 3119 | sleeptaskw_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3120 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3121 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3122 | }; |
| 3123 | |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 3124 | /* SockObject members */ |
| 3125 | static PyMemberDef sock_memberlist[] = { |
| 3126 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3127 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3128 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3129 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3130 | {0}, |
| 3131 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3132 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3133 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3134 | First close the file description. */ |
| 3135 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3136 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3137 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3138 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3139 | if (s->sock_fd != -1) |
| 3140 | (void) SOCKETCLOSE(s->sock_fd); |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3141 | if (s->weakreflist != NULL) |
| 3142 | PyObject_ClearWeakRefs((PyObject *)s); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3143 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3146 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3147 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3148 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3149 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3150 | char buf[512]; |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3151 | long sock_fd; |
| 3152 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 3153 | if (s->sock_fd == INVALID_SOCKET) { |
| 3154 | sock_fd = -1; |
| 3155 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3156 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3157 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3158 | /* this can occur on Win64, and actually there is a special |
| 3159 | ugly printf formatter for decimal pointer length integer |
| 3160 | printing, only bother if necessary*/ |
| 3161 | PyErr_SetString(PyExc_OverflowError, |
| 3162 | "no printf formatter to display " |
| 3163 | "the socket descriptor in decimal"); |
| 3164 | return NULL; |
| 3165 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3166 | #endif |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3167 | else |
| 3168 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3169 | PyOS_snprintf( |
| 3170 | buf, sizeof(buf), |
| 3171 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3172 | sock_fd, s->sock_family, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3173 | s->sock_type, |
| 3174 | s->sock_proto); |
| 3175 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3179 | /* Create a new, uninitialized socket object. */ |
| 3180 | |
| 3181 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3182 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3183 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3184 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3186 | new = type->tp_alloc(type, 0); |
| 3187 | if (new != NULL) { |
| 3188 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3189 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3190 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3191 | ((PySocketSockObject *)new)->weakreflist = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3192 | } |
| 3193 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | |
| 3197 | /* Initialize a new socket object. */ |
| 3198 | |
| 3199 | /*ARGSUSED*/ |
| 3200 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3201 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3202 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3203 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3204 | SOCKET_T fd; |
| 3205 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3206 | static char *keywords[] = {"family", "type", "proto", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3208 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 3209 | "|iii:socket", keywords, |
| 3210 | &family, &type, &proto)) |
| 3211 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3212 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3213 | Py_BEGIN_ALLOW_THREADS |
| 3214 | fd = socket(family, type, proto); |
| 3215 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3216 | |
Victor Stinner | 465db3c | 2014-07-26 14:47:56 +0200 | [diff] [blame] | 3217 | if (fd == INVALID_SOCKET) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3218 | set_error(); |
| 3219 | return -1; |
| 3220 | } |
| 3221 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3222 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3223 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3224 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3228 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3229 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3230 | static PyTypeObject sock_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3231 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 3232 | "_socket.socket", /* tp_name */ |
| 3233 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 3234 | 0, /* tp_itemsize */ |
| 3235 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 3236 | 0, /* tp_print */ |
| 3237 | 0, /* tp_getattr */ |
| 3238 | 0, /* tp_setattr */ |
| 3239 | 0, /* tp_compare */ |
| 3240 | (reprfunc)sock_repr, /* tp_repr */ |
| 3241 | 0, /* tp_as_number */ |
| 3242 | 0, /* tp_as_sequence */ |
| 3243 | 0, /* tp_as_mapping */ |
| 3244 | 0, /* tp_hash */ |
| 3245 | 0, /* tp_call */ |
| 3246 | 0, /* tp_str */ |
| 3247 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3248 | 0, /* tp_setattro */ |
| 3249 | 0, /* tp_as_buffer */ |
| 3250 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3251 | sock_doc, /* tp_doc */ |
| 3252 | 0, /* tp_traverse */ |
| 3253 | 0, /* tp_clear */ |
| 3254 | 0, /* tp_richcompare */ |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3255 | offsetof(PySocketSockObject, weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3256 | 0, /* tp_iter */ |
| 3257 | 0, /* tp_iternext */ |
| 3258 | sock_methods, /* tp_methods */ |
| 3259 | sock_memberlist, /* tp_members */ |
| 3260 | 0, /* tp_getset */ |
| 3261 | 0, /* tp_base */ |
| 3262 | 0, /* tp_dict */ |
| 3263 | 0, /* tp_descr_get */ |
| 3264 | 0, /* tp_descr_set */ |
| 3265 | 0, /* tp_dictoffset */ |
| 3266 | sock_initobj, /* tp_init */ |
| 3267 | PyType_GenericAlloc, /* tp_alloc */ |
| 3268 | sock_new, /* tp_new */ |
| 3269 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3270 | }; |
| 3271 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3272 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3273 | /* Python interface to gethostname(). */ |
| 3274 | |
| 3275 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3276 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3277 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3278 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3279 | char buf[1024]; |
| 3280 | int res; |
| 3281 | Py_BEGIN_ALLOW_THREADS |
| 3282 | res = gethostname(buf, (int) sizeof buf - 1); |
| 3283 | Py_END_ALLOW_THREADS |
| 3284 | if (res < 0) |
| 3285 | return set_error(); |
| 3286 | buf[sizeof buf - 1] = '\0'; |
| 3287 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3288 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3290 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3291 | "gethostname() -> string\n\ |
| 3292 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3293 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3294 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3295 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3296 | /* Python interface to gethostbyname(name). */ |
| 3297 | |
| 3298 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3299 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3300 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3301 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3302 | char *name; |
| 3303 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3305 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
| 3306 | return NULL; |
| 3307 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
| 3308 | return NULL; |
| 3309 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3312 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3313 | "gethostbyname(host) -> address\n\ |
| 3314 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3315 | 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] | 3316 | |
| 3317 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3318 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 3319 | |
| 3320 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3321 | 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] | 3322 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3323 | char **pch; |
| 3324 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 3325 | PyObject *name_list = (PyObject *)NULL; |
| 3326 | PyObject *addr_list = (PyObject *)NULL; |
| 3327 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3328 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3329 | if (h == NULL) { |
| 3330 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3331 | #ifndef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3332 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3333 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3334 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3335 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3336 | return NULL; |
| 3337 | } |
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 (h->h_addrtype != af) { |
| 3340 | /* Let's get real error message to return */ |
| 3341 | PyErr_SetString(socket_error, |
| 3342 | (char *)strerror(EAFNOSUPPORT)); |
Brett Cannon | 10ed0f5 | 2008-03-18 15:35:58 +0000 | [diff] [blame] | 3343 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3344 | return NULL; |
| 3345 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3347 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3348 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3349 | case AF_INET: |
| 3350 | if (alen < sizeof(struct sockaddr_in)) |
| 3351 | return NULL; |
| 3352 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3353 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3354 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3355 | case AF_INET6: |
| 3356 | if (alen < sizeof(struct sockaddr_in6)) |
| 3357 | return NULL; |
| 3358 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3359 | #endif |
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 | } |
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 | if ((name_list = PyList_New(0)) == NULL) |
| 3364 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3366 | if ((addr_list = PyList_New(0)) == NULL) |
| 3367 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3368 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3369 | /* SF #1511317: h_aliases can be NULL */ |
| 3370 | if (h->h_aliases) { |
| 3371 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3372 | int status; |
| 3373 | tmp = PyString_FromString(*pch); |
| 3374 | if (tmp == NULL) |
| 3375 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3376 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3377 | status = PyList_Append(name_list, tmp); |
| 3378 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3379 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3380 | if (status) |
| 3381 | goto err; |
| 3382 | } |
| 3383 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3385 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3386 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3387 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3388 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3390 | case AF_INET: |
| 3391 | { |
| 3392 | struct sockaddr_in sin; |
| 3393 | memset(&sin, 0, sizeof(sin)); |
| 3394 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3395 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3396 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3397 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3398 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3399 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3400 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3401 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3402 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3403 | break; |
| 3404 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3405 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3406 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3407 | case AF_INET6: |
| 3408 | { |
| 3409 | struct sockaddr_in6 sin6; |
| 3410 | memset(&sin6, 0, sizeof(sin6)); |
| 3411 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3412 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3413 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3414 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3415 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3416 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3417 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3419 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3420 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3421 | break; |
| 3422 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3423 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3425 | default: /* can't happen */ |
| 3426 | PyErr_SetString(socket_error, |
| 3427 | "unsupported address family"); |
| 3428 | return NULL; |
| 3429 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3430 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3431 | if (tmp == NULL) |
| 3432 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3433 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3434 | status = PyList_Append(addr_list, tmp); |
| 3435 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3436 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3437 | if (status) |
| 3438 | goto err; |
| 3439 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3440 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3441 | 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] | 3442 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3443 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3444 | Py_XDECREF(name_list); |
| 3445 | Py_XDECREF(addr_list); |
| 3446 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | |
| 3450 | /* Python interface to gethostbyname_ex(name). */ |
| 3451 | |
| 3452 | /*ARGSUSED*/ |
| 3453 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3454 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3455 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3456 | char *name; |
| 3457 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3458 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3459 | struct sockaddr_storage addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3460 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3461 | struct sockaddr_in addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3462 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3463 | struct sockaddr *sa; |
| 3464 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3465 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3466 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3467 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3468 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3469 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3470 | char buf[16384]; |
| 3471 | int buf_len = (sizeof buf) - 1; |
| 3472 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3473 | #endif |
| 3474 | #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] | 3475 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3476 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3477 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3478 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3479 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
| 3480 | return NULL; |
| 3481 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
| 3482 | return NULL; |
| 3483 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3484 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3485 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3486 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3487 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3488 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3489 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3490 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3491 | memset((void *) &data, '\0', sizeof(data)); |
| 3492 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3493 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3494 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3495 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3496 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3497 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3498 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3499 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3500 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3501 | Py_END_ALLOW_THREADS |
| 3502 | /* Some C libraries would require addr.__ss_family instead of |
| 3503 | addr.ss_family. |
| 3504 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3505 | access sa_family. */ |
| 3506 | sa = (struct sockaddr*)&addr; |
| 3507 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3508 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3509 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3510 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3511 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3512 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3515 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3516 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3517 | \n\ |
| 3518 | 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] | 3519 | 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] | 3520 | |
| 3521 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3522 | /* Python interface to gethostbyaddr(IP). */ |
| 3523 | |
| 3524 | /*ARGSUSED*/ |
| 3525 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3526 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3527 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3528 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3529 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3530 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3531 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3532 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3533 | struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3534 | char *ip_num; |
| 3535 | struct hostent *h; |
| 3536 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3537 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3538 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3539 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3540 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3541 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3542 | /* glibcs up to 2.10 assume that the buf argument to |
| 3543 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3544 | does not ensure. The attribute below instructs the compiler |
| 3545 | to maintain this alignment. */ |
| 3546 | char buf[16384] Py_ALIGNED(8); |
| 3547 | int buf_len = (sizeof buf) - 1; |
| 3548 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3549 | #endif |
| 3550 | #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] | 3551 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3552 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3553 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3554 | char *ap; |
| 3555 | int al; |
| 3556 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3558 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
| 3559 | return NULL; |
| 3560 | af = AF_UNSPEC; |
| 3561 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
| 3562 | return NULL; |
| 3563 | af = sa->sa_family; |
| 3564 | ap = NULL; |
| 3565 | switch (af) { |
| 3566 | case AF_INET: |
| 3567 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3568 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3569 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3570 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3571 | case AF_INET6: |
| 3572 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3573 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3574 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3575 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3576 | default: |
| 3577 | PyErr_SetString(socket_error, "unsupported address family"); |
| 3578 | return NULL; |
| 3579 | } |
| 3580 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3581 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3582 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3583 | result = gethostbyaddr_r(ap, al, af, |
| 3584 | &hp_allocated, buf, buf_len, |
| 3585 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3586 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3587 | h = gethostbyaddr_r(ap, al, af, |
| 3588 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3589 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3590 | memset((void *) &data, '\0', sizeof(data)); |
| 3591 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3592 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3593 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3594 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3595 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3596 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3597 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3598 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3599 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3600 | Py_END_ALLOW_THREADS |
| 3601 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3602 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3603 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3604 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3605 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3608 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3609 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3610 | \n\ |
| 3611 | 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] | 3612 | 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] | 3613 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3614 | |
| 3615 | /* Python interface to getservbyname(name). |
| 3616 | This only returns the port number, since the other info is already |
| 3617 | known or not useful (like the list of aliases). */ |
| 3618 | |
| 3619 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3620 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3621 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3622 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3623 | char *name, *proto=NULL; |
| 3624 | struct servent *sp; |
| 3625 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3626 | return NULL; |
| 3627 | Py_BEGIN_ALLOW_THREADS |
| 3628 | sp = getservbyname(name, proto); |
| 3629 | Py_END_ALLOW_THREADS |
| 3630 | if (sp == NULL) { |
| 3631 | PyErr_SetString(socket_error, "service/proto not found"); |
| 3632 | return NULL; |
| 3633 | } |
| 3634 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3637 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3638 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3639 | \n\ |
| 3640 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3641 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3642 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3643 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3644 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3645 | /* Python interface to getservbyport(port). |
| 3646 | This only returns the service name, since the other info is already |
| 3647 | known or not useful (like the list of aliases). */ |
| 3648 | |
| 3649 | /*ARGSUSED*/ |
| 3650 | static PyObject * |
| 3651 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3652 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3653 | int port; |
| 3654 | char *proto=NULL; |
| 3655 | struct servent *sp; |
| 3656 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3657 | return NULL; |
| 3658 | if (port < 0 || port > 0xffff) { |
| 3659 | PyErr_SetString( |
| 3660 | PyExc_OverflowError, |
| 3661 | "getservbyport: port must be 0-65535."); |
| 3662 | return NULL; |
| 3663 | } |
| 3664 | Py_BEGIN_ALLOW_THREADS |
| 3665 | sp = getservbyport(htons((short)port), proto); |
| 3666 | Py_END_ALLOW_THREADS |
| 3667 | if (sp == NULL) { |
| 3668 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3669 | return NULL; |
| 3670 | } |
| 3671 | return PyString_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | PyDoc_STRVAR(getservbyport_doc, |
| 3675 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3676 | \n\ |
| 3677 | Return the service name from a port number and protocol name.\n\ |
| 3678 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3679 | otherwise any protocol will match."); |
| 3680 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3681 | /* Python interface to getprotobyname(name). |
| 3682 | This only returns the protocol number, since the other info is |
| 3683 | already known or not useful (like the list of aliases). */ |
| 3684 | |
| 3685 | /*ARGSUSED*/ |
| 3686 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3687 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3688 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3689 | char *name; |
| 3690 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3691 | #ifdef __BEOS__ |
| 3692 | /* Not available in BeOS yet. - [cjh] */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3693 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
| 3694 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3695 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3696 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3697 | return NULL; |
| 3698 | Py_BEGIN_ALLOW_THREADS |
| 3699 | sp = getprotobyname(name); |
| 3700 | Py_END_ALLOW_THREADS |
| 3701 | if (sp == NULL) { |
| 3702 | PyErr_SetString(socket_error, "protocol not found"); |
| 3703 | return NULL; |
| 3704 | } |
| 3705 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3706 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3709 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3710 | "getprotobyname(name) -> integer\n\ |
| 3711 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3712 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3713 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3714 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3715 | #ifdef HAVE_SOCKETPAIR |
| 3716 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3717 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3718 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3719 | |
| 3720 | /*ARGSUSED*/ |
| 3721 | static PyObject * |
| 3722 | socket_socketpair(PyObject *self, PyObject *args) |
| 3723 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3724 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3725 | SOCKET_T sv[2]; |
| 3726 | int family, type = SOCK_STREAM, proto = 0; |
| 3727 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3728 | |
| 3729 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3730 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3731 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3732 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3733 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3734 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3735 | &family, &type, &proto)) |
| 3736 | return NULL; |
| 3737 | /* Create a pair of socket fds */ |
| 3738 | if (socketpair(family, type, proto, sv) < 0) |
| 3739 | return set_error(); |
| 3740 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3741 | if (s0 == NULL) |
| 3742 | goto finally; |
| 3743 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3744 | if (s1 == NULL) |
| 3745 | goto finally; |
| 3746 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3747 | |
| 3748 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3749 | if (res == NULL) { |
| 3750 | if (s0 == NULL) |
| 3751 | SOCKETCLOSE(sv[0]); |
| 3752 | if (s1 == NULL) |
| 3753 | SOCKETCLOSE(sv[1]); |
| 3754 | } |
| 3755 | Py_XDECREF(s0); |
| 3756 | Py_XDECREF(s1); |
| 3757 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | PyDoc_STRVAR(socketpair_doc, |
| 3761 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3762 | \n\ |
| 3763 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3764 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3765 | 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] | 3766 | 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] | 3767 | |
| 3768 | #endif /* HAVE_SOCKETPAIR */ |
| 3769 | |
| 3770 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3771 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3772 | /* Create a socket object from a numeric file description. |
| 3773 | Useful e.g. if stdin is a socket. |
| 3774 | Additional arguments as for socket(). */ |
| 3775 | |
| 3776 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3777 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3778 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3779 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3780 | PySocketSockObject *s; |
| 3781 | SOCKET_T fd; |
| 3782 | int family, type, proto = 0; |
| 3783 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 3784 | &fd, &family, &type, &proto)) |
| 3785 | return NULL; |
| 3786 | /* Dup the fd so it and the socket can be closed independently */ |
| 3787 | fd = dup(fd); |
| 3788 | if (fd < 0) |
| 3789 | return set_error(); |
| 3790 | s = new_sockobject(fd, family, type, proto); |
| 3791 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3792 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3794 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3795 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 3796 | \n\ |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 3797 | Create a socket object from a duplicate of the given\n\ |
| 3798 | file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3799 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3800 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3801 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3802 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3803 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3804 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3805 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3806 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3807 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3808 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3809 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3810 | return NULL; |
| 3811 | } |
| 3812 | if (x1 < 0) { |
| 3813 | PyErr_SetString(PyExc_OverflowError, |
| 3814 | "can't convert negative number to unsigned long"); |
| 3815 | return NULL; |
| 3816 | } |
| 3817 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3818 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3821 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3822 | "ntohs(integer) -> integer\n\ |
| 3823 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3824 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3825 | |
| 3826 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3827 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3828 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3829 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3830 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3831 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3832 | if (PyInt_Check(arg)) { |
| 3833 | x = PyInt_AS_LONG(arg); |
| 3834 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3835 | return NULL; |
| 3836 | if ((long)x < 0) { |
| 3837 | PyErr_SetString(PyExc_OverflowError, |
| 3838 | "can't convert negative number to unsigned long"); |
| 3839 | return NULL; |
| 3840 | } |
| 3841 | } |
| 3842 | else if (PyLong_Check(arg)) { |
| 3843 | x = PyLong_AsUnsignedLong(arg); |
| 3844 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3845 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3846 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3847 | { |
| 3848 | unsigned long y; |
| 3849 | /* only want the trailing 32 bits */ |
| 3850 | y = x & 0xFFFFFFFFUL; |
| 3851 | if (y ^ x) |
| 3852 | return PyErr_Format(PyExc_OverflowError, |
| 3853 | "long int larger than 32 bits"); |
| 3854 | x = y; |
| 3855 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3856 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3857 | } |
| 3858 | else |
| 3859 | return PyErr_Format(PyExc_TypeError, |
| 3860 | "expected int/long, %s found", |
| 3861 | Py_TYPE(arg)->tp_name); |
| 3862 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3863 | return NULL; |
| 3864 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3867 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3868 | "ntohl(integer) -> integer\n\ |
| 3869 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3870 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3871 | |
| 3872 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3873 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3874 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3875 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3876 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3878 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3879 | return NULL; |
| 3880 | } |
| 3881 | if (x1 < 0) { |
| 3882 | PyErr_SetString(PyExc_OverflowError, |
| 3883 | "can't convert negative number to unsigned long"); |
| 3884 | return NULL; |
| 3885 | } |
| 3886 | x2 = (unsigned int)htons((unsigned short)x1); |
| 3887 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3890 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3891 | "htons(integer) -> integer\n\ |
| 3892 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3893 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3894 | |
| 3895 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3896 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3897 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3898 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3899 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3901 | if (PyInt_Check(arg)) { |
| 3902 | x = PyInt_AS_LONG(arg); |
| 3903 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3904 | return NULL; |
| 3905 | if ((long)x < 0) { |
| 3906 | PyErr_SetString(PyExc_OverflowError, |
| 3907 | "can't convert negative number to unsigned long"); |
| 3908 | return NULL; |
| 3909 | } |
| 3910 | } |
| 3911 | else if (PyLong_Check(arg)) { |
| 3912 | x = PyLong_AsUnsignedLong(arg); |
| 3913 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3914 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3915 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3916 | { |
| 3917 | unsigned long y; |
| 3918 | /* only want the trailing 32 bits */ |
| 3919 | y = x & 0xFFFFFFFFUL; |
| 3920 | if (y ^ x) |
| 3921 | return PyErr_Format(PyExc_OverflowError, |
| 3922 | "long int larger than 32 bits"); |
| 3923 | x = y; |
| 3924 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3925 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3926 | } |
| 3927 | else |
| 3928 | return PyErr_Format(PyExc_TypeError, |
| 3929 | "expected int/long, %s found", |
| 3930 | Py_TYPE(arg)->tp_name); |
| 3931 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3934 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3935 | "htonl(integer) -> integer\n\ |
| 3936 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3937 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3938 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3939 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3940 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3941 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3942 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3943 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3944 | 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] | 3945 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3946 | |
| 3947 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3948 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3949 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3950 | #ifndef INADDR_NONE |
| 3951 | #define INADDR_NONE (-1) |
| 3952 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3953 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3954 | struct in_addr buf; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3955 | #endif |
| 3956 | |
| 3957 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 3958 | #if (SIZEOF_INT != 4) |
| 3959 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3960 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3961 | /* Have to use inet_addr() instead */ |
| 3962 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3963 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3964 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3965 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3966 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3967 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3968 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3969 | |
| 3970 | #ifdef HAVE_INET_ATON |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3971 | |
| 3972 | #ifdef USE_INET_ATON_WEAKLINK |
| 3973 | if (inet_aton != NULL) { |
| 3974 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3975 | if (inet_aton(ip_addr, &buf)) |
| 3976 | return PyString_FromStringAndSize((char *)(&buf), |
| 3977 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3978 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3979 | PyErr_SetString(socket_error, |
| 3980 | "illegal IP address string passed to inet_aton"); |
| 3981 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3982 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3983 | #ifdef USE_INET_ATON_WEAKLINK |
| 3984 | } else { |
| 3985 | #endif |
| 3986 | |
| 3987 | #endif |
| 3988 | |
| 3989 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3990 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3991 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3992 | * for this */ |
| 3993 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3994 | packed_addr = 0xFFFFFFFF; |
| 3995 | } else { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3996 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3997 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3999 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 4000 | PyErr_SetString(socket_error, |
| 4001 | "illegal IP address string passed to inet_aton"); |
| 4002 | return NULL; |
| 4003 | } |
| 4004 | } |
| 4005 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 4006 | sizeof(packed_addr)); |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 4007 | |
| 4008 | #ifdef USE_INET_ATON_WEAKLINK |
| 4009 | } |
| 4010 | #endif |
| 4011 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4012 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4015 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 4016 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4017 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4018 | 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] | 4019 | |
| 4020 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4021 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4022 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4023 | char *packed_str; |
| 4024 | int addr_len; |
| 4025 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4026 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4027 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
| 4028 | return NULL; |
| 4029 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4030 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4031 | if (addr_len != sizeof(packed_addr)) { |
| 4032 | PyErr_SetString(socket_error, |
| 4033 | "packed IP wrong length for inet_ntoa"); |
| 4034 | return NULL; |
| 4035 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4036 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4037 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4039 | return PyString_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4040 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4041 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4042 | #ifdef HAVE_INET_PTON |
| 4043 | |
| 4044 | PyDoc_STRVAR(inet_pton_doc, |
| 4045 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 4046 | \n\ |
| 4047 | Convert an IP address from string format to a packed string suitable\n\ |
| 4048 | for use with low-level network functions."); |
| 4049 | |
| 4050 | static PyObject * |
| 4051 | socket_inet_pton(PyObject *self, PyObject *args) |
| 4052 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4053 | int af; |
| 4054 | char* ip; |
| 4055 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4056 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4057 | 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] | 4058 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4059 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4060 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4061 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 4062 | return NULL; |
| 4063 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4064 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 4065 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4066 | if(af == AF_INET6) { |
| 4067 | PyErr_SetString(socket_error, |
| 4068 | "can't use AF_INET6, IPv6 is disabled"); |
| 4069 | return NULL; |
| 4070 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4071 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 4072 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4073 | retval = inet_pton(af, ip, packed); |
| 4074 | if (retval < 0) { |
| 4075 | PyErr_SetFromErrno(socket_error); |
| 4076 | return NULL; |
| 4077 | } else if (retval == 0) { |
| 4078 | PyErr_SetString(socket_error, |
| 4079 | "illegal IP address string passed to inet_pton"); |
| 4080 | return NULL; |
| 4081 | } else if (af == AF_INET) { |
| 4082 | return PyString_FromStringAndSize(packed, |
| 4083 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4084 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4085 | } else if (af == AF_INET6) { |
| 4086 | return PyString_FromStringAndSize(packed, |
| 4087 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4088 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4089 | } else { |
| 4090 | PyErr_SetString(socket_error, "unknown address family"); |
| 4091 | return NULL; |
| 4092 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4093 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4094 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4095 | PyDoc_STRVAR(inet_ntop_doc, |
| 4096 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 4097 | \n\ |
| 4098 | Convert a packed IP address of the given family to string format."); |
| 4099 | |
| 4100 | static PyObject * |
| 4101 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 4102 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4103 | int af; |
| 4104 | char* packed; |
| 4105 | int len; |
| 4106 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4107 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4108 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4109 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4110 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4111 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4112 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4113 | /* Guarantee NUL-termination for PyString_FromString() below */ |
| 4114 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4116 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 4117 | return NULL; |
| 4118 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4119 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4120 | if (af == AF_INET) { |
| 4121 | if (len != sizeof(struct in_addr)) { |
| 4122 | PyErr_SetString(PyExc_ValueError, |
| 4123 | "invalid length of packed IP address string"); |
| 4124 | return NULL; |
| 4125 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4126 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4127 | } else if (af == AF_INET6) { |
| 4128 | if (len != sizeof(struct in6_addr)) { |
| 4129 | PyErr_SetString(PyExc_ValueError, |
| 4130 | "invalid length of packed IP address string"); |
| 4131 | return NULL; |
| 4132 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4133 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4134 | } else { |
| 4135 | PyErr_Format(PyExc_ValueError, |
| 4136 | "unknown address family %d", af); |
| 4137 | return NULL; |
| 4138 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4139 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4140 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 4141 | if (!retval) { |
| 4142 | PyErr_SetFromErrno(socket_error); |
| 4143 | return NULL; |
| 4144 | } else { |
| 4145 | return PyString_FromString(retval); |
| 4146 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4147 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4148 | /* NOTREACHED */ |
| 4149 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 4150 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | #endif /* HAVE_INET_PTON */ |
| 4154 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4155 | /* Python interface to getaddrinfo(host, port). */ |
| 4156 | |
| 4157 | /*ARGSUSED*/ |
| 4158 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4159 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4160 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4161 | struct addrinfo hints, *res; |
| 4162 | struct addrinfo *res0 = NULL; |
| 4163 | PyObject *hobj = NULL; |
| 4164 | PyObject *pobj = (PyObject *)NULL; |
| 4165 | char pbuf[30]; |
| 4166 | char *hptr, *pptr; |
| 4167 | int family, socktype, protocol, flags; |
| 4168 | int error; |
| 4169 | PyObject *all = (PyObject *)NULL; |
| 4170 | PyObject *single = (PyObject *)NULL; |
| 4171 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4172 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4173 | family = socktype = protocol = flags = 0; |
| 4174 | family = AF_UNSPEC; |
| 4175 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 4176 | &hobj, &pobj, &family, &socktype, |
| 4177 | &protocol, &flags)) { |
| 4178 | return NULL; |
| 4179 | } |
| 4180 | if (hobj == Py_None) { |
| 4181 | hptr = NULL; |
| 4182 | } else if (PyUnicode_Check(hobj)) { |
Victor Stinner | 9a2326b | 2015-09-11 12:42:13 +0200 | [diff] [blame] | 4183 | idna = PyUnicode_AsEncodedString(hobj, "idna", NULL); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4184 | if (!idna) |
| 4185 | return NULL; |
| 4186 | hptr = PyString_AsString(idna); |
| 4187 | } else if (PyString_Check(hobj)) { |
| 4188 | hptr = PyString_AsString(hobj); |
| 4189 | } else { |
| 4190 | PyErr_SetString(PyExc_TypeError, |
| 4191 | "getaddrinfo() argument 1 must be string or None"); |
| 4192 | return NULL; |
| 4193 | } |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4194 | if (PyInt_Check(pobj) || PyLong_Check(pobj)) { |
| 4195 | long value = PyLong_AsLong(pobj); |
| 4196 | if (value == -1 && PyErr_Occurred()) |
| 4197 | return NULL; |
| 4198 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4199 | pptr = pbuf; |
| 4200 | } else if (PyString_Check(pobj)) { |
| 4201 | pptr = PyString_AsString(pobj); |
| 4202 | } else if (pobj == Py_None) { |
| 4203 | pptr = (char *)NULL; |
| 4204 | } else { |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4205 | PyErr_SetString(socket_error, |
| 4206 | "getaddrinfo() argument 2 must be integer or string"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4207 | goto err; |
| 4208 | } |
Ronald Oussoren | 0e6283e | 2013-06-10 10:35:36 +0200 | [diff] [blame] | 4209 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 16c52a3 | 2013-05-24 13:45:27 +0200 | [diff] [blame] | 4210 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 4211 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
| 4212 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 4213 | * This workaround avoids a segfault in libsystem. |
| 4214 | */ |
| 4215 | pptr = "00"; |
| 4216 | } |
| 4217 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4218 | memset(&hints, 0, sizeof(hints)); |
| 4219 | hints.ai_family = family; |
| 4220 | hints.ai_socktype = socktype; |
| 4221 | hints.ai_protocol = protocol; |
| 4222 | hints.ai_flags = flags; |
| 4223 | Py_BEGIN_ALLOW_THREADS |
| 4224 | ACQUIRE_GETADDRINFO_LOCK |
| 4225 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 4226 | Py_END_ALLOW_THREADS |
| 4227 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4228 | if (error) { |
| 4229 | set_gaierror(error); |
| 4230 | goto err; |
| 4231 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4232 | |
Benjamin Peterson | a5a44ab | 2015-04-01 11:16:40 -0400 | [diff] [blame] | 4233 | all = PyList_New(0); |
| 4234 | if (all == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4235 | goto err; |
| 4236 | for (res = res0; res; res = res->ai_next) { |
| 4237 | PyObject *addr = |
| 4238 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 4239 | if (addr == NULL) |
| 4240 | goto err; |
| 4241 | single = Py_BuildValue("iiisO", res->ai_family, |
| 4242 | res->ai_socktype, res->ai_protocol, |
| 4243 | res->ai_canonname ? res->ai_canonname : "", |
| 4244 | addr); |
| 4245 | Py_DECREF(addr); |
| 4246 | if (single == NULL) |
| 4247 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4248 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4249 | if (PyList_Append(all, single)) |
| 4250 | goto err; |
| 4251 | Py_XDECREF(single); |
| 4252 | } |
| 4253 | Py_XDECREF(idna); |
| 4254 | if (res0) |
| 4255 | freeaddrinfo(res0); |
| 4256 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4257 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4258 | Py_XDECREF(single); |
| 4259 | Py_XDECREF(all); |
| 4260 | Py_XDECREF(idna); |
| 4261 | if (res0) |
| 4262 | freeaddrinfo(res0); |
| 4263 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4266 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4267 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 4268 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4269 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4270 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4271 | |
| 4272 | /* Python interface to getnameinfo(sa, flags). */ |
| 4273 | |
| 4274 | /*ARGSUSED*/ |
| 4275 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4276 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4277 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4278 | PyObject *sa = (PyObject *)NULL; |
| 4279 | int flags; |
| 4280 | char *hostp; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4281 | int port; |
| 4282 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4283 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 4284 | struct addrinfo hints, *res = NULL; |
| 4285 | int error; |
| 4286 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4287 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4288 | flags = flowinfo = scope_id = 0; |
| 4289 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 4290 | return NULL; |
| 4291 | if (!PyTuple_Check(sa)) { |
| 4292 | PyErr_SetString(PyExc_TypeError, |
| 4293 | "getnameinfo() argument 1 must be a tuple"); |
| 4294 | return NULL; |
| 4295 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4296 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4297 | &hostp, &port, &flowinfo, &scope_id)) |
| 4298 | return NULL; |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 4299 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4300 | PyErr_SetString(PyExc_OverflowError, |
| 4301 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 4302 | return NULL; |
| 4303 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4304 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 4305 | memset(&hints, 0, sizeof(hints)); |
| 4306 | hints.ai_family = AF_UNSPEC; |
| 4307 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
| 4308 | Py_BEGIN_ALLOW_THREADS |
| 4309 | ACQUIRE_GETADDRINFO_LOCK |
| 4310 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 4311 | Py_END_ALLOW_THREADS |
| 4312 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4313 | if (error) { |
| 4314 | set_gaierror(error); |
| 4315 | goto fail; |
| 4316 | } |
| 4317 | if (res->ai_next) { |
| 4318 | PyErr_SetString(socket_error, |
| 4319 | "sockaddr resolved to multiple addresses"); |
| 4320 | goto fail; |
| 4321 | } |
| 4322 | switch (res->ai_family) { |
| 4323 | case AF_INET: |
| 4324 | { |
| 4325 | if (PyTuple_GET_SIZE(sa) != 2) { |
| 4326 | PyErr_SetString(socket_error, |
| 4327 | "IPv4 sockaddr must be 2 tuple"); |
| 4328 | goto fail; |
| 4329 | } |
| 4330 | break; |
| 4331 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4332 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4333 | case AF_INET6: |
| 4334 | { |
| 4335 | struct sockaddr_in6 *sin6; |
| 4336 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4337 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4338 | sin6->sin6_scope_id = scope_id; |
| 4339 | break; |
| 4340 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4341 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4342 | } |
| 4343 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 4344 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 4345 | if (error) { |
| 4346 | set_gaierror(error); |
| 4347 | goto fail; |
| 4348 | } |
| 4349 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4350 | |
| 4351 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4352 | if (res) |
| 4353 | freeaddrinfo(res); |
| 4354 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4357 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4358 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4359 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4360 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4361 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4362 | |
| 4363 | /* Python API to getting and setting the default timeout value. */ |
| 4364 | |
| 4365 | static PyObject * |
| 4366 | socket_getdefaulttimeout(PyObject *self) |
| 4367 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4368 | if (defaulttimeout < 0.0) { |
| 4369 | Py_INCREF(Py_None); |
| 4370 | return Py_None; |
| 4371 | } |
| 4372 | else |
| 4373 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4377 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4378 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4379 | 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] | 4380 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4381 | When the socket module is first imported, the default is None."); |
| 4382 | |
| 4383 | static PyObject * |
| 4384 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 4385 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4386 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4387 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4388 | if (arg == Py_None) |
| 4389 | timeout = -1.0; |
| 4390 | else { |
| 4391 | timeout = PyFloat_AsDouble(arg); |
| 4392 | if (timeout < 0.0) { |
| 4393 | if (!PyErr_Occurred()) |
| 4394 | PyErr_SetString(PyExc_ValueError, |
| 4395 | "Timeout value out of range"); |
| 4396 | return NULL; |
| 4397 | } |
| 4398 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4400 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4401 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4402 | Py_INCREF(Py_None); |
| 4403 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4407 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4408 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4409 | 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] | 4410 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4411 | When the socket module is first imported, the default is None."); |
| 4412 | |
| 4413 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4414 | /* List of functions exported by this module. */ |
| 4415 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4416 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4417 | {"gethostbyname", socket_gethostbyname, |
| 4418 | METH_VARARGS, gethostbyname_doc}, |
| 4419 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4420 | METH_VARARGS, ghbn_ex_doc}, |
| 4421 | {"gethostbyaddr", socket_gethostbyaddr, |
| 4422 | METH_VARARGS, gethostbyaddr_doc}, |
| 4423 | {"gethostname", socket_gethostname, |
| 4424 | METH_NOARGS, gethostname_doc}, |
| 4425 | {"getservbyname", socket_getservbyname, |
| 4426 | METH_VARARGS, getservbyname_doc}, |
| 4427 | {"getservbyport", socket_getservbyport, |
| 4428 | METH_VARARGS, getservbyport_doc}, |
| 4429 | {"getprotobyname", socket_getprotobyname, |
| 4430 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4431 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4432 | {"fromfd", socket_fromfd, |
| 4433 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4434 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4435 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4436 | {"socketpair", socket_socketpair, |
| 4437 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4438 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4439 | {"ntohs", socket_ntohs, |
| 4440 | METH_VARARGS, ntohs_doc}, |
| 4441 | {"ntohl", socket_ntohl, |
| 4442 | METH_O, ntohl_doc}, |
| 4443 | {"htons", socket_htons, |
| 4444 | METH_VARARGS, htons_doc}, |
| 4445 | {"htonl", socket_htonl, |
| 4446 | METH_O, htonl_doc}, |
| 4447 | {"inet_aton", socket_inet_aton, |
| 4448 | METH_VARARGS, inet_aton_doc}, |
| 4449 | {"inet_ntoa", socket_inet_ntoa, |
| 4450 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4451 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4452 | {"inet_pton", socket_inet_pton, |
| 4453 | METH_VARARGS, inet_pton_doc}, |
| 4454 | {"inet_ntop", socket_inet_ntop, |
| 4455 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4456 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4457 | {"getaddrinfo", socket_getaddrinfo, |
| 4458 | METH_VARARGS, getaddrinfo_doc}, |
| 4459 | {"getnameinfo", socket_getnameinfo, |
| 4460 | METH_VARARGS, getnameinfo_doc}, |
| 4461 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4462 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4463 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4464 | METH_O, setdefaulttimeout_doc}, |
| 4465 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4466 | }; |
| 4467 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4468 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4469 | #ifdef RISCOS |
| 4470 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4471 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4472 | static int |
| 4473 | os_init(void) |
| 4474 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4475 | _kernel_swi_regs r; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4476 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4477 | r.r[0] = 0; |
| 4478 | _kernel_swi(0x43380, &r, &r); |
| 4479 | taskwindow = r.r[0]; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4480 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4481 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | #endif /* RISCOS */ |
| 4485 | |
| 4486 | |
| 4487 | #ifdef MS_WINDOWS |
| 4488 | #define OS_INIT_DEFINED |
| 4489 | |
| 4490 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4491 | |
| 4492 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4493 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4494 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4495 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4499 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4500 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4501 | WSADATA WSAData; |
| 4502 | int ret; |
| 4503 | char buf[100]; |
| 4504 | ret = WSAStartup(0x0101, &WSAData); |
| 4505 | switch (ret) { |
| 4506 | case 0: /* No error */ |
| 4507 | Py_AtExit(os_cleanup); |
| 4508 | return 1; /* Success */ |
| 4509 | case WSASYSNOTREADY: |
| 4510 | PyErr_SetString(PyExc_ImportError, |
| 4511 | "WSAStartup failed: network not ready"); |
| 4512 | break; |
| 4513 | case WSAVERNOTSUPPORTED: |
| 4514 | case WSAEINVAL: |
| 4515 | PyErr_SetString( |
| 4516 | PyExc_ImportError, |
| 4517 | "WSAStartup failed: requested version not supported"); |
| 4518 | break; |
| 4519 | default: |
| 4520 | PyOS_snprintf(buf, sizeof(buf), |
| 4521 | "WSAStartup failed: error code %d", ret); |
| 4522 | PyErr_SetString(PyExc_ImportError, buf); |
| 4523 | break; |
| 4524 | } |
| 4525 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4528 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4529 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4530 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4531 | #ifdef PYOS_OS2 |
| 4532 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4533 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4534 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4535 | |
| 4536 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4537 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4538 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4539 | #ifndef PYCC_GCC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4540 | char reason[64]; |
| 4541 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4542 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4543 | if (rc == 0) { |
| 4544 | return 1; /* Success */ |
| 4545 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4546 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4547 | PyOS_snprintf(reason, sizeof(reason), |
| 4548 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 4549 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4551 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4552 | #else |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4553 | /* No need to initialize sockets with GCC/EMX */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4554 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4555 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | #endif /* PYOS_OS2 */ |
| 4559 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4560 | |
| 4561 | #ifndef OS_INIT_DEFINED |
| 4562 | static int |
| 4563 | os_init(void) |
| 4564 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4565 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4566 | } |
| 4567 | #endif |
| 4568 | |
| 4569 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4570 | /* C API table - always add new things to the end for binary |
| 4571 | compatibility. */ |
| 4572 | static |
| 4573 | PySocketModule_APIObject PySocketModuleAPI = |
| 4574 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4575 | &sock_type, |
| 4576 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4577 | }; |
| 4578 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4579 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4580 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4581 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4582 | This module is actually called "_socket", and there's a wrapper |
| 4583 | "socket.py" which implements some additional functionality. On some |
| 4584 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 4585 | wrapper for the socket type that provides missing functionality such |
| 4586 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 4587 | with an ImportError exception if os-specific initialization fails. |
| 4588 | On Windows, this does WINSOCK initialization. When WINSOCK is |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4589 | initialized successfully, a call to WSACleanup() is scheduled to be |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4590 | made at exit time. |
| 4591 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4593 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4594 | "Implementation module for socket operations.\n\ |
| 4595 | \n\ |
| 4596 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4597 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4598 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4599 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4600 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4601 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4602 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4603 | if (!os_init()) |
| 4604 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4606 | Py_TYPE(&sock_type) = &PyType_Type; |
| 4607 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 4608 | socket_methods, |
| 4609 | socket_doc); |
| 4610 | if (m == NULL) |
| 4611 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4613 | socket_error = PyErr_NewException("socket.error", |
| 4614 | PyExc_IOError, NULL); |
| 4615 | if (socket_error == NULL) |
| 4616 | return; |
| 4617 | PySocketModuleAPI.error = socket_error; |
| 4618 | Py_INCREF(socket_error); |
| 4619 | PyModule_AddObject(m, "error", socket_error); |
| 4620 | socket_herror = PyErr_NewException("socket.herror", |
| 4621 | socket_error, NULL); |
| 4622 | if (socket_herror == NULL) |
| 4623 | return; |
| 4624 | Py_INCREF(socket_herror); |
| 4625 | PyModule_AddObject(m, "herror", socket_herror); |
| 4626 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4627 | NULL); |
| 4628 | if (socket_gaierror == NULL) |
| 4629 | return; |
| 4630 | Py_INCREF(socket_gaierror); |
| 4631 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4632 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4633 | socket_error, NULL); |
| 4634 | if (socket_timeout == NULL) |
| 4635 | return; |
| 4636 | Py_INCREF(socket_timeout); |
| 4637 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 4638 | Py_INCREF((PyObject *)&sock_type); |
| 4639 | if (PyModule_AddObject(m, "SocketType", |
| 4640 | (PyObject *)&sock_type) != 0) |
| 4641 | return; |
| 4642 | Py_INCREF((PyObject *)&sock_type); |
| 4643 | if (PyModule_AddObject(m, "socket", |
| 4644 | (PyObject *)&sock_type) != 0) |
| 4645 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4646 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4647 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4648 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4649 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4650 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4651 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4652 | Py_INCREF(has_ipv6); |
| 4653 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4654 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4655 | /* Export C API */ |
| 4656 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4657 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4658 | ) != 0) |
| 4659 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4661 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4662 | #ifdef AF_UNSPEC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4663 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4664 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4665 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4666 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4667 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4668 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4669 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4670 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4671 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4672 | #ifdef AF_AX25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4673 | /* Amateur Radio AX.25 */ |
| 4674 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4675 | #endif |
| 4676 | #ifdef AF_IPX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4677 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4678 | #endif |
| 4679 | #ifdef AF_APPLETALK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4680 | /* Appletalk DDP */ |
| 4681 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4682 | #endif |
| 4683 | #ifdef AF_NETROM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4684 | /* Amateur radio NetROM */ |
| 4685 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4686 | #endif |
| 4687 | #ifdef AF_BRIDGE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4688 | /* Multiprotocol bridge */ |
| 4689 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4690 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4691 | #ifdef AF_ATMPVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4692 | /* ATM PVCs */ |
| 4693 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4694 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4695 | #ifdef AF_AAL5 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4696 | /* Reserved for Werner's ATM */ |
| 4697 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4698 | #endif |
| 4699 | #ifdef AF_X25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4700 | /* Reserved for X.25 project */ |
| 4701 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4702 | #endif |
| 4703 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4704 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4705 | #endif |
| 4706 | #ifdef AF_ROSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4707 | /* Amateur Radio X.25 PLP */ |
| 4708 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4709 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4710 | #ifdef AF_DECnet |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4711 | /* Reserved for DECnet project */ |
| 4712 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4713 | #endif |
| 4714 | #ifdef AF_NETBEUI |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4715 | /* Reserved for 802.2LLC project */ |
| 4716 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4717 | #endif |
| 4718 | #ifdef AF_SECURITY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4719 | /* Security callback pseudo AF */ |
| 4720 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4721 | #endif |
| 4722 | #ifdef AF_KEY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4723 | /* PF_KEY key management API */ |
| 4724 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4725 | #endif |
| 4726 | #ifdef AF_NETLINK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4727 | /* */ |
| 4728 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4729 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4730 | #ifdef NETLINK_SKIP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4731 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4732 | #endif |
| 4733 | #ifdef NETLINK_W1 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4734 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4735 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4736 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4737 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4738 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4739 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4740 | #endif |
| 4741 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4742 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4743 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4744 | #ifdef NETLINK_XFRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4745 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4746 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4747 | #ifdef NETLINK_ARPD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4748 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4749 | #endif |
| 4750 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4751 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4752 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4753 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Martin v. Löwis | b1cc1d4 | 2007-02-13 12:14:19 +0000 | [diff] [blame] | 4754 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4755 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
| 4756 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4757 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4758 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4759 | #endif |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 4760 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4761 | #ifdef AF_ROUTE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4762 | /* Alias to emulate 4.4BSD */ |
| 4763 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4764 | #endif |
| 4765 | #ifdef AF_ASH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4766 | /* Ash */ |
| 4767 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4768 | #endif |
| 4769 | #ifdef AF_ECONET |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4770 | /* Acorn Econet */ |
| 4771 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4772 | #endif |
| 4773 | #ifdef AF_ATMSVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4774 | /* ATM SVCs */ |
| 4775 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4776 | #endif |
| 4777 | #ifdef AF_SNA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4778 | /* Linux SNA Project (nutters!) */ |
| 4779 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4780 | #endif |
| 4781 | #ifdef AF_IRDA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4782 | /* IRDA sockets */ |
| 4783 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4784 | #endif |
| 4785 | #ifdef AF_PPPOX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4786 | /* PPPoX sockets */ |
| 4787 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4788 | #endif |
| 4789 | #ifdef AF_WANPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4790 | /* Wanpipe API Sockets */ |
| 4791 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4792 | #endif |
| 4793 | #ifdef AF_LLC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4794 | /* Linux LLC */ |
| 4795 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4796 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4797 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4798 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4799 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4800 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4801 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4802 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4803 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4804 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4805 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4806 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4807 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4808 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4809 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4810 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4811 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4812 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4813 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4814 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4815 | 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] | 4816 | #endif |
| 4817 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 4818 | #ifdef AF_PACKET |
| 4819 | PyModule_AddIntMacro(m, AF_PACKET); |
| 4820 | #endif |
| 4821 | #ifdef PF_PACKET |
| 4822 | PyModule_AddIntMacro(m, PF_PACKET); |
| 4823 | #endif |
| 4824 | #ifdef PACKET_HOST |
| 4825 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 4826 | #endif |
| 4827 | #ifdef PACKET_BROADCAST |
| 4828 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 4829 | #endif |
| 4830 | #ifdef PACKET_MULTICAST |
| 4831 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 4832 | #endif |
| 4833 | #ifdef PACKET_OTHERHOST |
| 4834 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 4835 | #endif |
| 4836 | #ifdef PACKET_OUTGOING |
| 4837 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 4838 | #endif |
| 4839 | #ifdef PACKET_LOOPBACK |
| 4840 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 4841 | #endif |
| 4842 | #ifdef PACKET_FASTROUTE |
| 4843 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4844 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4845 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4846 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4847 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4849 | /* for addresses */ |
| 4850 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4851 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4852 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4853 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4854 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4855 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4856 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4857 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4858 | /* for setsockopt() */ |
| 4859 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4860 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4861 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4862 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4863 | TIPC_DEST_DROPPABLE); |
| 4864 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4865 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4866 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4867 | TIPC_LOW_IMPORTANCE); |
| 4868 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4869 | TIPC_MEDIUM_IMPORTANCE); |
| 4870 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4871 | TIPC_HIGH_IMPORTANCE); |
| 4872 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4873 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4874 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4875 | /* for subscriptions */ |
| 4876 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4877 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4878 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4879 | /* doesn't seem to be available everywhere */ |
| 4880 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4881 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4882 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4883 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4884 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4885 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4886 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4887 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4888 | #endif |
| 4889 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4890 | /* Socket types */ |
| 4891 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4892 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4893 | #ifndef __BEOS__ |
| 4894 | /* We have incomplete socket support. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4895 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4896 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4897 | #if defined(SOCK_RDM) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4898 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4899 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4900 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4901 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4902 | #ifdef SO_DEBUG |
| 4903 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
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_ACCEPTCONN |
| 4906 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
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_REUSEADDR |
| 4909 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4910 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4911 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4912 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4913 | #endif |
| 4914 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4915 | #ifdef SO_KEEPALIVE |
| 4916 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4917 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4918 | #ifdef SO_DONTROUTE |
| 4919 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4920 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4921 | #ifdef SO_BROADCAST |
| 4922 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4923 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4924 | #ifdef SO_USELOOPBACK |
| 4925 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4926 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4927 | #ifdef SO_LINGER |
| 4928 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4929 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4930 | #ifdef SO_OOBINLINE |
| 4931 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4932 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4933 | #ifdef SO_REUSEPORT |
| 4934 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4935 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4936 | #ifdef SO_SNDBUF |
| 4937 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4938 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4939 | #ifdef SO_RCVBUF |
| 4940 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4941 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4942 | #ifdef SO_SNDLOWAT |
| 4943 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4944 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4945 | #ifdef SO_RCVLOWAT |
| 4946 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4947 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4948 | #ifdef SO_SNDTIMEO |
| 4949 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4950 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4951 | #ifdef SO_RCVTIMEO |
| 4952 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4953 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4954 | #ifdef SO_ERROR |
| 4955 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4956 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4957 | #ifdef SO_TYPE |
| 4958 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4959 | #endif |
Larry Hastings | 3b958e3 | 2010-04-02 11:18:17 +0000 | [diff] [blame] | 4960 | #ifdef SO_SETFIB |
| 4961 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB); |
| 4962 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4963 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4964 | /* Maximum number of connections for "listen" */ |
| 4965 | #ifdef SOMAXCONN |
| 4966 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4967 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4968 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4969 | #endif |
| 4970 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4971 | /* Flags for send, recv */ |
| 4972 | #ifdef MSG_OOB |
| 4973 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4974 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4975 | #ifdef MSG_PEEK |
| 4976 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4977 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4978 | #ifdef MSG_DONTROUTE |
| 4979 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4980 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4981 | #ifdef MSG_DONTWAIT |
| 4982 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4983 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4984 | #ifdef MSG_EOR |
| 4985 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4986 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4987 | #ifdef MSG_TRUNC |
| 4988 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4989 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4990 | #ifdef MSG_CTRUNC |
| 4991 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4992 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4993 | #ifdef MSG_WAITALL |
| 4994 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4995 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4996 | #ifdef MSG_BTAG |
| 4997 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4998 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4999 | #ifdef MSG_ETAG |
| 5000 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5001 | #endif |
| 5002 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5003 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 5004 | #ifdef SOL_SOCKET |
| 5005 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5006 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5007 | #ifdef SOL_IP |
| 5008 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5009 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5010 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5011 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5012 | #ifdef SOL_IPX |
| 5013 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5014 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5015 | #ifdef SOL_AX25 |
| 5016 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5017 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5018 | #ifdef SOL_ATALK |
| 5019 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5020 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5021 | #ifdef SOL_NETROM |
| 5022 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5023 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5024 | #ifdef SOL_ROSE |
| 5025 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5026 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5027 | #ifdef SOL_TCP |
| 5028 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5029 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5030 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5031 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5032 | #ifdef SOL_UDP |
| 5033 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5034 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5035 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5036 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5037 | #ifdef IPPROTO_IP |
| 5038 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5039 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5040 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5041 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5042 | #ifdef IPPROTO_HOPOPTS |
| 5043 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5044 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5045 | #ifdef IPPROTO_ICMP |
| 5046 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5047 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5048 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
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_IGMP |
| 5051 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
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_GGP |
| 5054 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
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_IPV4 |
| 5057 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
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_IPV6 |
| 5060 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 5061 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5062 | #ifdef IPPROTO_IPIP |
| 5063 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
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_TCP |
| 5066 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5067 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5068 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5069 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5070 | #ifdef IPPROTO_EGP |
| 5071 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5072 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5073 | #ifdef IPPROTO_PUP |
| 5074 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5075 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5076 | #ifdef IPPROTO_UDP |
| 5077 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5078 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5079 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5080 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5081 | #ifdef IPPROTO_IDP |
| 5082 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5083 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5084 | #ifdef IPPROTO_HELLO |
| 5085 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5086 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5087 | #ifdef IPPROTO_ND |
| 5088 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5089 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5090 | #ifdef IPPROTO_TP |
| 5091 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5092 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5093 | #ifdef IPPROTO_IPV6 |
| 5094 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5095 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5096 | #ifdef IPPROTO_ROUTING |
| 5097 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5098 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5099 | #ifdef IPPROTO_FRAGMENT |
| 5100 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5101 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5102 | #ifdef IPPROTO_RSVP |
| 5103 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5104 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5105 | #ifdef IPPROTO_GRE |
| 5106 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5107 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5108 | #ifdef IPPROTO_ESP |
| 5109 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5110 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5111 | #ifdef IPPROTO_AH |
| 5112 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5113 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5114 | #ifdef IPPROTO_MOBILE |
| 5115 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5116 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5117 | #ifdef IPPROTO_ICMPV6 |
| 5118 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5119 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5120 | #ifdef IPPROTO_NONE |
| 5121 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5122 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5123 | #ifdef IPPROTO_DSTOPTS |
| 5124 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5125 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5126 | #ifdef IPPROTO_XTP |
| 5127 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5128 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5129 | #ifdef IPPROTO_EON |
| 5130 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5131 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5132 | #ifdef IPPROTO_PIM |
| 5133 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5134 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5135 | #ifdef IPPROTO_IPCOMP |
| 5136 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5137 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5138 | #ifdef IPPROTO_VRRP |
| 5139 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5140 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5141 | #ifdef IPPROTO_BIP |
| 5142 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5143 | #endif |
| 5144 | /**/ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5145 | #ifdef IPPROTO_RAW |
| 5146 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5147 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5148 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5149 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5150 | #ifdef IPPROTO_MAX |
| 5151 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5152 | #endif |
| 5153 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5154 | /* Some port configuration */ |
| 5155 | #ifdef IPPORT_RESERVED |
| 5156 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5157 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5158 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5159 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5160 | #ifdef IPPORT_USERRESERVED |
| 5161 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5162 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5163 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5164 | #endif |
| 5165 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5166 | /* Some reserved IP v.4 addresses */ |
| 5167 | #ifdef INADDR_ANY |
| 5168 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5169 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5170 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5171 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5172 | #ifdef INADDR_BROADCAST |
| 5173 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5174 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5175 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5176 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5177 | #ifdef INADDR_LOOPBACK |
| 5178 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5179 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5180 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5181 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5182 | #ifdef INADDR_UNSPEC_GROUP |
| 5183 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5184 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5185 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5186 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5187 | #ifdef INADDR_ALLHOSTS_GROUP |
| 5188 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 5189 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5190 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5191 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5192 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5193 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 5194 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 5195 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5196 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5197 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5198 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5199 | #ifdef INADDR_NONE |
| 5200 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5201 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5202 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5203 | #endif |
| 5204 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5205 | /* IPv4 [gs]etsockopt options */ |
| 5206 | #ifdef IP_OPTIONS |
| 5207 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5208 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5209 | #ifdef IP_HDRINCL |
| 5210 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5211 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5212 | #ifdef IP_TOS |
| 5213 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5214 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5215 | #ifdef IP_TTL |
| 5216 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5217 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5218 | #ifdef IP_RECVOPTS |
| 5219 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5220 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5221 | #ifdef IP_RECVRETOPTS |
| 5222 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5223 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5224 | #ifdef IP_RECVDSTADDR |
| 5225 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5226 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5227 | #ifdef IP_RETOPTS |
| 5228 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5229 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5230 | #ifdef IP_MULTICAST_IF |
| 5231 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5232 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5233 | #ifdef IP_MULTICAST_TTL |
| 5234 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5235 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5236 | #ifdef IP_MULTICAST_LOOP |
| 5237 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5238 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5239 | #ifdef IP_ADD_MEMBERSHIP |
| 5240 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5241 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5242 | #ifdef IP_DROP_MEMBERSHIP |
| 5243 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5244 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5245 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 5246 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 5247 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5248 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5249 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 5250 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 5251 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5252 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5253 | #ifdef IP_MAX_MEMBERSHIPS |
| 5254 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5255 | #endif |
| 5256 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5257 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 5258 | #ifdef IPV6_JOIN_GROUP |
| 5259 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5260 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5261 | #ifdef IPV6_LEAVE_GROUP |
| 5262 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5263 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5264 | #ifdef IPV6_MULTICAST_HOPS |
| 5265 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5266 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5267 | #ifdef IPV6_MULTICAST_IF |
| 5268 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5269 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5270 | #ifdef IPV6_MULTICAST_LOOP |
| 5271 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5272 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5273 | #ifdef IPV6_UNICAST_HOPS |
| 5274 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5275 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5276 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5277 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5278 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5279 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5280 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5281 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5282 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5283 | #endif |
| 5284 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5285 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5286 | #endif |
| 5287 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5288 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5289 | #endif |
| 5290 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5291 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5292 | #endif |
| 5293 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5294 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5295 | #endif |
| 5296 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5297 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5298 | #endif |
| 5299 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5300 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5301 | #endif |
| 5302 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5303 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5304 | #endif |
| 5305 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5306 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5307 | #endif |
| 5308 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5309 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5310 | #endif |
| 5311 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5312 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5313 | #endif |
| 5314 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5315 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5316 | #endif |
| 5317 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5318 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5319 | #endif |
| 5320 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5321 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5322 | #endif |
| 5323 | #ifdef IPV6_RTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5324 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5325 | #endif |
| 5326 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5327 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5328 | #endif |
| 5329 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5330 | 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] | 5331 | #endif |
| 5332 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5333 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5334 | #endif |
| 5335 | #ifdef IPV6_TCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5336 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5337 | #endif |
| 5338 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5339 | 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] | 5340 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5341 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5342 | /* TCP options */ |
| 5343 | #ifdef TCP_NODELAY |
| 5344 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5345 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5346 | #ifdef TCP_MAXSEG |
| 5347 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5348 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5349 | #ifdef TCP_CORK |
| 5350 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5351 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5352 | #ifdef TCP_KEEPIDLE |
| 5353 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5354 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5355 | #ifdef TCP_KEEPINTVL |
| 5356 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5357 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5358 | #ifdef TCP_KEEPCNT |
| 5359 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5360 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5361 | #ifdef TCP_SYNCNT |
| 5362 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5363 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5364 | #ifdef TCP_LINGER2 |
| 5365 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5366 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5367 | #ifdef TCP_DEFER_ACCEPT |
| 5368 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5369 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5370 | #ifdef TCP_WINDOW_CLAMP |
| 5371 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5372 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5373 | #ifdef TCP_INFO |
| 5374 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5375 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5376 | #ifdef TCP_QUICKACK |
| 5377 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5378 | #endif |
| 5379 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5380 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5381 | /* IPX options */ |
| 5382 | #ifdef IPX_TYPE |
| 5383 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5384 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5385 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5386 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5387 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5388 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5389 | #endif |
| 5390 | #ifdef EAI_AGAIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5391 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5392 | #endif |
| 5393 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5394 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5395 | #endif |
| 5396 | #ifdef EAI_FAIL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5397 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5398 | #endif |
| 5399 | #ifdef EAI_FAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5400 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5401 | #endif |
| 5402 | #ifdef EAI_MEMORY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5403 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5404 | #endif |
| 5405 | #ifdef EAI_NODATA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5406 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5407 | #endif |
| 5408 | #ifdef EAI_NONAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5409 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5410 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5411 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5412 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5413 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5414 | #ifdef EAI_SERVICE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5415 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5416 | #endif |
| 5417 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5418 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5419 | #endif |
| 5420 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5421 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5422 | #endif |
| 5423 | #ifdef EAI_BADHINTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5424 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5425 | #endif |
| 5426 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5427 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5428 | #endif |
| 5429 | #ifdef EAI_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5430 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5431 | #endif |
| 5432 | #ifdef AI_PASSIVE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5433 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5434 | #endif |
| 5435 | #ifdef AI_CANONNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5436 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5437 | #endif |
| 5438 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5439 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5440 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5441 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5442 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5443 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5444 | #ifdef AI_MASK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5445 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5446 | #endif |
| 5447 | #ifdef AI_ALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5448 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5449 | #endif |
| 5450 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5451 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5452 | #endif |
| 5453 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5454 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5455 | #endif |
| 5456 | #ifdef AI_V4MAPPED |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5457 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5458 | #endif |
| 5459 | #ifdef AI_DEFAULT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5460 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5461 | #endif |
| 5462 | #ifdef NI_MAXHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5463 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5464 | #endif |
| 5465 | #ifdef NI_MAXSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5466 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5467 | #endif |
| 5468 | #ifdef NI_NOFQDN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5469 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5470 | #endif |
| 5471 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5472 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5473 | #endif |
| 5474 | #ifdef NI_NAMEREQD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5475 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5476 | #endif |
| 5477 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5478 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5479 | #endif |
| 5480 | #ifdef NI_DGRAM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5481 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5482 | #endif |
| 5483 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5484 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5485 | #ifdef SHUT_RD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5486 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5487 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5488 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5489 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5490 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5491 | #endif |
| 5492 | #ifdef SHUT_WR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5493 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5494 | #elif defined(SD_SEND) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5495 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5496 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5497 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5498 | #endif |
| 5499 | #ifdef SHUT_RDWR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5500 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5501 | #elif defined(SD_BOTH) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5502 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5503 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5504 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5505 | #endif |
| 5506 | |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5507 | #ifdef SIO_RCVALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5508 | { |
| 5509 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 5510 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 5511 | int i; |
| 5512 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) { |
| 5513 | PyObject *tmp; |
| 5514 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 5515 | if (tmp == NULL) |
| 5516 | return; |
| 5517 | PyModule_AddObject(m, names[i], tmp); |
| 5518 | } |
| 5519 | } |
| 5520 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5521 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5522 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5523 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5524 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5525 | #endif |
| 5526 | #ifdef RCVALL_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5527 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5528 | #endif |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5529 | #endif /* _MSTCPIP_ */ |
| 5530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5531 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5532 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5533 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5534 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5535 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5536 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5537 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5538 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5539 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5540 | |
| 5541 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5542 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5543 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5544 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5545 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5546 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5547 | if (af == AF_INET) { |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 5548 | #if (SIZEOF_INT != 4) |
| 5549 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5550 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5551 | unsigned int packed_addr; |
| 5552 | packed_addr = inet_addr(src); |
| 5553 | if (packed_addr == INADDR_NONE) |
| 5554 | return 0; |
| 5555 | memcpy(dst, &packed_addr, 4); |
| 5556 | return 1; |
| 5557 | } |
| 5558 | /* Should set errno to EAFNOSUPPORT */ |
| 5559 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5562 | const char * |
| 5563 | 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] | 5564 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5565 | if (af == AF_INET) { |
| 5566 | struct in_addr packed_addr; |
| 5567 | if (size < 16) |
| 5568 | /* Should set errno to ENOSPC. */ |
| 5569 | return NULL; |
| 5570 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5571 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5572 | } |
| 5573 | /* Should set errno to EAFNOSUPPORT */ |
| 5574 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5575 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5576 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5577 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5578 | #endif |