liuxiaolong
2022-06-28 37714b1093c04061e636e5b1d27179652e671c0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sockaddr
 
import (
    "fmt"
    "net"
)
 
// IfAddr is a union of a SockAddr and a net.Interface.
type IfAddr struct {
    SockAddr
    net.Interface
}
 
// Attr returns the named attribute as a string
func (ifAddr IfAddr) Attr(attrName AttrName) (string, error) {
    val := IfAddrAttr(ifAddr, attrName)
    if val != "" {
        return val, nil
    }
 
    return Attr(ifAddr.SockAddr, attrName)
}
 
// Attr returns the named attribute as a string
func Attr(sa SockAddr, attrName AttrName) (string, error) {
    switch sockType := sa.Type(); {
    case sockType&TypeIP != 0:
        ip := *ToIPAddr(sa)
        attrVal := IPAddrAttr(ip, attrName)
        if attrVal != "" {
            return attrVal, nil
        }
 
        if sockType == TypeIPv4 {
            ipv4 := *ToIPv4Addr(sa)
            attrVal := IPv4AddrAttr(ipv4, attrName)
            if attrVal != "" {
                return attrVal, nil
            }
        } else if sockType == TypeIPv6 {
            ipv6 := *ToIPv6Addr(sa)
            attrVal := IPv6AddrAttr(ipv6, attrName)
            if attrVal != "" {
                return attrVal, nil
            }
        }
 
    case sockType == TypeUnix:
        us := *ToUnixSock(sa)
        attrVal := UnixSockAttr(us, attrName)
        if attrVal != "" {
            return attrVal, nil
        }
    }
 
    // Non type-specific attributes
    switch attrName {
    case "string":
        return sa.String(), nil
    case "type":
        return sa.Type().String(), nil
    }
 
    return "", fmt.Errorf("unsupported attribute name %q", attrName)
}