This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
listener.go
187 lines (153 loc) · 4.11 KB
/
listener.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// listener.go - Katzenpost server listener.
// Copyright (C) 2017 Yawning Angel.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package incoming implements the incoming connection support.
package incoming
import (
"bytes"
"container/list"
"fmt"
"net"
"sync"
"sync/atomic"
"github.com/katzenpost/core/worker"
"github.com/katzenpost/server/internal/constants"
"github.com/katzenpost/server/internal/glue"
"gopkg.in/op/go-logging.v1"
)
type listener struct {
sync.Mutex
worker.Worker
glue glue.Glue
log *logging.Logger
l net.Listener
conns *list.List
incomingCh chan<- interface{}
closeAllCh chan interface{}
closeAllWg sync.WaitGroup
sendRatePerMinute uint64
sendBurst uint64
}
func (l *listener) Halt() {
// Close the listener, wait for worker() to return.
l.l.Close()
l.Worker.Halt()
// Close all connections belonging to the listener.
//
// Note: Worst case this can take up to the handshake timeout to
// actually complete, since the channel isn't checked mid-handshake.
close(l.closeAllCh)
l.closeAllWg.Wait()
}
func (l *listener) OnNewSendRatePerMinute(sendRatePerMinute uint64) {
atomic.StoreUint64(&l.sendRatePerMinute, sendRatePerMinute)
}
func (l *listener) OnNewSendBurst(sendBurst uint64) {
atomic.StoreUint64(&l.sendBurst, sendBurst)
}
func (l *listener) worker() {
addr := l.l.Addr()
l.log.Noticef("Listening on: %v", addr)
defer func() {
l.log.Noticef("Stopping listening on: %v", addr)
l.l.Close() // Usually redundant, but harmless.
}()
for {
conn, err := l.l.Accept()
if err != nil {
if e, ok := err.(net.Error); ok && !e.Temporary() {
l.log.Errorf("Critical accept failure: %v", err)
return
}
continue
}
tcpConn := conn.(*net.TCPConn)
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(constants.KeepAliveInterval)
l.log.Debugf("Accepted new connection: %v", conn.RemoteAddr())
l.onNewConn(conn)
}
// NOTREACHED
}
func (l *listener) onNewConn(conn net.Conn) {
c := newIncomingConn(l, conn)
l.closeAllWg.Add(1)
l.Lock()
defer func() {
l.Unlock()
go c.worker()
}()
c.e = l.conns.PushFront(c)
}
func (l *listener) onInitializedConn(c *incomingConn) {
l.Lock()
defer l.Unlock()
c.isInitialized = true
}
func (l *listener) onClosedConn(c *incomingConn) {
l.Lock()
defer func() {
l.Unlock()
l.closeAllWg.Done()
}()
l.conns.Remove(c.e)
}
func (l *listener) CloseOldConns(ptr interface{}) error {
c := ptr.(*incomingConn)
l.Lock()
defer l.Unlock()
a, err := c.w.PeerCredentials()
if err != nil {
l.log.Errorf("Session fail: %s", err)
return err
}
for e := l.conns.Front(); e != nil; e = e.Next() {
cc := e.Value.(*incomingConn)
// Skip checking a conn against itself, or against pre-handshake conns.
if cc == c || cc.w == nil || !cc.isInitialized {
continue
}
// Compare both by AdditionalData and PublicKey.
b, err := cc.w.PeerCredentials()
if err != nil {
continue
}
if !bytes.Equal(a.AdditionalData, b.AdditionalData) {
continue
}
if !a.PublicKey.Equal(b.PublicKey) {
continue
}
cc.Close()
}
return nil
}
// New creates a new listener.
func New(glue glue.Glue, incomingCh chan<- interface{}, id int, addr string) (glue.Listener, error) {
var err error
l := &listener{
glue: glue,
log: glue.LogBackend().GetLogger(fmt.Sprintf("listener:%d", id)),
conns: list.New(),
incomingCh: incomingCh,
closeAllCh: make(chan interface{}),
}
l.l, err = net.Listen("tcp", addr)
if err != nil {
return nil, err
}
l.Go(l.worker)
return l, nil
}