1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
|
import sys, errno
from rpython.rlib import rsocket, rweaklist
from rpython.rlib.objectmodel import specialize
from rpython.rlib.rarithmetic import intmask
from rpython.rlib.rsocket import (
RSocket, AF_INET, SOCK_STREAM, SocketError, SocketErrorWithErrno,
RSocketError, SOMAXCONN
)
from rpython.rtyper.lltypesystem import lltype, rffi
from pypy.interpreter import gateway
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError, oefmt
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from pypy.interpreter.typedef import (
GetSetProperty, TypeDef, generic_new_descr, interp_attrproperty,
make_weakref_descr)
# XXX Hack to separate rpython and pypy
def addr_as_object(addr, fd, space):
from rpython.rlib import _rsocket_rffi as _c
if isinstance(addr, rsocket.INETAddress):
return space.newtuple([space.newtext(addr.get_host()),
space.newint(addr.get_port())])
elif isinstance(addr, rsocket.INET6Address):
return space.newtuple([space.newtext(addr.get_host()),
space.newint(addr.get_port()),
space.newint(addr.get_flowinfo()),
space.newint(addr.get_scope_id())])
elif rsocket.HAS_AF_PACKET and isinstance(addr, rsocket.PacketAddress):
return space.newtuple([space.newtext(addr.get_ifname(fd)),
space.newint(addr.get_protocol()),
space.newint(addr.get_pkttype()),
space.newint(addr.get_hatype()),
space.newbytes(addr.get_haddr())])
elif rsocket.HAS_AF_UNIX and isinstance(addr, rsocket.UNIXAddress):
path = addr.get_path()
if _c.linux and len(path) > 0 and path[0] == '\x00':
# Linux abstract namespace
return space.newbytes(path)
else:
return space.newfilename(path)
elif rsocket.HAS_AF_NETLINK and isinstance(addr, rsocket.NETLINKAddress):
return space.newtuple([space.newint(addr.get_pid()),
space.newint(addr.get_groups())])
# If we don't know the address family, don't raise an
# exception -- return it as a tuple.
a = addr.lock()
family = rffi.cast(lltype.Signed, a.c_sa_family)
datalen = addr.addrlen - rsocket.offsetof(_c.sockaddr, 'c_sa_data')
rawdata = ''.join([a.c_sa_data[i] for i in range(datalen)])
addr.unlock()
return space.newtuple([space.newint(family),
space.newtext(rawdata)])
# XXX Hack to seperate rpython and pypy
# XXX a bit of code duplication
def fill_from_object(addr, space, w_address):
from rpython.rlib import _rsocket_rffi as _c
if isinstance(addr, rsocket.INETAddress):
_, w_port = space.unpackiterable(w_address, 2)
port = space.int_w(w_port)
port = make_ushort_port(space, port)
a = addr.lock(_c.sockaddr_in)
rffi.setintfield(a, 'c_sin_port', rsocket.htons(port))
addr.unlock()
elif isinstance(addr, rsocket.INET6Address):
pieces_w = space.unpackiterable(w_address)
if not (2 <= len(pieces_w) <= 4):
raise RSocketError("AF_INET6 address must be a tuple of length 2 "
"to 4, not %d" % len(pieces_w))
port = space.int_w(pieces_w[1])
port = make_ushort_port(space, port)
if len(pieces_w) > 2: flowinfo = space.int_w(pieces_w[2])
else: flowinfo = 0
if len(pieces_w) > 3: scope_id = space.uint_w(pieces_w[3])
else: scope_id = 0
flowinfo = make_unsigned_flowinfo(space, flowinfo)
a = addr.lock(_c.sockaddr_in6)
rffi.setintfield(a, 'c_sin6_port', rsocket.htons(port))
rffi.setintfield(a, 'c_sin6_flowinfo', rsocket.htonl(flowinfo))
rffi.setintfield(a, 'c_sin6_scope_id', scope_id)
addr.unlock()
else:
raise NotImplementedError
def idna_converter(space, w_host):
# Converts w_host to a byte string. Similar to encode_idna()
# but accepts more types and refuses NULL bytes.
if space.isinstance_w(w_host, space.w_unicode):
try:
w_s = space.encode_unicode_object(w_host, 'ascii', None)
except OperationError as e:
if not e.match(space, space.w_UnicodeEncodeError):
raise
w_s = space.encode_unicode_object(w_host, 'idna', None)
s = space.bytes_w(w_s)
elif space.isinstance_w(w_host, space.w_bytes):
s = space.bytes_w(w_host)
elif space.isinstance_w(w_host, space.w_bytearray):
s = space.charbuf_w(w_host)
else:
raise oefmt(space.w_TypeError,
"string or unicode text buffer expected, not %T", w_host)
if '\x00' in s:
raise oefmt(space.w_TypeError,
"host name must not contain null character")
return s
# XXX Hack to seperate rpython and pypy
def addr_from_object(family, fd, space, w_address):
if family == rsocket.AF_INET:
w_host, w_port = space.unpackiterable(w_address, 2)
host = idna_converter(space, w_host)
port = space.int_w(w_port)
port = make_ushort_port(space, port)
return rsocket.INETAddress(host, port)
if family == rsocket.AF_INET6:
pieces_w = space.unpackiterable(w_address)
if not (2 <= len(pieces_w) <= 4):
raise oefmt(space.w_TypeError,
"AF_INET6 address must be a tuple of length 2 "
"to 4, not %d", len(pieces_w))
host = idna_converter(space, pieces_w[0])
port = space.int_w(pieces_w[1])
port = make_ushort_port(space, port)
if len(pieces_w) > 2: flowinfo = space.int_w(pieces_w[2])
else: flowinfo = 0
if len(pieces_w) > 3: scope_id = space.uint_w(pieces_w[3])
else: scope_id = 0
flowinfo = make_unsigned_flowinfo(space, flowinfo)
return rsocket.INET6Address(host, port, flowinfo, scope_id)
if rsocket.HAS_AF_UNIX and family == rsocket.AF_UNIX:
# Not using space.fsencode_w since Linux allows embedded NULs.
if space.isinstance_w(w_address, space.w_unicode):
w_address = space.fsencode(w_address)
elif space.isinstance_w(w_address, space.w_bytearray):
w_address = space.newbytes(space.charbuf_w(w_address))
bytelike = space.bytes_w(w_address) # getarg_w('y*', w_address)
return rsocket.UNIXAddress(bytelike)
if rsocket.HAS_AF_NETLINK and family == rsocket.AF_NETLINK:
w_pid, w_groups = space.unpackiterable(w_address, 2)
return rsocket.NETLINKAddress(space.uint_w(w_pid), space.uint_w(w_groups))
if rsocket.HAS_AF_PACKET and family == rsocket.AF_PACKET:
pieces_w = space.unpackiterable(w_address)
if not (2 <= len(pieces_w) <= 5):
raise oefmt(space.w_TypeError,
"AF_PACKET address must be a tuple of length 2 "
"to 5, not %d", len(pieces_w))
ifname = space.text_w(pieces_w[0])
ifindex = rsocket.PacketAddress.get_ifindex_from_ifname(fd, ifname)
protocol = space.int_w(pieces_w[1])
if len(pieces_w) > 2: pkttype = space.int_w(pieces_w[2])
else: pkttype = 0
if len(pieces_w) > 3: hatype = space.int_w(pieces_w[3])
else: hatype = 0
if len(pieces_w) > 4: haddr = space.text_w(pieces_w[4])
else: haddr = ""
if len(haddr) > 8:
raise oefmt(space.w_ValueError,
"Hardware address must be 8 bytes or less")
if protocol < 0 or protocol > 0xfffff:
raise oefmt(space.w_OverflowError, "protoNumber must be 0-65535.")
return rsocket.PacketAddress(ifindex, protocol, pkttype, hatype, haddr)
raise RSocketError("unknown address family")
# XXX Hack to seperate rpython and pypy
def make_ushort_port(space, port):
assert isinstance(port, int)
if port < 0 or port > 0xffff:
raise oefmt(space.w_OverflowError, "port must be 0-65535.")
return port
def make_unsigned_flowinfo(space, flowinfo):
if flowinfo < 0 or flowinfo > 0xfffff:
raise oefmt(space.w_OverflowError, "flowinfo must be 0-1048575.")
return rffi.cast(lltype.Unsigned, flowinfo)
# XXX Hack to seperate rpython and pypy
def ipaddr_from_object(space, w_sockaddr):
host = space.text_w(space.getitem(w_sockaddr, space.newint(0)))
addr = rsocket.makeipaddr(host)
fill_from_object(addr, space, w_sockaddr)
return addr
class W_Socket(W_Root):
def __init__(self, space, sock=None):
self.space = space
if sock is None:
self.sock = RSocket.empty_rsocket()
else:
register_socket(space, sock)
self.sock = sock
self.register_finalizer(space)
@unwrap_spec(family=int, type=int, proto=int,
w_fileno=WrappedDefault(None))
def descr_init(self, space, family=AF_INET, type=SOCK_STREAM, proto=0,
w_fileno=None):
try:
if not space.is_w(w_fileno, space.w_None):
sock = RSocket(family, type, proto,
fd=space.c_filedescriptor_w(w_fileno))
else:
sock = RSocket(family, type, proto, inheritable=False)
W_Socket.__init__(self, space, sock)
except SocketError as e:
raise converted_error(space, e)
def _finalize_(self):
sock = self.sock
if sock.fd != rsocket.INVALID_SOCKET:
try:
self._dealloc_warn()
finally:
try:
sock.close()
except SocketError:
pass
def get_type_w(self, space):
return space.newint(self.sock.type)
def get_proto_w(self, space):
return space.newint(self.sock.proto)
def get_family_w(self, space):
return space.newint(self.sock.family)
def _dealloc_warn(self):
space = self.space
try:
msg = (u"unclosed %s" %
space.unicode_w(space.repr(self)))
space.warn(space.newunicode(msg), space.w_ResourceWarning)
except OperationError as e:
# Spurious errors can appear at shutdown
if e.match(space, space.w_Warning):
e.write_unraisable(space, '', self)
def descr_repr(self, space):
fd = intmask(self.sock.fd) # Force to signed type even on Windows.
return space.newtext("<socket object, fd=%d, family=%d,"
" type=%d, proto=%d>" %
(fd, self.sock.family,
self.sock.type, self.sock.proto))
def _accept_w(self, space):
"""_accept() -> (socket object, address info)
Wait for an incoming connection. Return a new socket file descriptor
representing the connection, and the address of the client.
For IP sockets, the address info is a pair (hostaddr, port).
"""
while True:
try:
fd, addr = self.sock.accept(inheritable=False)
return space.newtuple([space.newint(fd),
addr_as_object(addr, fd, space)])
except SocketError as e:
converted_error(space, e, eintr_retry=True)
# convert an Address into an app-level object
def addr_as_object(self, space, address):
return addr_as_object(address, self.sock.fd, space)
# convert an app-level object into an Address
# based on the current socket's family
def addr_from_object(self, space, w_address):
fd = intmask(self.sock.fd)
return addr_from_object(self.sock.family, fd, space, w_address)
def bind_w(self, space, w_addr):
"""bind(address)
Bind the socket to a local address. For IP sockets, the address is a
pair (host, port); the host must refer to the local host. For raw packet
sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
"""
try:
self.sock.bind(self.addr_from_object(space, w_addr))
except SocketError as e:
raise converted_error(space, e)
def close_w(self, space):
"""close()
Close the socket. It cannot be used after this call.
"""
try:
self.sock.close()
except SocketError:
# cpython doesn't return any errors on close
pass
self.may_unregister_rpython_finalizer(space)
def connect_w(self, space, w_addr):
"""connect(address)
Connect the socket to a remote address. For IP sockets, the address
is a pair (host, port).
"""
while True:
try:
self.sock.connect(self.addr_from_object(space, w_addr))
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
def connect_ex_w(self, space, w_addr):
"""connect_ex(address) -> errno
This is like connect(address), but returns an error code (the errno value)
instead of raising an exception when an error occurs.
"""
try:
addr = self.addr_from_object(space, w_addr)
except SocketError as e:
raise converted_error(space, e)
while True:
error = self.sock.connect_ex(addr)
if error != errno.EINTR:
break
space.getexecutioncontext().checksignals()
return space.newint(error)
def fileno_w(self, space):
"""fileno() -> integer
Return the integer file descriptor of the socket.
"""
return space.newint(intmask(self.sock.fd))
def detach_w(self, space):
"""detach()
Close the socket object without closing the underlying file descriptor.
The object cannot be used after this call, but the file descriptor
can be reused for other purposes. The file descriptor is returned."""
fd = self.sock.detach()
return space.newint(intmask(fd))
def getpeername_w(self, space):
"""getpeername() -> address info
Return the address of the remote endpoint. For IP sockets, the address
info is a pair (hostaddr, port).
"""
try:
addr = self.sock.getpeername()
return addr_as_object(addr, self.sock.fd, space)
except SocketError as e:
raise converted_error(space, e)
def getsockname_w(self, space):
"""getsockname() -> address info
Return the address of the local endpoint. For IP sockets, the address
info is a pair (hostaddr, port).
"""
try:
addr = self.sock.getsockname()
return addr_as_object(addr, self.sock.fd, space)
except SocketError as e:
raise converted_error(space, e)
@unwrap_spec(level=int, optname=int, buflen=int)
def getsockopt_w(self, space, level, optname, buflen=0):
"""getsockopt(level, option[, buffersize]) -> value
Get a socket option. See the Unix manual for level and option.
If a nonzero buffersize argument is given, the return value is a
string of that length; otherwise it is an integer.
"""
if buflen == 0:
try:
return space.newint(self.sock.getsockopt_int(level, optname))
except SocketError as e:
raise converted_error(space, e)
if buflen < 0 or buflen > 1024:
raise explicit_socket_error(space, "getsockopt buflen out of range")
return space.newbytes(self.sock.getsockopt(level, optname, buflen))
def gettimeout_w(self, space):
"""gettimeout() -> timeout
Returns the timeout in floating seconds associated with socket
operations. A timeout of None indicates that timeouts on socket
"""
timeout = self.sock.gettimeout()
if timeout < 0.0:
return space.w_None
return space.newfloat(timeout)
@unwrap_spec(backlog="c_int")
def listen_w(self, space, backlog=min(SOMAXCONN, 128)):
"""listen(backlog)
Enable a server to accept connections. The backlog argument must be at
least 0 (if it is lower, it is set to 0); it specifies the number of
unaccepted connection that the system will allow before refusing new
connections. If not specified, a default reasonable value is chosen.
"""
if backlog < 0:
backlog = 0
try:
self.sock.listen(backlog)
except SocketError as e:
raise converted_error(space, e)
@unwrap_spec(buffersize='nonnegint', flags=int)
def recv_w(self, space, buffersize, flags=0):
"""recv(buffersize[, flags]) -> data
Receive up to buffersize bytes from the socket. For the optional flags
argument, see the Unix manual. When no data is available, block until
at least one byte is available or until the remote end is closed. When
the remote end is closed and all data is read, return the empty string.
"""
while True:
try:
data = self.sock.recv(buffersize, flags)
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
return space.newbytes(data)
@unwrap_spec(buffersize='nonnegint', flags=int)
def recvfrom_w(self, space, buffersize, flags=0):
"""recvfrom(buffersize[, flags]) -> (data, address info)
Like recv(buffersize, flags) but also return the sender's address info.
"""
while True:
try:
data, addr = self.sock.recvfrom(buffersize, flags)
if addr:
w_addr = addr_as_object(addr, self.sock.fd, space)
else:
w_addr = space.w_None
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
return space.newtuple([space.newbytes(data), w_addr])
@unwrap_spec(message_size=int, ancbufsize=int, flags=int)
def recvmsg_w(self, space, message_size, ancbufsize=0, flags=0):
"""
recvmsg(message_size[, ancbufsize[, flags]]) -> (message, ancillary, flags, address)
Receive normal data (up to bufsize bytes) and ancillary data from the socket.
The ancbufsize argument sets the size in bytes of the internal buffer used to receive the ancillary data;
it defaults to 0, meaning that no ancillary data will be received.
Appropriate buffer sizes for ancillary data can be calculated using CMSG_SPACE() or CMSG_LEN(),
and items which do not fit into the buffer might be truncated or discarded.
The flags argument defaults to 0 and has the same meaning as for recv().
The ancdata item is a list of zero or more tuples (cmsg_level, cmsg_type, cmsg_data):
cmsg_level and cmsg_type are integers specifying the protocol level and protocol-specific type respectively,
and cmsg_data is a bytes object holding the associated data.
:param space: Non useable parameter. It represents the object space.
:param message_size: Maximum size of the message to be received
:param ancbufsize: Maximum size of the ancillary data to be received
:param flags: Receive flag. For more details, please check the Unix manual
:return: a tuple consisting of the message, the ancillary data, return flag and the address.
"""
if message_size < 0:
raise oefmt(space.w_ValueError, "negative buffer size in recvmsg()")
if ancbufsize < 0:
raise oefmt(space.w_ValueError, "invalid ancillary data buffer length")
while True:
try:
recvtup = self.sock.recvmsg(message_size, ancbufsize, flags)
w_message = space.newbytes(recvtup[0])
anclist = []
for l in recvtup[1]:
tup = space.newtuple([space.newint(l[0]), space.newint(l[1]), space.newbytes(l[2])])
anclist.append(tup)
w_anc = space.newlist(anclist)
w_flag = space.newint(recvtup[2])
if (recvtup[3] is not None):
w_address = addr_as_object(recvtup[3], self.sock.fd, space)
else:
w_address = space.w_None
rettup = space.newtuple([w_message, w_anc, w_flag, w_address])
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
return rettup
@unwrap_spec(data='bufferstr', flags=int)
def send_w(self, space, data, flags=0):
"""send(data[, flags]) -> count
Send a data string to the socket. For the optional flags
argument, see the Unix manual. Return the number of bytes
sent; this may be less than len(data) if the network is busy.
"""
while True:
try:
count = self.sock.send(data, flags)
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
return space.newint(count)
@unwrap_spec(data='bufferstr', flags=int)
def sendall_w(self, space, data, flags=0):
"""sendall(data[, flags])
Send a data string to the socket. For the optional flags
argument, see the Unix manual. This calls send() repeatedly
until all data is sent. If an error occurs, it's impossible
to tell how much data has been sent.
"""
try:
self.sock.sendall(
data, flags, space.getexecutioncontext().checksignals)
except SocketError as e:
raise converted_error(space, e)
def sendto_w(self, space, w_data, w_param2, w_param3=None):
"""sendto(data[, flags], address) -> count
Like send(data, flags) but allows specifying the destination address.
For IP sockets, the address is a pair (hostaddr, port).
"""
data = space.charbuf_w(w_data)
if w_param3 is None:
# 2 args version
flags = 0
w_addr = w_param2
else:
# 3 args version
flags = space.int_w(w_param2)
w_addr = w_param3
while True:
try:
addr = self.addr_from_object(space, w_addr)
count = self.sock.sendto(data, len(data), flags, addr)
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
return space.newint(count)
@unwrap_spec(flags=int)
def sendmsg_w(self, space, w_data, w_ancillary=None, flags=0 ,w_address=None):
"""
sendmsg(data[,ancillary[,flags[,address]]]) -> bytes_sent
Send normal and ancillary data to the socket, gathering the non-ancillary data
from a series of buffers and concatenating it into a single message.
The ancdata argument specifies the ancillary data (control messages) as an iterable of zero or more tuples
(cmsg_level, cmsg_type, cmsg_data), where cmsg_level and cmsg_type are integers specifying the protocol level
and protocol-specific type respectively, and cmsg_data is a bytes-like object holding the associated data.
:param space: Represents the object space.
:param w_data: The message(s). needs to be a bytes like object
:param w_ancillary: needs to be a sequence object Can remain unspecified.
:param w_flags: needs to be an integer. Can remain unspecified.
:param w_address: needs to be a bytes-like object Can remain unspecified.
:return: Bytes sent from the message
"""
# Get the flag and address from the object space
while True:
try:
address = None
if not space.is_none(w_address):
address = self.addr_from_object(space, w_address)
# find data's type in the ObjectSpace and get a list of string out of it.
data = []
data_iter = space.unpackiterable(w_data)
for i in data_iter:
data.append(space.readbuf_w(i).as_str())
# find the ancillary's type in the ObjectSpace and get a list of tuples out of it.
ancillary = []
if w_ancillary is not None:
anc_iter = space.unpackiterable(w_ancillary)
for w_i in anc_iter:
if not space.isinstance_w(w_i, space.w_tuple):
raise oefmt(space.w_TypeError, "[sendmsg() ancillary data items]() argument must be sequence")
if space.len_w(w_i) == 3:
intemtup = space.unpackiterable(w_i)
level = space.int_w(intemtup[0])
type = space.int_w(intemtup[1])
cont = space.readbuf_w(intemtup[2]).as_str()
tup = (level, type, cont)
ancillary.append(tup)
else:
raise oefmt(space.w_TypeError,
"[sendmsg() ancillary data items]() argument must be sequence of length 3")
count = self.sock.sendmsg(data, ancillary, flags, address)
if count < 0:
if (count == -1000):
raise oefmt(space.w_OSError, "sending multiple control messages not supported")
if (count == -1001):
raise oefmt(space.w_OSError, "ancillary data item too large")
if (count == -1002):
raise oefmt(space.w_OSError, "too much ancillary data")
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
return space.newint(count)
@unwrap_spec(flag=int)
def setblocking_w(self, flag):
"""setblocking(flag)
Set the socket to blocking (flag is true) or non-blocking (false).
setblocking(True) is equivalent to settimeout(None);
setblocking(False) is equivalent to settimeout(0.0).
"""
self.sock.setblocking(bool(flag))
@unwrap_spec(level=int, optname=int)
def setsockopt_w(self, space, level, optname, w_optval):
"""setsockopt(level, option, value)
Set a socket option. See the Unix manual for level and option.
The value argument can either be an integer or a string.
"""
try:
optval = space.c_int_w(w_optval)
except OperationError as e:
if e.async(space):
raise
optval = space.bytes_w(w_optval)
try:
self.sock.setsockopt(level, optname, optval)
except SocketError as e:
raise converted_error(space, e)
return
try:
self.sock.setsockopt_int(level, optname, optval)
except SocketError as e:
raise converted_error(space, e)
def settimeout_w(self, space, w_timeout):
"""settimeout(timeout)
Set a timeout on socket operations. 'timeout' can be a float,
giving in seconds, or None. Setting a timeout of None disables
the timeout feature and is equivalent to setblocking(1).
Setting a timeout of zero is the same as setblocking(0).
"""
if space.is_w(w_timeout, space.w_None):
timeout = -1.0
else:
timeout = space.float_w(w_timeout)
if timeout < 0.0:
raise oefmt(space.w_ValueError, "Timeout value out of range")
self.sock.settimeout(timeout)
@unwrap_spec(nbytes=int, flags=int)
def recv_into_w(self, space, w_buffer, nbytes=0, flags=0):
"""recv_into(buffer, [nbytes[, flags]]) -> nbytes_read
A version of recv() that stores its data into a buffer rather than creating
a new string. Receive up to buffersize bytes from the socket. If buffersize
is not specified (or 0), receive up to the size available in the given buffer.
See recv() for documentation about the flags.
"""
rwbuffer = space.writebuf_w(w_buffer)
lgt = rwbuffer.getlength()
if nbytes < 0:
raise oefmt(space.w_ValueError, "negative buffersize in recv_into")
if nbytes == 0:
nbytes = lgt
if lgt < nbytes:
raise oefmt(space.w_ValueError, "buffer too small for requested bytes")
while True:
try:
nbytes_read = self.sock.recvinto(rwbuffer, nbytes, flags)
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
return space.newint(nbytes_read)
@unwrap_spec(nbytes=int, flags=int)
def recvfrom_into_w(self, space, w_buffer, nbytes=0, flags=0):
"""recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)
Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.
"""
rwbuffer = space.writebuf_w(w_buffer)
lgt = rwbuffer.getlength()
if nbytes == 0:
nbytes = lgt
elif nbytes > lgt:
raise oefmt(space.w_ValueError,
"nbytes is greater than the length of the buffer")
while True:
try:
readlgt, addr = self.sock.recvfrom_into(rwbuffer, nbytes, flags)
break
except SocketError as e:
converted_error(space, e, eintr_retry=True)
if addr:
try:
w_addr = addr_as_object(addr, self.sock.fd, space)
except SocketError as e:
raise converted_error(space, e)
else:
w_addr = space.w_None
return space.newtuple([space.newint(readlgt), w_addr])
@unwrap_spec(cmd=int)
def ioctl_w(self, space, cmd, w_option):
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import rwin32
from rpython.rlib.rsocket import _c
recv_ptr = lltype.malloc(rwin32.LPDWORD.TO, 1, flavor='raw')
try:
if cmd == _c.SIO_RCVALL:
value_size = rffi.sizeof(rffi.INTP)
elif cmd == _c.SIO_KEEPALIVE_VALS:
value_size = rffi.sizeof(_c.tcp_keepalive)
else:
raise oefmt(space.w_ValueError,
"invalid ioctl command %d", cmd)
value_ptr = lltype.malloc(rffi.VOIDP.TO, value_size, flavor='raw')
try:
if cmd == _c.SIO_RCVALL:
option_ptr = rffi.cast(rffi.INTP, value_ptr)
option_ptr[0] = space.int_w(w_option)
elif cmd == _c.SIO_KEEPALIVE_VALS:
w_onoff, w_time, w_interval = space.unpackiterable(w_option, 3)
option_ptr = rffi.cast(lltype.Ptr(_c.tcp_keepalive), value_ptr)
option_ptr.c_onoff = space.uint_w(w_onoff)
option_ptr.c_keepalivetime = space.uint_w(w_time)
option_ptr.c_keepaliveinterval = space.uint_w(w_interval)
res = _c.WSAIoctl(
self.sock.fd, cmd, value_ptr, value_size,
rffi.NULL, 0, recv_ptr, rffi.NULL, rffi.NULL)
if res < 0:
raise converted_error(space, rsocket.last_error())
finally:
if value_ptr:
lltype.free(value_ptr, flavor='raw')
return space.newint(recv_ptr[0])
finally:
lltype.free(recv_ptr, flavor='raw')
@unwrap_spec(how="c_int")
def shutdown_w(self, space, how):
"""shutdown(flag)
Shut down the reading side of the socket (flag == SHUT_RD), the
writing side of the socket (flag == SHUT_WR), or both ends
(flag == SHUT_RDWR).
"""
try:
self.sock.shutdown(how)
except SocketError as e:
raise converted_error(space, e)
#------------------------------------------------------------
# Support functions for socket._socketobject
usecount = 1
def _reuse_w(self):
"""_resue()
Increase the usecount of the socketobject.
Intended only to be used by socket._socketobject
"""
self.usecount += 1
def _drop_w(self, space):
"""_drop()
Decrease the usecount of the socketobject. If the
usecount reaches 0 close the socket.
Intended only to be used by socket._socketobject
"""
self.usecount -= 1
if self.usecount > 0:
return
self.close_w(space)
# ____________________________________________________________
# Automatic shutdown()/close()
# On some systems, the C library does not guarantee that when the program
# finishes, all data sent so far is really sent even if the socket is not
# explicitly closed. This behavior has been observed on Windows but not
# on Linux, so far.
NEED_EXPLICIT_CLOSE = (sys.platform == 'win32')
class OpenRSockets(rweaklist.RWeakListMixin):
pass
class OpenRSocketsState:
def __init__(self, space):
self.openrsockets = OpenRSockets()
self.openrsockets.initialize()
def getopenrsockets(space):
if NEED_EXPLICIT_CLOSE and space.config.translation.rweakref:
return space.fromcache(OpenRSocketsState).openrsockets
else:
return None
def register_socket(space, socket):
openrsockets = getopenrsockets(space)
if openrsockets is not None:
openrsockets.add_handle(socket)
def close_all_sockets(space):
openrsockets = getopenrsockets(space)
if openrsockets is not None:
for sock_wref in openrsockets.get_all_handles():
sock = sock_wref()
if sock is not None:
try:
sock.close()
except SocketError:
pass
# ____________________________________________________________
# Error handling
class SocketAPI:
def __init__(self, space):
self.w_error = space.w_OSError
self.w_herror = space.new_exception_class(
"_socket.herror", self.w_error)
self.w_gaierror = space.new_exception_class(
"_socket.gaierror", self.w_error)
self.w_timeout = space.new_exception_class(
"_socket.timeout", self.w_error)
self.errors_w = {'error': self.w_error,
'herror': self.w_herror,
'gaierror': self.w_gaierror,
'timeout': self.w_timeout,
}
def get_exception(self, applevelerrcls):
return self.errors_w[applevelerrcls]
def get_error(space, name):
return space.fromcache(SocketAPI).get_exception(name)
@specialize.arg(2)
def converted_error(space, e, eintr_retry=False):
message = e.get_msg_unicode()
w_exception_class = get_error(space, e.applevelerrcls)
if isinstance(e, SocketErrorWithErrno):
if e.errno == errno.EINTR:
space.getexecutioncontext().checksignals()
if eintr_retry:
return # only return None if eintr_retry==True
w_exception = space.call_function(w_exception_class, space.newint(e.errno),
space.newunicode(message))
else:
w_exception = space.call_function(w_exception_class, space.newunicode(message))
raise OperationError(w_exception_class, w_exception)
def explicit_socket_error(space, msg):
w_exception_class = space.fromcache(SocketAPI).w_error
w_exception = space.call_function(w_exception_class, space.newtext(msg))
return OperationError(w_exception_class, w_exception)
# ____________________________________________________________
socketmethodnames = """
_accept bind close connect connect_ex fileno detach
getpeername getsockname getsockopt gettimeout listen
recv recvfrom send sendall sendto setblocking
setsockopt settimeout shutdown _reuse _drop recv_into recvfrom_into
""".split()
if hasattr(rsocket._c, 'WSAIoctl'):
socketmethodnames.append('ioctl')
if rsocket._c.HAVE_SENDMSG:
socketmethodnames.append('sendmsg')
socketmethodnames.append('recvmsg')
socketmethods = {}
for methodname in socketmethodnames:
method = getattr(W_Socket, methodname + '_w')
socketmethods[methodname] = interp2app(method)
W_Socket.typedef = TypeDef("_socket.socket",
__doc__ = """\
socket([family[, type[, proto]]]) -> socket object
Open a socket of the given type. The family argument specifies the
address family; it defaults to AF_INET. The type argument specifies
whether this is a stream (SOCK_STREAM, this is the default)
or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,
specifying the default protocol. Keyword arguments are accepted.
A socket object represents one endpoint of a network connection.
Methods of socket objects (keyword arguments not allowed):
_accept() -- accept a connection, returning new socket fd and client address
bind(addr) -- bind the socket to a local address
close() -- close the socket
connect(addr) -- connect the socket to a remote address
connect_ex(addr) -- connect, return an error code instead of an exception
fileno() -- return underlying file descriptor
getpeername() -- return remote address [*]
getsockname() -- return local address
getsockopt(level, optname[, buflen]) -- get socket options
gettimeout() -- return timeout or None
listen(n) -- start listening for incoming connections
recv(buflen[, flags]) -- receive data
recvfrom(buflen[, flags]) -- receive data and sender's address
sendall(data[, flags]) -- send all data
send(data[, flags]) -- send data, may not send all of it
sendto(data[, flags], addr) -- send data to a given address
sendmsg(messages[, ancillary[, flags[, address]]]) -- send data and ancillary payload in a packet. May specifiy flags or the address
recvmsg(message_size,[ ancillary_size,[ flags]]) -- receive data and ancillary payload. Return a tup of message, ancdata, flags and address
setblocking(0 | 1) -- set or clear the blocking I/O flag
setsockopt(level, optname, value) -- set socket options
settimeout(None | float) -- set or clear the timeout
shutdown(how) -- shut down traffic in one or both directions
[*] not available on all platforms!""",
__new__ = generic_new_descr(W_Socket),
__init__ = interp2app(W_Socket.descr_init),
__repr__ = interp2app(W_Socket.descr_repr),
type = GetSetProperty(W_Socket.get_type_w),
proto = GetSetProperty(W_Socket.get_proto_w),
family = GetSetProperty(W_Socket.get_family_w),
** socketmethods
)
|