Пример #1
0
    def handle(self, msg, callback):
        pkt = packet.Packet(data=msg.data)
        p_arp = self._find_protocol(pkt, "arp")
        datapath = msg.datapath
        parser = datapath.ofproto_parser
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        if p_arp.opcode == arp.ARP_REQUEST:
            src_ip = str(netaddr.IPAddress(p_arp.src_ip))
            dst_ip = str(netaddr.IPAddress(p_arp.dst_ip))
            src_mac = str(p_arp.src_mac)
            dst_mac = str(p_arp.dst_mac)

            #try to add src mac to network
            if not self.networkMap.findActiveHostByIP(src_ip):
                self.networkMap.addActiveHost(datapath, msg.match['in_port'],
                                              host.host(src_mac, src_ip))

            if self.networkMap.findActiveHostByIP(dst_ip):
                dst_mac = self.networkMap.findActiveHostByIP(dst_ip).mac
                e = ethernet.ethernet(src_mac, dst_mac, ether.ETH_TYPE_ARP)
                a = arp.arp(hwtype=1,
                            proto=ether.ETH_TYPE_IP,
                            hlen=6,
                            plen=4,
                            opcode=arp.ARP_REPLY,
                            src_mac=dst_mac,
                            src_ip=dst_ip,
                            dst_mac=src_mac,
                            dst_ip=src_ip)
                p = packet.Packet()
                p.add_protocol(e)
                p.add_protocol(a)
                actions = [parser.OFPActionOutput(port=msg.match['in_port'])]
                callback(datapath, actions, p, ofproto.OFPP_CONTROLLER)

            else:
                actions = [parser.OFPActionOutput(ofproto_v1_2.OFPP_FLOOD)]
                callback(datapath, actions, pkt, msg.match['in_port'])
                #out = parser.OFPPacketOut(datapath=datapath,
                #                  buffer_id=ofproto.OFP_NO_BUFFER,
                #                  in_port=in_port, actions=actions,
                #                  data=msg.data)
                #datapath.send_msg(out)

        elif p_arp.opcode == arp.ARP_REPLY:
            if not self.networkMap.findActiveHostByIP(p_arp.src_ip):
                self.networkMap.addActiveHost(
                    msg.datapath, msg.match['in_port'],
                    host.host(p_arp.src_mac, p_arp.src_ip))
            port = self.networkMap.findPortByHostMac(p_arp.dst_mac)
            if port:
                actions = [parser.OFPActionOutput(port=port.port_no)]
                callback(datapath, actions, pkt, ofproto.OFPP_CONTROLLER)
            else:
                actions = [parser.OFPActionOutput(ofproto_v1_2.OFPP_FLOOD)]
                callback(datapath, actions, pkt, msg.match['in_port'])
Пример #2
0
def test_high_speed_host():
    h = host(timeout=TIMEOUT)
    c = client(timeout=TIMEOUT)
    h.start()
    c.start()
    report("Sockets started")
    c.write("connected", True)
    s(0.5)
    h.write_ALL("Test0", "test of port 0")
    h.write_ALL("Test1", "test of port 1")
    h.write_ALL("Test2", "test of port 2")
    h.write_ALL("Test3", "test of port 3")
    h.write_ALL("Test4", "test of port 4")
    h.write_ALL("Test5", "test of port 5")
    h.write_ALL("Test6", "test of port 6")
    h.write_ALL("Test7", "test of port 7")
    h.write_ALL("Test8", "test of port 8")
    h.write_ALL("Test9", "test of port 9")
    s(0.1)
    assert (c.get("Test0") == "test of port 0")
    assert (c.get("Test1") == "test of port 1")
    assert (c.get("Test2") == "test of port 2")
    assert (c.get("Test3") == "test of port 3")
    assert (c.get("Test4") == "test of port 4")
    assert (c.get("Test5") == "test of port 5")
    assert (c.get("Test6") == "test of port 6")
    assert (c.get("Test7") == "test of port 7")
    assert (c.get("Test8") == "test of port 8")
    assert (c.get("Test9") == "test of port 9")
    c.close()
    h.close()
    success("High Speed Host -> Client Passed")
