[Twisted-Python] New user, help needed with raw UDP packets

Richard Taylor r.taylor at eris.qinetiq.com
Fri Apr 16 03:00:18 EDT 2004

Hash: SHA1


On Thursday 15 April 2004 03:37, Jp Calderone wrote:
> [snip]
>   More likely you'll want either a RawUDPProtocol subclass, or something =
> very much like one.  You'll also need to create a new transport class =
> which works on raw sockets.  

Thanks for the pointers. At least I know now that I am not just being 
stupid :-)

I have been looking at how to subclass usp.Port to get what I want but I have 
come up against a problem that looks like a show stopper to me. It does not 
look like Python's socket library supports SOCK_RAW. I may be mistaken, but I 
think that I need to put the socket into RAW mode to get the incoming headers 
and to set my own headers on the outgoing packets. Without SOCK_RAW I am 

My code so far looks like this:

	from twisted.internet import protocol 
	from twisted.internet import udp
	from twisted.internet import reactor

	class Echo(protocol.DatagramProtocol):
	    def datagramReceived(self, data, (host, port)):
	        print "received %r from %s:%d" % (data, host, port)

	class RawUDPPort(udp.Port):
	    """Raw udp port."""

	    __implements__ = udp.Port.__implements__

	    socketType = socket.SOCK_RAW # Overide socket type.

	                   proto=Echo(), port=9999, reactor=reactor)

When run I get the following trace back:

	Traceback (most recent call last):
	  File "./asb_proxy.py", line 45, in ?
	    proto=Echo(), port=9999, reactor=reactor)
	  File "/usr/lib/python2.2/site-packages/twisted/internet/default.py", line 	
301, in listenWith
	  File "/usr/lib/python2.2/site-packages/twisted/internet/udp.py", line 83, 	
in startListening
	  File "/usr/lib/python2.2/site-packages/twisted/internet/udp.py", line 92, 	
in _bindSocket
	    raise error.CannotListenError, (self.interface, self.port, le)
	twisted.internet.error.CannotListenError: Couldn't listen on any:9999: (94, 
	'Socket type not supported').

I am sure this is the underlying call to socket as the following Python 
session shows:

	Python 2.2.3 (#1, Sep 15 2003, 12:38:33)
	[GCC 2.95.3 20010315 (release)] on linux2
	Type "help", "copyright", "credits" or "license" for more information.
	>>> import socket
	>>> s = socket.socket(socket.AF_INET, socket.SOCK_RAW)
	Traceback (most recent call last):
	  File "<stdin>", line 1, in ?
	socket.error: (94, 'Socket type not supported')

I guess that I might be able to use the pcap library to access the raw socket 
but integrating that into Twisted is starting to look like a big job.

Has anyone got any ideas as to how I should approach this.

> [snip]
>   If you get it working (documented, tested,etc ;), =
> it might be nice if you could contribute it back, since raw socket =
> support is something that does get asked for every once in a while.  =
> It'd be nice to be able to tell someone how to use existing support, =
> instead of telling them they have to implement it.

If I get something working I will certainly contribute it back to twisted.

Many thanks.


- -- 
B009 Woodward Building
St. Andrews Road
Worcs WR14 3PS
Jabber: RichardTaylor at jabber.org
PGPKey: http://search.keyserver.net:11371/pks/lookup?op=get&search=0xA7DA9FD9
Key fingerprint = D051 A121 E7C3 485F 3C0E  1593 ED9E D868 A7DA 9FD9
Version: GnuPG v1.2.1 (GNU/Linux)


More information about the Twisted-Python mailing list