Esempio n. 1
0
    def __init__(self, host, entry_duration=5000):
        self.updated = Signal()
        self.send_frame = Signal()
        self.report = Signal()

        super(ARP, self).__init__(host)
        self.ip2mac = CacheTable(entry_duration=entry_duration)
Esempio n. 2
0
    def __init__(self, host, entry_duration=5000):
        self.updated = Signal()
        self.send_frame = Signal()
        self.report = Signal()

        super(ARP, self).__init__(host)
        self.ip2mac = CacheTable(entry_duration=entry_duration)
        self.ip2mac.set(IP_Broadcast, MAC_Broadcast, float('inf'))
Esempio n. 3
0
 def __init__(self, n_interfaces=3):
     super(Switch, self).__init__(n_interfaces)
     self.mac2port = CacheTable(entry_duration=1000)
     self.ports = [
         Port(
             host=self,
             nic=nic,
             mac2port=self.mac2port,
             index=i,
         ) for i, nic in enumerate(self.nics)
     ]
     each(self.ports).calc_other_ports(self.ports)
Esempio n. 4
0
 def __init__(self, n_tips=3):
     super(Switch, self).__init__(n_tips)
     self.nics = [NIC(tip) for tip in self.tips]
     mac2port = CacheTable(entry_duration=1000)
     self.ports = [
         Port(
             host=self,
             nic=nic,
             mac2port=mac2port,
             index=i,
         ) for i, nic in enumerate(self.nics)
     ]
     each(self.ports).calc_other_ports(self.ports)
     mint.worker(self.run, priority=simulation.SWITCH_PRIORITY)
Esempio n. 5
0
    def __init__(self, ip=None, mac=None):
        super(Host, self).__init__(n_tips=1)
        self.tip = self.tips[0]
        self.nic = NIC(self.tip)
        self.libsocket = LibSocket(self)

        if mac is None:
            mac = self.index
        if ip is None:
            ip = '1.0.0.{}'.format(self.index)
        self.ip = ip_from_user(ip)
        self.mac = mac_from_user(mac)

        self.ip2mac = CacheTable(entry_duration=1000)
        self.arp_table = self.ip2mac
        self.avail_ports = defaultdict(lambda: 49152)
        self.pending_packets = defaultdict(deque)
        self.pending_frames = deque()
        self.sockets = {}

        mint.worker(self.run)
Esempio n. 6
0
class ARP(Protocol):

    def __init__(self, host, entry_duration=5000):
        self.updated = Signal()
        self.send_frame = Signal()
        self.report = Signal()

        super(ARP, self).__init__(host)
        self.ip2mac = CacheTable(entry_duration=entry_duration)
        self.ip2mac.set(IP_Broadcast, MAC_Broadcast, float('inf'))

    def process(self, raw):
        pdu = PDU(raw)
        self.ip2mac[pdu.src_ip] = pdu.src_mac
        if pdu.oper == PDU.Oper.IAm:
            self.update(pdu)
            return
        if pdu.dst_ip != self.host.ip:
            self.report(
                'got irrelevent request for {} (i\'m {})',
                pdu.dst_ip, self.host.ip)
            return
        if pdu.oper == PDU.Oper.WhoIs:
            self.respond(pdu)
            return

    def update(self, respond):
        ip, mac = respond.src_ip, respond.src_mac
        self.report('oh, {} is {}', mac, ip)
        self.ip2mac[ip] = mac
        self.updated(ip, mac)

    def query(self, dst_ip):
        pdu = PDU(
            oper=PDU.Oper.WhoIs,
            src_ip=self.host.ip,
            src_mac=self.host.mac,
            dst_ip=dst_ip,
            dst_mac=MAC_Invalid,
        )
        frame = Frame(
            dst_mac=MAC_Broadcast,
            src_mac=self.host.mac,
            ethertype=Frame.EtherType.ARP,
            payload=pdu.raw,
        )
        self.send_frame(frame)
        self.report('Who has {}? Tell {}',
                    pdu.dst_ip,
                    pdu.src_ip)

    def respond(self, request):
        pdu = PDU(
            oper=PDU.Oper.IAm,
            src_ip=self.host.ip,
            src_mac=self.host.mac,
            dst_ip=request.src_ip,
            dst_mac=request.src_mac,
        )
        frame = Frame(
            dst_mac=request.src_mac,
            src_mac=self.host.mac,
            ethertype=Frame.EtherType.ARP,
            payload=pdu.raw,
        )
        self.send_frame(frame)
        self.report(
            'tell {} that {} is {}',
            pdu.dst_ip, pdu.src_mac, pdu.src_ip,
        )