Mercurial > cpython-lru_cache-weakref
comparison Modules/socketmodule.c @ 61467:f197407bdba2 3.1
Recorded merge of revisions 81032 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
................
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 May 2010 16:14:21 +0000 |
| parents | e918b84ef968 |
| children | 2a4ceebdfcfb |
comparison
equal
deleted
inserted
replaced
| 61459:f8c19c5f5dd2 | 61467:f197407bdba2 |
|---|---|
| 15 | 15 |
| 16 Module interface: | 16 Module interface: |
| 17 | 17 |
| 18 - socket.error: exception raised for socket specific errors | 18 - socket.error: exception raised for socket specific errors |
| 19 - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, | 19 - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
| 20 a subclass of socket.error | 20 a subclass of socket.error |
| 21 - socket.herror: exception raised for gethostby* errors, | 21 - socket.herror: exception raised for gethostby* errors, |
| 22 a subclass of socket.error | 22 a subclass of socket.error |
| 23 - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') | 23 - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
| 24 - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) | 24 - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
| 25 - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') | 25 - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
| 26 - socket.getprotobyname(protocolname) --> protocol number | 26 - socket.getprotobyname(protocolname) --> protocol number |
| 27 - socket.getservbyname(servicename[, protocolname]) --> port number | 27 - socket.getservbyname(servicename[, protocolname]) --> port number |
| 28 - socket.getservbyport(portnumber[, protocolname]) --> service name | 28 - socket.getservbyport(portnumber[, protocolname]) --> service name |
| 29 - socket.socket([family[, type [, proto, fileno]]]) --> new socket object | 29 - socket.socket([family[, type [, proto, fileno]]]) --> new socket object |
| 30 (fileno specifies a pre-existing socket file descriptor) | 30 (fileno specifies a pre-existing socket file descriptor) |
| 31 - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) | 31 - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
| 32 - socket.ntohs(16 bit value) --> new int object | 32 - socket.ntohs(16 bit value) --> new int object |
| 33 - socket.ntohl(32 bit value) --> new int object | 33 - socket.ntohl(32 bit value) --> new int object |
| 34 - socket.htons(16 bit value) --> new int object | 34 - socket.htons(16 bit value) --> new int object |
| 35 - socket.htonl(32 bit value) --> new int object | 35 - socket.htonl(32 bit value) --> new int object |
| 36 - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) | 36 - socket.getaddrinfo(host, port [, family, socktype, proto, flags]) |
| 37 --> List of (family, socktype, proto, canonname, sockaddr) | 37 --> List of (family, socktype, proto, canonname, sockaddr) |
| 38 - socket.getnameinfo(sockaddr, flags) --> (host, port) | 38 - socket.getnameinfo(sockaddr, flags) --> (host, port) |
| 39 - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> | 39 - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
| 40 - socket.has_ipv6: boolean value indicating if IPv6 is supported | 40 - socket.has_ipv6: boolean value indicating if IPv6 is supported |
| 41 - socket.inet_aton(IP address) -> 32-bit packed IP representation | 41 - socket.inet_aton(IP address) -> 32-bit packed IP representation |
| 42 - socket.inet_ntoa(packed IP) -> IP address string | 42 - socket.inet_ntoa(packed IP) -> IP address string |
| 52 the Ethernet protocol number to be received. For example: | 52 the Ethernet protocol number to be received. For example: |
| 53 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple | 53 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
| 54 specify packet-type and ha-type/addr. | 54 specify packet-type and ha-type/addr. |
| 55 - an AF_TIPC socket address is expressed as | 55 - an AF_TIPC socket address is expressed as |
| 56 (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: | 56 (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
| 57 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; | 57 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
| 58 and scope can be one of: | 58 and scope can be one of: |
| 59 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. | 59 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
| 60 The meaning of v1, v2 and v3 depends on the value of addr_type: | 60 The meaning of v1, v2 and v3 depends on the value of addr_type: |
| 61 if addr_type is TIPC_ADDR_NAME: | 61 if addr_type is TIPC_ADDR_NAME: |
| 62 v1 is the server type | 62 v1 is the server type |
| 63 v2 is the port identifier | 63 v2 is the port identifier |
| 64 v3 is ignored | 64 v3 is ignored |
| 65 if addr_type is TIPC_ADDR_NAMESEQ: | 65 if addr_type is TIPC_ADDR_NAMESEQ: |
| 66 v1 is the server type | 66 v1 is the server type |
| 67 v2 is the lower port number | 67 v2 is the lower port number |
| 68 v3 is the upper port number | 68 v3 is the upper port number |
| 69 if addr_type is TIPC_ADDR_ID: | 69 if addr_type is TIPC_ADDR_ID: |
| 70 v1 is the node | 70 v1 is the node |
| 71 v2 is the ref | 71 v2 is the ref |
| 72 v3 is ignored | 72 v3 is ignored |
| 73 | 73 |
| 74 | 74 |
| 75 Local naming conventions: | 75 Local naming conventions: |
| 76 | 76 |
| 77 - names starting with sock_ are socket object methods | 77 - names starting with sock_ are socket object methods |
| 281 #endif | 281 #endif |
| 282 | 282 |
| 283 #include <stddef.h> | 283 #include <stddef.h> |
| 284 | 284 |
| 285 #ifndef offsetof | 285 #ifndef offsetof |
| 286 # define offsetof(type, member) ((size_t)(&((type *)0)->member)) | 286 # define offsetof(type, member) ((size_t)(&((type *)0)->member)) |
| 287 #endif | 287 #endif |
| 288 | 288 |
| 289 #ifndef O_NONBLOCK | 289 #ifndef O_NONBLOCK |
| 290 # define O_NONBLOCK O_NDELAY | 290 # define O_NONBLOCK O_NDELAY |
| 291 #endif | 291 #endif |
| 349 #ifdef MS_WINDOWS | 349 #ifdef MS_WINDOWS |
| 350 /* On Windows a socket is really a handle not an fd */ | 350 /* On Windows a socket is really a handle not an fd */ |
| 351 static SOCKET | 351 static SOCKET |
| 352 dup_socket(SOCKET handle) | 352 dup_socket(SOCKET handle) |
| 353 { | 353 { |
| 354 HANDLE newhandle; | 354 HANDLE newhandle; |
| 355 | 355 |
| 356 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle, | 356 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle, |
| 357 GetCurrentProcess(), &newhandle, | 357 GetCurrentProcess(), &newhandle, |
| 358 0, FALSE, DUPLICATE_SAME_ACCESS)) | 358 0, FALSE, DUPLICATE_SAME_ACCESS)) |
| 359 { | 359 { |
| 360 WSASetLastError(GetLastError()); | 360 WSASetLastError(GetLastError()); |
| 361 return INVALID_SOCKET; | 361 return INVALID_SOCKET; |
| 362 } | 362 } |
| 363 return (SOCKET)newhandle; | 363 return (SOCKET)newhandle; |
| 364 } | 364 } |
| 365 #define SOCKETCLOSE closesocket | 365 #define SOCKETCLOSE closesocket |
| 366 #else | 366 #else |
| 367 /* On Unix we can use dup to duplicate the file descriptor of a socket*/ | 367 /* On Unix we can use dup to duplicate the file descriptor of a socket*/ |
| 368 #define dup_socket(fd) dup(fd) | 368 #define dup_socket(fd) dup(fd) |
| 416 #ifdef __VMS | 416 #ifdef __VMS |
| 417 /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ | 417 /* TCP/IP Services for VMS uses a maximum send/recv buffer length */ |
| 418 #define SEGMENT_SIZE (32 * 1024 -1) | 418 #define SEGMENT_SIZE (32 * 1024 -1) |
| 419 #endif | 419 #endif |
| 420 | 420 |
| 421 #define SAS2SA(x) ((struct sockaddr *)(x)) | 421 #define SAS2SA(x) ((struct sockaddr *)(x)) |
| 422 | 422 |
| 423 /* | 423 /* |
| 424 * Constants for getnameinfo() | 424 * Constants for getnameinfo() |
| 425 */ | 425 */ |
| 426 #if !defined(NI_MAXHOST) | 426 #if !defined(NI_MAXHOST) |
| 471 #endif | 471 #endif |
| 472 | 472 |
| 473 static PyObject* | 473 static PyObject* |
| 474 select_error(void) | 474 select_error(void) |
| 475 { | 475 { |
| 476 PyErr_SetString(socket_error, "unable to select on socket"); | 476 PyErr_SetString(socket_error, "unable to select on socket"); |
| 477 return NULL; | 477 return NULL; |
| 478 } | 478 } |
| 479 | 479 |
| 480 /* Convenience function to raise an error according to errno | 480 /* Convenience function to raise an error according to errno |
| 481 and return a NULL pointer from a function. */ | 481 and return a NULL pointer from a function. */ |
| 482 | 482 |
| 483 static PyObject * | 483 static PyObject * |
| 484 set_error(void) | 484 set_error(void) |
| 485 { | 485 { |
| 486 #ifdef MS_WINDOWS | 486 #ifdef MS_WINDOWS |
| 487 int err_no = WSAGetLastError(); | 487 int err_no = WSAGetLastError(); |
| 488 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which | 488 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
| 489 recognizes the error codes used by both GetLastError() and | 489 recognizes the error codes used by both GetLastError() and |
| 490 WSAGetLastError */ | 490 WSAGetLastError */ |
| 491 if (err_no) | 491 if (err_no) |
| 492 return PyErr_SetExcFromWindowsErr(socket_error, err_no); | 492 return PyErr_SetExcFromWindowsErr(socket_error, err_no); |
| 493 #endif | 493 #endif |
| 494 | 494 |
| 495 #if defined(PYOS_OS2) && !defined(PYCC_GCC) | 495 #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 496 if (sock_errno() != NO_ERROR) { | 496 if (sock_errno() != NO_ERROR) { |
| 497 APIRET rc; | 497 APIRET rc; |
| 498 ULONG msglen; | 498 ULONG msglen; |
| 499 char outbuf[100]; | 499 char outbuf[100]; |
| 500 int myerrorcode = sock_errno(); | 500 int myerrorcode = sock_errno(); |
| 501 | 501 |
| 502 /* Retrieve socket-related error message from MPTN.MSG file */ | 502 /* Retrieve socket-related error message from MPTN.MSG file */ |
| 503 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), | 503 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf), |
| 504 myerrorcode - SOCBASEERR + 26, | 504 myerrorcode - SOCBASEERR + 26, |
| 505 "mptn.msg", | 505 "mptn.msg", |
| 506 &msglen); | 506 &msglen); |
| 507 if (rc == NO_ERROR) { | 507 if (rc == NO_ERROR) { |
| 508 PyObject *v; | 508 PyObject *v; |
| 509 | 509 |
| 510 /* OS/2 doesn't guarantee a terminator */ | 510 /* OS/2 doesn't guarantee a terminator */ |
| 511 outbuf[msglen] = '\0'; | 511 outbuf[msglen] = '\0'; |
| 512 if (strlen(outbuf) > 0) { | 512 if (strlen(outbuf) > 0) { |
| 513 /* If non-empty msg, trim CRLF */ | 513 /* If non-empty msg, trim CRLF */ |
| 514 char *lastc = &outbuf[ strlen(outbuf)-1 ]; | 514 char *lastc = &outbuf[ strlen(outbuf)-1 ]; |
| 515 while (lastc > outbuf && | 515 while (lastc > outbuf && |
| 516 isspace(Py_CHARMASK(*lastc))) { | 516 isspace(Py_CHARMASK(*lastc))) { |
| 517 /* Trim trailing whitespace (CRLF) */ | 517 /* Trim trailing whitespace (CRLF) */ |
| 518 *lastc-- = '\0'; | 518 *lastc-- = '\0'; |
| 519 } | 519 } |
| 520 } | 520 } |
| 521 v = Py_BuildValue("(is)", myerrorcode, outbuf); | 521 v = Py_BuildValue("(is)", myerrorcode, outbuf); |
| 522 if (v != NULL) { | 522 if (v != NULL) { |
| 523 PyErr_SetObject(socket_error, v); | 523 PyErr_SetObject(socket_error, v); |
| 524 Py_DECREF(v); | 524 Py_DECREF(v); |
| 525 } | 525 } |
| 526 return NULL; | 526 return NULL; |
| 527 } | 527 } |
| 528 } | 528 } |
| 529 #endif | 529 #endif |
| 530 | 530 |
| 531 return PyErr_SetFromErrno(socket_error); | 531 return PyErr_SetFromErrno(socket_error); |
| 532 } | 532 } |
| 533 | 533 |
| 534 | 534 |
| 535 static PyObject * | 535 static PyObject * |
| 536 set_herror(int h_error) | 536 set_herror(int h_error) |
| 537 { | 537 { |
| 538 PyObject *v; | 538 PyObject *v; |
| 539 | 539 |
| 540 #ifdef HAVE_HSTRERROR | 540 #ifdef HAVE_HSTRERROR |
| 541 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); | 541 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
| 542 #else | 542 #else |
| 543 v = Py_BuildValue("(is)", h_error, "host not found"); | 543 v = Py_BuildValue("(is)", h_error, "host not found"); |
| 544 #endif | 544 #endif |
| 545 if (v != NULL) { | 545 if (v != NULL) { |
| 546 PyErr_SetObject(socket_herror, v); | 546 PyErr_SetObject(socket_herror, v); |
| 547 Py_DECREF(v); | 547 Py_DECREF(v); |
| 548 } | 548 } |
| 549 | 549 |
| 550 return NULL; | 550 return NULL; |
| 551 } | 551 } |
| 552 | 552 |
| 553 | 553 |
| 554 static PyObject * | 554 static PyObject * |
| 555 set_gaierror(int error) | 555 set_gaierror(int error) |
| 556 { | 556 { |
| 557 PyObject *v; | 557 PyObject *v; |
| 558 | 558 |
| 559 #ifdef EAI_SYSTEM | 559 #ifdef EAI_SYSTEM |
| 560 /* EAI_SYSTEM is not available on Windows XP. */ | 560 /* EAI_SYSTEM is not available on Windows XP. */ |
| 561 if (error == EAI_SYSTEM) | 561 if (error == EAI_SYSTEM) |
| 562 return set_error(); | 562 return set_error(); |
| 563 #endif | 563 #endif |
| 564 | 564 |
| 565 #ifdef HAVE_GAI_STRERROR | 565 #ifdef HAVE_GAI_STRERROR |
| 566 v = Py_BuildValue("(is)", error, gai_strerror(error)); | 566 v = Py_BuildValue("(is)", error, gai_strerror(error)); |
| 567 #else | 567 #else |
| 568 v = Py_BuildValue("(is)", error, "getaddrinfo failed"); | 568 v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
| 569 #endif | 569 #endif |
| 570 if (v != NULL) { | 570 if (v != NULL) { |
| 571 PyErr_SetObject(socket_gaierror, v); | 571 PyErr_SetObject(socket_gaierror, v); |
| 572 Py_DECREF(v); | 572 Py_DECREF(v); |
| 573 } | 573 } |
| 574 | 574 |
| 575 return NULL; | 575 return NULL; |
| 576 } | 576 } |
| 577 | 577 |
| 578 #ifdef __VMS | 578 #ifdef __VMS |
| 579 /* Function to send in segments */ | 579 /* Function to send in segments */ |
| 580 static int | 580 static int |
| 581 sendsegmented(int sock_fd, char *buf, int len, int flags) | 581 sendsegmented(int sock_fd, char *buf, int len, int flags) |
| 582 { | 582 { |
| 583 int n = 0; | 583 int n = 0; |
| 584 int remaining = len; | 584 int remaining = len; |
| 585 | 585 |
| 586 while (remaining > 0) { | 586 while (remaining > 0) { |
| 587 unsigned int segment; | 587 unsigned int segment; |
| 588 | 588 |
| 589 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); | 589 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); |
| 590 n = send(sock_fd, buf, segment, flags); | 590 n = send(sock_fd, buf, segment, flags); |
| 591 if (n < 0) { | 591 if (n < 0) { |
| 592 return n; | 592 return n; |
| 593 } | 593 } |
| 594 remaining -= segment; | 594 remaining -= segment; |
| 595 buf += segment; | 595 buf += segment; |
| 596 } /* end while */ | 596 } /* end while */ |
| 597 | 597 |
| 598 return len; | 598 return len; |
| 599 } | 599 } |
| 600 #endif | 600 #endif |
| 601 | 601 |
| 602 /* Function to perform the setting of socket blocking mode | 602 /* Function to perform the setting of socket blocking mode |
| 603 internally. block = (1 | 0). */ | 603 internally. block = (1 | 0). */ |
| 604 static int | 604 static int |
| 605 internal_setblocking(PySocketSockObject *s, int block) | 605 internal_setblocking(PySocketSockObject *s, int block) |
| 606 { | 606 { |
| 607 #ifndef MS_WINDOWS | 607 #ifndef MS_WINDOWS |
| 608 int delay_flag; | 608 int delay_flag; |
| 609 #endif | 609 #endif |
| 610 | 610 |
| 611 Py_BEGIN_ALLOW_THREADS | 611 Py_BEGIN_ALLOW_THREADS |
| 612 #ifndef MS_WINDOWS | 612 #ifndef MS_WINDOWS |
| 613 #if defined(PYOS_OS2) && !defined(PYCC_GCC) | 613 #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 614 block = !block; | 614 block = !block; |
| 615 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); | 615 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block)); |
| 616 #elif defined(__VMS) | 616 #elif defined(__VMS) |
| 617 block = !block; | 617 block = !block; |
| 618 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); | 618 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); |
| 619 #else /* !PYOS_OS2 && !__VMS */ | 619 #else /* !PYOS_OS2 && !__VMS */ |
| 620 delay_flag = fcntl(s->sock_fd, F_GETFL, 0); | 620 delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
| 621 if (block) | 621 if (block) |
| 622 delay_flag &= (~O_NONBLOCK); | 622 delay_flag &= (~O_NONBLOCK); |
| 623 else | 623 else |
| 624 delay_flag |= O_NONBLOCK; | 624 delay_flag |= O_NONBLOCK; |
| 625 fcntl(s->sock_fd, F_SETFL, delay_flag); | 625 fcntl(s->sock_fd, F_SETFL, delay_flag); |
| 626 #endif /* !PYOS_OS2 */ | 626 #endif /* !PYOS_OS2 */ |
| 627 #else /* MS_WINDOWS */ | 627 #else /* MS_WINDOWS */ |
| 628 block = !block; | 628 block = !block; |
| 629 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); | 629 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |
| 630 #endif /* MS_WINDOWS */ | 630 #endif /* MS_WINDOWS */ |
| 631 Py_END_ALLOW_THREADS | 631 Py_END_ALLOW_THREADS |
| 632 | 632 |
| 633 /* Since these don't return anything */ | 633 /* Since these don't return anything */ |
| 634 return 1; | 634 return 1; |
| 635 } | 635 } |
| 636 | 636 |
| 637 /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0). | 637 /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0). |
| 638 The argument writing indicates the direction. | 638 The argument writing indicates the direction. |
| 639 This does not raise an exception; we'll let our caller do that | 639 This does not raise an exception; we'll let our caller do that |
| 640 after they've reacquired the interpreter lock. | 640 after they've reacquired the interpreter lock. |
| 641 Returns 1 on timeout, -1 on error, 0 otherwise. */ | 641 Returns 1 on timeout, -1 on error, 0 otherwise. */ |
| 642 static int | 642 static int |
| 643 internal_select(PySocketSockObject *s, int writing) | 643 internal_select(PySocketSockObject *s, int writing) |
| 644 { | 644 { |
| 645 int n; | 645 int n; |
| 646 | 646 |
| 647 /* Nothing to do unless we're in timeout mode (not non-blocking) */ | 647 /* Nothing to do unless we're in timeout mode (not non-blocking) */ |
| 648 if (s->sock_timeout <= 0.0) | 648 if (s->sock_timeout <= 0.0) |
| 649 return 0; | 649 return 0; |
| 650 | 650 |
| 651 /* Guard against closed socket */ | 651 /* Guard against closed socket */ |
| 652 if (s->sock_fd < 0) | 652 if (s->sock_fd < 0) |
| 653 return 0; | 653 return 0; |
| 654 | 654 |
| 655 /* Prefer poll, if available, since you can poll() any fd | 655 /* Prefer poll, if available, since you can poll() any fd |
| 656 * which can't be done with select(). */ | 656 * which can't be done with select(). */ |
| 657 #ifdef HAVE_POLL | 657 #ifdef HAVE_POLL |
| 658 { | 658 { |
| 659 struct pollfd pollfd; | 659 struct pollfd pollfd; |
| 660 int timeout; | 660 int timeout; |
| 661 | 661 |
| 662 pollfd.fd = s->sock_fd; | 662 pollfd.fd = s->sock_fd; |
| 663 pollfd.events = writing ? POLLOUT : POLLIN; | 663 pollfd.events = writing ? POLLOUT : POLLIN; |
| 664 | 664 |
| 665 /* s->sock_timeout is in seconds, timeout in ms */ | 665 /* s->sock_timeout is in seconds, timeout in ms */ |
| 666 timeout = (int)(s->sock_timeout * 1000 + 0.5); | 666 timeout = (int)(s->sock_timeout * 1000 + 0.5); |
| 667 n = poll(&pollfd, 1, timeout); | 667 n = poll(&pollfd, 1, timeout); |
| 668 } | 668 } |
| 669 #else | 669 #else |
| 670 { | 670 { |
| 671 /* Construct the arguments to select */ | 671 /* Construct the arguments to select */ |
| 672 fd_set fds; | 672 fd_set fds; |
| 673 struct timeval tv; | 673 struct timeval tv; |
| 674 tv.tv_sec = (int)s->sock_timeout; | 674 tv.tv_sec = (int)s->sock_timeout; |
| 675 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); | 675 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 676 FD_ZERO(&fds); | 676 FD_ZERO(&fds); |
| 677 FD_SET(s->sock_fd, &fds); | 677 FD_SET(s->sock_fd, &fds); |
| 678 | 678 |
| 679 /* See if the socket is ready */ | 679 /* See if the socket is ready */ |
| 680 if (writing) | 680 if (writing) |
| 681 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); | 681 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv); |
| 682 else | 682 else |
| 683 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); | 683 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv); |
| 684 } | 684 } |
| 685 #endif | 685 #endif |
| 686 | 686 |
| 687 if (n < 0) | 687 if (n < 0) |
| 688 return -1; | 688 return -1; |
| 689 if (n == 0) | 689 if (n == 0) |
| 690 return 1; | 690 return 1; |
| 691 return 0; | 691 return 0; |
| 692 } | 692 } |
| 693 | 693 |
| 694 /* Initialize a new socket object. */ | 694 /* Initialize a new socket object. */ |
| 695 | 695 |
| 696 static double defaulttimeout = -1.0; /* Default timeout for new sockets */ | 696 static double defaulttimeout = -1.0; /* Default timeout for new sockets */ |
| 697 | 697 |
| 698 static void | 698 static void |
| 699 init_sockobject(PySocketSockObject *s, | 699 init_sockobject(PySocketSockObject *s, |
| 700 SOCKET_T fd, int family, int type, int proto) | 700 SOCKET_T fd, int family, int type, int proto) |
| 701 { | 701 { |
| 702 s->sock_fd = fd; | 702 s->sock_fd = fd; |
| 703 s->sock_family = family; | 703 s->sock_family = family; |
| 704 s->sock_type = type; | 704 s->sock_type = type; |
| 705 s->sock_proto = proto; | 705 s->sock_proto = proto; |
| 706 s->sock_timeout = defaulttimeout; | 706 s->sock_timeout = defaulttimeout; |
| 707 | 707 |
| 708 s->errorhandler = &set_error; | 708 s->errorhandler = &set_error; |
| 709 | 709 |
| 710 if (defaulttimeout >= 0.0) | 710 if (defaulttimeout >= 0.0) |
| 711 internal_setblocking(s, 0); | 711 internal_setblocking(s, 0); |
| 712 | 712 |
| 713 } | 713 } |
| 714 | 714 |
| 715 | 715 |
| 716 /* Create a new socket object. | 716 /* Create a new socket object. |
| 719 in NEWOBJ()). */ | 719 in NEWOBJ()). */ |
| 720 | 720 |
| 721 static PySocketSockObject * | 721 static PySocketSockObject * |
| 722 new_sockobject(SOCKET_T fd, int family, int type, int proto) | 722 new_sockobject(SOCKET_T fd, int family, int type, int proto) |
| 723 { | 723 { |
| 724 PySocketSockObject *s; | 724 PySocketSockObject *s; |
| 725 s = (PySocketSockObject *) | 725 s = (PySocketSockObject *) |
| 726 PyType_GenericNew(&sock_type, NULL, NULL); | 726 PyType_GenericNew(&sock_type, NULL, NULL); |
| 727 if (s != NULL) | 727 if (s != NULL) |
| 728 init_sockobject(s, fd, family, type, proto); | 728 init_sockobject(s, fd, family, type, proto); |
| 729 return s; | 729 return s; |
| 730 } | 730 } |
| 731 | 731 |
| 732 | 732 |
| 733 /* Lock to allow python interpreter to continue, but only allow one | 733 /* Lock to allow python interpreter to continue, but only allow one |
| 734 thread to be in gethostbyname or getaddrinfo */ | 734 thread to be in gethostbyname or getaddrinfo */ |
| 744 an error occurred; then an exception is raised. */ | 744 an error occurred; then an exception is raised. */ |
| 745 | 745 |
| 746 static int | 746 static int |
| 747 setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af) | 747 setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af) |
| 748 { | 748 { |
| 749 struct addrinfo hints, *res; | 749 struct addrinfo hints, *res; |
| 750 int error; | 750 int error; |
| 751 int d1, d2, d3, d4; | 751 int d1, d2, d3, d4; |
| 752 char ch; | 752 char ch; |
| 753 | 753 |
| 754 memset((void *) addr_ret, '\0', sizeof(*addr_ret)); | 754 memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
| 755 if (name[0] == '\0') { | 755 if (name[0] == '\0') { |
| 756 int siz; | 756 int siz; |
| 757 memset(&hints, 0, sizeof(hints)); | 757 memset(&hints, 0, sizeof(hints)); |
| 758 hints.ai_family = af; | 758 hints.ai_family = af; |
| 759 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | 759 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
| 760 hints.ai_flags = AI_PASSIVE; | 760 hints.ai_flags = AI_PASSIVE; |
| 761 Py_BEGIN_ALLOW_THREADS | 761 Py_BEGIN_ALLOW_THREADS |
| 762 ACQUIRE_GETADDRINFO_LOCK | 762 ACQUIRE_GETADDRINFO_LOCK |
| 763 error = getaddrinfo(NULL, "0", &hints, &res); | 763 error = getaddrinfo(NULL, "0", &hints, &res); |
| 764 Py_END_ALLOW_THREADS | 764 Py_END_ALLOW_THREADS |
| 765 /* We assume that those thread-unsafe getaddrinfo() versions | 765 /* We assume that those thread-unsafe getaddrinfo() versions |
| 766 *are* safe regarding their return value, ie. that a | 766 *are* safe regarding their return value, ie. that a |
| 767 subsequent call to getaddrinfo() does not destroy the | 767 subsequent call to getaddrinfo() does not destroy the |
| 768 outcome of the first call. */ | 768 outcome of the first call. */ |
| 769 RELEASE_GETADDRINFO_LOCK | 769 RELEASE_GETADDRINFO_LOCK |
| 770 if (error) { | 770 if (error) { |
| 771 set_gaierror(error); | 771 set_gaierror(error); |
| 772 return -1; | 772 return -1; |
| 773 } | 773 } |
| 774 switch (res->ai_family) { | 774 switch (res->ai_family) { |
| 775 case AF_INET: | 775 case AF_INET: |
| 776 siz = 4; | 776 siz = 4; |
| 777 break; | 777 break; |
| 778 #ifdef ENABLE_IPV6 | 778 #ifdef ENABLE_IPV6 |
| 779 case AF_INET6: | 779 case AF_INET6: |
| 780 siz = 16; | 780 siz = 16; |
| 781 break; | 781 break; |
| 782 #endif | 782 #endif |
| 783 default: | 783 default: |
| 784 freeaddrinfo(res); | 784 freeaddrinfo(res); |
| 785 PyErr_SetString(socket_error, | 785 PyErr_SetString(socket_error, |
| 786 "unsupported address family"); | 786 "unsupported address family"); |
| 787 return -1; | 787 return -1; |
| 788 } | 788 } |
| 789 if (res->ai_next) { | 789 if (res->ai_next) { |
| 790 freeaddrinfo(res); | 790 freeaddrinfo(res); |
| 791 PyErr_SetString(socket_error, | 791 PyErr_SetString(socket_error, |
| 792 "wildcard resolved to multiple address"); | 792 "wildcard resolved to multiple address"); |
| 793 return -1; | 793 return -1; |
| 794 } | 794 } |
| 795 if (res->ai_addrlen < addr_ret_size) | 795 if (res->ai_addrlen < addr_ret_size) |
| 796 addr_ret_size = res->ai_addrlen; | 796 addr_ret_size = res->ai_addrlen; |
| 797 memcpy(addr_ret, res->ai_addr, addr_ret_size); | 797 memcpy(addr_ret, res->ai_addr, addr_ret_size); |
| 798 freeaddrinfo(res); | 798 freeaddrinfo(res); |
| 799 return siz; | 799 return siz; |
| 800 } | 800 } |
| 801 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { | 801 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { |
| 802 struct sockaddr_in *sin; | 802 struct sockaddr_in *sin; |
| 803 if (af != AF_INET && af != AF_UNSPEC) { | 803 if (af != AF_INET && af != AF_UNSPEC) { |
| 804 PyErr_SetString(socket_error, | 804 PyErr_SetString(socket_error, |
| 805 "address family mismatched"); | 805 "address family mismatched"); |
| 806 return -1; | 806 return -1; |
| 807 } | 807 } |
| 808 sin = (struct sockaddr_in *)addr_ret; | 808 sin = (struct sockaddr_in *)addr_ret; |
| 809 memset((void *) sin, '\0', sizeof(*sin)); | 809 memset((void *) sin, '\0', sizeof(*sin)); |
| 810 sin->sin_family = AF_INET; | 810 sin->sin_family = AF_INET; |
| 811 #ifdef HAVE_SOCKADDR_SA_LEN | 811 #ifdef HAVE_SOCKADDR_SA_LEN |
| 812 sin->sin_len = sizeof(*sin); | 812 sin->sin_len = sizeof(*sin); |
| 813 #endif | 813 #endif |
| 814 sin->sin_addr.s_addr = INADDR_BROADCAST; | 814 sin->sin_addr.s_addr = INADDR_BROADCAST; |
| 815 return sizeof(sin->sin_addr); | 815 return sizeof(sin->sin_addr); |
| 816 } | 816 } |
| 817 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && | 817 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 && |
| 818 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && | 818 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 && |
| 819 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { | 819 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) { |
| 820 struct sockaddr_in *sin; | 820 struct sockaddr_in *sin; |
| 821 sin = (struct sockaddr_in *)addr_ret; | 821 sin = (struct sockaddr_in *)addr_ret; |
| 822 sin->sin_addr.s_addr = htonl( | 822 sin->sin_addr.s_addr = htonl( |
| 823 ((long) d1 << 24) | ((long) d2 << 16) | | 823 ((long) d1 << 24) | ((long) d2 << 16) | |
| 824 ((long) d3 << 8) | ((long) d4 << 0)); | 824 ((long) d3 << 8) | ((long) d4 << 0)); |
| 825 sin->sin_family = AF_INET; | 825 sin->sin_family = AF_INET; |
| 826 #ifdef HAVE_SOCKADDR_SA_LEN | 826 #ifdef HAVE_SOCKADDR_SA_LEN |
| 827 sin->sin_len = sizeof(*sin); | 827 sin->sin_len = sizeof(*sin); |
| 828 #endif | 828 #endif |
| 829 return 4; | 829 return 4; |
| 830 } | 830 } |
| 831 memset(&hints, 0, sizeof(hints)); | 831 memset(&hints, 0, sizeof(hints)); |
| 832 hints.ai_family = af; | 832 hints.ai_family = af; |
| 833 Py_BEGIN_ALLOW_THREADS | 833 Py_BEGIN_ALLOW_THREADS |
| 834 ACQUIRE_GETADDRINFO_LOCK | 834 ACQUIRE_GETADDRINFO_LOCK |
| 835 error = getaddrinfo(name, NULL, &hints, &res); | 835 error = getaddrinfo(name, NULL, &hints, &res); |
| 836 #if defined(__digital__) && defined(__unix__) | 836 #if defined(__digital__) && defined(__unix__) |
| 837 if (error == EAI_NONAME && af == AF_UNSPEC) { | 837 if (error == EAI_NONAME && af == AF_UNSPEC) { |
| 838 /* On Tru64 V5.1, numeric-to-addr conversion fails | 838 /* On Tru64 V5.1, numeric-to-addr conversion fails |
| 839 if no address family is given. Assume IPv4 for now.*/ | 839 if no address family is given. Assume IPv4 for now.*/ |
| 840 hints.ai_family = AF_INET; | 840 hints.ai_family = AF_INET; |
| 841 error = getaddrinfo(name, NULL, &hints, &res); | 841 error = getaddrinfo(name, NULL, &hints, &res); |
| 842 } | 842 } |
| 843 #endif | 843 #endif |
| 844 Py_END_ALLOW_THREADS | 844 Py_END_ALLOW_THREADS |
| 845 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ | 845 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 846 if (error) { | 846 if (error) { |
| 847 set_gaierror(error); | 847 set_gaierror(error); |
| 848 return -1; | 848 return -1; |
| 849 } | 849 } |
| 850 if (res->ai_addrlen < addr_ret_size) | 850 if (res->ai_addrlen < addr_ret_size) |
| 851 addr_ret_size = res->ai_addrlen; | 851 addr_ret_size = res->ai_addrlen; |
| 852 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); | 852 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
| 853 freeaddrinfo(res); | 853 freeaddrinfo(res); |
| 854 switch (addr_ret->sa_family) { | 854 switch (addr_ret->sa_family) { |
| 855 case AF_INET: | 855 case AF_INET: |
| 856 return 4; | 856 return 4; |
| 857 #ifdef ENABLE_IPV6 | 857 #ifdef ENABLE_IPV6 |
| 858 case AF_INET6: | 858 case AF_INET6: |
| 859 return 16; | 859 return 16; |
| 860 #endif | 860 #endif |
| 861 default: | 861 default: |
| 862 PyErr_SetString(socket_error, "unknown address family"); | 862 PyErr_SetString(socket_error, "unknown address family"); |
| 863 return -1; | 863 return -1; |
| 864 } | 864 } |
| 865 } | 865 } |
| 866 | 866 |
| 867 | 867 |
| 868 /* Create a string object representing an IP address. | 868 /* Create a string object representing an IP address. |
| 869 This is always a string of the form 'dd.dd.dd.dd' (with variable | 869 This is always a string of the form 'dd.dd.dd.dd' (with variable |
| 870 size numbers). */ | 870 size numbers). */ |
| 871 | 871 |
| 872 static PyObject * | 872 static PyObject * |
| 873 makeipaddr(struct sockaddr *addr, int addrlen) | 873 makeipaddr(struct sockaddr *addr, int addrlen) |
| 874 { | 874 { |
| 875 char buf[NI_MAXHOST]; | 875 char buf[NI_MAXHOST]; |
| 876 int error; | 876 int error; |
| 877 | 877 |
| 878 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, | 878 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 879 NI_NUMERICHOST); | 879 NI_NUMERICHOST); |
| 880 if (error) { | 880 if (error) { |
| 881 set_gaierror(error); | 881 set_gaierror(error); |
| 882 return NULL; | 882 return NULL; |
| 883 } | 883 } |
| 884 return PyUnicode_FromString(buf); | 884 return PyUnicode_FromString(buf); |
| 885 } | 885 } |
| 886 | 886 |
| 887 | 887 |
| 888 #ifdef USE_BLUETOOTH | 888 #ifdef USE_BLUETOOTH |
| 889 /* Convert a string representation of a Bluetooth address into a numeric | 889 /* Convert a string representation of a Bluetooth address into a numeric |
| 891 an error occurred. */ | 891 an error occurred. */ |
| 892 | 892 |
| 893 static int | 893 static int |
| 894 setbdaddr(char *name, bdaddr_t *bdaddr) | 894 setbdaddr(char *name, bdaddr_t *bdaddr) |
| 895 { | 895 { |
| 896 unsigned int b0, b1, b2, b3, b4, b5; | 896 unsigned int b0, b1, b2, b3, b4, b5; |
| 897 char ch; | 897 char ch; |
| 898 int n; | 898 int n; |
| 899 | 899 |
| 900 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", | 900 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
| 901 &b5, &b4, &b3, &b2, &b1, &b0, &ch); | 901 &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
| 902 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { | 902 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
| 903 bdaddr->b[0] = b0; | 903 bdaddr->b[0] = b0; |
| 904 bdaddr->b[1] = b1; | 904 bdaddr->b[1] = b1; |
| 905 bdaddr->b[2] = b2; | 905 bdaddr->b[2] = b2; |
| 906 bdaddr->b[3] = b3; | 906 bdaddr->b[3] = b3; |
| 907 bdaddr->b[4] = b4; | 907 bdaddr->b[4] = b4; |
| 908 bdaddr->b[5] = b5; | 908 bdaddr->b[5] = b5; |
| 909 return 6; | 909 return 6; |
| 910 } else { | 910 } else { |
| 911 PyErr_SetString(socket_error, "bad bluetooth address"); | 911 PyErr_SetString(socket_error, "bad bluetooth address"); |
| 912 return -1; | 912 return -1; |
| 913 } | 913 } |
| 914 } | 914 } |
| 915 | 915 |
| 916 /* Create a string representation of the Bluetooth address. This is always a | 916 /* Create a string representation of the Bluetooth address. This is always a |
| 917 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal | 917 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
| 918 value (zero padded if necessary). */ | 918 value (zero padded if necessary). */ |
| 919 | 919 |
| 920 static PyObject * | 920 static PyObject * |
| 921 makebdaddr(bdaddr_t *bdaddr) | 921 makebdaddr(bdaddr_t *bdaddr) |
| 922 { | 922 { |
| 923 char buf[(6 * 2) + 5 + 1]; | 923 char buf[(6 * 2) + 5 + 1]; |
| 924 | 924 |
| 925 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", | 925 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 926 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], | 926 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
| 927 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); | 927 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
| 928 return PyUnicode_FromString(buf); | 928 return PyUnicode_FromString(buf); |
| 929 } | 929 } |
| 930 #endif | 930 #endif |
| 931 | 931 |
| 932 | 932 |
| 933 /* Create an object representing the given socket address, | 933 /* Create an object representing the given socket address, |
| 937 | 937 |
| 938 /*ARGSUSED*/ | 938 /*ARGSUSED*/ |
| 939 static PyObject * | 939 static PyObject * |
| 940 makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) | 940 makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto) |
| 941 { | 941 { |
| 942 if (addrlen == 0) { | 942 if (addrlen == 0) { |
| 943 /* No address -- may be recvfrom() from known socket */ | 943 /* No address -- may be recvfrom() from known socket */ |
| 944 Py_INCREF(Py_None); | 944 Py_INCREF(Py_None); |
| 945 return Py_None; | 945 return Py_None; |
| 946 } | 946 } |
| 947 | 947 |
| 948 switch (addr->sa_family) { | 948 switch (addr->sa_family) { |
| 949 | 949 |
| 950 case AF_INET: | 950 case AF_INET: |
| 951 { | 951 { |
| 952 struct sockaddr_in *a; | 952 struct sockaddr_in *a; |
| 953 PyObject *addrobj = makeipaddr(addr, sizeof(*a)); | 953 PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 954 PyObject *ret = NULL; | 954 PyObject *ret = NULL; |
| 955 if (addrobj) { | 955 if (addrobj) { |
| 956 a = (struct sockaddr_in *)addr; | 956 a = (struct sockaddr_in *)addr; |
| 957 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); | 957 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
| 958 Py_DECREF(addrobj); | 958 Py_DECREF(addrobj); |
| 959 } | 959 } |
| 960 return ret; | 960 return ret; |
| 961 } | 961 } |
| 962 | 962 |
| 963 #if defined(AF_UNIX) | 963 #if defined(AF_UNIX) |
| 964 case AF_UNIX: | 964 case AF_UNIX: |
| 965 { | 965 { |
| 966 struct sockaddr_un *a = (struct sockaddr_un *) addr; | 966 struct sockaddr_un *a = (struct sockaddr_un *) addr; |
| 967 #ifdef linux | 967 #ifdef linux |
| 968 if (a->sun_path[0] == 0) { /* Linux abstract namespace */ | 968 if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
| 969 addrlen -= offsetof(struct sockaddr_un, sun_path); | 969 addrlen -= offsetof(struct sockaddr_un, sun_path); |
| 970 return PyBytes_FromStringAndSize(a->sun_path, addrlen); | 970 return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
| 971 } | 971 } |
| 972 else | 972 else |
| 973 #endif /* linux */ | 973 #endif /* linux */ |
| 974 { | 974 { |
| 975 /* regular NULL-terminated string */ | 975 /* regular NULL-terminated string */ |
| 976 return PyUnicode_FromString(a->sun_path); | 976 return PyUnicode_FromString(a->sun_path); |
| 977 } | 977 } |
| 978 } | 978 } |
| 979 #endif /* AF_UNIX */ | 979 #endif /* AF_UNIX */ |
| 980 | 980 |
| 981 #if defined(AF_NETLINK) | 981 #if defined(AF_NETLINK) |
| 982 case AF_NETLINK: | 982 case AF_NETLINK: |
| 983 { | 983 { |
| 984 struct sockaddr_nl *a = (struct sockaddr_nl *) addr; | 984 struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
| 985 return Py_BuildValue("II", a->nl_pid, a->nl_groups); | 985 return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
| 986 } | 986 } |
| 987 #endif /* AF_NETLINK */ | 987 #endif /* AF_NETLINK */ |
| 988 | 988 |
| 989 #ifdef ENABLE_IPV6 | 989 #ifdef ENABLE_IPV6 |
| 990 case AF_INET6: | 990 case AF_INET6: |
| 991 { | 991 { |
| 992 struct sockaddr_in6 *a; | 992 struct sockaddr_in6 *a; |
| 993 PyObject *addrobj = makeipaddr(addr, sizeof(*a)); | 993 PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
| 994 PyObject *ret = NULL; | 994 PyObject *ret = NULL; |
| 995 if (addrobj) { | 995 if (addrobj) { |
| 996 a = (struct sockaddr_in6 *)addr; | 996 a = (struct sockaddr_in6 *)addr; |
| 997 ret = Py_BuildValue("Oiii", | 997 ret = Py_BuildValue("Oiii", |
| 998 addrobj, | 998 addrobj, |
| 999 ntohs(a->sin6_port), | 999 ntohs(a->sin6_port), |
| 1000 a->sin6_flowinfo, | 1000 a->sin6_flowinfo, |
| 1001 a->sin6_scope_id); | 1001 a->sin6_scope_id); |
| 1002 Py_DECREF(addrobj); | 1002 Py_DECREF(addrobj); |
| 1003 } | 1003 } |
| 1004 return ret; | 1004 return ret; |
| 1005 } | 1005 } |
| 1006 #endif | 1006 #endif |
| 1007 | 1007 |
| 1008 #ifdef USE_BLUETOOTH | 1008 #ifdef USE_BLUETOOTH |
| 1009 case AF_BLUETOOTH: | 1009 case AF_BLUETOOTH: |
| 1010 switch (proto) { | 1010 switch (proto) { |
| 1011 | 1011 |
| 1012 case BTPROTO_L2CAP: | 1012 case BTPROTO_L2CAP: |
| 1013 { | 1013 { |
| 1014 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; | 1014 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
| 1015 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); | 1015 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
| 1016 PyObject *ret = NULL; | 1016 PyObject *ret = NULL; |
| 1017 if (addrobj) { | 1017 if (addrobj) { |
| 1018 ret = Py_BuildValue("Oi", | 1018 ret = Py_BuildValue("Oi", |
| 1019 addrobj, | 1019 addrobj, |
| 1020 _BT_L2_MEMB(a, psm)); | 1020 _BT_L2_MEMB(a, psm)); |
| 1021 Py_DECREF(addrobj); | 1021 Py_DECREF(addrobj); |
| 1022 } | 1022 } |
| 1023 return ret; | 1023 return ret; |
| 1024 } | 1024 } |
| 1025 | 1025 |
| 1026 case BTPROTO_RFCOMM: | 1026 case BTPROTO_RFCOMM: |
| 1027 { | 1027 { |
| 1028 struct sockaddr_rc *a = (struct sockaddr_rc *) addr; | 1028 struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
| 1029 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); | 1029 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
| 1030 PyObject *ret = NULL; | 1030 PyObject *ret = NULL; |
| 1031 if (addrobj) { | 1031 if (addrobj) { |
| 1032 ret = Py_BuildValue("Oi", | 1032 ret = Py_BuildValue("Oi", |
| 1033 addrobj, | 1033 addrobj, |
| 1034 _BT_RC_MEMB(a, channel)); | 1034 _BT_RC_MEMB(a, channel)); |
| 1035 Py_DECREF(addrobj); | 1035 Py_DECREF(addrobj); |
| 1036 } | 1036 } |
| 1037 return ret; | 1037 return ret; |
| 1038 } | 1038 } |
| 1039 | 1039 |
| 1040 case BTPROTO_HCI: | 1040 case BTPROTO_HCI: |
| 1041 { | 1041 { |
| 1042 struct sockaddr_hci *a = (struct sockaddr_hci *) addr; | 1042 struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
| 1043 PyObject *ret = NULL; | 1043 PyObject *ret = NULL; |
| 1044 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); | 1044 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
| 1045 return ret; | 1045 return ret; |
| 1046 } | 1046 } |
| 1047 | 1047 |
| 1048 #if !defined(__FreeBSD__) | 1048 #if !defined(__FreeBSD__) |
| 1049 case BTPROTO_SCO: | 1049 case BTPROTO_SCO: |
| 1050 { | 1050 { |
| 1051 struct sockaddr_sco *a = (struct sockaddr_sco *) addr; | 1051 struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
| 1052 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); | 1052 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
| 1053 } | 1053 } |
| 1054 #endif | 1054 #endif |
| 1055 | 1055 |
| 1056 default: | 1056 default: |
| 1057 PyErr_SetString(PyExc_ValueError, | 1057 PyErr_SetString(PyExc_ValueError, |
| 1058 "Unknown Bluetooth protocol"); | 1058 "Unknown Bluetooth protocol"); |
| 1059 return NULL; | 1059 return NULL; |
| 1060 } | 1060 } |
| 1061 #endif | 1061 #endif |
| 1062 | 1062 |
| 1063 #ifdef HAVE_NETPACKET_PACKET_H | 1063 #ifdef HAVE_NETPACKET_PACKET_H |
| 1064 case AF_PACKET: | 1064 case AF_PACKET: |
| 1065 { | 1065 { |
| 1066 struct sockaddr_ll *a = (struct sockaddr_ll *)addr; | 1066 struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
| 1067 char *ifname = ""; | 1067 char *ifname = ""; |
| 1068 struct ifreq ifr; | 1068 struct ifreq ifr; |
| 1069 /* need to look up interface name give index */ | 1069 /* need to look up interface name give index */ |
| 1070 if (a->sll_ifindex) { | 1070 if (a->sll_ifindex) { |
| 1071 ifr.ifr_ifindex = a->sll_ifindex; | 1071 ifr.ifr_ifindex = a->sll_ifindex; |
| 1072 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) | 1072 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
| 1073 ifname = ifr.ifr_name; | 1073 ifname = ifr.ifr_name; |
| 1074 } | 1074 } |
| 1075 return Py_BuildValue("shbhy#", | 1075 return Py_BuildValue("shbhy#", |
| 1076 ifname, | 1076 ifname, |
| 1077 ntohs(a->sll_protocol), | 1077 ntohs(a->sll_protocol), |
| 1078 a->sll_pkttype, | 1078 a->sll_pkttype, |
| 1079 a->sll_hatype, | 1079 a->sll_hatype, |
| 1080 a->sll_addr, | 1080 a->sll_addr, |
| 1081 a->sll_halen); | 1081 a->sll_halen); |
| 1082 } | 1082 } |
| 1083 #endif | 1083 #endif |
| 1084 | 1084 |
| 1085 #ifdef HAVE_LINUX_TIPC_H | 1085 #ifdef HAVE_LINUX_TIPC_H |
| 1086 case AF_TIPC: | 1086 case AF_TIPC: |
| 1087 { | 1087 { |
| 1088 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; | 1088 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
| 1089 if (a->addrtype == TIPC_ADDR_NAMESEQ) { | 1089 if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
| 1090 return Py_BuildValue("IIIII", | 1090 return Py_BuildValue("IIIII", |
| 1091 a->addrtype, | 1091 a->addrtype, |
| 1092 a->addr.nameseq.type, | 1092 a->addr.nameseq.type, |
| 1093 a->addr.nameseq.lower, | 1093 a->addr.nameseq.lower, |
| 1094 a->addr.nameseq.upper, | 1094 a->addr.nameseq.upper, |
| 1095 a->scope); | 1095 a->scope); |
| 1096 } else if (a->addrtype == TIPC_ADDR_NAME) { | 1096 } else if (a->addrtype == TIPC_ADDR_NAME) { |
| 1097 return Py_BuildValue("IIIII", | 1097 return Py_BuildValue("IIIII", |
| 1098 a->addrtype, | 1098 a->addrtype, |
| 1099 a->addr.name.name.type, | 1099 a->addr.name.name.type, |
| 1100 a->addr.name.name.instance, | 1100 a->addr.name.name.instance, |
| 1101 a->addr.name.name.instance, | 1101 a->addr.name.name.instance, |
| 1102 a->scope); | 1102 a->scope); |
| 1103 } else if (a->addrtype == TIPC_ADDR_ID) { | 1103 } else if (a->addrtype == TIPC_ADDR_ID) { |
| 1104 return Py_BuildValue("IIIII", | 1104 return Py_BuildValue("IIIII", |
| 1105 a->addrtype, | 1105 a->addrtype, |
| 1106 a->addr.id.node, | 1106 a->addr.id.node, |
| 1107 a->addr.id.ref, | 1107 a->addr.id.ref, |
| 1108 0, | 1108 0, |
| 1109 a->scope); | 1109 a->scope); |
| 1110 } else { | 1110 } else { |
| 1111 PyErr_SetString(PyExc_ValueError, | 1111 PyErr_SetString(PyExc_ValueError, |
| 1112 "Invalid address type"); | 1112 "Invalid address type"); |
| 1113 return NULL; | 1113 return NULL; |
| 1114 } | 1114 } |
| 1115 } | 1115 } |
| 1116 #endif | 1116 #endif |
| 1117 | 1117 |
| 1118 /* More cases here... */ | 1118 /* More cases here... */ |
| 1119 | 1119 |
| 1120 default: | 1120 default: |
| 1121 /* If we don't know the address family, don't raise an | 1121 /* If we don't know the address family, don't raise an |
| 1122 exception -- return it as an (int, bytes) tuple. */ | 1122 exception -- return it as an (int, bytes) tuple. */ |
| 1123 return Py_BuildValue("iy#", | 1123 return Py_BuildValue("iy#", |
| 1124 addr->sa_family, | 1124 addr->sa_family, |
| 1125 addr->sa_data, | 1125 addr->sa_data, |
| 1126 sizeof(addr->sa_data)); | 1126 sizeof(addr->sa_data)); |
| 1127 | 1127 |
| 1128 } | 1128 } |
| 1129 } | 1129 } |
| 1130 | 1130 |
| 1131 | 1131 |
| 1132 /* Parse a socket address argument according to the socket object's | 1132 /* Parse a socket address argument according to the socket object's |
| 1133 address family. Return 1 if the address was in the proper format, | 1133 address family. Return 1 if the address was in the proper format, |
| 1134 0 of not. The address is returned through addr_ret, its length | 1134 0 of not. The address is returned through addr_ret, its length |
| 1135 through len_ret. */ | 1135 through len_ret. */ |
| 1136 | 1136 |
| 1137 static int | 1137 static int |
| 1138 getsockaddrarg(PySocketSockObject *s, PyObject *args, | 1138 getsockaddrarg(PySocketSockObject *s, PyObject *args, |
| 1139 struct sockaddr *addr_ret, int *len_ret) | 1139 struct sockaddr *addr_ret, int *len_ret) |
| 1140 { | 1140 { |
| 1141 switch (s->sock_family) { | 1141 switch (s->sock_family) { |
| 1142 | 1142 |
| 1143 #if defined(AF_UNIX) | 1143 #if defined(AF_UNIX) |
| 1144 case AF_UNIX: | 1144 case AF_UNIX: |
| 1145 { | 1145 { |
| 1146 struct sockaddr_un* addr; | 1146 struct sockaddr_un* addr; |
| 1147 char *path; | 1147 char *path; |
| 1148 int len; | 1148 int len; |
| 1149 if (!PyArg_Parse(args, "s#", &path, &len)) | 1149 if (!PyArg_Parse(args, "s#", &path, &len)) |
| 1150 return 0; | 1150 return 0; |
| 1151 | 1151 |
| 1152 addr = (struct sockaddr_un*)addr_ret; | 1152 addr = (struct sockaddr_un*)addr_ret; |
| 1153 #ifdef linux | 1153 #ifdef linux |
| 1154 if (len > 0 && path[0] == 0) { | 1154 if (len > 0 && path[0] == 0) { |
| 1155 /* Linux abstract namespace extension */ | 1155 /* Linux abstract namespace extension */ |
| 1156 if (len > sizeof addr->sun_path) { | 1156 if (len > sizeof addr->sun_path) { |
| 1157 PyErr_SetString(socket_error, | 1157 PyErr_SetString(socket_error, |
| 1158 "AF_UNIX path too long"); | 1158 "AF_UNIX path too long"); |
| 1159 return 0; | 1159 return 0; |
| 1160 } | 1160 } |
| 1161 } | 1161 } |
| 1162 else | 1162 else |
| 1163 #endif /* linux */ | 1163 #endif /* linux */ |
| 1164 { | 1164 { |
| 1165 /* regular NULL-terminated string */ | 1165 /* regular NULL-terminated string */ |
| 1166 if (len >= sizeof addr->sun_path) { | 1166 if (len >= sizeof addr->sun_path) { |
| 1167 PyErr_SetString(socket_error, | 1167 PyErr_SetString(socket_error, |
| 1168 "AF_UNIX path too long"); | 1168 "AF_UNIX path too long"); |
| 1169 return 0; | 1169 return 0; |
| 1170 } | 1170 } |
| 1171 addr->sun_path[len] = 0; | 1171 addr->sun_path[len] = 0; |
| 1172 } | 1172 } |
| 1173 addr->sun_family = s->sock_family; | 1173 addr->sun_family = s->sock_family; |
| 1174 memcpy(addr->sun_path, path, len); | 1174 memcpy(addr->sun_path, path, len); |
| 1175 #if defined(PYOS_OS2) | 1175 #if defined(PYOS_OS2) |
| 1176 *len_ret = sizeof(*addr); | 1176 *len_ret = sizeof(*addr); |
| 1177 #else | 1177 #else |
| 1178 *len_ret = len + offsetof(struct sockaddr_un, sun_path); | 1178 *len_ret = len + offsetof(struct sockaddr_un, sun_path); |
| 1179 #endif | 1179 #endif |
| 1180 return 1; | 1180 return 1; |
| 1181 } | 1181 } |
| 1182 #endif /* AF_UNIX */ | 1182 #endif /* AF_UNIX */ |
| 1183 | 1183 |
| 1184 #if defined(AF_NETLINK) | 1184 #if defined(AF_NETLINK) |
| 1185 case AF_NETLINK: | 1185 case AF_NETLINK: |
| 1186 { | 1186 { |
| 1187 struct sockaddr_nl* addr; | 1187 struct sockaddr_nl* addr; |
| 1188 int pid, groups; | 1188 int pid, groups; |
| 1189 addr = (struct sockaddr_nl *)addr_ret; | 1189 addr = (struct sockaddr_nl *)addr_ret; |
| 1190 if (!PyTuple_Check(args)) { | 1190 if (!PyTuple_Check(args)) { |
| 1191 PyErr_Format( | 1191 PyErr_Format( |
| 1192 PyExc_TypeError, | 1192 PyExc_TypeError, |
| 1193 "getsockaddrarg: " | 1193 "getsockaddrarg: " |
| 1194 "AF_NETLINK address must be tuple, not %.500s", | 1194 "AF_NETLINK address must be tuple, not %.500s", |
| 1195 Py_TYPE(args)->tp_name); | 1195 Py_TYPE(args)->tp_name); |
| 1196 return 0; | 1196 return 0; |
| 1197 } | 1197 } |
| 1198 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) | 1198 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
| 1199 return 0; | 1199 return 0; |
| 1200 addr->nl_family = AF_NETLINK; | 1200 addr->nl_family = AF_NETLINK; |
| 1201 addr->nl_pid = pid; | 1201 addr->nl_pid = pid; |
| 1202 addr->nl_groups = groups; | 1202 addr->nl_groups = groups; |
| 1203 *len_ret = sizeof(*addr); | 1203 *len_ret = sizeof(*addr); |
| 1204 return 1; | 1204 return 1; |
| 1205 } | 1205 } |
| 1206 #endif | 1206 #endif |
| 1207 | 1207 |
| 1208 case AF_INET: | 1208 case AF_INET: |
| 1209 { | 1209 { |
| 1210 struct sockaddr_in* addr; | 1210 struct sockaddr_in* addr; |
| 1211 char *host; | 1211 char *host; |
| 1212 int port, result; | 1212 int port, result; |
| 1213 if (!PyTuple_Check(args)) { | 1213 if (!PyTuple_Check(args)) { |
| 1214 PyErr_Format( | 1214 PyErr_Format( |
| 1215 PyExc_TypeError, | 1215 PyExc_TypeError, |
| 1216 "getsockaddrarg: " | 1216 "getsockaddrarg: " |
| 1217 "AF_INET address must be tuple, not %.500s", | 1217 "AF_INET address must be tuple, not %.500s", |
| 1218 Py_TYPE(args)->tp_name); | 1218 Py_TYPE(args)->tp_name); |
| 1219 return 0; | 1219 return 0; |
| 1220 } | 1220 } |
| 1221 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", | 1221 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg", |
| 1222 "idna", &host, &port)) | 1222 "idna", &host, &port)) |
| 1223 return 0; | 1223 return 0; |
| 1224 addr=(struct sockaddr_in*)addr_ret; | 1224 addr=(struct sockaddr_in*)addr_ret; |
| 1225 result = setipaddr(host, (struct sockaddr *)addr, | 1225 result = setipaddr(host, (struct sockaddr *)addr, |
| 1226 sizeof(*addr), AF_INET); | 1226 sizeof(*addr), AF_INET); |
| 1227 PyMem_Free(host); | 1227 PyMem_Free(host); |
| 1228 if (result < 0) | 1228 if (result < 0) |
| 1229 return 0; | 1229 return 0; |
| 1230 if (port < 0 || port > 0xffff) { | 1230 if (port < 0 || port > 0xffff) { |
| 1231 PyErr_SetString( | 1231 PyErr_SetString( |
| 1232 PyExc_OverflowError, | 1232 PyExc_OverflowError, |
| 1233 "getsockaddrarg: port must be 0-65535."); | 1233 "getsockaddrarg: port must be 0-65535."); |
| 1234 return 0; | 1234 return 0; |
| 1235 } | 1235 } |
| 1236 addr->sin_family = AF_INET; | 1236 addr->sin_family = AF_INET; |
| 1237 addr->sin_port = htons((short)port); | 1237 addr->sin_port = htons((short)port); |
| 1238 *len_ret = sizeof *addr; | 1238 *len_ret = sizeof *addr; |
| 1239 return 1; | 1239 return 1; |
| 1240 } | 1240 } |
| 1241 | 1241 |
| 1242 #ifdef ENABLE_IPV6 | 1242 #ifdef ENABLE_IPV6 |
| 1243 case AF_INET6: | 1243 case AF_INET6: |
| 1244 { | 1244 { |
| 1245 struct sockaddr_in6* addr; | 1245 struct sockaddr_in6* addr; |
| 1246 char *host; | 1246 char *host; |
| 1247 int port, flowinfo, scope_id, result; | 1247 int port, flowinfo, scope_id, result; |
| 1248 flowinfo = scope_id = 0; | 1248 flowinfo = scope_id = 0; |
| 1249 if (!PyTuple_Check(args)) { | 1249 if (!PyTuple_Check(args)) { |
| 1250 PyErr_Format( | 1250 PyErr_Format( |
| 1251 PyExc_TypeError, | 1251 PyExc_TypeError, |
| 1252 "getsockaddrarg: " | 1252 "getsockaddrarg: " |
| 1253 "AF_INET6 address must be tuple, not %.500s", | 1253 "AF_INET6 address must be tuple, not %.500s", |
| 1254 Py_TYPE(args)->tp_name); | 1254 Py_TYPE(args)->tp_name); |
| 1255 return 0; | 1255 return 0; |
| 1256 } | 1256 } |
| 1257 if (!PyArg_ParseTuple(args, "eti|ii", | 1257 if (!PyArg_ParseTuple(args, "eti|ii", |
| 1258 "idna", &host, &port, &flowinfo, | 1258 "idna", &host, &port, &flowinfo, |
| 1259 &scope_id)) { | 1259 &scope_id)) { |
| 1260 return 0; | 1260 return 0; |
| 1261 } | 1261 } |
| 1262 addr = (struct sockaddr_in6*)addr_ret; | 1262 addr = (struct sockaddr_in6*)addr_ret; |
| 1263 result = setipaddr(host, (struct sockaddr *)addr, | 1263 result = setipaddr(host, (struct sockaddr *)addr, |
| 1264 sizeof(*addr), AF_INET6); | 1264 sizeof(*addr), AF_INET6); |
| 1265 PyMem_Free(host); | 1265 PyMem_Free(host); |
| 1266 if (result < 0) | 1266 if (result < 0) |
| 1267 return 0; | 1267 return 0; |
| 1268 if (port < 0 || port > 0xffff) { | 1268 if (port < 0 || port > 0xffff) { |
| 1269 PyErr_SetString( | 1269 PyErr_SetString( |
| 1270 PyExc_OverflowError, | 1270 PyExc_OverflowError, |
| 1271 "getsockaddrarg: port must be 0-65535."); | 1271 "getsockaddrarg: port must be 0-65535."); |
| 1272 return 0; | 1272 return 0; |
| 1273 } | 1273 } |
| 1274 addr->sin6_family = s->sock_family; | 1274 addr->sin6_family = s->sock_family; |
| 1275 addr->sin6_port = htons((short)port); | 1275 addr->sin6_port = htons((short)port); |
| 1276 addr->sin6_flowinfo = flowinfo; | 1276 addr->sin6_flowinfo = flowinfo; |
| 1277 addr->sin6_scope_id = scope_id; | 1277 addr->sin6_scope_id = scope_id; |
| 1278 *len_ret = sizeof *addr; | 1278 *len_ret = sizeof *addr; |
| 1279 return 1; | 1279 return 1; |
| 1280 } | 1280 } |
| 1281 #endif | 1281 #endif |
| 1282 | 1282 |
| 1283 #ifdef USE_BLUETOOTH | 1283 #ifdef USE_BLUETOOTH |
| 1284 case AF_BLUETOOTH: | 1284 case AF_BLUETOOTH: |
| 1285 { | 1285 { |
| 1286 switch (s->sock_proto) { | 1286 switch (s->sock_proto) { |
| 1287 case BTPROTO_L2CAP: | 1287 case BTPROTO_L2CAP: |
| 1288 { | 1288 { |
| 1289 struct sockaddr_l2 *addr; | 1289 struct sockaddr_l2 *addr; |
| 1290 char *straddr; | 1290 char *straddr; |
| 1291 | 1291 |
| 1292 addr = (struct sockaddr_l2 *)addr_ret; | 1292 addr = (struct sockaddr_l2 *)addr_ret; |
| 1293 memset(addr, 0, sizeof(struct sockaddr_l2)); | 1293 memset(addr, 0, sizeof(struct sockaddr_l2)); |
| 1294 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; | 1294 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
| 1295 if (!PyArg_ParseTuple(args, "si", &straddr, | 1295 if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1296 &_BT_L2_MEMB(addr, psm))) { | 1296 &_BT_L2_MEMB(addr, psm))) { |
| 1297 PyErr_SetString(socket_error, "getsockaddrarg: " | 1297 PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1298 "wrong format"); | 1298 "wrong format"); |
| 1299 return 0; | 1299 return 0; |
| 1300 } | 1300 } |
| 1301 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) | 1301 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
| 1302 return 0; | 1302 return 0; |
| 1303 | 1303 |
| 1304 *len_ret = sizeof *addr; | 1304 *len_ret = sizeof *addr; |
| 1305 return 1; | 1305 return 1; |
| 1306 } | 1306 } |
| 1307 case BTPROTO_RFCOMM: | 1307 case BTPROTO_RFCOMM: |
| 1308 { | 1308 { |
| 1309 struct sockaddr_rc *addr; | 1309 struct sockaddr_rc *addr; |
| 1310 char *straddr; | 1310 char *straddr; |
| 1311 | 1311 |
| 1312 addr = (struct sockaddr_rc *)addr_ret; | 1312 addr = (struct sockaddr_rc *)addr_ret; |
| 1313 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; | 1313 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
| 1314 if (!PyArg_ParseTuple(args, "si", &straddr, | 1314 if (!PyArg_ParseTuple(args, "si", &straddr, |
| 1315 &_BT_RC_MEMB(addr, channel))) { | 1315 &_BT_RC_MEMB(addr, channel))) { |
| 1316 PyErr_SetString(socket_error, "getsockaddrarg: " | 1316 PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1317 "wrong format"); | 1317 "wrong format"); |
| 1318 return 0; | 1318 return 0; |
| 1319 } | 1319 } |
| 1320 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) | 1320 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
| 1321 return 0; | 1321 return 0; |
| 1322 | 1322 |
| 1323 *len_ret = sizeof *addr; | 1323 *len_ret = sizeof *addr; |
| 1324 return 1; | 1324 return 1; |
| 1325 } | 1325 } |
| 1326 case BTPROTO_HCI: | 1326 case BTPROTO_HCI: |
| 1327 { | 1327 { |
| 1328 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; | 1328 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
| 1329 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; | 1329 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
| 1330 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { | 1330 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
| 1331 PyErr_SetString(socket_error, "getsockaddrarg: " | 1331 PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1332 "wrong format"); | 1332 "wrong format"); |
| 1333 return 0; | 1333 return 0; |
| 1334 } | 1334 } |
| 1335 *len_ret = sizeof *addr; | 1335 *len_ret = sizeof *addr; |
| 1336 return 1; | 1336 return 1; |
| 1337 } | 1337 } |
| 1338 #if !defined(__FreeBSD__) | 1338 #if !defined(__FreeBSD__) |
| 1339 case BTPROTO_SCO: | 1339 case BTPROTO_SCO: |
| 1340 { | 1340 { |
| 1341 struct sockaddr_sco *addr; | 1341 struct sockaddr_sco *addr; |
| 1342 char *straddr; | 1342 char *straddr; |
| 1343 | 1343 |
| 1344 addr = (struct sockaddr_sco *)addr_ret; | 1344 addr = (struct sockaddr_sco *)addr_ret; |
| 1345 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; | 1345 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
| 1346 if (!PyBytes_Check(args)) { | 1346 if (!PyBytes_Check(args)) { |
| 1347 PyErr_SetString(socket_error, "getsockaddrarg: " | 1347 PyErr_SetString(socket_error, "getsockaddrarg: " |
| 1348 "wrong format"); | 1348 "wrong format"); |
| 1349 return 0; | 1349 return 0; |
| 1350 } | 1350 } |
| 1351 straddr = PyBytes_AS_STRING(args); | 1351 straddr = PyBytes_AS_STRING(args); |
| 1352 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) | 1352 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
| 1353 return 0; | 1353 return 0; |
| 1354 | 1354 |
| 1355 *len_ret = sizeof *addr; | 1355 *len_ret = sizeof *addr; |
| 1356 return 1; | 1356 return 1; |
| 1357 } | 1357 } |
| 1358 #endif | 1358 #endif |
| 1359 default: | 1359 default: |
| 1360 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); | 1360 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol"); |
| 1361 return 0; | 1361 return 0; |
| 1362 } | 1362 } |
| 1363 } | 1363 } |
| 1364 #endif | 1364 #endif |
| 1365 | 1365 |
| 1366 #ifdef HAVE_NETPACKET_PACKET_H | 1366 #ifdef HAVE_NETPACKET_PACKET_H |
| 1367 case AF_PACKET: | 1367 case AF_PACKET: |
| 1368 { | 1368 { |
| 1369 struct sockaddr_ll* addr; | 1369 struct sockaddr_ll* addr; |
| 1370 struct ifreq ifr; | 1370 struct ifreq ifr; |
| 1371 char *interfaceName; | 1371 char *interfaceName; |
| 1372 int protoNumber; | 1372 int protoNumber; |
| 1373 int hatype = 0; | 1373 int hatype = 0; |
| 1374 int pkttype = 0; | 1374 int pkttype = 0; |
| 1375 char *haddr = NULL; | 1375 char *haddr = NULL; |
| 1376 unsigned int halen = 0; | 1376 unsigned int halen = 0; |
| 1377 | 1377 |
| 1378 if (!PyTuple_Check(args)) { | 1378 if (!PyTuple_Check(args)) { |
| 1379 PyErr_Format( | 1379 PyErr_Format( |
| 1380 PyExc_TypeError, | 1380 PyExc_TypeError, |
| 1381 "getsockaddrarg: " | 1381 "getsockaddrarg: " |
| 1382 "AF_PACKET address must be tuple, not %.500s", | 1382 "AF_PACKET address must be tuple, not %.500s", |
| 1383 Py_TYPE(args)->tp_name); | 1383 Py_TYPE(args)->tp_name); |
| 1384 return 0; | 1384 return 0; |
| 1385 } | 1385 } |
| 1386 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, | 1386 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName, |
| 1387 &protoNumber, &pkttype, &hatype, | 1387 &protoNumber, &pkttype, &hatype, |
| 1388 &haddr, &halen)) | 1388 &haddr, &halen)) |
| 1389 return 0; | 1389 return 0; |
| 1390 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); | 1390 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
| 1391 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; | 1391 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
| 1392 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { | 1392 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
| 1393 s->errorhandler(); | 1393 s->errorhandler(); |
| 1394 return 0; | 1394 return 0; |
| 1395 } | 1395 } |
| 1396 if (halen > 8) { | 1396 if (halen > 8) { |
| 1397 PyErr_SetString(PyExc_ValueError, | 1397 PyErr_SetString(PyExc_ValueError, |
| 1398 "Hardware address must be 8 bytes or less"); | 1398 "Hardware address must be 8 bytes or less"); |
| 1399 return 0; | 1399 return 0; |
| 1400 } | 1400 } |
| 1401 if (protoNumber < 0 || protoNumber > 0xffff) { | 1401 if (protoNumber < 0 || protoNumber > 0xffff) { |
| 1402 PyErr_SetString( | 1402 PyErr_SetString( |
| 1403 PyExc_OverflowError, | 1403 PyExc_OverflowError, |
| 1404 "getsockaddrarg: protoNumber must be 0-65535."); | 1404 "getsockaddrarg: protoNumber must be 0-65535."); |
| 1405 return 0; | 1405 return 0; |
| 1406 } | 1406 } |
| 1407 addr = (struct sockaddr_ll*)addr_ret; | 1407 addr = (struct sockaddr_ll*)addr_ret; |
| 1408 addr->sll_family = AF_PACKET; | 1408 addr->sll_family = AF_PACKET; |
| 1409 addr->sll_protocol = htons((short)protoNumber); | 1409 addr->sll_protocol = htons((short)protoNumber); |
| 1410 addr->sll_ifindex = ifr.ifr_ifindex; | 1410 addr->sll_ifindex = ifr.ifr_ifindex; |
| 1411 addr->sll_pkttype = pkttype; | 1411 addr->sll_pkttype = pkttype; |
| 1412 addr->sll_hatype = hatype; | 1412 addr->sll_hatype = hatype; |
| 1413 if (halen != 0) { | 1413 if (halen != 0) { |
| 1414 memcpy(&addr->sll_addr, haddr, halen); | 1414 memcpy(&addr->sll_addr, haddr, halen); |
| 1415 } | 1415 } |
| 1416 addr->sll_halen = halen; | 1416 addr->sll_halen = halen; |
| 1417 *len_ret = sizeof *addr; | 1417 *len_ret = sizeof *addr; |
| 1418 return 1; | 1418 return 1; |
| 1419 } | 1419 } |
| 1420 #endif | 1420 #endif |
| 1421 | 1421 |
| 1422 #ifdef HAVE_LINUX_TIPC_H | 1422 #ifdef HAVE_LINUX_TIPC_H |
| 1423 case AF_TIPC: | 1423 case AF_TIPC: |
| 1424 { | 1424 { |
| 1425 unsigned int atype, v1, v2, v3; | 1425 unsigned int atype, v1, v2, v3; |
| 1426 unsigned int scope = TIPC_CLUSTER_SCOPE; | 1426 unsigned int scope = TIPC_CLUSTER_SCOPE; |
| 1427 struct sockaddr_tipc *addr; | 1427 struct sockaddr_tipc *addr; |
| 1428 | 1428 |
| 1429 if (!PyTuple_Check(args)) { | 1429 if (!PyTuple_Check(args)) { |
| 1430 PyErr_Format( | 1430 PyErr_Format( |
| 1431 PyExc_TypeError, | 1431 PyExc_TypeError, |
| 1432 "getsockaddrarg: " | 1432 "getsockaddrarg: " |
| 1433 "AF_TIPC address must be tuple, not %.500s", | 1433 "AF_TIPC address must be tuple, not %.500s", |
| 1434 Py_TYPE(args)->tp_name); | 1434 Py_TYPE(args)->tp_name); |
| 1435 return 0; | 1435 return 0; |
| 1436 } | 1436 } |
| 1437 | 1437 |
| 1438 if (!PyArg_ParseTuple(args, | 1438 if (!PyArg_ParseTuple(args, |
| 1439 "IIII|I;Invalid TIPC address format", | 1439 "IIII|I;Invalid TIPC address format", |
| 1440 &atype, &v1, &v2, &v3, &scope)) | 1440 &atype, &v1, &v2, &v3, &scope)) |
| 1441 return 0; | 1441 return 0; |
| 1442 | 1442 |
| 1443 addr = (struct sockaddr_tipc *) addr_ret; | 1443 addr = (struct sockaddr_tipc *) addr_ret; |
| 1444 memset(addr, 0, sizeof(struct sockaddr_tipc)); | 1444 memset(addr, 0, sizeof(struct sockaddr_tipc)); |
| 1445 | 1445 |
| 1446 addr->family = AF_TIPC; | 1446 addr->family = AF_TIPC; |
| 1447 addr->scope = scope; | 1447 addr->scope = scope; |
| 1448 addr->addrtype = atype; | 1448 addr->addrtype = atype; |
| 1449 | 1449 |
| 1450 if (atype == TIPC_ADDR_NAMESEQ) { | 1450 if (atype == TIPC_ADDR_NAMESEQ) { |
| 1451 addr->addr.nameseq.type = v1; | 1451 addr->addr.nameseq.type = v1; |
| 1452 addr->addr.nameseq.lower = v2; | 1452 addr->addr.nameseq.lower = v2; |
| 1453 addr->addr.nameseq.upper = v3; | 1453 addr->addr.nameseq.upper = v3; |
| 1454 } else if (atype == TIPC_ADDR_NAME) { | 1454 } else if (atype == TIPC_ADDR_NAME) { |
| 1455 addr->addr.name.name.type = v1; | 1455 addr->addr.name.name.type = v1; |
| 1456 addr->addr.name.name.instance = v2; | 1456 addr->addr.name.name.instance = v2; |
| 1457 } else if (atype == TIPC_ADDR_ID) { | 1457 } else if (atype == TIPC_ADDR_ID) { |
| 1458 addr->addr.id.node = v1; | 1458 addr->addr.id.node = v1; |
| 1459 addr->addr.id.ref = v2; | 1459 addr->addr.id.ref = v2; |
| 1460 } else { | 1460 } else { |
| 1461 /* Shouldn't happen */ | 1461 /* Shouldn't happen */ |
| 1462 PyErr_SetString(PyExc_TypeError, "Invalid address type"); | 1462 PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
| 1463 return 0; | 1463 return 0; |
| 1464 } | 1464 } |
| 1465 | 1465 |
| 1466 *len_ret = sizeof(*addr); | 1466 *len_ret = sizeof(*addr); |
| 1467 | 1467 |
| 1468 return 1; | 1468 return 1; |
| 1469 } | 1469 } |
| 1470 #endif | 1470 #endif |
| 1471 | 1471 |
| 1472 /* More cases here... */ | 1472 /* More cases here... */ |
| 1473 | 1473 |
| 1474 default: | 1474 default: |
| 1475 PyErr_SetString(socket_error, "getsockaddrarg: bad family"); | 1475 PyErr_SetString(socket_error, "getsockaddrarg: bad family"); |
| 1476 return 0; | 1476 return 0; |
| 1477 | 1477 |
| 1478 } | 1478 } |
| 1479 } | 1479 } |
| 1480 | 1480 |
| 1481 | 1481 |
| 1482 /* Get the address length according to the socket object's address family. | 1482 /* Get the address length according to the socket object's address family. |
| 1483 Return 1 if the family is known, 0 otherwise. The length is returned | 1483 Return 1 if the family is known, 0 otherwise. The length is returned |
| 1484 through len_ret. */ | 1484 through len_ret. */ |
| 1485 | 1485 |
| 1486 static int | 1486 static int |
| 1487 getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) | 1487 getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
| 1488 { | 1488 { |
| 1489 switch (s->sock_family) { | 1489 switch (s->sock_family) { |
| 1490 | 1490 |
| 1491 #if defined(AF_UNIX) | 1491 #if defined(AF_UNIX) |
| 1492 case AF_UNIX: | 1492 case AF_UNIX: |
| 1493 { | 1493 { |
| 1494 *len_ret = sizeof (struct sockaddr_un); | 1494 *len_ret = sizeof (struct sockaddr_un); |
| 1495 return 1; | 1495 return 1; |
| 1496 } | 1496 } |
| 1497 #endif /* AF_UNIX */ | 1497 #endif /* AF_UNIX */ |
| 1498 #if defined(AF_NETLINK) | 1498 #if defined(AF_NETLINK) |
| 1499 case AF_NETLINK: | 1499 case AF_NETLINK: |
| 1500 { | 1500 { |
| 1501 *len_ret = sizeof (struct sockaddr_nl); | 1501 *len_ret = sizeof (struct sockaddr_nl); |
| 1502 return 1; | 1502 return 1; |
| 1503 } | 1503 } |
| 1504 #endif | 1504 #endif |
| 1505 | 1505 |
| 1506 case AF_INET: | 1506 case AF_INET: |
| 1507 { | 1507 { |
| 1508 *len_ret = sizeof (struct sockaddr_in); | 1508 *len_ret = sizeof (struct sockaddr_in); |
| 1509 return 1; | 1509 return 1; |
| 1510 } | 1510 } |
| 1511 | 1511 |
| 1512 #ifdef ENABLE_IPV6 | 1512 #ifdef ENABLE_IPV6 |
| 1513 case AF_INET6: | 1513 case AF_INET6: |
| 1514 { | 1514 { |
| 1515 *len_ret = sizeof (struct sockaddr_in6); | 1515 *len_ret = sizeof (struct sockaddr_in6); |
| 1516 return 1; | 1516 return 1; |
| 1517 } | 1517 } |
| 1518 #endif | 1518 #endif |
| 1519 | 1519 |
| 1520 #ifdef USE_BLUETOOTH | 1520 #ifdef USE_BLUETOOTH |
| 1521 case AF_BLUETOOTH: | 1521 case AF_BLUETOOTH: |
| 1522 { | 1522 { |
| 1523 switch(s->sock_proto) | 1523 switch(s->sock_proto) |
| 1524 { | 1524 { |
| 1525 | 1525 |
| 1526 case BTPROTO_L2CAP: | 1526 case BTPROTO_L2CAP: |
| 1527 *len_ret = sizeof (struct sockaddr_l2); | 1527 *len_ret = sizeof (struct sockaddr_l2); |
| 1528 return 1; | 1528 return 1; |
| 1529 case BTPROTO_RFCOMM: | 1529 case BTPROTO_RFCOMM: |
| 1530 *len_ret = sizeof (struct sockaddr_rc); | 1530 *len_ret = sizeof (struct sockaddr_rc); |
| 1531 return 1; | 1531 return 1; |
| 1532 case BTPROTO_HCI: | 1532 case BTPROTO_HCI: |
| 1533 *len_ret = sizeof (struct sockaddr_hci); | 1533 *len_ret = sizeof (struct sockaddr_hci); |
| 1534 return 1; | 1534 return 1; |
| 1535 #if !defined(__FreeBSD__) | 1535 #if !defined(__FreeBSD__) |
| 1536 case BTPROTO_SCO: | 1536 case BTPROTO_SCO: |
| 1537 *len_ret = sizeof (struct sockaddr_sco); | 1537 *len_ret = sizeof (struct sockaddr_sco); |
| 1538 return 1; | 1538 return 1; |
| 1539 #endif | 1539 #endif |
| 1540 default: | 1540 default: |
| 1541 PyErr_SetString(socket_error, "getsockaddrlen: " | 1541 PyErr_SetString(socket_error, "getsockaddrlen: " |
| 1542 "unknown BT protocol"); | 1542 "unknown BT protocol"); |
| 1543 return 0; | 1543 return 0; |
| 1544 | 1544 |
| 1545 } | 1545 } |
| 1546 } | 1546 } |
| 1547 #endif | 1547 #endif |
| 1548 | 1548 |
| 1549 #ifdef HAVE_NETPACKET_PACKET_H | 1549 #ifdef HAVE_NETPACKET_PACKET_H |
| 1550 case AF_PACKET: | 1550 case AF_PACKET: |
| 1551 { | 1551 { |
| 1552 *len_ret = sizeof (struct sockaddr_ll); | 1552 *len_ret = sizeof (struct sockaddr_ll); |
| 1553 return 1; | 1553 return 1; |
| 1554 } | 1554 } |
| 1555 #endif | 1555 #endif |
| 1556 | 1556 |
| 1557 #ifdef HAVE_LINUX_TIPC_H | 1557 #ifdef HAVE_LINUX_TIPC_H |
| 1558 case AF_TIPC: | 1558 case AF_TIPC: |
| 1559 { | 1559 { |
| 1560 *len_ret = sizeof (struct sockaddr_tipc); | 1560 *len_ret = sizeof (struct sockaddr_tipc); |
| 1561 return 1; | 1561 return 1; |
| 1562 } | 1562 } |
| 1563 #endif | 1563 #endif |
| 1564 | 1564 |
| 1565 /* More cases here... */ | 1565 /* More cases here... */ |
| 1566 | 1566 |
| 1567 default: | 1567 default: |
| 1568 PyErr_SetString(socket_error, "getsockaddrlen: bad family"); | 1568 PyErr_SetString(socket_error, "getsockaddrlen: bad family"); |
| 1569 return 0; | 1569 return 0; |
| 1570 | 1570 |
| 1571 } | 1571 } |
| 1572 } | 1572 } |
| 1573 | 1573 |
| 1574 | 1574 |
| 1575 /* s._accept() -> (fd, address) */ | 1575 /* s._accept() -> (fd, address) */ |
| 1576 | 1576 |
| 1577 static PyObject * | 1577 static PyObject * |
| 1578 sock_accept(PySocketSockObject *s) | 1578 sock_accept(PySocketSockObject *s) |
| 1579 { | 1579 { |
| 1580 sock_addr_t addrbuf; | 1580 sock_addr_t addrbuf; |
| 1581 SOCKET_T newfd = INVALID_SOCKET; | 1581 SOCKET_T newfd = INVALID_SOCKET; |
| 1582 socklen_t addrlen; | 1582 socklen_t addrlen; |
| 1583 PyObject *sock = NULL; | 1583 PyObject *sock = NULL; |
| 1584 PyObject *addr = NULL; | 1584 PyObject *addr = NULL; |
| 1585 PyObject *res = NULL; | 1585 PyObject *res = NULL; |
| 1586 int timeout; | 1586 int timeout; |
| 1587 | 1587 |
| 1588 if (!getsockaddrlen(s, &addrlen)) | 1588 if (!getsockaddrlen(s, &addrlen)) |
| 1589 return NULL; | 1589 return NULL; |
| 1590 memset(&addrbuf, 0, addrlen); | 1590 memset(&addrbuf, 0, addrlen); |
| 1591 | 1591 |
| 1592 if (!IS_SELECTABLE(s)) | 1592 if (!IS_SELECTABLE(s)) |
| 1593 return select_error(); | 1593 return select_error(); |
| 1594 | 1594 |
| 1595 Py_BEGIN_ALLOW_THREADS | 1595 Py_BEGIN_ALLOW_THREADS |
| 1596 timeout = internal_select(s, 0); | 1596 timeout = internal_select(s, 0); |
| 1597 if (!timeout) | 1597 if (!timeout) |
| 1598 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); | 1598 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 1599 Py_END_ALLOW_THREADS | 1599 Py_END_ALLOW_THREADS |
| 1600 | 1600 |
| 1601 if (timeout == 1) { | 1601 if (timeout == 1) { |
| 1602 PyErr_SetString(socket_timeout, "timed out"); | 1602 PyErr_SetString(socket_timeout, "timed out"); |
| 1603 return NULL; | 1603 return NULL; |
| 1604 } | 1604 } |
| 1605 | 1605 |
| 1606 if (newfd == INVALID_SOCKET) | 1606 if (newfd == INVALID_SOCKET) |
| 1607 return s->errorhandler(); | 1607 return s->errorhandler(); |
| 1608 | 1608 |
| 1609 sock = PyLong_FromSocket_t(newfd); | 1609 sock = PyLong_FromSocket_t(newfd); |
| 1610 if (sock == NULL) { | 1610 if (sock == NULL) { |
| 1611 SOCKETCLOSE(newfd); | 1611 SOCKETCLOSE(newfd); |
| 1612 goto finally; | 1612 goto finally; |
| 1613 } | 1613 } |
| 1614 | 1614 |
| 1615 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), | 1615 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 1616 addrlen, s->sock_proto); | 1616 addrlen, s->sock_proto); |
| 1617 if (addr == NULL) | 1617 if (addr == NULL) |
| 1618 goto finally; | 1618 goto finally; |
| 1619 | 1619 |
| 1620 res = PyTuple_Pack(2, sock, addr); | 1620 res = PyTuple_Pack(2, sock, addr); |
| 1621 | 1621 |
| 1622 finally: | 1622 finally: |
| 1623 Py_XDECREF(sock); | 1623 Py_XDECREF(sock); |
| 1624 Py_XDECREF(addr); | 1624 Py_XDECREF(addr); |
| 1625 return res; | 1625 return res; |
| 1626 } | 1626 } |
| 1627 | 1627 |
| 1628 PyDoc_STRVAR(accept_doc, | 1628 PyDoc_STRVAR(accept_doc, |
| 1629 "_accept() -> (integer, address info)\n\ | 1629 "_accept() -> (integer, address info)\n\ |
| 1630 \n\ | 1630 \n\ |
| 1638 */ | 1638 */ |
| 1639 | 1639 |
| 1640 static PyObject * | 1640 static PyObject * |
| 1641 sock_setblocking(PySocketSockObject *s, PyObject *arg) | 1641 sock_setblocking(PySocketSockObject *s, PyObject *arg) |
| 1642 { | 1642 { |
| 1643 int block; | 1643 int block; |
| 1644 | 1644 |
| 1645 block = PyLong_AsLong(arg); | 1645 block = PyLong_AsLong(arg); |
| 1646 if (block == -1 && PyErr_Occurred()) | 1646 if (block == -1 && PyErr_Occurred()) |
| 1647 return NULL; | 1647 return NULL; |
| 1648 | 1648 |
| 1649 s->sock_timeout = block ? -1.0 : 0.0; | 1649 s->sock_timeout = block ? -1.0 : 0.0; |
| 1650 internal_setblocking(s, block); | 1650 internal_setblocking(s, block); |
| 1651 | 1651 |
| 1652 Py_INCREF(Py_None); | 1652 Py_INCREF(Py_None); |
| 1653 return Py_None; | 1653 return Py_None; |
| 1654 } | 1654 } |
| 1655 | 1655 |
| 1656 PyDoc_STRVAR(setblocking_doc, | 1656 PyDoc_STRVAR(setblocking_doc, |
| 1657 "setblocking(flag)\n\ | 1657 "setblocking(flag)\n\ |
| 1658 \n\ | 1658 \n\ |
| 1667 < 0 -- illegal; raises an exception | 1667 < 0 -- illegal; raises an exception |
| 1668 */ | 1668 */ |
| 1669 static PyObject * | 1669 static PyObject * |
| 1670 sock_settimeout(PySocketSockObject *s, PyObject *arg) | 1670 sock_settimeout(PySocketSockObject *s, PyObject *arg) |
| 1671 { | 1671 { |
| 1672 double timeout; | 1672 double timeout; |
| 1673 | 1673 |
| 1674 if (arg == Py_None) | 1674 if (arg == Py_None) |
| 1675 timeout = -1.0; | 1675 timeout = -1.0; |
| 1676 else { | 1676 else { |
| 1677 timeout = PyFloat_AsDouble(arg); | 1677 timeout = PyFloat_AsDouble(arg); |
| 1678 if (timeout < 0.0) { | 1678 if (timeout < 0.0) { |
| 1679 if (!PyErr_Occurred()) | 1679 if (!PyErr_Occurred()) |
| 1680 PyErr_SetString(PyExc_ValueError, | 1680 PyErr_SetString(PyExc_ValueError, |
| 1681 "Timeout value out of range"); | 1681 "Timeout value out of range"); |
| 1682 return NULL; | 1682 return NULL; |
| 1683 } | 1683 } |
| 1684 } | 1684 } |
| 1685 | 1685 |
| 1686 s->sock_timeout = timeout; | 1686 s->sock_timeout = timeout; |
| 1687 internal_setblocking(s, timeout < 0.0); | 1687 internal_setblocking(s, timeout < 0.0); |
| 1688 | 1688 |
| 1689 Py_INCREF(Py_None); | 1689 Py_INCREF(Py_None); |
| 1690 return Py_None; | 1690 return Py_None; |
| 1691 } | 1691 } |
| 1692 | 1692 |
| 1693 PyDoc_STRVAR(settimeout_doc, | 1693 PyDoc_STRVAR(settimeout_doc, |
| 1694 "settimeout(timeout)\n\ | 1694 "settimeout(timeout)\n\ |
| 1695 \n\ | 1695 \n\ |
| 1701 /* s.gettimeout() method. | 1701 /* s.gettimeout() method. |
| 1702 Returns the timeout associated with a socket. */ | 1702 Returns the timeout associated with a socket. */ |
| 1703 static PyObject * | 1703 static PyObject * |
| 1704 sock_gettimeout(PySocketSockObject *s) | 1704 sock_gettimeout(PySocketSockObject *s) |
| 1705 { | 1705 { |
| 1706 if (s->sock_timeout < 0.0) { | 1706 if (s->sock_timeout < 0.0) { |
| 1707 Py_INCREF(Py_None); | 1707 Py_INCREF(Py_None); |
| 1708 return Py_None; | 1708 return Py_None; |
| 1709 } | 1709 } |
| 1710 else | 1710 else |
| 1711 return PyFloat_FromDouble(s->sock_timeout); | 1711 return PyFloat_FromDouble(s->sock_timeout); |
| 1712 } | 1712 } |
| 1713 | 1713 |
| 1714 PyDoc_STRVAR(gettimeout_doc, | 1714 PyDoc_STRVAR(gettimeout_doc, |
| 1715 "gettimeout() -> timeout\n\ | 1715 "gettimeout() -> timeout\n\ |
| 1716 \n\ | 1716 \n\ |
| 1724 use optional built-in module 'struct' to encode the string. */ | 1724 use optional built-in module 'struct' to encode the string. */ |
| 1725 | 1725 |
| 1726 static PyObject * | 1726 static PyObject * |
| 1727 sock_setsockopt(PySocketSockObject *s, PyObject *args) | 1727 sock_setsockopt(PySocketSockObject *s, PyObject *args) |
| 1728 { | 1728 { |
| 1729 int level; | 1729 int level; |
| 1730 int optname; | 1730 int optname; |
| 1731 int res; | 1731 int res; |
| 1732 char *buf; | 1732 char *buf; |
| 1733 int buflen; | 1733 int buflen; |
| 1734 int flag; | 1734 int flag; |
| 1735 | 1735 |
| 1736 if (PyArg_ParseTuple(args, "iii:setsockopt", | 1736 if (PyArg_ParseTuple(args, "iii:setsockopt", |
| 1737 &level, &optname, &flag)) { | 1737 &level, &optname, &flag)) { |
| 1738 buf = (char *) &flag; | 1738 buf = (char *) &flag; |
| 1739 buflen = sizeof flag; | 1739 buflen = sizeof flag; |
| 1740 } | 1740 } |
| 1741 else { | 1741 else { |
| 1742 PyErr_Clear(); | 1742 PyErr_Clear(); |
| 1743 if (!PyArg_ParseTuple(args, "iiy#:setsockopt", | 1743 if (!PyArg_ParseTuple(args, "iiy#:setsockopt", |
| 1744 &level, &optname, &buf, &buflen)) | 1744 &level, &optname, &buf, &buflen)) |
| 1745 return NULL; | 1745 return NULL; |
| 1746 } | 1746 } |
| 1747 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); | 1747 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen); |
| 1748 if (res < 0) | 1748 if (res < 0) |
| 1749 return s->errorhandler(); | 1749 return s->errorhandler(); |
| 1750 Py_INCREF(Py_None); | 1750 Py_INCREF(Py_None); |
| 1751 return Py_None; | 1751 return Py_None; |
| 1752 } | 1752 } |
| 1753 | 1753 |
| 1754 PyDoc_STRVAR(setsockopt_doc, | 1754 PyDoc_STRVAR(setsockopt_doc, |
| 1755 "setsockopt(level, option, value)\n\ | 1755 "setsockopt(level, option, value)\n\ |
| 1756 \n\ | 1756 \n\ |
| 1764 use optional built-in module 'struct' to decode the string. */ | 1764 use optional built-in module 'struct' to decode the string. */ |
| 1765 | 1765 |
| 1766 static PyObject * | 1766 static PyObject * |
| 1767 sock_getsockopt(PySocketSockObject *s, PyObject *args) | 1767 sock_getsockopt(PySocketSockObject *s, PyObject *args) |
| 1768 { | 1768 { |
| 1769 int level; | 1769 int level; |
| 1770 int optname; | 1770 int optname; |
| 1771 int res; | 1771 int res; |
| 1772 PyObject *buf; | 1772 PyObject *buf; |
| 1773 socklen_t buflen = 0; | 1773 socklen_t buflen = 0; |
| 1774 | 1774 |
| 1775 if (!PyArg_ParseTuple(args, "ii|i:getsockopt", | 1775 if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
| 1776 &level, &optname, &buflen)) | 1776 &level, &optname, &buflen)) |
| 1777 return NULL; | 1777 return NULL; |
| 1778 | 1778 |
| 1779 if (buflen == 0) { | 1779 if (buflen == 0) { |
| 1780 int flag = 0; | 1780 int flag = 0; |
| 1781 socklen_t flagsize = sizeof flag; | 1781 socklen_t flagsize = sizeof flag; |
| 1782 res = getsockopt(s->sock_fd, level, optname, | 1782 res = getsockopt(s->sock_fd, level, optname, |
| 1783 (void *)&flag, &flagsize); | 1783 (void *)&flag, &flagsize); |
| 1784 if (res < 0) | 1784 if (res < 0) |
| 1785 return s->errorhandler(); | 1785 return s->errorhandler(); |
| 1786 return PyLong_FromLong(flag); | 1786 return PyLong_FromLong(flag); |
| 1787 } | 1787 } |
| 1788 #ifdef __VMS | 1788 #ifdef __VMS |
| 1789 /* socklen_t is unsigned so no negative test is needed, | 1789 /* socklen_t is unsigned so no negative test is needed, |
| 1790 test buflen == 0 is previously done */ | 1790 test buflen == 0 is previously done */ |
| 1791 if (buflen > 1024) { | 1791 if (buflen > 1024) { |
| 1792 #else | 1792 #else |
| 1793 if (buflen <= 0 || buflen > 1024) { | 1793 if (buflen <= 0 || buflen > 1024) { |
| 1794 #endif | 1794 #endif |
| 1795 PyErr_SetString(socket_error, | 1795 PyErr_SetString(socket_error, |
| 1796 "getsockopt buflen out of range"); | 1796 "getsockopt buflen out of range"); |
| 1797 return NULL; | 1797 return NULL; |
| 1798 } | 1798 } |
| 1799 buf = PyBytes_FromStringAndSize((char *)NULL, buflen); | 1799 buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
| 1800 if (buf == NULL) | 1800 if (buf == NULL) |
| 1801 return NULL; | 1801 return NULL; |
| 1802 res = getsockopt(s->sock_fd, level, optname, | 1802 res = getsockopt(s->sock_fd, level, optname, |
| 1803 (void *)PyBytes_AS_STRING(buf), &buflen); | 1803 (void *)PyBytes_AS_STRING(buf), &buflen); |
| 1804 if (res < 0) { | 1804 if (res < 0) { |
| 1805 Py_DECREF(buf); | 1805 Py_DECREF(buf); |
| 1806 return s->errorhandler(); | 1806 return s->errorhandler(); |
| 1807 } | 1807 } |
| 1808 _PyBytes_Resize(&buf, buflen); | 1808 _PyBytes_Resize(&buf, buflen); |
| 1809 return buf; | 1809 return buf; |
| 1810 } | 1810 } |
| 1811 | 1811 |
| 1812 PyDoc_STRVAR(getsockopt_doc, | 1812 PyDoc_STRVAR(getsockopt_doc, |
| 1813 "getsockopt(level, option[, buffersize]) -> value\n\ | 1813 "getsockopt(level, option[, buffersize]) -> value\n\ |
| 1814 \n\ | 1814 \n\ |
| 1820 /* s.bind(sockaddr) method */ | 1820 /* s.bind(sockaddr) method */ |
| 1821 | 1821 |
| 1822 static PyObject * | 1822 static PyObject * |
| 1823 sock_bind(PySocketSockObject *s, PyObject *addro) | 1823 sock_bind(PySocketSockObject *s, PyObject *addro) |
| 1824 { | 1824 { |
| 1825 sock_addr_t addrbuf; | 1825 sock_addr_t addrbuf; |
| 1826 int addrlen; | 1826 int addrlen; |
| 1827 int res; | 1827 int res; |
| 1828 | 1828 |
| 1829 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) | 1829 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1830 return NULL; | 1830 return NULL; |
| 1831 Py_BEGIN_ALLOW_THREADS | 1831 Py_BEGIN_ALLOW_THREADS |
| 1832 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); | 1832 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
| 1833 Py_END_ALLOW_THREADS | 1833 Py_END_ALLOW_THREADS |
| 1834 if (res < 0) | 1834 if (res < 0) |
| 1835 return s->errorhandler(); | 1835 return s->errorhandler(); |
| 1836 Py_INCREF(Py_None); | 1836 Py_INCREF(Py_None); |
| 1837 return Py_None; | 1837 return Py_None; |
| 1838 } | 1838 } |
| 1839 | 1839 |
| 1840 PyDoc_STRVAR(bind_doc, | 1840 PyDoc_STRVAR(bind_doc, |
| 1841 "bind(address)\n\ | 1841 "bind(address)\n\ |
| 1842 \n\ | 1842 \n\ |
| 1850 will surely fail. */ | 1850 will surely fail. */ |
| 1851 | 1851 |
| 1852 static PyObject * | 1852 static PyObject * |
| 1853 sock_close(PySocketSockObject *s) | 1853 sock_close(PySocketSockObject *s) |
| 1854 { | 1854 { |
| 1855 SOCKET_T fd; | 1855 SOCKET_T fd; |
| 1856 | 1856 |
| 1857 if ((fd = s->sock_fd) != -1) { | 1857 if ((fd = s->sock_fd) != -1) { |
| 1858 s->sock_fd = -1; | 1858 s->sock_fd = -1; |
| 1859 Py_BEGIN_ALLOW_THREADS | 1859 Py_BEGIN_ALLOW_THREADS |
| 1860 (void) SOCKETCLOSE(fd); | 1860 (void) SOCKETCLOSE(fd); |
| 1861 Py_END_ALLOW_THREADS | 1861 Py_END_ALLOW_THREADS |
| 1862 } | 1862 } |
| 1863 Py_INCREF(Py_None); | 1863 Py_INCREF(Py_None); |
| 1864 return Py_None; | 1864 return Py_None; |
| 1865 } | 1865 } |
| 1866 | 1866 |
| 1867 PyDoc_STRVAR(close_doc, | 1867 PyDoc_STRVAR(close_doc, |
| 1868 "close()\n\ | 1868 "close()\n\ |
| 1869 \n\ | 1869 \n\ |
| 1870 Close the socket. It cannot be used after this call."); | 1870 Close the socket. It cannot be used after this call."); |
| 1871 | 1871 |
| 1872 static int | 1872 static int |
| 1873 internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, | 1873 internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
| 1874 int *timeoutp) | 1874 int *timeoutp) |
| 1875 { | 1875 { |
| 1876 int res, timeout; | 1876 int res, timeout; |
| 1877 | 1877 |
| 1878 timeout = 0; | 1878 timeout = 0; |
| 1879 res = connect(s->sock_fd, addr, addrlen); | 1879 res = connect(s->sock_fd, addr, addrlen); |
| 1880 | 1880 |
| 1881 #ifdef MS_WINDOWS | 1881 #ifdef MS_WINDOWS |
| 1882 | 1882 |
| 1883 if (s->sock_timeout > 0.0) { | 1883 if (s->sock_timeout > 0.0) { |
| 1884 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && | 1884 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK && |
| 1885 IS_SELECTABLE(s)) { | 1885 IS_SELECTABLE(s)) { |
| 1886 /* This is a mess. Best solution: trust select */ | 1886 /* This is a mess. Best solution: trust select */ |
| 1887 fd_set fds; | 1887 fd_set fds; |
| 1888 fd_set fds_exc; | 1888 fd_set fds_exc; |
| 1889 struct timeval tv; | 1889 struct timeval tv; |
| 1890 tv.tv_sec = (int)s->sock_timeout; | 1890 tv.tv_sec = (int)s->sock_timeout; |
| 1891 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); | 1891 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); |
| 1892 FD_ZERO(&fds); | 1892 FD_ZERO(&fds); |
| 1893 FD_SET(s->sock_fd, &fds); | 1893 FD_SET(s->sock_fd, &fds); |
| 1894 FD_ZERO(&fds_exc); | 1894 FD_ZERO(&fds_exc); |
| 1895 FD_SET(s->sock_fd, &fds_exc); | 1895 FD_SET(s->sock_fd, &fds_exc); |
| 1896 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); | 1896 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv); |
| 1897 if (res == 0) { | 1897 if (res == 0) { |
| 1898 res = WSAEWOULDBLOCK; | 1898 res = WSAEWOULDBLOCK; |
| 1899 timeout = 1; | 1899 timeout = 1; |
| 1900 } else if (res > 0) { | 1900 } else if (res > 0) { |
| 1901 if (FD_ISSET(s->sock_fd, &fds)) | 1901 if (FD_ISSET(s->sock_fd, &fds)) |
| 1902 /* The socket is in the writable set - this | 1902 /* The socket is in the writable set - this |
| 1903 means connected */ | 1903 means connected */ |
| 1904 res = 0; | 1904 res = 0; |
| 1905 else { | 1905 else { |
| 1906 /* As per MS docs, we need to call getsockopt() | 1906 /* As per MS docs, we need to call getsockopt() |
| 1907 to get the underlying error */ | 1907 to get the underlying error */ |
| 1908 int res_size = sizeof res; | 1908 int res_size = sizeof res; |
| 1909 /* It must be in the exception set */ | 1909 /* It must be in the exception set */ |
| 1910 assert(FD_ISSET(s->sock_fd, &fds_exc)); | 1910 assert(FD_ISSET(s->sock_fd, &fds_exc)); |
| 1911 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, | 1911 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, |
| 1912 (char *)&res, &res_size)) | 1912 (char *)&res, &res_size)) |
| 1913 /* getsockopt also clears WSAGetLastError, | 1913 /* getsockopt also clears WSAGetLastError, |
| 1914 so reset it back. */ | 1914 so reset it back. */ |
| 1915 WSASetLastError(res); | 1915 WSASetLastError(res); |
| 1916 else | 1916 else |
| 1917 res = WSAGetLastError(); | 1917 res = WSAGetLastError(); |
| 1918 } | 1918 } |
| 1919 } | 1919 } |
| 1920 /* else if (res < 0) an error occurred */ | 1920 /* else if (res < 0) an error occurred */ |
| 1921 } | 1921 } |
| 1922 } | 1922 } |
| 1923 | 1923 |
| 1924 if (res < 0) | 1924 if (res < 0) |
| 1925 res = WSAGetLastError(); | 1925 res = WSAGetLastError(); |
| 1926 | 1926 |
| 1927 #else | 1927 #else |
| 1928 | 1928 |
| 1929 if (s->sock_timeout > 0.0) { | 1929 if (s->sock_timeout > 0.0) { |
| 1930 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { | 1930 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { |
| 1931 timeout = internal_select(s, 1); | 1931 timeout = internal_select(s, 1); |
| 1932 if (timeout == 0) { | 1932 if (timeout == 0) { |
| 1933 /* Bug #1019808: in case of an EINPROGRESS, | 1933 /* Bug #1019808: in case of an EINPROGRESS, |
| 1934 use getsockopt(SO_ERROR) to get the real | 1934 use getsockopt(SO_ERROR) to get the real |
| 1935 error. */ | 1935 error. */ |
| 1936 socklen_t res_size = sizeof res; | 1936 socklen_t res_size = sizeof res; |
| 1937 (void)getsockopt(s->sock_fd, SOL_SOCKET, | 1937 (void)getsockopt(s->sock_fd, SOL_SOCKET, |
| 1938 SO_ERROR, &res, &res_size); | 1938 SO_ERROR, &res, &res_size); |
| 1939 if (res == EISCONN) | 1939 if (res == EISCONN) |
| 1940 res = 0; | 1940 res = 0; |
| 1941 errno = res; | 1941 errno = res; |
| 1942 } | 1942 } |
| 1943 else if (timeout == -1) { | 1943 else if (timeout == -1) { |
| 1944 res = errno; /* had error */ | 1944 res = errno; /* had error */ |
| 1945 } | 1945 } |
| 1946 else | 1946 else |
| 1947 res = EWOULDBLOCK; /* timed out */ | 1947 res = EWOULDBLOCK; /* timed out */ |
| 1948 } | 1948 } |
| 1949 } | 1949 } |
| 1950 | 1950 |
| 1951 if (res < 0) | 1951 if (res < 0) |
| 1952 res = errno; | 1952 res = errno; |
| 1953 | 1953 |
| 1954 #endif | 1954 #endif |
| 1955 *timeoutp = timeout; | 1955 *timeoutp = timeout; |
| 1956 | 1956 |
| 1957 return res; | 1957 return res; |
| 1958 } | 1958 } |
| 1959 | 1959 |
| 1960 /* s.connect(sockaddr) method */ | 1960 /* s.connect(sockaddr) method */ |
| 1961 | 1961 |
| 1962 static PyObject * | 1962 static PyObject * |
| 1963 sock_connect(PySocketSockObject *s, PyObject *addro) | 1963 sock_connect(PySocketSockObject *s, PyObject *addro) |
| 1964 { | 1964 { |
| 1965 sock_addr_t addrbuf; | 1965 sock_addr_t addrbuf; |
| 1966 int addrlen; | 1966 int addrlen; |
| 1967 int res; | 1967 int res; |
| 1968 int timeout; | 1968 int timeout; |
| 1969 | 1969 |
| 1970 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) | 1970 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 1971 return NULL; | 1971 return NULL; |
| 1972 | 1972 |
| 1973 Py_BEGIN_ALLOW_THREADS | 1973 Py_BEGIN_ALLOW_THREADS |
| 1974 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); | 1974 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 1975 Py_END_ALLOW_THREADS | 1975 Py_END_ALLOW_THREADS |
| 1976 | 1976 |
| 1977 if (timeout == 1) { | 1977 if (timeout == 1) { |
| 1978 PyErr_SetString(socket_timeout, "timed out"); | 1978 PyErr_SetString(socket_timeout, "timed out"); |
| 1979 return NULL; | 1979 return NULL; |
| 1980 } | 1980 } |
| 1981 if (res != 0) | 1981 if (res != 0) |
| 1982 return s->errorhandler(); | 1982 return s->errorhandler(); |
| 1983 Py_INCREF(Py_None); | 1983 Py_INCREF(Py_None); |
| 1984 return Py_None; | 1984 return Py_None; |
| 1985 } | 1985 } |
| 1986 | 1986 |
| 1987 PyDoc_STRVAR(connect_doc, | 1987 PyDoc_STRVAR(connect_doc, |
| 1988 "connect(address)\n\ | 1988 "connect(address)\n\ |
| 1989 \n\ | 1989 \n\ |
| 1994 /* s.connect_ex(sockaddr) method */ | 1994 /* s.connect_ex(sockaddr) method */ |
| 1995 | 1995 |
| 1996 static PyObject * | 1996 static PyObject * |
| 1997 sock_connect_ex(PySocketSockObject *s, PyObject *addro) | 1997 sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
| 1998 { | 1998 { |
| 1999 sock_addr_t addrbuf; | 1999 sock_addr_t addrbuf; |
| 2000 int addrlen; | 2000 int addrlen; |
| 2001 int res; | 2001 int res; |
| 2002 int timeout; | 2002 int timeout; |
| 2003 | 2003 |
| 2004 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) | 2004 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
| 2005 return NULL; | 2005 return NULL; |
| 2006 | 2006 |
| 2007 Py_BEGIN_ALLOW_THREADS | 2007 Py_BEGIN_ALLOW_THREADS |
| 2008 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); | 2008 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); |
| 2009 Py_END_ALLOW_THREADS | 2009 Py_END_ALLOW_THREADS |
| 2010 | 2010 |
| 2011 /* Signals are not errors (though they may raise exceptions). Adapted | 2011 /* Signals are not errors (though they may raise exceptions). Adapted |
| 2012 from PyErr_SetFromErrnoWithFilenameObject(). */ | 2012 from PyErr_SetFromErrnoWithFilenameObject(). */ |
| 2013 #ifdef EINTR | 2013 #ifdef EINTR |
| 2014 if (res == EINTR && PyErr_CheckSignals()) | 2014 if (res == EINTR && PyErr_CheckSignals()) |
| 2015 return NULL; | 2015 return NULL; |
| 2016 #endif | 2016 #endif |
| 2017 | 2017 |
| 2018 return PyLong_FromLong((long) res); | 2018 return PyLong_FromLong((long) res); |
| 2019 } | 2019 } |
| 2020 | 2020 |
| 2021 PyDoc_STRVAR(connect_ex_doc, | 2021 PyDoc_STRVAR(connect_ex_doc, |
| 2022 "connect_ex(address) -> errno\n\ | 2022 "connect_ex(address) -> errno\n\ |
| 2023 \n\ | 2023 \n\ |
| 2028 /* s.fileno() method */ | 2028 /* s.fileno() method */ |
| 2029 | 2029 |
| 2030 static PyObject * | 2030 static PyObject * |
| 2031 sock_fileno(PySocketSockObject *s) | 2031 sock_fileno(PySocketSockObject *s) |
| 2032 { | 2032 { |
| 2033 return PyLong_FromSocket_t(s->sock_fd); | 2033 return PyLong_FromSocket_t(s->sock_fd); |
| 2034 } | 2034 } |
| 2035 | 2035 |
| 2036 PyDoc_STRVAR(fileno_doc, | 2036 PyDoc_STRVAR(fileno_doc, |
| 2037 "fileno() -> integer\n\ | 2037 "fileno() -> integer\n\ |
| 2038 \n\ | 2038 \n\ |
| 2042 /* s.getsockname() method */ | 2042 /* s.getsockname() method */ |
| 2043 | 2043 |
| 2044 static PyObject * | 2044 static PyObject * |
| 2045 sock_getsockname(PySocketSockObject *s) | 2045 sock_getsockname(PySocketSockObject *s) |
| 2046 { | 2046 { |
| 2047 sock_addr_t addrbuf; | 2047 sock_addr_t addrbuf; |
| 2048 int res; | 2048 int res; |
| 2049 socklen_t addrlen; | 2049 socklen_t addrlen; |
| 2050 | 2050 |
| 2051 if (!getsockaddrlen(s, &addrlen)) | 2051 if (!getsockaddrlen(s, &addrlen)) |
| 2052 return NULL; | 2052 return NULL; |
| 2053 memset(&addrbuf, 0, addrlen); | 2053 memset(&addrbuf, 0, addrlen); |
| 2054 Py_BEGIN_ALLOW_THREADS | 2054 Py_BEGIN_ALLOW_THREADS |
| 2055 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); | 2055 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2056 Py_END_ALLOW_THREADS | 2056 Py_END_ALLOW_THREADS |
| 2057 if (res < 0) | 2057 if (res < 0) |
| 2058 return s->errorhandler(); | 2058 return s->errorhandler(); |
| 2059 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, | 2059 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2060 s->sock_proto); | 2060 s->sock_proto); |
| 2061 } | 2061 } |
| 2062 | 2062 |
| 2063 PyDoc_STRVAR(getsockname_doc, | 2063 PyDoc_STRVAR(getsockname_doc, |
| 2064 "getsockname() -> address info\n\ | 2064 "getsockname() -> address info\n\ |
| 2065 \n\ | 2065 \n\ |
| 2066 Return the address of the local endpoint. For IP sockets, the address\n\ | 2066 Return the address of the local endpoint. For IP sockets, the address\n\ |
| 2067 info is a pair (hostaddr, port)."); | 2067 info is a pair (hostaddr, port)."); |
| 2068 | 2068 |
| 2069 | 2069 |
| 2070 #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ | 2070 #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
| 2071 /* s.getpeername() method */ | 2071 /* s.getpeername() method */ |
| 2072 | 2072 |
| 2073 static PyObject * | 2073 static PyObject * |
| 2074 sock_getpeername(PySocketSockObject *s) | 2074 sock_getpeername(PySocketSockObject *s) |
| 2075 { | 2075 { |
| 2076 sock_addr_t addrbuf; | 2076 sock_addr_t addrbuf; |
| 2077 int res; | 2077 int res; |
| 2078 socklen_t addrlen; | 2078 socklen_t addrlen; |
| 2079 | 2079 |
| 2080 if (!getsockaddrlen(s, &addrlen)) | 2080 if (!getsockaddrlen(s, &addrlen)) |
| 2081 return NULL; | 2081 return NULL; |
| 2082 memset(&addrbuf, 0, addrlen); | 2082 memset(&addrbuf, 0, addrlen); |
| 2083 Py_BEGIN_ALLOW_THREADS | 2083 Py_BEGIN_ALLOW_THREADS |
| 2084 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); | 2084 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
| 2085 Py_END_ALLOW_THREADS | 2085 Py_END_ALLOW_THREADS |
| 2086 if (res < 0) | 2086 if (res < 0) |
| 2087 return s->errorhandler(); | 2087 return s->errorhandler(); |
| 2088 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, | 2088 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
| 2089 s->sock_proto); | 2089 s->sock_proto); |
| 2090 } | 2090 } |
| 2091 | 2091 |
| 2092 PyDoc_STRVAR(getpeername_doc, | 2092 PyDoc_STRVAR(getpeername_doc, |
| 2093 "getpeername() -> address info\n\ | 2093 "getpeername() -> address info\n\ |
| 2094 \n\ | 2094 \n\ |
| 2101 /* s.listen(n) method */ | 2101 /* s.listen(n) method */ |
| 2102 | 2102 |
| 2103 static PyObject * | 2103 static PyObject * |
| 2104 sock_listen(PySocketSockObject *s, PyObject *arg) | 2104 sock_listen(PySocketSockObject *s, PyObject *arg) |
| 2105 { | 2105 { |
| 2106 int backlog; | 2106 int backlog; |
| 2107 int res; | 2107 int res; |
| 2108 | 2108 |
| 2109 backlog = PyLong_AsLong(arg); | 2109 backlog = PyLong_AsLong(arg); |
| 2110 if (backlog == -1 && PyErr_Occurred()) | 2110 if (backlog == -1 && PyErr_Occurred()) |
| 2111 return NULL; | 2111 return NULL; |
| 2112 Py_BEGIN_ALLOW_THREADS | 2112 Py_BEGIN_ALLOW_THREADS |
| 2113 if (backlog < 1) | 2113 if (backlog < 1) |
| 2114 backlog = 1; | 2114 backlog = 1; |
| 2115 res = listen(s->sock_fd, backlog); | 2115 res = listen(s->sock_fd, backlog); |
| 2116 Py_END_ALLOW_THREADS | 2116 Py_END_ALLOW_THREADS |
| 2117 if (res < 0) | 2117 if (res < 0) |
| 2118 return s->errorhandler(); | 2118 return s->errorhandler(); |
| 2119 Py_INCREF(Py_None); | 2119 Py_INCREF(Py_None); |
| 2120 return Py_None; | 2120 return Py_None; |
| 2121 } | 2121 } |
| 2122 | 2122 |
| 2123 PyDoc_STRVAR(listen_doc, | 2123 PyDoc_STRVAR(listen_doc, |
| 2124 "listen(backlog)\n\ | 2124 "listen(backlog)\n\ |
| 2125 \n\ | 2125 \n\ |
| 2137 * bytes. | 2137 * bytes. |
| 2138 */ | 2138 */ |
| 2139 static ssize_t | 2139 static ssize_t |
| 2140 sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) | 2140 sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags) |
| 2141 { | 2141 { |
| 2142 ssize_t outlen = -1; | 2142 ssize_t outlen = -1; |
| 2143 int timeout; | 2143 int timeout; |
| 2144 #ifdef __VMS | 2144 #ifdef __VMS |
| 2145 int remaining; | 2145 int remaining; |
| 2146 char *read_buf; | 2146 char *read_buf; |
| 2147 #endif | 2147 #endif |
| 2148 | 2148 |
| 2149 if (!IS_SELECTABLE(s)) { | 2149 if (!IS_SELECTABLE(s)) { |
| 2150 select_error(); | 2150 select_error(); |
| 2151 return -1; | 2151 return -1; |
| 2152 } | 2152 } |
| 2153 if (len == 0) { | 2153 if (len == 0) { |
| 2154 /* If 0 bytes were requested, do nothing. */ | 2154 /* If 0 bytes were requested, do nothing. */ |
| 2155 return 0; | 2155 return 0; |
| 2156 } | 2156 } |
| 2157 | 2157 |
| 2158 #ifndef __VMS | 2158 #ifndef __VMS |
| 2159 Py_BEGIN_ALLOW_THREADS | 2159 Py_BEGIN_ALLOW_THREADS |
| 2160 timeout = internal_select(s, 0); | 2160 timeout = internal_select(s, 0); |
| 2161 if (!timeout) | 2161 if (!timeout) |
| 2162 outlen = recv(s->sock_fd, cbuf, len, flags); | 2162 outlen = recv(s->sock_fd, cbuf, len, flags); |
| 2163 Py_END_ALLOW_THREADS | 2163 Py_END_ALLOW_THREADS |
| 2164 | 2164 |
| 2165 if (timeout == 1) { | 2165 if (timeout == 1) { |
| 2166 PyErr_SetString(socket_timeout, "timed out"); | 2166 PyErr_SetString(socket_timeout, "timed out"); |
| 2167 return -1; | 2167 return -1; |
| 2168 } | 2168 } |
| 2169 if (outlen < 0) { | 2169 if (outlen < 0) { |
| 2170 /* Note: the call to errorhandler() ALWAYS indirectly returned | 2170 /* Note: the call to errorhandler() ALWAYS indirectly returned |
| 2171 NULL, so ignore its return value */ | 2171 NULL, so ignore its return value */ |
| 2172 s->errorhandler(); | 2172 s->errorhandler(); |
| 2173 return -1; | 2173 return -1; |
| 2174 } | 2174 } |
| 2175 #else | 2175 #else |
| 2176 read_buf = cbuf; | 2176 read_buf = cbuf; |
| 2177 remaining = len; | 2177 remaining = len; |
| 2178 while (remaining != 0) { | 2178 while (remaining != 0) { |
| 2179 unsigned int segment; | 2179 unsigned int segment; |
| 2180 int nread = -1; | 2180 int nread = -1; |
| 2181 | 2181 |
| 2182 segment = remaining /SEGMENT_SIZE; | 2182 segment = remaining /SEGMENT_SIZE; |
| 2183 if (segment != 0) { | 2183 if (segment != 0) { |
| 2184 segment = SEGMENT_SIZE; | 2184 segment = SEGMENT_SIZE; |
| 2185 } | 2185 } |
| 2186 else { | 2186 else { |
| 2187 segment = remaining; | 2187 segment = remaining; |
| 2188 } | 2188 } |
| 2189 | 2189 |
| 2190 Py_BEGIN_ALLOW_THREADS | 2190 Py_BEGIN_ALLOW_THREADS |
| 2191 timeout = internal_select(s, 0); | 2191 timeout = internal_select(s, 0); |
| 2192 if (!timeout) | 2192 if (!timeout) |
| 2193 nread = recv(s->sock_fd, read_buf, segment, flags); | 2193 nread = recv(s->sock_fd, read_buf, segment, flags); |
| 2194 Py_END_ALLOW_THREADS | 2194 Py_END_ALLOW_THREADS |
| 2195 | 2195 |
| 2196 if (timeout == 1) { | 2196 if (timeout == 1) { |
| 2197 PyErr_SetString(socket_timeout, "timed out"); | 2197 PyErr_SetString(socket_timeout, "timed out"); |
| 2198 return -1; | 2198 return -1; |
| 2199 } | 2199 } |
| 2200 if (nread < 0) { | 2200 if (nread < 0) { |
| 2201 s->errorhandler(); | 2201 s->errorhandler(); |
| 2202 return -1; | 2202 return -1; |
| 2203 } | 2203 } |
| 2204 if (nread != remaining) { | 2204 if (nread != remaining) { |
| 2205 read_buf += nread; | 2205 read_buf += nread; |
| 2206 break; | 2206 break; |
| 2207 } | 2207 } |
| 2208 | 2208 |
| 2209 remaining -= segment; | 2209 remaining -= segment; |
| 2210 read_buf += segment; | 2210 read_buf += segment; |
| 2211 } | 2211 } |
| 2212 outlen = read_buf - cbuf; | 2212 outlen = read_buf - cbuf; |
| 2213 #endif /* !__VMS */ | 2213 #endif /* !__VMS */ |
| 2214 | 2214 |
| 2215 return outlen; | 2215 return outlen; |
| 2216 } | 2216 } |
| 2217 | 2217 |
| 2218 | 2218 |
| 2219 /* s.recv(nbytes [,flags]) method */ | 2219 /* s.recv(nbytes [,flags]) method */ |
| 2220 | 2220 |
| 2221 static PyObject * | 2221 static PyObject * |
| 2222 sock_recv(PySocketSockObject *s, PyObject *args) | 2222 sock_recv(PySocketSockObject *s, PyObject *args) |
| 2223 { | 2223 { |
| 2224 int recvlen, flags = 0; | 2224 int recvlen, flags = 0; |
| 2225 ssize_t outlen; | 2225 ssize_t outlen; |
| 2226 PyObject *buf; | 2226 PyObject *buf; |
| 2227 | 2227 |
| 2228 if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) | 2228 if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags)) |
| 2229 return NULL; | 2229 return NULL; |
| 2230 | 2230 |
| 2231 if (recvlen < 0) { | 2231 if (recvlen < 0) { |
| 2232 PyErr_SetString(PyExc_ValueError, | 2232 PyErr_SetString(PyExc_ValueError, |
| 2233 "negative buffersize in recv"); | 2233 "negative buffersize in recv"); |
| 2234 return NULL; | 2234 return NULL; |
| 2235 } | 2235 } |
| 2236 | 2236 |
| 2237 /* Allocate a new string. */ | 2237 /* Allocate a new string. */ |
| 2238 buf = PyBytes_FromStringAndSize((char *) 0, recvlen); | 2238 buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2239 if (buf == NULL) | 2239 if (buf == NULL) |
| 2240 return NULL; | 2240 return NULL; |
| 2241 | 2241 |
| 2242 /* Call the guts */ | 2242 /* Call the guts */ |
| 2243 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); | 2243 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
| 2244 if (outlen < 0) { | 2244 if (outlen < 0) { |
| 2245 /* An error occurred, release the string and return an | 2245 /* An error occurred, release the string and return an |
| 2246 error. */ | 2246 error. */ |
| 2247 Py_DECREF(buf); | 2247 Py_DECREF(buf); |
| 2248 return NULL; | 2248 return NULL; |
| 2249 } | 2249 } |
| 2250 if (outlen != recvlen) { | 2250 if (outlen != recvlen) { |
| 2251 /* We did not read as many bytes as we anticipated, resize the | 2251 /* We did not read as many bytes as we anticipated, resize the |
| 2252 string if possible and be successful. */ | 2252 string if possible and be successful. */ |
| 2253 _PyBytes_Resize(&buf, outlen); | 2253 _PyBytes_Resize(&buf, outlen); |
| 2254 } | 2254 } |
| 2255 | 2255 |
| 2256 return buf; | 2256 return buf; |
| 2257 } | 2257 } |
| 2258 | 2258 |
| 2259 PyDoc_STRVAR(recv_doc, | 2259 PyDoc_STRVAR(recv_doc, |
| 2260 "recv(buffersize[, flags]) -> data\n\ | 2260 "recv(buffersize[, flags]) -> data\n\ |
| 2261 \n\ | 2261 \n\ |
| 2268 /* s.recv_into(buffer, [nbytes [,flags]]) method */ | 2268 /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
| 2269 | 2269 |
| 2270 static PyObject* | 2270 static PyObject* |
| 2271 sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) | 2271 sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
| 2272 { | 2272 { |
| 2273 static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; | 2273 static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
| 2274 | 2274 |
| 2275 int recvlen = 0, flags = 0; | 2275 int recvlen = 0, flags = 0; |
| 2276 ssize_t readlen; | 2276 ssize_t readlen; |
| 2277 Py_buffer pbuf; | 2277 Py_buffer pbuf; |
| 2278 char *buf; | 2278 char *buf; |
| 2279 int buflen; | 2279 int buflen; |
| 2280 | 2280 |
| 2281 /* Get the buffer's memory */ | 2281 /* Get the buffer's memory */ |
| 2282 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist, | 2282 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist, |
| 2283 &pbuf, &recvlen, &flags)) | 2283 &pbuf, &recvlen, &flags)) |
| 2284 return NULL; | 2284 return NULL; |
| 2285 buf = pbuf.buf; | 2285 buf = pbuf.buf; |
| 2286 buflen = pbuf.len; | 2286 buflen = pbuf.len; |
| 2287 | 2287 |
| 2288 if (recvlen < 0) { | 2288 if (recvlen < 0) { |
| 2289 PyBuffer_Release(&pbuf); | 2289 PyBuffer_Release(&pbuf); |
| 2290 PyErr_SetString(PyExc_ValueError, | 2290 PyErr_SetString(PyExc_ValueError, |
| 2291 "negative buffersize in recv_into"); | 2291 "negative buffersize in recv_into"); |
| 2292 return NULL; | 2292 return NULL; |
| 2293 } | 2293 } |
| 2294 if (recvlen == 0) { | 2294 if (recvlen == 0) { |
| 2295 /* If nbytes was not specified, use the buffer's length */ | 2295 /* If nbytes was not specified, use the buffer's length */ |
| 2296 recvlen = buflen; | 2296 recvlen = buflen; |
| 2297 } | 2297 } |
| 2298 | 2298 |
| 2299 /* Check if the buffer is large enough */ | 2299 /* Check if the buffer is large enough */ |
| 2300 if (buflen < recvlen) { | 2300 if (buflen < recvlen) { |
| 2301 PyBuffer_Release(&pbuf); | 2301 PyBuffer_Release(&pbuf); |
| 2302 PyErr_SetString(PyExc_ValueError, | 2302 PyErr_SetString(PyExc_ValueError, |
| 2303 "buffer too small for requested bytes"); | 2303 "buffer too small for requested bytes"); |
| 2304 return NULL; | 2304 return NULL; |
| 2305 } | 2305 } |
| 2306 | 2306 |
| 2307 /* Call the guts */ | 2307 /* Call the guts */ |
| 2308 readlen = sock_recv_guts(s, buf, recvlen, flags); | 2308 readlen = sock_recv_guts(s, buf, recvlen, flags); |
| 2309 if (readlen < 0) { | 2309 if (readlen < 0) { |
| 2310 /* Return an error. */ | 2310 /* Return an error. */ |
| 2311 PyBuffer_Release(&pbuf); | 2311 PyBuffer_Release(&pbuf); |
| 2312 return NULL; | 2312 return NULL; |
| 2313 } | 2313 } |
| 2314 | 2314 |
| 2315 PyBuffer_Release(&pbuf); | 2315 PyBuffer_Release(&pbuf); |
| 2316 /* Return the number of bytes read. Note that we do not do anything | 2316 /* Return the number of bytes read. Note that we do not do anything |
| 2317 special here in the case that readlen < recvlen. */ | 2317 special here in the case that readlen < recvlen. */ |
| 2318 return PyLong_FromSsize_t(readlen); | 2318 return PyLong_FromSsize_t(readlen); |
| 2319 } | 2319 } |
| 2320 | 2320 |
| 2321 PyDoc_STRVAR(recv_into_doc, | 2321 PyDoc_STRVAR(recv_into_doc, |
| 2322 "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ | 2322 "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
| 2323 \n\ | 2323 \n\ |
| 2339 * 'addr' is a return value for the address object. Note that you must decref | 2339 * 'addr' is a return value for the address object. Note that you must decref |
| 2340 * it yourself. | 2340 * it yourself. |
| 2341 */ | 2341 */ |
| 2342 static ssize_t | 2342 static ssize_t |
| 2343 sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, | 2343 sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags, |
| 2344 PyObject** addr) | 2344 PyObject** addr) |
| 2345 { | 2345 { |
| 2346 sock_addr_t addrbuf; | 2346 sock_addr_t addrbuf; |
| 2347 int timeout; | 2347 int timeout; |
| 2348 ssize_t n = -1; | 2348 ssize_t n = -1; |
| 2349 socklen_t addrlen; | 2349 socklen_t addrlen; |
| 2350 | 2350 |
| 2351 *addr = NULL; | 2351 *addr = NULL; |
| 2352 | 2352 |
| 2353 if (!getsockaddrlen(s, &addrlen)) | 2353 if (!getsockaddrlen(s, &addrlen)) |
| 2354 return -1; | 2354 return -1; |
| 2355 | 2355 |
| 2356 if (!IS_SELECTABLE(s)) { | 2356 if (!IS_SELECTABLE(s)) { |
| 2357 select_error(); | 2357 select_error(); |
| 2358 return -1; | 2358 return -1; |
| 2359 } | 2359 } |
| 2360 | 2360 |
| 2361 Py_BEGIN_ALLOW_THREADS | 2361 Py_BEGIN_ALLOW_THREADS |
| 2362 memset(&addrbuf, 0, addrlen); | 2362 memset(&addrbuf, 0, addrlen); |
| 2363 timeout = internal_select(s, 0); | 2363 timeout = internal_select(s, 0); |
| 2364 if (!timeout) { | 2364 if (!timeout) { |
| 2365 #ifndef MS_WINDOWS | 2365 #ifndef MS_WINDOWS |
| 2366 #if defined(PYOS_OS2) && !defined(PYCC_GCC) | 2366 #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
| 2367 n = recvfrom(s->sock_fd, cbuf, len, flags, | 2367 n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2368 SAS2SA(&addrbuf), &addrlen); | 2368 SAS2SA(&addrbuf), &addrlen); |
| 2369 #else | 2369 #else |
| 2370 n = recvfrom(s->sock_fd, cbuf, len, flags, | 2370 n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2371 (void *) &addrbuf, &addrlen); | 2371 (void *) &addrbuf, &addrlen); |
| 2372 #endif | 2372 #endif |
| 2373 #else | 2373 #else |
| 2374 n = recvfrom(s->sock_fd, cbuf, len, flags, | 2374 n = recvfrom(s->sock_fd, cbuf, len, flags, |
| 2375 SAS2SA(&addrbuf), &addrlen); | 2375 SAS2SA(&addrbuf), &addrlen); |
| 2376 #endif | 2376 #endif |
| 2377 } | 2377 } |
| 2378 Py_END_ALLOW_THREADS | 2378 Py_END_ALLOW_THREADS |
| 2379 | 2379 |
| 2380 if (timeout == 1) { | 2380 if (timeout == 1) { |
| 2381 PyErr_SetString(socket_timeout, "timed out"); | 2381 PyErr_SetString(socket_timeout, "timed out"); |
| 2382 return -1; | 2382 return -1; |
| 2383 } | 2383 } |
| 2384 if (n < 0) { | 2384 if (n < 0) { |
| 2385 s->errorhandler(); | 2385 s->errorhandler(); |
| 2386 return -1; | 2386 return -1; |
| 2387 } | 2387 } |
| 2388 | 2388 |
| 2389 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), | 2389 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
| 2390 addrlen, s->sock_proto))) | 2390 addrlen, s->sock_proto))) |
| 2391 return -1; | 2391 return -1; |
| 2392 | 2392 |
| 2393 return n; | 2393 return n; |
| 2394 } | 2394 } |
| 2395 | 2395 |
| 2396 /* s.recvfrom(nbytes [,flags]) method */ | 2396 /* s.recvfrom(nbytes [,flags]) method */ |
| 2397 | 2397 |
| 2398 static PyObject * | 2398 static PyObject * |
| 2399 sock_recvfrom(PySocketSockObject *s, PyObject *args) | 2399 sock_recvfrom(PySocketSockObject *s, PyObject *args) |
| 2400 { | 2400 { |
| 2401 PyObject *buf = NULL; | 2401 PyObject *buf = NULL; |
| 2402 PyObject *addr = NULL; | 2402 PyObject *addr = NULL; |
| 2403 PyObject *ret = NULL; | 2403 PyObject *ret = NULL; |
| 2404 int recvlen, flags = 0; | 2404 int recvlen, flags = 0; |
| 2405 ssize_t outlen; | 2405 ssize_t outlen; |
| 2406 | 2406 |
| 2407 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) | 2407 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags)) |
| 2408 return NULL; | 2408 return NULL; |
| 2409 | 2409 |
| 2410 if (recvlen < 0) { | 2410 if (recvlen < 0) { |
| 2411 PyErr_SetString(PyExc_ValueError, | 2411 PyErr_SetString(PyExc_ValueError, |
| 2412 "negative buffersize in recvfrom"); | 2412 "negative buffersize in recvfrom"); |
| 2413 return NULL; | 2413 return NULL; |
| 2414 } | 2414 } |
| 2415 | 2415 |
| 2416 buf = PyBytes_FromStringAndSize((char *) 0, recvlen); | 2416 buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
| 2417 if (buf == NULL) | 2417 if (buf == NULL) |
| 2418 return NULL; | 2418 return NULL; |
| 2419 | 2419 |
| 2420 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), | 2420 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
| 2421 recvlen, flags, &addr); | 2421 recvlen, flags, &addr); |
| 2422 if (outlen < 0) { | 2422 if (outlen < 0) { |
| 2423 goto finally; | 2423 goto finally; |
| 2424 } | 2424 } |
| 2425 | 2425 |
| 2426 if (outlen != recvlen) { | 2426 if (outlen != recvlen) { |
| 2427 /* We did not read as many bytes as we anticipated, resize the | 2427 /* We did not read as many bytes as we anticipated, resize the |
| 2428 string if possible and be succesful. */ | 2428 string if possible and be succesful. */ |
| 2429 if (_PyBytes_Resize(&buf, outlen) < 0) | 2429 if (_PyBytes_Resize(&buf, outlen) < 0) |
| 2430 /* Oopsy, not so succesful after all. */ | 2430 /* Oopsy, not so succesful after all. */ |
| 2431 goto finally; | 2431 goto finally; |
| 2432 } | 2432 } |
| 2433 | 2433 |
| 2434 ret = PyTuple_Pack(2, buf, addr); | 2434 ret = PyTuple_Pack(2, buf, addr); |
| 2435 | 2435 |
| 2436 finally: | 2436 finally: |
| 2437 Py_XDECREF(buf); | 2437 Py_XDECREF(buf); |
| 2438 Py_XDECREF(addr); | 2438 Py_XDECREF(addr); |
| 2439 return ret; | 2439 return ret; |
| 2440 } | 2440 } |
| 2441 | 2441 |
| 2442 PyDoc_STRVAR(recvfrom_doc, | 2442 PyDoc_STRVAR(recvfrom_doc, |
| 2443 "recvfrom(buffersize[, flags]) -> (data, address info)\n\ | 2443 "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
| 2444 \n\ | 2444 \n\ |
| 2448 /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ | 2448 /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
| 2449 | 2449 |
| 2450 static PyObject * | 2450 static PyObject * |
| 2451 sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) | 2451 sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
| 2452 { | 2452 { |
| 2453 static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; | 2453 static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
| 2454 | 2454 |
| 2455 int recvlen = 0, flags = 0; | 2455 int recvlen = 0, flags = 0; |
| 2456 ssize_t readlen; | 2456 ssize_t readlen; |
| 2457 Py_buffer pbuf; | 2457 Py_buffer pbuf; |
| 2458 char *buf; | 2458 char *buf; |
| 2459 int buflen; | 2459 int buflen; |
| 2460 | 2460 |
| 2461 PyObject *addr = NULL; | 2461 PyObject *addr = NULL; |
| 2462 | 2462 |
| 2463 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into", | 2463 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into", |
| 2464 kwlist, &pbuf, | 2464 kwlist, &pbuf, |
| 2465 &recvlen, &flags)) | 2465 &recvlen, &flags)) |
| 2466 return NULL; | 2466 return NULL; |
| 2467 buf = pbuf.buf; | 2467 buf = pbuf.buf; |
| 2468 buflen = pbuf.len; | 2468 buflen = pbuf.len; |
| 2469 assert(buf != 0 && buflen > 0); | 2469 assert(buf != 0 && buflen > 0); |
| 2470 | 2470 |
| 2471 if (recvlen < 0) { | 2471 if (recvlen < 0) { |
| 2472 PyBuffer_Release(&pbuf); | 2472 PyBuffer_Release(&pbuf); |
| 2473 PyErr_SetString(PyExc_ValueError, | 2473 PyErr_SetString(PyExc_ValueError, |
| 2474 "negative buffersize in recvfrom_into"); | 2474 "negative buffersize in recvfrom_into"); |
| 2475 return NULL; | 2475 return NULL; |
| 2476 } | 2476 } |
| 2477 if (recvlen == 0) { | 2477 if (recvlen == 0) { |
| 2478 /* If nbytes was not specified, use the buffer's length */ | 2478 /* If nbytes was not specified, use the buffer's length */ |
| 2479 recvlen = buflen; | 2479 recvlen = buflen; |
| 2480 } | 2480 } |
| 2481 | 2481 |
| 2482 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); | 2482 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
| 2483 if (readlen < 0) { | 2483 if (readlen < 0) { |
| 2484 PyBuffer_Release(&pbuf); | 2484 PyBuffer_Release(&pbuf); |
| 2485 /* Return an error */ | 2485 /* Return an error */ |
| 2486 Py_XDECREF(addr); | 2486 Py_XDECREF(addr); |
| 2487 return NULL; | 2487 return NULL; |
| 2488 } | 2488 } |
| 2489 | 2489 |
| 2490 PyBuffer_Release(&pbuf); | 2490 PyBuffer_Release(&pbuf); |
| 2491 /* Return the number of bytes read and the address. Note that we do | 2491 /* Return the number of bytes read and the address. Note that we do |
| 2492 not do anything special here in the case that readlen < recvlen. */ | 2492 not do anything special here in the case that readlen < recvlen. */ |
| 2493 return Py_BuildValue("lN", readlen, addr); | 2493 return Py_BuildValue("lN", readlen, addr); |
| 2494 } | 2494 } |
| 2495 | 2495 |
| 2496 PyDoc_STRVAR(recvfrom_into_doc, | 2496 PyDoc_STRVAR(recvfrom_into_doc, |
| 2497 "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ | 2497 "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
| 2498 \n\ | 2498 \n\ |
| 2502 /* s.send(data [,flags]) method */ | 2502 /* s.send(data [,flags]) method */ |
| 2503 | 2503 |
| 2504 static PyObject * | 2504 static PyObject * |
| 2505 sock_send(PySocketSockObject *s, PyObject *args) | 2505 sock_send(PySocketSockObject *s, PyObject *args) |
| 2506 { | 2506 { |
| 2507 char *buf; | 2507 char *buf; |
| 2508 int len, n = -1, flags = 0, timeout; | 2508 int len, n = -1, flags = 0, timeout; |
| 2509 Py_buffer pbuf; | 2509 Py_buffer pbuf; |
| 2510 | 2510 |
| 2511 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) | 2511 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
| 2512 return NULL; | 2512 return NULL; |
| 2513 | 2513 |
| 2514 if (!IS_SELECTABLE(s)) { | 2514 if (!IS_SELECTABLE(s)) { |
| 2515 PyBuffer_Release(&pbuf); | 2515 PyBuffer_Release(&pbuf); |
| 2516 return select_error(); | 2516 return select_error(); |
| 2517 } | 2517 } |
| 2518 buf = pbuf.buf; | 2518 buf = pbuf.buf; |
| 2519 len = pbuf.len; | 2519 len = pbuf.len; |
| 2520 | 2520 |
| 2521 Py_BEGIN_ALLOW_THREADS | 2521 Py_BEGIN_ALLOW_THREADS |
| 2522 timeout = internal_select(s, 1); | 2522 timeout = internal_select(s, 1); |
| 2523 if (!timeout) | 2523 if (!timeout) |
| 2524 #ifdef __VMS | 2524 #ifdef __VMS |
| 2525 n = sendsegmented(s->sock_fd, buf, len, flags); | 2525 n = sendsegmented(s->sock_fd, buf, len, flags); |
| 2526 #else | 2526 #else |
| 2527 n = send(s->sock_fd, buf, len, flags); | 2527 n = send(s->sock_fd, buf, len, flags); |
| 2528 #endif | 2528 #endif |
| 2529 Py_END_ALLOW_THREADS | 2529 Py_END_ALLOW_THREADS |
| 2530 | 2530 |
| 2531 PyBuffer_Release(&pbuf); | 2531 PyBuffer_Release(&pbuf); |
| 2532 | 2532 |
| 2533 if (timeout == 1) { | 2533 if (timeout == 1) { |
| 2534 PyErr_SetString(socket_timeout, "timed out"); | 2534 PyErr_SetString(socket_timeout, "timed out"); |
| 2535 return NULL; | 2535 return NULL; |
| 2536 } | 2536 } |
| 2537 if (n < 0) | 2537 if (n < 0) |
| 2538 return s->errorhandler(); | 2538 return s->errorhandler(); |
| 2539 return PyLong_FromLong((long)n); | 2539 return PyLong_FromLong((long)n); |
| 2540 } | 2540 } |
| 2541 | 2541 |
| 2542 PyDoc_STRVAR(send_doc, | 2542 PyDoc_STRVAR(send_doc, |
| 2543 "send(data[, flags]) -> count\n\ | 2543 "send(data[, flags]) -> count\n\ |
| 2544 \n\ | 2544 \n\ |
| 2550 /* s.sendall(data [,flags]) method */ | 2550 /* s.sendall(data [,flags]) method */ |
| 2551 | 2551 |
| 2552 static PyObject * | 2552 static PyObject * |
| 2553 sock_sendall(PySocketSockObject *s, PyObject *args) | 2553 sock_sendall(PySocketSockObject *s, PyObject *args) |
| 2554 { | 2554 { |
| 2555 char *buf; | 2555 char *buf; |
| 2556 int len, n = -1, flags = 0, timeout; | 2556 int len, n = -1, flags = 0, timeout; |
| 2557 Py_buffer pbuf; | 2557 Py_buffer pbuf; |
| 2558 | 2558 |
| 2559 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) | 2559 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
| 2560 return NULL; | 2560 return NULL; |
| 2561 buf = pbuf.buf; | 2561 buf = pbuf.buf; |
| 2562 len = pbuf.len; | 2562 len = pbuf.len; |
| 2563 | 2563 |
| 2564 if (!IS_SELECTABLE(s)) { | 2564 if (!IS_SELECTABLE(s)) { |
| 2565 PyBuffer_Release(&pbuf); | 2565 PyBuffer_Release(&pbuf); |
| 2566 return select_error(); | 2566 return select_error(); |
| 2567 } | 2567 } |
| 2568 | 2568 |
| 2569 Py_BEGIN_ALLOW_THREADS | 2569 Py_BEGIN_ALLOW_THREADS |
| 2570 do { | 2570 do { |
| 2571 timeout = internal_select(s, 1); | 2571 timeout = internal_select(s, 1); |
| 2572 n = -1; | 2572 n = -1; |
| 2573 if (timeout) | 2573 if (timeout) |
| 2574 break; | 2574 break; |
| 2575 #ifdef __VMS | 2575 #ifdef __VMS |
| 2576 n = sendsegmented(s->sock_fd, buf, len, flags); | 2576 n = sendsegmented(s->sock_fd, buf, len, flags); |
| 2577 #else | 2577 #else |
| 2578 n = send(s->sock_fd, buf, len, flags); | 2578 n = send(s->sock_fd, buf, len, flags); |
| 2579 #endif | 2579 #endif |
| 2580 if (n < 0) | 2580 if (n < 0) |
| 2581 break; | 2581 break; |
| 2582 buf += n; | 2582 buf += n; |
| 2583 len -= n; | 2583 len -= n; |
| 2584 } while (len > 0); | 2584 } while (len > 0); |
| 2585 Py_END_ALLOW_THREADS | 2585 Py_END_ALLOW_THREADS |
| 2586 PyBuffer_Release(&pbuf); | 2586 PyBuffer_Release(&pbuf); |
| 2587 | 2587 |
| 2588 if (timeout == 1) { | 2588 if (timeout == 1) { |
| 2589 PyErr_SetString(socket_timeout, "timed out"); | 2589 PyErr_SetString(socket_timeout, "timed out"); |
| 2590 return NULL; | 2590 return NULL; |
| 2591 } | 2591 } |
| 2592 if (n < 0) | 2592 if (n < 0) |
| 2593 return s->errorhandler(); | 2593 return s->errorhandler(); |
| 2594 | 2594 |
| 2595 Py_INCREF(Py_None); | 2595 Py_INCREF(Py_None); |
| 2596 return Py_None; | 2596 return Py_None; |
| 2597 } | 2597 } |
| 2598 | 2598 |
| 2599 PyDoc_STRVAR(sendall_doc, | 2599 PyDoc_STRVAR(sendall_doc, |
| 2600 "sendall(data[, flags])\n\ | 2600 "sendall(data[, flags])\n\ |
| 2601 \n\ | 2601 \n\ |
| 2608 /* s.sendto(data, [flags,] sockaddr) method */ | 2608 /* s.sendto(data, [flags,] sockaddr) method */ |
| 2609 | 2609 |
| 2610 static PyObject * | 2610 static PyObject * |
| 2611 sock_sendto(PySocketSockObject *s, PyObject *args) | 2611 sock_sendto(PySocketSockObject *s, PyObject *args) |
| 2612 { | 2612 { |
| 2613 Py_buffer pbuf; | 2613 Py_buffer pbuf; |
| 2614 PyObject *addro; | 2614 PyObject *addro; |
| 2615 char *buf; | 2615 char *buf; |
| 2616 Py_ssize_t len; | 2616 Py_ssize_t len; |
| 2617 sock_addr_t addrbuf; | 2617 sock_addr_t addrbuf; |
| 2618 int addrlen, n = -1, flags, timeout; | 2618 int addrlen, n = -1, flags, timeout; |
| 2619 | 2619 |
| 2620 flags = 0; | 2620 flags = 0; |
| 2621 if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) { | 2621 if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) { |
| 2622 PyErr_Clear(); | 2622 PyErr_Clear(); |
| 2623 if (!PyArg_ParseTuple(args, "y*iO:sendto", | 2623 if (!PyArg_ParseTuple(args, "y*iO:sendto", |
| 2624 &pbuf, &flags, &addro)) | 2624 &pbuf, &flags, &addro)) |
| 2625 return NULL; | 2625 return NULL; |
| 2626 } | 2626 } |
| 2627 buf = pbuf.buf; | 2627 buf = pbuf.buf; |
| 2628 len = pbuf.len; | 2628 len = pbuf.len; |
| 2629 | 2629 |
| 2630 if (!IS_SELECTABLE(s)) { | 2630 if (!IS_SELECTABLE(s)) { |
| 2631 PyBuffer_Release(&pbuf); | 2631 PyBuffer_Release(&pbuf); |
| 2632 return select_error(); | 2632 return select_error(); |
| 2633 } | 2633 } |
| 2634 | 2634 |
| 2635 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { | 2635 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
| 2636 PyBuffer_Release(&pbuf); | 2636 PyBuffer_Release(&pbuf); |
| 2637 return NULL; | 2637 return NULL; |
| 2638 } | 2638 } |
| 2639 | 2639 |
| 2640 Py_BEGIN_ALLOW_THREADS | 2640 Py_BEGIN_ALLOW_THREADS |
| 2641 timeout = internal_select(s, 1); | 2641 timeout = internal_select(s, 1); |
| 2642 if (!timeout) | 2642 if (!timeout) |
| 2643 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); | 2643 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen); |
| 2644 Py_END_ALLOW_THREADS | 2644 Py_END_ALLOW_THREADS |
| 2645 | 2645 |
| 2646 PyBuffer_Release(&pbuf); | 2646 PyBuffer_Release(&pbuf); |
| 2647 if (timeout == 1) { | 2647 if (timeout == 1) { |
| 2648 PyErr_SetString(socket_timeout, "timed out"); | 2648 PyErr_SetString(socket_timeout, "timed out"); |
| 2649 return NULL; | 2649 return NULL; |
| 2650 } | 2650 } |
| 2651 if (n < 0) | 2651 if (n < 0) |
| 2652 return s->errorhandler(); | 2652 return s->errorhandler(); |
| 2653 return PyLong_FromLong((long)n); | 2653 return PyLong_FromLong((long)n); |
| 2654 } | 2654 } |
| 2655 | 2655 |
| 2656 PyDoc_STRVAR(sendto_doc, | 2656 PyDoc_STRVAR(sendto_doc, |
| 2657 "sendto(data[, flags], address) -> count\n\ | 2657 "sendto(data[, flags], address) -> count\n\ |
| 2658 \n\ | 2658 \n\ |
| 2663 /* s.shutdown(how) method */ | 2663 /* s.shutdown(how) method */ |
| 2664 | 2664 |
| 2665 static PyObject * | 2665 static PyObject * |
| 2666 sock_shutdown(PySocketSockObject *s, PyObject *arg) | 2666 sock_shutdown(PySocketSockObject *s, PyObject *arg) |
| 2667 { | 2667 { |
| 2668 int how; | 2668 int how; |
| 2669 int res; | 2669 int res; |
| 2670 | 2670 |
| 2671 how = PyLong_AsLong(arg); | 2671 how = PyLong_AsLong(arg); |
| 2672 if (how == -1 && PyErr_Occurred()) | 2672 if (how == -1 && PyErr_Occurred()) |
| 2673 return NULL; | 2673 return NULL; |
| 2674 Py_BEGIN_ALLOW_THREADS | 2674 Py_BEGIN_ALLOW_THREADS |
| 2675 res = shutdown(s->sock_fd, how); | 2675 res = shutdown(s->sock_fd, how); |
| 2676 Py_END_ALLOW_THREADS | 2676 Py_END_ALLOW_THREADS |
| 2677 if (res < 0) | 2677 if (res < 0) |
| 2678 return s->errorhandler(); | 2678 return s->errorhandler(); |
| 2679 Py_INCREF(Py_None); | 2679 Py_INCREF(Py_None); |
| 2680 return Py_None; | 2680 return Py_None; |
| 2681 } | 2681 } |
| 2682 | 2682 |
| 2683 PyDoc_STRVAR(shutdown_doc, | 2683 PyDoc_STRVAR(shutdown_doc, |
| 2684 "shutdown(flag)\n\ | 2684 "shutdown(flag)\n\ |
| 2685 \n\ | 2685 \n\ |
| 2688 | 2688 |
| 2689 #if defined(MS_WINDOWS) && defined(SIO_RCVALL) | 2689 #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
| 2690 static PyObject* | 2690 static PyObject* |
| 2691 sock_ioctl(PySocketSockObject *s, PyObject *arg) | 2691 sock_ioctl(PySocketSockObject *s, PyObject *arg) |
| 2692 { | 2692 { |
| 2693 unsigned long cmd = SIO_RCVALL; | 2693 unsigned long cmd = SIO_RCVALL; |
| 2694 unsigned int option = RCVALL_ON; | 2694 unsigned int option = RCVALL_ON; |
| 2695 DWORD recv; | 2695 DWORD recv; |
| 2696 | 2696 |
| 2697 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) | 2697 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
| 2698 return NULL; | 2698 return NULL; |
| 2699 | 2699 |
| 2700 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), | 2700 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
| 2701 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { | 2701 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
| 2702 return set_error(); | 2702 return set_error(); |
| 2703 } | 2703 } |
| 2704 return PyLong_FromUnsignedLong(recv); | 2704 return PyLong_FromUnsignedLong(recv); |
| 2705 } | 2705 } |
| 2706 PyDoc_STRVAR(sock_ioctl_doc, | 2706 PyDoc_STRVAR(sock_ioctl_doc, |
| 2707 "ioctl(cmd, option) -> long\n\ | 2707 "ioctl(cmd, option) -> long\n\ |
| 2708 \n\ | 2708 \n\ |
| 2709 Control the socket with WSAIoctl syscall. Currently only socket.SIO_RCVALL\n\ | 2709 Control the socket with WSAIoctl syscall. Currently only socket.SIO_RCVALL\n\ |
| 2713 #endif | 2713 #endif |
| 2714 | 2714 |
| 2715 /* List of methods for socket objects */ | 2715 /* List of methods for socket objects */ |
| 2716 | 2716 |
| 2717 static PyMethodDef sock_methods[] = { | 2717 static PyMethodDef sock_methods[] = { |
| 2718 {"_accept", (PyCFunction)sock_accept, METH_NOARGS, | 2718 {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
| 2719 accept_doc}, | 2719 accept_doc}, |
| 2720 {"bind", (PyCFunction)sock_bind, METH_O, | 2720 {"bind", (PyCFunction)sock_bind, METH_O, |
| 2721 bind_doc}, | 2721 bind_doc}, |
| 2722 {"close", (PyCFunction)sock_close, METH_NOARGS, | 2722 {"close", (PyCFunction)sock_close, METH_NOARGS, |
| 2723 close_doc}, | 2723 close_doc}, |
| 2724 {"connect", (PyCFunction)sock_connect, METH_O, | 2724 {"connect", (PyCFunction)sock_connect, METH_O, |
| 2725 connect_doc}, | 2725 connect_doc}, |
| 2726 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, | 2726 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
| 2727 connect_ex_doc}, | 2727 connect_ex_doc}, |
| 2728 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, | 2728 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
| 2729 fileno_doc}, | 2729 fileno_doc}, |
| 2730 #ifdef HAVE_GETPEERNAME | 2730 #ifdef HAVE_GETPEERNAME |
| 2731 {"getpeername", (PyCFunction)sock_getpeername, | 2731 {"getpeername", (PyCFunction)sock_getpeername, |
| 2732 METH_NOARGS, getpeername_doc}, | 2732 METH_NOARGS, getpeername_doc}, |
| 2733 #endif | 2733 #endif |
| 2734 {"getsockname", (PyCFunction)sock_getsockname, | 2734 {"getsockname", (PyCFunction)sock_getsockname, |
| 2735 METH_NOARGS, getsockname_doc}, | 2735 METH_NOARGS, getsockname_doc}, |
| 2736 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, | 2736 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
| 2737 getsockopt_doc}, | 2737 getsockopt_doc}, |
| 2738 #if defined(MS_WINDOWS) && defined(SIO_RCVALL) | 2738 #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
| 2739 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, | 2739 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
| 2740 sock_ioctl_doc}, | 2740 sock_ioctl_doc}, |
| 2741 #endif | 2741 #endif |
| 2742 {"listen", (PyCFunction)sock_listen, METH_O, | 2742 {"listen", (PyCFunction)sock_listen, METH_O, |
| 2743 listen_doc}, | 2743 listen_doc}, |
| 2744 {"recv", (PyCFunction)sock_recv, METH_VARARGS, | 2744 {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
| 2745 recv_doc}, | 2745 recv_doc}, |
| 2746 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, | 2746 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
| 2747 recv_into_doc}, | 2747 recv_into_doc}, |
| 2748 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, | 2748 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
| 2749 recvfrom_doc}, | 2749 recvfrom_doc}, |
| 2750 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, | 2750 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
| 2751 recvfrom_into_doc}, | 2751 recvfrom_into_doc}, |
| 2752 {"send", (PyCFunction)sock_send, METH_VARARGS, | 2752 {"send", (PyCFunction)sock_send, METH_VARARGS, |
| 2753 send_doc}, | 2753 send_doc}, |
| 2754 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, | 2754 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
| 2755 sendall_doc}, | 2755 sendall_doc}, |
| 2756 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, | 2756 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
| 2757 sendto_doc}, | 2757 sendto_doc}, |
| 2758 {"setblocking", (PyCFunction)sock_setblocking, METH_O, | 2758 {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
| 2759 setblocking_doc}, | 2759 setblocking_doc}, |
| 2760 {"settimeout", (PyCFunction)sock_settimeout, METH_O, | 2760 {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
| 2761 settimeout_doc}, | 2761 settimeout_doc}, |
| 2762 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, | 2762 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
| 2763 gettimeout_doc}, | 2763 gettimeout_doc}, |
| 2764 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, | 2764 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
| 2765 setsockopt_doc}, | 2765 setsockopt_doc}, |
| 2766 {"shutdown", (PyCFunction)sock_shutdown, METH_O, | 2766 {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
| 2767 shutdown_doc}, | 2767 shutdown_doc}, |
| 2768 {NULL, NULL} /* sentinel */ | 2768 {NULL, NULL} /* sentinel */ |
| 2769 }; | 2769 }; |
| 2770 | 2770 |
| 2771 /* SockObject members */ | 2771 /* SockObject members */ |
| 2772 static PyMemberDef sock_memberlist[] = { | 2772 static PyMemberDef sock_memberlist[] = { |
| 2773 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, | 2773 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
| 2781 First close the file description. */ | 2781 First close the file description. */ |
| 2782 | 2782 |
| 2783 static void | 2783 static void |
| 2784 sock_dealloc(PySocketSockObject *s) | 2784 sock_dealloc(PySocketSockObject *s) |
| 2785 { | 2785 { |
| 2786 if (s->sock_fd != -1) | 2786 if (s->sock_fd != -1) |
| 2787 (void) SOCKETCLOSE(s->sock_fd); | 2787 (void) SOCKETCLOSE(s->sock_fd); |
| 2788 Py_TYPE(s)->tp_free((PyObject *)s); | 2788 Py_TYPE(s)->tp_free((PyObject *)s); |
| 2789 } | 2789 } |
| 2790 | 2790 |
| 2791 | 2791 |
| 2792 static PyObject * | 2792 static PyObject * |
| 2793 sock_repr(PySocketSockObject *s) | 2793 sock_repr(PySocketSockObject *s) |
| 2794 { | 2794 { |
| 2795 #if SIZEOF_SOCKET_T > SIZEOF_LONG | 2795 #if SIZEOF_SOCKET_T > SIZEOF_LONG |
| 2796 if (s->sock_fd > LONG_MAX) { | 2796 if (s->sock_fd > LONG_MAX) { |
| 2797 /* this can occur on Win64, and actually there is a special | 2797 /* this can occur on Win64, and actually there is a special |
| 2798 ugly printf formatter for decimal pointer length integer | 2798 ugly printf formatter for decimal pointer length integer |
| 2799 printing, only bother if necessary*/ | 2799 printing, only bother if necessary*/ |
| 2800 PyErr_SetString(PyExc_OverflowError, | 2800 PyErr_SetString(PyExc_OverflowError, |
| 2801 "no printf formatter to display " | 2801 "no printf formatter to display " |
| 2802 "the socket descriptor in decimal"); | 2802 "the socket descriptor in decimal"); |
| 2803 return NULL; | 2803 return NULL; |
| 2804 } | 2804 } |
| 2805 #endif | 2805 #endif |
| 2806 return PyUnicode_FromFormat( | 2806 return PyUnicode_FromFormat( |
| 2807 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", | 2807 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
| 2808 (long)s->sock_fd, s->sock_family, | 2808 (long)s->sock_fd, s->sock_family, |
| 2809 s->sock_type, | 2809 s->sock_type, |
| 2810 s->sock_proto); | 2810 s->sock_proto); |
| 2811 } | 2811 } |
| 2812 | 2812 |
| 2813 | 2813 |
| 2814 /* Create a new, uninitialized socket object. */ | 2814 /* Create a new, uninitialized socket object. */ |
| 2815 | 2815 |
| 2816 static PyObject * | 2816 static PyObject * |
| 2817 sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 2817 sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2818 { | 2818 { |
| 2819 PyObject *new; | 2819 PyObject *new; |
| 2820 | 2820 |
| 2821 new = type->tp_alloc(type, 0); | 2821 new = type->tp_alloc(type, 0); |
| 2822 if (new != NULL) { | 2822 if (new != NULL) { |
| 2823 ((PySocketSockObject *)new)->sock_fd = -1; | 2823 ((PySocketSockObject *)new)->sock_fd = -1; |
| 2824 ((PySocketSockObject *)new)->sock_timeout = -1.0; | 2824 ((PySocketSockObject *)new)->sock_timeout = -1.0; |
| 2825 ((PySocketSockObject *)new)->errorhandler = &set_error; | 2825 ((PySocketSockObject *)new)->errorhandler = &set_error; |
| 2826 } | 2826 } |
| 2827 return new; | 2827 return new; |
| 2828 } | 2828 } |
| 2829 | 2829 |
| 2830 | 2830 |
| 2831 /* Initialize a new socket object. */ | 2831 /* Initialize a new socket object. */ |
| 2832 | 2832 |
| 2833 /*ARGSUSED*/ | 2833 /*ARGSUSED*/ |
| 2834 static int | 2834 static int |
| 2835 sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) | 2835 sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
| 2836 { | 2836 { |
| 2837 PySocketSockObject *s = (PySocketSockObject *)self; | 2837 PySocketSockObject *s = (PySocketSockObject *)self; |
| 2838 PyObject *fdobj = NULL; | 2838 PyObject *fdobj = NULL; |
| 2839 SOCKET_T fd = INVALID_SOCKET; | 2839 SOCKET_T fd = INVALID_SOCKET; |
| 2840 int family = AF_INET, type = SOCK_STREAM, proto = 0; | 2840 int family = AF_INET, type = SOCK_STREAM, proto = 0; |
| 2841 static char *keywords[] = {"family", "type", "proto", "fileno", 0}; | 2841 static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
| 2842 | 2842 |
| 2843 if (!PyArg_ParseTupleAndKeywords(args, kwds, | 2843 if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2844 "|iiiO:socket", keywords, | 2844 "|iiiO:socket", keywords, |
| 2845 &family, &type, &proto, &fdobj)) | 2845 &family, &type, &proto, &fdobj)) |
| 2846 return -1; | 2846 return -1; |
| 2847 | 2847 |
| 2848 if (fdobj != NULL && fdobj != Py_None) { | 2848 if (fdobj != NULL && fdobj != Py_None) { |
| 2849 fd = PyLong_AsSocket_t(fdobj); | 2849 fd = PyLong_AsSocket_t(fdobj); |
| 2850 if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) | 2850 if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 2851 return -1; | 2851 return -1; |
| 2852 if (fd == INVALID_SOCKET) { | 2852 if (fd == INVALID_SOCKET) { |
| 2853 PyErr_SetString(PyExc_ValueError, | 2853 PyErr_SetString(PyExc_ValueError, |
| 2854 "can't use invalid socket value"); | 2854 "can't use invalid socket value"); |
| 2855 return -1; | 2855 return -1; |
| 2856 } | 2856 } |
| 2857 } | 2857 } |
| 2858 else { | 2858 else { |
| 2859 Py_BEGIN_ALLOW_THREADS | 2859 Py_BEGIN_ALLOW_THREADS |
| 2860 fd = socket(family, type, proto); | 2860 fd = socket(family, type, proto); |
| 2861 Py_END_ALLOW_THREADS | 2861 Py_END_ALLOW_THREADS |
| 2862 | 2862 |
| 2863 if (fd == INVALID_SOCKET) { | 2863 if (fd == INVALID_SOCKET) { |
| 2864 set_error(); | 2864 set_error(); |
| 2865 return -1; | 2865 return -1; |
| 2866 } | 2866 } |
| 2867 } | 2867 } |
| 2868 init_sockobject(s, fd, family, type, proto); | 2868 init_sockobject(s, fd, family, type, proto); |
| 2869 | 2869 |
| 2870 return 0; | 2870 return 0; |
| 2871 | 2871 |
| 2872 } | 2872 } |
| 2873 | 2873 |
| 2874 | 2874 |
| 2875 /* Type object for socket objects. */ | 2875 /* Type object for socket objects. */ |
| 2876 | 2876 |
| 2877 static PyTypeObject sock_type = { | 2877 static PyTypeObject sock_type = { |
| 2878 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ | 2878 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
| 2879 "_socket.socket", /* tp_name */ | 2879 "_socket.socket", /* tp_name */ |
| 2880 sizeof(PySocketSockObject), /* tp_basicsize */ | 2880 sizeof(PySocketSockObject), /* tp_basicsize */ |
| 2881 0, /* tp_itemsize */ | 2881 0, /* tp_itemsize */ |
| 2882 (destructor)sock_dealloc, /* tp_dealloc */ | 2882 (destructor)sock_dealloc, /* tp_dealloc */ |
| 2883 0, /* tp_print */ | 2883 0, /* tp_print */ |
| 2884 0, /* tp_getattr */ | 2884 0, /* tp_getattr */ |
| 2885 0, /* tp_setattr */ | 2885 0, /* tp_setattr */ |
| 2886 0, /* tp_reserved */ | 2886 0, /* tp_reserved */ |
| 2887 (reprfunc)sock_repr, /* tp_repr */ | 2887 (reprfunc)sock_repr, /* tp_repr */ |
| 2888 0, /* tp_as_number */ | 2888 0, /* tp_as_number */ |
| 2889 0, /* tp_as_sequence */ | 2889 0, /* tp_as_sequence */ |
| 2890 0, /* tp_as_mapping */ | 2890 0, /* tp_as_mapping */ |
| 2891 0, /* tp_hash */ | 2891 0, /* tp_hash */ |
| 2892 0, /* tp_call */ | 2892 0, /* tp_call */ |
| 2893 0, /* tp_str */ | 2893 0, /* tp_str */ |
| 2894 PyObject_GenericGetAttr, /* tp_getattro */ | 2894 PyObject_GenericGetAttr, /* tp_getattro */ |
| 2895 0, /* tp_setattro */ | 2895 0, /* tp_setattro */ |
| 2896 0, /* tp_as_buffer */ | 2896 0, /* tp_as_buffer */ |
| 2897 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | 2897 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2898 sock_doc, /* tp_doc */ | 2898 sock_doc, /* tp_doc */ |
| 2899 0, /* tp_traverse */ | 2899 0, /* tp_traverse */ |
| 2900 0, /* tp_clear */ | 2900 0, /* tp_clear */ |
| 2901 0, /* tp_richcompare */ | 2901 0, /* tp_richcompare */ |
| 2902 0, /* tp_weaklistoffset */ | 2902 0, /* tp_weaklistoffset */ |
| 2903 0, /* tp_iter */ | 2903 0, /* tp_iter */ |
| 2904 0, /* tp_iternext */ | 2904 0, /* tp_iternext */ |
| 2905 sock_methods, /* tp_methods */ | 2905 sock_methods, /* tp_methods */ |
| 2906 sock_memberlist, /* tp_members */ | 2906 sock_memberlist, /* tp_members */ |
| 2907 0, /* tp_getset */ | 2907 0, /* tp_getset */ |
| 2908 0, /* tp_base */ | 2908 0, /* tp_base */ |
| 2909 0, /* tp_dict */ | 2909 0, /* tp_dict */ |
| 2910 0, /* tp_descr_get */ | 2910 0, /* tp_descr_get */ |
| 2911 0, /* tp_descr_set */ | 2911 0, /* tp_descr_set */ |
| 2912 0, /* tp_dictoffset */ | 2912 0, /* tp_dictoffset */ |
| 2913 sock_initobj, /* tp_init */ | 2913 sock_initobj, /* tp_init */ |
| 2914 PyType_GenericAlloc, /* tp_alloc */ | 2914 PyType_GenericAlloc, /* tp_alloc */ |
| 2915 sock_new, /* tp_new */ | 2915 sock_new, /* tp_new */ |
| 2916 PyObject_Del, /* tp_free */ | 2916 PyObject_Del, /* tp_free */ |
| 2917 }; | 2917 }; |
| 2918 | 2918 |
| 2919 | 2919 |
| 2920 /* Python interface to gethostname(). */ | 2920 /* Python interface to gethostname(). */ |
| 2921 | 2921 |
| 2922 /*ARGSUSED*/ | 2922 /*ARGSUSED*/ |
| 2923 static PyObject * | 2923 static PyObject * |
| 2924 socket_gethostname(PyObject *self, PyObject *unused) | 2924 socket_gethostname(PyObject *self, PyObject *unused) |
| 2925 { | 2925 { |
| 2926 char buf[1024]; | 2926 char buf[1024]; |
| 2927 int res; | 2927 int res; |
| 2928 Py_BEGIN_ALLOW_THREADS | 2928 Py_BEGIN_ALLOW_THREADS |
| 2929 res = gethostname(buf, (int) sizeof buf - 1); | 2929 res = gethostname(buf, (int) sizeof buf - 1); |
| 2930 Py_END_ALLOW_THREADS | 2930 Py_END_ALLOW_THREADS |
| 2931 if (res < 0) | 2931 if (res < 0) |
| 2932 return set_error(); | 2932 return set_error(); |
| 2933 buf[sizeof buf - 1] = '\0'; | 2933 buf[sizeof buf - 1] = '\0'; |
| 2934 return PyUnicode_FromString(buf); | 2934 return PyUnicode_FromString(buf); |
| 2935 } | 2935 } |
| 2936 | 2936 |
| 2937 PyDoc_STRVAR(gethostname_doc, | 2937 PyDoc_STRVAR(gethostname_doc, |
| 2938 "gethostname() -> string\n\ | 2938 "gethostname() -> string\n\ |
| 2939 \n\ | 2939 \n\ |
| 2944 | 2944 |
| 2945 /*ARGSUSED*/ | 2945 /*ARGSUSED*/ |
| 2946 static PyObject * | 2946 static PyObject * |
| 2947 socket_gethostbyname(PyObject *self, PyObject *args) | 2947 socket_gethostbyname(PyObject *self, PyObject *args) |
| 2948 { | 2948 { |
| 2949 char *name; | 2949 char *name; |
| 2950 sock_addr_t addrbuf; | 2950 sock_addr_t addrbuf; |
| 2951 | 2951 |
| 2952 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) | 2952 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name)) |
| 2953 return NULL; | 2953 return NULL; |
| 2954 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) | 2954 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
| 2955 return NULL; | 2955 return NULL; |
| 2956 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); | 2956 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
| 2957 } | 2957 } |
| 2958 | 2958 |
| 2959 PyDoc_STRVAR(gethostbyname_doc, | 2959 PyDoc_STRVAR(gethostbyname_doc, |
| 2960 "gethostbyname(host) -> address\n\ | 2960 "gethostbyname(host) -> address\n\ |
| 2961 \n\ | 2961 \n\ |
| 2965 /* Convenience function common to gethostbyname_ex and gethostbyaddr */ | 2965 /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
| 2966 | 2966 |
| 2967 static PyObject * | 2967 static PyObject * |
| 2968 gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af) | 2968 gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af) |
| 2969 { | 2969 { |
| 2970 char **pch; | 2970 char **pch; |
| 2971 PyObject *rtn_tuple = (PyObject *)NULL; | 2971 PyObject *rtn_tuple = (PyObject *)NULL; |
| 2972 PyObject *name_list = (PyObject *)NULL; | 2972 PyObject *name_list = (PyObject *)NULL; |
| 2973 PyObject *addr_list = (PyObject *)NULL; | 2973 PyObject *addr_list = (PyObject *)NULL; |
| 2974 PyObject *tmp; | 2974 PyObject *tmp; |
| 2975 | 2975 |
| 2976 if (h == NULL) { | 2976 if (h == NULL) { |
| 2977 /* Let's get real error message to return */ | 2977 /* Let's get real error message to return */ |
| 2978 set_herror(h_errno); | 2978 set_herror(h_errno); |
| 2979 return NULL; | 2979 return NULL; |
| 2980 } | 2980 } |
| 2981 | 2981 |
| 2982 if (h->h_addrtype != af) { | 2982 if (h->h_addrtype != af) { |
| 2983 /* Let's get real error message to return */ | 2983 /* Let's get real error message to return */ |
| 2984 PyErr_SetString(socket_error, | 2984 PyErr_SetString(socket_error, |
| 2985 (char *)strerror(EAFNOSUPPORT)); | 2985 (char *)strerror(EAFNOSUPPORT)); |
| 2986 | 2986 |
| 2987 return NULL; | 2987 return NULL; |
| 2988 } | 2988 } |
| 2989 | 2989 |
| 2990 switch (af) { | 2990 switch (af) { |
| 2991 | 2991 |
| 2992 case AF_INET: | 2992 case AF_INET: |
| 2993 if (alen < sizeof(struct sockaddr_in)) | 2993 if (alen < sizeof(struct sockaddr_in)) |
| 2994 return NULL; | 2994 return NULL; |
| 2995 break; | 2995 break; |
| 2996 | 2996 |
| 2997 #ifdef ENABLE_IPV6 | 2997 #ifdef ENABLE_IPV6 |
| 2998 case AF_INET6: | 2998 case AF_INET6: |
| 2999 if (alen < sizeof(struct sockaddr_in6)) | 2999 if (alen < sizeof(struct sockaddr_in6)) |
| 3000 return NULL; | 3000 return NULL; |
| 3001 break; | 3001 break; |
| 3002 #endif | 3002 #endif |
| 3003 | 3003 |
| 3004 } | 3004 } |
| 3005 | 3005 |
| 3006 if ((name_list = PyList_New(0)) == NULL) | 3006 if ((name_list = PyList_New(0)) == NULL) |
| 3007 goto err; | 3007 goto err; |
| 3008 | 3008 |
| 3009 if ((addr_list = PyList_New(0)) == NULL) | 3009 if ((addr_list = PyList_New(0)) == NULL) |
| 3010 goto err; | 3010 goto err; |
| 3011 | 3011 |
| 3012 /* SF #1511317: h_aliases can be NULL */ | 3012 /* SF #1511317: h_aliases can be NULL */ |
| 3013 if (h->h_aliases) { | 3013 if (h->h_aliases) { |
| 3014 for (pch = h->h_aliases; *pch != NULL; pch++) { | 3014 for (pch = h->h_aliases; *pch != NULL; pch++) { |
| 3015 int status; | 3015 int status; |
| 3016 tmp = PyUnicode_FromString(*pch); | 3016 tmp = PyUnicode_FromString(*pch); |
| 3017 if (tmp == NULL) | 3017 if (tmp == NULL) |
| 3018 goto err; | 3018 goto err; |
| 3019 | 3019 |
| 3020 status = PyList_Append(name_list, tmp); | 3020 status = PyList_Append(name_list, tmp); |
| 3021 Py_DECREF(tmp); | 3021 Py_DECREF(tmp); |
| 3022 | 3022 |
| 3023 if (status) | 3023 if (status) |
| 3024 goto err; | 3024 goto err; |
| 3025 } | 3025 } |
| 3026 } | 3026 } |
| 3027 | 3027 |
| 3028 for (pch = h->h_addr_list; *pch != NULL; pch++) { | 3028 for (pch = h->h_addr_list; *pch != NULL; pch++) { |
| 3029 int status; | 3029 int status; |
| 3030 | 3030 |
| 3031 switch (af) { | 3031 switch (af) { |
| 3032 | 3032 |
| 3033 case AF_INET: | 3033 case AF_INET: |
| 3034 { | 3034 { |
| 3035 struct sockaddr_in sin; | 3035 struct sockaddr_in sin; |
| 3036 memset(&sin, 0, sizeof(sin)); | 3036 memset(&sin, 0, sizeof(sin)); |
| 3037 sin.sin_family = af; | 3037 sin.sin_family = af; |
| 3038 #ifdef HAVE_SOCKADDR_SA_LEN | 3038 #ifdef HAVE_SOCKADDR_SA_LEN |
| 3039 sin.sin_len = sizeof(sin); | 3039 sin.sin_len = sizeof(sin); |
| 3040 #endif | 3040 #endif |
| 3041 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); | 3041 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
| 3042 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); | 3042 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
| 3043 | 3043 |
| 3044 if (pch == h->h_addr_list && alen >= sizeof(sin)) | 3044 if (pch == h->h_addr_list && alen >= sizeof(sin)) |
| 3045 memcpy((char *) addr, &sin, sizeof(sin)); | 3045 memcpy((char *) addr, &sin, sizeof(sin)); |
| 3046 break; | 3046 break; |
| 3047 } | 3047 } |
| 3048 | 3048 |
| 3049 #ifdef ENABLE_IPV6 | 3049 #ifdef ENABLE_IPV6 |
| 3050 case AF_INET6: | 3050 case AF_INET6: |
| 3051 { | 3051 { |
| 3052 struct sockaddr_in6 sin6; | 3052 struct sockaddr_in6 sin6; |
| 3053 memset(&sin6, 0, sizeof(sin6)); | 3053 memset(&sin6, 0, sizeof(sin6)); |
| 3054 sin6.sin6_family = af; | 3054 sin6.sin6_family = af; |
| 3055 #ifdef HAVE_SOCKADDR_SA_LEN | 3055 #ifdef HAVE_SOCKADDR_SA_LEN |
| 3056 sin6.sin6_len = sizeof(sin6); | 3056 sin6.sin6_len = sizeof(sin6); |
| 3057 #endif | 3057 #endif |
| 3058 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); | 3058 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
| 3059 tmp = makeipaddr((struct sockaddr *)&sin6, | 3059 tmp = makeipaddr((struct sockaddr *)&sin6, |
| 3060 sizeof(sin6)); | 3060 sizeof(sin6)); |
| 3061 | 3061 |
| 3062 if (pch == h->h_addr_list && alen >= sizeof(sin6)) | 3062 if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
| 3063 memcpy((char *) addr, &sin6, sizeof(sin6)); | 3063 memcpy((char *) addr, &sin6, sizeof(sin6)); |
| 3064 break; | 3064 break; |
| 3065 } | 3065 } |
| 3066 #endif | 3066 #endif |
| 3067 | 3067 |
| 3068 default: /* can't happen */ | 3068 default: /* can't happen */ |
| 3069 PyErr_SetString(socket_error, | 3069 PyErr_SetString(socket_error, |
| 3070 "unsupported address family"); | 3070 "unsupported address family"); |
| 3071 return NULL; | 3071 return NULL; |
| 3072 } | 3072 } |
| 3073 | 3073 |
| 3074 if (tmp == NULL) | 3074 if (tmp == NULL) |
| 3075 goto err; | 3075 goto err; |
| 3076 | 3076 |
| 3077 status = PyList_Append(addr_list, tmp); | 3077 status = PyList_Append(addr_list, tmp); |
| 3078 Py_DECREF(tmp); | 3078 Py_DECREF(tmp); |
| 3079 | 3079 |
| 3080 if (status) | 3080 if (status) |
| 3081 goto err; | 3081 goto err; |
| 3082 } | 3082 } |
| 3083 | 3083 |
| 3084 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); | 3084 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); |
| 3085 | 3085 |
| 3086 err: | 3086 err: |
| 3087 Py_XDECREF(name_list); | 3087 Py_XDECREF(name_list); |
| 3088 Py_XDECREF(addr_list); | 3088 Py_XDECREF(addr_list); |
| 3089 return rtn_tuple; | 3089 return rtn_tuple; |
| 3090 } | 3090 } |
| 3091 | 3091 |
| 3092 | 3092 |
| 3093 /* Python interface to gethostbyname_ex(name). */ | 3093 /* Python interface to gethostbyname_ex(name). */ |
| 3094 | 3094 |
| 3095 /*ARGSUSED*/ | 3095 /*ARGSUSED*/ |
| 3096 static PyObject * | 3096 static PyObject * |
| 3097 socket_gethostbyname_ex(PyObject *self, PyObject *args) | 3097 socket_gethostbyname_ex(PyObject *self, PyObject *args) |
| 3098 { | 3098 { |
| 3099 char *name; | 3099 char *name; |
| 3100 struct hostent *h; | 3100 struct hostent *h; |
| 3101 #ifdef ENABLE_IPV6 | 3101 #ifdef ENABLE_IPV6 |
| 3102 struct sockaddr_storage addr; | 3102 struct sockaddr_storage addr; |
| 3103 #else | 3103 #else |
| 3104 struct sockaddr_in addr; | 3104 struct sockaddr_in addr; |
| 3105 #endif | 3105 #endif |
| 3106 struct sockaddr *sa; | 3106 struct sockaddr *sa; |
| 3107 PyObject *ret; | 3107 PyObject *ret; |
| 3108 #ifdef HAVE_GETHOSTBYNAME_R | 3108 #ifdef HAVE_GETHOSTBYNAME_R |
| 3109 struct hostent hp_allocated; | 3109 struct hostent hp_allocated; |
| 3110 #ifdef HAVE_GETHOSTBYNAME_R_3_ARG | 3110 #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 3111 struct hostent_data data; | 3111 struct hostent_data data; |
| 3112 #else | 3112 #else |
| 3113 char buf[16384]; | 3113 char buf[16384]; |
| 3114 int buf_len = (sizeof buf) - 1; | 3114 int buf_len = (sizeof buf) - 1; |
| 3115 int errnop; | 3115 int errnop; |
| 3116 #endif | 3116 #endif |
| 3117 #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) | 3117 #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
| 3118 int result; | 3118 int result; |
| 3119 #endif | 3119 #endif |
| 3120 #endif /* HAVE_GETHOSTBYNAME_R */ | 3120 #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3121 | 3121 |
| 3122 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) | 3122 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name)) |
| 3123 return NULL; | 3123 return NULL; |
| 3124 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) | 3124 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0) |
| 3125 return NULL; | 3125 return NULL; |
| 3126 Py_BEGIN_ALLOW_THREADS | 3126 Py_BEGIN_ALLOW_THREADS |
| 3127 #ifdef HAVE_GETHOSTBYNAME_R | 3127 #ifdef HAVE_GETHOSTBYNAME_R |
| 3128 #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) | 3128 #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
| 3129 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, | 3129 result = gethostbyname_r(name, &hp_allocated, buf, buf_len, |
| 3130 &h, &errnop); | 3130 &h, &errnop); |
| 3131 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) | 3131 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
| 3132 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); | 3132 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
| 3133 #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ | 3133 #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
| 3134 memset((void *) &data, '\0', sizeof(data)); | 3134 memset((void *) &data, '\0', sizeof(data)); |
| 3135 result = gethostbyname_r(name, &hp_allocated, &data); | 3135 result = gethostbyname_r(name, &hp_allocated, &data); |
| 3136 h = (result != 0) ? NULL : &hp_allocated; | 3136 h = (result != 0) ? NULL : &hp_allocated; |
| 3137 #endif | 3137 #endif |
| 3138 #else /* not HAVE_GETHOSTBYNAME_R */ | 3138 #else /* not HAVE_GETHOSTBYNAME_R */ |
| 3139 #ifdef USE_GETHOSTBYNAME_LOCK | 3139 #ifdef USE_GETHOSTBYNAME_LOCK |
| 3140 PyThread_acquire_lock(netdb_lock, 1); | 3140 PyThread_acquire_lock(netdb_lock, 1); |
| 3141 #endif | 3141 #endif |
| 3142 h = gethostbyname(name); | 3142 h = gethostbyname(name); |
| 3143 #endif /* HAVE_GETHOSTBYNAME_R */ | 3143 #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3144 Py_END_ALLOW_THREADS | 3144 Py_END_ALLOW_THREADS |
| 3145 /* Some C libraries would require addr.__ss_family instead of | 3145 /* Some C libraries would require addr.__ss_family instead of |
| 3146 addr.ss_family. | 3146 addr.ss_family. |
| 3147 Therefore, we cast the sockaddr_storage into sockaddr to | 3147 Therefore, we cast the sockaddr_storage into sockaddr to |
| 3148 access sa_family. */ | 3148 access sa_family. */ |
| 3149 sa = (struct sockaddr*)&addr; | 3149 sa = (struct sockaddr*)&addr; |
| 3150 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), | 3150 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), |
| 3151 sa->sa_family); | 3151 sa->sa_family); |
| 3152 #ifdef USE_GETHOSTBYNAME_LOCK | 3152 #ifdef USE_GETHOSTBYNAME_LOCK |
| 3153 PyThread_release_lock(netdb_lock); | 3153 PyThread_release_lock(netdb_lock); |
| 3154 #endif | 3154 #endif |
| 3155 return ret; | 3155 return ret; |
| 3156 } | 3156 } |
| 3157 | 3157 |
| 3158 PyDoc_STRVAR(ghbn_ex_doc, | 3158 PyDoc_STRVAR(ghbn_ex_doc, |
| 3159 "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ | 3159 "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
| 3160 \n\ | 3160 \n\ |
| 3167 /*ARGSUSED*/ | 3167 /*ARGSUSED*/ |
| 3168 static PyObject * | 3168 static PyObject * |
| 3169 socket_gethostbyaddr(PyObject *self, PyObject *args) | 3169 socket_gethostbyaddr(PyObject *self, PyObject *args) |
| 3170 { | 3170 { |
| 3171 #ifdef ENABLE_IPV6 | 3171 #ifdef ENABLE_IPV6 |
| 3172 struct sockaddr_storage addr; | 3172 struct sockaddr_storage addr; |
| 3173 #else | 3173 #else |
| 3174 struct sockaddr_in addr; | 3174 struct sockaddr_in addr; |
| 3175 #endif | 3175 #endif |
| 3176 struct sockaddr *sa = (struct sockaddr *)&addr; | 3176 struct sockaddr *sa = (struct sockaddr *)&addr; |
| 3177 char *ip_num; | 3177 char *ip_num; |
| 3178 struct hostent *h; | 3178 struct hostent *h; |
| 3179 PyObject *ret; | 3179 PyObject *ret; |
| 3180 #ifdef HAVE_GETHOSTBYNAME_R | 3180 #ifdef HAVE_GETHOSTBYNAME_R |
| 3181 struct hostent hp_allocated; | 3181 struct hostent hp_allocated; |
| 3182 #ifdef HAVE_GETHOSTBYNAME_R_3_ARG | 3182 #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
| 3183 struct hostent_data data; | 3183 struct hostent_data data; |
| 3184 #else | 3184 #else |
| 3185 /* glibcs up to 2.10 assume that the buf argument to | 3185 /* glibcs up to 2.10 assume that the buf argument to |
| 3186 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc | 3186 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
| 3187 does not ensure. The attribute below instructs the compiler | 3187 does not ensure. The attribute below instructs the compiler |
| 3188 to maintain this alignment. */ | 3188 to maintain this alignment. */ |
| 3189 char buf[16384] Py_ALIGNED(8); | 3189 char buf[16384] Py_ALIGNED(8); |
| 3190 int buf_len = (sizeof buf) - 1; | 3190 int buf_len = (sizeof buf) - 1; |
| 3191 int errnop; | 3191 int errnop; |
| 3192 #endif | 3192 #endif |
| 3193 #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) | 3193 #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
| 3194 int result; | 3194 int result; |
| 3195 #endif | 3195 #endif |
| 3196 #endif /* HAVE_GETHOSTBYNAME_R */ | 3196 #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3197 char *ap; | 3197 char *ap; |
| 3198 int al; | 3198 int al; |
| 3199 int af; | 3199 int af; |
| 3200 | 3200 |
| 3201 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) | 3201 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num)) |
| 3202 return NULL; | 3202 return NULL; |
| 3203 af = AF_UNSPEC; | 3203 af = AF_UNSPEC; |
| 3204 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) | 3204 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
| 3205 return NULL; | 3205 return NULL; |
| 3206 af = sa->sa_family; | 3206 af = sa->sa_family; |
| 3207 ap = NULL; | 3207 ap = NULL; |
| 3208 al = 0; | 3208 al = 0; |
| 3209 switch (af) { | 3209 switch (af) { |
| 3210 case AF_INET: | 3210 case AF_INET: |
| 3211 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; | 3211 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
| 3212 al = sizeof(((struct sockaddr_in *)sa)->sin_addr); | 3212 al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
| 3213 break; | 3213 break; |
| 3214 #ifdef ENABLE_IPV6 | 3214 #ifdef ENABLE_IPV6 |
| 3215 case AF_INET6: | 3215 case AF_INET6: |
| 3216 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; | 3216 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
| 3217 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); | 3217 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
| 3218 break; | 3218 break; |
| 3219 #endif | 3219 #endif |
| 3220 default: | 3220 default: |
| 3221 PyErr_SetString(socket_error, "unsupported address family"); | 3221 PyErr_SetString(socket_error, "unsupported address family"); |
| 3222 return NULL; | 3222 return NULL; |
| 3223 } | 3223 } |
| 3224 Py_BEGIN_ALLOW_THREADS | 3224 Py_BEGIN_ALLOW_THREADS |
| 3225 #ifdef HAVE_GETHOSTBYNAME_R | 3225 #ifdef HAVE_GETHOSTBYNAME_R |
| 3226 #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) | 3226 #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
| 3227 result = gethostbyaddr_r(ap, al, af, | 3227 result = gethostbyaddr_r(ap, al, af, |
| 3228 &hp_allocated, buf, buf_len, | 3228 &hp_allocated, buf, buf_len, |
| 3229 &h, &errnop); | 3229 &h, &errnop); |
| 3230 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) | 3230 #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
| 3231 h = gethostbyaddr_r(ap, al, af, | 3231 h = gethostbyaddr_r(ap, al, af, |
| 3232 &hp_allocated, buf, buf_len, &errnop); | 3232 &hp_allocated, buf, buf_len, &errnop); |
| 3233 #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ | 3233 #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
| 3234 memset((void *) &data, '\0', sizeof(data)); | 3234 memset((void *) &data, '\0', sizeof(data)); |
| 3235 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); | 3235 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
| 3236 h = (result != 0) ? NULL : &hp_allocated; | 3236 h = (result != 0) ? NULL : &hp_allocated; |
| 3237 #endif | 3237 #endif |
| 3238 #else /* not HAVE_GETHOSTBYNAME_R */ | 3238 #else /* not HAVE_GETHOSTBYNAME_R */ |
| 3239 #ifdef USE_GETHOSTBYNAME_LOCK | 3239 #ifdef USE_GETHOSTBYNAME_LOCK |
| 3240 PyThread_acquire_lock(netdb_lock, 1); | 3240 PyThread_acquire_lock(netdb_lock, 1); |
| 3241 #endif | 3241 #endif |
| 3242 h = gethostbyaddr(ap, al, af); | 3242 h = gethostbyaddr(ap, al, af); |
| 3243 #endif /* HAVE_GETHOSTBYNAME_R */ | 3243 #endif /* HAVE_GETHOSTBYNAME_R */ |
| 3244 Py_END_ALLOW_THREADS | 3244 Py_END_ALLOW_THREADS |
| 3245 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); | 3245 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af); |
| 3246 #ifdef USE_GETHOSTBYNAME_LOCK | 3246 #ifdef USE_GETHOSTBYNAME_LOCK |
| 3247 PyThread_release_lock(netdb_lock); | 3247 PyThread_release_lock(netdb_lock); |
| 3248 #endif | 3248 #endif |
| 3249 return ret; | 3249 return ret; |
| 3250 } | 3250 } |
| 3251 | 3251 |
| 3252 PyDoc_STRVAR(gethostbyaddr_doc, | 3252 PyDoc_STRVAR(gethostbyaddr_doc, |
| 3253 "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ | 3253 "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
| 3254 \n\ | 3254 \n\ |
| 3262 | 3262 |
| 3263 /*ARGSUSED*/ | 3263 /*ARGSUSED*/ |
| 3264 static PyObject * | 3264 static PyObject * |
| 3265 socket_getservbyname(PyObject *self, PyObject *args) | 3265 socket_getservbyname(PyObject *self, PyObject *args) |
| 3266 { | 3266 { |
| 3267 char *name, *proto=NULL; | 3267 char *name, *proto=NULL; |
| 3268 struct servent *sp; | 3268 struct servent *sp; |
| 3269 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) | 3269 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
| 3270 return NULL; | 3270 return NULL; |
| 3271 Py_BEGIN_ALLOW_THREADS | 3271 Py_BEGIN_ALLOW_THREADS |
| 3272 sp = getservbyname(name, proto); | 3272 sp = getservbyname(name, proto); |
| 3273 Py_END_ALLOW_THREADS | 3273 Py_END_ALLOW_THREADS |
| 3274 if (sp == NULL) { | 3274 if (sp == NULL) { |
| 3275 PyErr_SetString(socket_error, "service/proto not found"); | 3275 PyErr_SetString(socket_error, "service/proto not found"); |
| 3276 return NULL; | 3276 return NULL; |
| 3277 } | 3277 } |
| 3278 return PyLong_FromLong((long) ntohs(sp->s_port)); | 3278 return PyLong_FromLong((long) ntohs(sp->s_port)); |
| 3279 } | 3279 } |
| 3280 | 3280 |
| 3281 PyDoc_STRVAR(getservbyname_doc, | 3281 PyDoc_STRVAR(getservbyname_doc, |
| 3282 "getservbyname(servicename[, protocolname]) -> integer\n\ | 3282 "getservbyname(servicename[, protocolname]) -> integer\n\ |
| 3283 \n\ | 3283 \n\ |
| 3292 | 3292 |
| 3293 /*ARGSUSED*/ | 3293 /*ARGSUSED*/ |
| 3294 static PyObject * | 3294 static PyObject * |
| 3295 socket_getservbyport(PyObject *self, PyObject *args) | 3295 socket_getservbyport(PyObject *self, PyObject *args) |
| 3296 { | 3296 { |
| 3297 int port; | 3297 int port; |
| 3298 char *proto=NULL; | 3298 char *proto=NULL; |
| 3299 struct servent *sp; | 3299 struct servent *sp; |
| 3300 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) | 3300 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
| 3301 return NULL; | 3301 return NULL; |
| 3302 if (port < 0 || port > 0xffff) { | 3302 if (port < 0 || port > 0xffff) { |
| 3303 PyErr_SetString( | 3303 PyErr_SetString( |
| 3304 PyExc_OverflowError, | 3304 PyExc_OverflowError, |
| 3305 "getservbyport: port must be 0-65535."); | 3305 "getservbyport: port must be 0-65535."); |
| 3306 return NULL; | 3306 return NULL; |
| 3307 } | 3307 } |
| 3308 Py_BEGIN_ALLOW_THREADS | 3308 Py_BEGIN_ALLOW_THREADS |
| 3309 sp = getservbyport(htons((short)port), proto); | 3309 sp = getservbyport(htons((short)port), proto); |
| 3310 Py_END_ALLOW_THREADS | 3310 Py_END_ALLOW_THREADS |
| 3311 if (sp == NULL) { | 3311 if (sp == NULL) { |
| 3312 PyErr_SetString(socket_error, "port/proto not found"); | 3312 PyErr_SetString(socket_error, "port/proto not found"); |
| 3313 return NULL; | 3313 return NULL; |
| 3314 } | 3314 } |
| 3315 return PyUnicode_FromString(sp->s_name); | 3315 return PyUnicode_FromString(sp->s_name); |
| 3316 } | 3316 } |
| 3317 | 3317 |
| 3318 PyDoc_STRVAR(getservbyport_doc, | 3318 PyDoc_STRVAR(getservbyport_doc, |
| 3319 "getservbyport(port[, protocolname]) -> string\n\ | 3319 "getservbyport(port[, protocolname]) -> string\n\ |
| 3320 \n\ | 3320 \n\ |
| 3328 | 3328 |
| 3329 /*ARGSUSED*/ | 3329 /*ARGSUSED*/ |
| 3330 static PyObject * | 3330 static PyObject * |
| 3331 socket_getprotobyname(PyObject *self, PyObject *args) | 3331 socket_getprotobyname(PyObject *self, PyObject *args) |
| 3332 { | 3332 { |
| 3333 char *name; | 3333 char *name; |
| 3334 struct protoent *sp; | 3334 struct protoent *sp; |
| 3335 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) | 3335 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
| 3336 return NULL; | 3336 return NULL; |
| 3337 Py_BEGIN_ALLOW_THREADS | 3337 Py_BEGIN_ALLOW_THREADS |
| 3338 sp = getprotobyname(name); | 3338 sp = getprotobyname(name); |
| 3339 Py_END_ALLOW_THREADS | 3339 Py_END_ALLOW_THREADS |
| 3340 if (sp == NULL) { | 3340 if (sp == NULL) { |
| 3341 PyErr_SetString(socket_error, "protocol not found"); | 3341 PyErr_SetString(socket_error, "protocol not found"); |
| 3342 return NULL; | 3342 return NULL; |
| 3343 } | 3343 } |
| 3344 return PyLong_FromLong((long) sp->p_proto); | 3344 return PyLong_FromLong((long) sp->p_proto); |
| 3345 } | 3345 } |
| 3346 | 3346 |
| 3347 PyDoc_STRVAR(getprotobyname_doc, | 3347 PyDoc_STRVAR(getprotobyname_doc, |
| 3348 "getprotobyname(name) -> integer\n\ | 3348 "getprotobyname(name) -> integer\n\ |
| 3349 \n\ | 3349 \n\ |
| 3354 /* dup() function for socket fds */ | 3354 /* dup() function for socket fds */ |
| 3355 | 3355 |
| 3356 static PyObject * | 3356 static PyObject * |
| 3357 socket_dup(PyObject *self, PyObject *fdobj) | 3357 socket_dup(PyObject *self, PyObject *fdobj) |
| 3358 { | 3358 { |
| 3359 SOCKET_T fd, newfd; | 3359 SOCKET_T fd, newfd; |
| 3360 PyObject *newfdobj; | 3360 PyObject *newfdobj; |
| 3361 | 3361 |
| 3362 | 3362 |
| 3363 fd = PyLong_AsSocket_t(fdobj); | 3363 fd = PyLong_AsSocket_t(fdobj); |
| 3364 if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) | 3364 if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
| 3365 return NULL; | 3365 return NULL; |
| 3366 | 3366 |
| 3367 newfd = dup_socket(fd); | 3367 newfd = dup_socket(fd); |
| 3368 if (newfd == INVALID_SOCKET) | 3368 if (newfd == INVALID_SOCKET) |
| 3369 return set_error(); | 3369 return set_error(); |
| 3370 | 3370 |
| 3371 newfdobj = PyLong_FromSocket_t(newfd); | 3371 newfdobj = PyLong_FromSocket_t(newfd); |
| 3372 if (newfdobj == NULL) | 3372 if (newfdobj == NULL) |
| 3373 SOCKETCLOSE(newfd); | 3373 SOCKETCLOSE(newfd); |
| 3374 return newfdobj; | 3374 return newfdobj; |
| 3375 } | 3375 } |
| 3376 | 3376 |
| 3377 PyDoc_STRVAR(dup_doc, | 3377 PyDoc_STRVAR(dup_doc, |
| 3378 "dup(integer) -> integer\n\ | 3378 "dup(integer) -> integer\n\ |
| 3379 \n\ | 3379 \n\ |
| 3389 | 3389 |
| 3390 /*ARGSUSED*/ | 3390 /*ARGSUSED*/ |
| 3391 static PyObject * | 3391 static PyObject * |
| 3392 socket_socketpair(PyObject *self, PyObject *args) | 3392 socket_socketpair(PyObject *self, PyObject *args) |
| 3393 { | 3393 { |
| 3394 PySocketSockObject *s0 = NULL, *s1 = NULL; | 3394 PySocketSockObject *s0 = NULL, *s1 = NULL; |
| 3395 SOCKET_T sv[2]; | 3395 SOCKET_T sv[2]; |
| 3396 int family, type = SOCK_STREAM, proto = 0; | 3396 int family, type = SOCK_STREAM, proto = 0; |
| 3397 PyObject *res = NULL; | 3397 PyObject *res = NULL; |
| 3398 | 3398 |
| 3399 #if defined(AF_UNIX) | 3399 #if defined(AF_UNIX) |
| 3400 family = AF_UNIX; | 3400 family = AF_UNIX; |
| 3401 #else | 3401 #else |
| 3402 family = AF_INET; | 3402 family = AF_INET; |
| 3403 #endif | 3403 #endif |
| 3404 if (!PyArg_ParseTuple(args, "|iii:socketpair", | 3404 if (!PyArg_ParseTuple(args, "|iii:socketpair", |
| 3405 &family, &type, &proto)) | 3405 &family, &type, &proto)) |
| 3406 return NULL; | 3406 return NULL; |
| 3407 /* Create a pair of socket fds */ | 3407 /* Create a pair of socket fds */ |
| 3408 if (socketpair(family, type, proto, sv) < 0) | 3408 if (socketpair(family, type, proto, sv) < 0) |
| 3409 return set_error(); | 3409 return set_error(); |
| 3410 s0 = new_sockobject(sv[0], family, type, proto); | 3410 s0 = new_sockobject(sv[0], family, type, proto); |
| 3411 if (s0 == NULL) | 3411 if (s0 == NULL) |
| 3412 goto finally; | 3412 goto finally; |
| 3413 s1 = new_sockobject(sv[1], family, type, proto); | 3413 s1 = new_sockobject(sv[1], family, type, proto); |
| 3414 if (s1 == NULL) | 3414 if (s1 == NULL) |
| 3415 goto finally; | 3415 goto finally; |
| 3416 res = PyTuple_Pack(2, s0, s1); | 3416 res = PyTuple_Pack(2, s0, s1); |
| 3417 | 3417 |
| 3418 finally: | 3418 finally: |
| 3419 if (res == NULL) { | 3419 if (res == NULL) { |
| 3420 if (s0 == NULL) | 3420 if (s0 == NULL) |
| 3421 SOCKETCLOSE(sv[0]); | 3421 SOCKETCLOSE(sv[0]); |
| 3422 if (s1 == NULL) | 3422 if (s1 == NULL) |
| 3423 SOCKETCLOSE(sv[1]); | 3423 SOCKETCLOSE(sv[1]); |
| 3424 } | 3424 } |
| 3425 Py_XDECREF(s0); | 3425 Py_XDECREF(s0); |
| 3426 Py_XDECREF(s1); | 3426 Py_XDECREF(s1); |
| 3427 return res; | 3427 return res; |
| 3428 } | 3428 } |
| 3429 | 3429 |
| 3430 PyDoc_STRVAR(socketpair_doc, | 3430 PyDoc_STRVAR(socketpair_doc, |
| 3431 "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ | 3431 "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\ |
| 3432 \n\ | 3432 \n\ |
| 3439 | 3439 |
| 3440 | 3440 |
| 3441 static PyObject * | 3441 static PyObject * |
| 3442 socket_ntohs(PyObject *self, PyObject *args) | 3442 socket_ntohs(PyObject *self, PyObject *args) |
| 3443 { | 3443 { |
| 3444 int x1, x2; | 3444 int x1, x2; |
| 3445 | 3445 |
| 3446 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { | 3446 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { |
| 3447 return NULL; | 3447 return NULL; |
| 3448 } | 3448 } |
| 3449 if (x1 < 0) { | 3449 if (x1 < 0) { |
| 3450 PyErr_SetString(PyExc_OverflowError, | 3450 PyErr_SetString(PyExc_OverflowError, |
| 3451 "can't convert negative number to unsigned long"); | 3451 "can't convert negative number to unsigned long"); |
| 3452 return NULL; | 3452 return NULL; |
| 3453 } | 3453 } |
| 3454 x2 = (unsigned int)ntohs((unsigned short)x1); | 3454 x2 = (unsigned int)ntohs((unsigned short)x1); |
| 3455 return PyLong_FromLong(x2); | 3455 return PyLong_FromLong(x2); |
| 3456 } | 3456 } |
| 3457 | 3457 |
| 3458 PyDoc_STRVAR(ntohs_doc, | 3458 PyDoc_STRVAR(ntohs_doc, |
| 3459 "ntohs(integer) -> integer\n\ | 3459 "ntohs(integer) -> integer\n\ |
| 3460 \n\ | 3460 \n\ |
| 3462 | 3462 |
| 3463 | 3463 |
| 3464 static PyObject * | 3464 static PyObject * |
| 3465 socket_ntohl(PyObject *self, PyObject *arg) | 3465 socket_ntohl(PyObject *self, PyObject *arg) |
| 3466 { | 3466 { |
| 3467 unsigned long x; | 3467 unsigned long x; |
| 3468 | 3468 |
| 3469 if (PyLong_Check(arg)) { | 3469 if (PyLong_Check(arg)) { |
| 3470 x = PyLong_AsUnsignedLong(arg); | 3470 x = PyLong_AsUnsignedLong(arg); |
| 3471 if (x == (unsigned long) -1 && PyErr_Occurred()) | 3471 if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3472 return NULL; | 3472 return NULL; |
| 3473 #if SIZEOF_LONG > 4 | 3473 #if SIZEOF_LONG > 4 |
| 3474 { | 3474 { |
| 3475 unsigned long y; | 3475 unsigned long y; |
| 3476 /* only want the trailing 32 bits */ | 3476 /* only want the trailing 32 bits */ |
| 3477 y = x & 0xFFFFFFFFUL; | 3477 y = x & 0xFFFFFFFFUL; |
| 3478 if (y ^ x) | 3478 if (y ^ x) |
| 3479 return PyErr_Format(PyExc_OverflowError, | 3479 return PyErr_Format(PyExc_OverflowError, |
| 3480 "long int larger than 32 bits"); | 3480 "long int larger than 32 bits"); |
| 3481 x = y; | 3481 x = y; |
| 3482 } | 3482 } |
| 3483 #endif | 3483 #endif |
| 3484 } | 3484 } |
| 3485 else | 3485 else |
| 3486 return PyErr_Format(PyExc_TypeError, | 3486 return PyErr_Format(PyExc_TypeError, |
| 3487 "expected int/long, %s found", | 3487 "expected int/long, %s found", |
| 3488 Py_TYPE(arg)->tp_name); | 3488 Py_TYPE(arg)->tp_name); |
| 3489 if (x == (unsigned long) -1 && PyErr_Occurred()) | 3489 if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3490 return NULL; | 3490 return NULL; |
| 3491 return PyLong_FromUnsignedLong(ntohl(x)); | 3491 return PyLong_FromUnsignedLong(ntohl(x)); |
| 3492 } | 3492 } |
| 3493 | 3493 |
| 3494 PyDoc_STRVAR(ntohl_doc, | 3494 PyDoc_STRVAR(ntohl_doc, |
| 3495 "ntohl(integer) -> integer\n\ | 3495 "ntohl(integer) -> integer\n\ |
| 3496 \n\ | 3496 \n\ |
| 3498 | 3498 |
| 3499 | 3499 |
| 3500 static PyObject * | 3500 static PyObject * |
| 3501 socket_htons(PyObject *self, PyObject *args) | 3501 socket_htons(PyObject *self, PyObject *args) |
| 3502 { | 3502 { |
| 3503 int x1, x2; | 3503 int x1, x2; |
| 3504 | 3504 |
| 3505 if (!PyArg_ParseTuple(args, "i:htons", &x1)) { | 3505 if (!PyArg_ParseTuple(args, "i:htons", &x1)) { |
| 3506 return NULL; | 3506 return NULL; |
| 3507 } | 3507 } |
| 3508 if (x1 < 0) { | 3508 if (x1 < 0) { |
| 3509 PyErr_SetString(PyExc_OverflowError, | 3509 PyErr_SetString(PyExc_OverflowError, |
| 3510 "can't convert negative number to unsigned long"); | 3510 "can't convert negative number to unsigned long"); |
| 3511 return NULL; | 3511 return NULL; |
| 3512 } | 3512 } |
| 3513 x2 = (unsigned int)htons((unsigned short)x1); | 3513 x2 = (unsigned int)htons((unsigned short)x1); |
| 3514 return PyLong_FromLong(x2); | 3514 return PyLong_FromLong(x2); |
| 3515 } | 3515 } |
| 3516 | 3516 |
| 3517 PyDoc_STRVAR(htons_doc, | 3517 PyDoc_STRVAR(htons_doc, |
| 3518 "htons(integer) -> integer\n\ | 3518 "htons(integer) -> integer\n\ |
| 3519 \n\ | 3519 \n\ |
| 3521 | 3521 |
| 3522 | 3522 |
| 3523 static PyObject * | 3523 static PyObject * |
| 3524 socket_htonl(PyObject *self, PyObject *arg) | 3524 socket_htonl(PyObject *self, PyObject *arg) |
| 3525 { | 3525 { |
| 3526 unsigned long x; | 3526 unsigned long x; |
| 3527 | 3527 |
| 3528 if (PyLong_Check(arg)) { | 3528 if (PyLong_Check(arg)) { |
| 3529 x = PyLong_AsUnsignedLong(arg); | 3529 x = PyLong_AsUnsignedLong(arg); |
| 3530 if (x == (unsigned long) -1 && PyErr_Occurred()) | 3530 if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 3531 return NULL; | 3531 return NULL; |
| 3532 #if SIZEOF_LONG > 4 | 3532 #if SIZEOF_LONG > 4 |
| 3533 { | 3533 { |
| 3534 unsigned long y; | 3534 unsigned long y; |
| 3535 /* only want the trailing 32 bits */ | 3535 /* only want the trailing 32 bits */ |
| 3536 y = x & 0xFFFFFFFFUL; | 3536 y = x & 0xFFFFFFFFUL; |
| 3537 if (y ^ x) | 3537 if (y ^ x) |
| 3538 return PyErr_Format(PyExc_OverflowError, | 3538 return PyErr_Format(PyExc_OverflowError, |
| 3539 "long int larger than 32 bits"); | 3539 "long int larger than 32 bits"); |
| 3540 x = y; | 3540 x = y; |
| 3541 } | 3541 } |
| 3542 #endif | 3542 #endif |
| 3543 } | 3543 } |
| 3544 else | 3544 else |
| 3545 return PyErr_Format(PyExc_TypeError, | 3545 return PyErr_Format(PyExc_TypeError, |
| 3546 "expected int/long, %s found", | 3546 "expected int/long, %s found", |
| 3547 Py_TYPE(arg)->tp_name); | 3547 Py_TYPE(arg)->tp_name); |
| 3548 return PyLong_FromUnsignedLong(htonl((unsigned long)x)); | 3548 return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
| 3549 } | 3549 } |
| 3550 | 3550 |
| 3551 PyDoc_STRVAR(htonl_doc, | 3551 PyDoc_STRVAR(htonl_doc, |
| 3552 "htonl(integer) -> integer\n\ | 3552 "htonl(integer) -> integer\n\ |
| 3553 \n\ | 3553 \n\ |
| 3566 { | 3566 { |
| 3567 #ifndef INADDR_NONE | 3567 #ifndef INADDR_NONE |
| 3568 #define INADDR_NONE (-1) | 3568 #define INADDR_NONE (-1) |
| 3569 #endif | 3569 #endif |
| 3570 #ifdef HAVE_INET_ATON | 3570 #ifdef HAVE_INET_ATON |
| 3571 struct in_addr buf; | 3571 struct in_addr buf; |
| 3572 #endif | 3572 #endif |
| 3573 | 3573 |
| 3574 #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) | 3574 #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3575 #if (SIZEOF_INT != 4) | 3575 #if (SIZEOF_INT != 4) |
| 3576 #error "Not sure if in_addr_t exists and int is not 32-bits." | 3576 #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 3577 #endif | 3577 #endif |
| 3578 /* Have to use inet_addr() instead */ | 3578 /* Have to use inet_addr() instead */ |
| 3579 unsigned int packed_addr; | 3579 unsigned int packed_addr; |
| 3580 #endif | 3580 #endif |
| 3581 char *ip_addr; | 3581 char *ip_addr; |
| 3582 | 3582 |
| 3583 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) | 3583 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
| 3584 return NULL; | 3584 return NULL; |
| 3585 | 3585 |
| 3586 | 3586 |
| 3587 #ifdef HAVE_INET_ATON | 3587 #ifdef HAVE_INET_ATON |
| 3588 | 3588 |
| 3589 #ifdef USE_INET_ATON_WEAKLINK | 3589 #ifdef USE_INET_ATON_WEAKLINK |
| 3590 if (inet_aton != NULL) { | 3590 if (inet_aton != NULL) { |
| 3591 #endif | 3591 #endif |
| 3592 if (inet_aton(ip_addr, &buf)) | 3592 if (inet_aton(ip_addr, &buf)) |
| 3593 return PyBytes_FromStringAndSize((char *)(&buf), | 3593 return PyBytes_FromStringAndSize((char *)(&buf), |
| 3594 sizeof(buf)); | 3594 sizeof(buf)); |
| 3595 | 3595 |
| 3596 PyErr_SetString(socket_error, | 3596 PyErr_SetString(socket_error, |
| 3597 "illegal IP address string passed to inet_aton"); | 3597 "illegal IP address string passed to inet_aton"); |
| 3598 return NULL; | 3598 return NULL; |
| 3599 | 3599 |
| 3600 #ifdef USE_INET_ATON_WEAKLINK | 3600 #ifdef USE_INET_ATON_WEAKLINK |
| 3601 } else { | 3601 } else { |
| 3602 #endif | 3602 #endif |
| 3603 | 3603 |
| 3604 #endif | 3604 #endif |
| 3605 | 3605 |
| 3606 #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) | 3606 #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
| 3607 | 3607 |
| 3608 /* special-case this address as inet_addr might return INADDR_NONE | 3608 /* special-case this address as inet_addr might return INADDR_NONE |
| 3609 * for this */ | 3609 * for this */ |
| 3610 if (strcmp(ip_addr, "255.255.255.255") == 0) { | 3610 if (strcmp(ip_addr, "255.255.255.255") == 0) { |
| 3611 packed_addr = 0xFFFFFFFF; | 3611 packed_addr = 0xFFFFFFFF; |
| 3612 } else { | 3612 } else { |
| 3613 | 3613 |
| 3614 packed_addr = inet_addr(ip_addr); | 3614 packed_addr = inet_addr(ip_addr); |
| 3615 | 3615 |
| 3616 if (packed_addr == INADDR_NONE) { /* invalid address */ | 3616 if (packed_addr == INADDR_NONE) { /* invalid address */ |
| 3617 PyErr_SetString(socket_error, | 3617 PyErr_SetString(socket_error, |
| 3618 "illegal IP address string passed to inet_aton"); | 3618 "illegal IP address string passed to inet_aton"); |
| 3619 return NULL; | 3619 return NULL; |
| 3620 } | 3620 } |
| 3621 } | 3621 } |
| 3622 return PyBytes_FromStringAndSize((char *) &packed_addr, | 3622 return PyBytes_FromStringAndSize((char *) &packed_addr, |
| 3623 sizeof(packed_addr)); | 3623 sizeof(packed_addr)); |
| 3624 | 3624 |
| 3625 #ifdef USE_INET_ATON_WEAKLINK | 3625 #ifdef USE_INET_ATON_WEAKLINK |
| 3626 } | 3626 } |
| 3627 #endif | 3627 #endif |
| 3628 | 3628 |
| 3635 Convert an IP address from 32-bit packed binary format to string format"); | 3635 Convert an IP address from 32-bit packed binary format to string format"); |
| 3636 | 3636 |
| 3637 static PyObject* | 3637 static PyObject* |
| 3638 socket_inet_ntoa(PyObject *self, PyObject *args) | 3638 socket_inet_ntoa(PyObject *self, PyObject *args) |
| 3639 { | 3639 { |
| 3640 char *packed_str; | 3640 char *packed_str; |
| 3641 int addr_len; | 3641 int addr_len; |
| 3642 struct in_addr packed_addr; | 3642 struct in_addr packed_addr; |
| 3643 | 3643 |
| 3644 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { | 3644 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) { |
| 3645 return NULL; | 3645 return NULL; |
| 3646 } | 3646 } |
| 3647 | 3647 |
| 3648 if (addr_len != sizeof(packed_addr)) { | 3648 if (addr_len != sizeof(packed_addr)) { |
| 3649 PyErr_SetString(socket_error, | 3649 PyErr_SetString(socket_error, |
| 3650 "packed IP wrong length for inet_ntoa"); | 3650 "packed IP wrong length for inet_ntoa"); |
| 3651 return NULL; | 3651 return NULL; |
| 3652 } | 3652 } |
| 3653 | 3653 |
| 3654 memcpy(&packed_addr, packed_str, addr_len); | 3654 memcpy(&packed_addr, packed_str, addr_len); |
| 3655 | 3655 |
| 3656 return PyUnicode_FromString(inet_ntoa(packed_addr)); | 3656 return PyUnicode_FromString(inet_ntoa(packed_addr)); |
| 3657 } | 3657 } |
| 3658 | 3658 |
| 3659 #ifdef HAVE_INET_PTON | 3659 #ifdef HAVE_INET_PTON |
| 3660 | 3660 |
| 3661 PyDoc_STRVAR(inet_pton_doc, | 3661 PyDoc_STRVAR(inet_pton_doc, |
| 3665 for use with low-level network functions."); | 3665 for use with low-level network functions."); |
| 3666 | 3666 |
| 3667 static PyObject * | 3667 static PyObject * |
| 3668 socket_inet_pton(PyObject *self, PyObject *args) | 3668 socket_inet_pton(PyObject *self, PyObject *args) |
| 3669 { | 3669 { |
| 3670 int af; | 3670 int af; |
| 3671 char* ip; | 3671 char* ip; |
| 3672 int retval; | 3672 int retval; |
| 3673 #ifdef ENABLE_IPV6 | 3673 #ifdef ENABLE_IPV6 |
| 3674 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; | 3674 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; |
| 3675 #else | 3675 #else |
| 3676 char packed[sizeof(struct in_addr)]; | 3676 char packed[sizeof(struct in_addr)]; |
| 3677 #endif | 3677 #endif |
| 3678 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { | 3678 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
| 3679 return NULL; | 3679 return NULL; |
| 3680 } | 3680 } |
| 3681 | 3681 |
| 3682 #if !defined(ENABLE_IPV6) && defined(AF_INET6) | 3682 #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
| 3683 if(af == AF_INET6) { | 3683 if(af == AF_INET6) { |
| 3684 PyErr_SetString(socket_error, | 3684 PyErr_SetString(socket_error, |
| 3685 "can't use AF_INET6, IPv6 is disabled"); | 3685 "can't use AF_INET6, IPv6 is disabled"); |
| 3686 return NULL; | 3686 return NULL; |
| 3687 } | 3687 } |
| 3688 #endif | 3688 #endif |
| 3689 | 3689 |
| 3690 retval = inet_pton(af, ip, packed); | 3690 retval = inet_pton(af, ip, packed); |
| 3691 if (retval < 0) { | 3691 if (retval < 0) { |
| 3692 PyErr_SetFromErrno(socket_error); | 3692 PyErr_SetFromErrno(socket_error); |
| 3693 return NULL; | 3693 return NULL; |
| 3694 } else if (retval == 0) { | 3694 } else if (retval == 0) { |
| 3695 PyErr_SetString(socket_error, | 3695 PyErr_SetString(socket_error, |
| 3696 "illegal IP address string passed to inet_pton"); | 3696 "illegal IP address string passed to inet_pton"); |
| 3697 return NULL; | 3697 return NULL; |
| 3698 } else if (af == AF_INET) { | 3698 } else if (af == AF_INET) { |
| 3699 return PyBytes_FromStringAndSize(packed, | 3699 return PyBytes_FromStringAndSize(packed, |
| 3700 sizeof(struct in_addr)); | 3700 sizeof(struct in_addr)); |
| 3701 #ifdef ENABLE_IPV6 | 3701 #ifdef ENABLE_IPV6 |
| 3702 } else if (af == AF_INET6) { | 3702 } else if (af == AF_INET6) { |
| 3703 return PyBytes_FromStringAndSize(packed, | 3703 return PyBytes_FromStringAndSize(packed, |
| 3704 sizeof(struct in6_addr)); | 3704 sizeof(struct in6_addr)); |
| 3705 #endif | 3705 #endif |
| 3706 } else { | 3706 } else { |
| 3707 PyErr_SetString(socket_error, "unknown address family"); | 3707 PyErr_SetString(socket_error, "unknown address family"); |
| 3708 return NULL; | 3708 return NULL; |
| 3709 } | 3709 } |
| 3710 } | 3710 } |
| 3711 | 3711 |
| 3712 PyDoc_STRVAR(inet_ntop_doc, | 3712 PyDoc_STRVAR(inet_ntop_doc, |
| 3713 "inet_ntop(af, packed_ip) -> string formatted IP address\n\ | 3713 "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
| 3714 \n\ | 3714 \n\ |
| 3715 Convert a packed IP address of the given family to string format."); | 3715 Convert a packed IP address of the given family to string format."); |
| 3716 | 3716 |
| 3717 static PyObject * | 3717 static PyObject * |
| 3718 socket_inet_ntop(PyObject *self, PyObject *args) | 3718 socket_inet_ntop(PyObject *self, PyObject *args) |
| 3719 { | 3719 { |
| 3720 int af; | 3720 int af; |
| 3721 char* packed; | 3721 char* packed; |
| 3722 int len; | 3722 int len; |
| 3723 const char* retval; | 3723 const char* retval; |
| 3724 #ifdef ENABLE_IPV6 | 3724 #ifdef ENABLE_IPV6 |
| 3725 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; | 3725 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
| 3726 #else | 3726 #else |
| 3727 char ip[INET_ADDRSTRLEN + 1]; | 3727 char ip[INET_ADDRSTRLEN + 1]; |
| 3728 #endif | 3728 #endif |
| 3729 | 3729 |
| 3730 /* Guarantee NUL-termination for PyUnicode_FromString() below */ | 3730 /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
| 3731 memset((void *) &ip[0], '\0', sizeof(ip)); | 3731 memset((void *) &ip[0], '\0', sizeof(ip)); |
| 3732 | 3732 |
| 3733 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { | 3733 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) { |
| 3734 return NULL; | 3734 return NULL; |
| 3735 } | 3735 } |
| 3736 | 3736 |
| 3737 if (af == AF_INET) { | 3737 if (af == AF_INET) { |
| 3738 if (len != sizeof(struct in_addr)) { | 3738 if (len != sizeof(struct in_addr)) { |
| 3739 PyErr_SetString(PyExc_ValueError, | 3739 PyErr_SetString(PyExc_ValueError, |
| 3740 "invalid length of packed IP address string"); | 3740 "invalid length of packed IP address string"); |
| 3741 return NULL; | 3741 return NULL; |
| 3742 } | 3742 } |
| 3743 #ifdef ENABLE_IPV6 | 3743 #ifdef ENABLE_IPV6 |
| 3744 } else if (af == AF_INET6) { | 3744 } else if (af == AF_INET6) { |
| 3745 if (len != sizeof(struct in6_addr)) { | 3745 if (len != sizeof(struct in6_addr)) { |
| 3746 PyErr_SetString(PyExc_ValueError, | 3746 PyErr_SetString(PyExc_ValueError, |
| 3747 "invalid length of packed IP address string"); | 3747 "invalid length of packed IP address string"); |
| 3748 return NULL; | 3748 return NULL; |
| 3749 } | 3749 } |
| 3750 #endif | 3750 #endif |
| 3751 } else { | 3751 } else { |
| 3752 PyErr_Format(PyExc_ValueError, | 3752 PyErr_Format(PyExc_ValueError, |
| 3753 "unknown address family %d", af); | 3753 "unknown address family %d", af); |
| 3754 return NULL; | 3754 return NULL; |
| 3755 } | 3755 } |
| 3756 | 3756 |
| 3757 retval = inet_ntop(af, packed, ip, sizeof(ip)); | 3757 retval = inet_ntop(af, packed, ip, sizeof(ip)); |
| 3758 if (!retval) { | 3758 if (!retval) { |
| 3759 PyErr_SetFromErrno(socket_error); | 3759 PyErr_SetFromErrno(socket_error); |
| 3760 return NULL; | 3760 return NULL; |
| 3761 } else { | 3761 } else { |
| 3762 return PyUnicode_FromString(retval); | 3762 return PyUnicode_FromString(retval); |
| 3763 } | 3763 } |
| 3764 | 3764 |
| 3765 /* NOTREACHED */ | 3765 /* NOTREACHED */ |
| 3766 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); | 3766 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); |
| 3767 return NULL; | 3767 return NULL; |
| 3768 } | 3768 } |
| 3769 | 3769 |
| 3770 #endif /* HAVE_INET_PTON */ | 3770 #endif /* HAVE_INET_PTON */ |
| 3771 | 3771 |
| 3772 /* Python interface to getaddrinfo(host, port). */ | 3772 /* Python interface to getaddrinfo(host, port). */ |
| 3773 | 3773 |
| 3774 /*ARGSUSED*/ | 3774 /*ARGSUSED*/ |
| 3775 static PyObject * | 3775 static PyObject * |
| 3776 socket_getaddrinfo(PyObject *self, PyObject *args) | 3776 socket_getaddrinfo(PyObject *self, PyObject *args) |
| 3777 { | 3777 { |
| 3778 struct addrinfo hints, *res; | 3778 struct addrinfo hints, *res; |
| 3779 struct addrinfo *res0 = NULL; | 3779 struct addrinfo *res0 = NULL; |
| 3780 PyObject *hobj = NULL; | 3780 PyObject *hobj = NULL; |
| 3781 PyObject *pobj = (PyObject *)NULL; | 3781 PyObject *pobj = (PyObject *)NULL; |
| 3782 char pbuf[30]; | 3782 char pbuf[30]; |
| 3783 char *hptr, *pptr; | 3783 char *hptr, *pptr; |
| 3784 int family, socktype, protocol, flags; | 3784 int family, socktype, protocol, flags; |
| 3785 int error; | 3785 int error; |
| 3786 PyObject *all = (PyObject *)NULL; | 3786 PyObject *all = (PyObject *)NULL; |
| 3787 PyObject *idna = NULL; | 3787 PyObject *idna = NULL; |
| 3788 | 3788 |
| 3789 family = socktype = protocol = flags = 0; | 3789 family = socktype = protocol = flags = 0; |
| 3790 family = AF_UNSPEC; | 3790 family = AF_UNSPEC; |
| 3791 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", | 3791 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", |
| 3792 &hobj, &pobj, &family, &socktype, | 3792 &hobj, &pobj, &family, &socktype, |
| 3793 &protocol, &flags)) { | 3793 &protocol, &flags)) { |
| 3794 return NULL; | 3794 return NULL; |
| 3795 } | 3795 } |
| 3796 if (hobj == Py_None) { | 3796 if (hobj == Py_None) { |
| 3797 hptr = NULL; | 3797 hptr = NULL; |
| 3798 } else if (PyUnicode_Check(hobj)) { | 3798 } else if (PyUnicode_Check(hobj)) { |
| 3799 idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); | 3799 idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); |
| 3800 if (!idna) | 3800 if (!idna) |
| 3801 return NULL; | 3801 return NULL; |
| 3802 assert(PyBytes_Check(idna)); | 3802 assert(PyBytes_Check(idna)); |
| 3803 hptr = PyBytes_AS_STRING(idna); | 3803 hptr = PyBytes_AS_STRING(idna); |
| 3804 } else if (PyBytes_Check(hobj)) { | 3804 } else if (PyBytes_Check(hobj)) { |
| 3805 hptr = PyBytes_AsString(hobj); | 3805 hptr = PyBytes_AsString(hobj); |
| 3806 } else { | 3806 } else { |
| 3807 PyErr_SetString(PyExc_TypeError, | 3807 PyErr_SetString(PyExc_TypeError, |
| 3808 "getaddrinfo() argument 1 must be string or None"); | 3808 "getaddrinfo() argument 1 must be string or None"); |
| 3809 return NULL; | 3809 return NULL; |
| 3810 } | 3810 } |
| 3811 if (PyLong_CheckExact(pobj)) { | 3811 if (PyLong_CheckExact(pobj)) { |
| 3812 long value = PyLong_AsLong(pobj); | 3812 long value = PyLong_AsLong(pobj); |
| 3813 if (value == -1 && PyErr_Occurred()) | 3813 if (value == -1 && PyErr_Occurred()) |
| 3814 goto err; | 3814 goto err; |
| 3815 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); | 3815 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
| 3816 pptr = pbuf; | 3816 pptr = pbuf; |
| 3817 } else if (PyUnicode_Check(pobj)) { | 3817 } else if (PyUnicode_Check(pobj)) { |
| 3818 pptr = _PyUnicode_AsString(pobj); | 3818 pptr = _PyUnicode_AsString(pobj); |
| 3819 } else if (PyBytes_Check(pobj)) { | 3819 } else if (PyBytes_Check(pobj)) { |
| 3820 pptr = PyBytes_AsString(pobj); | 3820 pptr = PyBytes_AsString(pobj); |
| 3821 } else if (pobj == Py_None) { | 3821 } else if (pobj == Py_None) { |
| 3822 pptr = (char *)NULL; | 3822 pptr = (char *)NULL; |
| 3823 } else { | 3823 } else { |
| 3824 PyErr_SetString(socket_error, "Int or String expected"); | 3824 PyErr_SetString(socket_error, "Int or String expected"); |
| 3825 goto err; | 3825 goto err; |
| 3826 } | 3826 } |
| 3827 memset(&hints, 0, sizeof(hints)); | 3827 memset(&hints, 0, sizeof(hints)); |
| 3828 hints.ai_family = family; | 3828 hints.ai_family = family; |
| 3829 hints.ai_socktype = socktype; | 3829 hints.ai_socktype = socktype; |
| 3830 hints.ai_protocol = protocol; | 3830 hints.ai_protocol = protocol; |
| 3831 hints.ai_flags = flags; | 3831 hints.ai_flags = flags; |
| 3832 Py_BEGIN_ALLOW_THREADS | 3832 Py_BEGIN_ALLOW_THREADS |
| 3833 ACQUIRE_GETADDRINFO_LOCK | 3833 ACQUIRE_GETADDRINFO_LOCK |
| 3834 error = getaddrinfo(hptr, pptr, &hints, &res0); | 3834 error = getaddrinfo(hptr, pptr, &hints, &res0); |
| 3835 Py_END_ALLOW_THREADS | 3835 Py_END_ALLOW_THREADS |
| 3836 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ | 3836 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 3837 if (error) { | 3837 if (error) { |
| 3838 set_gaierror(error); | 3838 set_gaierror(error); |
| 3839 goto err; | 3839 goto err; |
| 3840 } | 3840 } |
| 3841 | 3841 |
| 3842 if ((all = PyList_New(0)) == NULL) | 3842 if ((all = PyList_New(0)) == NULL) |
| 3843 goto err; | 3843 goto err; |
| 3844 for (res = res0; res; res = res->ai_next) { | 3844 for (res = res0; res; res = res->ai_next) { |
| 3845 PyObject *single; | 3845 PyObject *single; |
| 3846 PyObject *addr = | 3846 PyObject *addr = |
| 3847 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); | 3847 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
| 3848 if (addr == NULL) | 3848 if (addr == NULL) |
| 3849 goto err; | 3849 goto err; |
| 3850 single = Py_BuildValue("iiisO", res->ai_family, | 3850 single = Py_BuildValue("iiisO", res->ai_family, |
| 3851 res->ai_socktype, res->ai_protocol, | 3851 res->ai_socktype, res->ai_protocol, |
| 3852 res->ai_canonname ? res->ai_canonname : "", | 3852 res->ai_canonname ? res->ai_canonname : "", |
| 3853 addr); | 3853 addr); |
| 3854 Py_DECREF(addr); | 3854 Py_DECREF(addr); |
| 3855 if (single == NULL) | 3855 if (single == NULL) |
| 3856 goto err; | 3856 goto err; |
| 3857 | 3857 |
| 3858 if (PyList_Append(all, single)) | 3858 if (PyList_Append(all, single)) |
| 3859 goto err; | 3859 goto err; |
| 3860 Py_XDECREF(single); | 3860 Py_XDECREF(single); |
| 3861 } | 3861 } |
| 3862 Py_XDECREF(idna); | 3862 Py_XDECREF(idna); |
| 3863 if (res0) | 3863 if (res0) |
| 3864 freeaddrinfo(res0); | 3864 freeaddrinfo(res0); |
| 3865 return all; | 3865 return all; |
| 3866 err: | 3866 err: |
| 3867 Py_XDECREF(all); | 3867 Py_XDECREF(all); |
| 3868 Py_XDECREF(idna); | 3868 Py_XDECREF(idna); |
| 3869 if (res0) | 3869 if (res0) |
| 3870 freeaddrinfo(res0); | 3870 freeaddrinfo(res0); |
| 3871 return (PyObject *)NULL; | 3871 return (PyObject *)NULL; |
| 3872 } | 3872 } |
| 3873 | 3873 |
| 3874 PyDoc_STRVAR(getaddrinfo_doc, | 3874 PyDoc_STRVAR(getaddrinfo_doc, |
| 3875 "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ | 3875 "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ |
| 3876 -> list of (family, socktype, proto, canonname, sockaddr)\n\ | 3876 -> list of (family, socktype, proto, canonname, sockaddr)\n\ |
| 3881 | 3881 |
| 3882 /*ARGSUSED*/ | 3882 /*ARGSUSED*/ |
| 3883 static PyObject * | 3883 static PyObject * |
| 3884 socket_getnameinfo(PyObject *self, PyObject *args) | 3884 socket_getnameinfo(PyObject *self, PyObject *args) |
| 3885 { | 3885 { |
| 3886 PyObject *sa = (PyObject *)NULL; | 3886 PyObject *sa = (PyObject *)NULL; |
| 3887 int flags; | 3887 int flags; |
| 3888 char *hostp; | 3888 char *hostp; |
| 3889 int port, flowinfo, scope_id; | 3889 int port, flowinfo, scope_id; |
| 3890 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; | 3890 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
| 3891 struct addrinfo hints, *res = NULL; | 3891 struct addrinfo hints, *res = NULL; |
| 3892 int error; | 3892 int error; |
| 3893 PyObject *ret = (PyObject *)NULL; | 3893 PyObject *ret = (PyObject *)NULL; |
| 3894 | 3894 |
| 3895 flags = flowinfo = scope_id = 0; | 3895 flags = flowinfo = scope_id = 0; |
| 3896 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) | 3896 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
| 3897 return NULL; | 3897 return NULL; |
| 3898 if (!PyTuple_Check(sa)) { | 3898 if (!PyTuple_Check(sa)) { |
| 3899 PyErr_SetString(PyExc_TypeError, | 3899 PyErr_SetString(PyExc_TypeError, |
| 3900 "getnameinfo() argument 1 must be a tuple"); | 3900 "getnameinfo() argument 1 must be a tuple"); |
| 3901 return NULL; | 3901 return NULL; |
| 3902 } | 3902 } |
| 3903 if (!PyArg_ParseTuple(sa, "si|ii", | 3903 if (!PyArg_ParseTuple(sa, "si|ii", |
| 3904 &hostp, &port, &flowinfo, &scope_id)) | 3904 &hostp, &port, &flowinfo, &scope_id)) |
| 3905 return NULL; | 3905 return NULL; |
| 3906 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); | 3906 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
| 3907 memset(&hints, 0, sizeof(hints)); | 3907 memset(&hints, 0, sizeof(hints)); |
| 3908 hints.ai_family = AF_UNSPEC; | 3908 hints.ai_family = AF_UNSPEC; |
| 3909 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ | 3909 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
| 3910 Py_BEGIN_ALLOW_THREADS | 3910 Py_BEGIN_ALLOW_THREADS |
| 3911 ACQUIRE_GETADDRINFO_LOCK | 3911 ACQUIRE_GETADDRINFO_LOCK |
| 3912 error = getaddrinfo(hostp, pbuf, &hints, &res); | 3912 error = getaddrinfo(hostp, pbuf, &hints, &res); |
| 3913 Py_END_ALLOW_THREADS | 3913 Py_END_ALLOW_THREADS |
| 3914 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ | 3914 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
| 3915 if (error) { | 3915 if (error) { |
| 3916 set_gaierror(error); | 3916 set_gaierror(error); |
| 3917 goto fail; | 3917 goto fail; |
| 3918 } | 3918 } |
| 3919 if (res->ai_next) { | 3919 if (res->ai_next) { |
| 3920 PyErr_SetString(socket_error, | 3920 PyErr_SetString(socket_error, |
| 3921 "sockaddr resolved to multiple addresses"); | 3921 "sockaddr resolved to multiple addresses"); |
| 3922 goto fail; | 3922 goto fail; |
| 3923 } | 3923 } |
| 3924 switch (res->ai_family) { | 3924 switch (res->ai_family) { |
| 3925 case AF_INET: | 3925 case AF_INET: |
| 3926 { | 3926 { |
| 3927 if (PyTuple_GET_SIZE(sa) != 2) { | 3927 if (PyTuple_GET_SIZE(sa) != 2) { |
| 3928 PyErr_SetString(socket_error, | 3928 PyErr_SetString(socket_error, |
| 3929 "IPv4 sockaddr must be 2 tuple"); | 3929 "IPv4 sockaddr must be 2 tuple"); |
| 3930 goto fail; | 3930 goto fail; |
| 3931 } | 3931 } |
| 3932 break; | 3932 break; |
| 3933 } | 3933 } |
| 3934 #ifdef ENABLE_IPV6 | 3934 #ifdef ENABLE_IPV6 |
| 3935 case AF_INET6: | 3935 case AF_INET6: |
| 3936 { | 3936 { |
| 3937 struct sockaddr_in6 *sin6; | 3937 struct sockaddr_in6 *sin6; |
| 3938 sin6 = (struct sockaddr_in6 *)res->ai_addr; | 3938 sin6 = (struct sockaddr_in6 *)res->ai_addr; |
| 3939 sin6->sin6_flowinfo = flowinfo; | 3939 sin6->sin6_flowinfo = flowinfo; |
| 3940 sin6->sin6_scope_id = scope_id; | 3940 sin6->sin6_scope_id = scope_id; |
| 3941 break; | 3941 break; |
| 3942 } | 3942 } |
| 3943 #endif | 3943 #endif |
| 3944 } | 3944 } |
| 3945 error = getnameinfo(res->ai_addr, res->ai_addrlen, | 3945 error = getnameinfo(res->ai_addr, res->ai_addrlen, |
| 3946 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); | 3946 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
| 3947 if (error) { | 3947 if (error) { |
| 3948 set_gaierror(error); | 3948 set_gaierror(error); |
| 3949 goto fail; | 3949 goto fail; |
| 3950 } | 3950 } |
| 3951 ret = Py_BuildValue("ss", hbuf, pbuf); | 3951 ret = Py_BuildValue("ss", hbuf, pbuf); |
| 3952 | 3952 |
| 3953 fail: | 3953 fail: |
| 3954 if (res) | 3954 if (res) |
| 3955 freeaddrinfo(res); | 3955 freeaddrinfo(res); |
| 3956 return ret; | 3956 return ret; |
| 3957 } | 3957 } |
| 3958 | 3958 |
| 3959 PyDoc_STRVAR(getnameinfo_doc, | 3959 PyDoc_STRVAR(getnameinfo_doc, |
| 3960 "getnameinfo(sockaddr, flags) --> (host, port)\n\ | 3960 "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
| 3961 \n\ | 3961 \n\ |
| 3965 /* Python API to getting and setting the default timeout value. */ | 3965 /* Python API to getting and setting the default timeout value. */ |
| 3966 | 3966 |
| 3967 static PyObject * | 3967 static PyObject * |
| 3968 socket_getdefaulttimeout(PyObject *self) | 3968 socket_getdefaulttimeout(PyObject *self) |
| 3969 { | 3969 { |
| 3970 if (defaulttimeout < 0.0) { | 3970 if (defaulttimeout < 0.0) { |
| 3971 Py_INCREF(Py_None); | 3971 Py_INCREF(Py_None); |
| 3972 return Py_None; | 3972 return Py_None; |
| 3973 } | 3973 } |
| 3974 else | 3974 else |
| 3975 return PyFloat_FromDouble(defaulttimeout); | 3975 return PyFloat_FromDouble(defaulttimeout); |
| 3976 } | 3976 } |
| 3977 | 3977 |
| 3978 PyDoc_STRVAR(getdefaulttimeout_doc, | 3978 PyDoc_STRVAR(getdefaulttimeout_doc, |
| 3979 "getdefaulttimeout() -> timeout\n\ | 3979 "getdefaulttimeout() -> timeout\n\ |
| 3980 \n\ | 3980 \n\ |
| 3983 When the socket module is first imported, the default is None."); | 3983 When the socket module is first imported, the default is None."); |
| 3984 | 3984 |
| 3985 static PyObject * | 3985 static PyObject * |
| 3986 socket_setdefaulttimeout(PyObject *self, PyObject *arg) | 3986 socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
| 3987 { | 3987 { |
| 3988 double timeout; | 3988 double timeout; |
| 3989 | 3989 |
| 3990 if (arg == Py_None) | 3990 if (arg == Py_None) |
| 3991 timeout = -1.0; | 3991 timeout = -1.0; |
| 3992 else { | 3992 else { |
| 3993 timeout = PyFloat_AsDouble(arg); | 3993 timeout = PyFloat_AsDouble(arg); |
| 3994 if (timeout < 0.0) { | 3994 if (timeout < 0.0) { |
| 3995 if (!PyErr_Occurred()) | 3995 if (!PyErr_Occurred()) |
| 3996 PyErr_SetString(PyExc_ValueError, | 3996 PyErr_SetString(PyExc_ValueError, |
| 3997 "Timeout value out of range"); | 3997 "Timeout value out of range"); |
| 3998 return NULL; | 3998 return NULL; |
| 3999 } | 3999 } |
| 4000 } | 4000 } |
| 4001 | 4001 |
| 4002 defaulttimeout = timeout; | 4002 defaulttimeout = timeout; |
| 4003 | 4003 |
| 4004 Py_INCREF(Py_None); | 4004 Py_INCREF(Py_None); |
| 4005 return Py_None; | 4005 return Py_None; |
| 4006 } | 4006 } |
| 4007 | 4007 |
| 4008 PyDoc_STRVAR(setdefaulttimeout_doc, | 4008 PyDoc_STRVAR(setdefaulttimeout_doc, |
| 4009 "setdefaulttimeout(timeout)\n\ | 4009 "setdefaulttimeout(timeout)\n\ |
| 4010 \n\ | 4010 \n\ |
| 4014 | 4014 |
| 4015 | 4015 |
| 4016 /* List of functions exported by this module. */ | 4016 /* List of functions exported by this module. */ |
| 4017 | 4017 |
| 4018 static PyMethodDef socket_methods[] = { | 4018 static PyMethodDef socket_methods[] = { |
| 4019 {"gethostbyname", socket_gethostbyname, | 4019 {"gethostbyname", socket_gethostbyname, |
| 4020 METH_VARARGS, gethostbyname_doc}, | 4020 METH_VARARGS, gethostbyname_doc}, |
| 4021 {"gethostbyname_ex", socket_gethostbyname_ex, | 4021 {"gethostbyname_ex", socket_gethostbyname_ex, |
| 4022 METH_VARARGS, ghbn_ex_doc}, | 4022 METH_VARARGS, ghbn_ex_doc}, |
| 4023 {"gethostbyaddr", socket_gethostbyaddr, | 4023 {"gethostbyaddr", socket_gethostbyaddr, |
| 4024 METH_VARARGS, gethostbyaddr_doc}, | 4024 METH_VARARGS, gethostbyaddr_doc}, |
| 4025 {"gethostname", socket_gethostname, | 4025 {"gethostname", socket_gethostname, |
| 4026 METH_NOARGS, gethostname_doc}, | 4026 METH_NOARGS, gethostname_doc}, |
| 4027 {"getservbyname", socket_getservbyname, | 4027 {"getservbyname", socket_getservbyname, |
| 4028 METH_VARARGS, getservbyname_doc}, | 4028 METH_VARARGS, getservbyname_doc}, |
| 4029 {"getservbyport", socket_getservbyport, | 4029 {"getservbyport", socket_getservbyport, |
| 4030 METH_VARARGS, getservbyport_doc}, | 4030 METH_VARARGS, getservbyport_doc}, |
| 4031 {"getprotobyname", socket_getprotobyname, | 4031 {"getprotobyname", socket_getprotobyname, |
| 4032 METH_VARARGS, getprotobyname_doc}, | 4032 METH_VARARGS, getprotobyname_doc}, |
| 4033 #ifndef NO_DUP | 4033 #ifndef NO_DUP |
| 4034 {"dup", socket_dup, | 4034 {"dup", socket_dup, |
| 4035 METH_O, dup_doc}, | 4035 METH_O, dup_doc}, |
| 4036 #endif | 4036 #endif |
| 4037 #ifdef HAVE_SOCKETPAIR | 4037 #ifdef HAVE_SOCKETPAIR |
| 4038 {"socketpair", socket_socketpair, | 4038 {"socketpair", socket_socketpair, |
| 4039 METH_VARARGS, socketpair_doc}, | 4039 METH_VARARGS, socketpair_doc}, |
| 4040 #endif | 4040 #endif |
| 4041 {"ntohs", socket_ntohs, | 4041 {"ntohs", socket_ntohs, |
| 4042 METH_VARARGS, ntohs_doc}, | 4042 METH_VARARGS, ntohs_doc}, |
| 4043 {"ntohl", socket_ntohl, | 4043 {"ntohl", socket_ntohl, |
| 4044 METH_O, ntohl_doc}, | 4044 METH_O, ntohl_doc}, |
| 4045 {"htons", socket_htons, | 4045 {"htons", socket_htons, |
| 4046 METH_VARARGS, htons_doc}, | 4046 METH_VARARGS, htons_doc}, |
| 4047 {"htonl", socket_htonl, | 4047 {"htonl", socket_htonl, |
| 4048 METH_O, htonl_doc}, | 4048 METH_O, htonl_doc}, |
| 4049 {"inet_aton", socket_inet_aton, | 4049 {"inet_aton", socket_inet_aton, |
| 4050 METH_VARARGS, inet_aton_doc}, | 4050 METH_VARARGS, inet_aton_doc}, |
| 4051 {"inet_ntoa", socket_inet_ntoa, | 4051 {"inet_ntoa", socket_inet_ntoa, |
| 4052 METH_VARARGS, inet_ntoa_doc}, | 4052 METH_VARARGS, inet_ntoa_doc}, |
| 4053 #ifdef HAVE_INET_PTON | 4053 #ifdef HAVE_INET_PTON |
| 4054 {"inet_pton", socket_inet_pton, | 4054 {"inet_pton", socket_inet_pton, |
| 4055 METH_VARARGS, inet_pton_doc}, | 4055 METH_VARARGS, inet_pton_doc}, |
| 4056 {"inet_ntop", socket_inet_ntop, | 4056 {"inet_ntop", socket_inet_ntop, |
| 4057 METH_VARARGS, inet_ntop_doc}, | 4057 METH_VARARGS, inet_ntop_doc}, |
| 4058 #endif | 4058 #endif |
| 4059 {"getaddrinfo", socket_getaddrinfo, | 4059 {"getaddrinfo", socket_getaddrinfo, |
| 4060 METH_VARARGS, getaddrinfo_doc}, | 4060 METH_VARARGS, getaddrinfo_doc}, |
| 4061 {"getnameinfo", socket_getnameinfo, | 4061 {"getnameinfo", socket_getnameinfo, |
| 4062 METH_VARARGS, getnameinfo_doc}, | 4062 METH_VARARGS, getnameinfo_doc}, |
| 4063 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, | 4063 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
| 4064 METH_NOARGS, getdefaulttimeout_doc}, | 4064 METH_NOARGS, getdefaulttimeout_doc}, |
| 4065 {"setdefaulttimeout", socket_setdefaulttimeout, | 4065 {"setdefaulttimeout", socket_setdefaulttimeout, |
| 4066 METH_O, setdefaulttimeout_doc}, | 4066 METH_O, setdefaulttimeout_doc}, |
| 4067 {NULL, NULL} /* Sentinel */ | 4067 {NULL, NULL} /* Sentinel */ |
| 4068 }; | 4068 }; |
| 4069 | 4069 |
| 4070 | 4070 |
| 4071 #ifdef MS_WINDOWS | 4071 #ifdef MS_WINDOWS |
| 4072 #define OS_INIT_DEFINED | 4072 #define OS_INIT_DEFINED |
| 4074 /* Additional initialization and cleanup for Windows */ | 4074 /* Additional initialization and cleanup for Windows */ |
| 4075 | 4075 |
| 4076 static void | 4076 static void |
| 4077 os_cleanup(void) | 4077 os_cleanup(void) |
| 4078 { | 4078 { |
| 4079 WSACleanup(); | 4079 WSACleanup(); |
| 4080 } | 4080 } |
| 4081 | 4081 |
| 4082 static int | 4082 static int |
| 4083 os_init(void) | 4083 os_init(void) |
| 4084 { | 4084 { |
| 4085 WSADATA WSAData; | 4085 WSADATA WSAData; |
| 4086 int ret; | 4086 int ret; |
| 4087 ret = WSAStartup(0x0101, &WSAData); | 4087 ret = WSAStartup(0x0101, &WSAData); |
| 4088 switch (ret) { | 4088 switch (ret) { |
| 4089 case 0: /* No error */ | 4089 case 0: /* No error */ |
| 4090 Py_AtExit(os_cleanup); | 4090 Py_AtExit(os_cleanup); |
| 4091 return 1; /* Success */ | 4091 return 1; /* Success */ |
| 4092 case WSASYSNOTREADY: | 4092 case WSASYSNOTREADY: |
| 4093 PyErr_SetString(PyExc_ImportError, | 4093 PyErr_SetString(PyExc_ImportError, |
| 4094 "WSAStartup failed: network not ready"); | 4094 "WSAStartup failed: network not ready"); |
| 4095 break; | 4095 break; |
| 4096 case WSAVERNOTSUPPORTED: | 4096 case WSAVERNOTSUPPORTED: |
| 4097 case WSAEINVAL: | 4097 case WSAEINVAL: |
| 4098 PyErr_SetString( | 4098 PyErr_SetString( |
| 4099 PyExc_ImportError, | 4099 PyExc_ImportError, |
| 4100 "WSAStartup failed: requested version not supported"); | 4100 "WSAStartup failed: requested version not supported"); |
| 4101 break; | 4101 break; |
| 4102 default: | 4102 default: |
| 4103 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); | 4103 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
| 4104 break; | 4104 break; |
| 4105 } | 4105 } |
| 4106 return 0; /* Failure */ | 4106 return 0; /* Failure */ |
| 4107 } | 4107 } |
| 4108 | 4108 |
| 4109 #endif /* MS_WINDOWS */ | 4109 #endif /* MS_WINDOWS */ |
| 4110 | 4110 |
| 4111 | 4111 |
| 4116 | 4116 |
| 4117 static int | 4117 static int |
| 4118 os_init(void) | 4118 os_init(void) |
| 4119 { | 4119 { |
| 4120 #ifndef PYCC_GCC | 4120 #ifndef PYCC_GCC |
| 4121 int rc = sock_init(); | 4121 int rc = sock_init(); |
| 4122 | 4122 |
| 4123 if (rc == 0) { | 4123 if (rc == 0) { |
| 4124 return 1; /* Success */ | 4124 return 1; /* Success */ |
| 4125 } | 4125 } |
| 4126 | 4126 |
| 4127 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno()); | 4127 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno()); |
| 4128 | 4128 |
| 4129 return 0; /* Failure */ | 4129 return 0; /* Failure */ |
| 4130 #else | 4130 #else |
| 4131 /* No need to initialise sockets with GCC/EMX */ | 4131 /* No need to initialise sockets with GCC/EMX */ |
| 4132 return 1; /* Success */ | 4132 return 1; /* Success */ |
| 4133 #endif | 4133 #endif |
| 4134 } | 4134 } |
| 4135 | 4135 |
| 4136 #endif /* PYOS_OS2 */ | 4136 #endif /* PYOS_OS2 */ |
| 4137 | 4137 |
| 4138 | 4138 |
| 4139 #ifndef OS_INIT_DEFINED | 4139 #ifndef OS_INIT_DEFINED |
| 4140 static int | 4140 static int |
| 4141 os_init(void) | 4141 os_init(void) |
| 4142 { | 4142 { |
| 4143 return 1; /* Success */ | 4143 return 1; /* Success */ |
| 4144 } | 4144 } |
| 4145 #endif | 4145 #endif |
| 4146 | 4146 |
| 4147 | 4147 |
| 4148 /* C API table - always add new things to the end for binary | 4148 /* C API table - always add new things to the end for binary |
| 4149 compatibility. */ | 4149 compatibility. */ |
| 4150 static | 4150 static |
| 4151 PySocketModule_APIObject PySocketModuleAPI = | 4151 PySocketModule_APIObject PySocketModuleAPI = |
| 4152 { | 4152 { |
| 4153 &sock_type, | 4153 &sock_type, |
| 4154 NULL | 4154 NULL |
| 4155 }; | 4155 }; |
| 4156 | 4156 |
| 4157 | 4157 |
| 4158 /* Initialize the _socket module. | 4158 /* Initialize the _socket module. |
| 4159 | 4159 |
| 4169 "Implementation module for socket operations.\n\ | 4169 "Implementation module for socket operations.\n\ |
| 4170 \n\ | 4170 \n\ |
| 4171 See the socket module for documentation."); | 4171 See the socket module for documentation."); |
| 4172 | 4172 |
| 4173 static struct PyModuleDef socketmodule = { | 4173 static struct PyModuleDef socketmodule = { |
| 4174 PyModuleDef_HEAD_INIT, | 4174 PyModuleDef_HEAD_INIT, |
| 4175 PySocket_MODULE_NAME, | 4175 PySocket_MODULE_NAME, |
| 4176 socket_doc, | 4176 socket_doc, |
| 4177 -1, | 4177 -1, |
| 4178 socket_methods, | 4178 socket_methods, |
| 4179 NULL, | 4179 NULL, |
| 4180 NULL, | 4180 NULL, |
| 4181 NULL, | 4181 NULL, |
| 4182 NULL | 4182 NULL |
| 4183 }; | 4183 }; |
| 4184 | 4184 |
| 4185 PyMODINIT_FUNC | 4185 PyMODINIT_FUNC |
| 4186 PyInit__socket(void) | 4186 PyInit__socket(void) |
| 4187 { | 4187 { |
| 4188 PyObject *m, *has_ipv6; | 4188 PyObject *m, *has_ipv6; |
| 4189 | 4189 |
| 4190 if (!os_init()) | 4190 if (!os_init()) |
| 4191 return NULL; | 4191 return NULL; |
| 4192 | 4192 |
| 4193 Py_TYPE(&sock_type) = &PyType_Type; | 4193 Py_TYPE(&sock_type) = &PyType_Type; |
| 4194 m = PyModule_Create(&socketmodule); | 4194 m = PyModule_Create(&socketmodule); |
| 4195 if (m == NULL) | 4195 if (m == NULL) |
| 4196 return NULL; | 4196 return NULL; |
| 4197 | 4197 |
| 4198 socket_error = PyErr_NewException("socket.error", | 4198 socket_error = PyErr_NewException("socket.error", |
| 4199 PyExc_IOError, NULL); | 4199 PyExc_IOError, NULL); |
| 4200 if (socket_error == NULL) | 4200 if (socket_error == NULL) |
| 4201 return NULL; | 4201 return NULL; |
| 4202 PySocketModuleAPI.error = socket_error; | 4202 PySocketModuleAPI.error = socket_error; |
| 4203 Py_INCREF(socket_error); | 4203 Py_INCREF(socket_error); |
| 4204 PyModule_AddObject(m, "error", socket_error); | 4204 PyModule_AddObject(m, "error", socket_error); |
| 4205 socket_herror = PyErr_NewException("socket.herror", | 4205 socket_herror = PyErr_NewException("socket.herror", |
| 4206 socket_error, NULL); | 4206 socket_error, NULL); |
| 4207 if (socket_herror == NULL) | 4207 if (socket_herror == NULL) |
| 4208 return NULL; | 4208 return NULL; |
| 4209 Py_INCREF(socket_herror); | 4209 Py_INCREF(socket_herror); |
| 4210 PyModule_AddObject(m, "herror", socket_herror); | 4210 PyModule_AddObject(m, "herror", socket_herror); |
| 4211 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, | 4211 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error, |
| 4212 NULL); | 4212 NULL); |
| 4213 if (socket_gaierror == NULL) | 4213 if (socket_gaierror == NULL) |
| 4214 return NULL; | 4214 return NULL; |
| 4215 Py_INCREF(socket_gaierror); | 4215 Py_INCREF(socket_gaierror); |
| 4216 PyModule_AddObject(m, "gaierror", socket_gaierror); | 4216 PyModule_AddObject(m, "gaierror", socket_gaierror); |
| 4217 socket_timeout = PyErr_NewException("socket.timeout", | 4217 socket_timeout = PyErr_NewException("socket.timeout", |
| 4218 socket_error, NULL); | 4218 socket_error, NULL); |
| 4219 if (socket_timeout == NULL) | 4219 if (socket_timeout == NULL) |
| 4220 return NULL; | 4220 return NULL; |
| 4221 Py_INCREF(socket_timeout); | 4221 Py_INCREF(socket_timeout); |
| 4222 PyModule_AddObject(m, "timeout", socket_timeout); | 4222 PyModule_AddObject(m, "timeout", socket_timeout); |
| 4223 Py_INCREF((PyObject *)&sock_type); | 4223 Py_INCREF((PyObject *)&sock_type); |
| 4224 if (PyModule_AddObject(m, "SocketType", | 4224 if (PyModule_AddObject(m, "SocketType", |
| 4225 (PyObject *)&sock_type) != 0) | 4225 (PyObject *)&sock_type) != 0) |
| 4226 return NULL; | 4226 return NULL; |
| 4227 Py_INCREF((PyObject *)&sock_type); | 4227 Py_INCREF((PyObject *)&sock_type); |
| 4228 if (PyModule_AddObject(m, "socket", | 4228 if (PyModule_AddObject(m, "socket", |
| 4229 (PyObject *)&sock_type) != 0) | 4229 (PyObject *)&sock_type) != 0) |
| 4230 return NULL; | 4230 return NULL; |
| 4231 | 4231 |
| 4232 #ifdef ENABLE_IPV6 | 4232 #ifdef ENABLE_IPV6 |
| 4233 has_ipv6 = Py_True; | 4233 has_ipv6 = Py_True; |
| 4234 #else | 4234 #else |
| 4235 has_ipv6 = Py_False; | 4235 has_ipv6 = Py_False; |
| 4236 #endif | 4236 #endif |
| 4237 Py_INCREF(has_ipv6); | 4237 Py_INCREF(has_ipv6); |
| 4238 PyModule_AddObject(m, "has_ipv6", has_ipv6); | 4238 PyModule_AddObject(m, "has_ipv6", has_ipv6); |
| 4239 | 4239 |
| 4240 /* Export C API */ | 4240 /* Export C API */ |
| 4241 if (PyModule_AddObject(m, PySocket_CAPI_NAME, | 4241 if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
| 4242 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) | 4242 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
| 4243 ) != 0) | 4243 ) != 0) |
| 4244 return NULL; | 4244 return NULL; |
| 4245 | 4245 |
| 4246 /* Address families (we only support AF_INET and AF_UNIX) */ | 4246 /* Address families (we only support AF_INET and AF_UNIX) */ |
| 4247 #ifdef AF_UNSPEC | 4247 #ifdef AF_UNSPEC |
| 4248 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); | 4248 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC); |
| 4249 #endif | 4249 #endif |
| 4250 PyModule_AddIntConstant(m, "AF_INET", AF_INET); | 4250 PyModule_AddIntConstant(m, "AF_INET", AF_INET); |
| 4251 #ifdef AF_INET6 | 4251 #ifdef AF_INET6 |
| 4252 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); | 4252 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); |
| 4253 #endif /* AF_INET6 */ | 4253 #endif /* AF_INET6 */ |
| 4254 #if defined(AF_UNIX) | 4254 #if defined(AF_UNIX) |
| 4255 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); | 4255 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX); |
| 4256 #endif /* AF_UNIX */ | 4256 #endif /* AF_UNIX */ |
| 4257 #ifdef AF_AX25 | 4257 #ifdef AF_AX25 |
| 4258 /* Amateur Radio AX.25 */ | 4258 /* Amateur Radio AX.25 */ |
| 4259 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); | 4259 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25); |
| 4260 #endif | 4260 #endif |
| 4261 #ifdef AF_IPX | 4261 #ifdef AF_IPX |
| 4262 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ | 4262 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */ |
| 4263 #endif | 4263 #endif |
| 4264 #ifdef AF_APPLETALK | 4264 #ifdef AF_APPLETALK |
| 4265 /* Appletalk DDP */ | 4265 /* Appletalk DDP */ |
| 4266 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); | 4266 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK); |
| 4267 #endif | 4267 #endif |
| 4268 #ifdef AF_NETROM | 4268 #ifdef AF_NETROM |
| 4269 /* Amateur radio NetROM */ | 4269 /* Amateur radio NetROM */ |
| 4270 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); | 4270 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM); |
| 4271 #endif | 4271 #endif |
| 4272 #ifdef AF_BRIDGE | 4272 #ifdef AF_BRIDGE |
| 4273 /* Multiprotocol bridge */ | 4273 /* Multiprotocol bridge */ |
| 4274 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); | 4274 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE); |
| 4275 #endif | 4275 #endif |
| 4276 #ifdef AF_ATMPVC | 4276 #ifdef AF_ATMPVC |
| 4277 /* ATM PVCs */ | 4277 /* ATM PVCs */ |
| 4278 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); | 4278 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC); |
| 4279 #endif | 4279 #endif |
| 4280 #ifdef AF_AAL5 | 4280 #ifdef AF_AAL5 |
| 4281 /* Reserved for Werner's ATM */ | 4281 /* Reserved for Werner's ATM */ |
| 4282 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); | 4282 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5); |
| 4283 #endif | 4283 #endif |
| 4284 #ifdef AF_X25 | 4284 #ifdef AF_X25 |
| 4285 /* Reserved for X.25 project */ | 4285 /* Reserved for X.25 project */ |
| 4286 PyModule_AddIntConstant(m, "AF_X25", AF_X25); | 4286 PyModule_AddIntConstant(m, "AF_X25", AF_X25); |
| 4287 #endif | 4287 #endif |
| 4288 #ifdef AF_INET6 | 4288 #ifdef AF_INET6 |
| 4289 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ | 4289 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */ |
| 4290 #endif | 4290 #endif |
| 4291 #ifdef AF_ROSE | 4291 #ifdef AF_ROSE |
| 4292 /* Amateur Radio X.25 PLP */ | 4292 /* Amateur Radio X.25 PLP */ |
| 4293 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); | 4293 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE); |
| 4294 #endif | 4294 #endif |
| 4295 #ifdef AF_DECnet | 4295 #ifdef AF_DECnet |
| 4296 /* Reserved for DECnet project */ | 4296 /* Reserved for DECnet project */ |
| 4297 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); | 4297 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet); |
| 4298 #endif | 4298 #endif |
| 4299 #ifdef AF_NETBEUI | 4299 #ifdef AF_NETBEUI |
| 4300 /* Reserved for 802.2LLC project */ | 4300 /* Reserved for 802.2LLC project */ |
| 4301 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); | 4301 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI); |
| 4302 #endif | 4302 #endif |
| 4303 #ifdef AF_SECURITY | 4303 #ifdef AF_SECURITY |
| 4304 /* Security callback pseudo AF */ | 4304 /* Security callback pseudo AF */ |
| 4305 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); | 4305 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY); |
| 4306 #endif | 4306 #endif |
| 4307 #ifdef AF_KEY | 4307 #ifdef AF_KEY |
| 4308 /* PF_KEY key management API */ | 4308 /* PF_KEY key management API */ |
| 4309 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); | 4309 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY); |
| 4310 #endif | 4310 #endif |
| 4311 #ifdef AF_NETLINK | 4311 #ifdef AF_NETLINK |
| 4312 /* */ | 4312 /* */ |
| 4313 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); | 4313 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK); |
| 4314 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); | 4314 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE); |
| 4315 #ifdef NETLINK_SKIP | 4315 #ifdef NETLINK_SKIP |
| 4316 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); | 4316 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP); |
| 4317 #endif | 4317 #endif |
| 4318 #ifdef NETLINK_W1 | 4318 #ifdef NETLINK_W1 |
| 4319 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); | 4319 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1); |
| 4320 #endif | 4320 #endif |
| 4321 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); | 4321 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK); |
| 4322 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); | 4322 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL); |
| 4323 #ifdef NETLINK_TCPDIAG | 4323 #ifdef NETLINK_TCPDIAG |
| 4324 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); | 4324 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG); |
| 4325 #endif | 4325 #endif |
| 4326 #ifdef NETLINK_NFLOG | 4326 #ifdef NETLINK_NFLOG |
| 4327 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); | 4327 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG); |
| 4328 #endif | 4328 #endif |
| 4329 #ifdef NETLINK_XFRM | 4329 #ifdef NETLINK_XFRM |
| 4330 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); | 4330 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM); |
| 4331 #endif | 4331 #endif |
| 4332 #ifdef NETLINK_ARPD | 4332 #ifdef NETLINK_ARPD |
| 4333 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); | 4333 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD); |
| 4334 #endif | 4334 #endif |
| 4335 #ifdef NETLINK_ROUTE6 | 4335 #ifdef NETLINK_ROUTE6 |
| 4336 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); | 4336 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6); |
| 4337 #endif | 4337 #endif |
| 4338 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); | 4338 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW); |
| 4339 #ifdef NETLINK_DNRTMSG | 4339 #ifdef NETLINK_DNRTMSG |
| 4340 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); | 4340 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG); |
| 4341 #endif | 4341 #endif |
| 4342 #ifdef NETLINK_TAPBASE | 4342 #ifdef NETLINK_TAPBASE |
| 4343 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); | 4343 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE); |
| 4344 #endif | 4344 #endif |
| 4345 #endif /* AF_NETLINK */ | 4345 #endif /* AF_NETLINK */ |
| 4346 #ifdef AF_ROUTE | 4346 #ifdef AF_ROUTE |
| 4347 /* Alias to emulate 4.4BSD */ | 4347 /* Alias to emulate 4.4BSD */ |
| 4348 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); | 4348 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE); |
| 4349 #endif | 4349 #endif |
| 4350 #ifdef AF_ASH | 4350 #ifdef AF_ASH |
| 4351 /* Ash */ | 4351 /* Ash */ |
| 4352 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); | 4352 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH); |
| 4353 #endif | 4353 #endif |
| 4354 #ifdef AF_ECONET | 4354 #ifdef AF_ECONET |
| 4355 /* Acorn Econet */ | 4355 /* Acorn Econet */ |
| 4356 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); | 4356 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET); |
| 4357 #endif | 4357 #endif |
| 4358 #ifdef AF_ATMSVC | 4358 #ifdef AF_ATMSVC |
| 4359 /* ATM SVCs */ | 4359 /* ATM SVCs */ |
| 4360 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); | 4360 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC); |
| 4361 #endif | 4361 #endif |
| 4362 #ifdef AF_SNA | 4362 #ifdef AF_SNA |
| 4363 /* Linux SNA Project (nutters!) */ | 4363 /* Linux SNA Project (nutters!) */ |
| 4364 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); | 4364 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA); |
| 4365 #endif | 4365 #endif |
| 4366 #ifdef AF_IRDA | 4366 #ifdef AF_IRDA |
| 4367 /* IRDA sockets */ | 4367 /* IRDA sockets */ |
| 4368 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); | 4368 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA); |
| 4369 #endif | 4369 #endif |
| 4370 #ifdef AF_PPPOX | 4370 #ifdef AF_PPPOX |
| 4371 /* PPPoX sockets */ | 4371 /* PPPoX sockets */ |
| 4372 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); | 4372 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX); |
| 4373 #endif | 4373 #endif |
| 4374 #ifdef AF_WANPIPE | 4374 #ifdef AF_WANPIPE |
| 4375 /* Wanpipe API Sockets */ | 4375 /* Wanpipe API Sockets */ |
| 4376 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); | 4376 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE); |
| 4377 #endif | 4377 #endif |
| 4378 #ifdef AF_LLC | 4378 #ifdef AF_LLC |
| 4379 /* Linux LLC */ | 4379 /* Linux LLC */ |
| 4380 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); | 4380 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC); |
| 4381 #endif | 4381 #endif |
| 4382 | 4382 |
| 4383 #ifdef USE_BLUETOOTH | 4383 #ifdef USE_BLUETOOTH |
| 4384 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); | 4384 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH); |
| 4385 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); | 4385 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP); |
| 4386 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); | 4386 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI); |
| 4387 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); | 4387 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI); |
| 4388 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); | 4388 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER); |
| 4389 #if !defined(__FreeBSD__) | 4389 #if !defined(__FreeBSD__) |
| 4390 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); | 4390 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP); |
| 4391 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); | 4391 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR); |
| 4392 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); | 4392 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO); |
| 4393 #endif | 4393 #endif |
| 4394 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); | 4394 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM); |
| 4395 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); | 4395 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
| 4396 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF"); | 4396 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF"); |
| 4397 #endif | 4397 #endif |
| 4398 | 4398 |
| 4399 #ifdef HAVE_NETPACKET_PACKET_H | 4399 #ifdef HAVE_NETPACKET_PACKET_H |
| 4400 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); | 4400 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET); |
| 4401 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); | 4401 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET); |
| 4402 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); | 4402 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST); |
| 4403 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); | 4403 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST); |
| 4404 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); | 4404 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST); |
| 4405 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); | 4405 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST); |
| 4406 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); | 4406 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING); |
| 4407 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); | 4407 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK); |
| 4408 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); | 4408 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE); |
| 4409 #endif | 4409 #endif |
| 4410 | 4410 |
| 4411 #ifdef HAVE_LINUX_TIPC_H | 4411 #ifdef HAVE_LINUX_TIPC_H |
| 4412 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); | 4412 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC); |
| 4413 | 4413 |
| 4414 /* for addresses */ | 4414 /* for addresses */ |
| 4415 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); | 4415 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ); |
| 4416 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); | 4416 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME); |
| 4417 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); | 4417 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID); |
| 4418 | 4418 |
| 4419 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); | 4419 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE); |
| 4420 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); | 4420 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE); |
| 4421 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); | 4421 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE); |
| 4422 | 4422 |
| 4423 /* for setsockopt() */ | 4423 /* for setsockopt() */ |
| 4424 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); | 4424 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC); |
| 4425 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); | 4425 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE); |
| 4426 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); | 4426 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE); |
| 4427 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", | 4427 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE", |
| 4428 TIPC_DEST_DROPPABLE); | 4428 TIPC_DEST_DROPPABLE); |
| 4429 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); | 4429 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT); |
| 4430 | 4430 |
| 4431 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", | 4431 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE", |
| 4432 TIPC_LOW_IMPORTANCE); | 4432 TIPC_LOW_IMPORTANCE); |
| 4433 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", | 4433 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE", |
| 4434 TIPC_MEDIUM_IMPORTANCE); | 4434 TIPC_MEDIUM_IMPORTANCE); |
| 4435 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", | 4435 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE", |
| 4436 TIPC_HIGH_IMPORTANCE); | 4436 TIPC_HIGH_IMPORTANCE); |
| 4437 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", | 4437 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE", |
| 4438 TIPC_CRITICAL_IMPORTANCE); | 4438 TIPC_CRITICAL_IMPORTANCE); |
| 4439 | 4439 |
| 4440 /* for subscriptions */ | 4440 /* for subscriptions */ |
| 4441 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); | 4441 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); |
| 4442 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); | 4442 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); |
| 4443 #ifdef TIPC_SUB_CANCEL | 4443 #ifdef TIPC_SUB_CANCEL |
| 4444 /* doesn't seem to be available everywhere */ | 4444 /* doesn't seem to be available everywhere */ |
| 4445 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); | 4445 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); |
| 4446 #endif | 4446 #endif |
| 4447 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); | 4447 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); |
| 4448 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); | 4448 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); |
| 4449 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); | 4449 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); |
| 4450 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); | 4450 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT); |
| 4451 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); | 4451 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV); |
| 4452 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); | 4452 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV); |
| 4453 #endif | 4453 #endif |
| 4454 | 4454 |
| 4455 /* Socket types */ | 4455 /* Socket types */ |
| 4456 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); | 4456 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM); |
| 4457 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); | 4457 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM); |
| 4458 /* We have incomplete socket support. */ | 4458 /* We have incomplete socket support. */ |
| 4459 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); | 4459 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW); |
| 4460 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); | 4460 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET); |
| 4461 #if defined(SOCK_RDM) | 4461 #if defined(SOCK_RDM) |
| 4462 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); | 4462 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM); |
| 4463 #endif | 4463 #endif |
| 4464 | 4464 |
| 4465 #ifdef SO_DEBUG | 4465 #ifdef SO_DEBUG |
| 4466 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); | 4466 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG); |
| 4467 #endif | 4467 #endif |
| 4468 #ifdef SO_ACCEPTCONN | 4468 #ifdef SO_ACCEPTCONN |
| 4469 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); | 4469 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN); |
| 4470 #endif | 4470 #endif |
| 4471 #ifdef SO_REUSEADDR | 4471 #ifdef SO_REUSEADDR |
| 4472 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); | 4472 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR); |
| 4473 #endif | 4473 #endif |
| 4474 #ifdef SO_EXCLUSIVEADDRUSE | 4474 #ifdef SO_EXCLUSIVEADDRUSE |
| 4475 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); | 4475 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE); |
| 4476 #endif | 4476 #endif |
| 4477 | 4477 |
| 4478 #ifdef SO_KEEPALIVE | 4478 #ifdef SO_KEEPALIVE |
| 4479 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); | 4479 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE); |
| 4480 #endif | 4480 #endif |
| 4481 #ifdef SO_DONTROUTE | 4481 #ifdef SO_DONTROUTE |
| 4482 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); | 4482 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE); |
| 4483 #endif | 4483 #endif |
| 4484 #ifdef SO_BROADCAST | 4484 #ifdef SO_BROADCAST |
| 4485 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); | 4485 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST); |
| 4486 #endif | 4486 #endif |
| 4487 #ifdef SO_USELOOPBACK | 4487 #ifdef SO_USELOOPBACK |
| 4488 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); | 4488 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK); |
| 4489 #endif | 4489 #endif |
| 4490 #ifdef SO_LINGER | 4490 #ifdef SO_LINGER |
| 4491 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); | 4491 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER); |
| 4492 #endif | 4492 #endif |
| 4493 #ifdef SO_OOBINLINE | 4493 #ifdef SO_OOBINLINE |
| 4494 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); | 4494 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE); |
| 4495 #endif | 4495 #endif |
| 4496 #ifdef SO_REUSEPORT | 4496 #ifdef SO_REUSEPORT |
| 4497 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); | 4497 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT); |
| 4498 #endif | 4498 #endif |
| 4499 #ifdef SO_SNDBUF | 4499 #ifdef SO_SNDBUF |
| 4500 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); | 4500 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF); |
| 4501 #endif | 4501 #endif |
| 4502 #ifdef SO_RCVBUF | 4502 #ifdef SO_RCVBUF |
| 4503 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); | 4503 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF); |
| 4504 #endif | 4504 #endif |
| 4505 #ifdef SO_SNDLOWAT | 4505 #ifdef SO_SNDLOWAT |
| 4506 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); | 4506 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT); |
| 4507 #endif | 4507 #endif |
| 4508 #ifdef SO_RCVLOWAT | 4508 #ifdef SO_RCVLOWAT |
| 4509 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); | 4509 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT); |
| 4510 #endif | 4510 #endif |
| 4511 #ifdef SO_SNDTIMEO | 4511 #ifdef SO_SNDTIMEO |
| 4512 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); | 4512 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO); |
| 4513 #endif | 4513 #endif |
| 4514 #ifdef SO_RCVTIMEO | 4514 #ifdef SO_RCVTIMEO |
| 4515 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); | 4515 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO); |
| 4516 #endif | 4516 #endif |
| 4517 #ifdef SO_ERROR | 4517 #ifdef SO_ERROR |
| 4518 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); | 4518 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR); |
| 4519 #endif | 4519 #endif |
| 4520 #ifdef SO_TYPE | 4520 #ifdef SO_TYPE |
| 4521 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); | 4521 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE); |
| 4522 #endif | 4522 #endif |
| 4523 | 4523 |
| 4524 /* Maximum number of connections for "listen" */ | 4524 /* Maximum number of connections for "listen" */ |
| 4525 #ifdef SOMAXCONN | 4525 #ifdef SOMAXCONN |
| 4526 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); | 4526 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN); |
| 4527 #else | 4527 #else |
| 4528 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ | 4528 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
| 4529 #endif | 4529 #endif |
| 4530 | 4530 |
| 4531 /* Flags for send, recv */ | 4531 /* Flags for send, recv */ |
| 4532 #ifdef MSG_OOB | 4532 #ifdef MSG_OOB |
| 4533 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); | 4533 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB); |
| 4534 #endif | 4534 #endif |
| 4535 #ifdef MSG_PEEK | 4535 #ifdef MSG_PEEK |
| 4536 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); | 4536 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK); |
| 4537 #endif | 4537 #endif |
| 4538 #ifdef MSG_DONTROUTE | 4538 #ifdef MSG_DONTROUTE |
| 4539 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); | 4539 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE); |
| 4540 #endif | 4540 #endif |
| 4541 #ifdef MSG_DONTWAIT | 4541 #ifdef MSG_DONTWAIT |
| 4542 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); | 4542 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT); |
| 4543 #endif | 4543 #endif |
| 4544 #ifdef MSG_EOR | 4544 #ifdef MSG_EOR |
| 4545 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); | 4545 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR); |
| 4546 #endif | 4546 #endif |
| 4547 #ifdef MSG_TRUNC | 4547 #ifdef MSG_TRUNC |
| 4548 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); | 4548 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC); |
| 4549 #endif | 4549 #endif |
| 4550 #ifdef MSG_CTRUNC | 4550 #ifdef MSG_CTRUNC |
| 4551 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); | 4551 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC); |
| 4552 #endif | 4552 #endif |
| 4553 #ifdef MSG_WAITALL | 4553 #ifdef MSG_WAITALL |
| 4554 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); | 4554 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL); |
| 4555 #endif | 4555 #endif |
| 4556 #ifdef MSG_BTAG | 4556 #ifdef MSG_BTAG |
| 4557 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); | 4557 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG); |
| 4558 #endif | 4558 #endif |
| 4559 #ifdef MSG_ETAG | 4559 #ifdef MSG_ETAG |
| 4560 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); | 4560 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG); |
| 4561 #endif | 4561 #endif |
| 4562 | 4562 |
| 4563 /* Protocol level and numbers, usable for [gs]etsockopt */ | 4563 /* Protocol level and numbers, usable for [gs]etsockopt */ |
| 4564 #ifdef SOL_SOCKET | 4564 #ifdef SOL_SOCKET |
| 4565 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); | 4565 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET); |
| 4566 #endif | 4566 #endif |
| 4567 #ifdef SOL_IP | 4567 #ifdef SOL_IP |
| 4568 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); | 4568 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP); |
| 4569 #else | 4569 #else |
| 4570 PyModule_AddIntConstant(m, "SOL_IP", 0); | 4570 PyModule_AddIntConstant(m, "SOL_IP", 0); |
| 4571 #endif | 4571 #endif |
| 4572 #ifdef SOL_IPX | 4572 #ifdef SOL_IPX |
| 4573 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); | 4573 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX); |
| 4574 #endif | 4574 #endif |
| 4575 #ifdef SOL_AX25 | 4575 #ifdef SOL_AX25 |
| 4576 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); | 4576 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25); |
| 4577 #endif | 4577 #endif |
| 4578 #ifdef SOL_ATALK | 4578 #ifdef SOL_ATALK |
| 4579 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); | 4579 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK); |
| 4580 #endif | 4580 #endif |
| 4581 #ifdef SOL_NETROM | 4581 #ifdef SOL_NETROM |
| 4582 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); | 4582 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM); |
| 4583 #endif | 4583 #endif |
| 4584 #ifdef SOL_ROSE | 4584 #ifdef SOL_ROSE |
| 4585 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); | 4585 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE); |
| 4586 #endif | 4586 #endif |
| 4587 #ifdef SOL_TCP | 4587 #ifdef SOL_TCP |
| 4588 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); | 4588 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP); |
| 4589 #else | 4589 #else |
| 4590 PyModule_AddIntConstant(m, "SOL_TCP", 6); | 4590 PyModule_AddIntConstant(m, "SOL_TCP", 6); |
| 4591 #endif | 4591 #endif |
| 4592 #ifdef SOL_UDP | 4592 #ifdef SOL_UDP |
| 4593 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); | 4593 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP); |
| 4594 #else | 4594 #else |
| 4595 PyModule_AddIntConstant(m, "SOL_UDP", 17); | 4595 PyModule_AddIntConstant(m, "SOL_UDP", 17); |
| 4596 #endif | 4596 #endif |
| 4597 #ifdef IPPROTO_IP | 4597 #ifdef IPPROTO_IP |
| 4598 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); | 4598 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP); |
| 4599 #else | 4599 #else |
| 4600 PyModule_AddIntConstant(m, "IPPROTO_IP", 0); | 4600 PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
| 4601 #endif | 4601 #endif |
| 4602 #ifdef IPPROTO_HOPOPTS | 4602 #ifdef IPPROTO_HOPOPTS |
| 4603 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); | 4603 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS); |
| 4604 #endif | 4604 #endif |
| 4605 #ifdef IPPROTO_ICMP | 4605 #ifdef IPPROTO_ICMP |
| 4606 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); | 4606 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP); |
| 4607 #else | 4607 #else |
| 4608 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); | 4608 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
| 4609 #endif | 4609 #endif |
| 4610 #ifdef IPPROTO_IGMP | 4610 #ifdef IPPROTO_IGMP |
| 4611 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); | 4611 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP); |
| 4612 #endif | 4612 #endif |
| 4613 #ifdef IPPROTO_GGP | 4613 #ifdef IPPROTO_GGP |
| 4614 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); | 4614 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP); |
| 4615 #endif | 4615 #endif |
| 4616 #ifdef IPPROTO_IPV4 | 4616 #ifdef IPPROTO_IPV4 |
| 4617 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); | 4617 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4); |
| 4618 #endif | 4618 #endif |
| 4619 #ifdef IPPROTO_IPV6 | 4619 #ifdef IPPROTO_IPV6 |
| 4620 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); | 4620 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
| 4621 #endif | 4621 #endif |
| 4622 #ifdef IPPROTO_IPIP | 4622 #ifdef IPPROTO_IPIP |
| 4623 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); | 4623 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP); |
| 4624 #endif | 4624 #endif |
| 4625 #ifdef IPPROTO_TCP | 4625 #ifdef IPPROTO_TCP |
| 4626 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); | 4626 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP); |
| 4627 #else | 4627 #else |
| 4628 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); | 4628 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
| 4629 #endif | 4629 #endif |
| 4630 #ifdef IPPROTO_EGP | 4630 #ifdef IPPROTO_EGP |
| 4631 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); | 4631 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP); |
| 4632 #endif | 4632 #endif |
| 4633 #ifdef IPPROTO_PUP | 4633 #ifdef IPPROTO_PUP |
| 4634 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); | 4634 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP); |
| 4635 #endif | 4635 #endif |
| 4636 #ifdef IPPROTO_UDP | 4636 #ifdef IPPROTO_UDP |
| 4637 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); | 4637 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP); |
| 4638 #else | 4638 #else |
| 4639 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); | 4639 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
| 4640 #endif | 4640 #endif |
| 4641 #ifdef IPPROTO_IDP | 4641 #ifdef IPPROTO_IDP |
| 4642 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); | 4642 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP); |
| 4643 #endif | 4643 #endif |
| 4644 #ifdef IPPROTO_HELLO | 4644 #ifdef IPPROTO_HELLO |
| 4645 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); | 4645 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO); |
| 4646 #endif | 4646 #endif |
| 4647 #ifdef IPPROTO_ND | 4647 #ifdef IPPROTO_ND |
| 4648 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); | 4648 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND); |
| 4649 #endif | 4649 #endif |
| 4650 #ifdef IPPROTO_TP | 4650 #ifdef IPPROTO_TP |
| 4651 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); | 4651 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP); |
| 4652 #endif | 4652 #endif |
| 4653 #ifdef IPPROTO_IPV6 | 4653 #ifdef IPPROTO_IPV6 |
| 4654 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); | 4654 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6); |
| 4655 #endif | 4655 #endif |
| 4656 #ifdef IPPROTO_ROUTING | 4656 #ifdef IPPROTO_ROUTING |
| 4657 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); | 4657 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING); |
| 4658 #endif | 4658 #endif |
| 4659 #ifdef IPPROTO_FRAGMENT | 4659 #ifdef IPPROTO_FRAGMENT |
| 4660 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); | 4660 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT); |
| 4661 #endif | 4661 #endif |
| 4662 #ifdef IPPROTO_RSVP | 4662 #ifdef IPPROTO_RSVP |
| 4663 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); | 4663 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP); |
| 4664 #endif | 4664 #endif |
| 4665 #ifdef IPPROTO_GRE | 4665 #ifdef IPPROTO_GRE |
| 4666 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); | 4666 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE); |
| 4667 #endif | 4667 #endif |
| 4668 #ifdef IPPROTO_ESP | 4668 #ifdef IPPROTO_ESP |
| 4669 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); | 4669 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP); |
| 4670 #endif | 4670 #endif |
| 4671 #ifdef IPPROTO_AH | 4671 #ifdef IPPROTO_AH |
| 4672 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); | 4672 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH); |
| 4673 #endif | 4673 #endif |
| 4674 #ifdef IPPROTO_MOBILE | 4674 #ifdef IPPROTO_MOBILE |
| 4675 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); | 4675 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE); |
| 4676 #endif | 4676 #endif |
| 4677 #ifdef IPPROTO_ICMPV6 | 4677 #ifdef IPPROTO_ICMPV6 |
| 4678 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); | 4678 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6); |
| 4679 #endif | 4679 #endif |
| 4680 #ifdef IPPROTO_NONE | 4680 #ifdef IPPROTO_NONE |
| 4681 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); | 4681 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE); |
| 4682 #endif | 4682 #endif |
| 4683 #ifdef IPPROTO_DSTOPTS | 4683 #ifdef IPPROTO_DSTOPTS |
| 4684 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); | 4684 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS); |
| 4685 #endif | 4685 #endif |
| 4686 #ifdef IPPROTO_XTP | 4686 #ifdef IPPROTO_XTP |
| 4687 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); | 4687 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP); |
| 4688 #endif | 4688 #endif |
| 4689 #ifdef IPPROTO_EON | 4689 #ifdef IPPROTO_EON |
| 4690 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); | 4690 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON); |
| 4691 #endif | 4691 #endif |
| 4692 #ifdef IPPROTO_PIM | 4692 #ifdef IPPROTO_PIM |
| 4693 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); | 4693 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM); |
| 4694 #endif | 4694 #endif |
| 4695 #ifdef IPPROTO_IPCOMP | 4695 #ifdef IPPROTO_IPCOMP |
| 4696 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); | 4696 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP); |
| 4697 #endif | 4697 #endif |
| 4698 #ifdef IPPROTO_VRRP | 4698 #ifdef IPPROTO_VRRP |
| 4699 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); | 4699 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP); |
| 4700 #endif | 4700 #endif |
| 4701 #ifdef IPPROTO_BIP | 4701 #ifdef IPPROTO_BIP |
| 4702 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); | 4702 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP); |
| 4703 #endif | 4703 #endif |
| 4704 /**/ | 4704 /**/ |
| 4705 #ifdef IPPROTO_RAW | 4705 #ifdef IPPROTO_RAW |
| 4706 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); | 4706 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW); |
| 4707 #else | 4707 #else |
| 4708 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); | 4708 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
| 4709 #endif | 4709 #endif |
| 4710 #ifdef IPPROTO_MAX | 4710 #ifdef IPPROTO_MAX |
| 4711 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); | 4711 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX); |
| 4712 #endif | 4712 #endif |
| 4713 | 4713 |
| 4714 /* Some port configuration */ | 4714 /* Some port configuration */ |
| 4715 #ifdef IPPORT_RESERVED | 4715 #ifdef IPPORT_RESERVED |
| 4716 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); | 4716 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED); |
| 4717 #else | 4717 #else |
| 4718 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); | 4718 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
| 4719 #endif | 4719 #endif |
| 4720 #ifdef IPPORT_USERRESERVED | 4720 #ifdef IPPORT_USERRESERVED |
| 4721 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); | 4721 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED); |
| 4722 #else | 4722 #else |
| 4723 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); | 4723 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
| 4724 #endif | 4724 #endif |
| 4725 | 4725 |
| 4726 /* Some reserved IP v.4 addresses */ | 4726 /* Some reserved IP v.4 addresses */ |
| 4727 #ifdef INADDR_ANY | 4727 #ifdef INADDR_ANY |
| 4728 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); | 4728 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY); |
| 4729 #else | 4729 #else |
| 4730 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); | 4730 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
| 4731 #endif | 4731 #endif |
| 4732 #ifdef INADDR_BROADCAST | 4732 #ifdef INADDR_BROADCAST |
| 4733 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); | 4733 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST); |
| 4734 #else | 4734 #else |
| 4735 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); | 4735 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
| 4736 #endif | 4736 #endif |
| 4737 #ifdef INADDR_LOOPBACK | 4737 #ifdef INADDR_LOOPBACK |
| 4738 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); | 4738 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK); |
| 4739 #else | 4739 #else |
| 4740 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); | 4740 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
| 4741 #endif | 4741 #endif |
| 4742 #ifdef INADDR_UNSPEC_GROUP | 4742 #ifdef INADDR_UNSPEC_GROUP |
| 4743 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); | 4743 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP); |
| 4744 #else | 4744 #else |
| 4745 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); | 4745 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
| 4746 #endif | 4746 #endif |
| 4747 #ifdef INADDR_ALLHOSTS_GROUP | 4747 #ifdef INADDR_ALLHOSTS_GROUP |
| 4748 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", | 4748 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
| 4749 INADDR_ALLHOSTS_GROUP); | 4749 INADDR_ALLHOSTS_GROUP); |
| 4750 #else | 4750 #else |
| 4751 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); | 4751 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
| 4752 #endif | 4752 #endif |
| 4753 #ifdef INADDR_MAX_LOCAL_GROUP | 4753 #ifdef INADDR_MAX_LOCAL_GROUP |
| 4754 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", | 4754 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", |
| 4755 INADDR_MAX_LOCAL_GROUP); | 4755 INADDR_MAX_LOCAL_GROUP); |
| 4756 #else | 4756 #else |
| 4757 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); | 4757 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
| 4758 #endif | 4758 #endif |
| 4759 #ifdef INADDR_NONE | 4759 #ifdef INADDR_NONE |
| 4760 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); | 4760 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE); |
| 4761 #else | 4761 #else |
| 4762 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); | 4762 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
| 4763 #endif | 4763 #endif |
| 4764 | 4764 |
| 4765 /* IPv4 [gs]etsockopt options */ | 4765 /* IPv4 [gs]etsockopt options */ |
| 4766 #ifdef IP_OPTIONS | 4766 #ifdef IP_OPTIONS |
| 4767 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); | 4767 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS); |
| 4768 #endif | 4768 #endif |
| 4769 #ifdef IP_HDRINCL | 4769 #ifdef IP_HDRINCL |
| 4770 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); | 4770 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL); |
| 4771 #endif | 4771 #endif |
| 4772 #ifdef IP_TOS | 4772 #ifdef IP_TOS |
| 4773 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); | 4773 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS); |
| 4774 #endif | 4774 #endif |
| 4775 #ifdef IP_TTL | 4775 #ifdef IP_TTL |
| 4776 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); | 4776 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL); |
| 4777 #endif | 4777 #endif |
| 4778 #ifdef IP_RECVOPTS | 4778 #ifdef IP_RECVOPTS |
| 4779 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); | 4779 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS); |
| 4780 #endif | 4780 #endif |
| 4781 #ifdef IP_RECVRETOPTS | 4781 #ifdef IP_RECVRETOPTS |
| 4782 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); | 4782 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS); |
| 4783 #endif | 4783 #endif |
| 4784 #ifdef IP_RECVDSTADDR | 4784 #ifdef IP_RECVDSTADDR |
| 4785 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); | 4785 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR); |
| 4786 #endif | 4786 #endif |
| 4787 #ifdef IP_RETOPTS | 4787 #ifdef IP_RETOPTS |
| 4788 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); | 4788 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS); |
| 4789 #endif | 4789 #endif |
| 4790 #ifdef IP_MULTICAST_IF | 4790 #ifdef IP_MULTICAST_IF |
| 4791 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); | 4791 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF); |
| 4792 #endif | 4792 #endif |
| 4793 #ifdef IP_MULTICAST_TTL | 4793 #ifdef IP_MULTICAST_TTL |
| 4794 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); | 4794 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL); |
| 4795 #endif | 4795 #endif |
| 4796 #ifdef IP_MULTICAST_LOOP | 4796 #ifdef IP_MULTICAST_LOOP |
| 4797 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); | 4797 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP); |
| 4798 #endif | 4798 #endif |
| 4799 #ifdef IP_ADD_MEMBERSHIP | 4799 #ifdef IP_ADD_MEMBERSHIP |
| 4800 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); | 4800 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP); |
| 4801 #endif | 4801 #endif |
| 4802 #ifdef IP_DROP_MEMBERSHIP | 4802 #ifdef IP_DROP_MEMBERSHIP |
| 4803 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); | 4803 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP); |
| 4804 #endif | 4804 #endif |
| 4805 #ifdef IP_DEFAULT_MULTICAST_TTL | 4805 #ifdef IP_DEFAULT_MULTICAST_TTL |
| 4806 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", | 4806 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL", |
| 4807 IP_DEFAULT_MULTICAST_TTL); | 4807 IP_DEFAULT_MULTICAST_TTL); |
| 4808 #endif | 4808 #endif |
| 4809 #ifdef IP_DEFAULT_MULTICAST_LOOP | 4809 #ifdef IP_DEFAULT_MULTICAST_LOOP |
| 4810 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", | 4810 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP", |
| 4811 IP_DEFAULT_MULTICAST_LOOP); | 4811 IP_DEFAULT_MULTICAST_LOOP); |
| 4812 #endif | 4812 #endif |
| 4813 #ifdef IP_MAX_MEMBERSHIPS | 4813 #ifdef IP_MAX_MEMBERSHIPS |
| 4814 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); | 4814 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS); |
| 4815 #endif | 4815 #endif |
| 4816 | 4816 |
| 4817 /* IPv6 [gs]etsockopt options, defined in RFC2553 */ | 4817 /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
| 4818 #ifdef IPV6_JOIN_GROUP | 4818 #ifdef IPV6_JOIN_GROUP |
| 4819 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); | 4819 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP); |
| 4820 #endif | 4820 #endif |
| 4821 #ifdef IPV6_LEAVE_GROUP | 4821 #ifdef IPV6_LEAVE_GROUP |
| 4822 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); | 4822 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP); |
| 4823 #endif | 4823 #endif |
| 4824 #ifdef IPV6_MULTICAST_HOPS | 4824 #ifdef IPV6_MULTICAST_HOPS |
| 4825 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); | 4825 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS); |
| 4826 #endif | 4826 #endif |
| 4827 #ifdef IPV6_MULTICAST_IF | 4827 #ifdef IPV6_MULTICAST_IF |
| 4828 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); | 4828 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF); |
| 4829 #endif | 4829 #endif |
| 4830 #ifdef IPV6_MULTICAST_LOOP | 4830 #ifdef IPV6_MULTICAST_LOOP |
| 4831 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); | 4831 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP); |
| 4832 #endif | 4832 #endif |
| 4833 #ifdef IPV6_UNICAST_HOPS | 4833 #ifdef IPV6_UNICAST_HOPS |
| 4834 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); | 4834 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS); |
| 4835 #endif | 4835 #endif |
| 4836 /* Additional IPV6 socket options, defined in RFC 3493 */ | 4836 /* Additional IPV6 socket options, defined in RFC 3493 */ |
| 4837 #ifdef IPV6_V6ONLY | 4837 #ifdef IPV6_V6ONLY |
| 4838 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); | 4838 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY); |
| 4839 #endif | 4839 #endif |
| 4840 /* Advanced IPV6 socket options, from RFC 3542 */ | 4840 /* Advanced IPV6 socket options, from RFC 3542 */ |
| 4841 #ifdef IPV6_CHECKSUM | 4841 #ifdef IPV6_CHECKSUM |
| 4842 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); | 4842 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM); |
| 4843 #endif | 4843 #endif |
| 4844 #ifdef IPV6_DONTFRAG | 4844 #ifdef IPV6_DONTFRAG |
| 4845 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); | 4845 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG); |
| 4846 #endif | 4846 #endif |
| 4847 #ifdef IPV6_DSTOPTS | 4847 #ifdef IPV6_DSTOPTS |
| 4848 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); | 4848 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS); |
| 4849 #endif | 4849 #endif |
| 4850 #ifdef IPV6_HOPLIMIT | 4850 #ifdef IPV6_HOPLIMIT |
| 4851 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); | 4851 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT); |
| 4852 #endif | 4852 #endif |
| 4853 #ifdef IPV6_HOPOPTS | 4853 #ifdef IPV6_HOPOPTS |
| 4854 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); | 4854 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS); |
| 4855 #endif | 4855 #endif |
| 4856 #ifdef IPV6_NEXTHOP | 4856 #ifdef IPV6_NEXTHOP |
| 4857 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); | 4857 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP); |
| 4858 #endif | 4858 #endif |
| 4859 #ifdef IPV6_PATHMTU | 4859 #ifdef IPV6_PATHMTU |
| 4860 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); | 4860 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU); |
| 4861 #endif | 4861 #endif |
| 4862 #ifdef IPV6_PKTINFO | 4862 #ifdef IPV6_PKTINFO |
| 4863 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); | 4863 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO); |
| 4864 #endif | 4864 #endif |
| 4865 #ifdef IPV6_RECVDSTOPTS | 4865 #ifdef IPV6_RECVDSTOPTS |
| 4866 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); | 4866 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS); |
| 4867 #endif | 4867 #endif |
| 4868 #ifdef IPV6_RECVHOPLIMIT | 4868 #ifdef IPV6_RECVHOPLIMIT |
| 4869 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); | 4869 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT); |
| 4870 #endif | 4870 #endif |
| 4871 #ifdef IPV6_RECVHOPOPTS | 4871 #ifdef IPV6_RECVHOPOPTS |
| 4872 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); | 4872 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS); |
| 4873 #endif | 4873 #endif |
| 4874 #ifdef IPV6_RECVPKTINFO | 4874 #ifdef IPV6_RECVPKTINFO |
| 4875 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); | 4875 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO); |
| 4876 #endif | 4876 #endif |
| 4877 #ifdef IPV6_RECVRTHDR | 4877 #ifdef IPV6_RECVRTHDR |
| 4878 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); | 4878 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR); |
| 4879 #endif | 4879 #endif |
| 4880 #ifdef IPV6_RECVTCLASS | 4880 #ifdef IPV6_RECVTCLASS |
| 4881 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); | 4881 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS); |
| 4882 #endif | 4882 #endif |
| 4883 #ifdef IPV6_RTHDR | 4883 #ifdef IPV6_RTHDR |
| 4884 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); | 4884 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR); |
| 4885 #endif | 4885 #endif |
| 4886 #ifdef IPV6_RTHDRDSTOPTS | 4886 #ifdef IPV6_RTHDRDSTOPTS |
| 4887 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); | 4887 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS); |
| 4888 #endif | 4888 #endif |
| 4889 #ifdef IPV6_RTHDR_TYPE_0 | 4889 #ifdef IPV6_RTHDR_TYPE_0 |
| 4890 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); | 4890 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0); |
| 4891 #endif | 4891 #endif |
| 4892 #ifdef IPV6_RECVPATHMTU | 4892 #ifdef IPV6_RECVPATHMTU |
| 4893 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); | 4893 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU); |
| 4894 #endif | 4894 #endif |
| 4895 #ifdef IPV6_TCLASS | 4895 #ifdef IPV6_TCLASS |
| 4896 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); | 4896 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS); |
| 4897 #endif | 4897 #endif |
| 4898 #ifdef IPV6_USE_MIN_MTU | 4898 #ifdef IPV6_USE_MIN_MTU |
| 4899 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); | 4899 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU); |
| 4900 #endif | 4900 #endif |
| 4901 | 4901 |
| 4902 /* TCP options */ | 4902 /* TCP options */ |
| 4903 #ifdef TCP_NODELAY | 4903 #ifdef TCP_NODELAY |
| 4904 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); | 4904 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY); |
| 4905 #endif | 4905 #endif |
| 4906 #ifdef TCP_MAXSEG | 4906 #ifdef TCP_MAXSEG |
| 4907 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); | 4907 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG); |
| 4908 #endif | 4908 #endif |
| 4909 #ifdef TCP_CORK | 4909 #ifdef TCP_CORK |
| 4910 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); | 4910 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK); |
| 4911 #endif | 4911 #endif |
| 4912 #ifdef TCP_KEEPIDLE | 4912 #ifdef TCP_KEEPIDLE |
| 4913 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); | 4913 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE); |
| 4914 #endif | 4914 #endif |
| 4915 #ifdef TCP_KEEPINTVL | 4915 #ifdef TCP_KEEPINTVL |
| 4916 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); | 4916 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL); |
| 4917 #endif | 4917 #endif |
| 4918 #ifdef TCP_KEEPCNT | 4918 #ifdef TCP_KEEPCNT |
| 4919 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); | 4919 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT); |
| 4920 #endif | 4920 #endif |
| 4921 #ifdef TCP_SYNCNT | 4921 #ifdef TCP_SYNCNT |
| 4922 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); | 4922 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT); |
| 4923 #endif | 4923 #endif |
| 4924 #ifdef TCP_LINGER2 | 4924 #ifdef TCP_LINGER2 |
| 4925 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); | 4925 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2); |
| 4926 #endif | 4926 #endif |
| 4927 #ifdef TCP_DEFER_ACCEPT | 4927 #ifdef TCP_DEFER_ACCEPT |
| 4928 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); | 4928 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT); |
| 4929 #endif | 4929 #endif |
| 4930 #ifdef TCP_WINDOW_CLAMP | 4930 #ifdef TCP_WINDOW_CLAMP |
| 4931 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); | 4931 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP); |
| 4932 #endif | 4932 #endif |
| 4933 #ifdef TCP_INFO | 4933 #ifdef TCP_INFO |
| 4934 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); | 4934 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO); |
| 4935 #endif | 4935 #endif |
| 4936 #ifdef TCP_QUICKACK | 4936 #ifdef TCP_QUICKACK |
| 4937 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); | 4937 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK); |
| 4938 #endif | 4938 #endif |
| 4939 | 4939 |
| 4940 | 4940 |
| 4941 /* IPX options */ | 4941 /* IPX options */ |
| 4942 #ifdef IPX_TYPE | 4942 #ifdef IPX_TYPE |
| 4943 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); | 4943 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE); |
| 4944 #endif | 4944 #endif |
| 4945 | 4945 |
| 4946 /* get{addr,name}info parameters */ | 4946 /* get{addr,name}info parameters */ |
| 4947 #ifdef EAI_ADDRFAMILY | 4947 #ifdef EAI_ADDRFAMILY |
| 4948 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); | 4948 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY); |
| 4949 #endif | 4949 #endif |
| 4950 #ifdef EAI_AGAIN | 4950 #ifdef EAI_AGAIN |
| 4951 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); | 4951 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN); |
| 4952 #endif | 4952 #endif |
| 4953 #ifdef EAI_BADFLAGS | 4953 #ifdef EAI_BADFLAGS |
| 4954 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); | 4954 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS); |
| 4955 #endif | 4955 #endif |
| 4956 #ifdef EAI_FAIL | 4956 #ifdef EAI_FAIL |
| 4957 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); | 4957 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL); |
| 4958 #endif | 4958 #endif |
| 4959 #ifdef EAI_FAMILY | 4959 #ifdef EAI_FAMILY |
| 4960 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); | 4960 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY); |
| 4961 #endif | 4961 #endif |
| 4962 #ifdef EAI_MEMORY | 4962 #ifdef EAI_MEMORY |
| 4963 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); | 4963 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY); |
| 4964 #endif | 4964 #endif |
| 4965 #ifdef EAI_NODATA | 4965 #ifdef EAI_NODATA |
| 4966 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); | 4966 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA); |
| 4967 #endif | 4967 #endif |
| 4968 #ifdef EAI_NONAME | 4968 #ifdef EAI_NONAME |
| 4969 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); | 4969 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME); |
| 4970 #endif | 4970 #endif |
| 4971 #ifdef EAI_OVERFLOW | 4971 #ifdef EAI_OVERFLOW |
| 4972 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); | 4972 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW); |
| 4973 #endif | 4973 #endif |
| 4974 #ifdef EAI_SERVICE | 4974 #ifdef EAI_SERVICE |
| 4975 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); | 4975 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE); |
| 4976 #endif | 4976 #endif |
| 4977 #ifdef EAI_SOCKTYPE | 4977 #ifdef EAI_SOCKTYPE |
| 4978 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); | 4978 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE); |
| 4979 #endif | 4979 #endif |
| 4980 #ifdef EAI_SYSTEM | 4980 #ifdef EAI_SYSTEM |
| 4981 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); | 4981 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM); |
| 4982 #endif | 4982 #endif |
| 4983 #ifdef EAI_BADHINTS | 4983 #ifdef EAI_BADHINTS |
| 4984 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); | 4984 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS); |
| 4985 #endif | 4985 #endif |
| 4986 #ifdef EAI_PROTOCOL | 4986 #ifdef EAI_PROTOCOL |
| 4987 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); | 4987 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL); |
| 4988 #endif | 4988 #endif |
| 4989 #ifdef EAI_MAX | 4989 #ifdef EAI_MAX |
| 4990 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); | 4990 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX); |
| 4991 #endif | 4991 #endif |
| 4992 #ifdef AI_PASSIVE | 4992 #ifdef AI_PASSIVE |
| 4993 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); | 4993 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE); |
| 4994 #endif | 4994 #endif |
| 4995 #ifdef AI_CANONNAME | 4995 #ifdef AI_CANONNAME |
| 4996 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); | 4996 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME); |
| 4997 #endif | 4997 #endif |
| 4998 #ifdef AI_NUMERICHOST | 4998 #ifdef AI_NUMERICHOST |
| 4999 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); | 4999 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST); |
| 5000 #endif | 5000 #endif |
| 5001 #ifdef AI_NUMERICSERV | 5001 #ifdef AI_NUMERICSERV |
| 5002 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); | 5002 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV); |
| 5003 #endif | 5003 #endif |
| 5004 #ifdef AI_MASK | 5004 #ifdef AI_MASK |
| 5005 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); | 5005 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK); |
| 5006 #endif | 5006 #endif |
| 5007 #ifdef AI_ALL | 5007 #ifdef AI_ALL |
| 5008 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); | 5008 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL); |
| 5009 #endif | 5009 #endif |
| 5010 #ifdef AI_V4MAPPED_CFG | 5010 #ifdef AI_V4MAPPED_CFG |
| 5011 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); | 5011 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG); |
| 5012 #endif | 5012 #endif |
| 5013 #ifdef AI_ADDRCONFIG | 5013 #ifdef AI_ADDRCONFIG |
| 5014 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); | 5014 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG); |
| 5015 #endif | 5015 #endif |
| 5016 #ifdef AI_V4MAPPED | 5016 #ifdef AI_V4MAPPED |
| 5017 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); | 5017 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED); |
| 5018 #endif | 5018 #endif |
| 5019 #ifdef AI_DEFAULT | 5019 #ifdef AI_DEFAULT |
| 5020 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); | 5020 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT); |
| 5021 #endif | 5021 #endif |
| 5022 #ifdef NI_MAXHOST | 5022 #ifdef NI_MAXHOST |
| 5023 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); | 5023 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST); |
| 5024 #endif | 5024 #endif |
| 5025 #ifdef NI_MAXSERV | 5025 #ifdef NI_MAXSERV |
| 5026 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); | 5026 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV); |
| 5027 #endif | 5027 #endif |
| 5028 #ifdef NI_NOFQDN | 5028 #ifdef NI_NOFQDN |
| 5029 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); | 5029 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN); |
| 5030 #endif | 5030 #endif |
| 5031 #ifdef NI_NUMERICHOST | 5031 #ifdef NI_NUMERICHOST |
| 5032 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); | 5032 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST); |
| 5033 #endif | 5033 #endif |
| 5034 #ifdef NI_NAMEREQD | 5034 #ifdef NI_NAMEREQD |
| 5035 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); | 5035 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD); |
| 5036 #endif | 5036 #endif |
| 5037 #ifdef NI_NUMERICSERV | 5037 #ifdef NI_NUMERICSERV |
| 5038 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); | 5038 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV); |
| 5039 #endif | 5039 #endif |
| 5040 #ifdef NI_DGRAM | 5040 #ifdef NI_DGRAM |
| 5041 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); | 5041 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM); |
| 5042 #endif | 5042 #endif |
| 5043 | 5043 |
| 5044 /* shutdown() parameters */ | 5044 /* shutdown() parameters */ |
| 5045 #ifdef SHUT_RD | 5045 #ifdef SHUT_RD |
| 5046 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); | 5046 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD); |
| 5047 #elif defined(SD_RECEIVE) | 5047 #elif defined(SD_RECEIVE) |
| 5048 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); | 5048 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
| 5049 #else | 5049 #else |
| 5050 PyModule_AddIntConstant(m, "SHUT_RD", 0); | 5050 PyModule_AddIntConstant(m, "SHUT_RD", 0); |
| 5051 #endif | 5051 #endif |
| 5052 #ifdef SHUT_WR | 5052 #ifdef SHUT_WR |
| 5053 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); | 5053 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR); |
| 5054 #elif defined(SD_SEND) | 5054 #elif defined(SD_SEND) |
| 5055 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); | 5055 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
| 5056 #else | 5056 #else |
| 5057 PyModule_AddIntConstant(m, "SHUT_WR", 1); | 5057 PyModule_AddIntConstant(m, "SHUT_WR", 1); |
| 5058 #endif | 5058 #endif |
| 5059 #ifdef SHUT_RDWR | 5059 #ifdef SHUT_RDWR |
| 5060 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); | 5060 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR); |
| 5061 #elif defined(SD_BOTH) | 5061 #elif defined(SD_BOTH) |
| 5062 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); | 5062 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
| 5063 #else | 5063 #else |
| 5064 PyModule_AddIntConstant(m, "SHUT_RDWR", 2); | 5064 PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
| 5065 #endif | 5065 #endif |
| 5066 | 5066 |
| 5067 #ifdef SIO_RCVALL | 5067 #ifdef SIO_RCVALL |
| 5068 { | 5068 { |
| 5069 PyObject *tmp; | 5069 PyObject *tmp; |
| 5070 tmp = PyLong_FromUnsignedLong(SIO_RCVALL); | 5070 tmp = PyLong_FromUnsignedLong(SIO_RCVALL); |
| 5071 if (tmp == NULL) | 5071 if (tmp == NULL) |
| 5072 return NULL; | 5072 return NULL; |
| 5073 PyModule_AddObject(m, "SIO_RCVALL", tmp); | 5073 PyModule_AddObject(m, "SIO_RCVALL", tmp); |
| 5074 } | 5074 } |
| 5075 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); | 5075 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF); |
| 5076 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); | 5076 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON); |
| 5077 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); | 5077 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY); |
| 5078 #ifdef RCVALL_IPLEVEL | 5078 #ifdef RCVALL_IPLEVEL |
| 5079 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); | 5079 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL); |
| 5080 #endif | 5080 #endif |
| 5081 #ifdef RCVALL_MAX | 5081 #ifdef RCVALL_MAX |
| 5082 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); | 5082 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX); |
| 5083 #endif | 5083 #endif |
| 5084 #endif /* _MSTCPIP_ */ | 5084 #endif /* _MSTCPIP_ */ |
| 5085 | 5085 |
| 5086 /* Initialize gethostbyname lock */ | 5086 /* Initialize gethostbyname lock */ |
| 5087 #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) | 5087 #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
| 5088 netdb_lock = PyThread_allocate_lock(); | 5088 netdb_lock = PyThread_allocate_lock(); |
| 5089 #endif | 5089 #endif |
| 5090 return m; | 5090 return m; |
| 5091 } | 5091 } |
| 5092 | 5092 |
| 5093 | 5093 |
| 5094 #ifndef HAVE_INET_PTON | 5094 #ifndef HAVE_INET_PTON |
| 5095 #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) | 5095 #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
| 5098 /* These are not exposed because they do not set errno properly */ | 5098 /* These are not exposed because they do not set errno properly */ |
| 5099 | 5099 |
| 5100 int | 5100 int |
| 5101 inet_pton(int af, const char *src, void *dst) | 5101 inet_pton(int af, const char *src, void *dst) |
| 5102 { | 5102 { |
| 5103 if (af == AF_INET) { | 5103 if (af == AF_INET) { |
| 5104 #if (SIZEOF_INT != 4) | 5104 #if (SIZEOF_INT != 4) |
| 5105 #error "Not sure if in_addr_t exists and int is not 32-bits." | 5105 #error "Not sure if in_addr_t exists and int is not 32-bits." |
| 5106 #endif | 5106 #endif |
| 5107 unsigned int packed_addr; | 5107 unsigned int packed_addr; |
| 5108 packed_addr = inet_addr(src); | 5108 packed_addr = inet_addr(src); |
| 5109 if (packed_addr == INADDR_NONE) | 5109 if (packed_addr == INADDR_NONE) |
| 5110 return 0; | 5110 return 0; |
| 5111 memcpy(dst, &packed_addr, 4); | 5111 memcpy(dst, &packed_addr, 4); |
| 5112 return 1; | 5112 return 1; |
| 5113 } | 5113 } |
| 5114 /* Should set errno to EAFNOSUPPORT */ | 5114 /* Should set errno to EAFNOSUPPORT */ |
| 5115 return -1; | 5115 return -1; |
| 5116 } | 5116 } |
| 5117 | 5117 |
| 5118 const char * | 5118 const char * |
| 5119 inet_ntop(int af, const void *src, char *dst, socklen_t size) | 5119 inet_ntop(int af, const void *src, char *dst, socklen_t size) |
| 5120 { | 5120 { |
| 5121 if (af == AF_INET) { | 5121 if (af == AF_INET) { |
| 5122 struct in_addr packed_addr; | 5122 struct in_addr packed_addr; |
| 5123 if (size < 16) | 5123 if (size < 16) |
| 5124 /* Should set errno to ENOSPC. */ | 5124 /* Should set errno to ENOSPC. */ |
| 5125 return NULL; | 5125 return NULL; |
| 5126 memcpy(&packed_addr, src, sizeof(packed_addr)); | 5126 memcpy(&packed_addr, src, sizeof(packed_addr)); |
| 5127 return strncpy(dst, inet_ntoa(packed_addr), size); | 5127 return strncpy(dst, inet_ntoa(packed_addr), size); |
| 5128 } | 5128 } |
| 5129 /* Should set errno to EAFNOSUPPORT */ | 5129 /* Should set errno to EAFNOSUPPORT */ |
| 5130 return NULL; | 5130 return NULL; |
| 5131 } | 5131 } |
| 5132 | 5132 |
| 5133 #endif | 5133 #endif |
| 5134 #endif | 5134 #endif |
