def discompose(self, raw): self.yiaddr = IP.from_bytes(raw.pop(4)) self.siaddr = IP.from_bytes(raw.pop(4)) self.chaddr = MAC.from_bytes(raw.pop(1)) self.options = OrderedDict() while True: opt = raw.pop(1) if opt == DHCP.Opt.End: break length = struct.unpack('!B', raw.pop(1))[0] content = raw.pop(length) if opt == DHCP.Opt.MessageType: message_type = DHCP.Opt.Ext.MessageType[content] self.options['MessageType'] = message_type self.MessageType = message_type elif opt == DHCP.Opt.RequestedIP: self.options['RequestedIP'] = IP.from_bytes(content) elif opt == DHCP.Opt.ServerIdentifier: self.options['ServerIdentifier'] = IP.from_bytes(content) elif opt == DHCP.Opt.SubnetMask: self.options['SubnetMask'] = IP.from_bytes(content) elif opt == DHCP.Opt.Router: ips = [content[i:i+4] for i in xrange(0, len(content), 4)] self.options['Router'] = map(IP.from_bytes, ips) elif opt == DHCP.Opt.LeaseTime: self.options['LeaseTime'] = struct.unpack('!I', content)[0]
def discompose(self, raw): self.yiaddr = IP.from_bytes(raw.pop(4)) self.siaddr = IP.from_bytes(raw.pop(4)) self.chaddr = MAC.from_bytes(raw.pop(1)) self.options = OrderedDict() while True: opt = raw.pop(1) if opt == DHCP.Opt.End: break length = struct.unpack("!B", raw.pop(1))[0] content = raw.pop(length) if opt == DHCP.Opt.MessageType: message_type = DHCP.Opt.Ext.MessageType[content] self.options["MessageType"] = message_type self.MessageType = message_type elif opt == DHCP.Opt.RequestedIP: self.options["RequestedIP"] = IP.from_bytes(content) elif opt == DHCP.Opt.ServerIdentifier: self.options["ServerIdentifier"] = IP.from_bytes(content) elif opt == DHCP.Opt.SubnetMask: self.options["SubnetMask"] = IP.from_bytes(content) elif opt == DHCP.Opt.Router: ips = [content[i : i + 4] for i in xrange(0, len(content), 4)] self.options["Router"] = map(IP.from_bytes, ips) elif opt == DHCP.Opt.LeaseTime: self.options["LeaseTime"] = struct.unpack("!I", content)[0]
def compose(self, yiaddr, siaddr, chaddr, *opts): r = '' r += IP(yiaddr) r += IP(siaddr) r += MAC(chaddr) opts = list(opts) for i, opt in enumerate(opts): if isinstance(opt, int): opts[i] = struct.pack('!B', opt) elif not isinstance(opt, str): opts[i] = str(opt) r += ''.join(opts) return r
def send_ip(self, data, dst_ip, protocol=Packet.Protocol.Raw): dst_ip = IP(dst_ip) if not dst_ip: self.report('invalid dst_ip {}, ignored', dst_ip) return try: _, _, gateway, interface = self.routes.find(dst_ip) except KeyError: self.report('no gateway for destination {}, ignored', dst_ip) return if gateway: self.report('{} is non-local, beg {} to deliver', dst_ip, gateway) else: self.report('{} is local, send directly', dst_ip) packet = Packet( src_ip=interface.ip, dst_ip=dst_ip, protocol=protocol, payload=data, ) interface.send_packet( packet.raw, gateway if gateway else dst_ip, Frame.EtherType.IPv4, )
def ip(self, val): if isinstance(val, str): if '/' in val: val, net_len = val.split('/') net_len = int(net_len) mask = '1' * net_len + '0' * (32 - net_len) self.mask = IP.from_bits(bitarray(mask)) self._ip = ip_from_user(val)
def ip(self, val): if isinstance(val, str): if '/' not in val: raise ValueError( 'interface need subnet mask (e.g. "{}/24")'.format(val)) val, net_len = val.split('/') net_len = int(net_len) mask = '1' * net_len + '0' * (32 - net_len) self.mask = IP.from_bits(bitarray(mask)) self._ip = ip_from_user(val)
def connect(self, addr): ip, port = addr ip = IP(ip) port = Port(port) if not self.bound: self.bind((IP_All, None)) segment = Segment( src_port=self.port, dst_port=port, syn=True, ) self.host.send_ip(segment.raw, ip, protocol=Packet.Protocol.TCP)
def bind(self, sock, addr): ip, port = addr ip = IP(ip) if not ip: ip = self.interface.ip if port is None: port = self.ports[sock.type].popleft() port = Port(port) addr = (ip, port) addr2socket = self.get_addr2socket(sock) addr2socket[addr] = sock return addr
def bind(self, sock, addr): ip, port = addr ip = IP(ip) if not ip: ip = self.interface.ip port = Port(port) # TODO: port == None addr = (ip, port) if sock.type == LibSocket.SOCK_DGRAM: addr2socket = self.sockets[Packet.Protocol.UDP] else: self.report('invalid bind') return (None, None) addr2socket[addr] = sock return addr
def ack(self, request): self.report('DHCP Ack {}', request.options['RequestedIP']) routers = [IP(ip).bytes_form for ip in self.routers] pdu = DHCP.PDU( # yiaddr (your ip address) request.options['RequestedIP'], # siaddr (server ip address) self.sock.ip, # chaddr (client hardware address) request.chaddr, # options DHCP.Opt.MessageType, 1, DHCP.Opt.Ext.MessageType.Ack, DHCP.Opt.SubnetMask, 4, self.subnet_mask.bytes_form, DHCP.Opt.Router, 4 * len(routers), ''.join(routers), DHCP.Opt.LeaseTime, 4, struct.pack('!I', self.lease_time), # end DHCP.Opt.End, ) self.sock.sendto(pdu.raw, (IP_Broadcast, DHCP.ClientPort))
def send(self, data, ip, protocol=Packet.Protocol.Raw): dst_ip = IP(ip) _, _, gateway, interface = self.routes.find(dst_ip) if gateway: self.report('{} is non-local, beg {} to deliver', dst_ip, gateway) else: self.report('{} is local, send directly', dst_ip) packet = Packet( src_ip=interface.ip, dst_ip=dst_ip, protocol=protocol, payload=data, ) interface.send_packet( packet.raw, gateway if gateway else dst_ip, Frame.EtherType.IPv4, )
def add(self, net, mask, gateway, interface): self.entries.append(Entry(IP(net), IP(mask), IP(gateway), interface))
def default_gateway(self, val): gateway = IP(val) self._default_gateway = gateway self.routes[-1].gateway = gateway
def bind(self, addr): ip, port = IP(addr[0]), Port(addr[1]) self.local_addr = self.host.bind(self, (ip, port)) self.ip, self.port = self.local_addr
def __init__(self, host): super(DHCPServer, self).__init__(host) self.pool = Pool() self.subnet_mask = IP('0.0.0.0') self.routers = ['0.0.0.0'] self.lease_time = 8000
def bind(self, addr): ip, port = IP(addr[0]), Port(addr[1]) if addr[1] else None self.local_addr = self.host.bind(self, (ip, port)) self.ip, self.port = self.local_addr self.bound = True
def default_gateway(self, val): gateway = IP(val) self._default_gateway = gateway self.routes[-1].gateway = gateway self.report('default gateway changed to {}', gateway)