aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBernd Fix <brf@hoi-polloi.org>2019-09-20 13:13:50 +0200
committerBernd Fix <brf@hoi-polloi.org>2019-09-20 13:13:50 +0200
commit426a73f6e4fef0fc9d4f44bec18cd670b8c62856 (patch)
tree241384fa701e76082b7ede3ed05e0162076c1857 /src
parent170ce0e4edadb079ad9ab98da23e4fda65c21851 (diff)
downloadgnunet-go-426a73f6e4fef0fc9d4f44bec18cd670b8c62856.tar.gz
gnunet-go-426a73f6e4fef0fc9d4f44bec18cd670b8c62856.zip
Use 'AbsoluteTime' and 'RelativeTime' types in messages.
Diffstat (limited to 'src')
-rw-r--r--src/cmd/peer_mockup/process.go14
-rw-r--r--src/gnunet/message/factory.go4
-rw-r--r--src/gnunet/message/msg_core.go26
-rw-r--r--src/gnunet/message/msg_dht.go26
-rw-r--r--src/gnunet/message/msg_gns.go14
-rw-r--r--src/gnunet/message/msg_namecache.go22
-rw-r--r--src/gnunet/message/msg_transport.go134
-rw-r--r--src/gnunet/service/gns/gns.go8
-rw-r--r--src/gnunet/service/gns/record.go7
-rw-r--r--src/gnunet/util/format.go10
-rw-r--r--src/gnunet/util/peer_id.go27
-rw-r--r--src/gnunet/util/time.go63
12 files changed, 201 insertions, 154 deletions
diff --git a/src/cmd/peer_mockup/process.go b/src/cmd/peer_mockup/process.go
index 51d4805..a26177b 100644
--- a/src/cmd/peer_mockup/process.go
+++ b/src/cmd/peer_mockup/process.go
@@ -32,7 +32,8 @@ func process(ch *transport.MsgChannel, from, to *core.Peer) (err error) {
32 // are we initiating the connection? 32 // are we initiating the connection?
33 init := (from == p) 33 init := (from == p)
34 if init { 34 if init {
35 c.Send(message.NewTransportTcpWelcomeMsg(p.GetID())) 35 peerid := util.NewPeerID(p.GetID())
36 c.Send(message.NewTransportTcpWelcomeMsg(peerid))
36 } 37 }
37 38
38 // remember peer addresses (only ONE!) 39 // remember peer addresses (only ONE!)
@@ -50,11 +51,13 @@ func process(ch *transport.MsgChannel, from, to *core.Peer) (err error) {
50 switch msg := m.(type) { 51 switch msg := m.(type) {
51 52
52 case *message.TransportTcpWelcomeMsg: 53 case *message.TransportTcpWelcomeMsg:
54 peerid := util.NewPeerID(p.GetID())
53 if init { 55 if init {
54 c.Send(message.NewHelloMsg(p.GetID())) 56 c.Send(message.NewHelloMsg(peerid))
55 c.Send(message.NewTransportPingMsg(t.GetID(), tAddr)) 57 target := util.NewPeerID(t.GetID())
58 c.Send(message.NewTransportPingMsg(target, tAddr))
56 } else { 59 } else {
57 c.Send(message.NewTransportTcpWelcomeMsg(p.GetID())) 60 c.Send(message.NewTransportTcpWelcomeMsg(peerid))
58 } 61 }
59 62
60 case *message.HelloMsg: 63 case *message.HelloMsg:
@@ -80,7 +83,8 @@ func process(ch *transport.MsgChannel, from, to *core.Peer) (err error) {
80 } 83 }
81 84
82 case *message.SessionSynMsg: 85 case *message.SessionSynMsg:
83 mOut := message.NewSessionSynAckMsg(msg.Timestamp) 86 mOut := message.NewSessionSynAckMsg()
87 mOut.Timestamp = msg.Timestamp
84 if send[message.TRANSPORT_PONG] { 88 if send[message.TRANSPORT_PONG] {
85 c.Send(mOut) 89 c.Send(mOut)
86 } else { 90 } else {
diff --git a/src/gnunet/message/factory.go b/src/gnunet/message/factory.go
index 5feb8c3..5f599b7 100644
--- a/src/gnunet/message/factory.go
+++ b/src/gnunet/message/factory.go
@@ -18,9 +18,9 @@ func NewEmptyMessage(msgType uint16) (Message, error) {
18 case TRANSPORT_SESSION_QUOTA: 18 case TRANSPORT_SESSION_QUOTA:
19 return NewSessionQuotaMsg(0), nil 19 return NewSessionQuotaMsg(0), nil
20 case TRANSPORT_SESSION_SYN: 20 case TRANSPORT_SESSION_SYN:
21 return NewSessionSynMsg(0), nil 21 return NewSessionSynMsg(), nil
22 case TRANSPORT_SESSION_SYN_ACK: 22 case TRANSPORT_SESSION_SYN_ACK:
23 return NewSessionSynAckMsg(0), nil 23 return NewSessionSynAckMsg(), nil
24 case TRANSPORT_SESSION_ACK: 24 case TRANSPORT_SESSION_ACK:
25 return new(SessionAckMsg), nil 25 return new(SessionAckMsg), nil
26 case TRANSPORT_PING: 26 case TRANSPORT_PING:
diff --git a/src/gnunet/message/msg_core.go b/src/gnunet/message/msg_core.go
index 0fb03e9..0ddbd8b 100644
--- a/src/gnunet/message/msg_core.go
+++ b/src/gnunet/message/msg_core.go
@@ -11,17 +11,13 @@ import (
11 "gnunet/util" 11 "gnunet/util"
12) 12)
13 13
14type PeerID struct {
15 Key []byte `size:"32"`
16}
17
18type EphKeyBlock struct { 14type EphKeyBlock struct {
19 SignSize uint32 `order:"big"` // length of signed block 15 SignSize uint32 `order:"big"` // length of signed block
20 SigPurpose uint32 `order:"big"` // signature purpose: SIG_ECC_KEY 16 SigPurpose uint32 `order:"big"` // signature purpose: SIG_ECC_KEY
21 CreateTime uint64 `order:"big"` // Time of key creation 17 CreateTime util.AbsoluteTime // Time of key creation
22 ExpireTime uint64 `order:"big"` // Time of key expiration 18 ExpireTime util.RelativeTime // Time to live for key
23 EphemeralKey []byte `size:"32"` // Ephemeral EdDSA public key 19 EphemeralKey []byte `size:"32"` // Ephemeral EdDSA public key
24 PeerID *PeerID // Peer identity (EdDSA public key) 20 PeerID *util.PeerID // Peer identity (EdDSA public key)
25} 21}
26 22
27type EphemeralKeyMsg struct { 23type EphemeralKeyMsg struct {
@@ -41,19 +37,19 @@ func NewEphemeralKeyMsg() *EphemeralKeyMsg {
41 SignedBlock: &EphKeyBlock{ 37 SignedBlock: &EphKeyBlock{
42 SignSize: 88, 38 SignSize: 88,
43 SigPurpose: enums.SIG_ECC_KEY, 39 SigPurpose: enums.SIG_ECC_KEY,
44 CreateTime: util.GetAbsoluteTimeNow(), 40 CreateTime: util.AbsoluteTimeNow(),
45 ExpireTime: util.GetAbsoluteTimeOffset(12 * time.Hour), 41 ExpireTime: util.NewRelativeTime(12 * time.Hour),
46 EphemeralKey: make([]byte, 32), 42 EphemeralKey: make([]byte, 32),
47 PeerID: new(PeerID), 43 PeerID: util.NewPeerID(nil),
48 }, 44 },
49 } 45 }
50} 46}
51 47
52func (m *EphemeralKeyMsg) String() string { 48func (m *EphemeralKeyMsg) String() string {
53 return fmt.Sprintf("EphKeyMsg{%s,%s,%s,%d}", 49 return fmt.Sprintf("EphKeyMsg{peer=%s,ephkey=%s,create=%s,expire=%s,status=%d}",
54 util.EncodeBinaryToString(m.SignedBlock.PeerID.Key), 50 util.EncodeBinaryToString(m.SignedBlock.PeerID.Key),
55 util.EncodeBinaryToString(m.SignedBlock.EphemeralKey), 51 util.EncodeBinaryToString(m.SignedBlock.EphemeralKey),
56 util.Timestamp(m.SignedBlock.ExpireTime), 52 m.SignedBlock.CreateTime, m.SignedBlock.ExpireTime,
57 m.SenderStatus) 53 m.SenderStatus)
58} 54}
59 55
diff --git a/src/gnunet/message/msg_dht.go b/src/gnunet/message/msg_dht.go
index 8b6aa41..bd3475a 100644
--- a/src/gnunet/message/msg_dht.go
+++ b/src/gnunet/message/msg_dht.go
@@ -67,17 +67,17 @@ func (msg *DHTClientGetMsg) Header() *MessageHeader {
67 67
68// DHTClientResultMsg 68// DHTClientResultMsg
69type DHTClientResultMsg struct { 69type DHTClientResultMsg struct {
70 MsgSize uint16 `order:"big"` // total size of message 70 MsgSize uint16 `order:"big"` // total size of message
71 MsgType uint16 `order:"big"` // DHT_CLIENT_RESULT (145) 71 MsgType uint16 `order:"big"` // DHT_CLIENT_RESULT (145)
72 Type uint32 `order:"big"` // The type for the data 72 Type uint32 `order:"big"` // The type for the data
73 PutPathLen uint32 `order:"big"` // Number of peers recorded in outgoing path 73 PutPathLen uint32 `order:"big"` // Number of peers recorded in outgoing path
74 GetPathLen uint32 `order:"big"` // Number of peers recorded from storage location 74 GetPathLen uint32 `order:"big"` // Number of peers recorded from storage location
75 Id uint64 `order:"big"` // Unique ID of the matching GET request 75 Id uint64 `order:"big"` // Unique ID of the matching GET request
76 Expire uint64 `order:"big"` // Expiration time 76 Expire util.AbsoluteTime // Expiration time
77 Key *crypto.HashCode // The key that was searched for 77 Key *crypto.HashCode // The key that was searched for
78 PutPath []*PeerID `size:"PutPathLen"` // put path 78 PutPath []*util.PeerID `size:"PutPathLen"` // put path
79 GetPath []*PeerID `size:"GetPathLen"` // get path 79 GetPath []*util.PeerID `size:"GetPathLen"` // get path
80 Data []byte `size:"*"` // data returned for query 80 Data []byte `size:"*"` // data returned for query
81} 81}
82 82
83// NewDHTClientResultMsg creates a new default DHTClientResultMsg object. 83// NewDHTClientResultMsg creates a new default DHTClientResultMsg object.
@@ -92,14 +92,14 @@ func NewDHTClientResultMsg(key *crypto.HashCode) *DHTClientResultMsg {
92 PutPathLen: 0, 92 PutPathLen: 0,
93 GetPathLen: 0, 93 GetPathLen: 0,
94 Id: 0, 94 Id: 0,
95 Expire: 0, 95 Expire: *new(util.AbsoluteTime),
96 Key: key, 96 Key: key,
97 Data: make([]byte, 0), 97 Data: make([]byte, 0),
98 } 98 }
99} 99}
100 100
101func (m *DHTClientResultMsg) String() string { 101func (m *DHTClientResultMsg) String() string {
102 return fmt.Sprintf("DHTClientResultMsg{Id:%d}", m.Id) 102 return fmt.Sprintf("DHTClientResultMsg{id:%d,expire=%s}", m.Id, m.Expire)
103} 103}
104 104
105// Header returns the message header in a separate instance. 105// Header returns the message header in a separate instance.
diff --git a/src/gnunet/message/msg_gns.go b/src/gnunet/message/msg_gns.go
index 7ea7d3c..0ff34be 100644
--- a/src/gnunet/message/msg_gns.go
+++ b/src/gnunet/message/msg_gns.go
@@ -73,16 +73,16 @@ func (msg *GNSLookupMsg) Header() *MessageHeader {
73//---------------------------------------------------------------------- 73//----------------------------------------------------------------------
74 74
75type GNSResourceRecord struct { 75type GNSResourceRecord struct {
76 Expires uint64 `order:"big"` // Expiration time for the record 76 Expires util.AbsoluteTime // Expiration time for the record
77 Size uint32 `order:"big"` // Number of bytes in 'Data' 77 Size uint32 `order:"big"` // Number of bytes in 'Data'
78 Type uint32 `order:"big"` // Type of the GNS/DNS record 78 Type uint32 `order:"big"` // Type of the GNS/DNS record
79 Flags uint32 `order:"big"` // Flags for the record 79 Flags uint32 `order:"big"` // Flags for the record
80 Data []byte `size:"Size"` // Record data 80 Data []byte `size:"Size"` // Record data
81} 81}
82 82
83func (r *GNSResourceRecord) String() string { 83func (r *GNSResourceRecord) String() string {
84 return fmt.Sprintf("GNSResourceRecord{Type=%s,Expire=%s,Flag=%d,Size=%d}", 84 return fmt.Sprintf("GNSResourceRecord{type=%s,expire=%s,flags=%d,size=%d}",
85 enums.GNS_TYPE[int(r.Type)], util.Timestamp(r.Expires), r.Flags, r.Size) 85 enums.GNS_TYPE[int(r.Type)], r.Expires, r.Flags, r.Size)
86} 86}
87 87
88// GNSLookupResultMsg 88// GNSLookupResultMsg
diff --git a/src/gnunet/message/msg_namecache.go b/src/gnunet/message/msg_namecache.go
index 421b625..8f82622 100644
--- a/src/gnunet/message/msg_namecache.go
+++ b/src/gnunet/message/msg_namecache.go
@@ -4,9 +4,7 @@ import (
4 "encoding/hex" 4 "encoding/hex"
5 "fmt" 5 "fmt"
6 6
7 //"github.com/bfix/gospel/logger"
8 "gnunet/crypto" 7 "gnunet/crypto"
9 //"gnunet/enums"
10 "gnunet/util" 8 "gnunet/util"
11) 9)
12 10
@@ -52,13 +50,13 @@ func (msg *NamecacheLookupMsg) Header() *MessageHeader {
52 50
53// NamecacheLookupResultMsg 51// NamecacheLookupResultMsg
54type NamecacheLookupResultMsg struct { 52type NamecacheLookupResultMsg struct {
55 MsgSize uint16 `order:"big"` // total size of message 53 MsgSize uint16 `order:"big"` // total size of message
56 MsgType uint16 `order:"big"` // NAMECACHE_LOOKUP_BLOCK_RESPONSE (432) 54 MsgType uint16 `order:"big"` // NAMECACHE_LOOKUP_BLOCK_RESPONSE (432)
57 Id uint32 `order:"big"` // Request Id 55 Id uint32 `order:"big"` // Request Id
58 Expire uint64 `order:"big"` // Expiration time 56 Expire util.AbsoluteTime // Expiration time
59 Signature []byte `size:"64"` // ECDSA signature 57 Signature []byte `size:"64"` // ECDSA signature
60 DerivedKey []byte `size:"32"` // Derived public key 58 DerivedKey []byte `size:"32"` // Derived public key
61 EncData []byte `size:"*"` // Encrypted block data 59 EncData []byte `size:"*"` // Encrypted block data
62} 60}
63 61
64// NewNamecacheLookupResultMsg creates a new default message. 62// NewNamecacheLookupResultMsg creates a new default message.
@@ -67,7 +65,7 @@ func NewNamecacheLookupResultMsg() *NamecacheLookupResultMsg {
67 MsgSize: 112, 65 MsgSize: 112,
68 MsgType: NAMECACHE_LOOKUP_BLOCK_RESPONSE, 66 MsgType: NAMECACHE_LOOKUP_BLOCK_RESPONSE,
69 Id: 0, 67 Id: 0,
70 Expire: 0, 68 Expire: *new(util.AbsoluteTime),
71 Signature: make([]byte, 64), 69 Signature: make([]byte, 64),
72 DerivedKey: make([]byte, 32), 70 DerivedKey: make([]byte, 32),
73 EncData: make([]byte, 0), 71 EncData: make([]byte, 0),
@@ -76,8 +74,8 @@ func NewNamecacheLookupResultMsg() *NamecacheLookupResultMsg {
76 74
77// String 75// String
78func (m *NamecacheLookupResultMsg) String() string { 76func (m *NamecacheLookupResultMsg) String() string {
79 return fmt.Sprintf("NamecacheLookupResultMsg{Id=%d,Expire=%s}", 77 return fmt.Sprintf("NamecacheLookupResultMsg{id=%d,expire=%s}",
80 m.Id, util.Timestamp(m.Expire)) 78 m.Id, m.Expire)
81} 79}
82 80
83// Header returns the message header in a separate instance. 81// Header returns the message header in a separate instance.
diff --git a/src/gnunet/message/msg_transport.go b/src/gnunet/message/msg_transport.go
index ec4e143..1459d6a 100644
--- a/src/gnunet/message/msg_transport.go
+++ b/src/gnunet/message/msg_transport.go
@@ -15,28 +15,24 @@ import (
15//---------------------------------------------------------------------- 15//----------------------------------------------------------------------
16 16
17type TransportTcpWelcomeMsg struct { 17type TransportTcpWelcomeMsg struct {
18 MsgSize uint16 `order:"big"` // total size of message 18 MsgSize uint16 `order:"big"` // total size of message
19 MsgType uint16 `order:"big"` // TRANSPORT_TCP_WELCOME (61) 19 MsgType uint16 `order:"big"` // TRANSPORT_TCP_WELCOME (61)
20 PeerID []byte `size:"32"` // Peer identity (EdDSA public key) 20 PeerID *util.PeerID // Peer identity (EdDSA public key)
21} 21}
22 22
23func NewTransportTcpWelcomeMsg(peerid []byte) *TransportTcpWelcomeMsg { 23func NewTransportTcpWelcomeMsg(peerid *util.PeerID) *TransportTcpWelcomeMsg {
24 msg := &TransportTcpWelcomeMsg{ 24 if peerid == nil {
25 peerid = util.NewPeerID(nil)
26 }
27 return &TransportTcpWelcomeMsg{
25 MsgSize: 36, 28 MsgSize: 36,
26 MsgType: TRANSPORT_TCP_WELCOME, 29 MsgType: TRANSPORT_TCP_WELCOME,
27 PeerID: make([]byte, 32), 30 PeerID: peerid,
28 }
29 if peerid != nil {
30 copy(msg.PeerID[:], peerid)
31 } else {
32 msg.MsgSize = 0
33 msg.MsgType = 0
34 } 31 }
35 return msg
36} 32}
37 33
38func (m *TransportTcpWelcomeMsg) String() string { 34func (m *TransportTcpWelcomeMsg) String() string {
39 return fmt.Sprintf("TransportTcpWelcomeMsg{'%s'}", util.EncodeBinaryToString(m.PeerID)) 35 return fmt.Sprintf("TransportTcpWelcomeMsg{peer=%s}", m.PeerID)
40} 36}
41 37
42// Header returns the message header in a separate instance. 38// Header returns the message header in a separate instance.
@@ -58,11 +54,11 @@ func (msg *TransportTcpWelcomeMsg) Header() *MessageHeader {
58//---------------------------------------------------------------------- 54//----------------------------------------------------------------------
59 55
60type SignedAddress struct { 56type SignedAddress struct {
61 SignLength uint32 `order:"big"` // Length of signed block 57 SignLength uint32 `order:"big"` // Length of signed block
62 Purpose uint32 `order:"big"` // SIG_TRANSPORT_PONG_OWN 58 Purpose uint32 `order:"big"` // SIG_TRANSPORT_PONG_OWN
63 ExpireOn uint64 `order:"big"` // usec epoch 59 ExpireOn util.AbsoluteTime // usec epoch
64 AddrSize uint32 `order:"big"` // size of address 60 AddrSize uint32 `order:"big"` // size of address
65 Address []byte `size:"AddrSize"` // address 61 Address []byte `size:"AddrSize"` // address
66} 62}
67 63
68func NewSignedAddress(a *util.Address) *SignedAddress { 64func NewSignedAddress(a *util.Address) *SignedAddress {
@@ -72,7 +68,7 @@ func NewSignedAddress(a *util.Address) *SignedAddress {
72 addr := &SignedAddress{ 68 addr := &SignedAddress{
73 SignLength: uint32(alen + 20), 69 SignLength: uint32(alen + 20),
74 Purpose: enums.SIG_TRANSPORT_PONG_OWN, 70 Purpose: enums.SIG_TRANSPORT_PONG_OWN,
75 ExpireOn: util.GetAbsoluteTimeOffset(12 * time.Hour), 71 ExpireOn: util.AbsoluteTimeNow().Add(12 * time.Hour),
76 AddrSize: uint32(alen), 72 AddrSize: uint32(alen),
77 Address: make([]byte, alen), 73 Address: make([]byte, alen),
78 } 74 }
@@ -107,9 +103,10 @@ func NewTransportPongMsg(challenge uint32, a *util.Address) *TransportPongMsg {
107func (m *TransportPongMsg) String() string { 103func (m *TransportPongMsg) String() string {
108 a := new(util.Address) 104 a := new(util.Address)
109 if err := data.Unmarshal(a, m.SignedBlock.Address); err == nil { 105 if err := data.Unmarshal(a, m.SignedBlock.Address); err == nil {
110 return fmt.Sprintf("TransportPongMsg{%s,%d}", a, m.Challenge) 106 return fmt.Sprintf("TransportPongMsg{addr=%s,challenge=%d}",
107 a, m.Challenge)
111 } 108 }
112 return fmt.Sprintf("TransportPongMsg{<unkown>,%d}", m.Challenge) 109 return fmt.Sprintf("TransportPongMsg{addr=<unkown>,%d}", m.Challenge)
113} 110}
114 111
115// Header returns the message header in a separate instance. 112// Header returns the message header in a separate instance.
@@ -120,12 +117,10 @@ func (msg *TransportPongMsg) Header() *MessageHeader {
120func (m *TransportPongMsg) Sign(prv *ed25519.PrivateKey) error { 117func (m *TransportPongMsg) Sign(prv *ed25519.PrivateKey) error {
121 data, err := data.Marshal(m.SignedBlock) 118 data, err := data.Marshal(m.SignedBlock)
122 if err != nil { 119 if err != nil {
123 fmt.Printf("Sign: %s\n", err)
124 return err 120 return err
125 } 121 }
126 sig, err := prv.EdSign(data) 122 sig, err := prv.EdSign(data)
127 if err != nil { 123 if err != nil {
128 fmt.Printf("Sign: %s\n", err)
129 return err 124 return err
130 } 125 }
131 copy(m.Signature, sig.Bytes()) 126 copy(m.Signature, sig.Bytes())
@@ -154,24 +149,24 @@ func (m *TransportPongMsg) Verify(pub *ed25519.PublicKey) (bool, error) {
154//---------------------------------------------------------------------- 149//----------------------------------------------------------------------
155 150
156type TransportPingMsg struct { 151type TransportPingMsg struct {
157 MsgSize uint16 `order:"big"` // total size of message 152 MsgSize uint16 `order:"big"` // total size of message
158 MsgType uint16 `order:"big"` // TRANSPORT_PING (372) 153 MsgType uint16 `order:"big"` // TRANSPORT_PING (372)
159 Challenge uint32 // Challenge code (to ensure fresh reply) 154 Challenge uint32 // Challenge code (to ensure fresh reply)
160 Target []byte `size:"32"` // EdDSA public key (long-term) of target peer 155 Target *util.PeerID // EdDSA public key (long-term) of target peer
161 Address []byte `size:"*"` // encoded address 156 Address []byte `size:"*"` // encoded address
162} 157}
163 158
164func NewTransportPingMsg(target []byte, a *util.Address) *TransportPingMsg { 159func NewTransportPingMsg(target *util.PeerID, a *util.Address) *TransportPingMsg {
160 if target == nil {
161 target = util.NewPeerID(nil)
162 }
165 m := &TransportPingMsg{ 163 m := &TransportPingMsg{
166 MsgSize: uint16(40), 164 MsgSize: uint16(40),
167 MsgType: TRANSPORT_PING, 165 MsgType: TRANSPORT_PING,
168 Challenge: util.RndUInt32(), 166 Challenge: util.RndUInt32(),
169 Target: make([]byte, 32), 167 Target: target,
170 Address: nil, 168 Address: nil,
171 } 169 }
172 if target != nil {
173 copy(m.Target, target)
174 }
175 if a != nil { 170 if a != nil {
176 if addrData, err := data.Marshal(a); err == nil { 171 if addrData, err := data.Marshal(a); err == nil {
177 m.Address = addrData 172 m.Address = addrData
@@ -184,8 +179,8 @@ func NewTransportPingMsg(target []byte, a *util.Address) *TransportPingMsg {
184func (m *TransportPingMsg) String() string { 179func (m *TransportPingMsg) String() string {
185 a := new(util.Address) 180 a := new(util.Address)
186 data.Unmarshal(a, m.Address) 181 data.Unmarshal(a, m.Address)
187 return fmt.Sprintf("TransportPingMsg{%s,%s,%d}", 182 return fmt.Sprintf("TransportPingMsg{target=%s,addr=%s,challenge=%d}",
188 util.EncodeBinaryToString(m.Target), a, m.Challenge) 183 m.Target, a, m.Challenge)
189} 184}
190 185
191// Header returns the message header in a separate instance. 186// Header returns the message header in a separate instance.
@@ -208,17 +203,17 @@ func (msg *TransportPingMsg) Header() *MessageHeader {
208//---------------------------------------------------------------------- 203//----------------------------------------------------------------------
209 204
210type HelloAddress struct { 205type HelloAddress struct {
211 Transport string // Name of transport 206 Transport string // Name of transport
212 AddrSize uint16 `order:"big"` // Size of address entry 207 AddrSize uint16 `order:"big"` // Size of address entry
213 ExpireOn uint64 `order:"big"` // Expiry date 208 ExpireOn util.AbsoluteTime // Expiry date
214 Address []byte `size:"AddrSize"` // Address specification 209 Address []byte `size:"AddrSize"` // Address specification
215} 210}
216 211
217func NewAddress(a *util.Address) *HelloAddress { 212func NewAddress(a *util.Address) *HelloAddress {
218 addr := &HelloAddress{ 213 addr := &HelloAddress{
219 Transport: a.Transport, 214 Transport: a.Transport,
220 AddrSize: uint16(len(a.Address)), 215 AddrSize: uint16(len(a.Address)),
221 ExpireOn: util.GetAbsoluteTimeOffset(12 * time.Hour), 216 ExpireOn: util.AbsoluteTimeNow().Add(12 * time.Hour),
222 Address: make([]byte, len(a.Address)), 217 Address: make([]byte, len(a.Address)),
223 } 218 }
224 copy(addr.Address, a.Address) 219 copy(addr.Address, a.Address)
@@ -226,33 +221,34 @@ func NewAddress(a *util.Address) *HelloAddress {
226} 221}
227 222
228func (a *HelloAddress) String() string { 223func (a *HelloAddress) String() string {
229 return fmt.Sprintf("Address{%s,%s}", util.AddressString(a.Transport, a.Address), util.Timestamp(a.ExpireOn)) 224 return fmt.Sprintf("Address{%s,expire=%s}",
225 util.AddressString(a.Transport, a.Address), a.ExpireOn)
230} 226}
231 227
232type HelloMsg struct { 228type HelloMsg struct {
233 MsgSize uint16 `order:"big"` // total size of message 229 MsgSize uint16 `order:"big"` // total size of message
234 MsgType uint16 `order:"big"` // HELLO (17) 230 MsgType uint16 `order:"big"` // HELLO (17)
235 FriendOnly uint32 `order:"big"` // =1: do not gossip this HELLO 231 FriendOnly uint32 `order:"big"` // =1: do not gossip this HELLO
236 PeerID []byte `size:"32"` // EdDSA public key (long-term) 232 PeerID *util.PeerID // EdDSA public key (long-term)
237 Addresses []*HelloAddress `size:"*"` // List of end-point addressess 233 Addresses []*HelloAddress `size:"*"` // List of end-point addressess
238} 234}
239 235
240func NewHelloMsg(peerid []byte) *HelloMsg { 236func NewHelloMsg(peerid *util.PeerID) *HelloMsg {
241 m := &HelloMsg{ 237 if peerid == nil {
238 peerid = util.NewPeerID(nil)
239 }
240 return &HelloMsg{
242 MsgSize: 40, 241 MsgSize: 40,
243 MsgType: HELLO, 242 MsgType: HELLO,
244 FriendOnly: 0, 243 FriendOnly: 0,
245 PeerID: make([]byte, 32), 244 PeerID: peerid,
246 Addresses: make([]*HelloAddress, 0), 245 Addresses: make([]*HelloAddress, 0),
247 } 246 }
248 if peerid != nil {
249 copy(m.PeerID, peerid)
250 }
251 return m
252} 247}
253 248
254func (m *HelloMsg) String() string { 249func (m *HelloMsg) String() string {
255 return fmt.Sprintf("HelloMsg{%s,%d,%v}", util.EncodeBinaryToString(m.PeerID), m.FriendOnly, m.Addresses) 250 return fmt.Sprintf("HelloMsg{peer=%s,friendsonly=%d,addr=%v}",
251 m.PeerID, m.FriendOnly, m.Addresses)
256} 252}
257 253
258func (m *HelloMsg) AddAddress(a *HelloAddress) { 254func (m *HelloMsg) AddAddress(a *HelloAddress) {
@@ -295,26 +291,23 @@ func (msg *SessionAckMsg) Header() *MessageHeader {
295//---------------------------------------------------------------------- 291//----------------------------------------------------------------------
296 292
297type SessionSynMsg struct { 293type SessionSynMsg struct {
298 MsgSize uint16 `order:"big"` // total size of message 294 MsgSize uint16 `order:"big"` // total size of message
299 MsgType uint16 `order:"big"` // TRANSPORT_SESSION_SYN (375) 295 MsgType uint16 `order:"big"` // TRANSPORT_SESSION_SYN (375)
300 Reserved uint32 `order:"big"` // reserved (=0) 296 Reserved uint32 `order:"big"` // reserved (=0)
301 Timestamp uint64 `order:"big"` // usec epoch 297 Timestamp util.AbsoluteTime // usec epoch
302} 298}
303 299
304func NewSessionSynMsg(t uint64) *SessionSynMsg { 300func NewSessionSynMsg() *SessionSynMsg {
305 if t == 0 {
306 t = util.GetAbsoluteTimeNow()
307 }
308 return &SessionSynMsg{ 301 return &SessionSynMsg{
309 MsgSize: 16, 302 MsgSize: 16,
310 MsgType: TRANSPORT_SESSION_SYN, 303 MsgType: TRANSPORT_SESSION_SYN,
311 Reserved: 0, 304 Reserved: 0,
312 Timestamp: t, 305 Timestamp: util.AbsoluteTimeNow(),
313 } 306 }
314} 307}
315 308
316func (m *SessionSynMsg) String() string { 309func (m *SessionSynMsg) String() string {
317 return fmt.Sprintf("SessionSyn{%s}", util.Timestamp(m.Timestamp)) 310 return fmt.Sprintf("SessionSyn{timestamp=%s}", m.Timestamp)
318} 311}
319 312
320// Header returns the message header in a separate instance. 313// Header returns the message header in a separate instance.
@@ -327,26 +320,23 @@ func (msg *SessionSynMsg) Header() *MessageHeader {
327//---------------------------------------------------------------------- 320//----------------------------------------------------------------------
328 321
329type SessionSynAckMsg struct { 322type SessionSynAckMsg struct {
330 MsgSize uint16 `order:"big"` // total size of message 323 MsgSize uint16 `order:"big"` // total size of message
331 MsgType uint16 `order:"big"` // TRANSPORT_SESSION_SYN_ACK (376) 324 MsgType uint16 `order:"big"` // TRANSPORT_SESSION_SYN_ACK (376)
332 Reserved uint32 `order:"big"` // reserved (=0) 325 Reserved uint32 `order:"big"` // reserved (=0)
333 Timestamp uint64 `order:"big"` // usec epoch 326 Timestamp util.AbsoluteTime // usec epoch
334} 327}
335 328
336func NewSessionSynAckMsg(t uint64) *SessionSynAckMsg { 329func NewSessionSynAckMsg() *SessionSynAckMsg {
337 if t == 0 {
338 t = util.GetAbsoluteTimeNow()
339 }
340 return &SessionSynAckMsg{ 330 return &SessionSynAckMsg{
341 MsgSize: 16, 331 MsgSize: 16,
342 MsgType: TRANSPORT_SESSION_SYN_ACK, 332 MsgType: TRANSPORT_SESSION_SYN_ACK,
343 Reserved: 0, 333 Reserved: 0,
344 Timestamp: t, 334 Timestamp: util.AbsoluteTimeNow(),
345 } 335 }
346} 336}
347 337
348func (m *SessionSynAckMsg) String() string { 338func (m *SessionSynAckMsg) String() string {
349 return fmt.Sprintf("SessionSynAck{%s}", util.Timestamp(m.Timestamp)) 339 return fmt.Sprintf("SessionSynAck{timestamp=%s}", m.Timestamp)
350} 340}
351 341
352// Header returns the message header in a separate instance. 342// Header returns the message header in a separate instance.
diff --git a/src/gnunet/service/gns/gns.go b/src/gnunet/service/gns/gns.go
index fb93c4b..b95a9e1 100644
--- a/src/gnunet/service/gns/gns.go
+++ b/src/gnunet/service/gns/gns.go
@@ -178,8 +178,8 @@ func (s *GNSService) LookupNamecache(query *crypto.HashCode, zoneKey *ed25519.Pu
178 break 178 break
179 } 179 }
180 // check if record has expired 180 // check if record has expired
181 if util.Expired(m.Expire) { 181 if m.Expire.Expired() {
182 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) 182 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", m.Expire)
183 break 183 break
184 } 184 }
185 185
@@ -245,8 +245,8 @@ func (s *GNSService) LookupDHT(query *crypto.HashCode, zoneKey *ed25519.PublicKe
245 break 245 break
246 } 246 }
247 // check if record has expired 247 // check if record has expired
248 if util.Expired(m.Expire) { 248 if m.Expire.Expired() {
249 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) 249 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", m.Expire)
250 break 250 break
251 } 251 }
252 // check if result is of requested type 252 // check if result is of requested type
diff --git a/src/gnunet/service/gns/record.go b/src/gnunet/service/gns/record.go
index 9befefe..0e6315b 100644
--- a/src/gnunet/service/gns/record.go
+++ b/src/gnunet/service/gns/record.go
@@ -7,6 +7,7 @@ import (
7 "github.com/bfix/gospel/data" 7 "github.com/bfix/gospel/data"
8 "gnunet/crypto" 8 "gnunet/crypto"
9 "gnunet/message" 9 "gnunet/message"
10 "gnunet/util"
10) 11)
11 12
12var ( 13var (
@@ -29,8 +30,8 @@ func NewGNSRecordSet() *GNSRecordSet {
29 30
30type SignedBlockData struct { 31type SignedBlockData struct {
31 Purpose *crypto.SignaturePurpose // Size and purpose of signature (8 bytes) 32 Purpose *crypto.SignaturePurpose // Size and purpose of signature (8 bytes)
32 Expire uint64 `order:"big"` // Expiration time of the block. 33 Expire util.AbsoluteTime // Expiration time of the block.
33 EncData []byte `size:"*"` // encrypted GNSRecordSet 34 EncData []byte `size:"*"` // encrypted GNSRecordSet
34 35
35 // transient data (not serialized) 36 // transient data (not serialized)
36 data []byte // unencrypted GNSRecord set 37 data []byte // unencrypted GNSRecord set
@@ -107,7 +108,7 @@ func NewGNSBlock() *GNSBlock {
107 DerivedKey: make([]byte, 32), 108 DerivedKey: make([]byte, 32),
108 Block: &SignedBlockData{ 109 Block: &SignedBlockData{
109 Purpose: new(crypto.SignaturePurpose), 110 Purpose: new(crypto.SignaturePurpose),
110 Expire: 0, 111 Expire: *new(util.AbsoluteTime),
111 EncData: nil, 112 EncData: nil,
112 data: nil, 113 data: nil,
113 }, 114 },
diff --git a/src/gnunet/util/format.go b/src/gnunet/util/format.go
index 4d42520..722b9a7 100644
--- a/src/gnunet/util/format.go
+++ b/src/gnunet/util/format.go
@@ -3,9 +3,7 @@ package util
3import ( 3import (
4 "encoding/hex" 4 "encoding/hex"
5 "fmt" 5 "fmt"
6 "math"
7 "net" 6 "net"
8 "time"
9) 7)
10 8
11func AddressString(transport string, addr []byte) string { 9func AddressString(transport string, addr []byte) string {
@@ -17,14 +15,6 @@ func AddressString(transport string, addr []byte) string {
17 return fmt.Sprintf("%s:%s", transport, hex.EncodeToString(addr)) 15 return fmt.Sprintf("%s:%s", transport, hex.EncodeToString(addr))
18} 16}
19 17
20func Timestamp(ts uint64) string {
21 if ts == math.MaxUint64 {
22 return "Never"
23 }
24 t := time.Unix(int64(ts/(1000*1000)), int64((ts%1000)*1000))
25 return t.Format(time.RFC3339Nano)
26}
27
28var scale = " kMGTPEO" 18var scale = " kMGTPEO"
29 19
30func Scale1024(n uint64) string { 20func Scale1024(n uint64) string {
diff --git a/src/gnunet/util/peer_id.go b/src/gnunet/util/peer_id.go
new file mode 100644
index 0000000..03bc73e
--- /dev/null
+++ b/src/gnunet/util/peer_id.go
@@ -0,0 +1,27 @@
1package util
2
3type PeerID struct {
4 Key []byte `size:"32"`
5}
6
7func NewPeerID(data []byte) *PeerID {
8 if data == nil {
9 data = make([]byte, 32)
10 } else {
11 size := len(data)
12 if size > 32 {
13 data = data[:32]
14 } else if size < 32 {
15 buf := make([]byte, 32)
16 CopyBlock(buf, data)
17 data = buf
18 }
19 }
20 return &PeerID{
21 Key: data,
22 }
23}
24
25func (p *PeerID) String() string {
26 return EncodeBinaryToString(p.Key)
27}
diff --git a/src/gnunet/util/time.go b/src/gnunet/util/time.go
index 78b0722..b513d5e 100644
--- a/src/gnunet/util/time.go
+++ b/src/gnunet/util/time.go
@@ -5,24 +5,65 @@ import (
5 "time" 5 "time"
6) 6)
7 7
8func GetAbsoluteTimeNow() uint64 { 8//----------------------------------------------------------------------
9 return getTimestamp(time.Now()) 9// Absolute time
10//----------------------------------------------------------------------
11
12type AbsoluteTime struct {
13 Val uint64 `order:"big"`
14}
15
16func NewAbsoluteTime(t time.Time) AbsoluteTime {
17 secs := t.Unix()
18 usecs := t.Nanosecond() / 1000
19 return AbsoluteTime{
20 Val: uint64(secs*1000000) + uint64(usecs),
21 }
10} 22}
11 23
12func GetAbsoluteTimeOffset(t time.Duration) uint64 { 24func AbsoluteTimeNow() AbsoluteTime {
13 return getTimestamp(time.Now().Add(t)) 25 return NewAbsoluteTime(time.Now())
14} 26}
15 27
16func Expired(ts uint64) bool { 28func (t AbsoluteTime) String() string {
29 if t.Val == math.MaxUint64 {
30 return "Never"
31 }
32 ts := time.Unix(int64(t.Val/(1000*1000)), int64((t.Val%1000)*1000))
33 return ts.Format(time.RFC3339Nano)
34}
35
36func (t AbsoluteTime) Add(d time.Duration) AbsoluteTime {
37 return AbsoluteTime{
38 Val: t.Val + uint64(d.Milliseconds()),
39 }
40}
41
42func (t AbsoluteTime) Expired() bool {
17 // check for "never" 43 // check for "never"
18 if ts == math.MaxUint64 { 44 if t.Val == math.MaxUint64 {
19 return false 45 return false
20 } 46 }
21 return ts < uint64(time.Now().Unix()) 47 return t.Val < uint64(time.Now().Unix())
22} 48}
23 49
24func getTimestamp(t time.Time) uint64 { 50//----------------------------------------------------------------------
25 secs := t.Unix() 51// Relative time
26 usecs := t.Nanosecond() / 1000 52//----------------------------------------------------------------------
27 return uint64(secs*1000000) + uint64(usecs) 53
54type RelativeTime struct {
55 Val uint64 `order:"big"`
56}
57
58func NewRelativeTime(d time.Duration) RelativeTime {
59 return RelativeTime{
60 Val: uint64(d.Milliseconds()),
61 }
62}
63
64func (t RelativeTime) String() string {
65 if t.Val == math.MaxUint64 {
66 return "Forever"
67 }
68 return time.Duration(t.Val * 1000).String()
28} 69}