Add rpc request/response for handshake

This commit is contained in:
9seconds
2018-07-01 12:36:30 +03:00
parent 6253648356
commit b9ee1e1857
5 changed files with 112 additions and 7 deletions
+8 -1
View File
@@ -1,5 +1,12 @@
package rpc
import "bytes"
const (
RPCNonceSeqNo = -2
RPCHandshakeSeqNo = -1
)
type RPC interface {
Bytes() []byte
Bytes() *bytes.Buffer
}
+47
View File
@@ -0,0 +1,47 @@
package rpc
import "bytes"
const (
rpcHandshakeTagLength = 4
rpcHandshakeFlagsLength = 4
rpcHandshakeSenderPIDLength = 12
rpcHandshakePeerPIDLength = rpcHandshakeSenderPIDLength
rpcHandshakeRequestLength = rpcHandshakeTagLength + rpcHandshakeFlagsLength +
rpcHandshakeSenderPIDLength + rpcHandshakePeerPIDLength
)
var (
rpcHandshakeSenderPID [rpcHandshakeSenderPIDLength]byte
rpcHandshakePeerPID [rpcHandshakePeerPIDLength]byte
rpcHandshakeTag = [rpcHandshakeTagLength]byte{0xf5, 0xee, 0x82, 0x76}
rpcHandshakeFlags = [rpcHandshakeFlagsLength]byte{0x00, 0x00, 0x00, 0x00}
rpcHandshakeBuffer *bytes.Buffer
)
type RPCHandshakeRequest struct {
}
func (r *RPCHandshakeRequest) Bytes() *bytes.Buffer {
buf := &bytes.Buffer{}
buf.Grow(rpcHandshakeRequestLength)
buf.Write(rpcHandshakeTag[:])
buf.Write(rpcHandshakeFlags[:])
buf.Write(rpcHandshakeSenderPID[:])
buf.Write(rpcHandshakePeerPID[:])
return buf
}
func init() {
copy(rpcHandshakeSenderPID[:], "IPIPPRPDTIME")
copy(rpcHandshakePeerPID[:], "IPIPPRPDTIME")
}
func NewRPCHandshakeRequest() *RPCHandshakeRequest {
return &RPCHandshakeRequest{}
}
+53
View File
@@ -0,0 +1,53 @@
package rpc
import (
"bytes"
"github.com/juju/errors"
)
const rpcHandshakeResponseLength = rpcHandshakeRequestLength
type RPCHandshakeResponse struct {
Type [rpcHandshakeTagLength]byte
Flags [rpcHandshakeFlagsLength]byte
SenderPID [rpcHandshakeSenderPIDLength]byte
PeerPID [rpcHandshakePeerPIDLength]byte
}
func (r *RPCHandshakeResponse) Bytes() *bytes.Buffer {
buf := &bytes.Buffer{}
buf.Grow(rpcHandshakeResponseLength)
buf.Write(r.Type[:])
buf.Write(r.Flags[:])
buf.Write(r.SenderPID[:])
buf.Write(r.PeerPID[:])
return buf
}
func (r *RPCHandshakeResponse) Valid(req *RPCHandshakeRequest) error {
if r.Type != rpcHandshakeTag {
return errors.New("Unexpected handshake tag")
}
if r.PeerPID != rpcHandshakeSenderPID {
return errors.New("Incorrect sender PID")
}
return nil
}
func NewRPCHandshakeResponse(data []byte) (*RPCHandshakeResponse, error) {
if len(data) != rpcHandshakeResponseLength {
return nil, errors.New("Incorrect handshake response length")
}
resp := RPCHandshakeResponse{}
copy(resp.Type[:], data[:4])
copy(resp.Flags[:], data[4:8])
copy(resp.SenderPID[:], data[8:20])
copy(resp.PeerPID[:], data[20:])
return &resp, nil
}
+2 -4
View File
@@ -10,8 +10,6 @@ import (
)
const (
RPCNonceSeqNo = -2
rpcNonceLength = 16
rpcNonceKeySelectorLength = 4
rpcNonceCryptoTSLength = 4
@@ -33,7 +31,7 @@ type RPCNonceRequest struct {
Nonce [rpcNonceLength]byte
}
func (r *RPCNonceRequest) Bytes() []byte {
func (r *RPCNonceRequest) Bytes() *bytes.Buffer {
buf := &bytes.Buffer{}
buf.Grow(rpcNonceRequestLength)
@@ -43,7 +41,7 @@ func (r *RPCNonceRequest) Bytes() []byte {
buf.Write(r.CryptoTS[:])
buf.Write(r.Nonce[:])
return buf.Bytes()
return buf
}
func NewRPCNonceRequest(proxySecret []byte) (*RPCNonceRequest, error) {
+2 -2
View File
@@ -15,7 +15,7 @@ type RPCNonceResponse struct {
Crypto [rpcNonceCryptoAESLength]byte
}
func (r *RPCNonceResponse) Bytes() []byte {
func (r *RPCNonceResponse) Bytes() *bytes.Buffer {
buf := &bytes.Buffer{}
buf.Grow(rpcNonceResponseLength)
@@ -25,7 +25,7 @@ func (r *RPCNonceResponse) Bytes() []byte {
buf.Write(r.CryptoTS[:])
buf.Write(r.Nonce[:])
return buf.Bytes()
return buf
}
func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {