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 |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 28 | - socket.getservbyname(servicename, protocolname) --> port number |
Guido van Rossum | 83a072d | 2002-09-03 19:10:18 +0000 | [diff] [blame] | 29 | - socket.socket([family[, type [, proto]]]) --> new socket object |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 30 | - socket.ntohs(16 bit value) --> new int object |
| 31 | - socket.ntohl(32 bit value) --> new int object |
| 32 | - socket.htons(16 bit value) --> new int object |
| 33 | - socket.htonl(32 bit value) --> new int object |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 34 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
| 35 | --> List of (family, socktype, proto, canonname, sockaddr) |
| 36 | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
Guido van Rossum | 27e177d | 1995-03-16 15:43:47 +0000 | [diff] [blame] | 37 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 38 | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 39 | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 40 | - socket.inet_ntoa(packed IP) -> IP address string |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 41 | - socket.getdefaulttimeout() -> None | float |
| 42 | - socket.setdefaulttimeout(None | float) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 43 | - an Internet socket address is a pair (hostname, port) |
| 44 | where hostname can be anything recognized by gethostbyname() |
| 45 | (including the dd.dd.dd.dd notation) and port is in host byte order |
| 46 | - 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] | 47 | - a UNIX domain socket address is a string specifying the pathname |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 48 | - an AF_PACKET socket address is a tuple containing a string |
| 49 | specifying the ethernet interface and an integer specifying |
| 50 | the Ethernet protocol number to be received. For example: |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 51 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
| 52 | specify packet-type and ha-type/addr -- these are ignored by |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 53 | networking code, but accepted since they are returned by the |
| 54 | getsockname() method. |
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 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 281 | #ifndef HAVE_INET_PTON |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 282 | int inet_pton(int af, const char *src, void *dst); |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 283 | 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] | 284 | #endif |
| 285 | |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 286 | #ifdef __APPLE__ |
| 287 | /* On OS X, getaddrinfo returns no error indication of lookup |
| 288 | failure, so we must use the emulation instead of the libinfo |
| 289 | implementation. Unfortunately, performing an autoconf test |
| 290 | for this bug would require DNS access for the machine performing |
| 291 | the configuration, which is not acceptable. Therefore, we |
| 292 | determine the bug just by checking for __APPLE__. If this bug |
| 293 | gets ever fixed, perhaps checking for sys/version.h would be |
| 294 | appropriate, which is 10/0 on the system with the bug. */ |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 295 | #ifndef HAVE_GETNAMEINFO |
| 296 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
| 297 | Find to check for Jaguar is that it has getnameinfo(), which |
| 298 | older releases don't have */ |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 299 | #undef HAVE_GETADDRINFO |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 300 | #endif |
Jack Jansen | 84262fb | 2002-07-02 14:40:42 +0000 | [diff] [blame] | 301 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 302 | |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 303 | /* 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] | 304 | #if !defined(HAVE_GETADDRINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 305 | /* avoid clashes with the C library definition of the symbol. */ |
| 306 | #define getaddrinfo fake_getaddrinfo |
| 307 | #define gai_strerror fake_gai_strerror |
| 308 | #define freeaddrinfo fake_freeaddrinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 309 | #include "getaddrinfo.c" |
| 310 | #endif |
Martin v. Löwis | ae26dc2 | 2001-08-03 10:02:29 +0000 | [diff] [blame] | 311 | #if !defined(HAVE_GETNAMEINFO) |
Martin v. Löwis | fccac2e | 2003-05-01 05:20:46 +0000 | [diff] [blame] | 312 | #define getnameinfo fake_getnameinfo |
Martin v. Löwis | 01dfdb3 | 2001-06-23 16:30:13 +0000 | [diff] [blame] | 313 | #include "getnameinfo.c" |
| 314 | #endif |
| 315 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 316 | #if defined(MS_WINDOWS) || defined(__BEOS__) |
| 317 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 318 | /* seem to be a few differences in the API */ |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 319 | #define SOCKETCLOSE closesocket |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 320 | #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] | 321 | #endif |
| 322 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 323 | #ifdef MS_WIN32 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 324 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
| 325 | #define snprintf _snprintf |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 326 | #endif |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 327 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 328 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 329 | #define SOCKETCLOSE soclose |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 330 | #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] | 331 | #endif |
| 332 | |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 333 | #ifndef SOCKETCLOSE |
| 334 | #define SOCKETCLOSE close |
| 335 | #endif |
| 336 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 337 | #ifdef __VMS |
| 338 | /* TCP/IP Services for VMS uses a maximum send/revc buffer length of 65535 */ |
| 339 | #define SEGMENT_SIZE 65535 |
| 340 | #endif |
| 341 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 342 | #if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) |
| 343 | #define USE_BLUETOOTH 1 |
| 344 | #if defined(__FreeBSD__) |
| 345 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
| 346 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
| 347 | #define sockaddr_l2 sockaddr_l2cap |
| 348 | #define sockaddr_rc sockaddr_rfcomm |
| 349 | #define _BT_SOCKADDR_MEMB(s, proto) &((s)->sock_addr) |
| 350 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
| 351 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
| 352 | #else |
Anthony Baxter | 5d7c067 | 2004-02-16 05:35:28 +0000 | [diff] [blame] | 353 | #define _BT_SOCKADDR_MEMB(s, proto) (&((s)->sock_addr).bt_##proto) |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 354 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
| 355 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
| 356 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
| 357 | #endif |
| 358 | #endif |
| 359 | |
Martin v. Löwis | e941617 | 2003-05-03 10:12:45 +0000 | [diff] [blame] | 360 | /* |
| 361 | * Constants for getnameinfo() |
| 362 | */ |
| 363 | #if !defined(NI_MAXHOST) |
| 364 | #define NI_MAXHOST 1025 |
| 365 | #endif |
| 366 | #if !defined(NI_MAXSERV) |
| 367 | #define NI_MAXSERV 32 |
| 368 | #endif |
| 369 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 370 | /* XXX There's a problem here: *static* functions are not supposed to have |
| 371 | a Py prefix (or use CapitalizedWords). Later... */ |
| 372 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 373 | /* Global variable holding the exception type for errors detected |
| 374 | by this module (but not argument type or memory errors, etc.). */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 375 | static PyObject *socket_error; |
| 376 | static PyObject *socket_herror; |
| 377 | static PyObject *socket_gaierror; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 378 | static PyObject *socket_timeout; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 380 | #ifdef RISCOS |
| 381 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */ |
| 382 | static int taskwindow; |
| 383 | #endif |
| 384 | |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 385 | /* A forward reference to the socket type object. |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 386 | The sock_type variable contains pointers to various functions, |
| 387 | some of which call new_sockobject(), which uses sock_type, so |
Tim Peters | 643a7fc | 2002-02-17 04:13:21 +0000 | [diff] [blame] | 388 | there has to be a circular reference. */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 389 | static PyTypeObject sock_type; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 391 | /* Convenience function to raise an error according to errno |
| 392 | and return a NULL pointer from a function. */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 394 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 395 | set_error(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 396 | { |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 397 | #ifdef MS_WINDOWS |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 398 | int err_no = WSAGetLastError(); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 399 | static struct { |
| 400 | int no; |
| 401 | const char *msg; |
| 402 | } *msgp, msgs[] = { |
| 403 | {WSAEINTR, "Interrupted system call"}, |
| 404 | {WSAEBADF, "Bad file descriptor"}, |
| 405 | {WSAEACCES, "Permission denied"}, |
| 406 | {WSAEFAULT, "Bad address"}, |
| 407 | {WSAEINVAL, "Invalid argument"}, |
| 408 | {WSAEMFILE, "Too many open files"}, |
| 409 | {WSAEWOULDBLOCK, |
| 410 | "The socket operation could not complete " |
| 411 | "without blocking"}, |
| 412 | {WSAEINPROGRESS, "Operation now in progress"}, |
| 413 | {WSAEALREADY, "Operation already in progress"}, |
| 414 | {WSAENOTSOCK, "Socket operation on non-socket"}, |
| 415 | {WSAEDESTADDRREQ, "Destination address required"}, |
| 416 | {WSAEMSGSIZE, "Message too long"}, |
| 417 | {WSAEPROTOTYPE, "Protocol wrong type for socket"}, |
| 418 | {WSAENOPROTOOPT, "Protocol not available"}, |
| 419 | {WSAEPROTONOSUPPORT, "Protocol not supported"}, |
| 420 | {WSAESOCKTNOSUPPORT, "Socket type not supported"}, |
| 421 | {WSAEOPNOTSUPP, "Operation not supported"}, |
| 422 | {WSAEPFNOSUPPORT, "Protocol family not supported"}, |
| 423 | {WSAEAFNOSUPPORT, "Address family not supported"}, |
| 424 | {WSAEADDRINUSE, "Address already in use"}, |
| 425 | {WSAEADDRNOTAVAIL, "Can't assign requested address"}, |
| 426 | {WSAENETDOWN, "Network is down"}, |
| 427 | {WSAENETUNREACH, "Network is unreachable"}, |
| 428 | {WSAENETRESET, "Network dropped connection on reset"}, |
| 429 | {WSAECONNABORTED, "Software caused connection abort"}, |
| 430 | {WSAECONNRESET, "Connection reset by peer"}, |
| 431 | {WSAENOBUFS, "No buffer space available"}, |
| 432 | {WSAEISCONN, "Socket is already connected"}, |
| 433 | {WSAENOTCONN, "Socket is not connected"}, |
| 434 | {WSAESHUTDOWN, "Can't send after socket shutdown"}, |
| 435 | {WSAETOOMANYREFS, "Too many references: can't splice"}, |
| 436 | {WSAETIMEDOUT, "Operation timed out"}, |
| 437 | {WSAECONNREFUSED, "Connection refused"}, |
| 438 | {WSAELOOP, "Too many levels of symbolic links"}, |
| 439 | {WSAENAMETOOLONG, "File name too long"}, |
| 440 | {WSAEHOSTDOWN, "Host is down"}, |
| 441 | {WSAEHOSTUNREACH, "No route to host"}, |
| 442 | {WSAENOTEMPTY, "Directory not empty"}, |
| 443 | {WSAEPROCLIM, "Too many processes"}, |
| 444 | {WSAEUSERS, "Too many users"}, |
| 445 | {WSAEDQUOT, "Disc quota exceeded"}, |
| 446 | {WSAESTALE, "Stale NFS file handle"}, |
| 447 | {WSAEREMOTE, "Too many levels of remote in path"}, |
| 448 | {WSASYSNOTREADY, "Network subsystem is unvailable"}, |
| 449 | {WSAVERNOTSUPPORTED, "WinSock version is not supported"}, |
| 450 | {WSANOTINITIALISED, |
| 451 | "Successful WSAStartup() not yet performed"}, |
| 452 | {WSAEDISCON, "Graceful shutdown in progress"}, |
| 453 | /* Resolver errors */ |
| 454 | {WSAHOST_NOT_FOUND, "No such host is known"}, |
| 455 | {WSATRY_AGAIN, "Host not found, or server failed"}, |
| 456 | {WSANO_RECOVERY, "Unexpected server error encountered"}, |
| 457 | {WSANO_DATA, "Valid name without requested data"}, |
| 458 | {WSANO_ADDRESS, "No address, look for MX record"}, |
| 459 | {0, NULL} |
| 460 | }; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 461 | if (err_no) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 462 | PyObject *v; |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 463 | const char *msg = "winsock error"; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 464 | |
Mark Hammond | 46a733d | 2000-07-24 01:45:11 +0000 | [diff] [blame] | 465 | for (msgp = msgs; msgp->msg; msgp++) { |
| 466 | if (err_no == msgp->no) { |
| 467 | msg = msgp->msg; |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | v = Py_BuildValue("(is)", err_no, msg); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 473 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 474 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 475 | Py_DECREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 476 | } |
| 477 | return NULL; |
| 478 | } |
| 479 | else |
| 480 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 481 | |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 482 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 483 | if (sock_errno() != NO_ERROR) { |
| 484 | APIRET rc; |
| 485 | ULONG msglen; |
| 486 | char outbuf[100]; |
| 487 | int myerrorcode = sock_errno(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 488 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 489 | /* Retrieve socket-related error message from MPTN.MSG file */ |
| 490 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 491 | myerrorcode - SOCBASEERR + 26, |
| 492 | "mptn.msg", |
| 493 | &msglen); |
| 494 | if (rc == NO_ERROR) { |
| 495 | PyObject *v; |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 497 | /* OS/2 doesn't guarantee a terminator */ |
| 498 | outbuf[msglen] = '\0'; |
| 499 | if (strlen(outbuf) > 0) { |
| 500 | /* If non-empty msg, trim CRLF */ |
| 501 | char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 502 | while (lastc > outbuf && isspace(*lastc)) { |
| 503 | /* Trim trailing whitespace (CRLF) */ |
| 504 | *lastc-- = '\0'; |
| 505 | } |
| 506 | } |
| 507 | v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 508 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 509 | PyErr_SetObject(socket_error, v); |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 510 | Py_DECREF(v); |
| 511 | } |
| 512 | return NULL; |
| 513 | } |
| 514 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 515 | #endif |
| 516 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 517 | #if defined(RISCOS) |
| 518 | if (_inet_error.errnum != NULL) { |
| 519 | PyObject *v; |
| 520 | v = Py_BuildValue("(is)", errno, _inet_err()); |
| 521 | if (v != NULL) { |
| 522 | PyErr_SetObject(socket_error, v); |
| 523 | Py_DECREF(v); |
| 524 | } |
| 525 | return NULL; |
| 526 | } |
| 527 | #endif |
| 528 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 529 | return PyErr_SetFromErrno(socket_error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 532 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 533 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 534 | set_herror(int h_error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 535 | { |
| 536 | PyObject *v; |
| 537 | |
| 538 | #ifdef HAVE_HSTRERROR |
| 539 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
| 540 | #else |
| 541 | v = Py_BuildValue("(is)", h_error, "host not found"); |
| 542 | #endif |
| 543 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 544 | PyErr_SetObject(socket_herror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 545 | Py_DECREF(v); |
| 546 | } |
| 547 | |
| 548 | return NULL; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 553 | set_gaierror(int error) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 554 | { |
| 555 | PyObject *v; |
| 556 | |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 557 | #ifdef EAI_SYSTEM |
| 558 | /* EAI_SYSTEM is not available on Windows XP. */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 559 | if (error == EAI_SYSTEM) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 560 | return set_error(); |
Martin v. Löwis | 272cb40 | 2002-03-01 08:31:07 +0000 | [diff] [blame] | 561 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 562 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 563 | #ifdef HAVE_GAI_STRERROR |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 564 | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 565 | #else |
| 566 | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
| 567 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 568 | if (v != NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 569 | PyErr_SetObject(socket_gaierror, v); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 570 | Py_DECREF(v); |
| 571 | } |
| 572 | |
| 573 | return NULL; |
| 574 | } |
| 575 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 576 | /* Function to perform the setting of socket blocking mode |
| 577 | internally. block = (1 | 0). */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 578 | static int |
| 579 | internal_setblocking(PySocketSockObject *s, int block) |
| 580 | { |
| 581 | #ifndef RISCOS |
| 582 | #ifndef MS_WINDOWS |
| 583 | int delay_flag; |
| 584 | #endif |
| 585 | #endif |
| 586 | |
| 587 | Py_BEGIN_ALLOW_THREADS |
| 588 | #ifdef __BEOS__ |
| 589 | block = !block; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 590 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK, |
| 591 | (void *)(&block), sizeof(int)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 592 | #else |
| 593 | #ifndef RISCOS |
| 594 | #ifndef MS_WINDOWS |
| 595 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 596 | block = !block; |
| 597 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 598 | #elif defined(__VMS) |
| 599 | block = !block; |
| 600 | ioctl(s->sock_fd, FIONBIO, (char *)&block); |
| 601 | #else /* !PYOS_OS2 && !_VMS */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 602 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 603 | if (block) |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 604 | delay_flag &= (~O_NONBLOCK); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 605 | else |
Neal Norwitz | 39d22e5 | 2002-11-02 19:55:21 +0000 | [diff] [blame] | 606 | delay_flag |= O_NONBLOCK; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 607 | fcntl(s->sock_fd, F_SETFL, delay_flag); |
| 608 | #endif /* !PYOS_OS2 */ |
| 609 | #else /* MS_WINDOWS */ |
| 610 | block = !block; |
| 611 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
| 612 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 613 | #else /* RISCOS */ |
| 614 | block = !block; |
| 615 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 616 | #endif /* RISCOS */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 617 | #endif /* __BEOS__ */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 618 | Py_END_ALLOW_THREADS |
| 619 | |
| 620 | /* Since these don't return anything */ |
| 621 | return 1; |
| 622 | } |
| 623 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 624 | /* Do a select() on the socket, if necessary (sock_timeout > 0). |
| 625 | The argument writing indicates the direction. |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 626 | This does not raise an exception; we'll let our caller do that |
| 627 | after they've reacquired the interpreter lock. |
| 628 | Returns 1 on timeout, 0 otherwise. */ |
| 629 | static int |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 630 | internal_select(PySocketSockObject *s, int writing) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 631 | { |
| 632 | fd_set fds; |
| 633 | struct timeval tv; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 634 | int n; |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 636 | /* 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] | 637 | if (s->sock_timeout <= 0.0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 638 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 639 | |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 640 | /* Guard against closed socket */ |
| 641 | if (s->sock_fd < 0) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 642 | return 0; |
Guido van Rossum | ad65490 | 2002-07-19 12:44:59 +0000 | [diff] [blame] | 643 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 644 | /* Construct the arguments to select */ |
| 645 | tv.tv_sec = (int)s->sock_timeout; |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 646 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 647 | FD_ZERO(&fds); |
| 648 | FD_SET(s->sock_fd, &fds); |
| 649 | |
| 650 | /* See if the socket is ready */ |
Guido van Rossum | b9e916a | 2002-06-07 01:42:47 +0000 | [diff] [blame] | 651 | if (writing) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 652 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 653 | else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 654 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 655 | if (n == 0) |
| 656 | return 1; |
| 657 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 660 | /* Initialize a new socket object. */ |
| 661 | |
Tim Peters | a12b4cf | 2002-07-18 22:38:44 +0000 | [diff] [blame] | 662 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 663 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 664 | PyMODINIT_FUNC |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 665 | init_sockobject(PySocketSockObject *s, |
| 666 | SOCKET_T fd, int family, int type, int proto) |
| 667 | { |
| 668 | #ifdef RISCOS |
| 669 | int block = 1; |
| 670 | #endif |
| 671 | s->sock_fd = fd; |
| 672 | s->sock_family = family; |
| 673 | s->sock_type = type; |
| 674 | s->sock_proto = proto; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 675 | s->sock_timeout = defaulttimeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 676 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 677 | s->errorhandler = &set_error; |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 678 | |
| 679 | if (defaulttimeout >= 0.0) |
| 680 | internal_setblocking(s, 0); |
| 681 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 682 | #ifdef RISCOS |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 683 | if (taskwindow) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 684 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 685 | #endif |
| 686 | } |
| 687 | |
| 688 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 689 | /* Create a new socket object. |
| 690 | This just creates the object and initializes it. |
| 691 | If the creation fails, return NULL and set an exception (implicit |
| 692 | in NEWOBJ()). */ |
| 693 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 694 | static PySocketSockObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 695 | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 696 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 697 | PySocketSockObject *s; |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 698 | s = (PySocketSockObject *) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 699 | PyType_GenericNew(&sock_type, NULL, NULL); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 700 | if (s != NULL) |
| 701 | init_sockobject(s, fd, family, type, proto); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 702 | return s; |
| 703 | } |
| 704 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 705 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 706 | /* Lock to allow python interpreter to continue, but only allow one |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 707 | thread to be in gethostbyname or getaddrinfo */ |
| 708 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 709 | PyThread_type_lock netdb_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 710 | #endif |
| 711 | |
| 712 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 713 | /* Convert a string specifying a host name or one of a few symbolic |
| 714 | names to a numeric IP address. This usually calls gethostbyname() |
| 715 | to do the work; the names "" and "<broadcast>" are special. |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 716 | Return the length (IPv4 should be 4 bytes), or negative if |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 717 | an error occurred; then an exception is raised. */ |
| 718 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 719 | static int |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 720 | 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] | 721 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 722 | struct addrinfo hints, *res; |
| 723 | int error; |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 724 | int d1, d2, d3, d4; |
| 725 | char ch; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 726 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 727 | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 728 | if (name[0] == '\0') { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 729 | int siz; |
| 730 | memset(&hints, 0, sizeof(hints)); |
| 731 | hints.ai_family = af; |
| 732 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 733 | hints.ai_flags = AI_PASSIVE; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 734 | Py_BEGIN_ALLOW_THREADS |
| 735 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 736 | error = getaddrinfo(NULL, "0", &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 737 | Py_END_ALLOW_THREADS |
| 738 | /* We assume that those thread-unsafe getaddrinfo() versions |
| 739 | *are* safe regarding their return value, ie. that a |
| 740 | subsequent call to getaddrinfo() does not destroy the |
| 741 | outcome of the first call. */ |
| 742 | RELEASE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 743 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 744 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 745 | return -1; |
| 746 | } |
| 747 | switch (res->ai_family) { |
| 748 | case AF_INET: |
| 749 | siz = 4; |
| 750 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 751 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 752 | case AF_INET6: |
| 753 | siz = 16; |
| 754 | break; |
| 755 | #endif |
| 756 | default: |
| 757 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 758 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 759 | "unsupported address family"); |
| 760 | return -1; |
| 761 | } |
| 762 | if (res->ai_next) { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 763 | freeaddrinfo(res); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 764 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 765 | "wildcard resolved to multiple address"); |
| 766 | return -1; |
| 767 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 768 | if (res->ai_addrlen < addr_ret_size) |
| 769 | addr_ret_size = res->ai_addrlen; |
| 770 | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 771 | freeaddrinfo(res); |
| 772 | return siz; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 773 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 774 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 775 | struct sockaddr_in *sin; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 776 | if (af != AF_INET && af != AF_UNSPEC) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 777 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 778 | "address family mismatched"); |
| 779 | return -1; |
| 780 | } |
| 781 | sin = (struct sockaddr_in *)addr_ret; |
| 782 | memset((void *) sin, '\0', sizeof(*sin)); |
| 783 | sin->sin_family = AF_INET; |
| 784 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 785 | sin->sin_len = sizeof(*sin); |
| 786 | #endif |
| 787 | sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 788 | return sizeof(sin->sin_addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 789 | } |
Anthony Baxter | 0e85f9d | 2003-05-02 15:40:46 +0000 | [diff] [blame] | 790 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 791 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 792 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 793 | struct sockaddr_in *sin; |
| 794 | sin = (struct sockaddr_in *)addr_ret; |
| 795 | sin->sin_addr.s_addr = htonl( |
| 796 | ((long) d1 << 24) | ((long) d2 << 16) | |
| 797 | ((long) d3 << 8) | ((long) d4 << 0)); |
| 798 | sin->sin_family = AF_INET; |
| 799 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 800 | sin->sin_len = sizeof(*sin); |
| 801 | #endif |
| 802 | return 4; |
| 803 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 804 | memset(&hints, 0, sizeof(hints)); |
| 805 | hints.ai_family = af; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 806 | Py_BEGIN_ALLOW_THREADS |
| 807 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 808 | error = getaddrinfo(name, NULL, &hints, &res); |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 809 | #if defined(__digital__) && defined(__unix__) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 810 | if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 811 | /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 812 | if no address family is given. Assume IPv4 for now.*/ |
| 813 | hints.ai_family = AF_INET; |
| 814 | error = getaddrinfo(name, NULL, &hints, &res); |
| 815 | } |
Martin v. Löwis | 7c4b5fa | 2001-10-25 09:04:03 +0000 | [diff] [blame] | 816 | #endif |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 817 | Py_END_ALLOW_THREADS |
| 818 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 819 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 820 | set_gaierror(error); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 821 | return -1; |
| 822 | } |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 823 | if (res->ai_addrlen < addr_ret_size) |
| 824 | addr_ret_size = res->ai_addrlen; |
| 825 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 826 | freeaddrinfo(res); |
| 827 | switch (addr_ret->sa_family) { |
| 828 | case AF_INET: |
| 829 | return 4; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 830 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 831 | case AF_INET6: |
| 832 | return 16; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 833 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 834 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 835 | PyErr_SetString(socket_error, "unknown address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 836 | return -1; |
| 837 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 840 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 841 | /* Create a string object representing an IP address. |
| 842 | This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 843 | size numbers). */ |
| 844 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 845 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 846 | makeipaddr(struct sockaddr *addr, int addrlen) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 847 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 848 | char buf[NI_MAXHOST]; |
| 849 | int error; |
| 850 | |
| 851 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 852 | NI_NUMERICHOST); |
| 853 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 854 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 855 | return NULL; |
| 856 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 857 | return PyString_FromString(buf); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | |
| 861 | /* Create an object representing the given socket address, |
| 862 | suitable for passing it back to bind(), connect() etc. |
| 863 | The family field of the sockaddr structure is inspected |
| 864 | to determine what kind of address it really is. */ |
| 865 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 866 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 867 | static PyObject * |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 868 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 869 | { |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 870 | if (addrlen == 0) { |
| 871 | /* No address -- may be recvfrom() from known socket */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 872 | Py_INCREF(Py_None); |
| 873 | return Py_None; |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 876 | #ifdef __BEOS__ |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 877 | /* XXX: BeOS version of accept() doesn't set family correctly */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 878 | addr->sa_family = AF_INET; |
| 879 | #endif |
| 880 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 881 | switch (addr->sa_family) { |
| 882 | |
| 883 | case AF_INET: |
| 884 | { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 885 | struct sockaddr_in *a; |
| 886 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 887 | PyObject *ret = NULL; |
| 888 | if (addrobj) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 889 | a = (struct sockaddr_in *)addr; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 890 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 891 | Py_DECREF(addrobj); |
| 892 | } |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 893 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 894 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 895 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 896 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 897 | case AF_UNIX: |
| 898 | { |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 899 | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 900 | return PyString_FromString(a->sun_path); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 901 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 902 | #endif /* AF_UNIX */ |
| 903 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 904 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 905 | case AF_INET6: |
| 906 | { |
| 907 | struct sockaddr_in6 *a; |
| 908 | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 909 | PyObject *ret = NULL; |
| 910 | if (addrobj) { |
| 911 | a = (struct sockaddr_in6 *)addr; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 912 | ret = Py_BuildValue("Oiii", |
| 913 | addrobj, |
| 914 | ntohs(a->sin6_port), |
| 915 | a->sin6_flowinfo, |
| 916 | a->sin6_scope_id); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 917 | Py_DECREF(addrobj); |
| 918 | } |
| 919 | return ret; |
| 920 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 921 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 922 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 923 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 924 | case AF_PACKET: |
| 925 | { |
| 926 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 927 | char *ifname = ""; |
| 928 | struct ifreq ifr; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 929 | /* need to look up interface name give index */ |
| 930 | if (a->sll_ifindex) { |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 931 | ifr.ifr_ifindex = a->sll_ifindex; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 932 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 933 | ifname = ifr.ifr_name; |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 934 | } |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 935 | return Py_BuildValue("shbhs#", |
| 936 | ifname, |
| 937 | ntohs(a->sll_protocol), |
| 938 | a->sll_pkttype, |
| 939 | a->sll_hatype, |
| 940 | a->sll_addr, |
| 941 | a->sll_halen); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 942 | } |
| 943 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 944 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 945 | /* More cases here... */ |
| 946 | |
| 947 | default: |
Guido van Rossum | aa948df | 1997-05-07 17:41:48 +0000 | [diff] [blame] | 948 | /* If we don't know the address family, don't raise an |
| 949 | exception -- return it as a tuple. */ |
| 950 | return Py_BuildValue("is#", |
| 951 | addr->sa_family, |
| 952 | addr->sa_data, |
| 953 | sizeof(addr->sa_data)); |
Guido van Rossum | 25bec8c | 1992-08-05 19:00:45 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 955 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 958 | |
| 959 | /* Parse a socket address argument according to the socket object's |
| 960 | address family. Return 1 if the address was in the proper format, |
| 961 | 0 of not. The address is returned through addr_ret, its length |
| 962 | through len_ret. */ |
| 963 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 964 | static int |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 965 | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 966 | struct sockaddr **addr_ret, int *len_ret) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 967 | { |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 968 | switch (s->sock_family) { |
| 969 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 970 | #if defined(AF_UNIX) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 971 | case AF_UNIX: |
| 972 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 973 | struct sockaddr_un* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 974 | char *path; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 975 | int len; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 976 | addr = (struct sockaddr_un*)&(s->sock_addr).un; |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 977 | if (!PyArg_Parse(args, "t#", &path, &len)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 978 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 979 | if (len > sizeof addr->sun_path) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 980 | PyErr_SetString(socket_error, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 981 | "AF_UNIX path too long"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 982 | return 0; |
| 983 | } |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 984 | addr->sun_family = s->sock_family; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 985 | memcpy(addr->sun_path, path, len); |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 986 | addr->sun_path[len] = 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 987 | *addr_ret = (struct sockaddr *) addr; |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 988 | #if defined(PYOS_OS2) |
| 989 | *len_ret = sizeof(*addr); |
| 990 | #else |
Guido van Rossum | 65af28a | 1996-06-11 18:36:33 +0000 | [diff] [blame] | 991 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path); |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 992 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 993 | return 1; |
| 994 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 995 | #endif /* AF_UNIX */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 996 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 997 | case AF_INET: |
| 998 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 999 | struct sockaddr_in* addr; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1000 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1001 | int port, result; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1002 | addr=(struct sockaddr_in*)&(s->sock_addr).in; |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1003 | if (!PyTuple_Check(args)) { |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1004 | PyErr_Format( |
| 1005 | PyExc_TypeError, |
| 1006 | "getsockaddrarg: " |
| 1007 | "AF_INET address must be tuple, not %.500s", |
| 1008 | args->ob_type->tp_name); |
Guido van Rossum | e4dad90 | 2000-12-01 13:13:11 +0000 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1011 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1012 | "idna", &host, &port)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1013 | return 0; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1014 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1015 | sizeof(*addr), AF_INET); |
| 1016 | PyMem_Free(host); |
| 1017 | if (result < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1018 | return 0; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1019 | addr->sin_family = AF_INET; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 1020 | addr->sin_port = htons((short)port); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1021 | *addr_ret = (struct sockaddr *) addr; |
| 1022 | *len_ret = sizeof *addr; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1023 | return 1; |
| 1024 | } |
| 1025 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1026 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1027 | case AF_INET6: |
| 1028 | { |
| 1029 | struct sockaddr_in6* addr; |
| 1030 | char *host; |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1031 | int port, flowinfo, scope_id, result; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1032 | addr = (struct sockaddr_in6*)&(s->sock_addr).in6; |
| 1033 | flowinfo = scope_id = 0; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1034 | if (!PyArg_ParseTuple(args, "eti|ii", |
| 1035 | "idna", &host, &port, &flowinfo, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1036 | &scope_id)) { |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 1039 | result = setipaddr(host, (struct sockaddr *)addr, |
| 1040 | sizeof(*addr), AF_INET6); |
| 1041 | PyMem_Free(host); |
| 1042 | if (result < 0) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1043 | return 0; |
| 1044 | addr->sin6_family = s->sock_family; |
| 1045 | addr->sin6_port = htons((short)port); |
| 1046 | addr->sin6_flowinfo = flowinfo; |
| 1047 | addr->sin6_scope_id = scope_id; |
| 1048 | *addr_ret = (struct sockaddr *) addr; |
| 1049 | *len_ret = sizeof *addr; |
| 1050 | return 1; |
| 1051 | } |
| 1052 | #endif |
| 1053 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1054 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1055 | case AF_BLUETOOTH: |
| 1056 | { |
| 1057 | switch( s->sock_proto ) |
| 1058 | { |
| 1059 | case BTPROTO_L2CAP: |
| 1060 | { |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1061 | struct sockaddr_l2* addr = (struct sockaddr_l2*)_BT_SOCKADDR_MEMB(s, l2); |
| 1062 | bdaddr_t* bdaddr = &_BT_L2_MEMB(addr, bdaddr); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1063 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1064 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1065 | if( !PyArg_ParseTuple(args, "(iiiiii)i", &bdaddr->b[0], &bdaddr->b[1], &bdaddr->b[2], &bdaddr->b[3], &bdaddr->b[4], &bdaddr->b[5], &_BT_L2_MEMB(addr, psm)) ) |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1066 | { |
| 1067 | PyErr_SetString(socket_error, "getsockaddrarg: wrong format"); |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | *addr_ret = (struct sockaddr *) addr; |
| 1072 | *len_ret = sizeof *addr; |
| 1073 | return 1; |
| 1074 | } |
| 1075 | case BTPROTO_RFCOMM: |
| 1076 | { |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1077 | struct sockaddr_rc* addr = (struct sockaddr_rc*)_BT_SOCKADDR_MEMB(s, rc); |
| 1078 | bdaddr_t* bdaddr = &_BT_RC_MEMB(addr, bdaddr); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1079 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1080 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1081 | if( !PyArg_ParseTuple(args, "(iiiiii)i", &bdaddr->b[0], &bdaddr->b[1], &bdaddr->b[2], &bdaddr->b[3], &bdaddr->b[4], &bdaddr->b[5], &_BT_RC_MEMB(addr, channel)) ) |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1082 | { |
| 1083 | PyErr_SetString(socket_error, "getsockaddrarg: wrong format"); |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | *addr_ret = (struct sockaddr *) addr; |
| 1088 | *len_ret = sizeof *addr; |
| 1089 | return 1; |
| 1090 | } |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1091 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1092 | case BTPROTO_SCO: |
| 1093 | { |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1094 | struct sockaddr_sco* addr = (struct sockaddr_sco*)_BT_SOCKADDR_MEMB(s, sco); |
| 1095 | bdaddr_t* bdaddr = &_BT_SCO_MEMB(addr, bdaddr); |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1096 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1097 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1098 | if( !PyArg_ParseTuple(args, "iiiiii", &bdaddr->b[0], &bdaddr->b[1], &bdaddr->b[2], &bdaddr->b[3], &bdaddr->b[4], &bdaddr->b[5]) ) |
| 1099 | { |
| 1100 | PyErr_SetString(socket_error, "getsockaddrarg: wrong format"); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | *addr_ret = (struct sockaddr *) addr; |
| 1105 | *len_ret = sizeof *addr; |
| 1106 | return 1; |
| 1107 | } |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1108 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1109 | default: |
| 1110 | { |
| 1111 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1112 | return 0; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | #endif |
| 1117 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1118 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1119 | case AF_PACKET: |
| 1120 | { |
| 1121 | struct sockaddr_ll* addr; |
| 1122 | struct ifreq ifr; |
| 1123 | char *interfaceName; |
| 1124 | int protoNumber; |
| 1125 | int hatype = 0; |
| 1126 | int pkttype = 0; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1127 | char *haddr; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1128 | |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1129 | if (!PyArg_ParseTuple(args, "si|iis", &interfaceName, |
| 1130 | &protoNumber, &pkttype, &hatype, &haddr)) |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1131 | return 0; |
| 1132 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1133 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1134 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1135 | s->errorhandler(); |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1136 | return 0; |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1137 | } |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1138 | addr = &(s->sock_addr.ll); |
| 1139 | addr->sll_family = AF_PACKET; |
| 1140 | addr->sll_protocol = htons((short)protoNumber); |
| 1141 | addr->sll_ifindex = ifr.ifr_ifindex; |
| 1142 | addr->sll_pkttype = pkttype; |
| 1143 | addr->sll_hatype = hatype; |
| 1144 | *addr_ret = (struct sockaddr *) addr; |
| 1145 | *len_ret = sizeof *addr; |
| 1146 | return 1; |
| 1147 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1148 | #endif |
| 1149 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1150 | /* More cases here... */ |
| 1151 | |
| 1152 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1153 | PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1154 | return 0; |
| 1155 | |
| 1156 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1159 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1160 | /* 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] | 1161 | Return 1 if the family is known, 0 otherwise. The length is returned |
| 1162 | through len_ret. */ |
| 1163 | |
| 1164 | static int |
Peter Schneider-Kamp | 618e25d | 2000-07-11 23:00:12 +0000 | [diff] [blame] | 1165 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1166 | { |
| 1167 | switch (s->sock_family) { |
| 1168 | |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 1169 | #if defined(AF_UNIX) |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1170 | case AF_UNIX: |
| 1171 | { |
| 1172 | *len_ret = sizeof (struct sockaddr_un); |
| 1173 | return 1; |
| 1174 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1175 | #endif /* AF_UNIX */ |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1176 | |
| 1177 | case AF_INET: |
| 1178 | { |
| 1179 | *len_ret = sizeof (struct sockaddr_in); |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 1183 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 1184 | case AF_INET6: |
| 1185 | { |
| 1186 | *len_ret = sizeof (struct sockaddr_in6); |
| 1187 | return 1; |
| 1188 | } |
| 1189 | #endif |
| 1190 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1191 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1192 | case AF_BLUETOOTH: |
| 1193 | { |
| 1194 | switch(s->sock_proto) |
| 1195 | { |
| 1196 | case BTPROTO_L2CAP: |
| 1197 | { |
| 1198 | *len_ret = sizeof (struct sockaddr_l2); |
| 1199 | return 1; |
| 1200 | } |
| 1201 | case BTPROTO_RFCOMM: |
| 1202 | { |
| 1203 | *len_ret = sizeof (struct sockaddr_rc); |
| 1204 | return 1; |
| 1205 | } |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1206 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1207 | case BTPROTO_SCO: |
| 1208 | { |
| 1209 | *len_ret = sizeof (struct sockaddr_sco); |
| 1210 | return 1; |
| 1211 | } |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 1212 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 1213 | default: |
| 1214 | { |
| 1215 | PyErr_SetString(socket_error, "getsockaddrlen: unknown BT protocol"); |
| 1216 | return 0; |
| 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | #endif |
| 1221 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 1222 | #ifdef HAVE_NETPACKET_PACKET_H |
Jeremy Hylton | 2230865 | 2001-02-02 03:23:09 +0000 | [diff] [blame] | 1223 | case AF_PACKET: |
| 1224 | { |
| 1225 | *len_ret = sizeof (struct sockaddr_ll); |
| 1226 | return 1; |
| 1227 | } |
| 1228 | #endif |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1229 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1230 | /* More cases here... */ |
| 1231 | |
| 1232 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1233 | PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1234 | return 0; |
| 1235 | |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1240 | /* s.accept() method */ |
| 1241 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1242 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1243 | sock_accept(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1244 | { |
| 1245 | char addrbuf[256]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1246 | SOCKET_T newfd; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1247 | socklen_t addrlen; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1248 | PyObject *sock = NULL; |
| 1249 | PyObject *addr = NULL; |
| 1250 | PyObject *res = NULL; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1251 | int timeout; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1252 | |
Guido van Rossum | 710e1df | 1992-06-12 10:39:36 +0000 | [diff] [blame] | 1253 | if (!getsockaddrlen(s, &addrlen)) |
| 1254 | return NULL; |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 1255 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1256 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1257 | #ifdef MS_WINDOWS |
| 1258 | newfd = INVALID_SOCKET; |
| 1259 | #else |
| 1260 | newfd = -1; |
| 1261 | #endif |
| 1262 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1263 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1264 | timeout = internal_select(s, 0); |
| 1265 | if (!timeout) |
| 1266 | newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf, |
| 1267 | &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1268 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1269 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1270 | if (timeout) { |
| 1271 | PyErr_SetString(socket_timeout, "timed out"); |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1275 | #ifdef MS_WINDOWS |
| 1276 | if (newfd == INVALID_SOCKET) |
| 1277 | #else |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1278 | if (newfd < 0) |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1279 | #endif |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1280 | return s->errorhandler(); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1281 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1282 | /* Create the new object with unspecified family, |
| 1283 | to avoid calls to bind() etc. on it. */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1284 | sock = (PyObject *) new_sockobject(newfd, |
| 1285 | s->sock_family, |
| 1286 | s->sock_type, |
| 1287 | s->sock_proto); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1288 | |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1289 | if (sock == NULL) { |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1290 | SOCKETCLOSE(newfd); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1291 | goto finally; |
| 1292 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1293 | addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1294 | addrlen); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1295 | if (addr == NULL) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1296 | goto finally; |
| 1297 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1298 | res = PyTuple_Pack(2, sock, addr); |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1299 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1300 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1301 | Py_XDECREF(sock); |
| 1302 | Py_XDECREF(addr); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1303 | return res; |
| 1304 | } |
| 1305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1306 | PyDoc_STRVAR(accept_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1307 | "accept() -> (socket object, address info)\n\ |
| 1308 | \n\ |
| 1309 | Wait for an incoming connection. Return a new socket representing the\n\ |
| 1310 | 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] | 1311 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1312 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1313 | /* s.setblocking(flag) method. Argument: |
| 1314 | False -- non-blocking mode; same as settimeout(0) |
| 1315 | True -- blocking mode; same as settimeout(None) |
| 1316 | */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1317 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1318 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1319 | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1320 | { |
| 1321 | int block; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1322 | |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1323 | block = PyInt_AsLong(arg); |
| 1324 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1325 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1326 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1327 | s->sock_timeout = block ? -1.0 : 0.0; |
Guido van Rossum | e8008f0 | 2002-06-07 03:36:20 +0000 | [diff] [blame] | 1328 | internal_setblocking(s, block); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1329 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1330 | Py_INCREF(Py_None); |
| 1331 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1332 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1334 | PyDoc_STRVAR(setblocking_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1335 | "setblocking(flag)\n\ |
| 1336 | \n\ |
| 1337 | 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] | 1338 | setblocking(True) is equivalent to settimeout(None);\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1339 | setblocking(False) is equivalent to settimeout(0.0)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1340 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1341 | /* s.settimeout(timeout) method. Argument: |
| 1342 | None -- no timeout, blocking mode; same as setblocking(True) |
| 1343 | 0.0 -- non-blocking mode; same as setblocking(False) |
| 1344 | > 0 -- timeout mode; operations time out after timeout seconds |
| 1345 | < 0 -- illegal; raises an exception |
| 1346 | */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1347 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1348 | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1349 | { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1350 | double timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1351 | |
| 1352 | if (arg == Py_None) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1353 | timeout = -1.0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1354 | else { |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1355 | timeout = PyFloat_AsDouble(arg); |
| 1356 | if (timeout < 0.0) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1357 | if (!PyErr_Occurred()) |
| 1358 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1359 | "Timeout value out of range"); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1360 | return NULL; |
| 1361 | } |
| 1362 | } |
| 1363 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1364 | s->sock_timeout = timeout; |
| 1365 | internal_setblocking(s, timeout < 0.0); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1366 | |
| 1367 | Py_INCREF(Py_None); |
| 1368 | return Py_None; |
| 1369 | } |
| 1370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1371 | PyDoc_STRVAR(settimeout_doc, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1372 | "settimeout(timeout)\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1373 | \n\ |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1374 | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
| 1375 | giving in seconds, or None. Setting a timeout of None disables\n\ |
| 1376 | the timeout feature and is equivalent to setblocking(1).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1377 | Setting a timeout of zero is the same as setblocking(0)."); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1378 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 1379 | /* s.gettimeout() method. |
| 1380 | Returns the timeout associated with a socket. */ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1381 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1382 | sock_gettimeout(PySocketSockObject *s) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1383 | { |
| 1384 | if (s->sock_timeout < 0.0) { |
| 1385 | Py_INCREF(Py_None); |
| 1386 | return Py_None; |
| 1387 | } |
| 1388 | else |
| 1389 | return PyFloat_FromDouble(s->sock_timeout); |
| 1390 | } |
| 1391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1392 | PyDoc_STRVAR(gettimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1393 | "gettimeout() -> timeout\n\ |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1394 | \n\ |
| 1395 | Returns the timeout in floating seconds associated with socket \n\ |
| 1396 | 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] | 1397 | operations are disabled."); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 1398 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1399 | #ifdef RISCOS |
| 1400 | /* s.sleeptaskw(1 | 0) method */ |
| 1401 | |
| 1402 | static PyObject * |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1403 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg) |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1404 | { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1405 | int block; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 1406 | block = PyInt_AsLong(arg); |
| 1407 | if (block == -1 && PyErr_Occurred()) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1408 | return NULL; |
| 1409 | Py_BEGIN_ALLOW_THREADS |
| 1410 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); |
| 1411 | Py_END_ALLOW_THREADS |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1412 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1413 | Py_INCREF(Py_None); |
| 1414 | return Py_None; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1415 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1416 | PyDoc_STRVAR(sleeptaskw_doc, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1417 | "sleeptaskw(flag)\n\ |
| 1418 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1419 | Allow sleeps in taskwindows."); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1420 | #endif |
| 1421 | |
| 1422 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1423 | /* s.setsockopt() method. |
| 1424 | With an integer third argument, sets an integer option. |
| 1425 | With a string third argument, sets an option from a buffer; |
| 1426 | use optional built-in module 'struct' to encode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1427 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1428 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1429 | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1430 | { |
| 1431 | int level; |
| 1432 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1433 | int res; |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1434 | char *buf; |
| 1435 | int buflen; |
| 1436 | int flag; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1437 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1438 | if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1439 | &level, &optname, &flag)) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1440 | buf = (char *) &flag; |
| 1441 | buflen = sizeof flag; |
| 1442 | } |
| 1443 | else { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1444 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1445 | if (!PyArg_ParseTuple(args, "iis#:setsockopt", |
| 1446 | &level, &optname, &buf, &buflen)) |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1447 | return NULL; |
| 1448 | } |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1449 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1450 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1451 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1452 | Py_INCREF(Py_None); |
| 1453 | return Py_None; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1456 | PyDoc_STRVAR(setsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1457 | "setsockopt(level, option, value)\n\ |
| 1458 | \n\ |
| 1459 | 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] | 1460 | The value argument can either be an integer or a string."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1462 | |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1463 | /* s.getsockopt() method. |
| 1464 | With two arguments, retrieves an integer option. |
| 1465 | With a third integer argument, retrieves a string buffer of that size; |
| 1466 | use optional built-in module 'struct' to decode the string. */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1467 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1468 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1469 | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1470 | { |
| 1471 | int level; |
| 1472 | int optname; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1473 | int res; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1474 | PyObject *buf; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1475 | socklen_t buflen = 0; |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1476 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1477 | #ifdef __BEOS__ |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1478 | /* We have incomplete socket support. */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1479 | PyErr_SetString(socket_error, "getsockopt not supported"); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1480 | return NULL; |
| 1481 | #else |
| 1482 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 1483 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1484 | &level, &optname, &buflen)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1485 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1486 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1487 | if (buflen == 0) { |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1488 | int flag = 0; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1489 | socklen_t flagsize = sizeof flag; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1490 | res = getsockopt(s->sock_fd, level, optname, |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1491 | (void *)&flag, &flagsize); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1492 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1493 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1494 | return PyInt_FromLong(flag); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1495 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1496 | #ifdef __VMS |
| 1497 | if (buflen > 1024) { |
| 1498 | #else |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1499 | if (buflen <= 0 || buflen > 1024) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1500 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1501 | PyErr_SetString(socket_error, |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1502 | "getsockopt buflen out of range"); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1503 | return NULL; |
| 1504 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1505 | buf = PyString_FromStringAndSize((char *)NULL, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1506 | if (buf == NULL) |
| 1507 | return NULL; |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 1508 | res = getsockopt(s->sock_fd, level, optname, |
Fred Drake | 564a6cc | 2001-05-11 20:12:26 +0000 | [diff] [blame] | 1509 | (void *)PyString_AS_STRING(buf), &buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1510 | if (res < 0) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1511 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1512 | return s->errorhandler(); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1513 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1514 | _PyString_Resize(&buf, buflen); |
Guido van Rossum | aee0879 | 1992-09-08 09:05:33 +0000 | [diff] [blame] | 1515 | return buf; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 1516 | #endif /* __BEOS__ */ |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1519 | PyDoc_STRVAR(getsockopt_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1520 | "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1521 | \n\ |
| 1522 | Get a socket option. See the Unix manual for level and option.\n\ |
| 1523 | 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] | 1524 | string of that length; otherwise it is an integer."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1525 | |
Guido van Rossum | 0e69587d | 1992-06-05 15:11:30 +0000 | [diff] [blame] | 1526 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1527 | /* s.bind(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1528 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1529 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1530 | sock_bind(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1531 | { |
| 1532 | struct sockaddr *addr; |
| 1533 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1534 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1535 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1536 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1537 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1538 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1539 | res = bind(s->sock_fd, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1540 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1541 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1542 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1543 | Py_INCREF(Py_None); |
| 1544 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1547 | PyDoc_STRVAR(bind_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1548 | "bind(address)\n\ |
| 1549 | \n\ |
| 1550 | 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] | 1551 | 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] | 1552 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1553 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1554 | |
| 1555 | /* s.close() method. |
| 1556 | Set the file descriptor to -1 so operations tried subsequently |
| 1557 | will surely fail. */ |
| 1558 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1559 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1560 | sock_close(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1561 | { |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1562 | SOCKET_T fd; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1563 | |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1564 | if ((fd = s->sock_fd) != -1) { |
| 1565 | s->sock_fd = -1; |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1566 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 20d3fc0 | 2000-12-18 22:23:44 +0000 | [diff] [blame] | 1567 | (void) SOCKETCLOSE(fd); |
Guido van Rossum | 0848146 | 1996-10-12 14:07:22 +0000 | [diff] [blame] | 1568 | Py_END_ALLOW_THREADS |
| 1569 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1570 | Py_INCREF(Py_None); |
| 1571 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1574 | PyDoc_STRVAR(close_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1575 | "close()\n\ |
| 1576 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1577 | Close the socket. It cannot be used after this call."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1578 | |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1579 | static int |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1580 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
| 1581 | int *timeoutp) |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1582 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1583 | int res, timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1584 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1585 | timeout = 0; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1586 | res = connect(s->sock_fd, addr, addrlen); |
| 1587 | |
| 1588 | #ifdef MS_WINDOWS |
| 1589 | |
| 1590 | if (s->sock_timeout > 0.0) { |
| 1591 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1592 | /* This is a mess. Best solution: trust select */ |
| 1593 | fd_set fds; |
| 1594 | struct timeval tv; |
| 1595 | tv.tv_sec = (int)s->sock_timeout; |
| 1596 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1597 | FD_ZERO(&fds); |
| 1598 | FD_SET(s->sock_fd, &fds); |
| 1599 | res = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1600 | if (res == 0) { |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1601 | res = WSAEWOULDBLOCK; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1602 | timeout = 1; |
| 1603 | } else if (res > 0) |
Guido van Rossum | b76bdf8 | 2003-02-19 17:50:16 +0000 | [diff] [blame] | 1604 | res = 0; |
| 1605 | /* else if (res < 0) an error occurred */ |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | if (res < 0) |
| 1610 | res = WSAGetLastError(); |
| 1611 | |
| 1612 | #else |
| 1613 | |
| 1614 | if (s->sock_timeout > 0.0) { |
| 1615 | if (res < 0 && errno == EINPROGRESS) { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1616 | timeout = internal_select(s, 1); |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1617 | res = connect(s->sock_fd, addr, addrlen); |
Guido van Rossum | 12e3c71 | 2002-08-08 20:39:30 +0000 | [diff] [blame] | 1618 | if (res < 0 && errno == EISCONN) |
| 1619 | res = 0; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | if (res < 0) |
| 1624 | res = errno; |
| 1625 | |
| 1626 | #endif |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1627 | *timeoutp = timeout; |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1628 | |
| 1629 | return res; |
| 1630 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1631 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1632 | /* s.connect(sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1633 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1634 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1635 | sock_connect(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1636 | { |
| 1637 | struct sockaddr *addr; |
| 1638 | int addrlen; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1639 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1640 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1641 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1642 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1643 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1644 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1645 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1646 | res = internal_connect(s, addr, addrlen, &timeout); |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1647 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1648 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1649 | if (timeout) { |
| 1650 | PyErr_SetString(socket_timeout, "timed out"); |
| 1651 | return NULL; |
| 1652 | } |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +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(connect_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1660 | "connect(address)\n\ |
| 1661 | \n\ |
| 1662 | 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] | 1663 | is a pair (host, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1664 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1665 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1666 | /* s.connect_ex(sockaddr) method */ |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1667 | |
| 1668 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1669 | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1670 | { |
| 1671 | struct sockaddr *addr; |
| 1672 | int addrlen; |
| 1673 | int res; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1674 | int timeout; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1675 | |
Fred Drake | 728819a | 2000-07-01 03:40:12 +0000 | [diff] [blame] | 1676 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1677 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1678 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1679 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1680 | res = internal_connect(s, addr, addrlen, &timeout); |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1681 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1682 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1683 | return PyInt_FromLong((long) res); |
| 1684 | } |
| 1685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1686 | PyDoc_STRVAR(connect_ex_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 1687 | "connect_ex(address) -> errno\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1688 | \n\ |
| 1689 | 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] | 1690 | instead of raising an exception when an error occurs."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1691 | |
Guido van Rossum | fc4255d | 1997-11-19 18:57:13 +0000 | [diff] [blame] | 1692 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1693 | /* s.fileno() method */ |
| 1694 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1695 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1696 | sock_fileno(PySocketSockObject *s) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1697 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1698 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1699 | return PyInt_FromLong((long) s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1700 | #else |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1701 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1702 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1705 | PyDoc_STRVAR(fileno_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1706 | "fileno() -> integer\n\ |
| 1707 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1708 | Return the integer file descriptor of the socket."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1709 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1710 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1711 | #ifndef NO_DUP |
| 1712 | /* s.dup() method */ |
| 1713 | |
| 1714 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1715 | sock_dup(PySocketSockObject *s) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1716 | { |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1717 | SOCKET_T newfd; |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1718 | PyObject *sock; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1719 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1720 | newfd = dup(s->sock_fd); |
| 1721 | if (newfd < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1722 | return s->errorhandler(); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1723 | sock = (PyObject *) new_sockobject(newfd, |
| 1724 | s->sock_family, |
| 1725 | s->sock_type, |
| 1726 | s->sock_proto); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1727 | if (sock == NULL) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1728 | SOCKETCLOSE(newfd); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1729 | return sock; |
| 1730 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1732 | PyDoc_STRVAR(dup_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1733 | "dup() -> socket object\n\ |
| 1734 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1735 | Return a new socket object connected to the same system resource."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1736 | |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1737 | #endif |
| 1738 | |
| 1739 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1740 | /* s.getsockname() method */ |
| 1741 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1742 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1743 | sock_getsockname(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1744 | { |
| 1745 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1746 | int res; |
| 1747 | socklen_t addrlen; |
| 1748 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1749 | if (!getsockaddrlen(s, &addrlen)) |
| 1750 | return NULL; |
Guido van Rossum | ca42b16 | 1996-01-12 01:36:05 +0000 | [diff] [blame] | 1751 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1752 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1753 | res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1754 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1755 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1756 | return s->errorhandler(); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1757 | return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1760 | PyDoc_STRVAR(getsockname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1761 | "getsockname() -> address info\n\ |
| 1762 | \n\ |
| 1763 | 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] | 1764 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1765 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1766 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1767 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1768 | /* s.getpeername() method */ |
| 1769 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1770 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1771 | sock_getpeername(PySocketSockObject *s) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1772 | { |
| 1773 | char addrbuf[256]; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1774 | int res; |
| 1775 | socklen_t addrlen; |
| 1776 | |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1777 | if (!getsockaddrlen(s, &addrlen)) |
| 1778 | return NULL; |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 1779 | memset(addrbuf, 0, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1780 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1781 | res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1782 | Py_END_ALLOW_THREADS |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1783 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1784 | return s->errorhandler(); |
Jeremy Hylton | dbfb662 | 2001-02-02 19:55:17 +0000 | [diff] [blame] | 1785 | return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1786 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1787 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1788 | PyDoc_STRVAR(getpeername_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1789 | "getpeername() -> address info\n\ |
| 1790 | \n\ |
| 1791 | 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] | 1792 | info is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1793 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1794 | #endif /* HAVE_GETPEERNAME */ |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1795 | |
| 1796 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1797 | /* s.listen(n) method */ |
| 1798 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1799 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1800 | sock_listen(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1801 | { |
| 1802 | int backlog; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1803 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 1804 | |
| 1805 | backlog = PyInt_AsLong(arg); |
| 1806 | if (backlog == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1807 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1808 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1809 | if (backlog < 1) |
| 1810 | backlog = 1; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1811 | res = listen(s->sock_fd, backlog); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1812 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 1813 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1814 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1815 | Py_INCREF(Py_None); |
| 1816 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1819 | PyDoc_STRVAR(listen_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1820 | "listen(backlog)\n\ |
| 1821 | \n\ |
| 1822 | Enable a server to accept connections. The backlog argument must be at\n\ |
| 1823 | 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] | 1824 | will allow before refusing new connections."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1825 | |
| 1826 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1827 | #ifndef NO_DUP |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1828 | /* s.makefile(mode) method. |
| 1829 | Create a new open file object referring to a dupped version of |
| 1830 | the socket's file descriptor. (The dup() call is necessary so |
| 1831 | that the open file and socket objects may be closed independent |
| 1832 | of each other.) |
| 1833 | The mode argument specifies 'r' or 'w' passed to fdopen(). */ |
| 1834 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1835 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1836 | sock_makefile(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1837 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1838 | extern int fclose(FILE *); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1839 | char *mode = "r"; |
| 1840 | int bufsize = -1; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1841 | #ifdef MS_WIN32 |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 1842 | Py_intptr_t fd; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 1843 | #else |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1844 | int fd; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1845 | #endif |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1846 | FILE *fp; |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1847 | PyObject *f; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1848 | #ifdef __VMS |
| 1849 | char *mode_r = "r"; |
| 1850 | char *mode_w = "w"; |
| 1851 | #endif |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1852 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1853 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1854 | return NULL; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1855 | #ifdef __VMS |
| 1856 | if (strcmp(mode,"rb") == 0) { |
| 1857 | mode = mode_r; |
| 1858 | } |
| 1859 | else { |
| 1860 | if (strcmp(mode,"wb") == 0) { |
| 1861 | mode = mode_w; |
| 1862 | } |
| 1863 | } |
| 1864 | #endif |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 1865 | #ifdef MS_WIN32 |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1866 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) || |
| 1867 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL)) |
Guido van Rossum | 30b6b2b | 1995-06-14 22:28:08 +0000 | [diff] [blame] | 1868 | #else |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1869 | 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] | 1870 | #endif |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1871 | { |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1872 | if (fd >= 0) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 1873 | SOCKETCLOSE(fd); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1874 | return s->errorhandler(); |
Guido van Rossum | 6b14491 | 1995-03-14 15:05:13 +0000 | [diff] [blame] | 1875 | } |
| 1876 | f = PyFile_FromFile(fp, "<socket>", mode, fclose); |
| 1877 | if (f != NULL) |
| 1878 | PyFile_SetBufSize(f, bufsize); |
| 1879 | return f; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1880 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1881 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1882 | PyDoc_STRVAR(makefile_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1883 | "makefile([mode[, buffersize]]) -> file object\n\ |
| 1884 | \n\ |
| 1885 | Return a regular file object corresponding to the socket.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1886 | 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] | 1887 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 1888 | #endif /* NO_DUP */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1889 | |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1890 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1891 | /* s.recv(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1892 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1893 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1894 | sock_recv(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1895 | { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1896 | int len, n = 0, flags = 0, timeout; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1897 | PyObject *buf; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1898 | #ifdef __VMS |
| 1899 | int read_length; |
| 1900 | char *read_buf; |
| 1901 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1902 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1903 | if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 1904 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1905 | |
| 1906 | if (len < 0) { |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 1907 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 1908 | "negative buffersize in recv"); |
Martin v. Löwis | fba64e1 | 2001-11-19 10:41:26 +0000 | [diff] [blame] | 1909 | return NULL; |
| 1910 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1911 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1912 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1913 | if (buf == NULL) |
| 1914 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1915 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1916 | #ifndef __VMS |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1917 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1918 | timeout = internal_select(s, 0); |
| 1919 | if (!timeout) |
| 1920 | n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1921 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1922 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1923 | if (timeout) { |
| 1924 | Py_DECREF(buf); |
| 1925 | PyErr_SetString(socket_timeout, "timed out"); |
| 1926 | return NULL; |
| 1927 | } |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1928 | if (n < 0) { |
| 1929 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 1930 | return s->errorhandler(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 1931 | } |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1932 | if (n != len) |
| 1933 | _PyString_Resize(&buf, n); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1934 | #else |
| 1935 | read_buf = PyString_AsString(buf); |
| 1936 | read_length = len; |
| 1937 | while (read_length != 0) { |
| 1938 | unsigned int segment; |
| 1939 | |
| 1940 | segment = read_length /SEGMENT_SIZE; |
| 1941 | if (segment != 0) { |
| 1942 | segment = SEGMENT_SIZE; |
| 1943 | } |
| 1944 | else { |
| 1945 | segment = read_length; |
| 1946 | } |
| 1947 | |
| 1948 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1949 | timeout = internal_select(s, 0); |
| 1950 | if (!timeout) |
| 1951 | n = recv(s->sock_fd, read_buf, segment, flags); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1952 | Py_END_ALLOW_THREADS |
| 1953 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1954 | if (timeout) { |
| 1955 | Py_DECREF(buf); |
| 1956 | PyErr_SetString(socket_timeout, "timed out"); |
| 1957 | return NULL; |
| 1958 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1959 | if (n < 0) { |
| 1960 | Py_DECREF(buf); |
| 1961 | return s->errorhandler(); |
| 1962 | } |
| 1963 | if (n != read_length) { |
| 1964 | read_buf += n; |
| 1965 | break; |
| 1966 | } |
| 1967 | |
| 1968 | read_length -= segment; |
| 1969 | read_buf += segment; |
| 1970 | } |
| 1971 | if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0) |
| 1972 | { |
| 1973 | return NULL; |
| 1974 | } |
| 1975 | #endif /* !__VMS */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1976 | return buf; |
| 1977 | } |
| 1978 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1979 | PyDoc_STRVAR(recv_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 1980 | "recv(buffersize[, flags]) -> data\n\ |
| 1981 | \n\ |
| 1982 | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
| 1983 | argument, see the Unix manual. When no data is available, block until\n\ |
| 1984 | 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] | 1985 | 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] | 1986 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1987 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 1988 | /* s.recvfrom(nbytes [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 1989 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 1990 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 1991 | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 1992 | { |
| 1993 | char addrbuf[256]; |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 1994 | PyObject *buf = NULL; |
| 1995 | PyObject *addr = NULL; |
| 1996 | PyObject *ret = NULL; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 1997 | int len, n = 0, flags = 0, timeout; |
Guido van Rossum | ff3ab42 | 2000-04-24 15:16:03 +0000 | [diff] [blame] | 1998 | socklen_t addrlen; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1999 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2000 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2001 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2002 | |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 2003 | if (!getsockaddrlen(s, &addrlen)) |
| 2004 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2005 | buf = PyString_FromStringAndSize((char *) 0, len); |
Guido van Rossum | 18c9a4f | 1993-05-25 12:16:29 +0000 | [diff] [blame] | 2006 | if (buf == NULL) |
| 2007 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2008 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2009 | Py_BEGIN_ALLOW_THREADS |
Fred Drake | fd16834 | 2001-05-09 19:11:33 +0000 | [diff] [blame] | 2010 | memset(addrbuf, 0, addrlen); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2011 | timeout = internal_select(s, 0); |
| 2012 | if (!timeout) |
| 2013 | n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags, |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 2014 | #ifndef MS_WINDOWS |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2015 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2016 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2017 | #else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2018 | (void *)addrbuf, &addrlen |
Guido van Rossum | 32c575d | 1997-12-02 20:37:32 +0000 | [diff] [blame] | 2019 | #endif |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2020 | #else |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2021 | (struct sockaddr *)addrbuf, &addrlen |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2022 | #endif |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2023 | ); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2024 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2025 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2026 | if (timeout) { |
| 2027 | Py_DECREF(buf); |
| 2028 | PyErr_SetString(socket_timeout, "timed out"); |
| 2029 | return NULL; |
| 2030 | } |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2031 | if (n < 0) { |
| 2032 | Py_DECREF(buf); |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2033 | return s->errorhandler(); |
Guido van Rossum | 7c53b77 | 1995-09-13 18:39:47 +0000 | [diff] [blame] | 2034 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2035 | |
Guido van Rossum | 53a9bf8 | 1996-06-11 18:35:24 +0000 | [diff] [blame] | 2036 | if (n != len && _PyString_Resize(&buf, n) < 0) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2037 | return NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2038 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2039 | if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2040 | addrlen))) |
Barry Warsaw | 752300b | 1997-01-03 17:18:10 +0000 | [diff] [blame] | 2041 | goto finally; |
| 2042 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2043 | ret = PyTuple_Pack(2, buf, addr); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2044 | |
| 2045 | finally: |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2046 | Py_XDECREF(addr); |
| 2047 | Py_XDECREF(buf); |
Guido van Rossum | 6f5afc9 | 1993-02-05 09:46:15 +0000 | [diff] [blame] | 2048 | return ret; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2051 | PyDoc_STRVAR(recvfrom_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2052 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2053 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2054 | 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] | 2055 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2056 | /* s.send(data [,flags]) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2057 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2058 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2059 | sock_send(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2060 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2061 | char *buf; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2062 | int len, n = 0, flags = 0, timeout; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2063 | #ifdef __VMS |
| 2064 | int send_length; |
| 2065 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2066 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2067 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2068 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2069 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2070 | #ifndef __VMS |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2071 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2072 | timeout = internal_select(s, 1); |
| 2073 | if (!timeout) |
| 2074 | n = send(s->sock_fd, buf, len, flags); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2075 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2076 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2077 | if (timeout) { |
| 2078 | PyErr_SetString(socket_timeout, "timed out"); |
| 2079 | return NULL; |
| 2080 | } |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2081 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2082 | return s->errorhandler(); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2083 | #else |
| 2084 | /* Divide packet into smaller segments for */ |
| 2085 | /* TCP/IP Services for OpenVMS */ |
| 2086 | send_length = len; |
| 2087 | while (send_length != 0) { |
| 2088 | unsigned int segment; |
| 2089 | |
| 2090 | segment = send_length / SEGMENT_SIZE; |
| 2091 | if (segment != 0) { |
| 2092 | segment = SEGMENT_SIZE; |
| 2093 | } |
| 2094 | else { |
| 2095 | segment = send_length; |
| 2096 | } |
| 2097 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2098 | timeout = internal_select(s, 1); |
| 2099 | if (!timeout) |
| 2100 | n = send(s->sock_fd, buf, segment, flags); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2101 | Py_END_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2102 | if (timeout) { |
| 2103 | PyErr_SetString(socket_timeout, "timed out"); |
| 2104 | return NULL; |
| 2105 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 2106 | if (n < 0) { |
| 2107 | return s->errorhandler(); |
| 2108 | } |
| 2109 | send_length -= segment; |
| 2110 | buf += segment; |
| 2111 | } /* end while */ |
| 2112 | #endif /* !__VMS */ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2113 | return PyInt_FromLong((long)n); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2116 | PyDoc_STRVAR(send_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2117 | "send(data[, flags]) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2118 | \n\ |
| 2119 | 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] | 2120 | 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] | 2121 | 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] | 2122 | |
| 2123 | |
| 2124 | /* s.sendall(data [,flags]) method */ |
| 2125 | |
| 2126 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2127 | sock_sendall(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2128 | { |
| 2129 | char *buf; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2130 | int len, n = 0, flags = 0, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2131 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2132 | if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags)) |
| 2133 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2134 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2135 | Py_BEGIN_ALLOW_THREADS |
| 2136 | do { |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2137 | timeout = internal_select(s, 1); |
| 2138 | if (timeout) |
| 2139 | break; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2140 | n = send(s->sock_fd, buf, len, flags); |
| 2141 | if (n < 0) |
| 2142 | break; |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2143 | buf += n; |
| 2144 | len -= n; |
| 2145 | } while (len > 0); |
| 2146 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2147 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2148 | if (timeout) { |
| 2149 | PyErr_SetString(socket_timeout, "timed out"); |
| 2150 | return NULL; |
| 2151 | } |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2152 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2153 | return s->errorhandler(); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2154 | |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2155 | Py_INCREF(Py_None); |
| 2156 | return Py_None; |
| 2157 | } |
| 2158 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2159 | PyDoc_STRVAR(sendall_doc, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2160 | "sendall(data[, flags])\n\ |
| 2161 | \n\ |
| 2162 | Send a data string to the socket. For the optional flags\n\ |
| 2163 | argument, see the Unix manual. This calls send() repeatedly\n\ |
| 2164 | 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] | 2165 | to tell how much data has been sent."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2166 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2167 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2168 | /* s.sendto(data, [flags,] sockaddr) method */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2169 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2170 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2171 | sock_sendto(PySocketSockObject *s, PyObject *args) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2172 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2173 | PyObject *addro; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2174 | char *buf; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2175 | struct sockaddr *addr; |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2176 | int addrlen, len, n = 0, flags, timeout; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2177 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2178 | flags = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2179 | if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2180 | PyErr_Clear(); |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2181 | if (!PyArg_ParseTuple(args, "s#iO:sendto", |
| 2182 | &buf, &len, &flags, &addro)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2183 | return NULL; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2184 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2185 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 2186 | if (!getsockaddrarg(s, addro, &addr, &addrlen)) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2187 | return NULL; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2188 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2189 | Py_BEGIN_ALLOW_THREADS |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2190 | timeout = internal_select(s, 1); |
| 2191 | if (!timeout) |
| 2192 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2193 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2194 | |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 2195 | if (timeout) { |
| 2196 | PyErr_SetString(socket_timeout, "timed out"); |
| 2197 | return NULL; |
| 2198 | } |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2199 | if (n < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2200 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2201 | return PyInt_FromLong((long)n); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2204 | PyDoc_STRVAR(sendto_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 2205 | "sendto(data[, flags], address) -> count\n\ |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2206 | \n\ |
| 2207 | 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] | 2208 | For IP sockets, the address is a pair (hostaddr, port)."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2210 | |
| 2211 | /* s.shutdown(how) method */ |
| 2212 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2213 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2214 | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2215 | { |
| 2216 | int how; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2217 | int res; |
Jeremy Hylton | ae0013d | 2001-10-11 16:36:35 +0000 | [diff] [blame] | 2218 | |
| 2219 | how = PyInt_AsLong(arg); |
| 2220 | if (how == -1 && PyErr_Occurred()) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2221 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2222 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2223 | res = shutdown(s->sock_fd, how); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2224 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2225 | if (res < 0) |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 2226 | return s->errorhandler(); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2227 | Py_INCREF(Py_None); |
| 2228 | return Py_None; |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2231 | PyDoc_STRVAR(shutdown_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2232 | "shutdown(flag)\n\ |
| 2233 | \n\ |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 2234 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
| 2235 | 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] | 2236 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2237 | |
| 2238 | /* List of methods for socket objects */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2239 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2240 | static PyMethodDef sock_methods[] = { |
| 2241 | {"accept", (PyCFunction)sock_accept, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2242 | accept_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2243 | {"bind", (PyCFunction)sock_bind, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2244 | bind_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2245 | {"close", (PyCFunction)sock_close, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2246 | close_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2247 | {"connect", (PyCFunction)sock_connect, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2248 | connect_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2249 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2250 | connect_ex_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2251 | #ifndef NO_DUP |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2252 | {"dup", (PyCFunction)sock_dup, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2253 | dup_doc}, |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2254 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2255 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2256 | fileno_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2257 | #ifdef HAVE_GETPEERNAME |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2258 | {"getpeername", (PyCFunction)sock_getpeername, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2259 | METH_NOARGS, getpeername_doc}, |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 2260 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2261 | {"getsockname", (PyCFunction)sock_getsockname, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2262 | METH_NOARGS, getsockname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2263 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2264 | getsockopt_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2265 | {"listen", (PyCFunction)sock_listen, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2266 | listen_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2267 | #ifndef NO_DUP |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2268 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2269 | makefile_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2270 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2271 | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2272 | recv_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2273 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2274 | recvfrom_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2275 | {"send", (PyCFunction)sock_send, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2276 | send_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2277 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
Guido van Rossum | 9f7a539 | 2001-10-26 03:25:00 +0000 | [diff] [blame] | 2278 | sendall_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2279 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2280 | sendto_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2281 | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2282 | setblocking_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2283 | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2284 | settimeout_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2285 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2286 | gettimeout_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2287 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2288 | setsockopt_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2289 | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2290 | shutdown_doc}, |
| 2291 | #ifdef RISCOS |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 2292 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2293 | sleeptaskw_doc}, |
| 2294 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2295 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2296 | }; |
| 2297 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2298 | |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2299 | /* Deallocate a socket object in response to the last Py_DECREF(). |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2300 | First close the file description. */ |
| 2301 | |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2302 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2303 | sock_dealloc(PySocketSockObject *s) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2304 | { |
Guido van Rossum | fa972c9 | 2000-04-10 12:45:45 +0000 | [diff] [blame] | 2305 | if (s->sock_fd != -1) |
Guido van Rossum | 2dd8ddd | 2000-04-21 20:33:00 +0000 | [diff] [blame] | 2306 | (void) SOCKETCLOSE(s->sock_fd); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2307 | s->ob_type->tp_free((PyObject *)s); |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2310 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2311 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2312 | sock_repr(PySocketSockObject *s) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2313 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2314 | char buf[512]; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2315 | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
| 2316 | if (s->sock_fd > LONG_MAX) { |
| 2317 | /* this can occur on Win64, and actually there is a special |
| 2318 | ugly printf formatter for decimal pointer length integer |
| 2319 | printing, only bother if necessary*/ |
| 2320 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2321 | "no printf formatter to display " |
| 2322 | "the socket descriptor in decimal"); |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2323 | return NULL; |
| 2324 | } |
| 2325 | #endif |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2326 | PyOS_snprintf( |
| 2327 | buf, sizeof(buf), |
| 2328 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>", |
| 2329 | (long)s->sock_fd, s->sock_family, |
| 2330 | s->sock_type, |
| 2331 | s->sock_proto); |
Guido van Rossum | dc1c64a | 1996-02-27 15:21:19 +0000 | [diff] [blame] | 2332 | return PyString_FromString(buf); |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2336 | /* Create a new, uninitialized socket object. */ |
| 2337 | |
| 2338 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2339 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2340 | { |
| 2341 | PyObject *new; |
| 2342 | |
| 2343 | new = type->tp_alloc(type, 0); |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2344 | if (new != NULL) { |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2345 | ((PySocketSockObject *)new)->sock_fd = -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2346 | ((PySocketSockObject *)new)->sock_timeout = -1.0; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2347 | ((PySocketSockObject *)new)->errorhandler = &set_error; |
Guido van Rossum | e1c478f | 2002-06-06 20:08:25 +0000 | [diff] [blame] | 2348 | } |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2349 | return new; |
| 2350 | } |
| 2351 | |
| 2352 | |
| 2353 | /* Initialize a new socket object. */ |
| 2354 | |
| 2355 | /*ARGSUSED*/ |
| 2356 | static int |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2357 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2358 | { |
| 2359 | PySocketSockObject *s = (PySocketSockObject *)self; |
| 2360 | SOCKET_T fd; |
| 2361 | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 2362 | static char *keywords[] = {"family", "type", "proto", 0}; |
| 2363 | |
| 2364 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2365 | "|iii:socket", keywords, |
| 2366 | &family, &type, &proto)) |
| 2367 | return -1; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2368 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2369 | Py_BEGIN_ALLOW_THREADS |
| 2370 | fd = socket(family, type, proto); |
| 2371 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2372 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2373 | #ifdef MS_WINDOWS |
| 2374 | if (fd == INVALID_SOCKET) |
| 2375 | #else |
| 2376 | if (fd < 0) |
| 2377 | #endif |
| 2378 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2379 | set_error(); |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2380 | return -1; |
| 2381 | } |
| 2382 | init_sockobject(s, fd, family, type, proto); |
| 2383 | /* From now on, ignore SIGPIPE and let the error checking |
| 2384 | do the work. */ |
| 2385 | #ifdef SIGPIPE |
| 2386 | (void) signal(SIGPIPE, SIG_IGN); |
| 2387 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2388 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2389 | return 0; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2390 | |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2394 | /* Type object for socket objects. */ |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2395 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2396 | static PyTypeObject sock_type = { |
Guido van Rossum | 5555854 | 1996-05-23 22:54:50 +0000 | [diff] [blame] | 2397 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2398 | 0, /* ob_size */ |
Guido van Rossum | ca6dfa5 | 2001-10-28 12:31:33 +0000 | [diff] [blame] | 2399 | "_socket.socket", /* tp_name */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2400 | sizeof(PySocketSockObject), /* tp_basicsize */ |
| 2401 | 0, /* tp_itemsize */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2402 | (destructor)sock_dealloc, /* tp_dealloc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2403 | 0, /* tp_print */ |
| 2404 | 0, /* tp_getattr */ |
| 2405 | 0, /* tp_setattr */ |
| 2406 | 0, /* tp_compare */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2407 | (reprfunc)sock_repr, /* tp_repr */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2408 | 0, /* tp_as_number */ |
| 2409 | 0, /* tp_as_sequence */ |
| 2410 | 0, /* tp_as_mapping */ |
| 2411 | 0, /* tp_hash */ |
| 2412 | 0, /* tp_call */ |
| 2413 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2414 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2415 | 0, /* tp_setattro */ |
| 2416 | 0, /* tp_as_buffer */ |
| 2417 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2418 | sock_doc, /* tp_doc */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2419 | 0, /* tp_traverse */ |
| 2420 | 0, /* tp_clear */ |
| 2421 | 0, /* tp_richcompare */ |
| 2422 | 0, /* tp_weaklistoffset */ |
| 2423 | 0, /* tp_iter */ |
| 2424 | 0, /* tp_iternext */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2425 | sock_methods, /* tp_methods */ |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 2426 | 0, /* tp_members */ |
| 2427 | 0, /* tp_getset */ |
| 2428 | 0, /* tp_base */ |
| 2429 | 0, /* tp_dict */ |
| 2430 | 0, /* tp_descr_get */ |
| 2431 | 0, /* tp_descr_set */ |
| 2432 | 0, /* tp_dictoffset */ |
Andrew MacIntyre | 7aec4a2 | 2002-06-13 11:53:52 +0000 | [diff] [blame] | 2433 | sock_initobj, /* tp_init */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2434 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2435 | sock_new, /* tp_new */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2436 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 2437 | }; |
| 2438 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2439 | |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2440 | /* Python interface to gethostname(). */ |
| 2441 | |
| 2442 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2443 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2444 | socket_gethostname(PyObject *self, PyObject *args) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2445 | { |
| 2446 | char buf[1024]; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2447 | int res; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2448 | if (!PyArg_ParseTuple(args, ":gethostname")) |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2449 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2450 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2451 | res = gethostname(buf, (int) sizeof buf - 1); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2452 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2453 | if (res < 0) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2454 | return set_error(); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2455 | buf[sizeof buf - 1] = '\0'; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2456 | return PyString_FromString(buf); |
Guido van Rossum | 8119447 | 1991-07-27 21:42:02 +0000 | [diff] [blame] | 2457 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2459 | PyDoc_STRVAR(gethostname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2460 | "gethostname() -> string\n\ |
| 2461 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2462 | Return the current host name."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2463 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2464 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2465 | /* Python interface to gethostbyname(name). */ |
| 2466 | |
| 2467 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2468 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2469 | socket_gethostbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2470 | { |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2471 | char *name; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 2472 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2473 | struct sockaddr_storage addrbuf; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 2474 | #else |
| 2475 | struct sockaddr_in addrbuf; |
| 2476 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2477 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2478 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2479 | return NULL; |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 2480 | if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2481 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2482 | return makeipaddr((struct sockaddr *)&addrbuf, |
| 2483 | sizeof(struct sockaddr_in)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2486 | PyDoc_STRVAR(gethostbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2487 | "gethostbyname(host) -> address\n\ |
| 2488 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2489 | 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] | 2490 | |
| 2491 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2492 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 2493 | |
| 2494 | static PyObject * |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2495 | 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] | 2496 | { |
| 2497 | char **pch; |
| 2498 | PyObject *rtn_tuple = (PyObject *)NULL; |
| 2499 | PyObject *name_list = (PyObject *)NULL; |
| 2500 | PyObject *addr_list = (PyObject *)NULL; |
| 2501 | PyObject *tmp; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2502 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2503 | if (h == NULL) { |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2504 | /* Let's get real error message to return */ |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 2505 | #ifndef RISCOS |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2506 | set_herror(h_errno); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 2507 | #else |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2508 | PyErr_SetString(socket_error, "host not found"); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 2509 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2510 | return NULL; |
| 2511 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2512 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2513 | if (h->h_addrtype != af) { |
| 2514 | #ifdef HAVE_STRERROR |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2515 | /* Let's get real error message to return */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2516 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2517 | (char *)strerror(EAFNOSUPPORT)); |
| 2518 | #else |
| 2519 | PyErr_SetString( |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2520 | socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2521 | "Address family not supported by protocol family"); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2522 | #endif |
| 2523 | return NULL; |
| 2524 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2525 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2526 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2527 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2528 | case AF_INET: |
| 2529 | if (alen < sizeof(struct sockaddr_in)) |
| 2530 | return NULL; |
| 2531 | break; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2532 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2533 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2534 | case AF_INET6: |
| 2535 | if (alen < sizeof(struct sockaddr_in6)) |
| 2536 | return NULL; |
| 2537 | break; |
| 2538 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2539 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2540 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2541 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2542 | if ((name_list = PyList_New(0)) == NULL) |
| 2543 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2544 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2545 | if ((addr_list = PyList_New(0)) == NULL) |
| 2546 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2547 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2548 | for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 2549 | int status; |
| 2550 | tmp = PyString_FromString(*pch); |
| 2551 | if (tmp == NULL) |
| 2552 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2553 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2554 | status = PyList_Append(name_list, tmp); |
| 2555 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2556 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2557 | if (status) |
| 2558 | goto err; |
| 2559 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2560 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2561 | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 2562 | int status; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2563 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2564 | switch (af) { |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2565 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2566 | case AF_INET: |
| 2567 | { |
| 2568 | struct sockaddr_in sin; |
| 2569 | memset(&sin, 0, sizeof(sin)); |
| 2570 | sin.sin_family = af; |
| 2571 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2572 | sin.sin_len = sizeof(sin); |
| 2573 | #endif |
| 2574 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 2575 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2576 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2577 | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 2578 | memcpy((char *) addr, &sin, sizeof(sin)); |
| 2579 | break; |
| 2580 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2581 | |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2582 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2583 | case AF_INET6: |
| 2584 | { |
| 2585 | struct sockaddr_in6 sin6; |
| 2586 | memset(&sin6, 0, sizeof(sin6)); |
| 2587 | sin6.sin6_family = af; |
| 2588 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 2589 | sin6.sin6_len = sizeof(sin6); |
| 2590 | #endif |
| 2591 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 2592 | tmp = makeipaddr((struct sockaddr *)&sin6, |
| 2593 | sizeof(sin6)); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2594 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2595 | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 2596 | memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 2597 | break; |
| 2598 | } |
| 2599 | #endif |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2600 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2601 | default: /* can't happen */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2602 | PyErr_SetString(socket_error, |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2603 | "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2604 | return NULL; |
| 2605 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2606 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2607 | if (tmp == NULL) |
| 2608 | goto err; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2609 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2610 | status = PyList_Append(addr_list, tmp); |
| 2611 | Py_DECREF(tmp); |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2612 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2613 | if (status) |
| 2614 | goto err; |
| 2615 | } |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2616 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2617 | 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] | 2618 | |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2619 | err: |
| 2620 | Py_XDECREF(name_list); |
| 2621 | Py_XDECREF(addr_list); |
| 2622 | return rtn_tuple; |
| 2623 | } |
| 2624 | |
| 2625 | |
| 2626 | /* Python interface to gethostbyname_ex(name). */ |
| 2627 | |
| 2628 | /*ARGSUSED*/ |
| 2629 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2630 | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2631 | { |
| 2632 | char *name; |
| 2633 | struct hostent *h; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 2634 | #ifdef ENABLE_IPV6 |
| 2635 | struct sockaddr_storage addr; |
| 2636 | #else |
| 2637 | struct sockaddr_in addr; |
| 2638 | #endif |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 2639 | struct sockaddr *sa; |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 2640 | PyObject *ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2641 | #ifdef HAVE_GETHOSTBYNAME_R |
| 2642 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2643 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 2644 | struct hostent_data data; |
| 2645 | #else |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2646 | char buf[16384]; |
| 2647 | int buf_len = (sizeof buf) - 1; |
| 2648 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2649 | #endif |
| 2650 | #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] | 2651 | int result; |
| 2652 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2653 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2654 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2655 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2656 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 2657 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2658 | return NULL; |
| 2659 | Py_BEGIN_ALLOW_THREADS |
| 2660 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2661 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2662 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 2663 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2664 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2665 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2666 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 2667 | memset((void *) &data, '\0', sizeof(data)); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2668 | result = gethostbyname_r(name, &hp_allocated, &data); |
| 2669 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2670 | #endif |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2671 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2672 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2673 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2674 | #endif |
| 2675 | h = gethostbyname(name); |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2676 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 2677 | Py_END_ALLOW_THREADS |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2678 | /* Some C libraries would require addr.__ss_family instead of |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2679 | addr.ss_family. |
| 2680 | Therefore, we cast the sockaddr_storage into sockaddr to |
| 2681 | access sa_family. */ |
Martin v. Löwis | 9db2f57 | 2001-07-23 01:30:10 +0000 | [diff] [blame] | 2682 | sa = (struct sockaddr*)&addr; |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2683 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2684 | sa->sa_family); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2685 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2686 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 955becc | 1999-03-22 20:14:53 +0000 | [diff] [blame] | 2687 | #endif |
| 2688 | return ret; |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2691 | PyDoc_STRVAR(ghbn_ex_doc, |
Guido van Rossum | 7d896ab | 1998-08-04 22:16:43 +0000 | [diff] [blame] | 2692 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 2693 | \n\ |
| 2694 | 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] | 2695 | 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] | 2696 | |
| 2697 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2698 | /* Python interface to gethostbyaddr(IP). */ |
| 2699 | |
| 2700 | /*ARGSUSED*/ |
| 2701 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2702 | socket_gethostbyaddr(PyObject *self, PyObject *args) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2703 | { |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2704 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 2705 | struct sockaddr_storage addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2706 | #else |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2707 | struct sockaddr_in addr; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2708 | #endif |
| 2709 | struct sockaddr *sa = (struct sockaddr *)&addr; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2710 | char *ip_num; |
| 2711 | struct hostent *h; |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2712 | PyObject *ret; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2713 | #ifdef HAVE_GETHOSTBYNAME_R |
| 2714 | struct hostent hp_allocated; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2715 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 2716 | struct hostent_data data; |
| 2717 | #else |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2718 | char buf[16384]; |
| 2719 | int buf_len = (sizeof buf) - 1; |
| 2720 | int errnop; |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2721 | #endif |
| 2722 | #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] | 2723 | int result; |
| 2724 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2725 | #endif /* HAVE_GETHOSTBYNAME_R */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2726 | char *ap; |
| 2727 | int al; |
| 2728 | int af; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2729 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2730 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2731 | return NULL; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 2732 | af = AF_UNSPEC; |
Martin v. Löwis | ddc6f47 | 2002-07-28 16:10:31 +0000 | [diff] [blame] | 2733 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2734 | return NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2735 | af = sa->sa_family; |
| 2736 | ap = NULL; |
| 2737 | al = 0; |
| 2738 | switch (af) { |
| 2739 | case AF_INET: |
| 2740 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 2741 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 2742 | break; |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 2743 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2744 | case AF_INET6: |
| 2745 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 2746 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 2747 | break; |
| 2748 | #endif |
| 2749 | default: |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2750 | PyErr_SetString(socket_error, "unsupported address family"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2751 | return NULL; |
| 2752 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2753 | Py_BEGIN_ALLOW_THREADS |
| 2754 | #ifdef HAVE_GETHOSTBYNAME_R |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2755 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2756 | result = gethostbyaddr_r(ap, al, af, |
| 2757 | &hp_allocated, buf, buf_len, |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2758 | &h, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2759 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2760 | h = gethostbyaddr_r(ap, al, af, |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2761 | &hp_allocated, buf, buf_len, &errnop); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2762 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
Guido van Rossum | e7de206 | 1999-03-24 17:24:33 +0000 | [diff] [blame] | 2763 | memset((void *) &data, '\0', sizeof(data)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2764 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
Guido van Rossum | 7b6c71f | 1999-03-24 17:20:40 +0000 | [diff] [blame] | 2765 | h = (result != 0) ? NULL : &hp_allocated; |
Guido van Rossum | e9cd07b | 1999-03-15 21:40:14 +0000 | [diff] [blame] | 2766 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2767 | #else /* not HAVE_GETHOSTBYNAME_R */ |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2768 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2769 | PyThread_acquire_lock(netdb_lock, 1); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2770 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2771 | h = gethostbyaddr(ap, al, af); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 2772 | #endif /* HAVE_GETHOSTBYNAME_R */ |
| 2773 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 2774 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2775 | #ifdef USE_GETHOSTBYNAME_LOCK |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 2776 | PyThread_release_lock(netdb_lock); |
Guido van Rossum | 3baaa13 | 1999-03-22 21:44:51 +0000 | [diff] [blame] | 2777 | #endif |
| 2778 | return ret; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2781 | PyDoc_STRVAR(gethostbyaddr_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2782 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 2783 | \n\ |
| 2784 | 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] | 2785 | 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] | 2786 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2787 | |
| 2788 | /* Python interface to getservbyname(name). |
| 2789 | This only returns the port number, since the other info is already |
| 2790 | known or not useful (like the list of aliases). */ |
| 2791 | |
| 2792 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2793 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2794 | socket_getservbyname(PyObject *self, PyObject *args) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2795 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2796 | char *name, *proto; |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2797 | struct servent *sp; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2798 | if (!PyArg_ParseTuple(args, "ss:getservbyname", &name, &proto)) |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2799 | return NULL; |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2800 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 2801 | sp = getservbyname(name, proto); |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2802 | Py_END_ALLOW_THREADS |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2803 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2804 | PyErr_SetString(socket_error, "service/proto not found"); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2805 | return NULL; |
| 2806 | } |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2807 | return PyInt_FromLong((long) ntohs(sp->s_port)); |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2810 | PyDoc_STRVAR(getservbyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2811 | "getservbyname(servicename, protocolname) -> integer\n\ |
| 2812 | \n\ |
| 2813 | Return a port number from a service name and protocol name.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2814 | The protocol name should be 'tcp' or 'udp'."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2815 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 2816 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2817 | /* Python interface to getprotobyname(name). |
| 2818 | This only returns the protocol number, since the other info is |
| 2819 | already known or not useful (like the list of aliases). */ |
| 2820 | |
| 2821 | /*ARGSUSED*/ |
| 2822 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2823 | socket_getprotobyname(PyObject *self, PyObject *args) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2824 | { |
| 2825 | char *name; |
| 2826 | struct protoent *sp; |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2827 | #ifdef __BEOS__ |
| 2828 | /* Not available in BeOS yet. - [cjh] */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2829 | PyErr_SetString(socket_error, "getprotobyname not supported"); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2830 | return NULL; |
| 2831 | #else |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2832 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2833 | return NULL; |
| 2834 | Py_BEGIN_ALLOW_THREADS |
| 2835 | sp = getprotobyname(name); |
| 2836 | Py_END_ALLOW_THREADS |
| 2837 | if (sp == NULL) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2838 | PyErr_SetString(socket_error, "protocol not found"); |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2839 | return NULL; |
| 2840 | } |
| 2841 | return PyInt_FromLong((long) sp->p_proto); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 2842 | #endif |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2845 | PyDoc_STRVAR(getprotobyname_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2846 | "getprotobyname(name) -> integer\n\ |
| 2847 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2848 | Return the protocol number for the named protocol. (Rarely used.)"); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2849 | |
Guido van Rossum | 3901d85 | 1996-12-19 16:35:04 +0000 | [diff] [blame] | 2850 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2851 | #ifndef NO_DUP |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2852 | /* Create a socket object from a numeric file description. |
| 2853 | Useful e.g. if stdin is a socket. |
| 2854 | Additional arguments as for socket(). */ |
| 2855 | |
| 2856 | /*ARGSUSED*/ |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2857 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2858 | socket_fromfd(PyObject *self, PyObject *args) |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2859 | { |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2860 | PySocketSockObject *s; |
Fred Drake | a04eaad | 2000-06-30 02:46:07 +0000 | [diff] [blame] | 2861 | SOCKET_T fd; |
| 2862 | int family, type, proto = 0; |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2863 | if (!PyArg_ParseTuple(args, "iii|i:fromfd", |
| 2864 | &fd, &family, &type, &proto)) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 2865 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 2866 | /* Dup the fd so it and the socket can be closed independently */ |
| 2867 | fd = dup(fd); |
| 2868 | if (fd < 0) |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2869 | return set_error(); |
| 2870 | s = new_sockobject(fd, family, type, proto); |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2871 | /* From now on, ignore SIGPIPE and let the error checking |
| 2872 | do the work. */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2873 | #ifdef SIGPIPE |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2874 | (void) signal(SIGPIPE, SIG_IGN); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 2875 | #endif |
Guido van Rossum | 73624e9 | 1994-10-10 17:59:00 +0000 | [diff] [blame] | 2876 | return (PyObject *) s; |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2877 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2879 | PyDoc_STRVAR(fromfd_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2880 | "fromfd(fd, family, type[, proto]) -> socket object\n\ |
| 2881 | \n\ |
| 2882 | Create a socket object from the given file descriptor.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2883 | The remaining arguments are the same as for socket()."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2884 | |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 2885 | #endif /* NO_DUP */ |
Guido van Rossum | 2a7178e | 1992-12-08 13:38:24 +0000 | [diff] [blame] | 2886 | |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2887 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2888 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2889 | socket_ntohs(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2890 | { |
| 2891 | int x1, x2; |
| 2892 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2893 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2894 | return NULL; |
| 2895 | } |
| 2896 | x2 = (int)ntohs((short)x1); |
| 2897 | return PyInt_FromLong(x2); |
| 2898 | } |
| 2899 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2900 | PyDoc_STRVAR(ntohs_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2901 | "ntohs(integer) -> integer\n\ |
| 2902 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2903 | Convert a 16-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2904 | |
| 2905 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2906 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2907 | socket_ntohl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2908 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2909 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2910 | |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2911 | if (PyInt_Check(arg)) { |
| 2912 | x = PyInt_AS_LONG(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 2913 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 2914 | return NULL; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2915 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2916 | else if (PyLong_Check(arg)) { |
| 2917 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 2918 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 2919 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2920 | #if SIZEOF_LONG > 4 |
| 2921 | { |
| 2922 | unsigned long y; |
| 2923 | /* only want the trailing 32 bits */ |
| 2924 | y = x & 0xFFFFFFFFUL; |
| 2925 | if (y ^ x) |
| 2926 | return PyErr_Format(PyExc_OverflowError, |
| 2927 | "long int larger than 32 bits"); |
| 2928 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 2929 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2930 | #endif |
| 2931 | } |
| 2932 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 2933 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2934 | "expected int/long, %s found", |
| 2935 | arg->ob_type->tp_name); |
| 2936 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 2937 | return NULL; |
| 2938 | return PyInt_FromLong(ntohl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2941 | PyDoc_STRVAR(ntohl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2942 | "ntohl(integer) -> integer\n\ |
| 2943 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2944 | Convert a 32-bit integer from network to host byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2945 | |
| 2946 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2947 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 2948 | socket_htons(PyObject *self, PyObject *args) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2949 | { |
Hye-Shik Chang | a7b673f | 2003-12-17 09:40:23 +0000 | [diff] [blame] | 2950 | int x1, x2; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2951 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 2952 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2953 | return NULL; |
| 2954 | } |
| 2955 | x2 = (int)htons((short)x1); |
| 2956 | return PyInt_FromLong(x2); |
| 2957 | } |
| 2958 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2959 | PyDoc_STRVAR(htons_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2960 | "htons(integer) -> integer\n\ |
| 2961 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2962 | Convert a 16-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2963 | |
| 2964 | |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2965 | static PyObject * |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2966 | socket_htonl(PyObject *self, PyObject *arg) |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2967 | { |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2968 | unsigned long x; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2969 | |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2970 | if (PyInt_Check(arg)) { |
| 2971 | x = PyInt_AS_LONG(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 2972 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 2973 | return NULL; |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2974 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2975 | else if (PyLong_Check(arg)) { |
| 2976 | x = PyLong_AsUnsignedLong(arg); |
Jeremy Hylton | 825e47b | 2002-07-25 16:37:51 +0000 | [diff] [blame] | 2977 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 2978 | return NULL; |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2979 | #if SIZEOF_LONG > 4 |
| 2980 | { |
| 2981 | unsigned long y; |
| 2982 | /* only want the trailing 32 bits */ |
| 2983 | y = x & 0xFFFFFFFFUL; |
| 2984 | if (y ^ x) |
| 2985 | return PyErr_Format(PyExc_OverflowError, |
| 2986 | "long int larger than 32 bits"); |
| 2987 | x = y; |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 2988 | } |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2989 | #endif |
| 2990 | } |
| 2991 | else |
Tim Peters | 5814187 | 2002-08-06 22:25:02 +0000 | [diff] [blame] | 2992 | return PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2993 | "expected int/long, %s found", |
| 2994 | arg->ob_type->tp_name); |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 2995 | return PyInt_FromLong(htonl(x)); |
Guido van Rossum | 006bf91 | 1996-06-12 04:04:55 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2998 | PyDoc_STRVAR(htonl_doc, |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 2999 | "htonl(integer) -> integer\n\ |
| 3000 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3001 | Convert a 32-bit integer from host to network byte order."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3002 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3003 | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3004 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3005 | PyDoc_STRVAR(inet_aton_doc, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3006 | "inet_aton(string) -> packed 32-bit IP representation\n\ |
| 3007 | \n\ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3008 | 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] | 3009 | binary format used in low-level network functions."); |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3010 | |
| 3011 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3012 | socket_inet_aton(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3013 | { |
Guido van Rossum | a2e4855 | 1999-09-09 15:42:59 +0000 | [diff] [blame] | 3014 | #ifndef INADDR_NONE |
| 3015 | #define INADDR_NONE (-1) |
| 3016 | #endif |
Neal Norwitz | 88f115b | 2003-02-13 02:15:42 +0000 | [diff] [blame] | 3017 | #ifdef HAVE_INET_ATON |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3018 | struct in_addr buf; |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3019 | #else |
| 3020 | /* Have to use inet_addr() instead */ |
| 3021 | unsigned long packed_addr; |
| 3022 | #endif |
| 3023 | char *ip_addr; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3024 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3025 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3026 | return NULL; |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3027 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3028 | |
| 3029 | #ifdef HAVE_INET_ATON |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3030 | if (inet_aton(ip_addr, &buf)) |
| 3031 | return PyString_FromStringAndSize((char *)(&buf), |
| 3032 | sizeof(buf)); |
| 3033 | |
| 3034 | PyErr_SetString(socket_error, |
| 3035 | "illegal IP address string passed to inet_aton"); |
| 3036 | return NULL; |
| 3037 | |
Tim Peters | 1df9fdd | 2003-02-13 03:13:40 +0000 | [diff] [blame] | 3038 | #else /* ! HAVE_INET_ATON */ |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3039 | /* XXX Problem here: inet_aton('255.255.255.255') raises |
| 3040 | an exception while it should be a valid address. */ |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3041 | packed_addr = inet_addr(ip_addr); |
| 3042 | |
| 3043 | if (packed_addr == INADDR_NONE) { /* invalid address */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3044 | PyErr_SetString(socket_error, |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3045 | "illegal IP address string passed to inet_aton"); |
| 3046 | return NULL; |
| 3047 | } |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3048 | return PyString_FromStringAndSize((char *) &packed_addr, |
| 3049 | sizeof(packed_addr)); |
Guido van Rossum | ad05cdf | 2003-02-12 23:08:22 +0000 | [diff] [blame] | 3050 | #endif |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3053 | PyDoc_STRVAR(inet_ntoa_doc, |
Fred Drake | e066134 | 2000-03-07 14:05:16 +0000 | [diff] [blame] | 3054 | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3055 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3056 | 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] | 3057 | |
| 3058 | static PyObject* |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3059 | socket_inet_ntoa(PyObject *self, PyObject *args) |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3060 | { |
| 3061 | char *packed_str; |
Guido van Rossum | c6a164b | 1999-08-20 19:11:27 +0000 | [diff] [blame] | 3062 | int addr_len; |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3063 | struct in_addr packed_addr; |
| 3064 | |
Guido van Rossum | 27fc3c0 | 2000-03-24 20:56:56 +0000 | [diff] [blame] | 3065 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3066 | return NULL; |
| 3067 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3068 | |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3069 | if (addr_len != sizeof(packed_addr)) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3070 | PyErr_SetString(socket_error, |
Guido van Rossum | 5c9eb21 | 1999-08-20 18:21:51 +0000 | [diff] [blame] | 3071 | "packed IP wrong length for inet_ntoa"); |
| 3072 | return NULL; |
| 3073 | } |
| 3074 | |
| 3075 | memcpy(&packed_addr, packed_str, addr_len); |
| 3076 | |
| 3077 | return PyString_FromString(inet_ntoa(packed_addr)); |
| 3078 | } |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3079 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3080 | #ifdef HAVE_INET_PTON |
| 3081 | |
| 3082 | PyDoc_STRVAR(inet_pton_doc, |
| 3083 | "inet_pton(af, ip) -> packed IP address string\n\ |
| 3084 | \n\ |
| 3085 | Convert an IP address from string format to a packed string suitable\n\ |
| 3086 | for use with low-level network functions."); |
| 3087 | |
| 3088 | static PyObject * |
| 3089 | socket_inet_pton(PyObject *self, PyObject *args) |
| 3090 | { |
| 3091 | int af; |
| 3092 | char* ip; |
| 3093 | int retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3094 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3095 | 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] | 3096 | #else |
| 3097 | char packed[sizeof(struct in_addr)]; |
| 3098 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3099 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3100 | return NULL; |
| 3101 | } |
| 3102 | |
Martin v. Löwis | 1064909 | 2003-08-05 06:25:06 +0000 | [diff] [blame] | 3103 | #ifndef ENABLE_IPV6 |
| 3104 | if(af == AF_INET6) { |
| 3105 | PyErr_SetString(socket_error, |
| 3106 | "can't use AF_INET6, IPv6 is disabled"); |
| 3107 | return NULL; |
| 3108 | } |
| 3109 | #endif |
| 3110 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3111 | retval = inet_pton(af, ip, packed); |
| 3112 | if (retval < 0) { |
| 3113 | PyErr_SetFromErrno(socket_error); |
| 3114 | return NULL; |
| 3115 | } else if (retval == 0) { |
| 3116 | PyErr_SetString(socket_error, |
| 3117 | "illegal IP address string passed to inet_pton"); |
| 3118 | return NULL; |
| 3119 | } else if (af == AF_INET) { |
| 3120 | return PyString_FromStringAndSize(packed, |
| 3121 | sizeof(struct in_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3122 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3123 | } else if (af == AF_INET6) { |
| 3124 | return PyString_FromStringAndSize(packed, |
| 3125 | sizeof(struct in6_addr)); |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3126 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3127 | } else { |
| 3128 | PyErr_SetString(socket_error, "unknown address family"); |
| 3129 | return NULL; |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | PyDoc_STRVAR(inet_ntop_doc, |
| 3134 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3135 | \n\ |
| 3136 | Convert a packed IP address of the given family to string format."); |
| 3137 | |
| 3138 | static PyObject * |
| 3139 | socket_inet_ntop(PyObject *self, PyObject *args) |
| 3140 | { |
| 3141 | int af; |
| 3142 | char* packed; |
| 3143 | int len; |
| 3144 | const char* retval; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3145 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3146 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3147 | #else |
| 3148 | char ip[INET_ADDRSTRLEN + 1]; |
| 3149 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3150 | |
| 3151 | /* Guarantee NUL-termination for PyString_FromString() below */ |
| 3152 | memset((void *) &ip[0], '\0', sizeof(ip) + 1); |
| 3153 | |
| 3154 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { |
| 3155 | return NULL; |
| 3156 | } |
| 3157 | |
| 3158 | if (af == AF_INET) { |
| 3159 | if (len != sizeof(struct in_addr)) { |
| 3160 | PyErr_SetString(PyExc_ValueError, |
| 3161 | "invalid length of packed IP address string"); |
| 3162 | return NULL; |
| 3163 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3164 | #ifdef ENABLE_IPV6 |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3165 | } else if (af == AF_INET6) { |
| 3166 | if (len != sizeof(struct in6_addr)) { |
| 3167 | PyErr_SetString(PyExc_ValueError, |
| 3168 | "invalid length of packed IP address string"); |
| 3169 | return NULL; |
| 3170 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3171 | #endif |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3172 | } else { |
| 3173 | PyErr_Format(PyExc_ValueError, |
| 3174 | "unknown address family %d", af); |
| 3175 | return NULL; |
| 3176 | } |
| 3177 | |
| 3178 | retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 3179 | if (!retval) { |
| 3180 | PyErr_SetFromErrno(socket_error); |
| 3181 | return NULL; |
| 3182 | } else { |
| 3183 | return PyString_FromString(retval); |
| 3184 | } |
| 3185 | |
| 3186 | /* NOTREACHED */ |
| 3187 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 3188 | return NULL; |
| 3189 | } |
| 3190 | |
| 3191 | #endif /* HAVE_INET_PTON */ |
| 3192 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3193 | /* Python interface to getaddrinfo(host, port). */ |
| 3194 | |
| 3195 | /*ARGSUSED*/ |
| 3196 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3197 | socket_getaddrinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3198 | { |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3199 | struct addrinfo hints, *res; |
| 3200 | struct addrinfo *res0 = NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3201 | PyObject *hobj = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3202 | PyObject *pobj = (PyObject *)NULL; |
Martin v. Löwis | f65b1a1 | 2001-08-12 09:28:40 +0000 | [diff] [blame] | 3203 | char pbuf[30]; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3204 | char *hptr, *pptr; |
| 3205 | int family, socktype, protocol, flags; |
| 3206 | int error; |
| 3207 | PyObject *all = (PyObject *)NULL; |
| 3208 | PyObject *single = (PyObject *)NULL; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3209 | PyObject *idna = NULL; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3210 | |
| 3211 | family = socktype = protocol = flags = 0; |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3212 | family = AF_UNSPEC; |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3213 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 3214 | &hobj, &pobj, &family, &socktype, |
| 3215 | &protocol, &flags)) { |
| 3216 | return NULL; |
| 3217 | } |
| 3218 | if (hobj == Py_None) { |
| 3219 | hptr = NULL; |
| 3220 | } else if (PyUnicode_Check(hobj)) { |
| 3221 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 3222 | if (!idna) |
| 3223 | return NULL; |
| 3224 | hptr = PyString_AsString(idna); |
| 3225 | } else if (PyString_Check(hobj)) { |
| 3226 | hptr = PyString_AsString(hobj); |
| 3227 | } else { |
| 3228 | PyErr_SetString(PyExc_TypeError, |
| 3229 | "getaddrinfo() argument 1 must be string or None"); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3230 | return NULL; |
| 3231 | } |
| 3232 | if (PyInt_Check(pobj)) { |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3233 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3234 | pptr = pbuf; |
| 3235 | } else if (PyString_Check(pobj)) { |
| 3236 | pptr = PyString_AsString(pobj); |
| 3237 | } else if (pobj == Py_None) { |
| 3238 | pptr = (char *)NULL; |
| 3239 | } else { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3240 | PyErr_SetString(socket_error, "Int or String expected"); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3241 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3242 | } |
| 3243 | memset(&hints, 0, sizeof(hints)); |
| 3244 | hints.ai_family = family; |
| 3245 | hints.ai_socktype = socktype; |
| 3246 | hints.ai_protocol = protocol; |
| 3247 | hints.ai_flags = flags; |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3248 | Py_BEGIN_ALLOW_THREADS |
| 3249 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3250 | error = getaddrinfo(hptr, pptr, &hints, &res0); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3251 | Py_END_ALLOW_THREADS |
| 3252 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3253 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3254 | set_gaierror(error); |
Martin v. Löwis | 5db099a | 2003-08-07 11:55:15 +0000 | [diff] [blame] | 3255 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | if ((all = PyList_New(0)) == NULL) |
| 3259 | goto err; |
| 3260 | for (res = res0; res; res = res->ai_next) { |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3261 | PyObject *addr = |
| 3262 | makesockaddr(-1, res->ai_addr, res->ai_addrlen); |
| 3263 | if (addr == NULL) |
| 3264 | goto err; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3265 | single = Py_BuildValue("iiisO", res->ai_family, |
| 3266 | res->ai_socktype, res->ai_protocol, |
| 3267 | res->ai_canonname ? res->ai_canonname : "", |
Guido van Rossum | 716aac0 | 2001-10-12 18:59:27 +0000 | [diff] [blame] | 3268 | addr); |
| 3269 | Py_DECREF(addr); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3270 | if (single == NULL) |
| 3271 | goto err; |
| 3272 | |
| 3273 | if (PyList_Append(all, single)) |
| 3274 | goto err; |
| 3275 | Py_XDECREF(single); |
| 3276 | } |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3277 | Py_XDECREF(idna); |
Neal Norwitz | 90128ba | 2002-08-09 03:37:42 +0000 | [diff] [blame] | 3278 | if (res0) |
| 3279 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3280 | return all; |
| 3281 | err: |
| 3282 | Py_XDECREF(single); |
| 3283 | Py_XDECREF(all); |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 3284 | Py_XDECREF(idna); |
Martin v. Löwis | f0b11d2 | 2001-11-07 08:31:03 +0000 | [diff] [blame] | 3285 | if (res0) |
| 3286 | freeaddrinfo(res0); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3287 | return (PyObject *)NULL; |
| 3288 | } |
| 3289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3290 | PyDoc_STRVAR(getaddrinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3291 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 3292 | -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3293 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3294 | Resolve host and port into addrinfo struct."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3295 | |
| 3296 | /* Python interface to getnameinfo(sa, flags). */ |
| 3297 | |
| 3298 | /*ARGSUSED*/ |
| 3299 | static PyObject * |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3300 | socket_getnameinfo(PyObject *self, PyObject *args) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3301 | { |
| 3302 | PyObject *sa = (PyObject *)NULL; |
| 3303 | int flags; |
| 3304 | char *hostp; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3305 | int port, flowinfo, scope_id; |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3306 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 3307 | struct addrinfo hints, *res = NULL; |
| 3308 | int error; |
| 3309 | PyObject *ret = (PyObject *)NULL; |
| 3310 | |
| 3311 | flags = flowinfo = scope_id = 0; |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3312 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3313 | return NULL; |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3314 | if (!PyArg_ParseTuple(sa, "si|ii", |
| 3315 | &hostp, &port, &flowinfo, &scope_id)) |
Martin v. Löwis | 06b1d21 | 2001-11-02 23:34:52 +0000 | [diff] [blame] | 3316 | return NULL; |
Marc-André Lemburg | 03d1b18 | 2001-07-31 18:05:33 +0000 | [diff] [blame] | 3317 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3318 | memset(&hints, 0, sizeof(hints)); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3319 | hints.ai_family = AF_UNSPEC; |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3320 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3321 | Py_BEGIN_ALLOW_THREADS |
| 3322 | ACQUIRE_GETADDRINFO_LOCK |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3323 | error = getaddrinfo(hostp, pbuf, &hints, &res); |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 3324 | Py_END_ALLOW_THREADS |
| 3325 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3326 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3327 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3328 | goto fail; |
| 3329 | } |
| 3330 | if (res->ai_next) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3331 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3332 | "sockaddr resolved to multiple addresses"); |
| 3333 | goto fail; |
| 3334 | } |
| 3335 | switch (res->ai_family) { |
| 3336 | case AF_INET: |
| 3337 | { |
| 3338 | char *t1; |
| 3339 | int t2; |
| 3340 | if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3341 | PyErr_SetString(socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3342 | "IPv4 sockaddr must be 2 tuple"); |
| 3343 | goto fail; |
| 3344 | } |
| 3345 | break; |
| 3346 | } |
Martin v. Löwis | 44ddbde | 2001-12-02 10:15:37 +0000 | [diff] [blame] | 3347 | #ifdef ENABLE_IPV6 |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3348 | case AF_INET6: |
| 3349 | { |
| 3350 | struct sockaddr_in6 *sin6; |
| 3351 | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 3352 | sin6->sin6_flowinfo = flowinfo; |
| 3353 | sin6->sin6_scope_id = scope_id; |
| 3354 | break; |
| 3355 | } |
| 3356 | #endif |
| 3357 | } |
| 3358 | error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 3359 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 3360 | if (error) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3361 | set_gaierror(error); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3362 | goto fail; |
| 3363 | } |
| 3364 | ret = Py_BuildValue("ss", hbuf, pbuf); |
| 3365 | |
| 3366 | fail: |
| 3367 | if (res) |
| 3368 | freeaddrinfo(res); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3369 | return ret; |
| 3370 | } |
| 3371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3372 | PyDoc_STRVAR(getnameinfo_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3373 | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3374 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3375 | Get host and port for a sockaddr."); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3376 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3377 | |
| 3378 | /* Python API to getting and setting the default timeout value. */ |
| 3379 | |
| 3380 | static PyObject * |
| 3381 | socket_getdefaulttimeout(PyObject *self) |
| 3382 | { |
| 3383 | if (defaulttimeout < 0.0) { |
| 3384 | Py_INCREF(Py_None); |
| 3385 | return Py_None; |
| 3386 | } |
| 3387 | else |
| 3388 | return PyFloat_FromDouble(defaulttimeout); |
| 3389 | } |
| 3390 | |
| 3391 | PyDoc_STRVAR(getdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3392 | "getdefaulttimeout() -> timeout\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3393 | \n\ |
| 3394 | Returns the default timeout in floating seconds for new socket objects.\n\ |
| 3395 | A value of None indicates that new socket objects have no timeout.\n\ |
| 3396 | When the socket module is first imported, the default is None."); |
| 3397 | |
| 3398 | static PyObject * |
| 3399 | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 3400 | { |
| 3401 | double timeout; |
| 3402 | |
| 3403 | if (arg == Py_None) |
| 3404 | timeout = -1.0; |
| 3405 | else { |
| 3406 | timeout = PyFloat_AsDouble(arg); |
| 3407 | if (timeout < 0.0) { |
| 3408 | if (!PyErr_Occurred()) |
| 3409 | PyErr_SetString(PyExc_ValueError, |
| 3410 | "Timeout value out of range"); |
| 3411 | return NULL; |
| 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | defaulttimeout = timeout; |
| 3416 | |
| 3417 | Py_INCREF(Py_None); |
| 3418 | return Py_None; |
| 3419 | } |
| 3420 | |
| 3421 | PyDoc_STRVAR(setdefaulttimeout_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3422 | "setdefaulttimeout(timeout)\n\ |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3423 | \n\ |
| 3424 | Set the default timeout in floating seconds for new socket objects.\n\ |
| 3425 | A value of None indicates that new socket objects have no timeout.\n\ |
| 3426 | When the socket module is first imported, the default is None."); |
| 3427 | |
| 3428 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3429 | /* List of functions exported by this module. */ |
| 3430 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3431 | static PyMethodDef socket_methods[] = { |
| 3432 | {"gethostbyname", socket_gethostbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3433 | METH_VARARGS, gethostbyname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3434 | {"gethostbyname_ex", socket_gethostbyname_ex, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3435 | METH_VARARGS, ghbn_ex_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3436 | {"gethostbyaddr", socket_gethostbyaddr, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3437 | METH_VARARGS, gethostbyaddr_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3438 | {"gethostname", socket_gethostname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3439 | METH_VARARGS, gethostname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3440 | {"getservbyname", socket_getservbyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3441 | METH_VARARGS, getservbyname_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3442 | {"getprotobyname", socket_getprotobyname, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3443 | METH_VARARGS,getprotobyname_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3444 | #ifndef NO_DUP |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3445 | {"fromfd", socket_fromfd, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3446 | METH_VARARGS, fromfd_doc}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3447 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3448 | {"ntohs", socket_ntohs, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3449 | METH_VARARGS, ntohs_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3450 | {"ntohl", socket_ntohl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3451 | METH_O, ntohl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3452 | {"htons", socket_htons, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3453 | METH_VARARGS, htons_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3454 | {"htonl", socket_htonl, |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 3455 | METH_O, htonl_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3456 | {"inet_aton", socket_inet_aton, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3457 | METH_VARARGS, inet_aton_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3458 | {"inet_ntoa", socket_inet_ntoa, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 3459 | METH_VARARGS, inet_ntoa_doc}, |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3460 | #ifdef HAVE_INET_PTON |
| 3461 | {"inet_pton", socket_inet_pton, |
| 3462 | METH_VARARGS, inet_pton_doc}, |
| 3463 | {"inet_ntop", socket_inet_ntop, |
| 3464 | METH_VARARGS, inet_ntop_doc}, |
| 3465 | #endif |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3466 | {"getaddrinfo", socket_getaddrinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3467 | METH_VARARGS, getaddrinfo_doc}, |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3468 | {"getnameinfo", socket_getnameinfo, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3469 | METH_VARARGS, getnameinfo_doc}, |
Guido van Rossum | 1693ba8 | 2002-07-18 21:11:26 +0000 | [diff] [blame] | 3470 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 3471 | METH_NOARGS, getdefaulttimeout_doc}, |
| 3472 | {"setdefaulttimeout", socket_setdefaulttimeout, |
| 3473 | METH_O, setdefaulttimeout_doc}, |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3474 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3475 | }; |
| 3476 | |
Guido van Rossum | 30a685f | 1991-06-27 15:51:29 +0000 | [diff] [blame] | 3477 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3478 | #ifdef RISCOS |
| 3479 | #define OS_INIT_DEFINED |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3480 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3481 | static int |
| 3482 | os_init(void) |
| 3483 | { |
| 3484 | _kernel_swi_regs r; |
| 3485 | |
| 3486 | r.r[0] = 0; |
| 3487 | _kernel_swi(0x43380, &r, &r); |
| 3488 | taskwindow = r.r[0]; |
| 3489 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 3490 | return 1; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | #endif /* RISCOS */ |
| 3494 | |
| 3495 | |
| 3496 | #ifdef MS_WINDOWS |
| 3497 | #define OS_INIT_DEFINED |
| 3498 | |
| 3499 | /* Additional initialization and cleanup for Windows */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3500 | |
| 3501 | static void |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3502 | os_cleanup(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3503 | { |
| 3504 | WSACleanup(); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3508 | os_init(void) |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3509 | { |
| 3510 | WSADATA WSAData; |
| 3511 | int ret; |
| 3512 | char buf[100]; |
| 3513 | ret = WSAStartup(0x0101, &WSAData); |
| 3514 | switch (ret) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3515 | case 0: /* No error */ |
Mark Hammond | 14350ab | 2003-07-09 04:57:46 +0000 | [diff] [blame] | 3516 | Py_AtExit(os_cleanup); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3517 | return 1; /* Success */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3518 | case WSASYSNOTREADY: |
| 3519 | PyErr_SetString(PyExc_ImportError, |
| 3520 | "WSAStartup failed: network not ready"); |
| 3521 | break; |
| 3522 | case WSAVERNOTSUPPORTED: |
| 3523 | case WSAEINVAL: |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3524 | PyErr_SetString( |
| 3525 | PyExc_ImportError, |
| 3526 | "WSAStartup failed: requested version not supported"); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3527 | break; |
| 3528 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 3529 | PyOS_snprintf(buf, sizeof(buf), |
| 3530 | "WSAStartup failed: error code %d", ret); |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3531 | PyErr_SetString(PyExc_ImportError, buf); |
| 3532 | break; |
| 3533 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3534 | return 0; /* Failure */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
Guido van Rossum | 8d665e6 | 1996-06-26 18:22:49 +0000 | [diff] [blame] | 3537 | #endif /* MS_WINDOWS */ |
Guido van Rossum | be32c89 | 1996-06-20 16:25:29 +0000 | [diff] [blame] | 3538 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3539 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3540 | #ifdef PYOS_OS2 |
| 3541 | #define OS_INIT_DEFINED |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3542 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3543 | /* Additional initialization for OS/2 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3544 | |
| 3545 | static int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3546 | os_init(void) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3547 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3548 | #ifndef PYCC_GCC |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3549 | char reason[64]; |
| 3550 | int rc = sock_init(); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3551 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3552 | if (rc == 0) { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3553 | return 1; /* Success */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3554 | } |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3555 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3556 | PyOS_snprintf(reason, sizeof(reason), |
| 3557 | "OS/2 TCP/IP Error# %d", sock_errno()); |
| 3558 | PyErr_SetString(PyExc_ImportError, reason); |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3559 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3560 | return 0; /* Failure */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 3561 | #else |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3562 | /* No need to initialise sockets with GCC/EMX */ |
| 3563 | return 1; /* Success */ |
Andrew MacIntyre | ba43e87 | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 3564 | #endif |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | #endif /* PYOS_OS2 */ |
| 3568 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3569 | |
| 3570 | #ifndef OS_INIT_DEFINED |
| 3571 | static int |
| 3572 | os_init(void) |
| 3573 | { |
| 3574 | return 1; /* Success */ |
| 3575 | } |
| 3576 | #endif |
| 3577 | |
| 3578 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3579 | /* C API table - always add new things to the end for binary |
| 3580 | compatibility. */ |
| 3581 | static |
| 3582 | PySocketModule_APIObject PySocketModuleAPI = |
| 3583 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3584 | &sock_type, |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 3585 | NULL |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3586 | }; |
| 3587 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3588 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3589 | /* Initialize the _socket module. |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3590 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3591 | This module is actually called "_socket", and there's a wrapper |
| 3592 | "socket.py" which implements some additional functionality. On some |
| 3593 | platforms (e.g. Windows and OS/2), socket.py also implements a |
| 3594 | wrapper for the socket type that provides missing functionality such |
| 3595 | as makefile(), dup() and fromfd(). The import of "_socket" may fail |
| 3596 | with an ImportError exception if os-specific initialization fails. |
| 3597 | On Windows, this does WINSOCK initialization. When WINSOCK is |
| 3598 | initialized succesfully, a call to WSACleanup() is scheduled to be |
| 3599 | made at exit time. |
| 3600 | */ |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3601 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3602 | PyDoc_STRVAR(socket_doc, |
Guido van Rossum | 22a9715 | 2002-08-08 20:37:08 +0000 | [diff] [blame] | 3603 | "Implementation module for socket operations.\n\ |
| 3604 | \n\ |
| 3605 | See the socket module for documentation."); |
Guido van Rossum | 82a5c66 | 1998-07-07 20:45:43 +0000 | [diff] [blame] | 3606 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 3607 | PyMODINIT_FUNC |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 3608 | init_socket(void) |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 3609 | { |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3610 | PyObject *m, *has_ipv6; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3611 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3612 | if (!os_init()) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3613 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3614 | |
| 3615 | sock_type.ob_type = &PyType_Type; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3616 | m = Py_InitModule3(PySocket_MODULE_NAME, |
| 3617 | socket_methods, |
| 3618 | socket_doc); |
| 3619 | |
| 3620 | socket_error = PyErr_NewException("socket.error", NULL, NULL); |
| 3621 | if (socket_error == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3622 | return; |
Brett Cannon | 06c3479 | 2004-03-23 23:16:54 +0000 | [diff] [blame] | 3623 | PySocketModuleAPI.error = socket_error; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3624 | Py_INCREF(socket_error); |
| 3625 | PyModule_AddObject(m, "error", socket_error); |
| 3626 | socket_herror = PyErr_NewException("socket.herror", |
| 3627 | socket_error, NULL); |
| 3628 | if (socket_herror == NULL) |
| 3629 | return; |
| 3630 | Py_INCREF(socket_herror); |
| 3631 | PyModule_AddObject(m, "herror", socket_herror); |
| 3632 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3633 | NULL); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3634 | if (socket_gaierror == NULL) |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3635 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3636 | Py_INCREF(socket_gaierror); |
| 3637 | PyModule_AddObject(m, "gaierror", socket_gaierror); |
Raymond Hettinger | ef7343c | 2003-06-29 03:08:05 +0000 | [diff] [blame] | 3638 | socket_timeout = PyErr_NewException("socket.timeout", |
| 3639 | socket_error, NULL); |
| 3640 | if (socket_timeout == NULL) |
| 3641 | return; |
| 3642 | Py_INCREF(socket_timeout); |
| 3643 | PyModule_AddObject(m, "timeout", socket_timeout); |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3644 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3645 | if (PyModule_AddObject(m, "SocketType", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3646 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 3647 | return; |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3648 | Py_INCREF((PyObject *)&sock_type); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3649 | if (PyModule_AddObject(m, "socket", |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 3650 | (PyObject *)&sock_type) != 0) |
Guido van Rossum | 384ca9c | 2001-10-27 22:20:47 +0000 | [diff] [blame] | 3651 | return; |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3652 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 3653 | #ifdef ENABLE_IPV6 |
| 3654 | has_ipv6 = Py_True; |
| 3655 | #else |
| 3656 | has_ipv6 = Py_False; |
| 3657 | #endif |
| 3658 | Py_INCREF(has_ipv6); |
| 3659 | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
| 3660 | |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3661 | /* Export C API */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3662 | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
Marc-André Lemburg | a5d2b4c | 2002-02-16 18:23:30 +0000 | [diff] [blame] | 3663 | PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL) |
| 3664 | ) != 0) |
| 3665 | return; |
| 3666 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3667 | /* Address families (we only support AF_INET and AF_UNIX) */ |
| 3668 | #ifdef AF_UNSPEC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3669 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3670 | #endif |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3671 | PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3672 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3673 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 3674 | #endif /* AF_INET6 */ |
Andrew MacIntyre | d12dfbb | 2004-04-04 07:13:49 +0000 | [diff] [blame] | 3675 | #if defined(AF_UNIX) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3676 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3677 | #endif /* AF_UNIX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3678 | #ifdef AF_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3679 | /* Amateur Radio AX.25 */ |
| 3680 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3681 | #endif |
| 3682 | #ifdef AF_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3683 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3684 | #endif |
| 3685 | #ifdef AF_APPLETALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3686 | /* Appletalk DDP */ |
| 3687 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3688 | #endif |
| 3689 | #ifdef AF_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3690 | /* Amateur radio NetROM */ |
| 3691 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3692 | #endif |
| 3693 | #ifdef AF_BRIDGE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3694 | /* Multiprotocol bridge */ |
| 3695 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3696 | #endif |
| 3697 | #ifdef AF_AAL5 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3698 | /* Reserved for Werner's ATM */ |
| 3699 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3700 | #endif |
| 3701 | #ifdef AF_X25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3702 | /* Reserved for X.25 project */ |
| 3703 | PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3704 | #endif |
| 3705 | #ifdef AF_INET6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3706 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3707 | #endif |
| 3708 | #ifdef AF_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3709 | /* Amateur Radio X.25 PLP */ |
| 3710 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3711 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3712 | |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 3713 | #ifdef USE_BLUETOOTH |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3714 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 3715 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 3716 | #if !defined(__FreeBSD__) |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3717 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
Hye-Shik Chang | 8126860 | 2004-02-02 06:05:24 +0000 | [diff] [blame] | 3718 | #endif |
Martin v. Löwis | 12af048 | 2004-01-31 12:34:17 +0000 | [diff] [blame] | 3719 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 3720 | PyModule_AddObject(m, "BDADDR_ANY", Py_BuildValue( "iiiiii", 0,0,0,0,0,0 ) ); |
| 3721 | PyModule_AddObject(m, "BDADDR_LOCAL", Py_BuildValue( "iiiiii", 0,0,0,0xff,0xff,0xff ) ); |
| 3722 | #endif |
| 3723 | |
Martin v. Löwis | 1ba3fd5 | 2001-08-10 20:29:40 +0000 | [diff] [blame] | 3724 | #ifdef HAVE_NETPACKET_PACKET_H |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3725 | PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); |
| 3726 | PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); |
| 3727 | PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); |
| 3728 | PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 3729 | PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 3730 | PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 3731 | PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 3732 | PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 3733 | PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 3734 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3735 | |
| 3736 | /* Socket types */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3737 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 3738 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3739 | #ifndef __BEOS__ |
| 3740 | /* We have incomplete socket support. */ |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3741 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 3742 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3743 | #if defined(SOCK_RDM) |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3744 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 3745 | #endif |
Martin v. Löwis | cf8f47e | 2002-12-11 13:10:57 +0000 | [diff] [blame] | 3746 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3747 | |
| 3748 | #ifdef SO_DEBUG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3749 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3750 | #endif |
| 3751 | #ifdef SO_ACCEPTCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3752 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3753 | #endif |
| 3754 | #ifdef SO_REUSEADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3755 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3756 | #endif |
| 3757 | #ifdef SO_KEEPALIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3758 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3759 | #endif |
| 3760 | #ifdef SO_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3761 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3762 | #endif |
| 3763 | #ifdef SO_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3764 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3765 | #endif |
| 3766 | #ifdef SO_USELOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3767 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3768 | #endif |
| 3769 | #ifdef SO_LINGER |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3770 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3771 | #endif |
| 3772 | #ifdef SO_OOBINLINE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3773 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3774 | #endif |
| 3775 | #ifdef SO_REUSEPORT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3776 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3777 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3778 | #ifdef SO_SNDBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3779 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3780 | #endif |
| 3781 | #ifdef SO_RCVBUF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3782 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3783 | #endif |
| 3784 | #ifdef SO_SNDLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3785 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3786 | #endif |
| 3787 | #ifdef SO_RCVLOWAT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3788 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3789 | #endif |
| 3790 | #ifdef SO_SNDTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3791 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3792 | #endif |
| 3793 | #ifdef SO_RCVTIMEO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3794 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3795 | #endif |
| 3796 | #ifdef SO_ERROR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3797 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3798 | #endif |
| 3799 | #ifdef SO_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3800 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3801 | #endif |
| 3802 | |
| 3803 | /* Maximum number of connections for "listen" */ |
| 3804 | #ifdef SOMAXCONN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3805 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3806 | #else |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 3807 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3808 | #endif |
| 3809 | |
| 3810 | /* Flags for send, recv */ |
| 3811 | #ifdef MSG_OOB |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3812 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3813 | #endif |
| 3814 | #ifdef MSG_PEEK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3815 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3816 | #endif |
| 3817 | #ifdef MSG_DONTROUTE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3818 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3819 | #endif |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 3820 | #ifdef MSG_DONTWAIT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3821 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
Guido van Rossum | 2c8bcb8 | 2000-04-25 21:34:53 +0000 | [diff] [blame] | 3822 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3823 | #ifdef MSG_EOR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3824 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3825 | #endif |
| 3826 | #ifdef MSG_TRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3827 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3828 | #endif |
| 3829 | #ifdef MSG_CTRUNC |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3830 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3831 | #endif |
| 3832 | #ifdef MSG_WAITALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3833 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3834 | #endif |
| 3835 | #ifdef MSG_BTAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3836 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3837 | #endif |
| 3838 | #ifdef MSG_ETAG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3839 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3840 | #endif |
| 3841 | |
| 3842 | /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 3843 | #ifdef SOL_SOCKET |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3844 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3845 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3846 | #ifdef SOL_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3847 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3848 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3849 | PyModule_AddIntConstant(m, "SOL_IP", 0); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3850 | #endif |
| 3851 | #ifdef SOL_IPX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3852 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3853 | #endif |
| 3854 | #ifdef SOL_AX25 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3855 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3856 | #endif |
| 3857 | #ifdef SOL_ATALK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3858 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3859 | #endif |
| 3860 | #ifdef SOL_NETROM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3861 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3862 | #endif |
| 3863 | #ifdef SOL_ROSE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3864 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3865 | #endif |
| 3866 | #ifdef SOL_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3867 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3868 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3869 | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3870 | #endif |
| 3871 | #ifdef SOL_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3872 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3873 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3874 | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 3875 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3876 | #ifdef IPPROTO_IP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3877 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3878 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3879 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3880 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3881 | #ifdef IPPROTO_HOPOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3882 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3883 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3884 | #ifdef IPPROTO_ICMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3885 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3886 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3887 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3888 | #endif |
| 3889 | #ifdef IPPROTO_IGMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3890 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3891 | #endif |
| 3892 | #ifdef IPPROTO_GGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3893 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3894 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3895 | #ifdef IPPROTO_IPV4 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3896 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3897 | #endif |
Martin v. Löwis | a0f1734 | 2003-10-03 13:56:20 +0000 | [diff] [blame] | 3898 | #ifdef IPPROTO_IPV6 |
| 3899 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
| 3900 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3901 | #ifdef IPPROTO_IPIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3902 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3903 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3904 | #ifdef IPPROTO_TCP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3905 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3906 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3907 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3908 | #endif |
| 3909 | #ifdef IPPROTO_EGP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3910 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3911 | #endif |
| 3912 | #ifdef IPPROTO_PUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3913 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3914 | #endif |
| 3915 | #ifdef IPPROTO_UDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3916 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3917 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3918 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3919 | #endif |
| 3920 | #ifdef IPPROTO_IDP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3921 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3922 | #endif |
| 3923 | #ifdef IPPROTO_HELLO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3924 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3925 | #endif |
| 3926 | #ifdef IPPROTO_ND |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3927 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3928 | #endif |
| 3929 | #ifdef IPPROTO_TP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3930 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3931 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3932 | #ifdef IPPROTO_IPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3933 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3934 | #endif |
| 3935 | #ifdef IPPROTO_ROUTING |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3936 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3937 | #endif |
| 3938 | #ifdef IPPROTO_FRAGMENT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3939 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3940 | #endif |
| 3941 | #ifdef IPPROTO_RSVP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3942 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3943 | #endif |
| 3944 | #ifdef IPPROTO_GRE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3945 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3946 | #endif |
| 3947 | #ifdef IPPROTO_ESP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3948 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3949 | #endif |
| 3950 | #ifdef IPPROTO_AH |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3951 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3952 | #endif |
| 3953 | #ifdef IPPROTO_MOBILE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3954 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3955 | #endif |
| 3956 | #ifdef IPPROTO_ICMPV6 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3957 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3958 | #endif |
| 3959 | #ifdef IPPROTO_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3960 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3961 | #endif |
| 3962 | #ifdef IPPROTO_DSTOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3963 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3964 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3965 | #ifdef IPPROTO_XTP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3966 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3967 | #endif |
| 3968 | #ifdef IPPROTO_EON |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3969 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3970 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3971 | #ifdef IPPROTO_PIM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3972 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3973 | #endif |
| 3974 | #ifdef IPPROTO_IPCOMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3975 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3976 | #endif |
| 3977 | #ifdef IPPROTO_VRRP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3978 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 3979 | #endif |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3980 | #ifdef IPPROTO_BIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3981 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3982 | #endif |
| 3983 | /**/ |
| 3984 | #ifdef IPPROTO_RAW |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3985 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
Guido van Rossum | 578de30 | 1998-05-28 20:18:18 +0000 | [diff] [blame] | 3986 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3987 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3988 | #endif |
| 3989 | #ifdef IPPROTO_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3990 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3991 | #endif |
| 3992 | |
| 3993 | /* Some port configuration */ |
| 3994 | #ifdef IPPORT_RESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3995 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3996 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 3997 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 3998 | #endif |
| 3999 | #ifdef IPPORT_USERRESERVED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4000 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4001 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4002 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4003 | #endif |
| 4004 | |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4005 | /* Some reserved IP v.4 addresses */ |
| 4006 | #ifdef INADDR_ANY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4007 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4008 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4009 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4010 | #endif |
| 4011 | #ifdef INADDR_BROADCAST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4012 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4013 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4014 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4015 | #endif |
| 4016 | #ifdef INADDR_LOOPBACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4017 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4018 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4019 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4020 | #endif |
| 4021 | #ifdef INADDR_UNSPEC_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4022 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4023 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4024 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4025 | #endif |
| 4026 | #ifdef INADDR_ALLHOSTS_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4027 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 4028 | INADDR_ALLHOSTS_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4029 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4030 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4031 | #endif |
| 4032 | #ifdef INADDR_MAX_LOCAL_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4033 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 4034 | INADDR_MAX_LOCAL_GROUP); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4035 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4036 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4037 | #endif |
| 4038 | #ifdef INADDR_NONE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4039 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4040 | #else |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4041 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
Guido van Rossum | 5f05eb4 | 1995-02-17 15:11:07 +0000 | [diff] [blame] | 4042 | #endif |
| 4043 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4044 | /* IPv4 [gs]etsockopt options */ |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4045 | #ifdef IP_OPTIONS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4046 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4047 | #endif |
| 4048 | #ifdef IP_HDRINCL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4049 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4050 | #endif |
| 4051 | #ifdef IP_TOS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4052 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4053 | #endif |
| 4054 | #ifdef IP_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4055 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4056 | #endif |
| 4057 | #ifdef IP_RECVOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4058 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4059 | #endif |
| 4060 | #ifdef IP_RECVRETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4061 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4062 | #endif |
| 4063 | #ifdef IP_RECVDSTADDR |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4064 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4065 | #endif |
| 4066 | #ifdef IP_RETOPTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4067 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4068 | #endif |
| 4069 | #ifdef IP_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4070 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4071 | #endif |
| 4072 | #ifdef IP_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4073 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4074 | #endif |
| 4075 | #ifdef IP_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4076 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4077 | #endif |
| 4078 | #ifdef IP_ADD_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4079 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4080 | #endif |
| 4081 | #ifdef IP_DROP_MEMBERSHIP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4082 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
Guido van Rossum | 3fdf58b | 1995-02-07 15:39:52 +0000 | [diff] [blame] | 4083 | #endif |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4084 | #ifdef IP_DEFAULT_MULTICAST_TTL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4085 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 4086 | IP_DEFAULT_MULTICAST_TTL); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4087 | #endif |
| 4088 | #ifdef IP_DEFAULT_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4089 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 4090 | IP_DEFAULT_MULTICAST_LOOP); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4091 | #endif |
| 4092 | #ifdef IP_MAX_MEMBERSHIPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4093 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4094 | #endif |
| 4095 | |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4096 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 4097 | #ifdef IPV6_JOIN_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4098 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4099 | #endif |
| 4100 | #ifdef IPV6_LEAVE_GROUP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4101 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4102 | #endif |
| 4103 | #ifdef IPV6_MULTICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4104 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4105 | #endif |
| 4106 | #ifdef IPV6_MULTICAST_IF |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4107 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4108 | #endif |
| 4109 | #ifdef IPV6_MULTICAST_LOOP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4110 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4111 | #endif |
| 4112 | #ifdef IPV6_UNICAST_HOPS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4113 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4114 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4115 | /* Additional IPV6 socket options, defined in RFC 3493 */ |
| 4116 | #ifdef IPV6_V6ONLY |
| 4117 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
| 4118 | #endif |
| 4119 | /* Advanced IPV6 socket options, from RFC 3542 */ |
| 4120 | #ifdef IPV6_CHECKSUM |
| 4121 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
| 4122 | #endif |
| 4123 | #ifdef IPV6_DONTFRAG |
| 4124 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
| 4125 | #endif |
| 4126 | #ifdef IPV6_DSTOPTS |
| 4127 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
| 4128 | #endif |
| 4129 | #ifdef IPV6_HOPLIMIT |
| 4130 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
| 4131 | #endif |
| 4132 | #ifdef IPV6_HOPOPTS |
| 4133 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
| 4134 | #endif |
| 4135 | #ifdef IPV6_NEXTHOP |
| 4136 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
| 4137 | #endif |
| 4138 | #ifdef IPV6_PATHMTU |
| 4139 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
| 4140 | #endif |
| 4141 | #ifdef IPV6_PKTINFO |
| 4142 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
| 4143 | #endif |
| 4144 | #ifdef IPV6_RECVDSTOPTS |
| 4145 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
| 4146 | #endif |
| 4147 | #ifdef IPV6_RECVHOPLIMIT |
| 4148 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
| 4149 | #endif |
| 4150 | #ifdef IPV6_RECVHOPOPTS |
| 4151 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
| 4152 | #endif |
| 4153 | #ifdef IPV6_RECVPKTINFO |
| 4154 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
| 4155 | #endif |
| 4156 | #ifdef IPV6_RECVRTHDR |
| 4157 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
| 4158 | #endif |
| 4159 | #ifdef IPV6_RECVTCLASS |
| 4160 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
| 4161 | #endif |
| 4162 | #ifdef IPV6_RTHDR |
| 4163 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
| 4164 | #endif |
| 4165 | #ifdef IPV6_RTHDRDSTOPTS |
| 4166 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
| 4167 | #endif |
| 4168 | #ifdef IPV6_RTHDR_TYPE_0 |
| 4169 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); |
| 4170 | #endif |
| 4171 | #ifdef IPV6_RECVPATHMTU |
| 4172 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
| 4173 | #endif |
| 4174 | #ifdef IPV6_TCLASS |
| 4175 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
| 4176 | #endif |
| 4177 | #ifdef IPV6_USE_MIN_MTU |
| 4178 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); |
| 4179 | #endif |
Martin v. Löwis | bcf4b35 | 2001-08-04 22:37:23 +0000 | [diff] [blame] | 4180 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4181 | /* TCP options */ |
| 4182 | #ifdef TCP_NODELAY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4183 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4184 | #endif |
| 4185 | #ifdef TCP_MAXSEG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4186 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4187 | #endif |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4188 | #ifdef TCP_CORK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4189 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4190 | #endif |
| 4191 | #ifdef TCP_KEEPIDLE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4192 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4193 | #endif |
| 4194 | #ifdef TCP_KEEPINTVL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4195 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4196 | #endif |
| 4197 | #ifdef TCP_KEEPCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4198 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4199 | #endif |
| 4200 | #ifdef TCP_SYNCNT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4201 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4202 | #endif |
| 4203 | #ifdef TCP_LINGER2 |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4204 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4205 | #endif |
| 4206 | #ifdef TCP_DEFER_ACCEPT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4207 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4208 | #endif |
| 4209 | #ifdef TCP_WINDOW_CLAMP |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4210 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4211 | #endif |
| 4212 | #ifdef TCP_INFO |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4213 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4214 | #endif |
| 4215 | #ifdef TCP_QUICKACK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4216 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
Martin v. Löwis | 3cde2cb | 2001-12-22 15:05:32 +0000 | [diff] [blame] | 4217 | #endif |
| 4218 | |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4219 | |
| 4220 | /* IPX options */ |
| 4221 | #ifdef IPX_TYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4222 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
Guido van Rossum | 09be409 | 1999-08-09 14:40:40 +0000 | [diff] [blame] | 4223 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4224 | |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4225 | /* get{addr,name}info parameters */ |
| 4226 | #ifdef EAI_ADDRFAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4227 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4228 | #endif |
| 4229 | #ifdef EAI_AGAIN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4230 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4231 | #endif |
| 4232 | #ifdef EAI_BADFLAGS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4233 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4234 | #endif |
| 4235 | #ifdef EAI_FAIL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4236 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4237 | #endif |
| 4238 | #ifdef EAI_FAMILY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4239 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4240 | #endif |
| 4241 | #ifdef EAI_MEMORY |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4242 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4243 | #endif |
| 4244 | #ifdef EAI_NODATA |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4245 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4246 | #endif |
| 4247 | #ifdef EAI_NONAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4248 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4249 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4250 | #ifdef EAI_OVERFLOW |
| 4251 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
| 4252 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4253 | #ifdef EAI_SERVICE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4254 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4255 | #endif |
| 4256 | #ifdef EAI_SOCKTYPE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4257 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4258 | #endif |
| 4259 | #ifdef EAI_SYSTEM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4260 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4261 | #endif |
| 4262 | #ifdef EAI_BADHINTS |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4263 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4264 | #endif |
| 4265 | #ifdef EAI_PROTOCOL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4266 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4267 | #endif |
| 4268 | #ifdef EAI_MAX |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4269 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4270 | #endif |
| 4271 | #ifdef AI_PASSIVE |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4272 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4273 | #endif |
| 4274 | #ifdef AI_CANONNAME |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4275 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4276 | #endif |
| 4277 | #ifdef AI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4278 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4279 | #endif |
Martin v. Löwis | da91d02 | 2003-12-30 11:14:01 +0000 | [diff] [blame] | 4280 | #ifdef AI_NUMERICSERV |
| 4281 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
| 4282 | #endif |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4283 | #ifdef AI_MASK |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4284 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4285 | #endif |
| 4286 | #ifdef AI_ALL |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4287 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4288 | #endif |
| 4289 | #ifdef AI_V4MAPPED_CFG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4290 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4291 | #endif |
| 4292 | #ifdef AI_ADDRCONFIG |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4293 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4294 | #endif |
| 4295 | #ifdef AI_V4MAPPED |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4296 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4297 | #endif |
| 4298 | #ifdef AI_DEFAULT |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4299 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4300 | #endif |
| 4301 | #ifdef NI_MAXHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4302 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4303 | #endif |
| 4304 | #ifdef NI_MAXSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4305 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4306 | #endif |
| 4307 | #ifdef NI_NOFQDN |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4308 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4309 | #endif |
| 4310 | #ifdef NI_NUMERICHOST |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4311 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4312 | #endif |
| 4313 | #ifdef NI_NAMEREQD |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4314 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4315 | #endif |
| 4316 | #ifdef NI_NUMERICSERV |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4317 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4318 | #endif |
| 4319 | #ifdef NI_DGRAM |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 4320 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
Martin v. Löwis | 2d8d427 | 2001-07-21 18:05:31 +0000 | [diff] [blame] | 4321 | #endif |
| 4322 | |
Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 4323 | /* shutdown() parameters */ |
| 4324 | #ifdef SHUT_RD |
| 4325 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
| 4326 | #elif defined(SD_RECEIVE) |
| 4327 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
| 4328 | #else |
| 4329 | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
| 4330 | #endif |
| 4331 | #ifdef SHUT_WR |
| 4332 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
| 4333 | #elif defined(SD_SEND) |
| 4334 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
| 4335 | #else |
| 4336 | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
| 4337 | #endif |
| 4338 | #ifdef SHUT_RDWR |
| 4339 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
| 4340 | #elif defined(SD_BOTH) |
| 4341 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
| 4342 | #else |
| 4343 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
| 4344 | #endif |
| 4345 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4346 | /* Initialize gethostbyname lock */ |
Just van Rossum | 1040d2c | 2003-05-09 07:53:18 +0000 | [diff] [blame] | 4347 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 4348 | netdb_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 4349 | #endif |
Guido van Rossum | 6574b3e | 1991-06-25 21:36:08 +0000 | [diff] [blame] | 4350 | } |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4351 | |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4352 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4353 | #ifndef HAVE_INET_PTON |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4354 | |
| 4355 | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 4356 | /* These are not exposed because they do not set errno properly */ |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4357 | |
Guido van Rossum | 3eede5a | 2002-06-07 02:08:35 +0000 | [diff] [blame] | 4358 | int |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4359 | inet_pton(int af, const char *src, void *dst) |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4360 | { |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4361 | if (af == AF_INET) { |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4362 | long packed_addr; |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4363 | packed_addr = inet_addr(src); |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4364 | if (packed_addr == INADDR_NONE) |
| 4365 | return 0; |
| 4366 | memcpy(dst, &packed_addr, 4); |
| 4367 | return 1; |
| 4368 | } |
| 4369 | /* Should set errno to EAFNOSUPPORT */ |
| 4370 | return -1; |
| 4371 | } |
| 4372 | |
Martin v. Löwis | c925b153 | 2001-07-21 09:42:15 +0000 | [diff] [blame] | 4373 | const char * |
| 4374 | 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] | 4375 | { |
| 4376 | if (af == AF_INET) { |
| 4377 | struct in_addr packed_addr; |
| 4378 | if (size < 16) |
| 4379 | /* Should set errno to ENOSPC. */ |
| 4380 | return NULL; |
| 4381 | memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 4382 | return strncpy(dst, inet_ntoa(packed_addr), size); |
| 4383 | } |
| 4384 | /* Should set errno to EAFNOSUPPORT */ |
| 4385 | return NULL; |
| 4386 | } |
Guido van Rossum | c4fcfa3 | 2002-06-07 03:19:37 +0000 | [diff] [blame] | 4387 | |
Martin v. Löwis | b9ab159 | 2001-06-24 21:18:26 +0000 | [diff] [blame] | 4388 | #endif |