forked from chuckha/dht
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
71 lines (60 loc) · 1.53 KB
/
util.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package dht
import (
"crypto/sha1"
"math/big"
"net"
)
// inclusive id ε (left, right]
func InclusiveBetween(left, id, right *big.Int) bool {
// if the right is bigger than the left then we know it doesn't cross zero
if right.Cmp(left) == 1 {
return left.Cmp(id) == -1 && id.Cmp(right) <= 0
}
return left.Cmp(id) == -1 || id.Cmp(right) <= 0
}
// inclusive id ε (left, right)
func ExclusiveBetween(left, id, right *big.Int) bool {
if right.Cmp(left) == 1 {
return left.Cmp(id) == -1 && id.Cmp(right) < 0
}
return left.Cmp(id) == -1 || id.Cmp(right) < 0
}
func Hash(in string) *big.Int {
hasher := sha1.New()
hasher.Write([]byte(in))
return new(big.Int).SetBytes(hasher.Sum(nil))
}
const keySize = sha1.Size * 8
var hashMod = new(big.Int).Exp(big.NewInt(2), big.NewInt(keySize), nil)
func FingerEntry(start string, fingerentry int) *big.Int {
id := Hash(start)
two := big.NewInt(2)
exponent := big.NewInt(int64(fingerentry) - 1)
two.Exp(two, exponent, nil)
id.Add(id, two)
return id.Mod(id, hashMod)
}
func GetAddress() string {
interfaces, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, interf := range interfaces {
flags := interf.Flags
// get only not loopback and up interfaces
if flags&(net.FlagLoopback|flags&net.FlagUp) == net.FlagUp {
addrs, err := interf.Addrs()
if err != nil {
panic(err)
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
if ip4 := ipnet.IP.To4(); len(ip4) == net.IPv4len {
return ip4.String()
}
}
}
}
}
return ""
}