Ticket #5086: fix-ipv6-literals-udp.8.patch

File fix-ipv6-literals-udp.8.patch, 7.5 KB (added by marto1_, 3 years ago)

Got the patch wrong, here is without the old stuff

  • twisted/test/test_udp.py

     
    267267        return d
    268268
    269269
     270
    270271    def test_connectionRefused(self):
    271272        """
    272273        A L{ConnectionRefusedError} exception is raised when a connection
  • twisted/topfiles/5086.feature

     
     1IReactorUDP.listenUDP, IUDPTransport.write and IUDPTransport.connect now accept ipv6 address literals.
     2 No newline at end of file
  • twisted/internet/udp.py

     
    8080        self.interface = interface
    8181        self.setLogStr()
    8282        self._connectedAddr = None
     83        self._setAddressFamily()
    8384
    8485    def __repr__(self):
    8586        if self._realPortNumber is not None:
     
    207208        """
    208209        if self._connectedAddr:
    209210            raise RuntimeError("already connected, reconnecting is not currently supported")
    210         if not abstract.isIPAddress(host):
     211        if not abstract.isIPAddress(host) and not abstract.isIPv6Address(host):
    211212            raise ValueError("please pass only IP addresses, not domain names")
    212213        self._connectedAddr = (host, port)
    213214        self.socket.connect((host, port))
     
    252253        logPrefix = self._getLogPrefix(self.protocol)
    253254        self.logstr = "%s (UDP)" % logPrefix
    254255
     256    def _setAddressFamily(self):
     257        """
     258        Resolve address family for the socket.
     259        """
     260        if abstract.isIPv6Address(self.interface):
     261            self.addressFamily = socket.AF_INET6
     262        elif abstract.isIPAddress(self.interface):
     263            self.addressFamily = socket.AF_INET
     264       
    255265
     266
    256267    def logPrefix(self):
    257268        """
    258269        Return the prefix to log with.
     
    262273
    263274    def getHost(self):
    264275        """
    265         Returns an IPv4Address.
     276        Returns an IPv4Address or IPv6Address.
    266277
    267278        This indicates the address from which I am connecting.
    268279        """
    269         return address.IPv4Address('UDP', *self.socket.getsockname())
     280        addr = self.socket.getsockname()
     281        if self.addressFamily == socket.AF_INET:
     282            return address.IPv4Address('UDP', *addr)
     283        elif self.addressFamily == socket.AF_INET6:
     284            return address.IPv6Address('UDP', *(addr[:2]))
    270285
    271286
    272287
     
    332347    UDP Port that supports multicasting.
    333348    """
    334349
    335     def __init__(self, port, proto, interface='', maxPacketSize=8192, reactor=None, listenMultiple=False):
     350    def __init__(self, port, proto, interface='', maxPacketSize=8192,
     351                 reactor=None, listenMultiple=False):
    336352        """
    337353        @see: L{twisted.internet.interfaces.IReactorMulticast.listenMulticast}
    338354        """
  • twisted/internet/test/test_udp.py

     
    2020from twisted.internet.defer import Deferred, maybeDeferred
    2121from twisted.internet.interfaces import (
    2222    ILoggingContext, IListeningPort, IReactorUDP)
    23 from twisted.internet.address import IPv4Address
     23from twisted.internet.address import IPv4Address, IPv6Address
    2424from twisted.internet.protocol import DatagramProtocol
    2525
    2626from twisted.internet.test.connectionmixins import (LogObserverMixin,
    2727                                                    findFreePort)
     28from twisted.internet import protocol, error, defer, interfaces, udp
     29from twisted.test.test_udp import Server, GoodClient
    2830
    2931
    3032class UDPPortMixin(object):
     
    4850        """
    4951        return "(UDP Port %s Closed)" % (port.getHost().port,)
    5052
     53    def getIPv6ListeningPort(self, reactor, protocol):
     54        """
     55        Get a UDP port binded to ipv6 interface.
     56        """
     57        return reactor.listenUDP(0, protocol, "::1")
    5158
    5259
     60
    5361class DatagramTransportTestsMixin(LogObserverMixin):
    5462    """
    5563    Mixin defining tests which apply to any port/datagram based transport.
     
    162170        self.assertEqual(
    163171            port.getHost(), IPv4Address('UDP', host, portNumber))
    164172
     173    def test_getHostIPv6(self):
     174        """
     175        L{IListeningPort.getHost} returns an L{IPv6Address} giving a
     176        IPv6 address, the port number that the protocol is listening on
     177        and the port number.
     178        """
     179        reactor = self.buildReactor()
     180        port = self.getIPv6ListeningPort(reactor, DatagramProtocol())
     181        addr = port.getHost()
     182        self.assertEqual(addr.host, "::1")
    165183
     184
    166185    def test_logPrefix(self):
    167186        """
    168187        Datagram transports implement L{ILoggingContext.logPrefix} to return a
     
    215234        port = reactor.listenUDP(0, DatagramProtocol())
    216235        self.assertIn(repr(port.getHost().port), str(port))
    217236
     237    def test_bindToIPv6Interface(self):
     238        """
     239        Binds to ipv6 interface.
     240        """
     241        reactor = self.buildReactor()
     242        server = Server()
     243        p = reactor.listenUDP(0, server, interface="::1")
     244        self.assertEqual(p.getHost().host, "::1")
     245
     246        return p.stopListening()
     247
     248    def test_connectAndWriteToIPv6Interface(self):
     249        """
     250        Connects and writes to ipv6 address.
     251        """
     252
     253        reactor = self.buildReactor()
     254        server = Server()
     255        serverStarted = server.startedDeferred = defer.Deferred()
     256        port1 = reactor.listenUDP(0, server, interface="::1")
     257
     258        client = GoodClient()
     259        clientStarted = client.startedDeferred = defer.Deferred()
     260
     261        def cbServerStarted(ignored):
     262            """Client starts listening"""
     263            self.port2 = reactor.listenUDP(0, client, interface="::1")
     264            return clientStarted
     265
     266        def cbClientStarted(ignored):
     267            """Client sends messages before and after connecting"""
     268            client.transport.write("a",
     269                                   ("::1", server.transport.getHost().port))
     270            client.transport.connect("::1", server.transport.getHost().port)
     271            serverReceived = server.packetReceived = defer.Deferred()
     272            def cbSendAfterConnect(ignored):
     273                serverReceived = server.packetReceived = defer.Deferred()
     274                client.transport.write("hello")
     275                return serverReceived
     276
     277            serverReceived.addCallback(cbSendAfterConnect)
     278
     279            return serverReceived
     280
     281        def cbServerReceived(ignored):
     282            """Assert packets received in server"""
     283            unconnPacket, connPacket = server.packets[0], server.packets[1]
     284            cAddr = client.transport.getHost()
     285            sAddr = server.transport.getHost()
     286            self.assertEqual(unconnPacket, ("a", (cAddr.host, cAddr.port, 0, 0)))
     287            self.assertEqual(connPacket, ("hello",
     288                                          (cAddr.host, cAddr.port, 0, 0)))
     289           
     290
     291        def cbFinished(ignored):
     292            """Stops listening"""
     293            self.port2.stopListening()
     294            port1.stopListening()
     295            reactor.stop()
     296
     297        d = serverStarted.addCallback(cbServerStarted)
     298        d.addCallback(cbClientStarted)
     299        d.addCallback(cbServerReceived)
     300        d.addCallback(cbFinished)
     301        self.runReactor(reactor)
     302        return d
     303
     304
    218305globals().update(UDPServerTestsBuilder.makeTestCaseClasses())