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

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jp

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 
stuffed.

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.


	reactor.listenWith(RawUDPPort,
	                   proto=Echo(), port=9999, reactor=reactor)
	reactor.run()

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
	    p.startListening()
	  File "/usr/lib/python2.2/site-packages/twisted/internet/udp.py", line 83, 	
in startListening
	    self._bindSocket()
	  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.

Richard

- -- 
QinetiQ                                  
B009 Woodward Building
St. Andrews Road
Malvern
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFAf4SI7Z7YaKfan9kRAnBjAJoDYDWaaB3zqfrChSMIvXo901WWQwCeKt21
vKa2BRlNHXHXq92vD7xaHOc=
=POTK
-----END PGP SIGNATURE-----




More information about the Twisted-Python mailing list