예제 #1
0
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
예제 #2
0
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