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