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 | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 22 | - socket.fromfd(fd, family, type[, proto]) --> new socket object (created |
| 23 | from an existing file descriptor) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 24 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 25 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 26 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
Guido van Rossum | 25405c7 | 1996-12-19 16:42:52 +0000 | [diff] [blame] | 27 | - socket.getprotobyname(protocolname) --> protocol number |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 28 | - socket.getservbyname(servicename[, protocolname]) --> port number |
| 29 | - socket.getservbyport(portnumber[, protocolname]) --> service name |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 30 | - socket.socket([family[, type [, proto]]]) --> new socket object |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 31 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 32 | - socket.ntohs(16 bit value) --> new int object |
| 33 | - socket.ntohl(32 bit value) --> new int object |
| 34 | - socket.htons(16 bit value) --> new int object |
| 35 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 36 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
| 37 | --> List of (family, socktype, proto, canonname, sockaddr) |
| 38 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 39 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 40 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 41 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 42 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 43 | - socket.getdefaulttimeout() -> None | float |
| 44 | - socket.setdefaulttimeout(None | float) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 45 | - an Internet socket address is a pair (hostname, port) |
| 46 | where hostname can be anything recognized by gethostbyname() |
| 47 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 48 | - where a hostname is returned, the dd.dd.dd.dd notation is used |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 49 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 50 | - an AF_PACKET socket address is a tuple containing a string |
| 51 | specifying the ethernet interface and an integer specifying |
| 52 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 53 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 54 | specify packet-type and ha-type/addr. |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 56 | Local naming conventions: |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 58 | - names starting with sock_ are socket object methods |
| 59 | - names starting with socket_ are module-level functions |
| 60 | - names starting with PySocket are exported through socketmodule.h |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 62 | */ |
| 63 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 64 | #ifdef __APPLE__ |
| 65 | /* |
| 66 | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
| 67 | * that was build on 10.4 or later to work on that release, weak linking |
| 68 | * comes to the rescue. |
| 69 | */ |
| 70 | # pragma weak inet_aton |
| 71 | #endif |
| 72 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 73 | #include "Python.h" |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 74 | #include "structmember.h" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 76 | #undef MAX |
| 77 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 78 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 79 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 80 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 81 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 82 | \n\ |
| 83 | Open a socket of the given type. The family argument specifies the\n\ |
| 84 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 85 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 86 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 87 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 88 | \n\ |
| 89 | A socket object represents one endpoint of a network connection.\n\ |
| 90 | \n\ |
| 91 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 92 | \n\ |
| 93 | accept() -- accept a connection, returning new socket and client address\n\ |
| 94 | bind(addr) -- bind the socket to a local address\n\ |
| 95 | close() -- close the socket\n\ |
| 96 | connect(addr) -- connect the socket to a remote address\n\ |
| 97 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
| 98 | dup() -- return a new socket object identical to the current one [*]\n\ |
| 99 | fileno() -- return underlying file descriptor\n\ |
| 100 | getpeername() -- return remote address [*]\n\ |
| 101 | getsockname() -- return local address\n\ |
| 102 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 103 | gettimeout() -- return timeout or None\n\ |
| 104 | listen(n) -- start listening for incoming connections\n\ |
| 105 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\ |
| 106 | recv(buflen[, flags]) -- receive data\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 107 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 108 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 109 | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 110 | -- receive data and sender\'s address (into a buffer)\n\ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 111 | sendall(data[, flags]) -- send all data\n\ |
| 112 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 113 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 114 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 115 | setsockopt(level, optname, value) -- set socket options\n\ |
| 116 | settimeout(None | float) -- set or clear the timeout\n\ |
| 117 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 118 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 119 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 120 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 121 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 122 | I hope some day someone can clean this up please... */ |
| 123 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 124 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 125 | script doesn't get this right, so we hardcode some platform checks below. |
| 126 | On the other hand, not all Linux versions agree, so there the settings |
| 127 | computed by the configure script are needed! */ |
| 128 | |
| 129 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 130 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 131 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 132 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 133 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 135 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 136 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 137 | #endif |
| 138 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 139 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 140 | # if defined(_AIX) || defined(__osf__) |
| 141 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 142 | # elif defined(__sun) || defined(__sgi) |
| 143 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 144 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 145 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 146 | # else |
| 147 | # undef HAVE_GETHOSTBYNAME_R |
| 148 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 151 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 152 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 153 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 156 | /* To use __FreeBSD_version */ |
| 157 | #ifdef HAVE_SYS_PARAM_H |
| 158 | #include <sys/param.h> |
| 159 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 160 | /* 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] | 161 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Hye-Shik Chang | 9ceebd5 | 2005-09-24 14:58:47 +0000 | [diff] [blame] | 162 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \ |
| 163 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 164 | defined(__OpenBSD__) || defined(__NetBSD__) || \ |
| 165 | defined(__VMS) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 166 | #define USE_GETADDRINFO_LOCK |
| 167 | #endif |
| 168 | |
| 169 | #ifdef USE_GETADDRINFO_LOCK |
| 170 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 171 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 172 | #else |
| 173 | #define ACQUIRE_GETADDRINFO_LOCK |
| 174 | #define RELEASE_GETADDRINFO_LOCK |
| 175 | #endif |
| 176 | |
| 177 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 178 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 179 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 181 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 182 | # include <types.h> |
| 183 | # include <io.h> |
| 184 | # include <sys/ioctl.h> |
| 185 | # include <utils.h> |
| 186 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 187 | #endif |
| 188 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 189 | #if defined(__VMS) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 190 | # include <ioctl.h> |
| 191 | #endif |
| 192 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 193 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 194 | # define INCL_DOS |
| 195 | # define INCL_DOSERRORS |
| 196 | # define INCL_NOPMAPI |
| 197 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 198 | #endif |
| 199 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 200 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 201 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 202 | functions are declared correctly if compiling with |
| 203 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 204 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 205 | /* XXX Using _SGIAPI is the wrong thing, |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 206 | but I don't know what the right thing is. */ |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 207 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 208 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 209 | |
Trent Mick | 8ea5bdf | 2004-09-13 17:48:41 +0000 | [diff] [blame] | 210 | #undef _XOPEN_SOURCE |
| 211 | #include <sys/socket.h> |
| 212 | #include <sys/types.h> |
| 213 | #include <netinet/in.h> |
| 214 | #ifdef _SS_ALIGNSIZE |
| 215 | #define HAVE_GETADDRINFO 1 |
| 216 | #define HAVE_GETNAMEINFO 1 |
| 217 | #endif |
| 218 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 219 | #define HAVE_INET_PTON |
| 220 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 221 | #endif |
| 222 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 223 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 224 | 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] | 225 | are just busted. Same thing for Solaris. */ |
| 226 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 227 | #define INET_ADDRSTRLEN 16 |
| 228 | #endif |
| 229 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 230 | /* Generic includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 231 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 232 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 233 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 234 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 235 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 236 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 237 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 238 | |
| 239 | /* Addressing includes */ |
| 240 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 241 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 242 | |
| 243 | /* Non-MS WINDOWS includes */ |
| 244 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 246 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 247 | # ifdef __BEOS__ |
| 248 | # include <net/netdb.h> |
| 249 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 250 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 251 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 252 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 253 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 254 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 255 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 256 | # ifndef RISCOS |
| 257 | # include <fcntl.h> |
| 258 | # else |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 259 | # include <sys/ioctl.h> |
| 260 | # include <socklib.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 261 | # define NO_DUP |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 262 | int h_errno; /* not used */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 263 | # define INET_ADDRSTRLEN 16 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 264 | # endif |
| 265 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 266 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 267 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 268 | /* MS_WINDOWS includes */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 269 | # ifdef HAVE_FCNTL_H |
| 270 | # include <fcntl.h> |
| 271 | # endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 272 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 273 | #endif |
| 274 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 275 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 276 | |
| 277 | #ifndef offsetof |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 278 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 279 | #endif |
| 280 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 281 | #ifndef O_NONBLOCK |
| 282 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 283 | #endif |
| 284 | |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 285 | /* include Python's addrinfo.h unless it causes trouble */ |
| 286 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
| 287 | /* Do not include addinfo.h on some newer IRIX versions. |
| 288 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
| 289 | * for example, but not by 6.5.10. |
| 290 | */ |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 291 | #elif defined(_MSC_VER) && _MSC_VER>1201 |
Trent Mick | a708d6e | 2004-09-07 17:48:26 +0000 | [diff] [blame] | 292 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
| 293 | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
| 294 | */ |
| 295 | #else |
| 296 | # include "addrinfo.h" |
| 297 | #endif |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 298 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 299 | #ifndef HAVE_INET_PTON |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 300 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 301 | 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] | 302 | #endif |
| 303 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 304 | #ifdef __APPLE__ |
| 305 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 306 | failure, so we must use the emulation instead of the libinfo |
| 307 | implementation. Unfortunately, performing an autoconf test |
| 308 | for this bug would require DNS access for the machine performing |
| 309 | the configuration, which is not acceptable. Therefore, we |
| 310 | determine the bug just by checking for __APPLE__. If this bug |
| 311 | gets ever fixed, perhaps checking for sys/version.h would be |
| 312 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 313 | #ifndef HAVE_GETNAMEINFO |
| 314 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 315 | Find to check for Jaguar is that it has getnameinfo(), which |
| 316 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 317 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 318 | #endif |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 319 | |
| 320 | #ifdef HAVE_INET_ATON |
| 321 | #define USE_INET_ATON_WEAKLINK |
| 322 | #endif |
| 323 | |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 324 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 325 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 326 | /* 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] | 327 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 328 | /* avoid clashes with the C library definition of the symbol. */ |
| 329 | #define getaddrinfo fake_getaddrinfo |
| 330 | #define gai_strerror fake_gai_strerror |
| 331 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 332 | #include "getaddrinfo.c" |
| 333 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 334 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 335 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 336 | #include "getnameinfo.c" |
| 337 | #endif |
| 338 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 339 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 340 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 341 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 342 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 343 | #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] | 344 | #endif |
| 345 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 346 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 347 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 348 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 349 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 350 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 351 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 352 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 353 | #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] | 354 | #endif |
| 355 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 356 | #ifndef SOCKETCLOSE |
| 357 | #define SOCKETCLOSE close |
| 358 | #endif |
| 359 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 360 | #if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) |
| 361 | #define USE_BLUETOOTH 1 |
| 362 | #if defined(__FreeBSD__) |
| 363 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 364 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 365 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 366 | #define sockaddr_l2 sockaddr_l2cap |
| 367 | #define sockaddr_rc sockaddr_rfcomm |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 368 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 369 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 370 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 371 | #elif defined(__NetBSD__) |
| 372 | #define sockaddr_l2 sockaddr_bt |
| 373 | #define sockaddr_rc sockaddr_bt |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 374 | #define sockaddr_hci sockaddr_bt |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 375 | #define sockaddr_sco sockaddr_bt |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 376 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
| 377 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 378 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
Matt Fleming | ec92650 | 2006-07-28 11:27:27 +0000 | [diff] [blame] | 379 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 380 | #else |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 381 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 382 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 383 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 384 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 385 | #endif |
| 386 | #endif |
| 387 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 388 | #ifdef __VMS |
| 389 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 390 | #define SEGMENT_SIZE (32 * 1024 -1) |
| 391 | #endif |
| 392 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 393 | #define SAS2SA(x) ((struct sockaddr *)(x)) |
| 394 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 395 | /* |
| 396 | * Constants for getnameinfo() |
| 397 | */ |
| 398 | #if !defined(NI_MAXHOST) |
| 399 | #define NI_MAXHOST 1025 |
| 400 | #endif |
| 401 | #if !defined(NI_MAXSERV) |
| 402 | #define NI_MAXSERV 32 |
| 403 | #endif |
| 404 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 405 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 406 | a Py prefix (or use CapitalizedWords). Later... */ |
| 407 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 408 | /* Global variable holding the exception type for errors detected |
| 409 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 410 | static PyObject *socket_error; |
| 411 | static PyObject *socket_herror; |
| 412 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 413 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 415 | #ifdef RISCOS |
| 416 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 417 | static int taskwindow; |
| 418 | #endif |
| 419 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 420 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 421 | The sock_type variable contains pointers to various functions, |
| 422 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 423 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 424 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 425 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 426 | #if defined(HAVE_POLL_H) |
| 427 | #include <poll.h> |
| 428 | #elif defined(HAVE_SYS_POLL_H) |
| 429 | #include <sys/poll.h> |
| 430 | #endif |
| 431 | |
Martin v. Löwis | f84d1b9 | 2006-02-11 09:27:05 +0000 | [diff] [blame] | 432 | #ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE |
| 433 | /* Platform can select file descriptors beyond FD_SETSIZE */ |
| 434 | #define IS_SELECTABLE(s) 1 |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 435 | #elif defined(HAVE_POLL) |
| 436 | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
| 437 | #define IS_SELECTABLE(s) 1 |
| 438 | /* 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] | 439 | #else |
| 440 | /* POSIX says selecting file descriptors beyond FD_SETSIZE |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 441 | has undefined behaviour. If there's no timeout left, we don't have to |
| 442 | call select, so it's a safe, little white lie. */ |
| 443 | #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] | 444 | #endif |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 445 | |
| 446 | static PyObject* |
| 447 | select_error(void) |
| 448 | { |
| 449 | PyErr_SetString(socket_error, "unable to select on socket"); |
| 450 | return NULL; |
| 451 | } |
| 452 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 453 | /* Convenience function to raise an error according to errno |
| 454 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 455 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 456 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 457 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 458 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 459 | #ifdef MS_WINDOWS |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 460 | int err_no = WSAGetLastError(); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 461 | static struct { |
| 462 | int no; |
| 463 | const char *msg; |
| 464 | } *msgp, msgs[] = { |
| 465 | {WSAEINTR, "Interrupted system call"}, |
| 466 | {WSAEBADF, "Bad file descriptor"}, |
| 467 | {WSAEACCES, "Permission denied"}, |
| 468 | {WSAEFAULT, "Bad address"}, |
| 469 | {WSAEINVAL, "Invalid argument"}, |
| 470 | {WSAEMFILE, "Too many open files"}, |
| 471 | {WSAEWOULDBLOCK, |
| 472 | "The socket operation could not complete " |
| 473 | "without blocking"}, |
| 474 | {WSAEINPROGRESS, "Operation now in progress"}, |
| 475 | {WSAEALREADY, "Operation already in progress"}, |
| 476 | {WSAENOTSOCK, "Socket operation on non-socket"}, |
| 477 | {WSAEDESTADDRREQ, "Destination address required"}, |
| 478 | {WSAEMSGSIZE, "Message too long"}, |
| 479 | {WSAEPROTOTYPE, "Protocol wrong type for socket"}, |
| 480 | {WSAENOPROTOOPT, "Protocol not available"}, |
| 481 | {WSAEPROTONOSUPPORT, "Protocol not supported"}, |
| 482 | {WSAESOCKTNOSUPPORT, "Socket type not supported"}, |
| 483 | {WSAEOPNOTSUPP, "Operation not supported"}, |
| 484 | {WSAEPFNOSUPPORT, "Protocol family not supported"}, |
| 485 | {WSAEAFNOSUPPORT, "Address family not supported"}, |
| 486 | {WSAEADDRINUSE, "Address already in use"}, |
| 487 | {WSAEADDRNOTAVAIL, "Can't assign requested address"}, |
| 488 | {WSAENETDOWN, "Network is down"}, |
| 489 | {WSAENETUNREACH, "Network is unreachable"}, |
| 490 | {WSAENETRESET, "Network dropped connection on reset"}, |
| 491 | {WSAECONNABORTED, "Software caused connection abort"}, |
| 492 | {WSAECONNRESET, "Connection reset by peer"}, |
| 493 | {WSAENOBUFS, "No buffer space available"}, |
| 494 | {WSAEISCONN, "Socket is already connected"}, |
| 495 | {WSAENOTCONN, "Socket is not connected"}, |
| 496 | {WSAESHUTDOWN, "Can't send after socket shutdown"}, |
| 497 | {WSAETOOMANYREFS, "Too many references: can't splice"}, |
| 498 | {WSAETIMEDOUT, "Operation timed out"}, |
| 499 | {WSAECONNREFUSED, "Connection refused"}, |
| 500 | {WSAELOOP, "Too many levels of symbolic links"}, |
| 501 | {WSAENAMETOOLONG, "File name too long"}, |
| 502 | {WSAEHOSTDOWN, "Host is down"}, |
| 503 | {WSAEHOSTUNREACH, "No route to host"}, |
| 504 | {WSAENOTEMPTY, "Directory not empty"}, |
| 505 | {WSAEPROCLIM, "Too many processes"}, |
| 506 | {WSAEUSERS, "Too many users"}, |
| 507 | {WSAEDQUOT, "Disc quota exceeded"}, |
| 508 | {WSAESTALE, "Stale NFS file handle"}, |
| 509 | {WSAEREMOTE, "Too many levels of remote in path"}, |
| 510 | {WSASYSNOTREADY, "Network subsystem is unvailable"}, |
| 511 | {WSAVERNOTSUPPORTED, "WinSock version is not supported"}, |
| 512 | {WSANOTINITIALISED, |
| 513 | "Successful WSAStartup() not yet performed"}, |
| 514 | {WSAEDISCON, "Graceful shutdown in progress"}, |
| 515 | /* Resolver errors */ |
| 516 | {WSAHOST_NOT_FOUND, "No such host is known"}, |
| 517 | {WSATRY_AGAIN, "Host not found, or server failed"}, |
| 518 | {WSANO_RECOVERY, "Unexpected server error encountered"}, |
| 519 | {WSANO_DATA, "Valid name without requested data"}, |
| 520 | {WSANO_ADDRESS, "No address, look for MX record"}, |
| 521 | {0, NULL} |
| 522 | }; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 523 | if (err_no) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 524 | PyObject *v; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 525 | const char *msg = "winsock error"; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 526 | |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 527 | for (msgp = msgs; msgp->msg; msgp++) { |
| 528 | if (err_no == msgp->no) { |
| 529 | msg = msgp->msg; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | v = Py_BuildValue("(is)", err_no, msg); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 535 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 536 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 537 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 538 | } |
| 539 | return NULL; |
| 540 | } |
| 541 | else |
| 542 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 543 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 544 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 545 | if (sock_errno() != NO_ERROR) { |
| 546 | APIRET rc; |
| 547 | ULONG msglen; |
| 548 | char outbuf[100]; |
| 549 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 550 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 551 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 552 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 553 | myerrorcode - SOCBASEERR + 26, |
| 554 | "mptn.msg", |
| 555 | &msglen); |
| 556 | if (rc == NO_ERROR) { |
| 557 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 558 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 559 | /* OS/2 doesn't guarantee a terminator */ |
| 560 | outbuf[msglen] = '\0'; |
| 561 | if (strlen(outbuf) > 0) { |
| 562 | /* If non-empty msg, trim CRLF */ |
| 563 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 564 | while (lastc > outbuf && |
| 565 | isspace(Py_CHARMASK(*lastc))) { |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 566 | /* Trim trailing whitespace (CRLF) */ |
| 567 | *lastc-- = '\0'; |
| 568 | } |
| 569 | } |
| 570 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 571 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 572 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 573 | Py_DECREF(v); |
| 574 | } |
| 575 | return NULL; |
| 576 | } |
| 577 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 578 | #endif |
| 579 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 580 | #if defined(RISCOS) |
| 581 | if (_inet_error.errnum != NULL) { |
| 582 | PyObject *v; |
| 583 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 584 | if (v != NULL) { |
| 585 | PyErr_SetObject(socket_error, v); |
| 586 | Py_DECREF(v); |
| 587 | } |
| 588 | return NULL; |
| 589 | } |
| 590 | #endif |
| 591 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 592 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 595 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 596 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 597 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 598 | { |
| 599 | PyObject *v; |
| 600 | |
| 601 | #ifdef HAVE_HSTRERROR |
| 602 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
| 603 | #else |
| 604 | v = Py_BuildValue("(is)", h_error, "host not found"); |
| 605 | #endif |
| 606 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 607 | PyErr_SetObject(socket_herror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 608 | Py_DECREF(v); |
| 609 | } |
| 610 | |
| 611 | return NULL; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 616 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 617 | { |
| 618 | PyObject *v; |
| 619 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 620 | #ifdef EAI_SYSTEM |
| 621 | /* EAI_SYSTEM is not available on Windows XP. */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 622 | if (error == EAI_SYSTEM) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 623 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 624 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 625 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 626 | #ifdef HAVE_GAI_STRERROR |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 627 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 628 | #else |
| 629 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
| 630 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 631 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 632 | PyErr_SetObject(socket_gaierror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 633 | Py_DECREF(v); |
| 634 | } |
| 635 | |
| 636 | return NULL; |
| 637 | } |
| 638 | |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 639 | #ifdef __VMS |
| 640 | /* Function to send in segments */ |
| 641 | static int |
| 642 | sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 643 | { |
| 644 | int n = 0; |
| 645 | int remaining = len; |
| 646 | |
| 647 | while (remaining > 0) { |
| 648 | unsigned int segment; |
| 649 | |
| 650 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 651 | n = send(sock_fd, buf, segment, flags); |
| 652 | if (n < 0) { |
| 653 | return n; |
| 654 | } |
| 655 | remaining -= segment; |
| 656 | buf += segment; |
| 657 | } /* end while */ |
| 658 | |
| 659 | return len; |
| 660 | } |
| 661 | #endif |
| 662 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 663 | /* Function to perform the setting of socket blocking mode |
| 664 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 665 | static int |
| 666 | internal_setblocking(PySocketSockObject *s, int block) |
| 667 | { |
| 668 | #ifndef RISCOS |
| 669 | #ifndef MS_WINDOWS |
| 670 | int delay_flag; |
| 671 | #endif |
| 672 | #endif |
| 673 | |
| 674 | Py_BEGIN_ALLOW_THREADS |
| 675 | #ifdef __BEOS__ |
| 676 | block = !block; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 677 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 678 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 679 | #else |
| 680 | #ifndef RISCOS |
| 681 | #ifndef MS_WINDOWS |
| 682 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 683 | block = !block; |
| 684 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 685 | #elif defined(__VMS) |
| 686 | block = !block; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 687 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
| 688 | #else /* !PYOS_OS2 && !__VMS */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 689 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 690 | if (block) |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 691 | delay_flag &= (~O_NONBLOCK); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 692 | else |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 693 | delay_flag |= O_NONBLOCK; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 694 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
| 695 | #endif /* !PYOS_OS2 */ |
| 696 | #else /* MS_WINDOWS */ |
| 697 | block = !block; |
| 698 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
| 699 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 700 | #else /* RISCOS */ |
| 701 | block = !block; |
| 702 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 703 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 704 | #endif /* __BEOS__ */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 705 | Py_END_ALLOW_THREADS |
| 706 | |
| 707 | /* Since these don't return anything */ |
| 708 | return 1; |
| 709 | } |
| 710 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 711 | /* 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] | 712 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 713 | This does not raise an exception; we'll let our caller do that |
| 714 | after they've reacquired the interpreter lock. |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 715 | Returns 1 on timeout, -1 on error, 0 otherwise. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 716 | static int |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 717 | internal_select(PySocketSockObject *s, int writing) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 718 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 719 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 720 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 721 | /* 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] | 722 | if (s->sock_timeout <= 0.0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 723 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 724 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 725 | /* Guard against closed socket */ |
| 726 | if (s->sock_fd < 0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 727 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 728 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 729 | /* Prefer poll, if available, since you can poll() any fd |
| 730 | * which can't be done with select(). */ |
| 731 | #ifdef HAVE_POLL |
| 732 | { |
| 733 | struct pollfd pollfd; |
| 734 | int timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 735 | |
Anthony Baxter | 93ab5fa | 2006-07-11 02:04:09 +0000 | [diff] [blame] | 736 | pollfd.fd = s->sock_fd; |
| 737 | pollfd.events = writing ? POLLOUT : POLLIN; |
| 738 | |
| 739 | /* s->sock_timeout is in seconds, timeout in ms */ |
| 740 | timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 741 | n = poll(&pollfd, 1, timeout); |
| 742 | } |
| 743 | #else |
| 744 | { |
| 745 | /* Construct the arguments to select */ |
| 746 | fd_set fds; |
| 747 | struct timeval tv; |
| 748 | tv.tv_sec = (int)s->sock_timeout; |
| 749 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 750 | FD_ZERO(&fds); |
| 751 | FD_SET(s->sock_fd, &fds); |
| 752 | |
| 753 | /* See if the socket is ready */ |
| 754 | if (writing) |
| 755 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 756 | else |
| 757 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 758 | } |
| 759 | #endif |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 760 | |
| 761 | if (n < 0) |
| 762 | return -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 763 | if (n == 0) |
| 764 | return 1; |
| 765 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 768 | /* Initialize a new socket object. */ |
| 769 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 770 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 771 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 772 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 773 | init_sockobject(PySocketSockObject *s, |
| 774 | SOCKET_T fd, int family, int type, int proto) |
| 775 | { |
| 776 | #ifdef RISCOS |
| 777 | int block = 1; |
| 778 | #endif |
| 779 | s->sock_fd = fd; |
| 780 | s->sock_family = family; |
| 781 | s->sock_type = type; |
| 782 | s->sock_proto = proto; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 783 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 785 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 786 | |
| 787 | if (defaulttimeout >= 0.0) |
| 788 | internal_setblocking(s, 0); |
| 789 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 790 | #ifdef RISCOS |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 791 | if (taskwindow) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 792 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 793 | #endif |
| 794 | } |
| 795 | |
| 796 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 797 | /* Create a new socket object. |
| 798 | This just creates the object and initializes it. |
| 799 | If the creation fails, return NULL and set an exception (implicit |
| 800 | in NEWOBJ()). */ |
| 801 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 802 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 803 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 804 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 805 | PySocketSockObject *s; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 806 | s = (PySocketSockObject *) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 807 | PyType_GenericNew(&sock_type, NULL, NULL); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 808 | if (s != NULL) |
| 809 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 810 | return s; |
| 811 | } |
| 812 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 813 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 814 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 815 | thread to be in gethostbyname or getaddrinfo */ |
| 816 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 817 | PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 818 | #endif |
| 819 | |
| 820 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 821 | /* Convert a string specifying a host name or one of a few symbolic |
| 822 | names to a numeric IP address. This usually calls gethostbyname() |
| 823 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 824 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 825 | an error occurred; then an exception is raised. */ |
| 826 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 827 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 828 | 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] | 829 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 830 | struct addrinfo hints, *res; |
| 831 | int error; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 832 | int d1, d2, d3, d4; |
| 833 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 834 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 835 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 836 | if (name[0] == '\0') { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 837 | int siz; |
| 838 | memset(&hints, 0, sizeof(hints)); |
| 839 | hints.ai_family = af; |
| 840 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 841 | hints.ai_flags = AI_PASSIVE; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 842 | Py_BEGIN_ALLOW_THREADS |
| 843 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 844 | error = getaddrinfo(NULL, "0", &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 845 | Py_END_ALLOW_THREADS |
| 846 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 847 | *are* safe regarding their return value, ie. that a |
| 848 | subsequent call to getaddrinfo() does not destroy the |
| 849 | outcome of the first call. */ |
| 850 | RELEASE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 851 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 852 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 853 | return -1; |
| 854 | } |
| 855 | switch (res->ai_family) { |
| 856 | case AF_INET: |
| 857 | siz = 4; |
| 858 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 859 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 860 | case AF_INET6: |
| 861 | siz = 16; |
| 862 | break; |
| 863 | #endif |
| 864 | default: |
| 865 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 866 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 867 | "unsupported address family"); |
| 868 | return -1; |
| 869 | } |
| 870 | if (res->ai_next) { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 871 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 872 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 873 | "wildcard resolved to multiple address"); |
| 874 | return -1; |
| 875 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 876 | if (res->ai_addrlen < addr_ret_size) |
| 877 | addr_ret_size = res->ai_addrlen; |
| 878 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 879 | freeaddrinfo(res); |
| 880 | return siz; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 881 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 882 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 883 | struct sockaddr_in *sin; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 884 | if (af != AF_INET && af != AF_UNSPEC) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 885 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 886 | "address family mismatched"); |
| 887 | return -1; |
| 888 | } |
| 889 | sin = (struct sockaddr_in *)addr_ret; |
| 890 | memset((void *) sin, '\0', sizeof(*sin)); |
| 891 | sin->sin_family = AF_INET; |
| 892 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 893 | sin->sin_len = sizeof(*sin); |
| 894 | #endif |
| 895 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 896 | return sizeof(sin->sin_addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 897 | } |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 898 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 899 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 900 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 901 | struct sockaddr_in *sin; |
| 902 | sin = (struct sockaddr_in *)addr_ret; |
| 903 | sin->sin_addr.s_addr = htonl( |
| 904 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 905 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 906 | sin->sin_family = AF_INET; |
| 907 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 908 | sin->sin_len = sizeof(*sin); |
| 909 | #endif |
| 910 | return 4; |
| 911 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 912 | memset(&hints, 0, sizeof(hints)); |
| 913 | hints.ai_family = af; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 914 | Py_BEGIN_ALLOW_THREADS |
| 915 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 916 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 917 | #if defined(__digital__) && defined(__unix__) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 918 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 919 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 920 | if no address family is given. Assume IPv4 for now.*/ |
| 921 | hints.ai_family = AF_INET; |
| 922 | error = getaddrinfo(name, NULL, &hints, &res); |
| 923 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 924 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 925 | Py_END_ALLOW_THREADS |
| 926 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 927 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 928 | set_gaierror(error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 929 | return -1; |
| 930 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 931 | if (res->ai_addrlen < addr_ret_size) |
| 932 | addr_ret_size = res->ai_addrlen; |
| 933 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 934 | freeaddrinfo(res); |
| 935 | switch (addr_ret->sa_family) { |
| 936 | case AF_INET: |
| 937 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 938 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 939 | case AF_INET6: |
| 940 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 941 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 942 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 943 | PyErr_SetString(socket_error, "unknown address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 944 | return -1; |
| 945 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 948 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 949 | /* Create a string object representing an IP address. |
| 950 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 951 | size numbers). */ |
| 952 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 953 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 954 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 955 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 956 | char buf[NI_MAXHOST]; |
| 957 | int error; |
| 958 | |
| 959 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 960 | NI_NUMERICHOST); |
| 961 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 962 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 963 | return NULL; |
| 964 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 965 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 969 | #ifdef USE_BLUETOOTH |
| 970 | /* Convert a string representation of a Bluetooth address into a numeric |
| 971 | address. Returns the length (6), or raises an exception and returns -1 if |
| 972 | an error occurred. */ |
| 973 | |
| 974 | static int |
| 975 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 976 | { |
| 977 | unsigned int b0, b1, b2, b3, b4, b5; |
| 978 | char ch; |
| 979 | int n; |
| 980 | |
| 981 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 982 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 983 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 984 | bdaddr->b[0] = b0; |
| 985 | bdaddr->b[1] = b1; |
| 986 | bdaddr->b[2] = b2; |
| 987 | bdaddr->b[3] = b3; |
| 988 | bdaddr->b[4] = b4; |
| 989 | bdaddr->b[5] = b5; |
| 990 | return 6; |
| 991 | } else { |
| 992 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 993 | return -1; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /* Create a string representation of the Bluetooth address. This is always a |
| 998 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 999 | value (zero padded if necessary). */ |
| 1000 | |
| 1001 | static PyObject * |
| 1002 | makebdaddr(bdaddr_t *bdaddr) |
| 1003 | { |
| 1004 | char buf[(6 * 2) + 5 + 1]; |
| 1005 | |
| 1006 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1007 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 1008 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 1009 | return PyString_FromString(buf); |
| 1010 | } |
| 1011 | #endif |
| 1012 | |
| 1013 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1014 | /* Create an object representing the given socket address, |
| 1015 | suitable for passing it back to bind(), connect() etc. |
| 1016 | The family field of the sockaddr structure is inspected |
| 1017 | to determine what kind of address it really is. */ |
| 1018 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1019 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1020 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1021 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1022 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1023 | if (addrlen == 0) { |
| 1024 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1025 | Py_INCREF(Py_None); |
| 1026 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1029 | #ifdef __BEOS__ |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 1030 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1031 | addr->sa_family = AF_INET; |
| 1032 | #endif |
| 1033 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1034 | switch (addr->sa_family) { |
| 1035 | |
| 1036 | case AF_INET: |
| 1037 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1038 | struct sockaddr_in *a; |
| 1039 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1040 | PyObject *ret = NULL; |
| 1041 | if (addrobj) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1042 | a = (struct sockaddr_in *)addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1043 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 1044 | Py_DECREF(addrobj); |
| 1045 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 1046 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1047 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1048 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1049 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1050 | case AF_UNIX: |
| 1051 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1052 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1053 | #ifdef linux |
| 1054 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 1055 | addrlen -= (sizeof(*a) - sizeof(a->sun_path)); |
| 1056 | return PyString_FromStringAndSize(a->sun_path, |
| 1057 | addrlen); |
| 1058 | } |
| 1059 | else |
| 1060 | #endif /* linux */ |
| 1061 | { |
| 1062 | /* regular NULL-terminated string */ |
| 1063 | return PyString_FromString(a->sun_path); |
| 1064 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1065 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1066 | #endif /* AF_UNIX */ |
| 1067 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1068 | #if defined(AF_NETLINK) |
| 1069 | case AF_NETLINK: |
| 1070 | { |
| 1071 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
Martin v. Löwis | d056005 | 2006-01-15 07:49:20 +0000 | [diff] [blame] | 1072 | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1073 | } |
| 1074 | #endif /* AF_NETLINK */ |
| 1075 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1076 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1077 | case AF_INET6: |
| 1078 | { |
| 1079 | struct sockaddr_in6 *a; |
| 1080 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 1081 | PyObject *ret = NULL; |
| 1082 | if (addrobj) { |
| 1083 | a = (struct sockaddr_in6 *)addr; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1084 | ret = Py_BuildValue("Oiii", |
| 1085 | addrobj, |
| 1086 | ntohs(a->sin6_port), |
| 1087 | a->sin6_flowinfo, |
| 1088 | a->sin6_scope_id); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1089 | Py_DECREF(addrobj); |
| 1090 | } |
| 1091 | return ret; |
| 1092 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1093 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1094 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1095 | #ifdef USE_BLUETOOTH |
| 1096 | case AF_BLUETOOTH: |
| 1097 | switch (proto) { |
| 1098 | |
| 1099 | case BTPROTO_L2CAP: |
| 1100 | { |
| 1101 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1102 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1103 | PyObject *ret = NULL; |
| 1104 | if (addrobj) { |
| 1105 | ret = Py_BuildValue("Oi", |
| 1106 | addrobj, |
| 1107 | _BT_L2_MEMB(a, psm)); |
| 1108 | Py_DECREF(addrobj); |
| 1109 | } |
| 1110 | return ret; |
| 1111 | } |
| 1112 | |
| 1113 | case BTPROTO_RFCOMM: |
| 1114 | { |
| 1115 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1116 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1117 | PyObject *ret = NULL; |
| 1118 | if (addrobj) { |
| 1119 | ret = Py_BuildValue("Oi", |
| 1120 | addrobj, |
| 1121 | _BT_RC_MEMB(a, channel)); |
| 1122 | Py_DECREF(addrobj); |
| 1123 | } |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1127 | case BTPROTO_HCI: |
| 1128 | { |
| 1129 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
| 1130 | PyObject *ret = NULL; |
| 1131 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1135 | #if !defined(__FreeBSD__) |
| 1136 | case BTPROTO_SCO: |
| 1137 | { |
| 1138 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1139 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1140 | } |
| 1141 | #endif |
| 1142 | |
| 1143 | } |
| 1144 | #endif |
| 1145 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1146 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1147 | case AF_PACKET: |
| 1148 | { |
| 1149 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1150 | char *ifname = ""; |
| 1151 | struct ifreq ifr; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1152 | /* need to look up interface name give index */ |
| 1153 | if (a->sll_ifindex) { |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1154 | ifr.ifr_ifindex = a->sll_ifindex; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1155 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1156 | ifname = ifr.ifr_name; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1157 | } |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1158 | return Py_BuildValue("shbhs#", |
| 1159 | ifname, |
| 1160 | ntohs(a->sll_protocol), |
| 1161 | a->sll_pkttype, |
| 1162 | a->sll_hatype, |
| 1163 | a->sll_addr, |
| 1164 | a->sll_halen); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1165 | } |
| 1166 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1167 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1168 | /* More cases here... */ |
| 1169 | |
| 1170 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 1171 | /* If we don't know the address family, don't raise an |
| 1172 | exception -- return it as a tuple. */ |
| 1173 | return Py_BuildValue("is#", |
| 1174 | addr->sa_family, |
| 1175 | addr->sa_data, |
| 1176 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1177 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1178 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* Parse a socket address argument according to the socket object's |
| 1183 | address family. Return 1 if the address was in the proper format, |
| 1184 | 0 of not. The address is returned through addr_ret, its length |
| 1185 | through len_ret. */ |
| 1186 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1187 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1188 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1189 | struct sockaddr *addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1190 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1191 | switch (s->sock_family) { |
| 1192 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1193 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1194 | case AF_UNIX: |
| 1195 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1196 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1197 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1198 | int len; |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 1199 | if (!PyArg_Parse(args, "t#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1200 | return 0; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1201 | |
| 1202 | addr = (struct sockaddr_un*)addr_ret; |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1203 | #ifdef linux |
| 1204 | if (len > 0 && path[0] == 0) { |
| 1205 | /* Linux abstract namespace extension */ |
| 1206 | if (len > sizeof addr->sun_path) { |
| 1207 | PyErr_SetString(socket_error, |
| 1208 | "AF_UNIX path too long"); |
| 1209 | return 0; |
| 1210 | } |
| 1211 | } |
| 1212 | else |
| 1213 | #endif /* linux */ |
| 1214 | { |
| 1215 | /* regular NULL-terminated string */ |
| 1216 | if (len >= sizeof addr->sun_path) { |
| 1217 | PyErr_SetString(socket_error, |
| 1218 | "AF_UNIX path too long"); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | addr->sun_path[len] = 0; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1222 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1223 | addr->sun_family = s->sock_family; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1224 | memcpy(addr->sun_path, path, len); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1225 | #if defined(PYOS_OS2) |
| 1226 | *len_ret = sizeof(*addr); |
| 1227 | #else |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 1228 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1229 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1230 | return 1; |
| 1231 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1232 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1233 | |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1234 | #if defined(AF_NETLINK) |
| 1235 | case AF_NETLINK: |
| 1236 | { |
| 1237 | struct sockaddr_nl* addr; |
| 1238 | int pid, groups; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1239 | addr = (struct sockaddr_nl *)addr_ret; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1240 | if (!PyTuple_Check(args)) { |
| 1241 | PyErr_Format( |
| 1242 | PyExc_TypeError, |
| 1243 | "getsockaddrarg: " |
| 1244 | "AF_NETLINK address must be tuple, not %.500s", |
| 1245 | args->ob_type->tp_name); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1249 | return 0; |
| 1250 | addr->nl_family = AF_NETLINK; |
| 1251 | addr->nl_pid = pid; |
| 1252 | addr->nl_groups = groups; |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1253 | *len_ret = sizeof(*addr); |
| 1254 | return 1; |
| 1255 | } |
| 1256 | #endif |
| 1257 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1258 | case AF_INET: |
| 1259 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1260 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1261 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1262 | int port, result; |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1263 | if (!PyTuple_Check(args)) { |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1264 | PyErr_Format( |
| 1265 | PyExc_TypeError, |
| 1266 | "getsockaddrarg: " |
| 1267 | "AF_INET address must be tuple, not %.500s", |
| 1268 | args->ob_type->tp_name); |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1269 | return 0; |
| 1270 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1271 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1272 | "idna", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1273 | return 0; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1274 | addr=(struct sockaddr_in*)addr_ret; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1275 | result = setipaddr(host, (struct sockaddr *)addr, |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1276 | sizeof(*addr), AF_INET); |
| 1277 | PyMem_Free(host); |
| 1278 | if (result < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1279 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1280 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 1281 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1282 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1283 | return 1; |
| 1284 | } |
| 1285 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1286 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1287 | case AF_INET6: |
| 1288 | { |
| 1289 | struct sockaddr_in6* addr; |
| 1290 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1291 | int port, flowinfo, scope_id, result; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1292 | flowinfo = scope_id = 0; |
Thomas Wouters | 0452049 | 2006-04-25 15:08:10 +0000 | [diff] [blame] | 1293 | if (!PyTuple_Check(args)) { |
| 1294 | PyErr_Format( |
| 1295 | PyExc_TypeError, |
| 1296 | "getsockaddrarg: " |
| 1297 | "AF_INET6 address must be tuple, not %.500s", |
| 1298 | args->ob_type->tp_name); |
| 1299 | return 0; |
| 1300 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1301 | if (!PyArg_ParseTuple(args, "eti|ii", |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1302 | "idna", &host, &port, &flowinfo, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1303 | &scope_id)) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1304 | return 0; |
| 1305 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1306 | addr = (struct sockaddr_in6*)addr_ret; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1307 | result = setipaddr(host, (struct sockaddr *)addr, |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1308 | sizeof(*addr), AF_INET6); |
| 1309 | PyMem_Free(host); |
| 1310 | if (result < 0) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1311 | return 0; |
| 1312 | addr->sin6_family = s->sock_family; |
| 1313 | addr->sin6_port = htons((short)port); |
| 1314 | addr->sin6_flowinfo = flowinfo; |
| 1315 | addr->sin6_scope_id = scope_id; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1316 | *len_ret = sizeof *addr; |
| 1317 | return 1; |
| 1318 | } |
| 1319 | #endif |
| 1320 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1321 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1322 | case AF_BLUETOOTH: |
| 1323 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1324 | switch (s->sock_proto) { |
| 1325 | case BTPROTO_L2CAP: |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1326 | { |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1327 | struct sockaddr_l2 *addr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1328 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1329 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1330 | addr = (struct sockaddr_l2 *)addr_ret; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1331 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1332 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1333 | &_BT_L2_MEMB(addr, psm))) { |
| 1334 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1335 | "wrong format"); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1336 | return 0; |
| 1337 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1338 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1339 | return 0; |
| 1340 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1341 | *len_ret = sizeof *addr; |
| 1342 | return 1; |
| 1343 | } |
| 1344 | case BTPROTO_RFCOMM: |
| 1345 | { |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1346 | struct sockaddr_rc *addr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1347 | char *straddr; |
| 1348 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1349 | addr = (struct sockaddr_rc *)addr_ret; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1350 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1351 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1352 | &_BT_RC_MEMB(addr, channel))) { |
| 1353 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1354 | "wrong format"); |
| 1355 | return 0; |
| 1356 | } |
| 1357 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1358 | return 0; |
| 1359 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1360 | *len_ret = sizeof *addr; |
| 1361 | return 1; |
| 1362 | } |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1363 | case BTPROTO_HCI: |
| 1364 | { |
Martin v. Löwis | f228482 | 2007-02-14 11:30:07 +0000 | [diff] [blame] | 1365 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1366 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1367 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1368 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1369 | "wrong format"); |
| 1370 | return 0; |
| 1371 | } |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1372 | *len_ret = sizeof *addr; |
| 1373 | return 1; |
| 1374 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1375 | #if !defined(__FreeBSD__) |
| 1376 | case BTPROTO_SCO: |
| 1377 | { |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1378 | struct sockaddr_sco *addr; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1379 | char *straddr; |
| 1380 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1381 | addr = (struct sockaddr_sco *)addr_ret; |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1382 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1383 | straddr = PyString_AsString(args); |
| 1384 | if (straddr == NULL) { |
| 1385 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1386 | "wrong format"); |
| 1387 | return 0; |
| 1388 | } |
| 1389 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1390 | return 0; |
| 1391 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1392 | *len_ret = sizeof *addr; |
| 1393 | return 1; |
| 1394 | } |
| 1395 | #endif |
| 1396 | default: |
| 1397 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1398 | return 0; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | #endif |
| 1402 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1403 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1404 | case AF_PACKET: |
| 1405 | { |
| 1406 | struct sockaddr_ll* addr; |
| 1407 | struct ifreq ifr; |
| 1408 | char *interfaceName; |
| 1409 | int protoNumber; |
| 1410 | int hatype = 0; |
| 1411 | int pkttype = 0; |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1412 | char *haddr = NULL; |
| 1413 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1414 | |
Thomas Wouters | 0452049 | 2006-04-25 15:08:10 +0000 | [diff] [blame] | 1415 | if (!PyTuple_Check(args)) { |
| 1416 | PyErr_Format( |
| 1417 | PyExc_TypeError, |
| 1418 | "getsockaddrarg: " |
| 1419 | "AF_PACKET address must be tuple, not %.500s", |
| 1420 | args->ob_type->tp_name); |
| 1421 | return 0; |
| 1422 | } |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1423 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1424 | &protoNumber, &pkttype, &hatype, |
| 1425 | &haddr, &halen)) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1426 | return 0; |
| 1427 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1428 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1429 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1430 | s->errorhandler(); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1431 | return 0; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1432 | } |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1433 | if (halen > 8) { |
| 1434 | PyErr_SetString(PyExc_ValueError, |
| 1435 | "Hardware address must be 8 bytes or less"); |
| 1436 | return 0; |
| 1437 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1438 | addr = (struct sockaddr_ll*)addr_ret; |
| 1439 | addr->sll_family = AF_PACKET; |
| 1440 | addr->sll_protocol = htons((short)protoNumber); |
| 1441 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1442 | addr->sll_pkttype = pkttype; |
| 1443 | addr->sll_hatype = hatype; |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1444 | if (halen != 0) { |
| 1445 | memcpy(&addr->sll_addr, haddr, halen); |
| 1446 | } |
| 1447 | addr->sll_halen = halen; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1448 | *len_ret = sizeof *addr; |
| 1449 | return 1; |
| 1450 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1451 | #endif |
| 1452 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1453 | /* More cases here... */ |
| 1454 | |
| 1455 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1456 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1457 | return 0; |
| 1458 | |
| 1459 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1462 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1463 | /* 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] | 1464 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1465 | through len_ret. */ |
| 1466 | |
| 1467 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1468 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1469 | { |
| 1470 | switch (s->sock_family) { |
| 1471 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1472 | #if defined(AF_UNIX) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1473 | case AF_UNIX: |
| 1474 | { |
| 1475 | *len_ret = sizeof (struct sockaddr_un); |
| 1476 | return 1; |
| 1477 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1478 | #endif /* AF_UNIX */ |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 1479 | #if defined(AF_NETLINK) |
| 1480 | case AF_NETLINK: |
| 1481 | { |
| 1482 | *len_ret = sizeof (struct sockaddr_nl); |
| 1483 | return 1; |
| 1484 | } |
| 1485 | #endif |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1486 | |
| 1487 | case AF_INET: |
| 1488 | { |
| 1489 | *len_ret = sizeof (struct sockaddr_in); |
| 1490 | return 1; |
| 1491 | } |
| 1492 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1493 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1494 | case AF_INET6: |
| 1495 | { |
| 1496 | *len_ret = sizeof (struct sockaddr_in6); |
| 1497 | return 1; |
| 1498 | } |
| 1499 | #endif |
| 1500 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1501 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1502 | case AF_BLUETOOTH: |
| 1503 | { |
| 1504 | switch(s->sock_proto) |
| 1505 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1506 | |
| 1507 | case BTPROTO_L2CAP: |
| 1508 | *len_ret = sizeof (struct sockaddr_l2); |
| 1509 | return 1; |
| 1510 | case BTPROTO_RFCOMM: |
| 1511 | *len_ret = sizeof (struct sockaddr_rc); |
| 1512 | return 1; |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 1513 | case BTPROTO_HCI: |
| 1514 | *len_ret = sizeof (struct sockaddr_hci); |
| 1515 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1516 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1517 | case BTPROTO_SCO: |
| 1518 | *len_ret = sizeof (struct sockaddr_sco); |
| 1519 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1520 | #endif |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1521 | default: |
| 1522 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1523 | "unknown BT protocol"); |
| 1524 | return 0; |
| 1525 | |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1526 | } |
| 1527 | } |
| 1528 | #endif |
| 1529 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1530 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1531 | case AF_PACKET: |
| 1532 | { |
| 1533 | *len_ret = sizeof (struct sockaddr_ll); |
| 1534 | return 1; |
| 1535 | } |
| 1536 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1537 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1538 | /* More cases here... */ |
| 1539 | |
| 1540 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1541 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1542 | return 0; |
| 1543 | |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1548 | /* s.accept() method */ |
| 1549 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1550 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1551 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1552 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 1553 | sock_addr_t addrbuf; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1554 | SOCKET_T newfd; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1555 | socklen_t addrlen; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1556 | PyObject *sock = NULL; |
| 1557 | PyObject *addr = NULL; |
| 1558 | PyObject *res = NULL; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1559 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1560 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1561 | if (!getsockaddrlen(s, &addrlen)) |
| 1562 | return NULL; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 1563 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1564 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1565 | #ifdef MS_WINDOWS |
| 1566 | newfd = INVALID_SOCKET; |
| 1567 | #else |
| 1568 | newfd = -1; |
| 1569 | #endif |
| 1570 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1571 | if (!IS_SELECTABLE(s)) |
| 1572 | return select_error(); |
| 1573 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1574 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1575 | timeout = internal_select(s, 0); |
| 1576 | if (!timeout) |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1577 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1578 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1579 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1580 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1581 | PyErr_SetString(socket_timeout, "timed out"); |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1585 | #ifdef MS_WINDOWS |
| 1586 | if (newfd == INVALID_SOCKET) |
| 1587 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1588 | if (newfd < 0) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1589 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1590 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1591 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1592 | /* Create the new object with unspecified family, |
| 1593 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1594 | sock = (PyObject *) new_sockobject(newfd, |
| 1595 | s->sock_family, |
| 1596 | s->sock_type, |
| 1597 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1598 | |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1599 | if (sock == NULL) { |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1600 | SOCKETCLOSE(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1601 | goto finally; |
| 1602 | } |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1603 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1604 | addrlen, s->sock_proto); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1605 | if (addr == NULL) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1606 | goto finally; |
| 1607 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1608 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1609 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1610 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1611 | Py_XDECREF(sock); |
| 1612 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1613 | return res; |
| 1614 | } |
| 1615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1616 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1617 | "accept() -> (socket object, address info)\n\ |
| 1618 | \n\ |
| 1619 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1620 | 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] | 1621 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1622 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1623 | /* s.setblocking(flag) method. Argument: |
| 1624 | False -- non-blocking mode; same as settimeout(0) |
| 1625 | True -- blocking mode; same as settimeout(None) |
| 1626 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1627 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1628 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1629 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1630 | { |
| 1631 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1632 | |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1633 | block = PyInt_AsLong(arg); |
| 1634 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1635 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1636 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1637 | s->sock_timeout = block ? -1.0 : 0.0; |
Guido van Rossum | e8008f0 | 2002-06-07 03:36:20 +0000 | [diff] [blame] | 1638 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1639 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1640 | Py_INCREF(Py_None); |
| 1641 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1642 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1643 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1644 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1645 | "setblocking(flag)\n\ |
| 1646 | \n\ |
| 1647 | 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] | 1648 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1649 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1650 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1651 | /* s.settimeout(timeout) method. Argument: |
| 1652 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1653 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1654 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1655 | < 0 -- illegal; raises an exception |
| 1656 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1657 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1658 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1659 | { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1660 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1661 | |
| 1662 | if (arg == Py_None) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1663 | timeout = -1.0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1664 | else { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1665 | timeout = PyFloat_AsDouble(arg); |
| 1666 | if (timeout < 0.0) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1667 | if (!PyErr_Occurred()) |
| 1668 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1669 | "Timeout value out of range"); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1670 | return NULL; |
| 1671 | } |
| 1672 | } |
| 1673 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1674 | s->sock_timeout = timeout; |
| 1675 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1676 | |
| 1677 | Py_INCREF(Py_None); |
| 1678 | return Py_None; |
| 1679 | } |
| 1680 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1681 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1682 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1683 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1684 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1685 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1686 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1687 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1688 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1689 | /* s.gettimeout() method. |
| 1690 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1691 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1692 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1693 | { |
| 1694 | if (s->sock_timeout < 0.0) { |
| 1695 | Py_INCREF(Py_None); |
| 1696 | return Py_None; |
| 1697 | } |
| 1698 | else |
| 1699 | return PyFloat_FromDouble(s->sock_timeout); |
| 1700 | } |
| 1701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1702 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1703 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1704 | \n\ |
| 1705 | Returns the timeout in floating seconds associated with socket \n\ |
| 1706 | 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] | 1707 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1708 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1709 | #ifdef RISCOS |
| 1710 | /* s.sleeptaskw(1 | 0) method */ |
| 1711 | |
| 1712 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1713 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1714 | { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1715 | int block; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1716 | block = PyInt_AsLong(arg); |
| 1717 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1718 | return NULL; |
| 1719 | Py_BEGIN_ALLOW_THREADS |
| 1720 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1721 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1723 | Py_INCREF(Py_None); |
| 1724 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1725 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1726 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1727 | "sleeptaskw(flag)\n\ |
| 1728 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1729 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1730 | #endif |
| 1731 | |
| 1732 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1733 | /* s.setsockopt() method. |
| 1734 | With an integer third argument, sets an integer option. |
| 1735 | With a string third argument, sets an option from a buffer; |
| 1736 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1737 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1738 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1739 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1740 | { |
| 1741 | int level; |
| 1742 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1743 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1744 | char *buf; |
| 1745 | int buflen; |
| 1746 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1747 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1748 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1749 | &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1750 | buf = (char *) &flag; |
| 1751 | buflen = sizeof flag; |
| 1752 | } |
| 1753 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1754 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1755 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1756 | &level, &optname, &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1757 | return NULL; |
| 1758 | } |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1759 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1760 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1761 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1762 | Py_INCREF(Py_None); |
| 1763 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1766 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1767 | "setsockopt(level, option, value)\n\ |
| 1768 | \n\ |
| 1769 | 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] | 1770 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1771 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1772 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1773 | /* s.getsockopt() method. |
| 1774 | With two arguments, retrieves an integer option. |
| 1775 | With a third integer argument, retrieves a string buffer of that size; |
| 1776 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1777 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1778 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1779 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1780 | { |
| 1781 | int level; |
| 1782 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1783 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1784 | PyObject *buf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1785 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1786 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1787 | #ifdef __BEOS__ |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1788 | /* We have incomplete socket support. */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1789 | PyErr_SetString(socket_error, "getsockopt not supported"); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1790 | return NULL; |
| 1791 | #else |
| 1792 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1793 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1794 | &level, &optname, &buflen)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1795 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1796 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1797 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1798 | int flag = 0; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1799 | socklen_t flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1800 | res = getsockopt(s->sock_fd, level, optname, |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1801 | (void *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1802 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1803 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1804 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1805 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1806 | #ifdef __VMS |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 1807 | /* socklen_t is unsigned so no negative test is needed, |
| 1808 | test buflen == 0 is previously done */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1809 | if (buflen > 1024) { |
| 1810 | #else |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1811 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1812 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1813 | PyErr_SetString(socket_error, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1814 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1815 | return NULL; |
| 1816 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1817 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1818 | if (buf == NULL) |
| 1819 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1820 | res = getsockopt(s->sock_fd, level, optname, |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1821 | (void *)PyString_AS_STRING(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1822 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1823 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1824 | return s->errorhandler(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1825 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1826 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1827 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1828 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1831 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1832 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1833 | \n\ |
| 1834 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1835 | 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] | 1836 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1837 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1838 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1839 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1840 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1841 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1842 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1843 | { |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1844 | sock_addr_t addrbuf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1845 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1846 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1847 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1848 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1849 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1850 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1851 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1852 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1853 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1854 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1855 | Py_INCREF(Py_None); |
| 1856 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1859 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1860 | "bind(address)\n\ |
| 1861 | \n\ |
| 1862 | 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] | 1863 | 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] | 1864 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1865 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1866 | |
| 1867 | /* s.close() method. |
| 1868 | Set the file descriptor to -1 so operations tried subsequently |
| 1869 | will surely fail. */ |
| 1870 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1871 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1872 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1873 | { |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1874 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1875 | |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1876 | if ((fd = s->sock_fd) != -1) { |
| 1877 | s->sock_fd = -1; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1878 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1879 | (void) SOCKETCLOSE(fd); |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1880 | Py_END_ALLOW_THREADS |
| 1881 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1882 | Py_INCREF(Py_None); |
| 1883 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1886 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1887 | "close()\n\ |
| 1888 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1889 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1890 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1891 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1892 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
| 1893 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1894 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1895 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1896 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1897 | timeout = 0; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1898 | res = connect(s->sock_fd, addr, addrlen); |
| 1899 | |
| 1900 | #ifdef MS_WINDOWS |
| 1901 | |
| 1902 | if (s->sock_timeout > 0.0) { |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1903 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 1904 | IS_SELECTABLE(s)) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1905 | /* This is a mess. Best solution: trust select */ |
| 1906 | fd_set fds; |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1907 | fd_set fds_exc; |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1908 | struct timeval tv; |
| 1909 | tv.tv_sec = (int)s->sock_timeout; |
| 1910 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1911 | FD_ZERO(&fds); |
| 1912 | FD_SET(s->sock_fd, &fds); |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1913 | FD_ZERO(&fds_exc); |
| 1914 | FD_SET(s->sock_fd, &fds_exc); |
| 1915 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1916 | if (res == 0) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1917 | res = WSAEWOULDBLOCK; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1918 | timeout = 1; |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1919 | } else if (res > 0) { |
| 1920 | if (FD_ISSET(s->sock_fd, &fds)) |
| 1921 | /* The socket is in the writeable set - this |
| 1922 | means connected */ |
| 1923 | res = 0; |
| 1924 | else { |
| 1925 | /* As per MS docs, we need to call getsockopt() |
| 1926 | to get the underlying error */ |
| 1927 | int res_size = sizeof res; |
| 1928 | /* It must be in the exception set */ |
| 1929 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1930 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1931 | (char *)&res, &res_size)) |
| 1932 | /* getsockopt also clears WSAGetLastError, |
| 1933 | so reset it back. */ |
| 1934 | WSASetLastError(res); |
| 1935 | else |
| 1936 | res = WSAGetLastError(); |
| 1937 | } |
| 1938 | } |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1939 | /* else if (res < 0) an error occurred */ |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | if (res < 0) |
| 1944 | res = WSAGetLastError(); |
| 1945 | |
| 1946 | #else |
| 1947 | |
| 1948 | if (s->sock_timeout > 0.0) { |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 1949 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1950 | timeout = internal_select(s, 1); |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1951 | if (timeout == 0) { |
| 1952 | res = connect(s->sock_fd, addr, addrlen); |
| 1953 | if (res < 0 && errno == EISCONN) |
| 1954 | res = 0; |
| 1955 | } |
| 1956 | else if (timeout == -1) |
| 1957 | res = errno; /* had error */ |
| 1958 | else |
| 1959 | res = EWOULDBLOCK; /* timed out */ |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | if (res < 0) |
| 1964 | res = errno; |
| 1965 | |
| 1966 | #endif |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1967 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1968 | |
| 1969 | return res; |
| 1970 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1971 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1972 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1973 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1974 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1975 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1976 | { |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1977 | sock_addr_t addrbuf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1978 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1979 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1980 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1981 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1982 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1983 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1984 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1985 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 1986 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1987 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1988 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1989 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1990 | PyErr_SetString(socket_timeout, "timed out"); |
| 1991 | return NULL; |
| 1992 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1993 | if (res != 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1994 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1995 | Py_INCREF(Py_None); |
| 1996 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1999 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2000 | "connect(address)\n\ |
| 2001 | \n\ |
| 2002 | 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] | 2003 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2004 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2005 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 2006 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2007 | |
| 2008 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2009 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2010 | { |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2011 | sock_addr_t addrbuf; |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2012 | int addrlen; |
| 2013 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2014 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2015 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2016 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2017 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2018 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2019 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2020 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2021 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2022 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2023 | /* Signals are not errors (though they may raise exceptions). Adapted |
| 2024 | from PyErr_SetFromErrnoWithFilenameObject(). */ |
| 2025 | #ifdef EINTR |
| 2026 | if (res == EINTR && PyErr_CheckSignals()) |
| 2027 | return NULL; |
| 2028 | #endif |
| 2029 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2030 | return PyInt_FromLong((long) res); |
| 2031 | } |
| 2032 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2033 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2034 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2035 | \n\ |
| 2036 | 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] | 2037 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2038 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 2039 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2040 | /* s.fileno() method */ |
| 2041 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2042 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2043 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2044 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2045 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2046 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2047 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 2048 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2049 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2052 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2053 | "fileno() -> integer\n\ |
| 2054 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2055 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2058 | #ifndef NO_DUP |
| 2059 | /* s.dup() method */ |
| 2060 | |
| 2061 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2062 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2063 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2064 | SOCKET_T newfd; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2065 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2066 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2067 | newfd = dup(s->sock_fd); |
| 2068 | if (newfd < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2069 | return s->errorhandler(); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2070 | sock = (PyObject *) new_sockobject(newfd, |
| 2071 | s->sock_family, |
| 2072 | s->sock_type, |
| 2073 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2074 | if (sock == NULL) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 2075 | SOCKETCLOSE(newfd); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2076 | return sock; |
| 2077 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2079 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2080 | "dup() -> socket object\n\ |
| 2081 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2082 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2083 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2084 | #endif |
| 2085 | |
| 2086 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2087 | /* s.getsockname() method */ |
| 2088 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2089 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2090 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2091 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2092 | sock_addr_t addrbuf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2093 | int res; |
| 2094 | socklen_t addrlen; |
| 2095 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2096 | if (!getsockaddrlen(s, &addrlen)) |
| 2097 | return NULL; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2098 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2099 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2100 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2101 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2102 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2103 | return s->errorhandler(); |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2104 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 2105 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2108 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2109 | "getsockname() -> address info\n\ |
| 2110 | \n\ |
| 2111 | 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] | 2112 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2113 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2114 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2115 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2116 | /* s.getpeername() method */ |
| 2117 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2118 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2119 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2120 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2121 | sock_addr_t addrbuf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2122 | int res; |
| 2123 | socklen_t addrlen; |
| 2124 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2125 | if (!getsockaddrlen(s, &addrlen)) |
| 2126 | return NULL; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2127 | memset(&addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2128 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2129 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2130 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2131 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2132 | return s->errorhandler(); |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2133 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 2134 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2135 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2137 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2138 | "getpeername() -> address info\n\ |
| 2139 | \n\ |
| 2140 | 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] | 2141 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2142 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2143 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2144 | |
| 2145 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2146 | /* s.listen(n) method */ |
| 2147 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2148 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2149 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2150 | { |
| 2151 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2152 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2153 | |
| 2154 | backlog = PyInt_AsLong(arg); |
| 2155 | if (backlog == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2156 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2157 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2158 | if (backlog < 1) |
| 2159 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2160 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2161 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2162 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2163 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2164 | Py_INCREF(Py_None); |
| 2165 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2168 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2169 | "listen(backlog)\n\ |
| 2170 | \n\ |
| 2171 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 2172 | 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] | 2173 | will allow before refusing new connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2174 | |
| 2175 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2176 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2177 | /* s.makefile(mode) method. |
| 2178 | Create a new open file object referring to a dupped version of |
| 2179 | the socket's file descriptor. (The dup() call is necessary so |
| 2180 | that the open file and socket objects may be closed independent |
| 2181 | of each other.) |
| 2182 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 2183 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2184 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2185 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2186 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 2187 | extern int fclose(FILE *); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2188 | char *mode = "r"; |
| 2189 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2190 | #ifdef MS_WIN32 |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 2191 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2192 | #else |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2193 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2194 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2195 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2196 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2197 | #ifdef __VMS |
| 2198 | char *mode_r = "r"; |
| 2199 | char *mode_w = "w"; |
| 2200 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2201 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2202 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2203 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2204 | #ifdef __VMS |
| 2205 | if (strcmp(mode,"rb") == 0) { |
| 2206 | mode = mode_r; |
| 2207 | } |
| 2208 | else { |
| 2209 | if (strcmp(mode,"wb") == 0) { |
| 2210 | mode = mode_w; |
| 2211 | } |
| 2212 | } |
| 2213 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2214 | #ifdef MS_WIN32 |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2215 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 2216 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2217 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2218 | if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2219 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2220 | { |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2221 | if (fd >= 0) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 2222 | SOCKETCLOSE(fd); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2223 | return s->errorhandler(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2224 | } |
| 2225 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 2226 | if (f != NULL) |
| 2227 | PyFile_SetBufSize(f, bufsize); |
| 2228 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2229 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2231 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2232 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 2233 | \n\ |
| 2234 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2235 | The mode and buffersize arguments are as for the built-in open() function."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2236 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2237 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2238 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2239 | /* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2240 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Andrew M. Kuchling | 8d0baae | 2006-12-19 14:29:04 +0000 | [diff] [blame] | 2241 | * char buffer. If you have any inc/dec ref to do to the objects that contain |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2242 | * the buffer, do it in the caller. This function returns the number of bytes |
| 2243 | * succesfully read. If there was an error, it returns -1. Note that it is |
| 2244 | * also possible that we return a number of bytes smaller than the request |
| 2245 | * bytes. |
| 2246 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2247 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2248 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2249 | { |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2250 | ssize_t outlen = -1; |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2251 | int timeout; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2252 | #ifdef __VMS |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2253 | int remaining; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2254 | char *read_buf; |
| 2255 | #endif |
| 2256 | |
| 2257 | if (!IS_SELECTABLE(s)) { |
| 2258 | select_error(); |
| 2259 | return -1; |
| 2260 | } |
| 2261 | |
| 2262 | #ifndef __VMS |
| 2263 | Py_BEGIN_ALLOW_THREADS |
| 2264 | timeout = internal_select(s, 0); |
| 2265 | if (!timeout) |
| 2266 | outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2267 | Py_END_ALLOW_THREADS |
| 2268 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2269 | if (timeout == 1) { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2270 | PyErr_SetString(socket_timeout, "timed out"); |
| 2271 | return -1; |
| 2272 | } |
| 2273 | if (outlen < 0) { |
| 2274 | /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2275 | NULL, so ignore its return value */ |
| 2276 | s->errorhandler(); |
| 2277 | return -1; |
| 2278 | } |
| 2279 | #else |
| 2280 | read_buf = cbuf; |
| 2281 | remaining = len; |
| 2282 | while (remaining != 0) { |
| 2283 | unsigned int segment; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2284 | int nread = -1; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2285 | |
| 2286 | segment = remaining /SEGMENT_SIZE; |
| 2287 | if (segment != 0) { |
| 2288 | segment = SEGMENT_SIZE; |
| 2289 | } |
| 2290 | else { |
| 2291 | segment = remaining; |
| 2292 | } |
| 2293 | |
| 2294 | Py_BEGIN_ALLOW_THREADS |
| 2295 | timeout = internal_select(s, 0); |
| 2296 | if (!timeout) |
| 2297 | nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2298 | Py_END_ALLOW_THREADS |
| 2299 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2300 | if (timeout == 1) { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2301 | PyErr_SetString(socket_timeout, "timed out"); |
| 2302 | return -1; |
| 2303 | } |
| 2304 | if (nread < 0) { |
| 2305 | s->errorhandler(); |
| 2306 | return -1; |
| 2307 | } |
| 2308 | if (nread != remaining) { |
| 2309 | read_buf += nread; |
| 2310 | break; |
| 2311 | } |
| 2312 | |
| 2313 | remaining -= segment; |
| 2314 | read_buf += segment; |
| 2315 | } |
| 2316 | outlen = read_buf - cbuf; |
| 2317 | #endif /* !__VMS */ |
| 2318 | |
| 2319 | return outlen; |
| 2320 | } |
| 2321 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2322 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2323 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2324 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2325 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2326 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2327 | { |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2328 | int recvlen, flags = 0; |
| 2329 | ssize_t outlen; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2330 | PyObject *buf; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2331 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2332 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2333 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2334 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2335 | if (recvlen < 0) { |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 2336 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2337 | "negative buffersize in recv"); |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 2338 | return NULL; |
| 2339 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2340 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2341 | /* Allocate a new string. */ |
| 2342 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2343 | if (buf == NULL) |
| 2344 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2345 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2346 | /* Call the guts */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2347 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2348 | if (outlen < 0) { |
Georg Brandl | cddabbf | 2006-07-28 18:36:01 +0000 | [diff] [blame] | 2349 | /* An error occurred, release the string and return an |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2350 | error. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2351 | Py_DECREF(buf); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2352 | return NULL; |
| 2353 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2354 | if (outlen != recvlen) { |
| 2355 | /* We did not read as many bytes as we anticipated, resize the |
| 2356 | string if possible and be succesful. */ |
| 2357 | if (_PyString_Resize(&buf, outlen) < 0) |
| 2358 | /* Oopsy, not so succesful after all. */ |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2359 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2360 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2361 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2362 | return buf; |
| 2363 | } |
| 2364 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2365 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2366 | "recv(buffersize[, flags]) -> data\n\ |
| 2367 | \n\ |
| 2368 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2369 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2370 | 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] | 2371 | 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] | 2372 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2373 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2374 | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2375 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2376 | static PyObject* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2377 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2378 | { |
| 2379 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
| 2380 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2381 | int recvlen = 0, flags = 0; |
| 2382 | ssize_t readlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2383 | char *buf; |
| 2384 | int buflen; |
| 2385 | |
| 2386 | /* Get the buffer's memory */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2387 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv", kwlist, |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2388 | &buf, &buflen, &recvlen, &flags)) |
| 2389 | return NULL; |
| 2390 | assert(buf != 0 && buflen > 0); |
| 2391 | |
| 2392 | if (recvlen < 0) { |
| 2393 | PyErr_SetString(PyExc_ValueError, |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame^] | 2394 | "negative buffersize in recv_into"); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2395 | return NULL; |
| 2396 | } |
| 2397 | if (recvlen == 0) { |
| 2398 | /* If nbytes was not specified, use the buffer's length */ |
| 2399 | recvlen = buflen; |
| 2400 | } |
| 2401 | |
| 2402 | /* Check if the buffer is large enough */ |
| 2403 | if (buflen < recvlen) { |
| 2404 | PyErr_SetString(PyExc_ValueError, |
| 2405 | "buffer too small for requested bytes"); |
| 2406 | return NULL; |
| 2407 | } |
| 2408 | |
| 2409 | /* Call the guts */ |
| 2410 | readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2411 | if (readlen < 0) { |
| 2412 | /* Return an error. */ |
| 2413 | return NULL; |
| 2414 | } |
| 2415 | |
| 2416 | /* Return the number of bytes read. Note that we do not do anything |
| 2417 | special here in the case that readlen < recvlen. */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2418 | return PyInt_FromSsize_t(readlen); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2421 | PyDoc_STRVAR(recv_into_doc, |
| 2422 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2423 | \n\ |
| 2424 | A version of recv() that stores its data into a buffer rather than creating \n\ |
| 2425 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
| 2426 | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
| 2427 | \n\ |
| 2428 | See recv() for documentation about the flags."); |
| 2429 | |
| 2430 | |
| 2431 | /* |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2432 | * This is the guts of the recv() and recv_into() methods, which reads into a |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2433 | * char buffer. If you have any inc/def ref to do to the objects that contain |
| 2434 | * the buffer, do it in the caller. This function returns the number of bytes |
| 2435 | * succesfully read. If there was an error, it returns -1. Note that it is |
| 2436 | * also possible that we return a number of bytes smaller than the request |
| 2437 | * bytes. |
| 2438 | * |
| 2439 | * 'addr' is a return value for the address object. Note that you must decref |
| 2440 | * it yourself. |
| 2441 | */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2442 | static ssize_t |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2443 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
| 2444 | PyObject** addr) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2445 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2446 | sock_addr_t addrbuf; |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2447 | int timeout; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2448 | ssize_t n = -1; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2449 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2450 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2451 | *addr = NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2452 | |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 2453 | if (!getsockaddrlen(s, &addrlen)) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2454 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2455 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2456 | if (!IS_SELECTABLE(s)) { |
| 2457 | select_error(); |
| 2458 | return -1; |
| 2459 | } |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2460 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2461 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 2462 | memset(&addrbuf, 0, addrlen); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2463 | timeout = internal_select(s, 0); |
Martin v. Löwis | 7e75f1a | 2006-04-15 08:35:59 +0000 | [diff] [blame] | 2464 | if (!timeout) { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2465 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2466 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2467 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2468 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2469 | #else |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2470 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
Martin v. Löwis | 7e75f1a | 2006-04-15 08:35:59 +0000 | [diff] [blame] | 2471 | (void *) &addrbuf, &addrlen); |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2472 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2473 | #else |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2474 | n = recvfrom(s->sock_fd, cbuf, len, flags, |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2475 | SAS2SA(&addrbuf), &addrlen); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2476 | #endif |
Martin v. Löwis | 7e75f1a | 2006-04-15 08:35:59 +0000 | [diff] [blame] | 2477 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2478 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2479 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2480 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2481 | PyErr_SetString(socket_timeout, "timed out"); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2482 | return -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2483 | } |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2484 | if (n < 0) { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2485 | s->errorhandler(); |
| 2486 | return -1; |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2487 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2488 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2489 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2490 | addrlen, s->sock_proto))) |
| 2491 | return -1; |
| 2492 | |
| 2493 | return n; |
| 2494 | } |
| 2495 | |
| 2496 | /* s.recvfrom(nbytes [,flags]) method */ |
| 2497 | |
| 2498 | static PyObject * |
| 2499 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2500 | { |
| 2501 | PyObject *buf = NULL; |
| 2502 | PyObject *addr = NULL; |
| 2503 | PyObject *ret = NULL; |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2504 | int recvlen, flags = 0; |
| 2505 | ssize_t outlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2506 | |
| 2507 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2508 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2509 | |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame^] | 2510 | if (recvlen < 0) { |
| 2511 | PyErr_SetString(PyExc_ValueError, |
| 2512 | "negative buffersize in recvfrom"); |
| 2513 | return NULL; |
| 2514 | } |
| 2515 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2516 | buf = PyString_FromStringAndSize((char *) 0, recvlen); |
| 2517 | if (buf == NULL) |
| 2518 | return NULL; |
| 2519 | |
| 2520 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf), |
| 2521 | recvlen, flags, &addr); |
| 2522 | if (outlen < 0) { |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2523 | goto finally; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | if (outlen != recvlen) { |
| 2527 | /* We did not read as many bytes as we anticipated, resize the |
| 2528 | string if possible and be succesful. */ |
| 2529 | if (_PyString_Resize(&buf, outlen) < 0) |
| 2530 | /* Oopsy, not so succesful after all. */ |
| 2531 | goto finally; |
| 2532 | } |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2533 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2534 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2535 | |
| 2536 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2537 | Py_XDECREF(buf); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2538 | Py_XDECREF(addr); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 2539 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2542 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2543 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2544 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2545 | 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] | 2546 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2547 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2548 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2549 | |
| 2550 | static PyObject * |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2551 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2552 | { |
| 2553 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
| 2554 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2555 | int recvlen = 0, flags = 0; |
| 2556 | ssize_t readlen; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2557 | char *buf; |
| 2558 | int buflen; |
| 2559 | |
| 2560 | PyObject *addr = NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2561 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2562 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom", kwlist, |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2563 | &buf, &buflen, &recvlen, &flags)) |
| 2564 | return NULL; |
| 2565 | assert(buf != 0 && buflen > 0); |
| 2566 | |
| 2567 | if (recvlen < 0) { |
| 2568 | PyErr_SetString(PyExc_ValueError, |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame^] | 2569 | "negative buffersize in recvfrom_into"); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2570 | return NULL; |
| 2571 | } |
| 2572 | if (recvlen == 0) { |
| 2573 | /* If nbytes was not specified, use the buffer's length */ |
| 2574 | recvlen = buflen; |
| 2575 | } |
| 2576 | |
| 2577 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2578 | if (readlen < 0) { |
| 2579 | /* Return an error */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2580 | Py_XDECREF(addr); |
| 2581 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | /* Return the number of bytes read and the address. Note that we do |
| 2585 | not do anything special here in the case that readlen < recvlen. */ |
Neal Norwitz | f0cab1f | 2006-06-08 05:12:45 +0000 | [diff] [blame] | 2586 | return Py_BuildValue("lN", readlen, addr); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2589 | PyDoc_STRVAR(recvfrom_into_doc, |
| 2590 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2591 | \n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2592 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2593 | |
| 2594 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2595 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2596 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2597 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2598 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2599 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2600 | char *buf; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2601 | int len, n = -1, flags = 0, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2602 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2603 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2604 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2605 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2606 | if (!IS_SELECTABLE(s)) |
| 2607 | return select_error(); |
| 2608 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2609 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2610 | timeout = internal_select(s, 1); |
| 2611 | if (!timeout) |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2612 | #ifdef __VMS |
| 2613 | n = sendsegmented(s->sock_fd, buf, len, flags); |
| 2614 | #else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2615 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2616 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2617 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2618 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2619 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2620 | PyErr_SetString(socket_timeout, "timed out"); |
| 2621 | return NULL; |
| 2622 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2623 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2624 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2625 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2628 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2629 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2630 | \n\ |
| 2631 | 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] | 2632 | 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] | 2633 | 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] | 2634 | |
| 2635 | |
| 2636 | /* s.sendall(data [,flags]) method */ |
| 2637 | |
| 2638 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2639 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2640 | { |
| 2641 | char *buf; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2642 | int len, n = -1, flags = 0, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2643 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2644 | if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) |
| 2645 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2646 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2647 | if (!IS_SELECTABLE(s)) |
| 2648 | return select_error(); |
| 2649 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2650 | Py_BEGIN_ALLOW_THREADS |
| 2651 | do { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2652 | timeout = internal_select(s, 1); |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2653 | n = -1; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2654 | if (timeout) |
| 2655 | break; |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2656 | #ifdef __VMS |
| 2657 | n = sendsegmented(s->sock_fd, buf, len, flags); |
| 2658 | #else |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2659 | n = send(s->sock_fd, buf, len, flags); |
Neal Norwitz | 2a30cd0 | 2006-07-10 01:18:57 +0000 | [diff] [blame] | 2660 | #endif |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2661 | if (n < 0) |
| 2662 | break; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2663 | buf += n; |
| 2664 | len -= n; |
| 2665 | } while (len > 0); |
| 2666 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2667 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2668 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2669 | PyErr_SetString(socket_timeout, "timed out"); |
| 2670 | return NULL; |
| 2671 | } |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2672 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2673 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2674 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2675 | Py_INCREF(Py_None); |
| 2676 | return Py_None; |
| 2677 | } |
| 2678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2680 | "sendall(data[, flags])\n\ |
| 2681 | \n\ |
| 2682 | Send a data string to the socket. For the optional flags\n\ |
| 2683 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2684 | 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] | 2685 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2686 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2687 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2688 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2689 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2690 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2691 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2692 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2693 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2694 | char *buf; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2695 | sock_addr_t addrbuf; |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2696 | int addrlen, len, n = -1, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2697 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2698 | flags = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2699 | if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2700 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2701 | if (!PyArg_ParseTuple(args, "s#iO:sendto", |
| 2702 | &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2703 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2704 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2705 | |
Neal Norwitz | 082b2df | 2006-02-07 07:04:46 +0000 | [diff] [blame] | 2706 | if (!IS_SELECTABLE(s)) |
| 2707 | return select_error(); |
| 2708 | |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2709 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2710 | return NULL; |
| 2711 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2712 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2713 | timeout = internal_select(s, 1); |
| 2714 | if (!timeout) |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 2715 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2716 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2717 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 2718 | if (timeout == 1) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2719 | PyErr_SetString(socket_timeout, "timed out"); |
| 2720 | return NULL; |
| 2721 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2722 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2723 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2724 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2727 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2728 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2729 | \n\ |
| 2730 | 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] | 2731 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2732 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2733 | |
| 2734 | /* s.shutdown(how) method */ |
| 2735 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2736 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2737 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2738 | { |
| 2739 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2740 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2741 | |
| 2742 | how = PyInt_AsLong(arg); |
| 2743 | if (how == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2744 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2745 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2746 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2747 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2748 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2749 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2750 | Py_INCREF(Py_None); |
| 2751 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2754 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2755 | "shutdown(flag)\n\ |
| 2756 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2757 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2758 | 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] | 2759 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2760 | |
| 2761 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2762 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2763 | static PyMethodDef sock_methods[] = { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2764 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 2765 | accept_doc}, |
| 2766 | {"bind", (PyCFunction)sock_bind, METH_O, |
| 2767 | bind_doc}, |
| 2768 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 2769 | close_doc}, |
| 2770 | {"connect", (PyCFunction)sock_connect, METH_O, |
| 2771 | connect_doc}, |
| 2772 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 2773 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2774 | #ifndef NO_DUP |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2775 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
| 2776 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2777 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2778 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 2779 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2780 | #ifdef HAVE_GETPEERNAME |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2781 | {"getpeername", (PyCFunction)sock_getpeername, |
| 2782 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 2783 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2784 | {"getsockname", (PyCFunction)sock_getsockname, |
| 2785 | METH_NOARGS, getsockname_doc}, |
| 2786 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 2787 | getsockopt_doc}, |
| 2788 | {"listen", (PyCFunction)sock_listen, METH_O, |
| 2789 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2790 | #ifndef NO_DUP |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2791 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
| 2792 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2793 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2794 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 2795 | recv_doc}, |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2796 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 2797 | recv_into_doc}, |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2798 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 2799 | recvfrom_doc}, |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 2800 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 2801 | recvfrom_into_doc}, |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2802 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 2803 | send_doc}, |
| 2804 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 2805 | sendall_doc}, |
| 2806 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 2807 | sendto_doc}, |
| 2808 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 2809 | setblocking_doc}, |
| 2810 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 2811 | settimeout_doc}, |
| 2812 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 2813 | gettimeout_doc}, |
| 2814 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 2815 | setsockopt_doc}, |
| 2816 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 2817 | shutdown_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2818 | #ifdef RISCOS |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 2819 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
| 2820 | sleeptaskw_doc}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2821 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2822 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2823 | }; |
| 2824 | |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 2825 | /* SockObject members */ |
| 2826 | static PyMemberDef sock_memberlist[] = { |
| 2827 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 2828 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
| 2829 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
| 2830 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"}, |
| 2831 | {0}, |
| 2832 | }; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2833 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2834 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2835 | First close the file description. */ |
| 2836 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2837 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2838 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2839 | { |
Guido van Rossum | fa972c9 | 2000-04-10 12:45:45 +0000 | [diff] [blame] | 2840 | if (s->sock_fd != -1) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 2841 | (void) SOCKETCLOSE(s->sock_fd); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2842 | s->ob_type->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2845 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2846 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2847 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2848 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2849 | char buf[512]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2850 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
| 2851 | if (s->sock_fd > LONG_MAX) { |
| 2852 | /* this can occur on Win64, and actually there is a special |
| 2853 | ugly printf formatter for decimal pointer length integer |
| 2854 | printing, only bother if necessary*/ |
| 2855 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2856 | "no printf formatter to display " |
| 2857 | "the socket descriptor in decimal"); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2858 | return NULL; |
| 2859 | } |
| 2860 | #endif |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2861 | PyOS_snprintf( |
| 2862 | buf, sizeof(buf), |
| 2863 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
| 2864 | (long)s->sock_fd, s->sock_family, |
| 2865 | s->sock_type, |
| 2866 | s->sock_proto); |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 2867 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2871 | /* Create a new, uninitialized socket object. */ |
| 2872 | |
| 2873 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2874 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2875 | { |
| 2876 | PyObject *new; |
| 2877 | |
| 2878 | new = type->tp_alloc(type, 0); |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2879 | if (new != NULL) { |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2880 | ((PySocketSockObject *)new)->sock_fd = -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2881 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2882 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2883 | } |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2884 | return new; |
| 2885 | } |
| 2886 | |
| 2887 | |
| 2888 | /* Initialize a new socket object. */ |
| 2889 | |
| 2890 | /*ARGSUSED*/ |
| 2891 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2892 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2893 | { |
| 2894 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 2895 | SOCKET_T fd; |
| 2896 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2897 | static char *keywords[] = {"family", "type", "proto", 0}; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2898 | |
| 2899 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2900 | "|iii:socket", keywords, |
| 2901 | &family, &type, &proto)) |
| 2902 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2903 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2904 | Py_BEGIN_ALLOW_THREADS |
| 2905 | fd = socket(family, type, proto); |
| 2906 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2907 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2908 | #ifdef MS_WINDOWS |
| 2909 | if (fd == INVALID_SOCKET) |
| 2910 | #else |
| 2911 | if (fd < 0) |
| 2912 | #endif |
| 2913 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2914 | set_error(); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2915 | return -1; |
| 2916 | } |
| 2917 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2918 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2919 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2920 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2924 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2925 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2926 | static PyTypeObject sock_type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 2927 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2928 | 0, /* ob_size */ |
Guido van Rossum | ca6dfa5 | 2001-10-28 12:31:33 +0000 | [diff] [blame] | 2929 | "_socket.socket", /* tp_name */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2930 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 2931 | 0, /* tp_itemsize */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2932 | (destructor)sock_dealloc, /* tp_dealloc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2933 | 0, /* tp_print */ |
| 2934 | 0, /* tp_getattr */ |
| 2935 | 0, /* tp_setattr */ |
| 2936 | 0, /* tp_compare */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2937 | (reprfunc)sock_repr, /* tp_repr */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2938 | 0, /* tp_as_number */ |
| 2939 | 0, /* tp_as_sequence */ |
| 2940 | 0, /* tp_as_mapping */ |
| 2941 | 0, /* tp_hash */ |
| 2942 | 0, /* tp_call */ |
| 2943 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2944 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2945 | 0, /* tp_setattro */ |
| 2946 | 0, /* tp_as_buffer */ |
| 2947 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2948 | sock_doc, /* tp_doc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2949 | 0, /* tp_traverse */ |
| 2950 | 0, /* tp_clear */ |
| 2951 | 0, /* tp_richcompare */ |
| 2952 | 0, /* tp_weaklistoffset */ |
| 2953 | 0, /* tp_iter */ |
| 2954 | 0, /* tp_iternext */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2955 | sock_methods, /* tp_methods */ |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 2956 | sock_memberlist, /* tp_members */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2957 | 0, /* tp_getset */ |
| 2958 | 0, /* tp_base */ |
| 2959 | 0, /* tp_dict */ |
| 2960 | 0, /* tp_descr_get */ |
| 2961 | 0, /* tp_descr_set */ |
| 2962 | 0, /* tp_dictoffset */ |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2963 | sock_initobj, /* tp_init */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2964 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2965 | sock_new, /* tp_new */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2966 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2967 | }; |
| 2968 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2969 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2970 | /* Python interface to gethostname(). */ |
| 2971 | |
| 2972 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2973 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 2974 | socket_gethostname(PyObject *self, PyObject *unused) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2975 | { |
| 2976 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2977 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2978 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2979 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2980 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2981 | if (res < 0) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2982 | return set_error(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2983 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2984 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2985 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2987 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2988 | "gethostname() -> string\n\ |
| 2989 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2990 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2991 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2992 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2993 | /* Python interface to gethostbyname(name). */ |
| 2994 | |
| 2995 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2996 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2997 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2998 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2999 | char *name; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 3000 | sock_addr_t addrbuf; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3001 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3002 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3003 | return NULL; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 3004 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3005 | return NULL; |
Martin v. Löwis | 046c4d1 | 2006-12-03 11:23:45 +0000 | [diff] [blame] | 3006 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3009 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3010 | "gethostbyname(host) -> address\n\ |
| 3011 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3012 | 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] | 3013 | |
| 3014 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3015 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 3016 | |
| 3017 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3018 | 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] | 3019 | { |
| 3020 | char **pch; |
| 3021 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 3022 | PyObject *name_list = (PyObject *)NULL; |
| 3023 | PyObject *addr_list = (PyObject *)NULL; |
| 3024 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3025 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3026 | if (h == NULL) { |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3027 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3028 | #ifndef RISCOS |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3029 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3030 | #else |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3031 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 3032 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3033 | return NULL; |
| 3034 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3035 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3036 | if (h->h_addrtype != af) { |
| 3037 | #ifdef HAVE_STRERROR |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3038 | /* Let's get real error message to return */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3039 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3040 | (char *)strerror(EAFNOSUPPORT)); |
| 3041 | #else |
| 3042 | PyErr_SetString( |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3043 | socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3044 | "Address family not supported by protocol family"); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3045 | #endif |
| 3046 | return NULL; |
| 3047 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3048 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3049 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3050 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3051 | case AF_INET: |
| 3052 | if (alen < sizeof(struct sockaddr_in)) |
| 3053 | return NULL; |
| 3054 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3055 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3056 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3057 | case AF_INET6: |
| 3058 | if (alen < sizeof(struct sockaddr_in6)) |
| 3059 | return NULL; |
| 3060 | break; |
| 3061 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3062 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3063 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3064 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3065 | if ((name_list = PyList_New(0)) == NULL) |
| 3066 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3067 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3068 | if ((addr_list = PyList_New(0)) == NULL) |
| 3069 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3070 | |
Georg Brandl | 9dc7b7c | 2006-08-14 22:10:24 +0000 | [diff] [blame] | 3071 | /* SF #1511317: h_aliases can be NULL */ |
| 3072 | if (h->h_aliases) { |
| 3073 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3074 | int status; |
| 3075 | tmp = PyString_FromString(*pch); |
| 3076 | if (tmp == NULL) |
| 3077 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3078 | |
Georg Brandl | 9dc7b7c | 2006-08-14 22:10:24 +0000 | [diff] [blame] | 3079 | status = PyList_Append(name_list, tmp); |
| 3080 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3081 | |
Georg Brandl | 9dc7b7c | 2006-08-14 22:10:24 +0000 | [diff] [blame] | 3082 | if (status) |
| 3083 | goto err; |
| 3084 | } |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3085 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3086 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3087 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3088 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3089 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3090 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3091 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3092 | case AF_INET: |
| 3093 | { |
| 3094 | struct sockaddr_in sin; |
| 3095 | memset(&sin, 0, sizeof(sin)); |
| 3096 | sin.sin_family = af; |
| 3097 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 3098 | sin.sin_len = sizeof(sin); |
| 3099 | #endif |
| 3100 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3101 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3102 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3103 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3104 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 3105 | break; |
| 3106 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3107 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3108 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3109 | case AF_INET6: |
| 3110 | { |
| 3111 | struct sockaddr_in6 sin6; |
| 3112 | memset(&sin6, 0, sizeof(sin6)); |
| 3113 | sin6.sin6_family = af; |
| 3114 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 3115 | sin6.sin6_len = sizeof(sin6); |
| 3116 | #endif |
| 3117 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3118 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3119 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3120 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3121 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3122 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3123 | break; |
| 3124 | } |
| 3125 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3126 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3127 | default: /* can't happen */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3128 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3129 | "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3130 | return NULL; |
| 3131 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3132 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3133 | if (tmp == NULL) |
| 3134 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3135 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3136 | status = PyList_Append(addr_list, tmp); |
| 3137 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3138 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3139 | if (status) |
| 3140 | goto err; |
| 3141 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3142 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3143 | 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] | 3144 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3145 | err: |
| 3146 | Py_XDECREF(name_list); |
| 3147 | Py_XDECREF(addr_list); |
| 3148 | return rtn_tuple; |
| 3149 | } |
| 3150 | |
| 3151 | |
| 3152 | /* Python interface to gethostbyname_ex(name). */ |
| 3153 | |
| 3154 | /*ARGSUSED*/ |
| 3155 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3156 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3157 | { |
| 3158 | char *name; |
| 3159 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 3160 | #ifdef ENABLE_IPV6 |
| 3161 | struct sockaddr_storage addr; |
| 3162 | #else |
| 3163 | struct sockaddr_in addr; |
| 3164 | #endif |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 3165 | struct sockaddr *sa; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3166 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3167 | #ifdef HAVE_GETHOSTBYNAME_R |
| 3168 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3169 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 3170 | struct hostent_data data; |
| 3171 | #else |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3172 | char buf[16384]; |
| 3173 | int buf_len = (sizeof buf) - 1; |
| 3174 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3175 | #endif |
| 3176 | #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] | 3177 | int result; |
| 3178 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3179 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3180 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3181 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3182 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3183 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3184 | return NULL; |
| 3185 | Py_BEGIN_ALLOW_THREADS |
| 3186 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3187 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3188 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3189 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3190 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3191 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3192 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 3193 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3194 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 3195 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3196 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3197 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3198 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3199 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3200 | #endif |
| 3201 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3202 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3203 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3204 | /* Some C libraries would require addr.__ss_family instead of |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3205 | addr.ss_family. |
| 3206 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 3207 | access sa_family. */ |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 3208 | sa = (struct sockaddr*)&addr; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3209 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3210 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3211 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3212 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 3213 | #endif |
| 3214 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3217 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 3218 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3219 | \n\ |
| 3220 | 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] | 3221 | 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] | 3222 | |
| 3223 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3224 | /* Python interface to gethostbyaddr(IP). */ |
| 3225 | |
| 3226 | /*ARGSUSED*/ |
| 3227 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3228 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3229 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3230 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3231 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3232 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3233 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3234 | #endif |
| 3235 | struct sockaddr *sa = (struct sockaddr *)&addr; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3236 | char *ip_num; |
| 3237 | struct hostent *h; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3238 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3239 | #ifdef HAVE_GETHOSTBYNAME_R |
| 3240 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3241 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 3242 | struct hostent_data data; |
| 3243 | #else |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3244 | char buf[16384]; |
| 3245 | int buf_len = (sizeof buf) - 1; |
| 3246 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3247 | #endif |
| 3248 | #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] | 3249 | int result; |
| 3250 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3251 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3252 | char *ap; |
| 3253 | int al; |
| 3254 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3255 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3256 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3257 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3258 | af = AF_UNSPEC; |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 3259 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3260 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3261 | af = sa->sa_family; |
| 3262 | ap = NULL; |
| 3263 | al = 0; |
| 3264 | switch (af) { |
| 3265 | case AF_INET: |
| 3266 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3267 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3268 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3269 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3270 | case AF_INET6: |
| 3271 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3272 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3273 | break; |
| 3274 | #endif |
| 3275 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3276 | PyErr_SetString(socket_error, "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3277 | return NULL; |
| 3278 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3279 | Py_BEGIN_ALLOW_THREADS |
| 3280 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3281 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3282 | result = gethostbyaddr_r(ap, al, af, |
| 3283 | &hp_allocated, buf, buf_len, |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3284 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3285 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3286 | h = gethostbyaddr_r(ap, al, af, |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3287 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3288 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 3289 | memset((void *) &data, '\0', sizeof(data)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3290 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 3291 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 3292 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3293 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3294 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3295 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3296 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3297 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 3298 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3299 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3300 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3301 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3302 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 3303 | #endif |
| 3304 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3307 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3308 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3309 | \n\ |
| 3310 | 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] | 3311 | 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] | 3312 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3313 | |
| 3314 | /* Python interface to getservbyname(name). |
| 3315 | This only returns the port number, since the other info is already |
| 3316 | known or not useful (like the list of aliases). */ |
| 3317 | |
| 3318 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3319 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3320 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3321 | { |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3322 | char *name, *proto=NULL; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3323 | struct servent *sp; |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3324 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3325 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3326 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 3327 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3328 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3329 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3330 | PyErr_SetString(socket_error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3331 | return NULL; |
| 3332 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3333 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3336 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3337 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3338 | \n\ |
| 3339 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3340 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3341 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3342 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3343 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3344 | /* Python interface to getservbyport(port). |
| 3345 | This only returns the service name, since the other info is already |
| 3346 | known or not useful (like the list of aliases). */ |
| 3347 | |
| 3348 | /*ARGSUSED*/ |
| 3349 | static PyObject * |
| 3350 | socket_getservbyport(PyObject *self, PyObject *args) |
| 3351 | { |
Raymond Hettinger | 87de0ca | 2004-09-28 02:19:40 +0000 | [diff] [blame] | 3352 | unsigned short port; |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3353 | char *proto=NULL; |
| 3354 | struct servent *sp; |
Raymond Hettinger | 87de0ca | 2004-09-28 02:19:40 +0000 | [diff] [blame] | 3355 | if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto)) |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3356 | return NULL; |
| 3357 | Py_BEGIN_ALLOW_THREADS |
| 3358 | sp = getservbyport(htons(port), proto); |
| 3359 | Py_END_ALLOW_THREADS |
| 3360 | if (sp == NULL) { |
| 3361 | PyErr_SetString(socket_error, "port/proto not found"); |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | return PyString_FromString(sp->s_name); |
| 3365 | } |
| 3366 | |
| 3367 | PyDoc_STRVAR(getservbyport_doc, |
| 3368 | "getservbyport(port[, protocolname]) -> string\n\ |
| 3369 | \n\ |
| 3370 | Return the service name from a port number and protocol name.\n\ |
| 3371 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 3372 | otherwise any protocol will match."); |
| 3373 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3374 | /* Python interface to getprotobyname(name). |
| 3375 | This only returns the protocol number, since the other info is |
| 3376 | already known or not useful (like the list of aliases). */ |
| 3377 | |
| 3378 | /*ARGSUSED*/ |
| 3379 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3380 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3381 | { |
| 3382 | char *name; |
| 3383 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3384 | #ifdef __BEOS__ |
| 3385 | /* Not available in BeOS yet. - [cjh] */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3386 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3387 | return NULL; |
| 3388 | #else |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3389 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3390 | return NULL; |
| 3391 | Py_BEGIN_ALLOW_THREADS |
| 3392 | sp = getprotobyname(name); |
| 3393 | Py_END_ALLOW_THREADS |
| 3394 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3395 | PyErr_SetString(socket_error, "protocol not found"); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3396 | return NULL; |
| 3397 | } |
| 3398 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3399 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3402 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3403 | "getprotobyname(name) -> integer\n\ |
| 3404 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3405 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3406 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3407 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3408 | #ifdef HAVE_SOCKETPAIR |
| 3409 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3410 | Arguments as for socket() except the default family is AF_UNIX if |
Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 3411 | defined on the platform; otherwise, the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3412 | |
| 3413 | /*ARGSUSED*/ |
| 3414 | static PyObject * |
| 3415 | socket_socketpair(PyObject *self, PyObject *args) |
| 3416 | { |
| 3417 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3418 | SOCKET_T sv[2]; |
| 3419 | int family, type = SOCK_STREAM, proto = 0; |
| 3420 | PyObject *res = NULL; |
| 3421 | |
| 3422 | #if defined(AF_UNIX) |
| 3423 | family = AF_UNIX; |
| 3424 | #else |
| 3425 | family = AF_INET; |
| 3426 | #endif |
| 3427 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3428 | &family, &type, &proto)) |
| 3429 | return NULL; |
| 3430 | /* Create a pair of socket fds */ |
| 3431 | if (socketpair(family, type, proto, sv) < 0) |
| 3432 | return set_error(); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3433 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3434 | if (s0 == NULL) |
| 3435 | goto finally; |
| 3436 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3437 | if (s1 == NULL) |
| 3438 | goto finally; |
| 3439 | res = PyTuple_Pack(2, s0, s1); |
| 3440 | |
| 3441 | finally: |
| 3442 | if (res == NULL) { |
| 3443 | if (s0 == NULL) |
| 3444 | SOCKETCLOSE(sv[0]); |
| 3445 | if (s1 == NULL) |
| 3446 | SOCKETCLOSE(sv[1]); |
| 3447 | } |
| 3448 | Py_XDECREF(s0); |
| 3449 | Py_XDECREF(s1); |
| 3450 | return res; |
| 3451 | } |
| 3452 | |
| 3453 | PyDoc_STRVAR(socketpair_doc, |
| 3454 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3455 | \n\ |
| 3456 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3457 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 3458 | 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] | 3459 | 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] | 3460 | |
| 3461 | #endif /* HAVE_SOCKETPAIR */ |
| 3462 | |
| 3463 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3464 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3465 | /* Create a socket object from a numeric file description. |
| 3466 | Useful e.g. if stdin is a socket. |
| 3467 | Additional arguments as for socket(). */ |
| 3468 | |
| 3469 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3470 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3471 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3472 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3473 | PySocketSockObject *s; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3474 | SOCKET_T fd; |
| 3475 | int family, type, proto = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3476 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 3477 | &fd, &family, &type, &proto)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3478 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 3479 | /* Dup the fd so it and the socket can be closed independently */ |
| 3480 | fd = dup(fd); |
| 3481 | if (fd < 0) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3482 | return set_error(); |
| 3483 | s = new_sockobject(fd, family, type, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3484 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3485 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3487 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3488 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 3489 | \n\ |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 3490 | Create a socket object from a duplicate of the given\n\ |
| 3491 | file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3492 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3493 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3494 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3495 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3496 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3497 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3498 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3499 | { |
| 3500 | int x1, x2; |
| 3501 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3502 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3503 | return NULL; |
| 3504 | } |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 3505 | if (x1 < 0) { |
| 3506 | PyErr_SetString(PyExc_OverflowError, |
| 3507 | "can't convert negative number to unsigned long"); |
| 3508 | return NULL; |
| 3509 | } |
| 3510 | x2 = (unsigned int)ntohs((unsigned short)x1); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3511 | return PyInt_FromLong(x2); |
| 3512 | } |
| 3513 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3514 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3515 | "ntohs(integer) -> integer\n\ |
| 3516 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3517 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3518 | |
| 3519 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3520 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3521 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3522 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3523 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3524 | |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3525 | if (PyInt_Check(arg)) { |
| 3526 | x = PyInt_AS_LONG(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3527 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3528 | return NULL; |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 3529 | if ((long)x < 0) { |
| 3530 | PyErr_SetString(PyExc_OverflowError, |
| 3531 | "can't convert negative number to unsigned long"); |
| 3532 | return NULL; |
| 3533 | } |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3534 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3535 | else if (PyLong_Check(arg)) { |
| 3536 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3537 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3538 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3539 | #if SIZEOF_LONG > 4 |
| 3540 | { |
| 3541 | unsigned long y; |
| 3542 | /* only want the trailing 32 bits */ |
| 3543 | y = x & 0xFFFFFFFFUL; |
| 3544 | if (y ^ x) |
| 3545 | return PyErr_Format(PyExc_OverflowError, |
| 3546 | "long int larger than 32 bits"); |
| 3547 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3548 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3549 | #endif |
| 3550 | } |
| 3551 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3552 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3553 | "expected int/long, %s found", |
| 3554 | arg->ob_type->tp_name); |
| 3555 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3556 | return NULL; |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 3557 | return PyLong_FromUnsignedLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3560 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3561 | "ntohl(integer) -> integer\n\ |
| 3562 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3563 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3564 | |
| 3565 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3566 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3567 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3568 | { |
Hye-Shik Chang | a7b673f | 2003-12-17 09:40:23 +0000 | [diff] [blame] | 3569 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3570 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3571 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3572 | return NULL; |
| 3573 | } |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 3574 | if (x1 < 0) { |
| 3575 | PyErr_SetString(PyExc_OverflowError, |
| 3576 | "can't convert negative number to unsigned long"); |
| 3577 | return NULL; |
| 3578 | } |
| 3579 | x2 = (unsigned int)htons((unsigned short)x1); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3580 | return PyInt_FromLong(x2); |
| 3581 | } |
| 3582 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3583 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3584 | "htons(integer) -> integer\n\ |
| 3585 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3586 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3587 | |
| 3588 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3589 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3590 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3591 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3592 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3593 | |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3594 | if (PyInt_Check(arg)) { |
| 3595 | x = PyInt_AS_LONG(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3596 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3597 | return NULL; |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 3598 | if ((long)x < 0) { |
| 3599 | PyErr_SetString(PyExc_OverflowError, |
| 3600 | "can't convert negative number to unsigned long"); |
| 3601 | return NULL; |
| 3602 | } |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3603 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3604 | else if (PyLong_Check(arg)) { |
| 3605 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3606 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3607 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3608 | #if SIZEOF_LONG > 4 |
| 3609 | { |
| 3610 | unsigned long y; |
| 3611 | /* only want the trailing 32 bits */ |
| 3612 | y = x & 0xFFFFFFFFUL; |
| 3613 | if (y ^ x) |
| 3614 | return PyErr_Format(PyExc_OverflowError, |
| 3615 | "long int larger than 32 bits"); |
| 3616 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3617 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3618 | #endif |
| 3619 | } |
| 3620 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3621 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3622 | "expected int/long, %s found", |
| 3623 | arg->ob_type->tp_name); |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 3624 | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3627 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3628 | "htonl(integer) -> integer\n\ |
| 3629 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3630 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3631 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3632 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3633 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3634 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3635 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3636 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3637 | 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] | 3638 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3639 | |
| 3640 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3641 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3642 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3643 | #ifndef INADDR_NONE |
| 3644 | #define INADDR_NONE (-1) |
| 3645 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3646 | #ifdef HAVE_INET_ATON |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3647 | struct in_addr buf; |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3648 | #endif |
| 3649 | |
| 3650 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3651 | /* Have to use inet_addr() instead */ |
| 3652 | unsigned long packed_addr; |
| 3653 | #endif |
| 3654 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3655 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3656 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3657 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3658 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3659 | |
| 3660 | #ifdef HAVE_INET_ATON |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3661 | |
| 3662 | #ifdef USE_INET_ATON_WEAKLINK |
| 3663 | if (inet_aton != NULL) { |
| 3664 | #endif |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3665 | if (inet_aton(ip_addr, &buf)) |
| 3666 | return PyString_FromStringAndSize((char *)(&buf), |
| 3667 | sizeof(buf)); |
| 3668 | |
| 3669 | PyErr_SetString(socket_error, |
| 3670 | "illegal IP address string passed to inet_aton"); |
| 3671 | return NULL; |
| 3672 | |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3673 | #ifdef USE_INET_ATON_WEAKLINK |
| 3674 | } else { |
| 3675 | #endif |
| 3676 | |
| 3677 | #endif |
| 3678 | |
| 3679 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3680 | |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 3681 | /* special-case this address as inet_addr might return INADDR_NONE |
| 3682 | * for this */ |
| 3683 | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3684 | packed_addr = 0xFFFFFFFF; |
| 3685 | } else { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3686 | |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 3687 | packed_addr = inet_addr(ip_addr); |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3688 | |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 3689 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3690 | PyErr_SetString(socket_error, |
| 3691 | "illegal IP address string passed to inet_aton"); |
| 3692 | return NULL; |
| 3693 | } |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3694 | } |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3695 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 3696 | sizeof(packed_addr)); |
Ronald Oussoren | d06b6f2 | 2006-04-23 11:59:25 +0000 | [diff] [blame] | 3697 | |
| 3698 | #ifdef USE_INET_ATON_WEAKLINK |
| 3699 | } |
| 3700 | #endif |
| 3701 | |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3702 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3705 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3706 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3707 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3708 | 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] | 3709 | |
| 3710 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3711 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3712 | { |
| 3713 | char *packed_str; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3714 | int addr_len; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3715 | struct in_addr packed_addr; |
| 3716 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3717 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3718 | return NULL; |
| 3719 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3720 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3721 | if (addr_len != sizeof(packed_addr)) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3722 | PyErr_SetString(socket_error, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3723 | "packed IP wrong length for inet_ntoa"); |
| 3724 | return NULL; |
| 3725 | } |
| 3726 | |
| 3727 | memcpy(&packed_addr, packed_str, addr_len); |
| 3728 | |
| 3729 | return PyString_FromString(inet_ntoa(packed_addr)); |
| 3730 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3731 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3732 | #ifdef HAVE_INET_PTON |
| 3733 | |
| 3734 | PyDoc_STRVAR(inet_pton_doc, |
| 3735 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 3736 | \n\ |
| 3737 | Convert an IP address from string format to a packed string suitable\n\ |
| 3738 | for use with low-level network functions."); |
| 3739 | |
| 3740 | static PyObject * |
| 3741 | socket_inet_pton(PyObject *self, PyObject *args) |
| 3742 | { |
| 3743 | int af; |
| 3744 | char* ip; |
| 3745 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3746 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3747 | 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] | 3748 | #else |
| 3749 | char packed[sizeof(struct in_addr)]; |
| 3750 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3751 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3752 | return NULL; |
| 3753 | } |
| 3754 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 3755 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3756 | if(af == AF_INET6) { |
| 3757 | PyErr_SetString(socket_error, |
| 3758 | "can't use AF_INET6, IPv6 is disabled"); |
| 3759 | return NULL; |
| 3760 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3761 | #endif |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3762 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3763 | retval = inet_pton(af, ip, packed); |
| 3764 | if (retval < 0) { |
| 3765 | PyErr_SetFromErrno(socket_error); |
| 3766 | return NULL; |
| 3767 | } else if (retval == 0) { |
| 3768 | PyErr_SetString(socket_error, |
| 3769 | "illegal IP address string passed to inet_pton"); |
| 3770 | return NULL; |
| 3771 | } else if (af == AF_INET) { |
| 3772 | return PyString_FromStringAndSize(packed, |
| 3773 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3774 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3775 | } else if (af == AF_INET6) { |
| 3776 | return PyString_FromStringAndSize(packed, |
| 3777 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3778 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3779 | } else { |
| 3780 | PyErr_SetString(socket_error, "unknown address family"); |
| 3781 | return NULL; |
| 3782 | } |
| 3783 | } |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3784 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3785 | PyDoc_STRVAR(inet_ntop_doc, |
| 3786 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3787 | \n\ |
| 3788 | Convert a packed IP address of the given family to string format."); |
| 3789 | |
| 3790 | static PyObject * |
| 3791 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 3792 | { |
| 3793 | int af; |
| 3794 | char* packed; |
| 3795 | int len; |
| 3796 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3797 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3798 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3799 | #else |
| 3800 | char ip[INET_ADDRSTRLEN + 1]; |
| 3801 | #endif |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3802 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3803 | /* Guarantee NUL-termination for PyString_FromString() below */ |
Jeremy Hylton | 80961f3 | 2004-11-07 14:24:25 +0000 | [diff] [blame] | 3804 | memset((void *) &ip[0], '\0', sizeof(ip)); |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3805 | |
| 3806 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 3807 | return NULL; |
| 3808 | } |
| 3809 | |
| 3810 | if (af == AF_INET) { |
| 3811 | if (len != sizeof(struct in_addr)) { |
| 3812 | PyErr_SetString(PyExc_ValueError, |
| 3813 | "invalid length of packed IP address string"); |
| 3814 | return NULL; |
| 3815 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3816 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3817 | } else if (af == AF_INET6) { |
| 3818 | if (len != sizeof(struct in6_addr)) { |
| 3819 | PyErr_SetString(PyExc_ValueError, |
| 3820 | "invalid length of packed IP address string"); |
| 3821 | return NULL; |
| 3822 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3823 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3824 | } else { |
| 3825 | PyErr_Format(PyExc_ValueError, |
| 3826 | "unknown address family %d", af); |
| 3827 | return NULL; |
| 3828 | } |
| 3829 | |
| 3830 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 3831 | if (!retval) { |
| 3832 | PyErr_SetFromErrno(socket_error); |
| 3833 | return NULL; |
| 3834 | } else { |
| 3835 | return PyString_FromString(retval); |
| 3836 | } |
| 3837 | |
| 3838 | /* NOTREACHED */ |
| 3839 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 3840 | return NULL; |
| 3841 | } |
| 3842 | |
| 3843 | #endif /* HAVE_INET_PTON */ |
| 3844 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3845 | /* Python interface to getaddrinfo(host, port). */ |
| 3846 | |
| 3847 | /*ARGSUSED*/ |
| 3848 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3849 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3850 | { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3851 | struct addrinfo hints, *res; |
| 3852 | struct addrinfo *res0 = NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3853 | PyObject *hobj = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3854 | PyObject *pobj = (PyObject *)NULL; |
Martin v. Löwis | f65b1a1 | 2001-08-12 09:28:40 +0000 | [diff] [blame] | 3855 | char pbuf[30]; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3856 | char *hptr, *pptr; |
| 3857 | int family, socktype, protocol, flags; |
| 3858 | int error; |
| 3859 | PyObject *all = (PyObject *)NULL; |
| 3860 | PyObject *single = (PyObject *)NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3861 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3862 | |
| 3863 | family = socktype = protocol = flags = 0; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3864 | family = AF_UNSPEC; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3865 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 3866 | &hobj, &pobj, &family, &socktype, |
| 3867 | &protocol, &flags)) { |
| 3868 | return NULL; |
| 3869 | } |
| 3870 | if (hobj == Py_None) { |
| 3871 | hptr = NULL; |
| 3872 | } else if (PyUnicode_Check(hobj)) { |
| 3873 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 3874 | if (!idna) |
| 3875 | return NULL; |
| 3876 | hptr = PyString_AsString(idna); |
| 3877 | } else if (PyString_Check(hobj)) { |
| 3878 | hptr = PyString_AsString(hobj); |
| 3879 | } else { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 3880 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3881 | "getaddrinfo() argument 1 must be string or None"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3882 | return NULL; |
| 3883 | } |
| 3884 | if (PyInt_Check(pobj)) { |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3885 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3886 | pptr = pbuf; |
| 3887 | } else if (PyString_Check(pobj)) { |
| 3888 | pptr = PyString_AsString(pobj); |
| 3889 | } else if (pobj == Py_None) { |
| 3890 | pptr = (char *)NULL; |
| 3891 | } else { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3892 | PyErr_SetString(socket_error, "Int or String expected"); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3893 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3894 | } |
| 3895 | memset(&hints, 0, sizeof(hints)); |
| 3896 | hints.ai_family = family; |
| 3897 | hints.ai_socktype = socktype; |
| 3898 | hints.ai_protocol = protocol; |
| 3899 | hints.ai_flags = flags; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3900 | Py_BEGIN_ALLOW_THREADS |
| 3901 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3902 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3903 | Py_END_ALLOW_THREADS |
| 3904 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3905 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3906 | set_gaierror(error); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3907 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | if ((all = PyList_New(0)) == NULL) |
| 3911 | goto err; |
| 3912 | for (res = res0; res; res = res->ai_next) { |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3913 | PyObject *addr = |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 3914 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3915 | if (addr == NULL) |
| 3916 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3917 | single = Py_BuildValue("iiisO", res->ai_family, |
| 3918 | res->ai_socktype, res->ai_protocol, |
| 3919 | res->ai_canonname ? res->ai_canonname : "", |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3920 | addr); |
| 3921 | Py_DECREF(addr); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3922 | if (single == NULL) |
| 3923 | goto err; |
| 3924 | |
| 3925 | if (PyList_Append(all, single)) |
| 3926 | goto err; |
| 3927 | Py_XDECREF(single); |
| 3928 | } |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3929 | Py_XDECREF(idna); |
Neal Norwitz | 90128ba | 2002-08-09 03:37:42 +0000 | [diff] [blame] | 3930 | if (res0) |
| 3931 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3932 | return all; |
| 3933 | err: |
| 3934 | Py_XDECREF(single); |
| 3935 | Py_XDECREF(all); |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3936 | Py_XDECREF(idna); |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3937 | if (res0) |
| 3938 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3939 | return (PyObject *)NULL; |
| 3940 | } |
| 3941 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3942 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3943 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 3944 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3945 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3946 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3947 | |
| 3948 | /* Python interface to getnameinfo(sa, flags). */ |
| 3949 | |
| 3950 | /*ARGSUSED*/ |
| 3951 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3952 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3953 | { |
| 3954 | PyObject *sa = (PyObject *)NULL; |
| 3955 | int flags; |
| 3956 | char *hostp; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3957 | int port, flowinfo, scope_id; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3958 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 3959 | struct addrinfo hints, *res = NULL; |
| 3960 | int error; |
| 3961 | PyObject *ret = (PyObject *)NULL; |
| 3962 | |
| 3963 | flags = flowinfo = scope_id = 0; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3964 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3965 | return NULL; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3966 | if (!PyArg_ParseTuple(sa, "si|ii", |
| 3967 | &hostp, &port, &flowinfo, &scope_id)) |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3968 | return NULL; |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3969 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3970 | memset(&hints, 0, sizeof(hints)); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3971 | hints.ai_family = AF_UNSPEC; |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3972 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3973 | Py_BEGIN_ALLOW_THREADS |
| 3974 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3975 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3976 | Py_END_ALLOW_THREADS |
| 3977 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3978 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3979 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3980 | goto fail; |
| 3981 | } |
| 3982 | if (res->ai_next) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3983 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3984 | "sockaddr resolved to multiple addresses"); |
| 3985 | goto fail; |
| 3986 | } |
| 3987 | switch (res->ai_family) { |
| 3988 | case AF_INET: |
| 3989 | { |
| 3990 | char *t1; |
| 3991 | int t2; |
| 3992 | if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3993 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3994 | "IPv4 sockaddr must be 2 tuple"); |
| 3995 | goto fail; |
| 3996 | } |
| 3997 | break; |
| 3998 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3999 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4000 | case AF_INET6: |
| 4001 | { |
| 4002 | struct sockaddr_in6 *sin6; |
| 4003 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 4004 | sin6->sin6_flowinfo = flowinfo; |
| 4005 | sin6->sin6_scope_id = scope_id; |
| 4006 | break; |
| 4007 | } |
| 4008 | #endif |
| 4009 | } |
| 4010 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 4011 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 4012 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4013 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4014 | goto fail; |
| 4015 | } |
| 4016 | ret = Py_BuildValue("ss", hbuf, pbuf); |
| 4017 | |
| 4018 | fail: |
| 4019 | if (res) |
| 4020 | freeaddrinfo(res); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4021 | return ret; |
| 4022 | } |
| 4023 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4024 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4025 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4026 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4027 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4028 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4029 | |
| 4030 | /* Python API to getting and setting the default timeout value. */ |
| 4031 | |
| 4032 | static PyObject * |
| 4033 | socket_getdefaulttimeout(PyObject *self) |
| 4034 | { |
| 4035 | if (defaulttimeout < 0.0) { |
| 4036 | Py_INCREF(Py_None); |
| 4037 | return Py_None; |
| 4038 | } |
| 4039 | else |
| 4040 | return PyFloat_FromDouble(defaulttimeout); |
| 4041 | } |
| 4042 | |
| 4043 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4044 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4045 | \n\ |
| 4046 | Returns the default timeout in floating seconds for new socket objects.\n\ |
| 4047 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4048 | When the socket module is first imported, the default is None."); |
| 4049 | |
| 4050 | static PyObject * |
| 4051 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 4052 | { |
| 4053 | double timeout; |
| 4054 | |
| 4055 | if (arg == Py_None) |
| 4056 | timeout = -1.0; |
| 4057 | else { |
| 4058 | timeout = PyFloat_AsDouble(arg); |
| 4059 | if (timeout < 0.0) { |
| 4060 | if (!PyErr_Occurred()) |
| 4061 | PyErr_SetString(PyExc_ValueError, |
| 4062 | "Timeout value out of range"); |
| 4063 | return NULL; |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | defaulttimeout = timeout; |
| 4068 | |
| 4069 | Py_INCREF(Py_None); |
| 4070 | return Py_None; |
| 4071 | } |
| 4072 | |
| 4073 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4074 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4075 | \n\ |
| 4076 | Set the default timeout in floating seconds for new socket objects.\n\ |
| 4077 | A value of None indicates that new socket objects have no timeout.\n\ |
| 4078 | When the socket module is first imported, the default is None."); |
| 4079 | |
| 4080 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4081 | /* List of functions exported by this module. */ |
| 4082 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4083 | static PyMethodDef socket_methods[] = { |
| 4084 | {"gethostbyname", socket_gethostbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4085 | METH_VARARGS, gethostbyname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4086 | {"gethostbyname_ex", socket_gethostbyname_ex, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4087 | METH_VARARGS, ghbn_ex_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4088 | {"gethostbyaddr", socket_gethostbyaddr, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4089 | METH_VARARGS, gethostbyaddr_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4090 | {"gethostname", socket_gethostname, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 4091 | METH_NOARGS, gethostname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4092 | {"getservbyname", socket_getservbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4093 | METH_VARARGS, getservbyname_doc}, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 4094 | {"getservbyport", socket_getservbyport, |
| 4095 | METH_VARARGS, getservbyport_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4096 | {"getprotobyname", socket_getprotobyname, |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 4097 | METH_VARARGS, getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4098 | #ifndef NO_DUP |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4099 | {"fromfd", socket_fromfd, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4100 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4101 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4102 | #ifdef HAVE_SOCKETPAIR |
| 4103 | {"socketpair", socket_socketpair, |
| 4104 | METH_VARARGS, socketpair_doc}, |
| 4105 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4106 | {"ntohs", socket_ntohs, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4107 | METH_VARARGS, ntohs_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4108 | {"ntohl", socket_ntohl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4109 | METH_O, ntohl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4110 | {"htons", socket_htons, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4111 | METH_VARARGS, htons_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4112 | {"htonl", socket_htonl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 4113 | METH_O, htonl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4114 | {"inet_aton", socket_inet_aton, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4115 | METH_VARARGS, inet_aton_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4116 | {"inet_ntoa", socket_inet_ntoa, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 4117 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4118 | #ifdef HAVE_INET_PTON |
| 4119 | {"inet_pton", socket_inet_pton, |
| 4120 | METH_VARARGS, inet_pton_doc}, |
| 4121 | {"inet_ntop", socket_inet_ntop, |
| 4122 | METH_VARARGS, inet_ntop_doc}, |
| 4123 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4124 | {"getaddrinfo", socket_getaddrinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4125 | METH_VARARGS, getaddrinfo_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4126 | {"getnameinfo", socket_getnameinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4127 | METH_VARARGS, getnameinfo_doc}, |
Guido van Rossum | 1693ba8 | 2002-07-18 21:11:26 +0000 | [diff] [blame] | 4128 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 4129 | METH_NOARGS, getdefaulttimeout_doc}, |
| 4130 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4131 | METH_O, setdefaulttimeout_doc}, |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4132 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4133 | }; |
| 4134 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 4135 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4136 | #ifdef RISCOS |
| 4137 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4138 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4139 | static int |
| 4140 | os_init(void) |
| 4141 | { |
| 4142 | _kernel_swi_regs r; |
| 4143 | |
| 4144 | r.r[0] = 0; |
| 4145 | _kernel_swi(0x43380, &r, &r); |
| 4146 | taskwindow = r.r[0]; |
| 4147 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 4148 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | #endif /* RISCOS */ |
| 4152 | |
| 4153 | |
| 4154 | #ifdef MS_WINDOWS |
| 4155 | #define OS_INIT_DEFINED |
| 4156 | |
| 4157 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4158 | |
| 4159 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4160 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4161 | { |
| 4162 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4166 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4167 | { |
| 4168 | WSADATA WSAData; |
| 4169 | int ret; |
| 4170 | char buf[100]; |
| 4171 | ret = WSAStartup(0x0101, &WSAData); |
| 4172 | switch (ret) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4173 | case 0: /* No error */ |
Mark Hammond | 14350ab | 2003-07-09 04:57:46 +0000 | [diff] [blame] | 4174 | Py_AtExit(os_cleanup); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4175 | return 1; /* Success */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4176 | case WSASYSNOTREADY: |
| 4177 | PyErr_SetString(PyExc_ImportError, |
| 4178 | "WSAStartup failed: network not ready"); |
| 4179 | break; |
| 4180 | case WSAVERNOTSUPPORTED: |
| 4181 | case WSAEINVAL: |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4182 | PyErr_SetString( |
| 4183 | PyExc_ImportError, |
| 4184 | "WSAStartup failed: requested version not supported"); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4185 | break; |
| 4186 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 4187 | PyOS_snprintf(buf, sizeof(buf), |
| 4188 | "WSAStartup failed: error code %d", ret); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4189 | PyErr_SetString(PyExc_ImportError, buf); |
| 4190 | break; |
| 4191 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4192 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 4195 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 4196 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4197 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4198 | #ifdef PYOS_OS2 |
| 4199 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4200 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4201 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4202 | |
| 4203 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4204 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4205 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4206 | #ifndef PYCC_GCC |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4207 | char reason[64]; |
| 4208 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4209 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4210 | if (rc == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4211 | return 1; /* Success */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4212 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4213 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4214 | PyOS_snprintf(reason, sizeof(reason), |
| 4215 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 4216 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4217 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4218 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4219 | #else |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4220 | /* No need to initialise sockets with GCC/EMX */ |
| 4221 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 4222 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | #endif /* PYOS_OS2 */ |
| 4226 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4227 | |
| 4228 | #ifndef OS_INIT_DEFINED |
| 4229 | static int |
| 4230 | os_init(void) |
| 4231 | { |
| 4232 | return 1; /* Success */ |
| 4233 | } |
| 4234 | #endif |
| 4235 | |
| 4236 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4237 | /* C API table - always add new things to the end for binary |
| 4238 | compatibility. */ |
| 4239 | static |
| 4240 | PySocketModule_APIObject PySocketModuleAPI = |
| 4241 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4242 | &sock_type, |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 4243 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4244 | }; |
| 4245 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4246 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4247 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4248 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4249 | This module is actually called "_socket", and there's a wrapper |
| 4250 | "socket.py" which implements some additional functionality. On some |
| 4251 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 4252 | wrapper for the socket type that provides missing functionality such |
| 4253 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 4254 | with an ImportError exception if os-specific initialization fails. |
| 4255 | On Windows, this does WINSOCK initialization. When WINSOCK is |
| 4256 | initialized succesfully, a call to WSACleanup() is scheduled to be |
| 4257 | made at exit time. |
| 4258 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4259 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4260 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 4261 | "Implementation module for socket operations.\n\ |
| 4262 | \n\ |
| 4263 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 4264 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4265 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 4266 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4267 | { |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4268 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4269 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4270 | if (!os_init()) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4271 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4272 | |
| 4273 | sock_type.ob_type = &PyType_Type; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4274 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 4275 | socket_methods, |
| 4276 | socket_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 4277 | if (m == NULL) |
| 4278 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4279 | |
| 4280 | socket_error = PyErr_NewException("socket.error", NULL, NULL); |
| 4281 | if (socket_error == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4282 | return; |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 4283 | PySocketModuleAPI.error = socket_error; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4284 | Py_INCREF(socket_error); |
| 4285 | PyModule_AddObject(m, "error", socket_error); |
| 4286 | socket_herror = PyErr_NewException("socket.herror", |
| 4287 | socket_error, NULL); |
| 4288 | if (socket_herror == NULL) |
| 4289 | return; |
| 4290 | Py_INCREF(socket_herror); |
| 4291 | PyModule_AddObject(m, "herror", socket_herror); |
| 4292 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4293 | NULL); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4294 | if (socket_gaierror == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4295 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4296 | Py_INCREF(socket_gaierror); |
| 4297 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 4298 | socket_timeout = PyErr_NewException("socket.timeout", |
| 4299 | socket_error, NULL); |
| 4300 | if (socket_timeout == NULL) |
| 4301 | return; |
| 4302 | Py_INCREF(socket_timeout); |
| 4303 | PyModule_AddObject(m, "timeout", socket_timeout); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4304 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4305 | if (PyModule_AddObject(m, "SocketType", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4306 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 4307 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4308 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4309 | if (PyModule_AddObject(m, "socket", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4310 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 4311 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4312 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4313 | #ifdef ENABLE_IPV6 |
| 4314 | has_ipv6 = Py_True; |
| 4315 | #else |
| 4316 | has_ipv6 = Py_False; |
| 4317 | #endif |
| 4318 | Py_INCREF(has_ipv6); |
| 4319 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
| 4320 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4321 | /* Export C API */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4322 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 4323 | PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL) |
| 4324 | ) != 0) |
| 4325 | return; |
| 4326 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4327 | /* Address families (we only support AF_INET and AF_UNIX) */ |
| 4328 | #ifdef AF_UNSPEC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4329 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4330 | #endif |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4331 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4332 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4333 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4334 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 4335 | #if defined(AF_UNIX) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4336 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4337 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4338 | #ifdef AF_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4339 | /* Amateur Radio AX.25 */ |
| 4340 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4341 | #endif |
| 4342 | #ifdef AF_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4343 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4344 | #endif |
| 4345 | #ifdef AF_APPLETALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4346 | /* Appletalk DDP */ |
| 4347 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4348 | #endif |
| 4349 | #ifdef AF_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4350 | /* Amateur radio NetROM */ |
| 4351 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4352 | #endif |
| 4353 | #ifdef AF_BRIDGE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4354 | /* Multiprotocol bridge */ |
| 4355 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4356 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4357 | #ifdef AF_ATMPVC |
| 4358 | /* ATM PVCs */ |
| 4359 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
| 4360 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4361 | #ifdef AF_AAL5 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4362 | /* Reserved for Werner's ATM */ |
| 4363 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4364 | #endif |
| 4365 | #ifdef AF_X25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4366 | /* Reserved for X.25 project */ |
| 4367 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4368 | #endif |
| 4369 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4370 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4371 | #endif |
| 4372 | #ifdef AF_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4373 | /* Amateur Radio X.25 PLP */ |
| 4374 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4375 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4376 | #ifdef AF_DECnet |
| 4377 | /* Reserved for DECnet project */ |
| 4378 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
| 4379 | #endif |
| 4380 | #ifdef AF_NETBEUI |
| 4381 | /* Reserved for 802.2LLC project */ |
| 4382 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
| 4383 | #endif |
| 4384 | #ifdef AF_SECURITY |
| 4385 | /* Security callback pseudo AF */ |
| 4386 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
| 4387 | #endif |
| 4388 | #ifdef AF_KEY |
| 4389 | /* PF_KEY key management API */ |
| 4390 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
| 4391 | #endif |
| 4392 | #ifdef AF_NETLINK |
| 4393 | /* */ |
| 4394 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4395 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4396 | #ifdef NETLINK_SKIP |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4397 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4398 | #endif |
| 4399 | #ifdef NETLINK_W1 |
| 4400 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
| 4401 | #endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4402 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4403 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4404 | #ifdef NETLINK_TCPDIAG |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4405 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4406 | #endif |
| 4407 | #ifdef NETLINK_NFLOG |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4408 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
Guido van Rossum | 668a94a | 2006-02-21 01:07:27 +0000 | [diff] [blame] | 4409 | #endif |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4410 | #ifdef NETLINK_XFRM |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4411 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
Neal Norwitz | 6585166 | 2006-01-16 04:31:40 +0000 | [diff] [blame] | 4412 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4413 | #ifdef NETLINK_ARPD |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4414 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4415 | #endif |
| 4416 | #ifdef NETLINK_ROUTE6 |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4417 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4418 | #endif |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4419 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
Martin v. Löwis | b1cc1d4 | 2007-02-13 12:14:19 +0000 | [diff] [blame] | 4420 | #ifdef NETLINK_DNRTMSG |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4421 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
Martin v. Löwis | b1cc1d4 | 2007-02-13 12:14:19 +0000 | [diff] [blame] | 4422 | #endif |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4423 | #ifdef NETLINK_TAPBASE |
Martin v. Löwis | 11017b1 | 2006-01-14 18:12:57 +0000 | [diff] [blame] | 4424 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
Martin v. Löwis | 5fe60e7 | 2006-04-06 22:29:33 +0000 | [diff] [blame] | 4425 | #endif |
Georg Brandl | dcfdae7 | 2006-04-01 07:33:08 +0000 | [diff] [blame] | 4426 | #endif /* AF_NETLINK */ |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 4427 | #ifdef AF_ROUTE |
| 4428 | /* Alias to emulate 4.4BSD */ |
| 4429 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
| 4430 | #endif |
| 4431 | #ifdef AF_ASH |
| 4432 | /* Ash */ |
| 4433 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
| 4434 | #endif |
| 4435 | #ifdef AF_ECONET |
| 4436 | /* Acorn Econet */ |
| 4437 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
| 4438 | #endif |
| 4439 | #ifdef AF_ATMSVC |
| 4440 | /* ATM SVCs */ |
| 4441 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
| 4442 | #endif |
| 4443 | #ifdef AF_SNA |
| 4444 | /* Linux SNA Project (nutters!) */ |
| 4445 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
| 4446 | #endif |
| 4447 | #ifdef AF_IRDA |
| 4448 | /* IRDA sockets */ |
| 4449 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
| 4450 | #endif |
| 4451 | #ifdef AF_PPPOX |
| 4452 | /* PPPoX sockets */ |
| 4453 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
| 4454 | #endif |
| 4455 | #ifdef AF_WANPIPE |
| 4456 | /* Wanpipe API Sockets */ |
| 4457 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
| 4458 | #endif |
| 4459 | #ifdef AF_LLC |
| 4460 | /* Linux LLC */ |
| 4461 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
| 4462 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4463 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4464 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4465 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4466 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
Martin v. Löwis | 45423a7 | 2007-02-14 10:07:37 +0000 | [diff] [blame] | 4467 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4468 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
| 4469 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
| 4470 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4471 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4472 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4473 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 4474 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4475 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
Neal Norwitz | 1e44ca9 | 2006-05-30 03:18:50 +0000 | [diff] [blame] | 4476 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4477 | 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] | 4478 | #endif |
| 4479 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 4480 | #ifdef HAVE_NETPACKET_PACKET_H |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4481 | PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); |
| 4482 | PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); |
| 4483 | PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); |
| 4484 | PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 4485 | PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 4486 | PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 4487 | PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 4488 | PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 4489 | PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4490 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4491 | |
| 4492 | /* Socket types */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4493 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4494 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4495 | #ifndef __BEOS__ |
| 4496 | /* We have incomplete socket support. */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4497 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4498 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4499 | #if defined(SOCK_RDM) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4500 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4501 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4502 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4503 | |
| 4504 | #ifdef SO_DEBUG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4505 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4506 | #endif |
| 4507 | #ifdef SO_ACCEPTCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4508 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4509 | #endif |
| 4510 | #ifdef SO_REUSEADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4511 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4512 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4513 | #ifdef SO_EXCLUSIVEADDRUSE |
| 4514 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
| 4515 | #endif |
| 4516 | |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4517 | #ifdef SO_KEEPALIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4518 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4519 | #endif |
| 4520 | #ifdef SO_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4521 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4522 | #endif |
| 4523 | #ifdef SO_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4524 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4525 | #endif |
| 4526 | #ifdef SO_USELOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4527 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4528 | #endif |
| 4529 | #ifdef SO_LINGER |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4530 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4531 | #endif |
| 4532 | #ifdef SO_OOBINLINE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4533 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4534 | #endif |
| 4535 | #ifdef SO_REUSEPORT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4536 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4537 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4538 | #ifdef SO_SNDBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4539 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4540 | #endif |
| 4541 | #ifdef SO_RCVBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4542 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4543 | #endif |
| 4544 | #ifdef SO_SNDLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4545 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4546 | #endif |
| 4547 | #ifdef SO_RCVLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4548 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4549 | #endif |
| 4550 | #ifdef SO_SNDTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4551 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4552 | #endif |
| 4553 | #ifdef SO_RCVTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4554 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4555 | #endif |
| 4556 | #ifdef SO_ERROR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4557 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4558 | #endif |
| 4559 | #ifdef SO_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4560 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4561 | #endif |
| 4562 | |
| 4563 | /* Maximum number of connections for "listen" */ |
| 4564 | #ifdef SOMAXCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4565 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4566 | #else |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4567 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4568 | #endif |
| 4569 | |
| 4570 | /* Flags for send, recv */ |
| 4571 | #ifdef MSG_OOB |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4572 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4573 | #endif |
| 4574 | #ifdef MSG_PEEK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4575 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4576 | #endif |
| 4577 | #ifdef MSG_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4578 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4579 | #endif |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4580 | #ifdef MSG_DONTWAIT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4581 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4582 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4583 | #ifdef MSG_EOR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4584 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4585 | #endif |
| 4586 | #ifdef MSG_TRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4587 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4588 | #endif |
| 4589 | #ifdef MSG_CTRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4590 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4591 | #endif |
| 4592 | #ifdef MSG_WAITALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4593 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4594 | #endif |
| 4595 | #ifdef MSG_BTAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4596 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4597 | #endif |
| 4598 | #ifdef MSG_ETAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4599 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4600 | #endif |
| 4601 | |
| 4602 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4603 | #ifdef SOL_SOCKET |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4604 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4605 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4606 | #ifdef SOL_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4607 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4608 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4609 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4610 | #endif |
| 4611 | #ifdef SOL_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4612 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4613 | #endif |
| 4614 | #ifdef SOL_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4615 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4616 | #endif |
| 4617 | #ifdef SOL_ATALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4618 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4619 | #endif |
| 4620 | #ifdef SOL_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4621 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4622 | #endif |
| 4623 | #ifdef SOL_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4624 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4625 | #endif |
| 4626 | #ifdef SOL_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4627 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4628 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4629 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4630 | #endif |
| 4631 | #ifdef SOL_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4632 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4633 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4634 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4635 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4636 | #ifdef IPPROTO_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4637 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4638 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4639 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4640 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4641 | #ifdef IPPROTO_HOPOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4642 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4643 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4644 | #ifdef IPPROTO_ICMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4645 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4646 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4647 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4648 | #endif |
| 4649 | #ifdef IPPROTO_IGMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4650 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4651 | #endif |
| 4652 | #ifdef IPPROTO_GGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4653 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4654 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4655 | #ifdef IPPROTO_IPV4 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4656 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4657 | #endif |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 4658 | #ifdef IPPROTO_IPV6 |
| 4659 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
| 4660 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4661 | #ifdef IPPROTO_IPIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4662 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4663 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4664 | #ifdef IPPROTO_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4665 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4666 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4667 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4668 | #endif |
| 4669 | #ifdef IPPROTO_EGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4670 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4671 | #endif |
| 4672 | #ifdef IPPROTO_PUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4673 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4674 | #endif |
| 4675 | #ifdef IPPROTO_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4676 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4677 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4678 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4679 | #endif |
| 4680 | #ifdef IPPROTO_IDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4681 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4682 | #endif |
| 4683 | #ifdef IPPROTO_HELLO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4684 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4685 | #endif |
| 4686 | #ifdef IPPROTO_ND |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4687 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4688 | #endif |
| 4689 | #ifdef IPPROTO_TP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4690 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4691 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4692 | #ifdef IPPROTO_IPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4693 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4694 | #endif |
| 4695 | #ifdef IPPROTO_ROUTING |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4696 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4697 | #endif |
| 4698 | #ifdef IPPROTO_FRAGMENT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4699 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4700 | #endif |
| 4701 | #ifdef IPPROTO_RSVP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4702 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4703 | #endif |
| 4704 | #ifdef IPPROTO_GRE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4705 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4706 | #endif |
| 4707 | #ifdef IPPROTO_ESP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4708 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4709 | #endif |
| 4710 | #ifdef IPPROTO_AH |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4711 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4712 | #endif |
| 4713 | #ifdef IPPROTO_MOBILE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4714 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4715 | #endif |
| 4716 | #ifdef IPPROTO_ICMPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4717 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4718 | #endif |
| 4719 | #ifdef IPPROTO_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4720 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4721 | #endif |
| 4722 | #ifdef IPPROTO_DSTOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4723 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4724 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4725 | #ifdef IPPROTO_XTP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4726 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4727 | #endif |
| 4728 | #ifdef IPPROTO_EON |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4729 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4730 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4731 | #ifdef IPPROTO_PIM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4732 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4733 | #endif |
| 4734 | #ifdef IPPROTO_IPCOMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4735 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4736 | #endif |
| 4737 | #ifdef IPPROTO_VRRP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4738 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4739 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4740 | #ifdef IPPROTO_BIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4741 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4742 | #endif |
| 4743 | /**/ |
| 4744 | #ifdef IPPROTO_RAW |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4745 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4746 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4747 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4748 | #endif |
| 4749 | #ifdef IPPROTO_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4750 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4751 | #endif |
| 4752 | |
| 4753 | /* Some port configuration */ |
| 4754 | #ifdef IPPORT_RESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4755 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4756 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4757 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4758 | #endif |
| 4759 | #ifdef IPPORT_USERRESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4760 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4761 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4762 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4763 | #endif |
| 4764 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4765 | /* Some reserved IP v.4 addresses */ |
| 4766 | #ifdef INADDR_ANY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4767 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4768 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4769 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4770 | #endif |
| 4771 | #ifdef INADDR_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4772 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4773 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4774 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4775 | #endif |
| 4776 | #ifdef INADDR_LOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4777 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4778 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4779 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4780 | #endif |
| 4781 | #ifdef INADDR_UNSPEC_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4782 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4783 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4784 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4785 | #endif |
| 4786 | #ifdef INADDR_ALLHOSTS_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4787 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 4788 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4789 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4790 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4791 | #endif |
| 4792 | #ifdef INADDR_MAX_LOCAL_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4793 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 4794 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4795 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4796 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4797 | #endif |
| 4798 | #ifdef INADDR_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4799 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4800 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4801 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4802 | #endif |
| 4803 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4804 | /* IPv4 [gs]etsockopt options */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4805 | #ifdef IP_OPTIONS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4806 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4807 | #endif |
| 4808 | #ifdef IP_HDRINCL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4809 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4810 | #endif |
| 4811 | #ifdef IP_TOS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4812 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4813 | #endif |
| 4814 | #ifdef IP_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4815 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4816 | #endif |
| 4817 | #ifdef IP_RECVOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4818 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4819 | #endif |
| 4820 | #ifdef IP_RECVRETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4821 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4822 | #endif |
| 4823 | #ifdef IP_RECVDSTADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4824 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4825 | #endif |
| 4826 | #ifdef IP_RETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4827 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4828 | #endif |
| 4829 | #ifdef IP_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4830 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4831 | #endif |
| 4832 | #ifdef IP_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4833 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4834 | #endif |
| 4835 | #ifdef IP_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4836 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4837 | #endif |
| 4838 | #ifdef IP_ADD_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4839 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4840 | #endif |
| 4841 | #ifdef IP_DROP_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4842 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4843 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4844 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4845 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 4846 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4847 | #endif |
| 4848 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4849 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 4850 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4851 | #endif |
| 4852 | #ifdef IP_MAX_MEMBERSHIPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4853 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4854 | #endif |
| 4855 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4856 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 4857 | #ifdef IPV6_JOIN_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4858 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4859 | #endif |
| 4860 | #ifdef IPV6_LEAVE_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4861 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4862 | #endif |
| 4863 | #ifdef IPV6_MULTICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4864 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4865 | #endif |
| 4866 | #ifdef IPV6_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4867 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4868 | #endif |
| 4869 | #ifdef IPV6_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4870 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4871 | #endif |
| 4872 | #ifdef IPV6_UNICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4873 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4874 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4875 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
| 4876 | #ifdef IPV6_V6ONLY |
| 4877 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
| 4878 | #endif |
| 4879 | /* Advanced IPV6 socket options, from RFC 3542 */ |
| 4880 | #ifdef IPV6_CHECKSUM |
| 4881 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
| 4882 | #endif |
| 4883 | #ifdef IPV6_DONTFRAG |
| 4884 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
| 4885 | #endif |
| 4886 | #ifdef IPV6_DSTOPTS |
| 4887 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
| 4888 | #endif |
| 4889 | #ifdef IPV6_HOPLIMIT |
| 4890 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
| 4891 | #endif |
| 4892 | #ifdef IPV6_HOPOPTS |
| 4893 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
| 4894 | #endif |
| 4895 | #ifdef IPV6_NEXTHOP |
| 4896 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
| 4897 | #endif |
| 4898 | #ifdef IPV6_PATHMTU |
| 4899 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
| 4900 | #endif |
| 4901 | #ifdef IPV6_PKTINFO |
| 4902 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
| 4903 | #endif |
| 4904 | #ifdef IPV6_RECVDSTOPTS |
| 4905 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
| 4906 | #endif |
| 4907 | #ifdef IPV6_RECVHOPLIMIT |
| 4908 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
| 4909 | #endif |
| 4910 | #ifdef IPV6_RECVHOPOPTS |
| 4911 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
| 4912 | #endif |
| 4913 | #ifdef IPV6_RECVPKTINFO |
| 4914 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
| 4915 | #endif |
| 4916 | #ifdef IPV6_RECVRTHDR |
| 4917 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
| 4918 | #endif |
| 4919 | #ifdef IPV6_RECVTCLASS |
| 4920 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
| 4921 | #endif |
| 4922 | #ifdef IPV6_RTHDR |
| 4923 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
| 4924 | #endif |
| 4925 | #ifdef IPV6_RTHDRDSTOPTS |
| 4926 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
| 4927 | #endif |
| 4928 | #ifdef IPV6_RTHDR_TYPE_0 |
| 4929 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); |
| 4930 | #endif |
| 4931 | #ifdef IPV6_RECVPATHMTU |
| 4932 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
| 4933 | #endif |
| 4934 | #ifdef IPV6_TCLASS |
| 4935 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
| 4936 | #endif |
| 4937 | #ifdef IPV6_USE_MIN_MTU |
| 4938 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); |
| 4939 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4940 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4941 | /* TCP options */ |
| 4942 | #ifdef TCP_NODELAY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4943 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4944 | #endif |
| 4945 | #ifdef TCP_MAXSEG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4946 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4947 | #endif |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4948 | #ifdef TCP_CORK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4949 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4950 | #endif |
| 4951 | #ifdef TCP_KEEPIDLE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4952 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4953 | #endif |
| 4954 | #ifdef TCP_KEEPINTVL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4955 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4956 | #endif |
| 4957 | #ifdef TCP_KEEPCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4958 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4959 | #endif |
| 4960 | #ifdef TCP_SYNCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4961 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4962 | #endif |
| 4963 | #ifdef TCP_LINGER2 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4964 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4965 | #endif |
| 4966 | #ifdef TCP_DEFER_ACCEPT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4967 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4968 | #endif |
| 4969 | #ifdef TCP_WINDOW_CLAMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4970 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4971 | #endif |
| 4972 | #ifdef TCP_INFO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4973 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4974 | #endif |
| 4975 | #ifdef TCP_QUICKACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4976 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4977 | #endif |
| 4978 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4979 | |
| 4980 | /* IPX options */ |
| 4981 | #ifdef IPX_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4982 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4983 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4984 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4985 | /* get{addr,name}info parameters */ |
| 4986 | #ifdef EAI_ADDRFAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4987 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4988 | #endif |
| 4989 | #ifdef EAI_AGAIN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4990 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4991 | #endif |
| 4992 | #ifdef EAI_BADFLAGS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4993 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4994 | #endif |
| 4995 | #ifdef EAI_FAIL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4996 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4997 | #endif |
| 4998 | #ifdef EAI_FAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4999 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5000 | #endif |
| 5001 | #ifdef EAI_MEMORY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5002 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5003 | #endif |
| 5004 | #ifdef EAI_NODATA |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5005 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5006 | #endif |
| 5007 | #ifdef EAI_NONAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5008 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5009 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5010 | #ifdef EAI_OVERFLOW |
| 5011 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
| 5012 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5013 | #ifdef EAI_SERVICE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5014 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5015 | #endif |
| 5016 | #ifdef EAI_SOCKTYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5017 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5018 | #endif |
| 5019 | #ifdef EAI_SYSTEM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5020 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5021 | #endif |
| 5022 | #ifdef EAI_BADHINTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5023 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5024 | #endif |
| 5025 | #ifdef EAI_PROTOCOL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5026 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5027 | #endif |
| 5028 | #ifdef EAI_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5029 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5030 | #endif |
| 5031 | #ifdef AI_PASSIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5032 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5033 | #endif |
| 5034 | #ifdef AI_CANONNAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5035 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5036 | #endif |
| 5037 | #ifdef AI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5038 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5039 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 5040 | #ifdef AI_NUMERICSERV |
| 5041 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
| 5042 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5043 | #ifdef AI_MASK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5044 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5045 | #endif |
| 5046 | #ifdef AI_ALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5047 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5048 | #endif |
| 5049 | #ifdef AI_V4MAPPED_CFG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5050 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5051 | #endif |
| 5052 | #ifdef AI_ADDRCONFIG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5053 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5054 | #endif |
| 5055 | #ifdef AI_V4MAPPED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5056 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5057 | #endif |
| 5058 | #ifdef AI_DEFAULT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5059 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5060 | #endif |
| 5061 | #ifdef NI_MAXHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5062 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5063 | #endif |
| 5064 | #ifdef NI_MAXSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5065 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5066 | #endif |
| 5067 | #ifdef NI_NOFQDN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5068 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5069 | #endif |
| 5070 | #ifdef NI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5071 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5072 | #endif |
| 5073 | #ifdef NI_NAMEREQD |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5074 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5075 | #endif |
| 5076 | #ifdef NI_NUMERICSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5077 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5078 | #endif |
| 5079 | #ifdef NI_DGRAM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 5080 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 5081 | #endif |
| 5082 | |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 5083 | /* shutdown() parameters */ |
| 5084 | #ifdef SHUT_RD |
| 5085 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
| 5086 | #elif defined(SD_RECEIVE) |
| 5087 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
| 5088 | #else |
| 5089 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
| 5090 | #endif |
| 5091 | #ifdef SHUT_WR |
| 5092 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
| 5093 | #elif defined(SD_SEND) |
| 5094 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
| 5095 | #else |
| 5096 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
| 5097 | #endif |
| 5098 | #ifdef SHUT_RDWR |
| 5099 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
| 5100 | #elif defined(SD_BOTH) |
| 5101 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
| 5102 | #else |
| 5103 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
| 5104 | #endif |
| 5105 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5106 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 5107 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 5108 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 5109 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 5110 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5111 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5112 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5113 | #ifndef HAVE_INET_PTON |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5114 | |
| 5115 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 5116 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5117 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 5118 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5119 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5120 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5121 | if (af == AF_INET) { |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5122 | long packed_addr; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5123 | packed_addr = inet_addr(src); |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5124 | if (packed_addr == INADDR_NONE) |
| 5125 | return 0; |
| 5126 | memcpy(dst, &packed_addr, 4); |
| 5127 | return 1; |
| 5128 | } |
| 5129 | /* Should set errno to EAFNOSUPPORT */ |
| 5130 | return -1; |
| 5131 | } |
| 5132 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 5133 | const char * |
| 5134 | 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] | 5135 | { |
| 5136 | if (af == AF_INET) { |
| 5137 | struct in_addr packed_addr; |
| 5138 | if (size < 16) |
| 5139 | /* Should set errno to ENOSPC. */ |
| 5140 | return NULL; |
| 5141 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5142 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5143 | } |
| 5144 | /* Should set errno to EAFNOSUPPORT */ |
| 5145 | return NULL; |
| 5146 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 5147 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 5148 | #endif |