Skip to content
Snippets Groups Projects
Commit 6ec3fcdd authored by Josh Brooks's avatar Josh Brooks
Browse files

Rearrage and clean up connect.go

parent b053970e
No related branches found
No related tags found
3 merge requests!510Release,!216Xx 3895/authenticated connection,!207WIP: Client Restructure
...@@ -33,7 +33,8 @@ import ( ...@@ -33,7 +33,8 @@ import (
// Connection is a wrapper for the E2E and auth packages. // Connection is a wrapper for the E2E and auth packages.
// It can be used to automatically establish an E2E partnership // It can be used to automatically establish an E2E partnership
// with a partner.Manager, or be built from an existing E2E partnership. // with a partner.Manager, or be built from an existing E2E partnership.
// You can then use this interface to send to and receive from the newly-established partner.Manager. // You can then use this interface to send to and receive from the
// newly-established partner.Manager.
type Connection interface { type Connection interface {
// Closer deletes this Connection's partner.Manager and releases resources // Closer deletes this Connection's partner.Manager and releases resources
io.Closer io.Closer
...@@ -41,7 +42,8 @@ type Connection interface { ...@@ -41,7 +42,8 @@ type Connection interface {
// GetPartner returns the partner.Manager for this Connection // GetPartner returns the partner.Manager for this Connection
GetPartner() partner.Manager GetPartner() partner.Manager
// SendE2E is a wrapper for sending specifically to the Connection's partner.Manager // SendE2E is a wrapper for sending specifically to the Connection's
// partner.Manager
SendE2E(mt catalog.MessageType, payload []byte, params clientE2e.Params) ( SendE2E(mt catalog.MessageType, payload []byte, params clientE2e.Params) (
[]id.Round, e2e.MessageID, time.Time, error) []id.Round, e2e.MessageID, time.Time, error)
...@@ -53,24 +55,19 @@ type Connection interface { ...@@ -53,24 +55,19 @@ type Connection interface {
Unregister(listenerID receive.ListenerID) Unregister(listenerID receive.ListenerID)
} }
// Callback is the callback format required to retrieve new Connection objects as they are established // Callback is the callback format required to retrieve
// new Connection objects as they are established.
type Callback func(connection Connection) type Callback func(connection Connection)
// handler provides an implementation for the Connection interface // Params for managing Connection objects.
type handler struct {
partner partner.Manager
e2e clientE2e.Handler
params Params
}
// Params for managing Connection objects
type Params struct { type Params struct {
Auth auth.Param Auth auth.Param
Rekey rekey.Params Rekey rekey.Params
Event event.Reporter Event event.Reporter
Timeout time.Duration
} }
// GetDefaultParams returns a usable set of default Connection parameters // GetDefaultParams returns a usable set of default Connection parameters.
func GetDefaultParams() Params { func GetDefaultParams() Params {
return Params{ return Params{
Auth: auth.GetDefaultParams(), Auth: auth.GetDefaultParams(),
...@@ -81,9 +78,11 @@ func GetDefaultParams() Params { ...@@ -81,9 +78,11 @@ func GetDefaultParams() Params {
// Connect performs auth key negotiation with the given recipient, // Connect performs auth key negotiation with the given recipient,
// and returns a Connection object for the newly-created partner.Manager // and returns a Connection object for the newly-created partner.Manager
// This function is to be used sender-side and will block until the partner.Manager is confirmed // This function is to be used sender-side and will block until the
func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *fastRNG.StreamGenerator, // partner.Manager is confirmed.
grp *cyclic.Group, net cmix.Client, p Params) (Connection, error) { func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int,
rng *fastRNG.StreamGenerator, grp *cyclic.Group, net cmix.Client,
p Params) (Connection, error) {
// Build an ephemeral KV // Build an ephemeral KV
kv := versioned.NewKV(ekv.MakeMemstore()) kv := versioned.NewKV(ekv.MakeMemstore())
...@@ -119,14 +118,17 @@ func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *f ...@@ -119,14 +118,17 @@ func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *f
} }
// Block waiting for auth to confirm // Block waiting for auth to confirm
jww.DEBUG.Printf("Connection waiting for auth request for %s to be confirmed...", recipient.ID.String()) jww.DEBUG.Printf("Connection waiting for auth request "+
"for %s to be confirmed...", recipient.ID.String())
newConnection := <-signalChannel newConnection := <-signalChannel
// Verify the Connection is complete // Verify the Connection is complete
if newConnection == nil { if newConnection == nil {
return nil, errors.Errorf("Unable to complete connection with partner %s", recipient.ID.String()) return nil, errors.Errorf("Unable to complete connection "+
"with partner %s", recipient.ID.String())
} }
jww.DEBUG.Printf("Connection auth request for %s confirmed", recipient.ID.String()) jww.DEBUG.Printf("Connection auth request for %s confirmed",
recipient.ID.String())
return newConnection, nil return newConnection, nil
} }
...@@ -134,8 +136,9 @@ func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *f ...@@ -134,8 +136,9 @@ func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *f
// RegisterConnectionCallback assembles a Connection object on the reception-side // RegisterConnectionCallback assembles a Connection object on the reception-side
// and feeds it into the given Callback whenever an incoming request // and feeds it into the given Callback whenever an incoming request
// for an E2E partnership with a partner.Manager is confirmed. // for an E2E partnership with a partner.Manager is confirmed.
func RegisterConnectionCallback(cb Callback, myId *id.ID, privKey *cyclic.Int, rng *fastRNG.StreamGenerator, func RegisterConnectionCallback(cb Callback, myId *id.ID, privKey *cyclic.Int,
grp *cyclic.Group, net cmix.Client, p Params) error { rng *fastRNG.StreamGenerator, grp *cyclic.Group, net cmix.Client,
p Params) error {
// Build an ephemeral KV // Build an ephemeral KV
kv := versioned.NewKV(ekv.MakeMemstore()) kv := versioned.NewKV(ekv.MakeMemstore())
...@@ -159,9 +162,18 @@ func RegisterConnectionCallback(cb Callback, myId *id.ID, privKey *cyclic.Int, r ...@@ -159,9 +162,18 @@ func RegisterConnectionCallback(cb Callback, myId *id.ID, privKey *cyclic.Int, r
return err return err
} }
// handler provides an implementation for the Connection interface.
type handler struct {
partner partner.Manager
e2e clientE2e.Handler
params Params
}
// BuildConnection assembles a Connection object // BuildConnection assembles a Connection object
// after an E2E partnership has already been confirmed with the given partner.Manager // after an E2E partnership has already been confirmed with the given
func BuildConnection(partner partner.Manager, e2eHandler clientE2e.Handler, p Params) Connection { // partner.Manager.
func BuildConnection(partner partner.Manager, e2eHandler clientE2e.Handler,
p Params) Connection {
return &handler{ return &handler{
partner: partner, partner: partner,
params: p, params: p,
...@@ -169,36 +181,41 @@ func BuildConnection(partner partner.Manager, e2eHandler clientE2e.Handler, p Pa ...@@ -169,36 +181,41 @@ func BuildConnection(partner partner.Manager, e2eHandler clientE2e.Handler, p Pa
} }
} }
// Close deletes this Connection's partner.Manager and releases resources // Close deletes this Connection's partner.Manager and releases resources.
func (h *handler) Close() error { func (h *handler) Close() error {
return h.e2e.DeletePartner(h.partner.PartnerId()) return h.e2e.DeletePartner(h.partner.PartnerId())
} }
// GetPartner returns the partner.Manager for this Connection // GetPartner returns the partner.Manager for this Connection.
func (h *handler) GetPartner() partner.Manager { func (h *handler) GetPartner() partner.Manager {
return h.partner return h.partner
} }
// SendE2E is a wrapper for sending specifically to the Connection's partner.Manager // SendE2E is a wrapper for sending specifically to the Connection's
func (h *handler) SendE2E(mt catalog.MessageType, payload []byte, params clientE2e.Params) ( // partner.Manager.
func (h *handler) SendE2E(mt catalog.MessageType, payload []byte,
params clientE2e.Params) (
[]id.Round, e2e.MessageID, time.Time, error) { []id.Round, e2e.MessageID, time.Time, error) {
return h.e2e.SendE2E(mt, h.partner.PartnerId(), payload, params) return h.e2e.SendE2E(mt, h.partner.PartnerId(), payload, params)
} }
// RegisterListener is used for E2E reception // RegisterListener is used for E2E reception
// and allows for reading data sent from the partner.Manager // and allows for reading data sent from the partner.Manager.
func (h *handler) RegisterListener(messageType catalog.MessageType, newListener receive.Listener) receive.ListenerID { func (h *handler) RegisterListener(messageType catalog.MessageType,
return h.e2e.RegisterListener(h.partner.PartnerId(), messageType, newListener) newListener receive.Listener) receive.ListenerID {
return h.e2e.RegisterListener(h.partner.PartnerId(),
messageType, newListener)
} }
// Unregister listener for E2E reception // Unregister listener for E2E reception.
func (h *handler) Unregister(listenerID receive.ListenerID) { func (h *handler) Unregister(listenerID receive.ListenerID) {
h.e2e.Unregister(listenerID) h.e2e.Unregister(listenerID)
} }
// authCallback provides callback functionality for interfacing between auth.State and Connection // authCallback provides callback functionality for interfacing between
// This is used both for blocking creation of a Connection object until the auth Request is confirmed // auth.State and Connection. This is used both for blocking creation of a
// and for dynamically building new Connection objects when an auth Request is received. // Connection object until the auth Request is confirmed and for dynamically
// building new Connection objects when an auth Request is received.
type authCallback struct { type authCallback struct {
// Used for signaling confirmation of E2E partnership // Used for signaling confirmation of E2E partnership
connectionCallback Callback connectionCallback Callback
...@@ -208,8 +225,10 @@ type authCallback struct { ...@@ -208,8 +225,10 @@ type authCallback struct {
connectionParams Params connectionParams Params
} }
// getAuthCallback returns a callback interface to be passed into the creation of an auth.State object. // getAuthCallback returns a callback interface to be passed into the creation
func getAuthCallback(cb Callback, e2e clientE2e.Handler, params Params) authCallback { // of an auth.State object.
func getAuthCallback(cb Callback, e2e clientE2e.Handler,
params Params) authCallback {
return authCallback{ return authCallback{
connectionCallback: cb, connectionCallback: cb,
connectionE2e: e2e, connectionE2e: e2e,
...@@ -217,27 +236,33 @@ func getAuthCallback(cb Callback, e2e clientE2e.Handler, params Params) authCall ...@@ -217,27 +236,33 @@ func getAuthCallback(cb Callback, e2e clientE2e.Handler, params Params) authCall
} }
} }
// Confirm will be called when an auth Confirm message is processed // Confirm will be called when an auth Confirm message is processed.
func (a authCallback) Confirm(requestor contact.Contact, receptionID receptionID.EphemeralIdentity, round rounds.Round) { func (a authCallback) Confirm(requestor contact.Contact,
jww.DEBUG.Printf("Connection auth request for %s confirmed", requestor.ID.String()) receptionID receptionID.EphemeralIdentity, round rounds.Round) {
jww.DEBUG.Printf("Connection auth request for %s confirmed",
requestor.ID.String())
// After confirmation, get the new partner // After confirmation, get the new partner
newPartner, err := a.connectionE2e.GetPartner(requestor.ID) newPartner, err := a.connectionE2e.GetPartner(requestor.ID)
if err != nil { if err != nil {
jww.ERROR.Printf("Unable to build connection with partner %s: %+v", requestor.ID, err) jww.ERROR.Printf("Unable to build connection with "+
"partner %s: %+v", requestor.ID, err)
// Send a nil connection to avoid hold-ups down the line // Send a nil connection to avoid hold-ups down the line
a.connectionCallback(nil) a.connectionCallback(nil)
return return
} }
// Return the new Connection object // Return the new Connection object
a.connectionCallback(BuildConnection(newPartner, a.connectionE2e, a.connectionParams)) a.connectionCallback(BuildConnection(newPartner, a.connectionE2e,
a.connectionParams))
} }
// Request will be called when an auth Request message is processed // Request will be called when an auth Request message is processed.
func (a authCallback) Request(requestor contact.Contact, receptionID receptionID.EphemeralIdentity, round rounds.Round) { func (a authCallback) Request(requestor contact.Contact,
receptionID receptionID.EphemeralIdentity, round rounds.Round) {
} }
// Reset will be called when an auth Reset operation occurs // Reset will be called when an auth Reset operation occurs.
func (a authCallback) Reset(requestor contact.Contact, receptionID receptionID.EphemeralIdentity, round rounds.Round) { func (a authCallback) Reset(requestor contact.Contact,
receptionID receptionID.EphemeralIdentity, round rounds.Round) {
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment