Example #1
0
def main(net: switchyard.llnetbase.LLNetBase):
    my_interfaces = net.interfaces()
    mymacs = [intf.ethaddr for intf in my_interfaces]
    switch_table = {}
    while True:
        try:
            _, fromIface, packet = net.recv_packet()
        except NoPackets:
            continue
        except Shutdown:
            break

        log_debug(f"In {net.name} received packet {packet} on {fromIface}")
        eth = packet.get_header(Ethernet)
        if eth is None:
            log_info("Received a non-Ethernet packet?!")
            return
        if eth.dst in mymacs:
            log_info("Received a packet intended for me")
        else:
            if eth.dst in switch_table:
                log_info(
                    f"already in switch_table,send {packet} to {switch_table[eth.dst]}"
                )
                net.send_packet(switch_table[eth.dst], packet)
            else:
                for intf in my_interfaces:
                    if fromIface != intf.name:
                        log_info(f"Flooding packet {packet} to {intf.name}")
                        net.send_packet(intf, packet)
        switch_table[eth.src] = fromIface

    net.shutdown()
Example #2
0
def main(net: switchyard.llnetbase.LLNetBase):
    my_interfaces = net.interfaces()
    mymacs = [intf.ethaddr for intf in my_interfaces]
    switch_table = deque(maxlen=5)
    #maxsize=5
    while True:
        try:
            _, fromIface, packet = net.recv_packet()
        except NoPackets:
            continue
        except Shutdown:
            break

        log_debug(f"In {net.name} received packet {packet} on {fromIface}")
        eth = packet.get_header(Ethernet)
        find = False
        for i, [mac, entry, volume] in enumerate(switch_table):
            if eth.src == mac:
                find = True
                if entry != fromIface:
                    switch_table[i] = [mac, fromIface, volume]
        if find == False:
            if len(switch_table) == 5:
                a, b, minnum = switch_table[0]
                k = 0
                for i, [mac, entry, volume] in enumerate(switch_table):
                    if volume < minnum:
                        k = i
                        minnum = volume

                log_info(f"deleting the entry :{switch_table[k]}")
                switch_table.remove(switch_table[k])
                switch_table.append([eth.src, fromIface, 0])
            else:
                switch_table.append([eth.src, fromIface, 0])

        if eth is None:
            log_info("Received a non-Ethernet packet?!")
            return
        if eth.dst in mymacs:
            log_info("Received a packet intended for me")
        else:
            find = False
            for i, [mac, entry, traffic] in enumerate(switch_table):
                if mac == eth.dst:
                    find = True
                    log_info(
                        f"already in switch_table,send {packet} to {entry}")
                    net.send_packet(entry, packet)
                    switch_table[i] = [mac, entry, traffic + 1]
            if find == False:
                for intf in my_interfaces:
                    if fromIface != intf.name:
                        log_info(f"Flooding packet {packet} to {intf.name}")
                        net.send_packet(intf, packet)

    net.shutdown()
Example #3
0
def main(net: switchyard.llnetbase.LLNetBase):
    my_interfaces = net.interfaces()
    mymacs = [intf.ethaddr for intf in my_interfaces]

    icnt = 0
    ocnt = 0

    while True:
        try:
            _, fromIface, packet = net.recv_packet()
        except NoPackets:
            continue
        except Shutdown:
            break

        icnt += 1

        log_debug(f"In {net.name} received packet {packet} on {fromIface}")
        eth = packet.get_header(Ethernet)
        if eth is None:
            log_info("Received a non-Ethernet packet?!")
            return
        if eth.dst in mymacs:
            log_info("Received a packet intended for me")
        else:
            ocnt += 1
            for intf in my_interfaces:
                if fromIface != intf.name:
                    log_info(f"Flooding packet {packet} to {intf.name}")
                    net.send_packet(intf, packet)
        log_info(f"in:{icnt} out:{ocnt}")
    net.shutdown()