This repository has been archived by the owner on Mar 29, 2019. It is now read-only.
/
contact_req.go
259 lines (227 loc) · 7.53 KB
/
contact_req.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// contact_req.go - Ricochet contact request channel implementation.
//
// To the extent possible under law, Yawning Angel waived all copyright
// and related or neighboring rights to ricochet (library), using the Creative
// Commons "CC0" public domain dedication. See LICENSE or
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
package ricochet
import (
"errors"
"fmt"
"io"
"github.com/golang/protobuf/proto"
"github.com/yawning/ricochet/packet"
)
const (
contactReqChannelType = "im.ricochet.contact.request"
// ContactReqMessageMaxCharacters is the max characters in an optional
// contact request message.
ContactReqMessageMaxCharacters = 2000
// ContactReqNicknameMaxCharacters is the max characters in an optional
// contact request nickname.
ContactReqNicknameMaxCharacters = 30
)
// ContactRequest is a contact request event.
type ContactRequest struct {
Hostname string
MyNickname string
Message string
}
var errContactRequestAgain = errors.New("unspecified contact request error")
func (req *ContactRequest) String() string {
return fmt.Sprintf("'%v':'%v'", req.MyNickname, req.Message)
}
type contactReqChan struct {
conn *ricochetConn
chanID uint16
state chanState
reqData *ContactRequest
}
func (ch *contactReqChan) onOpenChannel() error {
log := ch.conn.endpoint.log
ch.state = chanStateOpen
// Stop the timer since the peer took sufficient action.
ch.conn.authTimer.Stop()
log.Printf("server: ContactRequest from: '%v' (%s)", ch.conn.hostname, ch.reqData)
// Check to see if we should mark the contact request as accepted,
// rejected or pending.
accept, err := ch.conn.endpoint.onContactRequest(ch.conn, ch.reqData)
if err != nil {
return err
}
resp := &packet.ContactRequestResponse{}
if accept {
resp.Status = packet.ContactRequestResponse_Accepted.Enum()
} else {
resp.Status = packet.ContactRequestResponse_Pending.Enum()
}
chanResult := &packet.ChannelResult{
ChannelIdentifier: proto.Int32((int32)(ch.chanID)),
Opened: proto.Bool(true),
}
if err := proto.SetExtension(chanResult, packet.E_Response, resp); err != nil {
return err
}
ctrlPkt := &packet.ControlPacket{ChannelResult: chanResult}
rawPkt, err := proto.Marshal(ctrlPkt)
if err != nil {
return err
}
err = ch.conn.sendPacket(controlChanID, rawPkt)
if !accept {
return err
}
// We immediately accepted the Contact Request.
ch.conn.endpoint.onConnectionEstablished(ch.conn)
return ch.conn.sendChanClose(ch.chanID)
}
func (ch *contactReqChan) onChannelResult(msg *packet.ChannelResult) error {
if ch.conn.isServer {
return fmt.Errorf("opened contact req channel to client")
}
if ch.state != chanStateOpening {
return fmt.Errorf("received spurious ContactRequest ChannelResult")
}
ch.state = chanStateOpen
// If this routine was called, the channel WAS opened, without incident.
// Extract the response, and take action accordingly.
ext, err := proto.GetExtension(msg, packet.E_Response)
if err != nil {
return err
}
resp := ext.(*packet.ContactRequestResponse)
return ch.onResponse(resp)
}
func (ch *contactReqChan) onPacket(rawPkt []byte) error {
if ch.conn.isServer {
return fmt.Errorf("received contact req packet from client")
}
if ch.state != chanStateOpen {
return fmt.Errorf("received contact req packet after completion")
}
var resp packet.ContactRequestResponse
if err := proto.Unmarshal(rawPkt, &resp); err != nil {
return err
}
return ch.onResponse(&resp)
}
func (ch *contactReqChan) onResponse(resp *packet.ContactRequestResponse) error {
log := ch.conn.endpoint.log
switch resp.GetStatus() {
case packet.ContactRequestResponse_Pending:
// Nothing to do for this case, further responses to come.
log.Printf("client: server '%s' acked pending contact request", ch.conn.hostname)
case packet.ContactRequestResponse_Accepted:
// Accepted.
log.Printf("client: server '%s' accepted contact request", ch.conn.hostname)
ch.conn.getControlChan().isKnownToPeer = true
ch.state = chanStateDone
ch.conn.endpoint.onConnectionEstablished(ch.conn)
// XXX: Send a channel close? This is likewise something the server
// ought to be doing, so don't bother for now. This code will not use
// the channel past this point, apart from processing the server's
// code.
case packet.ContactRequestResponse_Rejected:
log.Printf("client: server '%s' rejected contact request", ch.conn.hostname)
ch.conn.endpoint.onRemoteReject(ch.conn.hostname)
return fmt.Errorf("contact request rejected by peer")
case packet.ContactRequestResponse_Error:
return errContactRequestAgain
default:
return fmt.Errorf("unknown ContactRequest Status")
}
return nil
}
func (ch *contactReqChan) onClose() error {
ch.conn.Lock()
defer ch.conn.Unlock()
if _, ok := ch.conn.chanMap[ch.chanID]; !ok {
return fmt.Errorf("received duplicate contact req chan close")
}
delete(ch.conn.chanMap, ch.chanID)
// Explicitly do not clear controlChan's contactReqChan field, since there
// is only one ContactRequest channel ever per connection.
if ch.state != chanStateDone {
// Peer closed the channel before sending a response...
// XXX: Treat this as a reject?
return io.EOF
}
return nil
}
func (ch *contactReqChan) sendContactReqResponse(accepted bool) error {
if !ch.conn.isServer {
return fmt.Errorf("attempted to send contact request response from server")
}
if ch.state != chanStateOpen {
return fmt.Errorf("attempted to send contact request response when closed")
}
ch.state = chanStateDone
resp := &packet.ContactRequestResponse{}
if accepted {
resp.Status = packet.ContactRequestResponse_Accepted.Enum()
} else {
resp.Status = packet.ContactRequestResponse_Rejected.Enum()
}
rawPkt, err := proto.Marshal(resp)
if err != nil {
return err
}
if err := ch.conn.sendPacket(ch.chanID, rawPkt); err != nil {
return err
}
return ch.conn.sendChanClose(ch.chanID)
}
func newServerContactReqChan(conn *ricochetConn, msg *packet.OpenChannel) (*contactReqChan, error) {
ch := new(contactReqChan)
ch.conn = conn
ch.chanID = (uint16)(msg.GetChannelIdentifier())
ch.reqData = new(ContactRequest)
ext, err := proto.GetExtension(msg, packet.E_ContactRequest)
if err != nil {
return nil, err
}
if ext == nil {
return nil, fmt.Errorf("server: missing ContactRequest extension")
}
req := ext.(*packet.ContactRequest)
ch.reqData.Hostname = conn.hostname
ch.reqData.MyNickname = req.GetNickname()
if len(ch.reqData.MyNickname) > ContactReqNicknameMaxCharacters {
return nil, fmt.Errorf("server: ContactRequest nickname too long")
}
ch.reqData.Message = req.GetMessageText()
if len(ch.reqData.Message) > ContactReqMessageMaxCharacters {
return nil, fmt.Errorf("server: ContactRequest message too long")
}
return ch, nil
}
func newClientContactReqChan(conn *ricochetConn, reqData *ContactRequest) (err error) {
ch := new(contactReqChan)
ch.conn = conn
ch.chanID, err = conn.allocateNextChanID()
if err != nil {
return
}
req := &packet.ContactRequest{
Nickname: proto.String(reqData.MyNickname),
MessageText: proto.String(reqData.Message),
}
openChan := &packet.OpenChannel{
ChannelIdentifier: proto.Int32((int32)(ch.chanID)),
ChannelType: proto.String(contactReqChannelType),
}
if err = proto.SetExtension(openChan, packet.E_ContactRequest, req); err != nil {
return
}
ctrlPkt := &packet.ControlPacket{OpenChannel: openChan}
rawPkt, err := proto.Marshal(ctrlPkt)
if err != nil {
return err
}
err = ch.conn.sendPacket(controlChanID, rawPkt)
conn.Lock()
defer conn.Unlock()
conn.chanMap[ch.chanID] = ch
conn.getControlChan().contactReqChan = (int)(ch.chanID)
return
}