gnunet-go

GNUnet Bindings for Go
Log | Files | Refs | README | LICENSE

commit 426a73f6e4fef0fc9d4f44bec18cd670b8c62856
parent 170ce0e4edadb079ad9ab98da23e4fda65c21851
Author: Bernd Fix <brf@hoi-polloi.org>
Date:   Fri, 20 Sep 2019 13:13:50 +0200

Use 'AbsoluteTime' and 'RelativeTime' types in messages.

Diffstat:
Msrc/cmd/peer_mockup/process.go | 14+++++++++-----
Msrc/gnunet/message/factory.go | 4++--
Msrc/gnunet/message/msg_core.go | 26+++++++++++---------------
Msrc/gnunet/message/msg_dht.go | 26+++++++++++++-------------
Msrc/gnunet/message/msg_gns.go | 14+++++++-------
Msrc/gnunet/message/msg_namecache.go | 22++++++++++------------
Msrc/gnunet/message/msg_transport.go | 134+++++++++++++++++++++++++++++++++++++------------------------------------------
Msrc/gnunet/service/gns/gns.go | 8++++----
Msrc/gnunet/service/gns/record.go | 7++++---
Msrc/gnunet/util/format.go | 10----------
Asrc/gnunet/util/peer_id.go | 27+++++++++++++++++++++++++++
Msrc/gnunet/util/time.go | 63++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
12 files changed, 201 insertions(+), 154 deletions(-)

