aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/message/msg_transport.go
blob: d0e927b207e7abc3954e04d5c23d63b4f3294afc (plain) (blame)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix  >Y<
//
// gnunet-go 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.
//
// gnunet-go 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/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package message

import (
	"fmt"
	"time"

	"gnunet/crypto"
	"gnunet/enums"
	"gnunet/util"

	"github.com/bfix/gospel/crypto/ed25519"
	"github.com/bfix/gospel/data"
)

//----------------------------------------------------------------------
// TRANSPORT_TCP_WELCOME
//----------------------------------------------------------------------

// TransportTCPWelcomeMsg is a welcome message for new TCP connections.
type TransportTCPWelcomeMsg struct {
	MsgSize uint16       `order:"big"` // total size of message
	MsgType uint16       `order:"big"` // TRANSPORT_TCP_WELCOME (61)
	PeerID  *util.PeerID // Peer identity (EdDSA public key)
}

// NewTransportTCPWelcomeMsg creates a new message for a given peer.
func NewTransportTCPWelcomeMsg(peerid *util.PeerID) *TransportTCPWelcomeMsg {
	if peerid == nil {
		peerid = util.NewPeerID(nil)
	}
	return &TransportTCPWelcomeMsg{
		MsgSize: 36,
		MsgType: TRANSPORT_TCP_WELCOME,
		PeerID:  peerid,
	}
}

// String returns a human-readable representation of the message.
func (m *TransportTCPWelcomeMsg) String() string {
	return fmt.Sprintf("TransportTcpWelcomeMsg{peer=%s}", m.PeerID)
}

