Skip to content

Commit

Permalink
Merge pull request #39 from libp2p/fix/cleanup
Browse files Browse the repository at this point in the history
use io.ReadFull instead of custom helper
  • Loading branch information
yusefnapora authored Jan 29, 2020
2 parents 3971c24 + 268e9e2 commit a1e6857
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
5 changes: 3 additions & 2 deletions p2p/security/noise/ik_handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package noise
import (
"context"
"fmt"
"io"

proto "github.com/gogo/protobuf/proto"
ik "github.com/libp2p/go-libp2p-noise/ik"
Expand All @@ -28,7 +29,7 @@ func (s *secureSession) ik_sendHandshakeMessage(payload []byte, initial_stage bo
}

// send message
_, err = writeAll(s.insecure, encMsgBuf)
_, err = s.insecure.Write(encMsgBuf)
if err != nil {
log.Error("ik_sendHandshakeMessage initiator=%v err=%s", s.initiator, err)
return fmt.Errorf("ik_sendHandshakeMessage write to conn err=%s", err)
Expand All @@ -45,7 +46,7 @@ func (s *secureSession) ik_recvHandshakeMessage(initial_stage bool) (buf []byte,

buf = make([]byte, l)

_, err = fillBuffer(buf, s.insecure)
_, err = io.ReadFull(s.insecure, buf)
if err != nil {
return buf, nil, false, fmt.Errorf("ik_recvHandshakeMessage read from conn err=%s", err)
}
Expand Down
9 changes: 5 additions & 4 deletions p2p/security/noise/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"sync"
"time"
Expand Down Expand Up @@ -121,14 +122,14 @@ func (s *secureSession) NoisePrivateKey() [32]byte {

func (s *secureSession) readLength() (int, error) {
buf := make([]byte, 2)
_, err := fillBuffer(buf, s.insecure)
_, err := io.ReadFull(s.insecure, buf)
return int(binary.BigEndian.Uint16(buf)), err
}

func (s *secureSession) writeLength(length int) error {
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, uint16(length))
_, err := writeAll(s.insecure, buf)
_, err := s.insecure.Write(buf)
return err
}

Expand Down Expand Up @@ -259,7 +260,7 @@ func (s *secureSession) Read(buf []byte) (int, error) {

// read and decrypt ciphertext
ciphertext := make([]byte, l)
_, err = fillBuffer(ciphertext, s.insecure)
_, err = io.ReadFull(s.insecure, ciphertext)
if err != nil {
log.Error("read ciphertext err", err)
return 0, err
Expand Down Expand Up @@ -337,7 +338,7 @@ func (s *secureSession) Write(in []byte) (int, error) {
return 0, err
}

_, err = writeAll(s.insecure, ciphertext)
_, err = s.insecure.Write(ciphertext)
return len(in), err
}

Expand Down
31 changes: 0 additions & 31 deletions p2p/security/noise/util.go

This file was deleted.

5 changes: 3 additions & 2 deletions p2p/security/noise/xx_handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package noise
import (
"context"
"fmt"
"io"

proto "github.com/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p-core/peer"
Expand All @@ -27,7 +28,7 @@ func (s *secureSession) xx_sendHandshakeMessage(payload []byte, initial_stage bo
return fmt.Errorf("xx_sendHandshakeMessage write length err=%s", err)
}

_, err = writeAll(s.insecure, encMsgBuf)
_, err = s.insecure.Write(encMsgBuf)
if err != nil {
log.Error("xx_sendHandshakeMessage initiator=%v err=%s", s.initiator, err)
return fmt.Errorf("xx_sendHandshakeMessage write to conn err=%s", err)
Expand All @@ -46,7 +47,7 @@ func (s *secureSession) xx_recvHandshakeMessage(initial_stage bool) (buf []byte,

buf = make([]byte, l)

_, err = fillBuffer(buf, s.insecure)
_, err = io.ReadFull(s.insecure, buf)
if err != nil {
return buf, nil, false, fmt.Errorf("xx_recvHandshakeMessage read from conn err=%s", err)
}
Expand Down

0 comments on commit a1e6857

Please sign in to comment.