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 |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 10 | portable manner, though AF_PACKET and AF_NETLINK are supported under Linux. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 11 | - No read/write operations (use sendall/recv or makefile instead). |
| 12 | - Additional restrictions apply on some non-Unix platforms (compensated |
| 13 | for by socket.py). |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 15 | Module interface: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 17 | - socket.error: exception raised for socket specific errors |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 18 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
| 19 | a subclass of socket.error |
| 20 | - socket.herror: exception raised for gethostby* errors, |
| 21 | a subclass of socket.error |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 22 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 23 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 24 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 25 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 26 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 27 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 28 | - socket.socket([family[, type [, proto, fileno]]]) --> new socket object |
| 29 | (fileno specifies a pre-existing socket file descriptor) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 30 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 31 | - socket.ntohs(16 bit value) --> new int object |
| 32 | - socket.ntohl(32 bit value) --> new int object |
| 33 | - socket.htons(16 bit value) --> new int object |
| 34 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 35 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
| 36 | --> List of (family, socktype, proto, canonname, sockaddr) |
| 37 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 38 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 39 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 40 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 41 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 42 | - socket.getdefaulttimeout() -> None | float |
| 43 | - socket.setdefaulttimeout(None | float) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 44 | - an Internet socket address is a pair (hostname, port) |
| 45 | where hostname can be anything recognized by gethostbyname() |
| 46 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 47 | - 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] | 48 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 49 | - an AF_PACKET socket address is a tuple containing a string |
| 50 | specifying the ethernet interface and an integer specifying |
| 51 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 52 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 53 | specify packet-type and ha-type/addr. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 55 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 57 | - names starting with sock_ are socket object methods |
| 58 | - names starting with socket_ are module-level functions |
| 59 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 61 | */ |
| 62 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 63 | #ifdef __APPLE__ |
| 64 | /* |
| 65 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 66 | * that was build on 10.4 or later to work on that release, weak linking |
| 67 | * comes to the rescue. |
| 68 | */ |
| 69 | # pragma weak inet_aton |
| 70 | #endif |
| 71 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 72 | #include "Python.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 75 | #undef MAX |
| 76 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 77 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 78 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 79 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 80 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 81 | \n\ |
| 82 | Open a socket of the given type. The family argument specifies the\n\ |
| 83 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 84 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 85 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 86 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 87 | \n\ |
| 88 | A socket object represents one endpoint of a network connection.\n\ |
| 89 | \n\ |
| 90 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 91 | \n\ |
| 92 | accept() -- accept a connection, returning new socket and client address\n\ |
| 93 | bind(addr) -- bind the socket to a local address\n\ |
| 94 | close() -- close the socket\n\ |
| 95 | connect(addr) -- connect the socket to a remote address\n\ |
| 96 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
| 97 | dup() -- return a new socket object identical to the current one [*]\n\ |
| 98 | fileno() -- return underlying file descriptor\n\ |
| 99 | getpeername() -- return remote address [*]\n\ |
| 100 | getsockname() -- return local address\n\ |
| 101 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 102 | gettimeout() -- return timeout or None\n\ |
| 103 | listen(n) -- start listening for incoming connections\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 104 | recv(buflen[, flags]) -- receive data\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 105 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 106 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 107 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 108 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 109 | sendall(data[, flags]) -- send all data\n\ |
| 110 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 111 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 112 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 113 | setsockopt(level, optname, value) -- set socket options\n\ |
| 114 | settimeout(None | float) -- set or clear the timeout\n\ |
| 115 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 116 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 117 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 118 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 119 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 120 | I hope some day someone can clean this up please... */ |
| 121 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 122 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 123 | script doesn't get this right, so we hardcode some platform checks below. |
| 124 | On the other hand, not all Linux versions agree, so there the settings |
| 125 | computed by the configure script are needed! */ |
| 126 | |
| 127 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 128 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 129 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 130 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 131 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 133 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 134 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 137 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 138 | # if defined(_AIX) || defined(__osf__) |
| 139 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 140 | # elif defined(__sun) || defined(__sgi) |
| 141 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 142 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 143 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 144 | # else |
| 145 | # undef HAVE_GETHOSTBYNAME_R |
| 146 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 149 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 150 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 151 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 152 | #endif |
| 153 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 154 | /* To use __FreeBSD_version */ |
| 155 | #ifdef HAVE_SYS_PARAM_H |
| 156 | #include <sys/param.h> |
| 157 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 158 | /* 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] | 159 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 160 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 161 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 162 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 163 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 164 | #define USE_GETADDRINFO_LOCK |
| 165 | #endif |
| 166 | |
| 167 | #ifdef USE_GETADDRINFO_LOCK |
| 168 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 169 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 170 | #else |
| 171 | #define ACQUIRE_GETADDRINFO_LOCK |
| 172 | #define RELEASE_GETADDRINFO_LOCK |
| 173 | #endif |
| 174 | |
| 175 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 176 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 177 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 179 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 180 | # include <types.h> |
| 181 | # include <io.h> |
| 182 | # include <sys/ioctl.h> |
| 183 | # include <utils.h> |
| 184 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 185 | #endif |
| 186 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 187 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 188 | # include <ioctl.h> |
| 189 | #endif |
| 190 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 191 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 192 | # define INCL_DOS |
| 193 | # define INCL_DOSERRORS |
| 194 | # define INCL_NOPMAPI |
| 195 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 196 | #endif |
| 197 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 198 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 199 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 200 | functions are declared correctly if compiling with |
| 201 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 202 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 203 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 204 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 205 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 206 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 207 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 208 | #undef _XOPEN_SOURCE |
| 209 | #include <sys/socket.h> |
| 210 | #include <sys/types.h> |
| 211 | #include <netinet/in.h> |
| 212 | #ifdef _SS_ALIGNSIZE |
| 213 | #define HAVE_GETADDRINFO 1 |
| 214 | #define HAVE_GETNAMEINFO 1 |
| 215 | #endif |
| 216 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 217 | #define HAVE_INET_PTON |
| 218 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 221 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 222 | 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] | 223 | are just busted. Same thing for Solaris. */ |
| 224 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 225 | #define INET_ADDRSTRLEN 16 |
| 226 | #endif |
| 227 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 228 | /* Generic includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 229 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 231 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 232 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 233 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 234 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 235 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 236 | |
| 237 | /* Addressing includes */ |
| 238 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 239 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 240 | |
| 241 | /* Non-MS WINDOWS includes */ |
| 242 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 244 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Skip Montanaro | eb33e5a | 2007-08-17 12:57:41 +0000 | [diff] [blame] | 245 | # if defined(PYOS_OS2) && defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 246 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 247 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 248 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 249 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 250 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 251 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 252 | # include <fcntl.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 254 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 255 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 256 | /* MS_WINDOWS includes */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 257 | # ifdef HAVE_FCNTL_H |
| 258 | # include <fcntl.h> |
| 259 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 260 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 261 | #endif |
| 262 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 263 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 264 | |
| 265 | #ifndef offsetof |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 266 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 267 | #endif |
| 268 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 269 | #ifndef O_NONBLOCK |
| 270 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 271 | #endif |
| 272 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 273 | /* include Python's addrinfo.h unless it causes trouble */ |
| 274 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 275 | /* Do not include addinfo.h on some newer IRIX versions. |
| 276 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 277 | * for example, but not by 6.5.10. |
| 278 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 279 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 280 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 281 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 282 | */ |
| 283 | #else |
| 284 | # include "addrinfo.h" |
| 285 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 286 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 287 | #ifndef HAVE_INET_PTON |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 288 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 289 | 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] | 290 | #endif |
| 291 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 292 | #ifdef __APPLE__ |
| 293 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 294 | failure, so we must use the emulation instead of the libinfo |
| 295 | implementation. Unfortunately, performing an autoconf test |
| 296 | for this bug would require DNS access for the machine performing |
| 297 | the configuration, which is not acceptable. Therefore, we |
| 298 | determine the bug just by checking for __APPLE__. If this bug |
| 299 | gets ever fixed, perhaps checking for sys/version.h would be |
| 300 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 301 | #ifndef HAVE_GETNAMEINFO |
| 302 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 303 | Find to check for Jaguar is that it has getnameinfo(), which |
| 304 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 305 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 306 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 307 | |
| 308 | #ifdef HAVE_INET_ATON |
| 309 | #define USE_INET_ATON_WEAKLINK |
| 310 | #endif |
| 311 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 312 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 313 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 314 | /* 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] | 315 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 316 | /* avoid clashes with the C library definition of the symbol. */ |
| 317 | #define getaddrinfo fake_getaddrinfo |
| 318 | #define gai_strerror fake_gai_strerror |
| 319 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 320 | #include "getaddrinfo.c" |
| 321 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 322 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 323 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 324 | #include "getnameinfo.c" |
| 325 | #endif |
| 326 | |
Skip Montanaro | eb33e5a | 2007-08-17 12:57:41 +0000 | [diff] [blame] | 327 | #if defined(MS_WINDOWS) |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 328 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 329 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 330 | #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] | 331 | #endif |
| 332 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 333 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 334 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 335 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 336 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 337 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 338 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 339 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 340 | #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] | 341 | #endif |
| 342 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 343 | #ifndef SOCKETCLOSE |
| 344 | #define SOCKETCLOSE close |
| 345 | #endif |
| 346 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 347 | #if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) |
| 348 | #define USE_BLUETOOTH 1 |
| 349 | #if defined(__FreeBSD__) |
| 350 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 351 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 352 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 353 | #define SOL_HCI SOL_HCI_RAW |
| 354 | #define HCI_FILTER SO_HCI_RAW_FILTER |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 355 | #define sockaddr_l2 sockaddr_l2cap |
| 356 | #define sockaddr_rc sockaddr_rfcomm |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 357 | #define hci_dev hci_node |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 358 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 359 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 360 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 361 | #elif defined(__NetBSD__) |
| 362 | #define sockaddr_l2 sockaddr_bt |
| 363 | #define sockaddr_rc sockaddr_bt |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 364 | #define sockaddr_hci sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 365 | #define sockaddr_sco sockaddr_bt |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 366 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 367 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 368 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 369 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 370 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 371 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 372 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 373 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 374 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 375 | #endif |
| 376 | #endif |
| 377 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 378 | #ifdef __VMS |
| 379 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 380 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 381 | #endif |
| 382 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 383 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
| 384 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 385 | /* |
| 386 | * Constants for getnameinfo() |
| 387 | */ |
| 388 | #if !defined(NI_MAXHOST) |
| 389 | #define NI_MAXHOST 1025 |
| 390 | #endif |
| 391 | #if !defined(NI_MAXSERV) |
| 392 | #define NI_MAXSERV 32 |
| 393 | #endif |
| 394 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 395 | #ifndef INVALID_SOCKET /* MS defines this */ |
| 396 | #define INVALID_SOCKET (-1) |
| 397 | #endif |
| 398 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 399 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 400 | a Py prefix (or use CapitalizedWords). Later... */ |
| 401 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 402 | /* Global variable holding the exception type for errors detected |
| 403 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 404 | static PyObject *socket_error; |
| 405 | static PyObject *socket_herror; |
| 406 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 407 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 408 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 409 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 410 | The sock_type variable contains pointers to various functions, |
| 411 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 412 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 413 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 414 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 415 | #if defined(HAVE_POLL_H) |
| 416 | #include <poll.h> |
| 417 | #elif defined(HAVE_SYS_POLL_H) |
| 418 | #include <sys/poll.h> |
| 419 | #endif |
| 420 | |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 421 | #ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
| 422 | /* Platform can select file descriptors beyond FD_SETSIZE */ |
| 423 | #define IS_SELECTABLE(s) 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 424 | #elif defined(HAVE_POLL) |
| 425 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 426 | #define IS_SELECTABLE(s) 1 |
| 427 | /* 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] | 428 | #else |
| 429 | /* POSIX says selecting file descriptors beyond FD_SETSIZE |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 430 | has undefined behaviour. If there's no timeout left, we don't have to |
| 431 | call select, so it's a safe, little white lie. */ |
| 432 | #define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0) |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 433 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 434 | |
| 435 | static PyObject* |
| 436 | select_error(void) |
| 437 | { |
| 438 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 439 | return NULL; |
| 440 | } |
| 441 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 442 | /* Convenience function to raise an error according to errno |
| 443 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 444 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 445 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 446 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 447 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 448 | #ifdef MS_WINDOWS |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 449 | int err_no = WSAGetLastError(); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 450 | static struct { |
| 451 | int no; |
| 452 | const char *msg; |
| 453 | } *msgp, msgs[] = { |
| 454 | {WSAEINTR, "Interrupted system call"}, |
| 455 | {WSAEBADF, "Bad file descriptor"}, |
| 456 | {WSAEACCES, "Permission denied"}, |
| 457 | {WSAEFAULT, "Bad address"}, |
| 458 | {WSAEINVAL, "Invalid argument"}, |
| 459 | {WSAEMFILE, "Too many open files"}, |
| 460 | {WSAEWOULDBLOCK, |
| 461 | "The socket operation could not complete " |
| 462 | "without blocking"}, |
| 463 | {WSAEINPROGRESS, "Operation now in progress"}, |
| 464 | {WSAEALREADY, "Operation already in progress"}, |
| 465 | {WSAENOTSOCK, "Socket operation on non-socket"}, |
| 466 | {WSAEDESTADDRREQ, "Destination address required"}, |
| 467 | {WSAEMSGSIZE, "Message too long"}, |
| 468 | {WSAEPROTOTYPE, "Protocol wrong type for socket"}, |
| 469 | {WSAENOPROTOOPT, "Protocol not available"}, |
| 470 | {WSAEPROTONOSUPPORT, "Protocol not supported"}, |
| 471 | {WSAESOCKTNOSUPPORT, "Socket type not supported"}, |
| 472 | {WSAEOPNOTSUPP, "Operation not supported"}, |
| 473 | {WSAEPFNOSUPPORT, "Protocol family not supported"}, |
| 474 | {WSAEAFNOSUPPORT, "Address family not supported"}, |
| 475 | {WSAEADDRINUSE, "Address already in use"}, |
| 476 | {WSAEADDRNOTAVAIL, "Can't assign requested address"}, |
| 477 | {WSAENETDOWN, "Network is down"}, |
| 478 | {WSAENETUNREACH, "Network is unreachable"}, |
| 479 | {WSAENETRESET, "Network dropped connection on reset"}, |
| 480 | {WSAECONNABORTED, "Software caused connection abort"}, |
| 481 | {WSAECONNRESET, "Connection reset by peer"}, |
| 482 | {WSAENOBUFS, "No buffer space available"}, |
| 483 | {WSAEISCONN, "Socket is already connected"}, |
| 484 | {WSAENOTCONN, "Socket is not connected"}, |
| 485 | {WSAESHUTDOWN, "Can't send after socket shutdown"}, |
| 486 | {WSAETOOMANYREFS, "Too many references: can't splice"}, |
| 487 | {WSAETIMEDOUT, "Operation timed out"}, |
| 488 | {WSAECONNREFUSED, "Connection refused"}, |
| 489 | {WSAELOOP, "Too many levels of symbolic links"}, |
| 490 | {WSAENAMETOOLONG, "File name too long"}, |
| 491 | {WSAEHOSTDOWN, "Host is down"}, |
| 492 | {WSAEHOSTUNREACH, "No route to host"}, |
| 493 | {WSAENOTEMPTY, "Directory not empty"}, |
| 494 | {WSAEPROCLIM, "Too many processes"}, |
| 495 | {WSAEUSERS, "Too many users"}, |
| 496 | {WSAEDQUOT, "Disc quota exceeded"}, |
| 497 | {WSAESTALE, "Stale NFS file handle"}, |
| 498 | {WSAEREMOTE, "Too many levels of remote in path"}, |
| 499 | {WSASYSNOTREADY, "Network subsystem is unvailable"}, |
| 500 | {WSAVERNOTSUPPORTED, "WinSock version is not supported"}, |
| 501 | {WSANOTINITIALISED, |
| 502 | "Successful WSAStartup() not yet performed"}, |
| 503 | {WSAEDISCON, "Graceful shutdown in progress"}, |
| 504 | /* Resolver errors */ |
| 505 | {WSAHOST_NOT_FOUND, "No such host is known"}, |
| 506 | {WSATRY_AGAIN, "Host not found, or server failed"}, |
| 507 | {WSANO_RECOVERY, "Unexpected server error encountered"}, |
| 508 | {WSANO_DATA, "Valid name without requested data"}, |
| 509 | {WSANO_ADDRESS, "No address, look for MX record"}, |
| 510 | {0, NULL} |
| 511 | }; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 512 | if (err_no) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 513 | PyObject *v; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 514 | const char *msg = "winsock error"; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 515 | |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 516 | for (msgp = msgs; msgp->msg; msgp++) { |
| 517 | if (err_no == msgp->no) { |
| 518 | msg = msgp->msg; |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | v = Py_BuildValue("(is)", err_no, msg); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 524 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 525 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 526 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 527 | } |
| 528 | return NULL; |
| 529 | } |
| 530 | else |
| 531 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 532 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 533 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 534 | if (sock_errno() != NO_ERROR) { |
| 535 | APIRET rc; |
| 536 | ULONG msglen; |
| 537 | char outbuf[100]; |
| 538 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 540 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 541 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 542 | myerrorcode - SOCBASEERR + 26, |
| 543 | "mptn.msg", |
| 544 | &msglen); |
| 545 | if (rc == NO_ERROR) { |
| 546 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 547 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 548 | /* OS/2 doesn't guarantee a terminator */ |
| 549 | outbuf[msglen] = '\0'; |
| 550 | if (strlen(outbuf) > 0) { |
| 551 | /* If non-empty msg, trim CRLF */ |
| 552 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 553 | while (lastc > outbuf && |
| 554 | isspace(Py_CHARMASK(*lastc))) { |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 555 | /* Trim trailing whitespace (CRLF) */ |
| 556 | *lastc-- = '\0'; |
| 557 | } |
| 558 | } |
| 559 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 560 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 561 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 562 | Py_DECREF(v); |
| 563 | } |
| 564 | return NULL; |
| 565 | } |
| 566 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 567 | #endif |
| 568 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 569 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 572 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 573 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 574 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 575 | { |
| 576 | PyObject *v; |
| 577 | |
| 578 | #ifdef HAVE_HSTRERROR |
| 579 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
| 580 | #else |
| 581 | v = Py_BuildValue("(is)", h_error, "host not found"); |
| 582 | #endif |
| 583 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 584 | PyErr_SetObject(socket_herror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 585 | Py_DECREF(v); |
| 586 | } |
| 587 | |
| 588 | return NULL; |
| 589 | } |
| 590 | |
| 591 | |
| 592 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 593 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 594 | { |
| 595 | PyObject *v; |
| 596 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 597 | #ifdef EAI_SYSTEM |
| 598 | /* EAI_SYSTEM is not available on Windows XP. */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 599 | if (error == EAI_SYSTEM) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 600 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 601 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 602 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 603 | #ifdef HAVE_GAI_STRERROR |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 604 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 605 | #else |
| 606 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
| 607 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 608 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 609 | PyErr_SetObject(socket_gaierror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 610 | Py_DECREF(v); |
| 611 | } |
| 612 | |
| 613 | return NULL; |
| 614 | } |
| 615 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 616 | #ifdef __VMS |
| 617 | /* Function to send in segments */ |
| 618 | static int |
| 619 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 620 | { |
| 621 | int n = 0; |
| 622 | int remaining = len; |
| 623 | |
| 624 | while (remaining > 0) { |
| 625 | unsigned int segment; |
| 626 | |
| 627 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 628 | n = send(sock_fd, buf, segment, flags); |
| 629 | if (n < 0) { |
| 630 | return n; |
| 631 | } |
| 632 | remaining -= segment; |
| 633 | buf += segment; |
| 634 | } /* end while */ |
| 635 | |
| 636 | return len; |
| 637 | } |
| 638 | #endif |
| 639 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 640 | /* Function to perform the setting of socket blocking mode |
| 641 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 642 | static int |
| 643 | internal_setblocking(PySocketSockObject *s, int block) |
| 644 | { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 645 | #ifndef MS_WINDOWS |
| 646 | int delay_flag; |
| 647 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 648 | |
| 649 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 650 | #ifndef MS_WINDOWS |
| 651 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 652 | block = !block; |
| 653 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 654 | #elif defined(__VMS) |
| 655 | block = !block; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 656 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
| 657 | #else /* !PYOS_OS2 && !__VMS */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 659 | if (block) |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 660 | delay_flag &= (~O_NONBLOCK); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 661 | else |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 662 | delay_flag |= O_NONBLOCK; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 663 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
| 664 | #endif /* !PYOS_OS2 */ |
| 665 | #else /* MS_WINDOWS */ |
| 666 | block = !block; |
| 667 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
| 668 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 669 | Py_END_ALLOW_THREADS |
| 670 | |
| 671 | /* Since these don't return anything */ |
| 672 | return 1; |
| 673 | } |
| 674 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 675 | /* 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] | 676 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 677 | This does not raise an exception; we'll let our caller do that |
| 678 | after they've reacquired the interpreter lock. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 679 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 680 | static int |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 681 | internal_select(PySocketSockObject *s, int writing) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 682 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 683 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 685 | /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 686 | if (s->sock_timeout <= 0.0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 687 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 688 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 689 | /* Guard against closed socket */ |
| 690 | if (s->sock_fd < 0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 691 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 692 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 693 | /* Prefer poll, if available, since you can poll() any fd |
| 694 | * which can't be done with select(). */ |
| 695 | #ifdef HAVE_POLL |
| 696 | { |
| 697 | struct pollfd pollfd; |
| 698 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 699 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 700 | pollfd.fd = s->sock_fd; |
| 701 | pollfd.events = writing ? POLLOUT : POLLIN; |
| 702 | |
| 703 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 704 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 705 | n = poll(&pollfd, 1, timeout); |
| 706 | } |
| 707 | #else |
| 708 | { |
| 709 | /* Construct the arguments to select */ |
| 710 | fd_set fds; |
| 711 | struct timeval tv; |
| 712 | tv.tv_sec = (int)s->sock_timeout; |
| 713 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 714 | FD_ZERO(&fds); |
| 715 | FD_SET(s->sock_fd, &fds); |
| 716 | |
| 717 | /* See if the socket is ready */ |
| 718 | if (writing) |
| 719 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 720 | else |
| 721 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 722 | } |
| 723 | #endif |
| 724 | |
| 725 | if (n < 0) |
| 726 | return -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 727 | if (n == 0) |
| 728 | return 1; |
| 729 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 732 | /* Initialize a new socket object. */ |
| 733 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 734 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 735 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 736 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 737 | init_sockobject(PySocketSockObject *s, |
| 738 | SOCKET_T fd, int family, int type, int proto) |
| 739 | { |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 740 | s->sock_fd = fd; |
| 741 | s->sock_family = family; |
| 742 | s->sock_type = type; |
| 743 | s->sock_proto = proto; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 744 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 746 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 747 | |
| 748 | if (defaulttimeout >= 0.0) |
| 749 | internal_setblocking(s, 0); |
| 750 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 754 | /* Create a new socket object. |
| 755 | This just creates the object and initializes it. |
| 756 | If the creation fails, return NULL and set an exception (implicit |
| 757 | in NEWOBJ()). */ |
| 758 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 759 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 760 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 761 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 762 | PySocketSockObject *s; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 763 | s = (PySocketSockObject *) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 764 | PyType_GenericNew(&sock_type, NULL, NULL); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 765 | if (s != NULL) |
| 766 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 767 | return s; |
| 768 | } |
| 769 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 770 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 771 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 772 | thread to be in gethostbyname or getaddrinfo */ |
| 773 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 774 | PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 775 | #endif |
| 776 | |
| 777 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 778 | /* Convert a string specifying a host name or one of a few symbolic |
| 779 | names to a numeric IP address. This usually calls gethostbyname() |
| 780 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 781 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 782 | an error occurred; then an exception is raised. */ |
| 783 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 784 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 785 | 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] | 786 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 787 | struct addrinfo hints, *res; |
| 788 | int error; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 789 | int d1, d2, d3, d4; |
| 790 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 791 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 792 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 793 | if (name[0] == '\0') { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 794 | int siz; |
| 795 | memset(&hints, 0, sizeof(hints)); |
| 796 | hints.ai_family = af; |
| 797 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 798 | hints.ai_flags = AI_PASSIVE; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 799 | Py_BEGIN_ALLOW_THREADS |
| 800 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 801 | error = getaddrinfo(NULL, "0", &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 802 | Py_END_ALLOW_THREADS |
| 803 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 804 | *are* safe regarding their return value, ie. that a |
| 805 | subsequent call to getaddrinfo() does not destroy the |
| 806 | outcome of the first call. */ |
| 807 | RELEASE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 808 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 809 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 810 | return -1; |
| 811 | } |
| 812 | switch (res->ai_family) { |
| 813 | case AF_INET: |
| 814 | siz = 4; |
| 815 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 816 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 817 | case AF_INET6: |
| 818 | siz = 16; |
| 819 | break; |
| 820 | #endif |
| 821 | default: |
| 822 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 823 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 824 | "unsupported address family"); |
| 825 | return -1; |
| 826 | } |
| 827 | if (res->ai_next) { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 828 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 829 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 830 | "wildcard resolved to multiple address"); |
| 831 | return -1; |
| 832 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 833 | if (res->ai_addrlen < addr_ret_size) |
| 834 | addr_ret_size = res->ai_addrlen; |
| 835 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 836 | freeaddrinfo(res); |
| 837 | return siz; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 838 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 839 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 840 | struct sockaddr_in *sin; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 841 | if (af != AF_INET && af != AF_UNSPEC) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 842 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 843 | "address family mismatched"); |
| 844 | return -1; |
| 845 | } |
| 846 | sin = (struct sockaddr_in *)addr_ret; |
| 847 | memset((void *) sin, '\0', sizeof(*sin)); |
| 848 | sin->sin_family = AF_INET; |
| 849 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 850 | sin->sin_len = sizeof(*sin); |
| 851 | #endif |
| 852 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 853 | return sizeof(sin->sin_addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 854 | } |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 855 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 856 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 857 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 858 | struct sockaddr_in *sin; |
| 859 | sin = (struct sockaddr_in *)addr_ret; |
| 860 | sin->sin_addr.s_addr = htonl( |
| 861 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 862 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 863 | sin->sin_family = AF_INET; |
| 864 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 865 | sin->sin_len = sizeof(*sin); |
| 866 | #endif |
| 867 | return 4; |
| 868 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 869 | memset(&hints, 0, sizeof(hints)); |
| 870 | hints.ai_family = af; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 871 | Py_BEGIN_ALLOW_THREADS |
| 872 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 873 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 874 | #if defined(__digital__) && defined(__unix__) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 875 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 876 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 877 | if no address family is given. Assume IPv4 for now.*/ |
| 878 | hints.ai_family = AF_INET; |
| 879 | error = getaddrinfo(name, NULL, &hints, &res); |
| 880 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 881 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 882 | Py_END_ALLOW_THREADS |
| 883 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 884 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 885 | set_gaierror(error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 886 | return -1; |
| 887 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 888 | if (res->ai_addrlen < addr_ret_size) |
| 889 | addr_ret_size = res->ai_addrlen; |
| 890 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 891 | freeaddrinfo(res); |
| 892 | switch (addr_ret->sa_family) { |
| 893 | case AF_INET: |
| 894 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 895 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 896 | case AF_INET6: |
| 897 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 898 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 899 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 900 | PyErr_SetString(socket_error, "unknown address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 901 | return -1; |
| 902 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 905 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 906 | /* Create a string object representing an IP address. |
| 907 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 908 | size numbers). */ |
| 909 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 910 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 911 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 912 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 913 | char buf[NI_MAXHOST]; |
| 914 | int error; |
| 915 | |
| 916 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 917 | NI_NUMERICHOST); |
| 918 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 919 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 920 | return NULL; |
| 921 | } |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 922 | return PyUnicode_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 926 | #ifdef USE_BLUETOOTH |
| 927 | /* Convert a string representation of a Bluetooth address into a numeric |
| 928 | address. Returns the length (6), or raises an exception and returns -1 if |
| 929 | an error occurred. */ |
| 930 | |
| 931 | static int |
| 932 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 933 | { |
| 934 | unsigned int b0, b1, b2, b3, b4, b5; |
| 935 | char ch; |
| 936 | int n; |
| 937 | |
| 938 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 939 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 940 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 941 | bdaddr->b[0] = b0; |
| 942 | bdaddr->b[1] = b1; |
| 943 | bdaddr->b[2] = b2; |
| 944 | bdaddr->b[3] = b3; |
| 945 | bdaddr->b[4] = b4; |
| 946 | bdaddr->b[5] = b5; |
| 947 | return 6; |
| 948 | } else { |
| 949 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 950 | return -1; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /* Create a string representation of the Bluetooth address. This is always a |
| 955 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 956 | value (zero padded if necessary). */ |
| 957 | |
| 958 | static PyObject * |
| 959 | makebdaddr(bdaddr_t *bdaddr) |
| 960 | { |
| 961 | char buf[(6 * 2) + 5 + 1]; |
| 962 | |
| 963 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 964 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 965 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 966 | return PyUnicode_FromString(buf); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 967 | } |
| 968 | #endif |
| 969 | |
| 970 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 971 | /* Create an object representing the given socket address, |
| 972 | suitable for passing it back to bind(), connect() etc. |
| 973 | The family field of the sockaddr structure is inspected |
| 974 | to determine what kind of address it really is. */ |
| 975 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 976 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 977 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 978 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 979 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 980 | if (addrlen == 0) { |
| 981 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 982 | Py_INCREF(Py_None); |
| 983 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 986 | switch (addr->sa_family) { |
| 987 | |
| 988 | case AF_INET: |
| 989 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 990 | struct sockaddr_in *a; |
| 991 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 992 | PyObject *ret = NULL; |
| 993 | if (addrobj) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 994 | a = (struct sockaddr_in *)addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 995 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 996 | Py_DECREF(addrobj); |
| 997 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 998 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 999 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1000 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1001 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1002 | case AF_UNIX: |
| 1003 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1004 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1005 | #ifdef linux |
| 1006 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1007 | addrlen -= (sizeof(*a) - sizeof(a->sun_path)); |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 1008 | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1009 | } |
| 1010 | else |
| 1011 | #endif /* linux */ |
| 1012 | { |
| 1013 | /* regular NULL-terminated string */ |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 1014 | return PyUnicode_FromString(a->sun_path); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1015 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1016 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1017 | #endif /* AF_UNIX */ |
| 1018 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1019 | #if defined(AF_NETLINK) |
| 1020 | case AF_NETLINK: |
| 1021 | { |
| 1022 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
Martin v. Löwis | d056005 | 2006-01-15 07:49:20 +0000 | [diff] [blame] | 1023 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1024 | } |
| 1025 | #endif /* AF_NETLINK */ |
| 1026 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1027 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1028 | case AF_INET6: |
| 1029 | { |
| 1030 | struct sockaddr_in6 *a; |
| 1031 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1032 | PyObject *ret = NULL; |
| 1033 | if (addrobj) { |
| 1034 | a = (struct sockaddr_in6 *)addr; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1035 | ret = Py_BuildValue("Oiii", |
| 1036 | addrobj, |
| 1037 | ntohs(a->sin6_port), |
| 1038 | a->sin6_flowinfo, |
| 1039 | a->sin6_scope_id); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1040 | Py_DECREF(addrobj); |
| 1041 | } |
| 1042 | return ret; |
| 1043 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1044 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1045 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1046 | #ifdef USE_BLUETOOTH |
| 1047 | case AF_BLUETOOTH: |
| 1048 | switch (proto) { |
| 1049 | |
| 1050 | case BTPROTO_L2CAP: |
| 1051 | { |
| 1052 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1053 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1054 | PyObject *ret = NULL; |
| 1055 | if (addrobj) { |
| 1056 | ret = Py_BuildValue("Oi", |
| 1057 | addrobj, |
| 1058 | _BT_L2_MEMB(a, psm)); |
| 1059 | Py_DECREF(addrobj); |
| 1060 | } |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | case BTPROTO_RFCOMM: |
| 1065 | { |
| 1066 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1067 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1068 | PyObject *ret = NULL; |
| 1069 | if (addrobj) { |
| 1070 | ret = Py_BuildValue("Oi", |
| 1071 | addrobj, |
| 1072 | _BT_RC_MEMB(a, channel)); |
| 1073 | Py_DECREF(addrobj); |
| 1074 | } |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1078 | case BTPROTO_HCI: |
| 1079 | { |
| 1080 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
| 1081 | PyObject *ret = NULL; |
| 1082 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1083 | return ret; |
| 1084 | } |
| 1085 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1086 | #if !defined(__FreeBSD__) |
| 1087 | case BTPROTO_SCO: |
| 1088 | { |
| 1089 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1090 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1091 | } |
| 1092 | #endif |
| 1093 | |
| 1094 | } |
| 1095 | #endif |
| 1096 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1097 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1098 | case AF_PACKET: |
| 1099 | { |
| 1100 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1101 | char *ifname = ""; |
| 1102 | struct ifreq ifr; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1103 | /* need to look up interface name give index */ |
| 1104 | if (a->sll_ifindex) { |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1105 | ifr.ifr_ifindex = a->sll_ifindex; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1106 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1107 | ifname = ifr.ifr_name; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1108 | } |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 1109 | return Py_BuildValue("shbhy#", |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1110 | ifname, |
| 1111 | ntohs(a->sll_protocol), |
| 1112 | a->sll_pkttype, |
| 1113 | a->sll_hatype, |
| 1114 | a->sll_addr, |
| 1115 | a->sll_halen); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1116 | } |
| 1117 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1118 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1119 | /* More cases here... */ |
| 1120 | |
| 1121 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 1122 | /* If we don't know the address family, don't raise an |
Guido van Rossum | 9ce8e38 | 2007-08-09 21:06:37 +0000 | [diff] [blame] | 1123 | exception -- return it as an (int, bytes) tuple. */ |
| 1124 | return Py_BuildValue("iy#", |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 1125 | addr->sa_family, |
| 1126 | addr->sa_data, |
| 1127 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1128 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1129 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1132 | |
| 1133 | /* Parse a socket address argument according to the socket object's |
| 1134 | address family. Return 1 if the address was in the proper format, |
| 1135 | 0 of not. The address is returned through addr_ret, its length |
| 1136 | through len_ret. */ |
| 1137 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1138 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1139 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1140 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1141 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1142 | switch (s->sock_family) { |
| 1143 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1144 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1145 | case AF_UNIX: |
| 1146 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1147 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1148 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1149 | int len; |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 1150 | if (!PyArg_Parse(args, "s#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1151 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1152 | |
| 1153 | addr = (struct sockaddr_un*)addr_ret; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1154 | #ifdef linux |
| 1155 | if (len > 0 && path[0] == 0) { |
| 1156 | /* Linux abstract namespace extension */ |
| 1157 | if (len > sizeof addr->sun_path) { |
| 1158 | PyErr_SetString(socket_error, |
| 1159 | "AF_UNIX path too long"); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | } |
| 1163 | else |
| 1164 | #endif /* linux */ |
| 1165 | { |
| 1166 | /* regular NULL-terminated string */ |
| 1167 | if (len >= sizeof addr->sun_path) { |
| 1168 | PyErr_SetString(socket_error, |
| 1169 | "AF_UNIX path too long"); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | addr->sun_path[len] = 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1173 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1174 | addr->sun_family = s->sock_family; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1175 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1176 | #if defined(PYOS_OS2) |
| 1177 | *len_ret = sizeof(*addr); |
| 1178 | #else |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 1179 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1180 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1181 | return 1; |
| 1182 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1183 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1184 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1185 | #if defined(AF_NETLINK) |
| 1186 | case AF_NETLINK: |
| 1187 | { |
| 1188 | struct sockaddr_nl* addr; |
| 1189 | int pid, groups; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1190 | addr = (struct sockaddr_nl *)addr_ret; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1191 | if (!PyTuple_Check(args)) { |
| 1192 | PyErr_Format( |
| 1193 | PyExc_TypeError, |
| 1194 | "getsockaddrarg: " |
| 1195 | "AF_NETLINK address must be tuple, not %.500s", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1196 | Py_Type(args)->tp_name); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1200 | return 0; |
| 1201 | addr->nl_family = AF_NETLINK; |
| 1202 | addr->nl_pid = pid; |
| 1203 | addr->nl_groups = groups; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1204 | *len_ret = sizeof(*addr); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | #endif |
| 1208 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1209 | case AF_INET: |
| 1210 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1211 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1212 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1213 | int port, result; |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1214 | if (!PyTuple_Check(args)) { |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1215 | PyErr_Format( |
| 1216 | PyExc_TypeError, |
| 1217 | "getsockaddrarg: " |
| 1218 | "AF_INET address must be tuple, not %.500s", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1219 | Py_Type(args)->tp_name); |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1220 | return 0; |
| 1221 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1222 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1223 | "idna", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1224 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1225 | addr=(struct sockaddr_in*)addr_ret; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1226 | result = setipaddr(host, (struct sockaddr *)addr, |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1227 | sizeof(*addr), AF_INET); |
| 1228 | PyMem_Free(host); |
| 1229 | if (result < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1230 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1231 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 1232 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1233 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1234 | return 1; |
| 1235 | } |
| 1236 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1237 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1238 | case AF_INET6: |
| 1239 | { |
| 1240 | struct sockaddr_in6* addr; |
| 1241 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1242 | int port, flowinfo, scope_id, result; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1243 | flowinfo = scope_id = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1244 | if (!PyTuple_Check(args)) { |
| 1245 | PyErr_Format( |
| 1246 | PyExc_TypeError, |
| 1247 | "getsockaddrarg: " |
| 1248 | "AF_INET6 address must be tuple, not %.500s", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1249 | Py_Type(args)->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | if (!PyArg_ParseTuple(args, "eti|ii", |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1253 | "idna", &host, &port, &flowinfo, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1254 | &scope_id)) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1255 | return 0; |
| 1256 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1257 | addr = (struct sockaddr_in6*)addr_ret; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1258 | result = setipaddr(host, (struct sockaddr *)addr, |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1259 | sizeof(*addr), AF_INET6); |
| 1260 | PyMem_Free(host); |
| 1261 | if (result < 0) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1262 | return 0; |
| 1263 | addr->sin6_family = s->sock_family; |
| 1264 | addr->sin6_port = htons((short)port); |
| 1265 | addr->sin6_flowinfo = flowinfo; |
| 1266 | addr->sin6_scope_id = scope_id; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1267 | *len_ret = sizeof *addr; |
| 1268 | return 1; |
| 1269 | } |
| 1270 | #endif |
| 1271 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1272 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1273 | case AF_BLUETOOTH: |
| 1274 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1275 | switch (s->sock_proto) { |
| 1276 | case BTPROTO_L2CAP: |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1277 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1278 | struct sockaddr_l2 *addr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1279 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1280 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1281 | addr = (struct sockaddr_l2 *)addr_ret; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1282 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1283 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1284 | &_BT_L2_MEMB(addr, psm))) { |
| 1285 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1286 | "wrong format"); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1287 | return 0; |
| 1288 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1289 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1290 | return 0; |
| 1291 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1292 | *len_ret = sizeof *addr; |
| 1293 | return 1; |
| 1294 | } |
| 1295 | case BTPROTO_RFCOMM: |
| 1296 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1297 | struct sockaddr_rc *addr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1298 | char *straddr; |
| 1299 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1300 | addr = (struct sockaddr_rc *)addr_ret; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1301 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1302 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1303 | &_BT_RC_MEMB(addr, channel))) { |
| 1304 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1305 | "wrong format"); |
| 1306 | return 0; |
| 1307 | } |
| 1308 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1309 | return 0; |
| 1310 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1311 | *len_ret = sizeof *addr; |
| 1312 | return 1; |
| 1313 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1314 | case BTPROTO_HCI: |
| 1315 | { |
| 1316 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
| 1317 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1318 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1319 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1320 | "wrong format"); |
| 1321 | return 0; |
| 1322 | } |
| 1323 | *len_ret = sizeof *addr; |
| 1324 | return 1; |
| 1325 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1326 | #if !defined(__FreeBSD__) |
| 1327 | case BTPROTO_SCO: |
| 1328 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1329 | struct sockaddr_sco *addr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1330 | char *straddr; |
| 1331 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1332 | addr = (struct sockaddr_sco *)addr_ret; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1333 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 1334 | if (!PyBytes_Check(args)) { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1335 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1336 | "wrong format"); |
| 1337 | return 0; |
| 1338 | } |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 1339 | straddr = PyBytes_AS_STRING(args); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1340 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1341 | return 0; |
| 1342 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1343 | *len_ret = sizeof *addr; |
| 1344 | return 1; |
| 1345 | } |
| 1346 | #endif |
| 1347 | default: |
| 1348 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1349 | return 0; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | #endif |
| 1353 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1354 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1355 | case AF_PACKET: |
| 1356 | { |
| 1357 | struct sockaddr_ll* addr; |
| 1358 | struct ifreq ifr; |
| 1359 | char *interfaceName; |
| 1360 | int protoNumber; |
| 1361 | int hatype = 0; |
| 1362 | int pkttype = 0; |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1363 | char *haddr = NULL; |
| 1364 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1365 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1366 | if (!PyTuple_Check(args)) { |
| 1367 | PyErr_Format( |
| 1368 | PyExc_TypeError, |
| 1369 | "getsockaddrarg: " |
| 1370 | "AF_PACKET address must be tuple, not %.500s", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1371 | Py_Type(args)->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1374 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1375 | &protoNumber, &pkttype, &hatype, |
| 1376 | &haddr, &halen)) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1377 | return 0; |
| 1378 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1379 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1380 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1381 | s->errorhandler(); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1382 | return 0; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1383 | } |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1384 | if (halen > 8) { |
| 1385 | PyErr_SetString(PyExc_ValueError, |
| 1386 | "Hardware address must be 8 bytes or less"); |
| 1387 | return 0; |
| 1388 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1389 | addr = (struct sockaddr_ll*)addr_ret; |
| 1390 | addr->sll_family = AF_PACKET; |
| 1391 | addr->sll_protocol = htons((short)protoNumber); |
| 1392 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1393 | addr->sll_pkttype = pkttype; |
| 1394 | addr->sll_hatype = hatype; |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1395 | if (halen != 0) { |
| 1396 | memcpy(&addr->sll_addr, haddr, halen); |
| 1397 | } |
| 1398 | addr->sll_halen = halen; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1399 | *len_ret = sizeof *addr; |
| 1400 | return 1; |
| 1401 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1402 | #endif |
| 1403 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1404 | /* More cases here... */ |
| 1405 | |
| 1406 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1407 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1408 | return 0; |
| 1409 | |
| 1410 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1413 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1414 | /* 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] | 1415 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1416 | through len_ret. */ |
| 1417 | |
| 1418 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1419 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1420 | { |
| 1421 | switch (s->sock_family) { |
| 1422 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1423 | #if defined(AF_UNIX) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1424 | case AF_UNIX: |
| 1425 | { |
| 1426 | *len_ret = sizeof (struct sockaddr_un); |
| 1427 | return 1; |
| 1428 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1429 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1430 | #if defined(AF_NETLINK) |
| 1431 | case AF_NETLINK: |
| 1432 | { |
| 1433 | *len_ret = sizeof (struct sockaddr_nl); |
| 1434 | return 1; |
| 1435 | } |
| 1436 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1437 | |
| 1438 | case AF_INET: |
| 1439 | { |
| 1440 | *len_ret = sizeof (struct sockaddr_in); |
| 1441 | return 1; |
| 1442 | } |
| 1443 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1444 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1445 | case AF_INET6: |
| 1446 | { |
| 1447 | *len_ret = sizeof (struct sockaddr_in6); |
| 1448 | return 1; |
| 1449 | } |
| 1450 | #endif |
| 1451 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1452 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1453 | case AF_BLUETOOTH: |
| 1454 | { |
| 1455 | switch(s->sock_proto) |
| 1456 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1457 | |
| 1458 | case BTPROTO_L2CAP: |
| 1459 | *len_ret = sizeof (struct sockaddr_l2); |
| 1460 | return 1; |
| 1461 | case BTPROTO_RFCOMM: |
| 1462 | *len_ret = sizeof (struct sockaddr_rc); |
| 1463 | return 1; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1464 | case BTPROTO_HCI: |
| 1465 | *len_ret = sizeof (struct sockaddr_hci); |
| 1466 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1467 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1468 | case BTPROTO_SCO: |
| 1469 | *len_ret = sizeof (struct sockaddr_sco); |
| 1470 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1471 | #endif |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1472 | default: |
| 1473 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1474 | "unknown BT protocol"); |
| 1475 | return 0; |
| 1476 | |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | #endif |
| 1480 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1481 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1482 | case AF_PACKET: |
| 1483 | { |
| 1484 | *len_ret = sizeof (struct sockaddr_ll); |
| 1485 | return 1; |
| 1486 | } |
| 1487 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1488 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1489 | /* More cases here... */ |
| 1490 | |
| 1491 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1492 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1493 | return 0; |
| 1494 | |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1499 | /* s.accept() method */ |
| 1500 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1501 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1502 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1503 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 1504 | sock_addr_t addrbuf; |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1505 | SOCKET_T newfd = INVALID_SOCKET; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1506 | socklen_t addrlen; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1507 | PyObject *sock = NULL; |
| 1508 | PyObject *addr = NULL; |
| 1509 | PyObject *res = NULL; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1510 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1511 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1512 | if (!getsockaddrlen(s, &addrlen)) |
| 1513 | return NULL; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 1514 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1515 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1516 | if (!IS_SELECTABLE(s)) |
| 1517 | return select_error(); |
| 1518 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1519 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1520 | timeout = internal_select(s, 0); |
| 1521 | if (!timeout) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1522 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1523 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1524 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1525 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1526 | PyErr_SetString(socket_timeout, "timed out"); |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1530 | if (newfd == INVALID_SOCKET) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1531 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1532 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1533 | /* Create the new object with unspecified family, |
| 1534 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1535 | sock = (PyObject *) new_sockobject(newfd, |
| 1536 | s->sock_family, |
| 1537 | s->sock_type, |
| 1538 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1539 | |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1540 | if (sock == NULL) { |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1541 | SOCKETCLOSE(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1542 | goto finally; |
| 1543 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1544 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1545 | addrlen, s->sock_proto); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1546 | if (addr == NULL) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1547 | goto finally; |
| 1548 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1549 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1550 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1551 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1552 | Py_XDECREF(sock); |
| 1553 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1554 | return res; |
| 1555 | } |
| 1556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1557 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1558 | "accept() -> (socket object, address info)\n\ |
| 1559 | \n\ |
| 1560 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1561 | 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] | 1562 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1563 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1564 | /* s.setblocking(flag) method. Argument: |
| 1565 | False -- non-blocking mode; same as settimeout(0) |
| 1566 | True -- blocking mode; same as settimeout(None) |
| 1567 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1569 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1570 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1571 | { |
| 1572 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1573 | |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1574 | block = PyInt_AsLong(arg); |
| 1575 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1576 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1577 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1578 | s->sock_timeout = block ? -1.0 : 0.0; |
Guido van Rossum | e8008f0 | 2002-06-07 03:36:20 +0000 | [diff] [blame] | 1579 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1580 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1581 | Py_INCREF(Py_None); |
| 1582 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1583 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1585 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1586 | "setblocking(flag)\n\ |
| 1587 | \n\ |
| 1588 | 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] | 1589 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1590 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1591 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1592 | /* s.settimeout(timeout) method. Argument: |
| 1593 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1594 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1595 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1596 | < 0 -- illegal; raises an exception |
| 1597 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1598 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1599 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1600 | { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1601 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1602 | |
| 1603 | if (arg == Py_None) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1604 | timeout = -1.0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1605 | else { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1606 | timeout = PyFloat_AsDouble(arg); |
| 1607 | if (timeout < 0.0) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1608 | if (!PyErr_Occurred()) |
| 1609 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1610 | "Timeout value out of range"); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1611 | return NULL; |
| 1612 | } |
| 1613 | } |
| 1614 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1615 | s->sock_timeout = timeout; |
| 1616 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1617 | |
| 1618 | Py_INCREF(Py_None); |
| 1619 | return Py_None; |
| 1620 | } |
| 1621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1622 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1623 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1624 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1625 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1626 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1627 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1628 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1629 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1630 | /* s.gettimeout() method. |
| 1631 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1632 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1633 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1634 | { |
| 1635 | if (s->sock_timeout < 0.0) { |
| 1636 | Py_INCREF(Py_None); |
| 1637 | return Py_None; |
| 1638 | } |
| 1639 | else |
| 1640 | return PyFloat_FromDouble(s->sock_timeout); |
| 1641 | } |
| 1642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1643 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1644 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1645 | \n\ |
| 1646 | Returns the timeout in floating seconds associated with socket \n\ |
| 1647 | 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] | 1648 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1649 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1650 | /* s.setsockopt() method. |
| 1651 | With an integer third argument, sets an integer option. |
| 1652 | With a string third argument, sets an option from a buffer; |
| 1653 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1654 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1655 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1656 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1657 | { |
| 1658 | int level; |
| 1659 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1660 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1661 | char *buf; |
| 1662 | int buflen; |
| 1663 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1664 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1665 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1666 | &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1667 | buf = (char *) &flag; |
| 1668 | buflen = sizeof flag; |
| 1669 | } |
| 1670 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1671 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1672 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1673 | &level, &optname, &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1674 | return NULL; |
| 1675 | } |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1676 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1677 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1678 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1679 | Py_INCREF(Py_None); |
| 1680 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1683 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1684 | "setsockopt(level, option, value)\n\ |
| 1685 | \n\ |
| 1686 | 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] | 1687 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1688 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1689 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1690 | /* s.getsockopt() method. |
| 1691 | With two arguments, retrieves an integer option. |
| 1692 | With a third integer argument, retrieves a string buffer of that size; |
| 1693 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1694 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1695 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1696 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1697 | { |
| 1698 | int level; |
| 1699 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1700 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1701 | PyObject *buf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1702 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1703 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1704 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1705 | &level, &optname, &buflen)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1706 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1707 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1708 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1709 | int flag = 0; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1710 | socklen_t flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1711 | res = getsockopt(s->sock_fd, level, optname, |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1712 | (void *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1713 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1714 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1715 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1716 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1717 | #ifdef __VMS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1718 | /* socklen_t is unsigned so no negative test is needed, |
| 1719 | test buflen == 0 is previously done */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1720 | if (buflen > 1024) { |
| 1721 | #else |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1722 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1723 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1724 | PyErr_SetString(socket_error, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1725 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1726 | return NULL; |
| 1727 | } |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1728 | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1729 | if (buf == NULL) |
| 1730 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1731 | res = getsockopt(s->sock_fd, level, optname, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1732 | (void *)PyBytes_AS_STRING(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1733 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1734 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1735 | return s->errorhandler(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1736 | } |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1737 | if (PyBytes_Resize(buf, buflen) < 0) { |
| 1738 | Py_DECREF(buf); |
| 1739 | return NULL; |
| 1740 | } |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1741 | return buf; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1744 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1745 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1746 | \n\ |
| 1747 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1748 | 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] | 1749 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1750 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1751 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1752 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1753 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1754 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1755 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1756 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1757 | sock_addr_t addrbuf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1758 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1759 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1760 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1761 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1762 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1763 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1764 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1765 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1766 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1767 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1768 | Py_INCREF(Py_None); |
| 1769 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1772 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1773 | "bind(address)\n\ |
| 1774 | \n\ |
| 1775 | 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] | 1776 | 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] | 1777 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1778 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1779 | |
| 1780 | /* s.close() method. |
| 1781 | Set the file descriptor to -1 so operations tried subsequently |
| 1782 | will surely fail. */ |
| 1783 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1784 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1785 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1786 | { |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1787 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1788 | |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1789 | if ((fd = s->sock_fd) != -1) { |
| 1790 | s->sock_fd = -1; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1791 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1792 | (void) SOCKETCLOSE(fd); |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1793 | Py_END_ALLOW_THREADS |
| 1794 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1795 | Py_INCREF(Py_None); |
| 1796 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1799 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1800 | "close()\n\ |
| 1801 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1802 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1803 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1804 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1805 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
| 1806 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1807 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1808 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1809 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1810 | timeout = 0; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1811 | res = connect(s->sock_fd, addr, addrlen); |
| 1812 | |
| 1813 | #ifdef MS_WINDOWS |
| 1814 | |
| 1815 | if (s->sock_timeout > 0.0) { |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1816 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 1817 | IS_SELECTABLE(s)) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1818 | /* This is a mess. Best solution: trust select */ |
| 1819 | fd_set fds; |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1820 | fd_set fds_exc; |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1821 | struct timeval tv; |
| 1822 | tv.tv_sec = (int)s->sock_timeout; |
| 1823 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1824 | FD_ZERO(&fds); |
| 1825 | FD_SET(s->sock_fd, &fds); |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1826 | FD_ZERO(&fds_exc); |
| 1827 | FD_SET(s->sock_fd, &fds_exc); |
| 1828 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1829 | if (res == 0) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1830 | res = WSAEWOULDBLOCK; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1831 | timeout = 1; |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1832 | } else if (res > 0) { |
| 1833 | if (FD_ISSET(s->sock_fd, &fds)) |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 1834 | /* The socket is in the writable set - this |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1835 | means connected */ |
| 1836 | res = 0; |
| 1837 | else { |
| 1838 | /* As per MS docs, we need to call getsockopt() |
| 1839 | to get the underlying error */ |
| 1840 | int res_size = sizeof res; |
| 1841 | /* It must be in the exception set */ |
| 1842 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1843 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1844 | (char *)&res, &res_size)) |
| 1845 | /* getsockopt also clears WSAGetLastError, |
| 1846 | so reset it back. */ |
| 1847 | WSASetLastError(res); |
| 1848 | else |
| 1849 | res = WSAGetLastError(); |
| 1850 | } |
| 1851 | } |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1852 | /* else if (res < 0) an error occurred */ |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | if (res < 0) |
| 1857 | res = WSAGetLastError(); |
| 1858 | |
| 1859 | #else |
| 1860 | |
| 1861 | if (s->sock_timeout > 0.0) { |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1862 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1863 | timeout = internal_select(s, 1); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1864 | if (timeout == 0) { |
| 1865 | res = connect(s->sock_fd, addr, addrlen); |
| 1866 | if (res < 0 && errno == EISCONN) |
| 1867 | res = 0; |
| 1868 | } |
| 1869 | else if (timeout == -1) |
| 1870 | res = errno; /* had error */ |
| 1871 | else |
| 1872 | res = EWOULDBLOCK; /* timed out */ |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | if (res < 0) |
| 1877 | res = errno; |
| 1878 | |
| 1879 | #endif |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1880 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1881 | |
| 1882 | return res; |
| 1883 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1884 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1885 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1886 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1887 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1888 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1889 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1890 | sock_addr_t addrbuf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1891 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1892 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1893 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1894 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1895 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1896 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1897 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1898 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1899 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1900 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1901 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1902 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1903 | PyErr_SetString(socket_timeout, "timed out"); |
| 1904 | return NULL; |
| 1905 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1906 | if (res != 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1907 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1908 | Py_INCREF(Py_None); |
| 1909 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1912 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1913 | "connect(address)\n\ |
| 1914 | \n\ |
| 1915 | 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] | 1916 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1917 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1918 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1919 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1920 | |
| 1921 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1922 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1923 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1924 | sock_addr_t addrbuf; |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1925 | int addrlen; |
| 1926 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1927 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1928 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1929 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1930 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1931 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1932 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1933 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1934 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1935 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1936 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 1937 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
| 1938 | #ifdef EINTR |
| 1939 | if (res == EINTR && PyErr_CheckSignals()) |
| 1940 | return NULL; |
| 1941 | #endif |
| 1942 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1943 | return PyInt_FromLong((long) res); |
| 1944 | } |
| 1945 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1946 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1947 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1948 | \n\ |
| 1949 | 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] | 1950 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1951 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1952 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1953 | /* s.fileno() method */ |
| 1954 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1955 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1956 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1957 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1958 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1959 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1960 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1961 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1962 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1965 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1966 | "fileno() -> integer\n\ |
| 1967 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1968 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1969 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1970 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1971 | #ifndef NO_DUP |
| 1972 | /* s.dup() method */ |
| 1973 | |
| 1974 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1975 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1976 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1977 | SOCKET_T newfd; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1978 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1979 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1980 | newfd = dup(s->sock_fd); |
| 1981 | if (newfd < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1982 | return s->errorhandler(); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1983 | sock = (PyObject *) new_sockobject(newfd, |
| 1984 | s->sock_family, |
| 1985 | s->sock_type, |
| 1986 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1987 | if (sock == NULL) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1988 | SOCKETCLOSE(newfd); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1989 | return sock; |
| 1990 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1991 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1992 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1993 | "dup() -> socket object\n\ |
| 1994 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1995 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1996 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1997 | #endif |
| 1998 | |
| 1999 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2000 | /* s.getsockname() method */ |
| 2001 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2002 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2003 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2004 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2005 | sock_addr_t addrbuf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2006 | int res; |
| 2007 | socklen_t addrlen; |
| 2008 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2009 | if (!getsockaddrlen(s, &addrlen)) |
| 2010 | return NULL; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2011 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2012 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2013 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2014 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2015 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2016 | return s->errorhandler(); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2017 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 2018 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2021 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2022 | "getsockname() -> address info\n\ |
| 2023 | \n\ |
| 2024 | 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] | 2025 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2026 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2027 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2028 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2029 | /* s.getpeername() method */ |
| 2030 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2031 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2032 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2033 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2034 | sock_addr_t addrbuf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2035 | int res; |
| 2036 | socklen_t addrlen; |
| 2037 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2038 | if (!getsockaddrlen(s, &addrlen)) |
| 2039 | return NULL; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2040 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2041 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2042 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2043 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2044 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2045 | return s->errorhandler(); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2046 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 2047 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2048 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2049 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2050 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2051 | "getpeername() -> address info\n\ |
| 2052 | \n\ |
| 2053 | 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] | 2054 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2055 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2056 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2057 | |
| 2058 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2059 | /* s.listen(n) method */ |
| 2060 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2061 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2062 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2063 | { |
| 2064 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2065 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2066 | |
| 2067 | backlog = PyInt_AsLong(arg); |
| 2068 | if (backlog == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2069 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2070 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2071 | if (backlog < 1) |
| 2072 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2073 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2074 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2075 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2076 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2077 | Py_INCREF(Py_None); |
| 2078 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2081 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2082 | "listen(backlog)\n\ |
| 2083 | \n\ |
| 2084 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 2085 | least 1; it specifies the number of unaccepted connection that the system\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2086 | will allow before refusing new connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2087 | |
| 2088 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2089 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2090 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 2091 | * char buffer. If you have any inc/dec ref to do to the objects that contain |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2092 | * the buffer, do it in the caller. This function returns the number of bytes |
| 2093 | * succesfully read. If there was an error, it returns -1. Note that it is |
| 2094 | * also possible that we return a number of bytes smaller than the request |
| 2095 | * bytes. |
| 2096 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2097 | static ssize_t |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2098 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2099 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2100 | ssize_t outlen = -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2101 | int timeout; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2102 | #ifdef __VMS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2103 | int remaining; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2104 | char *read_buf; |
| 2105 | #endif |
| 2106 | |
| 2107 | if (!IS_SELECTABLE(s)) { |
| 2108 | select_error(); |
| 2109 | return -1; |
| 2110 | } |
Guido van Rossum | c2de7c0 | 2007-08-03 22:27:51 +0000 | [diff] [blame] | 2111 | if (len == 0) { |
| 2112 | /* If 0 bytes were requested, do nothing. */ |
| 2113 | return 0; |
| 2114 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2115 | |
| 2116 | #ifndef __VMS |
| 2117 | Py_BEGIN_ALLOW_THREADS |
| 2118 | timeout = internal_select(s, 0); |
| 2119 | if (!timeout) |
| 2120 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2121 | Py_END_ALLOW_THREADS |
| 2122 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2123 | if (timeout == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2124 | PyErr_SetString(socket_timeout, "timed out"); |
| 2125 | return -1; |
| 2126 | } |
| 2127 | if (outlen < 0) { |
| 2128 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2129 | NULL, so ignore its return value */ |
| 2130 | s->errorhandler(); |
| 2131 | return -1; |
| 2132 | } |
| 2133 | #else |
| 2134 | read_buf = cbuf; |
| 2135 | remaining = len; |
| 2136 | while (remaining != 0) { |
| 2137 | unsigned int segment; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2138 | int nread = -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2139 | |
| 2140 | segment = remaining /SEGMENT_SIZE; |
| 2141 | if (segment != 0) { |
| 2142 | segment = SEGMENT_SIZE; |
| 2143 | } |
| 2144 | else { |
| 2145 | segment = remaining; |
| 2146 | } |
| 2147 | |
| 2148 | Py_BEGIN_ALLOW_THREADS |
| 2149 | timeout = internal_select(s, 0); |
| 2150 | if (!timeout) |
| 2151 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2152 | Py_END_ALLOW_THREADS |
| 2153 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2154 | if (timeout == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2155 | PyErr_SetString(socket_timeout, "timed out"); |
| 2156 | return -1; |
| 2157 | } |
| 2158 | if (nread < 0) { |
| 2159 | s->errorhandler(); |
| 2160 | return -1; |
| 2161 | } |
| 2162 | if (nread != remaining) { |
| 2163 | read_buf += nread; |
| 2164 | break; |
| 2165 | } |
| 2166 | |
| 2167 | remaining -= segment; |
| 2168 | read_buf += segment; |
| 2169 | } |
| 2170 | outlen = read_buf - cbuf; |
| 2171 | #endif /* !__VMS */ |
| 2172 | |
| 2173 | return outlen; |
| 2174 | } |
| 2175 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2176 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2177 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2178 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2179 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2180 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2181 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2182 | int recvlen, flags = 0; |
| 2183 | ssize_t outlen; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2184 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2185 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2186 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2187 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2188 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2189 | if (recvlen < 0) { |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 2190 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2191 | "negative buffersize in recv"); |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 2192 | return NULL; |
| 2193 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2194 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2195 | /* Allocate a new string. */ |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2196 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2197 | if (buf == NULL) |
| 2198 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2199 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2200 | /* Call the guts */ |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2201 | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2202 | if (outlen < 0) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2203 | /* An error occurred, release the string and return an |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2204 | error. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2205 | Py_DECREF(buf); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2206 | return NULL; |
| 2207 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2208 | if (outlen != recvlen) { |
| 2209 | /* We did not read as many bytes as we anticipated, resize the |
Kurt B. Kaiser | e405037 | 2007-08-22 21:38:31 +0000 | [diff] [blame] | 2210 | string if possible and be successful. */ |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2211 | if (PyBytes_Resize(buf, outlen) < 0) |
Kurt B. Kaiser | e405037 | 2007-08-22 21:38:31 +0000 | [diff] [blame] | 2212 | /* Oopsy, not so successful after all. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2213 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2214 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2215 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2216 | return buf; |
| 2217 | } |
| 2218 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2219 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2220 | "recv(buffersize[, flags]) -> data\n\ |
| 2221 | \n\ |
| 2222 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2223 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2224 | 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] | 2225 | 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] | 2226 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2227 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2228 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2229 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2230 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2231 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2232 | { |
| 2233 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
| 2234 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2235 | int recvlen = 0, flags = 0; |
| 2236 | ssize_t readlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2237 | char *buf; |
| 2238 | int buflen; |
| 2239 | |
| 2240 | /* Get the buffer's memory */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2241 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv_into", kwlist, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2242 | &buf, &buflen, &recvlen, &flags)) |
| 2243 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2244 | |
| 2245 | if (recvlen < 0) { |
| 2246 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2247 | "negative buffersize in recv_into"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2248 | return NULL; |
| 2249 | } |
| 2250 | if (recvlen == 0) { |
| 2251 | /* If nbytes was not specified, use the buffer's length */ |
| 2252 | recvlen = buflen; |
| 2253 | } |
| 2254 | |
| 2255 | /* Check if the buffer is large enough */ |
| 2256 | if (buflen < recvlen) { |
| 2257 | PyErr_SetString(PyExc_ValueError, |
| 2258 | "buffer too small for requested bytes"); |
| 2259 | return NULL; |
| 2260 | } |
| 2261 | |
| 2262 | /* Call the guts */ |
| 2263 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2264 | if (readlen < 0) { |
| 2265 | /* Return an error. */ |
| 2266 | return NULL; |
| 2267 | } |
| 2268 | |
| 2269 | /* Return the number of bytes read. Note that we do not do anything |
| 2270 | special here in the case that readlen < recvlen. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2271 | return PyInt_FromSsize_t(readlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2274 | PyDoc_STRVAR(recv_into_doc, |
| 2275 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2276 | \n\ |
| 2277 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2278 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2279 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2280 | \n\ |
| 2281 | See recv() for documentation about the flags."); |
| 2282 | |
| 2283 | |
| 2284 | /* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2285 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2286 | * char buffer. If you have any inc/def ref to do to the objects that contain |
| 2287 | * the buffer, do it in the caller. This function returns the number of bytes |
| 2288 | * succesfully read. If there was an error, it returns -1. Note that it is |
| 2289 | * also possible that we return a number of bytes smaller than the request |
| 2290 | * bytes. |
| 2291 | * |
| 2292 | * 'addr' is a return value for the address object. Note that you must decref |
| 2293 | * it yourself. |
| 2294 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2295 | static ssize_t |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2296 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
| 2297 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2298 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2299 | sock_addr_t addrbuf; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2300 | int timeout; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2301 | ssize_t n = -1; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2302 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2303 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2304 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2305 | |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 2306 | if (!getsockaddrlen(s, &addrlen)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2307 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2308 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2309 | if (!IS_SELECTABLE(s)) { |
| 2310 | select_error(); |
| 2311 | return -1; |
| 2312 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2313 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2314 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2315 | memset(&addrbuf, 0, addrlen); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2316 | timeout = internal_select(s, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2317 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2318 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2319 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2320 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2321 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2322 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2323 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2324 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2325 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2326 | #else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2327 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2328 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2329 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2330 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2331 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2332 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2333 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2334 | PyErr_SetString(socket_timeout, "timed out"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2335 | return -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2336 | } |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2337 | if (n < 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2338 | s->errorhandler(); |
| 2339 | return -1; |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2340 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2341 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2342 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2343 | addrlen, s->sock_proto))) |
| 2344 | return -1; |
| 2345 | |
| 2346 | return n; |
| 2347 | } |
| 2348 | |
| 2349 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2350 | |
| 2351 | static PyObject * |
| 2352 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2353 | { |
| 2354 | PyObject *buf = NULL; |
| 2355 | PyObject *addr = NULL; |
| 2356 | PyObject *ret = NULL; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2357 | int recvlen, flags = 0; |
| 2358 | ssize_t outlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2359 | |
| 2360 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2361 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2362 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2363 | if (recvlen < 0) { |
| 2364 | PyErr_SetString(PyExc_ValueError, |
| 2365 | "negative buffersize in recvfrom"); |
| 2366 | return NULL; |
| 2367 | } |
| 2368 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2369 | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2370 | if (buf == NULL) |
| 2371 | return NULL; |
| 2372 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2373 | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2374 | recvlen, flags, &addr); |
| 2375 | if (outlen < 0) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2376 | goto finally; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | if (outlen != recvlen) { |
| 2380 | /* We did not read as many bytes as we anticipated, resize the |
| 2381 | string if possible and be succesful. */ |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2382 | if (PyBytes_Resize(buf, outlen) < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2383 | /* Oopsy, not so succesful after all. */ |
| 2384 | goto finally; |
| 2385 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2386 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2387 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2388 | |
| 2389 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2390 | Py_XDECREF(buf); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2391 | Py_XDECREF(addr); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 2392 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2395 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2396 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2397 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2398 | 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] | 2399 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2400 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2401 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2402 | |
| 2403 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2404 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2405 | { |
| 2406 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
| 2407 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2408 | int recvlen = 0, flags = 0; |
| 2409 | ssize_t readlen; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2410 | char *buf; |
| 2411 | int buflen; |
| 2412 | |
| 2413 | PyObject *addr = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2414 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2415 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom_into", |
| 2416 | kwlist, &buf, &buflen, |
| 2417 | &recvlen, &flags)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2418 | return NULL; |
| 2419 | assert(buf != 0 && buflen > 0); |
| 2420 | |
| 2421 | if (recvlen < 0) { |
| 2422 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2423 | "negative buffersize in recvfrom_into"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2424 | return NULL; |
| 2425 | } |
| 2426 | if (recvlen == 0) { |
| 2427 | /* If nbytes was not specified, use the buffer's length */ |
| 2428 | recvlen = buflen; |
| 2429 | } |
| 2430 | |
| 2431 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2432 | if (readlen < 0) { |
| 2433 | /* Return an error */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2434 | Py_XDECREF(addr); |
| 2435 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | /* Return the number of bytes read and the address. Note that we do |
| 2439 | not do anything special here in the case that readlen < recvlen. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2440 | return Py_BuildValue("lN", readlen, addr); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2443 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2444 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2445 | \n\ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2446 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2447 | |
| 2448 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2449 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2450 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2451 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2452 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2453 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2454 | char *buf; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2455 | int len, n = -1, flags = 0, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2456 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2457 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2458 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2459 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2460 | if (!IS_SELECTABLE(s)) |
| 2461 | return select_error(); |
| 2462 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2463 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2464 | timeout = internal_select(s, 1); |
| 2465 | if (!timeout) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2466 | #ifdef __VMS |
| 2467 | n = sendsegmented(s->sock_fd, buf, len, flags); |
| 2468 | #else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2469 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2470 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2471 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2472 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2473 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2474 | PyErr_SetString(socket_timeout, "timed out"); |
| 2475 | return NULL; |
| 2476 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2477 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2478 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2479 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2482 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2483 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2484 | \n\ |
| 2485 | 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] | 2486 | 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] | 2487 | 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] | 2488 | |
| 2489 | |
| 2490 | /* s.sendall(data [,flags]) method */ |
| 2491 | |
| 2492 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2493 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2494 | { |
| 2495 | char *buf; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2496 | int len, n = -1, flags = 0, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2497 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2498 | if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) |
| 2499 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2500 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2501 | if (!IS_SELECTABLE(s)) |
| 2502 | return select_error(); |
| 2503 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2504 | Py_BEGIN_ALLOW_THREADS |
| 2505 | do { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2506 | timeout = internal_select(s, 1); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2507 | n = -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2508 | if (timeout) |
| 2509 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2510 | #ifdef __VMS |
| 2511 | n = sendsegmented(s->sock_fd, buf, len, flags); |
| 2512 | #else |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2513 | n = send(s->sock_fd, buf, len, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2514 | #endif |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2515 | if (n < 0) |
| 2516 | break; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2517 | buf += n; |
| 2518 | len -= n; |
| 2519 | } while (len > 0); |
| 2520 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2521 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2522 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2523 | PyErr_SetString(socket_timeout, "timed out"); |
| 2524 | return NULL; |
| 2525 | } |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2526 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2527 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2528 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2529 | Py_INCREF(Py_None); |
| 2530 | return Py_None; |
| 2531 | } |
| 2532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2533 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2534 | "sendall(data[, flags])\n\ |
| 2535 | \n\ |
| 2536 | Send a data string to the socket. For the optional flags\n\ |
| 2537 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2538 | 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] | 2539 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2540 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2541 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2542 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2543 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2544 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2545 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2546 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2547 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2548 | char *buf; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2549 | sock_addr_t addrbuf; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2550 | int addrlen, len, n = -1, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2551 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2552 | flags = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2553 | if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2554 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2555 | if (!PyArg_ParseTuple(args, "s#iO:sendto", |
| 2556 | &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2557 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2558 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2559 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2560 | if (!IS_SELECTABLE(s)) |
| 2561 | return select_error(); |
| 2562 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2563 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2564 | return NULL; |
| 2565 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2566 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2567 | timeout = internal_select(s, 1); |
| 2568 | if (!timeout) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2569 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2570 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2571 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2572 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2573 | PyErr_SetString(socket_timeout, "timed out"); |
| 2574 | return NULL; |
| 2575 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2576 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2577 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2578 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2581 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2582 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2583 | \n\ |
| 2584 | 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] | 2585 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2586 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2587 | |
| 2588 | /* s.shutdown(how) method */ |
| 2589 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2590 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2591 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2592 | { |
| 2593 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2594 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2595 | |
| 2596 | how = PyInt_AsLong(arg); |
| 2597 | if (how == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2598 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2599 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2600 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2601 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2602 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2603 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2604 | Py_INCREF(Py_None); |
| 2605 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2608 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2609 | "shutdown(flag)\n\ |
| 2610 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2611 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2612 | 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] | 2613 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2614 | |
| 2615 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2616 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2617 | static PyMethodDef sock_methods[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2618 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 2619 | accept_doc}, |
| 2620 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 2621 | bind_doc}, |
| 2622 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 2623 | close_doc}, |
| 2624 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 2625 | connect_doc}, |
| 2626 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 2627 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2628 | #ifndef NO_DUP |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2629 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
| 2630 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2631 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2632 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 2633 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2634 | #ifdef HAVE_GETPEERNAME |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2635 | {"getpeername", (PyCFunction)sock_getpeername, |
| 2636 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 2637 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2638 | {"getsockname", (PyCFunction)sock_getsockname, |
| 2639 | METH_NOARGS, getsockname_doc}, |
| 2640 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 2641 | getsockopt_doc}, |
| 2642 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 2643 | listen_doc}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2644 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 2645 | recv_doc}, |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2646 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 2647 | recv_into_doc}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2648 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 2649 | recvfrom_doc}, |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2650 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 2651 | recvfrom_into_doc}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2652 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 2653 | send_doc}, |
| 2654 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 2655 | sendall_doc}, |
| 2656 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 2657 | sendto_doc}, |
| 2658 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 2659 | setblocking_doc}, |
| 2660 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 2661 | settimeout_doc}, |
| 2662 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 2663 | gettimeout_doc}, |
| 2664 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 2665 | setsockopt_doc}, |
| 2666 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 2667 | shutdown_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2668 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2669 | }; |
| 2670 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2671 | /* SockObject members */ |
| 2672 | static PyMemberDef sock_memberlist[] = { |
| 2673 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 2674 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 2675 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 2676 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 2677 | {0}, |
| 2678 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2679 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2680 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2681 | First close the file description. */ |
| 2682 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2683 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2684 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2685 | { |
Guido van Rossum | fa972c9 | 2000-04-10 12:45:45 +0000 | [diff] [blame] | 2686 | if (s->sock_fd != -1) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 2687 | (void) SOCKETCLOSE(s->sock_fd); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2688 | Py_Type(s)->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2691 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2692 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2693 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2694 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2695 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
| 2696 | if (s->sock_fd > LONG_MAX) { |
| 2697 | /* this can occur on Win64, and actually there is a special |
| 2698 | ugly printf formatter for decimal pointer length integer |
| 2699 | printing, only bother if necessary*/ |
| 2700 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2701 | "no printf formatter to display " |
| 2702 | "the socket descriptor in decimal"); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2703 | return NULL; |
| 2704 | } |
| 2705 | #endif |
Walter Dörwald | a7eb93e | 2007-06-05 13:41:53 +0000 | [diff] [blame] | 2706 | return PyUnicode_FromFormat( |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2707 | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2708 | (long)s->sock_fd, s->sock_family, |
| 2709 | s->sock_type, |
| 2710 | s->sock_proto); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2714 | /* Create a new, uninitialized socket object. */ |
| 2715 | |
| 2716 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2717 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2718 | { |
| 2719 | PyObject *new; |
| 2720 | |
| 2721 | new = type->tp_alloc(type, 0); |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2722 | if (new != NULL) { |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2723 | ((PySocketSockObject *)new)->sock_fd = -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2724 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2725 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2726 | } |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2727 | return new; |
| 2728 | } |
| 2729 | |
| 2730 | |
| 2731 | /* Initialize a new socket object. */ |
| 2732 | |
| 2733 | /*ARGSUSED*/ |
| 2734 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2735 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2736 | { |
| 2737 | PySocketSockObject *s = (PySocketSockObject *)self; |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2738 | PyObject *fdobj = NULL; |
| 2739 | SOCKET_T fd = INVALID_SOCKET; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2740 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2741 | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2742 | |
| 2743 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2744 | "|iiiO:socket", keywords, |
| 2745 | &family, &type, &proto, &fdobj)) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2746 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2747 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2748 | if (fdobj != NULL) { |
| 2749 | fd = PyLong_AsLongLong(fdobj); |
| 2750 | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 2751 | return -1; |
| 2752 | if (fd == INVALID_SOCKET) { |
| 2753 | PyErr_SetString(PyExc_ValueError, |
| 2754 | "can't use invalid socket value"); |
| 2755 | return -1; |
| 2756 | } |
| 2757 | } |
| 2758 | else { |
| 2759 | Py_BEGIN_ALLOW_THREADS |
| 2760 | fd = socket(family, type, proto); |
| 2761 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2762 | |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 2763 | if (fd == INVALID_SOCKET) { |
| 2764 | set_error(); |
| 2765 | return -1; |
| 2766 | } |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2767 | } |
| 2768 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2769 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2770 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2771 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2775 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2776 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2777 | static PyTypeObject sock_type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2778 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
Guido van Rossum | ca6dfa5 | 2001-10-28 12:31:33 +0000 | [diff] [blame] | 2779 | "_socket.socket", /* tp_name */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2780 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 2781 | 0, /* tp_itemsize */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2782 | (destructor)sock_dealloc, /* tp_dealloc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2783 | 0, /* tp_print */ |
| 2784 | 0, /* tp_getattr */ |
| 2785 | 0, /* tp_setattr */ |
| 2786 | 0, /* tp_compare */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2787 | (reprfunc)sock_repr, /* tp_repr */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2788 | 0, /* tp_as_number */ |
| 2789 | 0, /* tp_as_sequence */ |
| 2790 | 0, /* tp_as_mapping */ |
| 2791 | 0, /* tp_hash */ |
| 2792 | 0, /* tp_call */ |
| 2793 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2794 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2795 | 0, /* tp_setattro */ |
| 2796 | 0, /* tp_as_buffer */ |
| 2797 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2798 | sock_doc, /* tp_doc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2799 | 0, /* tp_traverse */ |
| 2800 | 0, /* tp_clear */ |
| 2801 | 0, /* tp_richcompare */ |
| 2802 | 0, /* tp_weaklistoffset */ |
| 2803 | 0, /* tp_iter */ |
| 2804 | 0, /* tp_iternext */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2805 | sock_methods, /* tp_methods */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2806 | sock_memberlist, /* tp_members */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2807 | 0, /* tp_getset */ |
| 2808 | 0, /* tp_base */ |
| 2809 | 0, /* tp_dict */ |
| 2810 | 0, /* tp_descr_get */ |
| 2811 | 0, /* tp_descr_set */ |
| 2812 | 0, /* tp_dictoffset */ |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2813 | sock_initobj, /* tp_init */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2814 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2815 | sock_new, /* tp_new */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2816 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2817 | }; |
| 2818 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2819 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2820 | /* Python interface to gethostname(). */ |
| 2821 | |
| 2822 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2823 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2824 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2825 | { |
| 2826 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2827 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2828 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2829 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2830 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2831 | if (res < 0) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2832 | return set_error(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2833 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 2834 | return PyUnicode_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2835 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2836 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2837 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2838 | "gethostname() -> string\n\ |
| 2839 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2840 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2841 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2842 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2843 | /* Python interface to gethostbyname(name). */ |
| 2844 | |
| 2845 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2846 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2847 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2848 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2849 | char *name; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2850 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2851 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2852 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2853 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2854 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2855 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2856 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2859 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2860 | "gethostbyname(host) -> address\n\ |
| 2861 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2862 | 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] | 2863 | |
| 2864 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2865 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 2866 | |
| 2867 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2868 | 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] | 2869 | { |
| 2870 | char **pch; |
| 2871 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 2872 | PyObject *name_list = (PyObject *)NULL; |
| 2873 | PyObject *addr_list = (PyObject *)NULL; |
| 2874 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2875 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2876 | if (h == NULL) { |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2877 | /* Let's get real error message to return */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2878 | set_herror(h_errno); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2879 | return NULL; |
| 2880 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2881 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2882 | if (h->h_addrtype != af) { |
| 2883 | #ifdef HAVE_STRERROR |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2884 | /* Let's get real error message to return */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2885 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2886 | (char *)strerror(EAFNOSUPPORT)); |
| 2887 | #else |
| 2888 | PyErr_SetString( |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2889 | socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2890 | "Address family not supported by protocol family"); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2891 | #endif |
| 2892 | return NULL; |
| 2893 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2894 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2895 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2896 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2897 | case AF_INET: |
| 2898 | if (alen < sizeof(struct sockaddr_in)) |
| 2899 | return NULL; |
| 2900 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2901 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2902 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2903 | case AF_INET6: |
| 2904 | if (alen < sizeof(struct sockaddr_in6)) |
| 2905 | return NULL; |
| 2906 | break; |
| 2907 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2908 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2909 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2910 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2911 | if ((name_list = PyList_New(0)) == NULL) |
| 2912 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2913 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2914 | if ((addr_list = PyList_New(0)) == NULL) |
| 2915 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2916 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2917 | /* SF #1511317: h_aliases can be NULL */ |
| 2918 | if (h->h_aliases) { |
| 2919 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 2920 | int status; |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 2921 | tmp = PyUnicode_FromString(*pch); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2922 | if (tmp == NULL) |
| 2923 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2924 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2925 | status = PyList_Append(name_list, tmp); |
| 2926 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2927 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2928 | if (status) |
| 2929 | goto err; |
| 2930 | } |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2931 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2932 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2933 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 2934 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2935 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2936 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2937 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2938 | case AF_INET: |
| 2939 | { |
| 2940 | struct sockaddr_in sin; |
| 2941 | memset(&sin, 0, sizeof(sin)); |
| 2942 | sin.sin_family = af; |
| 2943 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2944 | sin.sin_len = sizeof(sin); |
| 2945 | #endif |
| 2946 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 2947 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2948 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2949 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 2950 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 2951 | break; |
| 2952 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2953 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2954 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2955 | case AF_INET6: |
| 2956 | { |
| 2957 | struct sockaddr_in6 sin6; |
| 2958 | memset(&sin6, 0, sizeof(sin6)); |
| 2959 | sin6.sin6_family = af; |
| 2960 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2961 | sin6.sin6_len = sizeof(sin6); |
| 2962 | #endif |
| 2963 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 2964 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 2965 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2966 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2967 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 2968 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 2969 | break; |
| 2970 | } |
| 2971 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2972 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2973 | default: /* can't happen */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2974 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2975 | "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2976 | return NULL; |
| 2977 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2978 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2979 | if (tmp == NULL) |
| 2980 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2981 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2982 | status = PyList_Append(addr_list, tmp); |
| 2983 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2984 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2985 | if (status) |
| 2986 | goto err; |
| 2987 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2988 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2989 | 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] | 2990 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2991 | err: |
| 2992 | Py_XDECREF(name_list); |
| 2993 | Py_XDECREF(addr_list); |
| 2994 | return rtn_tuple; |
| 2995 | } |
| 2996 | |
| 2997 | |
| 2998 | /* Python interface to gethostbyname_ex(name). */ |
| 2999 | |
| 3000 | /*ARGSUSED*/ |
| 3001 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3002 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3003 | { |
| 3004 | char *name; |
| 3005 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3006 | #ifdef ENABLE_IPV6 |
| 3007 | struct sockaddr_storage addr; |
| 3008 | #else |
| 3009 | struct sockaddr_in addr; |
| 3010 | #endif |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 3011 | struct sockaddr *sa; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3012 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3013 | #ifdef HAVE_GETHOSTBYNAME_R |
| 3014 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3015 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 3016 | struct hostent_data data; |
| 3017 | #else |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3018 | char buf[16384]; |
| 3019 | int buf_len = (sizeof buf) - 1; |
| 3020 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3021 | #endif |
| 3022 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3023 | int result; |
| 3024 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3025 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3026 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3027 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3028 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3029 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3030 | return NULL; |
| 3031 | Py_BEGIN_ALLOW_THREADS |
| 3032 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3033 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3034 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3035 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3036 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3037 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3038 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 3039 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3040 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3041 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3042 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3043 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3044 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3045 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3046 | #endif |
| 3047 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3048 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3049 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3050 | /* Some C libraries would require addr.__ss_family instead of |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3051 | addr.ss_family. |
| 3052 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3053 | access sa_family. */ |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 3054 | sa = (struct sockaddr*)&addr; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3055 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3056 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3057 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3058 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3059 | #endif |
| 3060 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3063 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3064 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3065 | \n\ |
| 3066 | 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] | 3067 | 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] | 3068 | |
| 3069 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3070 | /* Python interface to gethostbyaddr(IP). */ |
| 3071 | |
| 3072 | /*ARGSUSED*/ |
| 3073 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3074 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3075 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3076 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3077 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3078 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3079 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3080 | #endif |
| 3081 | struct sockaddr *sa = (struct sockaddr *)&addr; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3082 | char *ip_num; |
| 3083 | struct hostent *h; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3084 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3085 | #ifdef HAVE_GETHOSTBYNAME_R |
| 3086 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3087 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 3088 | struct hostent_data data; |
| 3089 | #else |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3090 | char buf[16384]; |
| 3091 | int buf_len = (sizeof buf) - 1; |
| 3092 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3093 | #endif |
| 3094 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3095 | int result; |
| 3096 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3097 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3098 | char *ap; |
| 3099 | int al; |
| 3100 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3101 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3102 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3103 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3104 | af = AF_UNSPEC; |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 3105 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3106 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3107 | af = sa->sa_family; |
| 3108 | ap = NULL; |
| 3109 | al = 0; |
| 3110 | switch (af) { |
| 3111 | case AF_INET: |
| 3112 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3113 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3114 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3115 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3116 | case AF_INET6: |
| 3117 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3118 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3119 | break; |
| 3120 | #endif |
| 3121 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3122 | PyErr_SetString(socket_error, "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3123 | return NULL; |
| 3124 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3125 | Py_BEGIN_ALLOW_THREADS |
| 3126 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3127 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3128 | result = gethostbyaddr_r(ap, al, af, |
| 3129 | &hp_allocated, buf, buf_len, |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3130 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3131 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3132 | h = gethostbyaddr_r(ap, al, af, |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3133 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3134 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 3135 | memset((void *) &data, '\0', sizeof(data)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3136 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3137 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3138 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3139 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3140 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3141 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3142 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3143 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3144 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3145 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3146 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3147 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3148 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3149 | #endif |
| 3150 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3153 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3154 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3155 | \n\ |
| 3156 | 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] | 3157 | 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] | 3158 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3159 | |
| 3160 | /* Python interface to getservbyname(name). |
| 3161 | This only returns the port number, since the other info is already |
| 3162 | known or not useful (like the list of aliases). */ |
| 3163 | |
| 3164 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3165 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3166 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3167 | { |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3168 | char *name, *proto=NULL; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3169 | struct servent *sp; |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3170 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3171 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3172 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3173 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3174 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3175 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3176 | PyErr_SetString(socket_error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3177 | return NULL; |
| 3178 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3179 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3182 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3183 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3184 | \n\ |
| 3185 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3186 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3187 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3188 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3189 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3190 | /* Python interface to getservbyport(port). |
| 3191 | This only returns the service name, since the other info is already |
| 3192 | known or not useful (like the list of aliases). */ |
| 3193 | |
| 3194 | /*ARGSUSED*/ |
| 3195 | static PyObject * |
| 3196 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3197 | { |
Raymond Hettinger | 87de0ca | 2004-09-28 02:19:40 +0000 | [diff] [blame] | 3198 | unsigned short port; |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3199 | char *proto=NULL; |
| 3200 | struct servent *sp; |
Raymond Hettinger | 87de0ca | 2004-09-28 02:19:40 +0000 | [diff] [blame] | 3201 | if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto)) |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3202 | return NULL; |
| 3203 | Py_BEGIN_ALLOW_THREADS |
| 3204 | sp = getservbyport(htons(port), proto); |
| 3205 | Py_END_ALLOW_THREADS |
| 3206 | if (sp == NULL) { |
| 3207 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3208 | return NULL; |
| 3209 | } |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3210 | return PyUnicode_FromString(sp->s_name); |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | PyDoc_STRVAR(getservbyport_doc, |
| 3214 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3215 | \n\ |
| 3216 | Return the service name from a port number and protocol name.\n\ |
| 3217 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3218 | otherwise any protocol will match."); |
| 3219 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3220 | /* Python interface to getprotobyname(name). |
| 3221 | This only returns the protocol number, since the other info is |
| 3222 | already known or not useful (like the list of aliases). */ |
| 3223 | |
| 3224 | /*ARGSUSED*/ |
| 3225 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3226 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3227 | { |
| 3228 | char *name; |
| 3229 | struct protoent *sp; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3230 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3231 | return NULL; |
| 3232 | Py_BEGIN_ALLOW_THREADS |
| 3233 | sp = getprotobyname(name); |
| 3234 | Py_END_ALLOW_THREADS |
| 3235 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3236 | PyErr_SetString(socket_error, "protocol not found"); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3237 | return NULL; |
| 3238 | } |
| 3239 | return PyInt_FromLong((long) sp->p_proto); |
| 3240 | } |
| 3241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3242 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3243 | "getprotobyname(name) -> integer\n\ |
| 3244 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3245 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3246 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3247 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3248 | #ifdef HAVE_SOCKETPAIR |
| 3249 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3250 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3251 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3252 | |
| 3253 | /*ARGSUSED*/ |
| 3254 | static PyObject * |
| 3255 | socket_socketpair(PyObject *self, PyObject *args) |
| 3256 | { |
| 3257 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3258 | SOCKET_T sv[2]; |
| 3259 | int family, type = SOCK_STREAM, proto = 0; |
| 3260 | PyObject *res = NULL; |
| 3261 | |
| 3262 | #if defined(AF_UNIX) |
| 3263 | family = AF_UNIX; |
| 3264 | #else |
| 3265 | family = AF_INET; |
| 3266 | #endif |
| 3267 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3268 | &family, &type, &proto)) |
| 3269 | return NULL; |
| 3270 | /* Create a pair of socket fds */ |
| 3271 | if (socketpair(family, type, proto, sv) < 0) |
| 3272 | return set_error(); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3273 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3274 | if (s0 == NULL) |
| 3275 | goto finally; |
| 3276 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3277 | if (s1 == NULL) |
| 3278 | goto finally; |
| 3279 | res = PyTuple_Pack(2, s0, s1); |
| 3280 | |
| 3281 | finally: |
| 3282 | if (res == NULL) { |
| 3283 | if (s0 == NULL) |
| 3284 | SOCKETCLOSE(sv[0]); |
| 3285 | if (s1 == NULL) |
| 3286 | SOCKETCLOSE(sv[1]); |
| 3287 | } |
| 3288 | Py_XDECREF(s0); |
| 3289 | Py_XDECREF(s1); |
| 3290 | return res; |
| 3291 | } |
| 3292 | |
| 3293 | PyDoc_STRVAR(socketpair_doc, |
| 3294 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3295 | \n\ |
| 3296 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3297 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3298 | 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] | 3299 | 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] | 3300 | |
| 3301 | #endif /* HAVE_SOCKETPAIR */ |
| 3302 | |
| 3303 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3304 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3305 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3306 | { |
| 3307 | int x1, x2; |
| 3308 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3309 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3310 | return NULL; |
| 3311 | } |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 3312 | if (x1 < 0) { |
| 3313 | PyErr_SetString(PyExc_OverflowError, |
| 3314 | "can't convert negative number to unsigned long"); |
| 3315 | return NULL; |
| 3316 | } |
| 3317 | x2 = (unsigned int)ntohs((unsigned short)x1); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3318 | return PyInt_FromLong(x2); |
| 3319 | } |
| 3320 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3321 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3322 | "ntohs(integer) -> integer\n\ |
| 3323 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3324 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3325 | |
| 3326 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3327 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3328 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3329 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3330 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3331 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3332 | if (PyLong_Check(arg)) { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3333 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3334 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3335 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3336 | #if SIZEOF_LONG > 4 |
| 3337 | { |
| 3338 | unsigned long y; |
| 3339 | /* only want the trailing 32 bits */ |
| 3340 | y = x & 0xFFFFFFFFUL; |
| 3341 | if (y ^ x) |
| 3342 | return PyErr_Format(PyExc_OverflowError, |
| 3343 | "long int larger than 32 bits"); |
| 3344 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3345 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3346 | #endif |
| 3347 | } |
| 3348 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3349 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3350 | "expected int/long, %s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3351 | Py_Type(arg)->tp_name); |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3352 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3353 | return NULL; |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 3354 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3357 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3358 | "ntohl(integer) -> integer\n\ |
| 3359 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3360 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3361 | |
| 3362 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3363 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3364 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3365 | { |
Hye-Shik Chang | a7b673f | 2003-12-17 09:40:23 +0000 | [diff] [blame] | 3366 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3367 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3368 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3369 | return NULL; |
| 3370 | } |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 3371 | if (x1 < 0) { |
| 3372 | PyErr_SetString(PyExc_OverflowError, |
| 3373 | "can't convert negative number to unsigned long"); |
| 3374 | return NULL; |
| 3375 | } |
| 3376 | x2 = (unsigned int)htons((unsigned short)x1); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3377 | return PyInt_FromLong(x2); |
| 3378 | } |
| 3379 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3380 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3381 | "htons(integer) -> integer\n\ |
| 3382 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3383 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3384 | |
| 3385 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3386 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3387 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3388 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3389 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3390 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3391 | if (PyLong_Check(arg)) { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3392 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3393 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3394 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3395 | #if SIZEOF_LONG > 4 |
| 3396 | { |
| 3397 | unsigned long y; |
| 3398 | /* only want the trailing 32 bits */ |
| 3399 | y = x & 0xFFFFFFFFUL; |
| 3400 | if (y ^ x) |
| 3401 | return PyErr_Format(PyExc_OverflowError, |
| 3402 | "long int larger than 32 bits"); |
| 3403 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3404 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3405 | #endif |
| 3406 | } |
| 3407 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3408 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3409 | "expected int/long, %s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3410 | Py_Type(arg)->tp_name); |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 3411 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3414 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3415 | "htonl(integer) -> integer\n\ |
| 3416 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3417 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3418 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3419 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3421 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 3422 | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3423 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3424 | 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] | 3425 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3426 | |
| 3427 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3428 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3429 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3430 | #ifndef INADDR_NONE |
| 3431 | #define INADDR_NONE (-1) |
| 3432 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3433 | #ifdef HAVE_INET_ATON |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3434 | struct in_addr buf; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3435 | #endif |
| 3436 | |
| 3437 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3438 | /* Have to use inet_addr() instead */ |
| 3439 | unsigned long packed_addr; |
| 3440 | #endif |
| 3441 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3442 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3443 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3444 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3445 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3446 | |
| 3447 | #ifdef HAVE_INET_ATON |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3448 | |
| 3449 | #ifdef USE_INET_ATON_WEAKLINK |
| 3450 | if (inet_aton != NULL) { |
| 3451 | #endif |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3452 | if (inet_aton(ip_addr, &buf)) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 3453 | return PyBytes_FromStringAndSize((char *)(&buf), |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3454 | sizeof(buf)); |
| 3455 | |
| 3456 | PyErr_SetString(socket_error, |
| 3457 | "illegal IP address string passed to inet_aton"); |
| 3458 | return NULL; |
| 3459 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3460 | #ifdef USE_INET_ATON_WEAKLINK |
| 3461 | } else { |
| 3462 | #endif |
| 3463 | |
| 3464 | #endif |
| 3465 | |
| 3466 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3467 | |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 3468 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3469 | * for this */ |
| 3470 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3471 | packed_addr = 0xFFFFFFFF; |
| 3472 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3473 | |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 3474 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3475 | |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 3476 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3477 | PyErr_SetString(socket_error, |
| 3478 | "illegal IP address string passed to inet_aton"); |
| 3479 | return NULL; |
| 3480 | } |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3481 | } |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 3482 | return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 3483 | sizeof(packed_addr)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3484 | |
| 3485 | #ifdef USE_INET_ATON_WEAKLINK |
| 3486 | } |
| 3487 | #endif |
| 3488 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3489 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3492 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3493 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3494 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3495 | 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] | 3496 | |
| 3497 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3498 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3499 | { |
| 3500 | char *packed_str; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3501 | int addr_len; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3502 | struct in_addr packed_addr; |
| 3503 | |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3504 | if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3505 | return NULL; |
| 3506 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3507 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3508 | if (addr_len != sizeof(packed_addr)) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3509 | PyErr_SetString(socket_error, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3510 | "packed IP wrong length for inet_ntoa"); |
| 3511 | return NULL; |
| 3512 | } |
| 3513 | |
| 3514 | memcpy(&packed_addr, packed_str, addr_len); |
| 3515 | |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3516 | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3517 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3518 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3519 | #ifdef HAVE_INET_PTON |
| 3520 | |
| 3521 | PyDoc_STRVAR(inet_pton_doc, |
| 3522 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 3523 | \n\ |
| 3524 | Convert an IP address from string format to a packed string suitable\n\ |
| 3525 | for use with low-level network functions."); |
| 3526 | |
| 3527 | static PyObject * |
| 3528 | socket_inet_pton(PyObject *self, PyObject *args) |
| 3529 | { |
| 3530 | int af; |
| 3531 | char* ip; |
| 3532 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3533 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3534 | 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] | 3535 | #else |
| 3536 | char packed[sizeof(struct in_addr)]; |
| 3537 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3538 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3539 | return NULL; |
| 3540 | } |
| 3541 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 3542 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3543 | if(af == AF_INET6) { |
| 3544 | PyErr_SetString(socket_error, |
| 3545 | "can't use AF_INET6, IPv6 is disabled"); |
| 3546 | return NULL; |
| 3547 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3548 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3549 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3550 | retval = inet_pton(af, ip, packed); |
| 3551 | if (retval < 0) { |
| 3552 | PyErr_SetFromErrno(socket_error); |
| 3553 | return NULL; |
| 3554 | } else if (retval == 0) { |
| 3555 | PyErr_SetString(socket_error, |
| 3556 | "illegal IP address string passed to inet_pton"); |
| 3557 | return NULL; |
| 3558 | } else if (af == AF_INET) { |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3559 | return PyBytes_FromStringAndSize(packed, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3560 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3561 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3562 | } else if (af == AF_INET6) { |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3563 | return PyBytes_FromStringAndSize(packed, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3564 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3565 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3566 | } else { |
| 3567 | PyErr_SetString(socket_error, "unknown address family"); |
| 3568 | return NULL; |
| 3569 | } |
| 3570 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3571 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3572 | PyDoc_STRVAR(inet_ntop_doc, |
| 3573 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3574 | \n\ |
| 3575 | Convert a packed IP address of the given family to string format."); |
| 3576 | |
| 3577 | static PyObject * |
| 3578 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 3579 | { |
| 3580 | int af; |
| 3581 | char* packed; |
| 3582 | int len; |
| 3583 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3584 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3585 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3586 | #else |
| 3587 | char ip[INET_ADDRSTRLEN + 1]; |
| 3588 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3589 | |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3590 | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
Jeremy Hylton | 80961f3 | 2004-11-07 14:24:25 +0000 | [diff] [blame] | 3591 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3592 | |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3593 | if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3594 | return NULL; |
| 3595 | } |
| 3596 | |
| 3597 | if (af == AF_INET) { |
| 3598 | if (len != sizeof(struct in_addr)) { |
| 3599 | PyErr_SetString(PyExc_ValueError, |
| 3600 | "invalid length of packed IP address string"); |
| 3601 | return NULL; |
| 3602 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3603 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3604 | } else if (af == AF_INET6) { |
| 3605 | if (len != sizeof(struct in6_addr)) { |
| 3606 | PyErr_SetString(PyExc_ValueError, |
| 3607 | "invalid length of packed IP address string"); |
| 3608 | return NULL; |
| 3609 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3610 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3611 | } else { |
| 3612 | PyErr_Format(PyExc_ValueError, |
| 3613 | "unknown address family %d", af); |
| 3614 | return NULL; |
| 3615 | } |
| 3616 | |
| 3617 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 3618 | if (!retval) { |
| 3619 | PyErr_SetFromErrno(socket_error); |
| 3620 | return NULL; |
| 3621 | } else { |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3622 | return PyUnicode_FromString(retval); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | /* NOTREACHED */ |
| 3626 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 3627 | return NULL; |
| 3628 | } |
| 3629 | |
| 3630 | #endif /* HAVE_INET_PTON */ |
| 3631 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3632 | /* Python interface to getaddrinfo(host, port). */ |
| 3633 | |
| 3634 | /*ARGSUSED*/ |
| 3635 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3636 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3637 | { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3638 | struct addrinfo hints, *res; |
| 3639 | struct addrinfo *res0 = NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3640 | PyObject *hobj = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3641 | PyObject *pobj = (PyObject *)NULL; |
Martin v. Löwis | f65b1a1 | 2001-08-12 09:28:40 +0000 | [diff] [blame] | 3642 | char pbuf[30]; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3643 | char *hptr, *pptr; |
| 3644 | int family, socktype, protocol, flags; |
| 3645 | int error; |
| 3646 | PyObject *all = (PyObject *)NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3647 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3648 | |
| 3649 | family = socktype = protocol = flags = 0; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3650 | family = AF_UNSPEC; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3651 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 3652 | &hobj, &pobj, &family, &socktype, |
| 3653 | &protocol, &flags)) { |
| 3654 | return NULL; |
| 3655 | } |
| 3656 | if (hobj == Py_None) { |
| 3657 | hptr = NULL; |
| 3658 | } else if (PyUnicode_Check(hobj)) { |
| 3659 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 3660 | if (!idna) |
| 3661 | return NULL; |
Guido van Rossum | b5b2270 | 2007-05-18 18:55:53 +0000 | [diff] [blame] | 3662 | assert(PyBytes_Check(idna)); |
| 3663 | hptr = PyBytes_AsString(idna); |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3664 | } else if (PyString_Check(hobj)) { |
| 3665 | hptr = PyString_AsString(hobj); |
| 3666 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3667 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3668 | "getaddrinfo() argument 1 must be string or None"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3669 | return NULL; |
| 3670 | } |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3671 | if (PyInt_CheckExact(pobj)) { |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3672 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3673 | pptr = pbuf; |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 3674 | } else if (PyUnicode_Check(pobj)) { |
| 3675 | pptr = PyUnicode_AsString(pobj); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3676 | } else if (PyString_Check(pobj)) { |
| 3677 | pptr = PyString_AsString(pobj); |
| 3678 | } else if (pobj == Py_None) { |
| 3679 | pptr = (char *)NULL; |
| 3680 | } else { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3681 | PyErr_SetString(socket_error, "Int or String expected"); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3682 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3683 | } |
| 3684 | memset(&hints, 0, sizeof(hints)); |
| 3685 | hints.ai_family = family; |
| 3686 | hints.ai_socktype = socktype; |
| 3687 | hints.ai_protocol = protocol; |
| 3688 | hints.ai_flags = flags; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3689 | Py_BEGIN_ALLOW_THREADS |
| 3690 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3691 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3692 | Py_END_ALLOW_THREADS |
| 3693 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3694 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3695 | set_gaierror(error); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3696 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | if ((all = PyList_New(0)) == NULL) |
| 3700 | goto err; |
| 3701 | for (res = res0; res; res = res->ai_next) { |
Guido van Rossum | 9ce8e38 | 2007-08-09 21:06:37 +0000 | [diff] [blame] | 3702 | PyObject *single; |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3703 | PyObject *addr = |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 3704 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3705 | if (addr == NULL) |
| 3706 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3707 | single = Py_BuildValue("iiisO", res->ai_family, |
| 3708 | res->ai_socktype, res->ai_protocol, |
| 3709 | res->ai_canonname ? res->ai_canonname : "", |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3710 | addr); |
| 3711 | Py_DECREF(addr); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3712 | if (single == NULL) |
| 3713 | goto err; |
| 3714 | |
| 3715 | if (PyList_Append(all, single)) |
| 3716 | goto err; |
| 3717 | Py_XDECREF(single); |
| 3718 | } |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3719 | Py_XDECREF(idna); |
Neal Norwitz | 90128ba | 2002-08-09 03:37:42 +0000 | [diff] [blame] | 3720 | if (res0) |
| 3721 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3722 | return all; |
| 3723 | err: |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3724 | Py_XDECREF(all); |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3725 | Py_XDECREF(idna); |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3726 | if (res0) |
| 3727 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3728 | return (PyObject *)NULL; |
| 3729 | } |
| 3730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3731 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3732 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 3733 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3734 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3735 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3736 | |
| 3737 | /* Python interface to getnameinfo(sa, flags). */ |
| 3738 | |
| 3739 | /*ARGSUSED*/ |
| 3740 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3741 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3742 | { |
| 3743 | PyObject *sa = (PyObject *)NULL; |
| 3744 | int flags; |
| 3745 | char *hostp; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3746 | int port, flowinfo, scope_id; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3747 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 3748 | struct addrinfo hints, *res = NULL; |
| 3749 | int error; |
| 3750 | PyObject *ret = (PyObject *)NULL; |
| 3751 | |
| 3752 | flags = flowinfo = scope_id = 0; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3753 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3754 | return NULL; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3755 | if (!PyArg_ParseTuple(sa, "si|ii", |
| 3756 | &hostp, &port, &flowinfo, &scope_id)) |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3757 | return NULL; |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3758 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3759 | memset(&hints, 0, sizeof(hints)); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3760 | hints.ai_family = AF_UNSPEC; |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3761 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3762 | Py_BEGIN_ALLOW_THREADS |
| 3763 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3764 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3765 | Py_END_ALLOW_THREADS |
| 3766 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3767 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3768 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3769 | goto fail; |
| 3770 | } |
| 3771 | if (res->ai_next) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3772 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3773 | "sockaddr resolved to multiple addresses"); |
| 3774 | goto fail; |
| 3775 | } |
| 3776 | switch (res->ai_family) { |
| 3777 | case AF_INET: |
| 3778 | { |
| 3779 | char *t1; |
| 3780 | int t2; |
| 3781 | if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3782 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3783 | "IPv4 sockaddr must be 2 tuple"); |
| 3784 | goto fail; |
| 3785 | } |
| 3786 | break; |
| 3787 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3788 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3789 | case AF_INET6: |
| 3790 | { |
| 3791 | struct sockaddr_in6 *sin6; |
| 3792 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 3793 | sin6->sin6_flowinfo = flowinfo; |
| 3794 | sin6->sin6_scope_id = scope_id; |
| 3795 | break; |
| 3796 | } |
| 3797 | #endif |
| 3798 | } |
| 3799 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 3800 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 3801 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3802 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3803 | goto fail; |
| 3804 | } |
| 3805 | ret = Py_BuildValue("ss", hbuf, pbuf); |
| 3806 | |
| 3807 | fail: |
| 3808 | if (res) |
| 3809 | freeaddrinfo(res); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3810 | return ret; |
| 3811 | } |
| 3812 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3813 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3814 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3815 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3816 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3817 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3818 | |
| 3819 | /* Python API to getting and setting the default timeout value. */ |
| 3820 | |
| 3821 | static PyObject * |
| 3822 | socket_getdefaulttimeout(PyObject *self) |
| 3823 | { |
| 3824 | if (defaulttimeout < 0.0) { |
| 3825 | Py_INCREF(Py_None); |
| 3826 | return Py_None; |
| 3827 | } |
| 3828 | else |
| 3829 | return PyFloat_FromDouble(defaulttimeout); |
| 3830 | } |
| 3831 | |
| 3832 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3833 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3834 | \n\ |
| 3835 | Returns the default timeout in floating seconds for new socket objects.\n\ |
| 3836 | A value of None indicates that new socket objects have no timeout.\n\ |
| 3837 | When the socket module is first imported, the default is None."); |
| 3838 | |
| 3839 | static PyObject * |
| 3840 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 3841 | { |
| 3842 | double timeout; |
| 3843 | |
| 3844 | if (arg == Py_None) |
| 3845 | timeout = -1.0; |
| 3846 | else { |
| 3847 | timeout = PyFloat_AsDouble(arg); |
| 3848 | if (timeout < 0.0) { |
| 3849 | if (!PyErr_Occurred()) |
| 3850 | PyErr_SetString(PyExc_ValueError, |
| 3851 | "Timeout value out of range"); |
| 3852 | return NULL; |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | defaulttimeout = timeout; |
| 3857 | |
| 3858 | Py_INCREF(Py_None); |
| 3859 | return Py_None; |
| 3860 | } |
| 3861 | |
| 3862 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3863 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3864 | \n\ |
| 3865 | Set the default timeout in floating seconds for new socket objects.\n\ |
| 3866 | A value of None indicates that new socket objects have no timeout.\n\ |
| 3867 | When the socket module is first imported, the default is None."); |
| 3868 | |
| 3869 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3870 | /* List of functions exported by this module. */ |
| 3871 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3872 | static PyMethodDef socket_methods[] = { |
| 3873 | {"gethostbyname", socket_gethostbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3874 | METH_VARARGS, gethostbyname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3875 | {"gethostbyname_ex", socket_gethostbyname_ex, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3876 | METH_VARARGS, ghbn_ex_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3877 | {"gethostbyaddr", socket_gethostbyaddr, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3878 | METH_VARARGS, gethostbyaddr_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3879 | {"gethostname", socket_gethostname, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 3880 | METH_NOARGS, gethostname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3881 | {"getservbyname", socket_getservbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3882 | METH_VARARGS, getservbyname_doc}, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3883 | {"getservbyport", socket_getservbyport, |
| 3884 | METH_VARARGS, getservbyport_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3885 | {"getprotobyname", socket_getprotobyname, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 3886 | METH_VARARGS, getprotobyname_doc}, |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3887 | #ifdef HAVE_SOCKETPAIR |
| 3888 | {"socketpair", socket_socketpair, |
| 3889 | METH_VARARGS, socketpair_doc}, |
| 3890 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3891 | {"ntohs", socket_ntohs, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3892 | METH_VARARGS, ntohs_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3893 | {"ntohl", socket_ntohl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3894 | METH_O, ntohl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3895 | {"htons", socket_htons, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3896 | METH_VARARGS, htons_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3897 | {"htonl", socket_htonl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3898 | METH_O, htonl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3899 | {"inet_aton", socket_inet_aton, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3900 | METH_VARARGS, inet_aton_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3901 | {"inet_ntoa", socket_inet_ntoa, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3902 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3903 | #ifdef HAVE_INET_PTON |
| 3904 | {"inet_pton", socket_inet_pton, |
| 3905 | METH_VARARGS, inet_pton_doc}, |
| 3906 | {"inet_ntop", socket_inet_ntop, |
| 3907 | METH_VARARGS, inet_ntop_doc}, |
| 3908 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3909 | {"getaddrinfo", socket_getaddrinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3910 | METH_VARARGS, getaddrinfo_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3911 | {"getnameinfo", socket_getnameinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3912 | METH_VARARGS, getnameinfo_doc}, |
Guido van Rossum | 1693ba8 | 2002-07-18 21:11:26 +0000 | [diff] [blame] | 3913 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3914 | METH_NOARGS, getdefaulttimeout_doc}, |
| 3915 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 3916 | METH_O, setdefaulttimeout_doc}, |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3917 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3918 | }; |
| 3919 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3920 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3921 | #ifdef MS_WINDOWS |
| 3922 | #define OS_INIT_DEFINED |
| 3923 | |
| 3924 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3925 | |
| 3926 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3927 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3928 | { |
| 3929 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3933 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3934 | { |
| 3935 | WSADATA WSAData; |
| 3936 | int ret; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3937 | ret = WSAStartup(0x0101, &WSAData); |
| 3938 | switch (ret) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3939 | case 0: /* No error */ |
Mark Hammond | 14350ab | 2003-07-09 04:57:46 +0000 | [diff] [blame] | 3940 | Py_AtExit(os_cleanup); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3941 | return 1; /* Success */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3942 | case WSASYSNOTREADY: |
| 3943 | PyErr_SetString(PyExc_ImportError, |
| 3944 | "WSAStartup failed: network not ready"); |
| 3945 | break; |
| 3946 | case WSAVERNOTSUPPORTED: |
| 3947 | case WSAEINVAL: |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3948 | PyErr_SetString( |
| 3949 | PyExc_ImportError, |
| 3950 | "WSAStartup failed: requested version not supported"); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3951 | break; |
| 3952 | default: |
Walter Dörwald | 0bbd8ab | 2007-06-05 13:49:43 +0000 | [diff] [blame] | 3953 | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3954 | break; |
| 3955 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3956 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3959 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3960 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3961 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3962 | #ifdef PYOS_OS2 |
| 3963 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3964 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3965 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3966 | |
| 3967 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3968 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3969 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3970 | #ifndef PYCC_GCC |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3971 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3972 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3973 | if (rc == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3974 | return 1; /* Success */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3975 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3976 | |
Walter Dörwald | 0bbd8ab | 2007-06-05 13:49:43 +0000 | [diff] [blame] | 3977 | PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno()); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3978 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3979 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 3980 | #else |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3981 | /* No need to initialise sockets with GCC/EMX */ |
| 3982 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 3983 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | #endif /* PYOS_OS2 */ |
| 3987 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3988 | |
| 3989 | #ifndef OS_INIT_DEFINED |
| 3990 | static int |
| 3991 | os_init(void) |
| 3992 | { |
| 3993 | return 1; /* Success */ |
| 3994 | } |
| 3995 | #endif |
| 3996 | |
| 3997 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3998 | /* C API table - always add new things to the end for binary |
| 3999 | compatibility. */ |
| 4000 | static |
| 4001 | PySocketModule_APIObject PySocketModuleAPI = |
| 4002 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4003 | &sock_type, |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 4004 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4005 | }; |
| 4006 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4007 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4008 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4009 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4010 | This module is actually called "_socket", and there's a wrapper |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4011 | "socket.py" which implements some additional functionality. |
| 4012 | The import of "_socket" may fail with an ImportError exception if |
| 4013 | os-specific initialization fails. On Windows, this does WINSOCK |
| 4014 | initialization. When WINSOCK is initialized succesfully, a call to |
| 4015 | WSACleanup() is scheduled to be made at exit time. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4016 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4017 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4018 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4019 | "Implementation module for socket operations.\n\ |
| 4020 | \n\ |
| 4021 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4022 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4023 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4024 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4025 | { |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4026 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4027 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4028 | if (!os_init()) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4029 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4030 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4031 | Py_Type(&sock_type) = &PyType_Type; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4032 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 4033 | socket_methods, |
| 4034 | socket_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 4035 | if (m == NULL) |
| 4036 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4037 | |
Gregory P. Smith | c111d9f | 2007-09-09 23:55:55 +0000 | [diff] [blame] | 4038 | socket_error = PyErr_NewException("socket.error", |
| 4039 | PyExc_IOError, NULL); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4040 | if (socket_error == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4041 | return; |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 4042 | PySocketModuleAPI.error = socket_error; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4043 | Py_INCREF(socket_error); |
| 4044 | PyModule_AddObject(m, "error", socket_error); |
| 4045 | socket_herror = PyErr_NewException("socket.herror", |
| 4046 | socket_error, NULL); |
| 4047 | if (socket_herror == NULL) |
| 4048 | return; |
| 4049 | Py_INCREF(socket_herror); |
| 4050 | PyModule_AddObject(m, "herror", socket_herror); |
| 4051 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4052 | NULL); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4053 | if (socket_gaierror == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4054 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4055 | Py_INCREF(socket_gaierror); |
| 4056 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 4057 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4058 | socket_error, NULL); |
| 4059 | if (socket_timeout == NULL) |
| 4060 | return; |
| 4061 | Py_INCREF(socket_timeout); |
| 4062 | PyModule_AddObject(m, "timeout", socket_timeout); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4063 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4064 | if (PyModule_AddObject(m, "SocketType", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4065 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4066 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4067 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4068 | if (PyModule_AddObject(m, "socket", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4069 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4070 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4071 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4072 | #ifdef ENABLE_IPV6 |
| 4073 | has_ipv6 = Py_True; |
| 4074 | #else |
| 4075 | has_ipv6 = Py_False; |
| 4076 | #endif |
| 4077 | Py_INCREF(has_ipv6); |
| 4078 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
| 4079 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4080 | /* Export C API */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4081 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4082 | PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL) |
| 4083 | ) != 0) |
| 4084 | return; |
| 4085 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4086 | /* Address families (we only support AF_INET and AF_UNIX) */ |
| 4087 | #ifdef AF_UNSPEC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4088 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4089 | #endif |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4090 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4091 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4092 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4093 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4094 | #if defined(AF_UNIX) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4095 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4096 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4097 | #ifdef AF_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4098 | /* Amateur Radio AX.25 */ |
| 4099 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4100 | #endif |
| 4101 | #ifdef AF_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4102 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4103 | #endif |
| 4104 | #ifdef AF_APPLETALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4105 | /* Appletalk DDP */ |
| 4106 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4107 | #endif |
| 4108 | #ifdef AF_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4109 | /* Amateur radio NetROM */ |
| 4110 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4111 | #endif |
| 4112 | #ifdef AF_BRIDGE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4113 | /* Multiprotocol bridge */ |
| 4114 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4115 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4116 | #ifdef AF_ATMPVC |
| 4117 | /* ATM PVCs */ |
| 4118 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
| 4119 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4120 | #ifdef AF_AAL5 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4121 | /* Reserved for Werner's ATM */ |
| 4122 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4123 | #endif |
| 4124 | #ifdef AF_X25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4125 | /* Reserved for X.25 project */ |
| 4126 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4127 | #endif |
| 4128 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4129 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4130 | #endif |
| 4131 | #ifdef AF_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4132 | /* Amateur Radio X.25 PLP */ |
| 4133 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4134 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4135 | #ifdef AF_DECnet |
| 4136 | /* Reserved for DECnet project */ |
| 4137 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
| 4138 | #endif |
| 4139 | #ifdef AF_NETBEUI |
| 4140 | /* Reserved for 802.2LLC project */ |
| 4141 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
| 4142 | #endif |
| 4143 | #ifdef AF_SECURITY |
| 4144 | /* Security callback pseudo AF */ |
| 4145 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
| 4146 | #endif |
| 4147 | #ifdef AF_KEY |
| 4148 | /* PF_KEY key management API */ |
| 4149 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
| 4150 | #endif |
| 4151 | #ifdef AF_NETLINK |
| 4152 | /* */ |
| 4153 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4154 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4155 | #ifdef NETLINK_SKIP |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4156 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4157 | #endif |
| 4158 | #ifdef NETLINK_W1 |
| 4159 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
| 4160 | #endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4161 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4162 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4163 | #ifdef NETLINK_TCPDIAG |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4164 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4165 | #endif |
| 4166 | #ifdef NETLINK_NFLOG |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4167 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4168 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4169 | #ifdef NETLINK_XFRM |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4170 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4171 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4172 | #ifdef NETLINK_ARPD |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4173 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4174 | #endif |
| 4175 | #ifdef NETLINK_ROUTE6 |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4176 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4177 | #endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4178 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4179 | #ifdef NETLINK_DNRTMSG |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4180 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4181 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4182 | #ifdef NETLINK_TAPBASE |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4183 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4184 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4185 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4186 | #ifdef AF_ROUTE |
| 4187 | /* Alias to emulate 4.4BSD */ |
| 4188 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
| 4189 | #endif |
| 4190 | #ifdef AF_ASH |
| 4191 | /* Ash */ |
| 4192 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
| 4193 | #endif |
| 4194 | #ifdef AF_ECONET |
| 4195 | /* Acorn Econet */ |
| 4196 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
| 4197 | #endif |
| 4198 | #ifdef AF_ATMSVC |
| 4199 | /* ATM SVCs */ |
| 4200 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
| 4201 | #endif |
| 4202 | #ifdef AF_SNA |
| 4203 | /* Linux SNA Project (nutters!) */ |
| 4204 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
| 4205 | #endif |
| 4206 | #ifdef AF_IRDA |
| 4207 | /* IRDA sockets */ |
| 4208 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
| 4209 | #endif |
| 4210 | #ifdef AF_PPPOX |
| 4211 | /* PPPoX sockets */ |
| 4212 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
| 4213 | #endif |
| 4214 | #ifdef AF_WANPIPE |
| 4215 | /* Wanpipe API Sockets */ |
| 4216 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
| 4217 | #endif |
| 4218 | #ifdef AF_LLC |
| 4219 | /* Linux LLC */ |
| 4220 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
| 4221 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4222 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4223 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4224 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4225 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4226 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4227 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4228 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4229 | #if !defined(__FreeBSD__) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4230 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
| 4231 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4232 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4233 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4234 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 4235 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4236 | 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] | 4237 | #endif |
| 4238 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 4239 | #ifdef HAVE_NETPACKET_PACKET_H |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4240 | PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); |
| 4241 | PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); |
| 4242 | PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); |
| 4243 | PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 4244 | PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 4245 | PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 4246 | PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 4247 | PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 4248 | PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4249 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4250 | |
| 4251 | /* Socket types */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4252 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4253 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4254 | /* We have incomplete socket support. */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4255 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4256 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4257 | #if defined(SOCK_RDM) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4258 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4259 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4260 | |
| 4261 | #ifdef SO_DEBUG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4262 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4263 | #endif |
| 4264 | #ifdef SO_ACCEPTCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4265 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4266 | #endif |
| 4267 | #ifdef SO_REUSEADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4268 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4269 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4270 | #ifdef SO_EXCLUSIVEADDRUSE |
| 4271 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
| 4272 | #endif |
| 4273 | |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4274 | #ifdef SO_KEEPALIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4275 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4276 | #endif |
| 4277 | #ifdef SO_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4278 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4279 | #endif |
| 4280 | #ifdef SO_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4281 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4282 | #endif |
| 4283 | #ifdef SO_USELOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4284 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4285 | #endif |
| 4286 | #ifdef SO_LINGER |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4287 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4288 | #endif |
| 4289 | #ifdef SO_OOBINLINE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4290 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4291 | #endif |
| 4292 | #ifdef SO_REUSEPORT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4293 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4294 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4295 | #ifdef SO_SNDBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4296 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4297 | #endif |
| 4298 | #ifdef SO_RCVBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4299 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4300 | #endif |
| 4301 | #ifdef SO_SNDLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4302 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4303 | #endif |
| 4304 | #ifdef SO_RCVLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4305 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4306 | #endif |
| 4307 | #ifdef SO_SNDTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4308 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4309 | #endif |
| 4310 | #ifdef SO_RCVTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4311 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4312 | #endif |
| 4313 | #ifdef SO_ERROR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4314 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4315 | #endif |
| 4316 | #ifdef SO_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4317 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4318 | #endif |
| 4319 | |
| 4320 | /* Maximum number of connections for "listen" */ |
| 4321 | #ifdef SOMAXCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4322 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4323 | #else |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4324 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4325 | #endif |
| 4326 | |
| 4327 | /* Flags for send, recv */ |
| 4328 | #ifdef MSG_OOB |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4329 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4330 | #endif |
| 4331 | #ifdef MSG_PEEK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4332 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4333 | #endif |
| 4334 | #ifdef MSG_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4335 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4336 | #endif |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4337 | #ifdef MSG_DONTWAIT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4338 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4339 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4340 | #ifdef MSG_EOR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4341 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4342 | #endif |
| 4343 | #ifdef MSG_TRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4344 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4345 | #endif |
| 4346 | #ifdef MSG_CTRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4347 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4348 | #endif |
| 4349 | #ifdef MSG_WAITALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4350 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4351 | #endif |
| 4352 | #ifdef MSG_BTAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4353 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4354 | #endif |
| 4355 | #ifdef MSG_ETAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4356 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4357 | #endif |
| 4358 | |
| 4359 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4360 | #ifdef SOL_SOCKET |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4361 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4362 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4363 | #ifdef SOL_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4364 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4365 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4366 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4367 | #endif |
| 4368 | #ifdef SOL_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4369 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4370 | #endif |
| 4371 | #ifdef SOL_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4372 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4373 | #endif |
| 4374 | #ifdef SOL_ATALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4375 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4376 | #endif |
| 4377 | #ifdef SOL_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4378 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4379 | #endif |
| 4380 | #ifdef SOL_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4381 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4382 | #endif |
| 4383 | #ifdef SOL_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4384 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4385 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4386 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4387 | #endif |
| 4388 | #ifdef SOL_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4389 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4390 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4391 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4392 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4393 | #ifdef IPPROTO_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4394 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4395 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4396 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4397 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4398 | #ifdef IPPROTO_HOPOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4399 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4400 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4401 | #ifdef IPPROTO_ICMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4402 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4403 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4404 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4405 | #endif |
| 4406 | #ifdef IPPROTO_IGMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4407 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4408 | #endif |
| 4409 | #ifdef IPPROTO_GGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4410 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4411 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4412 | #ifdef IPPROTO_IPV4 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4413 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4414 | #endif |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 4415 | #ifdef IPPROTO_IPV6 |
| 4416 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
| 4417 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4418 | #ifdef IPPROTO_IPIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4419 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4420 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4421 | #ifdef IPPROTO_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4422 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4423 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4424 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4425 | #endif |
| 4426 | #ifdef IPPROTO_EGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4427 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4428 | #endif |
| 4429 | #ifdef IPPROTO_PUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4430 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4431 | #endif |
| 4432 | #ifdef IPPROTO_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4433 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4434 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4435 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4436 | #endif |
| 4437 | #ifdef IPPROTO_IDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4438 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4439 | #endif |
| 4440 | #ifdef IPPROTO_HELLO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4441 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4442 | #endif |
| 4443 | #ifdef IPPROTO_ND |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4444 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4445 | #endif |
| 4446 | #ifdef IPPROTO_TP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4447 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4448 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4449 | #ifdef IPPROTO_IPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4450 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4451 | #endif |
| 4452 | #ifdef IPPROTO_ROUTING |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4453 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4454 | #endif |
| 4455 | #ifdef IPPROTO_FRAGMENT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4456 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4457 | #endif |
| 4458 | #ifdef IPPROTO_RSVP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4459 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4460 | #endif |
| 4461 | #ifdef IPPROTO_GRE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4462 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4463 | #endif |
| 4464 | #ifdef IPPROTO_ESP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4465 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4466 | #endif |
| 4467 | #ifdef IPPROTO_AH |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4468 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4469 | #endif |
| 4470 | #ifdef IPPROTO_MOBILE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4471 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4472 | #endif |
| 4473 | #ifdef IPPROTO_ICMPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4474 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4475 | #endif |
| 4476 | #ifdef IPPROTO_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4477 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4478 | #endif |
| 4479 | #ifdef IPPROTO_DSTOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4480 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4481 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4482 | #ifdef IPPROTO_XTP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4483 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4484 | #endif |
| 4485 | #ifdef IPPROTO_EON |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4486 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4487 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4488 | #ifdef IPPROTO_PIM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4489 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4490 | #endif |
| 4491 | #ifdef IPPROTO_IPCOMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4492 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4493 | #endif |
| 4494 | #ifdef IPPROTO_VRRP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4495 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4496 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4497 | #ifdef IPPROTO_BIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4498 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4499 | #endif |
| 4500 | /**/ |
| 4501 | #ifdef IPPROTO_RAW |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4502 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4503 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4504 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4505 | #endif |
| 4506 | #ifdef IPPROTO_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4507 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4508 | #endif |
| 4509 | |
| 4510 | /* Some port configuration */ |
| 4511 | #ifdef IPPORT_RESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4512 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4513 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4514 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4515 | #endif |
| 4516 | #ifdef IPPORT_USERRESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4517 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4518 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4519 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4520 | #endif |
| 4521 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4522 | /* Some reserved IP v.4 addresses */ |
| 4523 | #ifdef INADDR_ANY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4524 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4525 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4526 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4527 | #endif |
| 4528 | #ifdef INADDR_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4529 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4530 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4531 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4532 | #endif |
| 4533 | #ifdef INADDR_LOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4534 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4535 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4536 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4537 | #endif |
| 4538 | #ifdef INADDR_UNSPEC_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4539 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4540 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4541 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4542 | #endif |
| 4543 | #ifdef INADDR_ALLHOSTS_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4544 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 4545 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4546 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4547 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4548 | #endif |
| 4549 | #ifdef INADDR_MAX_LOCAL_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4550 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 4551 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4552 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4553 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4554 | #endif |
| 4555 | #ifdef INADDR_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4556 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4557 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4558 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4559 | #endif |
| 4560 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4561 | /* IPv4 [gs]etsockopt options */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4562 | #ifdef IP_OPTIONS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4563 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4564 | #endif |
| 4565 | #ifdef IP_HDRINCL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4566 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4567 | #endif |
| 4568 | #ifdef IP_TOS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4569 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4570 | #endif |
| 4571 | #ifdef IP_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4572 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4573 | #endif |
| 4574 | #ifdef IP_RECVOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4575 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4576 | #endif |
| 4577 | #ifdef IP_RECVRETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4578 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4579 | #endif |
| 4580 | #ifdef IP_RECVDSTADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4581 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4582 | #endif |
| 4583 | #ifdef IP_RETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4584 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4585 | #endif |
| 4586 | #ifdef IP_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4587 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4588 | #endif |
| 4589 | #ifdef IP_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4590 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4591 | #endif |
| 4592 | #ifdef IP_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4593 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4594 | #endif |
| 4595 | #ifdef IP_ADD_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4596 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4597 | #endif |
| 4598 | #ifdef IP_DROP_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4599 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4600 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4601 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4602 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 4603 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4604 | #endif |
| 4605 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4606 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 4607 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4608 | #endif |
| 4609 | #ifdef IP_MAX_MEMBERSHIPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4610 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4611 | #endif |
| 4612 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4613 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 4614 | #ifdef IPV6_JOIN_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4615 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4616 | #endif |
| 4617 | #ifdef IPV6_LEAVE_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4618 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4619 | #endif |
| 4620 | #ifdef IPV6_MULTICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4621 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4622 | #endif |
| 4623 | #ifdef IPV6_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4624 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4625 | #endif |
| 4626 | #ifdef IPV6_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4627 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4628 | #endif |
| 4629 | #ifdef IPV6_UNICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4630 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4631 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4632 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
| 4633 | #ifdef IPV6_V6ONLY |
| 4634 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
| 4635 | #endif |
| 4636 | /* Advanced IPV6 socket options, from RFC 3542 */ |
| 4637 | #ifdef IPV6_CHECKSUM |
| 4638 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
| 4639 | #endif |
| 4640 | #ifdef IPV6_DONTFRAG |
| 4641 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
| 4642 | #endif |
| 4643 | #ifdef IPV6_DSTOPTS |
| 4644 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
| 4645 | #endif |
| 4646 | #ifdef IPV6_HOPLIMIT |
| 4647 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
| 4648 | #endif |
| 4649 | #ifdef IPV6_HOPOPTS |
| 4650 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
| 4651 | #endif |
| 4652 | #ifdef IPV6_NEXTHOP |
| 4653 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
| 4654 | #endif |
| 4655 | #ifdef IPV6_PATHMTU |
| 4656 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
| 4657 | #endif |
| 4658 | #ifdef IPV6_PKTINFO |
| 4659 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
| 4660 | #endif |
| 4661 | #ifdef IPV6_RECVDSTOPTS |
| 4662 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
| 4663 | #endif |
| 4664 | #ifdef IPV6_RECVHOPLIMIT |
| 4665 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
| 4666 | #endif |
| 4667 | #ifdef IPV6_RECVHOPOPTS |
| 4668 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
| 4669 | #endif |
| 4670 | #ifdef IPV6_RECVPKTINFO |
| 4671 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
| 4672 | #endif |
| 4673 | #ifdef IPV6_RECVRTHDR |
| 4674 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
| 4675 | #endif |
| 4676 | #ifdef IPV6_RECVTCLASS |
| 4677 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
| 4678 | #endif |
| 4679 | #ifdef IPV6_RTHDR |
| 4680 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
| 4681 | #endif |
| 4682 | #ifdef IPV6_RTHDRDSTOPTS |
| 4683 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
| 4684 | #endif |
| 4685 | #ifdef IPV6_RTHDR_TYPE_0 |
| 4686 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); |
| 4687 | #endif |
| 4688 | #ifdef IPV6_RECVPATHMTU |
| 4689 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
| 4690 | #endif |
| 4691 | #ifdef IPV6_TCLASS |
| 4692 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
| 4693 | #endif |
| 4694 | #ifdef IPV6_USE_MIN_MTU |
| 4695 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); |
| 4696 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4697 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4698 | /* TCP options */ |
| 4699 | #ifdef TCP_NODELAY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4700 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4701 | #endif |
| 4702 | #ifdef TCP_MAXSEG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4703 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4704 | #endif |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4705 | #ifdef TCP_CORK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4706 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4707 | #endif |
| 4708 | #ifdef TCP_KEEPIDLE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4709 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4710 | #endif |
| 4711 | #ifdef TCP_KEEPINTVL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4712 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4713 | #endif |
| 4714 | #ifdef TCP_KEEPCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4715 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4716 | #endif |
| 4717 | #ifdef TCP_SYNCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4718 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4719 | #endif |
| 4720 | #ifdef TCP_LINGER2 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4721 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4722 | #endif |
| 4723 | #ifdef TCP_DEFER_ACCEPT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4724 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4725 | #endif |
| 4726 | #ifdef TCP_WINDOW_CLAMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4727 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4728 | #endif |
| 4729 | #ifdef TCP_INFO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4730 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4731 | #endif |
| 4732 | #ifdef TCP_QUICKACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4733 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4734 | #endif |
| 4735 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4736 | |
| 4737 | /* IPX options */ |
| 4738 | #ifdef IPX_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4739 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4740 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4741 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4742 | /* get{addr,name}info parameters */ |
| 4743 | #ifdef EAI_ADDRFAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4744 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4745 | #endif |
| 4746 | #ifdef EAI_AGAIN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4747 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4748 | #endif |
| 4749 | #ifdef EAI_BADFLAGS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4750 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4751 | #endif |
| 4752 | #ifdef EAI_FAIL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4753 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4754 | #endif |
| 4755 | #ifdef EAI_FAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4756 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4757 | #endif |
| 4758 | #ifdef EAI_MEMORY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4759 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4760 | #endif |
| 4761 | #ifdef EAI_NODATA |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4762 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4763 | #endif |
| 4764 | #ifdef EAI_NONAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4765 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4766 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4767 | #ifdef EAI_OVERFLOW |
| 4768 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
| 4769 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4770 | #ifdef EAI_SERVICE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4771 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4772 | #endif |
| 4773 | #ifdef EAI_SOCKTYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4774 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4775 | #endif |
| 4776 | #ifdef EAI_SYSTEM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4777 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4778 | #endif |
| 4779 | #ifdef EAI_BADHINTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4780 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4781 | #endif |
| 4782 | #ifdef EAI_PROTOCOL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4783 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4784 | #endif |
| 4785 | #ifdef EAI_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4786 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4787 | #endif |
| 4788 | #ifdef AI_PASSIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4789 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4790 | #endif |
| 4791 | #ifdef AI_CANONNAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4792 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4793 | #endif |
| 4794 | #ifdef AI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4795 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4796 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4797 | #ifdef AI_NUMERICSERV |
| 4798 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
| 4799 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4800 | #ifdef AI_MASK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4801 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4802 | #endif |
| 4803 | #ifdef AI_ALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4804 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4805 | #endif |
| 4806 | #ifdef AI_V4MAPPED_CFG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4807 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4808 | #endif |
| 4809 | #ifdef AI_ADDRCONFIG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4810 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4811 | #endif |
| 4812 | #ifdef AI_V4MAPPED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4813 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4814 | #endif |
| 4815 | #ifdef AI_DEFAULT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4816 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4817 | #endif |
| 4818 | #ifdef NI_MAXHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4819 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4820 | #endif |
| 4821 | #ifdef NI_MAXSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4822 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4823 | #endif |
| 4824 | #ifdef NI_NOFQDN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4825 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4826 | #endif |
| 4827 | #ifdef NI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4828 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4829 | #endif |
| 4830 | #ifdef NI_NAMEREQD |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4831 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4832 | #endif |
| 4833 | #ifdef NI_NUMERICSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4834 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4835 | #endif |
| 4836 | #ifdef NI_DGRAM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4837 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4838 | #endif |
| 4839 | |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 4840 | /* shutdown() parameters */ |
| 4841 | #ifdef SHUT_RD |
| 4842 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
| 4843 | #elif defined(SD_RECEIVE) |
| 4844 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
| 4845 | #else |
| 4846 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
| 4847 | #endif |
| 4848 | #ifdef SHUT_WR |
| 4849 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
| 4850 | #elif defined(SD_SEND) |
| 4851 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
| 4852 | #else |
| 4853 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
| 4854 | #endif |
| 4855 | #ifdef SHUT_RDWR |
| 4856 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
| 4857 | #elif defined(SD_BOTH) |
| 4858 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
| 4859 | #else |
| 4860 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
| 4861 | #endif |
| 4862 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4863 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 4864 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 4865 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4866 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4867 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4868 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4869 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4870 | #ifndef HAVE_INET_PTON |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4871 | |
| 4872 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4873 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4874 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4875 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4876 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4877 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4878 | if (af == AF_INET) { |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4879 | long packed_addr; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4880 | packed_addr = inet_addr(src); |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4881 | if (packed_addr == INADDR_NONE) |
| 4882 | return 0; |
| 4883 | memcpy(dst, &packed_addr, 4); |
| 4884 | return 1; |
| 4885 | } |
| 4886 | /* Should set errno to EAFNOSUPPORT */ |
| 4887 | return -1; |
| 4888 | } |
| 4889 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 4890 | const char * |
| 4891 | 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] | 4892 | { |
| 4893 | if (af == AF_INET) { |
| 4894 | struct in_addr packed_addr; |
| 4895 | if (size < 16) |
| 4896 | /* Should set errno to ENOSPC. */ |
| 4897 | return NULL; |
| 4898 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 4899 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 4900 | } |
| 4901 | /* Should set errno to EAFNOSUPPORT */ |
| 4902 | return NULL; |
| 4903 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4904 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4905 | #endif |