示例#1
0
def get_addr(hostname, socktype, protocol, port, address_to_fill):
    hostent = _c.gethostbyname(hostname)

    if not hostent:
        raise GAIError(EAI_FAIL)

    hname, aliases, address_list = gethost_common("", hostent)

    result = []

    for address in address_list:
        if address.family == _c.AF_INET:
            a = address.lock(_c.sockaddr_in)
            rffi.setintfield(a, 'c_sin_port', port & 0xffff)
            address.unlock()
        a = address.lock()
        addr = make_address(a, address.addrlen, address_to_fill)
        address.unlock()
        result.append((
            address.family,
            socktype,
            protocol,
            "",  # XXX canonname?
            addr))

    return result
示例#2
0
def get_addr(hostname, socktype, protocol, port, address_to_fill):
    hostent = _c.gethostbyname(hostname)

    if not hostent:
        raise GAIError(EAI_FAIL)

    hname, aliases, address_list = gethost_common("", hostent)
        
    result = []

    for address in address_list:
        if address.family == _c.AF_INET:
            a = address.lock(_c.sockaddr_in)
            rffi.setintfield(a, 'c_sin_port', r_uint(port) & 0xffff)
            address.unlock()
        a = address.lock()
        addr = make_address(a, address.addrlen, address_to_fill)
        address.unlock()
        result.append((address.family,
                       socktype,
                       protocol,
                       "", # XXX canonname?
                       addr))

    return result
示例#3
0
def get_name(hostname, addr, addrlen):
    hostent = _c.gethostbyaddr(pointer(addr), addrlen, _c.AF_INET)

    # if reverse lookup fail,
    # return address anyway to pacify calling application.
    if not hostent:
        return hostname

    hname, aliases, address_list = gethost_common("", hostent)
    if hostent and hostent.contents.h_name and hostent.contents.h_addr_list[0]:
        return hostent.contents.h_name
    else:
        return hostname
示例#4
0
def getnameinfo(address, flags):
    if address.family != _c.AF_INET:
        raise RSocketError("unknown address family")

    sockaddr = address.lock(_c.sockaddr_in)
    try:
        sin_port = sockaddr.c_sin_port
        sin_addr = sockaddr.c_sin_addr

        v4a = rffi.cast(lltype.Unsigned, _c.ntohl(sin_addr.c_s_addr))
        if (v4a & r_uint(0xf0000000) == r_uint(0xe0000000) or  # IN_MULTICAST()
                v4a & r_uint(0xe0000000)
                == r_uint(0xe0000000)):  # IN_EXPERIMENTAL()
            flags |= NI_NUMERICHOST
        # XXX Why does this work in CPython?
        # v4a >>= 24 # = IN_CLASSA_NSHIFT
        # if v4a in (0, 127): # = IN_LOOPBACKNET
        #     flags |= NI_NUMERICHOST
        numsize = _c.INET_ADDRSTRLEN

        serv = _getservicename(sin_port, flags)

        if not (flags & NI_NUMERICHOST):
            p = rffi.cast(rffi.VOIDP, sin_addr)
            hostent = _c.gethostbyaddr(p, rffi.sizeof(_c.in_addr),
                                       sockaddr.c_sin_family)
        else:
            hostent = None

        if hostent:
            from pypy.rlib.rsocket import gethost_common
            host, _, _ = gethost_common("", hostent)
        else:
            host = rffi.charp2str(_c.inet_ntoa(sin_addr))

    finally:
        address.unlock()

    return host, serv
示例#5
0
def get_addr(hostname, socktype, protocol, port, address_to_fill):
    hostent = _c.gethostbyname(hostname)

    if not hostent:
        raise GAIError(EAI_FAIL)

    hname, aliases, address_list = gethost_common("", hostent)
        
    result = []

    for address in address_list:
        if address.addr.sa_family == _c.AF_INET:
            a = cast(pointer(address.addr), POINTER(_c.sockaddr_in)).contents
            a.sin_port = port & 0xffff
        addr = make_address(pointer(address.addr),address.addrlen,address_to_fill)
        result.append((address.addr.sa_family,
                       socktype,
                       protocol,
                       "", # XXX canonname?
                       addr))

    return result
示例#6
0
def getnameinfo(address, flags):
    if address.family != _c.AF_INET:
        raise RSocketError("unknown address family")

    sockaddr = address.lock(_c.sockaddr_in)
    try:
        sin_port = sockaddr.c_sin_port
        sin_addr = sockaddr.c_sin_addr

        v4a = rffi.cast(lltype.Unsigned, _c.ntohl(sin_addr.c_s_addr))
        if (v4a & r_uint(0xf0000000) == r_uint(0xe0000000) or # IN_MULTICAST()
            v4a & r_uint(0xe0000000) == r_uint(0xe0000000)):  # IN_EXPERIMENTAL()
            flags |= NI_NUMERICHOST
        # XXX Why does this work in CPython?
        # v4a >>= 24 # = IN_CLASSA_NSHIFT
        # if v4a in (0, 127): # = IN_LOOPBACKNET
        #     flags |= NI_NUMERICHOST
        numsize = _c.INET_ADDRSTRLEN

        serv = _getservicename(sin_port, flags)

        if not (flags & NI_NUMERICHOST):
            p = rffi.cast(rffi.VOIDP, sin_addr)
            hostent = _c.gethostbyaddr(p, rffi.sizeof(_c.in_addr),
                                       sockaddr.c_sin_family)
        else:
            hostent = None

        if hostent:
            from pypy.rlib.rsocket import gethost_common
            host, _, _ = gethost_common("", hostent)
        else:
            host = rffi.charp2str(_c.inet_ntoa(sin_addr))

    finally:
        address.unlock()

    return host, serv
示例#7
0
def getnameinfo(_addr, flags):
    addr = _addr.addr

    if addr.sa_family != _c.AF_INET:
        raise RSocketError("unknown address family")

    sockaddr = cast(pointer(addr), POINTER(_c.sockaddr_in)).contents
    sin_port = sockaddr.sin_port
    sin_addr = sockaddr.sin_addr

    v4a = _c.ntohl(sin_addr.s_addr)
    if (v4a & 0xf0000000 == 0xe0000000 or # IN_MULTICAST()
        v4a & 0xe0000000 == 0xe0000000):  # IN_EXPERIMENTAL()
        flags |= NI_NUMERICHOST
    # XXX Why does this work in CPython?
    # v4a >>= 24 # = IN_CLASSA_NSHIFT
    # if v4a in (0, 127): # = IN_LOOPBACKNET
    #     flags |= NI_NUMERICHOST
    numsize = _c.INET_ADDRSTRLEN

    serv = _getservicename(sin_port, flags)

    if not (flags & NI_NUMERICHOST):
        hostent = _c.gethostbyaddr(pointer(sin_addr), sizeof(_c.in_addr), addr.sa_family)
    else:
        hostent = None

    if hostent:
        from pypy.rlib.rsocket import gethost_common
        host, _, _ = gethost_common("", hostent)
    else:
        from pypy.rlib.rsocket import copy_buffer
        host = _c.inet_ntoa(sin_addr)
        #buf = copy_buffer(ptr, len(ptr))
        #host = buf.raw
        
    return host, serv