Exemplo n.º 1
0
    def test_icmpv4(self):
        # create one packet, copy its bytes, then compare their fields
        icmp = icmpv4()
        assert icmp != None
        icmp.type = 8
        icmp.code = 0
        # Create a packet to compare against
        icmpnew = icmpv4()
        icmpnew.decode(icmp.bytes)

        self.assertEqual(icmp.bytes, icmpnew.bytes, "bytes not equal")
        self.assertEqual(icmpnew.type, 8, "type not equal %d" % icmp.type)
        self.assertEqual(icmpnew.code, 0, "code not equal %d" % icmp.code)
Exemplo n.º 2
0
    def test_icmpv4(self):
        # create one packet, copy its bytes, then compare their fields
        icmp = icmpv4()
        assert (icmp != None)
        icmp.type = 8
        icmp.code = 0
        # Create a packet to compare against
        icmpnew = icmpv4()
        icmpnew.decode(icmp.bytes)

        self.assertEqual(icmp.bytes, icmpnew.bytes, "bytes not equal")
        self.assertEqual(icmpnew.type, 8, "type not equal %d" % icmp.type)
        self.assertEqual(icmpnew.code, 0, "code not equal %d" % icmp.code)
Exemplo n.º 3
0
    def test_icmpv4_ping(self):
        import os

        uname = os.uname()[0]
        if uname == "FreeBSD":
            devname = "edsc0"
        elif uname == "Linux":
            devname = "lo"
        elif uname == "Darwin":
            devname = "en0"
        else:
            print "unknown host os %s" % uname
            return

        e = ethernet()
        e.type = 0x0800
        e.src = "\x00\x00\x00\x00\x00\x00"
        e.dst = "\xff\xff\xff\xff\xff\xff"
        e.type = 0x0800

        ip = ipv4()
        ip.version = 4
        ip.hlen = 5
        ip.tos = 0
        ip.length = 28
        ip.id = 1234
        ip.flags = 0
        ip.offset = 0
        ip.ttl = 64
        ip.protocol = IPPROTO_ICMP
        ip.src = inet_atol("127.0.0.1")
        ip.dst = inet_atol("127.0.0.1")

        icmp = icmpv4()
        icmp.type = 8
        icmp.code = 0
        icmp.cksum = 0

        echo = icmpv4echo()
        echo.id = 37123
        echo.sequence = 0

        ip.len = len(ip.bytes) + len(icmp.bytes) + len(echo.bytes)

        packet = Chain([e, ip, icmp, echo])

        packet.calc_checksums()
        packet.encode()

        input = PcapConnector(devname)
        input.setfilter("icmp")

        output = PcapConnector(devname)
        assert ip != None

        # XXX The use of IP triggers a bpf header format bug if used
        # with loopback device on FreeBSD, so we use edsc(4) there.

        n_out = output.write(packet.bytes, 42)
        assert n_out == 42

        packet_in = input.read()
        assert n_out == len(packet_in)
Exemplo n.º 4
0
Arquivo: ping.py Projeto: jeamland/PCS
def main():

    from optparse import OptionParser
    
    parser = OptionParser()
    parser.add_option("-c", "--count",
                      dest="count", default=1,
                      help="Stop after sending (and recieving) count ECHO_RESPONSE packets..")
    
    parser.add_option("-D", "--dont_fragment",
                      dest="df", default=False,
                      help="Set the Don't Fragment bit.")

    parser.add_option("-s", "--ip_source",
                      dest="ip_source", default=None,
                      help="The IP source address.")

    parser.add_option("-d", "--ip_dest",
                      dest="ip_dest", default=None,
                      help="The IP destination address.")

    parser.add_option("-I", "--ether_iface",
                      dest="ether_iface", default=None,
                      help="The name of the source interface.")

    parser.add_option("-e", "--ether_source",
                      dest="ether_source", default=None,
                      help="The host Ethernet source address.")

    parser.add_option("-g", "--ether_dest",
                      dest="ether_dest", default=None,
                      help="The gateway Ethernet destination address.")

    (options, args) = parser.parse_args()
 
    c = ethernet(src=ether_atob(options.ether_source),  \
                 dst=ether_atob(options.ether_dest)) /  \
        ipv4(ttl=64, src=inet_atol(options.ip_source),  \
                     dst=inet_atol(options.ip_dest)) / \
        icmpv4(type=8) / icmpv4echo(id=12345) / payload(payload="foobar")

    c.calc_lengths()

    #
    # Increment ICMP echo sequence number with each iteration.
    #
    output = PcapConnector(options.ether_iface)
    ip = c.packets[1]
    echo = c.packets[3]
    count = int(options.count)
    while (count > 0):
        c.calc_checksums()
        c.encode()

        out = output.write(c.bytes, len(c.bytes))
#        packet = input.read()
#        print packet
        sleep(1)
        count -= 1
        ip.id += 1
        echo.sequence += 1
Exemplo n.º 5
0
    def test_icmpv4_ping(self):
	import os
	uname = os.uname()[0]
	if uname == "FreeBSD":
	    devname = "edsc0"
	elif uname == "Linux":
	    devname = "lo"
        elif uname == "Darwin":
            devname = "en0"
	else:
	    print "unknown host os %s" % uname
	    return

	e = ethernet()
	e.type = 0x0800
        e.src = "\x00\x00\x00\x00\x00\x00"
        e.dst = "\xff\xff\xff\xff\xff\xff"
        e.type = 0x0800

        ip = ipv4()
        ip.version = 4
        ip.hlen = 5
        ip.tos = 0
        ip.length = 28
        ip.id = 1234
        ip.flags = 0
        ip.offset = 0
        ip.ttl = 64
        ip.protocol = IPPROTO_ICMP
        ip.src = inet_atol("127.0.0.1")
        ip.dst = inet_atol("127.0.0.1")

        icmp = icmpv4()
        icmp.type = 8
        icmp.code = 0
        icmp.cksum = 0
        
        echo = icmpv4echo()
        echo.id = 37123
        echo.sequence = 0

	ip.len = len(ip.bytes) + len(icmp.bytes) + len(echo.bytes)

        packet = Chain([e, ip, icmp, echo])

        packet.calc_checksums()
        packet.encode()

        input = PcapConnector(devname)
        input.setfilter("icmp")

        output = PcapConnector(devname)
        assert (ip != None)

	# XXX The use of IP triggers a bpf header format bug if used
	# with loopback device on FreeBSD, so we use edsc(4) there.

        n_out = output.write(packet.bytes, 42)
        assert (n_out == 42)

	packet_in = input.read()
	assert (n_out == len(packet_in))