[Twisted-Python] Setting source address of outgoing datagrams

Maciej Wasilak wasilak at gmail.com
Sat Oct 12 08:27:59 MDT 2013


Hello,

I've got a problem when using Twisted UDP-based app with IPv6. I know UDP
IPv6 is not yet officially supported, I am using the unoffcial hack found
somewhere on the net.

----------------UNOFFICIAL HACK----------------------
In twisted/internet/udp.py:

(...)
    def __init__(self, port, proto, interface='', maxPacketSize=8192,
reactor=None):
        """
        Initialize with a numeric port to listen on.
        """
        base.BasePort.__init__(self, reactor)
        self.port = port
        self.protocol = proto
        self.maxPacketSize = maxPacketSize
        self.interface = interface
        self.setLogStr()
        self._connectedAddr = None
+       if interface and abstract.isIPv6Address(interface): ###
+          self.addressFamily = socket.AF_INET6 ###

(...)
    def doRead(self):
        """
        Called when my socket is ready for reading.
        """
        read = 0
        while read < self.maxThroughput:
            try:
                data, addr = self.socket.recvfrom(self.maxPacketSize)
            except socket.error as se:
                no = se.args[0]
                if no in _sockErrReadIgnore:
                    return
                if no in _sockErrReadRefuse:
                    if self._connectedAddr:
                        self.protocol.connectionRefused()
                    return
                raise
            else:
                read += len(data)
                try:
+                 addr = (addr[0], addr[1]) ###
                    self.protocol.datagramReceived(data, addr)
                except:
                    log.err()
--------------END OF UNOFFICIAL HACK------------------------

I deploy my protocol like that:

reactor.listenUDP(5683, myapp.MyProtocol(), interface="::")
reactor.run()

The problem is:
1. My server receives a request from remote client (with destination
address being valid global IPv6 address)
2. The server sends a response, but uses different source address (which is
also valid and assigned to local wlan0 interface).
3. Remote host drops the response (destination address of the request and
source address of the response do not match).

It is common for IPv6 interfaces to have multiple global IPv6 addresses -
because of privacy reasons (both on Windows and Linux).

My question is - can I read manually the destination address from the
incoming request datagram, and set it as a source address manually in the
outgoing response datagram?

Best Regards
Maciej Wasilak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20131012/c623f869/attachment.html>


More information about the Twisted-Python mailing list