Пример #3
0
def test_bidirectional_messages():
    h = host(timeout=TIMEOUT)
    c = client(timeout=TIMEOUT)
    h.start()
    c.start()
    report("Sockets started")
    assert (c.get("Test") is None)
    assert (c.get("Test2") is None)
    c.write("connected", True)
    assert (c.get("Test") is None)
    assert (c.get("Test2") is None)
    s(0.5)
    h.write_ALL("Test", "test of port 1")
    h.write_ALL("Test2", "test of port 2")
    c.write("Test", "test of port 1")
    s(0.1)
    c.write("Test2", "test of port 2")
    s(0.5)
    assert (c.get("Test") == "test of port 1")
    assert (c.get("Test2") == "test of port 2")
    assert (h.get_ALL("Test") == ["test of port 1"])
    assert (h.get_ALL("Test2") == ["test of port 2"])
    h.close()
    c.close()
    success("Host <-> Client Passed")
Пример #4
0
    def _handle_dhcp(self, msg, datapath, callback):
        pkt = packet.Packet(data=msg.data)
        parser = msg.datapath.ofproto_parser
        ofproto = msg.datapath.ofproto
        pkt_dhcp = pkt.get_protocols(dhcp.dhcp)[0]
        dhcp_state = self.get_state(pkt_dhcp)
        if dhcp_state == 'DHCPDISCOVER':
            known = self.networkMap.findActiveHostByMac(
                pkt.get_protocol(ethernet.ethernet).src)
            if known:
                ip = known.ip
            else:
                ip = self._getNextAddr()
                self.networkMap.addActiveHost(
                    datapath, msg.match['in_port'],
                    host.host(pkt.get_protocol(ethernet.ethernet).src, ip))

            if ip:
                ans = self.assemble_offer(pkt, ip)
                if ans:
                    # self.networkMap.addInactiveHost(host.host(pkt.get_protocol(ethernet.ethernet).src, ip))
                    actions = [
                        parser.OFPActionOutput(port=msg.match['in_port'])
                    ]
                    callback(msg.datapath, actions, ans,
                             ofproto.OFPP_CONTROLLER)
            else:
                return None
        elif dhcp_state == 'DHCPREQUEST':
            ans = self.assemble_ack(pkt)
            if ans:
                actions = [parser.OFPActionOutput(port=msg.match['in_port'])]
                callback(msg.datapath, actions, ans, ofproto.OFPP_CONTROLLER)
        else:
            return None
Пример #5
0
    def handle(self, p_ipv4, msg, in_port, eth, callback):
        datapath = msg.datapath
        ofproto = datapath.ofproto

        LOG.debug("--- ICMP Packet!: \nIP Address src:%s\nIP Address Dest:%s\n",
                  p_ipv4.src, p_ipv4.dst)

        if self.networkMap.findActiveHostByMac(eth.dst):
            LOG.debug("This adress has been found!")
            if self.networkMap.isInactiveHost(eth.src):
                LOG.debug("Activate Host...")
                self.networkMap.addActiveHost(
                    datapath, msg.match['in_port'], host.host(eth.src, p_ipv4.src))
            out_port = self.networkMap.findPortByHostMac(eth.dst).port_no
        else:
            out_port = ofproto.OFPP_FLOOD

        actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
        #LOG.debug("out_port to the destination host is ", out_port)
        # install a flow to avoid packet_in next time
        if out_port != ofproto.OFPP_FLOOD:
            if (self.networkMap.findSwitchByDatapath(datapath) != self.networkMap.findSwitchByHostMac(eth.dst)):

                LOG.debug("###More than one Switch detected###")
                path = nx.shortest_path(self.networkMap.networkMap, self.networkMap.findSwitchByDatapath(
                    datapath), self.networkMap.findSwitchByHostMac(eth.dst))
                print("---- Way to go ", str(path))
                for item in range(1, (len(path) - 1)):
                    if isinstance(path[item], Port) and isinstance(path[item - 1], Switch):
                        datapath = path[item - 1].dp
                        port_no = path[item].port_no
                        match = datapath.ofproto_parser.OFPMatch(
                            in_port=in_port, ipv4_dst=p_ipv4.dst, ipv4_src=p_ipv4.src, eth_type=0x0800)
                        actions = [
                            datapath.ofproto_parser.OFPActionOutput(port_no)]
                        #LOG.debug("out_port to the next hope is", str(port_no))
                        #self.add_flow(datapath, port_no, actions, match)
                        callback(datapath, port_no, actions, match)

                    else:
                        LOG.debug("---- Error in establishing multiflow.")

                LOG.debug("###TO BE IMPLEMENTED###")
            else:
                match = datapath.ofproto_parser.OFPMatch(
                    in_port=in_port, ipv4_dst=p_ipv4.dst, ipv4_src=p_ipv4.src, eth_type=0x0800)

                callback(datapath, out_port, actions, match)


        data = None

        if msg.buffer_id == ofproto.OFP_NO_BUFFER:
            data = msg.data

        out = datapath.ofproto_parser.OFPPacketOut(
            datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,
            actions=actions, data=data)
        datapath.send_msg(out)
