views:

269

answers:

1

I am trying to proto-type send/recv via a packet socket using the asyncore dispatcher (code below). Although my handle_write method gets called promptly, the handle_read method doesn't seem to get invoked. The loop() does call the readable method every so often, but I am not able to receive anything. I know there are packets received on eth0 because a simple tcpdump shows incoming packets. Am I missing something?



#!/usr/bin/python

import asyncore, socket, IN, struct

class packet_socket(asyncore.dispatcher):

    def __init__(self):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_PACKET, socket.SOCK_RAW)
        self.buffer = '0180C20034350012545900040060078910'
        self.socket.setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("%ds" % (len("eth0")+1,), "eth0"))

    def handle_close(self):
        self.close()

    def handle_connect(self):
        pass

    def handle_read(self):
        print "handle_read() called"
        data,addr=self.recvfrom(1024)
        print data
        print addr

    def readable(self):
        print "Checking read flag"
        return True

    def writable(self):
        return (len(self.buffer) > 0)

    def handle_write(self):
        print "Writing buffer data to the socket"
        sent = self.sendto(self.buffer,("eth0",0xFFFF))
        self.buffer = self.buffer[sent:]

c = packet_socket()

asyncore.loop()


Thanks in advance.

+1  A: 

I finally got this to work with some help from a co-worker. This has to do with passing the protocol argument to the create_socket() method. Unfortunately create_socket() of the dispatcher doesn't take a third argument - so I had to modify my packet_socket() constructor to take a pre-created socket with protocol as ETH_P_ALL (or whatever protocol type you desire to receive) as an argument. Edited code below:



#!/usr/bin/python

import asyncore, socket, IN, struct

proto=3
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3))
s.bind(("eth0",proto))

class packet_socket(asyncore.dispatcher):

    def __init__(self,sock):
        asyncore.dispatcher.__init__(self,sock)
        #self.create_socket(socket.AF_PACKET, socket.SOCK_RAW,socket.htons(3))
        self.buffer = '0180C20034350012545900040060078910'
        self.socket.setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,struct.pack("%ds" % (len("eth0")+1,), "eth0"))

    def handle_close(self):
        self.close()

    def handle_connect(self):
        pass

    def handle_read(self):
        print "handle_read() called" 
    data,addr=self.recvfrom(1024)
        print data
        print addr

    def readable(self):
        print "Checking read flag" 
    return True

    def writable(self):
        return (len(self.buffer) > 0)

    def handle_write(self):
        print "Writing buffer data to the socket" 
    sent = self.sendto(self.buffer,("eth0",0xFFFF))
        self.buffer = self.buffer[sent:]

c = packet_socket(s)

asyncore.loop()


Thanks,

anbhat