def test_string(name, val): print "%s = %s " % (name, val), try: prefix = ipp.from_s(val) print "%s = %s" % (name, prefix) ps = str(prefix) if ps.find("::") >= 0: sc = ps.replace("::", "!") if sc.count(":") == 5: nps = sc.replace("!", ":0:") print ">>> nps = %s" % nps except Exception, err: #traceback.print_exc() print "err = %s" % err
def test_string(name, val): print "%s = %s " % (name, val), try: prefix = ipp.from_s(val) print "%s = %s" % (name, prefix) ps = str(prefix) if ps.find("::") >= 0: sc = ps.replace("::", "!") if sc.count(":") == 5: nps = sc.replace("!", ":0:"); print ">>> nps = %s" % nps except Exception, err: #traceback.print_exc() print "err = %s" % err
def add_to_iph(pref_s): global iph4, iph6 s = ipp.from_s(pref_s) if s.version == 4: v = iph4.get(s) if v: iph4[s] += 1 else: iph4[s] = 1 else: v = iph6.get(s) if v: iph6[s] += 1 else: iph6[s] = 1
def test_string(name, val): print("%s = %s" % (name, val)) try: prefix = ipp.from_s(val) print("%s = %s\n" % (name, prefix)) ps = str(prefix) if ps.find("::") >= 0: #sl = re.findall("[^:]:[^:]", ps) sc = ps.replace("::", "!") if sc.count(":") == 5: nps = sc.replace("!", ":0:") print(">>> nps = %s\n" % nps) except Exception as err: #traceback.print_exc() print("err = %s" % err)
import ipp v61 = ipp.from_s("d001:0df:0:123:0:0:130:216:38:123") v62 = ipp.from_s("e001:0df:0:123::130:216:38:123") v63 = ipp.from_s("f001:0df::123:0:0:130:216:38:123") print "ascii 2001:0df:0:123:0:0:130:216:38:123" print " v61 %s" % v61 print " v62 %s" % v62 print " v63 %s" % v63
import ipp from plt_testing import * ipa = ipp.from_s("1.2.3.4") # 1 ipb = ipp.from_s("1.2.3.5") # 2 ipc = ipp.from_s("1.2.4.4") # 3 ipd = ipp.from_s("1.3.3.4") # 4 ipe = ipp.from_s("2.2.3.4") # 5 h = {ipa: 1, ipb: 2, ipc: 3, ipd: 4, ipe: 5} for ip in sorted(h): test_println(str("%s -> %d, %d" % (ip, h[ip], ip.__hash__())), get_tag()) ip01 = ipp.from_s("192.168.10.1/16") test_println(str("ip01 = %s" % ip01), get_tag()) test_println(str("ip01.complement = %s" % ip01.complement), get_tag()) test_println(str("ip01.width = %d" % ip01.width), get_tag()) test_println(str("ip01.is_rfc1918 = %s" % ip01.is_rfc1918), get_tag()) ip88 = ipp.from_s("192.168.0.0/16") ip89 = ipp.from_s("192.168.0.0") def test_ip(ip): test_println(str("--- trying %s" % ip), get_tag()) test_println( str(" slash: %s.is_prefix(%s) -> %s" % (ip88, ip, ip88.is_prefix(ip))), get_tag()) r = ip.is_rfc1918 test_println(str("%s -> %s" % (ip, r)), get_tag())
test_string("p61", "2001:df0:0:321:1:2:3:4/128") test_string("p62", "2001:df0:0:0:1:2:3:4/128") test_string("p63", "2001:df0:0:0:1:2::") test_string("p64", "2001:df0:0:abcd::1") test_string("p65", "2001:0:0df0::2") test_string("p66", "2001::def0::2") test_string("p67", "::ffff:1.2.3.4") # From RFC 5952 test_string("\np41", "130.216.38.7/24") test_string("p42", "130.256.0.0") test_string("p43", "130.216.0.0/33") test_string("p44", "130.216.0.0/-1") test_string("p45", "130.216/24") p4 = ipp.from_s("130.216.0.0/24") print "\np4 = %s, type(p4) = %s" % (p4, type(p4)) ba = bytearray([130, 216, 128, 0]) q4 = ipp.IPprefix(4, ba, 25) print "q4 = %s, type(q4) = %s\n" % (q4, type(q4)) print "p4 < q4: result = %s, %s (T, F)" % (p4 < q4, q4 < p4) print "p4 <= q4: result = %s, %s (T, F)" % (p4 <= q4, q4 <= p4) print "p4 == q4: result = %s, %s (F, F)" % (p4 == q4, q4 == p4) print "p4 != q4: result = %s, %s (T, T)" % (p4 != q4, q4 != p4) print "p4 >= q4: result = %s, %s (F, T)" % (p4 >= q4, q4 >= p4) print "p4 > q4: result = %s, %s (F, T)" % (p4 > q4, q4 > p4) print "\np4.is_prefix(q4): result = %s, %s (T, F)" % ( p4.is_prefix(q4), q4.is_prefix(p4)) print "p4.is_prefix(p4): result = %s, %s (T, T)\n" % (
import ipp s = ipp.from_s("130.216.3.10") print "s = %s" % s print "version = %d, addr = %s, length = %d\n" % ( s.version, s.addr, s.length) iph4 = {} iph6 = {} def add_to_iph(pref_s): global iph4, iph6 s = ipp.from_s(pref_s) if s.version == 4: v = iph4.get(s) if v: iph4[s] += 1 else: iph4[s] = 1 else: v = iph6.get(s) if v: iph6[s] += 1 else: iph6[s] = 1 add_to_iph("130.216.3.10") add_to_iph("130.216.3.11") add_to_iph("130.216.3.10") add_to_iph("130.216.5.10")
#!/usr/bin/env python # Thu, 13 Mar 14 (PDT) # ip6.py: Demonstrate IPv6 objects # Copyright (C) 2016, Nevil Brownlee, U Auckland | WAND import ipp, plt import natkit import sys from plt_testing import * UAv4 = ipp.from_s("130.216.0.0/16") UAv6 = ipp.from_s("2001:df0::/47") linklocal = ipp.from_s("fe80::/10") multicast = ipp.from_s("ff00::/8") fh = natkit.FlowHome(UAv4, UAv6) # List of 'home' prefixes' # hf = fh(pkt) returns: # hf.src_in_home # hf.dst_in_home # # hf.home_key = if src in home or dst in home # flow key with home address as dest # else None # hf.inward = True if pkt dest was in home # (outward home_key has addresses and ports swapped) def print_flow(n, ipf, tag=''): test_println("%5d: %d %3d %5d %5d %s %s" % (n, # v6 ipf.version, ipf.proto, ipf.src_port, ipf.dst_port, ipf.src_prefix, ipf.dst_prefix), tag+get_tag())
6, bytearray( b"\x02\x00\x00\x01\x0d\x0d\x07\x00\x00\x12\x00\x00\x00\x00\xab\xcd"), 120) v6d = ipp.IPprefix( 6, bytearray( b"\x20\x01\x0d\xf0\x0d\x0d\x00\x00\xde\xad\x00\x00\x00\x00\xab\xcd"), 47) test_println("v6a = {0}".format(v6a), get_tag()) test_println("v6b = {0}".format(v6b), get_tag()) test_println("v6c = {0}".format(v6c), get_tag()) test_println("v6d = {0}\n".format(v6d), get_tag()) fs4 = ipp.from_s("130.216.1.1") test_println("fs4 = {0}".format(fs4), get_tag()) fs4s = ipp.from_s("130.216.1.1/16") test_println("fs4s = {0}".format(fs4s), get_tag()) fs6 = ipp.from_s("2001:0df0::00ef:0000:0000:0001") test_println("fs4 = {0}".format(fs6), get_tag()) fs6s = ipp.from_s("2001:0df0::/47") test_println("fs4s = {0}\n".format(fs6s), get_tag()) test_println(str(ipp.rfc1918s16), get_tag()) test_println(str(ipp.rfc1918s12), get_tag()) test_println(str(ipp.rfc1918s8), get_tag()) r1918_16 = ipp.from_s("192.168.1.1/24") n1918_16 = ipp.from_s("192.169.1.1/24")
test_string("p61", "2001:df0:0:321:1:2:3:4/128") test_string("p62", "2001:df0:0:0:1:2:3:4/128") test_string("p63", "2001:df0:0:0:1:2::") test_string("p64", "2001:df0:0:abcd::1") test_string("p65", "2001:0:0df0::2") test_string("p66", "2001::def0::2") test_string("p67", "::ffff:1.2.3.4") # From RFC 5952 test_string("\np41", "130.216.38.7/24") test_string("p42", "130.256.0.0") test_string("p43", "130.216.0.0/33") test_string("p44", "130.216.0.0/-1") test_string("p45", "130.216/24") p4 = ipp.from_s("130.216.0.0/24") print "\np4 = %s, type(p4) = %s" % (p4, type(p4)) ba = bytearray([130, 216, 128, 0]) q4 = ipp.IPprefix(4, ba, 25) print "q4 = %s, type(q4) = %s\n" % (q4, type(q4)) print "p4 < q4: result = %s, %s (T, F)" % (p4 < q4, q4 < p4) print "p4 <= q4: result = %s, %s (T, F)" % (p4 <= q4, q4 <= p4) print "p4 == q4: result = %s, %s (F, F)" % (p4 == q4, q4 == p4) print "p4 != q4: result = %s, %s (T, T)" % (p4 != q4, q4 != p4) print "p4 >= q4: result = %s, %s (F, T)" % (p4 >= q4, q4 >= p4) print "p4 > q4: result = %s, %s (F, T)" % (p4 > q4, q4 > p4) print "\np4.is_prefix(q4): result = %s, %s (T, F)" % (p4.is_prefix(q4), q4.is_prefix(p4)) print "p4.is_prefix(p4): result = %s, %s (T, T)\n" % (p4.is_prefix(p4),
print "err = %s" % err test_string("p61", "2001:df0:0:321:1:2:3:4/128") test_string("p62", "2001:df0:0:0:1:2:3:4/128") test_string("p63", "2001:df0:0:0:1:2::") test_string("p64", "2001:df0:0:abcd::1") test_string("p65", "2001:0:0df0::2") test_string("p66", "2001::def0::2") test_string("p67", "::ffff:1.2.3.4") # From RFC 5952 test_string("p41", "130.216.38.7/24") test_string("p42", "130.256.0.0") test_string("p43", "130.216.0.0/33") test_string("p44", "130.216.0.0/-1") test_string("p45", "130.216") test_string("p46", "130.216/24") p = ipp.from_s("130.216.0.0/24") print "\np = %s, type(q) = %s" % (p, type(p)) print ba = bytearray([130, 216, 0, 0]) q = ipp.IPprefix(4, ba, 25) #q = ipp.IPprefix() print "q = %s, type(q) = %s" % (q, type(q)) #HomeFlow.py and test-v6-tunnal.py now fail
#lib.t_ipp(tpp) ba = bytearray([130, 216, 1, 2]) print "type(ba) = %s" % type(ba) #nba = ffi.new("uint8_t[4]") #ffi.memmove(nba, ba, len(nba)) #for v in nba: # print "%02x" % v, #print #print "bp.version=%d, bp.addr=%s, bp.length=%d" % ( # bp.version, bp.addr, bp.length) s = "2001:df0::2006:20e:c6ff:fef7:1f9f/64" p1 = ipp.from_s(s) print "p1.version=%d, p1.addr=%s, p1.length=%s" % (p1.version, p1.addr, p1.length) print "p1 = %s" % p1 bp = ipp.IPprefix(4, ba, 24) print "bp = %s (type = %s)" % (bp, bp.__class__.__name__) try: xxx = ipp.IPprefix(4, "1.2.3.4") except: pass print s = "2001:df0:0:2006:20e:c6ff:fef7:1f9f/64" p6 = ipp.from_s(s)
import ipp s = ipp.from_s("130.216.3.10") print "s = %s" % s print "version = %d, addr = %s, length = %d\n" % (s.version, s.addr, s.length) iph4 = {} iph6 = {} def add_to_iph(pref_s): global iph4, iph6 s = ipp.from_s(pref_s) if s.version == 4: v = iph4.get(s) if v: iph4[s] += 1 else: iph4[s] = 1 else: v = iph6.get(s) if v: iph6[s] += 1 else: iph6[s] = 1 add_to_iph("130.216.3.10") add_to_iph("130.216.3.11") add_to_iph("130.216.3.10") add_to_iph("130.216.5.10")
nip += 1 #plt.Data_dump(ip.pi, "ip from pkt") print "--- n=%d --- cap_len=%d" % (n, pkt.capture_len) sa = ip.src_prefix da = ip.dst_prefix print "sa = %s, da= %s" % (sa, da) r = ip.test_l3_cksm() print "test_l3_cksm() returned %s" % r pr = ip.test_trans_cksm() print "test_trans_cksm() returned %s" % pr if ip.version == 4: nsa = ipp.from_s("1.2.3.4") nda = ipp.from_s("5.6.7.8") else: nsa = ipp.from_s("1:2:3:4::") nda = ipp.from_s("::5:6:7:8") ip.src_prefix = nsa ip.dst_prefix = nda sa = ip.src_prefix da = ip.dst_prefix print "sa = %s, da= %s" % (sa, da) d = ip.data print_all(d) print "ver=%d, proto=%d, traffic_class=%s, hop_limit=%d" % ( ip.version, ip.proto, qfmt("%x", ip.traffic_class), ip.hop_limit) ip.traffic_class = 0x34
ba = bytearray([130, 216, 1, 2]) print "type(ba) = %s" % type(ba) #nba = ffi.new("uint8_t[4]") #ffi.memmove(nba, ba, len(nba)) #for v in nba: # print "%02x" % v, #print #print "bp.version=%d, bp.addr=%s, bp.length=%d" % ( # bp.version, bp.addr, bp.length) s = "2001:df0::2006:20e:c6ff:fef7:1f9f/64" p1 = ipp.from_s(s) print "p1.version=%d, p1.addr=%s, p1.length=%s" % ( p1.version, p1.addr, p1.length) print "p1 = %s" % p1 bp = ipp.IPprefix(4, ba, 24) print "bp = %s (type = %s)" % (bp, bp.__class__.__name__) try: xxx = ipp.IPprefix(4, "1.2.3.4") except: pass print s = "2001:df0:0:2006:20e:c6ff:fef7:1f9f/64" p6 = ipp.from_s(s)