本文整理匯總了Golang中net.IP.To16方法的典型用法代碼示例。如果您正苦於以下問題:Golang IP.To16方法的具體用法?Golang IP.To16怎麽用?Golang IP.To16使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.IP
的用法示例。
在下文中一共展示了IP.To16方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Set
// 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
}
示例2: IpToRadixkey
func IpToRadixkey(prefix net.IP, prefixLen uint8) string {
b := prefix.To4()
if b == nil {
b = prefix.To16()
}
return toRadixkey(b, prefixLen)
}
示例3: Lookup
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]
}
示例4: bigForIP
// 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)
}
示例5: AddDefaultGw
// 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)
}
示例6: Hosts
// 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
}
示例7: routableIP
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
}
示例8: checksum
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
}
示例9: Lookup
// 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
}
示例10: isIPv6Addr
// 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
}
示例11: IPv6PseudoHeader
// 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
}
示例12: GetVersion
// 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
}
示例13: NewPrefix
// 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
}
示例14: ipToint64
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
}
示例15: canonicalizeIP
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)
}