Commits

Philip Jenvey  committed afdc189

tighten getnameinfo to only accept IP addresses

  • Participants
  • Parent commits bcbd82a
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/module/_socket/interp_func.py

 from pypy.interpreter.gateway import unwrap_spec, WrappedDefault
-from pypy.module._socket.interp_socket import converted_error, W_RSocket, addr_as_object, ipaddr_from_object
+from pypy.module._socket.interp_socket import (
+    converted_error, W_RSocket, addr_as_object, ipaddr_from_object, get_error)
 from rpython.rlib import rsocket
 from rpython.rlib.rsocket import SocketError, INVALID_SOCKET
 from pypy.interpreter.error import OperationError
 
     Get host and port for a sockaddr."""
     try:
-        addr = ipaddr_from_object(space, w_sockaddr)
+        w_host, w_port = space.fixedview(w_sockaddr, 2)
+        host = space.str_w(w_host)
+        port = str(space.int_w(w_port))
+        lst = rsocket.getaddrinfo(host, port, rsocket.AF_UNSPEC,
+                                  rsocket.SOCK_DGRAM, 0,
+                                  rsocket.AI_NUMERICHOST)
+        if len(lst) > 1:
+            raise OperationError(get_error(space, 'error'),
+                                 "sockaddr resolved to multiple addresses")
+        addr = lst[0][4]
         host, servport = rsocket.getnameinfo(addr, flags)
     except SocketError, e:
         raise converted_error(space, e)

File pypy/module/_socket/test/test_sock_app.py

         ])))
 
 def test_getnameinfo():
+    from pypy.module._socket.interp_socket import get_error
     host = "127.0.0.1"
     port = 25
     info = socket.getnameinfo((host, port), 0)
     w_l = space.appexec([w_socket, space.wrap(host), space.wrap(port)],
                         "(_socket, host, port): return _socket.getnameinfo((host, port), 0)")
     assert space.unwrap(w_l) == info
+    sockaddr = space.newtuple([space.wrap('mail.python.org'), space.wrap(0)])
+    space.raises_w(get_error(space, 'error'), space.appexec,
+                   [w_socket, sockaddr, space.wrap(0)],
+                   "(_socket, sockaddr, flags): return _socket.getnameinfo(sockaddr, flags)")
+
 
 def test_timeout():
     space.appexec([w_socket, space.wrap(25.4)],