Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1 | /* Socket module */ |
| 2 | |
| 3 | /* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5 | This module provides an interface to Berkeley socket IPC. |
| 6 | |
| 7 | Limitations: |
| 8 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 9 | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 10 | portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported |
| 11 | under Linux. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 12 | - No read/write operations (use sendall/recv or makefile instead). |
| 13 | - Additional restrictions apply on some non-Unix platforms (compensated |
| 14 | for by socket.py). |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 16 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 18 | - socket.error: exception raised for socket specific errors |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 19 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +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 | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 22 | a subclass of socket.error |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 23 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 24 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 25 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 26 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 27 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 28 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 29 | - socket.socket([family[, type [, proto, fileno]]]) --> new socket object |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 30 | (fileno specifies a pre-existing socket file descriptor) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 31 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 32 | - socket.ntohs(16 bit value) --> new int object |
| 33 | - socket.ntohl(32 bit value) --> new int object |
| 34 | - socket.htons(16 bit value) --> new int object |
| 35 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 36 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 37 | --> List of (family, socktype, proto, canonname, sockaddr) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 38 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 39 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 40 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 41 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 42 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 43 | - socket.getdefaulttimeout() -> None | float |
| 44 | - socket.setdefaulttimeout(None | float) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 45 | - an Internet socket address is a pair (hostname, port) |
| 46 | where hostname can be anything recognized by gethostbyname() |
| 47 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 48 | - where a hostname is returned, the dd.dd.dd.dd notation is used |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 49 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 50 | - an AF_PACKET socket address is a tuple containing a string |
| 51 | specifying the ethernet interface and an integer specifying |
| 52 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 53 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 54 | specify packet-type and ha-type/addr. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 55 | - an AF_TIPC socket address is expressed as |
| 56 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 57 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 58 | and scope can be one of: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 59 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 60 | The meaning of v1, v2 and v3 depends on the value of addr_type: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 61 | if addr_type is TIPC_ADDR_NAME: |
| 62 | v1 is the server type |
| 63 | v2 is the port identifier |
| 64 | v3 is ignored |
| 65 | if addr_type is TIPC_ADDR_NAMESEQ: |
| 66 | v1 is the server type |
| 67 | v2 is the lower port number |
| 68 | v3 is the upper port number |
| 69 | if addr_type is TIPC_ADDR_ID: |
| 70 | v1 is the node |
| 71 | v2 is the ref |
| 72 | v3 is ignored |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 75 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 77 | - names starting with sock_ are socket object methods |
| 78 | - names starting with socket_ are module-level functions |
| 79 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 81 | */ |
| 82 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 83 | #ifdef __APPLE__ |
| 84 | /* |
| 85 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 86 | * that was build on 10.4 or later to work on that release, weak linking |
| 87 | * comes to the rescue. |
| 88 | */ |
| 89 | # pragma weak inet_aton |
| 90 | #endif |
| 91 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 92 | #include "Python.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 95 | #undef MAX |
| 96 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 97 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 98 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 99 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 100 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 101 | \n\ |
| 102 | Open a socket of the given type. The family argument specifies the\n\ |
| 103 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 104 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 105 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 106 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 107 | \n\ |
| 108 | A socket object represents one endpoint of a network connection.\n\ |
| 109 | \n\ |
| 110 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 111 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 112 | _accept() -- accept connection, returning new socket fd and client address\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 113 | bind(addr) -- bind the socket to a local address\n\ |
| 114 | close() -- close the socket\n\ |
| 115 | connect(addr) -- connect the socket to a remote address\n\ |
| 116 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 117 | _dup() -- return a new socket fd duplicated from fileno()\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 118 | fileno() -- return underlying file descriptor\n\ |
| 119 | getpeername() -- return remote address [*]\n\ |
| 120 | getsockname() -- return local address\n\ |
| 121 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 122 | gettimeout() -- return timeout or None\n\ |
| 123 | listen(n) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 124 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 125 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 126 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 127 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 128 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 129 | sendall(data[, flags]) -- send all data\n\ |
| 130 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 131 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 132 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 133 | setsockopt(level, optname, value) -- set socket options\n\ |
| 134 | settimeout(None | float) -- set or clear the timeout\n\ |
| 135 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 136 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 137 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 138 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 139 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 140 | I hope some day someone can clean this up please... */ |
| 141 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 142 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 143 | script doesn't get this right, so we hardcode some platform checks below. |
| 144 | On the other hand, not all Linux versions agree, so there the settings |
| 145 | computed by the configure script are needed! */ |
| 146 | |
| 147 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 148 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 149 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 150 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 151 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 153 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 154 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 155 | #endif |
| 156 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 157 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 158 | # if defined(_AIX) || defined(__osf__) |
| 159 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 160 | # elif defined(__sun) || defined(__sgi) |
| 161 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 162 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 163 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 164 | # else |
| 165 | # undef HAVE_GETHOSTBYNAME_R |
| 166 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 169 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 170 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 171 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 172 | #endif |
| 173 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 174 | /* To use __FreeBSD_version */ |
| 175 | #ifdef HAVE_SYS_PARAM_H |
| 176 | #include <sys/param.h> |
| 177 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 178 | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
Just van Rossum | 09aecd7 | 2003-05-09 08:03:44 +0000 | [diff] [blame] | 179 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 180 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 181 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 182 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 183 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 184 | #define USE_GETADDRINFO_LOCK |
| 185 | #endif |
| 186 | |
| 187 | #ifdef USE_GETADDRINFO_LOCK |
| 188 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 189 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 190 | #else |
| 191 | #define ACQUIRE_GETADDRINFO_LOCK |
| 192 | #define RELEASE_GETADDRINFO_LOCK |
| 193 | #endif |
| 194 | |
| 195 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 196 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 197 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 199 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 200 | # include <types.h> |
| 201 | # include <io.h> |
| 202 | # include <sys/ioctl.h> |
| 203 | # include <utils.h> |
| 204 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 205 | #endif |
| 206 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 207 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 208 | # include <ioctl.h> |
| 209 | #endif |
| 210 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 211 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 212 | # define INCL_DOS |
| 213 | # define INCL_DOSERRORS |
| 214 | # define INCL_NOPMAPI |
| 215 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 216 | #endif |
| 217 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 218 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 219 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 220 | functions are declared correctly if compiling with |
| 221 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 222 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 223 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 224 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 225 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 226 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 227 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 228 | #undef _XOPEN_SOURCE |
| 229 | #include <sys/socket.h> |
| 230 | #include <sys/types.h> |
| 231 | #include <netinet/in.h> |
| 232 | #ifdef _SS_ALIGNSIZE |
| 233 | #define HAVE_GETADDRINFO 1 |
| 234 | #define HAVE_GETNAMEINFO 1 |
| 235 | #endif |
| 236 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 237 | #define HAVE_INET_PTON |
| 238 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 239 | #endif |
| 240 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 241 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 242 | for both GCC and SGI's compiler. I'd say that the SGI headers |
Georg Brandl | dbd8339 | 2006-02-20 09:42:33 +0000 | [diff] [blame] | 243 | are just busted. Same thing for Solaris. */ |
| 244 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 245 | #define INET_ADDRSTRLEN 16 |
| 246 | #endif |
| 247 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 248 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 249 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 252 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 253 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 254 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 255 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 256 | |
| 257 | /* Addressing includes */ |
| 258 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 259 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 260 | |
| 261 | /* Non-MS WINDOWS includes */ |
| 262 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 264 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Skip Montanaro | eb33e5a | 2007-08-17 12:57:41 +0000 | [diff] [blame] | 265 | # if defined(PYOS_OS2) && defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 266 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 267 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 268 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 269 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 270 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 271 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 272 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 274 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 275 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 276 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 277 | # ifdef HAVE_FCNTL_H |
| 278 | # include <fcntl.h> |
| 279 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 280 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 281 | #endif |
| 282 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 283 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 284 | |
| 285 | #ifndef offsetof |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 286 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 287 | #endif |
| 288 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 289 | #ifndef O_NONBLOCK |
| 290 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 291 | #endif |
| 292 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 293 | /* include Python's addrinfo.h unless it causes trouble */ |
| 294 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 295 | /* Do not include addinfo.h on some newer IRIX versions. |
| 296 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 297 | * for example, but not by 6.5.10. |
| 298 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 299 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 300 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 301 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 302 | */ |
| 303 | #else |
| 304 | # include "addrinfo.h" |
| 305 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 306 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 307 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 308 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 309 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 310 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 311 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 312 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 313 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 314 | #ifdef __APPLE__ |
| 315 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 316 | failure, so we must use the emulation instead of the libinfo |
| 317 | implementation. Unfortunately, performing an autoconf test |
| 318 | for this bug would require DNS access for the machine performing |
| 319 | the configuration, which is not acceptable. Therefore, we |
| 320 | determine the bug just by checking for __APPLE__. If this bug |
| 321 | gets ever fixed, perhaps checking for sys/version.h would be |
| 322 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 323 | #ifndef HAVE_GETNAMEINFO |
| 324 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 325 | Find to check for Jaguar is that it has getnameinfo(), which |
| 326 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 327 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 328 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 329 | |
| 330 | #ifdef HAVE_INET_ATON |
| 331 | #define USE_INET_ATON_WEAKLINK |
| 332 | #endif |
| 333 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 334 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 335 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 336 | /* I know this is a bad practice, but it is the easiest... */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 337 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 338 | /* avoid clashes with the C library definition of the symbol. */ |
| 339 | #define getaddrinfo fake_getaddrinfo |
| 340 | #define gai_strerror fake_gai_strerror |
| 341 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 342 | #include "getaddrinfo.c" |
| 343 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 344 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 345 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 346 | #include "getnameinfo.c" |
| 347 | #endif |
| 348 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 349 | #ifdef MS_WINDOWS |
| 350 | /* On Windows a socket is really a handle not an fd */ |
| 351 | static SOCKET |
| 352 | dup_socket(SOCKET handle) |
| 353 | { |
Daniel Stutzbach | ee541e0 | 2010-09-03 12:42:06 +0000 | [diff] [blame] | 354 | WSAPROTOCOL_INFO info; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 355 | |
Daniel Stutzbach | ee541e0 | 2010-09-03 12:42:06 +0000 | [diff] [blame] | 356 | if (WSADuplicateSocket(handle, GetCurrentProcessId(), &info)) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 357 | return INVALID_SOCKET; |
Daniel Stutzbach | ee541e0 | 2010-09-03 12:42:06 +0000 | [diff] [blame] | 358 | |
| 359 | return WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
| 360 | FROM_PROTOCOL_INFO, &info, 0, 0); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 361 | } |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 362 | #define SOCKETCLOSE closesocket |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 363 | #else |
| 364 | /* On Unix we can use dup to duplicate the file descriptor of a socket*/ |
| 365 | #define dup_socket(fd) dup(fd) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 366 | #endif |
| 367 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 368 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 369 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 370 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 371 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 372 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 373 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 374 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 375 | #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 376 | #endif |
| 377 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 378 | #ifndef SOCKETCLOSE |
| 379 | #define SOCKETCLOSE close |
| 380 | #endif |
| 381 | |
Jesse Noller | 32d68c2 | 2009-03-31 18:48:42 +0000 | [diff] [blame] | 382 | #if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) && !defined(__NetBSD__) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 383 | #define USE_BLUETOOTH 1 |
| 384 | #if defined(__FreeBSD__) |
| 385 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 386 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 387 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 388 | #define SOL_HCI SOL_HCI_RAW |
| 389 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 390 | #define sockaddr_l2 sockaddr_l2cap |
| 391 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 392 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 393 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 394 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 395 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 396 | #elif defined(__NetBSD__) |
| 397 | #define sockaddr_l2 sockaddr_bt |
| 398 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 399 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 400 | #define sockaddr_sco sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 401 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 402 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 403 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 404 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 405 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 406 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 407 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 408 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 409 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 410 | #endif |
| 411 | #endif |
| 412 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 413 | #ifdef __VMS |
| 414 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 415 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 416 | #endif |
| 417 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 418 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 419 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 420 | /* |
| 421 | * Constants for getnameinfo() |
| 422 | */ |
| 423 | #if !defined(NI_MAXHOST) |
| 424 | #define NI_MAXHOST 1025 |
| 425 | #endif |
| 426 | #if !defined(NI_MAXSERV) |
| 427 | #define NI_MAXSERV 32 |
| 428 | #endif |
| 429 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 430 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 431 | #define INVALID_SOCKET (-1) |
| 432 | #endif |
| 433 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 434 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 435 | a Py prefix (or use CapitalizedWords). Later... */ |
| 436 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 437 | /* Global variable holding the exception type for errors detected |
| 438 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 439 | static PyObject *socket_error; |
| 440 | static PyObject *socket_herror; |
| 441 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 442 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 443 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 444 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 445 | The sock_type variable contains pointers to various functions, |
| 446 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 447 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 448 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 449 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 450 | #if defined(HAVE_POLL_H) |
| 451 | #include <poll.h> |
| 452 | #elif defined(HAVE_SYS_POLL_H) |
| 453 | #include <sys/poll.h> |
| 454 | #endif |
| 455 | |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 456 | #ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
| 457 | /* Platform can select file descriptors beyond FD_SETSIZE */ |
| 458 | #define IS_SELECTABLE(s) 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 459 | #elif defined(HAVE_POLL) |
| 460 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 461 | #define IS_SELECTABLE(s) 1 |
| 462 | /* Can we call select() with this socket without a buffer overrun? */ |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 463 | #else |
| 464 | /* POSIX says selecting file descriptors beyond FD_SETSIZE |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 465 | has undefined behaviour. If there's no timeout left, we don't have to |
| 466 | call select, so it's a safe, little white lie. */ |
| 467 | #define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 468 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 469 | |
| 470 | static PyObject* |
| 471 | select_error(void) |
| 472 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 473 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 474 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 477 | /* Convenience function to raise an error according to errno |
| 478 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 479 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 480 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 481 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 482 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 483 | #ifdef MS_WINDOWS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 484 | int err_no = WSAGetLastError(); |
| 485 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 486 | recognizes the error codes used by both GetLastError() and |
| 487 | WSAGetLastError */ |
| 488 | if (err_no) |
| 489 | return PyErr_SetExcFromWindowsErr(socket_error, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 490 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 491 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 492 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 493 | if (sock_errno() != NO_ERROR) { |
| 494 | APIRET rc; |
| 495 | ULONG msglen; |
| 496 | char outbuf[100]; |
| 497 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 499 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 500 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 501 | myerrorcode - SOCBASEERR + 26, |
| 502 | "mptn.msg", |
| 503 | &msglen); |
| 504 | if (rc == NO_ERROR) { |
| 505 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 507 | /* OS/2 doesn't guarantee a terminator */ |
| 508 | outbuf[msglen] = '\0'; |
| 509 | if (strlen(outbuf) > 0) { |
| 510 | /* If non-empty msg, trim CRLF */ |
| 511 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 512 | while (lastc > outbuf && |
| 513 | isspace(Py_CHARMASK(*lastc))) { |
| 514 | /* Trim trailing whitespace (CRLF) */ |
| 515 | *lastc-- = '\0'; |
| 516 | } |
| 517 | } |
| 518 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 519 | if (v != NULL) { |
| 520 | PyErr_SetObject(socket_error, v); |
| 521 | Py_DECREF(v); |
| 522 | } |
| 523 | return NULL; |
| 524 | } |
| 525 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 526 | #endif |
| 527 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 528 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 531 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 532 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 533 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 534 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 535 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 536 | |
| 537 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 538 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 539 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 540 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 541 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 542 | if (v != NULL) { |
| 543 | PyErr_SetObject(socket_herror, v); |
| 544 | Py_DECREF(v); |
| 545 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 547 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 552 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 553 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 554 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 555 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 556 | #ifdef EAI_SYSTEM |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 557 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 558 | if (error == EAI_SYSTEM) |
| 559 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 560 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 561 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 562 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 563 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 564 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 565 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 566 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 567 | if (v != NULL) { |
| 568 | PyErr_SetObject(socket_gaierror, v); |
| 569 | Py_DECREF(v); |
| 570 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 571 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 572 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 575 | #ifdef __VMS |
| 576 | /* Function to send in segments */ |
| 577 | static int |
| 578 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 579 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 580 | int n = 0; |
| 581 | int remaining = len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 583 | while (remaining > 0) { |
| 584 | unsigned int segment; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 586 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 587 | n = send(sock_fd, buf, segment, flags); |
| 588 | if (n < 0) { |
| 589 | return n; |
| 590 | } |
| 591 | remaining -= segment; |
| 592 | buf += segment; |
| 593 | } /* end while */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 595 | return len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 596 | } |
| 597 | #endif |
| 598 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 599 | /* Function to perform the setting of socket blocking mode |
| 600 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 601 | static int |
| 602 | internal_setblocking(PySocketSockObject *s, int block) |
| 603 | { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 604 | #ifndef MS_WINDOWS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 605 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 606 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 608 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 609 | #ifndef MS_WINDOWS |
| 610 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 611 | block = !block; |
| 612 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 613 | #elif defined(__VMS) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 614 | block = !block; |
| 615 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 616 | #else /* !PYOS_OS2 && !__VMS */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 617 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 618 | if (block) |
| 619 | delay_flag &= (~O_NONBLOCK); |
| 620 | else |
| 621 | delay_flag |= O_NONBLOCK; |
| 622 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 623 | #endif /* !PYOS_OS2 */ |
| 624 | #else /* MS_WINDOWS */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 625 | block = !block; |
| 626 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 627 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 628 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 630 | /* Since these don't return anything */ |
| 631 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 634 | /* 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] | 635 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 636 | This does not raise an exception; we'll let our caller do that |
| 637 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 638 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 639 | static int |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 640 | internal_select(PySocketSockObject *s, int writing) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 641 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 642 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 644 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 645 | if (s->sock_timeout <= 0.0) |
| 646 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 647 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 648 | /* Guard against closed socket */ |
| 649 | if (s->sock_fd < 0) |
| 650 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 652 | /* Prefer poll, if available, since you can poll() any fd |
| 653 | * which can't be done with select(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 654 | #ifdef HAVE_POLL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 655 | { |
| 656 | struct pollfd pollfd; |
| 657 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 659 | pollfd.fd = s->sock_fd; |
| 660 | pollfd.events = writing ? POLLOUT : POLLIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 662 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 663 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 664 | n = poll(&pollfd, 1, timeout); |
| 665 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 666 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 667 | { |
| 668 | /* Construct the arguments to select */ |
| 669 | fd_set fds; |
| 670 | struct timeval tv; |
| 671 | tv.tv_sec = (int)s->sock_timeout; |
| 672 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 673 | FD_ZERO(&fds); |
| 674 | FD_SET(s->sock_fd, &fds); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 676 | /* See if the socket is ready */ |
| 677 | if (writing) |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 678 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 679 | NULL, &fds, NULL, &tv); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 680 | else |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 681 | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 682 | &fds, NULL, NULL, &tv); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 683 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 684 | #endif |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 686 | if (n < 0) |
| 687 | return -1; |
| 688 | if (n == 0) |
| 689 | return 1; |
| 690 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 693 | /* Initialize a new socket object. */ |
| 694 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 695 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 696 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 697 | static void |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 698 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 699 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 700 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 701 | s->sock_fd = fd; |
| 702 | s->sock_family = family; |
| 703 | s->sock_type = type; |
| 704 | s->sock_proto = proto; |
| 705 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 707 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 708 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 709 | if (defaulttimeout >= 0.0) |
| 710 | internal_setblocking(s, 0); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 711 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 715 | /* Create a new socket object. |
| 716 | This just creates the object and initializes it. |
| 717 | If the creation fails, return NULL and set an exception (implicit |
| 718 | in NEWOBJ()). */ |
| 719 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 720 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 721 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 722 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 723 | PySocketSockObject *s; |
| 724 | s = (PySocketSockObject *) |
| 725 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 726 | if (s != NULL) |
| 727 | init_sockobject(s, fd, family, type, proto); |
| 728 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 731 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 732 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 733 | thread to be in gethostbyname or getaddrinfo */ |
| 734 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 735 | PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 736 | #endif |
| 737 | |
| 738 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 739 | /* Convert a string specifying a host name or one of a few symbolic |
| 740 | names to a numeric IP address. This usually calls gethostbyname() |
| 741 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 742 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 743 | an error occurred; then an exception is raised. */ |
| 744 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 745 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 746 | 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] | 747 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 748 | struct addrinfo hints, *res; |
| 749 | int error; |
| 750 | int d1, d2, d3, d4; |
| 751 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 752 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 753 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 754 | if (name[0] == '\0') { |
| 755 | int siz; |
| 756 | memset(&hints, 0, sizeof(hints)); |
| 757 | hints.ai_family = af; |
| 758 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 759 | hints.ai_flags = AI_PASSIVE; |
| 760 | Py_BEGIN_ALLOW_THREADS |
| 761 | ACQUIRE_GETADDRINFO_LOCK |
| 762 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 763 | Py_END_ALLOW_THREADS |
| 764 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 765 | *are* safe regarding their return value, ie. that a |
| 766 | subsequent call to getaddrinfo() does not destroy the |
| 767 | outcome of the first call. */ |
| 768 | RELEASE_GETADDRINFO_LOCK |
| 769 | if (error) { |
| 770 | set_gaierror(error); |
| 771 | return -1; |
| 772 | } |
| 773 | switch (res->ai_family) { |
| 774 | case AF_INET: |
| 775 | siz = 4; |
| 776 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 777 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 778 | case AF_INET6: |
| 779 | siz = 16; |
| 780 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 781 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 782 | default: |
| 783 | freeaddrinfo(res); |
| 784 | PyErr_SetString(socket_error, |
| 785 | "unsupported address family"); |
| 786 | return -1; |
| 787 | } |
| 788 | if (res->ai_next) { |
| 789 | freeaddrinfo(res); |
| 790 | PyErr_SetString(socket_error, |
| 791 | "wildcard resolved to multiple address"); |
| 792 | return -1; |
| 793 | } |
| 794 | if (res->ai_addrlen < addr_ret_size) |
| 795 | addr_ret_size = res->ai_addrlen; |
| 796 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 797 | freeaddrinfo(res); |
| 798 | return siz; |
| 799 | } |
| 800 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 801 | struct sockaddr_in *sin; |
| 802 | if (af != AF_INET && af != AF_UNSPEC) { |
| 803 | PyErr_SetString(socket_error, |
| 804 | "address family mismatched"); |
| 805 | return -1; |
| 806 | } |
| 807 | sin = (struct sockaddr_in *)addr_ret; |
| 808 | memset((void *) sin, '\0', sizeof(*sin)); |
| 809 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 810 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 811 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 812 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 813 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 814 | return sizeof(sin->sin_addr); |
| 815 | } |
| 816 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 817 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 818 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 819 | struct sockaddr_in *sin; |
| 820 | sin = (struct sockaddr_in *)addr_ret; |
| 821 | sin->sin_addr.s_addr = htonl( |
| 822 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 823 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 824 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 825 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 826 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 827 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 828 | return 4; |
| 829 | } |
| 830 | memset(&hints, 0, sizeof(hints)); |
| 831 | hints.ai_family = af; |
| 832 | Py_BEGIN_ALLOW_THREADS |
| 833 | ACQUIRE_GETADDRINFO_LOCK |
| 834 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 835 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 836 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 837 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 838 | if no address family is given. Assume IPv4 for now.*/ |
| 839 | hints.ai_family = AF_INET; |
| 840 | error = getaddrinfo(name, NULL, &hints, &res); |
| 841 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 842 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 843 | Py_END_ALLOW_THREADS |
| 844 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 845 | if (error) { |
| 846 | set_gaierror(error); |
| 847 | return -1; |
| 848 | } |
| 849 | if (res->ai_addrlen < addr_ret_size) |
| 850 | addr_ret_size = res->ai_addrlen; |
| 851 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 852 | freeaddrinfo(res); |
| 853 | switch (addr_ret->sa_family) { |
| 854 | case AF_INET: |
| 855 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 856 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 857 | case AF_INET6: |
| 858 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 859 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 860 | default: |
| 861 | PyErr_SetString(socket_error, "unknown address family"); |
| 862 | return -1; |
| 863 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 867 | /* Create a string object representing an IP address. |
| 868 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 869 | size numbers). */ |
| 870 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 871 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 872 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 873 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 874 | char buf[NI_MAXHOST]; |
| 875 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 876 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 877 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 878 | NI_NUMERICHOST); |
| 879 | if (error) { |
| 880 | set_gaierror(error); |
| 881 | return NULL; |
| 882 | } |
| 883 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 887 | #ifdef USE_BLUETOOTH |
| 888 | /* Convert a string representation of a Bluetooth address into a numeric |
| 889 | address. Returns the length (6), or raises an exception and returns -1 if |
| 890 | an error occurred. */ |
| 891 | |
| 892 | static int |
| 893 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 894 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 895 | unsigned int b0, b1, b2, b3, b4, b5; |
| 896 | char ch; |
| 897 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 899 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 900 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 901 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 902 | bdaddr->b[0] = b0; |
| 903 | bdaddr->b[1] = b1; |
| 904 | bdaddr->b[2] = b2; |
| 905 | bdaddr->b[3] = b3; |
| 906 | bdaddr->b[4] = b4; |
| 907 | bdaddr->b[5] = b5; |
| 908 | return 6; |
| 909 | } else { |
| 910 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 911 | return -1; |
| 912 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /* Create a string representation of the Bluetooth address. This is always a |
| 916 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 917 | value (zero padded if necessary). */ |
| 918 | |
| 919 | static PyObject * |
| 920 | makebdaddr(bdaddr_t *bdaddr) |
| 921 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 922 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 924 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 925 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 926 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 927 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 928 | } |
| 929 | #endif |
| 930 | |
| 931 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 932 | /* Create an object representing the given socket address, |
| 933 | suitable for passing it back to bind(), connect() etc. |
| 934 | The family field of the sockaddr structure is inspected |
| 935 | to determine what kind of address it really is. */ |
| 936 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 937 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 938 | static PyObject * |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 939 | makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 940 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 941 | if (addrlen == 0) { |
| 942 | /* No address -- may be recvfrom() from known socket */ |
| 943 | Py_INCREF(Py_None); |
| 944 | return Py_None; |
| 945 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 947 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 949 | case AF_INET: |
| 950 | { |
| 951 | struct sockaddr_in *a; |
| 952 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 953 | PyObject *ret = NULL; |
| 954 | if (addrobj) { |
| 955 | a = (struct sockaddr_in *)addr; |
| 956 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 957 | Py_DECREF(addrobj); |
| 958 | } |
| 959 | return ret; |
| 960 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 961 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 962 | #if defined(AF_UNIX) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 963 | case AF_UNIX: |
| 964 | { |
| 965 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 966 | #ifdef linux |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 967 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 968 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 969 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
| 970 | } |
| 971 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 972 | #endif /* linux */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 973 | { |
| 974 | /* regular NULL-terminated string */ |
| 975 | return PyUnicode_FromString(a->sun_path); |
| 976 | } |
| 977 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 978 | #endif /* AF_UNIX */ |
| 979 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 980 | #if defined(AF_NETLINK) |
| 981 | case AF_NETLINK: |
| 982 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 983 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 984 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 985 | } |
| 986 | #endif /* AF_NETLINK */ |
| 987 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 988 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 989 | case AF_INET6: |
| 990 | { |
| 991 | struct sockaddr_in6 *a; |
| 992 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 993 | PyObject *ret = NULL; |
| 994 | if (addrobj) { |
| 995 | a = (struct sockaddr_in6 *)addr; |
| 996 | ret = Py_BuildValue("Oiii", |
| 997 | addrobj, |
| 998 | ntohs(a->sin6_port), |
| 999 | a->sin6_flowinfo, |
| 1000 | a->sin6_scope_id); |
| 1001 | Py_DECREF(addrobj); |
| 1002 | } |
| 1003 | return ret; |
| 1004 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1005 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1006 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1007 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1008 | case AF_BLUETOOTH: |
| 1009 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1011 | case BTPROTO_L2CAP: |
| 1012 | { |
| 1013 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1014 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1015 | PyObject *ret = NULL; |
| 1016 | if (addrobj) { |
| 1017 | ret = Py_BuildValue("Oi", |
| 1018 | addrobj, |
| 1019 | _BT_L2_MEMB(a, psm)); |
| 1020 | Py_DECREF(addrobj); |
| 1021 | } |
| 1022 | return ret; |
| 1023 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1025 | case BTPROTO_RFCOMM: |
| 1026 | { |
| 1027 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1028 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1029 | PyObject *ret = NULL; |
| 1030 | if (addrobj) { |
| 1031 | ret = Py_BuildValue("Oi", |
| 1032 | addrobj, |
| 1033 | _BT_RC_MEMB(a, channel)); |
| 1034 | Py_DECREF(addrobj); |
| 1035 | } |
| 1036 | return ret; |
| 1037 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1039 | case BTPROTO_HCI: |
| 1040 | { |
| 1041 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
| 1042 | PyObject *ret = NULL; |
| 1043 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1044 | return ret; |
| 1045 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1046 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1047 | #if !defined(__FreeBSD__) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1048 | case BTPROTO_SCO: |
| 1049 | { |
| 1050 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1051 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1052 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1053 | #endif |
| 1054 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1055 | default: |
| 1056 | PyErr_SetString(PyExc_ValueError, |
| 1057 | "Unknown Bluetooth protocol"); |
| 1058 | return NULL; |
| 1059 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1060 | #endif |
| 1061 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1062 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1063 | case AF_PACKET: |
| 1064 | { |
| 1065 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1066 | char *ifname = ""; |
| 1067 | struct ifreq ifr; |
| 1068 | /* need to look up interface name give index */ |
| 1069 | if (a->sll_ifindex) { |
| 1070 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1071 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1072 | ifname = ifr.ifr_name; |
| 1073 | } |
| 1074 | return Py_BuildValue("shbhy#", |
| 1075 | ifname, |
| 1076 | ntohs(a->sll_protocol), |
| 1077 | a->sll_pkttype, |
| 1078 | a->sll_hatype, |
| 1079 | a->sll_addr, |
| 1080 | a->sll_halen); |
| 1081 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1082 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1083 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1084 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1085 | case AF_TIPC: |
| 1086 | { |
| 1087 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1088 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1089 | return Py_BuildValue("IIIII", |
| 1090 | a->addrtype, |
| 1091 | a->addr.nameseq.type, |
| 1092 | a->addr.nameseq.lower, |
| 1093 | a->addr.nameseq.upper, |
| 1094 | a->scope); |
| 1095 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1096 | return Py_BuildValue("IIIII", |
| 1097 | a->addrtype, |
| 1098 | a->addr.name.name.type, |
| 1099 | a->addr.name.name.instance, |
| 1100 | a->addr.name.name.instance, |
| 1101 | a->scope); |
| 1102 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1103 | return Py_BuildValue("IIIII", |
| 1104 | a->addrtype, |
| 1105 | a->addr.id.node, |
| 1106 | a->addr.id.ref, |
| 1107 | 0, |
| 1108 | a->scope); |
| 1109 | } else { |
| 1110 | PyErr_SetString(PyExc_ValueError, |
| 1111 | "Invalid address type"); |
| 1112 | return NULL; |
| 1113 | } |
| 1114 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1115 | #endif |
| 1116 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1117 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1118 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1119 | default: |
| 1120 | /* If we don't know the address family, don't raise an |
| 1121 | exception -- return it as an (int, bytes) tuple. */ |
| 1122 | return Py_BuildValue("iy#", |
| 1123 | addr->sa_family, |
| 1124 | addr->sa_data, |
| 1125 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1126 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1127 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* Parse a socket address argument according to the socket object's |
| 1132 | address family. Return 1 if the address was in the proper format, |
| 1133 | 0 of not. The address is returned through addr_ret, its length |
| 1134 | through len_ret. */ |
| 1135 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1136 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1137 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1138 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1139 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1140 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1141 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1142 | #if defined(AF_UNIX) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1143 | case AF_UNIX: |
| 1144 | { |
| 1145 | struct sockaddr_un* addr; |
| 1146 | char *path; |
| 1147 | int len; |
| 1148 | if (!PyArg_Parse(args, "s#", &path, &len)) |
| 1149 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1150 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1151 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1152 | #ifdef linux |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1153 | if (len > 0 && path[0] == 0) { |
| 1154 | /* Linux abstract namespace extension */ |
| 1155 | if (len > sizeof addr->sun_path) { |
| 1156 | PyErr_SetString(socket_error, |
| 1157 | "AF_UNIX path too long"); |
| 1158 | return 0; |
| 1159 | } |
| 1160 | } |
| 1161 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1162 | #endif /* linux */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1163 | { |
| 1164 | /* regular NULL-terminated string */ |
| 1165 | if (len >= sizeof addr->sun_path) { |
| 1166 | PyErr_SetString(socket_error, |
| 1167 | "AF_UNIX path too long"); |
| 1168 | return 0; |
| 1169 | } |
| 1170 | addr->sun_path[len] = 0; |
| 1171 | } |
| 1172 | addr->sun_family = s->sock_family; |
| 1173 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1174 | #if defined(PYOS_OS2) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1175 | *len_ret = sizeof(*addr); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1176 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1177 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1178 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1179 | return 1; |
| 1180 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1181 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1182 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1183 | #if defined(AF_NETLINK) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1184 | case AF_NETLINK: |
| 1185 | { |
| 1186 | struct sockaddr_nl* addr; |
| 1187 | int pid, groups; |
| 1188 | addr = (struct sockaddr_nl *)addr_ret; |
| 1189 | if (!PyTuple_Check(args)) { |
| 1190 | PyErr_Format( |
| 1191 | PyExc_TypeError, |
| 1192 | "getsockaddrarg: " |
| 1193 | "AF_NETLINK address must be tuple, not %.500s", |
| 1194 | Py_TYPE(args)->tp_name); |
| 1195 | return 0; |
| 1196 | } |
| 1197 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1198 | return 0; |
| 1199 | addr->nl_family = AF_NETLINK; |
| 1200 | addr->nl_pid = pid; |
| 1201 | addr->nl_groups = groups; |
| 1202 | *len_ret = sizeof(*addr); |
| 1203 | return 1; |
| 1204 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1205 | #endif |
| 1206 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1207 | case AF_INET: |
| 1208 | { |
| 1209 | struct sockaddr_in* addr; |
| 1210 | char *host; |
| 1211 | int port, result; |
| 1212 | if (!PyTuple_Check(args)) { |
| 1213 | PyErr_Format( |
| 1214 | PyExc_TypeError, |
| 1215 | "getsockaddrarg: " |
| 1216 | "AF_INET address must be tuple, not %.500s", |
| 1217 | Py_TYPE(args)->tp_name); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1221 | "idna", &host, &port)) |
| 1222 | return 0; |
| 1223 | addr=(struct sockaddr_in*)addr_ret; |
| 1224 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1225 | sizeof(*addr), AF_INET); |
| 1226 | PyMem_Free(host); |
| 1227 | if (result < 0) |
| 1228 | return 0; |
| 1229 | if (port < 0 || port > 0xffff) { |
| 1230 | PyErr_SetString( |
| 1231 | PyExc_OverflowError, |
| 1232 | "getsockaddrarg: port must be 0-65535."); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | addr->sin_family = AF_INET; |
| 1236 | addr->sin_port = htons((short)port); |
| 1237 | *len_ret = sizeof *addr; |
| 1238 | return 1; |
| 1239 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1240 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1241 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1242 | case AF_INET6: |
| 1243 | { |
| 1244 | struct sockaddr_in6* addr; |
| 1245 | char *host; |
| 1246 | int port, flowinfo, scope_id, result; |
| 1247 | flowinfo = scope_id = 0; |
| 1248 | if (!PyTuple_Check(args)) { |
| 1249 | PyErr_Format( |
| 1250 | PyExc_TypeError, |
| 1251 | "getsockaddrarg: " |
| 1252 | "AF_INET6 address must be tuple, not %.500s", |
| 1253 | Py_TYPE(args)->tp_name); |
| 1254 | return 0; |
| 1255 | } |
| 1256 | if (!PyArg_ParseTuple(args, "eti|ii", |
| 1257 | "idna", &host, &port, &flowinfo, |
| 1258 | &scope_id)) { |
| 1259 | return 0; |
| 1260 | } |
| 1261 | addr = (struct sockaddr_in6*)addr_ret; |
| 1262 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1263 | sizeof(*addr), AF_INET6); |
| 1264 | PyMem_Free(host); |
| 1265 | if (result < 0) |
| 1266 | return 0; |
| 1267 | if (port < 0 || port > 0xffff) { |
| 1268 | PyErr_SetString( |
| 1269 | PyExc_OverflowError, |
| 1270 | "getsockaddrarg: port must be 0-65535."); |
| 1271 | return 0; |
| 1272 | } |
| 1273 | addr->sin6_family = s->sock_family; |
| 1274 | addr->sin6_port = htons((short)port); |
| 1275 | addr->sin6_flowinfo = flowinfo; |
| 1276 | addr->sin6_scope_id = scope_id; |
| 1277 | *len_ret = sizeof *addr; |
| 1278 | return 1; |
| 1279 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1280 | #endif |
| 1281 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1282 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1283 | case AF_BLUETOOTH: |
| 1284 | { |
| 1285 | switch (s->sock_proto) { |
| 1286 | case BTPROTO_L2CAP: |
| 1287 | { |
| 1288 | struct sockaddr_l2 *addr; |
| 1289 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1290 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1291 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1292 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1293 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1294 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1295 | &_BT_L2_MEMB(addr, psm))) { |
| 1296 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1297 | "wrong format"); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1301 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1303 | *len_ret = sizeof *addr; |
| 1304 | return 1; |
| 1305 | } |
| 1306 | case BTPROTO_RFCOMM: |
| 1307 | { |
| 1308 | struct sockaddr_rc *addr; |
| 1309 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1310 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1311 | addr = (struct sockaddr_rc *)addr_ret; |
| 1312 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1313 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1314 | &_BT_RC_MEMB(addr, channel))) { |
| 1315 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1316 | "wrong format"); |
| 1317 | return 0; |
| 1318 | } |
| 1319 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1320 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1322 | *len_ret = sizeof *addr; |
| 1323 | return 1; |
| 1324 | } |
| 1325 | case BTPROTO_HCI: |
| 1326 | { |
| 1327 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
| 1328 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1329 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1330 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1331 | "wrong format"); |
| 1332 | return 0; |
| 1333 | } |
| 1334 | *len_ret = sizeof *addr; |
| 1335 | return 1; |
| 1336 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1337 | #if !defined(__FreeBSD__) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1338 | case BTPROTO_SCO: |
| 1339 | { |
| 1340 | struct sockaddr_sco *addr; |
| 1341 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1343 | addr = (struct sockaddr_sco *)addr_ret; |
| 1344 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1345 | if (!PyBytes_Check(args)) { |
| 1346 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1347 | "wrong format"); |
| 1348 | return 0; |
| 1349 | } |
| 1350 | straddr = PyBytes_AS_STRING(args); |
| 1351 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1352 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1353 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1354 | *len_ret = sizeof *addr; |
| 1355 | return 1; |
| 1356 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1357 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1358 | default: |
| 1359 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1363 | #endif |
| 1364 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1365 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1366 | case AF_PACKET: |
| 1367 | { |
| 1368 | struct sockaddr_ll* addr; |
| 1369 | struct ifreq ifr; |
| 1370 | char *interfaceName; |
| 1371 | int protoNumber; |
| 1372 | int hatype = 0; |
| 1373 | int pkttype = 0; |
| 1374 | char *haddr = NULL; |
| 1375 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1377 | if (!PyTuple_Check(args)) { |
| 1378 | PyErr_Format( |
| 1379 | PyExc_TypeError, |
| 1380 | "getsockaddrarg: " |
| 1381 | "AF_PACKET address must be tuple, not %.500s", |
| 1382 | Py_TYPE(args)->tp_name); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, |
| 1386 | &protoNumber, &pkttype, &hatype, |
| 1387 | &haddr, &halen)) |
| 1388 | return 0; |
| 1389 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1390 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1391 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1392 | s->errorhandler(); |
| 1393 | return 0; |
| 1394 | } |
| 1395 | if (halen > 8) { |
| 1396 | PyErr_SetString(PyExc_ValueError, |
| 1397 | "Hardware address must be 8 bytes or less"); |
| 1398 | return 0; |
| 1399 | } |
| 1400 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1401 | PyErr_SetString( |
| 1402 | PyExc_OverflowError, |
| 1403 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | addr = (struct sockaddr_ll*)addr_ret; |
| 1407 | addr->sll_family = AF_PACKET; |
| 1408 | addr->sll_protocol = htons((short)protoNumber); |
| 1409 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1410 | addr->sll_pkttype = pkttype; |
| 1411 | addr->sll_hatype = hatype; |
| 1412 | if (halen != 0) { |
| 1413 | memcpy(&addr->sll_addr, haddr, halen); |
| 1414 | } |
| 1415 | addr->sll_halen = halen; |
| 1416 | *len_ret = sizeof *addr; |
| 1417 | return 1; |
| 1418 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1419 | #endif |
| 1420 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1421 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1422 | case AF_TIPC: |
| 1423 | { |
| 1424 | unsigned int atype, v1, v2, v3; |
| 1425 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1426 | struct sockaddr_tipc *addr; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1428 | if (!PyTuple_Check(args)) { |
| 1429 | PyErr_Format( |
| 1430 | PyExc_TypeError, |
| 1431 | "getsockaddrarg: " |
| 1432 | "AF_TIPC address must be tuple, not %.500s", |
| 1433 | Py_TYPE(args)->tp_name); |
| 1434 | return 0; |
| 1435 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1436 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1437 | if (!PyArg_ParseTuple(args, |
| 1438 | "IIII|I;Invalid TIPC address format", |
| 1439 | &atype, &v1, &v2, &v3, &scope)) |
| 1440 | return 0; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1442 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1443 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1445 | addr->family = AF_TIPC; |
| 1446 | addr->scope = scope; |
| 1447 | addr->addrtype = atype; |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1448 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1449 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1450 | addr->addr.nameseq.type = v1; |
| 1451 | addr->addr.nameseq.lower = v2; |
| 1452 | addr->addr.nameseq.upper = v3; |
| 1453 | } else if (atype == TIPC_ADDR_NAME) { |
| 1454 | addr->addr.name.name.type = v1; |
| 1455 | addr->addr.name.name.instance = v2; |
| 1456 | } else if (atype == TIPC_ADDR_ID) { |
| 1457 | addr->addr.id.node = v1; |
| 1458 | addr->addr.id.ref = v2; |
| 1459 | } else { |
| 1460 | /* Shouldn't happen */ |
| 1461 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1462 | return 0; |
| 1463 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1465 | *len_ret = sizeof(*addr); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1466 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1467 | return 1; |
| 1468 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1469 | #endif |
| 1470 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1471 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1472 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1473 | default: |
| 1474 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1475 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1476 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1477 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1480 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1481 | /* 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] | 1482 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1483 | through len_ret. */ |
| 1484 | |
| 1485 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1486 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1487 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1488 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1489 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1490 | #if defined(AF_UNIX) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1491 | case AF_UNIX: |
| 1492 | { |
| 1493 | *len_ret = sizeof (struct sockaddr_un); |
| 1494 | return 1; |
| 1495 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1496 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1497 | #if defined(AF_NETLINK) |
| 1498 | case AF_NETLINK: |
| 1499 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1500 | *len_ret = sizeof (struct sockaddr_nl); |
| 1501 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1502 | } |
| 1503 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1505 | case AF_INET: |
| 1506 | { |
| 1507 | *len_ret = sizeof (struct sockaddr_in); |
| 1508 | return 1; |
| 1509 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1510 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1511 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1512 | case AF_INET6: |
| 1513 | { |
| 1514 | *len_ret = sizeof (struct sockaddr_in6); |
| 1515 | return 1; |
| 1516 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1517 | #endif |
| 1518 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1519 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1520 | case AF_BLUETOOTH: |
| 1521 | { |
| 1522 | switch(s->sock_proto) |
| 1523 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1524 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1525 | case BTPROTO_L2CAP: |
| 1526 | *len_ret = sizeof (struct sockaddr_l2); |
| 1527 | return 1; |
| 1528 | case BTPROTO_RFCOMM: |
| 1529 | *len_ret = sizeof (struct sockaddr_rc); |
| 1530 | return 1; |
| 1531 | case BTPROTO_HCI: |
| 1532 | *len_ret = sizeof (struct sockaddr_hci); |
| 1533 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1534 | #if !defined(__FreeBSD__) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1535 | case BTPROTO_SCO: |
| 1536 | *len_ret = sizeof (struct sockaddr_sco); |
| 1537 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1538 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1539 | default: |
| 1540 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1541 | "unknown BT protocol"); |
| 1542 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1546 | #endif |
| 1547 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1548 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1549 | case AF_PACKET: |
| 1550 | { |
| 1551 | *len_ret = sizeof (struct sockaddr_ll); |
| 1552 | return 1; |
| 1553 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1554 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1555 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1556 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1557 | case AF_TIPC: |
| 1558 | { |
| 1559 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1560 | return 1; |
| 1561 | } |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1562 | #endif |
| 1563 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1564 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1566 | default: |
| 1567 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1568 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1570 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1574 | /* s._accept() -> (fd, address) */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1575 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1576 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1577 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1578 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1579 | sock_addr_t addrbuf; |
| 1580 | SOCKET_T newfd = INVALID_SOCKET; |
| 1581 | socklen_t addrlen; |
| 1582 | PyObject *sock = NULL; |
| 1583 | PyObject *addr = NULL; |
| 1584 | PyObject *res = NULL; |
| 1585 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1587 | if (!getsockaddrlen(s, &addrlen)) |
| 1588 | return NULL; |
| 1589 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1591 | if (!IS_SELECTABLE(s)) |
| 1592 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1594 | Py_BEGIN_ALLOW_THREADS |
| 1595 | timeout = internal_select(s, 0); |
| 1596 | if (!timeout) |
| 1597 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1598 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1599 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1600 | if (timeout == 1) { |
| 1601 | PyErr_SetString(socket_timeout, "timed out"); |
| 1602 | return NULL; |
| 1603 | } |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1604 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1605 | if (newfd == INVALID_SOCKET) |
| 1606 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1608 | sock = PyLong_FromSocket_t(newfd); |
| 1609 | if (sock == NULL) { |
| 1610 | SOCKETCLOSE(newfd); |
| 1611 | goto finally; |
| 1612 | } |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1613 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1614 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1615 | addrlen, s->sock_proto); |
| 1616 | if (addr == NULL) |
| 1617 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1619 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1620 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1621 | finally: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1622 | Py_XDECREF(sock); |
| 1623 | Py_XDECREF(addr); |
| 1624 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1627 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1628 | "_accept() -> (integer, address info)\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1629 | \n\ |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1630 | Wait for an incoming connection. Return a new socket file descriptor\n\ |
| 1631 | representing the connection, and the address of the client.\n\ |
| 1632 | For IP sockets, the address info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1633 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1634 | /* s.setblocking(flag) method. Argument: |
| 1635 | False -- non-blocking mode; same as settimeout(0) |
| 1636 | True -- blocking mode; same as settimeout(None) |
| 1637 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1638 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1639 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1640 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1641 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1642 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1643 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1644 | block = PyLong_AsLong(arg); |
| 1645 | if (block == -1 && PyErr_Occurred()) |
| 1646 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1648 | s->sock_timeout = block ? -1.0 : 0.0; |
| 1649 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1651 | Py_INCREF(Py_None); |
| 1652 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1653 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1655 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1656 | "setblocking(flag)\n\ |
| 1657 | \n\ |
| 1658 | 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] | 1659 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1660 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1661 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1662 | /* s.settimeout(timeout) method. Argument: |
| 1663 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1664 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1665 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1666 | < 0 -- illegal; raises an exception |
| 1667 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1668 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1669 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1670 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1671 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1672 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1673 | if (arg == Py_None) |
| 1674 | timeout = -1.0; |
| 1675 | else { |
| 1676 | timeout = PyFloat_AsDouble(arg); |
| 1677 | if (timeout < 0.0) { |
| 1678 | if (!PyErr_Occurred()) |
| 1679 | PyErr_SetString(PyExc_ValueError, |
| 1680 | "Timeout value out of range"); |
| 1681 | return NULL; |
| 1682 | } |
| 1683 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1685 | s->sock_timeout = timeout; |
| 1686 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1687 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1688 | Py_INCREF(Py_None); |
| 1689 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1692 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1693 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1694 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1695 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1696 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1697 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1698 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1699 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1700 | /* s.gettimeout() method. |
| 1701 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1702 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1703 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1704 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1705 | if (s->sock_timeout < 0.0) { |
| 1706 | Py_INCREF(Py_None); |
| 1707 | return Py_None; |
| 1708 | } |
| 1709 | else |
| 1710 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1713 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1714 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1715 | \n\ |
| 1716 | Returns the timeout in floating seconds associated with socket \n\ |
| 1717 | 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] | 1718 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1719 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1720 | /* s.setsockopt() method. |
| 1721 | With an integer third argument, sets an integer option. |
| 1722 | With a string third argument, sets an option from a buffer; |
| 1723 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1724 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1725 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1726 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1727 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1728 | int level; |
| 1729 | int optname; |
| 1730 | int res; |
| 1731 | char *buf; |
| 1732 | int buflen; |
| 1733 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1734 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1735 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1736 | &level, &optname, &flag)) { |
| 1737 | buf = (char *) &flag; |
| 1738 | buflen = sizeof flag; |
| 1739 | } |
| 1740 | else { |
| 1741 | PyErr_Clear(); |
| 1742 | if (!PyArg_ParseTuple(args, "iiy#:setsockopt", |
| 1743 | &level, &optname, &buf, &buflen)) |
| 1744 | return NULL; |
| 1745 | } |
| 1746 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1747 | if (res < 0) |
| 1748 | return s->errorhandler(); |
| 1749 | Py_INCREF(Py_None); |
| 1750 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1753 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1754 | "setsockopt(level, option, value)\n\ |
| 1755 | \n\ |
| 1756 | 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] | 1757 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1758 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1759 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1760 | /* s.getsockopt() method. |
| 1761 | With two arguments, retrieves an integer option. |
| 1762 | With a third integer argument, retrieves a string buffer of that size; |
| 1763 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1764 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1765 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1766 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1767 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1768 | int level; |
| 1769 | int optname; |
| 1770 | int res; |
| 1771 | PyObject *buf; |
| 1772 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1774 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1775 | &level, &optname, &buflen)) |
| 1776 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1778 | if (buflen == 0) { |
| 1779 | int flag = 0; |
| 1780 | socklen_t flagsize = sizeof flag; |
| 1781 | res = getsockopt(s->sock_fd, level, optname, |
| 1782 | (void *)&flag, &flagsize); |
| 1783 | if (res < 0) |
| 1784 | return s->errorhandler(); |
| 1785 | return PyLong_FromLong(flag); |
| 1786 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1787 | #ifdef __VMS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1788 | /* socklen_t is unsigned so no negative test is needed, |
| 1789 | test buflen == 0 is previously done */ |
| 1790 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1791 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1792 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1793 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1794 | PyErr_SetString(socket_error, |
| 1795 | "getsockopt buflen out of range"); |
| 1796 | return NULL; |
| 1797 | } |
| 1798 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 1799 | if (buf == NULL) |
| 1800 | return NULL; |
| 1801 | res = getsockopt(s->sock_fd, level, optname, |
| 1802 | (void *)PyBytes_AS_STRING(buf), &buflen); |
| 1803 | if (res < 0) { |
| 1804 | Py_DECREF(buf); |
| 1805 | return s->errorhandler(); |
| 1806 | } |
| 1807 | _PyBytes_Resize(&buf, buflen); |
| 1808 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1811 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1812 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1813 | \n\ |
| 1814 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1815 | 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] | 1816 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1817 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1818 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1819 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1820 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1821 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1822 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1823 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1824 | sock_addr_t addrbuf; |
| 1825 | int addrlen; |
| 1826 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1827 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1828 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1829 | return NULL; |
| 1830 | Py_BEGIN_ALLOW_THREADS |
| 1831 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 1832 | Py_END_ALLOW_THREADS |
| 1833 | if (res < 0) |
| 1834 | return s->errorhandler(); |
| 1835 | Py_INCREF(Py_None); |
| 1836 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1839 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1840 | "bind(address)\n\ |
| 1841 | \n\ |
| 1842 | 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] | 1843 | 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] | 1844 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1845 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1846 | |
| 1847 | /* s.close() method. |
| 1848 | Set the file descriptor to -1 so operations tried subsequently |
| 1849 | will surely fail. */ |
| 1850 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1851 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1852 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1853 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1854 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1855 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1856 | if ((fd = s->sock_fd) != -1) { |
| 1857 | s->sock_fd = -1; |
| 1858 | Py_BEGIN_ALLOW_THREADS |
| 1859 | (void) SOCKETCLOSE(fd); |
| 1860 | Py_END_ALLOW_THREADS |
| 1861 | } |
| 1862 | Py_INCREF(Py_None); |
| 1863 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1866 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1867 | "close()\n\ |
| 1868 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1869 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1870 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1871 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1872 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1873 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1874 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1875 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1876 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1877 | timeout = 0; |
| 1878 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1879 | |
| 1880 | #ifdef MS_WINDOWS |
| 1881 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1882 | if (s->sock_timeout > 0.0) { |
| 1883 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 1884 | IS_SELECTABLE(s)) { |
| 1885 | /* This is a mess. Best solution: trust select */ |
| 1886 | fd_set fds; |
| 1887 | fd_set fds_exc; |
| 1888 | struct timeval tv; |
| 1889 | tv.tv_sec = (int)s->sock_timeout; |
| 1890 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1891 | FD_ZERO(&fds); |
| 1892 | FD_SET(s->sock_fd, &fds); |
| 1893 | FD_ZERO(&fds_exc); |
| 1894 | FD_SET(s->sock_fd, &fds_exc); |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 1895 | res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
| 1896 | NULL, &fds, &fds_exc, &tv); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1897 | if (res == 0) { |
| 1898 | res = WSAEWOULDBLOCK; |
| 1899 | timeout = 1; |
| 1900 | } else if (res > 0) { |
| 1901 | if (FD_ISSET(s->sock_fd, &fds)) |
| 1902 | /* The socket is in the writable set - this |
| 1903 | means connected */ |
| 1904 | res = 0; |
| 1905 | else { |
| 1906 | /* As per MS docs, we need to call getsockopt() |
| 1907 | to get the underlying error */ |
| 1908 | int res_size = sizeof res; |
| 1909 | /* It must be in the exception set */ |
| 1910 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 1911 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 1912 | (char *)&res, &res_size)) |
| 1913 | /* getsockopt also clears WSAGetLastError, |
| 1914 | so reset it back. */ |
| 1915 | WSASetLastError(res); |
| 1916 | else |
| 1917 | res = WSAGetLastError(); |
| 1918 | } |
| 1919 | } |
| 1920 | /* else if (res < 0) an error occurred */ |
| 1921 | } |
| 1922 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1923 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1924 | if (res < 0) |
| 1925 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1926 | |
| 1927 | #else |
| 1928 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1929 | if (s->sock_timeout > 0.0) { |
| 1930 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 1931 | timeout = internal_select(s, 1); |
| 1932 | if (timeout == 0) { |
| 1933 | /* Bug #1019808: in case of an EINPROGRESS, |
| 1934 | use getsockopt(SO_ERROR) to get the real |
| 1935 | error. */ |
| 1936 | socklen_t res_size = sizeof res; |
| 1937 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 1938 | SO_ERROR, &res, &res_size); |
| 1939 | if (res == EISCONN) |
| 1940 | res = 0; |
| 1941 | errno = res; |
| 1942 | } |
| 1943 | else if (timeout == -1) { |
| 1944 | res = errno; /* had error */ |
| 1945 | } |
| 1946 | else |
| 1947 | res = EWOULDBLOCK; /* timed out */ |
| 1948 | } |
| 1949 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1950 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1951 | if (res < 0) |
| 1952 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1953 | |
| 1954 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1955 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1956 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1957 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1958 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1959 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1960 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1961 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1962 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1963 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1964 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1965 | sock_addr_t addrbuf; |
| 1966 | int addrlen; |
| 1967 | int res; |
| 1968 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1969 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1970 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1971 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1972 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1973 | Py_BEGIN_ALLOW_THREADS |
| 1974 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 1975 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1976 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1977 | if (timeout == 1) { |
| 1978 | PyErr_SetString(socket_timeout, "timed out"); |
| 1979 | return NULL; |
| 1980 | } |
| 1981 | if (res != 0) |
| 1982 | return s->errorhandler(); |
| 1983 | Py_INCREF(Py_None); |
| 1984 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1987 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1988 | "connect(address)\n\ |
| 1989 | \n\ |
| 1990 | 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] | 1991 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1992 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1993 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1994 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1995 | |
| 1996 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1997 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1998 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1999 | sock_addr_t addrbuf; |
| 2000 | int addrlen; |
| 2001 | int res; |
| 2002 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2003 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2004 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2005 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2006 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2007 | Py_BEGIN_ALLOW_THREADS |
| 2008 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2009 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2010 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2011 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2012 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2013 | #ifdef EINTR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2014 | if (res == EINTR && PyErr_CheckSignals()) |
| 2015 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2016 | #endif |
| 2017 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2018 | return PyLong_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2021 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2022 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2023 | \n\ |
| 2024 | 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] | 2025 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2026 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2027 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2028 | /* s.fileno() method */ |
| 2029 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2030 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2031 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2032 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2033 | return PyLong_FromSocket_t(s->sock_fd); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2036 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2037 | "fileno() -> integer\n\ |
| 2038 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2039 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2040 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2041 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2042 | /* s.getsockname() method */ |
| 2043 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2044 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2045 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2046 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2047 | sock_addr_t addrbuf; |
| 2048 | int res; |
| 2049 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2050 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2051 | if (!getsockaddrlen(s, &addrlen)) |
| 2052 | return NULL; |
| 2053 | memset(&addrbuf, 0, addrlen); |
| 2054 | Py_BEGIN_ALLOW_THREADS |
| 2055 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2056 | Py_END_ALLOW_THREADS |
| 2057 | if (res < 0) |
| 2058 | return s->errorhandler(); |
| 2059 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2060 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2063 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2064 | "getsockname() -> address info\n\ |
| 2065 | \n\ |
| 2066 | 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] | 2067 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2068 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2070 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2071 | /* s.getpeername() method */ |
| 2072 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2073 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2074 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2075 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2076 | sock_addr_t addrbuf; |
| 2077 | int res; |
| 2078 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2079 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2080 | if (!getsockaddrlen(s, &addrlen)) |
| 2081 | return NULL; |
| 2082 | memset(&addrbuf, 0, addrlen); |
| 2083 | Py_BEGIN_ALLOW_THREADS |
| 2084 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2085 | Py_END_ALLOW_THREADS |
| 2086 | if (res < 0) |
| 2087 | return s->errorhandler(); |
| 2088 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2089 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2090 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2091 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2092 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2093 | "getpeername() -> address info\n\ |
| 2094 | \n\ |
| 2095 | 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] | 2096 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2097 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2098 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2099 | |
| 2100 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2101 | /* s.listen(n) method */ |
| 2102 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2103 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2104 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2105 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2106 | int backlog; |
| 2107 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2108 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2109 | backlog = PyLong_AsLong(arg); |
| 2110 | if (backlog == -1 && PyErr_Occurred()) |
| 2111 | return NULL; |
| 2112 | Py_BEGIN_ALLOW_THREADS |
| 2113 | if (backlog < 1) |
| 2114 | backlog = 1; |
| 2115 | res = listen(s->sock_fd, backlog); |
| 2116 | Py_END_ALLOW_THREADS |
| 2117 | if (res < 0) |
| 2118 | return s->errorhandler(); |
| 2119 | Py_INCREF(Py_None); |
| 2120 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2123 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2124 | "listen(backlog)\n\ |
| 2125 | \n\ |
| 2126 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 2127 | least 1; it specifies the number of unaccepted connection that the system\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2128 | will allow before refusing new connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2129 | |
| 2130 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2131 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2132 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 2133 | * char buffer. If you have any inc/dec ref to do to the objects that contain |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2134 | * the buffer, do it in the caller. This function returns the number of bytes |
| 2135 | * succesfully read. If there was an error, it returns -1. Note that it is |
| 2136 | * also possible that we return a number of bytes smaller than the request |
| 2137 | * bytes. |
| 2138 | */ |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2139 | static Py_ssize_t |
| 2140 | sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2141 | { |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2142 | Py_ssize_t outlen = -1; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2143 | int timeout; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2144 | #ifdef __VMS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2145 | int remaining; |
| 2146 | char *read_buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2147 | #endif |
| 2148 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2149 | if (!IS_SELECTABLE(s)) { |
| 2150 | select_error(); |
| 2151 | return -1; |
| 2152 | } |
| 2153 | if (len == 0) { |
| 2154 | /* If 0 bytes were requested, do nothing. */ |
| 2155 | return 0; |
| 2156 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2157 | |
| 2158 | #ifndef __VMS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2159 | Py_BEGIN_ALLOW_THREADS |
| 2160 | timeout = internal_select(s, 0); |
| 2161 | if (!timeout) |
| 2162 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2163 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2164 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2165 | if (timeout == 1) { |
| 2166 | PyErr_SetString(socket_timeout, "timed out"); |
| 2167 | return -1; |
| 2168 | } |
| 2169 | if (outlen < 0) { |
| 2170 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2171 | NULL, so ignore its return value */ |
| 2172 | s->errorhandler(); |
| 2173 | return -1; |
| 2174 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2175 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2176 | read_buf = cbuf; |
| 2177 | remaining = len; |
| 2178 | while (remaining != 0) { |
| 2179 | unsigned int segment; |
| 2180 | int nread = -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2181 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2182 | segment = remaining /SEGMENT_SIZE; |
| 2183 | if (segment != 0) { |
| 2184 | segment = SEGMENT_SIZE; |
| 2185 | } |
| 2186 | else { |
| 2187 | segment = remaining; |
| 2188 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2189 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2190 | Py_BEGIN_ALLOW_THREADS |
| 2191 | timeout = internal_select(s, 0); |
| 2192 | if (!timeout) |
| 2193 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2194 | Py_END_ALLOW_THREADS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2196 | if (timeout == 1) { |
| 2197 | PyErr_SetString(socket_timeout, "timed out"); |
| 2198 | return -1; |
| 2199 | } |
| 2200 | if (nread < 0) { |
| 2201 | s->errorhandler(); |
| 2202 | return -1; |
| 2203 | } |
| 2204 | if (nread != remaining) { |
| 2205 | read_buf += nread; |
| 2206 | break; |
| 2207 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2208 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2209 | remaining -= segment; |
| 2210 | read_buf += segment; |
| 2211 | } |
| 2212 | outlen = read_buf - cbuf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2213 | #endif /* !__VMS */ |
| 2214 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2215 | return outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2218 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2219 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2220 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2221 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2222 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2223 | { |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2224 | Py_ssize_t recvlen, outlen; |
| 2225 | int flags = 0; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2226 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2227 | |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2228 | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2229 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2230 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2231 | if (recvlen < 0) { |
| 2232 | PyErr_SetString(PyExc_ValueError, |
| 2233 | "negative buffersize in recv"); |
| 2234 | return NULL; |
| 2235 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2236 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2237 | /* Allocate a new string. */ |
| 2238 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2239 | if (buf == NULL) |
| 2240 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2241 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2242 | /* Call the guts */ |
| 2243 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2244 | if (outlen < 0) { |
| 2245 | /* An error occurred, release the string and return an |
| 2246 | error. */ |
| 2247 | Py_DECREF(buf); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | if (outlen != recvlen) { |
| 2251 | /* We did not read as many bytes as we anticipated, resize the |
| 2252 | string if possible and be successful. */ |
| 2253 | _PyBytes_Resize(&buf, outlen); |
| 2254 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2256 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2259 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2260 | "recv(buffersize[, flags]) -> data\n\ |
| 2261 | \n\ |
| 2262 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2263 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2264 | 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] | 2265 | 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] | 2266 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2267 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2268 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2269 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2270 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2271 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2272 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2273 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2275 | int flags = 0; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2276 | Py_buffer pbuf; |
| 2277 | char *buf; |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2278 | Py_ssize_t buflen, readlen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2280 | /* Get the buffer's memory */ |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2281 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2282 | &pbuf, &recvlen, &flags)) |
| 2283 | return NULL; |
| 2284 | buf = pbuf.buf; |
| 2285 | buflen = pbuf.len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2286 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2287 | if (recvlen < 0) { |
| 2288 | PyBuffer_Release(&pbuf); |
| 2289 | PyErr_SetString(PyExc_ValueError, |
| 2290 | "negative buffersize in recv_into"); |
| 2291 | return NULL; |
| 2292 | } |
| 2293 | if (recvlen == 0) { |
| 2294 | /* If nbytes was not specified, use the buffer's length */ |
| 2295 | recvlen = buflen; |
| 2296 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2298 | /* Check if the buffer is large enough */ |
| 2299 | if (buflen < recvlen) { |
| 2300 | PyBuffer_Release(&pbuf); |
| 2301 | PyErr_SetString(PyExc_ValueError, |
| 2302 | "buffer too small for requested bytes"); |
| 2303 | return NULL; |
| 2304 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2305 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2306 | /* Call the guts */ |
| 2307 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2308 | if (readlen < 0) { |
| 2309 | /* Return an error. */ |
| 2310 | PyBuffer_Release(&pbuf); |
| 2311 | return NULL; |
| 2312 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2313 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2314 | PyBuffer_Release(&pbuf); |
| 2315 | /* Return the number of bytes read. Note that we do not do anything |
| 2316 | special here in the case that readlen < recvlen. */ |
| 2317 | return PyLong_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2320 | PyDoc_STRVAR(recv_into_doc, |
| 2321 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2322 | \n\ |
| 2323 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2324 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2325 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2326 | \n\ |
| 2327 | See recv() for documentation about the flags."); |
| 2328 | |
| 2329 | |
| 2330 | /* |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 2331 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2332 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2333 | * contain the buffer, do it in the caller. This function returns the number |
| 2334 | * of bytes succesfully read. If there was an error, it returns -1. Note |
| 2335 | * that it is also possible that we return a number of bytes smaller than the |
| 2336 | * request bytes. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2337 | * |
| 2338 | * 'addr' is a return value for the address object. Note that you must decref |
| 2339 | * it yourself. |
| 2340 | */ |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2341 | static Py_ssize_t |
| 2342 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2343 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2344 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2345 | sock_addr_t addrbuf; |
| 2346 | int timeout; |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2347 | Py_ssize_t n = -1; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2348 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2350 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2351 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2352 | if (!getsockaddrlen(s, &addrlen)) |
| 2353 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2354 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2355 | if (!IS_SELECTABLE(s)) { |
| 2356 | select_error(); |
| 2357 | return -1; |
| 2358 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2360 | Py_BEGIN_ALLOW_THREADS |
| 2361 | memset(&addrbuf, 0, addrlen); |
| 2362 | timeout = internal_select(s, 0); |
| 2363 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2364 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2365 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2366 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2367 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2368 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2369 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2370 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2371 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2372 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2373 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2374 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2375 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2376 | } |
| 2377 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2378 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2379 | if (timeout == 1) { |
| 2380 | PyErr_SetString(socket_timeout, "timed out"); |
| 2381 | return -1; |
| 2382 | } |
| 2383 | if (n < 0) { |
| 2384 | s->errorhandler(); |
| 2385 | return -1; |
| 2386 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2387 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2388 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2389 | addrlen, s->sock_proto))) |
| 2390 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2391 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2392 | return n; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2396 | |
| 2397 | static PyObject * |
| 2398 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2399 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2400 | PyObject *buf = NULL; |
| 2401 | PyObject *addr = NULL; |
| 2402 | PyObject *ret = NULL; |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2403 | int flags = 0; |
| 2404 | Py_ssize_t recvlen, outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2405 | |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2406 | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2407 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2408 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2409 | if (recvlen < 0) { |
| 2410 | PyErr_SetString(PyExc_ValueError, |
| 2411 | "negative buffersize in recvfrom"); |
| 2412 | return NULL; |
| 2413 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2414 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2415 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2416 | if (buf == NULL) |
| 2417 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2419 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2420 | recvlen, flags, &addr); |
| 2421 | if (outlen < 0) { |
| 2422 | goto finally; |
| 2423 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2425 | if (outlen != recvlen) { |
| 2426 | /* We did not read as many bytes as we anticipated, resize the |
| 2427 | string if possible and be succesful. */ |
| 2428 | if (_PyBytes_Resize(&buf, outlen) < 0) |
| 2429 | /* Oopsy, not so succesful after all. */ |
| 2430 | goto finally; |
| 2431 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2432 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2433 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2434 | |
| 2435 | finally: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2436 | Py_XDECREF(buf); |
| 2437 | Py_XDECREF(addr); |
| 2438 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2441 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2442 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2443 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2444 | 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] | 2445 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2446 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2447 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2448 | |
| 2449 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2450 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2451 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2452 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2453 | |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2454 | int flags = 0; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2455 | Py_buffer pbuf; |
| 2456 | char *buf; |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2457 | Py_ssize_t readlen, buflen, recvlen = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2458 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2459 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2460 | |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2461 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2462 | kwlist, &pbuf, |
| 2463 | &recvlen, &flags)) |
| 2464 | return NULL; |
| 2465 | buf = pbuf.buf; |
| 2466 | buflen = pbuf.len; |
| 2467 | assert(buf != 0 && buflen > 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2468 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2469 | if (recvlen < 0) { |
| 2470 | PyBuffer_Release(&pbuf); |
| 2471 | PyErr_SetString(PyExc_ValueError, |
| 2472 | "negative buffersize in recvfrom_into"); |
| 2473 | return NULL; |
| 2474 | } |
| 2475 | if (recvlen == 0) { |
| 2476 | /* If nbytes was not specified, use the buffer's length */ |
| 2477 | recvlen = buflen; |
| 2478 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2479 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2480 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2481 | if (readlen < 0) { |
| 2482 | PyBuffer_Release(&pbuf); |
| 2483 | /* Return an error */ |
| 2484 | Py_XDECREF(addr); |
| 2485 | return NULL; |
| 2486 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2488 | PyBuffer_Release(&pbuf); |
| 2489 | /* Return the number of bytes read and the address. Note that we do |
| 2490 | not do anything special here in the case that readlen < recvlen. */ |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2491 | return Py_BuildValue("nN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2494 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2495 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2496 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2497 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2498 | |
| 2499 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2500 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2501 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2502 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2503 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2504 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2505 | char *buf; |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2506 | Py_ssize_t len, n = -1; |
| 2507 | int flags = 0, timeout; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2508 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2509 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2510 | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 2511 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2512 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2513 | if (!IS_SELECTABLE(s)) { |
| 2514 | PyBuffer_Release(&pbuf); |
| 2515 | return select_error(); |
| 2516 | } |
| 2517 | buf = pbuf.buf; |
| 2518 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2519 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2520 | Py_BEGIN_ALLOW_THREADS |
| 2521 | timeout = internal_select(s, 1); |
| 2522 | if (!timeout) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2523 | #ifdef __VMS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2524 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2525 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2526 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2527 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2528 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2529 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2530 | PyBuffer_Release(&pbuf); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2531 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2532 | if (timeout == 1) { |
| 2533 | PyErr_SetString(socket_timeout, "timed out"); |
| 2534 | return NULL; |
| 2535 | } |
| 2536 | if (n < 0) |
| 2537 | return s->errorhandler(); |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2538 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2541 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2542 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2543 | \n\ |
| 2544 | 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] | 2545 | 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] | 2546 | 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] | 2547 | |
| 2548 | |
| 2549 | /* s.sendall(data [,flags]) method */ |
| 2550 | |
| 2551 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2552 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2553 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2554 | char *buf; |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2555 | Py_ssize_t len, n = -1; |
| 2556 | int flags = 0, timeout; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2557 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2558 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2559 | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 2560 | return NULL; |
| 2561 | buf = pbuf.buf; |
| 2562 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2564 | if (!IS_SELECTABLE(s)) { |
| 2565 | PyBuffer_Release(&pbuf); |
| 2566 | return select_error(); |
| 2567 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2568 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2569 | Py_BEGIN_ALLOW_THREADS |
| 2570 | do { |
| 2571 | timeout = internal_select(s, 1); |
| 2572 | n = -1; |
| 2573 | if (timeout) |
| 2574 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2575 | #ifdef __VMS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2576 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2577 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2578 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2579 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2580 | if (n < 0) |
| 2581 | break; |
| 2582 | buf += n; |
| 2583 | len -= n; |
| 2584 | } while (len > 0); |
| 2585 | Py_END_ALLOW_THREADS |
| 2586 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2587 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2588 | if (timeout == 1) { |
| 2589 | PyErr_SetString(socket_timeout, "timed out"); |
| 2590 | return NULL; |
| 2591 | } |
| 2592 | if (n < 0) |
| 2593 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2594 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2595 | Py_INCREF(Py_None); |
| 2596 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2599 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2600 | "sendall(data[, flags])\n\ |
| 2601 | \n\ |
| 2602 | Send a data string to the socket. For the optional flags\n\ |
| 2603 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2604 | 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] | 2605 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2606 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2607 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2608 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2609 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2610 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2611 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2612 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2613 | Py_buffer pbuf; |
| 2614 | PyObject *addro; |
| 2615 | char *buf; |
| 2616 | Py_ssize_t len; |
| 2617 | sock_addr_t addrbuf; |
| 2618 | int addrlen, n = -1, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2620 | flags = 0; |
| 2621 | if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) { |
| 2622 | PyErr_Clear(); |
| 2623 | if (!PyArg_ParseTuple(args, "y*iO:sendto", |
| 2624 | &pbuf, &flags, &addro)) |
| 2625 | return NULL; |
| 2626 | } |
| 2627 | buf = pbuf.buf; |
| 2628 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2629 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2630 | if (!IS_SELECTABLE(s)) { |
| 2631 | PyBuffer_Release(&pbuf); |
| 2632 | return select_error(); |
| 2633 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2634 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2635 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2636 | PyBuffer_Release(&pbuf); |
| 2637 | return NULL; |
| 2638 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2639 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2640 | Py_BEGIN_ALLOW_THREADS |
| 2641 | timeout = internal_select(s, 1); |
| 2642 | if (!timeout) |
| 2643 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2644 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2645 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2646 | PyBuffer_Release(&pbuf); |
| 2647 | if (timeout == 1) { |
| 2648 | PyErr_SetString(socket_timeout, "timed out"); |
| 2649 | return NULL; |
| 2650 | } |
| 2651 | if (n < 0) |
| 2652 | return s->errorhandler(); |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 2653 | return PyLong_FromSsize_t(n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2656 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2657 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2658 | \n\ |
| 2659 | 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] | 2660 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2661 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2662 | |
| 2663 | /* s.shutdown(how) method */ |
| 2664 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2665 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2666 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2667 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2668 | int how; |
| 2669 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2670 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2671 | how = PyLong_AsLong(arg); |
| 2672 | if (how == -1 && PyErr_Occurred()) |
| 2673 | return NULL; |
| 2674 | Py_BEGIN_ALLOW_THREADS |
| 2675 | res = shutdown(s->sock_fd, how); |
| 2676 | Py_END_ALLOW_THREADS |
| 2677 | if (res < 0) |
| 2678 | return s->errorhandler(); |
| 2679 | Py_INCREF(Py_None); |
| 2680 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2683 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2684 | "shutdown(flag)\n\ |
| 2685 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2686 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2687 | 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] | 2688 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 2689 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2690 | static PyObject* |
| 2691 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 2692 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2693 | unsigned long cmd = SIO_RCVALL; |
| 2694 | unsigned int option = RCVALL_ON; |
| 2695 | DWORD recv; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2696 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2697 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 2698 | return NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2699 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2700 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 2701 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 2702 | return set_error(); |
| 2703 | } |
| 2704 | return PyLong_FromUnsignedLong(recv); |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2705 | } |
| 2706 | PyDoc_STRVAR(sock_ioctl_doc, |
| 2707 | "ioctl(cmd, option) -> long\n\ |
| 2708 | \n\ |
| 2709 | Control the socket with WSAIoctl syscall. Currently only socket.SIO_RCVALL\n\ |
| 2710 | is supported as control. Options must be one of the socket.RCVALL_*\n\ |
| 2711 | constants."); |
| 2712 | |
| 2713 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2714 | |
| 2715 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2716 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2717 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2718 | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 2719 | accept_doc}, |
| 2720 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 2721 | bind_doc}, |
| 2722 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 2723 | close_doc}, |
| 2724 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 2725 | connect_doc}, |
| 2726 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 2727 | connect_ex_doc}, |
| 2728 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 2729 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2730 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2731 | {"getpeername", (PyCFunction)sock_getpeername, |
| 2732 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 2733 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2734 | {"getsockname", (PyCFunction)sock_getsockname, |
| 2735 | METH_NOARGS, getsockname_doc}, |
| 2736 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 2737 | getsockopt_doc}, |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 2738 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2739 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 2740 | sock_ioctl_doc}, |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 2741 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2742 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 2743 | listen_doc}, |
| 2744 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 2745 | recv_doc}, |
| 2746 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 2747 | recv_into_doc}, |
| 2748 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 2749 | recvfrom_doc}, |
| 2750 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 2751 | recvfrom_into_doc}, |
| 2752 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 2753 | send_doc}, |
| 2754 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 2755 | sendall_doc}, |
| 2756 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 2757 | sendto_doc}, |
| 2758 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 2759 | setblocking_doc}, |
| 2760 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 2761 | settimeout_doc}, |
| 2762 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 2763 | gettimeout_doc}, |
| 2764 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 2765 | setsockopt_doc}, |
| 2766 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 2767 | shutdown_doc}, |
| 2768 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2769 | }; |
| 2770 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2771 | /* SockObject members */ |
| 2772 | static PyMemberDef sock_memberlist[] = { |
| 2773 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 2774 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 2775 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 2776 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 2777 | {0}, |
| 2778 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2779 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2780 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2781 | First close the file description. */ |
| 2782 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2783 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2784 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2785 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2786 | if (s->sock_fd != -1) |
| 2787 | (void) SOCKETCLOSE(s->sock_fd); |
| 2788 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2791 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2792 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2793 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2794 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2795 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2796 | if (s->sock_fd > LONG_MAX) { |
| 2797 | /* this can occur on Win64, and actually there is a special |
| 2798 | ugly printf formatter for decimal pointer length integer |
| 2799 | printing, only bother if necessary*/ |
| 2800 | PyErr_SetString(PyExc_OverflowError, |
| 2801 | "no printf formatter to display " |
| 2802 | "the socket descriptor in decimal"); |
| 2803 | return NULL; |
| 2804 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2805 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2806 | return PyUnicode_FromFormat( |
| 2807 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
| 2808 | (long)s->sock_fd, s->sock_family, |
| 2809 | s->sock_type, |
| 2810 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2814 | /* Create a new, uninitialized socket object. */ |
| 2815 | |
| 2816 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2817 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2818 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2819 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2820 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2821 | new = type->tp_alloc(type, 0); |
| 2822 | if (new != NULL) { |
| 2823 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 2824 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 2825 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 2826 | } |
| 2827 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | |
| 2831 | /* Initialize a new socket object. */ |
| 2832 | |
| 2833 | /*ARGSUSED*/ |
| 2834 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2835 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2836 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2837 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 2838 | PyObject *fdobj = NULL; |
| 2839 | SOCKET_T fd = INVALID_SOCKET; |
| 2840 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 2841 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2843 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2844 | "|iiiO:socket", keywords, |
| 2845 | &family, &type, &proto, &fdobj)) |
| 2846 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2848 | if (fdobj != NULL && fdobj != Py_None) { |
| 2849 | fd = PyLong_AsSocket_t(fdobj); |
| 2850 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 2851 | return -1; |
| 2852 | if (fd == INVALID_SOCKET) { |
| 2853 | PyErr_SetString(PyExc_ValueError, |
| 2854 | "can't use invalid socket value"); |
| 2855 | return -1; |
| 2856 | } |
| 2857 | } |
| 2858 | else { |
| 2859 | Py_BEGIN_ALLOW_THREADS |
| 2860 | fd = socket(family, type, proto); |
| 2861 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2862 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2863 | if (fd == INVALID_SOCKET) { |
| 2864 | set_error(); |
| 2865 | return -1; |
| 2866 | } |
| 2867 | } |
| 2868 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2869 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2870 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2871 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2875 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2876 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2877 | static PyTypeObject sock_type = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2878 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 2879 | "_socket.socket", /* tp_name */ |
| 2880 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 2881 | 0, /* tp_itemsize */ |
| 2882 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 2883 | 0, /* tp_print */ |
| 2884 | 0, /* tp_getattr */ |
| 2885 | 0, /* tp_setattr */ |
| 2886 | 0, /* tp_reserved */ |
| 2887 | (reprfunc)sock_repr, /* tp_repr */ |
| 2888 | 0, /* tp_as_number */ |
| 2889 | 0, /* tp_as_sequence */ |
| 2890 | 0, /* tp_as_mapping */ |
| 2891 | 0, /* tp_hash */ |
| 2892 | 0, /* tp_call */ |
| 2893 | 0, /* tp_str */ |
| 2894 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2895 | 0, /* tp_setattro */ |
| 2896 | 0, /* tp_as_buffer */ |
| 2897 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2898 | sock_doc, /* tp_doc */ |
| 2899 | 0, /* tp_traverse */ |
| 2900 | 0, /* tp_clear */ |
| 2901 | 0, /* tp_richcompare */ |
| 2902 | 0, /* tp_weaklistoffset */ |
| 2903 | 0, /* tp_iter */ |
| 2904 | 0, /* tp_iternext */ |
| 2905 | sock_methods, /* tp_methods */ |
| 2906 | sock_memberlist, /* tp_members */ |
| 2907 | 0, /* tp_getset */ |
| 2908 | 0, /* tp_base */ |
| 2909 | 0, /* tp_dict */ |
| 2910 | 0, /* tp_descr_get */ |
| 2911 | 0, /* tp_descr_set */ |
| 2912 | 0, /* tp_dictoffset */ |
| 2913 | sock_initobj, /* tp_init */ |
| 2914 | PyType_GenericAlloc, /* tp_alloc */ |
| 2915 | sock_new, /* tp_new */ |
| 2916 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2917 | }; |
| 2918 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2919 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2920 | /* Python interface to gethostname(). */ |
| 2921 | |
| 2922 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2923 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2924 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2925 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2926 | char buf[1024]; |
| 2927 | int res; |
| 2928 | Py_BEGIN_ALLOW_THREADS |
| 2929 | res = gethostname(buf, (int) sizeof buf - 1); |
| 2930 | Py_END_ALLOW_THREADS |
| 2931 | if (res < 0) |
| 2932 | return set_error(); |
| 2933 | buf[sizeof buf - 1] = '\0'; |
| 2934 | return PyUnicode_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2935 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2936 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2937 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2938 | "gethostname() -> string\n\ |
| 2939 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2940 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2941 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2942 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2943 | /* Python interface to gethostbyname(name). */ |
| 2944 | |
| 2945 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2946 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2947 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2948 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2949 | char *name; |
| 2950 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2951 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2952 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
| 2953 | return NULL; |
| 2954 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
| 2955 | return NULL; |
| 2956 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2959 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2960 | "gethostbyname(host) -> address\n\ |
| 2961 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2962 | 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] | 2963 | |
| 2964 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2965 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 2966 | |
| 2967 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2968 | 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] | 2969 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2970 | char **pch; |
| 2971 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 2972 | PyObject *name_list = (PyObject *)NULL; |
| 2973 | PyObject *addr_list = (PyObject *)NULL; |
| 2974 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2975 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2976 | if (h == NULL) { |
| 2977 | /* Let's get real error message to return */ |
| 2978 | set_herror(h_errno); |
| 2979 | return NULL; |
| 2980 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2981 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2982 | if (h->h_addrtype != af) { |
| 2983 | /* Let's get real error message to return */ |
| 2984 | PyErr_SetString(socket_error, |
| 2985 | (char *)strerror(EAFNOSUPPORT)); |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 2986 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2987 | return NULL; |
| 2988 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2989 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2990 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2991 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2992 | case AF_INET: |
| 2993 | if (alen < sizeof(struct sockaddr_in)) |
| 2994 | return NULL; |
| 2995 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2996 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2997 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 2998 | case AF_INET6: |
| 2999 | if (alen < sizeof(struct sockaddr_in6)) |
| 3000 | return NULL; |
| 3001 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3002 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3003 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3004 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3005 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3006 | if ((name_list = PyList_New(0)) == NULL) |
| 3007 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3008 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3009 | if ((addr_list = PyList_New(0)) == NULL) |
| 3010 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3011 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3012 | /* SF #1511317: h_aliases can be NULL */ |
| 3013 | if (h->h_aliases) { |
| 3014 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3015 | int status; |
| 3016 | tmp = PyUnicode_FromString(*pch); |
| 3017 | if (tmp == NULL) |
| 3018 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3019 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3020 | status = PyList_Append(name_list, tmp); |
| 3021 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3022 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3023 | if (status) |
| 3024 | goto err; |
| 3025 | } |
| 3026 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3027 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3028 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3029 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3030 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3031 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3032 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3033 | case AF_INET: |
| 3034 | { |
| 3035 | struct sockaddr_in sin; |
| 3036 | memset(&sin, 0, sizeof(sin)); |
| 3037 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3038 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3039 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3040 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3041 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3042 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3043 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3044 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3045 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3046 | break; |
| 3047 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3048 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3049 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3050 | case AF_INET6: |
| 3051 | { |
| 3052 | struct sockaddr_in6 sin6; |
| 3053 | memset(&sin6, 0, sizeof(sin6)); |
| 3054 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3055 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3056 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3057 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3058 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3059 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3060 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3061 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3062 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3063 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3064 | break; |
| 3065 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3066 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3067 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3068 | default: /* can't happen */ |
| 3069 | PyErr_SetString(socket_error, |
| 3070 | "unsupported address family"); |
| 3071 | return NULL; |
| 3072 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3073 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3074 | if (tmp == NULL) |
| 3075 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3076 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3077 | status = PyList_Append(addr_list, tmp); |
| 3078 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3079 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3080 | if (status) |
| 3081 | goto err; |
| 3082 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3083 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3084 | 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] | 3085 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3086 | err: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3087 | Py_XDECREF(name_list); |
| 3088 | Py_XDECREF(addr_list); |
| 3089 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | /* Python interface to gethostbyname_ex(name). */ |
| 3094 | |
| 3095 | /*ARGSUSED*/ |
| 3096 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3097 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3098 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3099 | char *name; |
| 3100 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3101 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3102 | struct sockaddr_storage addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3103 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3104 | struct sockaddr_in addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3105 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3106 | struct sockaddr *sa; |
| 3107 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3108 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3109 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3110 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3111 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3112 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3113 | char buf[16384]; |
| 3114 | int buf_len = (sizeof buf) - 1; |
| 3115 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3116 | #endif |
| 3117 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3118 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3119 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3120 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3121 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3122 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
| 3123 | return NULL; |
| 3124 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
| 3125 | return NULL; |
| 3126 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3127 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3128 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3129 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3130 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3131 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3132 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3133 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3134 | memset((void *) &data, '\0', sizeof(data)); |
| 3135 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3136 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3137 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3138 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3139 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3140 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3141 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3142 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3143 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3144 | Py_END_ALLOW_THREADS |
| 3145 | /* Some C libraries would require addr.__ss_family instead of |
| 3146 | addr.ss_family. |
| 3147 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3148 | access sa_family. */ |
| 3149 | sa = (struct sockaddr*)&addr; |
| 3150 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3151 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3152 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3153 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3154 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3155 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3158 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3159 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3160 | \n\ |
| 3161 | 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] | 3162 | 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] | 3163 | |
| 3164 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3165 | /* Python interface to gethostbyaddr(IP). */ |
| 3166 | |
| 3167 | /*ARGSUSED*/ |
| 3168 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3169 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3170 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3171 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3172 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3173 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3174 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3175 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3176 | struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3177 | char *ip_num; |
| 3178 | struct hostent *h; |
| 3179 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3180 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3181 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3182 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3183 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3184 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3185 | /* glibcs up to 2.10 assume that the buf argument to |
| 3186 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3187 | does not ensure. The attribute below instructs the compiler |
| 3188 | to maintain this alignment. */ |
| 3189 | char buf[16384] Py_ALIGNED(8); |
| 3190 | int buf_len = (sizeof buf) - 1; |
| 3191 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3192 | #endif |
| 3193 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3194 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3195 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3196 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3197 | char *ap; |
| 3198 | int al; |
| 3199 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3200 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3201 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
| 3202 | return NULL; |
| 3203 | af = AF_UNSPEC; |
| 3204 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
| 3205 | return NULL; |
| 3206 | af = sa->sa_family; |
| 3207 | ap = NULL; |
| 3208 | al = 0; |
| 3209 | switch (af) { |
| 3210 | case AF_INET: |
| 3211 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3212 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3213 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3214 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3215 | case AF_INET6: |
| 3216 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3217 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3218 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3219 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3220 | default: |
| 3221 | PyErr_SetString(socket_error, "unsupported address family"); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3225 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3226 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3227 | result = gethostbyaddr_r(ap, al, af, |
| 3228 | &hp_allocated, buf, buf_len, |
| 3229 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3230 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3231 | h = gethostbyaddr_r(ap, al, af, |
| 3232 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3233 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3234 | memset((void *) &data, '\0', sizeof(data)); |
| 3235 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3236 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3237 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3238 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3239 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3240 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3241 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3242 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3243 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3244 | Py_END_ALLOW_THREADS |
| 3245 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3246 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3247 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3248 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3249 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3252 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3253 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3254 | \n\ |
| 3255 | 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] | 3256 | 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] | 3257 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3258 | |
| 3259 | /* Python interface to getservbyname(name). |
| 3260 | This only returns the port number, since the other info is already |
| 3261 | known or not useful (like the list of aliases). */ |
| 3262 | |
| 3263 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3264 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3265 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3266 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3267 | char *name, *proto=NULL; |
| 3268 | struct servent *sp; |
| 3269 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3270 | return NULL; |
| 3271 | Py_BEGIN_ALLOW_THREADS |
| 3272 | sp = getservbyname(name, proto); |
| 3273 | Py_END_ALLOW_THREADS |
| 3274 | if (sp == NULL) { |
| 3275 | PyErr_SetString(socket_error, "service/proto not found"); |
| 3276 | return NULL; |
| 3277 | } |
| 3278 | return PyLong_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3281 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3282 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3283 | \n\ |
| 3284 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3285 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3286 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3287 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3288 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3289 | /* Python interface to getservbyport(port). |
| 3290 | This only returns the service name, since the other info is already |
| 3291 | known or not useful (like the list of aliases). */ |
| 3292 | |
| 3293 | /*ARGSUSED*/ |
| 3294 | static PyObject * |
| 3295 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3296 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3297 | int port; |
| 3298 | char *proto=NULL; |
| 3299 | struct servent *sp; |
| 3300 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3301 | return NULL; |
| 3302 | if (port < 0 || port > 0xffff) { |
| 3303 | PyErr_SetString( |
| 3304 | PyExc_OverflowError, |
| 3305 | "getservbyport: port must be 0-65535."); |
| 3306 | return NULL; |
| 3307 | } |
| 3308 | Py_BEGIN_ALLOW_THREADS |
| 3309 | sp = getservbyport(htons((short)port), proto); |
| 3310 | Py_END_ALLOW_THREADS |
| 3311 | if (sp == NULL) { |
| 3312 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3313 | return NULL; |
| 3314 | } |
| 3315 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | PyDoc_STRVAR(getservbyport_doc, |
| 3319 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3320 | \n\ |
| 3321 | Return the service name from a port number and protocol name.\n\ |
| 3322 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3323 | otherwise any protocol will match."); |
| 3324 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3325 | /* Python interface to getprotobyname(name). |
| 3326 | This only returns the protocol number, since the other info is |
| 3327 | already known or not useful (like the list of aliases). */ |
| 3328 | |
| 3329 | /*ARGSUSED*/ |
| 3330 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3331 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3332 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3333 | char *name; |
| 3334 | struct protoent *sp; |
| 3335 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3336 | return NULL; |
| 3337 | Py_BEGIN_ALLOW_THREADS |
| 3338 | sp = getprotobyname(name); |
| 3339 | Py_END_ALLOW_THREADS |
| 3340 | if (sp == NULL) { |
| 3341 | PyErr_SetString(socket_error, "protocol not found"); |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | return PyLong_FromLong((long) sp->p_proto); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3347 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3348 | "getprotobyname(name) -> integer\n\ |
| 3349 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3350 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3351 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3352 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3353 | #ifndef NO_DUP |
| 3354 | /* dup() function for socket fds */ |
| 3355 | |
| 3356 | static PyObject * |
| 3357 | socket_dup(PyObject *self, PyObject *fdobj) |
| 3358 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3359 | SOCKET_T fd, newfd; |
| 3360 | PyObject *newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3361 | |
| 3362 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3363 | fd = PyLong_AsSocket_t(fdobj); |
| 3364 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 3365 | return NULL; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3366 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3367 | newfd = dup_socket(fd); |
| 3368 | if (newfd == INVALID_SOCKET) |
| 3369 | return set_error(); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3370 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3371 | newfdobj = PyLong_FromSocket_t(newfd); |
| 3372 | if (newfdobj == NULL) |
| 3373 | SOCKETCLOSE(newfd); |
| 3374 | return newfdobj; |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | PyDoc_STRVAR(dup_doc, |
| 3378 | "dup(integer) -> integer\n\ |
| 3379 | \n\ |
| 3380 | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
| 3381 | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
| 3382 | #endif |
| 3383 | |
| 3384 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3385 | #ifdef HAVE_SOCKETPAIR |
| 3386 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3387 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3388 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3389 | |
| 3390 | /*ARGSUSED*/ |
| 3391 | static PyObject * |
| 3392 | socket_socketpair(PyObject *self, PyObject *args) |
| 3393 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3394 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3395 | SOCKET_T sv[2]; |
| 3396 | int family, type = SOCK_STREAM, proto = 0; |
| 3397 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3398 | |
| 3399 | #if defined(AF_UNIX) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3400 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3401 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3402 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3403 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3404 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3405 | &family, &type, &proto)) |
| 3406 | return NULL; |
| 3407 | /* Create a pair of socket fds */ |
| 3408 | if (socketpair(family, type, proto, sv) < 0) |
| 3409 | return set_error(); |
| 3410 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3411 | if (s0 == NULL) |
| 3412 | goto finally; |
| 3413 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3414 | if (s1 == NULL) |
| 3415 | goto finally; |
| 3416 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3417 | |
| 3418 | finally: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3419 | if (res == NULL) { |
| 3420 | if (s0 == NULL) |
| 3421 | SOCKETCLOSE(sv[0]); |
| 3422 | if (s1 == NULL) |
| 3423 | SOCKETCLOSE(sv[1]); |
| 3424 | } |
| 3425 | Py_XDECREF(s0); |
| 3426 | Py_XDECREF(s1); |
| 3427 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | PyDoc_STRVAR(socketpair_doc, |
| 3431 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3432 | \n\ |
| 3433 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3434 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3435 | 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] | 3436 | 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] | 3437 | |
| 3438 | #endif /* HAVE_SOCKETPAIR */ |
| 3439 | |
| 3440 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3441 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3442 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3443 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3444 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3445 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3446 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3447 | return NULL; |
| 3448 | } |
| 3449 | if (x1 < 0) { |
| 3450 | PyErr_SetString(PyExc_OverflowError, |
| 3451 | "can't convert negative number to unsigned long"); |
| 3452 | return NULL; |
| 3453 | } |
| 3454 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3455 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3458 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3459 | "ntohs(integer) -> integer\n\ |
| 3460 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3461 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3462 | |
| 3463 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3464 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3465 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3466 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3467 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3469 | if (PyLong_Check(arg)) { |
| 3470 | x = PyLong_AsUnsignedLong(arg); |
| 3471 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3472 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3473 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3474 | { |
| 3475 | unsigned long y; |
| 3476 | /* only want the trailing 32 bits */ |
| 3477 | y = x & 0xFFFFFFFFUL; |
| 3478 | if (y ^ x) |
| 3479 | return PyErr_Format(PyExc_OverflowError, |
| 3480 | "long int larger than 32 bits"); |
| 3481 | x = y; |
| 3482 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3483 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3484 | } |
| 3485 | else |
| 3486 | return PyErr_Format(PyExc_TypeError, |
| 3487 | "expected int/long, %s found", |
| 3488 | Py_TYPE(arg)->tp_name); |
| 3489 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3490 | return NULL; |
| 3491 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3494 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3495 | "ntohl(integer) -> integer\n\ |
| 3496 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3497 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3498 | |
| 3499 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3500 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3501 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3502 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3503 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3504 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3505 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3506 | return NULL; |
| 3507 | } |
| 3508 | if (x1 < 0) { |
| 3509 | PyErr_SetString(PyExc_OverflowError, |
| 3510 | "can't convert negative number to unsigned long"); |
| 3511 | return NULL; |
| 3512 | } |
| 3513 | x2 = (unsigned int)htons((unsigned short)x1); |
| 3514 | return PyLong_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3517 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3518 | "htons(integer) -> integer\n\ |
| 3519 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3520 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3521 | |
| 3522 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3523 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3524 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3525 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3526 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3527 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3528 | if (PyLong_Check(arg)) { |
| 3529 | x = PyLong_AsUnsignedLong(arg); |
| 3530 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3531 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3532 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3533 | { |
| 3534 | unsigned long y; |
| 3535 | /* only want the trailing 32 bits */ |
| 3536 | y = x & 0xFFFFFFFFUL; |
| 3537 | if (y ^ x) |
| 3538 | return PyErr_Format(PyExc_OverflowError, |
| 3539 | "long int larger than 32 bits"); |
| 3540 | x = y; |
| 3541 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3542 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3543 | } |
| 3544 | else |
| 3545 | return PyErr_Format(PyExc_TypeError, |
| 3546 | "expected int/long, %s found", |
| 3547 | Py_TYPE(arg)->tp_name); |
| 3548 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3551 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3552 | "htonl(integer) -> integer\n\ |
| 3553 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3554 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3555 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3556 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3558 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 3559 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3560 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3561 | 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] | 3562 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3563 | |
| 3564 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3565 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3566 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3567 | #ifndef INADDR_NONE |
| 3568 | #define INADDR_NONE (-1) |
| 3569 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3570 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3571 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3572 | #endif |
| 3573 | |
| 3574 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 3575 | #if (SIZEOF_INT != 4) |
| 3576 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3577 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3578 | /* Have to use inet_addr() instead */ |
| 3579 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3580 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3581 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3582 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3583 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3584 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3585 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3586 | |
| 3587 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3588 | |
| 3589 | #ifdef USE_INET_ATON_WEAKLINK |
| 3590 | if (inet_aton != NULL) { |
| 3591 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3592 | if (inet_aton(ip_addr, &buf)) |
| 3593 | return PyBytes_FromStringAndSize((char *)(&buf), |
| 3594 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3595 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3596 | PyErr_SetString(socket_error, |
| 3597 | "illegal IP address string passed to inet_aton"); |
| 3598 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3599 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3600 | #ifdef USE_INET_ATON_WEAKLINK |
| 3601 | } else { |
| 3602 | #endif |
| 3603 | |
| 3604 | #endif |
| 3605 | |
| 3606 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3607 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3608 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3609 | * for this */ |
| 3610 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3611 | packed_addr = 0xFFFFFFFF; |
| 3612 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3613 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3614 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3615 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3616 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3617 | PyErr_SetString(socket_error, |
| 3618 | "illegal IP address string passed to inet_aton"); |
| 3619 | return NULL; |
| 3620 | } |
| 3621 | } |
| 3622 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 3623 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3624 | |
| 3625 | #ifdef USE_INET_ATON_WEAKLINK |
| 3626 | } |
| 3627 | #endif |
| 3628 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3629 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3632 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3633 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3634 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3635 | 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] | 3636 | |
| 3637 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3638 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3639 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3640 | char *packed_str; |
| 3641 | int addr_len; |
| 3642 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3643 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3644 | if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
| 3645 | return NULL; |
| 3646 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3647 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3648 | if (addr_len != sizeof(packed_addr)) { |
| 3649 | PyErr_SetString(socket_error, |
| 3650 | "packed IP wrong length for inet_ntoa"); |
| 3651 | return NULL; |
| 3652 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3653 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3654 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3655 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3656 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3657 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3658 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3659 | #ifdef HAVE_INET_PTON |
| 3660 | |
| 3661 | PyDoc_STRVAR(inet_pton_doc, |
| 3662 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 3663 | \n\ |
| 3664 | Convert an IP address from string format to a packed string suitable\n\ |
| 3665 | for use with low-level network functions."); |
| 3666 | |
| 3667 | static PyObject * |
| 3668 | socket_inet_pton(PyObject *self, PyObject *args) |
| 3669 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3670 | int af; |
| 3671 | char* ip; |
| 3672 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3673 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3674 | 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] | 3675 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3676 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3677 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3678 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3679 | return NULL; |
| 3680 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3681 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 3682 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3683 | if(af == AF_INET6) { |
| 3684 | PyErr_SetString(socket_error, |
| 3685 | "can't use AF_INET6, IPv6 is disabled"); |
| 3686 | return NULL; |
| 3687 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3688 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3689 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3690 | retval = inet_pton(af, ip, packed); |
| 3691 | if (retval < 0) { |
| 3692 | PyErr_SetFromErrno(socket_error); |
| 3693 | return NULL; |
| 3694 | } else if (retval == 0) { |
| 3695 | PyErr_SetString(socket_error, |
| 3696 | "illegal IP address string passed to inet_pton"); |
| 3697 | return NULL; |
| 3698 | } else if (af == AF_INET) { |
| 3699 | return PyBytes_FromStringAndSize(packed, |
| 3700 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3701 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3702 | } else if (af == AF_INET6) { |
| 3703 | return PyBytes_FromStringAndSize(packed, |
| 3704 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3705 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3706 | } else { |
| 3707 | PyErr_SetString(socket_error, "unknown address family"); |
| 3708 | return NULL; |
| 3709 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3710 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3711 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3712 | PyDoc_STRVAR(inet_ntop_doc, |
| 3713 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3714 | \n\ |
| 3715 | Convert a packed IP address of the given family to string format."); |
| 3716 | |
| 3717 | static PyObject * |
| 3718 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 3719 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3720 | int af; |
| 3721 | char* packed; |
| 3722 | int len; |
| 3723 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3724 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3725 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3726 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3727 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3728 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3729 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3730 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 3731 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3732 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3733 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 3734 | return NULL; |
| 3735 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3736 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3737 | if (af == AF_INET) { |
| 3738 | if (len != sizeof(struct in_addr)) { |
| 3739 | PyErr_SetString(PyExc_ValueError, |
| 3740 | "invalid length of packed IP address string"); |
| 3741 | return NULL; |
| 3742 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3743 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3744 | } else if (af == AF_INET6) { |
| 3745 | if (len != sizeof(struct in6_addr)) { |
| 3746 | PyErr_SetString(PyExc_ValueError, |
| 3747 | "invalid length of packed IP address string"); |
| 3748 | return NULL; |
| 3749 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3750 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3751 | } else { |
| 3752 | PyErr_Format(PyExc_ValueError, |
| 3753 | "unknown address family %d", af); |
| 3754 | return NULL; |
| 3755 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3756 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3757 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 3758 | if (!retval) { |
| 3759 | PyErr_SetFromErrno(socket_error); |
| 3760 | return NULL; |
| 3761 | } else { |
| 3762 | return PyUnicode_FromString(retval); |
| 3763 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3764 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3765 | /* NOTREACHED */ |
| 3766 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 3767 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | #endif /* HAVE_INET_PTON */ |
| 3771 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3772 | /* Python interface to getaddrinfo(host, port). */ |
| 3773 | |
| 3774 | /*ARGSUSED*/ |
| 3775 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3776 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3777 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3778 | struct addrinfo hints, *res; |
| 3779 | struct addrinfo *res0 = NULL; |
| 3780 | PyObject *hobj = NULL; |
| 3781 | PyObject *pobj = (PyObject *)NULL; |
| 3782 | char pbuf[30]; |
| 3783 | char *hptr, *pptr; |
| 3784 | int family, socktype, protocol, flags; |
| 3785 | int error; |
| 3786 | PyObject *all = (PyObject *)NULL; |
| 3787 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3788 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3789 | family = socktype = protocol = flags = 0; |
| 3790 | family = AF_UNSPEC; |
| 3791 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 3792 | &hobj, &pobj, &family, &socktype, |
| 3793 | &protocol, &flags)) { |
| 3794 | return NULL; |
| 3795 | } |
| 3796 | if (hobj == Py_None) { |
| 3797 | hptr = NULL; |
| 3798 | } else if (PyUnicode_Check(hobj)) { |
| 3799 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 3800 | if (!idna) |
| 3801 | return NULL; |
| 3802 | assert(PyBytes_Check(idna)); |
| 3803 | hptr = PyBytes_AS_STRING(idna); |
| 3804 | } else if (PyBytes_Check(hobj)) { |
| 3805 | hptr = PyBytes_AsString(hobj); |
| 3806 | } else { |
| 3807 | PyErr_SetString(PyExc_TypeError, |
| 3808 | "getaddrinfo() argument 1 must be string or None"); |
| 3809 | return NULL; |
| 3810 | } |
| 3811 | if (PyLong_CheckExact(pobj)) { |
| 3812 | long value = PyLong_AsLong(pobj); |
| 3813 | if (value == -1 && PyErr_Occurred()) |
| 3814 | goto err; |
| 3815 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 3816 | pptr = pbuf; |
| 3817 | } else if (PyUnicode_Check(pobj)) { |
| 3818 | pptr = _PyUnicode_AsString(pobj); |
| 3819 | } else if (PyBytes_Check(pobj)) { |
| 3820 | pptr = PyBytes_AsString(pobj); |
| 3821 | } else if (pobj == Py_None) { |
| 3822 | pptr = (char *)NULL; |
| 3823 | } else { |
| 3824 | PyErr_SetString(socket_error, "Int or String expected"); |
| 3825 | goto err; |
| 3826 | } |
| 3827 | memset(&hints, 0, sizeof(hints)); |
| 3828 | hints.ai_family = family; |
| 3829 | hints.ai_socktype = socktype; |
| 3830 | hints.ai_protocol = protocol; |
| 3831 | hints.ai_flags = flags; |
| 3832 | Py_BEGIN_ALLOW_THREADS |
| 3833 | ACQUIRE_GETADDRINFO_LOCK |
| 3834 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 3835 | Py_END_ALLOW_THREADS |
| 3836 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 3837 | if (error) { |
| 3838 | set_gaierror(error); |
| 3839 | goto err; |
| 3840 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3841 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3842 | if ((all = PyList_New(0)) == NULL) |
| 3843 | goto err; |
| 3844 | for (res = res0; res; res = res->ai_next) { |
| 3845 | PyObject *single; |
| 3846 | PyObject *addr = |
| 3847 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 3848 | if (addr == NULL) |
| 3849 | goto err; |
| 3850 | single = Py_BuildValue("iiisO", res->ai_family, |
| 3851 | res->ai_socktype, res->ai_protocol, |
| 3852 | res->ai_canonname ? res->ai_canonname : "", |
| 3853 | addr); |
| 3854 | Py_DECREF(addr); |
| 3855 | if (single == NULL) |
| 3856 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3857 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3858 | if (PyList_Append(all, single)) |
| 3859 | goto err; |
| 3860 | Py_XDECREF(single); |
| 3861 | } |
| 3862 | Py_XDECREF(idna); |
| 3863 | if (res0) |
| 3864 | freeaddrinfo(res0); |
| 3865 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3866 | err: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3867 | Py_XDECREF(all); |
| 3868 | Py_XDECREF(idna); |
| 3869 | if (res0) |
| 3870 | freeaddrinfo(res0); |
| 3871 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3874 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3875 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 3876 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3877 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3878 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3879 | |
| 3880 | /* Python interface to getnameinfo(sa, flags). */ |
| 3881 | |
| 3882 | /*ARGSUSED*/ |
| 3883 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3884 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3885 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3886 | PyObject *sa = (PyObject *)NULL; |
| 3887 | int flags; |
| 3888 | char *hostp; |
| 3889 | int port, flowinfo, scope_id; |
| 3890 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 3891 | struct addrinfo hints, *res = NULL; |
| 3892 | int error; |
| 3893 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3894 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3895 | flags = flowinfo = scope_id = 0; |
| 3896 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 3897 | return NULL; |
| 3898 | if (!PyTuple_Check(sa)) { |
| 3899 | PyErr_SetString(PyExc_TypeError, |
| 3900 | "getnameinfo() argument 1 must be a tuple"); |
| 3901 | return NULL; |
| 3902 | } |
| 3903 | if (!PyArg_ParseTuple(sa, "si|ii", |
| 3904 | &hostp, &port, &flowinfo, &scope_id)) |
| 3905 | return NULL; |
| 3906 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 3907 | memset(&hints, 0, sizeof(hints)); |
| 3908 | hints.ai_family = AF_UNSPEC; |
| 3909 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
| 3910 | Py_BEGIN_ALLOW_THREADS |
| 3911 | ACQUIRE_GETADDRINFO_LOCK |
| 3912 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 3913 | Py_END_ALLOW_THREADS |
| 3914 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 3915 | if (error) { |
| 3916 | set_gaierror(error); |
| 3917 | goto fail; |
| 3918 | } |
| 3919 | if (res->ai_next) { |
| 3920 | PyErr_SetString(socket_error, |
| 3921 | "sockaddr resolved to multiple addresses"); |
| 3922 | goto fail; |
| 3923 | } |
| 3924 | switch (res->ai_family) { |
| 3925 | case AF_INET: |
| 3926 | { |
| 3927 | if (PyTuple_GET_SIZE(sa) != 2) { |
| 3928 | PyErr_SetString(socket_error, |
| 3929 | "IPv4 sockaddr must be 2 tuple"); |
| 3930 | goto fail; |
| 3931 | } |
| 3932 | break; |
| 3933 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3934 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3935 | case AF_INET6: |
| 3936 | { |
| 3937 | struct sockaddr_in6 *sin6; |
| 3938 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 3939 | sin6->sin6_flowinfo = flowinfo; |
| 3940 | sin6->sin6_scope_id = scope_id; |
| 3941 | break; |
| 3942 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3943 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3944 | } |
Antoine Pitrou | f72006f | 2010-08-17 19:39:39 +0000 | [diff] [blame] | 3945 | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3946 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 3947 | if (error) { |
| 3948 | set_gaierror(error); |
| 3949 | goto fail; |
| 3950 | } |
| 3951 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3952 | |
| 3953 | fail: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3954 | if (res) |
| 3955 | freeaddrinfo(res); |
| 3956 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3959 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3960 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3961 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3962 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3963 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3964 | |
| 3965 | /* Python API to getting and setting the default timeout value. */ |
| 3966 | |
| 3967 | static PyObject * |
| 3968 | socket_getdefaulttimeout(PyObject *self) |
| 3969 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3970 | if (defaulttimeout < 0.0) { |
| 3971 | Py_INCREF(Py_None); |
| 3972 | return Py_None; |
| 3973 | } |
| 3974 | else |
| 3975 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3979 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3980 | \n\ |
| 3981 | Returns the default timeout in floating seconds for new socket objects.\n\ |
| 3982 | A value of None indicates that new socket objects have no timeout.\n\ |
| 3983 | When the socket module is first imported, the default is None."); |
| 3984 | |
| 3985 | static PyObject * |
| 3986 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 3987 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3988 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3989 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 3990 | if (arg == Py_None) |
| 3991 | timeout = -1.0; |
| 3992 | else { |
| 3993 | timeout = PyFloat_AsDouble(arg); |
| 3994 | if (timeout < 0.0) { |
| 3995 | if (!PyErr_Occurred()) |
| 3996 | PyErr_SetString(PyExc_ValueError, |
| 3997 | "Timeout value out of range"); |
| 3998 | return NULL; |
| 3999 | } |
| 4000 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4001 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4002 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4003 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4004 | Py_INCREF(Py_None); |
| 4005 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4009 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4010 | \n\ |
| 4011 | Set the default timeout in floating seconds for new socket objects.\n\ |
| 4012 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4013 | When the socket module is first imported, the default is None."); |
| 4014 | |
| 4015 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4016 | /* List of functions exported by this module. */ |
| 4017 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4018 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4019 | {"gethostbyname", socket_gethostbyname, |
| 4020 | METH_VARARGS, gethostbyname_doc}, |
| 4021 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4022 | METH_VARARGS, ghbn_ex_doc}, |
| 4023 | {"gethostbyaddr", socket_gethostbyaddr, |
| 4024 | METH_VARARGS, gethostbyaddr_doc}, |
| 4025 | {"gethostname", socket_gethostname, |
| 4026 | METH_NOARGS, gethostname_doc}, |
| 4027 | {"getservbyname", socket_getservbyname, |
| 4028 | METH_VARARGS, getservbyname_doc}, |
| 4029 | {"getservbyport", socket_getservbyport, |
| 4030 | METH_VARARGS, getservbyport_doc}, |
| 4031 | {"getprotobyname", socket_getprotobyname, |
| 4032 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4033 | #ifndef NO_DUP |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4034 | {"dup", socket_dup, |
| 4035 | METH_O, dup_doc}, |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4036 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4037 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4038 | {"socketpair", socket_socketpair, |
| 4039 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4040 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4041 | {"ntohs", socket_ntohs, |
| 4042 | METH_VARARGS, ntohs_doc}, |
| 4043 | {"ntohl", socket_ntohl, |
| 4044 | METH_O, ntohl_doc}, |
| 4045 | {"htons", socket_htons, |
| 4046 | METH_VARARGS, htons_doc}, |
| 4047 | {"htonl", socket_htonl, |
| 4048 | METH_O, htonl_doc}, |
| 4049 | {"inet_aton", socket_inet_aton, |
| 4050 | METH_VARARGS, inet_aton_doc}, |
| 4051 | {"inet_ntoa", socket_inet_ntoa, |
| 4052 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4053 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4054 | {"inet_pton", socket_inet_pton, |
| 4055 | METH_VARARGS, inet_pton_doc}, |
| 4056 | {"inet_ntop", socket_inet_ntop, |
| 4057 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4058 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4059 | {"getaddrinfo", socket_getaddrinfo, |
| 4060 | METH_VARARGS, getaddrinfo_doc}, |
| 4061 | {"getnameinfo", socket_getnameinfo, |
| 4062 | METH_VARARGS, getnameinfo_doc}, |
| 4063 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4064 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4065 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4066 | METH_O, setdefaulttimeout_doc}, |
| 4067 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4068 | }; |
| 4069 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4070 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4071 | #ifdef MS_WINDOWS |
| 4072 | #define OS_INIT_DEFINED |
| 4073 | |
| 4074 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4075 | |
| 4076 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4077 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4078 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4079 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4083 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4084 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4085 | WSADATA WSAData; |
| 4086 | int ret; |
| 4087 | ret = WSAStartup(0x0101, &WSAData); |
| 4088 | switch (ret) { |
| 4089 | case 0: /* No error */ |
| 4090 | Py_AtExit(os_cleanup); |
| 4091 | return 1; /* Success */ |
| 4092 | case WSASYSNOTREADY: |
| 4093 | PyErr_SetString(PyExc_ImportError, |
| 4094 | "WSAStartup failed: network not ready"); |
| 4095 | break; |
| 4096 | case WSAVERNOTSUPPORTED: |
| 4097 | case WSAEINVAL: |
| 4098 | PyErr_SetString( |
| 4099 | PyExc_ImportError, |
| 4100 | "WSAStartup failed: requested version not supported"); |
| 4101 | break; |
| 4102 | default: |
| 4103 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 4104 | break; |
| 4105 | } |
| 4106 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4109 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4110 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4111 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4112 | #ifdef PYOS_OS2 |
| 4113 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4114 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4115 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4116 | |
| 4117 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4118 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4119 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4120 | #ifndef PYCC_GCC |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4121 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4122 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4123 | if (rc == 0) { |
| 4124 | return 1; /* Success */ |
| 4125 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4126 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4127 | PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno()); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4128 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4129 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4130 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4131 | /* No need to initialise sockets with GCC/EMX */ |
| 4132 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4133 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | #endif /* PYOS_OS2 */ |
| 4137 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4138 | |
| 4139 | #ifndef OS_INIT_DEFINED |
| 4140 | static int |
| 4141 | os_init(void) |
| 4142 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4143 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4144 | } |
| 4145 | #endif |
| 4146 | |
| 4147 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4148 | /* C API table - always add new things to the end for binary |
| 4149 | compatibility. */ |
| 4150 | static |
| 4151 | PySocketModule_APIObject PySocketModuleAPI = |
| 4152 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4153 | &sock_type, |
| 4154 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4155 | }; |
| 4156 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4157 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4158 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4159 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4160 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4161 | "socket.py" which implements some additional functionality. |
| 4162 | The import of "_socket" may fail with an ImportError exception if |
| 4163 | os-specific initialization fails. On Windows, this does WINSOCK |
| 4164 | initialization. When WINSOCK is initialized succesfully, a call to |
| 4165 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4166 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4168 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4169 | "Implementation module for socket operations.\n\ |
| 4170 | \n\ |
| 4171 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4172 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4173 | static struct PyModuleDef socketmodule = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4174 | PyModuleDef_HEAD_INIT, |
| 4175 | PySocket_MODULE_NAME, |
| 4176 | socket_doc, |
| 4177 | -1, |
| 4178 | socket_methods, |
| 4179 | NULL, |
| 4180 | NULL, |
| 4181 | NULL, |
| 4182 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4183 | }; |
| 4184 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4185 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4186 | PyInit__socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4187 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4188 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4189 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4190 | if (!os_init()) |
| 4191 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4192 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4193 | Py_TYPE(&sock_type) = &PyType_Type; |
| 4194 | m = PyModule_Create(&socketmodule); |
| 4195 | if (m == NULL) |
| 4196 | return NULL; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4197 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4198 | socket_error = PyErr_NewException("socket.error", |
| 4199 | PyExc_IOError, NULL); |
| 4200 | if (socket_error == NULL) |
| 4201 | return NULL; |
| 4202 | PySocketModuleAPI.error = socket_error; |
| 4203 | Py_INCREF(socket_error); |
| 4204 | PyModule_AddObject(m, "error", socket_error); |
| 4205 | socket_herror = PyErr_NewException("socket.herror", |
| 4206 | socket_error, NULL); |
| 4207 | if (socket_herror == NULL) |
| 4208 | return NULL; |
| 4209 | Py_INCREF(socket_herror); |
| 4210 | PyModule_AddObject(m, "herror", socket_herror); |
| 4211 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4212 | NULL); |
| 4213 | if (socket_gaierror == NULL) |
| 4214 | return NULL; |
| 4215 | Py_INCREF(socket_gaierror); |
| 4216 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4217 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4218 | socket_error, NULL); |
| 4219 | if (socket_timeout == NULL) |
| 4220 | return NULL; |
| 4221 | Py_INCREF(socket_timeout); |
| 4222 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 4223 | Py_INCREF((PyObject *)&sock_type); |
| 4224 | if (PyModule_AddObject(m, "SocketType", |
| 4225 | (PyObject *)&sock_type) != 0) |
| 4226 | return NULL; |
| 4227 | Py_INCREF((PyObject *)&sock_type); |
| 4228 | if (PyModule_AddObject(m, "socket", |
| 4229 | (PyObject *)&sock_type) != 0) |
| 4230 | return NULL; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4231 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4232 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4233 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4234 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4235 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4236 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4237 | Py_INCREF(has_ipv6); |
| 4238 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4239 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4240 | /* Export C API */ |
| 4241 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4242 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4243 | ) != 0) |
| 4244 | return NULL; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4245 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4246 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4247 | #ifdef AF_UNSPEC |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4248 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4249 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4250 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4251 | #ifdef AF_INET6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4252 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4253 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4254 | #if defined(AF_UNIX) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4255 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4256 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4257 | #ifdef AF_AX25 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4258 | /* Amateur Radio AX.25 */ |
| 4259 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4260 | #endif |
| 4261 | #ifdef AF_IPX |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4262 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4263 | #endif |
| 4264 | #ifdef AF_APPLETALK |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4265 | /* Appletalk DDP */ |
| 4266 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4267 | #endif |
| 4268 | #ifdef AF_NETROM |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4269 | /* Amateur radio NetROM */ |
| 4270 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4271 | #endif |
| 4272 | #ifdef AF_BRIDGE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4273 | /* Multiprotocol bridge */ |
| 4274 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4275 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4276 | #ifdef AF_ATMPVC |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4277 | /* ATM PVCs */ |
| 4278 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4279 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4280 | #ifdef AF_AAL5 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4281 | /* Reserved for Werner's ATM */ |
| 4282 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4283 | #endif |
| 4284 | #ifdef AF_X25 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4285 | /* Reserved for X.25 project */ |
| 4286 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4287 | #endif |
| 4288 | #ifdef AF_INET6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4289 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4290 | #endif |
| 4291 | #ifdef AF_ROSE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4292 | /* Amateur Radio X.25 PLP */ |
| 4293 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4294 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4295 | #ifdef AF_DECnet |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4296 | /* Reserved for DECnet project */ |
| 4297 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4298 | #endif |
| 4299 | #ifdef AF_NETBEUI |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4300 | /* Reserved for 802.2LLC project */ |
| 4301 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4302 | #endif |
| 4303 | #ifdef AF_SECURITY |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4304 | /* Security callback pseudo AF */ |
| 4305 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4306 | #endif |
| 4307 | #ifdef AF_KEY |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4308 | /* PF_KEY key management API */ |
| 4309 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4310 | #endif |
| 4311 | #ifdef AF_NETLINK |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4312 | /* */ |
| 4313 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4314 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4315 | #ifdef NETLINK_SKIP |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4316 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4317 | #endif |
| 4318 | #ifdef NETLINK_W1 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4319 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4320 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4321 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4322 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4323 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4324 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4325 | #endif |
| 4326 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4327 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4328 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4329 | #ifdef NETLINK_XFRM |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4330 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4331 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4332 | #ifdef NETLINK_ARPD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4333 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4334 | #endif |
| 4335 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4336 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4337 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4338 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4339 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4340 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 4341 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4342 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4343 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4344 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4345 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4346 | #ifdef AF_ROUTE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4347 | /* Alias to emulate 4.4BSD */ |
| 4348 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4349 | #endif |
| 4350 | #ifdef AF_ASH |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4351 | /* Ash */ |
| 4352 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4353 | #endif |
| 4354 | #ifdef AF_ECONET |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4355 | /* Acorn Econet */ |
| 4356 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4357 | #endif |
| 4358 | #ifdef AF_ATMSVC |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4359 | /* ATM SVCs */ |
| 4360 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4361 | #endif |
| 4362 | #ifdef AF_SNA |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4363 | /* Linux SNA Project (nutters!) */ |
| 4364 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4365 | #endif |
| 4366 | #ifdef AF_IRDA |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4367 | /* IRDA sockets */ |
| 4368 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4369 | #endif |
| 4370 | #ifdef AF_PPPOX |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4371 | /* PPPoX sockets */ |
| 4372 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4373 | #endif |
| 4374 | #ifdef AF_WANPIPE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4375 | /* Wanpipe API Sockets */ |
| 4376 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4377 | #endif |
| 4378 | #ifdef AF_LLC |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4379 | /* Linux LLC */ |
| 4380 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4381 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4382 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4383 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4384 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4385 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4386 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4387 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
| 4388 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4389 | #if !defined(__FreeBSD__) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4390 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
| 4391 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4392 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4393 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4394 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4395 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4396 | 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] | 4397 | #endif |
| 4398 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 4399 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4400 | PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); |
| 4401 | PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); |
| 4402 | PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); |
| 4403 | PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 4404 | PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 4405 | PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 4406 | PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 4407 | PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 4408 | PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4409 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4410 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4411 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4412 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4413 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4414 | /* for addresses */ |
| 4415 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4416 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4417 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4418 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4419 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4420 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4421 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4422 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4423 | /* for setsockopt() */ |
| 4424 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4425 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4426 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4427 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4428 | TIPC_DEST_DROPPABLE); |
| 4429 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4430 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4431 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4432 | TIPC_LOW_IMPORTANCE); |
| 4433 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4434 | TIPC_MEDIUM_IMPORTANCE); |
| 4435 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4436 | TIPC_HIGH_IMPORTANCE); |
| 4437 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4438 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4439 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4440 | /* for subscriptions */ |
| 4441 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4442 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 4443 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4444 | /* doesn't seem to be available everywhere */ |
| 4445 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 4446 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4447 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4448 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4449 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4450 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4451 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4452 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4453 | #endif |
| 4454 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4455 | /* Socket types */ |
| 4456 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4457 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4458 | /* We have incomplete socket support. */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4459 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4460 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4461 | #if defined(SOCK_RDM) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4462 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4463 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4464 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4465 | #ifdef SO_DEBUG |
| 4466 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4467 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4468 | #ifdef SO_ACCEPTCONN |
| 4469 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4470 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4471 | #ifdef SO_REUSEADDR |
| 4472 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4473 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4474 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4475 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4476 | #endif |
| 4477 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4478 | #ifdef SO_KEEPALIVE |
| 4479 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4480 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4481 | #ifdef SO_DONTROUTE |
| 4482 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4483 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4484 | #ifdef SO_BROADCAST |
| 4485 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4486 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4487 | #ifdef SO_USELOOPBACK |
| 4488 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4489 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4490 | #ifdef SO_LINGER |
| 4491 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4492 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4493 | #ifdef SO_OOBINLINE |
| 4494 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4495 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4496 | #ifdef SO_REUSEPORT |
| 4497 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4498 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4499 | #ifdef SO_SNDBUF |
| 4500 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4501 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4502 | #ifdef SO_RCVBUF |
| 4503 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4504 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4505 | #ifdef SO_SNDLOWAT |
| 4506 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4507 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4508 | #ifdef SO_RCVLOWAT |
| 4509 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4510 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4511 | #ifdef SO_SNDTIMEO |
| 4512 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4513 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4514 | #ifdef SO_RCVTIMEO |
| 4515 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4516 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4517 | #ifdef SO_ERROR |
| 4518 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4519 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4520 | #ifdef SO_TYPE |
| 4521 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4522 | #endif |
| 4523 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4524 | /* Maximum number of connections for "listen" */ |
| 4525 | #ifdef SOMAXCONN |
| 4526 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4527 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4528 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4529 | #endif |
| 4530 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4531 | /* Flags for send, recv */ |
| 4532 | #ifdef MSG_OOB |
| 4533 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4534 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4535 | #ifdef MSG_PEEK |
| 4536 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4537 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4538 | #ifdef MSG_DONTROUTE |
| 4539 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4540 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4541 | #ifdef MSG_DONTWAIT |
| 4542 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4543 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4544 | #ifdef MSG_EOR |
| 4545 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4546 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4547 | #ifdef MSG_TRUNC |
| 4548 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4549 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4550 | #ifdef MSG_CTRUNC |
| 4551 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4552 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4553 | #ifdef MSG_WAITALL |
| 4554 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4555 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4556 | #ifdef MSG_BTAG |
| 4557 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4558 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4559 | #ifdef MSG_ETAG |
| 4560 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4561 | #endif |
| 4562 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4563 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4564 | #ifdef SOL_SOCKET |
| 4565 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4566 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4567 | #ifdef SOL_IP |
| 4568 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4569 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4570 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4571 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4572 | #ifdef SOL_IPX |
| 4573 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4574 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4575 | #ifdef SOL_AX25 |
| 4576 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4577 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4578 | #ifdef SOL_ATALK |
| 4579 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4580 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4581 | #ifdef SOL_NETROM |
| 4582 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4583 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4584 | #ifdef SOL_ROSE |
| 4585 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4586 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4587 | #ifdef SOL_TCP |
| 4588 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4589 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4590 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4591 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4592 | #ifdef SOL_UDP |
| 4593 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4594 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4595 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4596 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4597 | #ifdef IPPROTO_IP |
| 4598 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4599 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4600 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4601 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4602 | #ifdef IPPROTO_HOPOPTS |
| 4603 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4604 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4605 | #ifdef IPPROTO_ICMP |
| 4606 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4607 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4608 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4609 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4610 | #ifdef IPPROTO_IGMP |
| 4611 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4612 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4613 | #ifdef IPPROTO_GGP |
| 4614 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4615 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4616 | #ifdef IPPROTO_IPV4 |
| 4617 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4618 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4619 | #ifdef IPPROTO_IPV6 |
| 4620 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 4621 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4622 | #ifdef IPPROTO_IPIP |
| 4623 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4624 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4625 | #ifdef IPPROTO_TCP |
| 4626 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4627 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4628 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4629 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4630 | #ifdef IPPROTO_EGP |
| 4631 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4632 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4633 | #ifdef IPPROTO_PUP |
| 4634 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4635 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4636 | #ifdef IPPROTO_UDP |
| 4637 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4638 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4639 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4640 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4641 | #ifdef IPPROTO_IDP |
| 4642 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4643 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4644 | #ifdef IPPROTO_HELLO |
| 4645 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4646 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4647 | #ifdef IPPROTO_ND |
| 4648 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4649 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4650 | #ifdef IPPROTO_TP |
| 4651 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4652 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4653 | #ifdef IPPROTO_IPV6 |
| 4654 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4655 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4656 | #ifdef IPPROTO_ROUTING |
| 4657 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4658 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4659 | #ifdef IPPROTO_FRAGMENT |
| 4660 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4661 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4662 | #ifdef IPPROTO_RSVP |
| 4663 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4664 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4665 | #ifdef IPPROTO_GRE |
| 4666 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4667 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4668 | #ifdef IPPROTO_ESP |
| 4669 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4670 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4671 | #ifdef IPPROTO_AH |
| 4672 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4673 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4674 | #ifdef IPPROTO_MOBILE |
| 4675 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4676 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4677 | #ifdef IPPROTO_ICMPV6 |
| 4678 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4679 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4680 | #ifdef IPPROTO_NONE |
| 4681 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4682 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4683 | #ifdef IPPROTO_DSTOPTS |
| 4684 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4685 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4686 | #ifdef IPPROTO_XTP |
| 4687 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4688 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4689 | #ifdef IPPROTO_EON |
| 4690 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4691 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4692 | #ifdef IPPROTO_PIM |
| 4693 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4694 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4695 | #ifdef IPPROTO_IPCOMP |
| 4696 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4697 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4698 | #ifdef IPPROTO_VRRP |
| 4699 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4700 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4701 | #ifdef IPPROTO_BIP |
| 4702 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4703 | #endif |
| 4704 | /**/ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4705 | #ifdef IPPROTO_RAW |
| 4706 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4707 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4708 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4709 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4710 | #ifdef IPPROTO_MAX |
| 4711 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4712 | #endif |
| 4713 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4714 | /* Some port configuration */ |
| 4715 | #ifdef IPPORT_RESERVED |
| 4716 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4717 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4718 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4719 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4720 | #ifdef IPPORT_USERRESERVED |
| 4721 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4722 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4723 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4724 | #endif |
| 4725 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4726 | /* Some reserved IP v.4 addresses */ |
| 4727 | #ifdef INADDR_ANY |
| 4728 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4729 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4730 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4731 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4732 | #ifdef INADDR_BROADCAST |
| 4733 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4734 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4735 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4736 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4737 | #ifdef INADDR_LOOPBACK |
| 4738 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4739 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4740 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4741 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4742 | #ifdef INADDR_UNSPEC_GROUP |
| 4743 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4744 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4745 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4746 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4747 | #ifdef INADDR_ALLHOSTS_GROUP |
| 4748 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 4749 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4750 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4751 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4752 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4753 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 4754 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 4755 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4756 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4757 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4758 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4759 | #ifdef INADDR_NONE |
| 4760 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4761 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4762 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4763 | #endif |
| 4764 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4765 | /* IPv4 [gs]etsockopt options */ |
| 4766 | #ifdef IP_OPTIONS |
| 4767 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4768 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4769 | #ifdef IP_HDRINCL |
| 4770 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4771 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4772 | #ifdef IP_TOS |
| 4773 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4774 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4775 | #ifdef IP_TTL |
| 4776 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4777 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4778 | #ifdef IP_RECVOPTS |
| 4779 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4780 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4781 | #ifdef IP_RECVRETOPTS |
| 4782 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4783 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4784 | #ifdef IP_RECVDSTADDR |
| 4785 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4786 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4787 | #ifdef IP_RETOPTS |
| 4788 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4789 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4790 | #ifdef IP_MULTICAST_IF |
| 4791 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4792 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4793 | #ifdef IP_MULTICAST_TTL |
| 4794 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4795 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4796 | #ifdef IP_MULTICAST_LOOP |
| 4797 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4798 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4799 | #ifdef IP_ADD_MEMBERSHIP |
| 4800 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4801 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4802 | #ifdef IP_DROP_MEMBERSHIP |
| 4803 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4804 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4805 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 4806 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 4807 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4808 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4809 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 4810 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 4811 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4812 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4813 | #ifdef IP_MAX_MEMBERSHIPS |
| 4814 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4815 | #endif |
| 4816 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4817 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 4818 | #ifdef IPV6_JOIN_GROUP |
| 4819 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4820 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4821 | #ifdef IPV6_LEAVE_GROUP |
| 4822 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4823 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4824 | #ifdef IPV6_MULTICAST_HOPS |
| 4825 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4826 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4827 | #ifdef IPV6_MULTICAST_IF |
| 4828 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4829 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4830 | #ifdef IPV6_MULTICAST_LOOP |
| 4831 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4832 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4833 | #ifdef IPV6_UNICAST_HOPS |
| 4834 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4835 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4836 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4837 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4838 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4839 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4840 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4841 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4842 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4843 | #endif |
| 4844 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4845 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4846 | #endif |
| 4847 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4848 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4849 | #endif |
| 4850 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4851 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4852 | #endif |
| 4853 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4854 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4855 | #endif |
| 4856 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4857 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4858 | #endif |
| 4859 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4860 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4861 | #endif |
| 4862 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4863 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4864 | #endif |
| 4865 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4866 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4867 | #endif |
| 4868 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4869 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4870 | #endif |
| 4871 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4872 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4873 | #endif |
| 4874 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4875 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4876 | #endif |
| 4877 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4878 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4879 | #endif |
| 4880 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4881 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4882 | #endif |
| 4883 | #ifdef IPV6_RTHDR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4884 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4885 | #endif |
| 4886 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4887 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4888 | #endif |
| 4889 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4890 | 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] | 4891 | #endif |
| 4892 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4893 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4894 | #endif |
| 4895 | #ifdef IPV6_TCLASS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4896 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4897 | #endif |
| 4898 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4899 | 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] | 4900 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4901 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4902 | /* TCP options */ |
| 4903 | #ifdef TCP_NODELAY |
| 4904 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4905 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4906 | #ifdef TCP_MAXSEG |
| 4907 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4908 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4909 | #ifdef TCP_CORK |
| 4910 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4911 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4912 | #ifdef TCP_KEEPIDLE |
| 4913 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4914 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4915 | #ifdef TCP_KEEPINTVL |
| 4916 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4917 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4918 | #ifdef TCP_KEEPCNT |
| 4919 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4920 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4921 | #ifdef TCP_SYNCNT |
| 4922 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4923 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4924 | #ifdef TCP_LINGER2 |
| 4925 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4926 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4927 | #ifdef TCP_DEFER_ACCEPT |
| 4928 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4929 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4930 | #ifdef TCP_WINDOW_CLAMP |
| 4931 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4932 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4933 | #ifdef TCP_INFO |
| 4934 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4935 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4936 | #ifdef TCP_QUICKACK |
| 4937 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4938 | #endif |
| 4939 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4940 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4941 | /* IPX options */ |
| 4942 | #ifdef IPX_TYPE |
| 4943 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4944 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4945 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4946 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4947 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4948 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4949 | #endif |
| 4950 | #ifdef EAI_AGAIN |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4951 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4952 | #endif |
| 4953 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4954 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4955 | #endif |
| 4956 | #ifdef EAI_FAIL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4957 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4958 | #endif |
| 4959 | #ifdef EAI_FAMILY |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4960 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4961 | #endif |
| 4962 | #ifdef EAI_MEMORY |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4963 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4964 | #endif |
| 4965 | #ifdef EAI_NODATA |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4966 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4967 | #endif |
| 4968 | #ifdef EAI_NONAME |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4969 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4970 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4971 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4972 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4973 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4974 | #ifdef EAI_SERVICE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4975 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4976 | #endif |
| 4977 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4978 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4979 | #endif |
| 4980 | #ifdef EAI_SYSTEM |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4981 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4982 | #endif |
| 4983 | #ifdef EAI_BADHINTS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4984 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4985 | #endif |
| 4986 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4987 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4988 | #endif |
| 4989 | #ifdef EAI_MAX |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4990 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4991 | #endif |
| 4992 | #ifdef AI_PASSIVE |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4993 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4994 | #endif |
| 4995 | #ifdef AI_CANONNAME |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4996 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4997 | #endif |
| 4998 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 4999 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5000 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5001 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5002 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5003 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5004 | #ifdef AI_MASK |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5005 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5006 | #endif |
| 5007 | #ifdef AI_ALL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5008 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5009 | #endif |
| 5010 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5011 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5012 | #endif |
| 5013 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5014 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5015 | #endif |
| 5016 | #ifdef AI_V4MAPPED |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5017 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5018 | #endif |
| 5019 | #ifdef AI_DEFAULT |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5020 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5021 | #endif |
| 5022 | #ifdef NI_MAXHOST |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5023 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5024 | #endif |
| 5025 | #ifdef NI_MAXSERV |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5026 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5027 | #endif |
| 5028 | #ifdef NI_NOFQDN |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5029 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5030 | #endif |
| 5031 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5032 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5033 | #endif |
| 5034 | #ifdef NI_NAMEREQD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5035 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5036 | #endif |
| 5037 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5038 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5039 | #endif |
| 5040 | #ifdef NI_DGRAM |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5041 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5042 | #endif |
| 5043 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5044 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5045 | #ifdef SHUT_RD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5046 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5047 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5048 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5049 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5050 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5051 | #endif |
| 5052 | #ifdef SHUT_WR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5053 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5054 | #elif defined(SD_SEND) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5055 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5056 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5057 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5058 | #endif |
| 5059 | #ifdef SHUT_RDWR |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5060 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5061 | #elif defined(SD_BOTH) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5062 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5063 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5064 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5065 | #endif |
| 5066 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 5067 | #ifdef SIO_RCVALL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5068 | { |
| 5069 | PyObject *tmp; |
| 5070 | tmp = PyLong_FromUnsignedLong(SIO_RCVALL); |
| 5071 | if (tmp == NULL) |
| 5072 | return NULL; |
| 5073 | PyModule_AddObject(m, "SIO_RCVALL", tmp); |
| 5074 | } |
| 5075 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5076 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5077 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 5078 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5079 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 5080 | #endif |
| 5081 | #ifdef RCVALL_MAX |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5082 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 762681b | 2008-06-12 23:03:41 +0000 | [diff] [blame] | 5083 | #endif |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 5084 | #endif /* _MSTCPIP_ */ |
| 5085 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5086 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5087 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5088 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5089 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5090 | return m; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5091 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5092 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5093 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5094 | #ifndef HAVE_INET_PTON |
Christian Heimes | 96e7b3d | 2007-11-20 06:51:17 +0000 | [diff] [blame] | 5095 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5096 | |
| 5097 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5098 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5099 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5100 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5101 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5102 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5103 | if (af == AF_INET) { |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 5104 | #if (SIZEOF_INT != 4) |
| 5105 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5106 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5107 | unsigned int packed_addr; |
| 5108 | packed_addr = inet_addr(src); |
| 5109 | if (packed_addr == INADDR_NONE) |
| 5110 | return 0; |
| 5111 | memcpy(dst, &packed_addr, 4); |
| 5112 | return 1; |
| 5113 | } |
| 5114 | /* Should set errno to EAFNOSUPPORT */ |
| 5115 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5118 | const char * |
| 5119 | 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] | 5120 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 5121 | if (af == AF_INET) { |
| 5122 | struct in_addr packed_addr; |
| 5123 | if (size < 16) |
| 5124 | /* Should set errno to ENOSPC. */ |
| 5125 | return NULL; |
| 5126 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5127 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5128 | } |
| 5129 | /* Should set errno to EAFNOSUPPORT */ |
| 5130 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5131 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5132 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5133 | #endif |
Christian Heimes | b615069 | 2007-11-15 23:37:07 +0000 | [diff] [blame] | 5134 | #endif |