]> Cypherpunks.ru repositories - nncp.git/commitdiff
Simplify Pkt struct with less useless pointers
authorSergey Matveev <stargrave@stargrave.org>
Wed, 6 Jan 2021 14:11:39 +0000 (17:11 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 6 Jan 2021 14:11:39 +0000 (17:11 +0300)
src/pkt.go

index 425ac9dfc2a74119d894ff2814c71b33002d5222..5fdaad7bbdaeb85c3e5fd92a7782da0da953a597 100644 (file)
@@ -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