def test_icmpv6(self): # create one packet, copy its bytes, then compare their fields icmp = icmpv6() assert (icmp != None) icmp.type = ICMP6_ECHO_REQUEST icmp.code = 0 # Create a packet to compare against icmpnew = icmpv6() icmpnew.decode(icmp.bytes) self.assertEqual(icmp.bytes, icmpnew.bytes, "bytes not equal") self.assertEqual(icmpnew.type, ICMP6_ECHO_REQUEST, "type not equal %d" % icmp.type) self.assertEqual(icmpnew.code, 0, "code not equal %d" % icmp.code)
def ICMPv6_pack_message(iface, pkt_type): #procedure to get iface address #iface = 'wlan2' #interface addrs = netifaces.ifaddresses(iface) ll_src_addr = addrs[netifaces.AF_INET6][1]['addr'] ll_src_addr = ll_src_addr[:ll_src_addr.find('%')] gl_src_addr = addrs[netifaces.AF_INET6][0]['addr'] #gl_src_addr = gl_src_addr[:gl_src_addr.find('%')] #print gl_src_addr #print ll_src_addr # building ethernet header e = ethernet() mac = netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]['addr'] #geting mac addr e.src = ether_atob(mac) e.dst = ether_atob('ff:ff:ff:ff:ff:ff') e.type = 0x86dd #ETHERTYPE_IPV6 #building ipv6 packet basic info ip6 = ipv6() ip6.version = 6 #cs = icmp6.cksum(ip6) & 0xffff # building ipv6 header ip6.traffic_class = 0x0a ip6.flow = 0 ip6.length = 50 ip6.next_header = 58#IPPROTO_ICMPV6 ip6.hop = 255 ip6.src = pcs.inet_pton(AF_INET6, ll_src_addr) ip6.dst = pcs.inet_pton(AF_INET6, 'ff02::1') # building icmpv6 icmp6 = icmpv6(ICMP6_ECHO_REQUEST) #ICMP6_ECHO_REPLY icmp6.code = 0 icmp6.seq = 0 icmp6.id = 0x03e8 icmp6.checksum = 0x0 #kt.calc_checksums() data = payload(payload = gl_src_addr) #message data ip6.length = len(icmp6.getbytes()) + len(data) #recalculation of packet length if(pkt_type == 0): icmp6.cheksum = 0xf220 ip6.traffic_class = 0x0a pkt = pcs.Chain([e, ip6, icmp6, data]) #appendin packet else: icmp6.checksum = 0xe91b ip6.traffic_class = 0x0f pkt = pcs.Chain([e, ip6, icmp6]) #appendin packet #icmp6.checksum = pkt.calc_checksums() pkt.encode() #print pkt return pkt
def test_icmpv6_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 = ipv6() ip.traffic_class = 1 ip.flow = 0 ip.length = 64 ip.next_header = IPV6_ICMP ip.hop = 64 ip.src = inet_pton(AF_INET6, "::1") ip.dst = inet_pton(AF_INET6, "::1") icmp = icmpv6() icmp.type = 128 icmp.code = 0 icmp.cksum = 0 ip.len = len(ip.bytes) + len(icmp.bytes) packet = Chain([e, ip, icmp]) packet.calc_checksums() packet.encode() input = PcapConnector(devname) input.setfilter("icmp6") 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))
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.") parser.add_option("-r", "--rt_hop", dest="hop", default="::", help="The intermediate router address.") (options, args) = parser.parse_args() rtcount = 1 # plen is 8 + rtcount * 16 for rt hdr, 8 for ICMP, 6 for foobar plen = 8 + rtcount * 16 + 8 + 6 c = ethernet(src=ether_atob(options.ether_source), \ dst=ether_atob(options.ether_dest)) / \ ipv6(hop=64, next_header = 43, length = plen, \ src=inet_pton(AF_INET6, options.ip_source), \ dst=inet_pton(AF_INET6, options.ip_dest)) / \ ipv6ext.rt_ext(next_header = 58, \ addr1 = inet_pton(AF_INET6, options.hop)) / \ icmpv6(type=ICMP6_ECHO_REQUEST, 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] icmpecho = c.packets[3] count = int(options.count) while (count > 0): # c.calc_checksums() # icmpecho.cksum = icmpv6.cksum(icmpecho, ip) # icmpecho.calc_checksum() c.encode() out = output.write(c.bytes, len(c.bytes)) # packet = input.read() # print packet sleep(1) count -= 1 icmpecho.sequence += 1