// Header returns the message header in a separate instance.
func (m *TransportTCPWelcomeMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// TRANSPORT_PING
//
// Message used to ask a peer to validate receipt (to check an address
// from a HELLO).  Followed by the address we are trying to validate,
// or an empty address if we are just sending a PING to confirm that a
// connection which the receiver (of the PING) initiated is still valid.
//----------------------------------------------------------------------

// TransportPingMsg is a PING request message
type TransportPingMsg struct {
	MsgSize   uint16       `order:"big"` // total size of message
	MsgType   uint16       `order:"big"` // TRANSPORT_PING (372)
	Challenge uint32       // Challenge code (to ensure fresh reply)
	Target    *util.PeerID // EdDSA public key (long-term) of target peer
	Address   []byte       `size:"*"` // encoded address
}

// NewTransportPingMsg creates a new message for given peer with an address to
// be validated.
func NewTransportPingMsg(target *util.PeerID, a *util.Address) *TransportPingMsg {
	if target == nil {
		target = util.NewPeerID(nil)
	}
	m := &TransportPingMsg{
		MsgSize:   uint16(40),
		MsgType:   TRANSPORT_PING,
		Challenge: util.RndUInt32(),
		Target:    target,
		Address:   nil,
	}
	if a != nil {
		if addrData, err := data.Marshal(a); err == nil {
			m.Address = addrData
			m.MsgSize += uint16(len(addrData))
		}
	}
	return m
}

// String returns a human-readable representation of the message.
func (m *TransportPingMsg) String() string {
	a := new(util.Address)
	data.Unmarshal(a, m.Address)
	return fmt.Sprintf("TransportPingMsg{target=%s,addr=%s,challenge=%d}",
		m.Target, a, m.Challenge)
}

// Header returns the message header in a separate instance.
func (m *TransportPingMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// TRANSPORT_PONG
//
// Message used to validate a HELLO.  The challenge is included in the
// confirmation to make matching of replies to requests possible.  The
// signature signs our public key, an expiration time and our address.<p>
//
// This message is followed by our transport address that the PING tried
// to confirm (if we liked it).  The address can be empty (zero bytes)
// if the PING had not address either (and we received the request via
// a connection that we initiated).
//----------------------------------------------------------------------

// SignedAddress is the signed block of data representing a node address
type SignedAddress struct {
	Purpose  *crypto.SignaturePurpose // SIG_TRANSPORT_PONG_OWN
	ExpireOn util.AbsoluteTime        // usec epoch
	AddrSize uint32                   `order:"big"`     // size of address
	Address  []byte                   `size:"AddrSize"` // address
}

// NewSignedAddress creates a new (signable) data block from an address.
func NewSignedAddress(a *util.Address) *SignedAddress {
	// serialize address
	addrData, _ := data.Marshal(a)
	alen := len(addrData)
	addr := &SignedAddress{
		Purpose: &crypto.SignaturePurpose{
			Size:    uint32(alen + 20),
			Purpose: enums.SIG_TRANSPORT_PONG_OWN,
		},
		ExpireOn: util.AbsoluteTimeNow().Add(12 * time.Hour),
		AddrSize: uint32(alen),
		Address:  make([]byte, alen),
	}
	copy(addr.Address, addrData)
	return addr
}

// TransportPongMsg is a reponse message for a PING request
type TransportPongMsg struct {
	MsgSize     uint16         `order:"big"` // total size of message
	MsgType     uint16         `order:"big"` // TRANSPORT_PING (372)
	Challenge   uint32         // Challenge code (to ensure fresh reply)
	Signature   []byte         `size:"64"` // Signature of address
	SignedBlock *SignedAddress // signed block of data
}

// NewTransportPongMsg creates a reponse message with an address the replying
// peer wants to be reached.
func NewTransportPongMsg(challenge uint32, a *util.Address) *TransportPongMsg {
	m := &TransportPongMsg{
		MsgSize:     72,
		MsgType:     TRANSPORT_PONG,
		Challenge:   challenge,
		Signature:   make([]byte, 64),
		SignedBlock: new(SignedAddress),
	}
	if a != nil {
		sa := NewSignedAddress(a)
		m.MsgSize += uint16(sa.Purpose.Size)
		m.SignedBlock = sa
	}
	return m
}

// String returns a human-readable representation of the message.
func (m *TransportPongMsg) String() string {
	a := new(util.Address)
	if err := data.Unmarshal(a, m.SignedBlock.Address); err == nil {
		return fmt.Sprintf("TransportPongMsg{addr=%s,challenge=%d}",
			a, m.Challenge)
	}
	return fmt.Sprintf("TransportPongMsg{addr=<unkown>,%d}", m.Challenge)
}

// Header returns the message header in a separate instance.
func (m *TransportPongMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

// Sign the address block of a pong message.
func (m *TransportPongMsg) Sign(prv *ed25519.PrivateKey) error {
	data, err := data.Marshal(m.SignedBlock)
	if err != nil {
		return err
	}
	sig, err := prv.EdSign(data)
	if err != nil {
		return err
	}
	copy(m.Signature, sig.Bytes())
	return nil
}

// Verify the address block of a pong message
func (m *TransportPongMsg) Verify(pub *ed25519.PublicKey) (bool, error) {
	data, err := data.Marshal(m.SignedBlock)
	if err != nil {
		return false, err
	}
	sig, err := ed25519.NewEdSignatureFromBytes(m.Signature)
	if err != nil {
		return false, err
	}
	return pub.EdVerify(data, sig)
}

//----------------------------------------------------------------------
// TRANSPORT_SESSION_ACK
//----------------------------------------------------------------------

// SessionAckMsg is a message to acknowlege a session request
type SessionAckMsg struct {
	MsgSize uint16 `order:"big"` // total size of message
	MsgType uint16 `order:"big"` // TRANSPORT_SESSION_ACK (377)
}

// NewSessionAckMsg creates an new message (no body required).
func NewSessionAckMsg() *SessionAckMsg {
	return &SessionAckMsg{
		MsgSize: 16,
		MsgType: TRANSPORT_SESSION_ACK,
	}
}

// String returns a human-readable representation of the message.
func (m *SessionAckMsg) String() string {
	return "SessionAck{}"
}

// Header returns the message header in a separate instance.
func (m *SessionAckMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// TRANSPORT_SESSION_SYN
//----------------------------------------------------------------------

// SessionSynMsg is a synchronization request message for sessions
type SessionSynMsg struct {
	MsgSize   uint16            `order:"big"` // total size of message
	MsgType   uint16            `order:"big"` // TRANSPORT_SESSION_SYN (375)
	Reserved  uint32            `order:"big"` // reserved (=0)
	Timestamp util.AbsoluteTime // usec epoch
}

// NewSessionSynMsg creates a SYN request for the a session
func NewSessionSynMsg() *SessionSynMsg {
	return &SessionSynMsg{
		MsgSize:   16,
		MsgType:   TRANSPORT_SESSION_SYN,
		Reserved:  0,
		Timestamp: util.AbsoluteTimeNow(),
	}
}

// String returns a human-readable representation of the message.
func (m *SessionSynMsg) String() string {
	return fmt.Sprintf("SessionSyn{timestamp=%s}", m.Timestamp)
}

// Header returns the message header in a separate instance.
func (m *SessionSynMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// TRANSPORT_SESSION_SYN_ACK
//----------------------------------------------------------------------

// SessionSynAckMsg responds to a SYN request message
type SessionSynAckMsg struct {
	MsgSize   uint16            `order:"big"` // total size of message
	MsgType   uint16            `order:"big"` // TRANSPORT_SESSION_SYN_ACK (376)
	Reserved  uint32            `order:"big"` // reserved (=0)
	Timestamp util.AbsoluteTime // usec epoch
}

// NewSessionSynAckMsg is an ACK for a SYN request
func NewSessionSynAckMsg() *SessionSynAckMsg {
	return &SessionSynAckMsg{
		MsgSize:   16,
		MsgType:   TRANSPORT_SESSION_SYN_ACK,
		Reserved:  0,
		Timestamp: util.AbsoluteTimeNow(),
	}
}

// String returns a human-readable representation of the message.
func (m *SessionSynAckMsg) String() string {
	return fmt.Sprintf("SessionSynAck{timestamp=%s}", m.Timestamp)
}

// Header returns the message header in a separate instance.
func (m *SessionSynAckMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// TRANSPORT_SESSION_QUOTA
//----------------------------------------------------------------------

// SessionQuotaMsg is a message to announce quotas for a session
type SessionQuotaMsg struct {
	MsgSize uint16 `order:"big"` // total size of message
	MsgType uint16 `order:"big"` // TRANSPORT_SESSION_QUOTA (379)
	Quota   uint32 `order:"big"` // Quota in bytes per second
}

// NewSessionQuotaMsg announces a session quota to the other end of the session.
func NewSessionQuotaMsg(quota uint32) *SessionQuotaMsg {
	m := new(SessionQuotaMsg)
	if quota > 0 {
		m.MsgSize = 8
		m.MsgType = TRANSPORT_SESSION_QUOTA
		m.Quota = quota
	}
	return m
}

// String returns a human-readable representation of the message.
func (m *SessionQuotaMsg) String() string {
	return fmt.Sprintf("SessionQuotaMsg{%sB/s}", util.Scale1024(uint64(m.Quota)))
}

// Header returns the message header in a separate instance.
func (m *SessionQuotaMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// TRANSPORT_SESSION_KEEPALIVE
//----------------------------------------------------------------------

// SessionKeepAliveMsg is a message send by peers to keep a session alive.
type SessionKeepAliveMsg struct {
	MsgSize uint16 `order:"big"` // total size of message
	MsgType uint16 `order:"big"` // TRANSPORT_SESSION_KEEPALIVE (381)
	Nonce   uint32
}

// NewSessionKeepAliveMsg creates a new request to keep a session.
func NewSessionKeepAliveMsg() *SessionKeepAliveMsg {
	m := &SessionKeepAliveMsg{
		MsgSize: 8,
		MsgType: TRANSPORT_SESSION_KEEPALIVE,
		Nonce:   util.RndUInt32(),
	}
	return m
}

// String returns a human-readable representation of the message.
func (m *SessionKeepAliveMsg) String() string {
	return fmt.Sprintf("SessionKeepAliveMsg{%d}", m.Nonce)
}

// Header returns the message header in a separate instance.
func (m *SessionKeepAliveMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// TRANSPORT_SESSION_KEEPALIVE_RESPONSE
//----------------------------------------------------------------------

// SessionKeepAliveRespMsg is a response for a peer to a "keep-alive" request.
type SessionKeepAliveRespMsg struct {
	MsgSize uint16 `order:"big"` // total size of message
	MsgType uint16 `order:"big"` // TRANSPORT_SESSION_KEEPALIVE_RESPONSE (382)
	Nonce   uint32
}

// NewSessionKeepAliveRespMsg is a response message for a "keep session" request.
func NewSessionKeepAliveRespMsg(nonce uint32) *SessionKeepAliveRespMsg {
	m := &SessionKeepAliveRespMsg{
		MsgSize: 8,
		MsgType: TRANSPORT_SESSION_KEEPALIVE_RESPONSE,
		Nonce:   nonce,
	}
	return m
}

// String returns a human-readable representation of the message.
func (m *SessionKeepAliveRespMsg) String() string {
	return fmt.Sprintf("SessionKeepAliveRespMsg{%d}", m.Nonce)
}

// Header returns the message header in a separate instance.
func (m *SessionKeepAliveRespMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}