Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1 | /* Socket module */ |
| 2 | |
| 3 | /* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5 | This module provides an interface to Berkeley socket IPC. |
| 6 | |
| 7 | Limitations: |
| 8 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 9 | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 10 | portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported |
| 11 | under Linux. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 12 | - No read/write operations (use sendall/recv or makefile instead). |
| 13 | - Additional restrictions apply on some non-Unix platforms (compensated |
| 14 | for by socket.py). |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 16 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 18 | - socket.error: exception raised for socket specific errors |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 19 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 20 | a subclass of socket.error |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 21 | - socket.herror: exception raised for gethostby* errors, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 22 | a subclass of socket.error |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 23 | - socket.fromfd(fd, family, type[, proto]) --> new socket object (created |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 24 | from an existing file descriptor) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 25 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 26 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 27 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 28 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 29 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 30 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 31 | - socket.socket([family[, type [, proto]]]) --> new socket object |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 32 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 33 | - socket.ntohs(16 bit value) --> new int object |
| 34 | - socket.ntohl(32 bit value) --> new int object |
| 35 | - socket.htons(16 bit value) --> new int object |
| 36 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 37 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 38 | --> List of (family, socktype, proto, canonname, sockaddr) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 39 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 40 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 41 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 42 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 43 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 44 | - socket.getdefaulttimeout() -> None | float |
| 45 | - socket.setdefaulttimeout(None | float) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 46 | - an Internet socket address is a pair (hostname, port) |
| 47 | where hostname can be anything recognized by gethostbyname() |
| 48 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 49 | - where a hostname is returned, the dd.dd.dd.dd notation is used |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 50 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 51 | - an AF_PACKET socket address is a tuple containing a string |
| 52 | specifying the ethernet interface and an integer specifying |
| 53 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 54 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 55 | specify packet-type and ha-type/addr. |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 56 | - an AF_TIPC socket address is expressed as |
| 57 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 59 | and scope can be one of: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 60 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 61 | The meaning of v1, v2 and v3 depends on the value of addr_type: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 62 | if addr_type is TIPC_ADDR_NAME: |
| 63 | v1 is the server type |
| 64 | v2 is the port identifier |
| 65 | v3 is ignored |
| 66 | if addr_type is TIPC_ADDR_NAMESEQ: |
| 67 | v1 is the server type |
| 68 | v2 is the lower port number |
| 69 | v3 is the upper port number |
| 70 | if addr_type is TIPC_ADDR_ID: |
| 71 | v1 is the node |
| 72 | v2 is the ref |
| 73 | v3 is ignored |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 76 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 78 | - names starting with sock_ are socket object methods |
| 79 | - names starting with socket_ are module-level functions |
| 80 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 84 | #ifdef __APPLE__ |
| 85 | /* |
| 86 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 87 | * that was build on 10.4 or later to work on that release, weak linking |
| 88 | * comes to the rescue. |
| 89 | */ |
| 90 | # pragma weak inet_aton |
| 91 | #endif |
| 92 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 93 | #include "Python.h" |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 94 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 96 | #undef MAX |
| 97 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 98 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 99 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 100 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 101 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 102 | \n\ |
| 103 | Open a socket of the given type. The family argument specifies the\n\ |
| 104 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 105 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 106 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 107 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 108 | \n\ |
| 109 | A socket object represents one endpoint of a network connection.\n\ |
| 110 | \n\ |
| 111 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 112 | \n\ |
| 113 | accept() -- accept a connection, returning new socket and client address\n\ |
| 114 | bind(addr) -- bind the socket to a local address\n\ |
| 115 | close() -- close the socket\n\ |
| 116 | connect(addr) -- connect the socket to a remote address\n\ |
| 117 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
| 118 | dup() -- return a new socket object identical to the current one [*]\n\ |
| 119 | fileno() -- return underlying file descriptor\n\ |
| 120 | getpeername() -- return remote address [*]\n\ |
| 121 | getsockname() -- return local address\n\ |
| 122 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 123 | gettimeout() -- return timeout or None\n\ |
| 124 | listen(n) -- start listening for incoming connections\n\ |
| 125 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\ |
| 126 | recv(buflen[, flags]) -- receive data\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 127 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 128 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 129 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 130 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 131 | sendall(data[, flags]) -- send all data\n\ |
| 132 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 133 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 134 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 135 | setsockopt(level, optname, value) -- set socket options\n\ |
| 136 | settimeout(None | float) -- set or clear the timeout\n\ |
| 137 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 138 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 139 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 140 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 141 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 142 | I hope some day someone can clean this up please... */ |
| 143 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 144 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 145 | script doesn't get this right, so we hardcode some platform checks below. |
| 146 | On the other hand, not all Linux versions agree, so there the settings |
| 147 | computed by the configure script are needed! */ |
| 148 | |
| 149 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 150 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 151 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 152 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 153 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 155 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 156 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 157 | #endif |
| 158 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 159 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 160 | # if defined(_AIX) || defined(__osf__) |
| 161 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 162 | # elif defined(__sun) || defined(__sgi) |
| 163 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 164 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 165 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 166 | # else |
| 167 | # undef HAVE_GETHOSTBYNAME_R |
| 168 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 169 | #endif |
| 170 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 171 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 172 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 173 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 174 | #endif |
| 175 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 176 | /* To use __FreeBSD_version */ |
| 177 | #ifdef HAVE_SYS_PARAM_H |
| 178 | #include <sys/param.h> |
| 179 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 180 | /* 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] | 181 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 182 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 183 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 184 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 185 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 186 | #define USE_GETADDRINFO_LOCK |
| 187 | #endif |
| 188 | |
| 189 | #ifdef USE_GETADDRINFO_LOCK |
| 190 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 191 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 192 | #else |
| 193 | #define ACQUIRE_GETADDRINFO_LOCK |
| 194 | #define RELEASE_GETADDRINFO_LOCK |
| 195 | #endif |
| 196 | |
| 197 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 198 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 199 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 201 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 202 | # include <types.h> |
| 203 | # include <io.h> |
| 204 | # include <sys/ioctl.h> |
| 205 | # include <utils.h> |
| 206 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 207 | #endif |
| 208 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 209 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 210 | # include <ioctl.h> |
| 211 | #endif |
| 212 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 213 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 214 | # define INCL_DOS |
| 215 | # define INCL_DOSERRORS |
| 216 | # define INCL_NOPMAPI |
| 217 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 218 | #endif |
| 219 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 220 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 221 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 222 | functions are declared correctly if compiling with |
| 223 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 224 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 225 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 226 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 227 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 228 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 229 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 230 | #undef _XOPEN_SOURCE |
| 231 | #include <sys/socket.h> |
| 232 | #include <sys/types.h> |
| 233 | #include <netinet/in.h> |
| 234 | #ifdef _SS_ALIGNSIZE |
| 235 | #define HAVE_GETADDRINFO 1 |
| 236 | #define HAVE_GETNAMEINFO 1 |
| 237 | #endif |
| 238 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 239 | #define HAVE_INET_PTON |
| 240 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 243 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 244 | 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] | 245 | are just busted. Same thing for Solaris. */ |
| 246 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 247 | #define INET_ADDRSTRLEN 16 |
| 248 | #endif |
| 249 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 250 | /* Generic includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 251 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 253 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 254 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 255 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 256 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 257 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 258 | |
| 259 | /* Addressing includes */ |
| 260 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 261 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 262 | |
| 263 | /* Non-MS WINDOWS includes */ |
| 264 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 266 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 267 | # ifdef __BEOS__ |
| 268 | # include <net/netdb.h> |
| 269 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 270 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 271 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 272 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 273 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 274 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 275 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 276 | # ifndef RISCOS |
| 277 | # include <fcntl.h> |
| 278 | # else |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 279 | # include <sys/ioctl.h> |
| 280 | # include <socklib.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 281 | # define NO_DUP |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 282 | int h_errno; /* not used */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 283 | # define INET_ADDRSTRLEN 16 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 284 | # endif |
| 285 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 286 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 287 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 288 | /* MS_WINDOWS includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 289 | # ifdef HAVE_FCNTL_H |
| 290 | # include <fcntl.h> |
| 291 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 292 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 293 | #endif |
| 294 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 295 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 296 | |
| 297 | #ifndef offsetof |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 299 | #endif |
| 300 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 301 | #ifndef O_NONBLOCK |
| 302 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 303 | #endif |
| 304 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 305 | /* include Python's addrinfo.h unless it causes trouble */ |
| 306 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 307 | /* Do not include addinfo.h on some newer IRIX versions. |
| 308 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 309 | * for example, but not by 6.5.10. |
| 310 | */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 311 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 312 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 313 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 314 | */ |
| 315 | #else |
| 316 | # include "addrinfo.h" |
| 317 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 318 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 319 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 320 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 321 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 322 | 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] | 323 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 324 | #endif |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 325 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 326 | #ifdef __APPLE__ |
| 327 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 328 | failure, so we must use the emulation instead of the libinfo |
| 329 | implementation. Unfortunately, performing an autoconf test |
| 330 | for this bug would require DNS access for the machine performing |
| 331 | the configuration, which is not acceptable. Therefore, we |
| 332 | determine the bug just by checking for __APPLE__. If this bug |
| 333 | gets ever fixed, perhaps checking for sys/version.h would be |
| 334 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 335 | #ifndef HAVE_GETNAMEINFO |
| 336 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 337 | Find to check for Jaguar is that it has getnameinfo(), which |
| 338 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 339 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 340 | #endif |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 341 | |
| 342 | #ifdef HAVE_INET_ATON |
| 343 | #define USE_INET_ATON_WEAKLINK |
| 344 | #endif |
| 345 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 346 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 347 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 348 | /* 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] | 349 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 350 | /* avoid clashes with the C library definition of the symbol. */ |
| 351 | #define getaddrinfo fake_getaddrinfo |
| 352 | #define gai_strerror fake_gai_strerror |
| 353 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 354 | #include "getaddrinfo.c" |
| 355 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 356 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 357 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 358 | #include "getnameinfo.c" |
| 359 | #endif |
| 360 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 361 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 362 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 363 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 364 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 365 | #define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 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 | |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 382 | #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 383 | #define USE_BLUETOOTH 1 |
| 384 | #if defined(__FreeBSD__) |
| 385 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 386 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 387 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +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 |
Hye-Shik Chang | 82958f0 | 2007-06-05 18:16:52 +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) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 395 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 396 | #elif defined(__NetBSD__) || defined(__DragonFly__) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 397 | #define sockaddr_l2 sockaddr_bt |
| 398 | #define sockaddr_rc sockaddr_bt |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 399 | #define sockaddr_hci sockaddr_bt |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 400 | #define sockaddr_sco sockaddr_bt |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 401 | #define SOL_HCI BTPROTO_HCI |
| 402 | #define HCI_DATA_DIR SO_HCI_DIRECTION |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 403 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 404 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 405 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 406 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 407 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 408 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 409 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 410 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 411 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 412 | #endif |
| 413 | #endif |
| 414 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 415 | #ifdef __VMS |
| 416 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 417 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 418 | #endif |
| 419 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 420 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 421 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 422 | /* |
| 423 | * Constants for getnameinfo() |
| 424 | */ |
| 425 | #if !defined(NI_MAXHOST) |
| 426 | #define NI_MAXHOST 1025 |
| 427 | #endif |
| 428 | #if !defined(NI_MAXSERV) |
| 429 | #define NI_MAXSERV 32 |
| 430 | #endif |
| 431 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 432 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 433 | a Py prefix (or use CapitalizedWords). Later... */ |
| 434 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 435 | /* Global variable holding the exception type for errors detected |
| 436 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 437 | static PyObject *socket_error; |
| 438 | static PyObject *socket_herror; |
| 439 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 440 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 442 | #ifdef RISCOS |
| 443 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 444 | static int taskwindow; |
| 445 | #endif |
| 446 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 447 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 448 | The sock_type variable contains pointers to various functions, |
| 449 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 450 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 451 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 452 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 453 | #if defined(HAVE_POLL_H) |
| 454 | #include <poll.h> |
| 455 | #elif defined(HAVE_SYS_POLL_H) |
| 456 | #include <sys/poll.h> |
| 457 | #endif |
| 458 | |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 459 | #ifdef HAVE_POLL |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 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 |
Charles-François Natali | fda7b37 | 2011-08-28 16:22:33 +0200 | [diff] [blame] | 464 | /* If there's no timeout left, we don't have to call select, so it's a safe, |
| 465 | * little white lie. */ |
| 466 | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 467 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 468 | |
| 469 | static PyObject* |
| 470 | select_error(void) |
| 471 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 472 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 473 | return NULL; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 476 | /* Convenience function to raise an error according to errno |
| 477 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 479 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 480 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 481 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 482 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 483 | int err_no = WSAGetLastError(); |
| 484 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 485 | recognizes the error codes used by both GetLastError() and |
| 486 | WSAGetLastError */ |
| 487 | if (err_no) |
| 488 | return PyErr_SetExcFromWindowsErr(socket_error, err_no); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 489 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 490 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 491 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 492 | if (sock_errno() != NO_ERROR) { |
| 493 | APIRET rc; |
| 494 | ULONG msglen; |
| 495 | char outbuf[100]; |
| 496 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 497 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 498 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 499 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 500 | myerrorcode - SOCBASEERR + 26, |
| 501 | "mptn.msg", |
| 502 | &msglen); |
| 503 | if (rc == NO_ERROR) { |
| 504 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 506 | /* OS/2 doesn't guarantee a terminator */ |
| 507 | outbuf[msglen] = '\0'; |
| 508 | if (strlen(outbuf) > 0) { |
| 509 | /* If non-empty msg, trim CRLF */ |
| 510 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 511 | while (lastc > outbuf && |
| 512 | isspace(Py_CHARMASK(*lastc))) { |
| 513 | /* Trim trailing whitespace (CRLF) */ |
| 514 | *lastc-- = '\0'; |
| 515 | } |
| 516 | } |
| 517 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 518 | if (v != NULL) { |
| 519 | PyErr_SetObject(socket_error, v); |
| 520 | Py_DECREF(v); |
| 521 | } |
| 522 | return NULL; |
| 523 | } |
| 524 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 525 | #endif |
| 526 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 527 | #if defined(RISCOS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 528 | if (_inet_error.errnum != NULL) { |
| 529 | PyObject *v; |
| 530 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 531 | if (v != NULL) { |
| 532 | PyErr_SetObject(socket_error, v); |
| 533 | Py_DECREF(v); |
| 534 | } |
| 535 | return NULL; |
| 536 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 537 | #endif |
| 538 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 539 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 542 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 543 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 544 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 545 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 546 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 547 | |
| 548 | #ifdef HAVE_HSTRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 549 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 550 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 551 | v = Py_BuildValue("(is)", h_error, "host not found"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 552 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 553 | if (v != NULL) { |
| 554 | PyErr_SetObject(socket_herror, v); |
| 555 | Py_DECREF(v); |
| 556 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 558 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
| 562 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 563 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 564 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 565 | PyObject *v; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 566 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 567 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 568 | /* EAI_SYSTEM is not available on Windows XP. */ |
| 569 | if (error == EAI_SYSTEM) |
| 570 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 571 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 572 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 573 | #ifdef HAVE_GAI_STRERROR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 574 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 575 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 576 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 577 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 578 | if (v != NULL) { |
| 579 | PyErr_SetObject(socket_gaierror, v); |
| 580 | Py_DECREF(v); |
| 581 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 583 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 586 | #ifdef __VMS |
| 587 | /* Function to send in segments */ |
| 588 | static int |
| 589 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 590 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 591 | int n = 0; |
| 592 | int remaining = len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 594 | while (remaining > 0) { |
| 595 | unsigned int segment; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 597 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 598 | n = send(sock_fd, buf, segment, flags); |
| 599 | if (n < 0) { |
| 600 | return n; |
| 601 | } |
| 602 | remaining -= segment; |
| 603 | buf += segment; |
| 604 | } /* end while */ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 606 | return len; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 607 | } |
| 608 | #endif |
| 609 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 610 | /* Function to perform the setting of socket blocking mode |
| 611 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 612 | static int |
| 613 | internal_setblocking(PySocketSockObject *s, int block) |
| 614 | { |
| 615 | #ifndef RISCOS |
| 616 | #ifndef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 617 | int delay_flag; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 618 | #endif |
| 619 | #endif |
| 620 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 621 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 622 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 623 | block = !block; |
| 624 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 625 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 626 | #else |
| 627 | #ifndef RISCOS |
| 628 | #ifndef MS_WINDOWS |
| 629 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 630 | block = !block; |
| 631 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 632 | #elif defined(__VMS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 633 | block = !block; |
| 634 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 635 | #else /* !PYOS_OS2 && !__VMS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 636 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 637 | if (block) |
| 638 | delay_flag &= (~O_NONBLOCK); |
| 639 | else |
| 640 | delay_flag |= O_NONBLOCK; |
| 641 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 642 | #endif /* !PYOS_OS2 */ |
| 643 | #else /* MS_WINDOWS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 644 | block = !block; |
| 645 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 646 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 647 | #else /* RISCOS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 648 | block = !block; |
| 649 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 650 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 651 | #endif /* __BEOS__ */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 652 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 654 | /* Since these don't return anything */ |
| 655 | return 1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 658 | /* 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] | 659 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 660 | This does not raise an exception; we'll let our caller do that |
| 661 | after they've reacquired the interpreter lock. |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 662 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 663 | static int |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 664 | internal_select(PySocketSockObject *s, int writing) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 665 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 666 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 667 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 668 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 669 | if (s->sock_timeout <= 0.0) |
| 670 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 672 | /* Guard against closed socket */ |
| 673 | if (s->sock_fd < 0) |
| 674 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 676 | /* Prefer poll, if available, since you can poll() any fd |
| 677 | * which can't be done with select(). */ |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 678 | #ifdef HAVE_POLL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 679 | { |
| 680 | struct pollfd pollfd; |
| 681 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 683 | pollfd.fd = s->sock_fd; |
| 684 | pollfd.events = writing ? POLLOUT : POLLIN; |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 686 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 687 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 688 | n = poll(&pollfd, 1, timeout); |
| 689 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 690 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 691 | { |
| 692 | /* Construct the arguments to select */ |
| 693 | fd_set fds; |
| 694 | struct timeval tv; |
| 695 | tv.tv_sec = (int)s->sock_timeout; |
| 696 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 697 | FD_ZERO(&fds); |
| 698 | FD_SET(s->sock_fd, &fds); |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 699 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 700 | /* See if the socket is ready */ |
| 701 | if (writing) |
| 702 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 703 | else |
| 704 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 705 | } |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 706 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 707 | |
| 708 | if (n < 0) |
| 709 | return -1; |
| 710 | if (n == 0) |
| 711 | return 1; |
| 712 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 715 | /* Initialize a new socket object. */ |
| 716 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 717 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 718 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 719 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 720 | init_sockobject(PySocketSockObject *s, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 721 | SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 722 | { |
| 723 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 724 | int block = 1; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 725 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 726 | s->sock_fd = fd; |
| 727 | s->sock_family = family; |
| 728 | s->sock_type = type; |
| 729 | s->sock_proto = proto; |
| 730 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 732 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 734 | if (defaulttimeout >= 0.0) |
| 735 | internal_setblocking(s, 0); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 736 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 737 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 738 | if (taskwindow) |
| 739 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 740 | #endif |
| 741 | } |
| 742 | |
| 743 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 744 | /* Create a new socket object. |
| 745 | This just creates the object and initializes it. |
| 746 | If the creation fails, return NULL and set an exception (implicit |
| 747 | in NEWOBJ()). */ |
| 748 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 749 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 750 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 751 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 752 | PySocketSockObject *s; |
| 753 | s = (PySocketSockObject *) |
| 754 | PyType_GenericNew(&sock_type, NULL, NULL); |
| 755 | if (s != NULL) |
| 756 | init_sockobject(s, fd, family, type, proto); |
| 757 | return s; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 761 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 762 | thread to be in gethostbyname or getaddrinfo */ |
| 763 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 764 | PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 765 | #endif |
| 766 | |
| 767 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 768 | /* Convert a string specifying a host name or one of a few symbolic |
| 769 | names to a numeric IP address. This usually calls gethostbyname() |
| 770 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 771 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 772 | an error occurred; then an exception is raised. */ |
| 773 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 774 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 775 | 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] | 776 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 777 | struct addrinfo hints, *res; |
| 778 | int error; |
| 779 | int d1, d2, d3, d4; |
| 780 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 781 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 782 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 783 | if (name[0] == '\0') { |
| 784 | int siz; |
| 785 | memset(&hints, 0, sizeof(hints)); |
| 786 | hints.ai_family = af; |
| 787 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 788 | hints.ai_flags = AI_PASSIVE; |
| 789 | Py_BEGIN_ALLOW_THREADS |
| 790 | ACQUIRE_GETADDRINFO_LOCK |
| 791 | error = getaddrinfo(NULL, "0", &hints, &res); |
| 792 | Py_END_ALLOW_THREADS |
| 793 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 794 | *are* safe regarding their return value, ie. that a |
| 795 | subsequent call to getaddrinfo() does not destroy the |
| 796 | outcome of the first call. */ |
| 797 | RELEASE_GETADDRINFO_LOCK |
| 798 | if (error) { |
| 799 | set_gaierror(error); |
| 800 | return -1; |
| 801 | } |
| 802 | switch (res->ai_family) { |
| 803 | case AF_INET: |
| 804 | siz = 4; |
| 805 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 806 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 807 | case AF_INET6: |
| 808 | siz = 16; |
| 809 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 810 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 811 | default: |
| 812 | freeaddrinfo(res); |
| 813 | PyErr_SetString(socket_error, |
| 814 | "unsupported address family"); |
| 815 | return -1; |
| 816 | } |
| 817 | if (res->ai_next) { |
| 818 | freeaddrinfo(res); |
| 819 | PyErr_SetString(socket_error, |
| 820 | "wildcard resolved to multiple address"); |
| 821 | return -1; |
| 822 | } |
| 823 | if (res->ai_addrlen < addr_ret_size) |
| 824 | addr_ret_size = res->ai_addrlen; |
| 825 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 826 | freeaddrinfo(res); |
| 827 | return siz; |
| 828 | } |
| 829 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 830 | struct sockaddr_in *sin; |
| 831 | if (af != AF_INET && af != AF_UNSPEC) { |
| 832 | PyErr_SetString(socket_error, |
| 833 | "address family mismatched"); |
| 834 | return -1; |
| 835 | } |
| 836 | sin = (struct sockaddr_in *)addr_ret; |
| 837 | memset((void *) sin, '\0', sizeof(*sin)); |
| 838 | sin->sin_family = AF_INET; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 839 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 840 | sin->sin_len = sizeof(*sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 841 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 842 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 843 | return sizeof(sin->sin_addr); |
| 844 | } |
| 845 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 846 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 847 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 848 | struct sockaddr_in *sin; |
| 849 | sin = (struct sockaddr_in *)addr_ret; |
| 850 | sin->sin_addr.s_addr = htonl( |
| 851 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 852 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 853 | sin->sin_family = AF_INET; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 854 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 855 | sin->sin_len = sizeof(*sin); |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 856 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 857 | return 4; |
| 858 | } |
| 859 | memset(&hints, 0, sizeof(hints)); |
| 860 | hints.ai_family = af; |
| 861 | Py_BEGIN_ALLOW_THREADS |
| 862 | ACQUIRE_GETADDRINFO_LOCK |
| 863 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 864 | #if defined(__digital__) && defined(__unix__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 865 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 866 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 867 | if no address family is given. Assume IPv4 for now.*/ |
| 868 | hints.ai_family = AF_INET; |
| 869 | error = getaddrinfo(name, NULL, &hints, &res); |
| 870 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 871 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 872 | Py_END_ALLOW_THREADS |
| 873 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 874 | if (error) { |
| 875 | set_gaierror(error); |
| 876 | return -1; |
| 877 | } |
| 878 | if (res->ai_addrlen < addr_ret_size) |
| 879 | addr_ret_size = res->ai_addrlen; |
| 880 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 881 | freeaddrinfo(res); |
| 882 | switch (addr_ret->sa_family) { |
| 883 | case AF_INET: |
| 884 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 885 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 886 | case AF_INET6: |
| 887 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 888 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 889 | default: |
| 890 | PyErr_SetString(socket_error, "unknown address family"); |
| 891 | return -1; |
| 892 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 895 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 896 | /* Create a string object representing an IP address. |
| 897 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 898 | size numbers). */ |
| 899 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 900 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 901 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 902 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 903 | char buf[NI_MAXHOST]; |
| 904 | int error; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 906 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 907 | NI_NUMERICHOST); |
| 908 | if (error) { |
| 909 | set_gaierror(error); |
| 910 | return NULL; |
| 911 | } |
| 912 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 916 | #ifdef USE_BLUETOOTH |
| 917 | /* Convert a string representation of a Bluetooth address into a numeric |
| 918 | address. Returns the length (6), or raises an exception and returns -1 if |
| 919 | an error occurred. */ |
| 920 | |
| 921 | static int |
| 922 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 923 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 924 | unsigned int b0, b1, b2, b3, b4, b5; |
| 925 | char ch; |
| 926 | int n; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 928 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 929 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 930 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 931 | bdaddr->b[0] = b0; |
| 932 | bdaddr->b[1] = b1; |
| 933 | bdaddr->b[2] = b2; |
| 934 | bdaddr->b[3] = b3; |
| 935 | bdaddr->b[4] = b4; |
| 936 | bdaddr->b[5] = b5; |
| 937 | return 6; |
| 938 | } else { |
| 939 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 940 | return -1; |
| 941 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* Create a string representation of the Bluetooth address. This is always a |
| 945 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 946 | value (zero padded if necessary). */ |
| 947 | |
| 948 | static PyObject * |
| 949 | makebdaddr(bdaddr_t *bdaddr) |
| 950 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 951 | char buf[(6 * 2) + 5 + 1]; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 953 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 954 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 955 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 956 | return PyString_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 957 | } |
| 958 | #endif |
| 959 | |
| 960 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 961 | /* Create an object representing the given socket address, |
| 962 | suitable for passing it back to bind(), connect() etc. |
| 963 | The family field of the sockaddr structure is inspected |
| 964 | to determine what kind of address it really is. */ |
| 965 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 966 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 967 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 968 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 969 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 970 | if (addrlen == 0) { |
| 971 | /* No address -- may be recvfrom() from known socket */ |
| 972 | Py_INCREF(Py_None); |
| 973 | return Py_None; |
| 974 | } |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 975 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 976 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 977 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
| 978 | addr->sa_family = AF_INET; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 979 | #endif |
| 980 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 981 | switch (addr->sa_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 983 | case AF_INET: |
| 984 | { |
| 985 | struct sockaddr_in *a; |
| 986 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 987 | PyObject *ret = NULL; |
| 988 | if (addrobj) { |
| 989 | a = (struct sockaddr_in *)addr; |
| 990 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 991 | Py_DECREF(addrobj); |
| 992 | } |
| 993 | return ret; |
| 994 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 995 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 996 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 997 | case AF_UNIX: |
| 998 | { |
| 999 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1000 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1001 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1002 | addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 1003 | return PyString_FromStringAndSize(a->sun_path, |
| 1004 | addrlen); |
| 1005 | } |
| 1006 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1007 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1008 | { |
| 1009 | /* regular NULL-terminated string */ |
| 1010 | return PyString_FromString(a->sun_path); |
| 1011 | } |
| 1012 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1013 | #endif /* AF_UNIX */ |
| 1014 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1015 | #if defined(AF_NETLINK) |
| 1016 | case AF_NETLINK: |
| 1017 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1018 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 1019 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1020 | } |
| 1021 | #endif /* AF_NETLINK */ |
| 1022 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1023 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1024 | case AF_INET6: |
| 1025 | { |
| 1026 | struct sockaddr_in6 *a; |
| 1027 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1028 | PyObject *ret = NULL; |
| 1029 | if (addrobj) { |
| 1030 | a = (struct sockaddr_in6 *)addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1031 | ret = Py_BuildValue("OiII", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1032 | addrobj, |
| 1033 | ntohs(a->sin6_port), |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1034 | ntohl(a->sin6_flowinfo), |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1035 | a->sin6_scope_id); |
| 1036 | Py_DECREF(addrobj); |
| 1037 | } |
| 1038 | return ret; |
| 1039 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1040 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1041 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1042 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1043 | case AF_BLUETOOTH: |
| 1044 | switch (proto) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1046 | case BTPROTO_L2CAP: |
| 1047 | { |
| 1048 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1049 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1050 | PyObject *ret = NULL; |
| 1051 | if (addrobj) { |
| 1052 | ret = Py_BuildValue("Oi", |
| 1053 | addrobj, |
| 1054 | _BT_L2_MEMB(a, psm)); |
| 1055 | Py_DECREF(addrobj); |
| 1056 | } |
| 1057 | return ret; |
| 1058 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1060 | case BTPROTO_RFCOMM: |
| 1061 | { |
| 1062 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1063 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1064 | PyObject *ret = NULL; |
| 1065 | if (addrobj) { |
| 1066 | ret = Py_BuildValue("Oi", |
| 1067 | addrobj, |
| 1068 | _BT_RC_MEMB(a, channel)); |
| 1069 | Py_DECREF(addrobj); |
| 1070 | } |
| 1071 | return ret; |
| 1072 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1073 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1074 | case BTPROTO_HCI: |
| 1075 | { |
| 1076 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1077 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1078 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
| 1079 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1080 | PyObject *ret = NULL; |
| 1081 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1082 | return ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1083 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1084 | } |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1085 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1086 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1087 | case BTPROTO_SCO: |
| 1088 | { |
| 1089 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1090 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1091 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1092 | #endif |
| 1093 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1094 | default: |
| 1095 | PyErr_SetString(PyExc_ValueError, |
| 1096 | "Unknown Bluetooth protocol"); |
| 1097 | return NULL; |
| 1098 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1099 | #endif |
| 1100 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1101 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1102 | case AF_PACKET: |
| 1103 | { |
| 1104 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1105 | char *ifname = ""; |
| 1106 | struct ifreq ifr; |
| 1107 | /* need to look up interface name give index */ |
| 1108 | if (a->sll_ifindex) { |
| 1109 | ifr.ifr_ifindex = a->sll_ifindex; |
| 1110 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1111 | ifname = ifr.ifr_name; |
| 1112 | } |
| 1113 | return Py_BuildValue("shbhs#", |
| 1114 | ifname, |
| 1115 | ntohs(a->sll_protocol), |
| 1116 | a->sll_pkttype, |
| 1117 | a->sll_hatype, |
| 1118 | a->sll_addr, |
| 1119 | a->sll_halen); |
| 1120 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1121 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1122 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1123 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1124 | case AF_TIPC: |
| 1125 | { |
| 1126 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1127 | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1128 | return Py_BuildValue("IIIII", |
| 1129 | a->addrtype, |
| 1130 | a->addr.nameseq.type, |
| 1131 | a->addr.nameseq.lower, |
| 1132 | a->addr.nameseq.upper, |
| 1133 | a->scope); |
| 1134 | } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1135 | return Py_BuildValue("IIIII", |
| 1136 | a->addrtype, |
| 1137 | a->addr.name.name.type, |
| 1138 | a->addr.name.name.instance, |
| 1139 | a->addr.name.name.instance, |
| 1140 | a->scope); |
| 1141 | } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1142 | return Py_BuildValue("IIIII", |
| 1143 | a->addrtype, |
| 1144 | a->addr.id.node, |
| 1145 | a->addr.id.ref, |
| 1146 | 0, |
| 1147 | a->scope); |
| 1148 | } else { |
| 1149 | PyErr_SetString(PyExc_ValueError, |
| 1150 | "Invalid address type"); |
| 1151 | return NULL; |
| 1152 | } |
| 1153 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1154 | #endif |
| 1155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1156 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1157 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1158 | default: |
| 1159 | /* If we don't know the address family, don't raise an |
| 1160 | exception -- return it as a tuple. */ |
| 1161 | return Py_BuildValue("is#", |
| 1162 | addr->sa_family, |
| 1163 | addr->sa_data, |
| 1164 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1166 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1169 | |
| 1170 | /* Parse a socket address argument according to the socket object's |
| 1171 | address family. Return 1 if the address was in the proper format, |
| 1172 | 0 of not. The address is returned through addr_ret, its length |
| 1173 | through len_ret. */ |
| 1174 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1175 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1176 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1177 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1178 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1179 | switch (s->sock_family) { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1180 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1181 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1182 | case AF_UNIX: |
| 1183 | { |
| 1184 | struct sockaddr_un* addr; |
| 1185 | char *path; |
| 1186 | int len; |
| 1187 | if (!PyArg_Parse(args, "t#", &path, &len)) |
| 1188 | return 0; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1190 | addr = (struct sockaddr_un*)addr_ret; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1191 | #ifdef linux |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1192 | if (len > 0 && path[0] == 0) { |
| 1193 | /* Linux abstract namespace extension */ |
| 1194 | if (len > sizeof addr->sun_path) { |
| 1195 | PyErr_SetString(socket_error, |
| 1196 | "AF_UNIX path too long"); |
| 1197 | return 0; |
| 1198 | } |
| 1199 | } |
| 1200 | else |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1201 | #endif /* linux */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1202 | { |
| 1203 | /* regular NULL-terminated string */ |
| 1204 | if (len >= sizeof addr->sun_path) { |
| 1205 | PyErr_SetString(socket_error, |
| 1206 | "AF_UNIX path too long"); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | addr->sun_path[len] = 0; |
| 1210 | } |
| 1211 | addr->sun_family = s->sock_family; |
| 1212 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1213 | #if defined(PYOS_OS2) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1214 | *len_ret = sizeof(*addr); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1215 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1216 | *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1217 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1218 | return 1; |
| 1219 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1220 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1221 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1222 | #if defined(AF_NETLINK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1223 | case AF_NETLINK: |
| 1224 | { |
| 1225 | struct sockaddr_nl* addr; |
| 1226 | int pid, groups; |
| 1227 | addr = (struct sockaddr_nl *)addr_ret; |
| 1228 | if (!PyTuple_Check(args)) { |
| 1229 | PyErr_Format( |
| 1230 | PyExc_TypeError, |
| 1231 | "getsockaddrarg: " |
| 1232 | "AF_NETLINK address must be tuple, not %.500s", |
| 1233 | Py_TYPE(args)->tp_name); |
| 1234 | return 0; |
| 1235 | } |
| 1236 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1237 | return 0; |
| 1238 | addr->nl_family = AF_NETLINK; |
| 1239 | addr->nl_pid = pid; |
| 1240 | addr->nl_groups = groups; |
| 1241 | *len_ret = sizeof(*addr); |
| 1242 | return 1; |
| 1243 | } |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1244 | #endif |
| 1245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1246 | case AF_INET: |
| 1247 | { |
| 1248 | struct sockaddr_in* addr; |
| 1249 | char *host; |
| 1250 | int port, result; |
| 1251 | if (!PyTuple_Check(args)) { |
| 1252 | PyErr_Format( |
| 1253 | PyExc_TypeError, |
| 1254 | "getsockaddrarg: " |
| 1255 | "AF_INET address must be tuple, not %.500s", |
| 1256 | Py_TYPE(args)->tp_name); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1260 | "idna", &host, &port)) |
| 1261 | return 0; |
| 1262 | addr=(struct sockaddr_in*)addr_ret; |
| 1263 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1264 | sizeof(*addr), AF_INET); |
| 1265 | PyMem_Free(host); |
| 1266 | if (result < 0) |
| 1267 | return 0; |
| 1268 | if (port < 0 || port > 0xffff) { |
| 1269 | PyErr_SetString( |
| 1270 | PyExc_OverflowError, |
| 1271 | "getsockaddrarg: port must be 0-65535."); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | addr->sin_family = AF_INET; |
| 1275 | addr->sin_port = htons((short)port); |
| 1276 | *len_ret = sizeof *addr; |
| 1277 | return 1; |
| 1278 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1279 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1280 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1281 | case AF_INET6: |
| 1282 | { |
| 1283 | struct sockaddr_in6* addr; |
| 1284 | char *host; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1285 | int port, result; |
| 1286 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1287 | flowinfo = scope_id = 0; |
| 1288 | if (!PyTuple_Check(args)) { |
| 1289 | PyErr_Format( |
| 1290 | PyExc_TypeError, |
| 1291 | "getsockaddrarg: " |
| 1292 | "AF_INET6 address must be tuple, not %.500s", |
| 1293 | Py_TYPE(args)->tp_name); |
| 1294 | return 0; |
| 1295 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1296 | if (!PyArg_ParseTuple(args, "eti|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1297 | "idna", &host, &port, &flowinfo, |
| 1298 | &scope_id)) { |
| 1299 | return 0; |
| 1300 | } |
| 1301 | addr = (struct sockaddr_in6*)addr_ret; |
| 1302 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1303 | sizeof(*addr), AF_INET6); |
| 1304 | PyMem_Free(host); |
| 1305 | if (result < 0) |
| 1306 | return 0; |
| 1307 | if (port < 0 || port > 0xffff) { |
| 1308 | PyErr_SetString( |
| 1309 | PyExc_OverflowError, |
| 1310 | "getsockaddrarg: port must be 0-65535."); |
| 1311 | return 0; |
| 1312 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1313 | if (flowinfo < 0 || flowinfo > 0xfffff) { |
| 1314 | PyErr_SetString( |
| 1315 | PyExc_OverflowError, |
| 1316 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 1317 | return 0; |
| 1318 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1319 | addr->sin6_family = s->sock_family; |
| 1320 | addr->sin6_port = htons((short)port); |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 1321 | addr->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1322 | addr->sin6_scope_id = scope_id; |
| 1323 | *len_ret = sizeof *addr; |
| 1324 | return 1; |
| 1325 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1328 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1329 | case AF_BLUETOOTH: |
| 1330 | { |
| 1331 | switch (s->sock_proto) { |
| 1332 | case BTPROTO_L2CAP: |
| 1333 | { |
| 1334 | struct sockaddr_l2 *addr; |
| 1335 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1337 | addr = (struct sockaddr_l2 *)addr_ret; |
| 1338 | memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1339 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1340 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1341 | &_BT_L2_MEMB(addr, psm))) { |
| 1342 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1343 | "wrong format"); |
| 1344 | return 0; |
| 1345 | } |
| 1346 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1347 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1348 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1349 | *len_ret = sizeof *addr; |
| 1350 | return 1; |
| 1351 | } |
| 1352 | case BTPROTO_RFCOMM: |
| 1353 | { |
| 1354 | struct sockaddr_rc *addr; |
| 1355 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1356 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1357 | addr = (struct sockaddr_rc *)addr_ret; |
| 1358 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1359 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1360 | &_BT_RC_MEMB(addr, channel))) { |
| 1361 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1362 | "wrong format"); |
| 1363 | return 0; |
| 1364 | } |
| 1365 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1366 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1367 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1368 | *len_ret = sizeof *addr; |
| 1369 | return 1; |
| 1370 | } |
| 1371 | case BTPROTO_HCI: |
| 1372 | { |
| 1373 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1374 | #if defined(__NetBSD__) || defined(__DragonFly__) |
| 1375 | char *straddr = PyBytes_AS_STRING(args); |
| 1376 | |
| 1377 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1378 | if (straddr == NULL) { |
| 1379 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1380 | "wrong format"); |
| 1381 | return 0; |
| 1382 | } |
| 1383 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
| 1384 | return 0; |
| 1385 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1386 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1387 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1388 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1389 | "wrong format"); |
| 1390 | return 0; |
| 1391 | } |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 1392 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1393 | *len_ret = sizeof *addr; |
| 1394 | return 1; |
| 1395 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1396 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1397 | case BTPROTO_SCO: |
| 1398 | { |
| 1399 | struct sockaddr_sco *addr; |
| 1400 | char *straddr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1401 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1402 | addr = (struct sockaddr_sco *)addr_ret; |
| 1403 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1404 | straddr = PyString_AsString(args); |
| 1405 | if (straddr == NULL) { |
| 1406 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1407 | "wrong format"); |
| 1408 | return 0; |
| 1409 | } |
| 1410 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1411 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1413 | *len_ret = sizeof *addr; |
| 1414 | return 1; |
| 1415 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1416 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1417 | default: |
| 1418 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1419 | return 0; |
| 1420 | } |
| 1421 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1422 | #endif |
| 1423 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 1424 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1425 | case AF_PACKET: |
| 1426 | { |
| 1427 | struct sockaddr_ll* addr; |
| 1428 | struct ifreq ifr; |
| 1429 | char *interfaceName; |
| 1430 | int protoNumber; |
| 1431 | int hatype = 0; |
| 1432 | int pkttype = 0; |
| 1433 | char *haddr = NULL; |
| 1434 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1436 | if (!PyTuple_Check(args)) { |
| 1437 | PyErr_Format( |
| 1438 | PyExc_TypeError, |
| 1439 | "getsockaddrarg: " |
| 1440 | "AF_PACKET address must be tuple, not %.500s", |
| 1441 | Py_TYPE(args)->tp_name); |
| 1442 | return 0; |
| 1443 | } |
| 1444 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1445 | &protoNumber, &pkttype, &hatype, |
| 1446 | &haddr, &halen)) |
| 1447 | return 0; |
| 1448 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1449 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1450 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1451 | s->errorhandler(); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | if (halen > 8) { |
| 1455 | PyErr_SetString(PyExc_ValueError, |
| 1456 | "Hardware address must be 8 bytes or less"); |
| 1457 | return 0; |
| 1458 | } |
| 1459 | if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1460 | PyErr_SetString( |
| 1461 | PyExc_OverflowError, |
| 1462 | "getsockaddrarg: protoNumber must be 0-65535."); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | addr = (struct sockaddr_ll*)addr_ret; |
| 1466 | addr->sll_family = AF_PACKET; |
| 1467 | addr->sll_protocol = htons((short)protoNumber); |
| 1468 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1469 | addr->sll_pkttype = pkttype; |
| 1470 | addr->sll_hatype = hatype; |
| 1471 | if (halen != 0) { |
| 1472 | memcpy(&addr->sll_addr, haddr, halen); |
| 1473 | } |
| 1474 | addr->sll_halen = halen; |
| 1475 | *len_ret = sizeof *addr; |
| 1476 | return 1; |
| 1477 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1478 | #endif |
| 1479 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1480 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1481 | case AF_TIPC: |
| 1482 | { |
| 1483 | unsigned int atype, v1, v2, v3; |
| 1484 | unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1485 | struct sockaddr_tipc *addr; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1487 | if (!PyTuple_Check(args)) { |
| 1488 | PyErr_Format( |
| 1489 | PyExc_TypeError, |
| 1490 | "getsockaddrarg: " |
| 1491 | "AF_TIPC address must be tuple, not %.500s", |
| 1492 | Py_TYPE(args)->tp_name); |
| 1493 | return 0; |
| 1494 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1495 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1496 | if (!PyArg_ParseTuple(args, |
| 1497 | "IIII|I;Invalid TIPC address format", |
| 1498 | &atype, &v1, &v2, &v3, &scope)) |
| 1499 | return 0; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1500 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1501 | addr = (struct sockaddr_tipc *) addr_ret; |
| 1502 | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1504 | addr->family = AF_TIPC; |
| 1505 | addr->scope = scope; |
| 1506 | addr->addrtype = atype; |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1508 | if (atype == TIPC_ADDR_NAMESEQ) { |
| 1509 | addr->addr.nameseq.type = v1; |
| 1510 | addr->addr.nameseq.lower = v2; |
| 1511 | addr->addr.nameseq.upper = v3; |
| 1512 | } else if (atype == TIPC_ADDR_NAME) { |
| 1513 | addr->addr.name.name.type = v1; |
| 1514 | addr->addr.name.name.instance = v2; |
| 1515 | } else if (atype == TIPC_ADDR_ID) { |
| 1516 | addr->addr.id.node = v1; |
| 1517 | addr->addr.id.ref = v2; |
| 1518 | } else { |
| 1519 | /* Shouldn't happen */ |
| 1520 | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1521 | return 0; |
| 1522 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1523 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1524 | *len_ret = sizeof(*addr); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1526 | return 1; |
| 1527 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1528 | #endif |
| 1529 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1530 | /* More cases here... */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1531 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1532 | default: |
| 1533 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1534 | return 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1535 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1536 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1539 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1540 | /* 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] | 1541 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1542 | through len_ret. */ |
| 1543 | |
| 1544 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1545 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1546 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1547 | switch (s->sock_family) { |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1548 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1549 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1550 | case AF_UNIX: |
| 1551 | { |
| 1552 | *len_ret = sizeof (struct sockaddr_un); |
| 1553 | return 1; |
| 1554 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1555 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1556 | #if defined(AF_NETLINK) |
| 1557 | case AF_NETLINK: |
| 1558 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1559 | *len_ret = sizeof (struct sockaddr_nl); |
| 1560 | return 1; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1561 | } |
| 1562 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1564 | case AF_INET: |
| 1565 | { |
| 1566 | *len_ret = sizeof (struct sockaddr_in); |
| 1567 | return 1; |
| 1568 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1569 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1570 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1571 | case AF_INET6: |
| 1572 | { |
| 1573 | *len_ret = sizeof (struct sockaddr_in6); |
| 1574 | return 1; |
| 1575 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1576 | #endif |
| 1577 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1578 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1579 | case AF_BLUETOOTH: |
| 1580 | { |
| 1581 | switch(s->sock_proto) |
| 1582 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1583 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1584 | case BTPROTO_L2CAP: |
| 1585 | *len_ret = sizeof (struct sockaddr_l2); |
| 1586 | return 1; |
| 1587 | case BTPROTO_RFCOMM: |
| 1588 | *len_ret = sizeof (struct sockaddr_rc); |
| 1589 | return 1; |
| 1590 | case BTPROTO_HCI: |
| 1591 | *len_ret = sizeof (struct sockaddr_hci); |
| 1592 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1593 | #if !defined(__FreeBSD__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1594 | case BTPROTO_SCO: |
| 1595 | *len_ret = sizeof (struct sockaddr_sco); |
| 1596 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1597 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1598 | default: |
| 1599 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1600 | "unknown BT protocol"); |
| 1601 | return 0; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1602 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1603 | } |
| 1604 | } |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1605 | #endif |
| 1606 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1607 | #ifdef HAVE_NETPACKET_PACKET_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1608 | case AF_PACKET: |
| 1609 | { |
| 1610 | *len_ret = sizeof (struct sockaddr_ll); |
| 1611 | return 1; |
| 1612 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1613 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1614 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1615 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1616 | case AF_TIPC: |
| 1617 | { |
| 1618 | *len_ret = sizeof (struct sockaddr_tipc); |
| 1619 | return 1; |
| 1620 | } |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1621 | #endif |
| 1622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1623 | /* More cases here... */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1625 | default: |
| 1626 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1627 | return 0; |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1628 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1629 | } |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1633 | /* s.accept() method */ |
| 1634 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1635 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1636 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1637 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1638 | sock_addr_t addrbuf; |
| 1639 | SOCKET_T newfd; |
| 1640 | socklen_t addrlen; |
| 1641 | PyObject *sock = NULL; |
| 1642 | PyObject *addr = NULL; |
| 1643 | PyObject *res = NULL; |
| 1644 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1645 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1646 | if (!getsockaddrlen(s, &addrlen)) |
| 1647 | return NULL; |
| 1648 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1649 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1650 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1651 | newfd = INVALID_SOCKET; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1652 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1653 | newfd = -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1654 | #endif |
| 1655 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1656 | if (!IS_SELECTABLE(s)) |
| 1657 | return select_error(); |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1659 | Py_BEGIN_ALLOW_THREADS |
| 1660 | timeout = internal_select(s, 0); |
| 1661 | if (!timeout) |
| 1662 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1663 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1665 | if (timeout == 1) { |
| 1666 | PyErr_SetString(socket_timeout, "timed out"); |
| 1667 | return NULL; |
| 1668 | } |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1669 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1670 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1671 | if (newfd == INVALID_SOCKET) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1672 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1673 | if (newfd < 0) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1674 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1675 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1676 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1677 | /* Create the new object with unspecified family, |
| 1678 | to avoid calls to bind() etc. on it. */ |
| 1679 | sock = (PyObject *) new_sockobject(newfd, |
| 1680 | s->sock_family, |
| 1681 | s->sock_type, |
| 1682 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1684 | if (sock == NULL) { |
| 1685 | SOCKETCLOSE(newfd); |
| 1686 | goto finally; |
| 1687 | } |
| 1688 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1689 | addrlen, s->sock_proto); |
| 1690 | if (addr == NULL) |
| 1691 | goto finally; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1692 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1693 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1694 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1695 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1696 | Py_XDECREF(sock); |
| 1697 | Py_XDECREF(addr); |
| 1698 | return res; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1701 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1702 | "accept() -> (socket object, address info)\n\ |
| 1703 | \n\ |
| 1704 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1705 | connection, and the address of the client. For IP sockets, the address\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1706 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1707 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1708 | /* s.setblocking(flag) method. Argument: |
| 1709 | False -- non-blocking mode; same as settimeout(0) |
| 1710 | True -- blocking mode; same as settimeout(None) |
| 1711 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1712 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1713 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1714 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1715 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1716 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1717 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1718 | block = PyInt_AsLong(arg); |
| 1719 | if (block == -1 && PyErr_Occurred()) |
| 1720 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1722 | s->sock_timeout = block ? -1.0 : 0.0; |
| 1723 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1725 | Py_INCREF(Py_None); |
| 1726 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1727 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1728 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1729 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1730 | "setblocking(flag)\n\ |
| 1731 | \n\ |
| 1732 | 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] | 1733 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1734 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1735 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1736 | /* s.settimeout(timeout) method. Argument: |
| 1737 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1738 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1739 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1740 | < 0 -- illegal; raises an exception |
| 1741 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1742 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1743 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1744 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1745 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1746 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1747 | if (arg == Py_None) |
| 1748 | timeout = -1.0; |
| 1749 | else { |
| 1750 | timeout = PyFloat_AsDouble(arg); |
| 1751 | if (timeout < 0.0) { |
| 1752 | if (!PyErr_Occurred()) |
| 1753 | PyErr_SetString(PyExc_ValueError, |
| 1754 | "Timeout value out of range"); |
| 1755 | return NULL; |
| 1756 | } |
| 1757 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1758 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1759 | s->sock_timeout = timeout; |
| 1760 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1762 | Py_INCREF(Py_None); |
| 1763 | return Py_None; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1766 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1767 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1768 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1769 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1770 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1771 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1772 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1773 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1774 | /* s.gettimeout() method. |
| 1775 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1776 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1777 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1778 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1779 | if (s->sock_timeout < 0.0) { |
| 1780 | Py_INCREF(Py_None); |
| 1781 | return Py_None; |
| 1782 | } |
| 1783 | else |
| 1784 | return PyFloat_FromDouble(s->sock_timeout); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1787 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1788 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1789 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 1790 | Returns the timeout in seconds (float) associated with socket \n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1791 | 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] | 1792 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1793 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1794 | #ifdef RISCOS |
| 1795 | /* s.sleeptaskw(1 | 0) method */ |
| 1796 | |
| 1797 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1798 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1799 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1800 | int block; |
| 1801 | block = PyInt_AsLong(arg); |
| 1802 | if (block == -1 && PyErr_Occurred()) |
| 1803 | return NULL; |
| 1804 | Py_BEGIN_ALLOW_THREADS |
| 1805 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1806 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1808 | Py_INCREF(Py_None); |
| 1809 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1810 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1811 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1812 | "sleeptaskw(flag)\n\ |
| 1813 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1814 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1815 | #endif |
| 1816 | |
| 1817 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1818 | /* s.setsockopt() method. |
| 1819 | With an integer third argument, sets an integer option. |
| 1820 | With a string third argument, sets an option from a buffer; |
| 1821 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1822 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1823 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1824 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1825 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1826 | int level; |
| 1827 | int optname; |
| 1828 | int res; |
| 1829 | char *buf; |
| 1830 | int buflen; |
| 1831 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1832 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1833 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1834 | &level, &optname, &flag)) { |
| 1835 | buf = (char *) &flag; |
| 1836 | buflen = sizeof flag; |
| 1837 | } |
| 1838 | else { |
| 1839 | PyErr_Clear(); |
| 1840 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1841 | &level, &optname, &buf, &buflen)) |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1845 | if (res < 0) |
| 1846 | return s->errorhandler(); |
| 1847 | Py_INCREF(Py_None); |
| 1848 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1851 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1852 | "setsockopt(level, option, value)\n\ |
| 1853 | \n\ |
| 1854 | 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] | 1855 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1856 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1857 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1858 | /* s.getsockopt() method. |
| 1859 | With two arguments, retrieves an integer option. |
| 1860 | With a third integer argument, retrieves a string buffer of that size; |
| 1861 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1862 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1863 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1864 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1865 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1866 | int level; |
| 1867 | int optname; |
| 1868 | int res; |
| 1869 | PyObject *buf; |
| 1870 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1871 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1872 | #ifdef __BEOS__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1873 | /* We have incomplete socket support. */ |
| 1874 | PyErr_SetString(socket_error, "getsockopt not supported"); |
| 1875 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1876 | #else |
| 1877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1878 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1879 | &level, &optname, &buflen)) |
| 1880 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1882 | if (buflen == 0) { |
| 1883 | int flag = 0; |
| 1884 | socklen_t flagsize = sizeof flag; |
| 1885 | res = getsockopt(s->sock_fd, level, optname, |
| 1886 | (void *)&flag, &flagsize); |
| 1887 | if (res < 0) |
| 1888 | return s->errorhandler(); |
| 1889 | return PyInt_FromLong(flag); |
| 1890 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1891 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1892 | /* socklen_t is unsigned so no negative test is needed, |
| 1893 | test buflen == 0 is previously done */ |
| 1894 | if (buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1895 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1896 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1897 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1898 | PyErr_SetString(socket_error, |
| 1899 | "getsockopt buflen out of range"); |
| 1900 | return NULL; |
| 1901 | } |
| 1902 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
| 1903 | if (buf == NULL) |
| 1904 | return NULL; |
| 1905 | res = getsockopt(s->sock_fd, level, optname, |
| 1906 | (void *)PyString_AS_STRING(buf), &buflen); |
| 1907 | if (res < 0) { |
| 1908 | Py_DECREF(buf); |
| 1909 | return s->errorhandler(); |
| 1910 | } |
| 1911 | _PyString_Resize(&buf, buflen); |
| 1912 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1913 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1916 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1917 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1918 | \n\ |
| 1919 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1920 | 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] | 1921 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1922 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1923 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1924 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1925 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1926 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1927 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1928 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1929 | sock_addr_t addrbuf; |
| 1930 | int addrlen; |
| 1931 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1933 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1934 | return NULL; |
| 1935 | Py_BEGIN_ALLOW_THREADS |
| 1936 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 1937 | Py_END_ALLOW_THREADS |
| 1938 | if (res < 0) |
| 1939 | return s->errorhandler(); |
| 1940 | Py_INCREF(Py_None); |
| 1941 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1944 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1945 | "bind(address)\n\ |
| 1946 | \n\ |
| 1947 | 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] | 1948 | 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] | 1949 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1950 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1951 | |
| 1952 | /* s.close() method. |
| 1953 | Set the file descriptor to -1 so operations tried subsequently |
| 1954 | will surely fail. */ |
| 1955 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1956 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1957 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1958 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1959 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1961 | if ((fd = s->sock_fd) != -1) { |
| 1962 | s->sock_fd = -1; |
| 1963 | Py_BEGIN_ALLOW_THREADS |
| 1964 | (void) SOCKETCLOSE(fd); |
| 1965 | Py_END_ALLOW_THREADS |
| 1966 | } |
| 1967 | Py_INCREF(Py_None); |
| 1968 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1971 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1972 | "close()\n\ |
| 1973 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1974 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1975 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1976 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1977 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1978 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1979 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1980 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1982 | timeout = 0; |
| 1983 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1984 | |
| 1985 | #ifdef MS_WINDOWS |
| 1986 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1987 | if (s->sock_timeout > 0.0) { |
| 1988 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 1989 | IS_SELECTABLE(s)) { |
| 1990 | /* This is a mess. Best solution: trust select */ |
| 1991 | fd_set fds; |
| 1992 | fd_set fds_exc; |
| 1993 | struct timeval tv; |
| 1994 | tv.tv_sec = (int)s->sock_timeout; |
| 1995 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1996 | FD_ZERO(&fds); |
| 1997 | FD_SET(s->sock_fd, &fds); |
| 1998 | FD_ZERO(&fds_exc); |
| 1999 | FD_SET(s->sock_fd, &fds_exc); |
| 2000 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
| 2001 | if (res == 0) { |
| 2002 | res = WSAEWOULDBLOCK; |
| 2003 | timeout = 1; |
| 2004 | } else if (res > 0) { |
| 2005 | if (FD_ISSET(s->sock_fd, &fds)) |
| 2006 | /* The socket is in the writeable set - this |
| 2007 | means connected */ |
| 2008 | res = 0; |
| 2009 | else { |
| 2010 | /* As per MS docs, we need to call getsockopt() |
| 2011 | to get the underlying error */ |
| 2012 | int res_size = sizeof res; |
| 2013 | /* It must be in the exception set */ |
| 2014 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 2015 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 2016 | (char *)&res, &res_size)) |
| 2017 | /* getsockopt also clears WSAGetLastError, |
| 2018 | so reset it back. */ |
| 2019 | WSASetLastError(res); |
| 2020 | else |
| 2021 | res = WSAGetLastError(); |
| 2022 | } |
| 2023 | } |
| 2024 | /* else if (res < 0) an error occurred */ |
| 2025 | } |
| 2026 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2027 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2028 | if (res < 0) |
| 2029 | res = WSAGetLastError(); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2030 | |
| 2031 | #else |
| 2032 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2033 | if (s->sock_timeout > 0.0) { |
| 2034 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 2035 | timeout = internal_select(s, 1); |
| 2036 | if (timeout == 0) { |
| 2037 | /* Bug #1019808: in case of an EINPROGRESS, |
| 2038 | use getsockopt(SO_ERROR) to get the real |
| 2039 | error. */ |
| 2040 | socklen_t res_size = sizeof res; |
| 2041 | (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 2042 | SO_ERROR, &res, &res_size); |
| 2043 | if (res == EISCONN) |
| 2044 | res = 0; |
| 2045 | errno = res; |
| 2046 | } |
| 2047 | else if (timeout == -1) { |
| 2048 | res = errno; /* had error */ |
| 2049 | } |
| 2050 | else |
| 2051 | res = EWOULDBLOCK; /* timed out */ |
| 2052 | } |
| 2053 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2055 | if (res < 0) |
| 2056 | res = errno; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2057 | |
| 2058 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2059 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2061 | return res; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 2062 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2063 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2064 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2065 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2066 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2067 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2068 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2069 | sock_addr_t addrbuf; |
| 2070 | int addrlen; |
| 2071 | int res; |
| 2072 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2073 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2074 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2075 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2076 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2077 | Py_BEGIN_ALLOW_THREADS |
| 2078 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2079 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2080 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2081 | if (timeout == 1) { |
| 2082 | PyErr_SetString(socket_timeout, "timed out"); |
| 2083 | return NULL; |
| 2084 | } |
| 2085 | if (res != 0) |
| 2086 | return s->errorhandler(); |
| 2087 | Py_INCREF(Py_None); |
| 2088 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2091 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2092 | "connect(address)\n\ |
| 2093 | \n\ |
| 2094 | 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] | 2095 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2096 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2097 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2098 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2099 | |
| 2100 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2101 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2102 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2103 | sock_addr_t addrbuf; |
| 2104 | int addrlen; |
| 2105 | int res; |
| 2106 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2107 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2108 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2109 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2111 | Py_BEGIN_ALLOW_THREADS |
| 2112 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2113 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2115 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2116 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2117 | #ifdef EINTR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2118 | if (res == EINTR && PyErr_CheckSignals()) |
| 2119 | return NULL; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2120 | #endif |
| 2121 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2122 | return PyInt_FromLong((long) res); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2125 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2126 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2127 | \n\ |
| 2128 | 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] | 2129 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2130 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2131 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2132 | /* s.fileno() method */ |
| 2133 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2134 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2135 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2136 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2137 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2138 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2139 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2140 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2141 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2144 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2145 | "fileno() -> integer\n\ |
| 2146 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2147 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2148 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2149 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2150 | #ifndef NO_DUP |
| 2151 | /* s.dup() method */ |
| 2152 | |
| 2153 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2154 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2155 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2156 | SOCKET_T newfd; |
| 2157 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2159 | newfd = dup(s->sock_fd); |
| 2160 | if (newfd < 0) |
| 2161 | return s->errorhandler(); |
| 2162 | sock = (PyObject *) new_sockobject(newfd, |
| 2163 | s->sock_family, |
| 2164 | s->sock_type, |
| 2165 | s->sock_proto); |
| 2166 | if (sock == NULL) |
| 2167 | SOCKETCLOSE(newfd); |
| 2168 | return sock; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2169 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2171 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2172 | "dup() -> socket object\n\ |
| 2173 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2174 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2175 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2176 | #endif |
| 2177 | |
| 2178 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2179 | /* s.getsockname() method */ |
| 2180 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2181 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2182 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2183 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2184 | sock_addr_t addrbuf; |
| 2185 | int res; |
| 2186 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2187 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2188 | if (!getsockaddrlen(s, &addrlen)) |
| 2189 | return NULL; |
| 2190 | memset(&addrbuf, 0, addrlen); |
| 2191 | Py_BEGIN_ALLOW_THREADS |
| 2192 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2193 | Py_END_ALLOW_THREADS |
| 2194 | if (res < 0) |
| 2195 | return s->errorhandler(); |
| 2196 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2197 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2200 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2201 | "getsockname() -> address info\n\ |
| 2202 | \n\ |
| 2203 | 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] | 2204 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2205 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2207 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2208 | /* s.getpeername() method */ |
| 2209 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2210 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2211 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2212 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2213 | sock_addr_t addrbuf; |
| 2214 | int res; |
| 2215 | socklen_t addrlen; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2216 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2217 | if (!getsockaddrlen(s, &addrlen)) |
| 2218 | return NULL; |
| 2219 | memset(&addrbuf, 0, addrlen); |
| 2220 | Py_BEGIN_ALLOW_THREADS |
| 2221 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2222 | Py_END_ALLOW_THREADS |
| 2223 | if (res < 0) |
| 2224 | return s->errorhandler(); |
| 2225 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2226 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2227 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2228 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2229 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2230 | "getpeername() -> address info\n\ |
| 2231 | \n\ |
| 2232 | 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] | 2233 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2234 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2235 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2236 | |
| 2237 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2238 | /* s.listen(n) method */ |
| 2239 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2240 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2241 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2242 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2243 | int backlog; |
| 2244 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2246 | backlog = PyInt_AsLong(arg); |
| 2247 | if (backlog == -1 && PyErr_Occurred()) |
| 2248 | return NULL; |
| 2249 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2250 | /* To avoid problems on systems that don't allow a negative backlog |
| 2251 | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
| 2252 | if (backlog < 0) |
| 2253 | backlog = 0; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2254 | res = listen(s->sock_fd, backlog); |
| 2255 | Py_END_ALLOW_THREADS |
| 2256 | if (res < 0) |
| 2257 | return s->errorhandler(); |
| 2258 | Py_INCREF(Py_None); |
| 2259 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2262 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2263 | "listen(backlog)\n\ |
| 2264 | \n\ |
| 2265 | Enable a server to accept connections. The backlog argument must be at\n\ |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 2266 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
| 2267 | unaccepted connections that the system will allow before refusing new\n\ |
| 2268 | connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2269 | |
| 2270 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2271 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2272 | /* s.makefile(mode) method. |
| 2273 | Create a new open file object referring to a dupped version of |
| 2274 | the socket's file descriptor. (The dup() call is necessary so |
| 2275 | that the open file and socket objects may be closed independent |
| 2276 | of each other.) |
| 2277 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 2278 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2279 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2280 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2281 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2282 | extern int fclose(FILE *); |
| 2283 | char *mode = "r"; |
| 2284 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2285 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2286 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2287 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2288 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2289 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2290 | FILE *fp; |
| 2291 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2292 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2293 | char *mode_r = "r"; |
| 2294 | char *mode_w = "w"; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2295 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2296 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2297 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
| 2298 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2299 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2300 | if (strcmp(mode,"rb") == 0) { |
| 2301 | mode = mode_r; |
| 2302 | } |
| 2303 | else { |
| 2304 | if (strcmp(mode,"wb") == 0) { |
| 2305 | mode = mode_w; |
| 2306 | } |
| 2307 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2308 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2309 | #ifdef MS_WIN32 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2310 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 2311 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2312 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2313 | if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2314 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2315 | { |
| 2316 | if (fd >= 0) |
| 2317 | SOCKETCLOSE(fd); |
| 2318 | return s->errorhandler(); |
| 2319 | } |
| 2320 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 2321 | if (f != NULL) |
| 2322 | PyFile_SetBufSize(f, bufsize); |
| 2323 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2324 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2326 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2327 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 2328 | \n\ |
| 2329 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2330 | The mode and buffersize arguments are as for the built-in open() function."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2331 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2332 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2333 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2334 | /* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2335 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Andrew M. Kuchling | 8d0baae | 2006-12-19 14:29:04 +0000 | [diff] [blame] | 2336 | * char buffer. If you have any inc/dec ref to do to the objects that contain |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2337 | * the buffer, do it in the caller. This function returns the number of bytes |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2338 | * successfully read. If there was an error, it returns -1. Note that it is |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2339 | * also possible that we return a number of bytes smaller than the request |
| 2340 | * bytes. |
| 2341 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2342 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2343 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2344 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2345 | ssize_t outlen = -1; |
| 2346 | int timeout; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2347 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2348 | int remaining; |
| 2349 | char *read_buf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2350 | #endif |
| 2351 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2352 | if (!IS_SELECTABLE(s)) { |
| 2353 | select_error(); |
| 2354 | return -1; |
| 2355 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2356 | |
| 2357 | #ifndef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2358 | Py_BEGIN_ALLOW_THREADS |
| 2359 | timeout = internal_select(s, 0); |
| 2360 | if (!timeout) |
| 2361 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2362 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2363 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2364 | if (timeout == 1) { |
| 2365 | PyErr_SetString(socket_timeout, "timed out"); |
| 2366 | return -1; |
| 2367 | } |
| 2368 | if (outlen < 0) { |
| 2369 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2370 | NULL, so ignore its return value */ |
| 2371 | s->errorhandler(); |
| 2372 | return -1; |
| 2373 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2374 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | read_buf = cbuf; |
| 2376 | remaining = len; |
| 2377 | while (remaining != 0) { |
| 2378 | unsigned int segment; |
| 2379 | int nread = -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2380 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2381 | segment = remaining /SEGMENT_SIZE; |
| 2382 | if (segment != 0) { |
| 2383 | segment = SEGMENT_SIZE; |
| 2384 | } |
| 2385 | else { |
| 2386 | segment = remaining; |
| 2387 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2388 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2389 | Py_BEGIN_ALLOW_THREADS |
| 2390 | timeout = internal_select(s, 0); |
| 2391 | if (!timeout) |
| 2392 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2393 | Py_END_ALLOW_THREADS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2394 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2395 | if (timeout == 1) { |
| 2396 | PyErr_SetString(socket_timeout, "timed out"); |
| 2397 | return -1; |
| 2398 | } |
| 2399 | if (nread < 0) { |
| 2400 | s->errorhandler(); |
| 2401 | return -1; |
| 2402 | } |
| 2403 | if (nread != remaining) { |
| 2404 | read_buf += nread; |
| 2405 | break; |
| 2406 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2408 | remaining -= segment; |
| 2409 | read_buf += segment; |
| 2410 | } |
| 2411 | outlen = read_buf - cbuf; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2412 | #endif /* !__VMS */ |
| 2413 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2414 | return outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2417 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2418 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2419 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2420 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2421 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2422 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2423 | int recvlen, flags = 0; |
| 2424 | ssize_t outlen; |
| 2425 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2426 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2427 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
| 2428 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2430 | if (recvlen < 0) { |
| 2431 | PyErr_SetString(PyExc_ValueError, |
| 2432 | "negative buffersize in recv"); |
| 2433 | return NULL; |
| 2434 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2435 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2436 | /* Allocate a new string. */ |
| 2437 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2438 | if (buf == NULL) |
| 2439 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2440 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2441 | /* Call the guts */ |
| 2442 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags); |
| 2443 | if (outlen < 0) { |
| 2444 | /* An error occurred, release the string and return an |
| 2445 | error. */ |
| 2446 | Py_DECREF(buf); |
| 2447 | return NULL; |
| 2448 | } |
| 2449 | if (outlen != recvlen) { |
| 2450 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2451 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2452 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2453 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2454 | return NULL; |
| 2455 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2456 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2457 | return buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2460 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2461 | "recv(buffersize[, flags]) -> data\n\ |
| 2462 | \n\ |
| 2463 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2464 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2465 | 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] | 2466 | 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] | 2467 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2468 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2469 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2470 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2471 | static PyObject* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2472 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2473 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2474 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2475 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2476 | int recvlen = 0, flags = 0; |
| 2477 | ssize_t readlen; |
| 2478 | Py_buffer buf; |
| 2479 | Py_ssize_t buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2480 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2481 | /* Get the buffer's memory */ |
| 2482 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist, |
| 2483 | &buf, &recvlen, &flags)) |
| 2484 | return NULL; |
| 2485 | buflen = buf.len; |
| 2486 | assert(buf.buf != 0 && buflen > 0); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2488 | if (recvlen < 0) { |
| 2489 | PyErr_SetString(PyExc_ValueError, |
| 2490 | "negative buffersize in recv_into"); |
| 2491 | goto error; |
| 2492 | } |
| 2493 | if (recvlen == 0) { |
| 2494 | /* If nbytes was not specified, use the buffer's length */ |
| 2495 | recvlen = buflen; |
| 2496 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2497 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2498 | /* Check if the buffer is large enough */ |
| 2499 | if (buflen < recvlen) { |
| 2500 | PyErr_SetString(PyExc_ValueError, |
| 2501 | "buffer too small for requested bytes"); |
| 2502 | goto error; |
| 2503 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2504 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2505 | /* Call the guts */ |
| 2506 | readlen = sock_recv_guts(s, buf.buf, recvlen, flags); |
| 2507 | if (readlen < 0) { |
| 2508 | /* Return an error. */ |
| 2509 | goto error; |
| 2510 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2511 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2512 | PyBuffer_Release(&buf); |
| 2513 | /* Return the number of bytes read. Note that we do not do anything |
| 2514 | special here in the case that readlen < recvlen. */ |
| 2515 | return PyInt_FromSsize_t(readlen); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2516 | |
| 2517 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2518 | PyBuffer_Release(&buf); |
| 2519 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2522 | PyDoc_STRVAR(recv_into_doc, |
| 2523 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2524 | \n\ |
| 2525 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2526 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2527 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2528 | \n\ |
| 2529 | See recv() for documentation about the flags."); |
| 2530 | |
| 2531 | |
| 2532 | /* |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2533 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
| 2534 | * into a char buffer. If you have any inc/def ref to do to the objects that |
| 2535 | * contain the buffer, do it in the caller. This function returns the number |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2536 | * of bytes successfully read. If there was an error, it returns -1. Note |
Guido van Rossum | 3c887b2 | 2007-12-18 20:10:42 +0000 | [diff] [blame] | 2537 | * that it is also possible that we return a number of bytes smaller than the |
| 2538 | * request bytes. |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2539 | * |
| 2540 | * 'addr' is a return value for the address object. Note that you must decref |
| 2541 | * it yourself. |
| 2542 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2543 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2544 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2545 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2546 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2547 | sock_addr_t addrbuf; |
| 2548 | int timeout; |
| 2549 | ssize_t n = -1; |
| 2550 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2551 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2552 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2553 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2554 | if (!getsockaddrlen(s, &addrlen)) |
| 2555 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2556 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2557 | if (!IS_SELECTABLE(s)) { |
| 2558 | select_error(); |
| 2559 | return -1; |
| 2560 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2561 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2562 | Py_BEGIN_ALLOW_THREADS |
| 2563 | memset(&addrbuf, 0, addrlen); |
| 2564 | timeout = internal_select(s, 0); |
| 2565 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2566 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2567 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2568 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2569 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2570 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2571 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2572 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2573 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2574 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2575 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2576 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2577 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2578 | } |
| 2579 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2581 | if (timeout == 1) { |
| 2582 | PyErr_SetString(socket_timeout, "timed out"); |
| 2583 | return -1; |
| 2584 | } |
| 2585 | if (n < 0) { |
| 2586 | s->errorhandler(); |
| 2587 | return -1; |
| 2588 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2589 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2590 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2591 | addrlen, s->sock_proto))) |
| 2592 | return -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2594 | return n; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2598 | |
| 2599 | static PyObject * |
| 2600 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2601 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2602 | PyObject *buf = NULL; |
| 2603 | PyObject *addr = NULL; |
| 2604 | PyObject *ret = NULL; |
| 2605 | int recvlen, flags = 0; |
| 2606 | ssize_t outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2607 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2608 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
| 2609 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2610 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2611 | if (recvlen < 0) { |
| 2612 | PyErr_SetString(PyExc_ValueError, |
| 2613 | "negative buffersize in recvfrom"); |
| 2614 | return NULL; |
| 2615 | } |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 2616 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2617 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2618 | if (buf == NULL) |
| 2619 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2621 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf), |
| 2622 | recvlen, flags, &addr); |
| 2623 | if (outlen < 0) { |
| 2624 | goto finally; |
| 2625 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2626 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2627 | if (outlen != recvlen) { |
| 2628 | /* We did not read as many bytes as we anticipated, resize the |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2629 | string if possible and be successful. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2630 | if (_PyString_Resize(&buf, outlen) < 0) |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2631 | /* Oopsy, not so successful after all. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2632 | goto finally; |
| 2633 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2634 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2635 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2636 | |
| 2637 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2638 | Py_XDECREF(buf); |
| 2639 | Py_XDECREF(addr); |
| 2640 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2643 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2644 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2645 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2646 | 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] | 2647 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2648 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2649 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2650 | |
| 2651 | static PyObject * |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2652 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2653 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2654 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2655 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2656 | int recvlen = 0, flags = 0; |
| 2657 | ssize_t readlen; |
| 2658 | Py_buffer buf; |
| 2659 | int buflen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2661 | PyObject *addr = NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2662 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2663 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into", |
| 2664 | kwlist, &buf, |
| 2665 | &recvlen, &flags)) |
| 2666 | return NULL; |
| 2667 | buflen = buf.len; |
| 2668 | assert(buf.buf != 0 && buflen > 0); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2669 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2670 | if (recvlen < 0) { |
| 2671 | PyErr_SetString(PyExc_ValueError, |
| 2672 | "negative buffersize in recvfrom_into"); |
| 2673 | goto error; |
| 2674 | } |
| 2675 | if (recvlen == 0) { |
| 2676 | /* If nbytes was not specified, use the buffer's length */ |
| 2677 | recvlen = buflen; |
| 2678 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2680 | readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr); |
| 2681 | if (readlen < 0) { |
| 2682 | /* Return an error */ |
| 2683 | goto error; |
| 2684 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2686 | PyBuffer_Release(&buf); |
| 2687 | /* Return the number of bytes read and the address. Note that we do |
| 2688 | not do anything special here in the case that readlen < recvlen. */ |
| 2689 | return Py_BuildValue("lN", readlen, addr); |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 2690 | |
| 2691 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2692 | Py_XDECREF(addr); |
| 2693 | PyBuffer_Release(&buf); |
| 2694 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2697 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2698 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2699 | \n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2700 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2701 | |
| 2702 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2703 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2704 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2705 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2706 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2707 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2708 | char *buf; |
| 2709 | int len, n = -1, flags = 0, timeout; |
| 2710 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2712 | if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags)) |
| 2713 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2714 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2715 | if (!IS_SELECTABLE(s)) { |
| 2716 | PyBuffer_Release(&pbuf); |
| 2717 | return select_error(); |
| 2718 | } |
| 2719 | buf = pbuf.buf; |
| 2720 | len = pbuf.len; |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2722 | Py_BEGIN_ALLOW_THREADS |
| 2723 | timeout = internal_select(s, 1); |
| 2724 | if (!timeout) |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2725 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2726 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2727 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2728 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2729 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2730 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2732 | PyBuffer_Release(&pbuf); |
Martin v. Löwis | f91d46a | 2008-08-12 14:49:50 +0000 | [diff] [blame] | 2733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2734 | if (timeout == 1) { |
| 2735 | PyErr_SetString(socket_timeout, "timed out"); |
| 2736 | return NULL; |
| 2737 | } |
| 2738 | if (n < 0) |
| 2739 | return s->errorhandler(); |
| 2740 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2743 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2744 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2745 | \n\ |
| 2746 | 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] | 2747 | 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] | 2748 | 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] | 2749 | |
| 2750 | |
| 2751 | /* s.sendall(data [,flags]) method */ |
| 2752 | |
| 2753 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2754 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2755 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2756 | char *buf; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2757 | int len, n = -1, flags = 0, timeout, saved_errno; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2758 | Py_buffer pbuf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2759 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2760 | if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags)) |
| 2761 | return NULL; |
| 2762 | buf = pbuf.buf; |
| 2763 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2764 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2765 | if (!IS_SELECTABLE(s)) { |
| 2766 | PyBuffer_Release(&pbuf); |
| 2767 | return select_error(); |
| 2768 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2769 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2770 | do { |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2771 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2772 | timeout = internal_select(s, 1); |
| 2773 | n = -1; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2774 | if (!timeout) { |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2775 | #ifdef __VMS |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2776 | n = sendsegmented(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2777 | #else |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2778 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2779 | #endif |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2780 | } |
| 2781 | Py_END_ALLOW_THREADS |
| 2782 | if (timeout == 1) { |
| 2783 | PyBuffer_Release(&pbuf); |
| 2784 | PyErr_SetString(socket_timeout, "timed out"); |
| 2785 | return NULL; |
| 2786 | } |
| 2787 | /* PyErr_CheckSignals() might change errno */ |
| 2788 | saved_errno = errno; |
| 2789 | /* We must run our signal handlers before looping again. |
| 2790 | send() can return a successful partial write when it is |
| 2791 | interrupted, so we can't restrict ourselves to EINTR. */ |
| 2792 | if (PyErr_CheckSignals()) { |
| 2793 | PyBuffer_Release(&pbuf); |
| 2794 | return NULL; |
| 2795 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2796 | if (n < 0) { |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2797 | /* If interrupted, try again */ |
| 2798 | if (saved_errno == EINTR) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2799 | continue; |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 2800 | else |
| 2801 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2802 | } |
| 2803 | buf += n; |
| 2804 | len -= n; |
| 2805 | } while (len > 0); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2806 | PyBuffer_Release(&pbuf); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2807 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2808 | if (n < 0) |
| 2809 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2810 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2811 | Py_INCREF(Py_None); |
| 2812 | return Py_None; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2815 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2816 | "sendall(data[, flags])\n\ |
| 2817 | \n\ |
| 2818 | Send a data string to the socket. For the optional flags\n\ |
| 2819 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2820 | 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] | 2821 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2822 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2823 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2824 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2825 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2826 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2827 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2828 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2829 | Py_buffer pbuf; |
| 2830 | PyObject *addro; |
| 2831 | char *buf; |
| 2832 | Py_ssize_t len; |
| 2833 | sock_addr_t addrbuf; |
| 2834 | int addrlen, n = -1, flags, timeout; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2835 | int arglen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2836 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2837 | flags = 0; |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2838 | arglen = PyTuple_Size(args); |
| 2839 | switch(arglen) { |
| 2840 | case 2: |
| 2841 | PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro); |
| 2842 | break; |
| 2843 | case 3: |
| 2844 | PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro); |
| 2845 | break; |
| 2846 | default: |
| 2847 | PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3" |
| 2848 | " arguments (%d given)", arglen); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2849 | } |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 2850 | if (PyErr_Occurred()) |
| 2851 | return NULL; |
| 2852 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2853 | buf = pbuf.buf; |
| 2854 | len = pbuf.len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2856 | if (!IS_SELECTABLE(s)) { |
| 2857 | PyBuffer_Release(&pbuf); |
| 2858 | return select_error(); |
| 2859 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2860 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2861 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2862 | PyBuffer_Release(&pbuf); |
| 2863 | return NULL; |
| 2864 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2865 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2866 | Py_BEGIN_ALLOW_THREADS |
| 2867 | timeout = internal_select(s, 1); |
| 2868 | if (!timeout) |
| 2869 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2870 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2871 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2872 | PyBuffer_Release(&pbuf); |
| 2873 | if (timeout == 1) { |
| 2874 | PyErr_SetString(socket_timeout, "timed out"); |
| 2875 | return NULL; |
| 2876 | } |
| 2877 | if (n < 0) |
| 2878 | return s->errorhandler(); |
| 2879 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2882 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2883 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2884 | \n\ |
| 2885 | 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] | 2886 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2887 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2888 | |
| 2889 | /* s.shutdown(how) method */ |
| 2890 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2891 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2892 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2893 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2894 | int how; |
| 2895 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2896 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2897 | how = PyInt_AsLong(arg); |
| 2898 | if (how == -1 && PyErr_Occurred()) |
| 2899 | return NULL; |
| 2900 | Py_BEGIN_ALLOW_THREADS |
| 2901 | res = shutdown(s->sock_fd, how); |
| 2902 | Py_END_ALLOW_THREADS |
| 2903 | if (res < 0) |
| 2904 | return s->errorhandler(); |
| 2905 | Py_INCREF(Py_None); |
| 2906 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2909 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2910 | "shutdown(flag)\n\ |
| 2911 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2912 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2913 | 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] | 2914 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 2915 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2916 | static PyObject* |
| 2917 | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 2918 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2919 | unsigned long cmd = SIO_RCVALL; |
| 2920 | PyObject *argO; |
| 2921 | DWORD recv; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2922 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2923 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
| 2924 | return NULL; |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2926 | switch (cmd) { |
| 2927 | case SIO_RCVALL: { |
| 2928 | unsigned int option = RCVALL_ON; |
| 2929 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 2930 | return NULL; |
| 2931 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 2932 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 2933 | return set_error(); |
| 2934 | } |
| 2935 | return PyLong_FromUnsignedLong(recv); } |
| 2936 | case SIO_KEEPALIVE_VALS: { |
| 2937 | struct tcp_keepalive ka; |
| 2938 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
| 2939 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
| 2940 | return NULL; |
| 2941 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
| 2942 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 2943 | return set_error(); |
| 2944 | } |
| 2945 | return PyLong_FromUnsignedLong(recv); } |
| 2946 | default: |
| 2947 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
| 2948 | return NULL; |
| 2949 | } |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2950 | } |
| 2951 | PyDoc_STRVAR(sock_ioctl_doc, |
| 2952 | "ioctl(cmd, option) -> long\n\ |
| 2953 | \n\ |
Kristján Valur Jónsson | 2fcd03b | 2009-09-25 15:19:51 +0000 | [diff] [blame] | 2954 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
| 2955 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
| 2956 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."); |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2957 | |
| 2958 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2959 | |
| 2960 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2961 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2962 | static PyMethodDef sock_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2963 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 2964 | accept_doc}, |
| 2965 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 2966 | bind_doc}, |
| 2967 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 2968 | close_doc}, |
| 2969 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 2970 | connect_doc}, |
| 2971 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 2972 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2973 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2974 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
| 2975 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2976 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2977 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 2978 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2979 | #ifdef HAVE_GETPEERNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2980 | {"getpeername", (PyCFunction)sock_getpeername, |
| 2981 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 2982 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2983 | {"getsockname", (PyCFunction)sock_getsockname, |
| 2984 | METH_NOARGS, getsockname_doc}, |
| 2985 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 2986 | getsockopt_doc}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 2987 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2988 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 2989 | sock_ioctl_doc}, |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 2990 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2991 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 2992 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2993 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2994 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
| 2995 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2996 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2997 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 2998 | recv_doc}, |
| 2999 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 3000 | recv_into_doc}, |
| 3001 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 3002 | recvfrom_doc}, |
| 3003 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 3004 | recvfrom_into_doc}, |
| 3005 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 3006 | send_doc}, |
| 3007 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 3008 | sendall_doc}, |
| 3009 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 3010 | sendto_doc}, |
| 3011 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 3012 | setblocking_doc}, |
| 3013 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 3014 | settimeout_doc}, |
| 3015 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 3016 | gettimeout_doc}, |
| 3017 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 3018 | setsockopt_doc}, |
| 3019 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 3020 | shutdown_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3021 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3022 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
| 3023 | sleeptaskw_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3024 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3025 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3026 | }; |
| 3027 | |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 3028 | /* SockObject members */ |
| 3029 | static PyMemberDef sock_memberlist[] = { |
| 3030 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 3031 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 3032 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 3033 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 3034 | {0}, |
| 3035 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3036 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3037 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3038 | First close the file description. */ |
| 3039 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3040 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3041 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3042 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3043 | if (s->sock_fd != -1) |
| 3044 | (void) SOCKETCLOSE(s->sock_fd); |
| 3045 | Py_TYPE(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3048 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3049 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3050 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3051 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3052 | char buf[512]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3053 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3054 | if (s->sock_fd > LONG_MAX) { |
| 3055 | /* this can occur on Win64, and actually there is a special |
| 3056 | ugly printf formatter for decimal pointer length integer |
| 3057 | printing, only bother if necessary*/ |
| 3058 | PyErr_SetString(PyExc_OverflowError, |
| 3059 | "no printf formatter to display " |
| 3060 | "the socket descriptor in decimal"); |
| 3061 | return NULL; |
| 3062 | } |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3063 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3064 | PyOS_snprintf( |
| 3065 | buf, sizeof(buf), |
| 3066 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
| 3067 | (long)s->sock_fd, s->sock_family, |
| 3068 | s->sock_type, |
| 3069 | s->sock_proto); |
| 3070 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3074 | /* Create a new, uninitialized socket object. */ |
| 3075 | |
| 3076 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3077 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3078 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3079 | PyObject *new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3080 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3081 | new = type->tp_alloc(type, 0); |
| 3082 | if (new != NULL) { |
| 3083 | ((PySocketSockObject *)new)->sock_fd = -1; |
| 3084 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 3085 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 3086 | } |
| 3087 | return new; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | |
| 3091 | /* Initialize a new socket object. */ |
| 3092 | |
| 3093 | /*ARGSUSED*/ |
| 3094 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 3095 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3096 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3097 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 3098 | SOCKET_T fd; |
| 3099 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 3100 | static char *keywords[] = {"family", "type", "proto", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3101 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3102 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 3103 | "|iii:socket", keywords, |
| 3104 | &family, &type, &proto)) |
| 3105 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3106 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3107 | Py_BEGIN_ALLOW_THREADS |
| 3108 | fd = socket(family, type, proto); |
| 3109 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3110 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3111 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3112 | if (fd == INVALID_SOCKET) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3113 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3114 | if (fd < 0) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3115 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3116 | { |
| 3117 | set_error(); |
| 3118 | return -1; |
| 3119 | } |
| 3120 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3121 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3122 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3123 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3127 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3128 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3129 | static PyTypeObject sock_type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3130 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 3131 | "_socket.socket", /* tp_name */ |
| 3132 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 3133 | 0, /* tp_itemsize */ |
| 3134 | (destructor)sock_dealloc, /* tp_dealloc */ |
| 3135 | 0, /* tp_print */ |
| 3136 | 0, /* tp_getattr */ |
| 3137 | 0, /* tp_setattr */ |
| 3138 | 0, /* tp_compare */ |
| 3139 | (reprfunc)sock_repr, /* tp_repr */ |
| 3140 | 0, /* tp_as_number */ |
| 3141 | 0, /* tp_as_sequence */ |
| 3142 | 0, /* tp_as_mapping */ |
| 3143 | 0, /* tp_hash */ |
| 3144 | 0, /* tp_call */ |
| 3145 | 0, /* tp_str */ |
| 3146 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3147 | 0, /* tp_setattro */ |
| 3148 | 0, /* tp_as_buffer */ |
| 3149 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3150 | sock_doc, /* tp_doc */ |
| 3151 | 0, /* tp_traverse */ |
| 3152 | 0, /* tp_clear */ |
| 3153 | 0, /* tp_richcompare */ |
| 3154 | 0, /* tp_weaklistoffset */ |
| 3155 | 0, /* tp_iter */ |
| 3156 | 0, /* tp_iternext */ |
| 3157 | sock_methods, /* tp_methods */ |
| 3158 | sock_memberlist, /* tp_members */ |
| 3159 | 0, /* tp_getset */ |
| 3160 | 0, /* tp_base */ |
| 3161 | 0, /* tp_dict */ |
| 3162 | 0, /* tp_descr_get */ |
| 3163 | 0, /* tp_descr_set */ |
| 3164 | 0, /* tp_dictoffset */ |
| 3165 | sock_initobj, /* tp_init */ |
| 3166 | PyType_GenericAlloc, /* tp_alloc */ |
| 3167 | sock_new, /* tp_new */ |
| 3168 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3169 | }; |
| 3170 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3171 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3172 | /* Python interface to gethostname(). */ |
| 3173 | |
| 3174 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3175 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3176 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3177 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3178 | char buf[1024]; |
| 3179 | int res; |
| 3180 | Py_BEGIN_ALLOW_THREADS |
| 3181 | res = gethostname(buf, (int) sizeof buf - 1); |
| 3182 | Py_END_ALLOW_THREADS |
| 3183 | if (res < 0) |
| 3184 | return set_error(); |
| 3185 | buf[sizeof buf - 1] = '\0'; |
| 3186 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 3187 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3189 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3190 | "gethostname() -> string\n\ |
| 3191 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3192 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3193 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3194 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3195 | /* Python interface to gethostbyname(name). */ |
| 3196 | |
| 3197 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3198 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3199 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3200 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3201 | char *name; |
| 3202 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3203 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3204 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
| 3205 | return NULL; |
| 3206 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
| 3207 | return NULL; |
| 3208 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3211 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3212 | "gethostbyname(host) -> address\n\ |
| 3213 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3214 | 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] | 3215 | |
| 3216 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3217 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 3218 | |
| 3219 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3220 | 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] | 3221 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3222 | char **pch; |
| 3223 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 3224 | PyObject *name_list = (PyObject *)NULL; |
| 3225 | PyObject *addr_list = (PyObject *)NULL; |
| 3226 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3228 | if (h == NULL) { |
| 3229 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3230 | #ifndef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3231 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3232 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3233 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3234 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3235 | return NULL; |
| 3236 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3237 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3238 | if (h->h_addrtype != af) { |
| 3239 | /* Let's get real error message to return */ |
| 3240 | PyErr_SetString(socket_error, |
| 3241 | (char *)strerror(EAFNOSUPPORT)); |
Brett Cannon | 10ed0f5 | 2008-03-18 15:35:58 +0000 | [diff] [blame] | 3242 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3243 | return NULL; |
| 3244 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3245 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3246 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3247 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3248 | case AF_INET: |
| 3249 | if (alen < sizeof(struct sockaddr_in)) |
| 3250 | return NULL; |
| 3251 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3252 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3253 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3254 | case AF_INET6: |
| 3255 | if (alen < sizeof(struct sockaddr_in6)) |
| 3256 | return NULL; |
| 3257 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3258 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3259 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3260 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3262 | if ((name_list = PyList_New(0)) == NULL) |
| 3263 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3264 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3265 | if ((addr_list = PyList_New(0)) == NULL) |
| 3266 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3267 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3268 | /* SF #1511317: h_aliases can be NULL */ |
| 3269 | if (h->h_aliases) { |
| 3270 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3271 | int status; |
| 3272 | tmp = PyString_FromString(*pch); |
| 3273 | if (tmp == NULL) |
| 3274 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3275 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3276 | status = PyList_Append(name_list, tmp); |
| 3277 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3278 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3279 | if (status) |
| 3280 | goto err; |
| 3281 | } |
| 3282 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3283 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3284 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3285 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3286 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3287 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3288 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3289 | case AF_INET: |
| 3290 | { |
| 3291 | struct sockaddr_in sin; |
| 3292 | memset(&sin, 0, sizeof(sin)); |
| 3293 | sin.sin_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3294 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3295 | sin.sin_len = sizeof(sin); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3296 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3297 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3298 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3299 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3300 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3301 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3302 | break; |
| 3303 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3304 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3305 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3306 | case AF_INET6: |
| 3307 | { |
| 3308 | struct sockaddr_in6 sin6; |
| 3309 | memset(&sin6, 0, sizeof(sin6)); |
| 3310 | sin6.sin6_family = af; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3311 | #ifdef HAVE_SOCKADDR_SA_LEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3312 | sin6.sin6_len = sizeof(sin6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3313 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3314 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3315 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3316 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3317 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3318 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3319 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3320 | break; |
| 3321 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3322 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3323 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3324 | default: /* can't happen */ |
| 3325 | PyErr_SetString(socket_error, |
| 3326 | "unsupported address family"); |
| 3327 | return NULL; |
| 3328 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3329 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3330 | if (tmp == NULL) |
| 3331 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3332 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3333 | status = PyList_Append(addr_list, tmp); |
| 3334 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3336 | if (status) |
| 3337 | goto err; |
| 3338 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3339 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3340 | 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] | 3341 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3342 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3343 | Py_XDECREF(name_list); |
| 3344 | Py_XDECREF(addr_list); |
| 3345 | return rtn_tuple; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | |
| 3349 | /* Python interface to gethostbyname_ex(name). */ |
| 3350 | |
| 3351 | /*ARGSUSED*/ |
| 3352 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3353 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3354 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3355 | char *name; |
| 3356 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3357 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3358 | struct sockaddr_storage addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3359 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3360 | struct sockaddr_in addr; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3361 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3362 | struct sockaddr *sa; |
| 3363 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3364 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3365 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3366 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3367 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3368 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3369 | char buf[16384]; |
| 3370 | int buf_len = (sizeof buf) - 1; |
| 3371 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3372 | #endif |
| 3373 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3374 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3375 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3376 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3378 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
| 3379 | return NULL; |
| 3380 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
| 3381 | return NULL; |
| 3382 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3383 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3384 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3385 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3386 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3387 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3388 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3389 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3390 | memset((void *) &data, '\0', sizeof(data)); |
| 3391 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3392 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3393 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3394 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3395 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3396 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3397 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3398 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3399 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3400 | Py_END_ALLOW_THREADS |
| 3401 | /* Some C libraries would require addr.__ss_family instead of |
| 3402 | addr.ss_family. |
| 3403 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3404 | access sa_family. */ |
| 3405 | sa = (struct sockaddr*)&addr; |
| 3406 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3407 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3408 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3409 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3410 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3411 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3414 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3415 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3416 | \n\ |
| 3417 | 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] | 3418 | 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] | 3419 | |
| 3420 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3421 | /* Python interface to gethostbyaddr(IP). */ |
| 3422 | |
| 3423 | /*ARGSUSED*/ |
| 3424 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3425 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3426 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3427 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3428 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3429 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3430 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3431 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3432 | struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3433 | char *ip_num; |
| 3434 | struct hostent *h; |
| 3435 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3436 | #ifdef HAVE_GETHOSTBYNAME_R |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3437 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3438 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3439 | struct hostent_data data; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3440 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3441 | /* glibcs up to 2.10 assume that the buf argument to |
| 3442 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3443 | does not ensure. The attribute below instructs the compiler |
| 3444 | to maintain this alignment. */ |
| 3445 | char buf[16384] Py_ALIGNED(8); |
| 3446 | int buf_len = (sizeof buf) - 1; |
| 3447 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3448 | #endif |
| 3449 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3450 | int result; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3451 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3452 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3453 | char *ap; |
| 3454 | int al; |
| 3455 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3456 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3457 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
| 3458 | return NULL; |
| 3459 | af = AF_UNSPEC; |
| 3460 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
| 3461 | return NULL; |
| 3462 | af = sa->sa_family; |
| 3463 | ap = NULL; |
| 3464 | switch (af) { |
| 3465 | case AF_INET: |
| 3466 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3467 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3468 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3469 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3470 | case AF_INET6: |
| 3471 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3472 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3473 | break; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3474 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3475 | default: |
| 3476 | PyErr_SetString(socket_error, "unsupported address family"); |
| 3477 | return NULL; |
| 3478 | } |
| 3479 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3480 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3481 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3482 | result = gethostbyaddr_r(ap, al, af, |
| 3483 | &hp_allocated, buf, buf_len, |
| 3484 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3485 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3486 | h = gethostbyaddr_r(ap, al, af, |
| 3487 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3488 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3489 | memset((void *) &data, '\0', sizeof(data)); |
| 3490 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3491 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3492 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3493 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3494 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3495 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3496 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3497 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3498 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3499 | Py_END_ALLOW_THREADS |
| 3500 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3501 | #ifdef USE_GETHOSTBYNAME_LOCK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3502 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3503 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3504 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3507 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3508 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3509 | \n\ |
| 3510 | 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] | 3511 | 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] | 3512 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3513 | |
| 3514 | /* Python interface to getservbyname(name). |
| 3515 | This only returns the port number, since the other info is already |
| 3516 | known or not useful (like the list of aliases). */ |
| 3517 | |
| 3518 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3519 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3520 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3521 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3522 | char *name, *proto=NULL; |
| 3523 | struct servent *sp; |
| 3524 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3525 | return NULL; |
| 3526 | Py_BEGIN_ALLOW_THREADS |
| 3527 | sp = getservbyname(name, proto); |
| 3528 | Py_END_ALLOW_THREADS |
| 3529 | if (sp == NULL) { |
| 3530 | PyErr_SetString(socket_error, "service/proto not found"); |
| 3531 | return NULL; |
| 3532 | } |
| 3533 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3536 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3537 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3538 | \n\ |
| 3539 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3540 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3541 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3542 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3543 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3544 | /* Python interface to getservbyport(port). |
| 3545 | This only returns the service name, since the other info is already |
| 3546 | known or not useful (like the list of aliases). */ |
| 3547 | |
| 3548 | /*ARGSUSED*/ |
| 3549 | static PyObject * |
| 3550 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3551 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3552 | int port; |
| 3553 | char *proto=NULL; |
| 3554 | struct servent *sp; |
| 3555 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3556 | return NULL; |
| 3557 | if (port < 0 || port > 0xffff) { |
| 3558 | PyErr_SetString( |
| 3559 | PyExc_OverflowError, |
| 3560 | "getservbyport: port must be 0-65535."); |
| 3561 | return NULL; |
| 3562 | } |
| 3563 | Py_BEGIN_ALLOW_THREADS |
| 3564 | sp = getservbyport(htons((short)port), proto); |
| 3565 | Py_END_ALLOW_THREADS |
| 3566 | if (sp == NULL) { |
| 3567 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3568 | return NULL; |
| 3569 | } |
| 3570 | return PyString_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | PyDoc_STRVAR(getservbyport_doc, |
| 3574 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3575 | \n\ |
| 3576 | Return the service name from a port number and protocol name.\n\ |
| 3577 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3578 | otherwise any protocol will match."); |
| 3579 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3580 | /* Python interface to getprotobyname(name). |
| 3581 | This only returns the protocol number, since the other info is |
| 3582 | already known or not useful (like the list of aliases). */ |
| 3583 | |
| 3584 | /*ARGSUSED*/ |
| 3585 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3586 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3587 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3588 | char *name; |
| 3589 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3590 | #ifdef __BEOS__ |
| 3591 | /* Not available in BeOS yet. - [cjh] */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3592 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
| 3593 | return NULL; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3594 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3595 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3596 | return NULL; |
| 3597 | Py_BEGIN_ALLOW_THREADS |
| 3598 | sp = getprotobyname(name); |
| 3599 | Py_END_ALLOW_THREADS |
| 3600 | if (sp == NULL) { |
| 3601 | PyErr_SetString(socket_error, "protocol not found"); |
| 3602 | return NULL; |
| 3603 | } |
| 3604 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3605 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3608 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3609 | "getprotobyname(name) -> integer\n\ |
| 3610 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3611 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3612 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3613 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3614 | #ifdef HAVE_SOCKETPAIR |
| 3615 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3616 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3617 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3618 | |
| 3619 | /*ARGSUSED*/ |
| 3620 | static PyObject * |
| 3621 | socket_socketpair(PyObject *self, PyObject *args) |
| 3622 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3623 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3624 | SOCKET_T sv[2]; |
| 3625 | int family, type = SOCK_STREAM, proto = 0; |
| 3626 | PyObject *res = NULL; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3627 | |
| 3628 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3629 | family = AF_UNIX; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3630 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3631 | family = AF_INET; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3632 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3633 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3634 | &family, &type, &proto)) |
| 3635 | return NULL; |
| 3636 | /* Create a pair of socket fds */ |
| 3637 | if (socketpair(family, type, proto, sv) < 0) |
| 3638 | return set_error(); |
| 3639 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3640 | if (s0 == NULL) |
| 3641 | goto finally; |
| 3642 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3643 | if (s1 == NULL) |
| 3644 | goto finally; |
| 3645 | res = PyTuple_Pack(2, s0, s1); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3646 | |
| 3647 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3648 | if (res == NULL) { |
| 3649 | if (s0 == NULL) |
| 3650 | SOCKETCLOSE(sv[0]); |
| 3651 | if (s1 == NULL) |
| 3652 | SOCKETCLOSE(sv[1]); |
| 3653 | } |
| 3654 | Py_XDECREF(s0); |
| 3655 | Py_XDECREF(s1); |
| 3656 | return res; |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | PyDoc_STRVAR(socketpair_doc, |
| 3660 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3661 | \n\ |
| 3662 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3663 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3664 | 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] | 3665 | 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] | 3666 | |
| 3667 | #endif /* HAVE_SOCKETPAIR */ |
| 3668 | |
| 3669 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3670 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3671 | /* Create a socket object from a numeric file description. |
| 3672 | Useful e.g. if stdin is a socket. |
| 3673 | Additional arguments as for socket(). */ |
| 3674 | |
| 3675 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3676 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3677 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3678 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3679 | PySocketSockObject *s; |
| 3680 | SOCKET_T fd; |
| 3681 | int family, type, proto = 0; |
| 3682 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 3683 | &fd, &family, &type, &proto)) |
| 3684 | return NULL; |
| 3685 | /* Dup the fd so it and the socket can be closed independently */ |
| 3686 | fd = dup(fd); |
| 3687 | if (fd < 0) |
| 3688 | return set_error(); |
| 3689 | s = new_sockobject(fd, family, type, proto); |
| 3690 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3691 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3693 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3694 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 3695 | \n\ |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 3696 | Create a socket object from a duplicate of the given\n\ |
| 3697 | file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3698 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3699 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3700 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3701 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3702 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3703 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3704 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3705 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3706 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3707 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3708 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3709 | return NULL; |
| 3710 | } |
| 3711 | if (x1 < 0) { |
| 3712 | PyErr_SetString(PyExc_OverflowError, |
| 3713 | "can't convert negative number to unsigned long"); |
| 3714 | return NULL; |
| 3715 | } |
| 3716 | x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3717 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3720 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3721 | "ntohs(integer) -> integer\n\ |
| 3722 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3723 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3724 | |
| 3725 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3726 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3727 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3728 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3729 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3730 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3731 | if (PyInt_Check(arg)) { |
| 3732 | x = PyInt_AS_LONG(arg); |
| 3733 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3734 | return NULL; |
| 3735 | if ((long)x < 0) { |
| 3736 | PyErr_SetString(PyExc_OverflowError, |
| 3737 | "can't convert negative number to unsigned long"); |
| 3738 | return NULL; |
| 3739 | } |
| 3740 | } |
| 3741 | else if (PyLong_Check(arg)) { |
| 3742 | x = PyLong_AsUnsignedLong(arg); |
| 3743 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3744 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3745 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3746 | { |
| 3747 | unsigned long y; |
| 3748 | /* only want the trailing 32 bits */ |
| 3749 | y = x & 0xFFFFFFFFUL; |
| 3750 | if (y ^ x) |
| 3751 | return PyErr_Format(PyExc_OverflowError, |
| 3752 | "long int larger than 32 bits"); |
| 3753 | x = y; |
| 3754 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3755 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3756 | } |
| 3757 | else |
| 3758 | return PyErr_Format(PyExc_TypeError, |
| 3759 | "expected int/long, %s found", |
| 3760 | Py_TYPE(arg)->tp_name); |
| 3761 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3762 | return NULL; |
| 3763 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3766 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3767 | "ntohl(integer) -> integer\n\ |
| 3768 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3769 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3770 | |
| 3771 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3772 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3773 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3774 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3775 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3776 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3777 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3778 | return NULL; |
| 3779 | } |
| 3780 | if (x1 < 0) { |
| 3781 | PyErr_SetString(PyExc_OverflowError, |
| 3782 | "can't convert negative number to unsigned long"); |
| 3783 | return NULL; |
| 3784 | } |
| 3785 | x2 = (unsigned int)htons((unsigned short)x1); |
| 3786 | return PyInt_FromLong(x2); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3789 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3790 | "htons(integer) -> integer\n\ |
| 3791 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3792 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3793 | |
| 3794 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3795 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3796 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3797 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3798 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3800 | if (PyInt_Check(arg)) { |
| 3801 | x = PyInt_AS_LONG(arg); |
| 3802 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3803 | return NULL; |
| 3804 | if ((long)x < 0) { |
| 3805 | PyErr_SetString(PyExc_OverflowError, |
| 3806 | "can't convert negative number to unsigned long"); |
| 3807 | return NULL; |
| 3808 | } |
| 3809 | } |
| 3810 | else if (PyLong_Check(arg)) { |
| 3811 | x = PyLong_AsUnsignedLong(arg); |
| 3812 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3813 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3814 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3815 | { |
| 3816 | unsigned long y; |
| 3817 | /* only want the trailing 32 bits */ |
| 3818 | y = x & 0xFFFFFFFFUL; |
| 3819 | if (y ^ x) |
| 3820 | return PyErr_Format(PyExc_OverflowError, |
| 3821 | "long int larger than 32 bits"); |
| 3822 | x = y; |
| 3823 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3824 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3825 | } |
| 3826 | else |
| 3827 | return PyErr_Format(PyExc_TypeError, |
| 3828 | "expected int/long, %s found", |
| 3829 | Py_TYPE(arg)->tp_name); |
| 3830 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3833 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3834 | "htonl(integer) -> integer\n\ |
| 3835 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3836 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3837 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3838 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3840 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3841 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3842 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3843 | 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] | 3844 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3845 | |
| 3846 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3847 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3848 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3849 | #ifndef INADDR_NONE |
| 3850 | #define INADDR_NONE (-1) |
| 3851 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3852 | #ifdef HAVE_INET_ATON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3853 | struct in_addr buf; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3854 | #endif |
| 3855 | |
| 3856 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 3857 | #if (SIZEOF_INT != 4) |
| 3858 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3859 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3860 | /* Have to use inet_addr() instead */ |
| 3861 | unsigned int packed_addr; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3862 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3863 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3865 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3866 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3867 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3868 | |
| 3869 | #ifdef HAVE_INET_ATON |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3870 | |
| 3871 | #ifdef USE_INET_ATON_WEAKLINK |
| 3872 | if (inet_aton != NULL) { |
| 3873 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3874 | if (inet_aton(ip_addr, &buf)) |
| 3875 | return PyString_FromStringAndSize((char *)(&buf), |
| 3876 | sizeof(buf)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3878 | PyErr_SetString(socket_error, |
| 3879 | "illegal IP address string passed to inet_aton"); |
| 3880 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3881 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3882 | #ifdef USE_INET_ATON_WEAKLINK |
| 3883 | } else { |
| 3884 | #endif |
| 3885 | |
| 3886 | #endif |
| 3887 | |
| 3888 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3889 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3890 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3891 | * for this */ |
| 3892 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3893 | packed_addr = 0xFFFFFFFF; |
| 3894 | } else { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3896 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3898 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3899 | PyErr_SetString(socket_error, |
| 3900 | "illegal IP address string passed to inet_aton"); |
| 3901 | return NULL; |
| 3902 | } |
| 3903 | } |
| 3904 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 3905 | sizeof(packed_addr)); |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3906 | |
| 3907 | #ifdef USE_INET_ATON_WEAKLINK |
| 3908 | } |
| 3909 | #endif |
| 3910 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3911 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3914 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3915 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3916 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3917 | 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] | 3918 | |
| 3919 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3920 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3921 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3922 | char *packed_str; |
| 3923 | int addr_len; |
| 3924 | struct in_addr packed_addr; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3926 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
| 3927 | return NULL; |
| 3928 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3930 | if (addr_len != sizeof(packed_addr)) { |
| 3931 | PyErr_SetString(socket_error, |
| 3932 | "packed IP wrong length for inet_ntoa"); |
| 3933 | return NULL; |
| 3934 | } |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3935 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3936 | memcpy(&packed_addr, packed_str, addr_len); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3938 | return PyString_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3939 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3940 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3941 | #ifdef HAVE_INET_PTON |
| 3942 | |
| 3943 | PyDoc_STRVAR(inet_pton_doc, |
| 3944 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 3945 | \n\ |
| 3946 | Convert an IP address from string format to a packed string suitable\n\ |
| 3947 | for use with low-level network functions."); |
| 3948 | |
| 3949 | static PyObject * |
| 3950 | socket_inet_pton(PyObject *self, PyObject *args) |
| 3951 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3952 | int af; |
| 3953 | char* ip; |
| 3954 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3955 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3956 | 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] | 3957 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3958 | char packed[sizeof(struct in_addr)]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3959 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3960 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3961 | return NULL; |
| 3962 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3963 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 3964 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3965 | if(af == AF_INET6) { |
| 3966 | PyErr_SetString(socket_error, |
| 3967 | "can't use AF_INET6, IPv6 is disabled"); |
| 3968 | return NULL; |
| 3969 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3970 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3972 | retval = inet_pton(af, ip, packed); |
| 3973 | if (retval < 0) { |
| 3974 | PyErr_SetFromErrno(socket_error); |
| 3975 | return NULL; |
| 3976 | } else if (retval == 0) { |
| 3977 | PyErr_SetString(socket_error, |
| 3978 | "illegal IP address string passed to inet_pton"); |
| 3979 | return NULL; |
| 3980 | } else if (af == AF_INET) { |
| 3981 | return PyString_FromStringAndSize(packed, |
| 3982 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3983 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3984 | } else if (af == AF_INET6) { |
| 3985 | return PyString_FromStringAndSize(packed, |
| 3986 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3987 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3988 | } else { |
| 3989 | PyErr_SetString(socket_error, "unknown address family"); |
| 3990 | return NULL; |
| 3991 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3992 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3993 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3994 | PyDoc_STRVAR(inet_ntop_doc, |
| 3995 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3996 | \n\ |
| 3997 | Convert a packed IP address of the given family to string format."); |
| 3998 | |
| 3999 | static PyObject * |
| 4000 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 4001 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4002 | int af; |
| 4003 | char* packed; |
| 4004 | int len; |
| 4005 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4006 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4007 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4008 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4009 | char ip[INET_ADDRSTRLEN + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4010 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 4011 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4012 | /* Guarantee NUL-termination for PyString_FromString() below */ |
| 4013 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4014 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4015 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 4016 | return NULL; |
| 4017 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4018 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4019 | if (af == AF_INET) { |
| 4020 | if (len != sizeof(struct in_addr)) { |
| 4021 | PyErr_SetString(PyExc_ValueError, |
| 4022 | "invalid length of packed IP address string"); |
| 4023 | return NULL; |
| 4024 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4025 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4026 | } else if (af == AF_INET6) { |
| 4027 | if (len != sizeof(struct in6_addr)) { |
| 4028 | PyErr_SetString(PyExc_ValueError, |
| 4029 | "invalid length of packed IP address string"); |
| 4030 | return NULL; |
| 4031 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4032 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4033 | } else { |
| 4034 | PyErr_Format(PyExc_ValueError, |
| 4035 | "unknown address family %d", af); |
| 4036 | return NULL; |
| 4037 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4039 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 4040 | if (!retval) { |
| 4041 | PyErr_SetFromErrno(socket_error); |
| 4042 | return NULL; |
| 4043 | } else { |
| 4044 | return PyString_FromString(retval); |
| 4045 | } |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4046 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4047 | /* NOTREACHED */ |
| 4048 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 4049 | return NULL; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | #endif /* HAVE_INET_PTON */ |
| 4053 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4054 | /* Python interface to getaddrinfo(host, port). */ |
| 4055 | |
| 4056 | /*ARGSUSED*/ |
| 4057 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4058 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4059 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4060 | struct addrinfo hints, *res; |
| 4061 | struct addrinfo *res0 = NULL; |
| 4062 | PyObject *hobj = NULL; |
| 4063 | PyObject *pobj = (PyObject *)NULL; |
| 4064 | char pbuf[30]; |
| 4065 | char *hptr, *pptr; |
| 4066 | int family, socktype, protocol, flags; |
| 4067 | int error; |
| 4068 | PyObject *all = (PyObject *)NULL; |
| 4069 | PyObject *single = (PyObject *)NULL; |
| 4070 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4071 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4072 | family = socktype = protocol = flags = 0; |
| 4073 | family = AF_UNSPEC; |
| 4074 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 4075 | &hobj, &pobj, &family, &socktype, |
| 4076 | &protocol, &flags)) { |
| 4077 | return NULL; |
| 4078 | } |
| 4079 | if (hobj == Py_None) { |
| 4080 | hptr = NULL; |
| 4081 | } else if (PyUnicode_Check(hobj)) { |
| 4082 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 4083 | if (!idna) |
| 4084 | return NULL; |
| 4085 | hptr = PyString_AsString(idna); |
| 4086 | } else if (PyString_Check(hobj)) { |
| 4087 | hptr = PyString_AsString(hobj); |
| 4088 | } else { |
| 4089 | PyErr_SetString(PyExc_TypeError, |
| 4090 | "getaddrinfo() argument 1 must be string or None"); |
| 4091 | return NULL; |
| 4092 | } |
| 4093 | if (PyInt_Check(pobj)) { |
| 4094 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); |
| 4095 | pptr = pbuf; |
| 4096 | } else if (PyString_Check(pobj)) { |
| 4097 | pptr = PyString_AsString(pobj); |
| 4098 | } else if (pobj == Py_None) { |
| 4099 | pptr = (char *)NULL; |
| 4100 | } else { |
| 4101 | PyErr_SetString(socket_error, "Int or String expected"); |
| 4102 | goto err; |
| 4103 | } |
| 4104 | memset(&hints, 0, sizeof(hints)); |
| 4105 | hints.ai_family = family; |
| 4106 | hints.ai_socktype = socktype; |
| 4107 | hints.ai_protocol = protocol; |
| 4108 | hints.ai_flags = flags; |
| 4109 | Py_BEGIN_ALLOW_THREADS |
| 4110 | ACQUIRE_GETADDRINFO_LOCK |
| 4111 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 4112 | Py_END_ALLOW_THREADS |
| 4113 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4114 | if (error) { |
| 4115 | set_gaierror(error); |
| 4116 | goto err; |
| 4117 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4118 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4119 | if ((all = PyList_New(0)) == NULL) |
| 4120 | goto err; |
| 4121 | for (res = res0; res; res = res->ai_next) { |
| 4122 | PyObject *addr = |
| 4123 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 4124 | if (addr == NULL) |
| 4125 | goto err; |
| 4126 | single = Py_BuildValue("iiisO", res->ai_family, |
| 4127 | res->ai_socktype, res->ai_protocol, |
| 4128 | res->ai_canonname ? res->ai_canonname : "", |
| 4129 | addr); |
| 4130 | Py_DECREF(addr); |
| 4131 | if (single == NULL) |
| 4132 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4133 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4134 | if (PyList_Append(all, single)) |
| 4135 | goto err; |
| 4136 | Py_XDECREF(single); |
| 4137 | } |
| 4138 | Py_XDECREF(idna); |
| 4139 | if (res0) |
| 4140 | freeaddrinfo(res0); |
| 4141 | return all; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4142 | err: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4143 | Py_XDECREF(single); |
| 4144 | Py_XDECREF(all); |
| 4145 | Py_XDECREF(idna); |
| 4146 | if (res0) |
| 4147 | freeaddrinfo(res0); |
| 4148 | return (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4151 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4152 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 4153 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4154 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4155 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4156 | |
| 4157 | /* Python interface to getnameinfo(sa, flags). */ |
| 4158 | |
| 4159 | /*ARGSUSED*/ |
| 4160 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4161 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4162 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4163 | PyObject *sa = (PyObject *)NULL; |
| 4164 | int flags; |
| 4165 | char *hostp; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4166 | int port; |
| 4167 | unsigned int flowinfo, scope_id; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4168 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 4169 | struct addrinfo hints, *res = NULL; |
| 4170 | int error; |
| 4171 | PyObject *ret = (PyObject *)NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4172 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4173 | flags = flowinfo = scope_id = 0; |
| 4174 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 4175 | return NULL; |
| 4176 | if (!PyTuple_Check(sa)) { |
| 4177 | PyErr_SetString(PyExc_TypeError, |
| 4178 | "getnameinfo() argument 1 must be a tuple"); |
| 4179 | return NULL; |
| 4180 | } |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4181 | if (!PyArg_ParseTuple(sa, "si|II", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4182 | &hostp, &port, &flowinfo, &scope_id)) |
| 4183 | return NULL; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4184 | if (flowinfo < 0 || flowinfo > 0xfffff) { |
| 4185 | PyErr_SetString(PyExc_OverflowError, |
| 4186 | "getsockaddrarg: flowinfo must be 0-1048575."); |
| 4187 | return NULL; |
| 4188 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4189 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 4190 | memset(&hints, 0, sizeof(hints)); |
| 4191 | hints.ai_family = AF_UNSPEC; |
| 4192 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
| 4193 | Py_BEGIN_ALLOW_THREADS |
| 4194 | ACQUIRE_GETADDRINFO_LOCK |
| 4195 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 4196 | Py_END_ALLOW_THREADS |
| 4197 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 4198 | if (error) { |
| 4199 | set_gaierror(error); |
| 4200 | goto fail; |
| 4201 | } |
| 4202 | if (res->ai_next) { |
| 4203 | PyErr_SetString(socket_error, |
| 4204 | "sockaddr resolved to multiple addresses"); |
| 4205 | goto fail; |
| 4206 | } |
| 4207 | switch (res->ai_family) { |
| 4208 | case AF_INET: |
| 4209 | { |
| 4210 | if (PyTuple_GET_SIZE(sa) != 2) { |
| 4211 | PyErr_SetString(socket_error, |
| 4212 | "IPv4 sockaddr must be 2 tuple"); |
| 4213 | goto fail; |
| 4214 | } |
| 4215 | break; |
| 4216 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 4217 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4218 | case AF_INET6: |
| 4219 | { |
| 4220 | struct sockaddr_in6 *sin6; |
| 4221 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 4222 | sin6->sin6_flowinfo = htonl(flowinfo); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4223 | sin6->sin6_scope_id = scope_id; |
| 4224 | break; |
| 4225 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4226 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4227 | } |
| 4228 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 4229 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 4230 | if (error) { |
| 4231 | set_gaierror(error); |
| 4232 | goto fail; |
| 4233 | } |
| 4234 | ret = Py_BuildValue("ss", hbuf, pbuf); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4235 | |
| 4236 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4237 | if (res) |
| 4238 | freeaddrinfo(res); |
| 4239 | return ret; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4242 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4243 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4244 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4245 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4246 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4247 | |
| 4248 | /* Python API to getting and setting the default timeout value. */ |
| 4249 | |
| 4250 | static PyObject * |
| 4251 | socket_getdefaulttimeout(PyObject *self) |
| 4252 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4253 | if (defaulttimeout < 0.0) { |
| 4254 | Py_INCREF(Py_None); |
| 4255 | return Py_None; |
| 4256 | } |
| 4257 | else |
| 4258 | return PyFloat_FromDouble(defaulttimeout); |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4262 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4263 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4264 | Returns the default timeout in seconds (float) for new socket objects.\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4265 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4266 | When the socket module is first imported, the default is None."); |
| 4267 | |
| 4268 | static PyObject * |
| 4269 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 4270 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4271 | double timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4272 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4273 | if (arg == Py_None) |
| 4274 | timeout = -1.0; |
| 4275 | else { |
| 4276 | timeout = PyFloat_AsDouble(arg); |
| 4277 | if (timeout < 0.0) { |
| 4278 | if (!PyErr_Occurred()) |
| 4279 | PyErr_SetString(PyExc_ValueError, |
| 4280 | "Timeout value out of range"); |
| 4281 | return NULL; |
| 4282 | } |
| 4283 | } |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4284 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4285 | defaulttimeout = timeout; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4286 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4287 | Py_INCREF(Py_None); |
| 4288 | return Py_None; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4292 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4293 | \n\ |
Ezio Melotti | ca5e908 | 2011-08-14 08:27:36 +0300 | [diff] [blame] | 4294 | Set the default timeout in seconds (float) for new socket objects.\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4295 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4296 | When the socket module is first imported, the default is None."); |
| 4297 | |
| 4298 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4299 | /* List of functions exported by this module. */ |
| 4300 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4301 | static PyMethodDef socket_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4302 | {"gethostbyname", socket_gethostbyname, |
| 4303 | METH_VARARGS, gethostbyname_doc}, |
| 4304 | {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4305 | METH_VARARGS, ghbn_ex_doc}, |
| 4306 | {"gethostbyaddr", socket_gethostbyaddr, |
| 4307 | METH_VARARGS, gethostbyaddr_doc}, |
| 4308 | {"gethostname", socket_gethostname, |
| 4309 | METH_NOARGS, gethostname_doc}, |
| 4310 | {"getservbyname", socket_getservbyname, |
| 4311 | METH_VARARGS, getservbyname_doc}, |
| 4312 | {"getservbyport", socket_getservbyport, |
| 4313 | METH_VARARGS, getservbyport_doc}, |
| 4314 | {"getprotobyname", socket_getprotobyname, |
| 4315 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4316 | #ifndef NO_DUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4317 | {"fromfd", socket_fromfd, |
| 4318 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4319 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4320 | #ifdef HAVE_SOCKETPAIR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4321 | {"socketpair", socket_socketpair, |
| 4322 | METH_VARARGS, socketpair_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4323 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4324 | {"ntohs", socket_ntohs, |
| 4325 | METH_VARARGS, ntohs_doc}, |
| 4326 | {"ntohl", socket_ntohl, |
| 4327 | METH_O, ntohl_doc}, |
| 4328 | {"htons", socket_htons, |
| 4329 | METH_VARARGS, htons_doc}, |
| 4330 | {"htonl", socket_htonl, |
| 4331 | METH_O, htonl_doc}, |
| 4332 | {"inet_aton", socket_inet_aton, |
| 4333 | METH_VARARGS, inet_aton_doc}, |
| 4334 | {"inet_ntoa", socket_inet_ntoa, |
| 4335 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4336 | #ifdef HAVE_INET_PTON |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4337 | {"inet_pton", socket_inet_pton, |
| 4338 | METH_VARARGS, inet_pton_doc}, |
| 4339 | {"inet_ntop", socket_inet_ntop, |
| 4340 | METH_VARARGS, inet_ntop_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4341 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4342 | {"getaddrinfo", socket_getaddrinfo, |
| 4343 | METH_VARARGS, getaddrinfo_doc}, |
| 4344 | {"getnameinfo", socket_getnameinfo, |
| 4345 | METH_VARARGS, getnameinfo_doc}, |
| 4346 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4347 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4348 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4349 | METH_O, setdefaulttimeout_doc}, |
| 4350 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4351 | }; |
| 4352 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4353 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4354 | #ifdef RISCOS |
| 4355 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4356 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4357 | static int |
| 4358 | os_init(void) |
| 4359 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4360 | _kernel_swi_regs r; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4361 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4362 | r.r[0] = 0; |
| 4363 | _kernel_swi(0x43380, &r, &r); |
| 4364 | taskwindow = r.r[0]; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4366 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | #endif /* RISCOS */ |
| 4370 | |
| 4371 | |
| 4372 | #ifdef MS_WINDOWS |
| 4373 | #define OS_INIT_DEFINED |
| 4374 | |
| 4375 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4376 | |
| 4377 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4378 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4379 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4380 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4384 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4385 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4386 | WSADATA WSAData; |
| 4387 | int ret; |
| 4388 | char buf[100]; |
| 4389 | ret = WSAStartup(0x0101, &WSAData); |
| 4390 | switch (ret) { |
| 4391 | case 0: /* No error */ |
| 4392 | Py_AtExit(os_cleanup); |
| 4393 | return 1; /* Success */ |
| 4394 | case WSASYSNOTREADY: |
| 4395 | PyErr_SetString(PyExc_ImportError, |
| 4396 | "WSAStartup failed: network not ready"); |
| 4397 | break; |
| 4398 | case WSAVERNOTSUPPORTED: |
| 4399 | case WSAEINVAL: |
| 4400 | PyErr_SetString( |
| 4401 | PyExc_ImportError, |
| 4402 | "WSAStartup failed: requested version not supported"); |
| 4403 | break; |
| 4404 | default: |
| 4405 | PyOS_snprintf(buf, sizeof(buf), |
| 4406 | "WSAStartup failed: error code %d", ret); |
| 4407 | PyErr_SetString(PyExc_ImportError, buf); |
| 4408 | break; |
| 4409 | } |
| 4410 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4413 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4414 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4415 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4416 | #ifdef PYOS_OS2 |
| 4417 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4418 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4419 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4420 | |
| 4421 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4422 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4423 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4424 | #ifndef PYCC_GCC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4425 | char reason[64]; |
| 4426 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4427 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4428 | if (rc == 0) { |
| 4429 | return 1; /* Success */ |
| 4430 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4432 | PyOS_snprintf(reason, sizeof(reason), |
| 4433 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 4434 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4435 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4436 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4437 | #else |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4438 | /* No need to initialize sockets with GCC/EMX */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4439 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4440 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | #endif /* PYOS_OS2 */ |
| 4444 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4445 | |
| 4446 | #ifndef OS_INIT_DEFINED |
| 4447 | static int |
| 4448 | os_init(void) |
| 4449 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4450 | return 1; /* Success */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4451 | } |
| 4452 | #endif |
| 4453 | |
| 4454 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4455 | /* C API table - always add new things to the end for binary |
| 4456 | compatibility. */ |
| 4457 | static |
| 4458 | PySocketModule_APIObject PySocketModuleAPI = |
| 4459 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4460 | &sock_type, |
| 4461 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4462 | }; |
| 4463 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4464 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4465 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4466 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4467 | This module is actually called "_socket", and there's a wrapper |
| 4468 | "socket.py" which implements some additional functionality. On some |
| 4469 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 4470 | wrapper for the socket type that provides missing functionality such |
| 4471 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 4472 | with an ImportError exception if os-specific initialization fails. |
| 4473 | On Windows, this does WINSOCK initialization. When WINSOCK is |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 4474 | initialized successfully, a call to WSACleanup() is scheduled to be |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4475 | made at exit time. |
| 4476 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4478 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4479 | "Implementation module for socket operations.\n\ |
| 4480 | \n\ |
| 4481 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4482 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4483 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4484 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4485 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4486 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4488 | if (!os_init()) |
| 4489 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4490 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4491 | Py_TYPE(&sock_type) = &PyType_Type; |
| 4492 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 4493 | socket_methods, |
| 4494 | socket_doc); |
| 4495 | if (m == NULL) |
| 4496 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4497 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4498 | socket_error = PyErr_NewException("socket.error", |
| 4499 | PyExc_IOError, NULL); |
| 4500 | if (socket_error == NULL) |
| 4501 | return; |
| 4502 | PySocketModuleAPI.error = socket_error; |
| 4503 | Py_INCREF(socket_error); |
| 4504 | PyModule_AddObject(m, "error", socket_error); |
| 4505 | socket_herror = PyErr_NewException("socket.herror", |
| 4506 | socket_error, NULL); |
| 4507 | if (socket_herror == NULL) |
| 4508 | return; |
| 4509 | Py_INCREF(socket_herror); |
| 4510 | PyModule_AddObject(m, "herror", socket_herror); |
| 4511 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4512 | NULL); |
| 4513 | if (socket_gaierror == NULL) |
| 4514 | return; |
| 4515 | Py_INCREF(socket_gaierror); |
| 4516 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4517 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4518 | socket_error, NULL); |
| 4519 | if (socket_timeout == NULL) |
| 4520 | return; |
| 4521 | Py_INCREF(socket_timeout); |
| 4522 | PyModule_AddObject(m, "timeout", socket_timeout); |
| 4523 | Py_INCREF((PyObject *)&sock_type); |
| 4524 | if (PyModule_AddObject(m, "SocketType", |
| 4525 | (PyObject *)&sock_type) != 0) |
| 4526 | return; |
| 4527 | Py_INCREF((PyObject *)&sock_type); |
| 4528 | if (PyModule_AddObject(m, "socket", |
| 4529 | (PyObject *)&sock_type) != 0) |
| 4530 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4531 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4532 | #ifdef ENABLE_IPV6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4533 | has_ipv6 = Py_True; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4534 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4535 | has_ipv6 = Py_False; |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4536 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4537 | Py_INCREF(has_ipv6); |
| 4538 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4539 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4540 | /* Export C API */ |
| 4541 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4542 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4543 | ) != 0) |
| 4544 | return; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4546 | /* Address families (we only support AF_INET and AF_UNIX) */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4547 | #ifdef AF_UNSPEC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4548 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4549 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4550 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4551 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4552 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4553 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4554 | #if defined(AF_UNIX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4555 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4556 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4557 | #ifdef AF_AX25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4558 | /* Amateur Radio AX.25 */ |
| 4559 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4560 | #endif |
| 4561 | #ifdef AF_IPX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4562 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4563 | #endif |
| 4564 | #ifdef AF_APPLETALK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4565 | /* Appletalk DDP */ |
| 4566 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4567 | #endif |
| 4568 | #ifdef AF_NETROM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4569 | /* Amateur radio NetROM */ |
| 4570 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4571 | #endif |
| 4572 | #ifdef AF_BRIDGE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4573 | /* Multiprotocol bridge */ |
| 4574 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4575 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4576 | #ifdef AF_ATMPVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4577 | /* ATM PVCs */ |
| 4578 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4579 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4580 | #ifdef AF_AAL5 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4581 | /* Reserved for Werner's ATM */ |
| 4582 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4583 | #endif |
| 4584 | #ifdef AF_X25 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4585 | /* Reserved for X.25 project */ |
| 4586 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4587 | #endif |
| 4588 | #ifdef AF_INET6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4589 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4590 | #endif |
| 4591 | #ifdef AF_ROSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4592 | /* Amateur Radio X.25 PLP */ |
| 4593 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4594 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4595 | #ifdef AF_DECnet |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4596 | /* Reserved for DECnet project */ |
| 4597 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4598 | #endif |
| 4599 | #ifdef AF_NETBEUI |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4600 | /* Reserved for 802.2LLC project */ |
| 4601 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4602 | #endif |
| 4603 | #ifdef AF_SECURITY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4604 | /* Security callback pseudo AF */ |
| 4605 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4606 | #endif |
| 4607 | #ifdef AF_KEY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4608 | /* PF_KEY key management API */ |
| 4609 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4610 | #endif |
| 4611 | #ifdef AF_NETLINK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4612 | /* */ |
| 4613 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4614 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4615 | #ifdef NETLINK_SKIP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4616 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4617 | #endif |
| 4618 | #ifdef NETLINK_W1 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4619 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4620 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4621 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4622 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4623 | #ifdef NETLINK_TCPDIAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4624 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4625 | #endif |
| 4626 | #ifdef NETLINK_NFLOG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4627 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4628 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4629 | #ifdef NETLINK_XFRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4630 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4631 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4632 | #ifdef NETLINK_ARPD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4633 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4634 | #endif |
| 4635 | #ifdef NETLINK_ROUTE6 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4636 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4637 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4638 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Martin v. Löwis | b1cc1d4 | 2007-02-13 12:14:19 +0000 | [diff] [blame] | 4639 | #ifdef NETLINK_DNRTMSG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4640 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
| 4641 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4642 | #ifdef NETLINK_TAPBASE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4643 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4644 | #endif |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 4645 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4646 | #ifdef AF_ROUTE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4647 | /* Alias to emulate 4.4BSD */ |
| 4648 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4649 | #endif |
| 4650 | #ifdef AF_ASH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4651 | /* Ash */ |
| 4652 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4653 | #endif |
| 4654 | #ifdef AF_ECONET |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4655 | /* Acorn Econet */ |
| 4656 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4657 | #endif |
| 4658 | #ifdef AF_ATMSVC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4659 | /* ATM SVCs */ |
| 4660 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4661 | #endif |
| 4662 | #ifdef AF_SNA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4663 | /* Linux SNA Project (nutters!) */ |
| 4664 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4665 | #endif |
| 4666 | #ifdef AF_IRDA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4667 | /* IRDA sockets */ |
| 4668 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4669 | #endif |
| 4670 | #ifdef AF_PPPOX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4671 | /* PPPoX sockets */ |
| 4672 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4673 | #endif |
| 4674 | #ifdef AF_WANPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4675 | /* Wanpipe API Sockets */ |
| 4676 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4677 | #endif |
| 4678 | #ifdef AF_LLC |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4679 | /* Linux LLC */ |
| 4680 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4681 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4682 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4683 | #ifdef USE_BLUETOOTH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4684 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4685 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4686 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4687 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4688 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4689 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4690 | #endif |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4691 | #if !defined(__FreeBSD__) |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4692 | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4693 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 4694 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4695 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4696 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4697 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4698 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4699 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4700 | 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] | 4701 | #endif |
| 4702 | |
Antoine Pitrou | e0c5f3c | 2010-10-27 20:35:26 +0000 | [diff] [blame] | 4703 | #ifdef AF_PACKET |
| 4704 | PyModule_AddIntMacro(m, AF_PACKET); |
| 4705 | #endif |
| 4706 | #ifdef PF_PACKET |
| 4707 | PyModule_AddIntMacro(m, PF_PACKET); |
| 4708 | #endif |
| 4709 | #ifdef PACKET_HOST |
| 4710 | PyModule_AddIntMacro(m, PACKET_HOST); |
| 4711 | #endif |
| 4712 | #ifdef PACKET_BROADCAST |
| 4713 | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
| 4714 | #endif |
| 4715 | #ifdef PACKET_MULTICAST |
| 4716 | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
| 4717 | #endif |
| 4718 | #ifdef PACKET_OTHERHOST |
| 4719 | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
| 4720 | #endif |
| 4721 | #ifdef PACKET_OUTGOING |
| 4722 | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
| 4723 | #endif |
| 4724 | #ifdef PACKET_LOOPBACK |
| 4725 | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
| 4726 | #endif |
| 4727 | #ifdef PACKET_FASTROUTE |
| 4728 | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4729 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4730 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4731 | #ifdef HAVE_LINUX_TIPC_H |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4732 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4734 | /* for addresses */ |
| 4735 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4736 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4737 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4738 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4739 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4740 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4741 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4742 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4743 | /* for setsockopt() */ |
| 4744 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4745 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4746 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4747 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4748 | TIPC_DEST_DROPPABLE); |
| 4749 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4751 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4752 | TIPC_LOW_IMPORTANCE); |
| 4753 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4754 | TIPC_MEDIUM_IMPORTANCE); |
| 4755 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4756 | TIPC_HIGH_IMPORTANCE); |
| 4757 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4758 | TIPC_CRITICAL_IMPORTANCE); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4759 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4760 | /* for subscriptions */ |
| 4761 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4762 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4763 | #ifdef TIPC_SUB_CANCEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4764 | /* doesn't seem to be available everywhere */ |
| 4765 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
Georg Brandl | ff15c86 | 2008-01-11 09:19:11 +0000 | [diff] [blame] | 4766 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4767 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4768 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4769 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4770 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4771 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4772 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 4773 | #endif |
| 4774 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4775 | /* Socket types */ |
| 4776 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4777 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4778 | #ifndef __BEOS__ |
| 4779 | /* We have incomplete socket support. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4780 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4781 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4782 | #if defined(SOCK_RDM) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4783 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4784 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4785 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4786 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4787 | #ifdef SO_DEBUG |
| 4788 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4789 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4790 | #ifdef SO_ACCEPTCONN |
| 4791 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4792 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4793 | #ifdef SO_REUSEADDR |
| 4794 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4795 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4796 | #ifdef SO_EXCLUSIVEADDRUSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4797 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4798 | #endif |
| 4799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4800 | #ifdef SO_KEEPALIVE |
| 4801 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4802 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4803 | #ifdef SO_DONTROUTE |
| 4804 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4805 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4806 | #ifdef SO_BROADCAST |
| 4807 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4808 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4809 | #ifdef SO_USELOOPBACK |
| 4810 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4811 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4812 | #ifdef SO_LINGER |
| 4813 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4814 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4815 | #ifdef SO_OOBINLINE |
| 4816 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4817 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4818 | #ifdef SO_REUSEPORT |
| 4819 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4820 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4821 | #ifdef SO_SNDBUF |
| 4822 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4823 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4824 | #ifdef SO_RCVBUF |
| 4825 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4826 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4827 | #ifdef SO_SNDLOWAT |
| 4828 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4829 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4830 | #ifdef SO_RCVLOWAT |
| 4831 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4832 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4833 | #ifdef SO_SNDTIMEO |
| 4834 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4835 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4836 | #ifdef SO_RCVTIMEO |
| 4837 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4838 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4839 | #ifdef SO_ERROR |
| 4840 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4841 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4842 | #ifdef SO_TYPE |
| 4843 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4844 | #endif |
Larry Hastings | 3b958e3 | 2010-04-02 11:18:17 +0000 | [diff] [blame] | 4845 | #ifdef SO_SETFIB |
| 4846 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB); |
| 4847 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4848 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4849 | /* Maximum number of connections for "listen" */ |
| 4850 | #ifdef SOMAXCONN |
| 4851 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4852 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4853 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4854 | #endif |
| 4855 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4856 | /* Flags for send, recv */ |
| 4857 | #ifdef MSG_OOB |
| 4858 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4859 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4860 | #ifdef MSG_PEEK |
| 4861 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4862 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4863 | #ifdef MSG_DONTROUTE |
| 4864 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4865 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4866 | #ifdef MSG_DONTWAIT |
| 4867 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4868 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4869 | #ifdef MSG_EOR |
| 4870 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4871 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4872 | #ifdef MSG_TRUNC |
| 4873 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4874 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4875 | #ifdef MSG_CTRUNC |
| 4876 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4877 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4878 | #ifdef MSG_WAITALL |
| 4879 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4880 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4881 | #ifdef MSG_BTAG |
| 4882 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4883 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4884 | #ifdef MSG_ETAG |
| 4885 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4886 | #endif |
| 4887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4888 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4889 | #ifdef SOL_SOCKET |
| 4890 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4891 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4892 | #ifdef SOL_IP |
| 4893 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4894 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4895 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4896 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4897 | #ifdef SOL_IPX |
| 4898 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4899 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4900 | #ifdef SOL_AX25 |
| 4901 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4902 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4903 | #ifdef SOL_ATALK |
| 4904 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4905 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4906 | #ifdef SOL_NETROM |
| 4907 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4908 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4909 | #ifdef SOL_ROSE |
| 4910 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4911 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4912 | #ifdef SOL_TCP |
| 4913 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4914 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4915 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4916 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4917 | #ifdef SOL_UDP |
| 4918 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4919 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4920 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4921 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4922 | #ifdef IPPROTO_IP |
| 4923 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4924 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4925 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4926 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4927 | #ifdef IPPROTO_HOPOPTS |
| 4928 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4929 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4930 | #ifdef IPPROTO_ICMP |
| 4931 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4932 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4933 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4934 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4935 | #ifdef IPPROTO_IGMP |
| 4936 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4937 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4938 | #ifdef IPPROTO_GGP |
| 4939 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4940 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4941 | #ifdef IPPROTO_IPV4 |
| 4942 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4943 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4944 | #ifdef IPPROTO_IPV6 |
| 4945 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 4946 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4947 | #ifdef IPPROTO_IPIP |
| 4948 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4949 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4950 | #ifdef IPPROTO_TCP |
| 4951 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4952 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4953 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4954 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4955 | #ifdef IPPROTO_EGP |
| 4956 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4957 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4958 | #ifdef IPPROTO_PUP |
| 4959 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4960 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4961 | #ifdef IPPROTO_UDP |
| 4962 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4963 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4964 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4965 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4966 | #ifdef IPPROTO_IDP |
| 4967 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4968 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4969 | #ifdef IPPROTO_HELLO |
| 4970 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4971 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4972 | #ifdef IPPROTO_ND |
| 4973 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4974 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4975 | #ifdef IPPROTO_TP |
| 4976 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4977 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4978 | #ifdef IPPROTO_IPV6 |
| 4979 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4980 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4981 | #ifdef IPPROTO_ROUTING |
| 4982 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4983 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4984 | #ifdef IPPROTO_FRAGMENT |
| 4985 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4986 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4987 | #ifdef IPPROTO_RSVP |
| 4988 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4989 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4990 | #ifdef IPPROTO_GRE |
| 4991 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4992 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4993 | #ifdef IPPROTO_ESP |
| 4994 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4995 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4996 | #ifdef IPPROTO_AH |
| 4997 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4998 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 4999 | #ifdef IPPROTO_MOBILE |
| 5000 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5001 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5002 | #ifdef IPPROTO_ICMPV6 |
| 5003 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5004 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5005 | #ifdef IPPROTO_NONE |
| 5006 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5007 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5008 | #ifdef IPPROTO_DSTOPTS |
| 5009 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5010 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5011 | #ifdef IPPROTO_XTP |
| 5012 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5013 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5014 | #ifdef IPPROTO_EON |
| 5015 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5016 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5017 | #ifdef IPPROTO_PIM |
| 5018 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5019 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5020 | #ifdef IPPROTO_IPCOMP |
| 5021 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5022 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5023 | #ifdef IPPROTO_VRRP |
| 5024 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5025 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5026 | #ifdef IPPROTO_BIP |
| 5027 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5028 | #endif |
| 5029 | /**/ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5030 | #ifdef IPPROTO_RAW |
| 5031 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 5032 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5033 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5034 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5035 | #ifdef IPPROTO_MAX |
| 5036 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5037 | #endif |
| 5038 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5039 | /* Some port configuration */ |
| 5040 | #ifdef IPPORT_RESERVED |
| 5041 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5042 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5043 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5044 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5045 | #ifdef IPPORT_USERRESERVED |
| 5046 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5047 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5048 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5049 | #endif |
| 5050 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5051 | /* Some reserved IP v.4 addresses */ |
| 5052 | #ifdef INADDR_ANY |
| 5053 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5054 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5055 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5056 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5057 | #ifdef INADDR_BROADCAST |
| 5058 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5059 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5060 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5061 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5062 | #ifdef INADDR_LOOPBACK |
| 5063 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5064 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5065 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5066 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5067 | #ifdef INADDR_UNSPEC_GROUP |
| 5068 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5069 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5070 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5071 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5072 | #ifdef INADDR_ALLHOSTS_GROUP |
| 5073 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 5074 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5075 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5076 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5077 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5078 | #ifdef INADDR_MAX_LOCAL_GROUP |
| 5079 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 5080 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5081 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5082 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5083 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5084 | #ifdef INADDR_NONE |
| 5085 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5086 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5087 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 5088 | #endif |
| 5089 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5090 | /* IPv4 [gs]etsockopt options */ |
| 5091 | #ifdef IP_OPTIONS |
| 5092 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5093 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5094 | #ifdef IP_HDRINCL |
| 5095 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5096 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5097 | #ifdef IP_TOS |
| 5098 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5099 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5100 | #ifdef IP_TTL |
| 5101 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5102 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5103 | #ifdef IP_RECVOPTS |
| 5104 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5105 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5106 | #ifdef IP_RECVRETOPTS |
| 5107 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5108 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5109 | #ifdef IP_RECVDSTADDR |
| 5110 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5111 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5112 | #ifdef IP_RETOPTS |
| 5113 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5114 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5115 | #ifdef IP_MULTICAST_IF |
| 5116 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5117 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5118 | #ifdef IP_MULTICAST_TTL |
| 5119 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5120 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5121 | #ifdef IP_MULTICAST_LOOP |
| 5122 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5123 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5124 | #ifdef IP_ADD_MEMBERSHIP |
| 5125 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5126 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5127 | #ifdef IP_DROP_MEMBERSHIP |
| 5128 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 5129 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5130 | #ifdef IP_DEFAULT_MULTICAST_TTL |
| 5131 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 5132 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5133 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5134 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 5135 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 5136 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5137 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5138 | #ifdef IP_MAX_MEMBERSHIPS |
| 5139 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5140 | #endif |
| 5141 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5142 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 5143 | #ifdef IPV6_JOIN_GROUP |
| 5144 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5145 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5146 | #ifdef IPV6_LEAVE_GROUP |
| 5147 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5148 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5149 | #ifdef IPV6_MULTICAST_HOPS |
| 5150 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5151 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5152 | #ifdef IPV6_MULTICAST_IF |
| 5153 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5154 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5155 | #ifdef IPV6_MULTICAST_LOOP |
| 5156 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5157 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5158 | #ifdef IPV6_UNICAST_HOPS |
| 5159 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5160 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5161 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5162 | #ifdef IPV6_V6ONLY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5163 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5164 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5165 | /* Advanced IPV6 socket options, from RFC 3542 */ |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5166 | #ifdef IPV6_CHECKSUM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5167 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5168 | #endif |
| 5169 | #ifdef IPV6_DONTFRAG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5170 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5171 | #endif |
| 5172 | #ifdef IPV6_DSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5173 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5174 | #endif |
| 5175 | #ifdef IPV6_HOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5176 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5177 | #endif |
| 5178 | #ifdef IPV6_HOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5179 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5180 | #endif |
| 5181 | #ifdef IPV6_NEXTHOP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5182 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5183 | #endif |
| 5184 | #ifdef IPV6_PATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5185 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5186 | #endif |
| 5187 | #ifdef IPV6_PKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5188 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5189 | #endif |
| 5190 | #ifdef IPV6_RECVDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5191 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5192 | #endif |
| 5193 | #ifdef IPV6_RECVHOPLIMIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5194 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5195 | #endif |
| 5196 | #ifdef IPV6_RECVHOPOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5197 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5198 | #endif |
| 5199 | #ifdef IPV6_RECVPKTINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5200 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5201 | #endif |
| 5202 | #ifdef IPV6_RECVRTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5203 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5204 | #endif |
| 5205 | #ifdef IPV6_RECVTCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5206 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5207 | #endif |
| 5208 | #ifdef IPV6_RTHDR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5209 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5210 | #endif |
| 5211 | #ifdef IPV6_RTHDRDSTOPTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5212 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5213 | #endif |
| 5214 | #ifdef IPV6_RTHDR_TYPE_0 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5215 | 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] | 5216 | #endif |
| 5217 | #ifdef IPV6_RECVPATHMTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5218 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5219 | #endif |
| 5220 | #ifdef IPV6_TCLASS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5221 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5222 | #endif |
| 5223 | #ifdef IPV6_USE_MIN_MTU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5224 | 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] | 5225 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 5226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5227 | /* TCP options */ |
| 5228 | #ifdef TCP_NODELAY |
| 5229 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5230 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5231 | #ifdef TCP_MAXSEG |
| 5232 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5233 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5234 | #ifdef TCP_CORK |
| 5235 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5236 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5237 | #ifdef TCP_KEEPIDLE |
| 5238 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5239 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5240 | #ifdef TCP_KEEPINTVL |
| 5241 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5242 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5243 | #ifdef TCP_KEEPCNT |
| 5244 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5245 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5246 | #ifdef TCP_SYNCNT |
| 5247 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5248 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5249 | #ifdef TCP_LINGER2 |
| 5250 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5251 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5252 | #ifdef TCP_DEFER_ACCEPT |
| 5253 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5254 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5255 | #ifdef TCP_WINDOW_CLAMP |
| 5256 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5257 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5258 | #ifdef TCP_INFO |
| 5259 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5260 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5261 | #ifdef TCP_QUICKACK |
| 5262 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 5263 | #endif |
| 5264 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5266 | /* IPX options */ |
| 5267 | #ifdef IPX_TYPE |
| 5268 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 5269 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5270 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5271 | /* get{addr,name}info parameters */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5272 | #ifdef EAI_ADDRFAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5273 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5274 | #endif |
| 5275 | #ifdef EAI_AGAIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5276 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5277 | #endif |
| 5278 | #ifdef EAI_BADFLAGS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5279 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5280 | #endif |
| 5281 | #ifdef EAI_FAIL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5282 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5283 | #endif |
| 5284 | #ifdef EAI_FAMILY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5285 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5286 | #endif |
| 5287 | #ifdef EAI_MEMORY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5288 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5289 | #endif |
| 5290 | #ifdef EAI_NODATA |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5291 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5292 | #endif |
| 5293 | #ifdef EAI_NONAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5294 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5295 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5296 | #ifdef EAI_OVERFLOW |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5297 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5298 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5299 | #ifdef EAI_SERVICE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5300 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5301 | #endif |
| 5302 | #ifdef EAI_SOCKTYPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5303 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5304 | #endif |
| 5305 | #ifdef EAI_SYSTEM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5306 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5307 | #endif |
| 5308 | #ifdef EAI_BADHINTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5309 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5310 | #endif |
| 5311 | #ifdef EAI_PROTOCOL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5312 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5313 | #endif |
| 5314 | #ifdef EAI_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5315 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5316 | #endif |
| 5317 | #ifdef AI_PASSIVE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5318 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5319 | #endif |
| 5320 | #ifdef AI_CANONNAME |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5321 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5322 | #endif |
| 5323 | #ifdef AI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5324 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5325 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5326 | #ifdef AI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5327 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5328 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5329 | #ifdef AI_MASK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5330 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5331 | #endif |
| 5332 | #ifdef AI_ALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5333 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5334 | #endif |
| 5335 | #ifdef AI_V4MAPPED_CFG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5336 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5337 | #endif |
| 5338 | #ifdef AI_ADDRCONFIG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5339 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5340 | #endif |
| 5341 | #ifdef AI_V4MAPPED |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5342 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5343 | #endif |
| 5344 | #ifdef AI_DEFAULT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5345 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5346 | #endif |
| 5347 | #ifdef NI_MAXHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5348 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5349 | #endif |
| 5350 | #ifdef NI_MAXSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5351 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5352 | #endif |
| 5353 | #ifdef NI_NOFQDN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5354 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5355 | #endif |
| 5356 | #ifdef NI_NUMERICHOST |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5357 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5358 | #endif |
| 5359 | #ifdef NI_NAMEREQD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5360 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5361 | #endif |
| 5362 | #ifdef NI_NUMERICSERV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5363 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5364 | #endif |
| 5365 | #ifdef NI_DGRAM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5366 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5367 | #endif |
| 5368 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5369 | /* shutdown() parameters */ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5370 | #ifdef SHUT_RD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5371 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5372 | #elif defined(SD_RECEIVE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5373 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5374 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5375 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5376 | #endif |
| 5377 | #ifdef SHUT_WR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5378 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5379 | #elif defined(SD_SEND) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5380 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5381 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5382 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5383 | #endif |
| 5384 | #ifdef SHUT_RDWR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5385 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5386 | #elif defined(SD_BOTH) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5387 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5388 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5389 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5390 | #endif |
| 5391 | |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5392 | #ifdef SIO_RCVALL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5393 | { |
| 5394 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; |
| 5395 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; |
| 5396 | int i; |
| 5397 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) { |
| 5398 | PyObject *tmp; |
| 5399 | tmp = PyLong_FromUnsignedLong(codes[i]); |
| 5400 | if (tmp == NULL) |
| 5401 | return; |
| 5402 | PyModule_AddObject(m, names[i], tmp); |
| 5403 | } |
| 5404 | } |
| 5405 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5406 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5407 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5408 | #ifdef RCVALL_IPLEVEL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5409 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5410 | #endif |
| 5411 | #ifdef RCVALL_MAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5412 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
Amaury Forgeot d'Arc | 94eba71 | 2008-03-28 21:55:29 +0000 | [diff] [blame] | 5413 | #endif |
Christian Heimes | 04ae916 | 2008-01-04 15:23:30 +0000 | [diff] [blame] | 5414 | #endif /* _MSTCPIP_ */ |
| 5415 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5416 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5417 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5418 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5419 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5420 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5421 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5422 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5423 | #ifndef HAVE_INET_PTON |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5424 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5425 | |
| 5426 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5427 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5428 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5429 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5430 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5431 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5432 | if (af == AF_INET) { |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 5433 | #if (SIZEOF_INT != 4) |
| 5434 | #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5435 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5436 | unsigned int packed_addr; |
| 5437 | packed_addr = inet_addr(src); |
| 5438 | if (packed_addr == INADDR_NONE) |
| 5439 | return 0; |
| 5440 | memcpy(dst, &packed_addr, 4); |
| 5441 | return 1; |
| 5442 | } |
| 5443 | /* Should set errno to EAFNOSUPPORT */ |
| 5444 | return -1; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5447 | const char * |
| 5448 | 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] | 5449 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5450 | if (af == AF_INET) { |
| 5451 | struct in_addr packed_addr; |
| 5452 | if (size < 16) |
| 5453 | /* Should set errno to ENOSPC. */ |
| 5454 | return NULL; |
| 5455 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5456 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5457 | } |
| 5458 | /* Should set errno to EAFNOSUPPORT */ |
| 5459 | return NULL; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5460 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5461 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5462 | #endif |
Christian Heimes | e8954f8 | 2007-11-22 11:21:16 +0000 | [diff] [blame] | 5463 | #endif |