Пример #6
0
 def setHosts(self, filename):
     if (self.fileCheck(filename)):
         f = open(filename, "r")
         for line in f.readlines():
             data = line.strip().split(";")
             h = host(data[0], data[1])
             if (h.getName() != "__destroy__"):
                 self.hosts.append(h)
         f.close()
Пример #7
0
def CreatNetwork(argv):
    data = utills.parseJson(argv)
    routerList = []  #lista de roteadores
    for i in data["routers"]:  #roda em cada roteador
        station = router(i["id"], 10)  #instancia um roteador
        HostsLocatios = utills.gerate_xy(len(i["hosts"]), i["width"],
                                         i["height"])
        for index, j in enumerate(i["hosts"]):  #roda em cada host
            station.insertHost(
                host(i["id"], j["id"], HostsLocatios[index], j["energy"],
                     j["range"]))
        routerList.append(station)
    return routerList
Пример #8
0
def test_empty_message():
    h = host(timeout=TIMEOUT)
    c = client(timeout=TIMEOUT)
    h.start()
    c.start()
    report("Sockets started")
    assert (c.get("Test") is None)
    assert (c.get("Test2") is None)
    assert (h.get_ALL("Test") == [])
    assert (h.get_ALL("Test2") == [])
    c.close()
    h.close()
    success("Empty messages test passed")
Пример #9
0
def initial():
    try:
        a = open("version\\version.txt","r")
        ccc = str(a.read().split("::")[3])
    except FileNotFoundError:
        print("错误!  文件不完整,请重新下载")
        fun.wrong()
    if ccc == "1":
        update_check()


    print("检查host文件完整性。。")
    host.host()
    print("检查文件重要备份点。。")
    if fun.dir("host","backup"):
        print("成功! 备份程序重要备份点")
    else:
        try:
            os.system(r"copy C:\Windows\System32\drivers\etc\hosts host\\backup")
            print("成功! 备份程序重要备份点")
        except BaseException:
            print("错误! 未能成功备份")
            fun.wrong()
    print()
    time.sleep(1)
    print("检查计算机隧道效应。。。")
    try:
        html_doc = "https://doub.io/sszhfx/"
        req = urllib.request.Request(html_doc)
        urllib.request.urlopen(req)
    except BaseException:
        print("错误!  网络配置错误")
        fun.wrong()
    else:
        print("成功!  网络适配成功")
    fun.fence()
Пример #10
0
def test_high_throughput_client():
    h = host(timeout=TIMEOUT)
    c = client(timeout=TIMEOUT)
    h.start()
    c.start()
    report("Sockets started")
    c.write("connected", True)
    s(0.5)
    for i in range(1000):
        c.write("Test{}".format(i), text)
    s(0.5)
    for i in range(1000):
        assert (h.get_ALL("Test{}".format(i)) == [text])
    c.close()
    h.close()
    success("High Throughput Client -> Host Passed")
