Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
def htonl(space, x):
    """htonl(integer) -> integer

    Convert a 32-bit integer from host to network byte order.
    """
    if x < r_longlong(0):
        raise oefmt(space.w_OverflowError,
                    "can't convert negative number to unsigned long")
    if x > LONGLONG_UINT32_MAX:
        raise oefmt(space.w_OverflowError, "long int larger than 32 bits")
    return space.newint(rsocket.htonl(r_uint32(x)))
Esempio n. 4
0
 def do_send_string(self, space, buf, offset, size):
     # Since str2charp copies the buf anyway, always combine the
     # "header" and the "body" of the message and send them at once.
     message = lltype.malloc(rffi.CCHARP.TO, size + 4, flavor='raw')
     try:
         length = rffi.r_uint(
             rsocket.htonl(rffi.cast(lltype.Unsigned, size)))
         rffi.cast(rffi.UINTP, message)[0] = length
         i = size - 1
         while i >= 0:
             message[4 + i] = buf[offset + i]
             i -= 1
         self._sendall(space, message, size + 4)
     finally:
         lltype.free(message, flavor='raw')
Esempio n. 5
0
 def do_send_string(self, space, buf, offset, size):
     # Since str2charp copies the buf anyway, always combine the
     # "header" and the "body" of the message and send them at once.
     message = lltype.malloc(rffi.CCHARP.TO, size + 4, flavor='raw')
     try:
         length = rffi.r_uint(rsocket.htonl(
                 rffi.cast(lltype.Unsigned, size)))
         rffi.cast(rffi.UINTP, message)[0] = length
         i = size - 1
         while i >= 0:
             message[4 + i] = buf[offset + i]
             i -= 1
         self._sendall(space, message, size + 4)
     finally:
         lltype.free(message, flavor='raw')
Esempio n. 6
0
def htonl(space, x):
    """htonl(integer) -> integer

    Convert a 32-bit integer from host to network byte order.
    """
    return space.wrap(rsocket.htonl(x))
Esempio n. 7
0
def htonl(space, x):
    """htonl(integer) -> integer

    Convert a 32-bit integer from host to network byte order.
    """
    return space.wrap(rsocket.htonl(x))