| 1 | // Copyright 2021 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 ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 19 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/" |
| 21 | ) |
| 22 | |
| 23 | // IP is a CNI maintained type inherited from net.IPNet which can |
| 24 | // represent a single IP address with or without prefix. |
| 25 | type IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP# struct { |
| 26 | netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#IPNet. |
| 27 | } |
| 28 | |
| 29 | // newIP will create an IP with net.IP and net.IPMask |
| 30 | func newIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/newIP().(ip netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP#, mask netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPMaskscip-go gomod github.com/golang/go/src go1.24.2 net/IPMask#) *IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP# { |
| 31 | return &IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#{ |
| 32 | IPNetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#IPNet.: netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#{ |
| 33 | IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP.: ip, |
| 34 | Maskscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#Mask.: mask, |
| 35 | }, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // ParseIP will parse string s as an IP, and return it. |
| 40 | // The string s must be formed like <ip>[/<prefix>]. |
| 41 | // If s is not a valid textual representation of an IP, |
| 42 | // will return nil. |
| 43 | func ParseIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ParseIP().(s string) *IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP# { |
| 44 | if stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.ContainsAnyscip-go gomod github.com/golang/go/src go1.24.2 strings/ContainsAny().(s, "/") { |
| 45 | ip, ipNet, err := netscip-go gomod github.com/golang/go/src go1.24.2 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.24.2 net/ParseCIDR().(s) |
| 46 | if err != nil { |
| 47 | return nil |
| 48 | } |
| 49 | return newIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/newIP().(ip, ipNet.Maskscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#Mask.) |
| 50 | } |
| 51 | ip := netscip-go gomod github.com/golang/go/src go1.24.2 net/.ParseIPscip-go gomod github.com/golang/go/src go1.24.2 net/ParseIP().(s) |
| 52 | if ip == nil { |
| 53 | return nil |
| 54 | } |
| 55 | return newIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/newIP().(ip, nil) |
| 56 | } |
| 57 | |
| 58 | // ToIP will return a net.IP in standard form from this IP. |
| 59 | // If this IP can not be converted to a valid net.IP, will return nil. |
| 60 | func (i *IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#) ToIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#ToIP().() netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP# { |
| 61 | switch { |
| 62 | case i.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() != nil: |
| 63 | return i.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() |
| 64 | case i.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..To16scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To16().() != nil: |
| 65 | return i.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..To16scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To16().() |
| 66 | default: |
| 67 | return nil |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // String returns the string form of this IP. |
| 72 | func (i *IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#) Stringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#String().() string { |
| 73 | if len(i.Maskscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#Mask.) > 0 { |
| 74 | return i.IPNetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#IPNet..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().() |
| 75 | } |
| 76 | return i.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().() |
| 77 | } |
| 78 | |
| 79 | // MarshalText implements the encoding.TextMarshaler interface. |
| 80 | // The encoding is the same as returned by String, |
| 81 | // But when len(ip) is zero, will return an empty slice. |
| 82 | func (i *IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#) MarshalTextscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#MarshalText().() ([]byte, error) { |
| 83 | if len(i.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#IP.) == 0 { |
| 84 | return []byte{}, nil |
| 85 | } |
| 86 | return []byte(i.Stringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#String().()), nil |
| 87 | } |
| 88 | |
| 89 | // UnmarshalText implements the encoding.TextUnmarshaler interface. |
| 90 | // The textual bytes are expected in a form accepted by Parse, |
| 91 | // But when len(b) is zero, will return an empty IP. |
| 92 | func (i *IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#) UnmarshalTextscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#UnmarshalText().(b []byte) error { |
| 93 | if len(b) == 0 { |
| 94 | *i = IPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/IP#{} |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | ip := ParseIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/ParseIP().(string(b)) |
| 99 | if ip == nil { |
| 100 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("invalid IP address %s", string(b)) |
| 101 | } |
| 102 | *i = *ip |
| 103 | return nil |
| 104 | } |