Exemplo n.º 1
0
// Set implements the Set method of ipaddr.Prefix interface.
func (p *IPv6) Set(ip net.IP, nbits int) error {
	if ipv6 := ip.To16(); ipv6 != nil && ipv6.To4() == nil && 0 <= nbits && nbits <= IPv6PrefixLen {
		p.set(ipToIPv6Int(ipv6), byte(nbits))
		return nil
	}
	return errInvalidArgument
}
Exemplo n.º 2
0
func IpToRadixkey(prefix net.IP, prefixLen uint8) string {
	b := prefix.To4()
	if b == nil {
		b = prefix.To16()
	}
	return toRadixkey(b, prefixLen)
}
Exemplo n.º 3
0
func (tab *Table) Lookup(
	proto protocols.Protocol,
	srcIP, dstIP net.IP,
	srcPort, dstPort uint16,
) *Route {
	target := Route{}
	target.Protocol = proto
	target.SetInboundSource(srcIP.To16(), srcPort)
	target.SetInboundDestination(dstIP.To16(), dstPort)

	sze := len(tab.routes)
	idx := sort.Search(sze, func(idx int) bool {
		return !lessInbound(tab.routes[idx], &target)
	})

	if idx >= sze {
		return nil
	}

	if lessInbound(&target, tab.routes[idx]) {
		return nil
	}

	return tab.routes[idx]
}
Exemplo n.º 4
0
// bigForIP creates a big.Int based on the provided net.IP
func bigForIP(ip net.IP) *big.Int {
	b := ip.To4()
	if b == nil {
		b = ip.To16()
	}
	return big.NewInt(0).SetBytes(b)
}
// Add a new default gateway. Identical to:
// ip route add default via $ip
func AddDefaultGw(ip net.IP) error {
	s, err := getNetlinkSocket()
	if err != nil {
		return err
	}
	defer s.Close()

	family := getIpFamily(ip)

	wb := newNetlinkRequest(syscall.RTM_NEWROUTE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)

	msg := newRtMsg(family)
	wb.AddData(msg)

	var ipData []byte
	if family == syscall.AF_INET {
		ipData = ip.To4()
	} else {
		ipData = ip.To16()
	}

	gateway := newRtAttr(syscall.RTA_GATEWAY, ipData)

	wb.AddData(gateway)

	if err := s.Send(wb); err != nil {
		return err
	}

	return s.HandleAck(wb.Seq)
}
Exemplo n.º 6
0
// Hosts implements the Hosts method of ipaddr.Prefix interface.
func (p *IPv6) Hosts(begin net.IP) []net.IP {
	if p.isDefaultRoute() {
		return nil
	}
	var cur ipv6Int
	if len(begin) != 0 {
		cur = ipToIPv6Int(begin.To16())
	} else {
		cur = p.addr
	}
	var hosts []net.IP
	if ok, _ := p.isHostAssignable(cur); ok && p.contains(cur) {
		hosts = append(hosts, cur.IP())
	}
	if IPv6PrefixLen-p.nbits < 17 { // don't bother runtime.makeslice by big number
		for p.contains(cur) {
			if _, eor := p.isHostAssignable(cur); eor {
				break
			}
			cur.incr()
			if ok, _ := p.isHostAssignable(cur); ok {
				hosts = append(hosts, cur.IP())
			}
		}
		return hosts
	}
	for h := range p.HostIter(begin) {
		hosts = append(hosts, h)
	}
	return hosts
}
Exemplo n.º 7
0
func routableIP(network string, ip net.IP) net.IP {
	if !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsGlobalUnicast() {
		return nil
	}
	switch network {
	case "ip4":
		if ip := ip.To4(); ip != nil {
			return ip
		}
	case "ip6":
		if ip.IsLoopback() { // addressing scope of the loopback address depends on each implementation
			return nil
		}
		if ip := ip.To16(); ip != nil && ip.To4() == nil {
			return ip
		}
	default:
		if ip := ip.To4(); ip != nil {
			return ip
		}
		if ip := ip.To16(); ip != nil {
			return ip
		}
	}
	return nil
}
Exemplo n.º 8
0
func checksum(advert *advertisement, srcIP, dstIP net.IP) (uint16, error) {
	buf := new(bytes.Buffer)
	if src, dst := srcIP.To4(), dstIP.To4(); src != nil && dst != nil {
		// IPv4
		hdr := &ipv4PseudoHeader{
			Protocol: 112,
			VRRPLen:  vrrpAdvertSize,
		}
		copy(hdr.Src[:], src)
		copy(hdr.Dst[:], dst)

		if err := binary.Write(buf, binary.BigEndian, hdr); err != nil {
			return 0, err
		}
	} else if src, dst := srcIP.To16(), dstIP.To16(); src != nil && dst != nil {
		// IPv6
		hdr := &ipv6PseudoHeader{
			VRRPLen:    vrrpAdvertSize,
			NextHeader: 112,
		}
		copy(hdr.Src[:], src)
		copy(hdr.Dst[:], dst)

		if err := binary.Write(buf, binary.BigEndian, hdr); err != nil {
			return 0, err
		}
	} else {
		return 0, fmt.Errorf("ha.checksum(%q, %q): Need two IPv4 or IPv6 addresses", srcIP, dstIP)
	}

	if err := binary.Write(buf, binary.BigEndian, advert); err != nil {
		return 0, err
	}
	return ipChecksum(buf.Bytes()), nil
}
Exemplo n.º 9
0
// Return the range the given IP is in. Returns nil if no range is found.
func (me *IPList) Lookup(ip net.IP) (r Range, ok bool) {
	if me == nil {
		return
	}
	// TODO: Perhaps all addresses should be converted to IPv6, if the future
	// of IP is to always be backwards compatible. But this will cost 4x the
	// memory for IPv4 addresses?
	v4 := ip.To4()
	if v4 != nil {
		r, ok = me.lookup(v4)
		if ok {
			return
		}
	}
	v6 := ip.To16()
	if v6 != nil {
		return me.lookup(v6)
	}
	if v4 == nil && v6 == nil {
		r = Range{
			Description: "bad IP",
		}
		ok = true
	}
	return
}
Exemplo n.º 10
0
// isIPv6Addr returns if an IP address is a valid IPv6 address.
func isIPv6Addr(ip net.IP) bool {
	if ip.To16() == nil {
		return false
	}

	return ip.To4() == nil
}
Exemplo n.º 11
0
Arquivo: ipv6.go Projeto: eswdd/bosun
// IPv6PseudoHeader returns an IPv6 pseudo header for checkusm
// calculation.
func IPv6PseudoHeader(src, dst net.IP) []byte {
	b := make([]byte, ipv6PseudoHeaderLen)
	copy(b, src.To16())
	copy(b[net.IPv6len:], dst.To16())
	b[len(b)-1] = byte(iana.ProtocolIPv6ICMP)
	return b
}
Exemplo n.º 12
0
// Returns the IP address version number (4 or 6);
// Returns 0 on error
func GetVersion(ip net.IP) int {
	if ip.To4() != nil {
		return 4
	} else if ip.To16() != nil {
		return 6
	}
	return 0
}
Exemplo n.º 13
0
// NewPrefix returns a new Prefix.
func NewPrefix(ip net.IP, nbits int) (Prefix, error) {
	if ipv4 := ip.To4(); ipv4 != nil && 0 <= nbits && nbits <= IPv4PrefixLen {
		return newIPv4(ipToIPv4Int(ipv4), byte(nbits)), nil
	} else if ipv6 := ip.To16(); ipv6 != nil && ipv6.To4() == nil && 0 <= nbits && nbits <= IPv6PrefixLen {
		return newIPv6(ipToIPv6Int(ipv6), byte(nbits)), nil
	}
	return nil, errInvalidArgument
}
Exemplo n.º 14
0
func ipToint64(ip net.IP) int64 {
	a := ip.To16()
	i := int64(a[12]) << uint(24)
	i += int64(a[13]) << uint(16)
	i += int64(a[14]) << uint(8)
	i += int64(a[15])
	return i
}
Exemplo n.º 15
0
func canonicalizeIP(ip net.IP) (net.IP, error) {
	if ip.To4() != nil {
		return ip.To4(), nil
	} else if ip.To16() != nil {
		return ip.To16(), nil
	}
	return nil, fmt.Errorf("IP %s not v4 nor v6", ip)
}
Exemplo n.º 16
0
func addr2AddressFamily(a net.IP) bgp.RouteFamily {
	if a.To4() != nil {
		return bgp.RF_IPv4_UC
	} else if a.To16() != nil {
		return bgp.RF_IPv6_UC
	}
	return bgp.RouteFamily(0)
}
Exemplo n.º 17
0
// Convert a net.IP address to an LWIP compatible one
func convert_IP_to_LWIP(netAddr net.IP) *C.struct_ip_addr {
	longIP := []byte(netAddr.To16())
	lwipAddr := new(C.struct_ip_addr)
	for idx := 0; idx < 4; idx++ {
		lwipAddr.addr[idx] = C.uint32_t(binary.LittleEndian.Uint32(longIP[4*idx : (4*idx)+4]))
	}
	return lwipAddr
}
Exemplo n.º 18
0
func (r *Registry) Lookup(address net.IP) (*Device, bool) {
	byteAddr := [16]byte{}
	copy(byteAddr[:], address.To16())

	dev, present := r.devices[byteAddr]

	return dev, present
}
Exemplo n.º 19
0
func (s *Service6Value) SetAddress(ip net.IP) error {
	if ip.To4() != nil {
		return fmt.Errorf("Not an IPv6 address")
	}

	copy(s.Address[:], ip.To16())
	return nil
}
Exemplo n.º 20
0
func NewRevNat6Value(ip net.IP, port uint16) *RevNat6Value {
	revNat := RevNat6Value{
		Port: port,
	}

	copy(revNat.Address[:], ip.To16())

	return &revNat
}
Exemplo n.º 21
0
// RouteGet gets a route to a specific destination from the host system.
// Equivalent to: 'ip route get'.
func RouteGet(destination net.IP) ([]Route, error) {
	req := nl.NewNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_REQUEST)
	family := nl.GetIPFamily(destination)
	var destinationData []byte
	var bitlen uint8
	if family == FAMILY_V4 {
		destinationData = destination.To4()
		bitlen = 32
	} else {
		destinationData = destination.To16()
		bitlen = 128
	}
	msg := &nl.RtMsg{}
	msg.Family = uint8(family)
	msg.Dst_len = bitlen
	req.AddData(msg)

	rtaDst := nl.NewRtAttr(syscall.RTA_DST, destinationData)
	req.AddData(rtaDst)

	msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
	if err != nil {
		return nil, err
	}

	native := nl.NativeEndian()
	res := make([]Route, 0)
	for _, m := range msgs {
		msg := nl.DeserializeRtMsg(m)
		attrs, err := nl.ParseRouteAttr(m[msg.Len():])
		if err != nil {
			return nil, err
		}

		route := Route{}
		for _, attr := range attrs {
			switch attr.Attr.Type {
			case syscall.RTA_GATEWAY:
				route.Gw = net.IP(attr.Value)
			case syscall.RTA_PREFSRC:
				route.Src = net.IP(attr.Value)
			case syscall.RTA_DST:
				route.Dst = &net.IPNet{
					IP:   attr.Value,
					Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
				}
			case syscall.RTA_OIF:
				routeIndex := int(native.Uint32(attr.Value[0:4]))
				route.LinkIndex = routeIndex
			}
		}
		res = append(res, route)
	}
	return res, nil

}
Exemplo n.º 22
0
// FromIP converts a net.IP type to a Multiaddr.
func FromIP(ip net.IP) (ma.Multiaddr, error) {
	switch {
	case ip.To4() != nil:
		return ma.NewMultiaddr("/ip4/" + ip.String())
	case ip.To16() != nil:
		return ma.NewMultiaddr("/ip6/" + ip.String())
	default:
		return nil, fmt.Errorf("incorrect network addr conversion")
	}
}
Exemplo n.º 23
0
// FromIP converts a net.IP type to a Multiaddr.
func FromIP(ip net.IP) (ma.Multiaddr, error) {
	switch {
	case ip.To4() != nil:
		return ma.NewMultiaddr("/ip4/" + ip.String())
	case ip.To16() != nil:
		return ma.NewMultiaddr("/ip6/" + ip.String())
	default:
		return nil, errIncorrectNetAddr
	}
}
Exemplo n.º 24
0
func NewService6Key(ip net.IP, port uint16, slave uint16) *Service6Key {
	key := Service6Key{
		Port:  port,
		Slave: slave,
	}

	copy(key.Address[:], ip.To16())

	return &key
}
Exemplo n.º 25
0
func (c *Cursor) set(pi int, ip net.IP) {
	c.pi = pi
	c.curr = ipToIPv6Int(ip.To16())
	c.start = ipToIPv6Int(c.ps[c.pi].IP.To16())
	if ip.To4() != nil {
		c.end = c.ps[c.pi].lastIPv4MappedIPv6Int()
	}
	if ip.To16() != nil && ip.To4() == nil {
		c.end = c.ps[c.pi].lastIPv6Int()
	}
}
Exemplo n.º 26
0
// Converts a 4 bytes IP into a 128 bit integer
func ipToBigInt(ip net.IP) *big.Int {
	x := big.NewInt(0)
	if ip4 := ip.To4(); ip4 != nil {
		return x.SetBytes(ip4)
	}
	if ip6 := ip.To16(); ip6 != nil {
		return x.SetBytes(ip6)
	}

	return nil
}
Exemplo n.º 27
0
func (x *XfrmAddress) FromIP(ip net.IP) {
	var empty = [16]byte{}
	if len(ip) < net.IPv4len {
		copy(x[4:16], empty[:])
	} else if GetIPFamily(ip) == FAMILY_V4 {
		copy(x[0:4], ip.To4()[0:4])
		copy(x[4:16], empty[:12])
	} else {
		copy(x[0:16], ip.To16()[0:16])
	}
}
Exemplo n.º 28
0
func NewService6Value(count uint16, target net.IP, port uint16, revNat uint16) *Service6Value {
	svc := Service6Value{
		Count:  count,
		Port:   port,
		RevNat: revNat,
	}

	copy(svc.Address[:], target.To16())

	return &svc
}
Exemplo n.º 29
0
// Converts a 4 bytes IP into a 128 bit integer
func ipToBigInt(ip net.IP) *big.Int {
	x := big.NewInt(0)
	if ip4 := ip.To4(); ip4 != nil {
		return x.SetBytes(ip4)
	}
	if ip6 := ip.To16(); ip6 != nil {
		return x.SetBytes(ip6)
	}

	log.Errorf("ipToBigInt: Wrong IP length! %s", ip)
	return nil
}
Exemplo n.º 30
0
// IPAF returns the syscall AF_* type for a given IP address
// returns AF_UNSPEC if unknown
func IPAF(ip net.IP) int {
	switch {
	case ip.To4() != nil:
		return AF_INET

	case ip.To16() != nil:
		return AF_INET6

	default:
		return AF_UNSPEC
	}
}