Esempio n. 1
0
def arp_reply_handler(pkt):
    pkt_in_msg = pkt.payload
    pkt_parsed = pkt_in_msg.payload

    if pkt_parsed.payload.psrc not in ARP_TABLE:
        arp_add(pkt_parsed.payload.psrc,
                pkt_parsed.payload.hwsrc)  #add arp record
    if pkt_parsed.payload.pdst in ARP_TABLE:
        ETHER = copy.deepcopy(pkt_parsed)
        ETHER.dst = pkt_parsed.payload.hwsrc
        ETHER.src = pkt_parsed.payload.hwdst
        ETHER.payload.op = 2  #reply
        ETHER.payload.hwdst = pkt_parsed.payload.hwsrc
        ETHER.payload.hwsrc = ARP_TABLE[pkt_parsed.payload.pdst]
        ETHER.payload.psrc = pkt_parsed.payload.pdst
        ETHER.payload.pdst = pkt_parsed.payload.psrc

        pkt_out = of.ofp_header() / of.ofp_pktout_header(
        ) / of.ofp_action_output() / ETHER
        pkt_out.payload.payload.port = pkt_in_msg.in_port
        pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
        pkt_out.payload.in_port = pkt_in_msg.in_port
        pkt_out.payload.actions_len = 8
        pkt_out.length = len(pkt_out)
        return pkt_out
    else:
        pkt_out = of.ofp_header() / of.ofp_pktout_header(
        ) / of.ofp_action_output()
        pkt_out.payload.payload.port = 0xfffb
        pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
        pkt_out.payload.in_port = pkt_in_msg.in_port
        pkt_out.payload.actions_len = 8
        pkt_out.length = len(pkt_out)
        return pkt_out
Esempio n. 2
0
def client_handler(address, fd, events):
    sock = fd_map[fd]
    if events & io_loop.READ:
        data = sock.recv(1024)
        if data == '':
            print ">>>Connection dropped"
            io_loop.remove_handler(fd)
        if len(data)<8:
            print ">>>Length of packet is too short"
        else:
            if len(data)>=8:
                rmsg = of.ofp_header(data[:8])
                #rmsg.show()
                body = data[8:]
            if rmsg.type == 0:
                msg = handler[0] (data)
                message_queue_map[sock].put(str(msg))
                message_queue_map[sock].put(str(of.ofp_header(type = 5)))
            elif rmsg.type == 6:
                handler[6] (data,fd)
            else:
                msg = handler[rmsg.type] (data,fd)
                message_queue_map[sock].put(str(msg))
            io_loop.update_handler(fd, io_loop.WRITE)

    if events & io_loop.WRITE:
        try:
            next_msg = message_queue_map[sock].get_nowait()
        except Queue.Empty:
            #print "%s queue empty" % str(address)
            io_loop.update_handler(fd, io_loop.READ)
        else:
            #print 'sending "%s" to %s' % (of.ofp_header(next_msg).type, address)
            sock.send(next_msg)
Esempio n. 3
0
def client_handler(address, fd, events):
    sock = fd_map[fd]
    if events & io_loop.READ:
        data = sock.recv(1024)
        if data == '':
            print ">>>Connection dropped"
            io_loop.remove_handler(fd)
        if len(data) < 8:
            print ">>>Length of packet is too short"
        else:
            if len(data) >= 8:
                rmsg = of.ofp_header(data[:8])
                #rmsg.show()
                body = data[8:]
            if rmsg.type == 0:
                msg = handler[0](data)
                message_queue_map[sock].put(str(msg))
                message_queue_map[sock].put(str(of.ofp_header(type=5)))
            elif rmsg.type == 6:
                handler[6](data, fd)
            else:
                msg = handler[rmsg.type](data, fd)
                message_queue_map[sock].put(str(msg))
            io_loop.update_handler(fd, io_loop.WRITE)

    if events & io_loop.WRITE:
        try:
            next_msg = message_queue_map[sock].get_nowait()
        except Queue.Empty:
            #print "%s queue empty" % str(address)
            io_loop.update_handler(fd, io_loop.READ)
        else:
            #print 'sending "%s" to %s' % (of.ofp_header(next_msg).type, address)
            sock.send(next_msg)
Esempio n. 4
0
def arp_reply_handler(pkt):
	pkt_in_msg =pkt.payload
	pkt_parsed =pkt_in_msg.payload
	
	if pkt_parsed.payload.psrc not in ARP_TABLE:
		arp_add(pkt_parsed.payload.psrc,pkt_parsed.payload.hwsrc) 		#add arp record
	if pkt_parsed.payload.pdst in ARP_TABLE:
		ETHER = copy.deepcopy(pkt_parsed)
		ETHER.dst = pkt_parsed.payload.hwsrc
		ETHER.src = pkt_parsed.payload.hwdst
		ETHER.payload.op = 2#reply
		ETHER.payload.hwdst = pkt_parsed.payload.hwsrc
		ETHER.payload.hwsrc = ARP_TABLE[pkt_parsed.payload.pdst]
		ETHER.payload.psrc = pkt_parsed.payload.pdst
		ETHER.payload.pdst = pkt_parsed.payload.psrc

		pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()/ETHER
		pkt_out.payload.payload.port = pkt_in_msg.in_port
		pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
		pkt_out.payload.in_port = pkt_in_msg.in_port
		pkt_out.payload.actions_len = 8
		pkt_out.length = len(pkt_out)
		return pkt_out
	else:
		pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()
		pkt_out.payload.payload.port = 0xfffb
		pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
		pkt_out.payload.in_port = pkt_in_msg.in_port
		pkt_out.payload.actions_len = 8
		pkt_out.length = len(pkt_out)
		return pkt_out
Esempio n. 5
0
def switch(pkt, dpid, *args):
    rmsg = pkt
    pkt_in_msg = pkt.payload
    pkt_parsed = pkt.payload.payload

    mactoport_add(src_mac=pkt_parsed.src, in_port=pkt_in_msg.in_port)
    if pkt_parsed.dst == "ff:ff:ff:ff:ff:ff":
        if pkt_parsed.payload.type == 0x0806:  #all ARP will be ARP REQUEST first.
            return arp_server.arp_reply_handler(pkt)
        else:
            pkt_out = of.ofp_header() / of.ofp_pktout_header(
            ) / of.ofp_action_output()
            pkt_out.payload.payload.port = 0xfffb
            pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
            pkt_out.payload.in_port = pkt_in_msg.in_port
            pkt_out.payload.actions_len = 8
            pkt_out.length = len(pkt_out)
            return pkt_out
    else:
        if pkt_parsed.dst in mactoport:
            out_port = mactoport[pkt_parsed.dst]
        else:
            out_port = 0xfffb
        flow_mod = of.create_flow(pkt, out_port)
        flow_database.flow_add(flow_mod, dpid)
        return flow_mod
Esempio n. 6
0
def switch(pkt,dpid,*args):
	rmsg = pkt
	pkt_in_msg = pkt.payload
	pkt_parsed = pkt.payload.payload
		
	mactoport_add(src_mac = pkt_parsed.src, in_port = pkt_in_msg.in_port)
	if pkt_parsed.dst == "ff:ff:ff:ff:ff:ff":
		if pkt_parsed.payload.type == 0x0806:   #all ARP will be ARP REQUEST first.
			return arp_server.arp_reply_handler(pkt)
		else:
			pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()
			pkt_out.payload.payload.port = 0xfffb
			pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
			pkt_out.payload.in_port = pkt_in_msg.in_port
			pkt_out.payload.actions_len = 8
			pkt_out.length = len(pkt_out)
			return pkt_out
	else:
		if pkt_parsed.dst in mactoport:
			out_port = mactoport[pkt_parsed.dst]
		else:
			out_port = 0xfffb
		flow_mod = of.create_flow(pkt, out_port)
		flow_database.flow_add(flow_mod,dpid) 
		return flow_mod
    def recv_handler(self, fd_map, message_queue_map):
        for fd in fd_map.keys():
            self.fd_lock.acquire()
            sock = fd_map[fd]
            self.fd_lock.release()

            self.sock_lock.acquire()
            print "sock_lock by recv_message"
            data = sock.recv(1024)
            self.sock_lock.release()
            print "sock_unlock by recv_message"

            if data == '':
                print ">>>Connection dropped"
                self.fd_lock.acquire()
                del fd_map[fd]
                #sock.close()
                self.fd_lock.release()
                self.queue_lock.acquire()
                del message_queue_map[sock]
                self.queue_lock.release()
            if len(data) < 8:
                print ">>>Length of packet is too short"
            else:
                if len(data) >= 8:
                    rmsg = of.ofp_header(data[0:8])
                    body = data[8:]
                if rmsg.type == 0:
                    msg = self.handler[0](data)
                    self.queue_lock.acquire()
                    print "hello queue_lock by recv_message"
                    message_queue_map[sock].put(str(msg))
                    message_queue_map[sock].put(str(of.ofp_header(type=5)))
                    self.queue_lock.release()
                    print "hello queue_unlock by recv_message"
                elif rmsg.type == 6:
                    self.handler[6](data, fd)
                else:
                    msg = self.handler[rmsg.type](data, fd)
                    queue_lock.acquire()
                    print "other queue_lock by recv_message"
                    message_queue_map[sock].put(str(msg))
                    queue_lock.release()
                    print "other queue_unlock by recv_message"
Esempio n. 8
0
    def recv_handler(self, fd_map, message_queue_map):
        for fd in fd_map.keys():
            self.fd_lock.acquire()
            sock = fd_map[fd]
            self.fd_lock.release()

            self.sock_lock.acquire()
            print "sock_lock by recv_message"
            data = sock.recv(1024)
            self.sock_lock.release()
            print "sock_unlock by recv_message"

            if data == "":
                print ">>>Connection dropped"
                self.fd_lock.acquire()
                del fd_map[fd]
                # sock.close()
                self.fd_lock.release()
                self.queue_lock.acquire()
                del message_queue_map[sock]
                self.queue_lock.release()
            if len(data) < 8:
                print ">>>Length of packet is too short"
            else:
                if len(data) >= 8:
                    rmsg = of.ofp_header(data[0:8])
                    body = data[8:]
                if rmsg.type == 0:
                    msg = self.handler[0](data)
                    self.queue_lock.acquire()
                    print "hello queue_lock by recv_message"
                    message_queue_map[sock].put(str(msg))
                    message_queue_map[sock].put(str(of.ofp_header(type=5)))
                    self.queue_lock.release()
                    print "hello queue_unlock by recv_message"
                elif rmsg.type == 6:
                    self.handler[6](data, fd)
                else:
                    msg = self.handler[rmsg.type](data, fd)
                    queue_lock.acquire()
                    print "other queue_lock by recv_message"
                    message_queue_map[sock].put(str(msg))
                    queue_lock.release()
                    print "other queue_unlock by recv_message"