def inet_ntoa(packed): "packet 32-bits string -> IPv4 dotted string" if len(packed) != sizeof(_c.in_addr): raise RSocketError("packed IP wrong length for inet_ntoa") buf = rffi.make(_c.in_addr) try: for i in range(sizeof(_c.in_addr)): rffi.cast(rffi.CCHARP, buf)[i] = packed[i] return rffi.charp2str(_c.inet_ntoa(buf)) finally: lltype.free(buf, flavor='raw')
def test_call_with_struct_argument(self): # XXX is there such a function in the standard C headers? from pypy.rlib import _rsocket_rffi buf = rffi.make(_rsocket_rffi.in_addr) rffi.cast(rffi.CCHARP, buf)[0] = '\x01' rffi.cast(rffi.CCHARP, buf)[1] = '\x02' rffi.cast(rffi.CCHARP, buf)[2] = '\x03' rffi.cast(rffi.CCHARP, buf)[3] = '\x04' p = _rsocket_rffi.inet_ntoa(buf) assert rffi.charp2str(p) == '1.2.3.4' lltype.free(buf, flavor='raw') assert not ALLOCATED # detects memory leaks in the test
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