
This adds basic plugins. "main" types: veth, bridge, macvlan "ipam" type: host-local The code has been ported over from github.com/coreos/rkt project and adapted to fit the CNI spec.
87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
// Copyright 2015 CoreOS, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package ip
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math/big"
|
|
"net"
|
|
)
|
|
|
|
// ParseCIDR takes a string like "10.2.3.1/24" and
|
|
// return IPNet with "10.2.3.1" and /24 mask
|
|
func ParseCIDR(s string) (*net.IPNet, error) {
|
|
ip, ipn, err := net.ParseCIDR(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ipn.IP = ip
|
|
return ipn, nil
|
|
}
|
|
|
|
// NextIP returns IP incremented by 1
|
|
func NextIP(ip net.IP) net.IP {
|
|
i := ipToInt(ip)
|
|
return intToIP(i.Add(i, big.NewInt(1)))
|
|
}
|
|
|
|
// PrevIP returns IP decremented by 1
|
|
func PrevIP(ip net.IP) net.IP {
|
|
i := ipToInt(ip)
|
|
return intToIP(i.Sub(i, big.NewInt(1)))
|
|
}
|
|
|
|
func ipToInt(ip net.IP) *big.Int {
|
|
if v := ip.To4(); v != nil {
|
|
return big.NewInt(0).SetBytes(v)
|
|
}
|
|
return big.NewInt(0).SetBytes(ip.To16())
|
|
}
|
|
|
|
func intToIP(i *big.Int) net.IP {
|
|
return net.IP(i.Bytes())
|
|
}
|
|
|
|
// Network masks off the host portion of the IP
|
|
func Network(ipn *net.IPNet) *net.IPNet {
|
|
return &net.IPNet{
|
|
IP: ipn.IP.Mask(ipn.Mask),
|
|
Mask: ipn.Mask,
|
|
}
|
|
}
|
|
|
|
// like net.IPNet but adds JSON marshalling and unmarshalling
|
|
type IPNet net.IPNet
|
|
|
|
func (n IPNet) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal((*net.IPNet)(&n).String())
|
|
}
|
|
|
|
func (n *IPNet) UnmarshalJSON(data []byte) error {
|
|
var s string
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
return err
|
|
}
|
|
|
|
tmp, err := ParseCIDR(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*n = IPNet(*tmp)
|
|
return nil
|
|
}
|