[2] | 1 | /* Socket module */
|
---|
| 2 |
|
---|
| 3 | /*
|
---|
| 4 |
|
---|
| 5 | This module provides an interface to Berkeley socket IPC.
|
---|
| 6 |
|
---|
| 7 | Limitations:
|
---|
| 8 |
|
---|
| 9 | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
|
---|
| 10 | portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
|
---|
| 11 | under Linux.
|
---|
| 12 | - No read/write operations (use sendall/recv or makefile instead).
|
---|
| 13 | - Additional restrictions apply on some non-Unix platforms (compensated
|
---|
| 14 | for by socket.py).
|
---|
| 15 |
|
---|
| 16 | Module interface:
|
---|
| 17 |
|
---|
| 18 | - socket.error: exception raised for socket specific errors
|
---|
| 19 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
|
---|
[391] | 20 | a subclass of socket.error
|
---|
[2] | 21 | - socket.herror: exception raised for gethostby* errors,
|
---|
[391] | 22 | a subclass of socket.error
|
---|
[2] | 23 | - socket.fromfd(fd, family, type[, proto]) --> new socket object (created
|
---|
[391] | 24 | from an existing file descriptor)
|
---|
[2] | 25 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
|
---|
| 26 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
|
---|
| 27 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
|
---|
| 28 | - socket.getprotobyname(protocolname) --> protocol number
|
---|
| 29 | - socket.getservbyname(servicename[, protocolname]) --> port number
|
---|
| 30 | - socket.getservbyport(portnumber[, protocolname]) --> service name
|
---|
| 31 | - socket.socket([family[, type [, proto]]]) --> new socket object
|
---|
| 32 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
|
---|
| 33 | - socket.ntohs(16 bit value) --> new int object
|
---|
| 34 | - socket.ntohl(32 bit value) --> new int object
|
---|
| 35 | - socket.htons(16 bit value) --> new int object
|
---|
| 36 | - socket.htonl(32 bit value) --> new int object
|
---|
| 37 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags])
|
---|
[391] | 38 | --> List of (family, socktype, proto, canonname, sockaddr)
|
---|
[2] | 39 | - socket.getnameinfo(sockaddr, flags) --> (host, port)
|
---|
| 40 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
|
---|
| 41 | - socket.has_ipv6: boolean value indicating if IPv6 is supported
|
---|
| 42 | - socket.inet_aton(IP address) -> 32-bit packed IP representation
|
---|
| 43 | - socket.inet_ntoa(packed IP) -> IP address string
|
---|
| 44 | - socket.getdefaulttimeout() -> None | float
|
---|
| 45 | - socket.setdefaulttimeout(None | float)
|
---|
| 46 | - an Internet socket address is a pair (hostname, port)
|
---|
| 47 | where hostname can be anything recognized by gethostbyname()
|
---|
| 48 | (including the dd.dd.dd.dd notation) and port is in host byte order
|
---|
| 49 | - where a hostname is returned, the dd.dd.dd.dd notation is used
|
---|
| 50 | - a UNIX domain socket address is a string specifying the pathname
|
---|
| 51 | - an AF_PACKET socket address is a tuple containing a string
|
---|
| 52 | specifying the ethernet interface and an integer specifying
|
---|
| 53 | the Ethernet protocol number to be received. For example:
|
---|
| 54 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
|
---|
| 55 | specify packet-type and ha-type/addr.
|
---|
| 56 | - an AF_TIPC socket address is expressed as
|
---|
| 57 | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of:
|
---|
[391] | 58 | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
|
---|
[2] | 59 | and scope can be one of:
|
---|
[391] | 60 | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
|
---|
[2] | 61 | The meaning of v1, v2 and v3 depends on the value of addr_type:
|
---|
[391] | 62 | if addr_type is TIPC_ADDR_NAME:
|
---|
| 63 | v1 is the server type
|
---|
| 64 | v2 is the port identifier
|
---|
| 65 | v3 is ignored
|
---|
| 66 | if addr_type is TIPC_ADDR_NAMESEQ:
|
---|
| 67 | v1 is the server type
|
---|
| 68 | v2 is the lower port number
|
---|
| 69 | v3 is the upper port number
|
---|
| 70 | if addr_type is TIPC_ADDR_ID:
|
---|
| 71 | v1 is the node
|
---|
| 72 | v2 is the ref
|
---|
| 73 | v3 is ignored
|
---|
[2] | 74 |
|
---|
| 75 |
|
---|
| 76 | Local naming conventions:
|
---|
| 77 |
|
---|
| 78 | - names starting with sock_ are socket object methods
|
---|
| 79 | - names starting with socket_ are module-level functions
|
---|
| 80 | - names starting with PySocket are exported through socketmodule.h
|
---|
| 81 |
|
---|
| 82 | */
|
---|
| 83 |
|
---|
| 84 | #ifdef __APPLE__
|
---|
| 85 | /*
|
---|
| 86 | * inet_aton is not available on OSX 10.3, yet we want to use a binary
|
---|
| 87 | * that was build on 10.4 or later to work on that release, weak linking
|
---|
| 88 | * comes to the rescue.
|
---|
| 89 | */
|
---|
| 90 | # pragma weak inet_aton
|
---|
| 91 | #endif
|
---|
| 92 |
|
---|
| 93 | #include "Python.h"
|
---|
| 94 | #include "structmember.h"
|
---|
[391] | 95 | #include "timefuncs.h"
|
---|
[2] | 96 |
|
---|
| 97 | #undef MAX
|
---|
| 98 | #define MAX(x, y) ((x) < (y) ? (y) : (x))
|
---|
| 99 |
|
---|
| 100 | /* Socket object documentation */
|
---|
| 101 | PyDoc_STRVAR(sock_doc,
|
---|
| 102 | "socket([family[, type[, proto]]]) -> socket object\n\
|
---|
| 103 | \n\
|
---|
| 104 | Open a socket of the given type. The family argument specifies the\n\
|
---|
| 105 | address family; it defaults to AF_INET. The type argument specifies\n\
|
---|
| 106 | whether this is a stream (SOCK_STREAM, this is the default)\n\
|
---|
| 107 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
|
---|
| 108 | specifying the default protocol. Keyword arguments are accepted.\n\
|
---|
| 109 | \n\
|
---|
| 110 | A socket object represents one endpoint of a network connection.\n\
|
---|
| 111 | \n\
|
---|
| 112 | Methods of socket objects (keyword arguments not allowed):\n\
|
---|
| 113 | \n\
|
---|
| 114 | accept() -- accept a connection, returning new socket and client address\n\
|
---|
| 115 | bind(addr) -- bind the socket to a local address\n\
|
---|
| 116 | close() -- close the socket\n\
|
---|
| 117 | connect(addr) -- connect the socket to a remote address\n\
|
---|
| 118 | connect_ex(addr) -- connect, return an error code instead of an exception\n\
|
---|
| 119 | dup() -- return a new socket object identical to the current one [*]\n\
|
---|
| 120 | fileno() -- return underlying file descriptor\n\
|
---|
| 121 | getpeername() -- return remote address [*]\n\
|
---|
| 122 | getsockname() -- return local address\n\
|
---|
| 123 | getsockopt(level, optname[, buflen]) -- get socket options\n\
|
---|
| 124 | gettimeout() -- return timeout or None\n\
|
---|
| 125 | listen(n) -- start listening for incoming connections\n\
|
---|
| 126 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
|
---|
| 127 | recv(buflen[, flags]) -- receive data\n\
|
---|
| 128 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
|
---|
| 129 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
|
---|
| 130 | recvfrom_into(buffer[, nbytes, [, flags])\n\
|
---|
| 131 | -- receive data and sender\'s address (into a buffer)\n\
|
---|
| 132 | sendall(data[, flags]) -- send all data\n\
|
---|
| 133 | send(data[, flags]) -- send data, may not send all of it\n\
|
---|
| 134 | sendto(data[, flags], addr) -- send data to a given address\n\
|
---|
| 135 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
|
---|
| 136 | setsockopt(level, optname, value) -- set socket options\n\
|
---|
| 137 | settimeout(None | float) -- set or clear the timeout\n\
|
---|
| 138 | shutdown(how) -- shut down traffic in one or both directions\n\
|
---|
| 139 | \n\
|
---|
| 140 | [*] not available on all platforms!");
|
---|
| 141 |
|
---|
| 142 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks.
|
---|
| 143 | I hope some day someone can clean this up please... */
|
---|
| 144 |
|
---|
| 145 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
|
---|
| 146 | script doesn't get this right, so we hardcode some platform checks below.
|
---|
| 147 | On the other hand, not all Linux versions agree, so there the settings
|
---|
| 148 | computed by the configure script are needed! */
|
---|
| 149 |
|
---|
| 150 | #ifndef linux
|
---|
| 151 | # undef HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
| 152 | # undef HAVE_GETHOSTBYNAME_R_5_ARG
|
---|
| 153 | # undef HAVE_GETHOSTBYNAME_R_6_ARG
|
---|
| 154 | #endif
|
---|
| 155 |
|
---|
| 156 | #ifndef WITH_THREAD
|
---|
| 157 | # undef HAVE_GETHOSTBYNAME_R
|
---|
| 158 | #endif
|
---|
| 159 |
|
---|
| 160 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
| 161 | # if defined(_AIX) || defined(__osf__)
|
---|
| 162 | # define HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
| 163 | # elif defined(__sun) || defined(__sgi)
|
---|
| 164 | # define HAVE_GETHOSTBYNAME_R_5_ARG
|
---|
| 165 | # elif defined(linux)
|
---|
| 166 | /* Rely on the configure script */
|
---|
| 167 | # else
|
---|
| 168 | # undef HAVE_GETHOSTBYNAME_R
|
---|
| 169 | # endif
|
---|
| 170 | #endif
|
---|
| 171 |
|
---|
| 172 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
|
---|
| 173 | !defined(MS_WINDOWS)
|
---|
| 174 | # define USE_GETHOSTBYNAME_LOCK
|
---|
| 175 | #endif
|
---|
| 176 |
|
---|
| 177 | /* To use __FreeBSD_version */
|
---|
| 178 | #ifdef HAVE_SYS_PARAM_H
|
---|
| 179 | #include <sys/param.h>
|
---|
| 180 | #endif
|
---|
| 181 | /* On systems on which getaddrinfo() is believed to not be thread-safe,
|
---|
| 182 | (this includes the getaddrinfo emulation) protect access with a lock. */
|
---|
| 183 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \
|
---|
| 184 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
|
---|
| 185 | defined(__OpenBSD__) || defined(__NetBSD__) || \
|
---|
| 186 | defined(__VMS) || !defined(HAVE_GETADDRINFO))
|
---|
| 187 | #define USE_GETADDRINFO_LOCK
|
---|
| 188 | #endif
|
---|
| 189 |
|
---|
| 190 | #ifdef USE_GETADDRINFO_LOCK
|
---|
| 191 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
|
---|
| 192 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
|
---|
| 193 | #else
|
---|
| 194 | #define ACQUIRE_GETADDRINFO_LOCK
|
---|
| 195 | #define RELEASE_GETADDRINFO_LOCK
|
---|
| 196 | #endif
|
---|
| 197 |
|
---|
| 198 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
|
---|
| 199 | # include "pythread.h"
|
---|
| 200 | #endif
|
---|
| 201 |
|
---|
| 202 | #if defined(PYCC_VACPP)
|
---|
| 203 | # include <types.h>
|
---|
| 204 | # include <io.h>
|
---|
| 205 | # include <sys/ioctl.h>
|
---|
| 206 | # include <utils.h>
|
---|
| 207 | # include <ctype.h>
|
---|
| 208 | #endif
|
---|
| 209 |
|
---|
| 210 | #if defined(__VMS)
|
---|
| 211 | # include <ioctl.h>
|
---|
| 212 | #endif
|
---|
| 213 |
|
---|
| 214 | #if defined(PYOS_OS2)
|
---|
| 215 | # define INCL_DOS
|
---|
| 216 | # define INCL_DOSERRORS
|
---|
| 217 | # define INCL_NOPMAPI
|
---|
| 218 | # include <os2.h>
|
---|
| 219 | #endif
|
---|
| 220 |
|
---|
| 221 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
|
---|
| 222 | /* make sure that the reentrant (gethostbyaddr_r etc)
|
---|
| 223 | functions are declared correctly if compiling with
|
---|
| 224 | MIPSPro 7.x in ANSI C mode (default) */
|
---|
| 225 |
|
---|
| 226 | /* XXX Using _SGIAPI is the wrong thing,
|
---|
| 227 | but I don't know what the right thing is. */
|
---|
| 228 | #undef _SGIAPI /* to avoid warning */
|
---|
| 229 | #define _SGIAPI 1
|
---|
| 230 |
|
---|
| 231 | #undef _XOPEN_SOURCE
|
---|
| 232 | #include <sys/socket.h>
|
---|
| 233 | #include <sys/types.h>
|
---|
| 234 | #include <netinet/in.h>
|
---|
| 235 | #ifdef _SS_ALIGNSIZE
|
---|
| 236 | #define HAVE_GETADDRINFO 1
|
---|
| 237 | #define HAVE_GETNAMEINFO 1
|
---|
| 238 | #endif
|
---|
| 239 |
|
---|
| 240 | #define HAVE_INET_PTON
|
---|
| 241 | #include <netdb.h>
|
---|
| 242 | #endif
|
---|
| 243 |
|
---|
| 244 | /* Irix 6.5 fails to define this variable at all. This is needed
|
---|
| 245 | for both GCC and SGI's compiler. I'd say that the SGI headers
|
---|
| 246 | are just busted. Same thing for Solaris. */
|
---|
| 247 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
|
---|
| 248 | #define INET_ADDRSTRLEN 16
|
---|
| 249 | #endif
|
---|
| 250 |
|
---|
| 251 | /* Generic includes */
|
---|
| 252 | #ifdef HAVE_SYS_TYPES_H
|
---|
| 253 | #include <sys/types.h>
|
---|
| 254 | #endif
|
---|
| 255 |
|
---|
| 256 | /* Generic socket object definitions and includes */
|
---|
| 257 | #define PySocket_BUILDING_SOCKET
|
---|
| 258 | #include "socketmodule.h"
|
---|
| 259 |
|
---|
| 260 | /* Addressing includes */
|
---|
| 261 |
|
---|
| 262 | #ifndef MS_WINDOWS
|
---|
| 263 |
|
---|
| 264 | /* Non-MS WINDOWS includes */
|
---|
| 265 | # include <netdb.h>
|
---|
| 266 |
|
---|
| 267 | /* Headers needed for inet_ntoa() and inet_addr() */
|
---|
| 268 | # ifdef __BEOS__
|
---|
| 269 | # include <net/netdb.h>
|
---|
| 270 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP)
|
---|
| 271 | # include <netdb.h>
|
---|
| 272 | typedef size_t socklen_t;
|
---|
| 273 | # else
|
---|
| 274 | # include <arpa/inet.h>
|
---|
| 275 | # endif
|
---|
| 276 |
|
---|
| 277 | # ifndef RISCOS
|
---|
| 278 | # include <fcntl.h>
|
---|
| 279 | # else
|
---|
| 280 | # include <sys/ioctl.h>
|
---|
| 281 | # include <socklib.h>
|
---|
| 282 | # define NO_DUP
|
---|
| 283 | int h_errno; /* not used */
|
---|
| 284 | # define INET_ADDRSTRLEN 16
|
---|
| 285 | # endif
|
---|
| 286 |
|
---|
| 287 | #else
|
---|
| 288 |
|
---|
| 289 | /* MS_WINDOWS includes */
|
---|
| 290 | # ifdef HAVE_FCNTL_H
|
---|
| 291 | # include <fcntl.h>
|
---|
| 292 | # endif
|
---|
| 293 |
|
---|
| 294 | #endif
|
---|
| 295 |
|
---|
| 296 | #include <stddef.h>
|
---|
| 297 |
|
---|
| 298 | #ifndef offsetof
|
---|
[391] | 299 | # define offsetof(type, member) ((size_t)(&((type *)0)->member))
|
---|
[2] | 300 | #endif
|
---|
| 301 |
|
---|
| 302 | #ifndef O_NONBLOCK
|
---|
| 303 | # define O_NONBLOCK O_NDELAY
|
---|
| 304 | #endif
|
---|
| 305 |
|
---|
| 306 | /* include Python's addrinfo.h unless it causes trouble */
|
---|
| 307 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
|
---|
| 308 | /* Do not include addinfo.h on some newer IRIX versions.
|
---|
| 309 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
|
---|
| 310 | * for example, but not by 6.5.10.
|
---|
| 311 | */
|
---|
| 312 | #elif defined(_MSC_VER) && _MSC_VER>1201
|
---|
| 313 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
|
---|
| 314 | * EAI_* constants are defined in (the already included) ws2tcpip.h.
|
---|
| 315 | */
|
---|
| 316 | #else
|
---|
| 317 | # include "addrinfo.h"
|
---|
| 318 | #endif
|
---|
| 319 |
|
---|
| 320 | #ifndef HAVE_INET_PTON
|
---|
| 321 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
|
---|
| 322 | int inet_pton(int af, const char *src, void *dst);
|
---|
| 323 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
|
---|
| 324 | #endif
|
---|
| 325 | #endif
|
---|
| 326 |
|
---|
| 327 | #ifdef __APPLE__
|
---|
| 328 | /* On OS X, getaddrinfo returns no error indication of lookup
|
---|
| 329 | failure, so we must use the emulation instead of the libinfo
|
---|
| 330 | implementation. Unfortunately, performing an autoconf test
|
---|
| 331 | for this bug would require DNS access for the machine performing
|
---|
| 332 | the configuration, which is not acceptable. Therefore, we
|
---|
| 333 | determine the bug just by checking for __APPLE__. If this bug
|
---|
| 334 | gets ever fixed, perhaps checking for sys/version.h would be
|
---|
| 335 | appropriate, which is 10/0 on the system with the bug. */
|
---|
| 336 | #ifndef HAVE_GETNAMEINFO
|
---|
| 337 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could
|
---|
| 338 | Find to check for Jaguar is that it has getnameinfo(), which
|
---|
| 339 | older releases don't have */
|
---|
| 340 | #undef HAVE_GETADDRINFO
|
---|
| 341 | #endif
|
---|
| 342 |
|
---|
| 343 | #ifdef HAVE_INET_ATON
|
---|
| 344 | #define USE_INET_ATON_WEAKLINK
|
---|
| 345 | #endif
|
---|
| 346 |
|
---|
| 347 | #endif
|
---|
| 348 |
|
---|
| 349 | /* I know this is a bad practice, but it is the easiest... */
|
---|
| 350 | #if !defined(HAVE_GETADDRINFO)
|
---|
| 351 | /* avoid clashes with the C library definition of the symbol. */
|
---|
| 352 | #define getaddrinfo fake_getaddrinfo
|
---|
| 353 | #define gai_strerror fake_gai_strerror
|
---|
| 354 | #define freeaddrinfo fake_freeaddrinfo
|
---|
| 355 | #include "getaddrinfo.c"
|
---|
| 356 | #endif
|
---|
| 357 | #if !defined(HAVE_GETNAMEINFO)
|
---|
| 358 | #define getnameinfo fake_getnameinfo
|
---|
| 359 | #include "getnameinfo.c"
|
---|
| 360 | #endif
|
---|
| 361 |
|
---|
| 362 | #if defined(MS_WINDOWS) || defined(__BEOS__)
|
---|
| 363 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
|
---|
| 364 | /* seem to be a few differences in the API */
|
---|
| 365 | #define SOCKETCLOSE closesocket
|
---|
| 366 | #define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
|
---|
| 367 | #endif
|
---|
| 368 |
|
---|
| 369 | #ifdef MS_WIN32
|
---|
| 370 | #define EAFNOSUPPORT WSAEAFNOSUPPORT
|
---|
| 371 | #define snprintf _snprintf
|
---|
| 372 | #endif
|
---|
| 373 |
|
---|
| 374 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
| 375 | #define SOCKETCLOSE soclose
|
---|
| 376 | #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
|
---|
| 377 | #endif
|
---|
| 378 |
|
---|
| 379 | #ifndef SOCKETCLOSE
|
---|
| 380 | #define SOCKETCLOSE close
|
---|
| 381 | #endif
|
---|
| 382 |
|
---|
[391] | 383 | #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
|
---|
[2] | 384 | #define USE_BLUETOOTH 1
|
---|
| 385 | #if defined(__FreeBSD__)
|
---|
| 386 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
|
---|
| 387 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
|
---|
| 388 | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI
|
---|
| 389 | #define SOL_HCI SOL_HCI_RAW
|
---|
| 390 | #define HCI_FILTER SO_HCI_RAW_FILTER
|
---|
| 391 | #define sockaddr_l2 sockaddr_l2cap
|
---|
| 392 | #define sockaddr_rc sockaddr_rfcomm
|
---|
| 393 | #define hci_dev hci_node
|
---|
| 394 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
|
---|
| 395 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
|
---|
| 396 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
|
---|
[391] | 397 | #elif defined(__NetBSD__) || defined(__DragonFly__)
|
---|
[2] | 398 | #define sockaddr_l2 sockaddr_bt
|
---|
| 399 | #define sockaddr_rc sockaddr_bt
|
---|
| 400 | #define sockaddr_hci sockaddr_bt
|
---|
| 401 | #define sockaddr_sco sockaddr_bt
|
---|
[391] | 402 | #define SOL_HCI BTPROTO_HCI
|
---|
| 403 | #define HCI_DATA_DIR SO_HCI_DIRECTION
|
---|
[2] | 404 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
|
---|
| 405 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
|
---|
| 406 | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
|
---|
| 407 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
|
---|
| 408 | #else
|
---|
| 409 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
|
---|
| 410 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
|
---|
| 411 | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
|
---|
| 412 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
|
---|
| 413 | #endif
|
---|
| 414 | #endif
|
---|
| 415 |
|
---|
| 416 | #ifdef __VMS
|
---|
| 417 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */
|
---|
| 418 | #define SEGMENT_SIZE (32 * 1024 -1)
|
---|
| 419 | #endif
|
---|
| 420 |
|
---|
[391] | 421 | #define SAS2SA(x) ((struct sockaddr *)(x))
|
---|
[2] | 422 |
|
---|
| 423 | /*
|
---|
| 424 | * Constants for getnameinfo()
|
---|
| 425 | */
|
---|
| 426 | #if !defined(NI_MAXHOST)
|
---|
| 427 | #define NI_MAXHOST 1025
|
---|
| 428 | #endif
|
---|
| 429 | #if !defined(NI_MAXSERV)
|
---|
| 430 | #define NI_MAXSERV 32
|
---|
| 431 | #endif
|
---|
| 432 |
|
---|
| 433 | /* XXX There's a problem here: *static* functions are not supposed to have
|
---|
| 434 | a Py prefix (or use CapitalizedWords). Later... */
|
---|
| 435 |
|
---|
| 436 | /* Global variable holding the exception type for errors detected
|
---|
| 437 | by this module (but not argument type or memory errors, etc.). */
|
---|
| 438 | static PyObject *socket_error;
|
---|
| 439 | static PyObject *socket_herror;
|
---|
| 440 | static PyObject *socket_gaierror;
|
---|
| 441 | static PyObject *socket_timeout;
|
---|
| 442 |
|
---|
| 443 | #ifdef RISCOS
|
---|
| 444 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
|
---|
| 445 | static int taskwindow;
|
---|
| 446 | #endif
|
---|
| 447 |
|
---|
| 448 | /* A forward reference to the socket type object.
|
---|
| 449 | The sock_type variable contains pointers to various functions,
|
---|
| 450 | some of which call new_sockobject(), which uses sock_type, so
|
---|
| 451 | there has to be a circular reference. */
|
---|
| 452 | static PyTypeObject sock_type;
|
---|
| 453 |
|
---|
| 454 | #if defined(HAVE_POLL_H)
|
---|
| 455 | #include <poll.h>
|
---|
| 456 | #elif defined(HAVE_SYS_POLL_H)
|
---|
| 457 | #include <sys/poll.h>
|
---|
| 458 | #endif
|
---|
| 459 |
|
---|
[391] | 460 | #ifdef HAVE_POLL
|
---|
[2] | 461 | /* Instead of select(), we'll use poll() since poll() works on any fd. */
|
---|
| 462 | #define IS_SELECTABLE(s) 1
|
---|
| 463 | /* Can we call select() with this socket without a buffer overrun? */
|
---|
| 464 | #else
|
---|
[391] | 465 | /* If there's no timeout left, we don't have to call select, so it's a safe,
|
---|
| 466 | * little white lie. */
|
---|
| 467 | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0)
|
---|
[2] | 468 | #endif
|
---|
| 469 |
|
---|
| 470 | static PyObject*
|
---|
| 471 | select_error(void)
|
---|
| 472 | {
|
---|
[391] | 473 | PyErr_SetString(socket_error, "unable to select on socket");
|
---|
| 474 | return NULL;
|
---|
[2] | 475 | }
|
---|
| 476 |
|
---|
[391] | 477 | #ifdef MS_WINDOWS
|
---|
| 478 | #ifndef WSAEAGAIN
|
---|
| 479 | #define WSAEAGAIN WSAEWOULDBLOCK
|
---|
| 480 | #endif
|
---|
| 481 | #define CHECK_ERRNO(expected) \
|
---|
| 482 | (WSAGetLastError() == WSA ## expected)
|
---|
| 483 | #else
|
---|
| 484 | #define CHECK_ERRNO(expected) \
|
---|
| 485 | (errno == expected)
|
---|
| 486 | #endif
|
---|
| 487 |
|
---|
[2] | 488 | /* Convenience function to raise an error according to errno
|
---|
| 489 | and return a NULL pointer from a function. */
|
---|
| 490 |
|
---|
| 491 | static PyObject *
|
---|
| 492 | set_error(void)
|
---|
| 493 | {
|
---|
| 494 | #ifdef MS_WINDOWS
|
---|
[391] | 495 | int err_no = WSAGetLastError();
|
---|
| 496 | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
|
---|
| 497 | recognizes the error codes used by both GetLastError() and
|
---|
| 498 | WSAGetLastError */
|
---|
| 499 | if (err_no)
|
---|
| 500 | return PyErr_SetExcFromWindowsErr(socket_error, err_no);
|
---|
[2] | 501 | #endif
|
---|
| 502 |
|
---|
| 503 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
[391] | 504 | if (sock_errno() != NO_ERROR) {
|
---|
| 505 | APIRET rc;
|
---|
| 506 | ULONG msglen;
|
---|
| 507 | char outbuf[100];
|
---|
| 508 | int myerrorcode = sock_errno();
|
---|
[2] | 509 |
|
---|
[391] | 510 | /* Retrieve socket-related error message from MPTN.MSG file */
|
---|
| 511 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
|
---|
| 512 | myerrorcode - SOCBASEERR + 26,
|
---|
| 513 | "mptn.msg",
|
---|
| 514 | &msglen);
|
---|
| 515 | if (rc == NO_ERROR) {
|
---|
| 516 | PyObject *v;
|
---|
[2] | 517 |
|
---|
[391] | 518 | /* OS/2 doesn't guarantee a terminator */
|
---|
| 519 | outbuf[msglen] = '\0';
|
---|
| 520 | if (strlen(outbuf) > 0) {
|
---|
| 521 | /* If non-empty msg, trim CRLF */
|
---|
| 522 | char *lastc = &outbuf[ strlen(outbuf)-1 ];
|
---|
| 523 | while (lastc > outbuf &&
|
---|
| 524 | isspace(Py_CHARMASK(*lastc))) {
|
---|
| 525 | /* Trim trailing whitespace (CRLF) */
|
---|
| 526 | *lastc-- = '\0';
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
| 529 | v = Py_BuildValue("(is)", myerrorcode, outbuf);
|
---|
| 530 | if (v != NULL) {
|
---|
| 531 | PyErr_SetObject(socket_error, v);
|
---|
| 532 | Py_DECREF(v);
|
---|
| 533 | }
|
---|
| 534 | return NULL;
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
[2] | 537 | #endif
|
---|
| 538 |
|
---|
| 539 | #if defined(RISCOS)
|
---|
[391] | 540 | if (_inet_error.errnum != NULL) {
|
---|
| 541 | PyObject *v;
|
---|
| 542 | v = Py_BuildValue("(is)", errno, _inet_err());
|
---|
| 543 | if (v != NULL) {
|
---|
| 544 | PyErr_SetObject(socket_error, v);
|
---|
| 545 | Py_DECREF(v);
|
---|
| 546 | }
|
---|
| 547 | return NULL;
|
---|
| 548 | }
|
---|
[2] | 549 | #endif
|
---|
| 550 |
|
---|
[391] | 551 | return PyErr_SetFromErrno(socket_error);
|
---|
[2] | 552 | }
|
---|
| 553 |
|
---|
| 554 |
|
---|
| 555 | static PyObject *
|
---|
| 556 | set_herror(int h_error)
|
---|
| 557 | {
|
---|
[391] | 558 | PyObject *v;
|
---|
[2] | 559 |
|
---|
| 560 | #ifdef HAVE_HSTRERROR
|
---|
[391] | 561 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
|
---|
[2] | 562 | #else
|
---|
[391] | 563 | v = Py_BuildValue("(is)", h_error, "host not found");
|
---|
[2] | 564 | #endif
|
---|
[391] | 565 | if (v != NULL) {
|
---|
| 566 | PyErr_SetObject(socket_herror, v);
|
---|
| 567 | Py_DECREF(v);
|
---|
| 568 | }
|
---|
[2] | 569 |
|
---|
[391] | 570 | return NULL;
|
---|
[2] | 571 | }
|
---|
| 572 |
|
---|
| 573 |
|
---|
| 574 | static PyObject *
|
---|
| 575 | set_gaierror(int error)
|
---|
| 576 | {
|
---|
[391] | 577 | PyObject *v;
|
---|
[2] | 578 |
|
---|
| 579 | #ifdef EAI_SYSTEM
|
---|
[391] | 580 | /* EAI_SYSTEM is not available on Windows XP. */
|
---|
| 581 | if (error == EAI_SYSTEM)
|
---|
| 582 | return set_error();
|
---|
[2] | 583 | #endif
|
---|
| 584 |
|
---|
| 585 | #ifdef HAVE_GAI_STRERROR
|
---|
[391] | 586 | v = Py_BuildValue("(is)", error, gai_strerror(error));
|
---|
[2] | 587 | #else
|
---|
[391] | 588 | v = Py_BuildValue("(is)", error, "getaddrinfo failed");
|
---|
[2] | 589 | #endif
|
---|
[391] | 590 | if (v != NULL) {
|
---|
| 591 | PyErr_SetObject(socket_gaierror, v);
|
---|
| 592 | Py_DECREF(v);
|
---|
| 593 | }
|
---|
[2] | 594 |
|
---|
[391] | 595 | return NULL;
|
---|
[2] | 596 | }
|
---|
| 597 |
|
---|
| 598 | #ifdef __VMS
|
---|
| 599 | /* Function to send in segments */
|
---|
| 600 | static int
|
---|
| 601 | sendsegmented(int sock_fd, char *buf, int len, int flags)
|
---|
| 602 | {
|
---|
[391] | 603 | int n = 0;
|
---|
| 604 | int remaining = len;
|
---|
[2] | 605 |
|
---|
[391] | 606 | while (remaining > 0) {
|
---|
| 607 | unsigned int segment;
|
---|
[2] | 608 |
|
---|
[391] | 609 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
|
---|
| 610 | n = send(sock_fd, buf, segment, flags);
|
---|
| 611 | if (n < 0) {
|
---|
| 612 | return n;
|
---|
| 613 | }
|
---|
| 614 | remaining -= segment;
|
---|
| 615 | buf += segment;
|
---|
| 616 | } /* end while */
|
---|
[2] | 617 |
|
---|
[391] | 618 | return len;
|
---|
[2] | 619 | }
|
---|
| 620 | #endif
|
---|
| 621 |
|
---|
| 622 | /* Function to perform the setting of socket blocking mode
|
---|
| 623 | internally. block = (1 | 0). */
|
---|
| 624 | static int
|
---|
| 625 | internal_setblocking(PySocketSockObject *s, int block)
|
---|
| 626 | {
|
---|
| 627 | #ifndef RISCOS
|
---|
| 628 | #ifndef MS_WINDOWS
|
---|
[391] | 629 | int delay_flag;
|
---|
[2] | 630 | #endif
|
---|
| 631 | #endif
|
---|
| 632 |
|
---|
[391] | 633 | Py_BEGIN_ALLOW_THREADS
|
---|
[2] | 634 | #ifdef __BEOS__
|
---|
[391] | 635 | block = !block;
|
---|
| 636 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
|
---|
| 637 | (void *)(&block), sizeof(int));
|
---|
[2] | 638 | #else
|
---|
| 639 | #ifndef RISCOS
|
---|
| 640 | #ifndef MS_WINDOWS
|
---|
| 641 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
[391] | 642 | block = !block;
|
---|
| 643 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
|
---|
[2] | 644 | #elif defined(__VMS)
|
---|
[391] | 645 | block = !block;
|
---|
| 646 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
|
---|
[2] | 647 | #else /* !PYOS_OS2 && !__VMS */
|
---|
[391] | 648 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
|
---|
| 649 | if (block)
|
---|
| 650 | delay_flag &= (~O_NONBLOCK);
|
---|
| 651 | else
|
---|
| 652 | delay_flag |= O_NONBLOCK;
|
---|
| 653 | fcntl(s->sock_fd, F_SETFL, delay_flag);
|
---|
[2] | 654 | #endif /* !PYOS_OS2 */
|
---|
| 655 | #else /* MS_WINDOWS */
|
---|
[391] | 656 | block = !block;
|
---|
| 657 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
|
---|
[2] | 658 | #endif /* MS_WINDOWS */
|
---|
| 659 | #else /* RISCOS */
|
---|
[391] | 660 | block = !block;
|
---|
| 661 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
|
---|
[2] | 662 | #endif /* RISCOS */
|
---|
| 663 | #endif /* __BEOS__ */
|
---|
[391] | 664 | Py_END_ALLOW_THREADS
|
---|
[2] | 665 |
|
---|
[391] | 666 | /* Since these don't return anything */
|
---|
| 667 | return 1;
|
---|
[2] | 668 | }
|
---|
| 669 |
|
---|
| 670 | /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
|
---|
| 671 | The argument writing indicates the direction.
|
---|
| 672 | This does not raise an exception; we'll let our caller do that
|
---|
| 673 | after they've reacquired the interpreter lock.
|
---|
| 674 | Returns 1 on timeout, -1 on error, 0 otherwise. */
|
---|
| 675 | static int
|
---|
[391] | 676 | internal_select_ex(PySocketSockObject *s, int writing, double interval)
|
---|
[2] | 677 | {
|
---|
[391] | 678 | int n;
|
---|
[2] | 679 |
|
---|
[391] | 680 | /* Nothing to do unless we're in timeout mode (not non-blocking) */
|
---|
| 681 | if (s->sock_timeout <= 0.0)
|
---|
| 682 | return 0;
|
---|
[2] | 683 |
|
---|
[391] | 684 | /* Guard against closed socket */
|
---|
| 685 | if (s->sock_fd < 0)
|
---|
| 686 | return 0;
|
---|
[2] | 687 |
|
---|
[391] | 688 | /* Handling this condition here simplifies the select loops */
|
---|
| 689 | if (interval < 0.0)
|
---|
| 690 | return 1;
|
---|
| 691 |
|
---|
| 692 | /* Prefer poll, if available, since you can poll() any fd
|
---|
| 693 | * which can't be done with select(). */
|
---|
[2] | 694 | #ifdef HAVE_POLL
|
---|
[391] | 695 | {
|
---|
| 696 | struct pollfd pollfd;
|
---|
| 697 | int timeout;
|
---|
[2] | 698 |
|
---|
[391] | 699 | pollfd.fd = s->sock_fd;
|
---|
| 700 | pollfd.events = writing ? POLLOUT : POLLIN;
|
---|
[2] | 701 |
|
---|
[391] | 702 | /* s->sock_timeout is in seconds, timeout in ms */
|
---|
| 703 | timeout = (int)(interval * 1000 + 0.5);
|
---|
| 704 | n = poll(&pollfd, 1, timeout);
|
---|
| 705 | }
|
---|
[2] | 706 | #else
|
---|
[391] | 707 | {
|
---|
| 708 | /* Construct the arguments to select */
|
---|
| 709 | fd_set fds;
|
---|
| 710 | struct timeval tv;
|
---|
| 711 | tv.tv_sec = (int)interval;
|
---|
| 712 | tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6);
|
---|
| 713 | FD_ZERO(&fds);
|
---|
| 714 | FD_SET(s->sock_fd, &fds);
|
---|
[2] | 715 |
|
---|
[391] | 716 | /* See if the socket is ready */
|
---|
| 717 | if (writing)
|
---|
| 718 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
|
---|
| 719 | else
|
---|
| 720 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
|
---|
| 721 | }
|
---|
[2] | 722 | #endif
|
---|
[391] | 723 |
|
---|
| 724 | if (n < 0)
|
---|
| 725 | return -1;
|
---|
| 726 | if (n == 0)
|
---|
| 727 | return 1;
|
---|
| 728 | return 0;
|
---|
[2] | 729 | }
|
---|
| 730 |
|
---|
[391] | 731 | static int
|
---|
| 732 | internal_select(PySocketSockObject *s, int writing)
|
---|
| 733 | {
|
---|
| 734 | return internal_select_ex(s, writing, s->sock_timeout);
|
---|
| 735 | }
|
---|
| 736 |
|
---|
| 737 | /*
|
---|
| 738 | Two macros for automatic retry of select() in case of false positives
|
---|
| 739 | (for example, select() could indicate a socket is ready for reading
|
---|
| 740 | but the data then discarded by the OS because of a wrong checksum).
|
---|
| 741 | Here is an example of use:
|
---|
| 742 |
|
---|
| 743 | BEGIN_SELECT_LOOP(s)
|
---|
| 744 | Py_BEGIN_ALLOW_THREADS
|
---|
| 745 | timeout = internal_select_ex(s, 0, interval);
|
---|
| 746 | if (!timeout)
|
---|
| 747 | outlen = recv(s->sock_fd, cbuf, len, flags);
|
---|
| 748 | Py_END_ALLOW_THREADS
|
---|
| 749 | if (timeout == 1) {
|
---|
| 750 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 751 | return -1;
|
---|
| 752 | }
|
---|
| 753 | END_SELECT_LOOP(s)
|
---|
| 754 | */
|
---|
| 755 | #define BEGIN_SELECT_LOOP(s) \
|
---|
| 756 | { \
|
---|
| 757 | double deadline, interval = s->sock_timeout; \
|
---|
| 758 | int has_timeout = s->sock_timeout > 0.0; \
|
---|
| 759 | if (has_timeout) { \
|
---|
| 760 | deadline = _PyTime_FloatTime() + s->sock_timeout; \
|
---|
| 761 | } \
|
---|
| 762 | while (1) { \
|
---|
| 763 | errno = 0;
|
---|
| 764 |
|
---|
| 765 | #define END_SELECT_LOOP(s) \
|
---|
| 766 | if (!has_timeout || \
|
---|
| 767 | (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
|
---|
| 768 | break; \
|
---|
| 769 | interval = deadline - _PyTime_FloatTime(); \
|
---|
| 770 | } \
|
---|
| 771 | }
|
---|
| 772 |
|
---|
[2] | 773 | /* Initialize a new socket object. */
|
---|
| 774 |
|
---|
| 775 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */
|
---|
| 776 |
|
---|
| 777 | PyMODINIT_FUNC
|
---|
| 778 | init_sockobject(PySocketSockObject *s,
|
---|
[391] | 779 | SOCKET_T fd, int family, int type, int proto)
|
---|
[2] | 780 | {
|
---|
| 781 | #ifdef RISCOS
|
---|
[391] | 782 | int block = 1;
|
---|
[2] | 783 | #endif
|
---|
[391] | 784 | s->sock_fd = fd;
|
---|
| 785 | s->sock_family = family;
|
---|
| 786 | s->sock_type = type;
|
---|
| 787 | s->sock_proto = proto;
|
---|
| 788 | s->sock_timeout = defaulttimeout;
|
---|
[2] | 789 |
|
---|
[391] | 790 | s->errorhandler = &set_error;
|
---|
[2] | 791 |
|
---|
[391] | 792 | if (defaulttimeout >= 0.0)
|
---|
| 793 | internal_setblocking(s, 0);
|
---|
[2] | 794 |
|
---|
| 795 | #ifdef RISCOS
|
---|
[391] | 796 | if (taskwindow)
|
---|
| 797 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
|
---|
[2] | 798 | #endif
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 |
|
---|
| 802 | /* Create a new socket object.
|
---|
| 803 | This just creates the object and initializes it.
|
---|
| 804 | If the creation fails, return NULL and set an exception (implicit
|
---|
| 805 | in NEWOBJ()). */
|
---|
| 806 |
|
---|
| 807 | static PySocketSockObject *
|
---|
| 808 | new_sockobject(SOCKET_T fd, int family, int type, int proto)
|
---|
| 809 | {
|
---|
[391] | 810 | PySocketSockObject *s;
|
---|
| 811 | s = (PySocketSockObject *)
|
---|
| 812 | PyType_GenericNew(&sock_type, NULL, NULL);
|
---|
| 813 | if (s != NULL)
|
---|
| 814 | init_sockobject(s, fd, family, type, proto);
|
---|
| 815 | return s;
|
---|
[2] | 816 | }
|
---|
| 817 |
|
---|
| 818 |
|
---|
| 819 | /* Lock to allow python interpreter to continue, but only allow one
|
---|
| 820 | thread to be in gethostbyname or getaddrinfo */
|
---|
| 821 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
|
---|
[391] | 822 | static PyThread_type_lock netdb_lock;
|
---|
[2] | 823 | #endif
|
---|
| 824 |
|
---|
| 825 |
|
---|
| 826 | /* Convert a string specifying a host name or one of a few symbolic
|
---|
| 827 | names to a numeric IP address. This usually calls gethostbyname()
|
---|
| 828 | to do the work; the names "" and "<broadcast>" are special.
|
---|
| 829 | Return the length (IPv4 should be 4 bytes), or negative if
|
---|
| 830 | an error occurred; then an exception is raised. */
|
---|
| 831 |
|
---|
| 832 | static int
|
---|
| 833 | setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
|
---|
| 834 | {
|
---|
[391] | 835 | struct addrinfo hints, *res;
|
---|
| 836 | int error;
|
---|
| 837 | int d1, d2, d3, d4;
|
---|
| 838 | char ch;
|
---|
[2] | 839 |
|
---|
[391] | 840 | memset((void *) addr_ret, '\0', sizeof(*addr_ret));
|
---|
| 841 | if (name[0] == '\0') {
|
---|
| 842 | int siz;
|
---|
| 843 | memset(&hints, 0, sizeof(hints));
|
---|
| 844 | hints.ai_family = af;
|
---|
| 845 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/
|
---|
| 846 | hints.ai_flags = AI_PASSIVE;
|
---|
| 847 | Py_BEGIN_ALLOW_THREADS
|
---|
| 848 | ACQUIRE_GETADDRINFO_LOCK
|
---|
| 849 | error = getaddrinfo(NULL, "0", &hints, &res);
|
---|
| 850 | Py_END_ALLOW_THREADS
|
---|
| 851 | /* We assume that those thread-unsafe getaddrinfo() versions
|
---|
| 852 | *are* safe regarding their return value, ie. that a
|
---|
| 853 | subsequent call to getaddrinfo() does not destroy the
|
---|
| 854 | outcome of the first call. */
|
---|
| 855 | RELEASE_GETADDRINFO_LOCK
|
---|
| 856 | if (error) {
|
---|
| 857 | set_gaierror(error);
|
---|
| 858 | return -1;
|
---|
| 859 | }
|
---|
| 860 | switch (res->ai_family) {
|
---|
| 861 | case AF_INET:
|
---|
| 862 | siz = 4;
|
---|
| 863 | break;
|
---|
[2] | 864 | #ifdef ENABLE_IPV6
|
---|
[391] | 865 | case AF_INET6:
|
---|
| 866 | siz = 16;
|
---|
| 867 | break;
|
---|
[2] | 868 | #endif
|
---|
[391] | 869 | default:
|
---|
| 870 | freeaddrinfo(res);
|
---|
| 871 | PyErr_SetString(socket_error,
|
---|
| 872 | "unsupported address family");
|
---|
| 873 | return -1;
|
---|
| 874 | }
|
---|
| 875 | if (res->ai_next) {
|
---|
| 876 | freeaddrinfo(res);
|
---|
| 877 | PyErr_SetString(socket_error,
|
---|
| 878 | "wildcard resolved to multiple address");
|
---|
| 879 | return -1;
|
---|
| 880 | }
|
---|
| 881 | if (res->ai_addrlen < addr_ret_size)
|
---|
| 882 | addr_ret_size = res->ai_addrlen;
|
---|
| 883 | memcpy(addr_ret, res->ai_addr, addr_ret_size);
|
---|
| 884 | freeaddrinfo(res);
|
---|
| 885 | return siz;
|
---|
| 886 | }
|
---|
| 887 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
|
---|
| 888 | struct sockaddr_in *sin;
|
---|
| 889 | if (af != AF_INET && af != AF_UNSPEC) {
|
---|
| 890 | PyErr_SetString(socket_error,
|
---|
| 891 | "address family mismatched");
|
---|
| 892 | return -1;
|
---|
| 893 | }
|
---|
| 894 | sin = (struct sockaddr_in *)addr_ret;
|
---|
| 895 | memset((void *) sin, '\0', sizeof(*sin));
|
---|
| 896 | sin->sin_family = AF_INET;
|
---|
[2] | 897 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
[391] | 898 | sin->sin_len = sizeof(*sin);
|
---|
[2] | 899 | #endif
|
---|
[391] | 900 | sin->sin_addr.s_addr = INADDR_BROADCAST;
|
---|
| 901 | return sizeof(sin->sin_addr);
|
---|
| 902 | }
|
---|
| 903 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
|
---|
| 904 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
|
---|
| 905 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
|
---|
| 906 | struct sockaddr_in *sin;
|
---|
| 907 | sin = (struct sockaddr_in *)addr_ret;
|
---|
| 908 | sin->sin_addr.s_addr = htonl(
|
---|
| 909 | ((long) d1 << 24) | ((long) d2 << 16) |
|
---|
| 910 | ((long) d3 << 8) | ((long) d4 << 0));
|
---|
| 911 | sin->sin_family = AF_INET;
|
---|
[2] | 912 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
[391] | 913 | sin->sin_len = sizeof(*sin);
|
---|
[2] | 914 | #endif
|
---|
[391] | 915 | return 4;
|
---|
| 916 | }
|
---|
| 917 | memset(&hints, 0, sizeof(hints));
|
---|
| 918 | hints.ai_family = af;
|
---|
| 919 | Py_BEGIN_ALLOW_THREADS
|
---|
| 920 | ACQUIRE_GETADDRINFO_LOCK
|
---|
| 921 | error = getaddrinfo(name, NULL, &hints, &res);
|
---|
[2] | 922 | #if defined(__digital__) && defined(__unix__)
|
---|
[391] | 923 | if (error == EAI_NONAME && af == AF_UNSPEC) {
|
---|
| 924 | /* On Tru64 V5.1, numeric-to-addr conversion fails
|
---|
| 925 | if no address family is given. Assume IPv4 for now.*/
|
---|
| 926 | hints.ai_family = AF_INET;
|
---|
| 927 | error = getaddrinfo(name, NULL, &hints, &res);
|
---|
| 928 | }
|
---|
[2] | 929 | #endif
|
---|
[391] | 930 | Py_END_ALLOW_THREADS
|
---|
| 931 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
|
---|
| 932 | if (error) {
|
---|
| 933 | set_gaierror(error);
|
---|
| 934 | return -1;
|
---|
| 935 | }
|
---|
| 936 | if (res->ai_addrlen < addr_ret_size)
|
---|
| 937 | addr_ret_size = res->ai_addrlen;
|
---|
| 938 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
|
---|
| 939 | freeaddrinfo(res);
|
---|
| 940 | switch (addr_ret->sa_family) {
|
---|
| 941 | case AF_INET:
|
---|
| 942 | return 4;
|
---|
[2] | 943 | #ifdef ENABLE_IPV6
|
---|
[391] | 944 | case AF_INET6:
|
---|
| 945 | return 16;
|
---|
[2] | 946 | #endif
|
---|
[391] | 947 | default:
|
---|
| 948 | PyErr_SetString(socket_error, "unknown address family");
|
---|
| 949 | return -1;
|
---|
| 950 | }
|
---|
[2] | 951 | }
|
---|
| 952 |
|
---|
| 953 |
|
---|
| 954 | /* Create a string object representing an IP address.
|
---|
| 955 | This is always a string of the form 'dd.dd.dd.dd' (with variable
|
---|
| 956 | size numbers). */
|
---|
| 957 |
|
---|
| 958 | static PyObject *
|
---|
| 959 | makeipaddr(struct sockaddr *addr, int addrlen)
|
---|
| 960 | {
|
---|
[391] | 961 | char buf[NI_MAXHOST];
|
---|
| 962 | int error;
|
---|
[2] | 963 |
|
---|
[391] | 964 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
|
---|
| 965 | NI_NUMERICHOST);
|
---|
| 966 | if (error) {
|
---|
| 967 | set_gaierror(error);
|
---|
| 968 | return NULL;
|
---|
| 969 | }
|
---|
| 970 | return PyString_FromString(buf);
|
---|
[2] | 971 | }
|
---|
| 972 |
|
---|
| 973 |
|
---|
| 974 | #ifdef USE_BLUETOOTH
|
---|
| 975 | /* Convert a string representation of a Bluetooth address into a numeric
|
---|
| 976 | address. Returns the length (6), or raises an exception and returns -1 if
|
---|
| 977 | an error occurred. */
|
---|
| 978 |
|
---|
| 979 | static int
|
---|
| 980 | setbdaddr(char *name, bdaddr_t *bdaddr)
|
---|
| 981 | {
|
---|
[391] | 982 | unsigned int b0, b1, b2, b3, b4, b5;
|
---|
| 983 | char ch;
|
---|
| 984 | int n;
|
---|
[2] | 985 |
|
---|
[391] | 986 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
|
---|
| 987 | &b5, &b4, &b3, &b2, &b1, &b0, &ch);
|
---|
| 988 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
|
---|
| 989 | bdaddr->b[0] = b0;
|
---|
| 990 | bdaddr->b[1] = b1;
|
---|
| 991 | bdaddr->b[2] = b2;
|
---|
| 992 | bdaddr->b[3] = b3;
|
---|
| 993 | bdaddr->b[4] = b4;
|
---|
| 994 | bdaddr->b[5] = b5;
|
---|
| 995 | return 6;
|
---|
| 996 | } else {
|
---|
| 997 | PyErr_SetString(socket_error, "bad bluetooth address");
|
---|
| 998 | return -1;
|
---|
| 999 | }
|
---|
[2] | 1000 | }
|
---|
| 1001 |
|
---|
| 1002 | /* Create a string representation of the Bluetooth address. This is always a
|
---|
| 1003 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
|
---|
| 1004 | value (zero padded if necessary). */
|
---|
| 1005 |
|
---|
| 1006 | static PyObject *
|
---|
| 1007 | makebdaddr(bdaddr_t *bdaddr)
|
---|
| 1008 | {
|
---|
[391] | 1009 | char buf[(6 * 2) + 5 + 1];
|
---|
[2] | 1010 |
|
---|
[391] | 1011 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
|
---|
| 1012 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
|
---|
| 1013 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
|
---|
| 1014 | return PyString_FromString(buf);
|
---|
[2] | 1015 | }
|
---|
| 1016 | #endif
|
---|
| 1017 |
|
---|
| 1018 |
|
---|
| 1019 | /* Create an object representing the given socket address,
|
---|
| 1020 | suitable for passing it back to bind(), connect() etc.
|
---|
| 1021 | The family field of the sockaddr structure is inspected
|
---|
| 1022 | to determine what kind of address it really is. */
|
---|
| 1023 |
|
---|
| 1024 | /*ARGSUSED*/
|
---|
| 1025 | static PyObject *
|
---|
| 1026 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
|
---|
| 1027 | {
|
---|
[391] | 1028 | if (addrlen == 0) {
|
---|
| 1029 | /* No address -- may be recvfrom() from known socket */
|
---|
| 1030 | Py_INCREF(Py_None);
|
---|
| 1031 | return Py_None;
|
---|
| 1032 | }
|
---|
[2] | 1033 |
|
---|
| 1034 | #ifdef __BEOS__
|
---|
[391] | 1035 | /* XXX: BeOS version of accept() doesn't set family correctly */
|
---|
| 1036 | addr->sa_family = AF_INET;
|
---|
[2] | 1037 | #endif
|
---|
| 1038 |
|
---|
[391] | 1039 | switch (addr->sa_family) {
|
---|
[2] | 1040 |
|
---|
[391] | 1041 | case AF_INET:
|
---|
| 1042 | {
|
---|
| 1043 | struct sockaddr_in *a;
|
---|
| 1044 | PyObject *addrobj = makeipaddr(addr, sizeof(*a));
|
---|
| 1045 | PyObject *ret = NULL;
|
---|
| 1046 | if (addrobj) {
|
---|
| 1047 | a = (struct sockaddr_in *)addr;
|
---|
| 1048 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
|
---|
| 1049 | Py_DECREF(addrobj);
|
---|
| 1050 | }
|
---|
| 1051 | return ret;
|
---|
| 1052 | }
|
---|
[2] | 1053 |
|
---|
| 1054 | #if defined(AF_UNIX)
|
---|
[391] | 1055 | case AF_UNIX:
|
---|
| 1056 | {
|
---|
| 1057 | struct sockaddr_un *a = (struct sockaddr_un *) addr;
|
---|
[2] | 1058 | #ifdef linux
|
---|
[391] | 1059 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */
|
---|
| 1060 | addrlen -= offsetof(struct sockaddr_un, sun_path);
|
---|
| 1061 | return PyString_FromStringAndSize(a->sun_path,
|
---|
| 1062 | addrlen);
|
---|
| 1063 | }
|
---|
| 1064 | else
|
---|
[2] | 1065 | #endif /* linux */
|
---|
[391] | 1066 | {
|
---|
| 1067 | /* regular NULL-terminated string */
|
---|
| 1068 | return PyString_FromString(a->sun_path);
|
---|
| 1069 | }
|
---|
| 1070 | }
|
---|
[2] | 1071 | #endif /* AF_UNIX */
|
---|
| 1072 |
|
---|
| 1073 | #if defined(AF_NETLINK)
|
---|
| 1074 | case AF_NETLINK:
|
---|
| 1075 | {
|
---|
[391] | 1076 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
|
---|
| 1077 | return Py_BuildValue("II", a->nl_pid, a->nl_groups);
|
---|
[2] | 1078 | }
|
---|
| 1079 | #endif /* AF_NETLINK */
|
---|
| 1080 |
|
---|
| 1081 | #ifdef ENABLE_IPV6
|
---|
[391] | 1082 | case AF_INET6:
|
---|
| 1083 | {
|
---|
| 1084 | struct sockaddr_in6 *a;
|
---|
| 1085 | PyObject *addrobj = makeipaddr(addr, sizeof(*a));
|
---|
| 1086 | PyObject *ret = NULL;
|
---|
| 1087 | if (addrobj) {
|
---|
| 1088 | a = (struct sockaddr_in6 *)addr;
|
---|
| 1089 | ret = Py_BuildValue("OiII",
|
---|
| 1090 | addrobj,
|
---|
| 1091 | ntohs(a->sin6_port),
|
---|
| 1092 | ntohl(a->sin6_flowinfo),
|
---|
| 1093 | a->sin6_scope_id);
|
---|
| 1094 | Py_DECREF(addrobj);
|
---|
| 1095 | }
|
---|
| 1096 | return ret;
|
---|
| 1097 | }
|
---|
[2] | 1098 | #endif
|
---|
| 1099 |
|
---|
| 1100 | #ifdef USE_BLUETOOTH
|
---|
[391] | 1101 | case AF_BLUETOOTH:
|
---|
| 1102 | switch (proto) {
|
---|
[2] | 1103 |
|
---|
[391] | 1104 | case BTPROTO_L2CAP:
|
---|
| 1105 | {
|
---|
| 1106 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
|
---|
| 1107 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
|
---|
| 1108 | PyObject *ret = NULL;
|
---|
| 1109 | if (addrobj) {
|
---|
| 1110 | ret = Py_BuildValue("Oi",
|
---|
| 1111 | addrobj,
|
---|
| 1112 | _BT_L2_MEMB(a, psm));
|
---|
| 1113 | Py_DECREF(addrobj);
|
---|
| 1114 | }
|
---|
| 1115 | return ret;
|
---|
| 1116 | }
|
---|
[2] | 1117 |
|
---|
[391] | 1118 | case BTPROTO_RFCOMM:
|
---|
| 1119 | {
|
---|
| 1120 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
|
---|
| 1121 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
|
---|
| 1122 | PyObject *ret = NULL;
|
---|
| 1123 | if (addrobj) {
|
---|
| 1124 | ret = Py_BuildValue("Oi",
|
---|
| 1125 | addrobj,
|
---|
| 1126 | _BT_RC_MEMB(a, channel));
|
---|
| 1127 | Py_DECREF(addrobj);
|
---|
| 1128 | }
|
---|
| 1129 | return ret;
|
---|
| 1130 | }
|
---|
[2] | 1131 |
|
---|
[391] | 1132 | case BTPROTO_HCI:
|
---|
| 1133 | {
|
---|
| 1134 | struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
|
---|
| 1135 | #if defined(__NetBSD__) || defined(__DragonFly__)
|
---|
| 1136 | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
|
---|
| 1137 | #else
|
---|
| 1138 | PyObject *ret = NULL;
|
---|
| 1139 | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
|
---|
| 1140 | return ret;
|
---|
| 1141 | #endif
|
---|
| 1142 | }
|
---|
[2] | 1143 |
|
---|
| 1144 | #if !defined(__FreeBSD__)
|
---|
[391] | 1145 | case BTPROTO_SCO:
|
---|
| 1146 | {
|
---|
| 1147 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
|
---|
| 1148 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
|
---|
| 1149 | }
|
---|
[2] | 1150 | #endif
|
---|
| 1151 |
|
---|
[391] | 1152 | default:
|
---|
| 1153 | PyErr_SetString(PyExc_ValueError,
|
---|
| 1154 | "Unknown Bluetooth protocol");
|
---|
| 1155 | return NULL;
|
---|
| 1156 | }
|
---|
[2] | 1157 | #endif
|
---|
| 1158 |
|
---|
[391] | 1159 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
|
---|
| 1160 | case AF_PACKET:
|
---|
| 1161 | {
|
---|
| 1162 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
|
---|
| 1163 | char *ifname = "";
|
---|
| 1164 | struct ifreq ifr;
|
---|
| 1165 | /* need to look up interface name give index */
|
---|
| 1166 | if (a->sll_ifindex) {
|
---|
| 1167 | ifr.ifr_ifindex = a->sll_ifindex;
|
---|
| 1168 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
|
---|
| 1169 | ifname = ifr.ifr_name;
|
---|
| 1170 | }
|
---|
| 1171 | return Py_BuildValue("shbhs#",
|
---|
| 1172 | ifname,
|
---|
| 1173 | ntohs(a->sll_protocol),
|
---|
| 1174 | a->sll_pkttype,
|
---|
| 1175 | a->sll_hatype,
|
---|
| 1176 | a->sll_addr,
|
---|
| 1177 | a->sll_halen);
|
---|
| 1178 | }
|
---|
[2] | 1179 | #endif
|
---|
| 1180 |
|
---|
| 1181 | #ifdef HAVE_LINUX_TIPC_H
|
---|
[391] | 1182 | case AF_TIPC:
|
---|
| 1183 | {
|
---|
| 1184 | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
|
---|
| 1185 | if (a->addrtype == TIPC_ADDR_NAMESEQ) {
|
---|
| 1186 | return Py_BuildValue("IIIII",
|
---|
| 1187 | a->addrtype,
|
---|
| 1188 | a->addr.nameseq.type,
|
---|
| 1189 | a->addr.nameseq.lower,
|
---|
| 1190 | a->addr.nameseq.upper,
|
---|
| 1191 | a->scope);
|
---|
| 1192 | } else if (a->addrtype == TIPC_ADDR_NAME) {
|
---|
| 1193 | return Py_BuildValue("IIIII",
|
---|
| 1194 | a->addrtype,
|
---|
| 1195 | a->addr.name.name.type,
|
---|
| 1196 | a->addr.name.name.instance,
|
---|
| 1197 | a->addr.name.name.instance,
|
---|
| 1198 | a->scope);
|
---|
| 1199 | } else if (a->addrtype == TIPC_ADDR_ID) {
|
---|
| 1200 | return Py_BuildValue("IIIII",
|
---|
| 1201 | a->addrtype,
|
---|
| 1202 | a->addr.id.node,
|
---|
| 1203 | a->addr.id.ref,
|
---|
| 1204 | 0,
|
---|
| 1205 | a->scope);
|
---|
| 1206 | } else {
|
---|
| 1207 | PyErr_SetString(PyExc_ValueError,
|
---|
| 1208 | "Invalid address type");
|
---|
| 1209 | return NULL;
|
---|
| 1210 | }
|
---|
| 1211 | }
|
---|
[2] | 1212 | #endif
|
---|
| 1213 |
|
---|
[391] | 1214 | /* More cases here... */
|
---|
[2] | 1215 |
|
---|
[391] | 1216 | default:
|
---|
| 1217 | /* If we don't know the address family, don't raise an
|
---|
| 1218 | exception -- return it as a tuple. */
|
---|
| 1219 | return Py_BuildValue("is#",
|
---|
| 1220 | addr->sa_family,
|
---|
| 1221 | addr->sa_data,
|
---|
| 1222 | sizeof(addr->sa_data));
|
---|
[2] | 1223 |
|
---|
[391] | 1224 | }
|
---|
[2] | 1225 | }
|
---|
| 1226 |
|
---|
| 1227 |
|
---|
| 1228 | /* Parse a socket address argument according to the socket object's
|
---|
| 1229 | address family. Return 1 if the address was in the proper format,
|
---|
| 1230 | 0 of not. The address is returned through addr_ret, its length
|
---|
| 1231 | through len_ret. */
|
---|
| 1232 |
|
---|
| 1233 | static int
|
---|
| 1234 | getsockaddrarg(PySocketSockObject *s, PyObject *args,
|
---|
[391] | 1235 | struct sockaddr *addr_ret, int *len_ret)
|
---|
[2] | 1236 | {
|
---|
[391] | 1237 | switch (s->sock_family) {
|
---|
[2] | 1238 |
|
---|
| 1239 | #if defined(AF_UNIX)
|
---|
[391] | 1240 | case AF_UNIX:
|
---|
| 1241 | {
|
---|
| 1242 | struct sockaddr_un* addr;
|
---|
| 1243 | char *path;
|
---|
| 1244 | int len;
|
---|
| 1245 | if (!PyArg_Parse(args, "t#", &path, &len))
|
---|
| 1246 | return 0;
|
---|
[2] | 1247 |
|
---|
[391] | 1248 | addr = (struct sockaddr_un*)addr_ret;
|
---|
[2] | 1249 | #ifdef linux
|
---|
[391] | 1250 | if (len > 0 && path[0] == 0) {
|
---|
| 1251 | /* Linux abstract namespace extension */
|
---|
| 1252 | if (len > sizeof addr->sun_path) {
|
---|
| 1253 | PyErr_SetString(socket_error,
|
---|
| 1254 | "AF_UNIX path too long");
|
---|
| 1255 | return 0;
|
---|
| 1256 | }
|
---|
| 1257 | }
|
---|
| 1258 | else
|
---|
[2] | 1259 | #endif /* linux */
|
---|
[391] | 1260 | {
|
---|
| 1261 | /* regular NULL-terminated string */
|
---|
| 1262 | if (len >= sizeof addr->sun_path) {
|
---|
| 1263 | PyErr_SetString(socket_error,
|
---|
| 1264 | "AF_UNIX path too long");
|
---|
| 1265 | return 0;
|
---|
| 1266 | }
|
---|
| 1267 | addr->sun_path[len] = 0;
|
---|
| 1268 | }
|
---|
| 1269 | addr->sun_family = s->sock_family;
|
---|
| 1270 | memcpy(addr->sun_path, path, len);
|
---|
[2] | 1271 | #if defined(PYOS_OS2)
|
---|
[391] | 1272 | *len_ret = sizeof(*addr);
|
---|
[2] | 1273 | #else
|
---|
[391] | 1274 | *len_ret = len + offsetof(struct sockaddr_un, sun_path);
|
---|
[2] | 1275 | #endif
|
---|
[391] | 1276 | return 1;
|
---|
| 1277 | }
|
---|
[2] | 1278 | #endif /* AF_UNIX */
|
---|
| 1279 |
|
---|
| 1280 | #if defined(AF_NETLINK)
|
---|
[391] | 1281 | case AF_NETLINK:
|
---|
| 1282 | {
|
---|
| 1283 | struct sockaddr_nl* addr;
|
---|
| 1284 | int pid, groups;
|
---|
| 1285 | addr = (struct sockaddr_nl *)addr_ret;
|
---|
| 1286 | if (!PyTuple_Check(args)) {
|
---|
| 1287 | PyErr_Format(
|
---|
| 1288 | PyExc_TypeError,
|
---|
| 1289 | "getsockaddrarg: "
|
---|
| 1290 | "AF_NETLINK address must be tuple, not %.500s",
|
---|
| 1291 | Py_TYPE(args)->tp_name);
|
---|
| 1292 | return 0;
|
---|
| 1293 | }
|
---|
| 1294 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
|
---|
| 1295 | return 0;
|
---|
| 1296 | addr->nl_family = AF_NETLINK;
|
---|
| 1297 | addr->nl_pid = pid;
|
---|
| 1298 | addr->nl_groups = groups;
|
---|
| 1299 | *len_ret = sizeof(*addr);
|
---|
| 1300 | return 1;
|
---|
| 1301 | }
|
---|
[2] | 1302 | #endif
|
---|
| 1303 |
|
---|
[391] | 1304 | case AF_INET:
|
---|
| 1305 | {
|
---|
| 1306 | struct sockaddr_in* addr;
|
---|
| 1307 | char *host;
|
---|
| 1308 | int port, result;
|
---|
| 1309 | if (!PyTuple_Check(args)) {
|
---|
| 1310 | PyErr_Format(
|
---|
| 1311 | PyExc_TypeError,
|
---|
| 1312 | "getsockaddrarg: "
|
---|
| 1313 | "AF_INET address must be tuple, not %.500s",
|
---|
| 1314 | Py_TYPE(args)->tp_name);
|
---|
| 1315 | return 0;
|
---|
| 1316 | }
|
---|
| 1317 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
|
---|
| 1318 | "idna", &host, &port))
|
---|
| 1319 | return 0;
|
---|
| 1320 | addr=(struct sockaddr_in*)addr_ret;
|
---|
| 1321 | result = setipaddr(host, (struct sockaddr *)addr,
|
---|
| 1322 | sizeof(*addr), AF_INET);
|
---|
| 1323 | PyMem_Free(host);
|
---|
| 1324 | if (result < 0)
|
---|
| 1325 | return 0;
|
---|
| 1326 | if (port < 0 || port > 0xffff) {
|
---|
| 1327 | PyErr_SetString(
|
---|
| 1328 | PyExc_OverflowError,
|
---|
| 1329 | "getsockaddrarg: port must be 0-65535.");
|
---|
| 1330 | return 0;
|
---|
| 1331 | }
|
---|
| 1332 | addr->sin_family = AF_INET;
|
---|
| 1333 | addr->sin_port = htons((short)port);
|
---|
| 1334 | *len_ret = sizeof *addr;
|
---|
| 1335 | return 1;
|
---|
| 1336 | }
|
---|
[2] | 1337 |
|
---|
| 1338 | #ifdef ENABLE_IPV6
|
---|
[391] | 1339 | case AF_INET6:
|
---|
| 1340 | {
|
---|
| 1341 | struct sockaddr_in6* addr;
|
---|
| 1342 | char *host;
|
---|
| 1343 | int port, result;
|
---|
| 1344 | unsigned int flowinfo, scope_id;
|
---|
| 1345 | flowinfo = scope_id = 0;
|
---|
| 1346 | if (!PyTuple_Check(args)) {
|
---|
| 1347 | PyErr_Format(
|
---|
| 1348 | PyExc_TypeError,
|
---|
| 1349 | "getsockaddrarg: "
|
---|
| 1350 | "AF_INET6 address must be tuple, not %.500s",
|
---|
| 1351 | Py_TYPE(args)->tp_name);
|
---|
| 1352 | return 0;
|
---|
| 1353 | }
|
---|
| 1354 | if (!PyArg_ParseTuple(args, "eti|II",
|
---|
| 1355 | "idna", &host, &port, &flowinfo,
|
---|
| 1356 | &scope_id)) {
|
---|
| 1357 | return 0;
|
---|
| 1358 | }
|
---|
| 1359 | addr = (struct sockaddr_in6*)addr_ret;
|
---|
| 1360 | result = setipaddr(host, (struct sockaddr *)addr,
|
---|
| 1361 | sizeof(*addr), AF_INET6);
|
---|
| 1362 | PyMem_Free(host);
|
---|
| 1363 | if (result < 0)
|
---|
| 1364 | return 0;
|
---|
| 1365 | if (port < 0 || port > 0xffff) {
|
---|
| 1366 | PyErr_SetString(
|
---|
| 1367 | PyExc_OverflowError,
|
---|
| 1368 | "getsockaddrarg: port must be 0-65535.");
|
---|
| 1369 | return 0;
|
---|
| 1370 | }
|
---|
| 1371 | if (flowinfo > 0xfffff) {
|
---|
| 1372 | PyErr_SetString(
|
---|
| 1373 | PyExc_OverflowError,
|
---|
| 1374 | "getsockaddrarg: flowinfo must be 0-1048575.");
|
---|
| 1375 | return 0;
|
---|
| 1376 | }
|
---|
| 1377 | addr->sin6_family = s->sock_family;
|
---|
| 1378 | addr->sin6_port = htons((short)port);
|
---|
| 1379 | addr->sin6_flowinfo = htonl(flowinfo);
|
---|
| 1380 | addr->sin6_scope_id = scope_id;
|
---|
| 1381 | *len_ret = sizeof *addr;
|
---|
| 1382 | return 1;
|
---|
| 1383 | }
|
---|
[2] | 1384 | #endif
|
---|
| 1385 |
|
---|
| 1386 | #ifdef USE_BLUETOOTH
|
---|
[391] | 1387 | case AF_BLUETOOTH:
|
---|
| 1388 | {
|
---|
| 1389 | switch (s->sock_proto) {
|
---|
| 1390 | case BTPROTO_L2CAP:
|
---|
| 1391 | {
|
---|
| 1392 | struct sockaddr_l2 *addr;
|
---|
| 1393 | char *straddr;
|
---|
[2] | 1394 |
|
---|
[391] | 1395 | addr = (struct sockaddr_l2 *)addr_ret;
|
---|
| 1396 | memset(addr, 0, sizeof(struct sockaddr_l2));
|
---|
| 1397 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
| 1398 | if (!PyArg_ParseTuple(args, "si", &straddr,
|
---|
| 1399 | &_BT_L2_MEMB(addr, psm))) {
|
---|
| 1400 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
| 1401 | "wrong format");
|
---|
| 1402 | return 0;
|
---|
| 1403 | }
|
---|
| 1404 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
|
---|
| 1405 | return 0;
|
---|
[2] | 1406 |
|
---|
[391] | 1407 | *len_ret = sizeof *addr;
|
---|
| 1408 | return 1;
|
---|
| 1409 | }
|
---|
| 1410 | case BTPROTO_RFCOMM:
|
---|
| 1411 | {
|
---|
| 1412 | struct sockaddr_rc *addr;
|
---|
| 1413 | char *straddr;
|
---|
[2] | 1414 |
|
---|
[391] | 1415 | addr = (struct sockaddr_rc *)addr_ret;
|
---|
| 1416 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
| 1417 | if (!PyArg_ParseTuple(args, "si", &straddr,
|
---|
| 1418 | &_BT_RC_MEMB(addr, channel))) {
|
---|
| 1419 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
| 1420 | "wrong format");
|
---|
| 1421 | return 0;
|
---|
| 1422 | }
|
---|
| 1423 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
|
---|
| 1424 | return 0;
|
---|
[2] | 1425 |
|
---|
[391] | 1426 | *len_ret = sizeof *addr;
|
---|
| 1427 | return 1;
|
---|
| 1428 | }
|
---|
| 1429 | case BTPROTO_HCI:
|
---|
| 1430 | {
|
---|
| 1431 | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
|
---|
| 1432 | #if defined(__NetBSD__) || defined(__DragonFly__)
|
---|
| 1433 | char *straddr = PyBytes_AS_STRING(args);
|
---|
| 1434 |
|
---|
[2] | 1435 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
[391] | 1436 | if (straddr == NULL) {
|
---|
| 1437 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
| 1438 | "wrong format");
|
---|
| 1439 | return 0;
|
---|
| 1440 | }
|
---|
| 1441 | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
|
---|
| 1442 | return 0;
|
---|
| 1443 | #else
|
---|
| 1444 | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
| 1445 | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
|
---|
| 1446 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
| 1447 | "wrong format");
|
---|
| 1448 | return 0;
|
---|
| 1449 | }
|
---|
| 1450 | #endif
|
---|
| 1451 | *len_ret = sizeof *addr;
|
---|
| 1452 | return 1;
|
---|
| 1453 | }
|
---|
[2] | 1454 | #if !defined(__FreeBSD__)
|
---|
[391] | 1455 | case BTPROTO_SCO:
|
---|
| 1456 | {
|
---|
| 1457 | struct sockaddr_sco *addr;
|
---|
| 1458 | char *straddr;
|
---|
[2] | 1459 |
|
---|
[391] | 1460 | addr = (struct sockaddr_sco *)addr_ret;
|
---|
| 1461 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
| 1462 | straddr = PyString_AsString(args);
|
---|
| 1463 | if (straddr == NULL) {
|
---|
| 1464 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
| 1465 | "wrong format");
|
---|
| 1466 | return 0;
|
---|
| 1467 | }
|
---|
| 1468 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
|
---|
| 1469 | return 0;
|
---|
[2] | 1470 |
|
---|
[391] | 1471 | *len_ret = sizeof *addr;
|
---|
| 1472 | return 1;
|
---|
| 1473 | }
|
---|
[2] | 1474 | #endif
|
---|
[391] | 1475 | default:
|
---|
| 1476 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
|
---|
| 1477 | return 0;
|
---|
| 1478 | }
|
---|
| 1479 | }
|
---|
[2] | 1480 | #endif
|
---|
| 1481 |
|
---|
[391] | 1482 | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
|
---|
| 1483 | case AF_PACKET:
|
---|
| 1484 | {
|
---|
| 1485 | struct sockaddr_ll* addr;
|
---|
| 1486 | struct ifreq ifr;
|
---|
| 1487 | char *interfaceName;
|
---|
| 1488 | int protoNumber;
|
---|
| 1489 | int hatype = 0;
|
---|
| 1490 | int pkttype = 0;
|
---|
| 1491 | char *haddr = NULL;
|
---|
| 1492 | unsigned int halen = 0;
|
---|
[2] | 1493 |
|
---|
[391] | 1494 | if (!PyTuple_Check(args)) {
|
---|
| 1495 | PyErr_Format(
|
---|
| 1496 | PyExc_TypeError,
|
---|
| 1497 | "getsockaddrarg: "
|
---|
| 1498 | "AF_PACKET address must be tuple, not %.500s",
|
---|
| 1499 | Py_TYPE(args)->tp_name);
|
---|
| 1500 | return 0;
|
---|
| 1501 | }
|
---|
| 1502 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
|
---|
| 1503 | &protoNumber, &pkttype, &hatype,
|
---|
| 1504 | &haddr, &halen))
|
---|
| 1505 | return 0;
|
---|
| 1506 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
|
---|
| 1507 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
|
---|
| 1508 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
|
---|
| 1509 | s->errorhandler();
|
---|
| 1510 | return 0;
|
---|
| 1511 | }
|
---|
| 1512 | if (halen > 8) {
|
---|
| 1513 | PyErr_SetString(PyExc_ValueError,
|
---|
| 1514 | "Hardware address must be 8 bytes or less");
|
---|
| 1515 | return 0;
|
---|
| 1516 | }
|
---|
| 1517 | if (protoNumber < 0 || protoNumber > 0xffff) {
|
---|
| 1518 | PyErr_SetString(
|
---|
| 1519 | PyExc_OverflowError,
|
---|
| 1520 | "getsockaddrarg: protoNumber must be 0-65535.");
|
---|
| 1521 | return 0;
|
---|
| 1522 | }
|
---|
| 1523 | addr = (struct sockaddr_ll*)addr_ret;
|
---|
| 1524 | addr->sll_family = AF_PACKET;
|
---|
| 1525 | addr->sll_protocol = htons((short)protoNumber);
|
---|
| 1526 | addr->sll_ifindex = ifr.ifr_ifindex;
|
---|
| 1527 | addr->sll_pkttype = pkttype;
|
---|
| 1528 | addr->sll_hatype = hatype;
|
---|
| 1529 | if (halen != 0) {
|
---|
| 1530 | memcpy(&addr->sll_addr, haddr, halen);
|
---|
| 1531 | }
|
---|
| 1532 | addr->sll_halen = halen;
|
---|
| 1533 | *len_ret = sizeof *addr;
|
---|
| 1534 | return 1;
|
---|
| 1535 | }
|
---|
[2] | 1536 | #endif
|
---|
| 1537 |
|
---|
| 1538 | #ifdef HAVE_LINUX_TIPC_H
|
---|
[391] | 1539 | case AF_TIPC:
|
---|
| 1540 | {
|
---|
| 1541 | unsigned int atype, v1, v2, v3;
|
---|
| 1542 | unsigned int scope = TIPC_CLUSTER_SCOPE;
|
---|
| 1543 | struct sockaddr_tipc *addr;
|
---|
[2] | 1544 |
|
---|
[391] | 1545 | if (!PyTuple_Check(args)) {
|
---|
| 1546 | PyErr_Format(
|
---|
| 1547 | PyExc_TypeError,
|
---|
| 1548 | "getsockaddrarg: "
|
---|
| 1549 | "AF_TIPC address must be tuple, not %.500s",
|
---|
| 1550 | Py_TYPE(args)->tp_name);
|
---|
| 1551 | return 0;
|
---|
| 1552 | }
|
---|
[2] | 1553 |
|
---|
[391] | 1554 | if (!PyArg_ParseTuple(args,
|
---|
| 1555 | "IIII|I;Invalid TIPC address format",
|
---|
| 1556 | &atype, &v1, &v2, &v3, &scope))
|
---|
| 1557 | return 0;
|
---|
[2] | 1558 |
|
---|
[391] | 1559 | addr = (struct sockaddr_tipc *) addr_ret;
|
---|
| 1560 | memset(addr, 0, sizeof(struct sockaddr_tipc));
|
---|
[2] | 1561 |
|
---|
[391] | 1562 | addr->family = AF_TIPC;
|
---|
| 1563 | addr->scope = scope;
|
---|
| 1564 | addr->addrtype = atype;
|
---|
[2] | 1565 |
|
---|
[391] | 1566 | if (atype == TIPC_ADDR_NAMESEQ) {
|
---|
| 1567 | addr->addr.nameseq.type = v1;
|
---|
| 1568 | addr->addr.nameseq.lower = v2;
|
---|
| 1569 | addr->addr.nameseq.upper = v3;
|
---|
| 1570 | } else if (atype == TIPC_ADDR_NAME) {
|
---|
| 1571 | addr->addr.name.name.type = v1;
|
---|
| 1572 | addr->addr.name.name.instance = v2;
|
---|
| 1573 | } else if (atype == TIPC_ADDR_ID) {
|
---|
| 1574 | addr->addr.id.node = v1;
|
---|
| 1575 | addr->addr.id.ref = v2;
|
---|
| 1576 | } else {
|
---|
| 1577 | /* Shouldn't happen */
|
---|
| 1578 | PyErr_SetString(PyExc_TypeError, "Invalid address type");
|
---|
| 1579 | return 0;
|
---|
| 1580 | }
|
---|
[2] | 1581 |
|
---|
[391] | 1582 | *len_ret = sizeof(*addr);
|
---|
[2] | 1583 |
|
---|
[391] | 1584 | return 1;
|
---|
| 1585 | }
|
---|
[2] | 1586 | #endif
|
---|
| 1587 |
|
---|
[391] | 1588 | /* More cases here... */
|
---|
[2] | 1589 |
|
---|
[391] | 1590 | default:
|
---|
| 1591 | PyErr_SetString(socket_error, "getsockaddrarg: bad family");
|
---|
| 1592 | return 0;
|
---|
[2] | 1593 |
|
---|
[391] | 1594 | }
|
---|
[2] | 1595 | }
|
---|
| 1596 |
|
---|
| 1597 |
|
---|
| 1598 | /* Get the address length according to the socket object's address family.
|
---|
| 1599 | Return 1 if the family is known, 0 otherwise. The length is returned
|
---|
| 1600 | through len_ret. */
|
---|
| 1601 |
|
---|
| 1602 | static int
|
---|
| 1603 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
|
---|
| 1604 | {
|
---|
[391] | 1605 | switch (s->sock_family) {
|
---|
[2] | 1606 |
|
---|
| 1607 | #if defined(AF_UNIX)
|
---|
[391] | 1608 | case AF_UNIX:
|
---|
| 1609 | {
|
---|
| 1610 | *len_ret = sizeof (struct sockaddr_un);
|
---|
| 1611 | return 1;
|
---|
| 1612 | }
|
---|
[2] | 1613 | #endif /* AF_UNIX */
|
---|
| 1614 | #if defined(AF_NETLINK)
|
---|
| 1615 | case AF_NETLINK:
|
---|
| 1616 | {
|
---|
[391] | 1617 | *len_ret = sizeof (struct sockaddr_nl);
|
---|
| 1618 | return 1;
|
---|
[2] | 1619 | }
|
---|
| 1620 | #endif
|
---|
| 1621 |
|
---|
[391] | 1622 | case AF_INET:
|
---|
| 1623 | {
|
---|
| 1624 | *len_ret = sizeof (struct sockaddr_in);
|
---|
| 1625 | return 1;
|
---|
| 1626 | }
|
---|
[2] | 1627 |
|
---|
| 1628 | #ifdef ENABLE_IPV6
|
---|
[391] | 1629 | case AF_INET6:
|
---|
| 1630 | {
|
---|
| 1631 | *len_ret = sizeof (struct sockaddr_in6);
|
---|
| 1632 | return 1;
|
---|
| 1633 | }
|
---|
[2] | 1634 | #endif
|
---|
| 1635 |
|
---|
| 1636 | #ifdef USE_BLUETOOTH
|
---|
[391] | 1637 | case AF_BLUETOOTH:
|
---|
| 1638 | {
|
---|
| 1639 | switch(s->sock_proto)
|
---|
| 1640 | {
|
---|
[2] | 1641 |
|
---|
[391] | 1642 | case BTPROTO_L2CAP:
|
---|
| 1643 | *len_ret = sizeof (struct sockaddr_l2);
|
---|
| 1644 | return 1;
|
---|
| 1645 | case BTPROTO_RFCOMM:
|
---|
| 1646 | *len_ret = sizeof (struct sockaddr_rc);
|
---|
| 1647 | return 1;
|
---|
| 1648 | case BTPROTO_HCI:
|
---|
| 1649 | *len_ret = sizeof (struct sockaddr_hci);
|
---|
| 1650 | return 1;
|
---|
[2] | 1651 | #if !defined(__FreeBSD__)
|
---|
[391] | 1652 | case BTPROTO_SCO:
|
---|
| 1653 | *len_ret = sizeof (struct sockaddr_sco);
|
---|
| 1654 | return 1;
|
---|
[2] | 1655 | #endif
|
---|
[391] | 1656 | default:
|
---|
| 1657 | PyErr_SetString(socket_error, "getsockaddrlen: "
|
---|
| 1658 | "unknown BT protocol");
|
---|
| 1659 | return 0;
|
---|
[2] | 1660 |
|
---|
[391] | 1661 | }
|
---|
| 1662 | }
|
---|
[2] | 1663 | #endif
|
---|
| 1664 |
|
---|
| 1665 | #ifdef HAVE_NETPACKET_PACKET_H
|
---|
[391] | 1666 | case AF_PACKET:
|
---|
| 1667 | {
|
---|
| 1668 | *len_ret = sizeof (struct sockaddr_ll);
|
---|
| 1669 | return 1;
|
---|
| 1670 | }
|
---|
[2] | 1671 | #endif
|
---|
| 1672 |
|
---|
| 1673 | #ifdef HAVE_LINUX_TIPC_H
|
---|
[391] | 1674 | case AF_TIPC:
|
---|
| 1675 | {
|
---|
| 1676 | *len_ret = sizeof (struct sockaddr_tipc);
|
---|
| 1677 | return 1;
|
---|
| 1678 | }
|
---|
[2] | 1679 | #endif
|
---|
| 1680 |
|
---|
[391] | 1681 | /* More cases here... */
|
---|
[2] | 1682 |
|
---|
[391] | 1683 | default:
|
---|
| 1684 | PyErr_SetString(socket_error, "getsockaddrlen: bad family");
|
---|
| 1685 | return 0;
|
---|
[2] | 1686 |
|
---|
[391] | 1687 | }
|
---|
[2] | 1688 | }
|
---|
| 1689 |
|
---|
| 1690 |
|
---|
| 1691 | /* s.accept() method */
|
---|
| 1692 |
|
---|
| 1693 | static PyObject *
|
---|
| 1694 | sock_accept(PySocketSockObject *s)
|
---|
| 1695 | {
|
---|
[391] | 1696 | sock_addr_t addrbuf;
|
---|
| 1697 | SOCKET_T newfd;
|
---|
| 1698 | socklen_t addrlen;
|
---|
| 1699 | PyObject *sock = NULL;
|
---|
| 1700 | PyObject *addr = NULL;
|
---|
| 1701 | PyObject *res = NULL;
|
---|
| 1702 | int timeout;
|
---|
[2] | 1703 |
|
---|
[391] | 1704 | if (!getsockaddrlen(s, &addrlen))
|
---|
| 1705 | return NULL;
|
---|
| 1706 | memset(&addrbuf, 0, addrlen);
|
---|
[2] | 1707 |
|
---|
| 1708 | #ifdef MS_WINDOWS
|
---|
[391] | 1709 | newfd = INVALID_SOCKET;
|
---|
[2] | 1710 | #else
|
---|
[391] | 1711 | newfd = -1;
|
---|
[2] | 1712 | #endif
|
---|
| 1713 |
|
---|
[391] | 1714 | if (!IS_SELECTABLE(s))
|
---|
| 1715 | return select_error();
|
---|
[2] | 1716 |
|
---|
[391] | 1717 | BEGIN_SELECT_LOOP(s)
|
---|
| 1718 | Py_BEGIN_ALLOW_THREADS
|
---|
| 1719 | timeout = internal_select_ex(s, 0, interval);
|
---|
| 1720 | if (!timeout)
|
---|
| 1721 | newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
|
---|
| 1722 | Py_END_ALLOW_THREADS
|
---|
[2] | 1723 |
|
---|
[391] | 1724 | if (timeout == 1) {
|
---|
| 1725 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 1726 | return NULL;
|
---|
| 1727 | }
|
---|
| 1728 | END_SELECT_LOOP(s)
|
---|
[2] | 1729 |
|
---|
| 1730 | #ifdef MS_WINDOWS
|
---|
[391] | 1731 | if (newfd == INVALID_SOCKET)
|
---|
[2] | 1732 | #else
|
---|
[391] | 1733 | if (newfd < 0)
|
---|
[2] | 1734 | #endif
|
---|
[391] | 1735 | return s->errorhandler();
|
---|
[2] | 1736 |
|
---|
[391] | 1737 | /* Create the new object with unspecified family,
|
---|
| 1738 | to avoid calls to bind() etc. on it. */
|
---|
| 1739 | sock = (PyObject *) new_sockobject(newfd,
|
---|
| 1740 | s->sock_family,
|
---|
| 1741 | s->sock_type,
|
---|
| 1742 | s->sock_proto);
|
---|
[2] | 1743 |
|
---|
[391] | 1744 | if (sock == NULL) {
|
---|
| 1745 | SOCKETCLOSE(newfd);
|
---|
| 1746 | goto finally;
|
---|
| 1747 | }
|
---|
| 1748 | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
|
---|
| 1749 | addrlen, s->sock_proto);
|
---|
| 1750 | if (addr == NULL)
|
---|
| 1751 | goto finally;
|
---|
[2] | 1752 |
|
---|
[391] | 1753 | res = PyTuple_Pack(2, sock, addr);
|
---|
[2] | 1754 |
|
---|
| 1755 | finally:
|
---|
[391] | 1756 | Py_XDECREF(sock);
|
---|
| 1757 | Py_XDECREF(addr);
|
---|
| 1758 | return res;
|
---|
[2] | 1759 | }
|
---|
| 1760 |
|
---|
| 1761 | PyDoc_STRVAR(accept_doc,
|
---|
| 1762 | "accept() -> (socket object, address info)\n\
|
---|
| 1763 | \n\
|
---|
| 1764 | Wait for an incoming connection. Return a new socket representing the\n\
|
---|
| 1765 | connection, and the address of the client. For IP sockets, the address\n\
|
---|
| 1766 | info is a pair (hostaddr, port).");
|
---|
| 1767 |
|
---|
| 1768 | /* s.setblocking(flag) method. Argument:
|
---|
| 1769 | False -- non-blocking mode; same as settimeout(0)
|
---|
| 1770 | True -- blocking mode; same as settimeout(None)
|
---|
| 1771 | */
|
---|
| 1772 |
|
---|
| 1773 | static PyObject *
|
---|
| 1774 | sock_setblocking(PySocketSockObject *s, PyObject *arg)
|
---|
| 1775 | {
|
---|
[391] | 1776 | long block;
|
---|
[2] | 1777 |
|
---|
[391] | 1778 | block = PyInt_AsLong(arg);
|
---|
| 1779 | if (block == -1 && PyErr_Occurred())
|
---|
| 1780 | return NULL;
|
---|
[2] | 1781 |
|
---|
[391] | 1782 | s->sock_timeout = block ? -1.0 : 0.0;
|
---|
| 1783 | internal_setblocking(s, block);
|
---|
[2] | 1784 |
|
---|
[391] | 1785 | Py_INCREF(Py_None);
|
---|
| 1786 | return Py_None;
|
---|
[2] | 1787 | }
|
---|
| 1788 |
|
---|
| 1789 | PyDoc_STRVAR(setblocking_doc,
|
---|
| 1790 | "setblocking(flag)\n\
|
---|
| 1791 | \n\
|
---|
| 1792 | Set the socket to blocking (flag is true) or non-blocking (false).\n\
|
---|
| 1793 | setblocking(True) is equivalent to settimeout(None);\n\
|
---|
| 1794 | setblocking(False) is equivalent to settimeout(0.0).");
|
---|
| 1795 |
|
---|
| 1796 | /* s.settimeout(timeout) method. Argument:
|
---|
| 1797 | None -- no timeout, blocking mode; same as setblocking(True)
|
---|
| 1798 | 0.0 -- non-blocking mode; same as setblocking(False)
|
---|
| 1799 | > 0 -- timeout mode; operations time out after timeout seconds
|
---|
| 1800 | < 0 -- illegal; raises an exception
|
---|
| 1801 | */
|
---|
| 1802 | static PyObject *
|
---|
| 1803 | sock_settimeout(PySocketSockObject *s, PyObject *arg)
|
---|
| 1804 | {
|
---|
[391] | 1805 | double timeout;
|
---|
[2] | 1806 |
|
---|
[391] | 1807 | if (arg == Py_None)
|
---|
| 1808 | timeout = -1.0;
|
---|
| 1809 | else {
|
---|
| 1810 | timeout = PyFloat_AsDouble(arg);
|
---|
| 1811 | if (timeout < 0.0) {
|
---|
| 1812 | if (!PyErr_Occurred())
|
---|
| 1813 | PyErr_SetString(PyExc_ValueError,
|
---|
| 1814 | "Timeout value out of range");
|
---|
| 1815 | return NULL;
|
---|
| 1816 | }
|
---|
| 1817 | }
|
---|
[2] | 1818 |
|
---|
[391] | 1819 | s->sock_timeout = timeout;
|
---|
| 1820 | internal_setblocking(s, timeout < 0.0);
|
---|
[2] | 1821 |
|
---|
[391] | 1822 | Py_INCREF(Py_None);
|
---|
| 1823 | return Py_None;
|
---|
[2] | 1824 | }
|
---|
| 1825 |
|
---|
| 1826 | PyDoc_STRVAR(settimeout_doc,
|
---|
| 1827 | "settimeout(timeout)\n\
|
---|
| 1828 | \n\
|
---|
| 1829 | Set a timeout on socket operations. 'timeout' can be a float,\n\
|
---|
| 1830 | giving in seconds, or None. Setting a timeout of None disables\n\
|
---|
| 1831 | the timeout feature and is equivalent to setblocking(1).\n\
|
---|
| 1832 | Setting a timeout of zero is the same as setblocking(0).");
|
---|
| 1833 |
|
---|
| 1834 | /* s.gettimeout() method.
|
---|
| 1835 | Returns the timeout associated with a socket. */
|
---|
| 1836 | static PyObject *
|
---|
| 1837 | sock_gettimeout(PySocketSockObject *s)
|
---|
| 1838 | {
|
---|
[391] | 1839 | if (s->sock_timeout < 0.0) {
|
---|
| 1840 | Py_INCREF(Py_None);
|
---|
| 1841 | return Py_None;
|
---|
| 1842 | }
|
---|
| 1843 | else
|
---|
| 1844 | return PyFloat_FromDouble(s->sock_timeout);
|
---|
[2] | 1845 | }
|
---|
| 1846 |
|
---|
| 1847 | PyDoc_STRVAR(gettimeout_doc,
|
---|
| 1848 | "gettimeout() -> timeout\n\
|
---|
| 1849 | \n\
|
---|
[391] | 1850 | Returns the timeout in seconds (float) associated with socket \n\
|
---|
[2] | 1851 | operations. A timeout of None indicates that timeouts on socket \n\
|
---|
| 1852 | operations are disabled.");
|
---|
| 1853 |
|
---|
| 1854 | #ifdef RISCOS
|
---|
| 1855 | /* s.sleeptaskw(1 | 0) method */
|
---|
| 1856 |
|
---|
| 1857 | static PyObject *
|
---|
| 1858 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
|
---|
| 1859 | {
|
---|
[391] | 1860 | int block;
|
---|
| 1861 | block = PyInt_AsLong(arg);
|
---|
| 1862 | if (block == -1 && PyErr_Occurred())
|
---|
| 1863 | return NULL;
|
---|
| 1864 | Py_BEGIN_ALLOW_THREADS
|
---|
| 1865 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
|
---|
| 1866 | Py_END_ALLOW_THREADS
|
---|
[2] | 1867 |
|
---|
[391] | 1868 | Py_INCREF(Py_None);
|
---|
| 1869 | return Py_None;
|
---|
[2] | 1870 | }
|
---|
| 1871 | PyDoc_STRVAR(sleeptaskw_doc,
|
---|
| 1872 | "sleeptaskw(flag)\n\
|
---|
| 1873 | \n\
|
---|
| 1874 | Allow sleeps in taskwindows.");
|
---|
| 1875 | #endif
|
---|
| 1876 |
|
---|
| 1877 |
|
---|
| 1878 | /* s.setsockopt() method.
|
---|
| 1879 | With an integer third argument, sets an integer option.
|
---|
| 1880 | With a string third argument, sets an option from a buffer;
|
---|
| 1881 | use optional built-in module 'struct' to encode the string. */
|
---|
| 1882 |
|
---|
| 1883 | static PyObject *
|
---|
| 1884 | sock_setsockopt(PySocketSockObject *s, PyObject *args)
|
---|
| 1885 | {
|
---|
[391] | 1886 | int level;
|
---|
| 1887 | int optname;
|
---|
| 1888 | int res;
|
---|
| 1889 | char *buf;
|
---|
| 1890 | int buflen;
|
---|
| 1891 | int flag;
|
---|
[2] | 1892 |
|
---|
[391] | 1893 | if (PyArg_ParseTuple(args, "iii:setsockopt",
|
---|
| 1894 | &level, &optname, &flag)) {
|
---|
| 1895 | buf = (char *) &flag;
|
---|
| 1896 | buflen = sizeof flag;
|
---|
| 1897 | }
|
---|
| 1898 | else {
|
---|
| 1899 | PyErr_Clear();
|
---|
| 1900 | if (!PyArg_ParseTuple(args, "iis#:setsockopt",
|
---|
| 1901 | &level, &optname, &buf, &buflen))
|
---|
| 1902 | return NULL;
|
---|
| 1903 | }
|
---|
| 1904 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
|
---|
| 1905 | if (res < 0)
|
---|
| 1906 | return s->errorhandler();
|
---|
| 1907 | Py_INCREF(Py_None);
|
---|
| 1908 | return Py_None;
|
---|
[2] | 1909 | }
|
---|
| 1910 |
|
---|
| 1911 | PyDoc_STRVAR(setsockopt_doc,
|
---|
| 1912 | "setsockopt(level, option, value)\n\
|
---|
| 1913 | \n\
|
---|
| 1914 | Set a socket option. See the Unix manual for level and option.\n\
|
---|
| 1915 | The value argument can either be an integer or a string.");
|
---|
| 1916 |
|
---|
| 1917 |
|
---|
| 1918 | /* s.getsockopt() method.
|
---|
| 1919 | With two arguments, retrieves an integer option.
|
---|
| 1920 | With a third integer argument, retrieves a string buffer of that size;
|
---|
| 1921 | use optional built-in module 'struct' to decode the string. */
|
---|
| 1922 |
|
---|
| 1923 | static PyObject *
|
---|
| 1924 | sock_getsockopt(PySocketSockObject *s, PyObject *args)
|
---|
| 1925 | {
|
---|
[391] | 1926 | int level;
|
---|
| 1927 | int optname;
|
---|
| 1928 | int res;
|
---|
| 1929 | PyObject *buf;
|
---|
| 1930 | socklen_t buflen = 0;
|
---|
[2] | 1931 |
|
---|
| 1932 | #ifdef __BEOS__
|
---|
[391] | 1933 | /* We have incomplete socket support. */
|
---|
| 1934 | PyErr_SetString(socket_error, "getsockopt not supported");
|
---|
| 1935 | return NULL;
|
---|
[2] | 1936 | #else
|
---|
| 1937 |
|
---|
[391] | 1938 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
|
---|
| 1939 | &level, &optname, &buflen))
|
---|
| 1940 | return NULL;
|
---|
[2] | 1941 |
|
---|
[391] | 1942 | if (buflen == 0) {
|
---|
| 1943 | int flag = 0;
|
---|
| 1944 | socklen_t flagsize = sizeof flag;
|
---|
| 1945 | res = getsockopt(s->sock_fd, level, optname,
|
---|
| 1946 | (void *)&flag, &flagsize);
|
---|
| 1947 | if (res < 0)
|
---|
| 1948 | return s->errorhandler();
|
---|
| 1949 | return PyInt_FromLong(flag);
|
---|
| 1950 | }
|
---|
[2] | 1951 | #ifdef __VMS
|
---|
[391] | 1952 | /* socklen_t is unsigned so no negative test is needed,
|
---|
| 1953 | test buflen == 0 is previously done */
|
---|
| 1954 | if (buflen > 1024) {
|
---|
[2] | 1955 | #else
|
---|
[391] | 1956 | if (buflen <= 0 || buflen > 1024) {
|
---|
[2] | 1957 | #endif
|
---|
[391] | 1958 | PyErr_SetString(socket_error,
|
---|
| 1959 | "getsockopt buflen out of range");
|
---|
| 1960 | return NULL;
|
---|
| 1961 | }
|
---|
| 1962 | buf = PyString_FromStringAndSize((char *)NULL, buflen);
|
---|
| 1963 | if (buf == NULL)
|
---|
| 1964 | return NULL;
|
---|
| 1965 | res = getsockopt(s->sock_fd, level, optname,
|
---|
| 1966 | (void *)PyString_AS_STRING(buf), &buflen);
|
---|
| 1967 | if (res < 0) {
|
---|
| 1968 | Py_DECREF(buf);
|
---|
| 1969 | return s->errorhandler();
|
---|
| 1970 | }
|
---|
| 1971 | _PyString_Resize(&buf, buflen);
|
---|
| 1972 | return buf;
|
---|
[2] | 1973 | #endif /* __BEOS__ */
|
---|
| 1974 | }
|
---|
| 1975 |
|
---|
| 1976 | PyDoc_STRVAR(getsockopt_doc,
|
---|
| 1977 | "getsockopt(level, option[, buffersize]) -> value\n\
|
---|
| 1978 | \n\
|
---|
| 1979 | Get a socket option. See the Unix manual for level and option.\n\
|
---|
| 1980 | If a nonzero buffersize argument is given, the return value is a\n\
|
---|
| 1981 | string of that length; otherwise it is an integer.");
|
---|
| 1982 |
|
---|
| 1983 |
|
---|
| 1984 | /* s.bind(sockaddr) method */
|
---|
| 1985 |
|
---|
| 1986 | static PyObject *
|
---|
| 1987 | sock_bind(PySocketSockObject *s, PyObject *addro)
|
---|
| 1988 | {
|
---|
[391] | 1989 | sock_addr_t addrbuf;
|
---|
| 1990 | int addrlen;
|
---|
| 1991 | int res;
|
---|
[2] | 1992 |
|
---|
[391] | 1993 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
|
---|
| 1994 | return NULL;
|
---|
| 1995 | Py_BEGIN_ALLOW_THREADS
|
---|
| 1996 | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
|
---|
| 1997 | Py_END_ALLOW_THREADS
|
---|
| 1998 | if (res < 0)
|
---|
| 1999 | return s->errorhandler();
|
---|
| 2000 | Py_INCREF(Py_None);
|
---|
| 2001 | return Py_None;
|
---|
[2] | 2002 | }
|
---|
| 2003 |
|
---|
| 2004 | PyDoc_STRVAR(bind_doc,
|
---|
| 2005 | "bind(address)\n\
|
---|
| 2006 | \n\
|
---|
| 2007 | Bind the socket to a local address. For IP sockets, the address is a\n\
|
---|
| 2008 | pair (host, port); the host must refer to the local host. For raw packet\n\
|
---|
| 2009 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
|
---|
| 2010 |
|
---|
| 2011 |
|
---|
| 2012 | /* s.close() method.
|
---|
| 2013 | Set the file descriptor to -1 so operations tried subsequently
|
---|
| 2014 | will surely fail. */
|
---|
| 2015 |
|
---|
| 2016 | static PyObject *
|
---|
| 2017 | sock_close(PySocketSockObject *s)
|
---|
| 2018 | {
|
---|
[391] | 2019 | SOCKET_T fd;
|
---|
[2] | 2020 |
|
---|
[391] | 2021 | if ((fd = s->sock_fd) != -1) {
|
---|
| 2022 | s->sock_fd = -1;
|
---|
| 2023 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2024 | (void) SOCKETCLOSE(fd);
|
---|
| 2025 | Py_END_ALLOW_THREADS
|
---|
| 2026 | }
|
---|
| 2027 | Py_INCREF(Py_None);
|
---|
| 2028 | return Py_None;
|
---|
[2] | 2029 | }
|
---|
| 2030 |
|
---|
| 2031 | PyDoc_STRVAR(close_doc,
|
---|
| 2032 | "close()\n\
|
---|
| 2033 | \n\
|
---|
| 2034 | Close the socket. It cannot be used after this call.");
|
---|
| 2035 |
|
---|
| 2036 | static int
|
---|
| 2037 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
|
---|
[391] | 2038 | int *timeoutp)
|
---|
[2] | 2039 | {
|
---|
[391] | 2040 | int res, timeout;
|
---|
[2] | 2041 |
|
---|
[391] | 2042 | timeout = 0;
|
---|
| 2043 | res = connect(s->sock_fd, addr, addrlen);
|
---|
[2] | 2044 |
|
---|
| 2045 | #ifdef MS_WINDOWS
|
---|
| 2046 |
|
---|
[391] | 2047 | if (s->sock_timeout > 0.0) {
|
---|
| 2048 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
|
---|
| 2049 | IS_SELECTABLE(s)) {
|
---|
| 2050 | /* This is a mess. Best solution: trust select */
|
---|
| 2051 | fd_set fds;
|
---|
| 2052 | fd_set fds_exc;
|
---|
| 2053 | struct timeval tv;
|
---|
| 2054 | tv.tv_sec = (int)s->sock_timeout;
|
---|
| 2055 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
|
---|
| 2056 | FD_ZERO(&fds);
|
---|
| 2057 | FD_SET(s->sock_fd, &fds);
|
---|
| 2058 | FD_ZERO(&fds_exc);
|
---|
| 2059 | FD_SET(s->sock_fd, &fds_exc);
|
---|
| 2060 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
|
---|
| 2061 | if (res == 0) {
|
---|
| 2062 | res = WSAEWOULDBLOCK;
|
---|
| 2063 | timeout = 1;
|
---|
| 2064 | } else if (res > 0) {
|
---|
| 2065 | if (FD_ISSET(s->sock_fd, &fds))
|
---|
| 2066 | /* The socket is in the writeable set - this
|
---|
| 2067 | means connected */
|
---|
| 2068 | res = 0;
|
---|
| 2069 | else {
|
---|
| 2070 | /* As per MS docs, we need to call getsockopt()
|
---|
| 2071 | to get the underlying error */
|
---|
| 2072 | int res_size = sizeof res;
|
---|
| 2073 | /* It must be in the exception set */
|
---|
| 2074 | assert(FD_ISSET(s->sock_fd, &fds_exc));
|
---|
| 2075 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
|
---|
| 2076 | (char *)&res, &res_size))
|
---|
| 2077 | /* getsockopt also clears WSAGetLastError,
|
---|
| 2078 | so reset it back. */
|
---|
| 2079 | WSASetLastError(res);
|
---|
| 2080 | else
|
---|
| 2081 | res = WSAGetLastError();
|
---|
| 2082 | }
|
---|
| 2083 | }
|
---|
| 2084 | /* else if (res < 0) an error occurred */
|
---|
| 2085 | }
|
---|
| 2086 | }
|
---|
[2] | 2087 |
|
---|
[391] | 2088 | if (res < 0)
|
---|
| 2089 | res = WSAGetLastError();
|
---|
[2] | 2090 |
|
---|
| 2091 | #else
|
---|
| 2092 |
|
---|
[391] | 2093 | if (s->sock_timeout > 0.0) {
|
---|
| 2094 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
|
---|
| 2095 | timeout = internal_select(s, 1);
|
---|
| 2096 | if (timeout == 0) {
|
---|
| 2097 | /* Bug #1019808: in case of an EINPROGRESS,
|
---|
| 2098 | use getsockopt(SO_ERROR) to get the real
|
---|
| 2099 | error. */
|
---|
| 2100 | socklen_t res_size = sizeof res;
|
---|
| 2101 | (void)getsockopt(s->sock_fd, SOL_SOCKET,
|
---|
| 2102 | SO_ERROR, &res, &res_size);
|
---|
| 2103 | if (res == EISCONN)
|
---|
| 2104 | res = 0;
|
---|
| 2105 | errno = res;
|
---|
| 2106 | }
|
---|
| 2107 | else if (timeout == -1) {
|
---|
| 2108 | res = errno; /* had error */
|
---|
| 2109 | }
|
---|
| 2110 | else
|
---|
| 2111 | res = EWOULDBLOCK; /* timed out */
|
---|
| 2112 | }
|
---|
| 2113 | }
|
---|
[2] | 2114 |
|
---|
[391] | 2115 | if (res < 0)
|
---|
| 2116 | res = errno;
|
---|
[2] | 2117 |
|
---|
| 2118 | #endif
|
---|
[391] | 2119 | *timeoutp = timeout;
|
---|
[2] | 2120 |
|
---|
[391] | 2121 | return res;
|
---|
[2] | 2122 | }
|
---|
| 2123 |
|
---|
| 2124 | /* s.connect(sockaddr) method */
|
---|
| 2125 |
|
---|
| 2126 | static PyObject *
|
---|
| 2127 | sock_connect(PySocketSockObject *s, PyObject *addro)
|
---|
| 2128 | {
|
---|
[391] | 2129 | sock_addr_t addrbuf;
|
---|
| 2130 | int addrlen;
|
---|
| 2131 | int res;
|
---|
| 2132 | int timeout;
|
---|
[2] | 2133 |
|
---|
[391] | 2134 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
|
---|
| 2135 | return NULL;
|
---|
[2] | 2136 |
|
---|
[391] | 2137 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2138 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
|
---|
| 2139 | Py_END_ALLOW_THREADS
|
---|
[2] | 2140 |
|
---|
[391] | 2141 | if (timeout == 1) {
|
---|
| 2142 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 2143 | return NULL;
|
---|
| 2144 | }
|
---|
| 2145 | if (res != 0)
|
---|
| 2146 | return s->errorhandler();
|
---|
| 2147 | Py_INCREF(Py_None);
|
---|
| 2148 | return Py_None;
|
---|
[2] | 2149 | }
|
---|
| 2150 |
|
---|
| 2151 | PyDoc_STRVAR(connect_doc,
|
---|
| 2152 | "connect(address)\n\
|
---|
| 2153 | \n\
|
---|
| 2154 | Connect the socket to a remote address. For IP sockets, the address\n\
|
---|
| 2155 | is a pair (host, port).");
|
---|
| 2156 |
|
---|
| 2157 |
|
---|
| 2158 | /* s.connect_ex(sockaddr) method */
|
---|
| 2159 |
|
---|
| 2160 | static PyObject *
|
---|
| 2161 | sock_connect_ex(PySocketSockObject *s, PyObject *addro)
|
---|
| 2162 | {
|
---|
[391] | 2163 | sock_addr_t addrbuf;
|
---|
| 2164 | int addrlen;
|
---|
| 2165 | int res;
|
---|
| 2166 | int timeout;
|
---|
[2] | 2167 |
|
---|
[391] | 2168 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
|
---|
| 2169 | return NULL;
|
---|
[2] | 2170 |
|
---|
[391] | 2171 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2172 | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
|
---|
| 2173 | Py_END_ALLOW_THREADS
|
---|
[2] | 2174 |
|
---|
[391] | 2175 | /* Signals are not errors (though they may raise exceptions). Adapted
|
---|
| 2176 | from PyErr_SetFromErrnoWithFilenameObject(). */
|
---|
[2] | 2177 | #ifdef EINTR
|
---|
[391] | 2178 | if (res == EINTR && PyErr_CheckSignals())
|
---|
| 2179 | return NULL;
|
---|
[2] | 2180 | #endif
|
---|
| 2181 |
|
---|
[391] | 2182 | return PyInt_FromLong((long) res);
|
---|
[2] | 2183 | }
|
---|
| 2184 |
|
---|
| 2185 | PyDoc_STRVAR(connect_ex_doc,
|
---|
| 2186 | "connect_ex(address) -> errno\n\
|
---|
| 2187 | \n\
|
---|
| 2188 | This is like connect(address), but returns an error code (the errno value)\n\
|
---|
| 2189 | instead of raising an exception when an error occurs.");
|
---|
| 2190 |
|
---|
| 2191 |
|
---|
| 2192 | /* s.fileno() method */
|
---|
| 2193 |
|
---|
| 2194 | static PyObject *
|
---|
| 2195 | sock_fileno(PySocketSockObject *s)
|
---|
| 2196 | {
|
---|
| 2197 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG
|
---|
[391] | 2198 | return PyInt_FromLong((long) s->sock_fd);
|
---|
[2] | 2199 | #else
|
---|
[391] | 2200 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
|
---|
[2] | 2201 | #endif
|
---|
| 2202 | }
|
---|
| 2203 |
|
---|
| 2204 | PyDoc_STRVAR(fileno_doc,
|
---|
| 2205 | "fileno() -> integer\n\
|
---|
| 2206 | \n\
|
---|
| 2207 | Return the integer file descriptor of the socket.");
|
---|
| 2208 |
|
---|
| 2209 |
|
---|
| 2210 | #ifndef NO_DUP
|
---|
| 2211 | /* s.dup() method */
|
---|
| 2212 |
|
---|
| 2213 | static PyObject *
|
---|
| 2214 | sock_dup(PySocketSockObject *s)
|
---|
| 2215 | {
|
---|
[391] | 2216 | SOCKET_T newfd;
|
---|
| 2217 | PyObject *sock;
|
---|
[2] | 2218 |
|
---|
[391] | 2219 | newfd = dup(s->sock_fd);
|
---|
| 2220 | if (newfd < 0)
|
---|
| 2221 | return s->errorhandler();
|
---|
| 2222 | sock = (PyObject *) new_sockobject(newfd,
|
---|
| 2223 | s->sock_family,
|
---|
| 2224 | s->sock_type,
|
---|
| 2225 | s->sock_proto);
|
---|
| 2226 | if (sock == NULL)
|
---|
| 2227 | SOCKETCLOSE(newfd);
|
---|
| 2228 | return sock;
|
---|
[2] | 2229 | }
|
---|
| 2230 |
|
---|
| 2231 | PyDoc_STRVAR(dup_doc,
|
---|
| 2232 | "dup() -> socket object\n\
|
---|
| 2233 | \n\
|
---|
| 2234 | Return a new socket object connected to the same system resource.");
|
---|
| 2235 |
|
---|
| 2236 | #endif
|
---|
| 2237 |
|
---|
| 2238 |
|
---|
| 2239 | /* s.getsockname() method */
|
---|
| 2240 |
|
---|
| 2241 | static PyObject *
|
---|
| 2242 | sock_getsockname(PySocketSockObject *s)
|
---|
| 2243 | {
|
---|
[391] | 2244 | sock_addr_t addrbuf;
|
---|
| 2245 | int res;
|
---|
| 2246 | socklen_t addrlen;
|
---|
[2] | 2247 |
|
---|
[391] | 2248 | if (!getsockaddrlen(s, &addrlen))
|
---|
| 2249 | return NULL;
|
---|
| 2250 | memset(&addrbuf, 0, addrlen);
|
---|
| 2251 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2252 | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
|
---|
| 2253 | Py_END_ALLOW_THREADS
|
---|
| 2254 | if (res < 0)
|
---|
| 2255 | return s->errorhandler();
|
---|
| 2256 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
|
---|
| 2257 | s->sock_proto);
|
---|
[2] | 2258 | }
|
---|
| 2259 |
|
---|
| 2260 | PyDoc_STRVAR(getsockname_doc,
|
---|
| 2261 | "getsockname() -> address info\n\
|
---|
| 2262 | \n\
|
---|
| 2263 | Return the address of the local endpoint. For IP sockets, the address\n\
|
---|
| 2264 | info is a pair (hostaddr, port).");
|
---|
| 2265 |
|
---|
| 2266 |
|
---|
[391] | 2267 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
|
---|
[2] | 2268 | /* s.getpeername() method */
|
---|
| 2269 |
|
---|
| 2270 | static PyObject *
|
---|
| 2271 | sock_getpeername(PySocketSockObject *s)
|
---|
| 2272 | {
|
---|
[391] | 2273 | sock_addr_t addrbuf;
|
---|
| 2274 | int res;
|
---|
| 2275 | socklen_t addrlen;
|
---|
[2] | 2276 |
|
---|
[391] | 2277 | if (!getsockaddrlen(s, &addrlen))
|
---|
| 2278 | return NULL;
|
---|
| 2279 | memset(&addrbuf, 0, addrlen);
|
---|
| 2280 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2281 | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
|
---|
| 2282 | Py_END_ALLOW_THREADS
|
---|
| 2283 | if (res < 0)
|
---|
| 2284 | return s->errorhandler();
|
---|
| 2285 | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
|
---|
| 2286 | s->sock_proto);
|
---|
[2] | 2287 | }
|
---|
| 2288 |
|
---|
| 2289 | PyDoc_STRVAR(getpeername_doc,
|
---|
| 2290 | "getpeername() -> address info\n\
|
---|
| 2291 | \n\
|
---|
| 2292 | Return the address of the remote endpoint. For IP sockets, the address\n\
|
---|
| 2293 | info is a pair (hostaddr, port).");
|
---|
| 2294 |
|
---|
| 2295 | #endif /* HAVE_GETPEERNAME */
|
---|
| 2296 |
|
---|
| 2297 |
|
---|
| 2298 | /* s.listen(n) method */
|
---|
| 2299 |
|
---|
| 2300 | static PyObject *
|
---|
| 2301 | sock_listen(PySocketSockObject *s, PyObject *arg)
|
---|
| 2302 | {
|
---|
[391] | 2303 | int backlog;
|
---|
| 2304 | int res;
|
---|
[2] | 2305 |
|
---|
[391] | 2306 | backlog = _PyInt_AsInt(arg);
|
---|
| 2307 | if (backlog == -1 && PyErr_Occurred())
|
---|
| 2308 | return NULL;
|
---|
| 2309 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2310 | /* To avoid problems on systems that don't allow a negative backlog
|
---|
| 2311 | * (which doesn't make sense anyway) we force a minimum value of 0. */
|
---|
| 2312 | if (backlog < 0)
|
---|
| 2313 | backlog = 0;
|
---|
| 2314 | res = listen(s->sock_fd, backlog);
|
---|
| 2315 | Py_END_ALLOW_THREADS
|
---|
| 2316 | if (res < 0)
|
---|
| 2317 | return s->errorhandler();
|
---|
| 2318 | Py_INCREF(Py_None);
|
---|
| 2319 | return Py_None;
|
---|
[2] | 2320 | }
|
---|
| 2321 |
|
---|
| 2322 | PyDoc_STRVAR(listen_doc,
|
---|
| 2323 | "listen(backlog)\n\
|
---|
| 2324 | \n\
|
---|
| 2325 | Enable a server to accept connections. The backlog argument must be at\n\
|
---|
[391] | 2326 | least 0 (if it is lower, it is set to 0); it specifies the number of\n\
|
---|
| 2327 | unaccepted connections that the system will allow before refusing new\n\
|
---|
| 2328 | connections.");
|
---|
[2] | 2329 |
|
---|
| 2330 |
|
---|
| 2331 | #ifndef NO_DUP
|
---|
| 2332 | /* s.makefile(mode) method.
|
---|
| 2333 | Create a new open file object referring to a dupped version of
|
---|
| 2334 | the socket's file descriptor. (The dup() call is necessary so
|
---|
| 2335 | that the open file and socket objects may be closed independent
|
---|
| 2336 | of each other.)
|
---|
| 2337 | The mode argument specifies 'r' or 'w' passed to fdopen(). */
|
---|
| 2338 |
|
---|
| 2339 | static PyObject *
|
---|
| 2340 | sock_makefile(PySocketSockObject *s, PyObject *args)
|
---|
| 2341 | {
|
---|
[391] | 2342 | extern int fclose(FILE *);
|
---|
| 2343 | char *mode = "r";
|
---|
| 2344 | int bufsize = -1;
|
---|
[2] | 2345 | #ifdef MS_WIN32
|
---|
[391] | 2346 | Py_intptr_t fd;
|
---|
[2] | 2347 | #else
|
---|
[391] | 2348 | int fd;
|
---|
[2] | 2349 | #endif
|
---|
[391] | 2350 | FILE *fp;
|
---|
| 2351 | PyObject *f;
|
---|
[2] | 2352 | #ifdef __VMS
|
---|
[391] | 2353 | char *mode_r = "r";
|
---|
| 2354 | char *mode_w = "w";
|
---|
[2] | 2355 | #endif
|
---|
| 2356 |
|
---|
[391] | 2357 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
|
---|
| 2358 | return NULL;
|
---|
[2] | 2359 | #ifdef __VMS
|
---|
[391] | 2360 | if (strcmp(mode,"rb") == 0) {
|
---|
| 2361 | mode = mode_r;
|
---|
| 2362 | }
|
---|
| 2363 | else {
|
---|
| 2364 | if (strcmp(mode,"wb") == 0) {
|
---|
| 2365 | mode = mode_w;
|
---|
| 2366 | }
|
---|
| 2367 | }
|
---|
[2] | 2368 | #endif
|
---|
| 2369 | #ifdef MS_WIN32
|
---|
[391] | 2370 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
|
---|
| 2371 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
|
---|
[2] | 2372 | #else
|
---|
[391] | 2373 | if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
|
---|
[2] | 2374 | #endif
|
---|
[391] | 2375 | {
|
---|
| 2376 | if (fd >= 0)
|
---|
| 2377 | SOCKETCLOSE(fd);
|
---|
| 2378 | return s->errorhandler();
|
---|
| 2379 | }
|
---|
| 2380 | f = PyFile_FromFile(fp, "<socket>", mode, fclose);
|
---|
| 2381 | if (f != NULL)
|
---|
| 2382 | PyFile_SetBufSize(f, bufsize);
|
---|
| 2383 | return f;
|
---|
[2] | 2384 | }
|
---|
| 2385 |
|
---|
| 2386 | PyDoc_STRVAR(makefile_doc,
|
---|
| 2387 | "makefile([mode[, buffersize]]) -> file object\n\
|
---|
| 2388 | \n\
|
---|
| 2389 | Return a regular file object corresponding to the socket.\n\
|
---|
| 2390 | The mode and buffersize arguments are as for the built-in open() function.");
|
---|
| 2391 |
|
---|
| 2392 | #endif /* NO_DUP */
|
---|
| 2393 |
|
---|
| 2394 | /*
|
---|
| 2395 | * This is the guts of the recv() and recv_into() methods, which reads into a
|
---|
| 2396 | * char buffer. If you have any inc/dec ref to do to the objects that contain
|
---|
| 2397 | * the buffer, do it in the caller. This function returns the number of bytes
|
---|
[391] | 2398 | * successfully read. If there was an error, it returns -1. Note that it is
|
---|
[2] | 2399 | * also possible that we return a number of bytes smaller than the request
|
---|
| 2400 | * bytes.
|
---|
| 2401 | */
|
---|
| 2402 | static ssize_t
|
---|
| 2403 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
|
---|
| 2404 | {
|
---|
[391] | 2405 | ssize_t outlen = -1;
|
---|
| 2406 | int timeout;
|
---|
[2] | 2407 | #ifdef __VMS
|
---|
[391] | 2408 | int remaining;
|
---|
| 2409 | char *read_buf;
|
---|
[2] | 2410 | #endif
|
---|
| 2411 |
|
---|
[391] | 2412 | if (!IS_SELECTABLE(s)) {
|
---|
| 2413 | select_error();
|
---|
| 2414 | return -1;
|
---|
| 2415 | }
|
---|
[2] | 2416 |
|
---|
| 2417 | #ifndef __VMS
|
---|
[391] | 2418 | BEGIN_SELECT_LOOP(s)
|
---|
| 2419 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2420 | timeout = internal_select_ex(s, 0, interval);
|
---|
| 2421 | if (!timeout)
|
---|
| 2422 | outlen = recv(s->sock_fd, cbuf, len, flags);
|
---|
| 2423 | Py_END_ALLOW_THREADS
|
---|
[2] | 2424 |
|
---|
[391] | 2425 | if (timeout == 1) {
|
---|
| 2426 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 2427 | return -1;
|
---|
| 2428 | }
|
---|
| 2429 | END_SELECT_LOOP(s)
|
---|
| 2430 | if (outlen < 0) {
|
---|
| 2431 | /* Note: the call to errorhandler() ALWAYS indirectly returned
|
---|
| 2432 | NULL, so ignore its return value */
|
---|
| 2433 | s->errorhandler();
|
---|
| 2434 | return -1;
|
---|
| 2435 | }
|
---|
[2] | 2436 | #else
|
---|
[391] | 2437 | read_buf = cbuf;
|
---|
| 2438 | remaining = len;
|
---|
| 2439 | while (remaining != 0) {
|
---|
| 2440 | unsigned int segment;
|
---|
| 2441 | int nread = -1;
|
---|
[2] | 2442 |
|
---|
[391] | 2443 | segment = remaining /SEGMENT_SIZE;
|
---|
| 2444 | if (segment != 0) {
|
---|
| 2445 | segment = SEGMENT_SIZE;
|
---|
| 2446 | }
|
---|
| 2447 | else {
|
---|
| 2448 | segment = remaining;
|
---|
| 2449 | }
|
---|
[2] | 2450 |
|
---|
[391] | 2451 | BEGIN_SELECT_LOOP(s)
|
---|
| 2452 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2453 | timeout = internal_select_ex(s, 0, interval);
|
---|
| 2454 | if (!timeout)
|
---|
| 2455 | nread = recv(s->sock_fd, read_buf, segment, flags);
|
---|
| 2456 | Py_END_ALLOW_THREADS
|
---|
[2] | 2457 |
|
---|
[391] | 2458 | if (timeout == 1) {
|
---|
| 2459 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 2460 | return -1;
|
---|
| 2461 | }
|
---|
| 2462 | END_SELECT_LOOP(s)
|
---|
[2] | 2463 |
|
---|
[391] | 2464 | if (nread < 0) {
|
---|
| 2465 | s->errorhandler();
|
---|
| 2466 | return -1;
|
---|
| 2467 | }
|
---|
| 2468 | if (nread != remaining) {
|
---|
| 2469 | read_buf += nread;
|
---|
| 2470 | break;
|
---|
| 2471 | }
|
---|
| 2472 |
|
---|
| 2473 | remaining -= segment;
|
---|
| 2474 | read_buf += segment;
|
---|
| 2475 | }
|
---|
| 2476 | outlen = read_buf - cbuf;
|
---|
[2] | 2477 | #endif /* !__VMS */
|
---|
| 2478 |
|
---|
[391] | 2479 | return outlen;
|
---|
[2] | 2480 | }
|
---|
| 2481 |
|
---|
| 2482 |
|
---|
| 2483 | /* s.recv(nbytes [,flags]) method */
|
---|
| 2484 |
|
---|
| 2485 | static PyObject *
|
---|
| 2486 | sock_recv(PySocketSockObject *s, PyObject *args)
|
---|
| 2487 | {
|
---|
[391] | 2488 | int recvlen, flags = 0;
|
---|
| 2489 | ssize_t outlen;
|
---|
| 2490 | PyObject *buf;
|
---|
[2] | 2491 |
|
---|
[391] | 2492 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
|
---|
| 2493 | return NULL;
|
---|
[2] | 2494 |
|
---|
[391] | 2495 | if (recvlen < 0) {
|
---|
| 2496 | PyErr_SetString(PyExc_ValueError,
|
---|
| 2497 | "negative buffersize in recv");
|
---|
| 2498 | return NULL;
|
---|
| 2499 | }
|
---|
[2] | 2500 |
|
---|
[391] | 2501 | /* Allocate a new string. */
|
---|
| 2502 | buf = PyString_FromStringAndSize((char *) 0, recvlen);
|
---|
| 2503 | if (buf == NULL)
|
---|
| 2504 | return NULL;
|
---|
[2] | 2505 |
|
---|
[391] | 2506 | /* Call the guts */
|
---|
| 2507 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);
|
---|
| 2508 | if (outlen < 0) {
|
---|
| 2509 | /* An error occurred, release the string and return an
|
---|
| 2510 | error. */
|
---|
| 2511 | Py_DECREF(buf);
|
---|
| 2512 | return NULL;
|
---|
| 2513 | }
|
---|
| 2514 | if (outlen != recvlen) {
|
---|
| 2515 | /* We did not read as many bytes as we anticipated, resize the
|
---|
| 2516 | string if possible and be successful. */
|
---|
| 2517 | if (_PyString_Resize(&buf, outlen) < 0)
|
---|
| 2518 | /* Oopsy, not so successful after all. */
|
---|
| 2519 | return NULL;
|
---|
| 2520 | }
|
---|
[2] | 2521 |
|
---|
[391] | 2522 | return buf;
|
---|
[2] | 2523 | }
|
---|
| 2524 |
|
---|
| 2525 | PyDoc_STRVAR(recv_doc,
|
---|
| 2526 | "recv(buffersize[, flags]) -> data\n\
|
---|
| 2527 | \n\
|
---|
| 2528 | Receive up to buffersize bytes from the socket. For the optional flags\n\
|
---|
| 2529 | argument, see the Unix manual. When no data is available, block until\n\
|
---|
| 2530 | at least one byte is available or until the remote end is closed. When\n\
|
---|
| 2531 | the remote end is closed and all data is read, return the empty string.");
|
---|
| 2532 |
|
---|
| 2533 |
|
---|
| 2534 | /* s.recv_into(buffer, [nbytes [,flags]]) method */
|
---|
| 2535 |
|
---|
| 2536 | static PyObject*
|
---|
| 2537 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
|
---|
| 2538 | {
|
---|
[391] | 2539 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
|
---|
[2] | 2540 |
|
---|
[391] | 2541 | int recvlen = 0, flags = 0;
|
---|
| 2542 | ssize_t readlen;
|
---|
| 2543 | Py_buffer buf;
|
---|
| 2544 | Py_ssize_t buflen;
|
---|
[2] | 2545 |
|
---|
[391] | 2546 | /* Get the buffer's memory */
|
---|
| 2547 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist,
|
---|
| 2548 | &buf, &recvlen, &flags))
|
---|
| 2549 | return NULL;
|
---|
| 2550 | buflen = buf.len;
|
---|
| 2551 | assert(buf.buf != 0 && buflen > 0);
|
---|
[2] | 2552 |
|
---|
[391] | 2553 | if (recvlen < 0) {
|
---|
| 2554 | PyErr_SetString(PyExc_ValueError,
|
---|
| 2555 | "negative buffersize in recv_into");
|
---|
| 2556 | goto error;
|
---|
| 2557 | }
|
---|
| 2558 | if (recvlen == 0) {
|
---|
| 2559 | /* If nbytes was not specified, use the buffer's length */
|
---|
| 2560 | recvlen = buflen;
|
---|
| 2561 | }
|
---|
[2] | 2562 |
|
---|
[391] | 2563 | /* Check if the buffer is large enough */
|
---|
| 2564 | if (buflen < recvlen) {
|
---|
| 2565 | PyErr_SetString(PyExc_ValueError,
|
---|
| 2566 | "buffer too small for requested bytes");
|
---|
| 2567 | goto error;
|
---|
| 2568 | }
|
---|
[2] | 2569 |
|
---|
[391] | 2570 | /* Call the guts */
|
---|
| 2571 | readlen = sock_recv_guts(s, buf.buf, recvlen, flags);
|
---|
| 2572 | if (readlen < 0) {
|
---|
| 2573 | /* Return an error. */
|
---|
| 2574 | goto error;
|
---|
| 2575 | }
|
---|
[2] | 2576 |
|
---|
[391] | 2577 | PyBuffer_Release(&buf);
|
---|
| 2578 | /* Return the number of bytes read. Note that we do not do anything
|
---|
| 2579 | special here in the case that readlen < recvlen. */
|
---|
| 2580 | return PyInt_FromSsize_t(readlen);
|
---|
| 2581 |
|
---|
| 2582 | error:
|
---|
| 2583 | PyBuffer_Release(&buf);
|
---|
| 2584 | return NULL;
|
---|
[2] | 2585 | }
|
---|
| 2586 |
|
---|
| 2587 | PyDoc_STRVAR(recv_into_doc,
|
---|
| 2588 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
|
---|
| 2589 | \n\
|
---|
| 2590 | A version of recv() that stores its data into a buffer rather than creating \n\
|
---|
| 2591 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
|
---|
| 2592 | is not specified (or 0), receive up to the size available in the given buffer.\n\
|
---|
| 2593 | \n\
|
---|
| 2594 | See recv() for documentation about the flags.");
|
---|
| 2595 |
|
---|
| 2596 |
|
---|
| 2597 | /*
|
---|
| 2598 | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
|
---|
| 2599 | * into a char buffer. If you have any inc/def ref to do to the objects that
|
---|
| 2600 | * contain the buffer, do it in the caller. This function returns the number
|
---|
[391] | 2601 | * of bytes successfully read. If there was an error, it returns -1. Note
|
---|
[2] | 2602 | * that it is also possible that we return a number of bytes smaller than the
|
---|
| 2603 | * request bytes.
|
---|
| 2604 | *
|
---|
| 2605 | * 'addr' is a return value for the address object. Note that you must decref
|
---|
| 2606 | * it yourself.
|
---|
| 2607 | */
|
---|
| 2608 | static ssize_t
|
---|
| 2609 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
|
---|
[391] | 2610 | PyObject** addr)
|
---|
[2] | 2611 | {
|
---|
[391] | 2612 | sock_addr_t addrbuf;
|
---|
| 2613 | int timeout;
|
---|
| 2614 | ssize_t n = -1;
|
---|
| 2615 | socklen_t addrlen;
|
---|
[2] | 2616 |
|
---|
[391] | 2617 | *addr = NULL;
|
---|
[2] | 2618 |
|
---|
[391] | 2619 | if (!getsockaddrlen(s, &addrlen))
|
---|
| 2620 | return -1;
|
---|
[2] | 2621 |
|
---|
[391] | 2622 | if (!IS_SELECTABLE(s)) {
|
---|
| 2623 | select_error();
|
---|
| 2624 | return -1;
|
---|
| 2625 | }
|
---|
[2] | 2626 |
|
---|
[391] | 2627 | BEGIN_SELECT_LOOP(s)
|
---|
| 2628 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2629 | memset(&addrbuf, 0, addrlen);
|
---|
| 2630 | timeout = internal_select_ex(s, 0, interval);
|
---|
| 2631 | if (!timeout) {
|
---|
[2] | 2632 | #ifndef MS_WINDOWS
|
---|
| 2633 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
[391] | 2634 | n = recvfrom(s->sock_fd, cbuf, len, flags,
|
---|
| 2635 | SAS2SA(&addrbuf), &addrlen);
|
---|
[2] | 2636 | #else
|
---|
[391] | 2637 | n = recvfrom(s->sock_fd, cbuf, len, flags,
|
---|
| 2638 | (void *) &addrbuf, &addrlen);
|
---|
[2] | 2639 | #endif
|
---|
| 2640 | #else
|
---|
[391] | 2641 | n = recvfrom(s->sock_fd, cbuf, len, flags,
|
---|
| 2642 | SAS2SA(&addrbuf), &addrlen);
|
---|
[2] | 2643 | #endif
|
---|
[391] | 2644 | }
|
---|
| 2645 | Py_END_ALLOW_THREADS
|
---|
[2] | 2646 |
|
---|
[391] | 2647 | if (timeout == 1) {
|
---|
| 2648 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 2649 | return -1;
|
---|
| 2650 | }
|
---|
| 2651 | END_SELECT_LOOP(s)
|
---|
| 2652 | if (n < 0) {
|
---|
| 2653 | s->errorhandler();
|
---|
| 2654 | return -1;
|
---|
| 2655 | }
|
---|
[2] | 2656 |
|
---|
[391] | 2657 | if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
|
---|
| 2658 | addrlen, s->sock_proto)))
|
---|
| 2659 | return -1;
|
---|
[2] | 2660 |
|
---|
[391] | 2661 | return n;
|
---|
[2] | 2662 | }
|
---|
| 2663 |
|
---|
| 2664 | /* s.recvfrom(nbytes [,flags]) method */
|
---|
| 2665 |
|
---|
| 2666 | static PyObject *
|
---|
| 2667 | sock_recvfrom(PySocketSockObject *s, PyObject *args)
|
---|
| 2668 | {
|
---|
[391] | 2669 | PyObject *buf = NULL;
|
---|
| 2670 | PyObject *addr = NULL;
|
---|
| 2671 | PyObject *ret = NULL;
|
---|
| 2672 | int recvlen, flags = 0;
|
---|
| 2673 | ssize_t outlen;
|
---|
[2] | 2674 |
|
---|
[391] | 2675 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
|
---|
| 2676 | return NULL;
|
---|
[2] | 2677 |
|
---|
[391] | 2678 | if (recvlen < 0) {
|
---|
| 2679 | PyErr_SetString(PyExc_ValueError,
|
---|
| 2680 | "negative buffersize in recvfrom");
|
---|
| 2681 | return NULL;
|
---|
| 2682 | }
|
---|
[2] | 2683 |
|
---|
[391] | 2684 | buf = PyString_FromStringAndSize((char *) 0, recvlen);
|
---|
| 2685 | if (buf == NULL)
|
---|
| 2686 | return NULL;
|
---|
[2] | 2687 |
|
---|
[391] | 2688 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),
|
---|
| 2689 | recvlen, flags, &addr);
|
---|
| 2690 | if (outlen < 0) {
|
---|
| 2691 | goto finally;
|
---|
| 2692 | }
|
---|
[2] | 2693 |
|
---|
[391] | 2694 | if (outlen != recvlen) {
|
---|
| 2695 | /* We did not read as many bytes as we anticipated, resize the
|
---|
| 2696 | string if possible and be successful. */
|
---|
| 2697 | if (_PyString_Resize(&buf, outlen) < 0)
|
---|
| 2698 | /* Oopsy, not so successful after all. */
|
---|
| 2699 | goto finally;
|
---|
| 2700 | }
|
---|
[2] | 2701 |
|
---|
[391] | 2702 | ret = PyTuple_Pack(2, buf, addr);
|
---|
[2] | 2703 |
|
---|
| 2704 | finally:
|
---|
[391] | 2705 | Py_XDECREF(buf);
|
---|
| 2706 | Py_XDECREF(addr);
|
---|
| 2707 | return ret;
|
---|
[2] | 2708 | }
|
---|
| 2709 |
|
---|
| 2710 | PyDoc_STRVAR(recvfrom_doc,
|
---|
| 2711 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\
|
---|
| 2712 | \n\
|
---|
| 2713 | Like recv(buffersize, flags) but also return the sender's address info.");
|
---|
| 2714 |
|
---|
| 2715 |
|
---|
| 2716 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
|
---|
| 2717 |
|
---|
| 2718 | static PyObject *
|
---|
| 2719 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
|
---|
| 2720 | {
|
---|
[391] | 2721 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
|
---|
[2] | 2722 |
|
---|
[391] | 2723 | int recvlen = 0, flags = 0;
|
---|
| 2724 | ssize_t readlen;
|
---|
| 2725 | Py_buffer buf;
|
---|
| 2726 | int buflen;
|
---|
[2] | 2727 |
|
---|
[391] | 2728 | PyObject *addr = NULL;
|
---|
[2] | 2729 |
|
---|
[391] | 2730 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into",
|
---|
| 2731 | kwlist, &buf,
|
---|
| 2732 | &recvlen, &flags))
|
---|
| 2733 | return NULL;
|
---|
| 2734 | buflen = buf.len;
|
---|
| 2735 | assert(buf.buf != 0 && buflen > 0);
|
---|
[2] | 2736 |
|
---|
[391] | 2737 | if (recvlen < 0) {
|
---|
| 2738 | PyErr_SetString(PyExc_ValueError,
|
---|
| 2739 | "negative buffersize in recvfrom_into");
|
---|
| 2740 | goto error;
|
---|
| 2741 | }
|
---|
| 2742 | if (recvlen == 0) {
|
---|
| 2743 | /* If nbytes was not specified, use the buffer's length */
|
---|
| 2744 | recvlen = buflen;
|
---|
| 2745 | }
|
---|
[2] | 2746 |
|
---|
[391] | 2747 | readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr);
|
---|
| 2748 | if (readlen < 0) {
|
---|
| 2749 | /* Return an error */
|
---|
| 2750 | goto error;
|
---|
| 2751 | }
|
---|
[2] | 2752 |
|
---|
[391] | 2753 | PyBuffer_Release(&buf);
|
---|
| 2754 | /* Return the number of bytes read and the address. Note that we do
|
---|
| 2755 | not do anything special here in the case that readlen < recvlen. */
|
---|
| 2756 | return Py_BuildValue("lN", readlen, addr);
|
---|
| 2757 |
|
---|
| 2758 | error:
|
---|
| 2759 | Py_XDECREF(addr);
|
---|
| 2760 | PyBuffer_Release(&buf);
|
---|
| 2761 | return NULL;
|
---|
[2] | 2762 | }
|
---|
| 2763 |
|
---|
| 2764 | PyDoc_STRVAR(recvfrom_into_doc,
|
---|
| 2765 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
|
---|
| 2766 | \n\
|
---|
| 2767 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
|
---|
| 2768 |
|
---|
| 2769 |
|
---|
| 2770 | /* s.send(data [,flags]) method */
|
---|
| 2771 |
|
---|
| 2772 | static PyObject *
|
---|
| 2773 | sock_send(PySocketSockObject *s, PyObject *args)
|
---|
| 2774 | {
|
---|
[391] | 2775 | char *buf;
|
---|
| 2776 | int len, n = -1, flags = 0, timeout;
|
---|
| 2777 | Py_buffer pbuf;
|
---|
[2] | 2778 |
|
---|
[391] | 2779 | if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags))
|
---|
| 2780 | return NULL;
|
---|
[2] | 2781 |
|
---|
[391] | 2782 | if (!IS_SELECTABLE(s)) {
|
---|
| 2783 | PyBuffer_Release(&pbuf);
|
---|
| 2784 | return select_error();
|
---|
| 2785 | }
|
---|
| 2786 | buf = pbuf.buf;
|
---|
| 2787 | len = pbuf.len;
|
---|
[2] | 2788 |
|
---|
[391] | 2789 | BEGIN_SELECT_LOOP(s)
|
---|
| 2790 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2791 | timeout = internal_select_ex(s, 1, interval);
|
---|
| 2792 | if (!timeout)
|
---|
[2] | 2793 | #ifdef __VMS
|
---|
[391] | 2794 | n = sendsegmented(s->sock_fd, buf, len, flags);
|
---|
[2] | 2795 | #else
|
---|
[391] | 2796 | n = send(s->sock_fd, buf, len, flags);
|
---|
[2] | 2797 | #endif
|
---|
[391] | 2798 | Py_END_ALLOW_THREADS
|
---|
| 2799 | if (timeout == 1) {
|
---|
| 2800 | PyBuffer_Release(&pbuf);
|
---|
| 2801 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 2802 | return NULL;
|
---|
| 2803 | }
|
---|
| 2804 | END_SELECT_LOOP(s)
|
---|
[2] | 2805 |
|
---|
[391] | 2806 | PyBuffer_Release(&pbuf);
|
---|
| 2807 | if (n < 0)
|
---|
| 2808 | return s->errorhandler();
|
---|
| 2809 | return PyInt_FromLong((long)n);
|
---|
[2] | 2810 | }
|
---|
| 2811 |
|
---|
| 2812 | PyDoc_STRVAR(send_doc,
|
---|
| 2813 | "send(data[, flags]) -> count\n\
|
---|
| 2814 | \n\
|
---|
| 2815 | Send a data string to the socket. For the optional flags\n\
|
---|
| 2816 | argument, see the Unix manual. Return the number of bytes\n\
|
---|
| 2817 | sent; this may be less than len(data) if the network is busy.");
|
---|
| 2818 |
|
---|
| 2819 |
|
---|
| 2820 | /* s.sendall(data [,flags]) method */
|
---|
| 2821 |
|
---|
| 2822 | static PyObject *
|
---|
| 2823 | sock_sendall(PySocketSockObject *s, PyObject *args)
|
---|
| 2824 | {
|
---|
[391] | 2825 | char *buf;
|
---|
| 2826 | int len, n = -1, flags = 0, timeout, saved_errno;
|
---|
| 2827 | Py_buffer pbuf;
|
---|
[2] | 2828 |
|
---|
[391] | 2829 | if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags))
|
---|
| 2830 | return NULL;
|
---|
| 2831 | buf = pbuf.buf;
|
---|
| 2832 | len = pbuf.len;
|
---|
[2] | 2833 |
|
---|
[391] | 2834 | if (!IS_SELECTABLE(s)) {
|
---|
| 2835 | PyBuffer_Release(&pbuf);
|
---|
| 2836 | return select_error();
|
---|
| 2837 | }
|
---|
[2] | 2838 |
|
---|
[391] | 2839 | do {
|
---|
| 2840 | BEGIN_SELECT_LOOP(s)
|
---|
| 2841 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2842 | timeout = internal_select_ex(s, 1, interval);
|
---|
| 2843 | n = -1;
|
---|
| 2844 | if (!timeout) {
|
---|
[2] | 2845 | #ifdef __VMS
|
---|
[391] | 2846 | n = sendsegmented(s->sock_fd, buf, len, flags);
|
---|
[2] | 2847 | #else
|
---|
[391] | 2848 | n = send(s->sock_fd, buf, len, flags);
|
---|
[2] | 2849 | #endif
|
---|
[391] | 2850 | }
|
---|
| 2851 | Py_END_ALLOW_THREADS
|
---|
| 2852 | if (timeout == 1) {
|
---|
| 2853 | PyBuffer_Release(&pbuf);
|
---|
| 2854 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 2855 | return NULL;
|
---|
| 2856 | }
|
---|
| 2857 | END_SELECT_LOOP(s)
|
---|
| 2858 | /* PyErr_CheckSignals() might change errno */
|
---|
| 2859 | saved_errno = errno;
|
---|
| 2860 | /* We must run our signal handlers before looping again.
|
---|
| 2861 | send() can return a successful partial write when it is
|
---|
| 2862 | interrupted, so we can't restrict ourselves to EINTR. */
|
---|
| 2863 | if (PyErr_CheckSignals()) {
|
---|
| 2864 | PyBuffer_Release(&pbuf);
|
---|
| 2865 | return NULL;
|
---|
| 2866 | }
|
---|
| 2867 | if (n < 0) {
|
---|
| 2868 | /* If interrupted, try again */
|
---|
| 2869 | if (saved_errno == EINTR)
|
---|
| 2870 | continue;
|
---|
| 2871 | else
|
---|
| 2872 | break;
|
---|
| 2873 | }
|
---|
| 2874 | buf += n;
|
---|
| 2875 | len -= n;
|
---|
| 2876 | } while (len > 0);
|
---|
| 2877 | PyBuffer_Release(&pbuf);
|
---|
[2] | 2878 |
|
---|
[391] | 2879 | if (n < 0)
|
---|
| 2880 | return s->errorhandler();
|
---|
[2] | 2881 |
|
---|
[391] | 2882 | Py_INCREF(Py_None);
|
---|
| 2883 | return Py_None;
|
---|
[2] | 2884 | }
|
---|
| 2885 |
|
---|
| 2886 | PyDoc_STRVAR(sendall_doc,
|
---|
| 2887 | "sendall(data[, flags])\n\
|
---|
| 2888 | \n\
|
---|
| 2889 | Send a data string to the socket. For the optional flags\n\
|
---|
| 2890 | argument, see the Unix manual. This calls send() repeatedly\n\
|
---|
| 2891 | until all data is sent. If an error occurs, it's impossible\n\
|
---|
| 2892 | to tell how much data has been sent.");
|
---|
| 2893 |
|
---|
| 2894 |
|
---|
| 2895 | /* s.sendto(data, [flags,] sockaddr) method */
|
---|
| 2896 |
|
---|
| 2897 | static PyObject *
|
---|
| 2898 | sock_sendto(PySocketSockObject *s, PyObject *args)
|
---|
| 2899 | {
|
---|
[391] | 2900 | Py_buffer pbuf;
|
---|
| 2901 | PyObject *addro;
|
---|
| 2902 | char *buf;
|
---|
| 2903 | Py_ssize_t len;
|
---|
| 2904 | sock_addr_t addrbuf;
|
---|
| 2905 | int addrlen, n = -1, flags, timeout;
|
---|
| 2906 | int arglen;
|
---|
[2] | 2907 |
|
---|
[391] | 2908 | flags = 0;
|
---|
| 2909 | arglen = PyTuple_Size(args);
|
---|
| 2910 | switch(arglen) {
|
---|
| 2911 | case 2:
|
---|
| 2912 | PyArg_ParseTuple(args, "s*O:sendto", &pbuf, &addro);
|
---|
| 2913 | break;
|
---|
| 2914 | case 3:
|
---|
| 2915 | PyArg_ParseTuple(args, "s*iO:sendto", &pbuf, &flags, &addro);
|
---|
| 2916 | break;
|
---|
| 2917 | default:
|
---|
| 2918 | PyErr_Format(PyExc_TypeError, "sendto() takes 2 or 3"
|
---|
| 2919 | " arguments (%d given)", arglen);
|
---|
| 2920 | }
|
---|
| 2921 | if (PyErr_Occurred())
|
---|
| 2922 | return NULL;
|
---|
[2] | 2923 |
|
---|
[391] | 2924 | buf = pbuf.buf;
|
---|
| 2925 | len = pbuf.len;
|
---|
[2] | 2926 |
|
---|
[391] | 2927 | if (!IS_SELECTABLE(s)) {
|
---|
| 2928 | PyBuffer_Release(&pbuf);
|
---|
| 2929 | return select_error();
|
---|
| 2930 | }
|
---|
[2] | 2931 |
|
---|
[391] | 2932 | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
|
---|
| 2933 | PyBuffer_Release(&pbuf);
|
---|
| 2934 | return NULL;
|
---|
| 2935 | }
|
---|
[2] | 2936 |
|
---|
[391] | 2937 | BEGIN_SELECT_LOOP(s)
|
---|
| 2938 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2939 | timeout = internal_select_ex(s, 1, interval);
|
---|
| 2940 | if (!timeout)
|
---|
| 2941 | n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
|
---|
| 2942 | Py_END_ALLOW_THREADS
|
---|
| 2943 |
|
---|
| 2944 | if (timeout == 1) {
|
---|
| 2945 | PyBuffer_Release(&pbuf);
|
---|
| 2946 | PyErr_SetString(socket_timeout, "timed out");
|
---|
| 2947 | return NULL;
|
---|
| 2948 | }
|
---|
| 2949 | END_SELECT_LOOP(s)
|
---|
| 2950 | PyBuffer_Release(&pbuf);
|
---|
| 2951 | if (n < 0)
|
---|
| 2952 | return s->errorhandler();
|
---|
| 2953 | return PyInt_FromLong((long)n);
|
---|
[2] | 2954 | }
|
---|
| 2955 |
|
---|
| 2956 | PyDoc_STRVAR(sendto_doc,
|
---|
| 2957 | "sendto(data[, flags], address) -> count\n\
|
---|
| 2958 | \n\
|
---|
| 2959 | Like send(data, flags) but allows specifying the destination address.\n\
|
---|
| 2960 | For IP sockets, the address is a pair (hostaddr, port).");
|
---|
| 2961 |
|
---|
| 2962 |
|
---|
| 2963 | /* s.shutdown(how) method */
|
---|
| 2964 |
|
---|
| 2965 | static PyObject *
|
---|
| 2966 | sock_shutdown(PySocketSockObject *s, PyObject *arg)
|
---|
| 2967 | {
|
---|
[391] | 2968 | int how;
|
---|
| 2969 | int res;
|
---|
[2] | 2970 |
|
---|
[391] | 2971 | how = _PyInt_AsInt(arg);
|
---|
| 2972 | if (how == -1 && PyErr_Occurred())
|
---|
| 2973 | return NULL;
|
---|
| 2974 | Py_BEGIN_ALLOW_THREADS
|
---|
| 2975 | res = shutdown(s->sock_fd, how);
|
---|
| 2976 | Py_END_ALLOW_THREADS
|
---|
| 2977 | if (res < 0)
|
---|
| 2978 | return s->errorhandler();
|
---|
| 2979 | Py_INCREF(Py_None);
|
---|
| 2980 | return Py_None;
|
---|
[2] | 2981 | }
|
---|
| 2982 |
|
---|
| 2983 | PyDoc_STRVAR(shutdown_doc,
|
---|
| 2984 | "shutdown(flag)\n\
|
---|
| 2985 | \n\
|
---|
| 2986 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
|
---|
| 2987 | of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
|
---|
| 2988 |
|
---|
| 2989 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL)
|
---|
| 2990 | static PyObject*
|
---|
| 2991 | sock_ioctl(PySocketSockObject *s, PyObject *arg)
|
---|
| 2992 | {
|
---|
[391] | 2993 | unsigned long cmd = SIO_RCVALL;
|
---|
| 2994 | PyObject *argO;
|
---|
| 2995 | DWORD recv;
|
---|
[2] | 2996 |
|
---|
[391] | 2997 | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
|
---|
| 2998 | return NULL;
|
---|
[2] | 2999 |
|
---|
[391] | 3000 | switch (cmd) {
|
---|
| 3001 | case SIO_RCVALL: {
|
---|
| 3002 | unsigned int option = RCVALL_ON;
|
---|
| 3003 | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
|
---|
| 3004 | return NULL;
|
---|
| 3005 | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
|
---|
| 3006 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
|
---|
| 3007 | return set_error();
|
---|
| 3008 | }
|
---|
| 3009 | return PyLong_FromUnsignedLong(recv); }
|
---|
| 3010 | case SIO_KEEPALIVE_VALS: {
|
---|
| 3011 | struct tcp_keepalive ka;
|
---|
| 3012 | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
|
---|
| 3013 | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
|
---|
| 3014 | return NULL;
|
---|
| 3015 | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
|
---|
| 3016 | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
|
---|
| 3017 | return set_error();
|
---|
| 3018 | }
|
---|
| 3019 | return PyLong_FromUnsignedLong(recv); }
|
---|
| 3020 | default:
|
---|
| 3021 | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
|
---|
| 3022 | return NULL;
|
---|
| 3023 | }
|
---|
[2] | 3024 | }
|
---|
| 3025 | PyDoc_STRVAR(sock_ioctl_doc,
|
---|
| 3026 | "ioctl(cmd, option) -> long\n\
|
---|
| 3027 | \n\
|
---|
[391] | 3028 | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
|
---|
| 3029 | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
|
---|
| 3030 | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
|
---|
[2] | 3031 |
|
---|
| 3032 | #endif
|
---|
| 3033 |
|
---|
| 3034 | /* List of methods for socket objects */
|
---|
| 3035 |
|
---|
| 3036 | static PyMethodDef sock_methods[] = {
|
---|
[391] | 3037 | {"accept", (PyCFunction)sock_accept, METH_NOARGS,
|
---|
| 3038 | accept_doc},
|
---|
| 3039 | {"bind", (PyCFunction)sock_bind, METH_O,
|
---|
| 3040 | bind_doc},
|
---|
| 3041 | {"close", (PyCFunction)sock_close, METH_NOARGS,
|
---|
| 3042 | close_doc},
|
---|
| 3043 | {"connect", (PyCFunction)sock_connect, METH_O,
|
---|
| 3044 | connect_doc},
|
---|
| 3045 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
|
---|
| 3046 | connect_ex_doc},
|
---|
[2] | 3047 | #ifndef NO_DUP
|
---|
[391] | 3048 | {"dup", (PyCFunction)sock_dup, METH_NOARGS,
|
---|
| 3049 | dup_doc},
|
---|
[2] | 3050 | #endif
|
---|
[391] | 3051 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
|
---|
| 3052 | fileno_doc},
|
---|
[2] | 3053 | #ifdef HAVE_GETPEERNAME
|
---|
[391] | 3054 | {"getpeername", (PyCFunction)sock_getpeername,
|
---|
| 3055 | METH_NOARGS, getpeername_doc},
|
---|
[2] | 3056 | #endif
|
---|
[391] | 3057 | {"getsockname", (PyCFunction)sock_getsockname,
|
---|
| 3058 | METH_NOARGS, getsockname_doc},
|
---|
| 3059 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
|
---|
| 3060 | getsockopt_doc},
|
---|
[2] | 3061 | #if defined(MS_WINDOWS) && defined(SIO_RCVALL)
|
---|
[391] | 3062 | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
|
---|
| 3063 | sock_ioctl_doc},
|
---|
[2] | 3064 | #endif
|
---|
[391] | 3065 | {"listen", (PyCFunction)sock_listen, METH_O,
|
---|
| 3066 | listen_doc},
|
---|
[2] | 3067 | #ifndef NO_DUP
|
---|
[391] | 3068 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
|
---|
| 3069 | makefile_doc},
|
---|
[2] | 3070 | #endif
|
---|
[391] | 3071 | {"recv", (PyCFunction)sock_recv, METH_VARARGS,
|
---|
| 3072 | recv_doc},
|
---|
| 3073 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
|
---|
| 3074 | recv_into_doc},
|
---|
| 3075 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
|
---|
| 3076 | recvfrom_doc},
|
---|
| 3077 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
|
---|
| 3078 | recvfrom_into_doc},
|
---|
| 3079 | {"send", (PyCFunction)sock_send, METH_VARARGS,
|
---|
| 3080 | send_doc},
|
---|
| 3081 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
|
---|
| 3082 | sendall_doc},
|
---|
| 3083 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
|
---|
| 3084 | sendto_doc},
|
---|
| 3085 | {"setblocking", (PyCFunction)sock_setblocking, METH_O,
|
---|
| 3086 | setblocking_doc},
|
---|
| 3087 | {"settimeout", (PyCFunction)sock_settimeout, METH_O,
|
---|
| 3088 | settimeout_doc},
|
---|
| 3089 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
|
---|
| 3090 | gettimeout_doc},
|
---|
| 3091 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
|
---|
| 3092 | setsockopt_doc},
|
---|
| 3093 | {"shutdown", (PyCFunction)sock_shutdown, METH_O,
|
---|
| 3094 | shutdown_doc},
|
---|
[2] | 3095 | #ifdef RISCOS
|
---|
[391] | 3096 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
|
---|
| 3097 | sleeptaskw_doc},
|
---|
[2] | 3098 | #endif
|
---|
[391] | 3099 | {NULL, NULL} /* sentinel */
|
---|
[2] | 3100 | };
|
---|
| 3101 |
|
---|
| 3102 | /* SockObject members */
|
---|
| 3103 | static PyMemberDef sock_memberlist[] = {
|
---|
| 3104 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
|
---|
| 3105 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
|
---|
| 3106 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
|
---|
| 3107 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
|
---|
| 3108 | {0},
|
---|
| 3109 | };
|
---|
| 3110 |
|
---|
| 3111 | /* Deallocate a socket object in response to the last Py_DECREF().
|
---|
| 3112 | First close the file description. */
|
---|
| 3113 |
|
---|
| 3114 | static void
|
---|
| 3115 | sock_dealloc(PySocketSockObject *s)
|
---|
| 3116 | {
|
---|
[391] | 3117 | if (s->sock_fd != -1)
|
---|
| 3118 | (void) SOCKETCLOSE(s->sock_fd);
|
---|
| 3119 | Py_TYPE(s)->tp_free((PyObject *)s);
|
---|
[2] | 3120 | }
|
---|
| 3121 |
|
---|
| 3122 |
|
---|
| 3123 | static PyObject *
|
---|
| 3124 | sock_repr(PySocketSockObject *s)
|
---|
| 3125 | {
|
---|
[391] | 3126 | char buf[512];
|
---|
[2] | 3127 | #if SIZEOF_SOCKET_T > SIZEOF_LONG
|
---|
[391] | 3128 | if (s->sock_fd > LONG_MAX) {
|
---|
| 3129 | /* this can occur on Win64, and actually there is a special
|
---|
| 3130 | ugly printf formatter for decimal pointer length integer
|
---|
| 3131 | printing, only bother if necessary*/
|
---|
| 3132 | PyErr_SetString(PyExc_OverflowError,
|
---|
| 3133 | "no printf formatter to display "
|
---|
| 3134 | "the socket descriptor in decimal");
|
---|
| 3135 | return NULL;
|
---|
| 3136 | }
|
---|
[2] | 3137 | #endif
|
---|
[391] | 3138 | PyOS_snprintf(
|
---|
| 3139 | buf, sizeof(buf),
|
---|
| 3140 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
|
---|
| 3141 | (long)s->sock_fd, s->sock_family,
|
---|
| 3142 | s->sock_type,
|
---|
| 3143 | s->sock_proto);
|
---|
| 3144 | return PyString_FromString(buf);
|
---|
[2] | 3145 | }
|
---|
| 3146 |
|
---|
| 3147 |
|
---|
| 3148 | /* Create a new, uninitialized socket object. */
|
---|
| 3149 |
|
---|
| 3150 | static PyObject *
|
---|
| 3151 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
---|
| 3152 | {
|
---|
[391] | 3153 | PyObject *new;
|
---|
[2] | 3154 |
|
---|
[391] | 3155 | new = type->tp_alloc(type, 0);
|
---|
| 3156 | if (new != NULL) {
|
---|
| 3157 | ((PySocketSockObject *)new)->sock_fd = -1;
|
---|
| 3158 | ((PySocketSockObject *)new)->sock_timeout = -1.0;
|
---|
| 3159 | ((PySocketSockObject *)new)->errorhandler = &set_error;
|
---|
| 3160 | }
|
---|
| 3161 | return new;
|
---|
[2] | 3162 | }
|
---|
| 3163 |
|
---|
| 3164 |
|
---|
| 3165 | /* Initialize a new socket object. */
|
---|
| 3166 |
|
---|
| 3167 | /*ARGSUSED*/
|
---|
| 3168 | static int
|
---|
| 3169 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
|
---|
| 3170 | {
|
---|
[391] | 3171 | PySocketSockObject *s = (PySocketSockObject *)self;
|
---|
| 3172 | SOCKET_T fd;
|
---|
| 3173 | int family = AF_INET, type = SOCK_STREAM, proto = 0;
|
---|
| 3174 | static char *keywords[] = {"family", "type", "proto", 0};
|
---|
[2] | 3175 |
|
---|
[391] | 3176 | if (!PyArg_ParseTupleAndKeywords(args, kwds,
|
---|
| 3177 | "|iii:socket", keywords,
|
---|
| 3178 | &family, &type, &proto))
|
---|
| 3179 | return -1;
|
---|
[2] | 3180 |
|
---|
[391] | 3181 | Py_BEGIN_ALLOW_THREADS
|
---|
| 3182 | fd = socket(family, type, proto);
|
---|
| 3183 | Py_END_ALLOW_THREADS
|
---|
[2] | 3184 |
|
---|
| 3185 | #ifdef MS_WINDOWS
|
---|
[391] | 3186 | if (fd == INVALID_SOCKET)
|
---|
[2] | 3187 | #else
|
---|
[391] | 3188 | if (fd < 0)
|
---|
[2] | 3189 | #endif
|
---|
[391] | 3190 | {
|
---|
| 3191 | set_error();
|
---|
| 3192 | return -1;
|
---|
| 3193 | }
|
---|
| 3194 | init_sockobject(s, fd, family, type, proto);
|
---|
[2] | 3195 |
|
---|
[391] | 3196 | return 0;
|
---|
[2] | 3197 |
|
---|
| 3198 | }
|
---|
| 3199 |
|
---|
| 3200 |
|
---|
| 3201 | /* Type object for socket objects. */
|
---|
| 3202 |
|
---|
| 3203 | static PyTypeObject sock_type = {
|
---|
[391] | 3204 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
|
---|
| 3205 | "_socket.socket", /* tp_name */
|
---|
| 3206 | sizeof(PySocketSockObject), /* tp_basicsize */
|
---|
| 3207 | 0, /* tp_itemsize */
|
---|
| 3208 | (destructor)sock_dealloc, /* tp_dealloc */
|
---|
| 3209 | 0, /* tp_print */
|
---|
| 3210 | 0, /* tp_getattr */
|
---|
| 3211 | 0, /* tp_setattr */
|
---|
| 3212 | 0, /* tp_compare */
|
---|
| 3213 | (reprfunc)sock_repr, /* tp_repr */
|
---|
| 3214 | 0, /* tp_as_number */
|
---|
| 3215 | 0, /* tp_as_sequence */
|
---|
| 3216 | 0, /* tp_as_mapping */
|
---|
| 3217 | 0, /* tp_hash */
|
---|
| 3218 | 0, /* tp_call */
|
---|
| 3219 | 0, /* tp_str */
|
---|
| 3220 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
| 3221 | 0, /* tp_setattro */
|
---|
| 3222 | 0, /* tp_as_buffer */
|
---|
| 3223 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
---|
| 3224 | sock_doc, /* tp_doc */
|
---|
| 3225 | 0, /* tp_traverse */
|
---|
| 3226 | 0, /* tp_clear */
|
---|
| 3227 | 0, /* tp_richcompare */
|
---|
| 3228 | 0, /* tp_weaklistoffset */
|
---|
| 3229 | 0, /* tp_iter */
|
---|
| 3230 | 0, /* tp_iternext */
|
---|
| 3231 | sock_methods, /* tp_methods */
|
---|
| 3232 | sock_memberlist, /* tp_members */
|
---|
| 3233 | 0, /* tp_getset */
|
---|
| 3234 | 0, /* tp_base */
|
---|
| 3235 | 0, /* tp_dict */
|
---|
| 3236 | 0, /* tp_descr_get */
|
---|
| 3237 | 0, /* tp_descr_set */
|
---|
| 3238 | 0, /* tp_dictoffset */
|
---|
| 3239 | sock_initobj, /* tp_init */
|
---|
| 3240 | PyType_GenericAlloc, /* tp_alloc */
|
---|
| 3241 | sock_new, /* tp_new */
|
---|
| 3242 | PyObject_Del, /* tp_free */
|
---|
[2] | 3243 | };
|
---|
| 3244 |
|
---|
| 3245 |
|
---|
| 3246 | /* Python interface to gethostname(). */
|
---|
| 3247 |
|
---|
| 3248 | /*ARGSUSED*/
|
---|
| 3249 | static PyObject *
|
---|
| 3250 | socket_gethostname(PyObject *self, PyObject *unused)
|
---|
| 3251 | {
|
---|
[391] | 3252 | char buf[1024];
|
---|
| 3253 | int res;
|
---|
| 3254 | Py_BEGIN_ALLOW_THREADS
|
---|
| 3255 | res = gethostname(buf, (int) sizeof buf - 1);
|
---|
| 3256 | Py_END_ALLOW_THREADS
|
---|
| 3257 | if (res < 0)
|
---|
| 3258 | return set_error();
|
---|
| 3259 | buf[sizeof buf - 1] = '\0';
|
---|
| 3260 | return PyString_FromString(buf);
|
---|
[2] | 3261 | }
|
---|
| 3262 |
|
---|
| 3263 | PyDoc_STRVAR(gethostname_doc,
|
---|
| 3264 | "gethostname() -> string\n\
|
---|
| 3265 | \n\
|
---|
| 3266 | Return the current host name.");
|
---|
| 3267 |
|
---|
| 3268 |
|
---|
| 3269 | /* Python interface to gethostbyname(name). */
|
---|
| 3270 |
|
---|
| 3271 | /*ARGSUSED*/
|
---|
| 3272 | static PyObject *
|
---|
| 3273 | socket_gethostbyname(PyObject *self, PyObject *args)
|
---|
| 3274 | {
|
---|
[391] | 3275 | char *name;
|
---|
| 3276 | sock_addr_t addrbuf;
|
---|
[2] | 3277 |
|
---|
[391] | 3278 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
|
---|
| 3279 | return NULL;
|
---|
| 3280 | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
|
---|
| 3281 | return NULL;
|
---|
| 3282 | return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
|
---|
[2] | 3283 | }
|
---|
| 3284 |
|
---|
| 3285 | PyDoc_STRVAR(gethostbyname_doc,
|
---|
| 3286 | "gethostbyname(host) -> address\n\
|
---|
| 3287 | \n\
|
---|
| 3288 | Return the IP address (a string of the form '255.255.255.255') for a host.");
|
---|
| 3289 |
|
---|
| 3290 |
|
---|
| 3291 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */
|
---|
| 3292 |
|
---|
| 3293 | static PyObject *
|
---|
| 3294 | gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
|
---|
| 3295 | {
|
---|
[391] | 3296 | char **pch;
|
---|
| 3297 | PyObject *rtn_tuple = (PyObject *)NULL;
|
---|
| 3298 | PyObject *name_list = (PyObject *)NULL;
|
---|
| 3299 | PyObject *addr_list = (PyObject *)NULL;
|
---|
| 3300 | PyObject *tmp;
|
---|
[2] | 3301 |
|
---|
[391] | 3302 | if (h == NULL) {
|
---|
| 3303 | /* Let's get real error message to return */
|
---|
[2] | 3304 | #ifndef RISCOS
|
---|
[391] | 3305 | set_herror(h_errno);
|
---|
[2] | 3306 | #else
|
---|
[391] | 3307 | PyErr_SetString(socket_error, "host not found");
|
---|
[2] | 3308 | #endif
|
---|
[391] | 3309 | return NULL;
|
---|
| 3310 | }
|
---|
[2] | 3311 |
|
---|
[391] | 3312 | if (h->h_addrtype != af) {
|
---|
| 3313 | /* Let's get real error message to return */
|
---|
| 3314 | PyErr_SetString(socket_error,
|
---|
| 3315 | (char *)strerror(EAFNOSUPPORT));
|
---|
[2] | 3316 |
|
---|
[391] | 3317 | return NULL;
|
---|
| 3318 | }
|
---|
[2] | 3319 |
|
---|
[391] | 3320 | switch (af) {
|
---|
[2] | 3321 |
|
---|
[391] | 3322 | case AF_INET:
|
---|
| 3323 | if (alen < sizeof(struct sockaddr_in))
|
---|
| 3324 | return NULL;
|
---|
| 3325 | break;
|
---|
[2] | 3326 |
|
---|
| 3327 | #ifdef ENABLE_IPV6
|
---|
[391] | 3328 | case AF_INET6:
|
---|
| 3329 | if (alen < sizeof(struct sockaddr_in6))
|
---|
| 3330 | return NULL;
|
---|
| 3331 | break;
|
---|
[2] | 3332 | #endif
|
---|
| 3333 |
|
---|
[391] | 3334 | }
|
---|
[2] | 3335 |
|
---|
[391] | 3336 | if ((name_list = PyList_New(0)) == NULL)
|
---|
| 3337 | goto err;
|
---|
[2] | 3338 |
|
---|
[391] | 3339 | if ((addr_list = PyList_New(0)) == NULL)
|
---|
| 3340 | goto err;
|
---|
[2] | 3341 |
|
---|
[391] | 3342 | /* SF #1511317: h_aliases can be NULL */
|
---|
| 3343 | if (h->h_aliases) {
|
---|
| 3344 | for (pch = h->h_aliases; *pch != NULL; pch++) {
|
---|
| 3345 | int status;
|
---|
| 3346 | tmp = PyString_FromString(*pch);
|
---|
| 3347 | if (tmp == NULL)
|
---|
| 3348 | goto err;
|
---|
[2] | 3349 |
|
---|
[391] | 3350 | status = PyList_Append(name_list, tmp);
|
---|
| 3351 | Py_DECREF(tmp);
|
---|
[2] | 3352 |
|
---|
[391] | 3353 | if (status)
|
---|
| 3354 | goto err;
|
---|
| 3355 | }
|
---|
| 3356 | }
|
---|
[2] | 3357 |
|
---|
[391] | 3358 | for (pch = h->h_addr_list; *pch != NULL; pch++) {
|
---|
| 3359 | int status;
|
---|
[2] | 3360 |
|
---|
[391] | 3361 | switch (af) {
|
---|
[2] | 3362 |
|
---|
[391] | 3363 | case AF_INET:
|
---|
| 3364 | {
|
---|
| 3365 | struct sockaddr_in sin;
|
---|
| 3366 | memset(&sin, 0, sizeof(sin));
|
---|
| 3367 | sin.sin_family = af;
|
---|
[2] | 3368 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
[391] | 3369 | sin.sin_len = sizeof(sin);
|
---|
[2] | 3370 | #endif
|
---|
[391] | 3371 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
|
---|
| 3372 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
|
---|
[2] | 3373 |
|
---|
[391] | 3374 | if (pch == h->h_addr_list && alen >= sizeof(sin))
|
---|
| 3375 | memcpy((char *) addr, &sin, sizeof(sin));
|
---|
| 3376 | break;
|
---|
| 3377 | }
|
---|
[2] | 3378 |
|
---|
| 3379 | #ifdef ENABLE_IPV6
|
---|
[391] | 3380 | case AF_INET6:
|
---|
| 3381 | {
|
---|
| 3382 | struct sockaddr_in6 sin6;
|
---|
| 3383 | memset(&sin6, 0, sizeof(sin6));
|
---|
| 3384 | sin6.sin6_family = af;
|
---|
[2] | 3385 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
[391] | 3386 | sin6.sin6_len = sizeof(sin6);
|
---|
[2] | 3387 | #endif
|
---|
[391] | 3388 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
|
---|
| 3389 | tmp = makeipaddr((struct sockaddr *)&sin6,
|
---|
| 3390 | sizeof(sin6));
|
---|
[2] | 3391 |
|
---|
[391] | 3392 | if (pch == h->h_addr_list && alen >= sizeof(sin6))
|
---|
| 3393 | memcpy((char *) addr, &sin6, sizeof(sin6));
|
---|
| 3394 | break;
|
---|
| 3395 | }
|
---|
[2] | 3396 | #endif
|
---|
| 3397 |
|
---|
[391] | 3398 | default: /* can't happen */
|
---|
| 3399 | PyErr_SetString(socket_error,
|
---|
| 3400 | "unsupported address family");
|
---|
| 3401 | return NULL;
|
---|
| 3402 | }
|
---|
[2] | 3403 |
|
---|
[391] | 3404 | if (tmp == NULL)
|
---|
| 3405 | goto err;
|
---|
[2] | 3406 |
|
---|
[391] | 3407 | status = PyList_Append(addr_list, tmp);
|
---|
| 3408 | Py_DECREF(tmp);
|
---|
[2] | 3409 |
|
---|
[391] | 3410 | if (status)
|
---|
| 3411 | goto err;
|
---|
| 3412 | }
|
---|
[2] | 3413 |
|
---|
[391] | 3414 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
|
---|
[2] | 3415 |
|
---|
| 3416 | err:
|
---|
[391] | 3417 | Py_XDECREF(name_list);
|
---|
| 3418 | Py_XDECREF(addr_list);
|
---|
| 3419 | return rtn_tuple;
|
---|
[2] | 3420 | }
|
---|
| 3421 |
|
---|
| 3422 |
|
---|
| 3423 | /* Python interface to gethostbyname_ex(name). */
|
---|
| 3424 |
|
---|
| 3425 | /*ARGSUSED*/
|
---|
| 3426 | static PyObject *
|
---|
| 3427 | socket_gethostbyname_ex(PyObject *self, PyObject *args)
|
---|
| 3428 | {
|
---|
[391] | 3429 | char *name;
|
---|
| 3430 | struct hostent *h;
|
---|
[2] | 3431 | #ifdef ENABLE_IPV6
|
---|
[391] | 3432 | struct sockaddr_storage addr;
|
---|
[2] | 3433 | #else
|
---|
[391] | 3434 | struct sockaddr_in addr;
|
---|
[2] | 3435 | #endif
|
---|
[391] | 3436 | struct sockaddr *sa;
|
---|
| 3437 | PyObject *ret;
|
---|
[2] | 3438 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
[391] | 3439 | struct hostent hp_allocated;
|
---|
[2] | 3440 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
[391] | 3441 | struct hostent_data data;
|
---|
[2] | 3442 | #else
|
---|
[391] | 3443 | char buf[16384];
|
---|
| 3444 | int buf_len = (sizeof buf) - 1;
|
---|
| 3445 | int errnop;
|
---|
[2] | 3446 | #endif
|
---|
| 3447 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
[391] | 3448 | int result;
|
---|
[2] | 3449 | #endif
|
---|
| 3450 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
| 3451 |
|
---|
[391] | 3452 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
|
---|
| 3453 | return NULL;
|
---|
| 3454 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
|
---|
| 3455 | return NULL;
|
---|
| 3456 | Py_BEGIN_ALLOW_THREADS
|
---|
[2] | 3457 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
| 3458 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
[391] | 3459 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
|
---|
| 3460 | &h, &errnop);
|
---|
[2] | 3461 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
|
---|
[391] | 3462 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
|
---|
[2] | 3463 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */
|
---|
[391] | 3464 | memset((void *) &data, '\0', sizeof(data));
|
---|
| 3465 | result = gethostbyname_r(name, &hp_allocated, &data);
|
---|
| 3466 | h = (result != 0) ? NULL : &hp_allocated;
|
---|
[2] | 3467 | #endif
|
---|
| 3468 | #else /* not HAVE_GETHOSTBYNAME_R */
|
---|
| 3469 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
[391] | 3470 | PyThread_acquire_lock(netdb_lock, 1);
|
---|
[2] | 3471 | #endif
|
---|
[391] | 3472 | h = gethostbyname(name);
|
---|
[2] | 3473 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
[391] | 3474 | Py_END_ALLOW_THREADS
|
---|
| 3475 | /* Some C libraries would require addr.__ss_family instead of
|
---|
| 3476 | addr.ss_family.
|
---|
| 3477 | Therefore, we cast the sockaddr_storage into sockaddr to
|
---|
| 3478 | access sa_family. */
|
---|
| 3479 | sa = (struct sockaddr*)&addr;
|
---|
| 3480 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
|
---|
| 3481 | sa->sa_family);
|
---|
[2] | 3482 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
[391] | 3483 | PyThread_release_lock(netdb_lock);
|
---|
[2] | 3484 | #endif
|
---|
[391] | 3485 | return ret;
|
---|
[2] | 3486 | }
|
---|
| 3487 |
|
---|
| 3488 | PyDoc_STRVAR(ghbn_ex_doc,
|
---|
| 3489 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
|
---|
| 3490 | \n\
|
---|
| 3491 | Return the true host name, a list of aliases, and a list of IP addresses,\n\
|
---|
| 3492 | for a host. The host argument is a string giving a host name or IP number.");
|
---|
| 3493 |
|
---|
| 3494 |
|
---|
| 3495 | /* Python interface to gethostbyaddr(IP). */
|
---|
| 3496 |
|
---|
| 3497 | /*ARGSUSED*/
|
---|
| 3498 | static PyObject *
|
---|
| 3499 | socket_gethostbyaddr(PyObject *self, PyObject *args)
|
---|
| 3500 | {
|
---|
| 3501 | #ifdef ENABLE_IPV6
|
---|
[391] | 3502 | struct sockaddr_storage addr;
|
---|
[2] | 3503 | #else
|
---|
[391] | 3504 | struct sockaddr_in addr;
|
---|
[2] | 3505 | #endif
|
---|
[391] | 3506 | struct sockaddr *sa = (struct sockaddr *)&addr;
|
---|
| 3507 | char *ip_num;
|
---|
| 3508 | struct hostent *h;
|
---|
| 3509 | PyObject *ret;
|
---|
[2] | 3510 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
[391] | 3511 | struct hostent hp_allocated;
|
---|
[2] | 3512 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
[391] | 3513 | struct hostent_data data;
|
---|
[2] | 3514 | #else
|
---|
[391] | 3515 | /* glibcs up to 2.10 assume that the buf argument to
|
---|
| 3516 | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
|
---|
| 3517 | does not ensure. The attribute below instructs the compiler
|
---|
| 3518 | to maintain this alignment. */
|
---|
| 3519 | char buf[16384] Py_ALIGNED(8);
|
---|
| 3520 | int buf_len = (sizeof buf) - 1;
|
---|
| 3521 | int errnop;
|
---|
[2] | 3522 | #endif
|
---|
| 3523 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
[391] | 3524 | int result;
|
---|
[2] | 3525 | #endif
|
---|
| 3526 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
[391] | 3527 | char *ap;
|
---|
| 3528 | int al;
|
---|
| 3529 | int af;
|
---|
[2] | 3530 |
|
---|
[391] | 3531 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
|
---|
| 3532 | return NULL;
|
---|
| 3533 | af = AF_UNSPEC;
|
---|
| 3534 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
|
---|
| 3535 | return NULL;
|
---|
| 3536 | af = sa->sa_family;
|
---|
| 3537 | ap = NULL;
|
---|
| 3538 | switch (af) {
|
---|
| 3539 | case AF_INET:
|
---|
| 3540 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
|
---|
| 3541 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
|
---|
| 3542 | break;
|
---|
[2] | 3543 | #ifdef ENABLE_IPV6
|
---|
[391] | 3544 | case AF_INET6:
|
---|
| 3545 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
|
---|
| 3546 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
|
---|
| 3547 | break;
|
---|
[2] | 3548 | #endif
|
---|
[391] | 3549 | default:
|
---|
| 3550 | PyErr_SetString(socket_error, "unsupported address family");
|
---|
| 3551 | return NULL;
|
---|
| 3552 | }
|
---|
| 3553 | Py_BEGIN_ALLOW_THREADS
|
---|
[2] | 3554 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
| 3555 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
[391] | 3556 | result = gethostbyaddr_r(ap, al, af,
|
---|
| 3557 | &hp_allocated, buf, buf_len,
|
---|
| 3558 | &h, &errnop);
|
---|
[2] | 3559 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
|
---|
[391] | 3560 | h = gethostbyaddr_r(ap, al, af,
|
---|
| 3561 | &hp_allocated, buf, buf_len, &errnop);
|
---|
[2] | 3562 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */
|
---|
[391] | 3563 | memset((void *) &data, '\0', sizeof(data));
|
---|
| 3564 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
|
---|
| 3565 | h = (result != 0) ? NULL : &hp_allocated;
|
---|
[2] | 3566 | #endif
|
---|
| 3567 | #else /* not HAVE_GETHOSTBYNAME_R */
|
---|
| 3568 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
[391] | 3569 | PyThread_acquire_lock(netdb_lock, 1);
|
---|
[2] | 3570 | #endif
|
---|
[391] | 3571 | h = gethostbyaddr(ap, al, af);
|
---|
[2] | 3572 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
[391] | 3573 | Py_END_ALLOW_THREADS
|
---|
| 3574 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
|
---|
[2] | 3575 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
[391] | 3576 | PyThread_release_lock(netdb_lock);
|
---|
[2] | 3577 | #endif
|
---|
[391] | 3578 | return ret;
|
---|
[2] | 3579 | }
|
---|
| 3580 |
|
---|
| 3581 | PyDoc_STRVAR(gethostbyaddr_doc,
|
---|
| 3582 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
|
---|
| 3583 | \n\
|
---|
| 3584 | Return the true host name, a list of aliases, and a list of IP addresses,\n\
|
---|
| 3585 | for a host. The host argument is a string giving a host name or IP number.");
|
---|
| 3586 |
|
---|
| 3587 |
|
---|
| 3588 | /* Python interface to getservbyname(name).
|
---|
| 3589 | This only returns the port number, since the other info is already
|
---|
| 3590 | known or not useful (like the list of aliases). */
|
---|
| 3591 |
|
---|
| 3592 | /*ARGSUSED*/
|
---|
| 3593 | static PyObject *
|
---|
| 3594 | socket_getservbyname(PyObject *self, PyObject *args)
|
---|
| 3595 | {
|
---|
[391] | 3596 | char *name, *proto=NULL;
|
---|
| 3597 | struct servent *sp;
|
---|
| 3598 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
|
---|
| 3599 | return NULL;
|
---|
| 3600 | Py_BEGIN_ALLOW_THREADS
|
---|
| 3601 | sp = getservbyname(name, proto);
|
---|
| 3602 | Py_END_ALLOW_THREADS
|
---|
| 3603 | if (sp == NULL) {
|
---|
| 3604 | PyErr_SetString(socket_error, "service/proto not found");
|
---|
| 3605 | return NULL;
|
---|
| 3606 | }
|
---|
| 3607 | return PyInt_FromLong((long) ntohs(sp->s_port));
|
---|
[2] | 3608 | }
|
---|
| 3609 |
|
---|
| 3610 | PyDoc_STRVAR(getservbyname_doc,
|
---|
| 3611 | "getservbyname(servicename[, protocolname]) -> integer\n\
|
---|
| 3612 | \n\
|
---|
| 3613 | Return a port number from a service name and protocol name.\n\
|
---|
| 3614 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\
|
---|
| 3615 | otherwise any protocol will match.");
|
---|
| 3616 |
|
---|
| 3617 |
|
---|
| 3618 | /* Python interface to getservbyport(port).
|
---|
| 3619 | This only returns the service name, since the other info is already
|
---|
| 3620 | known or not useful (like the list of aliases). */
|
---|
| 3621 |
|
---|
| 3622 | /*ARGSUSED*/
|
---|
| 3623 | static PyObject *
|
---|
| 3624 | socket_getservbyport(PyObject *self, PyObject *args)
|
---|
| 3625 | {
|
---|
[391] | 3626 | int port;
|
---|
| 3627 | char *proto=NULL;
|
---|
| 3628 | struct servent *sp;
|
---|
| 3629 | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
|
---|
| 3630 | return NULL;
|
---|
| 3631 | if (port < 0 || port > 0xffff) {
|
---|
| 3632 | PyErr_SetString(
|
---|
| 3633 | PyExc_OverflowError,
|
---|
| 3634 | "getservbyport: port must be 0-65535.");
|
---|
| 3635 | return NULL;
|
---|
| 3636 | }
|
---|
| 3637 | Py_BEGIN_ALLOW_THREADS
|
---|
| 3638 | sp = getservbyport(htons((short)port), proto);
|
---|
| 3639 | Py_END_ALLOW_THREADS
|
---|
| 3640 | if (sp == NULL) {
|
---|
| 3641 | PyErr_SetString(socket_error, "port/proto not found");
|
---|
| 3642 | return NULL;
|
---|
| 3643 | }
|
---|
| 3644 | return PyString_FromString(sp->s_name);
|
---|
[2] | 3645 | }
|
---|
| 3646 |
|
---|
| 3647 | PyDoc_STRVAR(getservbyport_doc,
|
---|
| 3648 | "getservbyport(port[, protocolname]) -> string\n\
|
---|
| 3649 | \n\
|
---|
| 3650 | Return the service name from a port number and protocol name.\n\
|
---|
| 3651 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\
|
---|
| 3652 | otherwise any protocol will match.");
|
---|
| 3653 |
|
---|
| 3654 | /* Python interface to getprotobyname(name).
|
---|
| 3655 | This only returns the protocol number, since the other info is
|
---|
| 3656 | already known or not useful (like the list of aliases). */
|
---|
| 3657 |
|
---|
| 3658 | /*ARGSUSED*/
|
---|
| 3659 | static PyObject *
|
---|
| 3660 | socket_getprotobyname(PyObject *self, PyObject *args)
|
---|
| 3661 | {
|
---|
[391] | 3662 | char *name;
|
---|
| 3663 | struct protoent *sp;
|
---|
[2] | 3664 | #ifdef __BEOS__
|
---|
| 3665 | /* Not available in BeOS yet. - [cjh] */
|
---|
[391] | 3666 | PyErr_SetString(socket_error, "getprotobyname not supported");
|
---|
| 3667 | return NULL;
|
---|
[2] | 3668 | #else
|
---|
[391] | 3669 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
|
---|
| 3670 | return NULL;
|
---|
| 3671 | Py_BEGIN_ALLOW_THREADS
|
---|
| 3672 | sp = getprotobyname(name);
|
---|
| 3673 | Py_END_ALLOW_THREADS
|
---|
| 3674 | if (sp == NULL) {
|
---|
| 3675 | PyErr_SetString(socket_error, "protocol not found");
|
---|
| 3676 | return NULL;
|
---|
| 3677 | }
|
---|
| 3678 | return PyInt_FromLong((long) sp->p_proto);
|
---|
[2] | 3679 | #endif
|
---|
| 3680 | }
|
---|
| 3681 |
|
---|
| 3682 | PyDoc_STRVAR(getprotobyname_doc,
|
---|
| 3683 | "getprotobyname(name) -> integer\n\
|
---|
| 3684 | \n\
|
---|
| 3685 | Return the protocol number for the named protocol. (Rarely used.)");
|
---|
| 3686 |
|
---|
| 3687 |
|
---|
| 3688 | #ifdef HAVE_SOCKETPAIR
|
---|
| 3689 | /* Create a pair of sockets using the socketpair() function.
|
---|
| 3690 | Arguments as for socket() except the default family is AF_UNIX if
|
---|
| 3691 | defined on the platform; otherwise, the default is AF_INET. */
|
---|
| 3692 |
|
---|
| 3693 | /*ARGSUSED*/
|
---|
| 3694 | static PyObject *
|
---|
| 3695 | socket_socketpair(PyObject *self, PyObject *args)
|
---|
| 3696 | {
|
---|
[391] | 3697 | PySocketSockObject *s0 = NULL, *s1 = NULL;
|
---|
| 3698 | SOCKET_T sv[2];
|
---|
| 3699 | int family, type = SOCK_STREAM, proto = 0;
|
---|
| 3700 | PyObject *res = NULL;
|
---|
[2] | 3701 |
|
---|
| 3702 | #if defined(AF_UNIX)
|
---|
[391] | 3703 | family = AF_UNIX;
|
---|
[2] | 3704 | #else
|
---|
[391] | 3705 | family = AF_INET;
|
---|
[2] | 3706 | #endif
|
---|
[391] | 3707 | if (!PyArg_ParseTuple(args, "|iii:socketpair",
|
---|
| 3708 | &family, &type, &proto))
|
---|
| 3709 | return NULL;
|
---|
| 3710 | /* Create a pair of socket fds */
|
---|
| 3711 | if (socketpair(family, type, proto, sv) < 0)
|
---|
| 3712 | return set_error();
|
---|
| 3713 | s0 = new_sockobject(sv[0], family, type, proto);
|
---|
| 3714 | if (s0 == NULL)
|
---|
| 3715 | goto finally;
|
---|
| 3716 | s1 = new_sockobject(sv[1], family, type, proto);
|
---|
| 3717 | if (s1 == NULL)
|
---|
| 3718 | goto finally;
|
---|
| 3719 | res = PyTuple_Pack(2, s0, s1);
|
---|
[2] | 3720 |
|
---|
| 3721 | finally:
|
---|
[391] | 3722 | if (res == NULL) {
|
---|
| 3723 | if (s0 == NULL)
|
---|
| 3724 | SOCKETCLOSE(sv[0]);
|
---|
| 3725 | if (s1 == NULL)
|
---|
| 3726 | SOCKETCLOSE(sv[1]);
|
---|
| 3727 | }
|
---|
| 3728 | Py_XDECREF(s0);
|
---|
| 3729 | Py_XDECREF(s1);
|
---|
| 3730 | return res;
|
---|
[2] | 3731 | }
|
---|
| 3732 |
|
---|
| 3733 | PyDoc_STRVAR(socketpair_doc,
|
---|
| 3734 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
|
---|
| 3735 | \n\
|
---|
| 3736 | Create a pair of socket objects from the sockets returned by the platform\n\
|
---|
| 3737 | socketpair() function.\n\
|
---|
| 3738 | The arguments are the same as for socket() except the default family is\n\
|
---|
| 3739 | AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
|
---|
| 3740 |
|
---|
| 3741 | #endif /* HAVE_SOCKETPAIR */
|
---|
| 3742 |
|
---|
| 3743 |
|
---|
| 3744 | #ifndef NO_DUP
|
---|
| 3745 | /* Create a socket object from a numeric file description.
|
---|
| 3746 | Useful e.g. if stdin is a socket.
|
---|
| 3747 | Additional arguments as for socket(). */
|
---|
| 3748 |
|
---|
| 3749 | /*ARGSUSED*/
|
---|
| 3750 | static PyObject *
|
---|
| 3751 | socket_fromfd(PyObject *self, PyObject *args)
|
---|
| 3752 | {
|
---|
[391] | 3753 | PySocketSockObject *s;
|
---|
| 3754 | SOCKET_T fd;
|
---|
| 3755 | int family, type, proto = 0;
|
---|
| 3756 | if (!PyArg_ParseTuple(args, "iii|i:fromfd",
|
---|
| 3757 | &fd, &family, &type, &proto))
|
---|
| 3758 | return NULL;
|
---|
| 3759 | /* Dup the fd so it and the socket can be closed independently */
|
---|
| 3760 | fd = dup(fd);
|
---|
| 3761 | if (fd < 0)
|
---|
| 3762 | return set_error();
|
---|
| 3763 | s = new_sockobject(fd, family, type, proto);
|
---|
| 3764 | return (PyObject *) s;
|
---|
[2] | 3765 | }
|
---|
| 3766 |
|
---|
| 3767 | PyDoc_STRVAR(fromfd_doc,
|
---|
| 3768 | "fromfd(fd, family, type[, proto]) -> socket object\n\
|
---|
| 3769 | \n\
|
---|
| 3770 | Create a socket object from a duplicate of the given\n\
|
---|
| 3771 | file descriptor.\n\
|
---|
| 3772 | The remaining arguments are the same as for socket().");
|
---|
| 3773 |
|
---|
| 3774 | #endif /* NO_DUP */
|
---|
| 3775 |
|
---|
| 3776 |
|
---|
| 3777 | static PyObject *
|
---|
| 3778 | socket_ntohs(PyObject *self, PyObject *args)
|
---|
| 3779 | {
|
---|
[391] | 3780 | int x1, x2;
|
---|
[2] | 3781 |
|
---|
[391] | 3782 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
|
---|
| 3783 | return NULL;
|
---|
| 3784 | }
|
---|
| 3785 | if (x1 < 0) {
|
---|
| 3786 | PyErr_SetString(PyExc_OverflowError,
|
---|
| 3787 | "can't convert negative number to unsigned long");
|
---|
| 3788 | return NULL;
|
---|
| 3789 | }
|
---|
| 3790 | x2 = (unsigned int)ntohs((unsigned short)x1);
|
---|
| 3791 | return PyInt_FromLong(x2);
|
---|
[2] | 3792 | }
|
---|
| 3793 |
|
---|
| 3794 | PyDoc_STRVAR(ntohs_doc,
|
---|
| 3795 | "ntohs(integer) -> integer\n\
|
---|
| 3796 | \n\
|
---|
| 3797 | Convert a 16-bit integer from network to host byte order.");
|
---|
| 3798 |
|
---|
| 3799 |
|
---|
| 3800 | static PyObject *
|
---|
| 3801 | socket_ntohl(PyObject *self, PyObject *arg)
|
---|
| 3802 | {
|
---|
[391] | 3803 | unsigned long x;
|
---|
[2] | 3804 |
|
---|
[391] | 3805 | if (PyInt_Check(arg)) {
|
---|
| 3806 | x = PyInt_AS_LONG(arg);
|
---|
| 3807 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
| 3808 | return NULL;
|
---|
| 3809 | if ((long)x < 0) {
|
---|
| 3810 | PyErr_SetString(PyExc_OverflowError,
|
---|
| 3811 | "can't convert negative number to unsigned long");
|
---|
| 3812 | return NULL;
|
---|
| 3813 | }
|
---|
| 3814 | }
|
---|
| 3815 | else if (PyLong_Check(arg)) {
|
---|
| 3816 | x = PyLong_AsUnsignedLong(arg);
|
---|
| 3817 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
| 3818 | return NULL;
|
---|
[2] | 3819 | #if SIZEOF_LONG > 4
|
---|
[391] | 3820 | {
|
---|
| 3821 | unsigned long y;
|
---|
| 3822 | /* only want the trailing 32 bits */
|
---|
| 3823 | y = x & 0xFFFFFFFFUL;
|
---|
| 3824 | if (y ^ x)
|
---|
| 3825 | return PyErr_Format(PyExc_OverflowError,
|
---|
| 3826 | "long int larger than 32 bits");
|
---|
| 3827 | x = y;
|
---|
| 3828 | }
|
---|
[2] | 3829 | #endif
|
---|
[391] | 3830 | }
|
---|
| 3831 | else
|
---|
| 3832 | return PyErr_Format(PyExc_TypeError,
|
---|
| 3833 | "expected int/long, %s found",
|
---|
| 3834 | Py_TYPE(arg)->tp_name);
|
---|
| 3835 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
| 3836 | return NULL;
|
---|
| 3837 | return PyLong_FromUnsignedLong(ntohl(x));
|
---|
[2] | 3838 | }
|
---|
| 3839 |
|
---|
| 3840 | PyDoc_STRVAR(ntohl_doc,
|
---|
| 3841 | "ntohl(integer) -> integer\n\
|
---|
| 3842 | \n\
|
---|
| 3843 | Convert a 32-bit integer from network to host byte order.");
|
---|
| 3844 |
|
---|
| 3845 |
|
---|
| 3846 | static PyObject *
|
---|
| 3847 | socket_htons(PyObject *self, PyObject *args)
|
---|
| 3848 | {
|
---|
[391] | 3849 | int x1, x2;
|
---|
[2] | 3850 |
|
---|
[391] | 3851 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
|
---|
| 3852 | return NULL;
|
---|
| 3853 | }
|
---|
| 3854 | if (x1 < 0) {
|
---|
| 3855 | PyErr_SetString(PyExc_OverflowError,
|
---|
| 3856 | "can't convert negative number to unsigned long");
|
---|
| 3857 | return NULL;
|
---|
| 3858 | }
|
---|
| 3859 | x2 = (unsigned int)htons((unsigned short)x1);
|
---|
| 3860 | return PyInt_FromLong(x2);
|
---|
[2] | 3861 | }
|
---|
| 3862 |
|
---|
| 3863 | PyDoc_STRVAR(htons_doc,
|
---|
| 3864 | "htons(integer) -> integer\n\
|
---|
| 3865 | \n\
|
---|
| 3866 | Convert a 16-bit integer from host to network byte order.");
|
---|
| 3867 |
|
---|
| 3868 |
|
---|
| 3869 | static PyObject *
|
---|
| 3870 | socket_htonl(PyObject *self, PyObject *arg)
|
---|
| 3871 | {
|
---|
[391] | 3872 | unsigned long x;
|
---|
[2] | 3873 |
|
---|
[391] | 3874 | if (PyInt_Check(arg)) {
|
---|
| 3875 | x = PyInt_AS_LONG(arg);
|
---|
| 3876 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
| 3877 | return NULL;
|
---|
| 3878 | if ((long)x < 0) {
|
---|
| 3879 | PyErr_SetString(PyExc_OverflowError,
|
---|
| 3880 | "can't convert negative number to unsigned long");
|
---|
| 3881 | return NULL;
|
---|
| 3882 | }
|
---|
| 3883 | }
|
---|
| 3884 | else if (PyLong_Check(arg)) {
|
---|
| 3885 | x = PyLong_AsUnsignedLong(arg);
|
---|
| 3886 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
| 3887 | return NULL;
|
---|
[2] | 3888 | #if SIZEOF_LONG > 4
|
---|
[391] | 3889 | {
|
---|
| 3890 | unsigned long y;
|
---|
| 3891 | /* only want the trailing 32 bits */
|
---|
| 3892 | y = x & 0xFFFFFFFFUL;
|
---|
| 3893 | if (y ^ x)
|
---|
| 3894 | return PyErr_Format(PyExc_OverflowError,
|
---|
| 3895 | "long int larger than 32 bits");
|
---|
| 3896 | x = y;
|
---|
| 3897 | }
|
---|
[2] | 3898 | #endif
|
---|
[391] | 3899 | }
|
---|
| 3900 | else
|
---|
| 3901 | return PyErr_Format(PyExc_TypeError,
|
---|
| 3902 | "expected int/long, %s found",
|
---|
| 3903 | Py_TYPE(arg)->tp_name);
|
---|
| 3904 | return PyLong_FromUnsignedLong(htonl((unsigned long)x));
|
---|
[2] | 3905 | }
|
---|
| 3906 |
|
---|
| 3907 | PyDoc_STRVAR(htonl_doc,
|
---|
| 3908 | "htonl(integer) -> integer\n\
|
---|
| 3909 | \n\
|
---|
| 3910 | Convert a 32-bit integer from host to network byte order.");
|
---|
| 3911 |
|
---|
| 3912 | /* socket.inet_aton() and socket.inet_ntoa() functions. */
|
---|
| 3913 |
|
---|
| 3914 | PyDoc_STRVAR(inet_aton_doc,
|
---|
| 3915 | "inet_aton(string) -> packed 32-bit IP representation\n\
|
---|
| 3916 | \n\
|
---|
| 3917 | Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
|
---|
| 3918 | binary format used in low-level network functions.");
|
---|
| 3919 |
|
---|
| 3920 | static PyObject*
|
---|
| 3921 | socket_inet_aton(PyObject *self, PyObject *args)
|
---|
| 3922 | {
|
---|
| 3923 | #ifndef INADDR_NONE
|
---|
| 3924 | #define INADDR_NONE (-1)
|
---|
| 3925 | #endif
|
---|
| 3926 | #ifdef HAVE_INET_ATON
|
---|
[391] | 3927 | struct in_addr buf;
|
---|
[2] | 3928 | #endif
|
---|
| 3929 |
|
---|
| 3930 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
|
---|
| 3931 | #if (SIZEOF_INT != 4)
|
---|
| 3932 | #error "Not sure if in_addr_t exists and int is not 32-bits."
|
---|
| 3933 | #endif
|
---|
[391] | 3934 | /* Have to use inet_addr() instead */
|
---|
| 3935 | unsigned int packed_addr;
|
---|
[2] | 3936 | #endif
|
---|
[391] | 3937 | char *ip_addr;
|
---|
[2] | 3938 |
|
---|
[391] | 3939 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
|
---|
| 3940 | return NULL;
|
---|
[2] | 3941 |
|
---|
| 3942 |
|
---|
| 3943 | #ifdef HAVE_INET_ATON
|
---|
| 3944 |
|
---|
| 3945 | #ifdef USE_INET_ATON_WEAKLINK
|
---|
| 3946 | if (inet_aton != NULL) {
|
---|
| 3947 | #endif
|
---|
[391] | 3948 | if (inet_aton(ip_addr, &buf))
|
---|
| 3949 | return PyString_FromStringAndSize((char *)(&buf),
|
---|
| 3950 | sizeof(buf));
|
---|
[2] | 3951 |
|
---|
[391] | 3952 | PyErr_SetString(socket_error,
|
---|
| 3953 | "illegal IP address string passed to inet_aton");
|
---|
| 3954 | return NULL;
|
---|
[2] | 3955 |
|
---|
| 3956 | #ifdef USE_INET_ATON_WEAKLINK
|
---|
| 3957 | } else {
|
---|
| 3958 | #endif
|
---|
| 3959 |
|
---|
| 3960 | #endif
|
---|
| 3961 |
|
---|
| 3962 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
|
---|
| 3963 |
|
---|
[391] | 3964 | /* special-case this address as inet_addr might return INADDR_NONE
|
---|
| 3965 | * for this */
|
---|
| 3966 | if (strcmp(ip_addr, "255.255.255.255") == 0) {
|
---|
| 3967 | packed_addr = 0xFFFFFFFF;
|
---|
| 3968 | } else {
|
---|
[2] | 3969 |
|
---|
[391] | 3970 | packed_addr = inet_addr(ip_addr);
|
---|
[2] | 3971 |
|
---|
[391] | 3972 | if (packed_addr == INADDR_NONE) { /* invalid address */
|
---|
| 3973 | PyErr_SetString(socket_error,
|
---|
| 3974 | "illegal IP address string passed to inet_aton");
|
---|
| 3975 | return NULL;
|
---|
| 3976 | }
|
---|
| 3977 | }
|
---|
| 3978 | return PyString_FromStringAndSize((char *) &packed_addr,
|
---|
| 3979 | sizeof(packed_addr));
|
---|
[2] | 3980 |
|
---|
| 3981 | #ifdef USE_INET_ATON_WEAKLINK
|
---|
| 3982 | }
|
---|
| 3983 | #endif
|
---|
| 3984 |
|
---|
| 3985 | #endif
|
---|
| 3986 | }
|
---|
| 3987 |
|
---|
| 3988 | PyDoc_STRVAR(inet_ntoa_doc,
|
---|
| 3989 | "inet_ntoa(packed_ip) -> ip_address_string\n\
|
---|
| 3990 | \n\
|
---|
| 3991 | Convert an IP address from 32-bit packed binary format to string format");
|
---|
| 3992 |
|
---|
| 3993 | static PyObject*
|
---|
| 3994 | socket_inet_ntoa(PyObject *self, PyObject *args)
|
---|
| 3995 | {
|
---|
[391] | 3996 | char *packed_str;
|
---|
| 3997 | int addr_len;
|
---|
| 3998 | struct in_addr packed_addr;
|
---|
[2] | 3999 |
|
---|
[391] | 4000 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
|
---|
| 4001 | return NULL;
|
---|
| 4002 | }
|
---|
[2] | 4003 |
|
---|
[391] | 4004 | if (addr_len != sizeof(packed_addr)) {
|
---|
| 4005 | PyErr_SetString(socket_error,
|
---|
| 4006 | "packed IP wrong length for inet_ntoa");
|
---|
| 4007 | return NULL;
|
---|
| 4008 | }
|
---|
[2] | 4009 |
|
---|
[391] | 4010 | memcpy(&packed_addr, packed_str, addr_len);
|
---|
[2] | 4011 |
|
---|
[391] | 4012 | return PyString_FromString(inet_ntoa(packed_addr));
|
---|
[2] | 4013 | }
|
---|
| 4014 |
|
---|
| 4015 | #ifdef HAVE_INET_PTON
|
---|
| 4016 |
|
---|
| 4017 | PyDoc_STRVAR(inet_pton_doc,
|
---|
| 4018 | "inet_pton(af, ip) -> packed IP address string\n\
|
---|
| 4019 | \n\
|
---|
| 4020 | Convert an IP address from string format to a packed string suitable\n\
|
---|
| 4021 | for use with low-level network functions.");
|
---|
| 4022 |
|
---|
| 4023 | static PyObject *
|
---|
| 4024 | socket_inet_pton(PyObject *self, PyObject *args)
|
---|
| 4025 | {
|
---|
[391] | 4026 | int af;
|
---|
| 4027 | char* ip;
|
---|
| 4028 | int retval;
|
---|
[2] | 4029 | #ifdef ENABLE_IPV6
|
---|
[391] | 4030 | char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
|
---|
[2] | 4031 | #else
|
---|
[391] | 4032 | char packed[sizeof(struct in_addr)];
|
---|
[2] | 4033 | #endif
|
---|
[391] | 4034 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
|
---|
| 4035 | return NULL;
|
---|
| 4036 | }
|
---|
[2] | 4037 |
|
---|
| 4038 | #if !defined(ENABLE_IPV6) && defined(AF_INET6)
|
---|
[391] | 4039 | if(af == AF_INET6) {
|
---|
| 4040 | PyErr_SetString(socket_error,
|
---|
| 4041 | "can't use AF_INET6, IPv6 is disabled");
|
---|
| 4042 | return NULL;
|
---|
| 4043 | }
|
---|
[2] | 4044 | #endif
|
---|
| 4045 |
|
---|
[391] | 4046 | retval = inet_pton(af, ip, packed);
|
---|
| 4047 | if (retval < 0) {
|
---|
| 4048 | PyErr_SetFromErrno(socket_error);
|
---|
| 4049 | return NULL;
|
---|
| 4050 | } else if (retval == 0) {
|
---|
| 4051 | PyErr_SetString(socket_error,
|
---|
| 4052 | "illegal IP address string passed to inet_pton");
|
---|
| 4053 | return NULL;
|
---|
| 4054 | } else if (af == AF_INET) {
|
---|
| 4055 | return PyString_FromStringAndSize(packed,
|
---|
| 4056 | sizeof(struct in_addr));
|
---|
[2] | 4057 | #ifdef ENABLE_IPV6
|
---|
[391] | 4058 | } else if (af == AF_INET6) {
|
---|
| 4059 | return PyString_FromStringAndSize(packed,
|
---|
| 4060 | sizeof(struct in6_addr));
|
---|
[2] | 4061 | #endif
|
---|
[391] | 4062 | } else {
|
---|
| 4063 | PyErr_SetString(socket_error, "unknown address family");
|
---|
| 4064 | return NULL;
|
---|
| 4065 | }
|
---|
[2] | 4066 | }
|
---|
| 4067 |
|
---|
| 4068 | PyDoc_STRVAR(inet_ntop_doc,
|
---|
| 4069 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\
|
---|
| 4070 | \n\
|
---|
| 4071 | Convert a packed IP address of the given family to string format.");
|
---|
| 4072 |
|
---|
| 4073 | static PyObject *
|
---|
| 4074 | socket_inet_ntop(PyObject *self, PyObject *args)
|
---|
| 4075 | {
|
---|
[391] | 4076 | int af;
|
---|
| 4077 | char* packed;
|
---|
| 4078 | int len;
|
---|
| 4079 | const char* retval;
|
---|
[2] | 4080 | #ifdef ENABLE_IPV6
|
---|
[391] | 4081 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
|
---|
[2] | 4082 | #else
|
---|
[391] | 4083 | char ip[INET_ADDRSTRLEN + 1];
|
---|
[2] | 4084 | #endif
|
---|
| 4085 |
|
---|
[391] | 4086 | /* Guarantee NUL-termination for PyString_FromString() below */
|
---|
| 4087 | memset((void *) &ip[0], '\0', sizeof(ip));
|
---|
[2] | 4088 |
|
---|
[391] | 4089 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
|
---|
| 4090 | return NULL;
|
---|
| 4091 | }
|
---|
[2] | 4092 |
|
---|
[391] | 4093 | if (af == AF_INET) {
|
---|
| 4094 | if (len != sizeof(struct in_addr)) {
|
---|
| 4095 | PyErr_SetString(PyExc_ValueError,
|
---|
| 4096 | "invalid length of packed IP address string");
|
---|
| 4097 | return NULL;
|
---|
| 4098 | }
|
---|
[2] | 4099 | #ifdef ENABLE_IPV6
|
---|
[391] | 4100 | } else if (af == AF_INET6) {
|
---|
| 4101 | if (len != sizeof(struct in6_addr)) {
|
---|
| 4102 | PyErr_SetString(PyExc_ValueError,
|
---|
| 4103 | "invalid length of packed IP address string");
|
---|
| 4104 | return NULL;
|
---|
| 4105 | }
|
---|
[2] | 4106 | #endif
|
---|
[391] | 4107 | } else {
|
---|
| 4108 | PyErr_Format(PyExc_ValueError,
|
---|
| 4109 | "unknown address family %d", af);
|
---|
| 4110 | return NULL;
|
---|
| 4111 | }
|
---|
[2] | 4112 |
|
---|
[391] | 4113 | retval = inet_ntop(af, packed, ip, sizeof(ip));
|
---|
| 4114 | if (!retval) {
|
---|
| 4115 | PyErr_SetFromErrno(socket_error);
|
---|
| 4116 | return NULL;
|
---|
| 4117 | } else {
|
---|
| 4118 | return PyString_FromString(retval);
|
---|
| 4119 | }
|
---|
[2] | 4120 |
|
---|
[391] | 4121 | /* NOTREACHED */
|
---|
| 4122 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
|
---|
| 4123 | return NULL;
|
---|
[2] | 4124 | }
|
---|
| 4125 |
|
---|
| 4126 | #endif /* HAVE_INET_PTON */
|
---|
| 4127 |
|
---|
| 4128 | /* Python interface to getaddrinfo(host, port). */
|
---|
| 4129 |
|
---|
| 4130 | /*ARGSUSED*/
|
---|
| 4131 | static PyObject *
|
---|
| 4132 | socket_getaddrinfo(PyObject *self, PyObject *args)
|
---|
| 4133 | {
|
---|
[391] | 4134 | struct addrinfo hints, *res;
|
---|
| 4135 | struct addrinfo *res0 = NULL;
|
---|
| 4136 | PyObject *hobj = NULL;
|
---|
| 4137 | PyObject *pobj = (PyObject *)NULL;
|
---|
| 4138 | char pbuf[30];
|
---|
| 4139 | char *hptr, *pptr;
|
---|
| 4140 | int family, socktype, protocol, flags;
|
---|
| 4141 | int error;
|
---|
| 4142 | PyObject *all = (PyObject *)NULL;
|
---|
| 4143 | PyObject *single = (PyObject *)NULL;
|
---|
| 4144 | PyObject *idna = NULL;
|
---|
[2] | 4145 |
|
---|
[391] | 4146 | family = socktype = protocol = flags = 0;
|
---|
| 4147 | family = AF_UNSPEC;
|
---|
| 4148 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
|
---|
| 4149 | &hobj, &pobj, &family, &socktype,
|
---|
| 4150 | &protocol, &flags)) {
|
---|
| 4151 | return NULL;
|
---|
| 4152 | }
|
---|
| 4153 | if (hobj == Py_None) {
|
---|
| 4154 | hptr = NULL;
|
---|
| 4155 | } else if (PyUnicode_Check(hobj)) {
|
---|
| 4156 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
|
---|
| 4157 | if (!idna)
|
---|
| 4158 | return NULL;
|
---|
| 4159 | hptr = PyString_AsString(idna);
|
---|
| 4160 | } else if (PyString_Check(hobj)) {
|
---|
| 4161 | hptr = PyString_AsString(hobj);
|
---|
| 4162 | } else {
|
---|
| 4163 | PyErr_SetString(PyExc_TypeError,
|
---|
| 4164 | "getaddrinfo() argument 1 must be string or None");
|
---|
| 4165 | return NULL;
|
---|
| 4166 | }
|
---|
| 4167 | if (PyInt_Check(pobj) || PyLong_Check(pobj)) {
|
---|
| 4168 | long value = PyLong_AsLong(pobj);
|
---|
| 4169 | if (value == -1 && PyErr_Occurred())
|
---|
| 4170 | return NULL;
|
---|
| 4171 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
|
---|
| 4172 | pptr = pbuf;
|
---|
| 4173 | } else if (PyString_Check(pobj)) {
|
---|
| 4174 | pptr = PyString_AsString(pobj);
|
---|
| 4175 | } else if (pobj == Py_None) {
|
---|
| 4176 | pptr = (char *)NULL;
|
---|
| 4177 | } else {
|
---|
| 4178 | PyErr_SetString(socket_error,
|
---|
| 4179 | "getaddrinfo() argument 2 must be integer or string");
|
---|
| 4180 | goto err;
|
---|
| 4181 | }
|
---|
| 4182 | #if defined(__APPLE__) && defined(AI_NUMERICSERV)
|
---|
| 4183 | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
|
---|
| 4184 | /* On OSX upto at least OSX 10.8 getaddrinfo crashes
|
---|
| 4185 | * if AI_NUMERICSERV is set and the servname is NULL or "0".
|
---|
| 4186 | * This workaround avoids a segfault in libsystem.
|
---|
| 4187 | */
|
---|
| 4188 | pptr = "00";
|
---|
| 4189 | }
|
---|
| 4190 | #endif
|
---|
| 4191 | memset(&hints, 0, sizeof(hints));
|
---|
| 4192 | hints.ai_family = family;
|
---|
| 4193 | hints.ai_socktype = socktype;
|
---|
| 4194 | hints.ai_protocol = protocol;
|
---|
| 4195 | hints.ai_flags = flags;
|
---|
| 4196 | Py_BEGIN_ALLOW_THREADS
|
---|
| 4197 | ACQUIRE_GETADDRINFO_LOCK
|
---|
| 4198 | error = getaddrinfo(hptr, pptr, &hints, &res0);
|
---|
| 4199 | Py_END_ALLOW_THREADS
|
---|
| 4200 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
|
---|
| 4201 | if (error) {
|
---|
| 4202 | set_gaierror(error);
|
---|
| 4203 | goto err;
|
---|
| 4204 | }
|
---|
[2] | 4205 |
|
---|
[391] | 4206 | if ((all = PyList_New(0)) == NULL)
|
---|
| 4207 | goto err;
|
---|
| 4208 | for (res = res0; res; res = res->ai_next) {
|
---|
| 4209 | PyObject *addr =
|
---|
| 4210 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
|
---|
| 4211 | if (addr == NULL)
|
---|
| 4212 | goto err;
|
---|
| 4213 | single = Py_BuildValue("iiisO", res->ai_family,
|
---|
| 4214 | res->ai_socktype, res->ai_protocol,
|
---|
| 4215 | res->ai_canonname ? res->ai_canonname : "",
|
---|
| 4216 | addr);
|
---|
| 4217 | Py_DECREF(addr);
|
---|
| 4218 | if (single == NULL)
|
---|
| 4219 | goto err;
|
---|
[2] | 4220 |
|
---|
[391] | 4221 | if (PyList_Append(all, single))
|
---|
| 4222 | goto err;
|
---|
| 4223 | Py_XDECREF(single);
|
---|
| 4224 | }
|
---|
| 4225 | Py_XDECREF(idna);
|
---|
| 4226 | if (res0)
|
---|
| 4227 | freeaddrinfo(res0);
|
---|
| 4228 | return all;
|
---|
[2] | 4229 | err:
|
---|
[391] | 4230 | Py_XDECREF(single);
|
---|
| 4231 | Py_XDECREF(all);
|
---|
| 4232 | Py_XDECREF(idna);
|
---|
| 4233 | if (res0)
|
---|
| 4234 | freeaddrinfo(res0);
|
---|
| 4235 | return (PyObject *)NULL;
|
---|
[2] | 4236 | }
|
---|
| 4237 |
|
---|
| 4238 | PyDoc_STRVAR(getaddrinfo_doc,
|
---|
| 4239 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\
|
---|
| 4240 | -> list of (family, socktype, proto, canonname, sockaddr)\n\
|
---|
| 4241 | \n\
|
---|
| 4242 | Resolve host and port into addrinfo struct.");
|
---|
| 4243 |
|
---|
| 4244 | /* Python interface to getnameinfo(sa, flags). */
|
---|
| 4245 |
|
---|
| 4246 | /*ARGSUSED*/
|
---|
| 4247 | static PyObject *
|
---|
| 4248 | socket_getnameinfo(PyObject *self, PyObject *args)
|
---|
| 4249 | {
|
---|
[391] | 4250 | PyObject *sa = (PyObject *)NULL;
|
---|
| 4251 | int flags;
|
---|
| 4252 | char *hostp;
|
---|
| 4253 | int port;
|
---|
| 4254 | unsigned int flowinfo, scope_id;
|
---|
| 4255 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
|
---|
| 4256 | struct addrinfo hints, *res = NULL;
|
---|
| 4257 | int error;
|
---|
| 4258 | PyObject *ret = (PyObject *)NULL;
|
---|
[2] | 4259 |
|
---|
[391] | 4260 | flags = flowinfo = scope_id = 0;
|
---|
| 4261 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
|
---|
| 4262 | return NULL;
|
---|
| 4263 | if (!PyTuple_Check(sa)) {
|
---|
| 4264 | PyErr_SetString(PyExc_TypeError,
|
---|
| 4265 | "getnameinfo() argument 1 must be a tuple");
|
---|
| 4266 | return NULL;
|
---|
| 4267 | }
|
---|
| 4268 | if (!PyArg_ParseTuple(sa, "si|II",
|
---|
| 4269 | &hostp, &port, &flowinfo, &scope_id))
|
---|
| 4270 | return NULL;
|
---|
| 4271 | if (flowinfo > 0xfffff) {
|
---|
| 4272 | PyErr_SetString(PyExc_OverflowError,
|
---|
| 4273 | "getsockaddrarg: flowinfo must be 0-1048575.");
|
---|
| 4274 | return NULL;
|
---|
| 4275 | }
|
---|
| 4276 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
|
---|
| 4277 | memset(&hints, 0, sizeof(hints));
|
---|
| 4278 | hints.ai_family = AF_UNSPEC;
|
---|
| 4279 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
|
---|
| 4280 | Py_BEGIN_ALLOW_THREADS
|
---|
| 4281 | ACQUIRE_GETADDRINFO_LOCK
|
---|
| 4282 | error = getaddrinfo(hostp, pbuf, &hints, &res);
|
---|
| 4283 | Py_END_ALLOW_THREADS
|
---|
| 4284 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
|
---|
| 4285 | if (error) {
|
---|
| 4286 | set_gaierror(error);
|
---|
| 4287 | goto fail;
|
---|
| 4288 | }
|
---|
| 4289 | if (res->ai_next) {
|
---|
| 4290 | PyErr_SetString(socket_error,
|
---|
| 4291 | "sockaddr resolved to multiple addresses");
|
---|
| 4292 | goto fail;
|
---|
| 4293 | }
|
---|
| 4294 | switch (res->ai_family) {
|
---|
| 4295 | case AF_INET:
|
---|
| 4296 | {
|
---|
| 4297 | if (PyTuple_GET_SIZE(sa) != 2) {
|
---|
| 4298 | PyErr_SetString(socket_error,
|
---|
| 4299 | "IPv4 sockaddr must be 2 tuple");
|
---|
| 4300 | goto fail;
|
---|
| 4301 | }
|
---|
| 4302 | break;
|
---|
| 4303 | }
|
---|
[2] | 4304 | #ifdef ENABLE_IPV6
|
---|
[391] | 4305 | case AF_INET6:
|
---|
| 4306 | {
|
---|
| 4307 | struct sockaddr_in6 *sin6;
|
---|
| 4308 | sin6 = (struct sockaddr_in6 *)res->ai_addr;
|
---|
| 4309 | sin6->sin6_flowinfo = htonl(flowinfo);
|
---|
| 4310 | sin6->sin6_scope_id = scope_id;
|
---|
| 4311 | break;
|
---|
| 4312 | }
|
---|
[2] | 4313 | #endif
|
---|
[391] | 4314 | }
|
---|
| 4315 | error = getnameinfo(res->ai_addr, res->ai_addrlen,
|
---|
| 4316 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
|
---|
| 4317 | if (error) {
|
---|
| 4318 | set_gaierror(error);
|
---|
| 4319 | goto fail;
|
---|
| 4320 | }
|
---|
| 4321 | ret = Py_BuildValue("ss", hbuf, pbuf);
|
---|
[2] | 4322 |
|
---|
| 4323 | fail:
|
---|
[391] | 4324 | if (res)
|
---|
| 4325 | freeaddrinfo(res);
|
---|
| 4326 | return ret;
|
---|
[2] | 4327 | }
|
---|
| 4328 |
|
---|
| 4329 | PyDoc_STRVAR(getnameinfo_doc,
|
---|
| 4330 | "getnameinfo(sockaddr, flags) --> (host, port)\n\
|
---|
| 4331 | \n\
|
---|
| 4332 | Get host and port for a sockaddr.");
|
---|
| 4333 |
|
---|
| 4334 |
|
---|
| 4335 | /* Python API to getting and setting the default timeout value. */
|
---|
| 4336 |
|
---|
| 4337 | static PyObject *
|
---|
| 4338 | socket_getdefaulttimeout(PyObject *self)
|
---|
| 4339 | {
|
---|
[391] | 4340 | if (defaulttimeout < 0.0) {
|
---|
| 4341 | Py_INCREF(Py_None);
|
---|
| 4342 | return Py_None;
|
---|
| 4343 | }
|
---|
| 4344 | else
|
---|
| 4345 | return PyFloat_FromDouble(defaulttimeout);
|
---|
[2] | 4346 | }
|
---|
| 4347 |
|
---|
| 4348 | PyDoc_STRVAR(getdefaulttimeout_doc,
|
---|
| 4349 | "getdefaulttimeout() -> timeout\n\
|
---|
| 4350 | \n\
|
---|
[391] | 4351 | Returns the default timeout in seconds (float) for new socket objects.\n\
|
---|
[2] | 4352 | A value of None indicates that new socket objects have no timeout.\n\
|
---|
| 4353 | When the socket module is first imported, the default is None.");
|
---|
| 4354 |
|
---|
| 4355 | static PyObject *
|
---|
| 4356 | socket_setdefaulttimeout(PyObject *self, PyObject *arg)
|
---|
| 4357 | {
|
---|
[391] | 4358 | double timeout;
|
---|
[2] | 4359 |
|
---|
[391] | 4360 | if (arg == Py_None)
|
---|
| 4361 | timeout = -1.0;
|
---|
| 4362 | else {
|
---|
| 4363 | timeout = PyFloat_AsDouble(arg);
|
---|
| 4364 | if (timeout < 0.0) {
|
---|
| 4365 | if (!PyErr_Occurred())
|
---|
| 4366 | PyErr_SetString(PyExc_ValueError,
|
---|
| 4367 | "Timeout value out of range");
|
---|
| 4368 | return NULL;
|
---|
| 4369 | }
|
---|
| 4370 | }
|
---|
[2] | 4371 |
|
---|
[391] | 4372 | defaulttimeout = timeout;
|
---|
[2] | 4373 |
|
---|
[391] | 4374 | Py_INCREF(Py_None);
|
---|
| 4375 | return Py_None;
|
---|
[2] | 4376 | }
|
---|
| 4377 |
|
---|
| 4378 | PyDoc_STRVAR(setdefaulttimeout_doc,
|
---|
| 4379 | "setdefaulttimeout(timeout)\n\
|
---|
| 4380 | \n\
|
---|
[391] | 4381 | Set the default timeout in seconds (float) for new socket objects.\n\
|
---|
[2] | 4382 | A value of None indicates that new socket objects have no timeout.\n\
|
---|
| 4383 | When the socket module is first imported, the default is None.");
|
---|
| 4384 |
|
---|
| 4385 |
|
---|
| 4386 | /* List of functions exported by this module. */
|
---|
| 4387 |
|
---|
| 4388 | static PyMethodDef socket_methods[] = {
|
---|
[391] | 4389 | {"gethostbyname", socket_gethostbyname,
|
---|
| 4390 | METH_VARARGS, gethostbyname_doc},
|
---|
| 4391 | {"gethostbyname_ex", socket_gethostbyname_ex,
|
---|
| 4392 | METH_VARARGS, ghbn_ex_doc},
|
---|
| 4393 | {"gethostbyaddr", socket_gethostbyaddr,
|
---|
| 4394 | METH_VARARGS, gethostbyaddr_doc},
|
---|
| 4395 | {"gethostname", socket_gethostname,
|
---|
| 4396 | METH_NOARGS, gethostname_doc},
|
---|
| 4397 | {"getservbyname", socket_getservbyname,
|
---|
| 4398 | METH_VARARGS, getservbyname_doc},
|
---|
| 4399 | {"getservbyport", socket_getservbyport,
|
---|
| 4400 | METH_VARARGS, getservbyport_doc},
|
---|
| 4401 | {"getprotobyname", socket_getprotobyname,
|
---|
| 4402 | METH_VARARGS, getprotobyname_doc},
|
---|
[2] | 4403 | #ifndef NO_DUP
|
---|
[391] | 4404 | {"fromfd", socket_fromfd,
|
---|
| 4405 | METH_VARARGS, fromfd_doc},
|
---|
[2] | 4406 | #endif
|
---|
| 4407 | #ifdef HAVE_SOCKETPAIR
|
---|
[391] | 4408 | {"socketpair", socket_socketpair,
|
---|
| 4409 | METH_VARARGS, socketpair_doc},
|
---|
[2] | 4410 | #endif
|
---|
[391] | 4411 | {"ntohs", socket_ntohs,
|
---|
| 4412 | METH_VARARGS, ntohs_doc},
|
---|
| 4413 | {"ntohl", socket_ntohl,
|
---|
| 4414 | METH_O, ntohl_doc},
|
---|
| 4415 | {"htons", socket_htons,
|
---|
| 4416 | METH_VARARGS, htons_doc},
|
---|
| 4417 | {"htonl", socket_htonl,
|
---|
| 4418 | METH_O, htonl_doc},
|
---|
| 4419 | {"inet_aton", socket_inet_aton,
|
---|
| 4420 | METH_VARARGS, inet_aton_doc},
|
---|
| 4421 | {"inet_ntoa", socket_inet_ntoa,
|
---|
| 4422 | METH_VARARGS, inet_ntoa_doc},
|
---|
[2] | 4423 | #ifdef HAVE_INET_PTON
|
---|
[391] | 4424 | {"inet_pton", socket_inet_pton,
|
---|
| 4425 | METH_VARARGS, inet_pton_doc},
|
---|
| 4426 | {"inet_ntop", socket_inet_ntop,
|
---|
| 4427 | METH_VARARGS, inet_ntop_doc},
|
---|
[2] | 4428 | #endif
|
---|
[391] | 4429 | {"getaddrinfo", socket_getaddrinfo,
|
---|
| 4430 | METH_VARARGS, getaddrinfo_doc},
|
---|
| 4431 | {"getnameinfo", socket_getnameinfo,
|
---|
| 4432 | METH_VARARGS, getnameinfo_doc},
|
---|
| 4433 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
|
---|
| 4434 | METH_NOARGS, getdefaulttimeout_doc},
|
---|
| 4435 | {"setdefaulttimeout", socket_setdefaulttimeout,
|
---|
| 4436 | METH_O, setdefaulttimeout_doc},
|
---|
| 4437 | {NULL, NULL} /* Sentinel */
|
---|
[2] | 4438 | };
|
---|
| 4439 |
|
---|
| 4440 |
|
---|
| 4441 | #ifdef RISCOS
|
---|
| 4442 | #define OS_INIT_DEFINED
|
---|
| 4443 |
|
---|
| 4444 | static int
|
---|
| 4445 | os_init(void)
|
---|
| 4446 | {
|
---|
[391] | 4447 | _kernel_swi_regs r;
|
---|
[2] | 4448 |
|
---|
[391] | 4449 | r.r[0] = 0;
|
---|
| 4450 | _kernel_swi(0x43380, &r, &r);
|
---|
| 4451 | taskwindow = r.r[0];
|
---|
[2] | 4452 |
|
---|
[391] | 4453 | return 1;
|
---|
[2] | 4454 | }
|
---|
| 4455 |
|
---|
| 4456 | #endif /* RISCOS */
|
---|
| 4457 |
|
---|
| 4458 |
|
---|
| 4459 | #ifdef MS_WINDOWS
|
---|
| 4460 | #define OS_INIT_DEFINED
|
---|
| 4461 |
|
---|
| 4462 | /* Additional initialization and cleanup for Windows */
|
---|
| 4463 |
|
---|
| 4464 | static void
|
---|
| 4465 | os_cleanup(void)
|
---|
| 4466 | {
|
---|
[391] | 4467 | WSACleanup();
|
---|
[2] | 4468 | }
|
---|
| 4469 |
|
---|
| 4470 | static int
|
---|
| 4471 | os_init(void)
|
---|
| 4472 | {
|
---|
[391] | 4473 | WSADATA WSAData;
|
---|
| 4474 | int ret;
|
---|
| 4475 | char buf[100];
|
---|
| 4476 | ret = WSAStartup(0x0101, &WSAData);
|
---|
| 4477 | switch (ret) {
|
---|
| 4478 | case 0: /* No error */
|
---|
| 4479 | Py_AtExit(os_cleanup);
|
---|
| 4480 | return 1; /* Success */
|
---|
| 4481 | case WSASYSNOTREADY:
|
---|
| 4482 | PyErr_SetString(PyExc_ImportError,
|
---|
| 4483 | "WSAStartup failed: network not ready");
|
---|
| 4484 | break;
|
---|
| 4485 | case WSAVERNOTSUPPORTED:
|
---|
| 4486 | case WSAEINVAL:
|
---|
| 4487 | PyErr_SetString(
|
---|
| 4488 | PyExc_ImportError,
|
---|
| 4489 | "WSAStartup failed: requested version not supported");
|
---|
| 4490 | break;
|
---|
| 4491 | default:
|
---|
| 4492 | PyOS_snprintf(buf, sizeof(buf),
|
---|
| 4493 | "WSAStartup failed: error code %d", ret);
|
---|
| 4494 | PyErr_SetString(PyExc_ImportError, buf);
|
---|
| 4495 | break;
|
---|
| 4496 | }
|
---|
| 4497 | return 0; /* Failure */
|
---|
[2] | 4498 | }
|
---|
| 4499 |
|
---|
| 4500 | #endif /* MS_WINDOWS */
|
---|
| 4501 |
|
---|
| 4502 |
|
---|
| 4503 | #ifdef PYOS_OS2
|
---|
| 4504 | #define OS_INIT_DEFINED
|
---|
| 4505 |
|
---|
| 4506 | /* Additional initialization for OS/2 */
|
---|
| 4507 |
|
---|
| 4508 | static int
|
---|
| 4509 | os_init(void)
|
---|
| 4510 | {
|
---|
| 4511 | #ifndef PYCC_GCC
|
---|
[391] | 4512 | char reason[64];
|
---|
| 4513 | int rc = sock_init();
|
---|
[2] | 4514 |
|
---|
[391] | 4515 | if (rc == 0) {
|
---|
| 4516 | return 1; /* Success */
|
---|
| 4517 | }
|
---|
[2] | 4518 |
|
---|
[391] | 4519 | PyOS_snprintf(reason, sizeof(reason),
|
---|
| 4520 | "OS/2 TCP/IP Error# %d", sock_errno());
|
---|
| 4521 | PyErr_SetString(PyExc_ImportError, reason);
|
---|
[2] | 4522 |
|
---|
[391] | 4523 | return 0; /* Failure */
|
---|
[2] | 4524 | #else
|
---|
[391] | 4525 | /* No need to initialize sockets with GCC/EMX */
|
---|
| 4526 | return 1; /* Success */
|
---|
[2] | 4527 | #endif
|
---|
| 4528 | }
|
---|
| 4529 |
|
---|
| 4530 | #endif /* PYOS_OS2 */
|
---|
| 4531 |
|
---|
| 4532 |
|
---|
| 4533 | #ifndef OS_INIT_DEFINED
|
---|
| 4534 | static int
|
---|
| 4535 | os_init(void)
|
---|
| 4536 | {
|
---|
[391] | 4537 | return 1; /* Success */
|
---|
[2] | 4538 | }
|
---|
| 4539 | #endif
|
---|
| 4540 |
|
---|
| 4541 |
|
---|
| 4542 | /* C API table - always add new things to the end for binary
|
---|
| 4543 | compatibility. */
|
---|
| 4544 | static
|
---|
| 4545 | PySocketModule_APIObject PySocketModuleAPI =
|
---|
| 4546 | {
|
---|
[391] | 4547 | &sock_type,
|
---|
| 4548 | NULL
|
---|
[2] | 4549 | };
|
---|
| 4550 |
|
---|
| 4551 |
|
---|
| 4552 | /* Initialize the _socket module.
|
---|
| 4553 |
|
---|
| 4554 | This module is actually called "_socket", and there's a wrapper
|
---|
| 4555 | "socket.py" which implements some additional functionality. On some
|
---|
| 4556 | platforms (e.g. Windows and OS/2), socket.py also implements a
|
---|
| 4557 | wrapper for the socket type that provides missing functionality such
|
---|
| 4558 | as makefile(), dup() and fromfd(). The import of "_socket" may fail
|
---|
| 4559 | with an ImportError exception if os-specific initialization fails.
|
---|
| 4560 | On Windows, this does WINSOCK initialization. When WINSOCK is
|
---|
[391] | 4561 | initialized successfully, a call to WSACleanup() is scheduled to be
|
---|
[2] | 4562 | made at exit time.
|
---|
| 4563 | */
|
---|
| 4564 |
|
---|
| 4565 | PyDoc_STRVAR(socket_doc,
|
---|
| 4566 | "Implementation module for socket operations.\n\
|
---|
| 4567 | \n\
|
---|
| 4568 | See the socket module for documentation.");
|
---|
| 4569 |
|
---|
| 4570 | PyMODINIT_FUNC
|
---|
| 4571 | init_socket(void)
|
---|
| 4572 | {
|
---|
[391] | 4573 | PyObject *m, *has_ipv6;
|
---|
[2] | 4574 |
|
---|
[391] | 4575 | if (!os_init())
|
---|
| 4576 | return;
|
---|
[2] | 4577 |
|
---|
[391] | 4578 | Py_TYPE(&sock_type) = &PyType_Type;
|
---|
| 4579 | m = Py_InitModule3(PySocket_MODULE_NAME,
|
---|
| 4580 | socket_methods,
|
---|
| 4581 | socket_doc);
|
---|
| 4582 | if (m == NULL)
|
---|
| 4583 | return;
|
---|
[2] | 4584 |
|
---|
[391] | 4585 | socket_error = PyErr_NewException("socket.error",
|
---|
| 4586 | PyExc_IOError, NULL);
|
---|
| 4587 | if (socket_error == NULL)
|
---|
| 4588 | return;
|
---|
| 4589 | PySocketModuleAPI.error = socket_error;
|
---|
| 4590 | Py_INCREF(socket_error);
|
---|
| 4591 | PyModule_AddObject(m, "error", socket_error);
|
---|
| 4592 | socket_herror = PyErr_NewException("socket.herror",
|
---|
| 4593 | socket_error, NULL);
|
---|
| 4594 | if (socket_herror == NULL)
|
---|
| 4595 | return;
|
---|
| 4596 | Py_INCREF(socket_herror);
|
---|
| 4597 | PyModule_AddObject(m, "herror", socket_herror);
|
---|
| 4598 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
|
---|
| 4599 | NULL);
|
---|
| 4600 | if (socket_gaierror == NULL)
|
---|
| 4601 | return;
|
---|
| 4602 | Py_INCREF(socket_gaierror);
|
---|
| 4603 | PyModule_AddObject(m, "gaierror", socket_gaierror);
|
---|
| 4604 | socket_timeout = PyErr_NewException("socket.timeout",
|
---|
| 4605 | socket_error, NULL);
|
---|
| 4606 | if (socket_timeout == NULL)
|
---|
| 4607 | return;
|
---|
| 4608 | Py_INCREF(socket_timeout);
|
---|
| 4609 | PyModule_AddObject(m, "timeout", socket_timeout);
|
---|
| 4610 | Py_INCREF((PyObject *)&sock_type);
|
---|
| 4611 | if (PyModule_AddObject(m, "SocketType",
|
---|
| 4612 | (PyObject *)&sock_type) != 0)
|
---|
| 4613 | return;
|
---|
| 4614 | Py_INCREF((PyObject *)&sock_type);
|
---|
| 4615 | if (PyModule_AddObject(m, "socket",
|
---|
| 4616 | (PyObject *)&sock_type) != 0)
|
---|
| 4617 | return;
|
---|
[2] | 4618 |
|
---|
| 4619 | #ifdef ENABLE_IPV6
|
---|
[391] | 4620 | has_ipv6 = Py_True;
|
---|
[2] | 4621 | #else
|
---|
[391] | 4622 | has_ipv6 = Py_False;
|
---|
[2] | 4623 | #endif
|
---|
[391] | 4624 | Py_INCREF(has_ipv6);
|
---|
| 4625 | PyModule_AddObject(m, "has_ipv6", has_ipv6);
|
---|
[2] | 4626 |
|
---|
[391] | 4627 | /* Export C API */
|
---|
| 4628 | if (PyModule_AddObject(m, PySocket_CAPI_NAME,
|
---|
| 4629 | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
|
---|
| 4630 | ) != 0)
|
---|
| 4631 | return;
|
---|
[2] | 4632 |
|
---|
[391] | 4633 | /* Address families (we only support AF_INET and AF_UNIX) */
|
---|
[2] | 4634 | #ifdef AF_UNSPEC
|
---|
[391] | 4635 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
|
---|
[2] | 4636 | #endif
|
---|
[391] | 4637 | PyModule_AddIntConstant(m, "AF_INET", AF_INET);
|
---|
[2] | 4638 | #ifdef AF_INET6
|
---|
[391] | 4639 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
|
---|
[2] | 4640 | #endif /* AF_INET6 */
|
---|
| 4641 | #if defined(AF_UNIX)
|
---|
[391] | 4642 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
|
---|
[2] | 4643 | #endif /* AF_UNIX */
|
---|
| 4644 | #ifdef AF_AX25
|
---|
[391] | 4645 | /* Amateur Radio AX.25 */
|
---|
| 4646 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
|
---|
[2] | 4647 | #endif
|
---|
| 4648 | #ifdef AF_IPX
|
---|
[391] | 4649 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
|
---|
[2] | 4650 | #endif
|
---|
| 4651 | #ifdef AF_APPLETALK
|
---|
[391] | 4652 | /* Appletalk DDP */
|
---|
| 4653 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
|
---|
[2] | 4654 | #endif
|
---|
| 4655 | #ifdef AF_NETROM
|
---|
[391] | 4656 | /* Amateur radio NetROM */
|
---|
| 4657 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
|
---|
[2] | 4658 | #endif
|
---|
| 4659 | #ifdef AF_BRIDGE
|
---|
[391] | 4660 | /* Multiprotocol bridge */
|
---|
| 4661 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
|
---|
[2] | 4662 | #endif
|
---|
| 4663 | #ifdef AF_ATMPVC
|
---|
[391] | 4664 | /* ATM PVCs */
|
---|
| 4665 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
|
---|
[2] | 4666 | #endif
|
---|
| 4667 | #ifdef AF_AAL5
|
---|
[391] | 4668 | /* Reserved for Werner's ATM */
|
---|
| 4669 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
|
---|
[2] | 4670 | #endif
|
---|
| 4671 | #ifdef AF_X25
|
---|
[391] | 4672 | /* Reserved for X.25 project */
|
---|
| 4673 | PyModule_AddIntConstant(m, "AF_X25", AF_X25);
|
---|
[2] | 4674 | #endif
|
---|
| 4675 | #ifdef AF_INET6
|
---|
[391] | 4676 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
|
---|
[2] | 4677 | #endif
|
---|
| 4678 | #ifdef AF_ROSE
|
---|
[391] | 4679 | /* Amateur Radio X.25 PLP */
|
---|
| 4680 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
|
---|
[2] | 4681 | #endif
|
---|
| 4682 | #ifdef AF_DECnet
|
---|
[391] | 4683 | /* Reserved for DECnet project */
|
---|
| 4684 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
|
---|
[2] | 4685 | #endif
|
---|
| 4686 | #ifdef AF_NETBEUI
|
---|
[391] | 4687 | /* Reserved for 802.2LLC project */
|
---|
| 4688 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
|
---|
[2] | 4689 | #endif
|
---|
| 4690 | #ifdef AF_SECURITY
|
---|
[391] | 4691 | /* Security callback pseudo AF */
|
---|
| 4692 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
|
---|
[2] | 4693 | #endif
|
---|
| 4694 | #ifdef AF_KEY
|
---|
[391] | 4695 | /* PF_KEY key management API */
|
---|
| 4696 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
|
---|
[2] | 4697 | #endif
|
---|
| 4698 | #ifdef AF_NETLINK
|
---|
[391] | 4699 | /* */
|
---|
| 4700 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
|
---|
| 4701 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
|
---|
[2] | 4702 | #ifdef NETLINK_SKIP
|
---|
[391] | 4703 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
|
---|
[2] | 4704 | #endif
|
---|
| 4705 | #ifdef NETLINK_W1
|
---|
[391] | 4706 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
|
---|
[2] | 4707 | #endif
|
---|
[391] | 4708 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
|
---|
| 4709 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
|
---|
[2] | 4710 | #ifdef NETLINK_TCPDIAG
|
---|
[391] | 4711 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
|
---|
[2] | 4712 | #endif
|
---|
| 4713 | #ifdef NETLINK_NFLOG
|
---|
[391] | 4714 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
|
---|
[2] | 4715 | #endif
|
---|
| 4716 | #ifdef NETLINK_XFRM
|
---|
[391] | 4717 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
|
---|
[2] | 4718 | #endif
|
---|
| 4719 | #ifdef NETLINK_ARPD
|
---|
[391] | 4720 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
|
---|
[2] | 4721 | #endif
|
---|
| 4722 | #ifdef NETLINK_ROUTE6
|
---|
[391] | 4723 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
|
---|
[2] | 4724 | #endif
|
---|
[391] | 4725 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
|
---|
[2] | 4726 | #ifdef NETLINK_DNRTMSG
|
---|
[391] | 4727 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
|
---|
| 4728 | #endif
|
---|
[2] | 4729 | #ifdef NETLINK_TAPBASE
|
---|
[391] | 4730 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
|
---|
[2] | 4731 | #endif
|
---|
| 4732 | #endif /* AF_NETLINK */
|
---|
| 4733 | #ifdef AF_ROUTE
|
---|
[391] | 4734 | /* Alias to emulate 4.4BSD */
|
---|
| 4735 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
|
---|
[2] | 4736 | #endif
|
---|
| 4737 | #ifdef AF_ASH
|
---|
[391] | 4738 | /* Ash */
|
---|
| 4739 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
|
---|
[2] | 4740 | #endif
|
---|
| 4741 | #ifdef AF_ECONET
|
---|
[391] | 4742 | /* Acorn Econet */
|
---|
| 4743 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
|
---|
[2] | 4744 | #endif
|
---|
| 4745 | #ifdef AF_ATMSVC
|
---|
[391] | 4746 | /* ATM SVCs */
|
---|
| 4747 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
|
---|
[2] | 4748 | #endif
|
---|
| 4749 | #ifdef AF_SNA
|
---|
[391] | 4750 | /* Linux SNA Project (nutters!) */
|
---|
| 4751 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
|
---|
[2] | 4752 | #endif
|
---|
| 4753 | #ifdef AF_IRDA
|
---|
[391] | 4754 | /* IRDA sockets */
|
---|
| 4755 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
|
---|
[2] | 4756 | #endif
|
---|
| 4757 | #ifdef AF_PPPOX
|
---|
[391] | 4758 | /* PPPoX sockets */
|
---|
| 4759 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
|
---|
[2] | 4760 | #endif
|
---|
| 4761 | #ifdef AF_WANPIPE
|
---|
[391] | 4762 | /* Wanpipe API Sockets */
|
---|
| 4763 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
|
---|
[2] | 4764 | #endif
|
---|
| 4765 | #ifdef AF_LLC
|
---|
[391] | 4766 | /* Linux LLC */
|
---|
| 4767 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
|
---|
[2] | 4768 | #endif
|
---|
| 4769 |
|
---|
| 4770 | #ifdef USE_BLUETOOTH
|
---|
[391] | 4771 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
|
---|
| 4772 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
|
---|
| 4773 | PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
|
---|
| 4774 | PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
|
---|
| 4775 | #if !defined(__NetBSD__) && !defined(__DragonFly__)
|
---|
| 4776 | PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
|
---|
| 4777 | #endif
|
---|
[2] | 4778 | #if !defined(__FreeBSD__)
|
---|
[391] | 4779 | #if !defined(__NetBSD__) && !defined(__DragonFly__)
|
---|
| 4780 | PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
|
---|
[2] | 4781 | #endif
|
---|
[391] | 4782 | PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
|
---|
| 4783 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
|
---|
[2] | 4784 | #endif
|
---|
[391] | 4785 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
|
---|
| 4786 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
|
---|
| 4787 | PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
|
---|
| 4788 | #endif
|
---|
[2] | 4789 |
|
---|
[391] | 4790 | #ifdef AF_PACKET
|
---|
| 4791 | PyModule_AddIntMacro(m, AF_PACKET);
|
---|
[2] | 4792 | #endif
|
---|
[391] | 4793 | #ifdef PF_PACKET
|
---|
| 4794 | PyModule_AddIntMacro(m, PF_PACKET);
|
---|
| 4795 | #endif
|
---|
| 4796 | #ifdef PACKET_HOST
|
---|
| 4797 | PyModule_AddIntMacro(m, PACKET_HOST);
|
---|
| 4798 | #endif
|
---|
| 4799 | #ifdef PACKET_BROADCAST
|
---|
| 4800 | PyModule_AddIntMacro(m, PACKET_BROADCAST);
|
---|
| 4801 | #endif
|
---|
| 4802 | #ifdef PACKET_MULTICAST
|
---|
| 4803 | PyModule_AddIntMacro(m, PACKET_MULTICAST);
|
---|
| 4804 | #endif
|
---|
| 4805 | #ifdef PACKET_OTHERHOST
|
---|
| 4806 | PyModule_AddIntMacro(m, PACKET_OTHERHOST);
|
---|
| 4807 | #endif
|
---|
| 4808 | #ifdef PACKET_OUTGOING
|
---|
| 4809 | PyModule_AddIntMacro(m, PACKET_OUTGOING);
|
---|
| 4810 | #endif
|
---|
| 4811 | #ifdef PACKET_LOOPBACK
|
---|
| 4812 | PyModule_AddIntMacro(m, PACKET_LOOPBACK);
|
---|
| 4813 | #endif
|
---|
| 4814 | #ifdef PACKET_FASTROUTE
|
---|
| 4815 | PyModule_AddIntMacro(m, PACKET_FASTROUTE);
|
---|
| 4816 | #endif
|
---|
[2] | 4817 |
|
---|
| 4818 | #ifdef HAVE_LINUX_TIPC_H
|
---|
[391] | 4819 | PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
|
---|
[2] | 4820 |
|
---|
[391] | 4821 | /* for addresses */
|
---|
| 4822 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
|
---|
| 4823 | PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
|
---|
| 4824 | PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
|
---|
[2] | 4825 |
|
---|
[391] | 4826 | PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
|
---|
| 4827 | PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
|
---|
| 4828 | PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
|
---|
[2] | 4829 |
|
---|
[391] | 4830 | /* for setsockopt() */
|
---|
| 4831 | PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
|
---|
| 4832 | PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
|
---|
| 4833 | PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
|
---|
| 4834 | PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
|
---|
| 4835 | TIPC_DEST_DROPPABLE);
|
---|
| 4836 | PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
|
---|
[2] | 4837 |
|
---|
[391] | 4838 | PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
|
---|
| 4839 | TIPC_LOW_IMPORTANCE);
|
---|
| 4840 | PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
|
---|
| 4841 | TIPC_MEDIUM_IMPORTANCE);
|
---|
| 4842 | PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
|
---|
| 4843 | TIPC_HIGH_IMPORTANCE);
|
---|
| 4844 | PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
|
---|
| 4845 | TIPC_CRITICAL_IMPORTANCE);
|
---|
[2] | 4846 |
|
---|
[391] | 4847 | /* for subscriptions */
|
---|
| 4848 | PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
|
---|
| 4849 | PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
|
---|
[2] | 4850 | #ifdef TIPC_SUB_CANCEL
|
---|
[391] | 4851 | /* doesn't seem to be available everywhere */
|
---|
| 4852 | PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
|
---|
[2] | 4853 | #endif
|
---|
[391] | 4854 | PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
|
---|
| 4855 | PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
|
---|
| 4856 | PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
|
---|
| 4857 | PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
|
---|
| 4858 | PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
|
---|
| 4859 | PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
|
---|
[2] | 4860 | #endif
|
---|
| 4861 |
|
---|
[391] | 4862 | /* Socket types */
|
---|
| 4863 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
|
---|
| 4864 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
|
---|
[2] | 4865 | #ifndef __BEOS__
|
---|
| 4866 | /* We have incomplete socket support. */
|
---|
[391] | 4867 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
|
---|
| 4868 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
|
---|
[2] | 4869 | #if defined(SOCK_RDM)
|
---|
[391] | 4870 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
|
---|
[2] | 4871 | #endif
|
---|
| 4872 | #endif
|
---|
| 4873 |
|
---|
[391] | 4874 | #ifdef SO_DEBUG
|
---|
| 4875 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
|
---|
[2] | 4876 | #endif
|
---|
[391] | 4877 | #ifdef SO_ACCEPTCONN
|
---|
| 4878 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
|
---|
[2] | 4879 | #endif
|
---|
[391] | 4880 | #ifdef SO_REUSEADDR
|
---|
| 4881 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
|
---|
[2] | 4882 | #endif
|
---|
| 4883 | #ifdef SO_EXCLUSIVEADDRUSE
|
---|
[391] | 4884 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
|
---|
[2] | 4885 | #endif
|
---|
| 4886 |
|
---|
[391] | 4887 | #ifdef SO_KEEPALIVE
|
---|
| 4888 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
|
---|
[2] | 4889 | #endif
|
---|
[391] | 4890 | #ifdef SO_DONTROUTE
|
---|
| 4891 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
|
---|
[2] | 4892 | #endif
|
---|
[391] | 4893 | #ifdef SO_BROADCAST
|
---|
| 4894 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
|
---|
[2] | 4895 | #endif
|
---|
[391] | 4896 | #ifdef SO_USELOOPBACK
|
---|
| 4897 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
|
---|
[2] | 4898 | #endif
|
---|
[391] | 4899 | #ifdef SO_LINGER
|
---|
| 4900 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
|
---|
[2] | 4901 | #endif
|
---|
[391] | 4902 | #ifdef SO_OOBINLINE
|
---|
| 4903 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
|
---|
[2] | 4904 | #endif
|
---|
[391] | 4905 | #ifdef SO_REUSEPORT
|
---|
| 4906 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
|
---|
[2] | 4907 | #endif
|
---|
[391] | 4908 | #ifdef SO_SNDBUF
|
---|
| 4909 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
|
---|
[2] | 4910 | #endif
|
---|
[391] | 4911 | #ifdef SO_RCVBUF
|
---|
| 4912 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
|
---|
[2] | 4913 | #endif
|
---|
[391] | 4914 | #ifdef SO_SNDLOWAT
|
---|
| 4915 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
|
---|
[2] | 4916 | #endif
|
---|
[391] | 4917 | #ifdef SO_RCVLOWAT
|
---|
| 4918 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
|
---|
[2] | 4919 | #endif
|
---|
[391] | 4920 | #ifdef SO_SNDTIMEO
|
---|
| 4921 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
|
---|
[2] | 4922 | #endif
|
---|
[391] | 4923 | #ifdef SO_RCVTIMEO
|
---|
| 4924 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
|
---|
[2] | 4925 | #endif
|
---|
[391] | 4926 | #ifdef SO_ERROR
|
---|
| 4927 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
|
---|
[2] | 4928 | #endif
|
---|
[391] | 4929 | #ifdef SO_TYPE
|
---|
| 4930 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
|
---|
[2] | 4931 | #endif
|
---|
[391] | 4932 | #ifdef SO_SETFIB
|
---|
| 4933 | PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB);
|
---|
| 4934 | #endif
|
---|
[2] | 4935 |
|
---|
[391] | 4936 | /* Maximum number of connections for "listen" */
|
---|
| 4937 | #ifdef SOMAXCONN
|
---|
| 4938 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
|
---|
[2] | 4939 | #else
|
---|
[391] | 4940 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
|
---|
[2] | 4941 | #endif
|
---|
| 4942 |
|
---|
[391] | 4943 | /* Flags for send, recv */
|
---|
| 4944 | #ifdef MSG_OOB
|
---|
| 4945 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
|
---|
[2] | 4946 | #endif
|
---|
[391] | 4947 | #ifdef MSG_PEEK
|
---|
| 4948 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
|
---|
[2] | 4949 | #endif
|
---|
[391] | 4950 | #ifdef MSG_DONTROUTE
|
---|
| 4951 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
|
---|
[2] | 4952 | #endif
|
---|
[391] | 4953 | #ifdef MSG_DONTWAIT
|
---|
| 4954 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
|
---|
[2] | 4955 | #endif
|
---|
[391] | 4956 | #ifdef MSG_EOR
|
---|
| 4957 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
|
---|
[2] | 4958 | #endif
|
---|
[391] | 4959 | #ifdef MSG_TRUNC
|
---|
| 4960 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
|
---|
[2] | 4961 | #endif
|
---|
[391] | 4962 | #ifdef MSG_CTRUNC
|
---|
| 4963 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
|
---|
[2] | 4964 | #endif
|
---|
[391] | 4965 | #ifdef MSG_WAITALL
|
---|
| 4966 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
|
---|
[2] | 4967 | #endif
|
---|
[391] | 4968 | #ifdef MSG_BTAG
|
---|
| 4969 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
|
---|
[2] | 4970 | #endif
|
---|
[391] | 4971 | #ifdef MSG_ETAG
|
---|
| 4972 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
|
---|
[2] | 4973 | #endif
|
---|
| 4974 |
|
---|
[391] | 4975 | /* Protocol level and numbers, usable for [gs]etsockopt */
|
---|
| 4976 | #ifdef SOL_SOCKET
|
---|
| 4977 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
|
---|
[2] | 4978 | #endif
|
---|
[391] | 4979 | #ifdef SOL_IP
|
---|
| 4980 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
|
---|
[2] | 4981 | #else
|
---|
[391] | 4982 | PyModule_AddIntConstant(m, "SOL_IP", 0);
|
---|
[2] | 4983 | #endif
|
---|
[391] | 4984 | #ifdef SOL_IPX
|
---|
| 4985 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
|
---|
[2] | 4986 | #endif
|
---|
[391] | 4987 | #ifdef SOL_AX25
|
---|
| 4988 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
|
---|
[2] | 4989 | #endif
|
---|
[391] | 4990 | #ifdef SOL_ATALK
|
---|
| 4991 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
|
---|
[2] | 4992 | #endif
|
---|
[391] | 4993 | #ifdef SOL_NETROM
|
---|
| 4994 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
|
---|
[2] | 4995 | #endif
|
---|
[391] | 4996 | #ifdef SOL_ROSE
|
---|
| 4997 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
|
---|
[2] | 4998 | #endif
|
---|
[391] | 4999 | #ifdef SOL_TCP
|
---|
| 5000 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
|
---|
[2] | 5001 | #else
|
---|
[391] | 5002 | PyModule_AddIntConstant(m, "SOL_TCP", 6);
|
---|
[2] | 5003 | #endif
|
---|
[391] | 5004 | #ifdef SOL_UDP
|
---|
| 5005 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
|
---|
[2] | 5006 | #else
|
---|
[391] | 5007 | PyModule_AddIntConstant(m, "SOL_UDP", 17);
|
---|
[2] | 5008 | #endif
|
---|
[391] | 5009 | #ifdef IPPROTO_IP
|
---|
| 5010 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
|
---|
[2] | 5011 | #else
|
---|
[391] | 5012 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
|
---|
[2] | 5013 | #endif
|
---|
[391] | 5014 | #ifdef IPPROTO_HOPOPTS
|
---|
| 5015 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
|
---|
[2] | 5016 | #endif
|
---|
[391] | 5017 | #ifdef IPPROTO_ICMP
|
---|
| 5018 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
|
---|
[2] | 5019 | #else
|
---|
[391] | 5020 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
|
---|
[2] | 5021 | #endif
|
---|
[391] | 5022 | #ifdef IPPROTO_IGMP
|
---|
| 5023 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
|
---|
[2] | 5024 | #endif
|
---|
[391] | 5025 | #ifdef IPPROTO_GGP
|
---|
| 5026 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
|
---|
[2] | 5027 | #endif
|
---|
[391] | 5028 | #ifdef IPPROTO_IPV4
|
---|
| 5029 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
|
---|
[2] | 5030 | #endif
|
---|
[391] | 5031 | #ifdef IPPROTO_IPV6
|
---|
| 5032 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
|
---|
[2] | 5033 | #endif
|
---|
[391] | 5034 | #ifdef IPPROTO_IPIP
|
---|
| 5035 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
|
---|
[2] | 5036 | #endif
|
---|
[391] | 5037 | #ifdef IPPROTO_TCP
|
---|
| 5038 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
|
---|
[2] | 5039 | #else
|
---|
[391] | 5040 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
|
---|
[2] | 5041 | #endif
|
---|
[391] | 5042 | #ifdef IPPROTO_EGP
|
---|
| 5043 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
|
---|
[2] | 5044 | #endif
|
---|
[391] | 5045 | #ifdef IPPROTO_PUP
|
---|
| 5046 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
|
---|
[2] | 5047 | #endif
|
---|
[391] | 5048 | #ifdef IPPROTO_UDP
|
---|
| 5049 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
|
---|
[2] | 5050 | #else
|
---|
[391] | 5051 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
|
---|
[2] | 5052 | #endif
|
---|
[391] | 5053 | #ifdef IPPROTO_IDP
|
---|
| 5054 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
|
---|
[2] | 5055 | #endif
|
---|
[391] | 5056 | #ifdef IPPROTO_HELLO
|
---|
| 5057 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
|
---|
[2] | 5058 | #endif
|
---|
[391] | 5059 | #ifdef IPPROTO_ND
|
---|
| 5060 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
|
---|
[2] | 5061 | #endif
|
---|
[391] | 5062 | #ifdef IPPROTO_TP
|
---|
| 5063 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
|
---|
[2] | 5064 | #endif
|
---|
[391] | 5065 | #ifdef IPPROTO_IPV6
|
---|
| 5066 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
|
---|
[2] | 5067 | #endif
|
---|
[391] | 5068 | #ifdef IPPROTO_ROUTING
|
---|
| 5069 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
|
---|
[2] | 5070 | #endif
|
---|
[391] | 5071 | #ifdef IPPROTO_FRAGMENT
|
---|
| 5072 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
|
---|
[2] | 5073 | #endif
|
---|
[391] | 5074 | #ifdef IPPROTO_RSVP
|
---|
| 5075 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
|
---|
[2] | 5076 | #endif
|
---|
[391] | 5077 | #ifdef IPPROTO_GRE
|
---|
| 5078 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
|
---|
[2] | 5079 | #endif
|
---|
[391] | 5080 | #ifdef IPPROTO_ESP
|
---|
| 5081 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
|
---|
[2] | 5082 | #endif
|
---|
[391] | 5083 | #ifdef IPPROTO_AH
|
---|
| 5084 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
|
---|
[2] | 5085 | #endif
|
---|
[391] | 5086 | #ifdef IPPROTO_MOBILE
|
---|
| 5087 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
|
---|
[2] | 5088 | #endif
|
---|
[391] | 5089 | #ifdef IPPROTO_ICMPV6
|
---|
| 5090 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
|
---|
[2] | 5091 | #endif
|
---|
[391] | 5092 | #ifdef IPPROTO_NONE
|
---|
| 5093 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
|
---|
[2] | 5094 | #endif
|
---|
[391] | 5095 | #ifdef IPPROTO_DSTOPTS
|
---|
| 5096 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
|
---|
[2] | 5097 | #endif
|
---|
[391] | 5098 | #ifdef IPPROTO_XTP
|
---|
| 5099 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
|
---|
[2] | 5100 | #endif
|
---|
[391] | 5101 | #ifdef IPPROTO_EON
|
---|
| 5102 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
|
---|
[2] | 5103 | #endif
|
---|
[391] | 5104 | #ifdef IPPROTO_PIM
|
---|
| 5105 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
|
---|
[2] | 5106 | #endif
|
---|
[391] | 5107 | #ifdef IPPROTO_IPCOMP
|
---|
| 5108 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
|
---|
[2] | 5109 | #endif
|
---|
[391] | 5110 | #ifdef IPPROTO_VRRP
|
---|
| 5111 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
|
---|
[2] | 5112 | #endif
|
---|
[391] | 5113 | #ifdef IPPROTO_BIP
|
---|
| 5114 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
|
---|
[2] | 5115 | #endif
|
---|
| 5116 | /**/
|
---|
[391] | 5117 | #ifdef IPPROTO_RAW
|
---|
| 5118 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
|
---|
[2] | 5119 | #else
|
---|
[391] | 5120 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
|
---|
[2] | 5121 | #endif
|
---|
[391] | 5122 | #ifdef IPPROTO_MAX
|
---|
| 5123 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
|
---|
[2] | 5124 | #endif
|
---|
| 5125 |
|
---|
[391] | 5126 | /* Some port configuration */
|
---|
| 5127 | #ifdef IPPORT_RESERVED
|
---|
| 5128 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
|
---|
[2] | 5129 | #else
|
---|
[391] | 5130 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
|
---|
[2] | 5131 | #endif
|
---|
[391] | 5132 | #ifdef IPPORT_USERRESERVED
|
---|
| 5133 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
|
---|
[2] | 5134 | #else
|
---|
[391] | 5135 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
|
---|
[2] | 5136 | #endif
|
---|
| 5137 |
|
---|
[391] | 5138 | /* Some reserved IP v.4 addresses */
|
---|
| 5139 | #ifdef INADDR_ANY
|
---|
| 5140 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
|
---|
[2] | 5141 | #else
|
---|
[391] | 5142 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
|
---|
[2] | 5143 | #endif
|
---|
[391] | 5144 | #ifdef INADDR_BROADCAST
|
---|
| 5145 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
|
---|
[2] | 5146 | #else
|
---|
[391] | 5147 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
|
---|
[2] | 5148 | #endif
|
---|
[391] | 5149 | #ifdef INADDR_LOOPBACK
|
---|
| 5150 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
|
---|
[2] | 5151 | #else
|
---|
[391] | 5152 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
|
---|
[2] | 5153 | #endif
|
---|
[391] | 5154 | #ifdef INADDR_UNSPEC_GROUP
|
---|
| 5155 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
|
---|
[2] | 5156 | #else
|
---|
[391] | 5157 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
|
---|
[2] | 5158 | #endif
|
---|
[391] | 5159 | #ifdef INADDR_ALLHOSTS_GROUP
|
---|
| 5160 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
|
---|
| 5161 | INADDR_ALLHOSTS_GROUP);
|
---|
[2] | 5162 | #else
|
---|
[391] | 5163 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
|
---|
[2] | 5164 | #endif
|
---|
[391] | 5165 | #ifdef INADDR_MAX_LOCAL_GROUP
|
---|
| 5166 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
|
---|
| 5167 | INADDR_MAX_LOCAL_GROUP);
|
---|
[2] | 5168 | #else
|
---|
[391] | 5169 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
|
---|
[2] | 5170 | #endif
|
---|
[391] | 5171 | #ifdef INADDR_NONE
|
---|
| 5172 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
|
---|
[2] | 5173 | #else
|
---|
[391] | 5174 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
|
---|
[2] | 5175 | #endif
|
---|
| 5176 |
|
---|
[391] | 5177 | /* IPv4 [gs]etsockopt options */
|
---|
| 5178 | #ifdef IP_OPTIONS
|
---|
| 5179 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
|
---|
[2] | 5180 | #endif
|
---|
[391] | 5181 | #ifdef IP_HDRINCL
|
---|
| 5182 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
|
---|
[2] | 5183 | #endif
|
---|
[391] | 5184 | #ifdef IP_TOS
|
---|
| 5185 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
|
---|
[2] | 5186 | #endif
|
---|
[391] | 5187 | #ifdef IP_TTL
|
---|
| 5188 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
|
---|
[2] | 5189 | #endif
|
---|
[391] | 5190 | #ifdef IP_RECVOPTS
|
---|
| 5191 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
|
---|
[2] | 5192 | #endif
|
---|
[391] | 5193 | #ifdef IP_RECVRETOPTS
|
---|
| 5194 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
|
---|
[2] | 5195 | #endif
|
---|
[391] | 5196 | #ifdef IP_RECVDSTADDR
|
---|
| 5197 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
|
---|
[2] | 5198 | #endif
|
---|
[391] | 5199 | #ifdef IP_RETOPTS
|
---|
| 5200 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
|
---|
[2] | 5201 | #endif
|
---|
[391] | 5202 | #ifdef IP_MULTICAST_IF
|
---|
| 5203 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
|
---|
[2] | 5204 | #endif
|
---|
[391] | 5205 | #ifdef IP_MULTICAST_TTL
|
---|
| 5206 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
|
---|
[2] | 5207 | #endif
|
---|
[391] | 5208 | #ifdef IP_MULTICAST_LOOP
|
---|
| 5209 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
|
---|
[2] | 5210 | #endif
|
---|
[391] | 5211 | #ifdef IP_ADD_MEMBERSHIP
|
---|
| 5212 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
|
---|
[2] | 5213 | #endif
|
---|
[391] | 5214 | #ifdef IP_DROP_MEMBERSHIP
|
---|
| 5215 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
|
---|
[2] | 5216 | #endif
|
---|
[391] | 5217 | #ifdef IP_DEFAULT_MULTICAST_TTL
|
---|
| 5218 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
|
---|
| 5219 | IP_DEFAULT_MULTICAST_TTL);
|
---|
[2] | 5220 | #endif
|
---|
[391] | 5221 | #ifdef IP_DEFAULT_MULTICAST_LOOP
|
---|
| 5222 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
|
---|
| 5223 | IP_DEFAULT_MULTICAST_LOOP);
|
---|
[2] | 5224 | #endif
|
---|
[391] | 5225 | #ifdef IP_MAX_MEMBERSHIPS
|
---|
| 5226 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
|
---|
[2] | 5227 | #endif
|
---|
| 5228 |
|
---|
[391] | 5229 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */
|
---|
| 5230 | #ifdef IPV6_JOIN_GROUP
|
---|
| 5231 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
|
---|
[2] | 5232 | #endif
|
---|
[391] | 5233 | #ifdef IPV6_LEAVE_GROUP
|
---|
| 5234 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
|
---|
[2] | 5235 | #endif
|
---|
[391] | 5236 | #ifdef IPV6_MULTICAST_HOPS
|
---|
| 5237 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
|
---|
[2] | 5238 | #endif
|
---|
[391] | 5239 | #ifdef IPV6_MULTICAST_IF
|
---|
| 5240 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
|
---|
[2] | 5241 | #endif
|
---|
[391] | 5242 | #ifdef IPV6_MULTICAST_LOOP
|
---|
| 5243 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
|
---|
[2] | 5244 | #endif
|
---|
[391] | 5245 | #ifdef IPV6_UNICAST_HOPS
|
---|
| 5246 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
|
---|
[2] | 5247 | #endif
|
---|
[391] | 5248 | /* Additional IPV6 socket options, defined in RFC 3493 */
|
---|
[2] | 5249 | #ifdef IPV6_V6ONLY
|
---|
[391] | 5250 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
|
---|
[2] | 5251 | #endif
|
---|
[391] | 5252 | /* Advanced IPV6 socket options, from RFC 3542 */
|
---|
[2] | 5253 | #ifdef IPV6_CHECKSUM
|
---|
[391] | 5254 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
|
---|
[2] | 5255 | #endif
|
---|
| 5256 | #ifdef IPV6_DONTFRAG
|
---|
[391] | 5257 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
|
---|
[2] | 5258 | #endif
|
---|
| 5259 | #ifdef IPV6_DSTOPTS
|
---|
[391] | 5260 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
|
---|
[2] | 5261 | #endif
|
---|
| 5262 | #ifdef IPV6_HOPLIMIT
|
---|
[391] | 5263 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
|
---|
[2] | 5264 | #endif
|
---|
| 5265 | #ifdef IPV6_HOPOPTS
|
---|
[391] | 5266 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
|
---|
[2] | 5267 | #endif
|
---|
| 5268 | #ifdef IPV6_NEXTHOP
|
---|
[391] | 5269 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
|
---|
[2] | 5270 | #endif
|
---|
| 5271 | #ifdef IPV6_PATHMTU
|
---|
[391] | 5272 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
|
---|
[2] | 5273 | #endif
|
---|
| 5274 | #ifdef IPV6_PKTINFO
|
---|
[391] | 5275 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
|
---|
[2] | 5276 | #endif
|
---|
| 5277 | #ifdef IPV6_RECVDSTOPTS
|
---|
[391] | 5278 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
|
---|
[2] | 5279 | #endif
|
---|
| 5280 | #ifdef IPV6_RECVHOPLIMIT
|
---|
[391] | 5281 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
|
---|
[2] | 5282 | #endif
|
---|
| 5283 | #ifdef IPV6_RECVHOPOPTS
|
---|
[391] | 5284 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
|
---|
[2] | 5285 | #endif
|
---|
| 5286 | #ifdef IPV6_RECVPKTINFO
|
---|
[391] | 5287 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
|
---|
[2] | 5288 | #endif
|
---|
| 5289 | #ifdef IPV6_RECVRTHDR
|
---|
[391] | 5290 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
|
---|
[2] | 5291 | #endif
|
---|
| 5292 | #ifdef IPV6_RECVTCLASS
|
---|
[391] | 5293 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
|
---|
[2] | 5294 | #endif
|
---|
| 5295 | #ifdef IPV6_RTHDR
|
---|
[391] | 5296 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
|
---|
[2] | 5297 | #endif
|
---|
| 5298 | #ifdef IPV6_RTHDRDSTOPTS
|
---|
[391] | 5299 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
|
---|
[2] | 5300 | #endif
|
---|
| 5301 | #ifdef IPV6_RTHDR_TYPE_0
|
---|
[391] | 5302 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
|
---|
[2] | 5303 | #endif
|
---|
| 5304 | #ifdef IPV6_RECVPATHMTU
|
---|
[391] | 5305 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
|
---|
[2] | 5306 | #endif
|
---|
| 5307 | #ifdef IPV6_TCLASS
|
---|
[391] | 5308 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
|
---|
[2] | 5309 | #endif
|
---|
| 5310 | #ifdef IPV6_USE_MIN_MTU
|
---|
[391] | 5311 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
|
---|
[2] | 5312 | #endif
|
---|
| 5313 |
|
---|
[391] | 5314 | /* TCP options */
|
---|
| 5315 | #ifdef TCP_NODELAY
|
---|
| 5316 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
|
---|
[2] | 5317 | #endif
|
---|
[391] | 5318 | #ifdef TCP_MAXSEG
|
---|
| 5319 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
|
---|
[2] | 5320 | #endif
|
---|
[391] | 5321 | #ifdef TCP_CORK
|
---|
| 5322 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
|
---|
[2] | 5323 | #endif
|
---|
[391] | 5324 | #ifdef TCP_KEEPIDLE
|
---|
| 5325 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
|
---|
[2] | 5326 | #endif
|
---|
[391] | 5327 | #ifdef TCP_KEEPINTVL
|
---|
| 5328 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
|
---|
[2] | 5329 | #endif
|
---|
[391] | 5330 | #ifdef TCP_KEEPCNT
|
---|
| 5331 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
|
---|
[2] | 5332 | #endif
|
---|
[391] | 5333 | #ifdef TCP_SYNCNT
|
---|
| 5334 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
|
---|
[2] | 5335 | #endif
|
---|
[391] | 5336 | #ifdef TCP_LINGER2
|
---|
| 5337 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
|
---|
[2] | 5338 | #endif
|
---|
[391] | 5339 | #ifdef TCP_DEFER_ACCEPT
|
---|
| 5340 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
|
---|
[2] | 5341 | #endif
|
---|
[391] | 5342 | #ifdef TCP_WINDOW_CLAMP
|
---|
| 5343 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
|
---|
[2] | 5344 | #endif
|
---|
[391] | 5345 | #ifdef TCP_INFO
|
---|
| 5346 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
|
---|
[2] | 5347 | #endif
|
---|
[391] | 5348 | #ifdef TCP_QUICKACK
|
---|
| 5349 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
|
---|
[2] | 5350 | #endif
|
---|
| 5351 |
|
---|
| 5352 |
|
---|
[391] | 5353 | /* IPX options */
|
---|
| 5354 | #ifdef IPX_TYPE
|
---|
| 5355 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
|
---|
[2] | 5356 | #endif
|
---|
| 5357 |
|
---|
[391] | 5358 | /* get{addr,name}info parameters */
|
---|
[2] | 5359 | #ifdef EAI_ADDRFAMILY
|
---|
[391] | 5360 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
|
---|
[2] | 5361 | #endif
|
---|
| 5362 | #ifdef EAI_AGAIN
|
---|
[391] | 5363 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
|
---|
[2] | 5364 | #endif
|
---|
| 5365 | #ifdef EAI_BADFLAGS
|
---|
[391] | 5366 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
|
---|
[2] | 5367 | #endif
|
---|
| 5368 | #ifdef EAI_FAIL
|
---|
[391] | 5369 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
|
---|
[2] | 5370 | #endif
|
---|
| 5371 | #ifdef EAI_FAMILY
|
---|
[391] | 5372 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
|
---|
[2] | 5373 | #endif
|
---|
| 5374 | #ifdef EAI_MEMORY
|
---|
[391] | 5375 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
|
---|
[2] | 5376 | #endif
|
---|
| 5377 | #ifdef EAI_NODATA
|
---|
[391] | 5378 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
|
---|
[2] | 5379 | #endif
|
---|
| 5380 | #ifdef EAI_NONAME
|
---|
[391] | 5381 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
|
---|
[2] | 5382 | #endif
|
---|
| 5383 | #ifdef EAI_OVERFLOW
|
---|
[391] | 5384 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
|
---|
[2] | 5385 | #endif
|
---|
| 5386 | #ifdef EAI_SERVICE
|
---|
[391] | 5387 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
|
---|
[2] | 5388 | #endif
|
---|
| 5389 | #ifdef EAI_SOCKTYPE
|
---|
[391] | 5390 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
|
---|
[2] | 5391 | #endif
|
---|
| 5392 | #ifdef EAI_SYSTEM
|
---|
[391] | 5393 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
|
---|
[2] | 5394 | #endif
|
---|
| 5395 | #ifdef EAI_BADHINTS
|
---|
[391] | 5396 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
|
---|
[2] | 5397 | #endif
|
---|
| 5398 | #ifdef EAI_PROTOCOL
|
---|
[391] | 5399 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
|
---|
[2] | 5400 | #endif
|
---|
| 5401 | #ifdef EAI_MAX
|
---|
[391] | 5402 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
|
---|
[2] | 5403 | #endif
|
---|
| 5404 | #ifdef AI_PASSIVE
|
---|
[391] | 5405 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
|
---|
[2] | 5406 | #endif
|
---|
| 5407 | #ifdef AI_CANONNAME
|
---|
[391] | 5408 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
|
---|
[2] | 5409 | #endif
|
---|
| 5410 | #ifdef AI_NUMERICHOST
|
---|
[391] | 5411 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
|
---|
[2] | 5412 | #endif
|
---|
| 5413 | #ifdef AI_NUMERICSERV
|
---|
[391] | 5414 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
|
---|
[2] | 5415 | #endif
|
---|
| 5416 | #ifdef AI_MASK
|
---|
[391] | 5417 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
|
---|
[2] | 5418 | #endif
|
---|
| 5419 | #ifdef AI_ALL
|
---|
[391] | 5420 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
|
---|
[2] | 5421 | #endif
|
---|
| 5422 | #ifdef AI_V4MAPPED_CFG
|
---|
[391] | 5423 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
|
---|
[2] | 5424 | #endif
|
---|
| 5425 | #ifdef AI_ADDRCONFIG
|
---|
[391] | 5426 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
|
---|
[2] | 5427 | #endif
|
---|
| 5428 | #ifdef AI_V4MAPPED
|
---|
[391] | 5429 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
|
---|
[2] | 5430 | #endif
|
---|
| 5431 | #ifdef AI_DEFAULT
|
---|
[391] | 5432 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
|
---|
[2] | 5433 | #endif
|
---|
| 5434 | #ifdef NI_MAXHOST
|
---|
[391] | 5435 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
|
---|
[2] | 5436 | #endif
|
---|
| 5437 | #ifdef NI_MAXSERV
|
---|
[391] | 5438 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
|
---|
[2] | 5439 | #endif
|
---|
| 5440 | #ifdef NI_NOFQDN
|
---|
[391] | 5441 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
|
---|
[2] | 5442 | #endif
|
---|
| 5443 | #ifdef NI_NUMERICHOST
|
---|
[391] | 5444 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
|
---|
[2] | 5445 | #endif
|
---|
| 5446 | #ifdef NI_NAMEREQD
|
---|
[391] | 5447 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
|
---|
[2] | 5448 | #endif
|
---|
| 5449 | #ifdef NI_NUMERICSERV
|
---|
[391] | 5450 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
|
---|
[2] | 5451 | #endif
|
---|
| 5452 | #ifdef NI_DGRAM
|
---|
[391] | 5453 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
|
---|
[2] | 5454 | #endif
|
---|
| 5455 |
|
---|
[391] | 5456 | /* shutdown() parameters */
|
---|
[2] | 5457 | #ifdef SHUT_RD
|
---|
[391] | 5458 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
|
---|
[2] | 5459 | #elif defined(SD_RECEIVE)
|
---|
[391] | 5460 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
|
---|
[2] | 5461 | #else
|
---|
[391] | 5462 | PyModule_AddIntConstant(m, "SHUT_RD", 0);
|
---|
[2] | 5463 | #endif
|
---|
| 5464 | #ifdef SHUT_WR
|
---|
[391] | 5465 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
|
---|
[2] | 5466 | #elif defined(SD_SEND)
|
---|
[391] | 5467 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
|
---|
[2] | 5468 | #else
|
---|
[391] | 5469 | PyModule_AddIntConstant(m, "SHUT_WR", 1);
|
---|
[2] | 5470 | #endif
|
---|
| 5471 | #ifdef SHUT_RDWR
|
---|
[391] | 5472 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
|
---|
[2] | 5473 | #elif defined(SD_BOTH)
|
---|
[391] | 5474 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
|
---|
[2] | 5475 | #else
|
---|
[391] | 5476 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
|
---|
[2] | 5477 | #endif
|
---|
| 5478 |
|
---|
| 5479 | #ifdef SIO_RCVALL
|
---|
[391] | 5480 | {
|
---|
| 5481 | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
|
---|
| 5482 | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
|
---|
| 5483 | int i;
|
---|
| 5484 | for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) {
|
---|
| 5485 | PyObject *tmp;
|
---|
| 5486 | tmp = PyLong_FromUnsignedLong(codes[i]);
|
---|
| 5487 | if (tmp == NULL)
|
---|
| 5488 | return;
|
---|
| 5489 | PyModule_AddObject(m, names[i], tmp);
|
---|
| 5490 | }
|
---|
| 5491 | }
|
---|
| 5492 | PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
|
---|
| 5493 | PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
|
---|
| 5494 | PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
|
---|
[2] | 5495 | #ifdef RCVALL_IPLEVEL
|
---|
[391] | 5496 | PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
|
---|
[2] | 5497 | #endif
|
---|
| 5498 | #ifdef RCVALL_MAX
|
---|
[391] | 5499 | PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
|
---|
[2] | 5500 | #endif
|
---|
| 5501 | #endif /* _MSTCPIP_ */
|
---|
| 5502 |
|
---|
[391] | 5503 | /* Initialize gethostbyname lock */
|
---|
[2] | 5504 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
|
---|
[391] | 5505 | netdb_lock = PyThread_allocate_lock();
|
---|
[2] | 5506 | #endif
|
---|
| 5507 | }
|
---|
| 5508 |
|
---|
| 5509 |
|
---|
| 5510 | #ifndef HAVE_INET_PTON
|
---|
| 5511 | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
|
---|
| 5512 |
|
---|
| 5513 | /* Simplistic emulation code for inet_pton that only works for IPv4 */
|
---|
| 5514 | /* These are not exposed because they do not set errno properly */
|
---|
| 5515 |
|
---|
| 5516 | int
|
---|
| 5517 | inet_pton(int af, const char *src, void *dst)
|
---|
| 5518 | {
|
---|
[391] | 5519 | if (af == AF_INET) {
|
---|
[2] | 5520 | #if (SIZEOF_INT != 4)
|
---|
| 5521 | #error "Not sure if in_addr_t exists and int is not 32-bits."
|
---|
| 5522 | #endif
|
---|
[391] | 5523 | unsigned int packed_addr;
|
---|
| 5524 | packed_addr = inet_addr(src);
|
---|
| 5525 | if (packed_addr == INADDR_NONE)
|
---|
| 5526 | return 0;
|
---|
| 5527 | memcpy(dst, &packed_addr, 4);
|
---|
| 5528 | return 1;
|
---|
| 5529 | }
|
---|
| 5530 | /* Should set errno to EAFNOSUPPORT */
|
---|
| 5531 | return -1;
|
---|
[2] | 5532 | }
|
---|
| 5533 |
|
---|
| 5534 | const char *
|
---|
| 5535 | inet_ntop(int af, const void *src, char *dst, socklen_t size)
|
---|
| 5536 | {
|
---|
[391] | 5537 | if (af == AF_INET) {
|
---|
| 5538 | struct in_addr packed_addr;
|
---|
| 5539 | if (size < 16)
|
---|
| 5540 | /* Should set errno to ENOSPC. */
|
---|
| 5541 | return NULL;
|
---|
| 5542 | memcpy(&packed_addr, src, sizeof(packed_addr));
|
---|
| 5543 | return strncpy(dst, inet_ntoa(packed_addr), size);
|
---|
| 5544 | }
|
---|
| 5545 | /* Should set errno to EAFNOSUPPORT */
|
---|
| 5546 | return NULL;
|
---|
[2] | 5547 | }
|
---|
| 5548 |
|
---|
| 5549 | #endif
|
---|
| 5550 | #endif
|
---|