📄 icmp.py
字号:
"""ICMP packets."""# Copyright 1997, Corporation for National Research Initiatives# written by Jeremy Hylton, jeremy@cnri.reston.va.usimport inetimport arrayimport structICMP_MINLEN = 8ICMP_MASKLEN = 12ICMP_ECHOREPLY = 0ICMP_UNREACH = 3ICMP_UNREACH_NET = 0ICMP_UNREACH_HOST = 1ICMP_UNREACH_PROTOCOL = 2ICMP_UNREACH_PORT = 3ICMP_UNREACH_NEEDFRAG = 4ICMP_UNREACH_SRCFAIL = 5ICMP_SOURCEQUENCH = 4ICMP_REDIRECT = 5ICMP_REDIRECT_NET = 0ICMP_REDIRECT_HOST = 1ICMP_REDIRECT_TOSNET = 2ICMP_REDIRECT_TOSHOST = 3ICMP_ECHO = 8ICMP_TIMXCEED = 11ICMP_TIMXCEED_INTRANS = 0ICMP_TIMXCEED_REASS = 1ICMP_PARAMPROB = 12ICMP_TSTAMP = 13ICMP_TSTAMPREPLY = 14ICMP_IREQ = 15ICMP_IREQREPLY = 16ICMP_MASKREQ = 17ICMP_MASKREPLY = 18class Packet: """Basic ICMP packet definition. Equivalent to ICMP_ECHO_REQUEST and ICMP_REPLY packets. Other packets are defined as subclasses probably. """ def __init__(self, packet=None, cksum=1): if packet: self.__disassemble(packet, cksum) else: self.type = 0 self.code = 0 self.cksum = 0 self.id = 0 self.seq = 0 self.data = '' def __repr__(self): return "<ICMP packet %d %d %d %d>" % (self.type, self.code, self.id, self.seq) def assemble(self, cksum=1): idseq = struct.pack('HH', self.id, self.seq) packet = chr(self.type) + chr(self.code) + '\000\000' + idseq \ + self.data if cksum: self.cksum = inet.cksum(packet) packet = chr(self.type) + chr(self.code) \ + struct.pack('H', self.cksum) + idseq + self.data # Don't need to do any byte-swapping, because idseq is # appplication defined and others are single byte values. self.__packet = packet return self.__packet def __disassemble(self, packet, cksum=1): if cksum: our_cksum = inet.cksum(packet) if our_cksum != 0: raise ValueError, packet self.type = ord(packet[0]) self.code = ord(packet[1]) elts = struct.unpack('HHH', packet[2:8]) [self.cksum, self.id, self.seq] = map(lambda x:x & 0xffff, elts) self.data = packet[8:] def __compute_cksum(self): "Use inet.cksum instead" packet = self.packet if len(packet) & 1: packet = packet + '\0' words = array.array('H', packet) sum = 0 for word in words: sum = sum + (word & 0xffff) hi = sum >> 16 lo = sum & 0xffff sum = hi + lo sum = sum + (sum >> 16) return (~sum) & 0xffff class TimeExceeded(Packet): def __init__(self, packet=None, cksum=1): Packet.__init__(self, packet, cksum) if packet: if self.type != ICMP_TIMXCEED: raise ValueError, "supplied packet of wrong type" else: self.type = ICMP_TIMXCEED self.id = self.seq = 0class Unreachable(Packet): def __init__(self, packet=None, cksum=1): Packet.__init__(self, packet, cksum) if packet: if self.type != ICMP_UNREACH: raise ValueError, "supplied packet of wrong type" else: self.type = ICMP_UNREACH self.id = self.seq = 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -