def ap_func( self ): global selfmac def sendlan( o ): o.dst = gwmac o.src = selfmac print "发送出去" print `o` print "发送结束" #lancap.send( o ) #self.send_s.sendto(str(o),0,(gwip,53) ) print "结果:", wincap.sendto( self.send_s, str(o), (gwip,53) ) print "开始AP线程." lancap = getcap( lanid ) apcap = getcap( apid ) for a, b in apcap: d = dpkt.ethernet.Ethernet( b ) #d.dst = gwmac if isinstance ( d.data, dpkt.ip6.IP6): continue print `d` ip = d.data if isinstance( d.data, dpkt.arp.ARP ): print "ARP包" #sendlan( d ) continue if ip.dst == "\xff" * 6: # 广播包,直接转 print "广播包" d.src = selfmac lancap.send( d ) if isinstance( d.data.data, dpkt.udp.UDP ): if not selfmac: selfmac = d.dst udp = ip.data print "UDP 包",tip(ip.src),ip.data.sport,"==>", tip(ip.dst),udp.dport if udp.dport == 67: print "DHCP" pak = DhcpPacket() pak.DecodePacket( udp.data ) print pak.str() if pak.GetOption("dhcp_message_type") continue p = self.up.handleOut( ip.src, ip.data.sport, ip.dst, ip.data.dport ) d.data.src = selfip if( ip.data.dport == 53 ): d.data.dst = dnsip d.data.sum = 0 d.data.id = 0 # sendlan( d ) def lan_func(self ): lancap = getcap( lanid ) apcap = getcap( apid ) lancap.setfilter( )
def recieve_next_dhcp_packet(self, timeout=30): print "Here" while True: data = select.select([self._socket], [], [], timeout)[0] if not len(data): return None packet_data, source_address = self._socket.recvfrom(2048) if packet_data: packet = DhcpPacket() packet.source_address = source_address packet.DecodePacket(packet_data) return packet
def handle_socket(self) -> None: """Retrieves the next, waiting DHCP packet, parses it and calls the handler of the associated request. """ try: data, source_address = self._socket.recvfrom(2048) if len(data) == 0: self._log.warning("unexpectedly received EOF!") return packet = DhcpPacket() packet.source_address = source_address packet.DecodePacket(data) if (not packet.IsDhcpPacket()) or ( not packet.IsOption("dhcp_message_type") ): self._log.debug("Ignoring invalid packet") return dhcp_type = packet.GetOption("dhcp_message_type")[0] if dhcp_type not in self._DHCP_TYPE_HANDLERS: self._log.debug("Ignoring packet of unexpected DHCP type %d", dhcp_type) return xid = int.from_bytes(packet.GetOption('xid'), "big") if xid not in self._requests: self._log.debug("Ignoring answer with xid %r", xid) return request = self._requests[xid] clb_name = self._DHCP_TYPE_HANDLERS[dhcp_type] if not hasattr(request, clb_name): self._log.error("request has no callback '%s'", clb_name) return clb = getattr(request, clb_name) clb(packet) except Exception: self._log.exception('handling DHCP packet failed')