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 |
Martin Panter | 2167666 | 2016-11-14 04:13:55 +0000 | [diff] [blame] | 170 | # if defined(_AIX) && !defined(_LINUX_SOURCE_COMPAT) || defined(__osf__) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 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 */ |
Martin Panter | 2167666 | 2016-11-14 04:13:55 +0000 | [diff] [blame] | 176 | # elif defined(_LINUX_SOURCE_COMPAT) /* Linux compatibility on AIX */ |
| 177 | # define HAVE_GETHOSTBYNAME_R_6_ARG |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 178 | # else |
| 179 | # undef HAVE_GETHOSTBYNAME_R |
| 180 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 181 | #endif |
| 182 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 183 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 184 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 185 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 186 | #endif |
| 187 | |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 188 | /* To use __FreeBSD_version, __OpenBSD__, and __NetBSD_Version__ */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 189 | #ifdef HAVE_SYS_PARAM_H |
| 190 | #include <sys/param.h> |
| 191 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 192 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Ned Deily | 1c2a7b5 | 2016-02-15 16:51:24 +1100 | [diff] [blame] | 193 | (this includes the getaddrinfo emulation) protect access with a lock. |
| 194 | |
| 195 | getaddrinfo is thread-safe on Mac OS X 10.5 and later. Originally it was |
| 196 | a mix of code including an unsafe implementation from an old BSD's |
| 197 | libresolv. In 10.5 Apple reimplemented it as a safe IPC call to the |
| 198 | mDNSResponder process. 10.5 is the first be UNIX '03 certified, which |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 199 | includes the requirement that getaddrinfo be thread-safe. See issue #25924. |
Ned Deily | 1c2a7b5 | 2016-02-15 16:51:24 +1100 | [diff] [blame] | 200 | |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 201 | It's thread-safe in OpenBSD starting with 5.4, released Nov 2013: |
| 202 | http://www.openbsd.org/plus54.html |
| 203 | |
| 204 | It's thread-safe in NetBSD starting with 4.0, released Dec 2007: |
| 205 | |
| 206 | http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getaddrinfo.c.diff?r1=1.82&r2=1.83 |
| 207 | */ |
Ned Deily | 1c2a7b5 | 2016-02-15 16:51:24 +1100 | [diff] [blame] | 208 | #if defined(WITH_THREAD) && ( \ |
| 209 | (defined(__APPLE__) && \ |
| 210 | 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] | 211 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Ned Deily | 3058eb4 | 2016-02-23 22:03:39 +1100 | [diff] [blame] | 212 | (defined(__OpenBSD__) && OpenBSD+0 < 201311) || \ |
| 213 | (defined(__NetBSD__) && __NetBSD_Version__+0 < 400000000) || \ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 214 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 215 | #define USE_GETADDRINFO_LOCK |
| 216 | #endif |
| 217 | |
| 218 | #ifdef USE_GETADDRINFO_LOCK |
| 219 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 220 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 221 | #else |
| 222 | #define ACQUIRE_GETADDRINFO_LOCK |
| 223 | #define RELEASE_GETADDRINFO_LOCK |
| 224 | #endif |
| 225 | |
| 226 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 227 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 228 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 230 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 231 | # include <types.h> |
| 232 | # include <io.h> |
| 233 | # include <sys/ioctl.h> |
| 234 | # include <utils.h> |
| 235 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 236 | #endif |
| 237 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 238 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 239 | # include <ioctl.h> |
| 240 | #endif |
| 241 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 242 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 243 | # define INCL_DOS |
| 244 | # define INCL_DOSERRORS |
| 245 | # define INCL_NOPMAPI |
| 246 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 247 | #endif |
| 248 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 249 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 250 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 251 | functions are declared correctly if compiling with |
| 252 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 253 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 254 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 255 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 256 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 257 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 258 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 259 | #undef _XOPEN_SOURCE |
| 260 | #include <sys/socket.h> |
| 261 | #include <sys/types.h> |
| 262 | #include <netinet/in.h> |
| 263 | #ifdef _SS_ALIGNSIZE |
| 264 | #define HAVE_GETADDRINFO 1 |
| 265 | #define HAVE_GETNAMEINFO 1 |
| 266 | #endif |
| 267 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 268 | #define HAVE_INET_PTON |
| 269 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 270 | #endif |
| 271 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 272 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 273 | 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] | 274 | are just busted. Same thing for Solaris. */ |
| 275 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 276 | #define INET_ADDRSTRLEN 16 |
| 277 | #endif |
| 278 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 279 | /* Generic includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 280 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 282 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 283 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 284 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 285 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 286 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 287 | |
| 288 | /* Addressing includes */ |
| 289 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 290 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 291 | |
| 292 | /* Non-MS WINDOWS includes */ |
| 293 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 295 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 296 | # ifdef __BEOS__ |
| 297 | # include <net/netdb.h> |
| 298 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 299 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 300 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 301 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 302 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 303 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 304 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 305 | # ifndef RISCOS |
| 306 | # include <fcntl.h> |
| 307 | # else |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 308 | # include <sys/ioctl.h> |
| 309 | # include <socklib.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 310 | # define NO_DUP |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 311 | int h_errno; /* not used */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 312 | # define INET_ADDRSTRLEN 16 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 313 | # endif |
| 314 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 315 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 316 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 317 | /* MS_WINDOWS includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 318 | # ifdef HAVE_FCNTL_H |
| 319 | # include <fcntl.h> |
| 320 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 321 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 322 | #endif |
| 323 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 324 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 325 | |
| 326 | #ifndef offsetof |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 327 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 328 | #endif |
| 329 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 330 | #ifndef O_NONBLOCK |
| 331 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 332 | #endif |
| 333 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 334 | /* include Python's addrinfo.h unless it causes trouble */ |
| 335 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 336 | /* Do not include addinfo.h on some newer IRIX versions. |
| 337 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 338 | * for example, but not by 6.5.10. |
| 339 | */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 340 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 341 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 342 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 343 | */ |
| 344 | #else |
| 345 | # include "addrinfo.h" |
| 346 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 347 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 348 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 349 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 350 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 351 | 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] | 352 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 353 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 354 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 355 | #ifdef __APPLE__ |
| 356 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 357 | failure, so we must use the emulation instead of the libinfo |
| 358 | implementation. Unfortunately, performing an autoconf test |
| 359 | for this bug would require DNS access for the machine performing |
| 360 | the configuration, which is not acceptable. Therefore, we |
| 361 | determine the bug just by checking for __APPLE__. If this bug |
| 362 | gets ever fixed, perhaps checking for sys/version.h would be |
| 363 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 364 | #ifndef HAVE_GETNAMEINFO |
| 365 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 366 | Find to check for Jaguar is that it has getnameinfo(), which |
| 367 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 368 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 369 | #endif |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 370 | |
| 371 | #ifdef HAVE_INET_ATON |
| 372 | #define USE_INET_ATON_WEAKLINK |
| 373 | #endif |
| 374 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 375 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 376 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 377 | /* 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] | 378 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 379 | /* avoid clashes with the C library definition of the symbol. */ |
| 380 | #define getaddrinfo fake_getaddrinfo |
| 381 | #define gai_strerror fake_gai_strerror |
| 382 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 383 | #include "getaddrinfo.c" |
| 384 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 385 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 386 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 387 | #include "getnameinfo.c" |
| 388 | #endif |
| 389 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 390 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 391 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 392 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 393 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 394 | #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] | 395 | #endif |
| 396 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 397 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 398 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 399 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 400 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 401 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 402 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 403 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 404 | #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] | 405 | #endif |
| 406 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 407 | #ifndef SOCKETCLOSE |
| 408 | #define SOCKETCLOSE close |
| 409 | #endif |
| 410 | |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 411 | #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] | 412 | #define USE_BLUETOOTH 1 |
| 413 | #if defined(__FreeBSD__) |
| 414 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 415 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 416 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 417 | #define SOL_HCI SOL_HCI_RAW |
| 418 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 419 | #define sockaddr_l2 sockaddr_l2cap |
| 420 | #define sockaddr_rc sockaddr_rfcomm |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +0000 | [diff] [blame] | 421 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 422 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 423 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 424 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 425 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 426 | #define sockaddr_l2 sockaddr_bt |
| 427 | #define sockaddr_rc sockaddr_bt |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 428 | #define sockaddr_hci sockaddr_bt |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 429 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 430 | #define SOL_HCI BTPROTO_HCI |
| 431 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 432 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 433 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 434 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 435 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 436 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 437 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 438 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 439 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 440 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 441 | #endif |
| 442 | #endif |
| 443 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 444 | #ifdef __VMS |
| 445 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 446 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 447 | #endif |
| 448 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 449 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 450 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 451 | /* |
| 452 | * Constants for getnameinfo() |
| 453 | */ |
| 454 | #if !defined(NI_MAXHOST) |
| 455 | #define NI_MAXHOST 1025 |
| 456 | #endif |
| 457 | #if !defined(NI_MAXSERV) |
| 458 | #define NI_MAXSERV 32 |
| 459 | #endif |
| 460 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 461 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 462 | a Py prefix (or use CapitalizedWords). Later... */ |
| 463 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 464 | /* Global variable holding the exception type for errors detected |
| 465 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 466 | static PyObject *socket_error; |
| 467 | static PyObject *socket_herror; |
| 468 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 469 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 471 | #ifdef RISCOS |
| 472 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 473 | static int taskwindow; |
| 474 | #endif |
| 475 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 476 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 477 | The sock_type variable contains pointers to various functions, |
| 478 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 479 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 480 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 481 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 482 | #if defined(HAVE_POLL_H) |
| 483 | #include <poll.h> |
| 484 | #elif defined(HAVE_SYS_POLL_H) |
| 485 | #include <sys/poll.h> |
| 486 | #endif |
| 487 | |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 488 | #ifdef HAVE_POLL |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 489 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 490 | #define IS_SELECTABLE(s) 1 |
| 491 | /* 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] | 492 | #else |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 493 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 494 | * little white lie. */ |
| 495 | #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] | 496 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 497 | |
| 498 | static PyObject* |
| 499 | select_error(void) |
| 500 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 501 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 502 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 505 | #ifdef MS_WINDOWS |
| 506 | #ifndef WSAEAGAIN |
| 507 | #define WSAEAGAIN WSAEWOULDBLOCK |
| 508 | #endif |
| 509 | #define CHECK_ERRNO(expected) \ |
| 510 | (WSAGetLastError() == WSA ## expected) |
| 511 | #else |
| 512 | #define CHECK_ERRNO(expected) \ |
| 513 | (errno == expected) |
| 514 | #endif |
| 515 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 516 | /* Convenience function to raise an error according to errno |
| 517 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 518 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 519 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 520 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 521 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 522 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 523 | int err_no = WSAGetLastError(); |
| 524 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 525 | recognizes the error codes used by both GetLastError() and |
| 526 | WSAGetLastError */ |
| 527 | if (err_no) |
| 528 | return PyErr_SetExcFromWindowsErr(socket_error, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 529 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 530 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 531 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 532 | if (sock_errno() != NO_ERROR) { |
| 533 | APIRET rc; |
| 534 | ULONG msglen; |
| 535 | char outbuf[100]; |
| 536 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 538 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 539 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 540 | myerrorcode - SOCBASEERR + 26, |
| 541 | "mptn.msg", |
| 542 | &msglen); |
| 543 | if (rc == NO_ERROR) { |
| 544 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 546 | /* OS/2 doesn't guarantee a terminator */ |
| 547 | outbuf[msglen] = '\0'; |
| 548 | if (strlen(outbuf) > 0) { |
| 549 | /* If non-empty msg, trim CRLF */ |
| 550 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 551 | while (lastc > outbuf && |
| 552 | isspace(Py_CHARMASK(*lastc))) { |
| 553 | /* Trim trailing whitespace (CRLF) */ |
| 554 | *lastc-- = '\0'; |
| 555 | } |
| 556 | } |
| 557 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 558 | if (v != NULL) { |
| 559 | PyErr_SetObject(socket_error, v); |
| 560 | Py_DECREF(v); |
| 561 | } |
| 562 | return NULL; |
| 563 | } |
| 564 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 565 | #endif |
| 566 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 567 | #if defined(RISCOS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 568 | if (_inet_error.errnum != NULL) { |
| 569 | PyObject *v; |
| 570 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 571 | if (v != NULL) { |
| 572 | PyErr_SetObject(socket_error, v); |
| 573 | Py_DECREF(v); |
| 574 | } |
| 575 | return NULL; |
| 576 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 577 | #endif |
| 578 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 579 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 582 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 583 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 584 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 585 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 586 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 587 | |
| 588 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 589 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 590 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 591 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 592 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 593 | if (v != NULL) { |
| 594 | PyErr_SetObject(socket_herror, v); |
| 595 | Py_DECREF(v); |
| 596 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 598 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
| 602 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 603 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 604 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 605 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 606 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 607 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 608 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 609 | if (error == EAI_SYSTEM) |
| 610 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 611 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 612 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 613 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 614 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 615 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 616 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 617 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 618 | if (v != NULL) { |
| 619 | PyErr_SetObject(socket_gaierror, v); |
| 620 | Py_DECREF(v); |
| 621 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 623 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 626 | #ifdef __VMS |
| 627 | /* Function to send in segments */ |
| 628 | static int |
| 629 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 630 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 631 | int n = 0; |
| 632 | int remaining = len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 633 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 634 | while (remaining > 0) { |
| 635 | unsigned int segment; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 637 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 638 | n = send(sock_fd, buf, segment, flags); |
| 639 | if (n < 0) { |
| 640 | return n; |
| 641 | } |
| 642 | remaining -= segment; |
| 643 | buf += segment; |
| 644 | } /* end while */ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 646 | return len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 647 | } |
| 648 | #endif |
| 649 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 650 | /* Function to perform the setting of socket blocking mode |
| 651 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 652 | static int |
| 653 | internal_setblocking(PySocketSockObject *s, int block) |
| 654 | { |
| 655 | #ifndef RISCOS |
| 656 | #ifndef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 657 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | #endif |
| 659 | #endif |
| 660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 661 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 662 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | block = !block; |
| 664 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 665 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 666 | #else |
| 667 | #ifndef RISCOS |
| 668 | #ifndef MS_WINDOWS |
| 669 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 670 | block = !block; |
| 671 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 672 | #elif defined(__VMS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 673 | block = !block; |
| 674 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 675 | #else /* !PYOS_OS2 && !__VMS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 676 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 677 | if (block) |
| 678 | delay_flag &= (~O_NONBLOCK); |
| 679 | else |
| 680 | delay_flag |= O_NONBLOCK; |
| 681 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 682 | #endif /* !PYOS_OS2 */ |
| 683 | #else /* MS_WINDOWS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 684 | block = !block; |
| 685 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 686 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 687 | #else /* RISCOS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 688 | block = !block; |
| 689 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 690 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 691 | #endif /* __BEOS__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 692 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 694 | /* Since these don't return anything */ |
| 695 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 698 | /* 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] | 699 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 700 | This does not raise an exception; we'll let our caller do that |
| 701 | after they've reacquired the interpreter lock. |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 702 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 703 | static int |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 704 | internal_select_ex(PySocketSockObject *s, int writing, double interval) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 705 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 706 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 707 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 708 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 709 | if (s->sock_timeout <= 0.0) |
| 710 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 712 | /* Guard against closed socket */ |
| 713 | if (s->sock_fd < 0) |
| 714 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 715 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 716 | /* Handling this condition here simplifies the select loops */ |
| 717 | if (interval < 0.0) |
| 718 | return 1; |
| 719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 720 | /* Prefer poll, if available, since you can poll() any fd |
| 721 | * which can't be done with select(). */ |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 722 | #ifdef HAVE_POLL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 723 | { |
| 724 | struct pollfd pollfd; |
| 725 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 726 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 727 | pollfd.fd = s->sock_fd; |
| 728 | pollfd.events = writing ? POLLOUT : POLLIN; |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 729 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 730 | /* s->sock_timeout is in seconds, timeout in ms */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 731 | timeout = (int)(interval * 1000 + 0.5); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 732 | n = poll(&pollfd, 1, timeout); |
| 733 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 734 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 735 | { |
| 736 | /* Construct the arguments to select */ |
| 737 | fd_set fds; |
| 738 | struct timeval tv; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 739 | tv.tv_sec = (int)interval; |
| 740 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 741 | FD_ZERO(&fds); |
| 742 | FD_SET(s->sock_fd, &fds); |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 743 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 744 | /* See if the socket is ready */ |
| 745 | if (writing) |
| 746 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 747 | else |
| 748 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 749 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 750 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 751 | |
| 752 | if (n < 0) |
| 753 | return -1; |
| 754 | if (n == 0) |
| 755 | return 1; |
| 756 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 759 | static int |
| 760 | internal_select(PySocketSockObject *s, int writing) |
| 761 | { |
| 762 | return internal_select_ex(s, writing, s->sock_timeout); |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | Two macros for automatic retry of select() in case of false positives |
| 767 | (for example, select() could indicate a socket is ready for reading |
| 768 | but the data then discarded by the OS because of a wrong checksum). |
| 769 | Here is an example of use: |
| 770 | |
| 771 | BEGIN_SELECT_LOOP(s) |
| 772 | Py_BEGIN_ALLOW_THREADS |
| 773 | timeout = internal_select_ex(s, 0, interval); |
| 774 | if (!timeout) |
| 775 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 776 | Py_END_ALLOW_THREADS |
| 777 | if (timeout == 1) { |
| 778 | PyErr_SetString(socket_timeout, "timed out"); |
| 779 | return -1; |
| 780 | } |
| 781 | END_SELECT_LOOP(s) |
| 782 | */ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 783 | #define BEGIN_SELECT_LOOP(s) \ |
| 784 | { \ |
Benjamin Peterson | e5fa8b0 | 2016-07-07 00:24:26 -0700 | [diff] [blame] | 785 | double deadline = 0, interval = s->sock_timeout; \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 786 | int has_timeout = s->sock_timeout > 0.0; \ |
| 787 | if (has_timeout) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 788 | deadline = _PyTime_FloatTime() + s->sock_timeout; \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 789 | } \ |
| 790 | while (1) { \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 791 | errno = 0; |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 792 | |
| 793 | #define END_SELECT_LOOP(s) \ |
| 794 | if (!has_timeout || \ |
| 795 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \ |
| 796 | break; \ |
Kristján Valur Jónsson | 868f0aa | 2013-03-19 13:53:56 -0700 | [diff] [blame] | 797 | interval = deadline - _PyTime_FloatTime(); \ |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 798 | } \ |
Kristján Valur Jónsson | 620e364 | 2013-03-19 13:01:05 -0700 | [diff] [blame] | 799 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 800 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 801 | /* Initialize a new socket object. */ |
| 802 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 803 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 804 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 805 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 806 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 807 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 808 | { |
| 809 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 810 | int block = 1; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 811 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 812 | s->sock_fd = fd; |
| 813 | s->sock_family = family; |
| 814 | s->sock_type = type; |
| 815 | s->sock_proto = proto; |
| 816 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 818 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 819 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 820 | if (defaulttimeout >= 0.0) |
| 821 | internal_setblocking(s, 0); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 823 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 824 | if (taskwindow) |
| 825 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 826 | #endif |
| 827 | } |
| 828 | |
| 829 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 830 | /* Create a new socket object. |
| 831 | This just creates the object and initializes it. |
| 832 | If the creation fails, return NULL and set an exception (implicit |
| 833 | in NEWOBJ()). */ |
| 834 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 835 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 836 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 837 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 838 | PySocketSockObject *s; |
| 839 | s = (PySocketSockObject *) |
| 840 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 841 | if (s != NULL) |
| 842 | init_sockobject(s, fd, family, type, proto); |
| 843 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 847 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 848 | thread to be in gethostbyname or getaddrinfo */ |
| 849 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Matthias Klose | c551776 | 2012-08-14 17:24:47 +0200 | [diff] [blame] | 850 | static PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 851 | #endif |
| 852 | |
| 853 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 854 | /* Convert a string specifying a host name or one of a few symbolic |
| 855 | names to a numeric IP address. This usually calls gethostbyname() |
| 856 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 857 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 858 | an error occurred; then an exception is raised. */ |
| 859 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 860 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 861 | 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] | 862 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 863 | struct addrinfo hints, *res; |
| 864 | int error; |
| 865 | int d1, d2, d3, d4; |
| 866 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 868 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 869 | if (name[0] == '\0') { |
| 870 | int siz; |
| 871 | memset(&hints, 0, sizeof(hints)); |
| 872 | hints.ai_family = af; |
| 873 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 874 | hints.ai_flags = AI_PASSIVE; |
| 875 | Py_BEGIN_ALLOW_THREADS |
| 876 | ACQUIRE_GETADDRINFO_LOCK |
| 877 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 878 | Py_END_ALLOW_THREADS |
| 879 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 880 | *are* safe regarding their return value, ie. that a |
| 881 | subsequent call to getaddrinfo() does not destroy the |
| 882 | outcome of the first call. */ |
| 883 | RELEASE_GETADDRINFO_LOCK |
| 884 | if (error) { |
| 885 | set_gaierror(error); |
| 886 | return -1; |
| 887 | } |
| 888 | switch (res->ai_family) { |
| 889 | case AF_INET: |
| 890 | siz = 4; |
| 891 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 892 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 893 | case AF_INET6: |
| 894 | siz = 16; |
| 895 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 896 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 897 | default: |
| 898 | freeaddrinfo(res); |
| 899 | PyErr_SetString(socket_error, |
| 900 | "unsupported address family"); |
| 901 | return -1; |
| 902 | } |
| 903 | if (res->ai_next) { |
| 904 | freeaddrinfo(res); |
| 905 | PyErr_SetString(socket_error, |
| 906 | "wildcard resolved to multiple address"); |
| 907 | return -1; |
| 908 | } |
| 909 | if (res->ai_addrlen < addr_ret_size) |
| 910 | addr_ret_size = res->ai_addrlen; |
| 911 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 912 | freeaddrinfo(res); |
| 913 | return siz; |
| 914 | } |
| 915 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 916 | struct sockaddr_in *sin; |
| 917 | if (af != AF_INET && af != AF_UNSPEC) { |
| 918 | PyErr_SetString(socket_error, |
| 919 | "address family mismatched"); |
| 920 | return -1; |
| 921 | } |
| 922 | sin = (struct sockaddr_in *)addr_ret; |
| 923 | memset((void *) sin, '\0', sizeof(*sin)); |
| 924 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 925 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 926 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 927 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 928 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 929 | return sizeof(sin->sin_addr); |
| 930 | } |
| 931 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 932 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 933 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 934 | struct sockaddr_in *sin; |
| 935 | sin = (struct sockaddr_in *)addr_ret; |
| 936 | sin->sin_addr.s_addr = htonl( |
| 937 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 938 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 939 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 940 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 941 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 942 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 943 | return 4; |
| 944 | } |
| 945 | memset(&hints, 0, sizeof(hints)); |
| 946 | hints.ai_family = af; |
| 947 | Py_BEGIN_ALLOW_THREADS |
| 948 | ACQUIRE_GETADDRINFO_LOCK |
| 949 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 950 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 951 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 952 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 953 | if no address family is given. Assume IPv4 for now.*/ |
| 954 | hints.ai_family = AF_INET; |
| 955 | error = getaddrinfo(name, NULL, &hints, &res); |
| 956 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 957 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 958 | Py_END_ALLOW_THREADS |
| 959 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 960 | if (error) { |
| 961 | set_gaierror(error); |
| 962 | return -1; |
| 963 | } |
| 964 | if (res->ai_addrlen < addr_ret_size) |
| 965 | addr_ret_size = res->ai_addrlen; |
| 966 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 967 | freeaddrinfo(res); |
| 968 | switch (addr_ret->sa_family) { |
| 969 | case AF_INET: |
| 970 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 971 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 972 | case AF_INET6: |
| 973 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 974 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 975 | default: |
| 976 | PyErr_SetString(socket_error, "unknown address family"); |
| 977 | return -1; |
| 978 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 981 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 982 | /* Create a string object representing an IP address. |
| 983 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 984 | size numbers). */ |
| 985 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 986 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 987 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 988 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 989 | char buf[NI_MAXHOST]; |
| 990 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 991 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 992 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 993 | NI_NUMERICHOST); |
| 994 | if (error) { |
| 995 | set_gaierror(error); |
| 996 | return NULL; |
| 997 | } |
| 998 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1002 | #ifdef USE_BLUETOOTH |
| 1003 | /* Convert a string representation of a Bluetooth address into a numeric |
| 1004 | address. Returns the length (6), or raises an exception and returns -1 if |
| 1005 | an error occurred. */ |
| 1006 | |
| 1007 | static int |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1008 | setbdaddr(const char *name, bdaddr_t *bdaddr) |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1009 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1010 | unsigned int b0, b1, b2, b3, b4, b5; |
| 1011 | char ch; |
| 1012 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1014 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 1015 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 1016 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 1017 | bdaddr->b[0] = b0; |
| 1018 | bdaddr->b[1] = b1; |
| 1019 | bdaddr->b[2] = b2; |
| 1020 | bdaddr->b[3] = b3; |
| 1021 | bdaddr->b[4] = b4; |
| 1022 | bdaddr->b[5] = b5; |
| 1023 | return 6; |
| 1024 | } else { |
| 1025 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 1026 | return -1; |
| 1027 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* Create a string representation of the Bluetooth address. This is always a |
| 1031 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 1032 | value (zero padded if necessary). */ |
| 1033 | |
| 1034 | static PyObject * |
| 1035 | makebdaddr(bdaddr_t *bdaddr) |
| 1036 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1037 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1039 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1040 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 1041 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 1042 | return PyString_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1043 | } |
| 1044 | #endif |
| 1045 | |
| 1046 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1047 | /* Create an object representing the given socket address, |
| 1048 | suitable for passing it back to bind(), connect() etc. |
| 1049 | The family field of the sockaddr structure is inspected |
| 1050 | to determine what kind of address it really is. */ |
| 1051 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1052 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1053 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1054 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1055 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1056 | if (addrlen == 0) { |
| 1057 | /* No address -- may be recvfrom() from known socket */ |
| 1058 | Py_INCREF(Py_None); |
| 1059 | return Py_None; |
| 1060 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1061 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1062 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1063 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
| 1064 | addr->sa_family = AF_INET; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1065 | #endif /* __BEOS__ */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1067 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1069 | case AF_INET: |
| 1070 | { |
| 1071 | struct sockaddr_in *a; |
| 1072 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1073 | PyObject *ret = NULL; |
| 1074 | if (addrobj) { |
| 1075 | a = (struct sockaddr_in *)addr; |
| 1076 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1077 | Py_DECREF(addrobj); |
| 1078 | } |
| 1079 | return ret; |
| 1080 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1081 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1082 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1083 | case AF_UNIX: |
| 1084 | { |
| 1085 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1086 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1087 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1088 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 1089 | return PyString_FromStringAndSize(a->sun_path, |
| 1090 | addrlen); |
| 1091 | } |
| 1092 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1093 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1094 | { |
| 1095 | /* regular NULL-terminated string */ |
| 1096 | return PyString_FromString(a->sun_path); |
| 1097 | } |
| 1098 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1099 | #endif /* AF_UNIX */ |
| 1100 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1101 | #if defined(AF_NETLINK) |
| 1102 | case AF_NETLINK: |
| 1103 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1104 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1105 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1106 | } |
| 1107 | #endif /* AF_NETLINK */ |
| 1108 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1109 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1110 | case AF_INET6: |
| 1111 | { |
| 1112 | struct sockaddr_in6 *a; |
| 1113 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1114 | PyObject *ret = NULL; |
| 1115 | if (addrobj) { |
| 1116 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1117 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1118 | addrobj, |
| 1119 | ntohs(a->sin6_port), |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1120 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1121 | a->sin6_scope_id); |
| 1122 | Py_DECREF(addrobj); |
| 1123 | } |
| 1124 | return ret; |
| 1125 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1126 | #endif /* ENABLE_IPV6 */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1127 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1128 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1129 | case AF_BLUETOOTH: |
| 1130 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1131 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1132 | case BTPROTO_L2CAP: |
| 1133 | { |
| 1134 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1135 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1136 | PyObject *ret = NULL; |
| 1137 | if (addrobj) { |
| 1138 | ret = Py_BuildValue("Oi", |
| 1139 | addrobj, |
| 1140 | _BT_L2_MEMB(a, psm)); |
| 1141 | Py_DECREF(addrobj); |
| 1142 | } |
| 1143 | return ret; |
| 1144 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1145 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1146 | case BTPROTO_RFCOMM: |
| 1147 | { |
| 1148 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1149 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1150 | PyObject *ret = NULL; |
| 1151 | if (addrobj) { |
| 1152 | ret = Py_BuildValue("Oi", |
| 1153 | addrobj, |
| 1154 | _BT_RC_MEMB(a, channel)); |
| 1155 | Py_DECREF(addrobj); |
| 1156 | } |
| 1157 | return ret; |
| 1158 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1159 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1160 | case BTPROTO_HCI: |
| 1161 | { |
| 1162 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1163 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1164 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1165 | #else /* __NetBSD__ || __DragonFly__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1166 | PyObject *ret = NULL; |
| 1167 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1168 | return ret; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1169 | #endif /* !(__NetBSD__ || __DragonFly__) */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1170 | } |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1171 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1172 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1173 | case BTPROTO_SCO: |
| 1174 | { |
| 1175 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1176 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1177 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1178 | #endif /* !__FreeBSD__ */ |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1179 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1180 | default: |
| 1181 | PyErr_SetString(PyExc_ValueError, |
| 1182 | "Unknown Bluetooth protocol"); |
| 1183 | return NULL; |
| 1184 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1185 | #endif /* USE_BLUETOOTH */ |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1186 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1187 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1188 | case AF_PACKET: |
| 1189 | { |
| 1190 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1191 | const char *ifname = ""; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1192 | struct ifreq ifr; |
| 1193 | /* need to look up interface name give index */ |
| 1194 | if (a->sll_ifindex) { |
| 1195 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1196 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1197 | ifname = ifr.ifr_name; |
| 1198 | } |
| 1199 | return Py_BuildValue("shbhs#", |
| 1200 | ifname, |
| 1201 | ntohs(a->sll_protocol), |
| 1202 | a->sll_pkttype, |
| 1203 | a->sll_hatype, |
| 1204 | a->sll_addr, |
| 1205 | a->sll_halen); |
| 1206 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1207 | #endif /* HAVE_NETPACKET_PACKET_H && SIOCGIFNAME */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1208 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1209 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1210 | case AF_TIPC: |
| 1211 | { |
| 1212 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1213 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1214 | return Py_BuildValue("IIIII", |
| 1215 | a->addrtype, |
| 1216 | a->addr.nameseq.type, |
| 1217 | a->addr.nameseq.lower, |
| 1218 | a->addr.nameseq.upper, |
| 1219 | a->scope); |
| 1220 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1221 | return Py_BuildValue("IIIII", |
| 1222 | a->addrtype, |
| 1223 | a->addr.name.name.type, |
| 1224 | a->addr.name.name.instance, |
| 1225 | a->addr.name.name.instance, |
| 1226 | a->scope); |
| 1227 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1228 | return Py_BuildValue("IIIII", |
| 1229 | a->addrtype, |
| 1230 | a->addr.id.node, |
| 1231 | a->addr.id.ref, |
| 1232 | 0, |
| 1233 | a->scope); |
| 1234 | } else { |
| 1235 | PyErr_SetString(PyExc_ValueError, |
| 1236 | "Invalid address type"); |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1240 | #endif /* HAVE_LINUX_TIPC_H */ |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1242 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1244 | default: |
| 1245 | /* If we don't know the address family, don't raise an |
| 1246 | exception -- return it as a tuple. */ |
| 1247 | return Py_BuildValue("is#", |
| 1248 | addr->sa_family, |
| 1249 | addr->sa_data, |
| 1250 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1252 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1255 | |
| 1256 | /* Parse a socket address argument according to the socket object's |
| 1257 | address family. Return 1 if the address was in the proper format, |
| 1258 | 0 of not. The address is returned through addr_ret, its length |
| 1259 | through len_ret. */ |
| 1260 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1261 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1262 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1263 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1264 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1265 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1266 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1267 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1268 | case AF_UNIX: |
| 1269 | { |
| 1270 | struct sockaddr_un* addr; |
| 1271 | char *path; |
| 1272 | int len; |
| 1273 | if (!PyArg_Parse(args, "t#", &path, &len)) |
| 1274 | return 0; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1275 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1276 | addr = (struct sockaddr_un*)addr_ret; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1277 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1278 | if (len > 0 && path[0] == 0) { |
| 1279 | /* Linux abstract namespace extension */ |
| 1280 | if (len > sizeof addr->sun_path) { |
| 1281 | PyErr_SetString(socket_error, |
| 1282 | "AF_UNIX path too long"); |
| 1283 | return 0; |
| 1284 | } |
| 1285 | } |
| 1286 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1287 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1288 | { |
| 1289 | /* regular NULL-terminated string */ |
| 1290 | if (len >= sizeof addr->sun_path) { |
| 1291 | PyErr_SetString(socket_error, |
| 1292 | "AF_UNIX path too long"); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | addr->sun_path[len] = 0; |
| 1296 | } |
| 1297 | addr->sun_family = s->sock_family; |
| 1298 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1299 | #if defined(PYOS_OS2) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1300 | *len_ret = sizeof(*addr); |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1301 | #else /* PYOS_OS2 */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1302 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1303 | #endif /* !PYOS_OS2 */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1304 | return 1; |
| 1305 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1306 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1307 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1308 | #if defined(AF_NETLINK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1309 | case AF_NETLINK: |
| 1310 | { |
| 1311 | struct sockaddr_nl* addr; |
| 1312 | int pid, groups; |
| 1313 | addr = (struct sockaddr_nl *)addr_ret; |
| 1314 | if (!PyTuple_Check(args)) { |
| 1315 | PyErr_Format( |
| 1316 | PyExc_TypeError, |
| 1317 | "getsockaddrarg: " |
| 1318 | "AF_NETLINK address must be tuple, not %.500s", |
| 1319 | Py_TYPE(args)->tp_name); |
| 1320 | return 0; |
| 1321 | } |
| 1322 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1323 | return 0; |
| 1324 | addr->nl_family = AF_NETLINK; |
| 1325 | addr->nl_pid = pid; |
| 1326 | addr->nl_groups = groups; |
| 1327 | *len_ret = sizeof(*addr); |
| 1328 | return 1; |
| 1329 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1330 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1332 | case AF_INET: |
| 1333 | { |
| 1334 | struct sockaddr_in* addr; |
| 1335 | char *host; |
| 1336 | int port, result; |
| 1337 | if (!PyTuple_Check(args)) { |
| 1338 | PyErr_Format( |
| 1339 | PyExc_TypeError, |
| 1340 | "getsockaddrarg: " |
| 1341 | "AF_INET address must be tuple, not %.500s", |
| 1342 | Py_TYPE(args)->tp_name); |
| 1343 | return 0; |
| 1344 | } |
| 1345 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1346 | "idna", &host, &port)) |
| 1347 | return 0; |
| 1348 | addr=(struct sockaddr_in*)addr_ret; |
| 1349 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1350 | sizeof(*addr), AF_INET); |
| 1351 | PyMem_Free(host); |
| 1352 | if (result < 0) |
| 1353 | return 0; |
| 1354 | if (port < 0 || port > 0xffff) { |
| 1355 | PyErr_SetString( |
| 1356 | PyExc_OverflowError, |
| 1357 | "getsockaddrarg: port must be 0-65535."); |
| 1358 | return 0; |
| 1359 | } |
| 1360 | addr->sin_family = AF_INET; |
| 1361 | addr->sin_port = htons((short)port); |
| 1362 | *len_ret = sizeof *addr; |
| 1363 | return 1; |
| 1364 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1365 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1366 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1367 | case AF_INET6: |
| 1368 | { |
| 1369 | struct sockaddr_in6* addr; |
| 1370 | char *host; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1371 | int port, result; |
| 1372 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1373 | flowinfo = scope_id = 0; |
| 1374 | if (!PyTuple_Check(args)) { |
| 1375 | PyErr_Format( |
| 1376 | PyExc_TypeError, |
| 1377 | "getsockaddrarg: " |
| 1378 | "AF_INET6 address must be tuple, not %.500s", |
| 1379 | Py_TYPE(args)->tp_name); |
| 1380 | return 0; |
| 1381 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1382 | if (!PyArg_ParseTuple(args, "eti|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1383 | "idna", &host, &port, &flowinfo, |
| 1384 | &scope_id)) { |
| 1385 | return 0; |
| 1386 | } |
| 1387 | addr = (struct sockaddr_in6*)addr_ret; |
| 1388 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1389 | sizeof(*addr), AF_INET6); |
| 1390 | PyMem_Free(host); |
| 1391 | if (result < 0) |
| 1392 | return 0; |
| 1393 | if (port < 0 || port > 0xffff) { |
| 1394 | PyErr_SetString( |
| 1395 | PyExc_OverflowError, |
| 1396 | "getsockaddrarg: port must be 0-65535."); |
| 1397 | return 0; |
| 1398 | } |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 1399 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1400 | PyErr_SetString( |
| 1401 | PyExc_OverflowError, |
| 1402 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1403 | return 0; |
| 1404 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1405 | addr->sin6_family = s->sock_family; |
| 1406 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1407 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1408 | addr->sin6_scope_id = scope_id; |
| 1409 | *len_ret = sizeof *addr; |
| 1410 | return 1; |
| 1411 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1412 | #endif /* ENABLE_IPV6 */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1413 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1414 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1415 | case AF_BLUETOOTH: |
| 1416 | { |
| 1417 | switch (s->sock_proto) { |
| 1418 | case BTPROTO_L2CAP: |
| 1419 | { |
| 1420 | struct sockaddr_l2 *addr; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1421 | const char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1422 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1423 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1424 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1425 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1426 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1427 | &_BT_L2_MEMB(addr, psm))) { |
| 1428 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1429 | "wrong format"); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1433 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1434 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1435 | *len_ret = sizeof *addr; |
| 1436 | return 1; |
| 1437 | } |
| 1438 | case BTPROTO_RFCOMM: |
| 1439 | { |
| 1440 | struct sockaddr_rc *addr; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1441 | const char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1442 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1443 | addr = (struct sockaddr_rc *)addr_ret; |
| 1444 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1445 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1446 | &_BT_RC_MEMB(addr, channel))) { |
| 1447 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1448 | "wrong format"); |
| 1449 | return 0; |
| 1450 | } |
| 1451 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1452 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1454 | *len_ret = sizeof *addr; |
| 1455 | return 1; |
| 1456 | } |
| 1457 | case BTPROTO_HCI: |
| 1458 | { |
| 1459 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1460 | #if defined(__NetBSD__) || defined(__DragonFly__) |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1461 | const char *straddr; |
| 1462 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1463 | if (!PyBytes_Check(args)) { |
| 1464 | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1465 | "wrong format"); |
| 1466 | return 0; |
| 1467 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1468 | straddr = PyBytes_AS_STRING(args); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1469 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1470 | return 0; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1471 | #else /* __NetBSD__ || __DragonFly__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1472 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1473 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1474 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1475 | "wrong format"); |
| 1476 | return 0; |
| 1477 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1478 | #endif /* !(__NetBSD__ || __DragonFly__) */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1479 | *len_ret = sizeof *addr; |
| 1480 | return 1; |
| 1481 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1482 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1483 | case BTPROTO_SCO: |
| 1484 | { |
| 1485 | struct sockaddr_sco *addr; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1486 | const char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1488 | addr = (struct sockaddr_sco *)addr_ret; |
| 1489 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1490 | straddr = PyString_AsString(args); |
| 1491 | if (straddr == NULL) { |
| 1492 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1493 | "wrong format"); |
| 1494 | return 0; |
| 1495 | } |
| 1496 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1497 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1498 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1499 | *len_ret = sizeof *addr; |
| 1500 | return 1; |
| 1501 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1502 | #endif /* !__FreeBSD__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1503 | default: |
| 1504 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1508 | #endif /* USE_BLUETOOTH */ |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1510 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1511 | case AF_PACKET: |
| 1512 | { |
| 1513 | struct sockaddr_ll* addr; |
| 1514 | struct ifreq ifr; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1515 | const char *interfaceName; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1516 | int protoNumber; |
| 1517 | int hatype = 0; |
| 1518 | int pkttype = 0; |
| 1519 | char *haddr = NULL; |
| 1520 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1521 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1522 | if (!PyTuple_Check(args)) { |
| 1523 | PyErr_Format( |
| 1524 | PyExc_TypeError, |
| 1525 | "getsockaddrarg: " |
| 1526 | "AF_PACKET address must be tuple, not %.500s", |
| 1527 | Py_TYPE(args)->tp_name); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1531 | &protoNumber, &pkttype, &hatype, |
| 1532 | &haddr, &halen)) |
| 1533 | return 0; |
| 1534 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1535 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1536 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1537 | s->errorhandler(); |
| 1538 | return 0; |
| 1539 | } |
| 1540 | if (halen > 8) { |
| 1541 | PyErr_SetString(PyExc_ValueError, |
| 1542 | "Hardware address must be 8 bytes or less"); |
| 1543 | return 0; |
| 1544 | } |
| 1545 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1546 | PyErr_SetString( |
| 1547 | PyExc_OverflowError, |
| 1548 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | addr = (struct sockaddr_ll*)addr_ret; |
| 1552 | addr->sll_family = AF_PACKET; |
| 1553 | addr->sll_protocol = htons((short)protoNumber); |
| 1554 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1555 | addr->sll_pkttype = pkttype; |
| 1556 | addr->sll_hatype = hatype; |
| 1557 | if (halen != 0) { |
| 1558 | memcpy(&addr->sll_addr, haddr, halen); |
| 1559 | } |
| 1560 | addr->sll_halen = halen; |
| 1561 | *len_ret = sizeof *addr; |
| 1562 | return 1; |
| 1563 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1564 | #endif /* HAVE_NETPACKET_PACKET_H && SIOCGIFINDEX */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1565 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1566 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1567 | case AF_TIPC: |
| 1568 | { |
| 1569 | unsigned int atype, v1, v2, v3; |
| 1570 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1571 | struct sockaddr_tipc *addr; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1573 | if (!PyTuple_Check(args)) { |
| 1574 | PyErr_Format( |
| 1575 | PyExc_TypeError, |
| 1576 | "getsockaddrarg: " |
| 1577 | "AF_TIPC address must be tuple, not %.500s", |
| 1578 | Py_TYPE(args)->tp_name); |
| 1579 | return 0; |
| 1580 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1582 | if (!PyArg_ParseTuple(args, |
| 1583 | "IIII|I;Invalid TIPC address format", |
| 1584 | &atype, &v1, &v2, &v3, &scope)) |
| 1585 | return 0; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1587 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1588 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1589 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1590 | addr->family = AF_TIPC; |
| 1591 | addr->scope = scope; |
| 1592 | addr->addrtype = atype; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1594 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1595 | addr->addr.nameseq.type = v1; |
| 1596 | addr->addr.nameseq.lower = v2; |
| 1597 | addr->addr.nameseq.upper = v3; |
| 1598 | } else if (atype == TIPC_ADDR_NAME) { |
| 1599 | addr->addr.name.name.type = v1; |
| 1600 | addr->addr.name.name.instance = v2; |
| 1601 | } else if (atype == TIPC_ADDR_ID) { |
| 1602 | addr->addr.id.node = v1; |
| 1603 | addr->addr.id.ref = v2; |
| 1604 | } else { |
| 1605 | /* Shouldn't happen */ |
| 1606 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1607 | return 0; |
| 1608 | } |
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 | *len_ret = sizeof(*addr); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1611 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1612 | return 1; |
| 1613 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1614 | #endif /* HAVE_LINUX_TIPC_H */ |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1615 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1616 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1618 | default: |
| 1619 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1620 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1622 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1625 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1626 | /* 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] | 1627 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1628 | through len_ret. */ |
| 1629 | |
| 1630 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1631 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1632 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1633 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1634 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1635 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1636 | case AF_UNIX: |
| 1637 | { |
| 1638 | *len_ret = sizeof (struct sockaddr_un); |
| 1639 | return 1; |
| 1640 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1641 | #endif /* AF_UNIX */ |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 1642 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1643 | #if defined(AF_NETLINK) |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 1644 | case AF_NETLINK: |
| 1645 | { |
| 1646 | *len_ret = sizeof (struct sockaddr_nl); |
| 1647 | return 1; |
| 1648 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1649 | #endif /* AF_NETLINK */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1651 | case AF_INET: |
| 1652 | { |
| 1653 | *len_ret = sizeof (struct sockaddr_in); |
| 1654 | return 1; |
| 1655 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1656 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1657 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1658 | case AF_INET6: |
| 1659 | { |
| 1660 | *len_ret = sizeof (struct sockaddr_in6); |
| 1661 | return 1; |
| 1662 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1663 | #endif /* ENABLE_IPV6 */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1664 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1665 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1666 | case AF_BLUETOOTH: |
| 1667 | { |
| 1668 | switch(s->sock_proto) |
| 1669 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1670 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1671 | case BTPROTO_L2CAP: |
| 1672 | *len_ret = sizeof (struct sockaddr_l2); |
| 1673 | return 1; |
| 1674 | case BTPROTO_RFCOMM: |
| 1675 | *len_ret = sizeof (struct sockaddr_rc); |
| 1676 | return 1; |
| 1677 | case BTPROTO_HCI: |
| 1678 | *len_ret = sizeof (struct sockaddr_hci); |
| 1679 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1680 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1681 | case BTPROTO_SCO: |
| 1682 | *len_ret = sizeof (struct sockaddr_sco); |
| 1683 | return 1; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1684 | #endif /* !__FreeBSD__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1685 | default: |
| 1686 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1687 | "unknown BT protocol"); |
| 1688 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1690 | } |
| 1691 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1692 | #endif /* USE_BLUETOOTH */ |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1693 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1694 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1695 | case AF_PACKET: |
| 1696 | { |
| 1697 | *len_ret = sizeof (struct sockaddr_ll); |
| 1698 | return 1; |
| 1699 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1700 | #endif /* HAVE_NETPACKET_PACKET_H */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1701 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1702 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1703 | case AF_TIPC: |
| 1704 | { |
| 1705 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1706 | return 1; |
| 1707 | } |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 1708 | #endif /* HAVE_LINUX_TIPC_H */ |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1710 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1712 | default: |
| 1713 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1714 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1715 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1716 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1720 | /* s.accept() method */ |
| 1721 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1722 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1723 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1724 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1725 | sock_addr_t addrbuf; |
| 1726 | SOCKET_T newfd; |
| 1727 | socklen_t addrlen; |
| 1728 | PyObject *sock = NULL; |
| 1729 | PyObject *addr = NULL; |
| 1730 | PyObject *res = NULL; |
| 1731 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1732 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1733 | if (!getsockaddrlen(s, &addrlen)) |
| 1734 | return NULL; |
| 1735 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1737 | newfd = INVALID_SOCKET; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1738 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1739 | if (!IS_SELECTABLE(s)) |
| 1740 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1741 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1742 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1743 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1744 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1745 | if (!timeout) |
| 1746 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1747 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1748 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1749 | if (timeout == 1) { |
| 1750 | PyErr_SetString(socket_timeout, "timed out"); |
| 1751 | return NULL; |
| 1752 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 1753 | END_SELECT_LOOP(s) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1755 | if (newfd == INVALID_SOCKET) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1756 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1758 | /* Create the new object with unspecified family, |
| 1759 | to avoid calls to bind() etc. on it. */ |
| 1760 | sock = (PyObject *) new_sockobject(newfd, |
| 1761 | s->sock_family, |
| 1762 | s->sock_type, |
| 1763 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1764 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1765 | if (sock == NULL) { |
| 1766 | SOCKETCLOSE(newfd); |
| 1767 | goto finally; |
| 1768 | } |
| 1769 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1770 | addrlen, s->sock_proto); |
| 1771 | if (addr == NULL) |
| 1772 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1774 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1775 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1776 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1777 | Py_XDECREF(sock); |
| 1778 | Py_XDECREF(addr); |
| 1779 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1782 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1783 | "accept() -> (socket object, address info)\n\ |
| 1784 | \n\ |
| 1785 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1786 | 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] | 1787 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1788 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1789 | /* s.setblocking(flag) method. Argument: |
| 1790 | False -- non-blocking mode; same as settimeout(0) |
| 1791 | True -- blocking mode; same as settimeout(None) |
| 1792 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1793 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1794 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1795 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1796 | { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 1797 | long block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1798 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1799 | block = PyInt_AsLong(arg); |
| 1800 | if (block == -1 && PyErr_Occurred()) |
| 1801 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1803 | s->sock_timeout = block ? -1.0 : 0.0; |
| 1804 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1805 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1806 | Py_INCREF(Py_None); |
| 1807 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1808 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1810 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1811 | "setblocking(flag)\n\ |
| 1812 | \n\ |
| 1813 | 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] | 1814 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1815 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1816 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1817 | /* s.settimeout(timeout) method. Argument: |
| 1818 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1819 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1820 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1821 | < 0 -- illegal; raises an exception |
| 1822 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1823 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1824 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1825 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1826 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1827 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1828 | if (arg == Py_None) |
| 1829 | timeout = -1.0; |
| 1830 | else { |
| 1831 | timeout = PyFloat_AsDouble(arg); |
| 1832 | if (timeout < 0.0) { |
| 1833 | if (!PyErr_Occurred()) |
| 1834 | PyErr_SetString(PyExc_ValueError, |
| 1835 | "Timeout value out of range"); |
| 1836 | return NULL; |
| 1837 | } |
| 1838 | } |
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 | s->sock_timeout = timeout; |
| 1841 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1842 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1843 | Py_INCREF(Py_None); |
| 1844 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1847 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1848 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1849 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1850 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1851 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1852 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1853 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1854 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1855 | /* s.gettimeout() method. |
| 1856 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1857 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1858 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1859 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1860 | if (s->sock_timeout < 0.0) { |
| 1861 | Py_INCREF(Py_None); |
| 1862 | return Py_None; |
| 1863 | } |
| 1864 | else |
| 1865 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1868 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1869 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1870 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 1871 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1872 | 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] | 1873 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1874 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1875 | #ifdef RISCOS |
| 1876 | /* s.sleeptaskw(1 | 0) method */ |
| 1877 | |
| 1878 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1879 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1880 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1881 | int block; |
| 1882 | block = PyInt_AsLong(arg); |
| 1883 | if (block == -1 && PyErr_Occurred()) |
| 1884 | return NULL; |
| 1885 | Py_BEGIN_ALLOW_THREADS |
| 1886 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1887 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1889 | Py_INCREF(Py_None); |
| 1890 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1891 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1892 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1893 | "sleeptaskw(flag)\n\ |
| 1894 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1895 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1896 | #endif |
| 1897 | |
| 1898 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1899 | /* s.setsockopt() method. |
| 1900 | With an integer third argument, sets an integer option. |
| 1901 | With a string third argument, sets an option from a buffer; |
| 1902 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1903 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1904 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1905 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1906 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1907 | int level; |
| 1908 | int optname; |
| 1909 | int res; |
| 1910 | char *buf; |
| 1911 | int buflen; |
| 1912 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1913 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1914 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1915 | &level, &optname, &flag)) { |
| 1916 | buf = (char *) &flag; |
| 1917 | buflen = sizeof flag; |
| 1918 | } |
| 1919 | else { |
| 1920 | PyErr_Clear(); |
| 1921 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1922 | &level, &optname, &buf, &buflen)) |
| 1923 | return NULL; |
| 1924 | } |
| 1925 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1926 | if (res < 0) |
| 1927 | return s->errorhandler(); |
| 1928 | Py_INCREF(Py_None); |
| 1929 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1932 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1933 | "setsockopt(level, option, value)\n\ |
| 1934 | \n\ |
| 1935 | 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] | 1936 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1937 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1938 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1939 | /* s.getsockopt() method. |
| 1940 | With two arguments, retrieves an integer option. |
| 1941 | With a third integer argument, retrieves a string buffer of that size; |
| 1942 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1943 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1944 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1945 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1946 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1947 | int level; |
| 1948 | int optname; |
| 1949 | int res; |
| 1950 | PyObject *buf; |
| 1951 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1952 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1953 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1954 | /* We have incomplete socket support. */ |
| 1955 | PyErr_SetString(socket_error, "getsockopt not supported"); |
| 1956 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1957 | #else |
| 1958 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1959 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1960 | &level, &optname, &buflen)) |
| 1961 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1962 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1963 | if (buflen == 0) { |
| 1964 | int flag = 0; |
| 1965 | socklen_t flagsize = sizeof flag; |
| 1966 | res = getsockopt(s->sock_fd, level, optname, |
| 1967 | (void *)&flag, &flagsize); |
| 1968 | if (res < 0) |
| 1969 | return s->errorhandler(); |
| 1970 | return PyInt_FromLong(flag); |
| 1971 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1972 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1973 | /* socklen_t is unsigned so no negative test is needed, |
| 1974 | test buflen == 0 is previously done */ |
| 1975 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1976 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1977 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1978 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1979 | PyErr_SetString(socket_error, |
| 1980 | "getsockopt buflen out of range"); |
| 1981 | return NULL; |
| 1982 | } |
| 1983 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
| 1984 | if (buf == NULL) |
| 1985 | return NULL; |
| 1986 | res = getsockopt(s->sock_fd, level, optname, |
| 1987 | (void *)PyString_AS_STRING(buf), &buflen); |
| 1988 | if (res < 0) { |
| 1989 | Py_DECREF(buf); |
| 1990 | return s->errorhandler(); |
| 1991 | } |
| 1992 | _PyString_Resize(&buf, buflen); |
| 1993 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1994 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1997 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1998 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1999 | \n\ |
| 2000 | Get a socket option. See the Unix manual for level and option.\n\ |
| 2001 | 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] | 2002 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2003 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 2004 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2005 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2006 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2007 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2008 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2009 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2010 | sock_addr_t addrbuf; |
| 2011 | int addrlen; |
| 2012 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2013 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2014 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2015 | return NULL; |
| 2016 | Py_BEGIN_ALLOW_THREADS |
| 2017 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 2018 | Py_END_ALLOW_THREADS |
| 2019 | if (res < 0) |
| 2020 | return s->errorhandler(); |
| 2021 | Py_INCREF(Py_None); |
| 2022 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2025 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2026 | "bind(address)\n\ |
| 2027 | \n\ |
| 2028 | 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] | 2029 | 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] | 2030 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2031 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2032 | |
| 2033 | /* s.close() method. |
| 2034 | Set the file descriptor to -1 so operations tried subsequently |
| 2035 | will surely fail. */ |
| 2036 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2037 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2038 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2039 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2040 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2041 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2042 | if ((fd = s->sock_fd) != -1) { |
| 2043 | s->sock_fd = -1; |
| 2044 | Py_BEGIN_ALLOW_THREADS |
| 2045 | (void) SOCKETCLOSE(fd); |
| 2046 | Py_END_ALLOW_THREADS |
| 2047 | } |
| 2048 | Py_INCREF(Py_None); |
| 2049 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2052 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2053 | "close()\n\ |
| 2054 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2055 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2057 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2058 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2059 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2060 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2061 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2062 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2063 | timeout = 0; |
| 2064 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2065 | |
| 2066 | #ifdef MS_WINDOWS |
| 2067 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2068 | if (s->sock_timeout > 0.0) { |
| 2069 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 2070 | IS_SELECTABLE(s)) { |
| 2071 | /* This is a mess. Best solution: trust select */ |
| 2072 | fd_set fds; |
| 2073 | fd_set fds_exc; |
| 2074 | struct timeval tv; |
| 2075 | tv.tv_sec = (int)s->sock_timeout; |
| 2076 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 2077 | FD_ZERO(&fds); |
| 2078 | FD_SET(s->sock_fd, &fds); |
| 2079 | FD_ZERO(&fds_exc); |
| 2080 | FD_SET(s->sock_fd, &fds_exc); |
| 2081 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
| 2082 | if (res == 0) { |
| 2083 | res = WSAEWOULDBLOCK; |
| 2084 | timeout = 1; |
| 2085 | } else if (res > 0) { |
| 2086 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2087 | /* The socket is in the writeable set - this |
| 2088 | means connected */ |
| 2089 | res = 0; |
| 2090 | else { |
| 2091 | /* As per MS docs, we need to call getsockopt() |
| 2092 | to get the underlying error */ |
| 2093 | int res_size = sizeof res; |
| 2094 | /* It must be in the exception set */ |
| 2095 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2096 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2097 | (char *)&res, &res_size)) |
| 2098 | /* getsockopt also clears WSAGetLastError, |
| 2099 | so reset it back. */ |
| 2100 | WSASetLastError(res); |
| 2101 | else |
| 2102 | res = WSAGetLastError(); |
| 2103 | } |
| 2104 | } |
| 2105 | /* else if (res < 0) an error occurred */ |
| 2106 | } |
| 2107 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2109 | if (res < 0) |
| 2110 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2111 | |
| 2112 | #else |
| 2113 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2114 | if (s->sock_timeout > 0.0) { |
| 2115 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2116 | timeout = internal_select(s, 1); |
| 2117 | if (timeout == 0) { |
| 2118 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2119 | use getsockopt(SO_ERROR) to get the real |
| 2120 | error. */ |
| 2121 | socklen_t res_size = sizeof res; |
| 2122 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2123 | SO_ERROR, &res, &res_size); |
| 2124 | if (res == EISCONN) |
| 2125 | res = 0; |
| 2126 | errno = res; |
| 2127 | } |
| 2128 | else if (timeout == -1) { |
| 2129 | res = errno; /* had error */ |
| 2130 | } |
| 2131 | else |
| 2132 | res = EWOULDBLOCK; /* timed out */ |
| 2133 | } |
| 2134 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2136 | if (res < 0) |
| 2137 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2138 | |
| 2139 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2140 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2141 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2142 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2143 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2144 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2145 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2146 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2147 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2148 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2149 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2150 | sock_addr_t addrbuf; |
| 2151 | int addrlen; |
| 2152 | int res; |
| 2153 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2154 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2155 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2156 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2157 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2158 | Py_BEGIN_ALLOW_THREADS |
| 2159 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2160 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2161 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2162 | if (timeout == 1) { |
| 2163 | PyErr_SetString(socket_timeout, "timed out"); |
| 2164 | return NULL; |
| 2165 | } |
| 2166 | if (res != 0) |
| 2167 | return s->errorhandler(); |
| 2168 | Py_INCREF(Py_None); |
| 2169 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2172 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2173 | "connect(address)\n\ |
| 2174 | \n\ |
| 2175 | 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] | 2176 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2177 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2178 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2179 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2180 | |
| 2181 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2182 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2183 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2184 | sock_addr_t addrbuf; |
| 2185 | int addrlen; |
| 2186 | int res; |
| 2187 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2188 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2189 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2190 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2192 | Py_BEGIN_ALLOW_THREADS |
| 2193 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2194 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2196 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2197 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2198 | #ifdef EINTR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2199 | if (res == EINTR && PyErr_CheckSignals()) |
| 2200 | return NULL; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2201 | #endif |
| 2202 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2203 | return PyInt_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2206 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2207 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2208 | \n\ |
| 2209 | 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] | 2210 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2211 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2212 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2213 | /* s.fileno() method */ |
| 2214 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2215 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2216 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2217 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2218 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2219 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2220 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2221 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2222 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2225 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2226 | "fileno() -> integer\n\ |
| 2227 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2228 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2229 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2230 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2231 | #ifndef NO_DUP |
| 2232 | /* s.dup() method */ |
| 2233 | |
| 2234 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2235 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2236 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2237 | SOCKET_T newfd; |
| 2238 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2239 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2240 | newfd = dup(s->sock_fd); |
| 2241 | if (newfd < 0) |
| 2242 | return s->errorhandler(); |
| 2243 | sock = (PyObject *) new_sockobject(newfd, |
| 2244 | s->sock_family, |
| 2245 | s->sock_type, |
| 2246 | s->sock_proto); |
| 2247 | if (sock == NULL) |
| 2248 | SOCKETCLOSE(newfd); |
| 2249 | return sock; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2250 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2252 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2253 | "dup() -> socket object\n\ |
| 2254 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2255 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2256 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2257 | #endif |
| 2258 | |
| 2259 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2260 | /* s.getsockname() method */ |
| 2261 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2262 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2263 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2264 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2265 | sock_addr_t addrbuf; |
| 2266 | int res; |
| 2267 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2268 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2269 | if (!getsockaddrlen(s, &addrlen)) |
| 2270 | return NULL; |
| 2271 | memset(&addrbuf, 0, addrlen); |
| 2272 | Py_BEGIN_ALLOW_THREADS |
| 2273 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2274 | Py_END_ALLOW_THREADS |
| 2275 | if (res < 0) |
| 2276 | return s->errorhandler(); |
| 2277 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2278 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2281 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2282 | "getsockname() -> address info\n\ |
| 2283 | \n\ |
| 2284 | 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] | 2285 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2286 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2287 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2288 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2289 | /* s.getpeername() method */ |
| 2290 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2291 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2292 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2293 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2294 | sock_addr_t addrbuf; |
| 2295 | int res; |
| 2296 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2298 | if (!getsockaddrlen(s, &addrlen)) |
| 2299 | return NULL; |
| 2300 | memset(&addrbuf, 0, addrlen); |
| 2301 | Py_BEGIN_ALLOW_THREADS |
| 2302 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2303 | Py_END_ALLOW_THREADS |
| 2304 | if (res < 0) |
| 2305 | return s->errorhandler(); |
| 2306 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2307 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2308 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2310 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2311 | "getpeername() -> address info\n\ |
| 2312 | \n\ |
| 2313 | 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] | 2314 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2315 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2316 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2317 | |
| 2318 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2319 | /* s.listen(n) method */ |
| 2320 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2321 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2322 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2323 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2324 | int backlog; |
| 2325 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2326 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2327 | backlog = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2328 | if (backlog == -1 && PyErr_Occurred()) |
| 2329 | return NULL; |
| 2330 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2331 | /* To avoid problems on systems that don't allow a negative backlog |
| 2332 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2333 | if (backlog < 0) |
| 2334 | backlog = 0; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2335 | res = listen(s->sock_fd, backlog); |
| 2336 | Py_END_ALLOW_THREADS |
| 2337 | if (res < 0) |
| 2338 | return s->errorhandler(); |
| 2339 | Py_INCREF(Py_None); |
| 2340 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2343 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2344 | "listen(backlog)\n\ |
| 2345 | \n\ |
| 2346 | 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] | 2347 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
| 2348 | unaccepted connections that the system will allow before refusing new\n\ |
| 2349 | connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2350 | |
| 2351 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2352 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2353 | /* s.makefile(mode) method. |
| 2354 | Create a new open file object referring to a dupped version of |
| 2355 | the socket's file descriptor. (The dup() call is necessary so |
| 2356 | that the open file and socket objects may be closed independent |
| 2357 | of each other.) |
| 2358 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 2359 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2360 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2361 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2362 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2363 | extern int fclose(FILE *); |
| 2364 | char *mode = "r"; |
| 2365 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2366 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2367 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2368 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2369 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2370 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2371 | FILE *fp; |
| 2372 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2373 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2374 | char *mode_r = "r"; |
| 2375 | char *mode_w = "w"; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2376 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2378 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
| 2379 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2380 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2381 | if (strcmp(mode,"rb") == 0) { |
| 2382 | mode = mode_r; |
| 2383 | } |
| 2384 | else { |
| 2385 | if (strcmp(mode,"wb") == 0) { |
| 2386 | mode = mode_w; |
| 2387 | } |
| 2388 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2389 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2390 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2391 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 2392 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2393 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2394 | 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] | 2395 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2396 | { |
| 2397 | if (fd >= 0) |
| 2398 | SOCKETCLOSE(fd); |
| 2399 | return s->errorhandler(); |
| 2400 | } |
| 2401 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 2402 | if (f != NULL) |
| 2403 | PyFile_SetBufSize(f, bufsize); |
| 2404 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2405 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2407 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2408 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 2409 | \n\ |
| 2410 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2411 | 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] | 2412 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2413 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2414 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2415 | /* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2416 | * 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] | 2417 | * 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] | 2418 | * 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] | 2419 | * 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] | 2420 | * also possible that we return a number of bytes smaller than the request |
| 2421 | * bytes. |
| 2422 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2423 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2424 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2425 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2426 | ssize_t outlen = -1; |
| 2427 | int timeout; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2428 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2429 | int remaining; |
| 2430 | char *read_buf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2431 | #endif |
| 2432 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2433 | if (!IS_SELECTABLE(s)) { |
| 2434 | select_error(); |
| 2435 | return -1; |
| 2436 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2437 | |
| 2438 | #ifndef __VMS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2439 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2440 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2441 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2442 | if (!timeout) |
| 2443 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2444 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2445 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2446 | if (timeout == 1) { |
| 2447 | PyErr_SetString(socket_timeout, "timed out"); |
| 2448 | return -1; |
| 2449 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2450 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2451 | if (outlen < 0) { |
| 2452 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2453 | NULL, so ignore its return value */ |
| 2454 | s->errorhandler(); |
| 2455 | return -1; |
| 2456 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2457 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2458 | read_buf = cbuf; |
| 2459 | remaining = len; |
| 2460 | while (remaining != 0) { |
| 2461 | unsigned int segment; |
| 2462 | int nread = -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2463 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2464 | segment = remaining /SEGMENT_SIZE; |
| 2465 | if (segment != 0) { |
| 2466 | segment = SEGMENT_SIZE; |
| 2467 | } |
| 2468 | else { |
| 2469 | segment = remaining; |
| 2470 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2471 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2472 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2473 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2474 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2475 | if (!timeout) |
| 2476 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2477 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2478 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2479 | if (timeout == 1) { |
| 2480 | PyErr_SetString(socket_timeout, "timed out"); |
| 2481 | return -1; |
| 2482 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2483 | END_SELECT_LOOP(s) |
| 2484 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2485 | if (nread < 0) { |
| 2486 | s->errorhandler(); |
| 2487 | return -1; |
| 2488 | } |
| 2489 | if (nread != remaining) { |
| 2490 | read_buf += nread; |
| 2491 | break; |
| 2492 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2493 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2494 | remaining -= segment; |
| 2495 | read_buf += segment; |
| 2496 | } |
| 2497 | outlen = read_buf - cbuf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2498 | #endif /* !__VMS */ |
| 2499 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2500 | return outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2503 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2504 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2505 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2506 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2507 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2508 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2509 | int recvlen, flags = 0; |
| 2510 | ssize_t outlen; |
| 2511 | PyObject *buf; |
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 (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
| 2514 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2515 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2516 | if (recvlen < 0) { |
| 2517 | PyErr_SetString(PyExc_ValueError, |
| 2518 | "negative buffersize in recv"); |
| 2519 | return NULL; |
| 2520 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2522 | /* Allocate a new string. */ |
| 2523 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2524 | if (buf == NULL) |
| 2525 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2526 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2527 | /* Call the guts */ |
| 2528 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags); |
| 2529 | if (outlen < 0) { |
| 2530 | /* An error occurred, release the string and return an |
| 2531 | error. */ |
| 2532 | Py_DECREF(buf); |
| 2533 | return NULL; |
| 2534 | } |
| 2535 | if (outlen != recvlen) { |
| 2536 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2537 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2538 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2539 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2540 | return NULL; |
| 2541 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2542 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2543 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2546 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2547 | "recv(buffersize[, flags]) -> data\n\ |
| 2548 | \n\ |
| 2549 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2550 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2551 | 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] | 2552 | 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] | 2553 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2554 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2555 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2556 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2557 | static PyObject* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2558 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2559 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2560 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2561 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2562 | int recvlen = 0, flags = 0; |
| 2563 | ssize_t readlen; |
| 2564 | Py_buffer buf; |
| 2565 | Py_ssize_t buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2567 | /* Get the buffer's memory */ |
| 2568 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist, |
| 2569 | &buf, &recvlen, &flags)) |
| 2570 | return NULL; |
| 2571 | buflen = buf.len; |
| 2572 | assert(buf.buf != 0 && buflen > 0); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2573 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2574 | if (recvlen < 0) { |
| 2575 | PyErr_SetString(PyExc_ValueError, |
| 2576 | "negative buffersize in recv_into"); |
| 2577 | goto error; |
| 2578 | } |
| 2579 | if (recvlen == 0) { |
| 2580 | /* If nbytes was not specified, use the buffer's length */ |
| 2581 | recvlen = buflen; |
| 2582 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2583 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2584 | /* Check if the buffer is large enough */ |
| 2585 | if (buflen < recvlen) { |
| 2586 | PyErr_SetString(PyExc_ValueError, |
| 2587 | "buffer too small for requested bytes"); |
| 2588 | goto error; |
| 2589 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2590 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2591 | /* Call the guts */ |
| 2592 | readlen = sock_recv_guts(s, buf.buf, recvlen, flags); |
| 2593 | if (readlen < 0) { |
| 2594 | /* Return an error. */ |
| 2595 | goto error; |
| 2596 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2598 | PyBuffer_Release(&buf); |
| 2599 | /* Return the number of bytes read. Note that we do not do anything |
| 2600 | special here in the case that readlen < recvlen. */ |
| 2601 | return PyInt_FromSsize_t(readlen); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2602 | |
| 2603 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2604 | PyBuffer_Release(&buf); |
| 2605 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2608 | PyDoc_STRVAR(recv_into_doc, |
| 2609 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2610 | \n\ |
| 2611 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2612 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2613 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2614 | \n\ |
| 2615 | See recv() for documentation about the flags."); |
| 2616 | |
| 2617 | |
| 2618 | /* |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2619 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2620 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2621 | * 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] | 2622 | * 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] | 2623 | * that it is also possible that we return a number of bytes smaller than the |
| 2624 | * request bytes. |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2625 | * |
| 2626 | * 'addr' is a return value for the address object. Note that you must decref |
| 2627 | * it yourself. |
| 2628 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2629 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2630 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2631 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2632 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2633 | sock_addr_t addrbuf; |
| 2634 | int timeout; |
| 2635 | ssize_t n = -1; |
| 2636 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2637 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2638 | *addr = NULL; |
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 (!getsockaddrlen(s, &addrlen)) |
| 2641 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2642 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2643 | if (!IS_SELECTABLE(s)) { |
| 2644 | select_error(); |
| 2645 | return -1; |
| 2646 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2647 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2648 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2649 | Py_BEGIN_ALLOW_THREADS |
| 2650 | memset(&addrbuf, 0, addrlen); |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2651 | timeout = internal_select_ex(s, 0, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2652 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2653 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2654 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2655 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2656 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2657 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2658 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2659 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2660 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2661 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2662 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2663 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2664 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2665 | } |
| 2666 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2667 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2668 | if (timeout == 1) { |
| 2669 | PyErr_SetString(socket_timeout, "timed out"); |
| 2670 | return -1; |
| 2671 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2672 | END_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2673 | if (n < 0) { |
| 2674 | s->errorhandler(); |
| 2675 | return -1; |
| 2676 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2678 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2679 | addrlen, s->sock_proto))) |
| 2680 | return -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2681 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2682 | return n; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2686 | |
| 2687 | static PyObject * |
| 2688 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2689 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2690 | PyObject *buf = NULL; |
| 2691 | PyObject *addr = NULL; |
| 2692 | PyObject *ret = NULL; |
| 2693 | int recvlen, flags = 0; |
| 2694 | ssize_t outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2695 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2696 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
| 2697 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2699 | if (recvlen < 0) { |
| 2700 | PyErr_SetString(PyExc_ValueError, |
| 2701 | "negative buffersize in recvfrom"); |
| 2702 | return NULL; |
| 2703 | } |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2705 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2706 | if (buf == NULL) |
| 2707 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2708 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2709 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf), |
| 2710 | recvlen, flags, &addr); |
| 2711 | if (outlen < 0) { |
| 2712 | goto finally; |
| 2713 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2714 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2715 | if (outlen != recvlen) { |
| 2716 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2717 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2718 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2719 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2720 | goto finally; |
| 2721 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2722 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2723 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2724 | |
| 2725 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2726 | Py_XDECREF(buf); |
| 2727 | Py_XDECREF(addr); |
| 2728 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2731 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2732 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2733 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2734 | 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] | 2735 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2736 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2737 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2738 | |
| 2739 | static PyObject * |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2740 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2741 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2742 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2743 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2744 | int recvlen = 0, flags = 0; |
| 2745 | ssize_t readlen; |
| 2746 | Py_buffer buf; |
| 2747 | int buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2748 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2749 | PyObject *addr = NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2751 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into", |
| 2752 | kwlist, &buf, |
| 2753 | &recvlen, &flags)) |
| 2754 | return NULL; |
| 2755 | buflen = buf.len; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2756 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2757 | if (recvlen < 0) { |
| 2758 | PyErr_SetString(PyExc_ValueError, |
| 2759 | "negative buffersize in recvfrom_into"); |
| 2760 | goto error; |
| 2761 | } |
| 2762 | if (recvlen == 0) { |
| 2763 | /* If nbytes was not specified, use the buffer's length */ |
| 2764 | recvlen = buflen; |
Benjamin Peterson | 28cf368 | 2014-01-13 22:59:38 -0500 | [diff] [blame] | 2765 | } else if (recvlen > buflen) { |
| 2766 | PyErr_SetString(PyExc_ValueError, |
| 2767 | "nbytes is greater than the length of the buffer"); |
| 2768 | goto error; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2769 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2771 | readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr); |
| 2772 | if (readlen < 0) { |
| 2773 | /* Return an error */ |
| 2774 | goto error; |
| 2775 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2776 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2777 | PyBuffer_Release(&buf); |
| 2778 | /* Return the number of bytes read and the address. Note that we do |
| 2779 | not do anything special here in the case that readlen < recvlen. */ |
| 2780 | return Py_BuildValue("lN", readlen, addr); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2781 | |
| 2782 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2783 | Py_XDECREF(addr); |
| 2784 | PyBuffer_Release(&buf); |
| 2785 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2788 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2789 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2790 | \n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2791 | 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] | 2792 | |
| 2793 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2794 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2795 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2796 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2797 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2798 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2799 | char *buf; |
| 2800 | int len, n = -1, flags = 0, timeout; |
| 2801 | Py_buffer pbuf; |
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 (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags)) |
| 2804 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2805 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2806 | if (!IS_SELECTABLE(s)) { |
| 2807 | PyBuffer_Release(&pbuf); |
| 2808 | return select_error(); |
| 2809 | } |
| 2810 | buf = pbuf.buf; |
| 2811 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2812 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2813 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2814 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2815 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2816 | if (!timeout) |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2817 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2818 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2819 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2820 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2821 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2822 | Py_END_ALLOW_THREADS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2823 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2824 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2825 | PyErr_SetString(socket_timeout, "timed out"); |
| 2826 | return NULL; |
| 2827 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2828 | END_SELECT_LOOP(s) |
| 2829 | |
| 2830 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2831 | if (n < 0) |
| 2832 | return s->errorhandler(); |
| 2833 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2836 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2837 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2838 | \n\ |
| 2839 | 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] | 2840 | 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] | 2841 | 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] | 2842 | |
| 2843 | |
| 2844 | /* s.sendall(data [,flags]) method */ |
| 2845 | |
| 2846 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2847 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2848 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2849 | char *buf; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2850 | int len, n = -1, flags = 0, timeout, saved_errno; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2851 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2853 | if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags)) |
| 2854 | return NULL; |
| 2855 | buf = pbuf.buf; |
| 2856 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2857 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2858 | if (!IS_SELECTABLE(s)) { |
| 2859 | PyBuffer_Release(&pbuf); |
| 2860 | return select_error(); |
| 2861 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2862 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2863 | do { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2864 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2865 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2866 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2867 | n = -1; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2868 | if (!timeout) { |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2869 | #ifdef __VMS |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2870 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2871 | #else |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2872 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2873 | #endif |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2874 | } |
| 2875 | Py_END_ALLOW_THREADS |
| 2876 | if (timeout == 1) { |
| 2877 | PyBuffer_Release(&pbuf); |
| 2878 | PyErr_SetString(socket_timeout, "timed out"); |
| 2879 | return NULL; |
| 2880 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2881 | END_SELECT_LOOP(s) |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2882 | /* PyErr_CheckSignals() might change errno */ |
| 2883 | saved_errno = errno; |
| 2884 | /* We must run our signal handlers before looping again. |
| 2885 | send() can return a successful partial write when it is |
| 2886 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 2887 | if (PyErr_CheckSignals()) { |
| 2888 | PyBuffer_Release(&pbuf); |
| 2889 | return NULL; |
| 2890 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2891 | if (n < 0) { |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2892 | /* If interrupted, try again */ |
| 2893 | if (saved_errno == EINTR) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2894 | continue; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2895 | else |
| 2896 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2897 | } |
| 2898 | buf += n; |
| 2899 | len -= n; |
| 2900 | } while (len > 0); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2901 | PyBuffer_Release(&pbuf); |
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 | if (n < 0) |
| 2904 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2906 | Py_INCREF(Py_None); |
| 2907 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2910 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2911 | "sendall(data[, flags])\n\ |
| 2912 | \n\ |
| 2913 | Send a data string to the socket. For the optional flags\n\ |
| 2914 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2915 | 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] | 2916 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2917 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2918 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2919 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2920 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2921 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2922 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2923 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2924 | Py_buffer pbuf; |
| 2925 | PyObject *addro; |
| 2926 | char *buf; |
| 2927 | Py_ssize_t len; |
| 2928 | sock_addr_t addrbuf; |
Victor Stinner | 31c7e4f | 2015-04-02 17:19:17 +0200 | [diff] [blame] | 2929 | int addrlen, flags, timeout; |
| 2930 | long n = -1; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2931 | int arglen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2933 | flags = 0; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2934 | arglen = PyTuple_Size(args); |
| 2935 | switch(arglen) { |
| 2936 | case 2: |
| 2937 | PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro); |
| 2938 | break; |
| 2939 | case 3: |
| 2940 | PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro); |
| 2941 | break; |
| 2942 | default: |
| 2943 | PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3" |
| 2944 | " arguments (%d given)", arglen); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2945 | } |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2946 | if (PyErr_Occurred()) |
| 2947 | return NULL; |
| 2948 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2949 | buf = pbuf.buf; |
| 2950 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2951 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2952 | if (!IS_SELECTABLE(s)) { |
| 2953 | PyBuffer_Release(&pbuf); |
| 2954 | return select_error(); |
| 2955 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2956 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2957 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2958 | PyBuffer_Release(&pbuf); |
| 2959 | return NULL; |
| 2960 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2961 | |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2962 | BEGIN_SELECT_LOOP(s) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2963 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2964 | timeout = internal_select_ex(s, 1, interval); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2965 | if (!timeout) |
| 2966 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2967 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2968 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2969 | if (timeout == 1) { |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2970 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2971 | PyErr_SetString(socket_timeout, "timed out"); |
| 2972 | return NULL; |
| 2973 | } |
Kristján Valur Jónsson | 6ebc8f3 | 2013-03-19 10:58:59 -0700 | [diff] [blame] | 2974 | END_SELECT_LOOP(s) |
| 2975 | PyBuffer_Release(&pbuf); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2976 | if (n < 0) |
| 2977 | return s->errorhandler(); |
| 2978 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2981 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2982 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2983 | \n\ |
| 2984 | 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] | 2985 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2986 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2987 | |
| 2988 | /* s.shutdown(how) method */ |
| 2989 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2990 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2991 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2992 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2993 | int how; |
| 2994 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2995 | |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2996 | how = _PyInt_AsInt(arg); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2997 | if (how == -1 && PyErr_Occurred()) |
| 2998 | return NULL; |
| 2999 | Py_BEGIN_ALLOW_THREADS |
| 3000 | res = shutdown(s->sock_fd, how); |
| 3001 | Py_END_ALLOW_THREADS |
| 3002 | if (res < 0) |
| 3003 | return s->errorhandler(); |
| 3004 | Py_INCREF(Py_None); |
| 3005 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3008 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3009 | "shutdown(flag)\n\ |
| 3010 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 3011 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 3012 | 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] | 3013 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 3014 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3015 | static PyObject* |
| 3016 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 3017 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3018 | unsigned long cmd = SIO_RCVALL; |
| 3019 | PyObject *argO; |
| 3020 | DWORD recv; |
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 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 3023 | return NULL; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3024 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3025 | switch (cmd) { |
| 3026 | case SIO_RCVALL: { |
| 3027 | unsigned int option = RCVALL_ON; |
| 3028 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 3029 | return NULL; |
| 3030 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 3031 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3032 | return set_error(); |
| 3033 | } |
| 3034 | return PyLong_FromUnsignedLong(recv); } |
| 3035 | case SIO_KEEPALIVE_VALS: { |
| 3036 | struct tcp_keepalive ka; |
| 3037 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 3038 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 3039 | return NULL; |
| 3040 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 3041 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 3042 | return set_error(); |
| 3043 | } |
| 3044 | return PyLong_FromUnsignedLong(recv); } |
| 3045 | default: |
| 3046 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 3047 | return NULL; |
| 3048 | } |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3049 | } |
| 3050 | PyDoc_STRVAR(sock_ioctl_doc, |
| 3051 | "ioctl(cmd, option) -> long\n\ |
| 3052 | \n\ |
Kristján Valur Jónsson | 2fcd03b | 2009-09-25 15:19:51 +0000 | [diff] [blame] | 3053 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 3054 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 3055 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."); |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3056 | |
| 3057 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3058 | |
| 3059 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3060 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3061 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3062 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 3063 | accept_doc}, |
| 3064 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 3065 | bind_doc}, |
| 3066 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 3067 | close_doc}, |
| 3068 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 3069 | connect_doc}, |
| 3070 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 3071 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3072 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3073 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
| 3074 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3075 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3076 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 3077 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3078 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3079 | {"getpeername", (PyCFunction)sock_getpeername, |
| 3080 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 3081 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3082 | {"getsockname", (PyCFunction)sock_getsockname, |
| 3083 | METH_NOARGS, getsockname_doc}, |
| 3084 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 3085 | getsockopt_doc}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 3086 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3087 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 3088 | sock_ioctl_doc}, |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 3089 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3090 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 3091 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3092 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3093 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
| 3094 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3095 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3096 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 3097 | recv_doc}, |
| 3098 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3099 | recv_into_doc}, |
| 3100 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3101 | recvfrom_doc}, |
| 3102 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3103 | recvfrom_into_doc}, |
| 3104 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3105 | send_doc}, |
| 3106 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3107 | sendall_doc}, |
| 3108 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3109 | sendto_doc}, |
| 3110 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3111 | setblocking_doc}, |
| 3112 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3113 | settimeout_doc}, |
| 3114 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3115 | gettimeout_doc}, |
| 3116 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3117 | setsockopt_doc}, |
| 3118 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3119 | shutdown_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3120 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3121 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
| 3122 | sleeptaskw_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3123 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3124 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3125 | }; |
| 3126 | |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 3127 | /* SockObject members */ |
| 3128 | static PyMemberDef sock_memberlist[] = { |
| 3129 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3130 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3131 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3132 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3133 | {0}, |
| 3134 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3135 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3136 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3137 | First close the file description. */ |
| 3138 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3139 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3140 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3141 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3142 | if (s->sock_fd != -1) |
| 3143 | (void) SOCKETCLOSE(s->sock_fd); |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3144 | if (s->weakreflist != NULL) |
| 3145 | PyObject_ClearWeakRefs((PyObject *)s); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3146 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3149 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3150 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3151 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3152 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3153 | char buf[512]; |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3154 | long sock_fd; |
| 3155 | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
| 3156 | if (s->sock_fd == INVALID_SOCKET) { |
| 3157 | sock_fd = -1; |
| 3158 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3159 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3160 | else if (s->sock_fd > LONG_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3161 | /* this can occur on Win64, and actually there is a special |
| 3162 | ugly printf formatter for decimal pointer length integer |
| 3163 | printing, only bother if necessary*/ |
| 3164 | PyErr_SetString(PyExc_OverflowError, |
| 3165 | "no printf formatter to display " |
| 3166 | "the socket descriptor in decimal"); |
| 3167 | return NULL; |
| 3168 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3169 | #endif |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3170 | else |
| 3171 | sock_fd = (long)s->sock_fd; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3172 | PyOS_snprintf( |
| 3173 | buf, sizeof(buf), |
| 3174 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
Victor Stinner | 0a649c7 | 2014-07-26 14:52:55 +0200 | [diff] [blame] | 3175 | sock_fd, s->sock_family, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3176 | s->sock_type, |
| 3177 | s->sock_proto); |
| 3178 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3182 | /* Create a new, uninitialized socket object. */ |
| 3183 | |
| 3184 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3185 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3186 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3187 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3188 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3189 | new = type->tp_alloc(type, 0); |
| 3190 | if (new != NULL) { |
| 3191 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3192 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3193 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3194 | ((PySocketSockObject *)new)->weakreflist = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3195 | } |
| 3196 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | |
| 3200 | /* Initialize a new socket object. */ |
| 3201 | |
| 3202 | /*ARGSUSED*/ |
| 3203 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3204 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3205 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3206 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3207 | SOCKET_T fd; |
| 3208 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3209 | static char *keywords[] = {"family", "type", "proto", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3211 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 3212 | "|iii:socket", keywords, |
| 3213 | &family, &type, &proto)) |
| 3214 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3215 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3216 | Py_BEGIN_ALLOW_THREADS |
| 3217 | fd = socket(family, type, proto); |
| 3218 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3219 | |
Victor Stinner | 465db3c | 2014-07-26 14:47:56 +0200 | [diff] [blame] | 3220 | if (fd == INVALID_SOCKET) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3221 | set_error(); |
| 3222 | return -1; |
| 3223 | } |
| 3224 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3226 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3227 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3231 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3232 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3233 | static PyTypeObject sock_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3234 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 3235 | "_socket.socket", /* tp_name */ |
| 3236 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 3237 | 0, /* tp_itemsize */ |
| 3238 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 3239 | 0, /* tp_print */ |
| 3240 | 0, /* tp_getattr */ |
| 3241 | 0, /* tp_setattr */ |
| 3242 | 0, /* tp_compare */ |
| 3243 | (reprfunc)sock_repr, /* tp_repr */ |
| 3244 | 0, /* tp_as_number */ |
| 3245 | 0, /* tp_as_sequence */ |
| 3246 | 0, /* tp_as_mapping */ |
| 3247 | 0, /* tp_hash */ |
| 3248 | 0, /* tp_call */ |
| 3249 | 0, /* tp_str */ |
| 3250 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3251 | 0, /* tp_setattro */ |
| 3252 | 0, /* tp_as_buffer */ |
| 3253 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3254 | sock_doc, /* tp_doc */ |
| 3255 | 0, /* tp_traverse */ |
| 3256 | 0, /* tp_clear */ |
| 3257 | 0, /* tp_richcompare */ |
Benjamin Peterson | 3d1f2d3 | 2014-10-06 14:38:20 -0400 | [diff] [blame] | 3258 | offsetof(PySocketSockObject, weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3259 | 0, /* tp_iter */ |
| 3260 | 0, /* tp_iternext */ |
| 3261 | sock_methods, /* tp_methods */ |
| 3262 | sock_memberlist, /* tp_members */ |
| 3263 | 0, /* tp_getset */ |
| 3264 | 0, /* tp_base */ |
| 3265 | 0, /* tp_dict */ |
| 3266 | 0, /* tp_descr_get */ |
| 3267 | 0, /* tp_descr_set */ |
| 3268 | 0, /* tp_dictoffset */ |
| 3269 | sock_initobj, /* tp_init */ |
| 3270 | PyType_GenericAlloc, /* tp_alloc */ |
| 3271 | sock_new, /* tp_new */ |
| 3272 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3273 | }; |
| 3274 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3275 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3276 | /* Python interface to gethostname(). */ |
| 3277 | |
| 3278 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3279 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3280 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3281 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3282 | char buf[1024]; |
| 3283 | int res; |
| 3284 | Py_BEGIN_ALLOW_THREADS |
| 3285 | res = gethostname(buf, (int) sizeof buf - 1); |
| 3286 | Py_END_ALLOW_THREADS |
| 3287 | if (res < 0) |
| 3288 | return set_error(); |
| 3289 | buf[sizeof buf - 1] = '\0'; |
| 3290 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3291 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3292 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3293 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3294 | "gethostname() -> string\n\ |
| 3295 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3296 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3297 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3298 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3299 | /* Python interface to gethostbyname(name). */ |
| 3300 | |
| 3301 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3302 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3303 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3304 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3305 | char *name; |
| 3306 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3307 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3308 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
| 3309 | return NULL; |
| 3310 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
| 3311 | return NULL; |
| 3312 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3315 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3316 | "gethostbyname(host) -> address\n\ |
| 3317 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3318 | 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] | 3319 | |
| 3320 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3321 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 3322 | |
| 3323 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3324 | 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] | 3325 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3326 | char **pch; |
| 3327 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 3328 | PyObject *name_list = (PyObject *)NULL; |
| 3329 | PyObject *addr_list = (PyObject *)NULL; |
| 3330 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3331 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3332 | if (h == NULL) { |
| 3333 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3334 | #ifndef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3335 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3336 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3337 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3338 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3339 | return NULL; |
| 3340 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3341 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3342 | if (h->h_addrtype != af) { |
| 3343 | /* Let's get real error message to return */ |
| 3344 | PyErr_SetString(socket_error, |
| 3345 | (char *)strerror(EAFNOSUPPORT)); |
Brett Cannon | 10ed0f5 | 2008-03-18 15:35:58 +0000 | [diff] [blame] | 3346 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3347 | return NULL; |
| 3348 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3349 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3350 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3351 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3352 | case AF_INET: |
| 3353 | if (alen < sizeof(struct sockaddr_in)) |
| 3354 | return NULL; |
| 3355 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3356 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3357 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3358 | case AF_INET6: |
| 3359 | if (alen < sizeof(struct sockaddr_in6)) |
| 3360 | return NULL; |
| 3361 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3362 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3363 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3364 | } |
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 ((name_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 | if ((addr_list = PyList_New(0)) == NULL) |
| 3370 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3372 | /* SF #1511317: h_aliases can be NULL */ |
| 3373 | if (h->h_aliases) { |
| 3374 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3375 | int status; |
| 3376 | tmp = PyString_FromString(*pch); |
| 3377 | if (tmp == NULL) |
| 3378 | goto err; |
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 | status = PyList_Append(name_list, tmp); |
| 3381 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3382 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3383 | if (status) |
| 3384 | goto err; |
| 3385 | } |
| 3386 | } |
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 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3389 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3390 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3391 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3393 | case AF_INET: |
| 3394 | { |
| 3395 | struct sockaddr_in sin; |
| 3396 | memset(&sin, 0, sizeof(sin)); |
| 3397 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3398 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3399 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3400 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3401 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3402 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3404 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3405 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3406 | break; |
| 3407 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3408 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3409 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3410 | case AF_INET6: |
| 3411 | { |
| 3412 | struct sockaddr_in6 sin6; |
| 3413 | memset(&sin6, 0, sizeof(sin6)); |
| 3414 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3415 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3416 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3417 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3418 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3419 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3420 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3421 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3422 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3423 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3424 | break; |
| 3425 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3426 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3427 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3428 | default: /* can't happen */ |
| 3429 | PyErr_SetString(socket_error, |
| 3430 | "unsupported address family"); |
| 3431 | return NULL; |
| 3432 | } |
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 | if (tmp == NULL) |
| 3435 | goto err; |
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 | status = PyList_Append(addr_list, tmp); |
| 3438 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3439 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3440 | if (status) |
| 3441 | goto err; |
| 3442 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3444 | 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] | 3445 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3446 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3447 | Py_XDECREF(name_list); |
| 3448 | Py_XDECREF(addr_list); |
| 3449 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | |
| 3453 | /* Python interface to gethostbyname_ex(name). */ |
| 3454 | |
| 3455 | /*ARGSUSED*/ |
| 3456 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3457 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3458 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3459 | char *name; |
| 3460 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3461 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3462 | struct sockaddr_storage addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3463 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3464 | struct sockaddr_in addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3465 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3466 | struct sockaddr *sa; |
| 3467 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3468 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3469 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3470 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3471 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3472 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3473 | char buf[16384]; |
| 3474 | int buf_len = (sizeof buf) - 1; |
| 3475 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3476 | #endif |
Serhiy Storchaka | 6a35bf6 | 2016-10-08 09:58:08 +0300 | [diff] [blame] | 3477 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3478 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3479 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3480 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3481 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3482 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
| 3483 | return NULL; |
| 3484 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
| 3485 | return NULL; |
| 3486 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3487 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3488 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Serhiy Storchaka | 6a35bf6 | 2016-10-08 09:58:08 +0300 | [diff] [blame] | 3489 | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3490 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3491 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3492 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3493 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3494 | memset((void *) &data, '\0', sizeof(data)); |
| 3495 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3496 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3497 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3498 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3499 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3500 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3501 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3502 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3503 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3504 | Py_END_ALLOW_THREADS |
| 3505 | /* Some C libraries would require addr.__ss_family instead of |
| 3506 | addr.ss_family. |
| 3507 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3508 | access sa_family. */ |
| 3509 | sa = (struct sockaddr*)&addr; |
| 3510 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3511 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3512 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3513 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3514 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3515 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3518 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3519 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3520 | \n\ |
| 3521 | 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] | 3522 | 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] | 3523 | |
| 3524 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3525 | /* Python interface to gethostbyaddr(IP). */ |
| 3526 | |
| 3527 | /*ARGSUSED*/ |
| 3528 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3529 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3530 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3531 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3532 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3533 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3534 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3535 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3536 | struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3537 | char *ip_num; |
| 3538 | struct hostent *h; |
| 3539 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3540 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3541 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3542 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3543 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3544 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3545 | /* glibcs up to 2.10 assume that the buf argument to |
| 3546 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3547 | does not ensure. The attribute below instructs the compiler |
| 3548 | to maintain this alignment. */ |
| 3549 | char buf[16384] Py_ALIGNED(8); |
| 3550 | int buf_len = (sizeof buf) - 1; |
| 3551 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3552 | #endif |
Serhiy Storchaka | 6a35bf6 | 2016-10-08 09:58:08 +0300 | [diff] [blame] | 3553 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3554 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3555 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3556 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 3557 | const char *ap; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3558 | int al; |
| 3559 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3560 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3561 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
| 3562 | return NULL; |
| 3563 | af = AF_UNSPEC; |
| 3564 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
| 3565 | return NULL; |
| 3566 | af = sa->sa_family; |
| 3567 | ap = NULL; |
| 3568 | switch (af) { |
| 3569 | case AF_INET: |
| 3570 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3571 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3572 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3573 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3574 | case AF_INET6: |
| 3575 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3576 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3577 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3578 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3579 | default: |
| 3580 | PyErr_SetString(socket_error, "unsupported address family"); |
| 3581 | return NULL; |
| 3582 | } |
| 3583 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3584 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3585 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Serhiy Storchaka | 6a35bf6 | 2016-10-08 09:58:08 +0300 | [diff] [blame] | 3586 | gethostbyaddr_r(ap, al, af, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3587 | &hp_allocated, buf, buf_len, |
| 3588 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3589 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3590 | h = gethostbyaddr_r(ap, al, af, |
| 3591 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3592 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3593 | memset((void *) &data, '\0', sizeof(data)); |
| 3594 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3595 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3596 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3597 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3598 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3599 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3600 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3601 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3602 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3603 | Py_END_ALLOW_THREADS |
| 3604 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3605 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3606 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3607 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3608 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3611 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3612 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3613 | \n\ |
| 3614 | 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] | 3615 | 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] | 3616 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3617 | |
| 3618 | /* Python interface to getservbyname(name). |
| 3619 | This only returns the port number, since the other info is already |
| 3620 | known or not useful (like the list of aliases). */ |
| 3621 | |
| 3622 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3623 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3624 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3625 | { |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 3626 | const char *name, *proto=NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3627 | struct servent *sp; |
| 3628 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3629 | return NULL; |
| 3630 | Py_BEGIN_ALLOW_THREADS |
| 3631 | sp = getservbyname(name, proto); |
| 3632 | Py_END_ALLOW_THREADS |
| 3633 | if (sp == NULL) { |
| 3634 | PyErr_SetString(socket_error, "service/proto not found"); |
| 3635 | return NULL; |
| 3636 | } |
| 3637 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3640 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3641 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3642 | \n\ |
| 3643 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3644 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3645 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3646 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3647 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3648 | /* Python interface to getservbyport(port). |
| 3649 | This only returns the service name, since the other info is already |
| 3650 | known or not useful (like the list of aliases). */ |
| 3651 | |
| 3652 | /*ARGSUSED*/ |
| 3653 | static PyObject * |
| 3654 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3655 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3656 | int port; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 3657 | const char *proto=NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3658 | struct servent *sp; |
| 3659 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3660 | return NULL; |
| 3661 | if (port < 0 || port > 0xffff) { |
| 3662 | PyErr_SetString( |
| 3663 | PyExc_OverflowError, |
| 3664 | "getservbyport: port must be 0-65535."); |
| 3665 | return NULL; |
| 3666 | } |
| 3667 | Py_BEGIN_ALLOW_THREADS |
| 3668 | sp = getservbyport(htons((short)port), proto); |
| 3669 | Py_END_ALLOW_THREADS |
| 3670 | if (sp == NULL) { |
| 3671 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3672 | return NULL; |
| 3673 | } |
| 3674 | return PyString_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | PyDoc_STRVAR(getservbyport_doc, |
| 3678 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3679 | \n\ |
| 3680 | Return the service name from a port number and protocol name.\n\ |
| 3681 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3682 | otherwise any protocol will match."); |
| 3683 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3684 | /* Python interface to getprotobyname(name). |
| 3685 | This only returns the protocol number, since the other info is |
| 3686 | already known or not useful (like the list of aliases). */ |
| 3687 | |
| 3688 | /*ARGSUSED*/ |
| 3689 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3690 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3691 | { |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 3692 | const char *name; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3693 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3694 | #ifdef __BEOS__ |
| 3695 | /* Not available in BeOS yet. - [cjh] */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3696 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
| 3697 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3698 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3699 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3700 | return NULL; |
| 3701 | Py_BEGIN_ALLOW_THREADS |
| 3702 | sp = getprotobyname(name); |
| 3703 | Py_END_ALLOW_THREADS |
| 3704 | if (sp == NULL) { |
| 3705 | PyErr_SetString(socket_error, "protocol not found"); |
| 3706 | return NULL; |
| 3707 | } |
| 3708 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3709 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3712 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3713 | "getprotobyname(name) -> integer\n\ |
| 3714 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3715 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3716 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3717 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3718 | #ifdef HAVE_SOCKETPAIR |
| 3719 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3720 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3721 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3722 | |
| 3723 | /*ARGSUSED*/ |
| 3724 | static PyObject * |
| 3725 | socket_socketpair(PyObject *self, PyObject *args) |
| 3726 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3727 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3728 | SOCKET_T sv[2]; |
| 3729 | int family, type = SOCK_STREAM, proto = 0; |
| 3730 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3731 | |
| 3732 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3733 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3734 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3735 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3736 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3737 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3738 | &family, &type, &proto)) |
| 3739 | return NULL; |
| 3740 | /* Create a pair of socket fds */ |
| 3741 | if (socketpair(family, type, proto, sv) < 0) |
| 3742 | return set_error(); |
| 3743 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3744 | if (s0 == NULL) |
| 3745 | goto finally; |
| 3746 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3747 | if (s1 == NULL) |
| 3748 | goto finally; |
| 3749 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3750 | |
| 3751 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3752 | if (res == NULL) { |
| 3753 | if (s0 == NULL) |
| 3754 | SOCKETCLOSE(sv[0]); |
| 3755 | if (s1 == NULL) |
| 3756 | SOCKETCLOSE(sv[1]); |
| 3757 | } |
| 3758 | Py_XDECREF(s0); |
| 3759 | Py_XDECREF(s1); |
| 3760 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | PyDoc_STRVAR(socketpair_doc, |
| 3764 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3765 | \n\ |
| 3766 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3767 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3768 | 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] | 3769 | 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] | 3770 | |
| 3771 | #endif /* HAVE_SOCKETPAIR */ |
| 3772 | |
| 3773 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3774 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3775 | /* Create a socket object from a numeric file description. |
| 3776 | Useful e.g. if stdin is a socket. |
| 3777 | Additional arguments as for socket(). */ |
| 3778 | |
| 3779 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3780 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3781 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3782 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3783 | PySocketSockObject *s; |
| 3784 | SOCKET_T fd; |
| 3785 | int family, type, proto = 0; |
| 3786 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 3787 | &fd, &family, &type, &proto)) |
| 3788 | return NULL; |
| 3789 | /* Dup the fd so it and the socket can be closed independently */ |
| 3790 | fd = dup(fd); |
| 3791 | if (fd < 0) |
| 3792 | return set_error(); |
| 3793 | s = new_sockobject(fd, family, type, proto); |
| 3794 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3795 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3797 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3798 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 3799 | \n\ |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 3800 | Create a socket object from a duplicate of the given\n\ |
| 3801 | file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3802 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3803 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3804 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3805 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3806 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3807 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3808 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3809 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3810 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3811 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3812 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3813 | return NULL; |
| 3814 | } |
| 3815 | if (x1 < 0) { |
| 3816 | PyErr_SetString(PyExc_OverflowError, |
| 3817 | "can't convert negative number to unsigned long"); |
| 3818 | return NULL; |
| 3819 | } |
| 3820 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3821 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3824 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3825 | "ntohs(integer) -> integer\n\ |
| 3826 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3827 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3828 | |
| 3829 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3830 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3831 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3832 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3833 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3834 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3835 | if (PyInt_Check(arg)) { |
| 3836 | x = PyInt_AS_LONG(arg); |
| 3837 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3838 | return NULL; |
| 3839 | if ((long)x < 0) { |
| 3840 | PyErr_SetString(PyExc_OverflowError, |
| 3841 | "can't convert negative number to unsigned long"); |
| 3842 | return NULL; |
| 3843 | } |
| 3844 | } |
| 3845 | else if (PyLong_Check(arg)) { |
| 3846 | x = PyLong_AsUnsignedLong(arg); |
| 3847 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3848 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3849 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3850 | { |
| 3851 | unsigned long y; |
| 3852 | /* only want the trailing 32 bits */ |
| 3853 | y = x & 0xFFFFFFFFUL; |
| 3854 | if (y ^ x) |
| 3855 | return PyErr_Format(PyExc_OverflowError, |
| 3856 | "long int larger than 32 bits"); |
| 3857 | x = y; |
| 3858 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3859 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3860 | } |
| 3861 | else |
| 3862 | return PyErr_Format(PyExc_TypeError, |
| 3863 | "expected int/long, %s found", |
| 3864 | Py_TYPE(arg)->tp_name); |
| 3865 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3866 | return NULL; |
| 3867 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3870 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3871 | "ntohl(integer) -> integer\n\ |
| 3872 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3873 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3874 | |
| 3875 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3876 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3877 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3878 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3879 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3880 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3881 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3882 | return NULL; |
| 3883 | } |
| 3884 | if (x1 < 0) { |
| 3885 | PyErr_SetString(PyExc_OverflowError, |
| 3886 | "can't convert negative number to unsigned long"); |
| 3887 | return NULL; |
| 3888 | } |
| 3889 | x2 = (unsigned int)htons((unsigned short)x1); |
| 3890 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3893 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3894 | "htons(integer) -> integer\n\ |
| 3895 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3896 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3897 | |
| 3898 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3899 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3900 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3901 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3902 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3904 | if (PyInt_Check(arg)) { |
| 3905 | x = PyInt_AS_LONG(arg); |
| 3906 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3907 | return NULL; |
| 3908 | if ((long)x < 0) { |
| 3909 | PyErr_SetString(PyExc_OverflowError, |
| 3910 | "can't convert negative number to unsigned long"); |
| 3911 | return NULL; |
| 3912 | } |
| 3913 | } |
| 3914 | else if (PyLong_Check(arg)) { |
| 3915 | x = PyLong_AsUnsignedLong(arg); |
| 3916 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3917 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3918 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3919 | { |
| 3920 | unsigned long y; |
| 3921 | /* only want the trailing 32 bits */ |
| 3922 | y = x & 0xFFFFFFFFUL; |
| 3923 | if (y ^ x) |
| 3924 | return PyErr_Format(PyExc_OverflowError, |
| 3925 | "long int larger than 32 bits"); |
| 3926 | x = y; |
| 3927 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3928 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3929 | } |
| 3930 | else |
| 3931 | return PyErr_Format(PyExc_TypeError, |
| 3932 | "expected int/long, %s found", |
| 3933 | Py_TYPE(arg)->tp_name); |
| 3934 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3937 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3938 | "htonl(integer) -> integer\n\ |
| 3939 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3940 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3941 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3942 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3944 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3945 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3946 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3947 | 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] | 3948 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3949 | |
| 3950 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3951 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3952 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3953 | #ifndef INADDR_NONE |
| 3954 | #define INADDR_NONE (-1) |
| 3955 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3956 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3957 | struct in_addr buf; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3958 | #endif |
| 3959 | |
| 3960 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 3961 | #if (SIZEOF_INT != 4) |
| 3962 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3963 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3964 | /* Have to use inet_addr() instead */ |
| 3965 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3966 | #endif |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 3967 | const char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3968 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3969 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3970 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3971 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3972 | |
| 3973 | #ifdef HAVE_INET_ATON |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3974 | |
| 3975 | #ifdef USE_INET_ATON_WEAKLINK |
| 3976 | if (inet_aton != NULL) { |
| 3977 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3978 | if (inet_aton(ip_addr, &buf)) |
| 3979 | return PyString_FromStringAndSize((char *)(&buf), |
| 3980 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3981 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3982 | PyErr_SetString(socket_error, |
| 3983 | "illegal IP address string passed to inet_aton"); |
| 3984 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3985 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3986 | #ifdef USE_INET_ATON_WEAKLINK |
| 3987 | } else { |
| 3988 | #endif |
| 3989 | |
| 3990 | #endif |
| 3991 | |
| 3992 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3993 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3994 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3995 | * for this */ |
| 3996 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3997 | packed_addr = 0xFFFFFFFF; |
| 3998 | } else { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3999 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4000 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 4001 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4002 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 4003 | PyErr_SetString(socket_error, |
| 4004 | "illegal IP address string passed to inet_aton"); |
| 4005 | return NULL; |
| 4006 | } |
| 4007 | } |
| 4008 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 4009 | sizeof(packed_addr)); |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 4010 | |
| 4011 | #ifdef USE_INET_ATON_WEAKLINK |
| 4012 | } |
| 4013 | #endif |
| 4014 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 4015 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4018 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 4019 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4020 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4021 | 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] | 4022 | |
| 4023 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4024 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4025 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4026 | char *packed_str; |
| 4027 | int addr_len; |
| 4028 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4029 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4030 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
| 4031 | return NULL; |
| 4032 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4033 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4034 | if (addr_len != sizeof(packed_addr)) { |
| 4035 | PyErr_SetString(socket_error, |
| 4036 | "packed IP wrong length for inet_ntoa"); |
| 4037 | return NULL; |
| 4038 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4039 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4040 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4041 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4042 | return PyString_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 4043 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4044 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4045 | #ifdef HAVE_INET_PTON |
| 4046 | |
| 4047 | PyDoc_STRVAR(inet_pton_doc, |
| 4048 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 4049 | \n\ |
| 4050 | Convert an IP address from string format to a packed string suitable\n\ |
| 4051 | for use with low-level network functions."); |
| 4052 | |
| 4053 | static PyObject * |
| 4054 | socket_inet_pton(PyObject *self, PyObject *args) |
| 4055 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4056 | int af; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 4057 | const char* ip; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4058 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4059 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4060 | 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] | 4061 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4062 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4063 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4064 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 4065 | return NULL; |
| 4066 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4067 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 4068 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4069 | if(af == AF_INET6) { |
| 4070 | PyErr_SetString(socket_error, |
| 4071 | "can't use AF_INET6, IPv6 is disabled"); |
| 4072 | return NULL; |
| 4073 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4074 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 4075 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4076 | retval = inet_pton(af, ip, packed); |
| 4077 | if (retval < 0) { |
| 4078 | PyErr_SetFromErrno(socket_error); |
| 4079 | return NULL; |
| 4080 | } else if (retval == 0) { |
| 4081 | PyErr_SetString(socket_error, |
| 4082 | "illegal IP address string passed to inet_pton"); |
| 4083 | return NULL; |
| 4084 | } else if (af == AF_INET) { |
| 4085 | return PyString_FromStringAndSize(packed, |
| 4086 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4087 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4088 | } else if (af == AF_INET6) { |
| 4089 | return PyString_FromStringAndSize(packed, |
| 4090 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4091 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4092 | } else { |
| 4093 | PyErr_SetString(socket_error, "unknown address family"); |
| 4094 | return NULL; |
| 4095 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4096 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4097 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4098 | PyDoc_STRVAR(inet_ntop_doc, |
| 4099 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 4100 | \n\ |
| 4101 | Convert a packed IP address of the given family to string format."); |
| 4102 | |
| 4103 | static PyObject * |
| 4104 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 4105 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4106 | int af; |
| 4107 | char* packed; |
| 4108 | int len; |
| 4109 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4110 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4111 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4112 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4113 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4114 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4116 | /* Guarantee NUL-termination for PyString_FromString() below */ |
| 4117 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4118 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4119 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 4120 | return NULL; |
| 4121 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4122 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4123 | if (af == AF_INET) { |
| 4124 | if (len != sizeof(struct in_addr)) { |
| 4125 | PyErr_SetString(PyExc_ValueError, |
| 4126 | "invalid length of packed IP address string"); |
| 4127 | return NULL; |
| 4128 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4129 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4130 | } else if (af == AF_INET6) { |
| 4131 | if (len != sizeof(struct in6_addr)) { |
| 4132 | PyErr_SetString(PyExc_ValueError, |
| 4133 | "invalid length of packed IP address string"); |
| 4134 | return NULL; |
| 4135 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4136 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4137 | } else { |
| 4138 | PyErr_Format(PyExc_ValueError, |
| 4139 | "unknown address family %d", af); |
| 4140 | return NULL; |
| 4141 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4142 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4143 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 4144 | if (!retval) { |
| 4145 | PyErr_SetFromErrno(socket_error); |
| 4146 | return NULL; |
| 4147 | } else { |
| 4148 | return PyString_FromString(retval); |
| 4149 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4150 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4151 | /* NOTREACHED */ |
| 4152 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 4153 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
| 4156 | #endif /* HAVE_INET_PTON */ |
| 4157 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4158 | /* Python interface to getaddrinfo(host, port). */ |
| 4159 | |
| 4160 | /*ARGSUSED*/ |
| 4161 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4162 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4163 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4164 | struct addrinfo hints, *res; |
| 4165 | struct addrinfo *res0 = NULL; |
| 4166 | PyObject *hobj = NULL; |
| 4167 | PyObject *pobj = (PyObject *)NULL; |
| 4168 | char pbuf[30]; |
| 4169 | char *hptr, *pptr; |
| 4170 | int family, socktype, protocol, flags; |
| 4171 | int error; |
| 4172 | PyObject *all = (PyObject *)NULL; |
| 4173 | PyObject *single = (PyObject *)NULL; |
| 4174 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4175 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4176 | family = socktype = protocol = flags = 0; |
| 4177 | family = AF_UNSPEC; |
| 4178 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 4179 | &hobj, &pobj, &family, &socktype, |
| 4180 | &protocol, &flags)) { |
| 4181 | return NULL; |
| 4182 | } |
| 4183 | if (hobj == Py_None) { |
| 4184 | hptr = NULL; |
| 4185 | } else if (PyUnicode_Check(hobj)) { |
Victor Stinner | 9a2326b | 2015-09-11 12:42:13 +0200 | [diff] [blame] | 4186 | idna = PyUnicode_AsEncodedString(hobj, "idna", NULL); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4187 | if (!idna) |
| 4188 | return NULL; |
| 4189 | hptr = PyString_AsString(idna); |
| 4190 | } else if (PyString_Check(hobj)) { |
| 4191 | hptr = PyString_AsString(hobj); |
| 4192 | } else { |
| 4193 | PyErr_SetString(PyExc_TypeError, |
| 4194 | "getaddrinfo() argument 1 must be string or None"); |
| 4195 | return NULL; |
| 4196 | } |
Serhiy Storchaka | 48c8bf2 | 2018-07-31 09:09:36 +0300 | [diff] [blame] | 4197 | if (_PyAnyInt_Check(pobj)) { |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4198 | long value = PyLong_AsLong(pobj); |
| 4199 | if (value == -1 && PyErr_Occurred()) |
| 4200 | return NULL; |
| 4201 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4202 | pptr = pbuf; |
| 4203 | } else if (PyString_Check(pobj)) { |
| 4204 | pptr = PyString_AsString(pobj); |
| 4205 | } else if (pobj == Py_None) { |
| 4206 | pptr = (char *)NULL; |
| 4207 | } else { |
Petri Lehtinen | ab7dd18 | 2012-12-20 21:06:14 +0200 | [diff] [blame] | 4208 | PyErr_SetString(socket_error, |
| 4209 | "getaddrinfo() argument 2 must be integer or string"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4210 | goto err; |
| 4211 | } |
Ronald Oussoren | 0e6283e | 2013-06-10 10:35:36 +0200 | [diff] [blame] | 4212 | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
Ronald Oussoren | 16c52a3 | 2013-05-24 13:45:27 +0200 | [diff] [blame] | 4213 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
| 4214 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
| 4215 | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
| 4216 | * This workaround avoids a segfault in libsystem. |
| 4217 | */ |
| 4218 | pptr = "00"; |
| 4219 | } |
| 4220 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4221 | memset(&hints, 0, sizeof(hints)); |
| 4222 | hints.ai_family = family; |
| 4223 | hints.ai_socktype = socktype; |
| 4224 | hints.ai_protocol = protocol; |
| 4225 | hints.ai_flags = flags; |
| 4226 | Py_BEGIN_ALLOW_THREADS |
| 4227 | ACQUIRE_GETADDRINFO_LOCK |
| 4228 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 4229 | Py_END_ALLOW_THREADS |
| 4230 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4231 | if (error) { |
| 4232 | set_gaierror(error); |
| 4233 | goto err; |
| 4234 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4235 | |
Benjamin Peterson | a5a44ab | 2015-04-01 11:16:40 -0400 | [diff] [blame] | 4236 | all = PyList_New(0); |
| 4237 | if (all == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4238 | goto err; |
| 4239 | for (res = res0; res; res = res->ai_next) { |
| 4240 | PyObject *addr = |
| 4241 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 4242 | if (addr == NULL) |
| 4243 | goto err; |
| 4244 | single = Py_BuildValue("iiisO", res->ai_family, |
| 4245 | res->ai_socktype, res->ai_protocol, |
| 4246 | res->ai_canonname ? res->ai_canonname : "", |
| 4247 | addr); |
| 4248 | Py_DECREF(addr); |
| 4249 | if (single == NULL) |
| 4250 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4251 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4252 | if (PyList_Append(all, single)) |
| 4253 | goto err; |
| 4254 | Py_XDECREF(single); |
| 4255 | } |
| 4256 | Py_XDECREF(idna); |
| 4257 | if (res0) |
| 4258 | freeaddrinfo(res0); |
| 4259 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4260 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4261 | Py_XDECREF(single); |
| 4262 | Py_XDECREF(all); |
| 4263 | Py_XDECREF(idna); |
| 4264 | if (res0) |
| 4265 | freeaddrinfo(res0); |
| 4266 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4269 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4270 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 4271 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4272 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4273 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4274 | |
| 4275 | /* Python interface to getnameinfo(sa, flags). */ |
| 4276 | |
| 4277 | /*ARGSUSED*/ |
| 4278 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4279 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4280 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4281 | PyObject *sa = (PyObject *)NULL; |
| 4282 | int flags; |
Serhiy Storchaka | 1bce4ef | 2017-11-09 23:05:59 +0200 | [diff] [blame] | 4283 | const char *hostp; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4284 | int port; |
| 4285 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4286 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 4287 | struct addrinfo hints, *res = NULL; |
| 4288 | int error; |
| 4289 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4290 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4291 | flags = flowinfo = scope_id = 0; |
| 4292 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 4293 | return NULL; |
| 4294 | if (!PyTuple_Check(sa)) { |
| 4295 | PyErr_SetString(PyExc_TypeError, |
| 4296 | "getnameinfo() argument 1 must be a tuple"); |
| 4297 | return NULL; |
| 4298 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4299 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4300 | &hostp, &port, &flowinfo, &scope_id)) |
| 4301 | return NULL; |
Charles-François Natali | 65dd745 | 2012-06-23 10:06:56 +0200 | [diff] [blame] | 4302 | if (flowinfo > 0xfffff) { |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4303 | PyErr_SetString(PyExc_OverflowError, |
| 4304 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 4305 | return NULL; |
| 4306 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4307 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 4308 | memset(&hints, 0, sizeof(hints)); |
| 4309 | hints.ai_family = AF_UNSPEC; |
| 4310 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
| 4311 | Py_BEGIN_ALLOW_THREADS |
| 4312 | ACQUIRE_GETADDRINFO_LOCK |
| 4313 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 4314 | Py_END_ALLOW_THREADS |
| 4315 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4316 | if (error) { |
| 4317 | set_gaierror(error); |
| 4318 | goto fail; |
| 4319 | } |
| 4320 | if (res->ai_next) { |
| 4321 | PyErr_SetString(socket_error, |
| 4322 | "sockaddr resolved to multiple addresses"); |
| 4323 | goto fail; |
| 4324 | } |
| 4325 | switch (res->ai_family) { |
| 4326 | case AF_INET: |
| 4327 | { |
| 4328 | if (PyTuple_GET_SIZE(sa) != 2) { |
| 4329 | PyErr_SetString(socket_error, |
| 4330 | "IPv4 sockaddr must be 2 tuple"); |
| 4331 | goto fail; |
| 4332 | } |
| 4333 | break; |
| 4334 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4335 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4336 | case AF_INET6: |
| 4337 | { |
| 4338 | struct sockaddr_in6 *sin6; |
| 4339 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4340 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4341 | sin6->sin6_scope_id = scope_id; |
| 4342 | break; |
| 4343 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4344 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4345 | } |
| 4346 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 4347 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 4348 | if (error) { |
| 4349 | set_gaierror(error); |
| 4350 | goto fail; |
| 4351 | } |
| 4352 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4353 | |
| 4354 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4355 | if (res) |
| 4356 | freeaddrinfo(res); |
| 4357 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4360 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4361 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4362 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4363 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4364 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4365 | |
| 4366 | /* Python API to getting and setting the default timeout value. */ |
| 4367 | |
| 4368 | static PyObject * |
| 4369 | socket_getdefaulttimeout(PyObject *self) |
| 4370 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4371 | if (defaulttimeout < 0.0) { |
| 4372 | Py_INCREF(Py_None); |
| 4373 | return Py_None; |
| 4374 | } |
| 4375 | else |
| 4376 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4380 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4381 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4382 | 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] | 4383 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4384 | When the socket module is first imported, the default is None."); |
| 4385 | |
| 4386 | static PyObject * |
| 4387 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 4388 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4389 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4390 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4391 | if (arg == Py_None) |
| 4392 | timeout = -1.0; |
| 4393 | else { |
| 4394 | timeout = PyFloat_AsDouble(arg); |
| 4395 | if (timeout < 0.0) { |
| 4396 | if (!PyErr_Occurred()) |
| 4397 | PyErr_SetString(PyExc_ValueError, |
| 4398 | "Timeout value out of range"); |
| 4399 | return NULL; |
| 4400 | } |
| 4401 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4402 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4403 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4404 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4405 | Py_INCREF(Py_None); |
| 4406 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4410 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4411 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4412 | 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] | 4413 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4414 | When the socket module is first imported, the default is None."); |
| 4415 | |
| 4416 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4417 | /* List of functions exported by this module. */ |
| 4418 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4419 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4420 | {"gethostbyname", socket_gethostbyname, |
| 4421 | METH_VARARGS, gethostbyname_doc}, |
| 4422 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4423 | METH_VARARGS, ghbn_ex_doc}, |
| 4424 | {"gethostbyaddr", socket_gethostbyaddr, |
| 4425 | METH_VARARGS, gethostbyaddr_doc}, |
| 4426 | {"gethostname", socket_gethostname, |
| 4427 | METH_NOARGS, gethostname_doc}, |
| 4428 | {"getservbyname", socket_getservbyname, |
| 4429 | METH_VARARGS, getservbyname_doc}, |
| 4430 | {"getservbyport", socket_getservbyport, |
| 4431 | METH_VARARGS, getservbyport_doc}, |
| 4432 | {"getprotobyname", socket_getprotobyname, |
| 4433 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4434 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4435 | {"fromfd", socket_fromfd, |
| 4436 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4437 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4438 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4439 | {"socketpair", socket_socketpair, |
| 4440 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4441 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4442 | {"ntohs", socket_ntohs, |
| 4443 | METH_VARARGS, ntohs_doc}, |
| 4444 | {"ntohl", socket_ntohl, |
| 4445 | METH_O, ntohl_doc}, |
| 4446 | {"htons", socket_htons, |
| 4447 | METH_VARARGS, htons_doc}, |
| 4448 | {"htonl", socket_htonl, |
| 4449 | METH_O, htonl_doc}, |
| 4450 | {"inet_aton", socket_inet_aton, |
| 4451 | METH_VARARGS, inet_aton_doc}, |
| 4452 | {"inet_ntoa", socket_inet_ntoa, |
| 4453 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4454 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4455 | {"inet_pton", socket_inet_pton, |
| 4456 | METH_VARARGS, inet_pton_doc}, |
| 4457 | {"inet_ntop", socket_inet_ntop, |
| 4458 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4459 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4460 | {"getaddrinfo", socket_getaddrinfo, |
| 4461 | METH_VARARGS, getaddrinfo_doc}, |
| 4462 | {"getnameinfo", socket_getnameinfo, |
| 4463 | METH_VARARGS, getnameinfo_doc}, |
| 4464 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4465 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4466 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4467 | METH_O, setdefaulttimeout_doc}, |
| 4468 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4469 | }; |
| 4470 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4471 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4472 | #ifdef RISCOS |
| 4473 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4474 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4475 | static int |
| 4476 | os_init(void) |
| 4477 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4478 | _kernel_swi_regs r; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4479 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4480 | r.r[0] = 0; |
| 4481 | _kernel_swi(0x43380, &r, &r); |
| 4482 | taskwindow = r.r[0]; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4483 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4484 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | #endif /* RISCOS */ |
| 4488 | |
| 4489 | |
| 4490 | #ifdef MS_WINDOWS |
| 4491 | #define OS_INIT_DEFINED |
| 4492 | |
| 4493 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4494 | |
| 4495 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4496 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4497 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4498 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4502 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4503 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4504 | WSADATA WSAData; |
| 4505 | int ret; |
| 4506 | char buf[100]; |
| 4507 | ret = WSAStartup(0x0101, &WSAData); |
| 4508 | switch (ret) { |
| 4509 | case 0: /* No error */ |
| 4510 | Py_AtExit(os_cleanup); |
| 4511 | return 1; /* Success */ |
| 4512 | case WSASYSNOTREADY: |
| 4513 | PyErr_SetString(PyExc_ImportError, |
| 4514 | "WSAStartup failed: network not ready"); |
| 4515 | break; |
| 4516 | case WSAVERNOTSUPPORTED: |
| 4517 | case WSAEINVAL: |
| 4518 | PyErr_SetString( |
| 4519 | PyExc_ImportError, |
| 4520 | "WSAStartup failed: requested version not supported"); |
| 4521 | break; |
| 4522 | default: |
| 4523 | PyOS_snprintf(buf, sizeof(buf), |
| 4524 | "WSAStartup failed: error code %d", ret); |
| 4525 | PyErr_SetString(PyExc_ImportError, buf); |
| 4526 | break; |
| 4527 | } |
| 4528 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4531 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4532 | |
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 | #ifdef PYOS_OS2 |
| 4535 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4536 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4537 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4538 | |
| 4539 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4540 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4541 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4542 | #ifndef PYCC_GCC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4543 | char reason[64]; |
| 4544 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4546 | if (rc == 0) { |
| 4547 | return 1; /* Success */ |
| 4548 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4549 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4550 | PyOS_snprintf(reason, sizeof(reason), |
| 4551 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 4552 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4553 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4554 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4555 | #else |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4556 | /* No need to initialize sockets with GCC/EMX */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4557 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4558 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | #endif /* PYOS_OS2 */ |
| 4562 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4563 | |
| 4564 | #ifndef OS_INIT_DEFINED |
| 4565 | static int |
| 4566 | os_init(void) |
| 4567 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4568 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4569 | } |
| 4570 | #endif |
| 4571 | |
| 4572 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4573 | /* C API table - always add new things to the end for binary |
| 4574 | compatibility. */ |
| 4575 | static |
| 4576 | PySocketModule_APIObject PySocketModuleAPI = |
| 4577 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4578 | &sock_type, |
| 4579 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4580 | }; |
| 4581 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4582 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4583 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4584 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4585 | This module is actually called "_socket", and there's a wrapper |
| 4586 | "socket.py" which implements some additional functionality. On some |
| 4587 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 4588 | wrapper for the socket type that provides missing functionality such |
| 4589 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 4590 | with an ImportError exception if os-specific initialization fails. |
| 4591 | On Windows, this does WINSOCK initialization. When WINSOCK is |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4592 | initialized successfully, a call to WSACleanup() is scheduled to be |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4593 | made at exit time. |
| 4594 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4595 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4596 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4597 | "Implementation module for socket operations.\n\ |
| 4598 | \n\ |
| 4599 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4600 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4601 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4602 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4603 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4604 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4606 | if (!os_init()) |
| 4607 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4608 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4609 | Py_TYPE(&sock_type) = &PyType_Type; |
| 4610 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 4611 | socket_methods, |
| 4612 | socket_doc); |
| 4613 | if (m == NULL) |
| 4614 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4615 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4616 | socket_error = PyErr_NewException("socket.error", |
| 4617 | PyExc_IOError, NULL); |
| 4618 | if (socket_error == NULL) |
| 4619 | return; |
| 4620 | PySocketModuleAPI.error = socket_error; |
| 4621 | Py_INCREF(socket_error); |
| 4622 | PyModule_AddObject(m, "error", socket_error); |
| 4623 | socket_herror = PyErr_NewException("socket.herror", |
| 4624 | socket_error, NULL); |
| 4625 | if (socket_herror == NULL) |
| 4626 | return; |
| 4627 | Py_INCREF(socket_herror); |
| 4628 | PyModule_AddObject(m, "herror", socket_herror); |
| 4629 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4630 | NULL); |
| 4631 | if (socket_gaierror == NULL) |
| 4632 | return; |
| 4633 | Py_INCREF(socket_gaierror); |
| 4634 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4635 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4636 | socket_error, NULL); |
| 4637 | if (socket_timeout == NULL) |
| 4638 | return; |
| 4639 | Py_INCREF(socket_timeout); |
| 4640 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 4641 | Py_INCREF((PyObject *)&sock_type); |
| 4642 | if (PyModule_AddObject(m, "SocketType", |
| 4643 | (PyObject *)&sock_type) != 0) |
| 4644 | return; |
| 4645 | Py_INCREF((PyObject *)&sock_type); |
| 4646 | if (PyModule_AddObject(m, "socket", |
| 4647 | (PyObject *)&sock_type) != 0) |
| 4648 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4649 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4650 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4651 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4652 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4653 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4654 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4655 | Py_INCREF(has_ipv6); |
| 4656 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4657 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4658 | /* Export C API */ |
| 4659 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4660 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4661 | ) != 0) |
| 4662 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4663 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4664 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4665 | #ifdef AF_UNSPEC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4666 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4667 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4668 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4669 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4670 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4671 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4672 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4673 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4674 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4675 | #ifdef AF_AX25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4676 | /* Amateur Radio AX.25 */ |
| 4677 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4678 | #endif |
| 4679 | #ifdef AF_IPX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4680 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4681 | #endif |
| 4682 | #ifdef AF_APPLETALK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4683 | /* Appletalk DDP */ |
| 4684 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4685 | #endif |
| 4686 | #ifdef AF_NETROM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4687 | /* Amateur radio NetROM */ |
| 4688 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4689 | #endif |
| 4690 | #ifdef AF_BRIDGE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4691 | /* Multiprotocol bridge */ |
| 4692 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4693 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4694 | #ifdef AF_ATMPVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4695 | /* ATM PVCs */ |
| 4696 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4697 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4698 | #ifdef AF_AAL5 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4699 | /* Reserved for Werner's ATM */ |
| 4700 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4701 | #endif |
| 4702 | #ifdef AF_X25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4703 | /* Reserved for X.25 project */ |
| 4704 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4705 | #endif |
| 4706 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4707 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4708 | #endif |
| 4709 | #ifdef AF_ROSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4710 | /* Amateur Radio X.25 PLP */ |
| 4711 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4712 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4713 | #ifdef AF_DECnet |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4714 | /* Reserved for DECnet project */ |
| 4715 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4716 | #endif |
| 4717 | #ifdef AF_NETBEUI |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4718 | /* Reserved for 802.2LLC project */ |
| 4719 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4720 | #endif |
| 4721 | #ifdef AF_SECURITY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4722 | /* Security callback pseudo AF */ |
| 4723 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4724 | #endif |
| 4725 | #ifdef AF_KEY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4726 | /* PF_KEY key management API */ |
| 4727 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4728 | #endif |
| 4729 | #ifdef AF_NETLINK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4730 | /* */ |
| 4731 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4732 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4733 | #ifdef NETLINK_SKIP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4734 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4735 | #endif |
| 4736 | #ifdef NETLINK_W1 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4737 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4738 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4739 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4740 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4741 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4742 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4743 | #endif |
| 4744 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4745 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4746 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4747 | #ifdef NETLINK_XFRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4748 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4749 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4750 | #ifdef NETLINK_ARPD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4751 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4752 | #endif |
| 4753 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4754 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4755 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4756 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Martin v. Löwis | b1cc1d4 | 2007-02-13 12:14:19 +0000 | [diff] [blame] | 4757 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4758 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
| 4759 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4760 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4761 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4762 | #endif |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 4763 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4764 | #ifdef AF_ROUTE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4765 | /* Alias to emulate 4.4BSD */ |
| 4766 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4767 | #endif |
| 4768 | #ifdef AF_ASH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4769 | /* Ash */ |
| 4770 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4771 | #endif |
| 4772 | #ifdef AF_ECONET |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4773 | /* Acorn Econet */ |
| 4774 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4775 | #endif |
| 4776 | #ifdef AF_ATMSVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4777 | /* ATM SVCs */ |
| 4778 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4779 | #endif |
| 4780 | #ifdef AF_SNA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4781 | /* Linux SNA Project (nutters!) */ |
| 4782 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4783 | #endif |
| 4784 | #ifdef AF_IRDA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4785 | /* IRDA sockets */ |
| 4786 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4787 | #endif |
| 4788 | #ifdef AF_PPPOX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4789 | /* PPPoX sockets */ |
| 4790 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4791 | #endif |
| 4792 | #ifdef AF_WANPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4793 | /* Wanpipe API Sockets */ |
| 4794 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4795 | #endif |
| 4796 | #ifdef AF_LLC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4797 | /* Linux LLC */ |
| 4798 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4799 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4800 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4801 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4802 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4803 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4804 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4805 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4806 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4807 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4808 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4809 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4810 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4811 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4812 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4813 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4814 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4815 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4816 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4817 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4818 | 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] | 4819 | #endif |
| 4820 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 4821 | #ifdef AF_PACKET |
| 4822 | PyModule_AddIntMacro(m, AF_PACKET); |
| 4823 | #endif |
| 4824 | #ifdef PF_PACKET |
| 4825 | PyModule_AddIntMacro(m, PF_PACKET); |
| 4826 | #endif |
| 4827 | #ifdef PACKET_HOST |
| 4828 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 4829 | #endif |
| 4830 | #ifdef PACKET_BROADCAST |
| 4831 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 4832 | #endif |
| 4833 | #ifdef PACKET_MULTICAST |
| 4834 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 4835 | #endif |
| 4836 | #ifdef PACKET_OTHERHOST |
| 4837 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 4838 | #endif |
| 4839 | #ifdef PACKET_OUTGOING |
| 4840 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 4841 | #endif |
| 4842 | #ifdef PACKET_LOOPBACK |
| 4843 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 4844 | #endif |
| 4845 | #ifdef PACKET_FASTROUTE |
| 4846 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4847 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4848 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4849 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4850 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4851 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4852 | /* for addresses */ |
| 4853 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4854 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4855 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4856 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4857 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4858 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4859 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4860 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4861 | /* for setsockopt() */ |
| 4862 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4863 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4864 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4865 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4866 | TIPC_DEST_DROPPABLE); |
| 4867 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4868 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4869 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4870 | TIPC_LOW_IMPORTANCE); |
| 4871 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4872 | TIPC_MEDIUM_IMPORTANCE); |
| 4873 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4874 | TIPC_HIGH_IMPORTANCE); |
| 4875 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4876 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4878 | /* for subscriptions */ |
| 4879 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4880 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4881 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4882 | /* doesn't seem to be available everywhere */ |
| 4883 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4884 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4885 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4886 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4887 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4888 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4889 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4890 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4891 | #endif |
| 4892 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4893 | /* Socket types */ |
| 4894 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4895 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4896 | #ifndef __BEOS__ |
| 4897 | /* We have incomplete socket support. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4898 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4899 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4900 | #if defined(SOCK_RDM) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4901 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4902 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4903 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4905 | #ifdef SO_DEBUG |
| 4906 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
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_ACCEPTCONN |
| 4909 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4910 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4911 | #ifdef SO_REUSEADDR |
| 4912 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4913 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4914 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4915 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4916 | #endif |
| 4917 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4918 | #ifdef SO_KEEPALIVE |
| 4919 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
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_DONTROUTE |
| 4922 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
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_BROADCAST |
| 4925 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
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_USELOOPBACK |
| 4928 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
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_LINGER |
| 4931 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
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_OOBINLINE |
| 4934 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
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_REUSEPORT |
| 4937 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
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_SNDBUF |
| 4940 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
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_RCVBUF |
| 4943 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
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_SNDLOWAT |
| 4946 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
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_RCVLOWAT |
| 4949 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
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_SNDTIMEO |
| 4952 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
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_RCVTIMEO |
| 4955 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
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_ERROR |
| 4958 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4959 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4960 | #ifdef SO_TYPE |
| 4961 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4962 | #endif |
Larry Hastings | 3b958e3 | 2010-04-02 11:18:17 +0000 | [diff] [blame] | 4963 | #ifdef SO_SETFIB |
| 4964 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB); |
| 4965 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4967 | /* Maximum number of connections for "listen" */ |
| 4968 | #ifdef SOMAXCONN |
| 4969 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4970 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4971 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4972 | #endif |
| 4973 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4974 | /* Flags for send, recv */ |
| 4975 | #ifdef MSG_OOB |
| 4976 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
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_PEEK |
| 4979 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
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_DONTROUTE |
| 4982 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4983 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4984 | #ifdef MSG_DONTWAIT |
| 4985 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4986 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4987 | #ifdef MSG_EOR |
| 4988 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
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_TRUNC |
| 4991 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
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_CTRUNC |
| 4994 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
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_WAITALL |
| 4997 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
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_BTAG |
| 5000 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5001 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5002 | #ifdef MSG_ETAG |
| 5003 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5004 | #endif |
| 5005 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5006 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 5007 | #ifdef SOL_SOCKET |
| 5008 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5009 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5010 | #ifdef SOL_IP |
| 5011 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5012 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5013 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
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_IPX |
| 5016 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
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_AX25 |
| 5019 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
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_ATALK |
| 5022 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
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_NETROM |
| 5025 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
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_ROSE |
| 5028 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5029 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5030 | #ifdef SOL_TCP |
| 5031 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5032 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5033 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5034 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5035 | #ifdef SOL_UDP |
| 5036 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5037 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5038 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5039 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5040 | #ifdef IPPROTO_IP |
| 5041 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5042 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5043 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5044 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5045 | #ifdef IPPROTO_HOPOPTS |
| 5046 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5047 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5048 | #ifdef IPPROTO_ICMP |
| 5049 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5050 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5051 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
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_IGMP |
| 5054 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
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_GGP |
| 5057 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5058 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5059 | #ifdef IPPROTO_IPV4 |
| 5060 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5061 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5062 | #ifdef IPPROTO_IPV6 |
| 5063 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 5064 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5065 | #ifdef IPPROTO_IPIP |
| 5066 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5067 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5068 | #ifdef IPPROTO_TCP |
| 5069 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5070 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5071 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
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_EGP |
| 5074 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
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_PUP |
| 5077 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5078 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5079 | #ifdef IPPROTO_UDP |
| 5080 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5081 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5082 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
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_IDP |
| 5085 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
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_HELLO |
| 5088 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
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_ND |
| 5091 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
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_TP |
| 5094 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5095 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5096 | #ifdef IPPROTO_IPV6 |
| 5097 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
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_ROUTING |
| 5100 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
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_FRAGMENT |
| 5103 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
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_RSVP |
| 5106 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
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_GRE |
| 5109 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
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_ESP |
| 5112 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
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_AH |
| 5115 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
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_MOBILE |
| 5118 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
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_ICMPV6 |
| 5121 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
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_NONE |
| 5124 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
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_DSTOPTS |
| 5127 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5128 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5129 | #ifdef IPPROTO_XTP |
| 5130 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
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_EON |
| 5133 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5134 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5135 | #ifdef IPPROTO_PIM |
| 5136 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
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_IPCOMP |
| 5139 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
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_VRRP |
| 5142 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5143 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5144 | #ifdef IPPROTO_BIP |
| 5145 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5146 | #endif |
| 5147 | /**/ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5148 | #ifdef IPPROTO_RAW |
| 5149 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5150 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5151 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5152 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5153 | #ifdef IPPROTO_MAX |
| 5154 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5155 | #endif |
| 5156 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5157 | /* Some port configuration */ |
| 5158 | #ifdef IPPORT_RESERVED |
| 5159 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5160 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5161 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5162 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5163 | #ifdef IPPORT_USERRESERVED |
| 5164 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5165 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5166 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5167 | #endif |
| 5168 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5169 | /* Some reserved IP v.4 addresses */ |
| 5170 | #ifdef INADDR_ANY |
| 5171 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5172 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5173 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5174 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5175 | #ifdef INADDR_BROADCAST |
| 5176 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5177 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5178 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5179 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5180 | #ifdef INADDR_LOOPBACK |
| 5181 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5182 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5183 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5184 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5185 | #ifdef INADDR_UNSPEC_GROUP |
| 5186 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5187 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5188 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5189 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5190 | #ifdef INADDR_ALLHOSTS_GROUP |
| 5191 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 5192 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5193 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5194 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5195 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5196 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 5197 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 5198 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5199 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5200 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5201 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5202 | #ifdef INADDR_NONE |
| 5203 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5204 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5205 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5206 | #endif |
| 5207 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5208 | /* IPv4 [gs]etsockopt options */ |
| 5209 | #ifdef IP_OPTIONS |
| 5210 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
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_HDRINCL |
| 5213 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
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_TOS |
| 5216 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
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_TTL |
| 5219 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
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_RECVOPTS |
| 5222 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
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_RECVRETOPTS |
| 5225 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
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_RECVDSTADDR |
| 5228 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
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_RETOPTS |
| 5231 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
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_IF |
| 5234 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
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_TTL |
| 5237 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
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_MULTICAST_LOOP |
| 5240 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
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_ADD_MEMBERSHIP |
| 5243 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_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_DROP_MEMBERSHIP |
| 5246 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5247 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5248 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 5249 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 5250 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5251 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5252 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 5253 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 5254 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5255 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5256 | #ifdef IP_MAX_MEMBERSHIPS |
| 5257 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5258 | #endif |
| 5259 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5260 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 5261 | #ifdef IPV6_JOIN_GROUP |
| 5262 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_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_LEAVE_GROUP |
| 5265 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
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_HOPS |
| 5268 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
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_IF |
| 5271 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
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_MULTICAST_LOOP |
| 5274 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
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 | #ifdef IPV6_UNICAST_HOPS |
| 5277 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5278 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5279 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5280 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5281 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5282 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5283 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5284 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5285 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5286 | #endif |
| 5287 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5288 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5289 | #endif |
| 5290 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5291 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5292 | #endif |
| 5293 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5294 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5295 | #endif |
| 5296 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5297 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5298 | #endif |
| 5299 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5300 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5301 | #endif |
| 5302 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5303 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5304 | #endif |
| 5305 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5306 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5307 | #endif |
| 5308 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5309 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5310 | #endif |
| 5311 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5312 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5313 | #endif |
| 5314 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5315 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5316 | #endif |
| 5317 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5318 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5319 | #endif |
| 5320 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5321 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5322 | #endif |
| 5323 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5324 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5325 | #endif |
| 5326 | #ifdef IPV6_RTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5327 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5328 | #endif |
| 5329 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5330 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5331 | #endif |
| 5332 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5333 | 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] | 5334 | #endif |
| 5335 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5336 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5337 | #endif |
| 5338 | #ifdef IPV6_TCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5339 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5340 | #endif |
| 5341 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5342 | 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] | 5343 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5344 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5345 | /* TCP options */ |
| 5346 | #ifdef TCP_NODELAY |
| 5347 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
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_MAXSEG |
| 5350 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5351 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5352 | #ifdef TCP_CORK |
| 5353 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
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_KEEPIDLE |
| 5356 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
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_KEEPINTVL |
| 5359 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
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_KEEPCNT |
| 5362 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
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_SYNCNT |
| 5365 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
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_LINGER2 |
| 5368 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
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_DEFER_ACCEPT |
| 5371 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
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_WINDOW_CLAMP |
| 5374 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
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_INFO |
| 5377 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5378 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5379 | #ifdef TCP_QUICKACK |
| 5380 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5381 | #endif |
| 5382 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5383 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5384 | /* IPX options */ |
| 5385 | #ifdef IPX_TYPE |
| 5386 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5387 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5388 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5389 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5390 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5391 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5392 | #endif |
| 5393 | #ifdef EAI_AGAIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5394 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5395 | #endif |
| 5396 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5397 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5398 | #endif |
| 5399 | #ifdef EAI_FAIL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5400 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5401 | #endif |
| 5402 | #ifdef EAI_FAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5403 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5404 | #endif |
| 5405 | #ifdef EAI_MEMORY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5406 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5407 | #endif |
| 5408 | #ifdef EAI_NODATA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5409 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5410 | #endif |
| 5411 | #ifdef EAI_NONAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5412 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5413 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5414 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5415 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5416 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5417 | #ifdef EAI_SERVICE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5418 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5419 | #endif |
| 5420 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5421 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5422 | #endif |
| 5423 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5424 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5425 | #endif |
| 5426 | #ifdef EAI_BADHINTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5427 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5428 | #endif |
| 5429 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5430 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5431 | #endif |
| 5432 | #ifdef EAI_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5433 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5434 | #endif |
| 5435 | #ifdef AI_PASSIVE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5436 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5437 | #endif |
| 5438 | #ifdef AI_CANONNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5439 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5440 | #endif |
| 5441 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5442 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5443 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5444 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5445 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5446 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5447 | #ifdef AI_MASK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5448 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5449 | #endif |
| 5450 | #ifdef AI_ALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5451 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5452 | #endif |
| 5453 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5454 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5455 | #endif |
| 5456 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5457 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5458 | #endif |
| 5459 | #ifdef AI_V4MAPPED |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5460 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5461 | #endif |
| 5462 | #ifdef AI_DEFAULT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5463 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5464 | #endif |
| 5465 | #ifdef NI_MAXHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5466 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5467 | #endif |
| 5468 | #ifdef NI_MAXSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5469 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5470 | #endif |
| 5471 | #ifdef NI_NOFQDN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5472 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5473 | #endif |
| 5474 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5475 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5476 | #endif |
| 5477 | #ifdef NI_NAMEREQD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5478 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5479 | #endif |
| 5480 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5481 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5482 | #endif |
| 5483 | #ifdef NI_DGRAM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5484 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5485 | #endif |
| 5486 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5487 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5488 | #ifdef SHUT_RD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5489 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5490 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5491 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5492 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5493 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5494 | #endif |
| 5495 | #ifdef SHUT_WR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5496 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5497 | #elif defined(SD_SEND) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5498 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5499 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5500 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5501 | #endif |
| 5502 | #ifdef SHUT_RDWR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5503 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5504 | #elif defined(SD_BOTH) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5505 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5506 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5507 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5508 | #endif |
| 5509 | |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5510 | #ifdef SIO_RCVALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5511 | { |
| 5512 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 5513 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 5514 | int i; |
| 5515 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) { |
| 5516 | PyObject *tmp; |
| 5517 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 5518 | if (tmp == NULL) |
| 5519 | return; |
| 5520 | PyModule_AddObject(m, names[i], tmp); |
| 5521 | } |
| 5522 | } |
| 5523 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5524 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5525 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5526 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5527 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5528 | #endif |
| 5529 | #ifdef RCVALL_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5530 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5531 | #endif |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5532 | #endif /* _MSTCPIP_ */ |
| 5533 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5534 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5535 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5536 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5537 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5538 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5539 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5540 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5541 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5542 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5543 | |
| 5544 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5545 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5546 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5547 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5548 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5549 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5550 | if (af == AF_INET) { |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 5551 | #if (SIZEOF_INT != 4) |
| 5552 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5553 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5554 | unsigned int packed_addr; |
| 5555 | packed_addr = inet_addr(src); |
| 5556 | if (packed_addr == INADDR_NONE) |
| 5557 | return 0; |
| 5558 | memcpy(dst, &packed_addr, 4); |
| 5559 | return 1; |
| 5560 | } |
| 5561 | /* Should set errno to EAFNOSUPPORT */ |
| 5562 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5565 | const char * |
| 5566 | 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] | 5567 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5568 | if (af == AF_INET) { |
| 5569 | struct in_addr packed_addr; |
| 5570 | if (size < 16) |
| 5571 | /* Should set errno to ENOSPC. */ |
| 5572 | return NULL; |
| 5573 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5574 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5575 | } |
| 5576 | /* Should set errno to EAFNOSUPPORT */ |
| 5577 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5578 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5579 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5580 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5581 | #endif |