def __init__(self, ip=None, mac=None): super(Host, self).__init__(n_tips=1) self.tip = self.tips[0] if mac is None: mac = self.index + 1 if ip is None: ip = '1.0.0.{}'.format(self.index + 1) # physical layer - frame delimiter self.delimiter = BitDelimiter(self.tip) # link layer - flow control self.framer = Framer(self.delimiter, mac_to_bytes(mac)) # network layer - ARP self.packeter = Packeter(self.framer, ip_from_str(ip))
class Host(Entity): def __init__(self, ip=None, mac=None): super(Host, self).__init__(n_tips=1) self.tip = self.tips[0] if mac is None: mac = self.index + 1 if ip is None: ip = '1.0.0.{}'.format(self.index + 1) # physical layer - frame delimiter self.delimiter = BitDelimiter(self.tip) # link layer - flow control self.framer = Framer(self.delimiter, mac_to_bytes(mac)) # network layer - ARP self.packeter = Packeter(self.framer, ip_from_str(ip)) @property def status(self): nic = OrderedDict([ ('ip', '{}'.format(self.ip)), ('mac', '{:02x}'.format(self.mac)), ('nic', self.delimiter), ('tip', self.tip), ]) return [('nic', nic), ('opdu', Discomposer(lambda: self.delimiter.odata)), ('ipdu', Discomposer(lambda: self.delimiter.idata)), ] @property def mac(self): return mac_from_bytes(self.framer.mac) @mac.setter def mac(self, val): self.framer.mac = mac_to_bytes(val) @property def ip(self): return ip_to_str(self.packeter.ip) @ip.setter def ip(self, val): self.packeter.ip = ip_from_str(val) def l3send(self, data, to, protocol=Packet.Protocol.Raw): self.packeter.send(data, to=ip_from_str(to), protocol=protocol) def l2send(self, data, to): self.framer.send(data, mac_to_bytes(to)) def l2recv(self): return self.framer.recv() def sending(self, at): return self.delimiter.sending def sent(self, at): return self.delimiter.sent