aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/message/msg_hello_dht.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet/message/msg_hello_dht.go')
-rw-r--r--src/gnunet/message/msg_hello_dht.go167
1 files changed, 0 insertions, 167 deletions
diff --git a/src/gnunet/message/msg_hello_dht.go b/src/gnunet/message/msg_hello_dht.go
deleted file mode 100644
index f51757c..0000000
--- a/src/gnunet/message/msg_hello_dht.go
+++ /dev/null
@@ -1,167 +0,0 @@
1// This file is part of gnunet-go, a GNUnet-implementation in Golang.
2// Copyright (C) 2019-2022 Bernd Fix >Y<
3//
4// gnunet-go is free software: you can redistribute it and/or modify it
5// under the terms of the GNU Affero General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// gnunet-go is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//
17// SPDX-License-Identifier: AGPL3.0-or-later
18
19package message
20
21import (
22 "bytes"
23 "crypto/sha512"
24 "encoding/binary"
25 "fmt"
26 "gnunet/enums"
27 "gnunet/util"
28 "time"
29
30 "github.com/bfix/gospel/crypto/ed25519"
31 "github.com/bfix/gospel/logger"
32)
33
34//----------------------------------------------------------------------
35// HELLO-DHT
36//
37// A HELLO message is used to exchange information about transports with
38// other DHT nodes. This struct is always followed by the actual network
39// addresses of type "HelloAddress"
40//----------------------------------------------------------------------
41
42// HelloDHTMsg is a message send by peers to announce their presence
43type HelloDHTMsg struct {
44 MsgSize uint16 `order:"big"` // total size of message
45 MsgType uint16 `order:"big"` // DHT_P2P_HELLO (157)
46 Reserved uint16 `order:"big"` // Reserved for further use
47 NumAddr uint16 `order:"big"` // Number of addresses in list
48 Signature []byte `size:"64"` // Signature
49 Expires util.AbsoluteTime `` // expiration time
50 AddrList []byte `size:"*"` // List of end-point addresses (HelloAddress)
51}
52
53// NewHelloMsgDHT creates an empty DHT_P2P_HELLO message.
54func NewHelloDHTMsg() *HelloDHTMsg {
55 // return empty HelloMessage
56 exp := time.Now().Add(HelloAddressExpiration)
57 return &HelloDHTMsg{
58 MsgSize: 80, // size without 'AddrList'
59 MsgType: DHT_P2P_HELLO, // DHT_P2P_HELLO (157)
60 Reserved: 0, // not used here
61 NumAddr: 0, // start with empty address list
62 Signature: make([]byte, 64), // signature
63 Expires: util.NewAbsoluteTime(exp), // default expiration
64 AddrList: make([]byte, 0), // list of addresses
65 }
66}
67
68// Addresses returns the list of HelloAddress
69func (m *HelloDHTMsg) Addresses() (list []*util.Address, err error) {
70 var addr *util.Address
71 var as string
72 num, pos := 0, 0
73 for {
74 // parse address string from stream
75 if as, pos = util.ReadCString(m.AddrList, pos); pos == -1 {
76 break
77 }
78 if addr, err = util.ParseAddress(as); err != nil {
79 return
80 }
81 addr.Expires = m.Expires
82 list = append(list, addr)
83 num++
84 }
85 // check numbers
86 if num != int(m.NumAddr) {
87 logger.Printf(logger.WARN, "[HelloDHTMsg] Number of addresses does not match (got %d, expected %d)", num, m.NumAddr)
88 }
89 return
90}
91
92// SetAddresses adds addresses to the HELLO message.
93func (m *HelloDHTMsg) SetAddresses(list []*util.Address) {
94 // write addresses as blob and track earliest expiration
95 exp := util.NewAbsoluteTime(time.Now().Add(HelloAddressExpiration))
96 wrt := new(bytes.Buffer)
97 for _, addr := range list {
98 // check if address expires before current expire
99 if exp.Compare(addr.Expires) > 0 {
100 exp = addr.Expires
101 }
102 n, _ := wrt.Write([]byte(addr.URI()))
103 wrt.WriteByte(0)
104 m.MsgSize += uint16(n + 1)
105 }
106 m.AddrList = wrt.Bytes()
107 m.Expires = exp
108 m.NumAddr = uint16(len(list))
109}
110
111// String returns a human-readable representation of the message.
112func (m *HelloDHTMsg) String() string {
113 addrs, _ := m.Addresses()
114 aList := ""
115 for i, a := range addrs {
116 if i > 0 {
117 aList += ","
118 }
119 aList += a.URI()
120 }
121 return fmt.Sprintf("HelloDHTMsg{expire:%s,addrs=%d:[%s]}", m.Expires, m.NumAddr, aList)
122}
123
124// Header returns the message header in a separate instance.
125func (m *HelloDHTMsg) Header() *Header {
126 return &Header{m.MsgSize, m.MsgType}
127}
128
129// Verify the message signature
130func (m *HelloDHTMsg) Verify(peer *util.PeerID) (bool, error) {
131 // assemble signed data and public key
132 sd := m.signedData()
133 pub := peer.PublicKey()
134 sig, err := ed25519.NewEdSignatureFromBytes(m.Signature)
135 if err != nil {
136 return false, err
137 }
138 return pub.EdVerify(sd, sig)
139}
140
141// Sign the HELLO data with private key
142func (m *HelloDHTMsg) Sign(prv *ed25519.PrivateKey) error {
143 // assemble signed data
144 sd := m.signedData()
145 sig, err := prv.EdSign(sd)
146 if err != nil {
147 return err
148 }
149 m.Signature = sig.Bytes()
150 return nil
151}
152
153// signedData assembles a data block for sign and verify operations.
154func (m *HelloDHTMsg) signedData() []byte {
155 // hash address block
156 hAddr := sha512.Sum512(m.AddrList)
157 var size uint32 = 80
158 purpose := uint32(enums.SIG_HELLO)
159
160 // assemble signed data
161 buf := new(bytes.Buffer)
162 binary.Write(buf, binary.BigEndian, size)
163 binary.Write(buf, binary.BigEndian, purpose)
164 binary.Write(buf, binary.BigEndian, m.Expires.Epoch()*1000000)
165 buf.Write(hAddr[:])
166 return buf.Bytes()
167}