-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiphelper.go
More file actions
167 lines (146 loc) · 3.59 KB
/
iphelper.go
File metadata and controls
167 lines (146 loc) · 3.59 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"errors"
"net"
"os"
"strings"
"sync"
)
type AddrList struct {
List []*net.TCPAddr
Mutex sync.RWMutex
}
func (al *AddrList) Add(addr ...*net.TCPAddr) {
defer al.Mutex.Unlock()
al.Mutex.Lock()
al.List = append(al.List, addr...)
}
func (al *AddrList) Contains(addr *net.TCPAddr) bool {
addrStr := addr.IP.String()
defer al.Mutex.RUnlock()
al.Mutex.RLock()
for _, k := range al.List {
if addrStr == k.IP.String() {
return true
}
}
return false
}
//Remove removes an address from the list based solely on its IP (not source port)
func (al *AddrList) Remove(addr *net.TCPAddr) bool {
defer al.Mutex.Unlock()
addrStr := addr.IP.String()
al.Mutex.Lock()
llen := len(al.List)
for i, k := range al.List {
if k.IP.String() != addrStr {
continue
}
al.List[i] = al.List[llen-1]
al.List = al.List[:llen-1]
return true
}
return false
}
//RemoveStrict removes an address from the list based on its IP AND source port
//TODO: because TCPAddr's are shared (as pointers) between lists, updating the port
//for one likely updates the port on any other list. This means that the p2p program
//will not gracefully hadle multiple peers connecting from behind the same router disconnecting, etc
func (al *AddrList) RemoveStrict(addr *net.TCPAddr) bool {
defer al.Mutex.Unlock()
addrStr := addr.IP.String()
al.Mutex.Lock()
llen := len(al.List)
for i, k := range al.List {
if k.IP.String() != addrStr {
continue
}
if k.Port != addr.Port {
continue
}
al.List[i] = al.List[llen-1]
al.List = al.List[:llen-1]
return true
}
return false
}
func (al *AddrList) Len() int {
defer al.Mutex.RUnlock()
al.Mutex.RLock()
return len(al.List)
}
func (al *AddrList) Do(doFunc func(lst []*net.TCPAddr)) {
defer al.Mutex.RUnlock()
al.Mutex.RLock()
doFunc(al.List)
}
func (al *AddrList) UpdatePort(addr *net.TCPAddr, newPort int) bool {
defer al.Mutex.Unlock()
al.Mutex.Lock()
ipStr := addr.IP.String()
for _, k := range al.List {
if k.IP.String() != ipStr {
continue
}
k.Port = newPort
return true
}
return false
}
//Difference takes an input AddrList and returns the difference, that is
//it only returns items that are in the original list and not the other list
//the result slice is passed by reference because this function is called in a
//fast running infinite loop, so that if a new slice was allocated each time
//it could significantly impact the memory usage of the program
func (al *AddrList) Difference(other *AddrList, result *[]*net.TCPAddr) {
defer func() {
al.Mutex.RUnlock()
other.Mutex.RUnlock()
}()
al.Mutex.RLock()
other.Mutex.RLock()
otherIPs := make(map[string]bool)
for _, k := range other.List {
otherIPs[k.IP.String()] = true
}
for _, k := range al.List {
ipstr := k.IP.String()
if _, ok := otherIPs[ipstr]; ok == false {
*result = append(*result, k)
}
}
}
func GetLocalIP() (string, error) {
/*
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _,iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil { continue; }
if iface.Flags & net.FlagLoopback != 0 { continue; }
if iface.Flags & net.FlagUp == 0 { continue; }
for _,addr := range addrs {
log.Printf("ADDRESS: %s\n", addr.String())
}
}
return "???",nil
*/
name, err := os.Hostname()
if err != nil {
return "", err
}
addrs, err := net.LookupHost(name)
if err != nil {
return "", err
}
for _, a := range addrs {
addr := strings.Split(a, "/")[0]
octects := strings.Split(addr, ".")
if len(octects) == 4 {
return addr, nil
}
}
return "", errors.New("no ipv4 interface available")
}