def _buildEcho(req): if not isinstance(req, Icmp): req = base.extract(req, Icmp) if req is None: return None resp = Icmp() resp.type = TYPE_ECHO_REPLY resp.code = req.code resp.set_payload(req.next) resp.csum = 0 resp.csum = computeChecksum(resp.pack(), 0) return resp
def checksumBak(packet): if not isinstance(packet, Icmp): packet = extract(packet) if packet is None: return 0 packet.csum = 0 icmpHdr = packet.hdr() if packet.next is None: nextHdr = "" elif isinstance(packet.next, str): nextHdr = packet.next elif isinstance(packet.next, DummyHdr): nextHdr = packet.next.hdr() else: nextHdr = packet.next.hdr() s = "%s%s" % (icmpHdr, nextHdr) icmpPkt = Icmp(arr=s, prev=packet.prev) icmpPkt.next = nextHdr return _checksum(icmpPkt)
def buildUnreachResp(req, packet, unreachCode): icmpPkt = Icmp() icmpPkt.type = TYPE_DEST_UNREACH icmpPkt.code = unreachCode icmpPkt.set_payload(packet) icmpPkt.parsed = True ipPkt = ipv4.buildIpResponse(req, icmpPkt, protocol=Ipv4.ICMP_PROTOCOL, tos=0xC0) dummy = DummyHdr("\x00\x00\x00\x00") icmpPkt.set_payload(dummy) dummy.set_payload(packet) ipLen = 0 a = ipPkt while a is not None: m = getattr(a, "hdr", None) if m: n = len(a.hdr()) else: n = len(a) ipLen += n a = getattr(a, "next", None) ipPkt.iplen = ipLen icmpPkt.prev = ipPkt ethPkt = ethernet.buildResponse(req, ipPkt) icmpPkt.csum = 0 icmpPkt.csum = checksum(icmpPkt) return ethPkt