Пример #11
0
def json_format(data, award_data):
    final_dict["hosts"] = host.host(data)
    final_dict["award_data"] = {}

    winners_dict = winners.winners(data, award_data)
    nominees_dict = nom.nominees(data, award_data, winners_dict)
    presenters_dict = extract_presenters(data, award_data, winners_dict)

    for award in award_list:
        final_dict["award_data"][award] = {
            "nominees": nominees_dict.get(award),
            "presenters": presenters_dict.get(award),
            "winners": winners_dict.get(award)
        }

    # print(final_dict)
    return final_dict
Пример #12
0
def test_async_ordering():
    h = host(timeout=TIMEOUT, open=False)
    c = client(timeout=TIMEOUT, open=False)
    assert (not h.opened)
    h.open()
    h.start()
    assert (h.opened)
    report("Host started")
    c.open()
    c.start()
    assert (c.opened)
    report("Client started")
    h.close()
    assert (h.stopped)
    report("Host closed")
    c.close()
    assert (c.stopped)
    report("Client closed")
    success("Async Ordering test passed")
Пример #13
0
def test_connection_BOTH():
    h = host(timeout=TIMEOUT)
    h.start()
    c = client(timeout=TIMEOUT)
    c.start()
    report("Sockets opened and started")
    assert (h.opened)
    assert (c.opened)
    s(TIMEOUT)
    assert (len(h.getClients()) is 1)
    report("Open state verified")
    c.close()
    assert (not c.opened)
    assert (c.stopped)
    h.close()
    report("Closed state verified")
    assert (not h.opened)
    assert (h.stopped)
    success("Connection test passed")
Пример #14
0
def test_host_messages():
    h = host(timeout=TIMEOUT)
    c = client(timeout=TIMEOUT)
    h.start()
    c.start()
    report("Sockets started")
    assert (h.get_ALL("Test") == [])
    assert (h.get_ALL("Test2") == [])
    c.write("Test", "test of port 1")
    s(0.5)
    assert (h.get_ALL("Test") == ["test of port 1"])
    assert (h.get_ALL("Test2") == [])
    c.write("Test2", "test of port 2")
    s(0.5)
    assert (h.get_ALL("Test") == ["test of port 1"])
    assert (h.get_ALL("Test2") == ["test of port 2"])
    assert (c.get("Test") is None)
    assert (c.get("Test2") is None)
    c.close()
    h.close()
    success("Client -> Host Passed")
Пример #15
0
def create_host_objects(host_names,host_map):
    for i in host_names:
        new_host = host(i)
        host_map[i] = new_host
    return host_map
Пример #16
0
    def handle(self, p_ipv4, msg, in_port, eth, callback):
        datapath = msg.datapath
        ofproto = datapath.ofproto

        LOG.debug("--- ICMP Packet!: \nIP Address src:%s\nIP Address Dest:%s\n",
                  p_ipv4.src, p_ipv4.dst)

        if not self.networkMap.findActiveHostByIP(p_ipv4.src):
            self.networkMap.addActiveHost(
                datapath, msg.match['in_port'], host.host(eth.src, p_ipv4.src))

        if self.networkMap.findActiveHostByMac(eth.dst):
            LOG.debug("This adress has been found!")
            if self.networkMap.isInactiveHost(eth.src):
                LOG.debug("Activate Host...")
                self.networkMap.addActiveHost(
                    datapath, msg.match['in_port'], host.host(eth.src, p_ipv4.src))
            out_port = self.networkMap.findPortByHostMac(eth.dst).port_no
        # else:
        #     out_port = ofproto.OFPP_FLOOD

        actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]

        if out_port != ofproto.OFPP_FLOOD:
            if (self.networkMap.findSwitchByDatapath(datapath) != self.networkMap.findSwitchByHostMac(eth.dst)):

                LOG.debug("###More than one Switch detected###")
                path = nx.shortest_path(self.networkMap.networkMap, self.networkMap.findSwitchByDatapath(
                    datapath), self.networkMap.findSwitchByHostMac(eth.dst))
                print("---- Way to go ", str(path))

                print("--- IP Packet from ", str(p_ipv4.src), 
                    "to", str(p_ipv4.dst),"--from Switch", str(msg.datapath.id))
                for item in range(1, (len(path))):
                    if isinstance(path[item], Port) and isinstance(path[item - 1], Switch):
                        datapath = path[item - 1].dp
                        port_no = path[item].port_no
                        match = datapath.ofproto_parser.OFPMatch(
                            in_port=in_port, eth_src=eth.src,
                            eth_dst=eth.dst)
                        actions = [
                            datapath.ofproto_parser.OFPActionOutput(port_no)]

                        callback(datapath, port_no, actions, match)

                    else:
                        LOG.debug("---- Error in establishing multiflow.")
            else:
                match = datapath.ofproto_parser.OFPMatch(
                    in_port=in_port, eth_src=eth.src,
                    eth_dst=eth.dst)

                callback(datapath, out_port, actions, match)

        data = None

        if msg.buffer_id == ofproto.OFP_NO_BUFFER:
            data = msg.data

        out = datapath.ofproto_parser.OFPPacketOut(
            datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,
            actions=actions, data=data)
        datapath.send_msg(out)
