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 addr_from_object(family, space, w_address): if family == rsocket.AF_INET: w_host, w_port = space.unpackiterable(w_address, 2) host = space.str_w(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 TypeError("AF_INET6 address must be a tuple of length 2 " "to 4, not %d" % len(pieces_w)) host = space.str_w(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: return rsocket.UNIXAddress(space.str_w(w_address)) 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)) raise RSocketError("unknown address family")
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")
def addr_from_object(family, fd, space, w_address): if family == rsocket.AF_INET: w_host, w_port = space.unpackiterable(w_address, 2) host = space.str_w(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 = space.str_w(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: return rsocket.UNIXAddress(space.str_w(w_address)) 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.str_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.str_w(pieces_w[4]) else: haddr = "" if len(haddr) > 8: raise OperationError(space.w_ValueError, space.wrap( "Hardware address must be 8 bytes or less")) if protocol < 0 or protocol > 0xfffff: raise OperationError(space.w_OverflowError, space.wrap( "protoNumber must be 0-65535.")) return rsocket.PacketAddress(ifindex, protocol, pkttype, hatype, haddr) raise RSocketError("unknown address family")