Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add version negotiation handshake wrapper #64

Draft
wants to merge 1 commit into
base: release/0.5.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions handshake/negotiate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package handshake

import (
"bytes"
"fmt"
"net"

"github.com/renproject/aw/codec"
"github.com/renproject/aw/wire"
"github.com/renproject/id"
"github.com/renproject/surge"
)

const VERSION_BYTES = 2
type versionType = uint16

func Negotiate(self id.Signatory, h Handshake) Handshake {
return func(conn net.Conn, enc codec.Encoder, dec codec.Decoder) (codec.Encoder, codec.Decoder, id.Signatory, error) {
e, d, remote, err := h(conn, enc, dec)
if err != nil {
return nil, nil, remote, fmt.Errorf("handshake before negotiating version: %v", err)
}

var versionBytes [VERSION_BYTES]byte
var version versionType
cmp := bytes.Compare(self[:], remote[:])
if cmp < 0 {
if _, err := dec(conn, versionBytes[:]); err != nil {
return nil, nil, remote, fmt.Errorf("decoding version: %v", err)
}
if _, _, err := surge.UnmarshalU16(&version, versionBytes[:], len(versionBytes)); err != nil {
return nil, nil, remote, fmt.Errorf("unmarshaling version: %v", err)
}
if _, err := enc(conn, versionBytes[:]); err != nil {
return nil, nil, remote, fmt.Errorf("encoding current version: %v", err)
}
if version == wire.CurrentVersion {
return e, d, remote, nil
}
return nil, nil, remote, fmt.Errorf("not current version: %v", err)
}
if _, _, err := surge.MarshalU16(wire.CurrentVersion, versionBytes[:], len(versionBytes)); err != nil {
return nil, nil, remote, fmt.Errorf("marshaling current version: %v", err)
}
if _, err := enc(conn, versionBytes[:]); err != nil {
return nil, nil, remote, fmt.Errorf("encoding version: %v", err)
}
if _, err := dec(conn, versionBytes[:]); err != nil {
return nil, nil, remote, fmt.Errorf("decoding version: %v", err)
}
if _, _, err := surge.UnmarshalU16(&version, versionBytes[:], len(versionBytes)); err != nil {
return nil, nil, remote, fmt.Errorf("unmarshaling version: %v", err)
}
if version == wire.CurrentVersion {
return e, d, remote, nil
}
return nil, nil, remote, fmt.Errorf("not current version: %v", err)
}
}
4 changes: 4 additions & 0 deletions wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import (
// Enumerate all valid MsgVersion values.
const (
MsgVersion1 = uint16(1)
CurrentVersion = MsgVersion1
)

var romanNumeralDict = map[uint16]struct{}{
}

// Enumerate all valid MsgType values.
const (
MsgTypePush = uint16(1)
Expand Down