forked from weaveworks/weave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer_name_mac.go
57 lines (47 loc) · 1.5 KB
/
peer_name_mac.go
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
// The !peer_name_alternative effectively makes this the default,
// i.e. to choose an alternative, run "go build -tags
// 'peer_name_alternative peer_name_hash'"
// +build peer_name_mac !peer_name_alternative
// Let peer names be MACs...
//
// MACs need to be unique across our network, or bad things will
// happen anyway. So they make pretty good candidates for peer
// names. And doing so is pretty efficient both computationally and
// network overhead wise.
//
// Note that we do not mandate *what* MAC should be used as the peer
// name. In particular it doesn't actually have to be the MAC of, say,
// the network interface the peer is sniffing on.
package weave
import (
"net"
)
// PeerName is used as a map key. Since net.HardwareAddr isn't
// suitable for that - it's a slice, and slices can't be map keys - we
// convert that to/from uint64.
type PeerName uint64
const (
PeerNameFlavour = "mac"
NameSize = 6
UnknownPeerName = PeerName(0)
)
func PeerNameFromUserInput(userInput string) (PeerName, error) {
res, err := PeerNameFromString(userInput)
return res, err
}
func PeerNameFromString(nameStr string) (PeerName, error) {
mac, err := net.ParseMAC(nameStr)
if err != nil {
return UnknownPeerName, err
}
return PeerName(macint(mac)), nil
}
func PeerNameFromBin(nameByte []byte) PeerName {
return PeerName(macint(net.HardwareAddr(nameByte)))
}
func (name PeerName) Bin() []byte {
return intmac(uint64(name))
}
func (name PeerName) String() string {
return intmac(uint64(name)).String()
}