Skip to content

Commit 73a45f7

Browse files
committed
Make duplicate connection detection work properly.
It turns out using the LL addresses doesn't work because they're unique between devices. Now it uses the full node addresses instead.
1 parent b3302bc commit 73a45f7

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

server.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,21 @@ type Server struct {
2222
listeners map[string]*internal.SSHListener
2323
// This contains the addresses of the nodes that we are currently connecting
2424
// to, so we don't try to connect to them twice at the same time.
25-
currently_connecting map[string]bool
25+
currently_connecting map[types.NodeAddress]bool
2626
// Mutex to protect accesses to te currently_connecting map.
27-
connecting_mutex sync.RWMutex
27+
connecting_mutex sync.Mutex
2828
listen_address string
2929
}
3030

3131
func NewServer(key Key, m types.Money) *Server {
3232
n := internal.NewNode(key.k, m, time.Tick(30*time.Second), time.Tick(30*time.Second))
3333
return &Server{n, make(map[string]*internal.SSHListener),
34-
make(map[string]bool), sync.RWMutex{}, ""}
34+
make(map[types.NodeAddress]bool), sync.Mutex{}, ""}
3535
}
3636

3737
func (s *Server) Connect(addr string) error {
38-
// Check that we should be connecting.
39-
s.connecting_mutex.RLock()
40-
_, addr_present := s.currently_connecting[addr]
41-
s.connecting_mutex.RUnlock()
42-
if addr_present {
43-
return errors.New(fmt.Sprintf("Already connecting to %s.\n", addr))
44-
}
45-
s.connecting_mutex.Lock()
46-
s.currently_connecting[addr] = true
47-
s.connecting_mutex.Unlock()
48-
4938
c, err := net.Dial("tcp6", addr)
5039

51-
// Now that we've tried connecting, remove it as a pending connection.
52-
s.connecting_mutex.Lock()
53-
delete(s.currently_connecting, addr)
54-
s.connecting_mutex.Unlock()
55-
5640
if err != nil {
5741
return err
5842
}
@@ -133,7 +117,23 @@ func (s *Server) findNeighbors(dev net.Interface, ll_addr net.IP, port uint16) {
133117
log.Print("Warning: Ignoring connection from self.\n")
134118
continue
135119
}
136-
err := s.Connect(fmt.Sprintf("[%s%%%s]:%v", neighbor.LLAddrStr, dev.Name, neighbor.Port))
120+
121+
// Check that we should be connecting.
122+
s.connecting_mutex.Lock()
123+
_, addr_present := s.currently_connecting[neighbor.FullNodeAddr]
124+
if addr_present {
125+
log.Printf("Already connecting to %s.\n", neighbor.FullNodeAddr)
126+
continue
127+
}
128+
s.currently_connecting[neighbor.FullNodeAddr] = true
129+
130+
err := s.Connect(fmt.Sprintf("[%s%%%s]:%v", neighbor.LLAddrStr, dev.Name,
131+
neighbor.Port))
132+
133+
// Now that we've tried connecting, remove it as a pending connection.
134+
delete(s.currently_connecting, neighbor.FullNodeAddr)
135+
s.connecting_mutex.Unlock()
136+
137137
if err != nil {
138138
log.Printf("Error connecting: %v", err)
139139
continue

0 commit comments

Comments
 (0)