Search code examples
pythonsocketsnetwork-programmingtwistedraw-sockets

Is it possible to use raw sockets with Twisted (Python)


I am writing an implementation of an NAT and have the need to use raw sockets. I have become accustomed to the Twisted architecture and like how it handles concurrent connections.

Data coming into a Twisted protocol is manipulated, NATed, tabulated, and sent out the raw socket. Data coming into the raw socket is manipulated, looked up, NATed, and directed to the appropriate protocol instance.

Would having a single raw socket suffice? What if a large number of connections came in at the same time. Doesn't twisted handle that, or is twisted pretty much a non advantage in connectionless protocols. If there is an advantage could anyone direct me to a raw sockets twisted example


Solution

  • Twisted supports connectionless protocols just fine. See, for example, listenUDP.

    There are modules for manipulating IP-level protocol data in twisted.pair, but not all of it works; in particular, tuntap support does not work.

    There are no examples of this that I know of, but as I understand it, a single raw socket should be fine. You will, however, need to write your own transport, wrapping the socket up in an IReadDescriptor / IWriteDescriptor and using IReactorFDSet. However, if you're adept enough to know you need raw sockets in the first place, this shouldn't be too hard.