Пример #17
0
def get_config():
    company = host()
    users_info = user_list(company.name)
    return company, users_info
	plt.plot(px, py, 'bo')
	plt.xlabel('X')
	plt.ylabel('Y')
	plt.title("Hostesses")#Muda título do gráfico
	plt.show()
	#plt.savefig('Plot_host.png')
'''
name = 'GUSSS'
logging.basicConfig(filename='test.log', level=logging.INFO) # testing log
#logging.info('Hello world, this is {}'.format(h1))

# Creating master
m1 = master(id = 100)

# Creating hostesses
h1 = host(mac=1, x=1, y=0, master=m1, reach=4)
h2 = host(mac=2, x=2, y=3, master=m1, reach=4)
h3 = host(mac=3, x=3, y=5, master=m1, reach=4)
h4 = host(mac=4, x=6, y=8, master=m1, reach=4)
h5 = host(mac=10, x=5, y=7.5, master=m1, reach=4)
#h6 = host(mac=6, x=3, y=2, master=m1, reach=4)

#y = h1.get_instances()
#plot_host(y)

# ******** Starting the simulation ********
c = 0
logging.info(f"TIME : {c} \n")

#h1.send_message("hello friend 2", 4)
h1.send_message("hello friend 2", 3)
Пример #19
0
def getHost():
    return host()
Пример #20
0
def getHost():
    return host()
Пример #21
0
    def handle(self, p_ipv4, msg, in_port, eth, dst_mac, callback):
        datapath = msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        LOG.debug(
            "--- moved host ipv4 Packet!: \nIP Address src:%s\nIP Address Dest:%s\n",
            p_ipv4.src, p_ipv4.dst)

        # crafting fake arp
        src_mac = eth.src
        LOG.debug("crafting a fake arp for moved host")
        e = ethernet.ethernet(src_mac, dst_mac, ether.ETH_TYPE_ARP)
        a = arp.arp(hwtype=1,
                    proto=ether.ETH_TYPE_IP,
                    hlen=6,
                    plen=4,
                    opcode=arp.ARP_REPLY,
                    src_mac=dst_mac,
                    src_ip=p_ipv4.dst,
                    dst_mac=src_mac,
                    dst_ip=p_ipv4.src)
        p = packet.Packet()
        p.add_protocol(e)
        p.add_protocol(a)
        actions = [parser.OFPActionOutput(port=msg.match['in_port'])]
        self._send_packet(datapath, actions, p, ofproto.OFPP_CONTROLLER)

        if self.networkMap.findActiveHostByMac(eth.dst):
            LOG.debug("This adress has been found!")
            if self.networkMap.isInactiveHost(eth.src):
                LOG.debug("Activate Host...")
                self.networkMap.addActiveHost(datapath, msg.match['in_port'],
                                              host.host(eth.src, p_ipv4.src))
            out_port = self.networkMap.findPortByHostMac(eth.dst).port_no
        else:
            out_port = ofproto.OFPP_FLOOD
        if self.networkMap.findActiveHostByMac(eth.dst):
            dst_Real_IP = self.networkMap.findActiveHostByMac(eth.dst).ip
            actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]

            if out_port != ofproto.OFPP_FLOOD:
                if (self.networkMap.findSwitchByDatapath(datapath) !=
                        self.networkMap.findSwitchByHostMac(eth.dst)):

                    LOG.debug("###More than one Switch detected###")

                    path1 = nx.shortest_path(
                        self.networkMap.networkMap,
                        self.networkMap.findSwitchByDatapath(datapath),
                        self.networkMap.findSwitchByHostMac(eth.dst))

                    for item in range(1, (len(path1) - 1)):
                        if isinstance(path1[item], Port) and isinstance(
                                path1[item - 1], Switch):
                            datapath = path1[item - 1].dp
                            port_no = path1[item].port_no

                            match = datapath.ofproto_parser.OFPMatch(
                                ipv4_src=p_ipv4.src,
                                ipv4_dst=p_ipv4.dst,
                                eth_type=0x0800)
                            actions = [
                                datapath.ofproto_parser.OFPActionSetField(
                                    ipv4_dst=dst_Real_IP),
                                datapath.ofproto_parser.OFPActionOutput(
                                    port_no)
                            ]

                            callback(datapath, port_no, actions, match)

                            port_no = self.networkMap.findPortByHostMac(
                                eth.src).port_no
                            match_back = datapath.ofproto_parser.OFPMatch(
                                ipv4_src=dst_Real_IP,
                                ipv4_dst=p_ipv4.src,
                                eth_type=0x0800)
                            actions = [
                                datapath.ofproto_parser.OFPActionSetField(
                                    ipv4_src=p_ipv4.dst),
                                datapath.ofproto_parser.OFPActionOutput(
                                    port_no)
                            ]

                            callback(datapath, port_no, actions, match_back)

                    # fake flow impel for the switch that has the moved host
                    datapath2 = self.networkMap.findSwitchByHostMac(eth.dst).dp
                    port_no = self.networkMap.findPortByHostMac(
                        eth.dst).port_no
                    match = datapath.ofproto_parser.OFPMatch(
                        ipv4_src=p_ipv4.src,
                        ipv4_dst=dst_Real_IP,
                        eth_type=0x0800)
                    actions = [
                        datapath.ofproto_parser.OFPActionOutput(port_no)
                    ]

                    callback(datapath2, port_no, actions, match)

                    path2 = nx.shortest_path(
                        self.networkMap.networkMap,
                        self.networkMap.findSwitchByHostMac(eth.dst),
                        self.networkMap.findSwitchByDatapath(datapath))

                    for item in range(1, (len(path2) - 1)):
                        if isinstance(path2[item], Port) and isinstance(
                                path2[item - 1], Switch):
                            datapath = path2[item - 1].dp
                            port_no2 = path2[item].port_no

                            match_back = datapath.ofproto_parser.OFPMatch(
                                ipv4_src=dst_Real_IP,
                                ipv4_dst=p_ipv4.src,
                                eth_type=0x0800)
                            actions = [
                                datapath.ofproto_parser.OFPActionOutput(
                                    port_no2)
                            ]

                            callback(datapath, port_no2, actions, match_back)

                        else:
                            LOG.debug("---- Error in establishing multiflow.")

                    LOG.debug("###TO BE IMPLEMENTED###")
                else:
                    # TBC
                    return
                    # match = datapath.ofproto_parser.OFPMatch(
                    #     in_port=in_port, ipv4_dst=p_ipv4.dst, ipv4_src=p_ipv4.src, eth_type=0x0800)

                    # callback(datapath, out_port, actions, match)

            data = None

            if msg.buffer_id == ofproto.OFP_NO_BUFFER:
                data = msg.data