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 |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 10 | portable manner, though AF_PACKET is 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 | |
Michael W. Hudson | 76b8cc8 | 2004-05-26 17:06:31 +0000 | [diff] [blame] | 64 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
| 65 | #define _BSD_TYPES |
| 66 | #endif |
| 67 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 68 | #include "Python.h" |
| 69 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 70 | #undef MAX |
| 71 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 72 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 73 | /* Socket object documentation */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 74 | PyDoc_STRVAR(sock_doc, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 75 | "socket([family[, type[, proto]]]) -> socket object\n\ |
| 76 | \n\ |
| 77 | Open a socket of the given type. The family argument specifies the\n\ |
| 78 | address family; it defaults to AF_INET. The type argument specifies\n\ |
| 79 | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
| 80 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
| 81 | specifying the default protocol. Keyword arguments are accepted.\n\ |
| 82 | \n\ |
| 83 | A socket object represents one endpoint of a network connection.\n\ |
| 84 | \n\ |
| 85 | Methods of socket objects (keyword arguments not allowed):\n\ |
| 86 | \n\ |
| 87 | accept() -- accept a connection, returning new socket and client address\n\ |
| 88 | bind(addr) -- bind the socket to a local address\n\ |
| 89 | close() -- close the socket\n\ |
| 90 | connect(addr) -- connect the socket to a remote address\n\ |
| 91 | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
| 92 | dup() -- return a new socket object identical to the current one [*]\n\ |
| 93 | fileno() -- return underlying file descriptor\n\ |
| 94 | getpeername() -- return remote address [*]\n\ |
| 95 | getsockname() -- return local address\n\ |
| 96 | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
| 97 | gettimeout() -- return timeout or None\n\ |
| 98 | listen(n) -- start listening for incoming connections\n\ |
| 99 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\ |
| 100 | recv(buflen[, flags]) -- receive data\n\ |
| 101 | recvfrom(buflen[, flags]) -- receive data and sender's address\n\ |
| 102 | sendall(data[, flags]) -- send all data\n\ |
| 103 | send(data[, flags]) -- send data, may not send all of it\n\ |
| 104 | sendto(data[, flags], addr) -- send data to a given address\n\ |
| 105 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
| 106 | setsockopt(level, optname, value) -- set socket options\n\ |
| 107 | settimeout(None | float) -- set or clear the timeout\n\ |
| 108 | shutdown(how) -- shut down traffic in one or both directions\n\ |
| 109 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 110 | [*] not available on all platforms!"); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 111 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 112 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 113 | I hope some day someone can clean this up please... */ |
| 114 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 115 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
| 116 | script doesn't get this right, so we hardcode some platform checks below. |
| 117 | On the other hand, not all Linux versions agree, so there the settings |
| 118 | computed by the configure script are needed! */ |
| 119 | |
| 120 | #ifndef linux |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 121 | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
| 122 | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
| 123 | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 124 | #endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 126 | #ifndef WITH_THREAD |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 127 | # undef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7a12299 | 1999-04-13 04:07:32 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 130 | #ifdef HAVE_GETHOSTBYNAME_R |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 131 | # if defined(_AIX) || defined(__osf__) |
| 132 | # define HAVE_GETHOSTBYNAME_R_3_ARG |
| 133 | # elif defined(__sun) || defined(__sgi) |
| 134 | # define HAVE_GETHOSTBYNAME_R_5_ARG |
| 135 | # elif defined(linux) |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 136 | /* Rely on the configure script */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 137 | # else |
| 138 | # undef HAVE_GETHOSTBYNAME_R |
| 139 | # endif |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 142 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
| 143 | !defined(MS_WINDOWS) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 144 | # define USE_GETHOSTBYNAME_LOCK |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 145 | #endif |
| 146 | |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 147 | /* 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] | 148 | (this includes the getaddrinfo emulation) protect access with a lock. */ |
Just van Rossum | 16e426b | 2003-05-09 08:12:00 +0000 | [diff] [blame] | 149 | #if defined(WITH_THREAD) && (defined(__APPLE__) || defined(__FreeBSD__) || \ |
| 150 | defined(__OpenBSD__) || defined(__NetBSD__) || !defined(HAVE_GETADDRINFO)) |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 151 | #define USE_GETADDRINFO_LOCK |
| 152 | #endif |
| 153 | |
| 154 | #ifdef USE_GETADDRINFO_LOCK |
| 155 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
| 156 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
| 157 | #else |
| 158 | #define ACQUIRE_GETADDRINFO_LOCK |
| 159 | #define RELEASE_GETADDRINFO_LOCK |
| 160 | #endif |
| 161 | |
| 162 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 163 | # include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 164 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 166 | #if defined(PYCC_VACPP) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 167 | # include <types.h> |
| 168 | # include <io.h> |
| 169 | # include <sys/ioctl.h> |
| 170 | # include <utils.h> |
| 171 | # include <ctype.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 172 | #endif |
| 173 | |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 174 | #if defined(__VMS) |
| 175 | #if ! defined(_SOCKADDR_LEN) |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 176 | # ifdef getaddrinfo |
| 177 | # undef getaddrinfo |
| 178 | # endif |
| 179 | # include "TCPIP_IOCTL_ROUTINE" |
| 180 | #else |
| 181 | # include <ioctl.h> |
| 182 | #endif |
Martin v. Löwis | 9e43730 | 2002-12-06 12:57:26 +0000 | [diff] [blame] | 183 | #endif |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 185 | #if defined(PYOS_OS2) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 186 | # define INCL_DOS |
| 187 | # define INCL_DOSERRORS |
| 188 | # define INCL_NOPMAPI |
| 189 | # include <os2.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 190 | #endif |
| 191 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 192 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 193 | /* make sure that the reentrant (gethostbyaddr_r etc) |
| 194 | functions are declared correctly if compiling with |
| 195 | MIPSPro 7.x in ANSI C mode (default) */ |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 196 | |
| 197 | /* XXX Using _SGIAPI is the wrong thing, |
| 198 | but I don't know what the right thing is. */ |
Michael W. Hudson | 76b8cc8 | 2004-05-26 17:06:31 +0000 | [diff] [blame] | 199 | #undef _SGIAPI /* to avoid warning */ |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 200 | #define _SGIAPI 1 |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 201 | |
Michael W. Hudson | 76b8cc8 | 2004-05-26 17:06:31 +0000 | [diff] [blame] | 202 | #undef _XOPEN_SOURCE |
| 203 | #include <sys/socket.h> |
| 204 | #include <sys/types.h> |
| 205 | #include <netinet/in.h> |
| 206 | #ifdef _SS_ALIGNSIZE |
| 207 | #define HAVE_GETADDRINFO 1 |
| 208 | #define HAVE_GETNAMEINFO 1 |
| 209 | #endif |
| 210 | |
Jeremy Hylton | fb509a3 | 2003-07-17 16:58:48 +0000 | [diff] [blame] | 211 | #define HAVE_INET_PTON |
| 212 | #include <netdb.h> |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 213 | #endif |
| 214 | |
Anthony Baxter | bab23cf | 2003-10-04 08:00:49 +0000 | [diff] [blame] | 215 | /* Irix 6.5 fails to define this variable at all. This is needed |
| 216 | for both GCC and SGI's compiler. I'd say that the SGI headers |
| 217 | are just busted. */ |
| 218 | #if defined(__sgi) && !defined(INET_ADDRSTRLEN) |
| 219 | #define INET_ADDRSTRLEN 16 |
| 220 | #endif |
| 221 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 222 | /* Generic includes */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | #include <sys/types.h> |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 224 | #include <signal.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 225 | |
Marc-André Lemburg | 976ade6 | 2002-02-16 18:47:07 +0000 | [diff] [blame] | 226 | /* Generic socket object definitions and includes */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 227 | #define PySocket_BUILDING_SOCKET |
Marc-André Lemburg | bb8b78b | 2002-02-16 18:44:52 +0000 | [diff] [blame] | 228 | #include "socketmodule.h" |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 229 | |
| 230 | /* Addressing includes */ |
| 231 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 232 | #ifndef MS_WINDOWS |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 233 | |
| 234 | /* Non-MS WINDOWS includes */ |
| 235 | # include <netdb.h> |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 9376b74 | 1999-09-15 22:01:40 +0000 | [diff] [blame] | 237 | /* Headers needed for inet_ntoa() and inet_addr() */ |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 238 | # ifdef __BEOS__ |
| 239 | # include <net/netdb.h> |
| 240 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 241 | # include <netdb.h> |
Tim Peters | 603c683 | 2001-11-05 02:45:59 +0000 | [diff] [blame] | 242 | typedef size_t socklen_t; |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 243 | # else |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 244 | # include <arpa/inet.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 245 | # endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 246 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 247 | # ifndef RISCOS |
| 248 | # include <fcntl.h> |
| 249 | # else |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 250 | # include <sys/ioctl.h> |
| 251 | # include <socklib.h> |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 252 | # define NO_DUP |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 253 | int h_errno; /* not used */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 254 | # define INET_ADDRSTRLEN 16 |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 255 | # endif |
| 256 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 257 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 258 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 259 | /* MS_WINDOWS includes */ |
| 260 | # include <fcntl.h> |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 261 | |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 262 | #endif |
| 263 | |
Skip Montanaro | 7befb99 | 2004-02-10 16:50:21 +0000 | [diff] [blame] | 264 | #include <stddef.h> |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 265 | |
| 266 | #ifndef offsetof |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 267 | # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
Martin v. Löwis | a45ecae | 2001-06-24 21:28:42 +0000 | [diff] [blame] | 268 | #endif |
| 269 | |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 270 | #ifndef O_NONBLOCK |
| 271 | # define O_NONBLOCK O_NDELAY |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 272 | #endif |
| 273 | |
Michael W. Hudson | 76b8cc8 | 2004-05-26 17:06:31 +0000 | [diff] [blame] | 274 | #if defined(__sgi) && _COMPILER_VERSION>700 \ |
| 275 | && !defined(_SS_ALIGNSIZE) /* defined in sys/socket.h */ |
| 276 | /* by some newer versions of IRIX */ |
| 277 | /* (e.g. not by 6.5.10 but by 6.5.21) */ |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 278 | #include "addrinfo.h" |
Michael W. Hudson | 76b8cc8 | 2004-05-26 17:06:31 +0000 | [diff] [blame] | 279 | #endif |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 280 | |
Andrew MacIntyre | 5a8b459 | 2004-07-07 14:02:59 +0000 | [diff] [blame] | 281 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 282 | #include "addrinfo.h" |
| 283 | #endif |
| 284 | |
Raymond Hettinger | cbcff93 | 2004-08-03 08:52:46 +0000 | [diff] [blame] | 285 | #if defined(_MSC_VER) && _MSC_VER == 1200 |
| 286 | #include "addrinfo.h" |
| 287 | #endif |
| 288 | |
Jason Tishler | c246cb7 | 2004-08-09 13:25:59 +0000 | [diff] [blame] | 289 | #if defined(__CYGWIN__) |
| 290 | #include "addrinfo.h" |
| 291 | #endif |
| 292 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 293 | #ifndef HAVE_INET_PTON |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 294 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 295 | 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] | 296 | #endif |
| 297 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 298 | #ifdef __APPLE__ |
| 299 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 300 | failure, so we must use the emulation instead of the libinfo |
| 301 | implementation. Unfortunately, performing an autoconf test |
| 302 | for this bug would require DNS access for the machine performing |
| 303 | the configuration, which is not acceptable. Therefore, we |
| 304 | determine the bug just by checking for __APPLE__. If this bug |
| 305 | gets ever fixed, perhaps checking for sys/version.h would be |
| 306 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 307 | #ifndef HAVE_GETNAMEINFO |
| 308 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 309 | Find to check for Jaguar is that it has getnameinfo(), which |
| 310 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 311 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 312 | #endif |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 313 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 314 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 315 | /* 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] | 316 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 317 | /* avoid clashes with the C library definition of the symbol. */ |
| 318 | #define getaddrinfo fake_getaddrinfo |
| 319 | #define gai_strerror fake_gai_strerror |
| 320 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 321 | #include "getaddrinfo.c" |
| 322 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 323 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 324 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 325 | #include "getnameinfo.c" |
| 326 | #endif |
| 327 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 328 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 329 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 330 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 331 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 332 | #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] | 333 | #endif |
| 334 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 335 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 336 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 337 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 338 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 339 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 340 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 341 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 342 | #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] | 343 | #endif |
| 344 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 345 | #ifndef SOCKETCLOSE |
| 346 | #define SOCKETCLOSE close |
| 347 | #endif |
| 348 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 349 | #ifdef __VMS |
| 350 | /* TCP/IP Services for VMS uses a maximum send/revc buffer length of 65535 */ |
| 351 | #define SEGMENT_SIZE 65535 |
| 352 | #endif |
| 353 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 354 | #if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) |
| 355 | #define USE_BLUETOOTH 1 |
| 356 | #if defined(__FreeBSD__) |
| 357 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 358 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
| 359 | #define sockaddr_l2 sockaddr_l2cap |
| 360 | #define sockaddr_rc sockaddr_rfcomm |
| 361 | #define _BT_SOCKADDR_MEMB(s, proto) &((s)->sock_addr) |
| 362 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 363 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
| 364 | #else |
Anthony Baxter | 5d7c067 | 2004-02-16 05:35:28 +0000 | [diff] [blame] | 365 | #define _BT_SOCKADDR_MEMB(s, proto) (&((s)->sock_addr).bt_##proto) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 366 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 367 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
| 368 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 369 | #endif |
| 370 | #endif |
| 371 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 372 | /* |
| 373 | * Constants for getnameinfo() |
| 374 | */ |
| 375 | #if !defined(NI_MAXHOST) |
| 376 | #define NI_MAXHOST 1025 |
| 377 | #endif |
| 378 | #if !defined(NI_MAXSERV) |
| 379 | #define NI_MAXSERV 32 |
| 380 | #endif |
| 381 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 382 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 383 | a Py prefix (or use CapitalizedWords). Later... */ |
| 384 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 385 | /* Global variable holding the exception type for errors detected |
| 386 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 387 | static PyObject *socket_error; |
| 388 | static PyObject *socket_herror; |
| 389 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 390 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 392 | #ifdef RISCOS |
| 393 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 394 | static int taskwindow; |
| 395 | #endif |
| 396 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 397 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 398 | The sock_type variable contains pointers to various functions, |
| 399 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 400 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 401 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 402 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 403 | /* Convenience function to raise an error according to errno |
| 404 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 406 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 407 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 408 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 409 | #ifdef MS_WINDOWS |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 410 | int err_no = WSAGetLastError(); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 411 | static struct { |
| 412 | int no; |
| 413 | const char *msg; |
| 414 | } *msgp, msgs[] = { |
| 415 | {WSAEINTR, "Interrupted system call"}, |
| 416 | {WSAEBADF, "Bad file descriptor"}, |
| 417 | {WSAEACCES, "Permission denied"}, |
| 418 | {WSAEFAULT, "Bad address"}, |
| 419 | {WSAEINVAL, "Invalid argument"}, |
| 420 | {WSAEMFILE, "Too many open files"}, |
| 421 | {WSAEWOULDBLOCK, |
| 422 | "The socket operation could not complete " |
| 423 | "without blocking"}, |
| 424 | {WSAEINPROGRESS, "Operation now in progress"}, |
| 425 | {WSAEALREADY, "Operation already in progress"}, |
| 426 | {WSAENOTSOCK, "Socket operation on non-socket"}, |
| 427 | {WSAEDESTADDRREQ, "Destination address required"}, |
| 428 | {WSAEMSGSIZE, "Message too long"}, |
| 429 | {WSAEPROTOTYPE, "Protocol wrong type for socket"}, |
| 430 | {WSAENOPROTOOPT, "Protocol not available"}, |
| 431 | {WSAEPROTONOSUPPORT, "Protocol not supported"}, |
| 432 | {WSAESOCKTNOSUPPORT, "Socket type not supported"}, |
| 433 | {WSAEOPNOTSUPP, "Operation not supported"}, |
| 434 | {WSAEPFNOSUPPORT, "Protocol family not supported"}, |
| 435 | {WSAEAFNOSUPPORT, "Address family not supported"}, |
| 436 | {WSAEADDRINUSE, "Address already in use"}, |
| 437 | {WSAEADDRNOTAVAIL, "Can't assign requested address"}, |
| 438 | {WSAENETDOWN, "Network is down"}, |
| 439 | {WSAENETUNREACH, "Network is unreachable"}, |
| 440 | {WSAENETRESET, "Network dropped connection on reset"}, |
| 441 | {WSAECONNABORTED, "Software caused connection abort"}, |
| 442 | {WSAECONNRESET, "Connection reset by peer"}, |
| 443 | {WSAENOBUFS, "No buffer space available"}, |
| 444 | {WSAEISCONN, "Socket is already connected"}, |
| 445 | {WSAENOTCONN, "Socket is not connected"}, |
| 446 | {WSAESHUTDOWN, "Can't send after socket shutdown"}, |
| 447 | {WSAETOOMANYREFS, "Too many references: can't splice"}, |
| 448 | {WSAETIMEDOUT, "Operation timed out"}, |
| 449 | {WSAECONNREFUSED, "Connection refused"}, |
| 450 | {WSAELOOP, "Too many levels of symbolic links"}, |
| 451 | {WSAENAMETOOLONG, "File name too long"}, |
| 452 | {WSAEHOSTDOWN, "Host is down"}, |
| 453 | {WSAEHOSTUNREACH, "No route to host"}, |
| 454 | {WSAENOTEMPTY, "Directory not empty"}, |
| 455 | {WSAEPROCLIM, "Too many processes"}, |
| 456 | {WSAEUSERS, "Too many users"}, |
| 457 | {WSAEDQUOT, "Disc quota exceeded"}, |
| 458 | {WSAESTALE, "Stale NFS file handle"}, |
| 459 | {WSAEREMOTE, "Too many levels of remote in path"}, |
| 460 | {WSASYSNOTREADY, "Network subsystem is unvailable"}, |
| 461 | {WSAVERNOTSUPPORTED, "WinSock version is not supported"}, |
| 462 | {WSANOTINITIALISED, |
| 463 | "Successful WSAStartup() not yet performed"}, |
| 464 | {WSAEDISCON, "Graceful shutdown in progress"}, |
| 465 | /* Resolver errors */ |
| 466 | {WSAHOST_NOT_FOUND, "No such host is known"}, |
| 467 | {WSATRY_AGAIN, "Host not found, or server failed"}, |
| 468 | {WSANO_RECOVERY, "Unexpected server error encountered"}, |
| 469 | {WSANO_DATA, "Valid name without requested data"}, |
| 470 | {WSANO_ADDRESS, "No address, look for MX record"}, |
| 471 | {0, NULL} |
| 472 | }; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 473 | if (err_no) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 474 | PyObject *v; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 475 | const char *msg = "winsock error"; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 476 | |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 477 | for (msgp = msgs; msgp->msg; msgp++) { |
| 478 | if (err_no == msgp->no) { |
| 479 | msg = msgp->msg; |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | v = Py_BuildValue("(is)", err_no, msg); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 485 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 486 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 487 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 488 | } |
| 489 | return NULL; |
| 490 | } |
| 491 | else |
| 492 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 493 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 494 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 495 | if (sock_errno() != NO_ERROR) { |
| 496 | APIRET rc; |
| 497 | ULONG msglen; |
| 498 | char outbuf[100]; |
| 499 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 500 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 501 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 502 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 503 | myerrorcode - SOCBASEERR + 26, |
| 504 | "mptn.msg", |
| 505 | &msglen); |
| 506 | if (rc == NO_ERROR) { |
| 507 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 509 | /* OS/2 doesn't guarantee a terminator */ |
| 510 | outbuf[msglen] = '\0'; |
| 511 | if (strlen(outbuf) > 0) { |
| 512 | /* If non-empty msg, trim CRLF */ |
| 513 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 514 | while (lastc > outbuf && isspace(*lastc)) { |
| 515 | /* Trim trailing whitespace (CRLF) */ |
| 516 | *lastc-- = '\0'; |
| 517 | } |
| 518 | } |
| 519 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 520 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 521 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 522 | Py_DECREF(v); |
| 523 | } |
| 524 | return NULL; |
| 525 | } |
| 526 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 527 | #endif |
| 528 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 529 | #if defined(RISCOS) |
| 530 | if (_inet_error.errnum != NULL) { |
| 531 | PyObject *v; |
| 532 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 533 | if (v != NULL) { |
| 534 | PyErr_SetObject(socket_error, v); |
| 535 | Py_DECREF(v); |
| 536 | } |
| 537 | return NULL; |
| 538 | } |
| 539 | #endif |
| 540 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 541 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 544 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 545 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 546 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 547 | { |
| 548 | PyObject *v; |
| 549 | |
| 550 | #ifdef HAVE_HSTRERROR |
| 551 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
| 552 | #else |
| 553 | v = Py_BuildValue("(is)", h_error, "host not found"); |
| 554 | #endif |
| 555 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 556 | PyErr_SetObject(socket_herror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 557 | Py_DECREF(v); |
| 558 | } |
| 559 | |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | |
| 564 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 565 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 566 | { |
| 567 | PyObject *v; |
| 568 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 569 | #ifdef EAI_SYSTEM |
| 570 | /* EAI_SYSTEM is not available on Windows XP. */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 571 | if (error == EAI_SYSTEM) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 572 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 573 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 574 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 575 | #ifdef HAVE_GAI_STRERROR |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 576 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 577 | #else |
| 578 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
| 579 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 580 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 581 | PyErr_SetObject(socket_gaierror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 582 | Py_DECREF(v); |
| 583 | } |
| 584 | |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 588 | /* Function to perform the setting of socket blocking mode |
| 589 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 590 | static int |
| 591 | internal_setblocking(PySocketSockObject *s, int block) |
| 592 | { |
| 593 | #ifndef RISCOS |
| 594 | #ifndef MS_WINDOWS |
| 595 | int delay_flag; |
| 596 | #endif |
| 597 | #endif |
| 598 | |
| 599 | Py_BEGIN_ALLOW_THREADS |
| 600 | #ifdef __BEOS__ |
| 601 | block = !block; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 602 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 603 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 604 | #else |
| 605 | #ifndef RISCOS |
| 606 | #ifndef MS_WINDOWS |
| 607 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 608 | block = !block; |
| 609 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 610 | #elif defined(__VMS) |
| 611 | block = !block; |
| 612 | ioctl(s->sock_fd, FIONBIO, (char *)&block); |
| 613 | #else /* !PYOS_OS2 && !_VMS */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 614 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 615 | if (block) |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 616 | delay_flag &= (~O_NONBLOCK); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 617 | else |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 618 | delay_flag |= O_NONBLOCK; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 619 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
| 620 | #endif /* !PYOS_OS2 */ |
| 621 | #else /* MS_WINDOWS */ |
| 622 | block = !block; |
| 623 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
| 624 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 625 | #else /* RISCOS */ |
| 626 | block = !block; |
| 627 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 628 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 629 | #endif /* __BEOS__ */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 630 | Py_END_ALLOW_THREADS |
| 631 | |
| 632 | /* Since these don't return anything */ |
| 633 | return 1; |
| 634 | } |
| 635 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 636 | /* Do a select() on the socket, if necessary (sock_timeout > 0). |
| 637 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 638 | This does not raise an exception; we'll let our caller do that |
| 639 | after they've reacquired the interpreter lock. |
| 640 | Returns 1 on timeout, 0 otherwise. */ |
| 641 | static int |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 642 | internal_select(PySocketSockObject *s, int writing) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 643 | { |
| 644 | fd_set fds; |
| 645 | struct timeval tv; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 646 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 648 | /* 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] | 649 | if (s->sock_timeout <= 0.0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 650 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 651 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 652 | /* Guard against closed socket */ |
| 653 | if (s->sock_fd < 0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 654 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 655 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 656 | /* Construct the arguments to select */ |
| 657 | tv.tv_sec = (int)s->sock_timeout; |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 658 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 659 | FD_ZERO(&fds); |
| 660 | FD_SET(s->sock_fd, &fds); |
| 661 | |
| 662 | /* See if the socket is ready */ |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 663 | if (writing) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 664 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 665 | else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 666 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 667 | if (n == 0) |
| 668 | return 1; |
| 669 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 672 | /* Initialize a new socket object. */ |
| 673 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 674 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 675 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 676 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 677 | init_sockobject(PySocketSockObject *s, |
| 678 | SOCKET_T fd, int family, int type, int proto) |
| 679 | { |
| 680 | #ifdef RISCOS |
| 681 | int block = 1; |
| 682 | #endif |
| 683 | s->sock_fd = fd; |
| 684 | s->sock_family = family; |
| 685 | s->sock_type = type; |
| 686 | s->sock_proto = proto; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 687 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 688 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 689 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 690 | |
| 691 | if (defaulttimeout >= 0.0) |
| 692 | internal_setblocking(s, 0); |
| 693 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 694 | #ifdef RISCOS |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 695 | if (taskwindow) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 696 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 697 | #endif |
| 698 | } |
| 699 | |
| 700 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 701 | /* Create a new socket object. |
| 702 | This just creates the object and initializes it. |
| 703 | If the creation fails, return NULL and set an exception (implicit |
| 704 | in NEWOBJ()). */ |
| 705 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 706 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 707 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 708 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 709 | PySocketSockObject *s; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 710 | s = (PySocketSockObject *) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 711 | PyType_GenericNew(&sock_type, NULL, NULL); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 712 | if (s != NULL) |
| 713 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 714 | return s; |
| 715 | } |
| 716 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 717 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 718 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 719 | thread to be in gethostbyname or getaddrinfo */ |
| 720 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 721 | PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 722 | #endif |
| 723 | |
| 724 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 725 | /* Convert a string specifying a host name or one of a few symbolic |
| 726 | names to a numeric IP address. This usually calls gethostbyname() |
| 727 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 728 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 729 | an error occurred; then an exception is raised. */ |
| 730 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 731 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 732 | 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] | 733 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 734 | struct addrinfo hints, *res; |
| 735 | int error; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 736 | int d1, d2, d3, d4; |
| 737 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 739 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 740 | if (name[0] == '\0') { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 741 | int siz; |
| 742 | memset(&hints, 0, sizeof(hints)); |
| 743 | hints.ai_family = af; |
| 744 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 745 | hints.ai_flags = AI_PASSIVE; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 746 | Py_BEGIN_ALLOW_THREADS |
| 747 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 748 | error = getaddrinfo(NULL, "0", &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 749 | Py_END_ALLOW_THREADS |
| 750 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 751 | *are* safe regarding their return value, ie. that a |
| 752 | subsequent call to getaddrinfo() does not destroy the |
| 753 | outcome of the first call. */ |
| 754 | RELEASE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 755 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 756 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 757 | return -1; |
| 758 | } |
| 759 | switch (res->ai_family) { |
| 760 | case AF_INET: |
| 761 | siz = 4; |
| 762 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 763 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 764 | case AF_INET6: |
| 765 | siz = 16; |
| 766 | break; |
| 767 | #endif |
| 768 | default: |
| 769 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 770 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 771 | "unsupported address family"); |
| 772 | return -1; |
| 773 | } |
| 774 | if (res->ai_next) { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 775 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 776 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 777 | "wildcard resolved to multiple address"); |
| 778 | return -1; |
| 779 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 780 | if (res->ai_addrlen < addr_ret_size) |
| 781 | addr_ret_size = res->ai_addrlen; |
| 782 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 783 | freeaddrinfo(res); |
| 784 | return siz; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 785 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 786 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 787 | struct sockaddr_in *sin; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 788 | if (af != AF_INET && af != AF_UNSPEC) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 789 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 790 | "address family mismatched"); |
| 791 | return -1; |
| 792 | } |
| 793 | sin = (struct sockaddr_in *)addr_ret; |
| 794 | memset((void *) sin, '\0', sizeof(*sin)); |
| 795 | sin->sin_family = AF_INET; |
| 796 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 797 | sin->sin_len = sizeof(*sin); |
| 798 | #endif |
| 799 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 800 | return sizeof(sin->sin_addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 801 | } |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 802 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 803 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 804 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 805 | struct sockaddr_in *sin; |
| 806 | sin = (struct sockaddr_in *)addr_ret; |
| 807 | sin->sin_addr.s_addr = htonl( |
| 808 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 809 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 810 | sin->sin_family = AF_INET; |
| 811 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 812 | sin->sin_len = sizeof(*sin); |
| 813 | #endif |
| 814 | return 4; |
| 815 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 816 | memset(&hints, 0, sizeof(hints)); |
| 817 | hints.ai_family = af; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 818 | Py_BEGIN_ALLOW_THREADS |
| 819 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 820 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 821 | #if defined(__digital__) && defined(__unix__) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 822 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 823 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 824 | if no address family is given. Assume IPv4 for now.*/ |
| 825 | hints.ai_family = AF_INET; |
| 826 | error = getaddrinfo(name, NULL, &hints, &res); |
| 827 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 828 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 829 | Py_END_ALLOW_THREADS |
| 830 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 831 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 832 | set_gaierror(error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 833 | return -1; |
| 834 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 835 | if (res->ai_addrlen < addr_ret_size) |
| 836 | addr_ret_size = res->ai_addrlen; |
| 837 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 838 | freeaddrinfo(res); |
| 839 | switch (addr_ret->sa_family) { |
| 840 | case AF_INET: |
| 841 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 842 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 843 | case AF_INET6: |
| 844 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 845 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 846 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 847 | PyErr_SetString(socket_error, "unknown address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 848 | return -1; |
| 849 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 853 | /* Create a string object representing an IP address. |
| 854 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 855 | size numbers). */ |
| 856 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 857 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 858 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 859 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 860 | char buf[NI_MAXHOST]; |
| 861 | int error; |
| 862 | |
| 863 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 864 | NI_NUMERICHOST); |
| 865 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 866 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 867 | return NULL; |
| 868 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 869 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 873 | #ifdef USE_BLUETOOTH |
| 874 | /* Convert a string representation of a Bluetooth address into a numeric |
| 875 | address. Returns the length (6), or raises an exception and returns -1 if |
| 876 | an error occurred. */ |
| 877 | |
| 878 | static int |
| 879 | setbdaddr(char *name, bdaddr_t *bdaddr) |
| 880 | { |
| 881 | unsigned int b0, b1, b2, b3, b4, b5; |
| 882 | char ch; |
| 883 | int n; |
| 884 | |
| 885 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 886 | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 887 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 888 | bdaddr->b[0] = b0; |
| 889 | bdaddr->b[1] = b1; |
| 890 | bdaddr->b[2] = b2; |
| 891 | bdaddr->b[3] = b3; |
| 892 | bdaddr->b[4] = b4; |
| 893 | bdaddr->b[5] = b5; |
| 894 | return 6; |
| 895 | } else { |
| 896 | PyErr_SetString(socket_error, "bad bluetooth address"); |
| 897 | return -1; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /* Create a string representation of the Bluetooth address. This is always a |
| 902 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 903 | value (zero padded if necessary). */ |
| 904 | |
| 905 | static PyObject * |
| 906 | makebdaddr(bdaddr_t *bdaddr) |
| 907 | { |
| 908 | char buf[(6 * 2) + 5 + 1]; |
| 909 | |
| 910 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 911 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 912 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 913 | return PyString_FromString(buf); |
| 914 | } |
| 915 | #endif |
| 916 | |
| 917 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 918 | /* Create an object representing the given socket address, |
| 919 | suitable for passing it back to bind(), connect() etc. |
| 920 | The family field of the sockaddr structure is inspected |
| 921 | to determine what kind of address it really is. */ |
| 922 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 923 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 924 | static PyObject * |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 925 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 926 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 927 | if (addrlen == 0) { |
| 928 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 929 | Py_INCREF(Py_None); |
| 930 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 933 | #ifdef __BEOS__ |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 934 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 935 | addr->sa_family = AF_INET; |
| 936 | #endif |
| 937 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 938 | switch (addr->sa_family) { |
| 939 | |
| 940 | case AF_INET: |
| 941 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 942 | struct sockaddr_in *a; |
| 943 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 944 | PyObject *ret = NULL; |
| 945 | if (addrobj) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 946 | a = (struct sockaddr_in *)addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 947 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 948 | Py_DECREF(addrobj); |
| 949 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 950 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 951 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 952 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 953 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 954 | case AF_UNIX: |
| 955 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 956 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 957 | return PyString_FromString(a->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 958 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 959 | #endif /* AF_UNIX */ |
| 960 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 961 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 962 | case AF_INET6: |
| 963 | { |
| 964 | struct sockaddr_in6 *a; |
| 965 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 966 | PyObject *ret = NULL; |
| 967 | if (addrobj) { |
| 968 | a = (struct sockaddr_in6 *)addr; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 969 | ret = Py_BuildValue("Oiii", |
| 970 | addrobj, |
| 971 | ntohs(a->sin6_port), |
| 972 | a->sin6_flowinfo, |
| 973 | a->sin6_scope_id); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 974 | Py_DECREF(addrobj); |
| 975 | } |
| 976 | return ret; |
| 977 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 978 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 979 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 980 | #ifdef USE_BLUETOOTH |
| 981 | case AF_BLUETOOTH: |
| 982 | switch (proto) { |
| 983 | |
| 984 | case BTPROTO_L2CAP: |
| 985 | { |
| 986 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 987 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 988 | PyObject *ret = NULL; |
| 989 | if (addrobj) { |
| 990 | ret = Py_BuildValue("Oi", |
| 991 | addrobj, |
| 992 | _BT_L2_MEMB(a, psm)); |
| 993 | Py_DECREF(addrobj); |
| 994 | } |
| 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | case BTPROTO_RFCOMM: |
| 999 | { |
| 1000 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1001 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1002 | PyObject *ret = NULL; |
| 1003 | if (addrobj) { |
| 1004 | ret = Py_BuildValue("Oi", |
| 1005 | addrobj, |
| 1006 | _BT_RC_MEMB(a, channel)); |
| 1007 | Py_DECREF(addrobj); |
| 1008 | } |
| 1009 | return ret; |
| 1010 | } |
| 1011 | |
| 1012 | #if !defined(__FreeBSD__) |
| 1013 | case BTPROTO_SCO: |
| 1014 | { |
| 1015 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1016 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
| 1020 | } |
| 1021 | #endif |
| 1022 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1023 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1024 | case AF_PACKET: |
| 1025 | { |
| 1026 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1027 | char *ifname = ""; |
| 1028 | struct ifreq ifr; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1029 | /* need to look up interface name give index */ |
| 1030 | if (a->sll_ifindex) { |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1031 | ifr.ifr_ifindex = a->sll_ifindex; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1032 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1033 | ifname = ifr.ifr_name; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1034 | } |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1035 | return Py_BuildValue("shbhs#", |
| 1036 | ifname, |
| 1037 | ntohs(a->sll_protocol), |
| 1038 | a->sll_pkttype, |
| 1039 | a->sll_hatype, |
| 1040 | a->sll_addr, |
| 1041 | a->sll_halen); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1042 | } |
| 1043 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1044 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1045 | /* More cases here... */ |
| 1046 | |
| 1047 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 1048 | /* If we don't know the address family, don't raise an |
| 1049 | exception -- return it as a tuple. */ |
| 1050 | return Py_BuildValue("is#", |
| 1051 | addr->sa_family, |
| 1052 | addr->sa_data, |
| 1053 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 1054 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1055 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1058 | |
| 1059 | /* Parse a socket address argument according to the socket object's |
| 1060 | address family. Return 1 if the address was in the proper format, |
| 1061 | 0 of not. The address is returned through addr_ret, its length |
| 1062 | through len_ret. */ |
| 1063 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1064 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1065 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1066 | struct sockaddr **addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1067 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1068 | switch (s->sock_family) { |
| 1069 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1070 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1071 | case AF_UNIX: |
| 1072 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1073 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1074 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1075 | int len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1076 | addr = (struct sockaddr_un*)&(s->sock_addr).un; |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 1077 | if (!PyArg_Parse(args, "t#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1078 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1079 | if (len > sizeof addr->sun_path) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1080 | PyErr_SetString(socket_error, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1081 | "AF_UNIX path too long"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1082 | return 0; |
| 1083 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1084 | addr->sun_family = s->sock_family; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1085 | memcpy(addr->sun_path, path, len); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 1086 | addr->sun_path[len] = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1087 | *addr_ret = (struct sockaddr *) addr; |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1088 | #if defined(PYOS_OS2) |
| 1089 | *len_ret = sizeof(*addr); |
| 1090 | #else |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 1091 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 1092 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1093 | return 1; |
| 1094 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1095 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1096 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1097 | case AF_INET: |
| 1098 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1099 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1100 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1101 | int port, result; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1102 | addr=(struct sockaddr_in*)&(s->sock_addr).in; |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1103 | if (!PyTuple_Check(args)) { |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1104 | PyErr_Format( |
| 1105 | PyExc_TypeError, |
| 1106 | "getsockaddrarg: " |
| 1107 | "AF_INET address must be tuple, not %.500s", |
| 1108 | args->ob_type->tp_name); |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1109 | return 0; |
| 1110 | } |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1111 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1112 | "idna", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1113 | return 0; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1114 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1115 | sizeof(*addr), AF_INET); |
| 1116 | PyMem_Free(host); |
| 1117 | if (result < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1118 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1119 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 1120 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1121 | *addr_ret = (struct sockaddr *) addr; |
| 1122 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1123 | return 1; |
| 1124 | } |
| 1125 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1126 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1127 | case AF_INET6: |
| 1128 | { |
| 1129 | struct sockaddr_in6* addr; |
| 1130 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1131 | int port, flowinfo, scope_id, result; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1132 | addr = (struct sockaddr_in6*)&(s->sock_addr).in6; |
| 1133 | flowinfo = scope_id = 0; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1134 | if (!PyArg_ParseTuple(args, "eti|ii", |
| 1135 | "idna", &host, &port, &flowinfo, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1136 | &scope_id)) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1137 | return 0; |
| 1138 | } |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1139 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1140 | sizeof(*addr), AF_INET6); |
| 1141 | PyMem_Free(host); |
| 1142 | if (result < 0) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1143 | return 0; |
| 1144 | addr->sin6_family = s->sock_family; |
| 1145 | addr->sin6_port = htons((short)port); |
| 1146 | addr->sin6_flowinfo = flowinfo; |
| 1147 | addr->sin6_scope_id = scope_id; |
| 1148 | *addr_ret = (struct sockaddr *) addr; |
| 1149 | *len_ret = sizeof *addr; |
| 1150 | return 1; |
| 1151 | } |
| 1152 | #endif |
| 1153 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1154 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1155 | case AF_BLUETOOTH: |
| 1156 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1157 | switch (s->sock_proto) { |
| 1158 | case BTPROTO_L2CAP: |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1159 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1160 | struct sockaddr_l2 *addr = (struct sockaddr_l2 *) _BT_SOCKADDR_MEMB(s, l2); |
| 1161 | char *straddr; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1162 | |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1163 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1164 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1165 | &_BT_L2_MEMB(addr, psm))) { |
| 1166 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1167 | "wrong format"); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1170 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1171 | return 0; |
| 1172 | |
| 1173 | *addr_ret = (struct sockaddr *) addr; |
| 1174 | *len_ret = sizeof *addr; |
| 1175 | return 1; |
| 1176 | } |
| 1177 | case BTPROTO_RFCOMM: |
| 1178 | { |
| 1179 | struct sockaddr_rc *addr = (struct sockaddr_rc *) _BT_SOCKADDR_MEMB(s, rc); |
| 1180 | char *straddr; |
| 1181 | |
| 1182 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1183 | if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1184 | &_BT_RC_MEMB(addr, channel))) { |
| 1185 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1186 | "wrong format"); |
| 1187 | return 0; |
| 1188 | } |
| 1189 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1190 | return 0; |
| 1191 | |
| 1192 | *addr_ret = (struct sockaddr *) addr; |
| 1193 | *len_ret = sizeof *addr; |
| 1194 | return 1; |
| 1195 | } |
| 1196 | #if !defined(__FreeBSD__) |
| 1197 | case BTPROTO_SCO: |
| 1198 | { |
| 1199 | struct sockaddr_sco *addr = (struct sockaddr_sco *) _BT_SOCKADDR_MEMB(s, sco); |
| 1200 | char *straddr; |
| 1201 | |
| 1202 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1203 | straddr = PyString_AsString(args); |
| 1204 | if (straddr == NULL) { |
| 1205 | PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1206 | "wrong format"); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1210 | return 0; |
| 1211 | |
| 1212 | *addr_ret = (struct sockaddr *) addr; |
| 1213 | *len_ret = sizeof *addr; |
| 1214 | return 1; |
| 1215 | } |
| 1216 | #endif |
| 1217 | default: |
| 1218 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1219 | return 0; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | #endif |
| 1223 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1224 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1225 | case AF_PACKET: |
| 1226 | { |
| 1227 | struct sockaddr_ll* addr; |
| 1228 | struct ifreq ifr; |
| 1229 | char *interfaceName; |
| 1230 | int protoNumber; |
| 1231 | int hatype = 0; |
| 1232 | int pkttype = 0; |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1233 | char *haddr = NULL; |
| 1234 | unsigned int halen = 0; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1235 | |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1236 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName, |
| 1237 | &protoNumber, &pkttype, &hatype, |
| 1238 | &haddr, &halen)) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1239 | return 0; |
| 1240 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1241 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1242 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1243 | s->errorhandler(); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1244 | return 0; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1245 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1246 | addr = &(s->sock_addr.ll); |
| 1247 | addr->sll_family = AF_PACKET; |
| 1248 | addr->sll_protocol = htons((short)protoNumber); |
| 1249 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1250 | addr->sll_pkttype = pkttype; |
| 1251 | addr->sll_hatype = hatype; |
Andrew M. Kuchling | b8e1717 | 2004-07-10 23:39:35 +0000 | [diff] [blame] | 1252 | if (halen > 8) { |
| 1253 | PyErr_SetString(PyExc_ValueError, |
| 1254 | "Hardware address must be 8 bytes or less"); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | if (halen != 0) { |
| 1258 | memcpy(&addr->sll_addr, haddr, halen); |
| 1259 | } |
| 1260 | addr->sll_halen = halen; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1261 | *addr_ret = (struct sockaddr *) addr; |
| 1262 | *len_ret = sizeof *addr; |
| 1263 | return 1; |
| 1264 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1265 | #endif |
| 1266 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1267 | /* More cases here... */ |
| 1268 | |
| 1269 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1270 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1271 | return 0; |
| 1272 | |
| 1273 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1276 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1277 | /* 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] | 1278 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1279 | through len_ret. */ |
| 1280 | |
| 1281 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1282 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1283 | { |
| 1284 | switch (s->sock_family) { |
| 1285 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1286 | #if defined(AF_UNIX) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1287 | case AF_UNIX: |
| 1288 | { |
| 1289 | *len_ret = sizeof (struct sockaddr_un); |
| 1290 | return 1; |
| 1291 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1292 | #endif /* AF_UNIX */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1293 | |
| 1294 | case AF_INET: |
| 1295 | { |
| 1296 | *len_ret = sizeof (struct sockaddr_in); |
| 1297 | return 1; |
| 1298 | } |
| 1299 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1300 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1301 | case AF_INET6: |
| 1302 | { |
| 1303 | *len_ret = sizeof (struct sockaddr_in6); |
| 1304 | return 1; |
| 1305 | } |
| 1306 | #endif |
| 1307 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1308 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1309 | case AF_BLUETOOTH: |
| 1310 | { |
| 1311 | switch(s->sock_proto) |
| 1312 | { |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1313 | |
| 1314 | case BTPROTO_L2CAP: |
| 1315 | *len_ret = sizeof (struct sockaddr_l2); |
| 1316 | return 1; |
| 1317 | case BTPROTO_RFCOMM: |
| 1318 | *len_ret = sizeof (struct sockaddr_rc); |
| 1319 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1320 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1321 | case BTPROTO_SCO: |
| 1322 | *len_ret = sizeof (struct sockaddr_sco); |
| 1323 | return 1; |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1324 | #endif |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1325 | default: |
| 1326 | PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1327 | "unknown BT protocol"); |
| 1328 | return 0; |
| 1329 | |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | #endif |
| 1333 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1334 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1335 | case AF_PACKET: |
| 1336 | { |
| 1337 | *len_ret = sizeof (struct sockaddr_ll); |
| 1338 | return 1; |
| 1339 | } |
| 1340 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1341 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1342 | /* More cases here... */ |
| 1343 | |
| 1344 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1345 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1346 | return 0; |
| 1347 | |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1352 | /* s.accept() method */ |
| 1353 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1354 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1355 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1356 | { |
| 1357 | char addrbuf[256]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1358 | SOCKET_T newfd; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1359 | socklen_t addrlen; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1360 | PyObject *sock = NULL; |
| 1361 | PyObject *addr = NULL; |
| 1362 | PyObject *res = NULL; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1363 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1364 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1365 | if (!getsockaddrlen(s, &addrlen)) |
| 1366 | return NULL; |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 1367 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1368 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1369 | #ifdef MS_WINDOWS |
| 1370 | newfd = INVALID_SOCKET; |
| 1371 | #else |
| 1372 | newfd = -1; |
| 1373 | #endif |
| 1374 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1375 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1376 | timeout = internal_select(s, 0); |
| 1377 | if (!timeout) |
| 1378 | newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, |
| 1379 | &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1380 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1381 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1382 | if (timeout) { |
| 1383 | PyErr_SetString(socket_timeout, "timed out"); |
| 1384 | return NULL; |
| 1385 | } |
| 1386 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1387 | #ifdef MS_WINDOWS |
| 1388 | if (newfd == INVALID_SOCKET) |
| 1389 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1390 | if (newfd < 0) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1391 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1392 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1393 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1394 | /* Create the new object with unspecified family, |
| 1395 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1396 | sock = (PyObject *) new_sockobject(newfd, |
| 1397 | s->sock_family, |
| 1398 | s->sock_type, |
| 1399 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1400 | |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1401 | if (sock == NULL) { |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1402 | SOCKETCLOSE(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1403 | goto finally; |
| 1404 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1405 | addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1406 | addrlen, s->sock_proto); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1407 | if (addr == NULL) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1408 | goto finally; |
| 1409 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1410 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1411 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1412 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1413 | Py_XDECREF(sock); |
| 1414 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1415 | return res; |
| 1416 | } |
| 1417 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1418 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1419 | "accept() -> (socket object, address info)\n\ |
| 1420 | \n\ |
| 1421 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1422 | 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] | 1423 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1424 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1425 | /* s.setblocking(flag) method. Argument: |
| 1426 | False -- non-blocking mode; same as settimeout(0) |
| 1427 | True -- blocking mode; same as settimeout(None) |
| 1428 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1429 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1430 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1431 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1432 | { |
| 1433 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1434 | |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1435 | block = PyInt_AsLong(arg); |
| 1436 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1437 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1438 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1439 | s->sock_timeout = block ? -1.0 : 0.0; |
Guido van Rossum | e8008f0 | 2002-06-07 03:36:20 +0000 | [diff] [blame] | 1440 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1441 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1442 | Py_INCREF(Py_None); |
| 1443 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1444 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1445 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1446 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1447 | "setblocking(flag)\n\ |
| 1448 | \n\ |
| 1449 | 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] | 1450 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1451 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1452 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1453 | /* s.settimeout(timeout) method. Argument: |
| 1454 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1455 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1456 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1457 | < 0 -- illegal; raises an exception |
| 1458 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1459 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1460 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1461 | { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1462 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1463 | |
| 1464 | if (arg == Py_None) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1465 | timeout = -1.0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1466 | else { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1467 | timeout = PyFloat_AsDouble(arg); |
| 1468 | if (timeout < 0.0) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1469 | if (!PyErr_Occurred()) |
| 1470 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1471 | "Timeout value out of range"); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1472 | return NULL; |
| 1473 | } |
| 1474 | } |
| 1475 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1476 | s->sock_timeout = timeout; |
| 1477 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1478 | |
| 1479 | Py_INCREF(Py_None); |
| 1480 | return Py_None; |
| 1481 | } |
| 1482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1483 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1484 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1485 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1486 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1487 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1488 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1489 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1490 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1491 | /* s.gettimeout() method. |
| 1492 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1493 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1494 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1495 | { |
| 1496 | if (s->sock_timeout < 0.0) { |
| 1497 | Py_INCREF(Py_None); |
| 1498 | return Py_None; |
| 1499 | } |
| 1500 | else |
| 1501 | return PyFloat_FromDouble(s->sock_timeout); |
| 1502 | } |
| 1503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1504 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1505 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1506 | \n\ |
| 1507 | Returns the timeout in floating seconds associated with socket \n\ |
| 1508 | 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] | 1509 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1510 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1511 | #ifdef RISCOS |
| 1512 | /* s.sleeptaskw(1 | 0) method */ |
| 1513 | |
| 1514 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1515 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1516 | { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1517 | int block; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1518 | block = PyInt_AsLong(arg); |
| 1519 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1520 | return NULL; |
| 1521 | Py_BEGIN_ALLOW_THREADS |
| 1522 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1523 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1524 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1525 | Py_INCREF(Py_None); |
| 1526 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1527 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1528 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1529 | "sleeptaskw(flag)\n\ |
| 1530 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1531 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1532 | #endif |
| 1533 | |
| 1534 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1535 | /* s.setsockopt() method. |
| 1536 | With an integer third argument, sets an integer option. |
| 1537 | With a string third argument, sets an option from a buffer; |
| 1538 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1539 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1540 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1541 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1542 | { |
| 1543 | int level; |
| 1544 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1545 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1546 | char *buf; |
| 1547 | int buflen; |
| 1548 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1549 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1550 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1551 | &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1552 | buf = (char *) &flag; |
| 1553 | buflen = sizeof flag; |
| 1554 | } |
| 1555 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1556 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1557 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1558 | &level, &optname, &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1559 | return NULL; |
| 1560 | } |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1561 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1562 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1563 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1564 | Py_INCREF(Py_None); |
| 1565 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1568 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1569 | "setsockopt(level, option, value)\n\ |
| 1570 | \n\ |
| 1571 | 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] | 1572 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1573 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1574 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1575 | /* s.getsockopt() method. |
| 1576 | With two arguments, retrieves an integer option. |
| 1577 | With a third integer argument, retrieves a string buffer of that size; |
| 1578 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1579 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1580 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1581 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1582 | { |
| 1583 | int level; |
| 1584 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1585 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1586 | PyObject *buf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1587 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1588 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1589 | #ifdef __BEOS__ |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1590 | /* We have incomplete socket support. */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1591 | PyErr_SetString(socket_error, "getsockopt not supported"); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1592 | return NULL; |
| 1593 | #else |
| 1594 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1595 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1596 | &level, &optname, &buflen)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1597 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1598 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1599 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1600 | int flag = 0; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1601 | socklen_t flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1602 | res = getsockopt(s->sock_fd, level, optname, |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1603 | (void *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1604 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1605 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1606 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1607 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1608 | #ifdef __VMS |
| 1609 | if (buflen > 1024) { |
| 1610 | #else |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1611 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1612 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1613 | PyErr_SetString(socket_error, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1614 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1615 | return NULL; |
| 1616 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1617 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1618 | if (buf == NULL) |
| 1619 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1620 | res = getsockopt(s->sock_fd, level, optname, |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1621 | (void *)PyString_AS_STRING(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1622 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1623 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1624 | return s->errorhandler(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1625 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1626 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1627 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1628 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1631 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1632 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1633 | \n\ |
| 1634 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1635 | 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] | 1636 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1637 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1638 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1639 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1640 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1641 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1642 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1643 | { |
| 1644 | struct sockaddr *addr; |
| 1645 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1646 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1647 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1648 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1649 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1650 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1651 | res = bind(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1652 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1653 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1654 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1655 | Py_INCREF(Py_None); |
| 1656 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1659 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1660 | "bind(address)\n\ |
| 1661 | \n\ |
| 1662 | 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] | 1663 | 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] | 1664 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1665 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1666 | |
| 1667 | /* s.close() method. |
| 1668 | Set the file descriptor to -1 so operations tried subsequently |
| 1669 | will surely fail. */ |
| 1670 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1671 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1672 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1673 | { |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1674 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1675 | |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1676 | if ((fd = s->sock_fd) != -1) { |
| 1677 | s->sock_fd = -1; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1678 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1679 | (void) SOCKETCLOSE(fd); |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1680 | Py_END_ALLOW_THREADS |
| 1681 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1682 | Py_INCREF(Py_None); |
| 1683 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1686 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1687 | "close()\n\ |
| 1688 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1689 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1690 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1691 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1692 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
| 1693 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1694 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1695 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1696 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1697 | timeout = 0; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1698 | res = connect(s->sock_fd, addr, addrlen); |
| 1699 | |
| 1700 | #ifdef MS_WINDOWS |
| 1701 | |
| 1702 | if (s->sock_timeout > 0.0) { |
| 1703 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1704 | /* This is a mess. Best solution: trust select */ |
| 1705 | fd_set fds; |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1706 | fd_set fds_exc; |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1707 | struct timeval tv; |
| 1708 | tv.tv_sec = (int)s->sock_timeout; |
| 1709 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1710 | FD_ZERO(&fds); |
| 1711 | FD_SET(s->sock_fd, &fds); |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1712 | FD_ZERO(&fds_exc); |
| 1713 | FD_SET(s->sock_fd, &fds_exc); |
| 1714 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1715 | if (res == 0) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1716 | res = WSAEWOULDBLOCK; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1717 | timeout = 1; |
Mark Hammond | a57ec93 | 2004-08-03 05:06:26 +0000 | [diff] [blame] | 1718 | } else if (res > 0) { |
| 1719 | if (FD_ISSET(s->sock_fd, &fds)) |
| 1720 | /* The socket is in the writeable set - this |
| 1721 | means connected */ |
| 1722 | res = 0; |
| 1723 | else { |
| 1724 | /* As per MS docs, we need to call getsockopt() |
| 1725 | to get the underlying error */ |
| 1726 | int res_size = sizeof res; |
| 1727 | /* It must be in the exception set */ |
| 1728 | assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 1729 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 1730 | (char *)&res, &res_size)) |
| 1731 | /* getsockopt also clears WSAGetLastError, |
| 1732 | so reset it back. */ |
| 1733 | WSASetLastError(res); |
| 1734 | else |
| 1735 | res = WSAGetLastError(); |
| 1736 | } |
| 1737 | } |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1738 | /* else if (res < 0) an error occurred */ |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | if (res < 0) |
| 1743 | res = WSAGetLastError(); |
| 1744 | |
| 1745 | #else |
| 1746 | |
| 1747 | if (s->sock_timeout > 0.0) { |
| 1748 | if (res < 0 && errno == EINPROGRESS) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1749 | timeout = internal_select(s, 1); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1750 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 12e3c71 | 2002-08-08 20:39:30 +0000 | [diff] [blame] | 1751 | if (res < 0 && errno == EISCONN) |
| 1752 | res = 0; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | if (res < 0) |
| 1757 | res = errno; |
| 1758 | |
| 1759 | #endif |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1760 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1761 | |
| 1762 | return res; |
| 1763 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1764 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1765 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1766 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1767 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1768 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1769 | { |
| 1770 | struct sockaddr *addr; |
| 1771 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1772 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1773 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1774 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1775 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1776 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1777 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1778 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1779 | res = internal_connect(s, addr, addrlen, &timeout); |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1780 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1781 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1782 | if (timeout) { |
| 1783 | PyErr_SetString(socket_timeout, "timed out"); |
| 1784 | return NULL; |
| 1785 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1786 | if (res != 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1787 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1788 | Py_INCREF(Py_None); |
| 1789 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1792 | PyDoc_STRVAR(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1793 | "connect(address)\n\ |
| 1794 | \n\ |
| 1795 | 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] | 1796 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1797 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1798 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1799 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1800 | |
| 1801 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1802 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1803 | { |
| 1804 | struct sockaddr *addr; |
| 1805 | int addrlen; |
| 1806 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1807 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1808 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1809 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1810 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1811 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1812 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1813 | res = internal_connect(s, addr, addrlen, &timeout); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1814 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1815 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1816 | return PyInt_FromLong((long) res); |
| 1817 | } |
| 1818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1819 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1820 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1821 | \n\ |
| 1822 | 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] | 1823 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1824 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1825 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1826 | /* s.fileno() method */ |
| 1827 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1828 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1829 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1830 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1831 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1832 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1833 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1834 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1835 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1838 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1839 | "fileno() -> integer\n\ |
| 1840 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1841 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1842 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1843 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1844 | #ifndef NO_DUP |
| 1845 | /* s.dup() method */ |
| 1846 | |
| 1847 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1848 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1849 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1850 | SOCKET_T newfd; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1851 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1852 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1853 | newfd = dup(s->sock_fd); |
| 1854 | if (newfd < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1855 | return s->errorhandler(); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1856 | sock = (PyObject *) new_sockobject(newfd, |
| 1857 | s->sock_family, |
| 1858 | s->sock_type, |
| 1859 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1860 | if (sock == NULL) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1861 | SOCKETCLOSE(newfd); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1862 | return sock; |
| 1863 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1865 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1866 | "dup() -> socket object\n\ |
| 1867 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1868 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1869 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1870 | #endif |
| 1871 | |
| 1872 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1873 | /* s.getsockname() method */ |
| 1874 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1875 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1876 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1877 | { |
| 1878 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1879 | int res; |
| 1880 | socklen_t addrlen; |
| 1881 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1882 | if (!getsockaddrlen(s, &addrlen)) |
| 1883 | return NULL; |
Guido van Rossum | ca42b16 | 1996-01-12 01:36:05 +0000 | [diff] [blame] | 1884 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1885 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1886 | res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1887 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1888 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1889 | return s->errorhandler(); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1890 | return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen, |
| 1891 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1894 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1895 | "getsockname() -> address info\n\ |
| 1896 | \n\ |
| 1897 | 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] | 1898 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1899 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1900 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1901 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1902 | /* s.getpeername() method */ |
| 1903 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1904 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1905 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1906 | { |
| 1907 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1908 | int res; |
| 1909 | socklen_t addrlen; |
| 1910 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1911 | if (!getsockaddrlen(s, &addrlen)) |
| 1912 | return NULL; |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 1913 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1914 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1915 | res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1916 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1917 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1918 | return s->errorhandler(); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 1919 | return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen, |
| 1920 | s->sock_proto); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1921 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1922 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1923 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1924 | "getpeername() -> address info\n\ |
| 1925 | \n\ |
| 1926 | 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] | 1927 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1928 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1929 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1930 | |
| 1931 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1932 | /* s.listen(n) method */ |
| 1933 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1934 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1935 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1936 | { |
| 1937 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1938 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1939 | |
| 1940 | backlog = PyInt_AsLong(arg); |
| 1941 | if (backlog == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1942 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1943 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1944 | if (backlog < 1) |
| 1945 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1946 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1947 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1948 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1949 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1950 | Py_INCREF(Py_None); |
| 1951 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1954 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1955 | "listen(backlog)\n\ |
| 1956 | \n\ |
| 1957 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 1958 | 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] | 1959 | will allow before refusing new connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1960 | |
| 1961 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1962 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1963 | /* s.makefile(mode) method. |
| 1964 | Create a new open file object referring to a dupped version of |
| 1965 | the socket's file descriptor. (The dup() call is necessary so |
| 1966 | that the open file and socket objects may be closed independent |
| 1967 | of each other.) |
| 1968 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 1969 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1970 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1971 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1972 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1973 | extern int fclose(FILE *); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1974 | char *mode = "r"; |
| 1975 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1976 | #ifdef MS_WIN32 |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1977 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1978 | #else |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1979 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1980 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1981 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1982 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1983 | #ifdef __VMS |
| 1984 | char *mode_r = "r"; |
| 1985 | char *mode_w = "w"; |
| 1986 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1987 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1988 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1989 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1990 | #ifdef __VMS |
| 1991 | if (strcmp(mode,"rb") == 0) { |
| 1992 | mode = mode_r; |
| 1993 | } |
| 1994 | else { |
| 1995 | if (strcmp(mode,"wb") == 0) { |
| 1996 | mode = mode_w; |
| 1997 | } |
| 1998 | } |
| 1999 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2000 | #ifdef MS_WIN32 |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2001 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 2002 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 2003 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2004 | 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] | 2005 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2006 | { |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2007 | if (fd >= 0) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 2008 | SOCKETCLOSE(fd); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2009 | return s->errorhandler(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 2010 | } |
| 2011 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 2012 | if (f != NULL) |
| 2013 | PyFile_SetBufSize(f, bufsize); |
| 2014 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2015 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2017 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2018 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 2019 | \n\ |
| 2020 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2021 | 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] | 2022 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2023 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2024 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2025 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2026 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2027 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2028 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2029 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2030 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2031 | int len, n = 0, flags = 0, timeout; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2032 | PyObject *buf; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2033 | #ifdef __VMS |
| 2034 | int read_length; |
| 2035 | char *read_buf; |
| 2036 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2037 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2038 | if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2039 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2040 | |
| 2041 | if (len < 0) { |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 2042 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 2043 | "negative buffersize in recv"); |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 2044 | return NULL; |
| 2045 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2046 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2047 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2048 | if (buf == NULL) |
| 2049 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2050 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2051 | #ifndef __VMS |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2052 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2053 | timeout = internal_select(s, 0); |
| 2054 | if (!timeout) |
| 2055 | n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2056 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2057 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2058 | if (timeout) { |
| 2059 | Py_DECREF(buf); |
| 2060 | PyErr_SetString(socket_timeout, "timed out"); |
| 2061 | return NULL; |
| 2062 | } |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2063 | if (n < 0) { |
| 2064 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2065 | return s->errorhandler(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2066 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2067 | if (n != len) |
| 2068 | _PyString_Resize(&buf, n); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2069 | #else |
| 2070 | read_buf = PyString_AsString(buf); |
| 2071 | read_length = len; |
| 2072 | while (read_length != 0) { |
| 2073 | unsigned int segment; |
| 2074 | |
| 2075 | segment = read_length /SEGMENT_SIZE; |
| 2076 | if (segment != 0) { |
| 2077 | segment = SEGMENT_SIZE; |
| 2078 | } |
| 2079 | else { |
| 2080 | segment = read_length; |
| 2081 | } |
| 2082 | |
| 2083 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2084 | timeout = internal_select(s, 0); |
| 2085 | if (!timeout) |
| 2086 | n = recv(s->sock_fd, read_buf, segment, flags); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2087 | Py_END_ALLOW_THREADS |
| 2088 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2089 | if (timeout) { |
| 2090 | Py_DECREF(buf); |
| 2091 | PyErr_SetString(socket_timeout, "timed out"); |
| 2092 | return NULL; |
| 2093 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2094 | if (n < 0) { |
| 2095 | Py_DECREF(buf); |
| 2096 | return s->errorhandler(); |
| 2097 | } |
| 2098 | if (n != read_length) { |
| 2099 | read_buf += n; |
| 2100 | break; |
| 2101 | } |
| 2102 | |
| 2103 | read_length -= segment; |
| 2104 | read_buf += segment; |
| 2105 | } |
| 2106 | if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0) |
| 2107 | { |
| 2108 | return NULL; |
| 2109 | } |
| 2110 | #endif /* !__VMS */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2111 | return buf; |
| 2112 | } |
| 2113 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2114 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2115 | "recv(buffersize[, flags]) -> data\n\ |
| 2116 | \n\ |
| 2117 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 2118 | argument, see the Unix manual. When no data is available, block until\n\ |
| 2119 | 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] | 2120 | 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] | 2121 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2122 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2123 | /* s.recvfrom(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2124 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2125 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2126 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2127 | { |
| 2128 | char addrbuf[256]; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2129 | PyObject *buf = NULL; |
| 2130 | PyObject *addr = NULL; |
| 2131 | PyObject *ret = NULL; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2132 | int len, n = 0, flags = 0, timeout; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 2133 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2134 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2135 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2136 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2137 | |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 2138 | if (!getsockaddrlen(s, &addrlen)) |
| 2139 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2140 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 2141 | if (buf == NULL) |
| 2142 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2143 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2144 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 2145 | memset(addrbuf, 0, addrlen); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2146 | timeout = internal_select(s, 0); |
| 2147 | if (!timeout) |
| 2148 | n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags, |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2149 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2150 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2151 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2152 | #else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2153 | (void *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2154 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2155 | #else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2156 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2157 | #endif |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2158 | ); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2159 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2160 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2161 | if (timeout) { |
| 2162 | Py_DECREF(buf); |
| 2163 | PyErr_SetString(socket_timeout, "timed out"); |
| 2164 | return NULL; |
| 2165 | } |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2166 | if (n < 0) { |
| 2167 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2168 | return s->errorhandler(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2169 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2170 | |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 2171 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2172 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2173 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2174 | if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 2175 | addrlen, s->sock_proto))) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2176 | goto finally; |
| 2177 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2178 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2179 | |
| 2180 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2181 | Py_XDECREF(addr); |
| 2182 | Py_XDECREF(buf); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 2183 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2186 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2187 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2188 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2189 | 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] | 2190 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2191 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2192 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2193 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2194 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2195 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2196 | char *buf; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2197 | int len, n = 0, flags = 0, timeout; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2198 | #ifdef __VMS |
| 2199 | int send_length; |
| 2200 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2201 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2202 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2203 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2204 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2205 | #ifndef __VMS |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2206 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2207 | timeout = internal_select(s, 1); |
| 2208 | if (!timeout) |
| 2209 | n = send(s->sock_fd, buf, len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2210 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2211 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2212 | if (timeout) { |
| 2213 | PyErr_SetString(socket_timeout, "timed out"); |
| 2214 | return NULL; |
| 2215 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2216 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2217 | return s->errorhandler(); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2218 | #else |
| 2219 | /* Divide packet into smaller segments for */ |
| 2220 | /* TCP/IP Services for OpenVMS */ |
| 2221 | send_length = len; |
| 2222 | while (send_length != 0) { |
| 2223 | unsigned int segment; |
| 2224 | |
| 2225 | segment = send_length / SEGMENT_SIZE; |
| 2226 | if (segment != 0) { |
| 2227 | segment = SEGMENT_SIZE; |
| 2228 | } |
| 2229 | else { |
| 2230 | segment = send_length; |
| 2231 | } |
| 2232 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2233 | timeout = internal_select(s, 1); |
| 2234 | if (!timeout) |
| 2235 | n = send(s->sock_fd, buf, segment, flags); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2236 | Py_END_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2237 | if (timeout) { |
| 2238 | PyErr_SetString(socket_timeout, "timed out"); |
| 2239 | return NULL; |
| 2240 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2241 | if (n < 0) { |
| 2242 | return s->errorhandler(); |
| 2243 | } |
| 2244 | send_length -= segment; |
| 2245 | buf += segment; |
| 2246 | } /* end while */ |
| 2247 | #endif /* !__VMS */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2248 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2251 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2252 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2253 | \n\ |
| 2254 | 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] | 2255 | 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] | 2256 | 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] | 2257 | |
| 2258 | |
| 2259 | /* s.sendall(data [,flags]) method */ |
| 2260 | |
| 2261 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2262 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2263 | { |
| 2264 | char *buf; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2265 | int len, n = 0, flags = 0, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2266 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2267 | if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) |
| 2268 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2269 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2270 | Py_BEGIN_ALLOW_THREADS |
| 2271 | do { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2272 | timeout = internal_select(s, 1); |
| 2273 | if (timeout) |
| 2274 | break; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2275 | n = send(s->sock_fd, buf, len, flags); |
| 2276 | if (n < 0) |
| 2277 | break; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2278 | buf += n; |
| 2279 | len -= n; |
| 2280 | } while (len > 0); |
| 2281 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2282 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2283 | if (timeout) { |
| 2284 | PyErr_SetString(socket_timeout, "timed out"); |
| 2285 | return NULL; |
| 2286 | } |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2287 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2288 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2289 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2290 | Py_INCREF(Py_None); |
| 2291 | return Py_None; |
| 2292 | } |
| 2293 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2294 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2295 | "sendall(data[, flags])\n\ |
| 2296 | \n\ |
| 2297 | Send a data string to the socket. For the optional flags\n\ |
| 2298 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2299 | 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] | 2300 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2301 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2302 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2303 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2304 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2305 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2306 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2307 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2308 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2309 | char *buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2310 | struct sockaddr *addr; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2311 | int addrlen, len, n = 0, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2313 | flags = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2314 | if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2315 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2316 | if (!PyArg_ParseTuple(args, "s#iO:sendto", |
| 2317 | &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2318 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2319 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2320 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2321 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2322 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2323 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2324 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2325 | timeout = internal_select(s, 1); |
| 2326 | if (!timeout) |
| 2327 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2328 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2329 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2330 | if (timeout) { |
| 2331 | PyErr_SetString(socket_timeout, "timed out"); |
| 2332 | return NULL; |
| 2333 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2334 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2335 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2336 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2339 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2340 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2341 | \n\ |
| 2342 | 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] | 2343 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2344 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2345 | |
| 2346 | /* s.shutdown(how) method */ |
| 2347 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2348 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2349 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2350 | { |
| 2351 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2352 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2353 | |
| 2354 | how = PyInt_AsLong(arg); |
| 2355 | if (how == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2356 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2357 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2358 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2359 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2360 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2361 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2362 | Py_INCREF(Py_None); |
| 2363 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2366 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2367 | "shutdown(flag)\n\ |
| 2368 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2369 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2370 | 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] | 2371 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2372 | |
| 2373 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2374 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2375 | static PyMethodDef sock_methods[] = { |
| 2376 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2377 | accept_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2378 | {"bind", (PyCFunction)sock_bind, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2379 | bind_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2380 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2381 | close_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2382 | {"connect", (PyCFunction)sock_connect, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2383 | connect_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2384 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2385 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2386 | #ifndef NO_DUP |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2387 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2388 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2389 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2390 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2391 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2392 | #ifdef HAVE_GETPEERNAME |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2393 | {"getpeername", (PyCFunction)sock_getpeername, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2394 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 2395 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2396 | {"getsockname", (PyCFunction)sock_getsockname, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2397 | METH_NOARGS, getsockname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2398 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2399 | getsockopt_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2400 | {"listen", (PyCFunction)sock_listen, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2401 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2402 | #ifndef NO_DUP |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2403 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2404 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2405 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2406 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2407 | recv_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2408 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2409 | recvfrom_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2410 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2411 | send_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2412 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2413 | sendall_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2414 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2415 | sendto_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2416 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2417 | setblocking_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2418 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2419 | settimeout_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2420 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2421 | gettimeout_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2422 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2423 | setsockopt_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2424 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2425 | shutdown_doc}, |
| 2426 | #ifdef RISCOS |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 2427 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2428 | sleeptaskw_doc}, |
| 2429 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2430 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2431 | }; |
| 2432 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2433 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2434 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2435 | First close the file description. */ |
| 2436 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2437 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2438 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2439 | { |
Guido van Rossum | fa972c9 | 2000-04-10 12:45:45 +0000 | [diff] [blame] | 2440 | if (s->sock_fd != -1) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 2441 | (void) SOCKETCLOSE(s->sock_fd); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2442 | s->ob_type->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2445 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2446 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2447 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2448 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2449 | char buf[512]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2450 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
| 2451 | if (s->sock_fd > LONG_MAX) { |
| 2452 | /* this can occur on Win64, and actually there is a special |
| 2453 | ugly printf formatter for decimal pointer length integer |
| 2454 | printing, only bother if necessary*/ |
| 2455 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2456 | "no printf formatter to display " |
| 2457 | "the socket descriptor in decimal"); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2458 | return NULL; |
| 2459 | } |
| 2460 | #endif |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2461 | PyOS_snprintf( |
| 2462 | buf, sizeof(buf), |
| 2463 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
| 2464 | (long)s->sock_fd, s->sock_family, |
| 2465 | s->sock_type, |
| 2466 | s->sock_proto); |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 2467 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2471 | /* Create a new, uninitialized socket object. */ |
| 2472 | |
| 2473 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2474 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2475 | { |
| 2476 | PyObject *new; |
| 2477 | |
| 2478 | new = type->tp_alloc(type, 0); |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2479 | if (new != NULL) { |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2480 | ((PySocketSockObject *)new)->sock_fd = -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2481 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2482 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2483 | } |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2484 | return new; |
| 2485 | } |
| 2486 | |
| 2487 | |
| 2488 | /* Initialize a new socket object. */ |
| 2489 | |
| 2490 | /*ARGSUSED*/ |
| 2491 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2492 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2493 | { |
| 2494 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 2495 | SOCKET_T fd; |
| 2496 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 2497 | static char *keywords[] = {"family", "type", "proto", 0}; |
| 2498 | |
| 2499 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2500 | "|iii:socket", keywords, |
| 2501 | &family, &type, &proto)) |
| 2502 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2503 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2504 | Py_BEGIN_ALLOW_THREADS |
| 2505 | fd = socket(family, type, proto); |
| 2506 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2507 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2508 | #ifdef MS_WINDOWS |
| 2509 | if (fd == INVALID_SOCKET) |
| 2510 | #else |
| 2511 | if (fd < 0) |
| 2512 | #endif |
| 2513 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2514 | set_error(); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2515 | return -1; |
| 2516 | } |
| 2517 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2518 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2519 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2520 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2524 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2525 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2526 | static PyTypeObject sock_type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 2527 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2528 | 0, /* ob_size */ |
Guido van Rossum | ca6dfa5 | 2001-10-28 12:31:33 +0000 | [diff] [blame] | 2529 | "_socket.socket", /* tp_name */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2530 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 2531 | 0, /* tp_itemsize */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2532 | (destructor)sock_dealloc, /* tp_dealloc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2533 | 0, /* tp_print */ |
| 2534 | 0, /* tp_getattr */ |
| 2535 | 0, /* tp_setattr */ |
| 2536 | 0, /* tp_compare */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2537 | (reprfunc)sock_repr, /* tp_repr */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2538 | 0, /* tp_as_number */ |
| 2539 | 0, /* tp_as_sequence */ |
| 2540 | 0, /* tp_as_mapping */ |
| 2541 | 0, /* tp_hash */ |
| 2542 | 0, /* tp_call */ |
| 2543 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2544 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2545 | 0, /* tp_setattro */ |
| 2546 | 0, /* tp_as_buffer */ |
| 2547 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2548 | sock_doc, /* tp_doc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2549 | 0, /* tp_traverse */ |
| 2550 | 0, /* tp_clear */ |
| 2551 | 0, /* tp_richcompare */ |
| 2552 | 0, /* tp_weaklistoffset */ |
| 2553 | 0, /* tp_iter */ |
| 2554 | 0, /* tp_iternext */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2555 | sock_methods, /* tp_methods */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2556 | 0, /* tp_members */ |
| 2557 | 0, /* tp_getset */ |
| 2558 | 0, /* tp_base */ |
| 2559 | 0, /* tp_dict */ |
| 2560 | 0, /* tp_descr_get */ |
| 2561 | 0, /* tp_descr_set */ |
| 2562 | 0, /* tp_dictoffset */ |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2563 | sock_initobj, /* tp_init */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2564 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2565 | sock_new, /* tp_new */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2566 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2567 | }; |
| 2568 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2569 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2570 | /* Python interface to gethostname(). */ |
| 2571 | |
| 2572 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2573 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2574 | socket_gethostname(PyObject *self, PyObject *args) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2575 | { |
| 2576 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2577 | int res; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2578 | if (!PyArg_ParseTuple(args, ":gethostname")) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2579 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2580 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2581 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2582 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2583 | if (res < 0) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2584 | return set_error(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2585 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2586 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2587 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2589 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2590 | "gethostname() -> string\n\ |
| 2591 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2592 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2593 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2594 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2595 | /* Python interface to gethostbyname(name). */ |
| 2596 | |
| 2597 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2598 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2599 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2600 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2601 | char *name; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 2602 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2603 | struct sockaddr_storage addrbuf; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 2604 | #else |
| 2605 | struct sockaddr_in addrbuf; |
| 2606 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2607 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2608 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2609 | return NULL; |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 2610 | if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2611 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2612 | return makeipaddr((struct sockaddr *)&addrbuf, |
| 2613 | sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2616 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2617 | "gethostbyname(host) -> address\n\ |
| 2618 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2619 | 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] | 2620 | |
| 2621 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2622 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 2623 | |
| 2624 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2625 | 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] | 2626 | { |
| 2627 | char **pch; |
| 2628 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 2629 | PyObject *name_list = (PyObject *)NULL; |
| 2630 | PyObject *addr_list = (PyObject *)NULL; |
| 2631 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2632 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2633 | if (h == NULL) { |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2634 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 2635 | #ifndef RISCOS |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2636 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 2637 | #else |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2638 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 2639 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2640 | return NULL; |
| 2641 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2642 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2643 | if (h->h_addrtype != af) { |
| 2644 | #ifdef HAVE_STRERROR |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2645 | /* Let's get real error message to return */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2646 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2647 | (char *)strerror(EAFNOSUPPORT)); |
| 2648 | #else |
| 2649 | PyErr_SetString( |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2650 | socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2651 | "Address family not supported by protocol family"); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2652 | #endif |
| 2653 | return NULL; |
| 2654 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2655 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2656 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2657 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2658 | case AF_INET: |
| 2659 | if (alen < sizeof(struct sockaddr_in)) |
| 2660 | return NULL; |
| 2661 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2662 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2663 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2664 | case AF_INET6: |
| 2665 | if (alen < sizeof(struct sockaddr_in6)) |
| 2666 | return NULL; |
| 2667 | break; |
| 2668 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2669 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2670 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2671 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2672 | if ((name_list = PyList_New(0)) == NULL) |
| 2673 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2674 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2675 | if ((addr_list = PyList_New(0)) == NULL) |
| 2676 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2677 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2678 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 2679 | int status; |
| 2680 | tmp = PyString_FromString(*pch); |
| 2681 | if (tmp == NULL) |
| 2682 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2683 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2684 | status = PyList_Append(name_list, tmp); |
| 2685 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2686 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2687 | if (status) |
| 2688 | goto err; |
| 2689 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2690 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2691 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 2692 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2693 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2694 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2695 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2696 | case AF_INET: |
| 2697 | { |
| 2698 | struct sockaddr_in sin; |
| 2699 | memset(&sin, 0, sizeof(sin)); |
| 2700 | sin.sin_family = af; |
| 2701 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2702 | sin.sin_len = sizeof(sin); |
| 2703 | #endif |
| 2704 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 2705 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2706 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2707 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 2708 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 2709 | break; |
| 2710 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2711 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2712 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2713 | case AF_INET6: |
| 2714 | { |
| 2715 | struct sockaddr_in6 sin6; |
| 2716 | memset(&sin6, 0, sizeof(sin6)); |
| 2717 | sin6.sin6_family = af; |
| 2718 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2719 | sin6.sin6_len = sizeof(sin6); |
| 2720 | #endif |
| 2721 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 2722 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 2723 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2724 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2725 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 2726 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 2727 | break; |
| 2728 | } |
| 2729 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2730 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2731 | default: /* can't happen */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2732 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2733 | "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2734 | return NULL; |
| 2735 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2736 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2737 | if (tmp == NULL) |
| 2738 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2739 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2740 | status = PyList_Append(addr_list, tmp); |
| 2741 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2742 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2743 | if (status) |
| 2744 | goto err; |
| 2745 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2746 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2747 | 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] | 2748 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2749 | err: |
| 2750 | Py_XDECREF(name_list); |
| 2751 | Py_XDECREF(addr_list); |
| 2752 | return rtn_tuple; |
| 2753 | } |
| 2754 | |
| 2755 | |
| 2756 | /* Python interface to gethostbyname_ex(name). */ |
| 2757 | |
| 2758 | /*ARGSUSED*/ |
| 2759 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2760 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2761 | { |
| 2762 | char *name; |
| 2763 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 2764 | #ifdef ENABLE_IPV6 |
| 2765 | struct sockaddr_storage addr; |
| 2766 | #else |
| 2767 | struct sockaddr_in addr; |
| 2768 | #endif |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 2769 | struct sockaddr *sa; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 2770 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2771 | #ifdef HAVE_GETHOSTBYNAME_R |
| 2772 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2773 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 2774 | struct hostent_data data; |
| 2775 | #else |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2776 | char buf[16384]; |
| 2777 | int buf_len = (sizeof buf) - 1; |
| 2778 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2779 | #endif |
| 2780 | #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] | 2781 | int result; |
| 2782 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2783 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2784 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2785 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2786 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 2787 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2788 | return NULL; |
| 2789 | Py_BEGIN_ALLOW_THREADS |
| 2790 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2791 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2792 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 2793 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2794 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2795 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2796 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 2797 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2798 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 2799 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2800 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2801 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2802 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2803 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2804 | #endif |
| 2805 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2806 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 2807 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2808 | /* Some C libraries would require addr.__ss_family instead of |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2809 | addr.ss_family. |
| 2810 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 2811 | access sa_family. */ |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 2812 | sa = (struct sockaddr*)&addr; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2813 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2814 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2815 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2816 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 2817 | #endif |
| 2818 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2821 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2822 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 2823 | \n\ |
| 2824 | 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] | 2825 | 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] | 2826 | |
| 2827 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2828 | /* Python interface to gethostbyaddr(IP). */ |
| 2829 | |
| 2830 | /*ARGSUSED*/ |
| 2831 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2832 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2833 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2834 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2835 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2836 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2837 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2838 | #endif |
| 2839 | struct sockaddr *sa = (struct sockaddr *)&addr; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2840 | char *ip_num; |
| 2841 | struct hostent *h; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2842 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2843 | #ifdef HAVE_GETHOSTBYNAME_R |
| 2844 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2845 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 2846 | struct hostent_data data; |
| 2847 | #else |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2848 | char buf[16384]; |
| 2849 | int buf_len = (sizeof buf) - 1; |
| 2850 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2851 | #endif |
| 2852 | #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] | 2853 | int result; |
| 2854 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2855 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2856 | char *ap; |
| 2857 | int al; |
| 2858 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2859 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2860 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2861 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 2862 | af = AF_UNSPEC; |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 2863 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2864 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2865 | af = sa->sa_family; |
| 2866 | ap = NULL; |
| 2867 | al = 0; |
| 2868 | switch (af) { |
| 2869 | case AF_INET: |
| 2870 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 2871 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 2872 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2873 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2874 | case AF_INET6: |
| 2875 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 2876 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 2877 | break; |
| 2878 | #endif |
| 2879 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2880 | PyErr_SetString(socket_error, "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2881 | return NULL; |
| 2882 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2883 | Py_BEGIN_ALLOW_THREADS |
| 2884 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2885 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2886 | result = gethostbyaddr_r(ap, al, af, |
| 2887 | &hp_allocated, buf, buf_len, |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2888 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2889 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2890 | h = gethostbyaddr_r(ap, al, af, |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2891 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2892 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 2893 | memset((void *) &data, '\0', sizeof(data)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2894 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2895 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2896 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2897 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2898 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2899 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2900 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2901 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2902 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 2903 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2904 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2905 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2906 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2907 | #endif |
| 2908 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2911 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2912 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 2913 | \n\ |
| 2914 | 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] | 2915 | 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] | 2916 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2917 | |
| 2918 | /* Python interface to getservbyname(name). |
| 2919 | This only returns the port number, since the other info is already |
| 2920 | known or not useful (like the list of aliases). */ |
| 2921 | |
| 2922 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2923 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2924 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2925 | { |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 2926 | char *name, *proto=NULL; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2927 | struct servent *sp; |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 2928 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2929 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2930 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2931 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2932 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2933 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2934 | PyErr_SetString(socket_error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2935 | return NULL; |
| 2936 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2937 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2940 | PyDoc_STRVAR(getservbyname_doc, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 2941 | "getservbyname(servicename[, protocolname]) -> integer\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2942 | \n\ |
| 2943 | Return a port number from a service name and protocol name.\n\ |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 2944 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 2945 | otherwise any protocol will match."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2946 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2947 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 2948 | /* Python interface to getservbyport(port). |
| 2949 | This only returns the service name, since the other info is already |
| 2950 | known or not useful (like the list of aliases). */ |
| 2951 | |
| 2952 | /*ARGSUSED*/ |
| 2953 | static PyObject * |
| 2954 | socket_getservbyport(PyObject *self, PyObject *args) |
| 2955 | { |
| 2956 | int port; |
| 2957 | char *proto=NULL; |
| 2958 | struct servent *sp; |
| 2959 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 2960 | return NULL; |
| 2961 | Py_BEGIN_ALLOW_THREADS |
| 2962 | sp = getservbyport(htons(port), proto); |
| 2963 | Py_END_ALLOW_THREADS |
| 2964 | if (sp == NULL) { |
| 2965 | PyErr_SetString(socket_error, "port/proto not found"); |
| 2966 | return NULL; |
| 2967 | } |
| 2968 | return PyString_FromString(sp->s_name); |
| 2969 | } |
| 2970 | |
| 2971 | PyDoc_STRVAR(getservbyport_doc, |
| 2972 | "getservbyport(port[, protocolname]) -> string\n\ |
| 2973 | \n\ |
| 2974 | Return the service name from a port number and protocol name.\n\ |
| 2975 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
| 2976 | otherwise any protocol will match."); |
| 2977 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2978 | /* Python interface to getprotobyname(name). |
| 2979 | This only returns the protocol number, since the other info is |
| 2980 | already known or not useful (like the list of aliases). */ |
| 2981 | |
| 2982 | /*ARGSUSED*/ |
| 2983 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2984 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2985 | { |
| 2986 | char *name; |
| 2987 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2988 | #ifdef __BEOS__ |
| 2989 | /* Not available in BeOS yet. - [cjh] */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2990 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2991 | return NULL; |
| 2992 | #else |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2993 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2994 | return NULL; |
| 2995 | Py_BEGIN_ALLOW_THREADS |
| 2996 | sp = getprotobyname(name); |
| 2997 | Py_END_ALLOW_THREADS |
| 2998 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2999 | PyErr_SetString(socket_error, "protocol not found"); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3000 | return NULL; |
| 3001 | } |
| 3002 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3003 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3006 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3007 | "getprotobyname(name) -> integer\n\ |
| 3008 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3009 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3010 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 3011 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3012 | #ifdef HAVE_SOCKETPAIR |
| 3013 | /* Create a pair of sockets using the socketpair() function. |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame^] | 3014 | Arguments as for socket() except the default family is AF_UNIX if |
| 3015 | defined for the platform, otherwise the default is AF_INET. */ |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3016 | |
| 3017 | /*ARGSUSED*/ |
| 3018 | static PyObject * |
| 3019 | socket_socketpair(PyObject *self, PyObject *args) |
| 3020 | { |
| 3021 | PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3022 | SOCKET_T sv[2]; |
| 3023 | int family, type = SOCK_STREAM, proto = 0; |
| 3024 | PyObject *res = NULL; |
| 3025 | |
| 3026 | #if defined(AF_UNIX) |
| 3027 | family = AF_UNIX; |
| 3028 | #else |
| 3029 | family = AF_INET; |
| 3030 | #endif |
| 3031 | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3032 | &family, &type, &proto)) |
| 3033 | return NULL; |
| 3034 | /* Create a pair of socket fds */ |
| 3035 | if (socketpair(family, type, proto, sv) < 0) |
| 3036 | return set_error(); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3037 | s0 = new_sockobject(sv[0], family, type, proto); |
| 3038 | if (s0 == NULL) |
| 3039 | goto finally; |
| 3040 | s1 = new_sockobject(sv[1], family, type, proto); |
| 3041 | if (s1 == NULL) |
| 3042 | goto finally; |
| 3043 | res = PyTuple_Pack(2, s0, s1); |
| 3044 | |
| 3045 | finally: |
| 3046 | if (res == NULL) { |
| 3047 | if (s0 == NULL) |
| 3048 | SOCKETCLOSE(sv[0]); |
| 3049 | if (s1 == NULL) |
| 3050 | SOCKETCLOSE(sv[1]); |
| 3051 | } |
| 3052 | Py_XDECREF(s0); |
| 3053 | Py_XDECREF(s1); |
| 3054 | return res; |
| 3055 | } |
| 3056 | |
| 3057 | PyDoc_STRVAR(socketpair_doc, |
| 3058 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3059 | \n\ |
| 3060 | Create a pair of socket objects from the sockets returned by the platform\n\ |
| 3061 | socketpair() function.\n\ |
Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame^] | 3062 | The arguments are the same as for socket() except the default family is\n\ |
| 3063 | AF_UNIX if defined for the platform, otherwise the default is AF_INET."); |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3064 | |
| 3065 | #endif /* HAVE_SOCKETPAIR */ |
| 3066 | |
| 3067 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3068 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3069 | /* Create a socket object from a numeric file description. |
| 3070 | Useful e.g. if stdin is a socket. |
| 3071 | Additional arguments as for socket(). */ |
| 3072 | |
| 3073 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3074 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3075 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3076 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3077 | PySocketSockObject *s; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 3078 | SOCKET_T fd; |
| 3079 | int family, type, proto = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3080 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 3081 | &fd, &family, &type, &proto)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3082 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 3083 | /* Dup the fd so it and the socket can be closed independently */ |
| 3084 | fd = dup(fd); |
| 3085 | if (fd < 0) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3086 | return set_error(); |
| 3087 | s = new_sockobject(fd, family, type, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 3088 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3089 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3090 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3091 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3092 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 3093 | \n\ |
| 3094 | Create a socket object from the given file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3095 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3096 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3097 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 3098 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3099 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3100 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3101 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3102 | { |
| 3103 | int x1, x2; |
| 3104 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3105 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3106 | return NULL; |
| 3107 | } |
| 3108 | x2 = (int)ntohs((short)x1); |
| 3109 | return PyInt_FromLong(x2); |
| 3110 | } |
| 3111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3112 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3113 | "ntohs(integer) -> integer\n\ |
| 3114 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3115 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3116 | |
| 3117 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3118 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3119 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3120 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3121 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3122 | |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3123 | if (PyInt_Check(arg)) { |
| 3124 | x = PyInt_AS_LONG(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3125 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3126 | return NULL; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3127 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3128 | else if (PyLong_Check(arg)) { |
| 3129 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3130 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3131 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3132 | #if SIZEOF_LONG > 4 |
| 3133 | { |
| 3134 | unsigned long y; |
| 3135 | /* only want the trailing 32 bits */ |
| 3136 | y = x & 0xFFFFFFFFUL; |
| 3137 | if (y ^ x) |
| 3138 | return PyErr_Format(PyExc_OverflowError, |
| 3139 | "long int larger than 32 bits"); |
| 3140 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3141 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3142 | #endif |
| 3143 | } |
| 3144 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3145 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3146 | "expected int/long, %s found", |
| 3147 | arg->ob_type->tp_name); |
| 3148 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3149 | return NULL; |
| 3150 | return PyInt_FromLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3153 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3154 | "ntohl(integer) -> integer\n\ |
| 3155 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3156 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3157 | |
| 3158 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3159 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3160 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3161 | { |
Hye-Shik Chang | a7b673f | 2003-12-17 09:40:23 +0000 | [diff] [blame] | 3162 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3163 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3164 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3165 | return NULL; |
| 3166 | } |
| 3167 | x2 = (int)htons((short)x1); |
| 3168 | return PyInt_FromLong(x2); |
| 3169 | } |
| 3170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3171 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3172 | "htons(integer) -> integer\n\ |
| 3173 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3174 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3175 | |
| 3176 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3177 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3178 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3179 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3180 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3181 | |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3182 | if (PyInt_Check(arg)) { |
| 3183 | x = PyInt_AS_LONG(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3184 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3185 | return NULL; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3186 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3187 | else if (PyLong_Check(arg)) { |
| 3188 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 3189 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3190 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3191 | #if SIZEOF_LONG > 4 |
| 3192 | { |
| 3193 | unsigned long y; |
| 3194 | /* only want the trailing 32 bits */ |
| 3195 | y = x & 0xFFFFFFFFUL; |
| 3196 | if (y ^ x) |
| 3197 | return PyErr_Format(PyExc_OverflowError, |
| 3198 | "long int larger than 32 bits"); |
| 3199 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3200 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3201 | #endif |
| 3202 | } |
| 3203 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 3204 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3205 | "expected int/long, %s found", |
| 3206 | arg->ob_type->tp_name); |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3207 | return PyInt_FromLong(htonl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3210 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3211 | "htonl(integer) -> integer\n\ |
| 3212 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3213 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3214 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3215 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3217 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3218 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3219 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3220 | 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] | 3221 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3222 | |
| 3223 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3224 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3225 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3226 | #ifndef INADDR_NONE |
| 3227 | #define INADDR_NONE (-1) |
| 3228 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3229 | #ifdef HAVE_INET_ATON |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3230 | struct in_addr buf; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3231 | #else |
| 3232 | /* Have to use inet_addr() instead */ |
| 3233 | unsigned long packed_addr; |
| 3234 | #endif |
| 3235 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3236 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3237 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3238 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3239 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3240 | |
| 3241 | #ifdef HAVE_INET_ATON |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3242 | if (inet_aton(ip_addr, &buf)) |
| 3243 | return PyString_FromStringAndSize((char *)(&buf), |
| 3244 | sizeof(buf)); |
| 3245 | |
| 3246 | PyErr_SetString(socket_error, |
| 3247 | "illegal IP address string passed to inet_aton"); |
| 3248 | return NULL; |
| 3249 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3250 | #else /* ! HAVE_INET_ATON */ |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3251 | /* XXX Problem here: inet_aton('255.255.255.255') raises |
| 3252 | an exception while it should be a valid address. */ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3253 | packed_addr = inet_addr(ip_addr); |
| 3254 | |
| 3255 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3256 | PyErr_SetString(socket_error, |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3257 | "illegal IP address string passed to inet_aton"); |
| 3258 | return NULL; |
| 3259 | } |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3260 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 3261 | sizeof(packed_addr)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3262 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3265 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3266 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3267 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3268 | 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] | 3269 | |
| 3270 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3271 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3272 | { |
| 3273 | char *packed_str; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3274 | int addr_len; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3275 | struct in_addr packed_addr; |
| 3276 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3277 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3278 | return NULL; |
| 3279 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3280 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3281 | if (addr_len != sizeof(packed_addr)) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3282 | PyErr_SetString(socket_error, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3283 | "packed IP wrong length for inet_ntoa"); |
| 3284 | return NULL; |
| 3285 | } |
| 3286 | |
| 3287 | memcpy(&packed_addr, packed_str, addr_len); |
| 3288 | |
| 3289 | return PyString_FromString(inet_ntoa(packed_addr)); |
| 3290 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3291 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3292 | #ifdef HAVE_INET_PTON |
| 3293 | |
| 3294 | PyDoc_STRVAR(inet_pton_doc, |
| 3295 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 3296 | \n\ |
| 3297 | Convert an IP address from string format to a packed string suitable\n\ |
| 3298 | for use with low-level network functions."); |
| 3299 | |
| 3300 | static PyObject * |
| 3301 | socket_inet_pton(PyObject *self, PyObject *args) |
| 3302 | { |
| 3303 | int af; |
| 3304 | char* ip; |
| 3305 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3306 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3307 | 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] | 3308 | #else |
| 3309 | char packed[sizeof(struct in_addr)]; |
| 3310 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3311 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3312 | return NULL; |
| 3313 | } |
| 3314 | |
Martin v. Löwis | 04697e8 | 2004-06-02 12:35:29 +0000 | [diff] [blame] | 3315 | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3316 | if(af == AF_INET6) { |
| 3317 | PyErr_SetString(socket_error, |
| 3318 | "can't use AF_INET6, IPv6 is disabled"); |
| 3319 | return NULL; |
| 3320 | } |
| 3321 | #endif |
| 3322 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3323 | retval = inet_pton(af, ip, packed); |
| 3324 | if (retval < 0) { |
| 3325 | PyErr_SetFromErrno(socket_error); |
| 3326 | return NULL; |
| 3327 | } else if (retval == 0) { |
| 3328 | PyErr_SetString(socket_error, |
| 3329 | "illegal IP address string passed to inet_pton"); |
| 3330 | return NULL; |
| 3331 | } else if (af == AF_INET) { |
| 3332 | return PyString_FromStringAndSize(packed, |
| 3333 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3334 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3335 | } else if (af == AF_INET6) { |
| 3336 | return PyString_FromStringAndSize(packed, |
| 3337 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3338 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3339 | } else { |
| 3340 | PyErr_SetString(socket_error, "unknown address family"); |
| 3341 | return NULL; |
| 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | PyDoc_STRVAR(inet_ntop_doc, |
| 3346 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3347 | \n\ |
| 3348 | Convert a packed IP address of the given family to string format."); |
| 3349 | |
| 3350 | static PyObject * |
| 3351 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 3352 | { |
| 3353 | int af; |
| 3354 | char* packed; |
| 3355 | int len; |
| 3356 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3357 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3358 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3359 | #else |
| 3360 | char ip[INET_ADDRSTRLEN + 1]; |
| 3361 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3362 | |
| 3363 | /* Guarantee NUL-termination for PyString_FromString() below */ |
| 3364 | memset((void *) &ip[0], '\0', sizeof(ip) + 1); |
| 3365 | |
| 3366 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 3367 | return NULL; |
| 3368 | } |
| 3369 | |
| 3370 | if (af == AF_INET) { |
| 3371 | if (len != sizeof(struct in_addr)) { |
| 3372 | PyErr_SetString(PyExc_ValueError, |
| 3373 | "invalid length of packed IP address string"); |
| 3374 | return NULL; |
| 3375 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3376 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3377 | } else if (af == AF_INET6) { |
| 3378 | if (len != sizeof(struct in6_addr)) { |
| 3379 | PyErr_SetString(PyExc_ValueError, |
| 3380 | "invalid length of packed IP address string"); |
| 3381 | return NULL; |
| 3382 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3383 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3384 | } else { |
| 3385 | PyErr_Format(PyExc_ValueError, |
| 3386 | "unknown address family %d", af); |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | |
| 3390 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 3391 | if (!retval) { |
| 3392 | PyErr_SetFromErrno(socket_error); |
| 3393 | return NULL; |
| 3394 | } else { |
| 3395 | return PyString_FromString(retval); |
| 3396 | } |
| 3397 | |
| 3398 | /* NOTREACHED */ |
| 3399 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 3400 | return NULL; |
| 3401 | } |
| 3402 | |
| 3403 | #endif /* HAVE_INET_PTON */ |
| 3404 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3405 | /* Python interface to getaddrinfo(host, port). */ |
| 3406 | |
| 3407 | /*ARGSUSED*/ |
| 3408 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3409 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3410 | { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3411 | struct addrinfo hints, *res; |
| 3412 | struct addrinfo *res0 = NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3413 | PyObject *hobj = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3414 | PyObject *pobj = (PyObject *)NULL; |
Martin v. Löwis | f65b1a1 | 2001-08-12 09:28:40 +0000 | [diff] [blame] | 3415 | char pbuf[30]; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3416 | char *hptr, *pptr; |
| 3417 | int family, socktype, protocol, flags; |
| 3418 | int error; |
| 3419 | PyObject *all = (PyObject *)NULL; |
| 3420 | PyObject *single = (PyObject *)NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3421 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3422 | |
| 3423 | family = socktype = protocol = flags = 0; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3424 | family = AF_UNSPEC; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3425 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 3426 | &hobj, &pobj, &family, &socktype, |
| 3427 | &protocol, &flags)) { |
| 3428 | return NULL; |
| 3429 | } |
| 3430 | if (hobj == Py_None) { |
| 3431 | hptr = NULL; |
| 3432 | } else if (PyUnicode_Check(hobj)) { |
| 3433 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 3434 | if (!idna) |
| 3435 | return NULL; |
| 3436 | hptr = PyString_AsString(idna); |
| 3437 | } else if (PyString_Check(hobj)) { |
| 3438 | hptr = PyString_AsString(hobj); |
| 3439 | } else { |
| 3440 | PyErr_SetString(PyExc_TypeError, |
| 3441 | "getaddrinfo() argument 1 must be string or None"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3442 | return NULL; |
| 3443 | } |
| 3444 | if (PyInt_Check(pobj)) { |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3445 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3446 | pptr = pbuf; |
| 3447 | } else if (PyString_Check(pobj)) { |
| 3448 | pptr = PyString_AsString(pobj); |
| 3449 | } else if (pobj == Py_None) { |
| 3450 | pptr = (char *)NULL; |
| 3451 | } else { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3452 | PyErr_SetString(socket_error, "Int or String expected"); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3453 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3454 | } |
| 3455 | memset(&hints, 0, sizeof(hints)); |
| 3456 | hints.ai_family = family; |
| 3457 | hints.ai_socktype = socktype; |
| 3458 | hints.ai_protocol = protocol; |
| 3459 | hints.ai_flags = flags; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3460 | Py_BEGIN_ALLOW_THREADS |
| 3461 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3462 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3463 | Py_END_ALLOW_THREADS |
| 3464 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3465 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3466 | set_gaierror(error); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3467 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | if ((all = PyList_New(0)) == NULL) |
| 3471 | goto err; |
| 3472 | for (res = res0; res; res = res->ai_next) { |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3473 | PyObject *addr = |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 3474 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3475 | if (addr == NULL) |
| 3476 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3477 | single = Py_BuildValue("iiisO", res->ai_family, |
| 3478 | res->ai_socktype, res->ai_protocol, |
| 3479 | res->ai_canonname ? res->ai_canonname : "", |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3480 | addr); |
| 3481 | Py_DECREF(addr); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3482 | if (single == NULL) |
| 3483 | goto err; |
| 3484 | |
| 3485 | if (PyList_Append(all, single)) |
| 3486 | goto err; |
| 3487 | Py_XDECREF(single); |
| 3488 | } |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3489 | Py_XDECREF(idna); |
Neal Norwitz | 90128ba | 2002-08-09 03:37:42 +0000 | [diff] [blame] | 3490 | if (res0) |
| 3491 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3492 | return all; |
| 3493 | err: |
| 3494 | Py_XDECREF(single); |
| 3495 | Py_XDECREF(all); |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3496 | Py_XDECREF(idna); |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3497 | if (res0) |
| 3498 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3499 | return (PyObject *)NULL; |
| 3500 | } |
| 3501 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3502 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3503 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 3504 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3505 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3506 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3507 | |
| 3508 | /* Python interface to getnameinfo(sa, flags). */ |
| 3509 | |
| 3510 | /*ARGSUSED*/ |
| 3511 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3512 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3513 | { |
| 3514 | PyObject *sa = (PyObject *)NULL; |
| 3515 | int flags; |
| 3516 | char *hostp; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3517 | int port, flowinfo, scope_id; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3518 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 3519 | struct addrinfo hints, *res = NULL; |
| 3520 | int error; |
| 3521 | PyObject *ret = (PyObject *)NULL; |
| 3522 | |
| 3523 | flags = flowinfo = scope_id = 0; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3524 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3525 | return NULL; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3526 | if (!PyArg_ParseTuple(sa, "si|ii", |
| 3527 | &hostp, &port, &flowinfo, &scope_id)) |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3528 | return NULL; |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3529 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3530 | memset(&hints, 0, sizeof(hints)); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3531 | hints.ai_family = AF_UNSPEC; |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3532 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3533 | Py_BEGIN_ALLOW_THREADS |
| 3534 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3535 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3536 | Py_END_ALLOW_THREADS |
| 3537 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3538 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3539 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3540 | goto fail; |
| 3541 | } |
| 3542 | if (res->ai_next) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3543 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3544 | "sockaddr resolved to multiple addresses"); |
| 3545 | goto fail; |
| 3546 | } |
| 3547 | switch (res->ai_family) { |
| 3548 | case AF_INET: |
| 3549 | { |
| 3550 | char *t1; |
| 3551 | int t2; |
| 3552 | if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3553 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3554 | "IPv4 sockaddr must be 2 tuple"); |
| 3555 | goto fail; |
| 3556 | } |
| 3557 | break; |
| 3558 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3559 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3560 | case AF_INET6: |
| 3561 | { |
| 3562 | struct sockaddr_in6 *sin6; |
| 3563 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 3564 | sin6->sin6_flowinfo = flowinfo; |
| 3565 | sin6->sin6_scope_id = scope_id; |
| 3566 | break; |
| 3567 | } |
| 3568 | #endif |
| 3569 | } |
| 3570 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 3571 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 3572 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3573 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3574 | goto fail; |
| 3575 | } |
| 3576 | ret = Py_BuildValue("ss", hbuf, pbuf); |
| 3577 | |
| 3578 | fail: |
| 3579 | if (res) |
| 3580 | freeaddrinfo(res); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3581 | return ret; |
| 3582 | } |
| 3583 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3584 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3585 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3586 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3587 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3588 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3589 | |
| 3590 | /* Python API to getting and setting the default timeout value. */ |
| 3591 | |
| 3592 | static PyObject * |
| 3593 | socket_getdefaulttimeout(PyObject *self) |
| 3594 | { |
| 3595 | if (defaulttimeout < 0.0) { |
| 3596 | Py_INCREF(Py_None); |
| 3597 | return Py_None; |
| 3598 | } |
| 3599 | else |
| 3600 | return PyFloat_FromDouble(defaulttimeout); |
| 3601 | } |
| 3602 | |
| 3603 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3604 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3605 | \n\ |
| 3606 | Returns the default timeout in floating seconds for new socket objects.\n\ |
| 3607 | A value of None indicates that new socket objects have no timeout.\n\ |
| 3608 | When the socket module is first imported, the default is None."); |
| 3609 | |
| 3610 | static PyObject * |
| 3611 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 3612 | { |
| 3613 | double timeout; |
| 3614 | |
| 3615 | if (arg == Py_None) |
| 3616 | timeout = -1.0; |
| 3617 | else { |
| 3618 | timeout = PyFloat_AsDouble(arg); |
| 3619 | if (timeout < 0.0) { |
| 3620 | if (!PyErr_Occurred()) |
| 3621 | PyErr_SetString(PyExc_ValueError, |
| 3622 | "Timeout value out of range"); |
| 3623 | return NULL; |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | defaulttimeout = timeout; |
| 3628 | |
| 3629 | Py_INCREF(Py_None); |
| 3630 | return Py_None; |
| 3631 | } |
| 3632 | |
| 3633 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3634 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3635 | \n\ |
| 3636 | Set the default timeout in floating seconds for new socket objects.\n\ |
| 3637 | A value of None indicates that new socket objects have no timeout.\n\ |
| 3638 | When the socket module is first imported, the default is None."); |
| 3639 | |
| 3640 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3641 | /* List of functions exported by this module. */ |
| 3642 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3643 | static PyMethodDef socket_methods[] = { |
| 3644 | {"gethostbyname", socket_gethostbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3645 | METH_VARARGS, gethostbyname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3646 | {"gethostbyname_ex", socket_gethostbyname_ex, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3647 | METH_VARARGS, ghbn_ex_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3648 | {"gethostbyaddr", socket_gethostbyaddr, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3649 | METH_VARARGS, gethostbyaddr_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3650 | {"gethostname", socket_gethostname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3651 | METH_VARARGS, gethostname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3652 | {"getservbyname", socket_getservbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3653 | METH_VARARGS, getservbyname_doc}, |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 3654 | {"getservbyport", socket_getservbyport, |
| 3655 | METH_VARARGS, getservbyport_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3656 | {"getprotobyname", socket_getprotobyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3657 | METH_VARARGS,getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3658 | #ifndef NO_DUP |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3659 | {"fromfd", socket_fromfd, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3660 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3661 | #endif |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3662 | #ifdef HAVE_SOCKETPAIR |
| 3663 | {"socketpair", socket_socketpair, |
| 3664 | METH_VARARGS, socketpair_doc}, |
| 3665 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3666 | {"ntohs", socket_ntohs, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3667 | METH_VARARGS, ntohs_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3668 | {"ntohl", socket_ntohl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3669 | METH_O, ntohl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3670 | {"htons", socket_htons, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3671 | METH_VARARGS, htons_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3672 | {"htonl", socket_htonl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3673 | METH_O, htonl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3674 | {"inet_aton", socket_inet_aton, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3675 | METH_VARARGS, inet_aton_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3676 | {"inet_ntoa", socket_inet_ntoa, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3677 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3678 | #ifdef HAVE_INET_PTON |
| 3679 | {"inet_pton", socket_inet_pton, |
| 3680 | METH_VARARGS, inet_pton_doc}, |
| 3681 | {"inet_ntop", socket_inet_ntop, |
| 3682 | METH_VARARGS, inet_ntop_doc}, |
| 3683 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3684 | {"getaddrinfo", socket_getaddrinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3685 | METH_VARARGS, getaddrinfo_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3686 | {"getnameinfo", socket_getnameinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3687 | METH_VARARGS, getnameinfo_doc}, |
Guido van Rossum | 1693ba8 | 2002-07-18 21:11:26 +0000 | [diff] [blame] | 3688 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3689 | METH_NOARGS, getdefaulttimeout_doc}, |
| 3690 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 3691 | METH_O, setdefaulttimeout_doc}, |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3692 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3693 | }; |
| 3694 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3695 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3696 | #ifdef RISCOS |
| 3697 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3698 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3699 | static int |
| 3700 | os_init(void) |
| 3701 | { |
| 3702 | _kernel_swi_regs r; |
| 3703 | |
| 3704 | r.r[0] = 0; |
| 3705 | _kernel_swi(0x43380, &r, &r); |
| 3706 | taskwindow = r.r[0]; |
| 3707 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3708 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | #endif /* RISCOS */ |
| 3712 | |
| 3713 | |
| 3714 | #ifdef MS_WINDOWS |
| 3715 | #define OS_INIT_DEFINED |
| 3716 | |
| 3717 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3718 | |
| 3719 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3720 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3721 | { |
| 3722 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3726 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3727 | { |
| 3728 | WSADATA WSAData; |
| 3729 | int ret; |
| 3730 | char buf[100]; |
| 3731 | ret = WSAStartup(0x0101, &WSAData); |
| 3732 | switch (ret) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3733 | case 0: /* No error */ |
Mark Hammond | 14350ab | 2003-07-09 04:57:46 +0000 | [diff] [blame] | 3734 | Py_AtExit(os_cleanup); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3735 | return 1; /* Success */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3736 | case WSASYSNOTREADY: |
| 3737 | PyErr_SetString(PyExc_ImportError, |
| 3738 | "WSAStartup failed: network not ready"); |
| 3739 | break; |
| 3740 | case WSAVERNOTSUPPORTED: |
| 3741 | case WSAEINVAL: |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3742 | PyErr_SetString( |
| 3743 | PyExc_ImportError, |
| 3744 | "WSAStartup failed: requested version not supported"); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3745 | break; |
| 3746 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 3747 | PyOS_snprintf(buf, sizeof(buf), |
| 3748 | "WSAStartup failed: error code %d", ret); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3749 | PyErr_SetString(PyExc_ImportError, buf); |
| 3750 | break; |
| 3751 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3752 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3755 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3756 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3757 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3758 | #ifdef PYOS_OS2 |
| 3759 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3760 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3761 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3762 | |
| 3763 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3764 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3765 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3766 | #ifndef PYCC_GCC |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3767 | char reason[64]; |
| 3768 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3769 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3770 | if (rc == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3771 | return 1; /* Success */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3772 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3773 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3774 | PyOS_snprintf(reason, sizeof(reason), |
| 3775 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 3776 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3777 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3778 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 3779 | #else |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3780 | /* No need to initialise sockets with GCC/EMX */ |
| 3781 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 3782 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | #endif /* PYOS_OS2 */ |
| 3786 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3787 | |
| 3788 | #ifndef OS_INIT_DEFINED |
| 3789 | static int |
| 3790 | os_init(void) |
| 3791 | { |
| 3792 | return 1; /* Success */ |
| 3793 | } |
| 3794 | #endif |
| 3795 | |
| 3796 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3797 | /* C API table - always add new things to the end for binary |
| 3798 | compatibility. */ |
| 3799 | static |
| 3800 | PySocketModule_APIObject PySocketModuleAPI = |
| 3801 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3802 | &sock_type, |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 3803 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3804 | }; |
| 3805 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3806 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3807 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3808 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3809 | This module is actually called "_socket", and there's a wrapper |
| 3810 | "socket.py" which implements some additional functionality. On some |
| 3811 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 3812 | wrapper for the socket type that provides missing functionality such |
| 3813 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 3814 | with an ImportError exception if os-specific initialization fails. |
| 3815 | On Windows, this does WINSOCK initialization. When WINSOCK is |
| 3816 | initialized succesfully, a call to WSACleanup() is scheduled to be |
| 3817 | made at exit time. |
| 3818 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3820 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3821 | "Implementation module for socket operations.\n\ |
| 3822 | \n\ |
| 3823 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3824 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3825 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3826 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3827 | { |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3828 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3829 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3830 | if (!os_init()) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3831 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3832 | |
| 3833 | sock_type.ob_type = &PyType_Type; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3834 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 3835 | socket_methods, |
| 3836 | socket_doc); |
| 3837 | |
| 3838 | socket_error = PyErr_NewException("socket.error", NULL, NULL); |
| 3839 | if (socket_error == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3840 | return; |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 3841 | PySocketModuleAPI.error = socket_error; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3842 | Py_INCREF(socket_error); |
| 3843 | PyModule_AddObject(m, "error", socket_error); |
| 3844 | socket_herror = PyErr_NewException("socket.herror", |
| 3845 | socket_error, NULL); |
| 3846 | if (socket_herror == NULL) |
| 3847 | return; |
| 3848 | Py_INCREF(socket_herror); |
| 3849 | PyModule_AddObject(m, "herror", socket_herror); |
| 3850 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3851 | NULL); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3852 | if (socket_gaierror == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3853 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3854 | Py_INCREF(socket_gaierror); |
| 3855 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 3856 | socket_timeout = PyErr_NewException("socket.timeout", |
| 3857 | socket_error, NULL); |
| 3858 | if (socket_timeout == NULL) |
| 3859 | return; |
| 3860 | Py_INCREF(socket_timeout); |
| 3861 | PyModule_AddObject(m, "timeout", socket_timeout); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3862 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3863 | if (PyModule_AddObject(m, "SocketType", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3864 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3865 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3866 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3867 | if (PyModule_AddObject(m, "socket", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3868 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3869 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3870 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3871 | #ifdef ENABLE_IPV6 |
| 3872 | has_ipv6 = Py_True; |
| 3873 | #else |
| 3874 | has_ipv6 = Py_False; |
| 3875 | #endif |
| 3876 | Py_INCREF(has_ipv6); |
| 3877 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
| 3878 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3879 | /* Export C API */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3880 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3881 | PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL) |
| 3882 | ) != 0) |
| 3883 | return; |
| 3884 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3885 | /* Address families (we only support AF_INET and AF_UNIX) */ |
| 3886 | #ifdef AF_UNSPEC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3887 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3888 | #endif |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3889 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3890 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3891 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3892 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 3893 | #if defined(AF_UNIX) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3894 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3895 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3896 | #ifdef AF_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3897 | /* Amateur Radio AX.25 */ |
| 3898 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3899 | #endif |
| 3900 | #ifdef AF_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3901 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3902 | #endif |
| 3903 | #ifdef AF_APPLETALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3904 | /* Appletalk DDP */ |
| 3905 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3906 | #endif |
| 3907 | #ifdef AF_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3908 | /* Amateur radio NetROM */ |
| 3909 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3910 | #endif |
| 3911 | #ifdef AF_BRIDGE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3912 | /* Multiprotocol bridge */ |
| 3913 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3914 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 3915 | #ifdef AF_ATMPVC |
| 3916 | /* ATM PVCs */ |
| 3917 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
| 3918 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3919 | #ifdef AF_AAL5 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3920 | /* Reserved for Werner's ATM */ |
| 3921 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3922 | #endif |
| 3923 | #ifdef AF_X25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3924 | /* Reserved for X.25 project */ |
| 3925 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3926 | #endif |
| 3927 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3928 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3929 | #endif |
| 3930 | #ifdef AF_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3931 | /* Amateur Radio X.25 PLP */ |
| 3932 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3933 | #endif |
Martin v. Löwis | 81aec4b | 2004-07-19 17:01:20 +0000 | [diff] [blame] | 3934 | #ifdef AF_DECnet |
| 3935 | /* Reserved for DECnet project */ |
| 3936 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
| 3937 | #endif |
| 3938 | #ifdef AF_NETBEUI |
| 3939 | /* Reserved for 802.2LLC project */ |
| 3940 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
| 3941 | #endif |
| 3942 | #ifdef AF_SECURITY |
| 3943 | /* Security callback pseudo AF */ |
| 3944 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
| 3945 | #endif |
| 3946 | #ifdef AF_KEY |
| 3947 | /* PF_KEY key management API */ |
| 3948 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
| 3949 | #endif |
| 3950 | #ifdef AF_NETLINK |
| 3951 | /* */ |
| 3952 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 3953 | #endif |
| 3954 | #ifdef AF_ROUTE |
| 3955 | /* Alias to emulate 4.4BSD */ |
| 3956 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
| 3957 | #endif |
| 3958 | #ifdef AF_ASH |
| 3959 | /* Ash */ |
| 3960 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
| 3961 | #endif |
| 3962 | #ifdef AF_ECONET |
| 3963 | /* Acorn Econet */ |
| 3964 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
| 3965 | #endif |
| 3966 | #ifdef AF_ATMSVC |
| 3967 | /* ATM SVCs */ |
| 3968 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
| 3969 | #endif |
| 3970 | #ifdef AF_SNA |
| 3971 | /* Linux SNA Project (nutters!) */ |
| 3972 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
| 3973 | #endif |
| 3974 | #ifdef AF_IRDA |
| 3975 | /* IRDA sockets */ |
| 3976 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
| 3977 | #endif |
| 3978 | #ifdef AF_PPPOX |
| 3979 | /* PPPoX sockets */ |
| 3980 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
| 3981 | #endif |
| 3982 | #ifdef AF_WANPIPE |
| 3983 | /* Wanpipe API Sockets */ |
| 3984 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
| 3985 | #endif |
| 3986 | #ifdef AF_LLC |
| 3987 | /* Linux LLC */ |
| 3988 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
| 3989 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3990 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 3991 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3992 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 3993 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 3994 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3995 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 3996 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3997 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
Martin v. Löwis | 558d9bf | 2004-06-03 09:24:42 +0000 | [diff] [blame] | 3998 | PyModule_AddObject(m, "BDADDR_ANY", Py_BuildValue("s", "00:00:00:00:00:00")); |
| 3999 | PyModule_AddObject(m, "BDADDR_LOCAL", Py_BuildValue("s", "00:00:00:FF:FF:FF")); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 4000 | #endif |
| 4001 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 4002 | #ifdef HAVE_NETPACKET_PACKET_H |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4003 | PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); |
| 4004 | PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); |
| 4005 | PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); |
| 4006 | PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 4007 | PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 4008 | PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 4009 | PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 4010 | PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 4011 | PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 4012 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4013 | |
| 4014 | /* Socket types */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4015 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4016 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4017 | #ifndef __BEOS__ |
| 4018 | /* We have incomplete socket support. */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4019 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4020 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4021 | #if defined(SOCK_RDM) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4022 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 4023 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 4024 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4025 | |
| 4026 | #ifdef SO_DEBUG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4027 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4028 | #endif |
| 4029 | #ifdef SO_ACCEPTCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4030 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4031 | #endif |
| 4032 | #ifdef SO_REUSEADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4033 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4034 | #endif |
Andrew M. Kuchling | 42851ab | 2004-07-10 14:19:21 +0000 | [diff] [blame] | 4035 | #ifdef SO_EXCLUSIVEADDRUSE |
| 4036 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
| 4037 | #endif |
| 4038 | |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4039 | #ifdef SO_KEEPALIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4040 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4041 | #endif |
| 4042 | #ifdef SO_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4043 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4044 | #endif |
| 4045 | #ifdef SO_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4046 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4047 | #endif |
| 4048 | #ifdef SO_USELOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4049 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4050 | #endif |
| 4051 | #ifdef SO_LINGER |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4052 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4053 | #endif |
| 4054 | #ifdef SO_OOBINLINE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4055 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4056 | #endif |
| 4057 | #ifdef SO_REUSEPORT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4058 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4059 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4060 | #ifdef SO_SNDBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4061 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4062 | #endif |
| 4063 | #ifdef SO_RCVBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4064 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4065 | #endif |
| 4066 | #ifdef SO_SNDLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4067 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4068 | #endif |
| 4069 | #ifdef SO_RCVLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4070 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4071 | #endif |
| 4072 | #ifdef SO_SNDTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4073 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4074 | #endif |
| 4075 | #ifdef SO_RCVTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4076 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4077 | #endif |
| 4078 | #ifdef SO_ERROR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4079 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4080 | #endif |
| 4081 | #ifdef SO_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4082 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4083 | #endif |
| 4084 | |
| 4085 | /* Maximum number of connections for "listen" */ |
| 4086 | #ifdef SOMAXCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4087 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4088 | #else |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4089 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4090 | #endif |
| 4091 | |
| 4092 | /* Flags for send, recv */ |
| 4093 | #ifdef MSG_OOB |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4094 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4095 | #endif |
| 4096 | #ifdef MSG_PEEK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4097 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4098 | #endif |
| 4099 | #ifdef MSG_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4100 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4101 | #endif |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4102 | #ifdef MSG_DONTWAIT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4103 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 4104 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4105 | #ifdef MSG_EOR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4106 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4107 | #endif |
| 4108 | #ifdef MSG_TRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4109 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4110 | #endif |
| 4111 | #ifdef MSG_CTRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4112 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4113 | #endif |
| 4114 | #ifdef MSG_WAITALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4115 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4116 | #endif |
| 4117 | #ifdef MSG_BTAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4118 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4119 | #endif |
| 4120 | #ifdef MSG_ETAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4121 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4122 | #endif |
| 4123 | |
| 4124 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4125 | #ifdef SOL_SOCKET |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4126 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4127 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4128 | #ifdef SOL_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4129 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4130 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4131 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4132 | #endif |
| 4133 | #ifdef SOL_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4134 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4135 | #endif |
| 4136 | #ifdef SOL_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4137 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4138 | #endif |
| 4139 | #ifdef SOL_ATALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4140 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4141 | #endif |
| 4142 | #ifdef SOL_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4143 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4144 | #endif |
| 4145 | #ifdef SOL_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4146 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4147 | #endif |
| 4148 | #ifdef SOL_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4149 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4150 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4151 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4152 | #endif |
| 4153 | #ifdef SOL_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4154 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4155 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4156 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4157 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4158 | #ifdef IPPROTO_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4159 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4160 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4161 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4162 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4163 | #ifdef IPPROTO_HOPOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4164 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4165 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4166 | #ifdef IPPROTO_ICMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4167 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4168 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4169 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4170 | #endif |
| 4171 | #ifdef IPPROTO_IGMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4172 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4173 | #endif |
| 4174 | #ifdef IPPROTO_GGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4175 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4176 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4177 | #ifdef IPPROTO_IPV4 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4178 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4179 | #endif |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 4180 | #ifdef IPPROTO_IPV6 |
| 4181 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
| 4182 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4183 | #ifdef IPPROTO_IPIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4184 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4185 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4186 | #ifdef IPPROTO_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4187 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4188 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4189 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4190 | #endif |
| 4191 | #ifdef IPPROTO_EGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4192 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4193 | #endif |
| 4194 | #ifdef IPPROTO_PUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4195 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4196 | #endif |
| 4197 | #ifdef IPPROTO_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4198 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4199 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4200 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4201 | #endif |
| 4202 | #ifdef IPPROTO_IDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4203 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4204 | #endif |
| 4205 | #ifdef IPPROTO_HELLO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4206 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4207 | #endif |
| 4208 | #ifdef IPPROTO_ND |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4209 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4210 | #endif |
| 4211 | #ifdef IPPROTO_TP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4212 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4213 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4214 | #ifdef IPPROTO_IPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4215 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4216 | #endif |
| 4217 | #ifdef IPPROTO_ROUTING |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4218 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4219 | #endif |
| 4220 | #ifdef IPPROTO_FRAGMENT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4221 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4222 | #endif |
| 4223 | #ifdef IPPROTO_RSVP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4224 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4225 | #endif |
| 4226 | #ifdef IPPROTO_GRE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4227 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4228 | #endif |
| 4229 | #ifdef IPPROTO_ESP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4230 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4231 | #endif |
| 4232 | #ifdef IPPROTO_AH |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4233 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4234 | #endif |
| 4235 | #ifdef IPPROTO_MOBILE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4236 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4237 | #endif |
| 4238 | #ifdef IPPROTO_ICMPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4239 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4240 | #endif |
| 4241 | #ifdef IPPROTO_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4242 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4243 | #endif |
| 4244 | #ifdef IPPROTO_DSTOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4245 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4246 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4247 | #ifdef IPPROTO_XTP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4248 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4249 | #endif |
| 4250 | #ifdef IPPROTO_EON |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4251 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4252 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4253 | #ifdef IPPROTO_PIM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4254 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4255 | #endif |
| 4256 | #ifdef IPPROTO_IPCOMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4257 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4258 | #endif |
| 4259 | #ifdef IPPROTO_VRRP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4260 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4261 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4262 | #ifdef IPPROTO_BIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4263 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4264 | #endif |
| 4265 | /**/ |
| 4266 | #ifdef IPPROTO_RAW |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4267 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 4268 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4269 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4270 | #endif |
| 4271 | #ifdef IPPROTO_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4272 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4273 | #endif |
| 4274 | |
| 4275 | /* Some port configuration */ |
| 4276 | #ifdef IPPORT_RESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4277 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4278 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4279 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4280 | #endif |
| 4281 | #ifdef IPPORT_USERRESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4282 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4283 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4284 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4285 | #endif |
| 4286 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4287 | /* Some reserved IP v.4 addresses */ |
| 4288 | #ifdef INADDR_ANY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4289 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4290 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4291 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4292 | #endif |
| 4293 | #ifdef INADDR_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4294 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4295 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4296 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4297 | #endif |
| 4298 | #ifdef INADDR_LOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4299 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4300 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4301 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4302 | #endif |
| 4303 | #ifdef INADDR_UNSPEC_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4304 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4305 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4306 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4307 | #endif |
| 4308 | #ifdef INADDR_ALLHOSTS_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4309 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 4310 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4311 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4312 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4313 | #endif |
| 4314 | #ifdef INADDR_MAX_LOCAL_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4315 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 4316 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4317 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4318 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4319 | #endif |
| 4320 | #ifdef INADDR_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4321 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4322 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4323 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4324 | #endif |
| 4325 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4326 | /* IPv4 [gs]etsockopt options */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4327 | #ifdef IP_OPTIONS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4328 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4329 | #endif |
| 4330 | #ifdef IP_HDRINCL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4331 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4332 | #endif |
| 4333 | #ifdef IP_TOS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4334 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4335 | #endif |
| 4336 | #ifdef IP_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4337 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4338 | #endif |
| 4339 | #ifdef IP_RECVOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4340 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4341 | #endif |
| 4342 | #ifdef IP_RECVRETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4343 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4344 | #endif |
| 4345 | #ifdef IP_RECVDSTADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4346 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4347 | #endif |
| 4348 | #ifdef IP_RETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4349 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4350 | #endif |
| 4351 | #ifdef IP_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4352 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4353 | #endif |
| 4354 | #ifdef IP_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4355 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4356 | #endif |
| 4357 | #ifdef IP_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4358 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4359 | #endif |
| 4360 | #ifdef IP_ADD_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4361 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4362 | #endif |
| 4363 | #ifdef IP_DROP_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4364 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4365 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4366 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4367 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 4368 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4369 | #endif |
| 4370 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4371 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 4372 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4373 | #endif |
| 4374 | #ifdef IP_MAX_MEMBERSHIPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4375 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4376 | #endif |
| 4377 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4378 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 4379 | #ifdef IPV6_JOIN_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4380 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4381 | #endif |
| 4382 | #ifdef IPV6_LEAVE_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4383 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4384 | #endif |
| 4385 | #ifdef IPV6_MULTICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4386 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4387 | #endif |
| 4388 | #ifdef IPV6_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4389 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4390 | #endif |
| 4391 | #ifdef IPV6_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4392 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4393 | #endif |
| 4394 | #ifdef IPV6_UNICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4395 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4396 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4397 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
| 4398 | #ifdef IPV6_V6ONLY |
| 4399 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
| 4400 | #endif |
| 4401 | /* Advanced IPV6 socket options, from RFC 3542 */ |
| 4402 | #ifdef IPV6_CHECKSUM |
| 4403 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
| 4404 | #endif |
| 4405 | #ifdef IPV6_DONTFRAG |
| 4406 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
| 4407 | #endif |
| 4408 | #ifdef IPV6_DSTOPTS |
| 4409 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
| 4410 | #endif |
| 4411 | #ifdef IPV6_HOPLIMIT |
| 4412 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
| 4413 | #endif |
| 4414 | #ifdef IPV6_HOPOPTS |
| 4415 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
| 4416 | #endif |
| 4417 | #ifdef IPV6_NEXTHOP |
| 4418 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
| 4419 | #endif |
| 4420 | #ifdef IPV6_PATHMTU |
| 4421 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
| 4422 | #endif |
| 4423 | #ifdef IPV6_PKTINFO |
| 4424 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
| 4425 | #endif |
| 4426 | #ifdef IPV6_RECVDSTOPTS |
| 4427 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
| 4428 | #endif |
| 4429 | #ifdef IPV6_RECVHOPLIMIT |
| 4430 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
| 4431 | #endif |
| 4432 | #ifdef IPV6_RECVHOPOPTS |
| 4433 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
| 4434 | #endif |
| 4435 | #ifdef IPV6_RECVPKTINFO |
| 4436 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
| 4437 | #endif |
| 4438 | #ifdef IPV6_RECVRTHDR |
| 4439 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
| 4440 | #endif |
| 4441 | #ifdef IPV6_RECVTCLASS |
| 4442 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
| 4443 | #endif |
| 4444 | #ifdef IPV6_RTHDR |
| 4445 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
| 4446 | #endif |
| 4447 | #ifdef IPV6_RTHDRDSTOPTS |
| 4448 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
| 4449 | #endif |
| 4450 | #ifdef IPV6_RTHDR_TYPE_0 |
| 4451 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); |
| 4452 | #endif |
| 4453 | #ifdef IPV6_RECVPATHMTU |
| 4454 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
| 4455 | #endif |
| 4456 | #ifdef IPV6_TCLASS |
| 4457 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
| 4458 | #endif |
| 4459 | #ifdef IPV6_USE_MIN_MTU |
| 4460 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); |
| 4461 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4462 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4463 | /* TCP options */ |
| 4464 | #ifdef TCP_NODELAY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4465 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4466 | #endif |
| 4467 | #ifdef TCP_MAXSEG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4468 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4469 | #endif |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4470 | #ifdef TCP_CORK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4471 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4472 | #endif |
| 4473 | #ifdef TCP_KEEPIDLE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4474 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4475 | #endif |
| 4476 | #ifdef TCP_KEEPINTVL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4477 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4478 | #endif |
| 4479 | #ifdef TCP_KEEPCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4480 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4481 | #endif |
| 4482 | #ifdef TCP_SYNCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4483 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4484 | #endif |
| 4485 | #ifdef TCP_LINGER2 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4486 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4487 | #endif |
| 4488 | #ifdef TCP_DEFER_ACCEPT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4489 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4490 | #endif |
| 4491 | #ifdef TCP_WINDOW_CLAMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4492 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4493 | #endif |
| 4494 | #ifdef TCP_INFO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4495 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4496 | #endif |
| 4497 | #ifdef TCP_QUICKACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4498 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4499 | #endif |
| 4500 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4501 | |
| 4502 | /* IPX options */ |
| 4503 | #ifdef IPX_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4504 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4505 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4506 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4507 | /* get{addr,name}info parameters */ |
| 4508 | #ifdef EAI_ADDRFAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4509 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4510 | #endif |
| 4511 | #ifdef EAI_AGAIN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4512 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4513 | #endif |
| 4514 | #ifdef EAI_BADFLAGS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4515 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4516 | #endif |
| 4517 | #ifdef EAI_FAIL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4518 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4519 | #endif |
| 4520 | #ifdef EAI_FAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4521 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4522 | #endif |
| 4523 | #ifdef EAI_MEMORY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4524 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4525 | #endif |
| 4526 | #ifdef EAI_NODATA |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4527 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4528 | #endif |
| 4529 | #ifdef EAI_NONAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4530 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4531 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4532 | #ifdef EAI_OVERFLOW |
| 4533 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
| 4534 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4535 | #ifdef EAI_SERVICE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4536 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4537 | #endif |
| 4538 | #ifdef EAI_SOCKTYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4539 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4540 | #endif |
| 4541 | #ifdef EAI_SYSTEM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4542 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4543 | #endif |
| 4544 | #ifdef EAI_BADHINTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4545 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4546 | #endif |
| 4547 | #ifdef EAI_PROTOCOL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4548 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4549 | #endif |
| 4550 | #ifdef EAI_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4551 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4552 | #endif |
| 4553 | #ifdef AI_PASSIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4554 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4555 | #endif |
| 4556 | #ifdef AI_CANONNAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4557 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4558 | #endif |
| 4559 | #ifdef AI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4560 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4561 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4562 | #ifdef AI_NUMERICSERV |
| 4563 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
| 4564 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4565 | #ifdef AI_MASK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4566 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4567 | #endif |
| 4568 | #ifdef AI_ALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4569 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4570 | #endif |
| 4571 | #ifdef AI_V4MAPPED_CFG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4572 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4573 | #endif |
| 4574 | #ifdef AI_ADDRCONFIG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4575 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4576 | #endif |
| 4577 | #ifdef AI_V4MAPPED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4578 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4579 | #endif |
| 4580 | #ifdef AI_DEFAULT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4581 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4582 | #endif |
| 4583 | #ifdef NI_MAXHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4584 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4585 | #endif |
| 4586 | #ifdef NI_MAXSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4587 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4588 | #endif |
| 4589 | #ifdef NI_NOFQDN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4590 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4591 | #endif |
| 4592 | #ifdef NI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4593 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4594 | #endif |
| 4595 | #ifdef NI_NAMEREQD |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4596 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4597 | #endif |
| 4598 | #ifdef NI_NUMERICSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4599 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4600 | #endif |
| 4601 | #ifdef NI_DGRAM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4602 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4603 | #endif |
| 4604 | |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 4605 | /* shutdown() parameters */ |
| 4606 | #ifdef SHUT_RD |
| 4607 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
| 4608 | #elif defined(SD_RECEIVE) |
| 4609 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
| 4610 | #else |
| 4611 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
| 4612 | #endif |
| 4613 | #ifdef SHUT_WR |
| 4614 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
| 4615 | #elif defined(SD_SEND) |
| 4616 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
| 4617 | #else |
| 4618 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
| 4619 | #endif |
| 4620 | #ifdef SHUT_RDWR |
| 4621 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
| 4622 | #elif defined(SD_BOTH) |
| 4623 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
| 4624 | #else |
| 4625 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
| 4626 | #endif |
| 4627 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4628 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 4629 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 4630 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4631 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4632 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4633 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4634 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4635 | #ifndef HAVE_INET_PTON |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4636 | |
| 4637 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4638 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4639 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4640 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4641 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4642 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4643 | if (af == AF_INET) { |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4644 | long packed_addr; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4645 | packed_addr = inet_addr(src); |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4646 | if (packed_addr == INADDR_NONE) |
| 4647 | return 0; |
| 4648 | memcpy(dst, &packed_addr, 4); |
| 4649 | return 1; |
| 4650 | } |
| 4651 | /* Should set errno to EAFNOSUPPORT */ |
| 4652 | return -1; |
| 4653 | } |
| 4654 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 4655 | const char * |
| 4656 | 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] | 4657 | { |
| 4658 | if (af == AF_INET) { |
| 4659 | struct in_addr packed_addr; |
| 4660 | if (size < 16) |
| 4661 | /* Should set errno to ENOSPC. */ |
| 4662 | return NULL; |
| 4663 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 4664 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 4665 | } |
| 4666 | /* Should set errno to EAFNOSUPPORT */ |
| 4667 | return NULL; |
| 4668 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4669 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4670 | #endif |