public static Result <IPAddress> enhance_if_mapped(IPAddress ip) { // println!("real mapped {:x} {:x}", &ip.host_address, ip.host_address.clone().shr(32)); if (ip.is_mapped()) { return(Result <IPAddress> .Ok(ip)); } var ipv6_top_96bit = ip.host_address >> 32; if (ipv6_top_96bit == 0xffff) { // println!("enhance_if_mapped-1:{}", ); var num = ip.host_address % (new BigInteger(1) << 32); if (num == 0) { return(Result <IPAddress> .Ok(ip)); } //println!("ip:{},{:x}", ip.to_string(), num); var ipv4_bits = IpBits.V4; if (ipv4_bits.bits < ip.prefix.host_prefix()) { //println!("enhance_if_mapped-2:{}:{}", ip.to_string(), ip.prefix.host_prefix()); return(Result <IPAddress> .Err("enhance_if_mapped prefix not ipv4 compatible <<ip.prefix.host_prefix()>>")); } var mapped = IpV4.from_u32((UInt32)num, (ipv4_bits.bits - ip.prefix.host_prefix())); if (mapped.isErr()) { //println!("enhance_if_mapped-3"); return(mapped); } // println!("real mapped!!!!!={}", mapped.clone().unwrap().to_string()); return(Result <IPAddress> .Ok(ip.setMapped(mapped.unwrap()))); } return(Result <IPAddress> .Ok(ip)); }
// Return the ip address in a format compatible // with the IPv6 Mapped IPv4 addresses // // Example: // // ip = IPAddress("172.16.10.1/24") // // ip.to_ipv6 // // => "ac10:0a01" // // pub fn to_ipv6(my: &IPAddress) { // let part_mod = BigUint::one() << 16; // return String.format("{:04x}:{:04x}", // (my.host_address >> 16).mod_floor(&part_mod).to_u16().unwrap(), // my.host_address.mod_floor(&part_mod).to_u16().unwrap()); // } // Creates a new IPv4 object from an // unsigned 32bits integer. // // ip = IPAddress::IPv4::parse_u32(167772160) // // ip.prefix = 8 // ip.to_string // // => "10.0.0.0/8" // // The +prefix+ parameter is optional: // // ip = IPAddress::IPv4::parse_u32(167772160, 8) // // ip.to_string // // => "10.0.0.0/8" // // pub fn parse_u32(ip32: u32, prefix: u8) { // IPv4::new(String.format("{}/{}", IPv4::to_ipv4_str(ip32), prefix)) // } // Creates a new IPv4 object from binary data, // like the one you get from a network stream. // // For example, on a network stream the IP 172.16.0.1 // is represented with the binary "\254\020\n\001". // // ip = IPAddress::IPv4::parse_data "\254\020\n\001" // ip.prefix = 24 // // ip.to_string // // => "172.16.10.1/24" // // pub fn self.parse_data(str, prefix=32) // self.new(str.unpack("C4").join(".")+"/// {prefix}") // end // Extract an IPv4 address from a string and // returns a new object // // Example: // // str = "foobar172.16.10.1barbaz" // ip = IPAddress::IPv4::extract str // // ip.to_s // // => "172.16.10.1" // // pub fn self.extract(str) { // let re = Regexp::new(r"((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1 // \d\d|[1-9]\d|\d)") // IPv4::new(.match(str).to_s // } // Summarization (or aggregation) is the process when two or more // networks are taken together to check if a supernet, including all // and only these networks, exists. If it exists then this supernet // is called the summarized (or aggregated) network. // // It is very important to understand that summarization can only // occur if there are no holes in the aggregated network, or, in other // words, if the given networks fill completely the address space // of the supernet. So the two rules are: // // 1) The aggregate network must contain +all+ the IP addresses of the // original networks; // 2) The aggregate network must contain +only+ the IP addresses of the // original networks; // // A few examples will help clarify the above. Let's consider for // instance the following two networks: // // ip1 = IPAddress("172.16.10.0/24") // ip2 = IPAddress("172.16.11.0/24") // // These two networks can be expressed using only one IP address // network if we change the prefix. Let Ruby do the work: // // IPAddress::IPv4::summarize(ip1,ip2).to_s // // => "172.16.10.0/23" // // We note how the network "172.16.10.0/23" includes all the addresses // specified in the above networks, and (more important) includes // ONLY those addresses. // // If we summarized +ip1+ and +ip2+ with the following network: // // "172.16.0.0/16" // // we would have satisfied rule // 1 above, but not rule // 2. So "172.16.0.0/16" // is not an aggregate network for +ip1+ and +ip2+. // // If it's not possible to compute a single aggregated network for all the // original networks, the method returns an array with all the aggregate // networks found. For example, the following four networks can be // aggregated in a single /22: // // ip1 = IPAddress("10.0.0.1/24") // ip2 = IPAddress("10.0.1.1/24") // ip3 = IPAddress("10.0.2.1/24") // ip4 = IPAddress("10.0.3.1/24") // // IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).to_string // // => "10.0.0.0/22", // // But the following networks can't be summarized in a single network: // // ip1 = IPAddress("10.0.1.1/24") // ip2 = IPAddress("10.0.2.1/24") // ip3 = IPAddress("10.0.3.1/24") // ip4 = IPAddress("10.0.4.1/24") // // IPAddress::IPv4::summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string} // // => ["10.0.1.0/24","10.0.2.0/23","10.0.4.0/24"] // // pub fn self.summarize(args) // IPAddress.summarize(args) // end // Creates a new IPv4 address object by parsing the // address in a classful way. // // Classful addresses have a fixed netmask based on the // class they belong to: // // * Class A, from 0.0.0.0 to 127.255.255.255 // * Class B, from 128.0.0.0 to 191.255.255.255 // * Class C, D and E, from 192.0.0.0 to 255.255.255.254 // // Example: // // ip = IPAddress::IPv4.parse_classful "10.0.0.1" // // ip.netmask // // => "255.0.0.0" // ip.a? // // => true // // Note that classes C, D and E will all have a default // prefix of /24 or 255.255.255.0 // public static Result <IPAddress> parse_classful(String ip_s) { if (!IPAddress.is_valid_ipv4(ip_s)) { return(Result <IPAddress> .Err("Invalid IP <<ip_si>>")); } var o_ip = IPAddress.parse(ip_s); if (o_ip.isErr()) { return(o_ip); } var ip = o_ip.unwrap(); if (IpV4.is_class_a(ip)) { return(IPAddress.parse(string.Format("{0}/8", ip.to_s()))); } else if (IpV4.is_class_b(ip)) { return(IPAddress.parse(string.Format("{0}/16", ip.to_s()))); } else if (IpV4.is_class_c(ip)) { return(IPAddress.parse(string.Format("{0}/24", ip.to_s()))); } return(Result <IPAddress> .Ok(ip)); }