diff --git 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) { // are we initiating the connection? init := (from == p) if init { - c.Send(message.NewTransportTcpWelcomeMsg(p.GetID())) + peerid := util.NewPeerID(p.GetID()) + c.Send(message.NewTransportTcpWelcomeMsg(peerid)) } // remember peer addresses (only ONE!) @@ -50,11 +51,13 @@ func process(ch *transport.MsgChannel, from, to *core.Peer) (err error) { switch msg := m.(type) { case *message.TransportTcpWelcomeMsg: + peerid := util.NewPeerID(p.GetID()) if init { - c.Send(message.NewHelloMsg(p.GetID())) - c.Send(message.NewTransportPingMsg(t.GetID(), tAddr)) + c.Send(message.NewHelloMsg(peerid)) + target := util.NewPeerID(t.GetID()) + c.Send(message.NewTransportPingMsg(target, tAddr)) } else { - c.Send(message.NewTransportTcpWelcomeMsg(p.GetID())) + c.Send(message.NewTransportTcpWelcomeMsg(peerid)) } case *message.HelloMsg: @@ -80,7 +83,8 @@ func process(ch *transport.MsgChannel, from, to *core.Peer) (err error) { } case *message.SessionSynMsg: - mOut := message.NewSessionSynAckMsg(msg.Timestamp) + mOut := message.NewSessionSynAckMsg() + mOut.Timestamp = msg.Timestamp if send[message.TRANSPORT_PONG] { c.Send(mOut) } else { diff --git a/src/gnunet/message/factory.go b/src/gnunet/message/factory.go @@ -18,9 +18,9 @@ func NewEmptyMessage(msgType uint16) (Message, error) { case TRANSPORT_SESSION_QUOTA: return NewSessionQuotaMsg(0), nil case TRANSPORT_SESSION_SYN: - return NewSessionSynMsg(0), nil + return NewSessionSynMsg(), nil case TRANSPORT_SESSION_SYN_ACK: - return NewSessionSynAckMsg(0), nil + return NewSessionSynAckMsg(), nil case TRANSPORT_SESSION_ACK: return new(SessionAckMsg), nil case TRANSPORT_PING: diff --git a/src/gnunet/message/msg_core.go b/src/gnunet/message/msg_core.go @@ -11,17 +11,13 @@ import ( "gnunet/util" ) -type PeerID struct { - Key []byte `size:"32"` -} - type EphKeyBlock struct { - SignSize uint32 `order:"big"` // length of signed block - SigPurpose uint32 `order:"big"` // signature purpose: SIG_ECC_KEY - CreateTime uint64 `order:"big"` // Time of key creation - ExpireTime uint64 `order:"big"` // Time of key expiration - EphemeralKey []byte `size:"32"` // Ephemeral EdDSA public key - PeerID *PeerID // Peer identity (EdDSA public key) + SignSize uint32 `order:"big"` // length of signed block + SigPurpose uint32 `order:"big"` // signature purpose: SIG_ECC_KEY + CreateTime util.AbsoluteTime // Time of key creation + ExpireTime util.RelativeTime // Time to live for key + EphemeralKey []byte `size:"32"` // Ephemeral EdDSA public key + PeerID *util.PeerID // Peer identity (EdDSA public key) } type EphemeralKeyMsg struct { @@ -41,19 +37,19 @@ func NewEphemeralKeyMsg() *EphemeralKeyMsg { SignedBlock: &EphKeyBlock{ SignSize: 88, SigPurpose: enums.SIG_ECC_KEY, - CreateTime: util.GetAbsoluteTimeNow(), - ExpireTime: util.GetAbsoluteTimeOffset(12 * time.Hour), + CreateTime: util.AbsoluteTimeNow(), + ExpireTime: util.NewRelativeTime(12 * time.Hour), EphemeralKey: make([]byte, 32), - PeerID: new(PeerID), + PeerID: util.NewPeerID(nil), }, } } func (m *EphemeralKeyMsg) String() string { - return fmt.Sprintf("EphKeyMsg{%s,%s,%s,%d}", + return fmt.Sprintf("EphKeyMsg{peer=%s,ephkey=%s,create=%s,expire=%s,status=%d}", util.EncodeBinaryToString(m.SignedBlock.PeerID.Key), util.EncodeBinaryToString(m.SignedBlock.EphemeralKey), - util.Timestamp(m.SignedBlock.ExpireTime), + m.SignedBlock.CreateTime, m.SignedBlock.ExpireTime, m.SenderStatus) } diff --git a/src/gnunet/message/msg_dht.go b/src/gnunet/message/msg_dht.go @@ -67,17 +67,17 @@ func (msg *DHTClientGetMsg) Header() *MessageHeader { // DHTClientResultMsg type DHTClientResultMsg struct { - MsgSize uint16 `order:"big"` // total size of message - MsgType uint16 `order:"big"` // DHT_CLIENT_RESULT (145) - Type uint32 `order:"big"` // The type for the data - PutPathLen uint32 `order:"big"` // Number of peers recorded in outgoing path - GetPathLen uint32 `order:"big"` // Number of peers recorded from storage location - Id uint64 `order:"big"` // Unique ID of the matching GET request - Expire uint64 `order:"big"` // Expiration time - Key *crypto.HashCode // The key that was searched for - PutPath []*PeerID `size:"PutPathLen"` // put path - GetPath []*PeerID `size:"GetPathLen"` // get path - Data []byte `size:"*"` // data returned for query + MsgSize uint16 `order:"big"` // total size of message + MsgType uint16 `order:"big"` // DHT_CLIENT_RESULT (145) + Type uint32 `order:"big"` // The type for the data + PutPathLen uint32 `order:"big"` // Number of peers recorded in outgoing path + GetPathLen uint32 `order:"big"` // Number of peers recorded from storage location + Id uint64 `order:"big"` // Unique ID of the matching GET request + Expire util.AbsoluteTime // Expiration time + Key *crypto.HashCode // The key that was searched for + PutPath []*util.PeerID `size:"PutPathLen"` // put path + GetPath []*util.PeerID `size:"GetPathLen"` // get path + Data []byte `size:"*"` // data returned for query } // NewDHTClientResultMsg creates a new default DHTClientResultMsg object. @@ -92,14 +92,14 @@ func NewDHTClientResultMsg(key *crypto.HashCode) *DHTClientResultMsg { PutPathLen: 0, GetPathLen: 0, Id: 0, - Expire: 0, + Expire: *new(util.AbsoluteTime), Key: key, Data: make([]byte, 0), } } func (m *DHTClientResultMsg) String() string { - return fmt.Sprintf("DHTClientResultMsg{Id:%d}", m.Id) + return fmt.Sprintf("DHTClientResultMsg{id:%d,expire=%s}", m.Id, m.Expire) } // 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 @@ -73,16 +73,16 @@ func (msg *GNSLookupMsg) Header() *MessageHeader { //---------------------------------------------------------------------- type GNSResourceRecord struct { - Expires uint64 `order:"big"` // Expiration time for the record - Size uint32 `order:"big"` // Number of bytes in 'Data' - Type uint32 `order:"big"` // Type of the GNS/DNS record - Flags uint32 `order:"big"` // Flags for the record - Data []byte `size:"Size"` // Record data + Expires util.AbsoluteTime // Expiration time for the record + Size uint32 `order:"big"` // Number of bytes in 'Data' + Type uint32 `order:"big"` // Type of the GNS/DNS record + Flags uint32 `order:"big"` // Flags for the record + Data []byte `size:"Size"` // Record data } func (r *GNSResourceRecord) String() string { - return fmt.Sprintf("GNSResourceRecord{Type=%s,Expire=%s,Flag=%d,Size=%d}", - enums.GNS_TYPE[int(r.Type)], util.Timestamp(r.Expires), r.Flags, r.Size) + return fmt.Sprintf("GNSResourceRecord{type=%s,expire=%s,flags=%d,size=%d}", + enums.GNS_TYPE[int(r.Type)], r.Expires, r.Flags, r.Size) } // GNSLookupResultMsg diff --git a/src/gnunet/message/msg_namecache.go b/src/gnunet/message/msg_namecache.go @@ -4,9 +4,7 @@ import ( "encoding/hex" "fmt" - //"github.com/bfix/gospel/logger" "gnunet/crypto" - //"gnunet/enums" "gnunet/util" ) @@ -52,13 +50,13 @@ func (msg *NamecacheLookupMsg) Header() *MessageHeader { // NamecacheLookupResultMsg type NamecacheLookupResultMsg struct { - MsgSize uint16 `order:"big"` // total size of message - MsgType uint16 `order:"big"` // NAMECACHE_LOOKUP_BLOCK_RESPONSE (432) - Id uint32 `order:"big"` // Request Id - Expire uint64 `order:"big"` // Expiration time - Signature []byte `size:"64"` // ECDSA signature - DerivedKey []byte `size:"32"` // Derived public key - EncData []byte `size:"*"` // Encrypted block data + MsgSize uint16 `order:"big"` // total size of message + MsgType uint16 `order:"big"` // NAMECACHE_LOOKUP_BLOCK_RESPONSE (432) + Id uint32 `order:"big"` // Request Id + Expire util.AbsoluteTime // Expiration time + Signature []byte `size:"64"` // ECDSA signature + DerivedKey []byte `size:"32"` // Derived public key + EncData []byte `size:"*"` // Encrypted block data } // NewNamecacheLookupResultMsg creates a new default message. @@ -67,7 +65,7 @@ func NewNamecacheLookupResultMsg() *NamecacheLookupResultMsg { MsgSize: 112, MsgType: NAMECACHE_LOOKUP_BLOCK_RESPONSE, Id: 0, - Expire: 0, + Expire: *new(util.AbsoluteTime), Signature: make([]byte, 64), DerivedKey: make([]byte, 32), EncData: make([]byte, 0), @@ -76,8 +74,8 @@ func NewNamecacheLookupResultMsg() *NamecacheLookupResultMsg { // String func (m *NamecacheLookupResultMsg) String() string { - return fmt.Sprintf("NamecacheLookupResultMsg{Id=%d,Expire=%s}", - m.Id, util.Timestamp(m.Expire)) + return fmt.Sprintf("NamecacheLookupResultMsg{id=%d,expire=%s}", + m.Id, m.Expire) } // 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 @@ -15,28 +15,24 @@ import ( //---------------------------------------------------------------------- type TransportTcpWelcomeMsg struct { - MsgSize uint16 `order:"big"` // total size of message - MsgType uint16 `order:"big"` // TRANSPORT_TCP_WELCOME (61) - PeerID []byte `size:"32"` // Peer identity (EdDSA public key) + MsgSize uint16 `order:"big"` // total size of message + MsgType uint16 `order:"big"` // TRANSPORT_TCP_WELCOME (61) + PeerID *util.PeerID // Peer identity (EdDSA public key) } -func NewTransportTcpWelcomeMsg(peerid []byte) *TransportTcpWelcomeMsg { - msg := &TransportTcpWelcomeMsg{ +func NewTransportTcpWelcomeMsg(peerid *util.PeerID) *TransportTcpWelcomeMsg { + if peerid == nil { + peerid = util.NewPeerID(nil) + } + return &TransportTcpWelcomeMsg{ MsgSize: 36, MsgType: TRANSPORT_TCP_WELCOME, - PeerID: make([]byte, 32), - } - if peerid != nil { - copy(msg.PeerID[:], peerid) - } else { - msg.MsgSize = 0 - msg.MsgType = 0 + PeerID: peerid, } - return msg } func (m *TransportTcpWelcomeMsg) String() string { - return fmt.Sprintf("TransportTcpWelcomeMsg{'%s'}", util.EncodeBinaryToString(m.PeerID)) + return fmt.Sprintf("TransportTcpWelcomeMsg{peer=%s}", m.PeerID) } // Header returns the message header in a separate instance. @@ -58,11 +54,11 @@ func (msg *TransportTcpWelcomeMsg) Header() *MessageHeader { //---------------------------------------------------------------------- type SignedAddress struct { - SignLength uint32 `order:"big"` // Length of signed block - Purpose uint32 `order:"big"` // SIG_TRANSPORT_PONG_OWN - ExpireOn uint64 `order:"big"` // usec epoch - AddrSize uint32 `order:"big"` // size of address - Address []byte `size:"AddrSize"` // address + SignLength uint32 `order:"big"` // Length of signed block + Purpose uint32 `order:"big"` // SIG_TRANSPORT_PONG_OWN + ExpireOn util.AbsoluteTime // usec epoch + AddrSize uint32 `order:"big"` // size of address + Address []byte `size:"AddrSize"` // address } func NewSignedAddress(a *util.Address) *SignedAddress { @@ -72,7 +68,7 @@ func NewSignedAddress(a *util.Address) *SignedAddress { addr := &SignedAddress{ SignLength: uint32(alen + 20), Purpose: enums.SIG_TRANSPORT_PONG_OWN, - ExpireOn: util.GetAbsoluteTimeOffset(12 * time.Hour), + ExpireOn: util.AbsoluteTimeNow().Add(12 * time.Hour), AddrSize: uint32(alen), Address: make([]byte, alen), } @@ -107,9 +103,10 @@ func NewTransportPongMsg(challenge uint32, a *util.Address) *TransportPongMsg { func (m *TransportPongMsg) String() string { a := new(util.Address) if err := data.Unmarshal(a, m.SignedBlock.Address); err == nil { - return fmt.Sprintf("TransportPongMsg{%s,%d}", a, m.Challenge) + return fmt.Sprintf("TransportPongMsg{addr=%s,challenge=%d}", + a, m.Challenge) } - return fmt.Sprintf("TransportPongMsg{<unkown>,%d}", m.Challenge) + return fmt.Sprintf("TransportPongMsg{addr=<unkown>,%d}", m.Challenge) } // Header returns the message header in a separate instance. @@ -120,12 +117,10 @@ func (msg *TransportPongMsg) Header() *MessageHeader { func (m *TransportPongMsg) Sign(prv *ed25519.PrivateKey) error { data, err := data.Marshal(m.SignedBlock) if err != nil { - fmt.Printf("Sign: %s\n", err) return err } sig, err := prv.EdSign(data) if err != nil { - fmt.Printf("Sign: %s\n", err) return err } copy(m.Signature, sig.Bytes()) @@ -154,24 +149,24 @@ func (m *TransportPongMsg) Verify(pub *ed25519.PublicKey) (bool, error) { //---------------------------------------------------------------------- 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 []byte `size:"32"` // EdDSA public key (long-term) of target peer - Address []byte `size:"*"` // encoded address + 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 } -func NewTransportPingMsg(target []byte, a *util.Address) *TransportPingMsg { +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: make([]byte, 32), + Target: target, Address: nil, } - if target != nil { - copy(m.Target, target) - } if a != nil { if addrData, err := data.Marshal(a); err == nil { m.Address = addrData @@ -184,8 +179,8 @@ func NewTransportPingMsg(target []byte, a *util.Address) *TransportPingMsg { func (m *TransportPingMsg) String() string { a := new(util.Address) data.Unmarshal(a, m.Address) - return fmt.Sprintf("TransportPingMsg{%s,%s,%d}", - util.EncodeBinaryToString(m.Target), a, m.Challenge) + return fmt.Sprintf("TransportPingMsg{target=%s,addr=%s,challenge=%d}", + m.Target, a, m.Challenge) } // Header returns the message header in a separate instance. @@ -208,17 +203,17 @@ func (msg *TransportPingMsg) Header() *MessageHeader { //---------------------------------------------------------------------- type HelloAddress struct { - Transport string // Name of transport - AddrSize uint16 `order:"big"` // Size of address entry - ExpireOn uint64 `order:"big"` // Expiry date - Address []byte `size:"AddrSize"` // Address specification + Transport string // Name of transport + AddrSize uint16 `order:"big"` // Size of address entry + ExpireOn util.AbsoluteTime // Expiry date + Address []byte `size:"AddrSize"` // Address specification } func NewAddress(a *util.Address) *HelloAddress { addr := &HelloAddress{ Transport: a.Transport, AddrSize: uint16(len(a.Address)), - ExpireOn: util.GetAbsoluteTimeOffset(12 * time.Hour), + ExpireOn: util.AbsoluteTimeNow().Add(12 * time.Hour), Address: make([]byte, len(a.Address)), } copy(addr.Address, a.Address) @@ -226,33 +221,34 @@ func NewAddress(a *util.Address) *HelloAddress { } func (a *HelloAddress) String() string { - return fmt.Sprintf("Address{%s,%s}", util.AddressString(a.Transport, a.Address), util.Timestamp(a.ExpireOn)) + return fmt.Sprintf("Address{%s,expire=%s}", + util.AddressString(a.Transport, a.Address), a.ExpireOn) } type HelloMsg struct { MsgSize uint16 `order:"big"` // total size of message MsgType uint16 `order:"big"` // HELLO (17) FriendOnly uint32 `order:"big"` // =1: do not gossip this HELLO - PeerID []byte `size:"32"` // EdDSA public key (long-term) - Addresses []*HelloAddress `size:"*"` // List of end-point addressess + PeerID *util.PeerID // EdDSA public key (long-term) + Addresses []*HelloAddress `size:"*"` // List of end-point addressess } -func NewHelloMsg(peerid []byte) *HelloMsg { - m := &HelloMsg{ +func NewHelloMsg(peerid *util.PeerID) *HelloMsg { + if peerid == nil { + peerid = util.NewPeerID(nil) + } + return &HelloMsg{ MsgSize: 40, MsgType: HELLO, FriendOnly: 0, - PeerID: make([]byte, 32), + PeerID: peerid, Addresses: make([]*HelloAddress, 0), } - if peerid != nil { - copy(m.PeerID, peerid) - } - return m } func (m *HelloMsg) String() string { - return fmt.Sprintf("HelloMsg{%s,%d,%v}", util.EncodeBinaryToString(m.PeerID), m.FriendOnly, m.Addresses) + return fmt.Sprintf("HelloMsg{peer=%s,friendsonly=%d,addr=%v}", + m.PeerID, m.FriendOnly, m.Addresses) } func (m *HelloMsg) AddAddress(a *HelloAddress) { @@ -295,26 +291,23 @@ func (msg *SessionAckMsg) Header() *MessageHeader { //---------------------------------------------------------------------- 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 uint64 `order:"big"` // usec epoch + 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 } -func NewSessionSynMsg(t uint64) *SessionSynMsg { - if t == 0 { - t = util.GetAbsoluteTimeNow() - } +func NewSessionSynMsg() *SessionSynMsg { return &SessionSynMsg{ MsgSize: 16, MsgType: TRANSPORT_SESSION_SYN, Reserved: 0, - Timestamp: t, + Timestamp: util.AbsoluteTimeNow(), } } func (m *SessionSynMsg) String() string { - return fmt.Sprintf("SessionSyn{%s}", util.Timestamp(m.Timestamp)) + return fmt.Sprintf("SessionSyn{timestamp=%s}", m.Timestamp) } // Header returns the message header in a separate instance. @@ -327,26 +320,23 @@ func (msg *SessionSynMsg) Header() *MessageHeader { //---------------------------------------------------------------------- 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 uint64 `order:"big"` // usec epoch + 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 } -func NewSessionSynAckMsg(t uint64) *SessionSynAckMsg { - if t == 0 { - t = util.GetAbsoluteTimeNow() - } +func NewSessionSynAckMsg() *SessionSynAckMsg { return &SessionSynAckMsg{ MsgSize: 16, MsgType: TRANSPORT_SESSION_SYN_ACK, Reserved: 0, - Timestamp: t, + Timestamp: util.AbsoluteTimeNow(), } } func (m *SessionSynAckMsg) String() string { - return fmt.Sprintf("SessionSynAck{%s}", util.Timestamp(m.Timestamp)) + return fmt.Sprintf("SessionSynAck{timestamp=%s}", m.Timestamp) } // 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 @@ -178,8 +178,8 @@ func (s *GNSService) LookupNamecache(query *crypto.HashCode, zoneKey *ed25519.Pu break } // check if record has expired - if util.Expired(m.Expire) { - logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) + if m.Expire.Expired() { + logger.Printf(logger.ERROR, "[gns] block expired at %s\n", m.Expire) break } @@ -245,8 +245,8 @@ func (s *GNSService) LookupDHT(query *crypto.HashCode, zoneKey *ed25519.PublicKe break } // check if record has expired - if util.Expired(m.Expire) { - logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) + if m.Expire.Expired() { + logger.Printf(logger.ERROR, "[gns] block expired at %s\n", m.Expire) break } // check if result is of requested type diff --git a/src/gnunet/service/gns/record.go b/src/gnunet/service/gns/record.go @@ -7,6 +7,7 @@ import ( "github.com/bfix/gospel/data" "gnunet/crypto" "gnunet/message" + "gnunet/util" ) var ( @@ -29,8 +30,8 @@ func NewGNSRecordSet() *GNSRecordSet { type SignedBlockData struct { Purpose *crypto.SignaturePurpose // Size and purpose of signature (8 bytes) - Expire uint64 `order:"big"` // Expiration time of the block. - EncData []byte `size:"*"` // encrypted GNSRecordSet + Expire util.AbsoluteTime // Expiration time of the block. + EncData []byte `size:"*"` // encrypted GNSRecordSet // transient data (not serialized) data []byte // unencrypted GNSRecord set @@ -107,7 +108,7 @@ func NewGNSBlock() *GNSBlock { DerivedKey: make([]byte, 32), Block: &SignedBlockData{ Purpose: new(crypto.SignaturePurpose), - Expire: 0, + Expire: *new(util.AbsoluteTime), EncData: nil, data: nil, }, diff --git a/src/gnunet/util/format.go b/src/gnunet/util/format.go @@ -3,9 +3,7 @@ package util import ( "encoding/hex" "fmt" - "math" "net" - "time" ) func AddressString(transport string, addr []byte) string { @@ -17,14 +15,6 @@ func AddressString(transport string, addr []byte) string { return fmt.Sprintf("%s:%s", transport, hex.EncodeToString(addr)) } -func Timestamp(ts uint64) string { - if ts == math.MaxUint64 { - return "Never" - } - t := time.Unix(int64(ts/(1000*1000)), int64((ts%1000)*1000)) - return t.Format(time.RFC3339Nano) -} - var scale = " kMGTPEO" func Scale1024(n uint64) string { diff --git a/src/gnunet/util/peer_id.go b/src/gnunet/util/peer_id.go @@ -0,0 +1,27 @@ +package util + +type PeerID struct { + Key []byte `size:"32"` +} + +func NewPeerID(data []byte) *PeerID { + if data == nil { + data = make([]byte, 32) + } else { + size := len(data) + if size > 32 { + data = data[:32] + } else if size < 32 { + buf := make([]byte, 32) + CopyBlock(buf, data) + data = buf + } + } + return &PeerID{ + Key: data, + } +} + +func (p *PeerID) String() string { + return EncodeBinaryToString(p.Key) +} diff --git a/src/gnunet/util/time.go b/src/gnunet/util/time.go @@ -5,24 +5,65 @@ import ( "time" ) -func GetAbsoluteTimeNow() uint64 { - return getTimestamp(time.Now()) +//---------------------------------------------------------------------- +// Absolute time +//---------------------------------------------------------------------- + +type AbsoluteTime struct { + Val uint64 `order:"big"` +} + +func NewAbsoluteTime(t time.Time) AbsoluteTime { + secs := t.Unix() + usecs := t.Nanosecond() / 1000 + return AbsoluteTime{ + Val: uint64(secs*1000000) + uint64(usecs), + } } -func GetAbsoluteTimeOffset(t time.Duration) uint64 { - return getTimestamp(time.Now().Add(t)) +func AbsoluteTimeNow() AbsoluteTime { + return NewAbsoluteTime(time.Now()) } -func Expired(ts uint64) bool { +func (t AbsoluteTime) String() string { + if t.Val == math.MaxUint64 { + return "Never" + } + ts := time.Unix(int64(t.Val/(1000*1000)), int64((t.Val%1000)*1000)) + return ts.Format(time.RFC3339Nano) +} + +func (t AbsoluteTime) Add(d time.Duration) AbsoluteTime { + return AbsoluteTime{ + Val: t.Val + uint64(d.Milliseconds()), + } +} + +func (t AbsoluteTime) Expired() bool { // check for "never" - if ts == math.MaxUint64 { + if t.Val == math.MaxUint64 { return false } - return ts < uint64(time.Now().Unix()) + return t.Val < uint64(time.Now().Unix()) } -func getTimestamp(t time.Time) uint64 { - secs := t.Unix() - usecs := t.Nanosecond() / 1000 - return uint64(secs*1000000) + uint64(usecs) +//---------------------------------------------------------------------- +// Relative time +//---------------------------------------------------------------------- + +type RelativeTime struct { + Val uint64 `order:"big"` +} + +func NewRelativeTime(d time.Duration) RelativeTime { + return RelativeTime{ + Val: uint64(d.Milliseconds()), + } +} + +func (t RelativeTime) String() string { + if t.Val == math.MaxUint64 { + return "Forever" + } + return time.Duration(t.Val * 1000).String() }