1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
6 // +build darwin dragonfly freebsd linux netbsd openbsd solaris windows
20 func marshalInetAddr(a net.Addr) []byte {
21 switch a := a.(type) {
23 return marshalSockaddr(a.IP, a.Port, a.Zone)
25 return marshalSockaddr(a.IP, a.Port, a.Zone)
27 return marshalSockaddr(a.IP, 0, a.Zone)
33 func marshalSockaddr(ip net.IP, port int, zone string) []byte {
34 if ip4 := ip.To4(); ip4 != nil {
35 b := make([]byte, sizeofSockaddrInet)
37 case "linux", "solaris", "windows":
38 NativeEndian.PutUint16(b[:2], uint16(sysAF_INET))
40 b[0] = sizeofSockaddrInet
43 binary.BigEndian.PutUint16(b[2:4], uint16(port))
47 if ip6 := ip.To16(); ip6 != nil && ip.To4() == nil {
48 b := make([]byte, sizeofSockaddrInet6)
50 case "linux", "solaris", "windows":
51 NativeEndian.PutUint16(b[:2], uint16(sysAF_INET6))
53 b[0] = sizeofSockaddrInet6
56 binary.BigEndian.PutUint16(b[2:4], uint16(port))
59 NativeEndian.PutUint32(b[24:28], uint32(zoneCache.index(zone)))
66 func parseInetAddr(b []byte, network string) (net.Addr, error) {
68 return nil, errors.New("invalid address")
72 case "linux", "solaris", "windows":
73 af = int(NativeEndian.Uint16(b[:2]))
80 if len(b) < sizeofSockaddrInet {
81 return nil, errors.New("short address")
83 ip = make(net.IP, net.IPv4len)
86 if af == sysAF_INET6 {
87 if len(b) < sizeofSockaddrInet6 {
88 return nil, errors.New("short address")
90 ip = make(net.IP, net.IPv6len)
92 if id := int(NativeEndian.Uint32(b[24:28])); id > 0 {
93 zone = zoneCache.name(id)
97 case "tcp", "tcp4", "tcp6":
98 return &net.TCPAddr{IP: ip, Port: int(binary.BigEndian.Uint16(b[2:4])), Zone: zone}, nil
99 case "udp", "udp4", "udp6":
100 return &net.UDPAddr{IP: ip, Port: int(binary.BigEndian.Uint16(b[2:4])), Zone: zone}, nil
102 return &net.IPAddr{IP: ip, Zone: zone}, nil
106 // An ipv6ZoneCache represents a cache holding partial network
107 // interface information. It is used for reducing the cost of IPv6
108 // addressing scope zone resolution.
110 // Multiple names sharing the index are managed by first-come
111 // first-served basis for consistency.
112 type ipv6ZoneCache struct {
113 sync.RWMutex // guard the following
114 lastFetched time.Time // last time routing information was fetched
115 toIndex map[string]int // interface name to its index
116 toName map[int]string // interface index to its name
119 var zoneCache = ipv6ZoneCache{
120 toIndex: make(map[string]int),
121 toName: make(map[int]string),
124 func (zc *ipv6ZoneCache) update(ift []net.Interface) {
128 if zc.lastFetched.After(now.Add(-60 * time.Second)) {
134 if ift, err = net.Interfaces(); err != nil {
138 zc.toIndex = make(map[string]int, len(ift))
139 zc.toName = make(map[int]string, len(ift))
140 for _, ifi := range ift {
141 zc.toIndex[ifi.Name] = ifi.Index
142 if _, ok := zc.toName[ifi.Index]; !ok {
143 zc.toName[ifi.Index] = ifi.Name
148 func (zc *ipv6ZoneCache) name(zone int) string {
149 zoneCache.update(nil)
151 defer zoneCache.RUnlock()
152 name, ok := zoneCache.toName[zone]
154 name = strconv.Itoa(zone)
159 func (zc *ipv6ZoneCache) index(zone string) int {
160 zoneCache.update(nil)
162 defer zoneCache.RUnlock()
163 index, ok := zoneCache.toIndex[zone]
165 index, _ = strconv.Atoi(zone)