From: Sergey Matveev Date: Wed, 6 Jan 2021 14:11:39 +0000 (+0300) Subject: Simplify Pkt struct with less useless pointers X-Git-Tag: v5.5.0^2~9 X-Git-Url: http://www.git.cypherpunks.ru/?a=commitdiff_plain;h=645a7eed7bb6318933bc586e65e6c9f6ec67b28a;p=nncp.git Simplify Pkt struct with less useless pointers --- diff --git a/src/pkt.go b/src/pkt.go index 425ac9d..5fdaad7 100644 --- a/src/pkt.go +++ b/src/pkt.go @@ -48,6 +48,8 @@ const ( MaxPathSize = 1<<8 - 1 NNCPBundlePrefix = "NNCP" + + PktSizeOverhead = 8 + poly1305.TagSize ) var ( @@ -56,9 +58,8 @@ var ( BadMagic error = errors.New("Unknown magic number") BadPktType error = errors.New("Unknown packet type") - PktOverhead int64 - PktEncOverhead int64 - PktSizeOverhead int64 = 8 + poly1305.TagSize + PktOverhead int64 + PktEncOverhead int64 ) type Pkt struct { @@ -66,7 +67,7 @@ type Pkt struct { Type PktType Nice uint8 PathLen uint8 - Path *[MaxPathSize]byte + Path [MaxPathSize]byte } type PktTbs struct { @@ -74,7 +75,7 @@ type PktTbs struct { Nice uint8 Sender *NodeId Recipient *NodeId - ExchPub *[32]byte + ExchPub [32]byte } type PktEnc struct { @@ -82,15 +83,12 @@ type PktEnc struct { Nice uint8 Sender *NodeId Recipient *NodeId - ExchPub *[32]byte - Sign *[ed25519.SignatureSize]byte + ExchPub [32]byte + Sign [ed25519.SignatureSize]byte } func init() { - pkt := Pkt{ - Type: PktTypeFile, - Path: new([MaxPathSize]byte), - } + pkt := Pkt{Type: PktTypeFile} var buf bytes.Buffer n, err := xdr.Marshal(&buf, pkt) if err != nil { @@ -105,11 +103,8 @@ func init() { } pktEnc := PktEnc{ Magic: MagicNNCPEv4, - Nice: 123, Sender: dummyId, Recipient: dummyId, - ExchPub: new([32]byte), - Sign: new([ed25519.SignatureSize]byte), } n, err = xdr.Marshal(&buf, pktEnc) if err != nil { @@ -127,7 +122,6 @@ func NewPkt(typ PktType, nice uint8, path []byte) (*Pkt, error) { Type: typ, Nice: nice, PathLen: uint8(len(path)), - Path: new([MaxPathSize]byte), } copy(pkt.Path[:], path) return &pkt, nil @@ -211,7 +205,7 @@ func PktEncWrite( Nice: nice, Sender: our.Id, Recipient: their.Id, - ExchPub: pubEph, + ExchPub: *pubEph, } var tbsBuf bytes.Buffer if _, err = xdr.Marshal(&tbsBuf, &tbs); err != nil { @@ -224,8 +218,8 @@ func PktEncWrite( Nice: nice, Sender: our.Id, Recipient: their.Id, - ExchPub: pubEph, - Sign: signature, + ExchPub: *pubEph, + Sign: *signature, } if _, err = xdr.Marshal(out, &pktEnc); err != nil { return err @@ -325,7 +319,7 @@ func PktEncRead( return their, 0, errors.New("Invalid signature") } sharedKey := new([32]byte) - curve25519.ScalarMult(sharedKey, our.ExchPrv, pktEnc.ExchPub) + curve25519.ScalarMult(sharedKey, our.ExchPrv, &pktEnc.ExchPub) kdf, err := blake2b.NewXOF(KDFXOFSize, sharedKey[:]) if err != nil { return their, 0, err