[pypy-svn] r25852 - pypy/dist/pypy/rpython/rctypes/socketmodule

arigo at codespeak.net arigo at codespeak.net
Sat Apr 15 13:02:15 CEST 2006


Author: arigo
Date: Sat Apr 15 13:02:14 2006
New Revision: 25852

Added:
   pypy/dist/pypy/rpython/rctypes/socketmodule/test_addr.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/rctypes/socketmodule/_socket.py
   pypy/dist/pypy/rpython/rctypes/socketmodule/ctypes_socket.py
Log:
Very incomplete implementation of getaddrinfo(), with a test
(only works on on-line machines so far).  The idea is that rctypes
should now support all ctypes constructions that were necessary.
I will start a regular mixed-module _socket based on this, but
first we need to figure out how to best test mixed-modules based
on ctypes -- ideally, they should be testable and compilable
without the rest of the PyPy interpreter...


Modified: pypy/dist/pypy/rpython/rctypes/socketmodule/_socket.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/socketmodule/_socket.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/socketmodule/_socket.py	Sat Apr 15 13:02:14 2006
@@ -1,4 +1,4 @@
-from ctypes import c_char_p, POINTER, byref
+from ctypes import c_char_p, POINTER, byref, cast, create_string_buffer
 import ctypes_socket as _c
 
 
@@ -42,12 +42,21 @@
             XXX
 
 
+def makeipaddr(caddr, caddrlen):
+    buf = create_string_buffer(NI_MAXHOST)
+    error = _c.getnameinfo(caddr, caddrlen, buf, NI_MAXHOST,
+                           c_char_p(), 0, NI_NUMERICHOST)
+    if error:
+        XXX
+    return buf.value
+
 def makesockaddr(caddr, caddrlen, proto):
-    if addrlen == 0:
+    if caddrlen == 0:
         # No address -- may be recvfrom() from known socket
         return None
-    if caddr.sa_family == AF_INET:
-        return makeipaddr(caddr), _c.ntohs(
+    if caddr.contents.sa_family == AF_INET:
+        a = cast(caddr, POINTER(_c.sockaddr_in))
+        return makeipaddr(caddr, caddrlen), _c.ntohs(a.contents.sin_port)
     else:
         XXX
 
@@ -66,12 +75,12 @@
     if error:
         XXX
     try:
-        return _getaddrinfo_chainedlist(res0)
+        return _getaddrinfo_chainedlist(res0, proto)
     finally:
         if res0:
             _c.freeaddrinfo(res0)
 
-def _getaddrinfo_chainedlist(res):
+def _getaddrinfo_chainedlist(res, proto):
     result = []
     while res:
         res = res.contents

Modified: pypy/dist/pypy/rpython/rctypes/socketmodule/ctypes_socket.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/socketmodule/ctypes_socket.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/socketmodule/ctypes_socket.py	Sat Apr 15 13:02:14 2006
@@ -2,7 +2,12 @@
 from pypy.rpython.rctypes import ctypes_platform
 from ctypes import *
 
-includes = ('sys/types.h', 'sys/socket.h', 'netinet/in.h', 'netdb.h')
+includes = ('sys/types.h',
+            'sys/socket.h',
+            'netinet/in.h',
+            'netdb.h',
+            'arpa/inet.h',
+            )
 HEADER = ''.join(['#include <%s>\n' % filename for filename in includes])
 constants = {}
 
@@ -11,10 +16,19 @@
              'AF_UNSPEC',
              'SOCK_STREAM',
              'SOCK_DGRAM',
+             'NI_MAXHOST',
+             'NI_NUMERICHOST',
              ]:
     constants[name] = ctypes_platform.getconstantinteger(name, HEADER)
 
 # types
+uint16_t = ctypes_platform.getsimpletype('uint16_t', HEADER, c_ushort)
+uint32_t = ctypes_platform.getsimpletype('uint32_t', HEADER, c_uint)
+size_t = ctypes_platform.getsimpletype('size_t', HEADER, c_int)
+size_t = ctypes_platform.getsimpletype('size_t', HEADER, c_int)
+socklen_t = ctypes_platform.getsimpletype('socklen_t', HEADER, c_int)
+
+# struct types
 sockaddr = ctypes_platform.getstruct('struct sockaddr', HEADER,
                                      [('sa_family', c_int),
                                       # unknown and variable fields follow
@@ -54,3 +68,25 @@
 freeaddrinfo = socketdll.freeaddrinfo
 freeaddrinfo.argtypes = [POINTER(addrinfo)]
 freeaddrinfo.restype = None
+
+getnameinfo = socketdll.getnameinfo
+getnameinfo.argtypes = [POINTER(sockaddr), socklen_t,
+                        c_char_p, size_t,
+                        c_char_p, size_t, c_int]
+getnameinfo.restype = c_int
+
+htonl = socketdll.htonl
+htonl.argtypes = [uint32_t]
+htonl.restype = uint32_t
+
+htons = socketdll.htonl
+htons.argtypes = [uint16_t]
+htons.restype = uint16_t
+
+ntohl = socketdll.htonl
+ntohl.argtypes = [uint32_t]
+ntohl.restype = uint32_t
+
+ntohs = socketdll.htonl
+ntohs.argtypes = [uint16_t]
+ntohs.restype = uint16_t

Added: pypy/dist/pypy/rpython/rctypes/socketmodule/test_addr.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/socketmodule/test_addr.py	Sat Apr 15 13:02:14 2006
@@ -0,0 +1,10 @@
+from _socket import *
+
+def test_getaddrinfo():
+    lst = getaddrinfo('snake.cs.uni-duesseldorf.de', None)
+    assert isinstance(lst, list)
+    found = False
+    for family, socktype, protocol, canonname, (host, port) in lst:
+        if host == '134.99.112.214':
+            found = True
+    assert found, lst



More information about the Pypy-commit mailing list