| 1 | // Copyright 2015 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package ip |
| 16 | |
| 17 | import ( |
| 18 | "math/big" |
| 19 | "net" |
| 20 | ) |
| 21 | |
| 22 | // NextIP returns IP incremented by 1, if IP is invalid, return nil |
| 23 | func NextIP(ip net.IP) net.IP { |
| 24 | normalizedIP := normalizeIP(ip) |
| 25 | if normalizedIP == nil { |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | i := ipToInt(normalizedIP) |
| 30 | return intToIP(i.Add(i, big.NewInt(1)), len(normalizedIP) == net.IPv6len) |
| 31 | } |
| 32 | |
| 33 | // PrevIP returns IP decremented by 1, if IP is invalid, return nil |
| 34 | func PrevIP(ip net.IP) net.IP { |
| 35 | normalizedIP := normalizeIP(ip) |
| 36 | if normalizedIP == nil { |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | i := ipToInt(normalizedIP) |
| 41 | return intToIP(i.Sub(i, big.NewInt(1)), len(normalizedIP) == net.IPv6len) |
| 42 | } |
| 43 | |
| 44 | // Cmp compares two IPs, returning the usual ordering: |
| 45 | // a < b : -1 |
| 46 | // a == b : 0 |
| 47 | // a > b : 1 |
| 48 | // incomparable : -2 |
| 49 | func Cmp(a, b net.IP) int { |
| 50 | normalizedA := normalizeIP(a) |
| 51 | normalizedB := normalizeIP(b) |
| 52 | |
| 53 | if len(normalizedA) == len(normalizedB) && len(normalizedA) != 0 { |
| 54 | return ipToInt(normalizedA).Cmp(ipToInt(normalizedB)) |
| 55 | } |
| 56 | |
| 57 | return -2 |
| 58 | } |
| 59 | |
| 60 | func ipToInt(ip net.IP) *big.Int { |
| 61 | return big.NewInt(0).SetBytes(ip) |
| 62 | } |
| 63 | |
| 64 | func intToIP(i *big.Int, isIPv6 bool) net.IP { |
| 65 | intBytes := i.Bytes() |
| 66 | |
| 67 | if len(intBytes) == net.IPv4len || len(intBytes) == net.IPv6len { |
| 68 | return intBytes |
| 69 | } |
| 70 | |
| 71 | if isIPv6 { |
| 72 | return append(make([]byte, net.IPv6len-len(intBytes)), intBytes...) |
| 73 | } |
| 74 | |
| 75 | return append(make([]byte, net.IPv4len-len(intBytes)), intBytes...) |
| 76 | } |
| 77 | |
| 78 | // normalizeIP will normalize IP by family, |
| 79 | // IPv4 : 4-byte form |
| 80 | // IPv6 : 16-byte form |
| 81 | // others : nil |
| 82 | func normalizeIP(ip net.IP) net.IP { |
| 83 | if ipTo4 := ip.To4(); ipTo4 != nil { |
| 84 | return ipTo4 |
| 85 | } |
| 86 | return ip.To16() |
| 87 | } |
| 88 | |
| 89 | // Network masks off the host portion of the IP, if IPNet is invalid, |
| 90 | // return nil |
| 91 | func Network(ipn *net.IPNet) *net.IPNet { |
| 92 | if ipn == nil { |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | maskedIP := ipn.IP.Mask(ipn.Mask) |
| 97 | if maskedIP == nil { |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | return &net.IPNet{ |
| 102 | IP: maskedIP, |
| 103 | Mask: ipn.Mask, |
| 104 | } |
| 105 | } |