func ParseCIDR(cidr string) (n *net.IPNet, e error) {
	var i net.IP
	if i, n, e = net.ParseCIDR(cidr); e == nil {
		n.IP = i
	}
	return
}
ParseCIDR returns the *net.IPNet represented by the passed CIDR notation
package types
import (
	"bytes"
	"fmt"
	"net"
	"strconv"
	"strings"
)
type UUID string
type TransportPort struct {
	Proto Protocol
	Port  uint16
}
func (t *TransportPort) Equal(o *TransportPort) bool {
	if t == o {
		return true
	}
	if o == nil {
		return false
	}
	if t.Proto != o.Proto || t.Port != o.Port {
		return false
	}
	return true
}
func (t *TransportPort) GetCopy() TransportPort {
	return TransportPort{Proto: t.Proto, Port: t.Port}
}
func (t *TransportPort) String() string {
	return fmt.Sprintf("%s/%d", t.Proto.String(), t.Port)
}
func (t *TransportPort) FromString(s string) error {
	ps := strings.Split(s, "/")
	if len(ps) == 2 {
		t.Proto = ParseProtocol(ps[0])
		if p, err := strconv.ParseUint(ps[1], 10, 16); err == nil {
			t.Port = uint16(p)
			return nil
		}
	}
	return BadRequestErrorf("invalid format for transport port: %s", s)
}
type PortBinding struct {
	Proto       Protocol
	IP          net.IP
	Port        uint16
	HostIP      net.IP
	HostPort    uint16
	HostPortEnd uint16
}
func (p PortBinding) HostAddr() (net.Addr, error) {
	switch p.Proto {
	case UDP:
		return &net.UDPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
	case TCP:
		return &net.TCPAddr{IP: p.HostIP, Port: int(p.HostPort)}, nil
	default:
		return nil, ErrInvalidProtocolBinding(p.Proto.String())
	}
}
func (p PortBinding) ContainerAddr() (net.Addr, error) {
	switch p.Proto {
	case UDP:
		return &net.UDPAddr{IP: p.IP, Port: int(p.Port)}, nil
	case TCP:
		return &net.TCPAddr{IP: p.IP, Port: int(p.Port)}, nil
	default:
		return nil, ErrInvalidProtocolBinding(p.Proto.String())
	}
}
func (p *PortBinding) GetCopy() PortBinding {
	return PortBinding{
		Proto:       p.Proto,
		IP:          GetIPCopy(p.IP),
		Port:        p.Port,
		HostIP:      GetIPCopy(p.HostIP),
		HostPort:    p.HostPort,
		HostPortEnd: p.HostPortEnd,
	}
}
func (p *PortBinding) String() string {
	ret := fmt.Sprintf("%s/", p.Proto)
	if p.IP != nil {
		ret = fmt.Sprintf("%s%s", ret, p.IP.String())
	}
	ret = fmt.Sprintf("%s:%d/", ret, p.Port)
	if p.HostIP != nil {
		ret = fmt.Sprintf("%s%s", ret, p.HostIP.String())
	}
	ret = fmt.Sprintf("%s:%d", ret, p.HostPort)
	return ret
}
func (p *PortBinding) FromString(s string) error {
	ps := strings.Split(s, "/")
	if len(ps) != 3 {
		return BadRequestErrorf("invalid format for port binding: %s", s)
	}
	p.Proto = ParseProtocol(ps[0])
	var err error
	if p.IP, p.Port, err = parseIPPort(ps[1]); err != nil {
		return BadRequestErrorf("failed to parse Container IP/Port in port binding: %s", err.Error())
	}
	if p.HostIP, p.HostPort, err = parseIPPort(ps[2]); err != nil {
		return BadRequestErrorf("failed to parse Host IP/Port in port binding: %s", err.Error())
	}
	return nil
}
func parseIPPort(s string) (net.IP, uint16, error) {
	pp := strings.Split(s, ":")
	if len(pp) != 2 {
		return nil, 0, BadRequestErrorf("invalid format: %s", s)
	}
	var ip net.IP
	if pp[0] != "" {
		if ip = net.ParseIP(pp[0]); ip == nil {
			return nil, 0, BadRequestErrorf("invalid ip: %s", pp[0])
		}
	}
	port, err := strconv.ParseUint(pp[1], 10, 16)
	if err != nil {
		return nil, 0, BadRequestErrorf("invalid port: %s", pp[1])
	}
	return ip, uint16(port), nil
}
func (p *PortBinding) Equal(o *PortBinding) bool {
	if p == o {
		return true
	}
	if o == nil {
		return false
	}
	if p.Proto != o.Proto || p.Port != o.Port ||
		p.HostPort != o.HostPort || p.HostPortEnd != o.HostPortEnd {
		return false
	}
	if p.IP != nil {
		if !p.IP.Equal(o.IP) {
			return false
		}
	} else {
		if o.IP != nil {
			return false
		}
	}
	if p.HostIP != nil {
		if !p.HostIP.Equal(o.HostIP) {
			return false
		}
	} else {
		if o.HostIP != nil {
			return false
		}
	}
	return true
}
type ErrInvalidProtocolBinding string
func (ipb ErrInvalidProtocolBinding) Error() string {
	return fmt.Sprintf("invalid transport protocol: %s", string(ipb))
}
const (
	ICMP = 1
	TCP = 6
	UDP = 17
)
type Protocol uint8
func (p Protocol) String() string {
	switch p {
	case ICMP:
		return "icmp"
	case TCP:
		return "tcp"
	case UDP:
		return "udp"
	default:
		return fmt.Sprintf("%d", p)
	}
}
func ParseProtocol(s string) Protocol {
	switch strings.ToLower(s) {
	case "icmp":
		return ICMP
	case "udp":
		return UDP
	case "tcp":
		return TCP
	default:
		return 0
	}
}
func GetMacCopy(from net.HardwareAddr) net.HardwareAddr {
	if from == nil {
		return nil
	}
	to := make(net.HardwareAddr, len(from))
	copy(to, from)
	return to
}
func GetIPCopy(from net.IP) net.IP {
	if from == nil {
		return nil
	}
	to := make(net.IP, len(from))
	copy(to, from)
	return to
}
func GetIPNetCopy(from *net.IPNet) *net.IPNet {
	if from == nil {
		return nil
	}
	bm := make(net.IPMask, len(from.Mask))
	copy(bm, from.Mask)
	return &net.IPNet{IP: GetIPCopy(from.IP), Mask: bm}
}
func GetIPNetCanonical(nw *net.IPNet) *net.IPNet {
	if nw == nil {
		return nil
	}
	c := GetIPNetCopy(nw)
	c.IP = c.IP.Mask(nw.Mask)
	return c
}
func CompareIPNet(a, b *net.IPNet) bool {
	if a == b {
		return true
	}
	if a == nil || b == nil {
		return false
	}
	return a.IP.Equal(b.IP) && bytes.Equal(a.Mask, b.Mask)
}
func GetMinimalIP(ip net.IP) net.IP {
	if ip != nil && ip.To4() != nil {
		return ip.To4()
	}
	return ip
}
func GetMinimalIPNet(nw *net.IPNet) *net.IPNet {
	if nw == nil {
		return nil
	}
	if len(nw.IP) == 16 && nw.IP.To4() != nil {
		m := nw.Mask
		if len(m) == 16 {
			m = m[12:16]
		}
		return &net.IPNet{IP: nw.IP.To4(), Mask: m}
	}
	return nw
}
var v4inV6MaskPrefix = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
func compareIPMask(ip net.IP, mask net.IPMask) (is int, ms int, err error) {
	if len(ip) == net.IPv6len && ip.To4() != nil {
		is = 12
	}
	if len(ip[is:]) == net.IPv4len && len(mask) == net.IPv6len && bytes.Equal(mask[:12], v4inV6MaskPrefix) {
		ms = 12
	}
	if len(ip[is:]) != len(mask[ms:]) {
		err = fmt.Errorf("ip and mask are not compatible: (%#v, %#v)", ip, mask)
	}
	return
}
func GetHostPartIP(ip net.IP, mask net.IPMask) (net.IP, error) {
	is, ms, err := compareIPMask(ip, mask)
	if err != nil {
		return nil, fmt.Errorf("cannot compute host portion ip address because %s", err)
	}
	out := GetIPCopy(ip)
	for i := 0; i < len(mask[ms:]); i++ {
		out[is+i] &= ^mask[ms+i]
	}
	return out, nil
}
func GetBroadcastIP(ip net.IP, mask net.IPMask) (net.IP, error) {
	is, ms, err := compareIPMask(ip, mask)
	if err != nil {
		return nil, fmt.Errorf("cannot compute broadcast ip address because %s", err)
	}
	out := GetIPCopy(ip)
	for i := 0; i < len(mask[ms:]); i++ {
		out[is+i] |= ^mask[ms+i]
	}
	return out, nil
}