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

Move message-specific behavior out of generic RPC library #2707

Merged
merged 5 commits into from
May 22, 2019
Merged
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
2 changes: 2 additions & 0 deletions go/lib/infra/messenger/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ go_library(
"//go/proto:go_default_library",
"@com_github_lucas_clemente_quic_go//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_zombiezen_go_capnproto2//:go_default_library",
"@com_zombiezen_go_capnproto2//pogs:go_default_library",
],
)

Expand Down
14 changes: 12 additions & 2 deletions go/lib/infra/messenger/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,13 +884,23 @@ func (r *QUICRequester) Request(ctx context.Context, pld *ctrl.Pld,
return nil, err
}

request := &rpc.Request{SignedPld: signedPld}
msg, err := signedPldToMsg(signedPld)
if err != nil {
return nil, err
}

request := &rpc.Request{Message: msg}
reply, err := r.QUICClientConfig.Request(ctx, request, newAddr)
if err != nil {
return nil, err
}

replyPld, err := reply.SignedPld.UnsafePld()
replySignedPld, err := msgToSignedPld(reply.Message)
if err != nil {
return nil, err
}

replyPld, err := replySignedPld.UnsafePld()
if err != nil {
return nil, err
}
Expand Down
38 changes: 37 additions & 1 deletion go/lib/infra/messenger/quic_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ import (
"sync"
"time"

capnp "zombiezen.com/go/capnproto2"
"zombiezen.com/go/capnproto2/pogs"

"github.com/scionproto/scion/go/lib/ctrl"
"github.com/scionproto/scion/go/lib/infra"
"github.com/scionproto/scion/go/lib/infra/rpc"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/proto"
)

var _ rpc.Handler = (*QUICHandler)(nil)
Expand All @@ -39,7 +44,11 @@ type QUICHandler struct {
}

func (h *QUICHandler) ServeRPC(rw rpc.ReplyWriter, request *rpc.Request) {
signedPld := request.SignedPld
signedPld, err := msgToSignedPld(request.Message)
if err != nil {
log.Error("Unable to extract SignedPld from capnp", "from", request.Address, "err", err)
return
}

pld, err := signedPld.UnsafePld()
if err != nil {
Expand Down Expand Up @@ -85,3 +94,30 @@ func (h *QUICHandler) Handle(msgType infra.MessageType, handler infra.Handler) {
h.handlers[msgType] = handler
h.handlersLock.Unlock()
}

func msgToSignedPld(msg *capnp.Message) (*ctrl.SignedPld, error) {
root, err := msg.RootPtr()
if err != nil {
return nil, err
}
signedPld := &ctrl.SignedPld{}
if err := proto.SafeExtract(signedPld, proto.SignedCtrlPld_TypeID, root.Struct()); err != nil {
return nil, err
}
return signedPld, nil
}

func signedPldToMsg(signedPld *ctrl.SignedPld) (*capnp.Message, error) {
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
return nil, err
}
root, err := proto.NewRootSignedCtrlPld(seg)
if err != nil {
return nil, err
}
if err := pogs.Insert(proto.SignedCtrlPld_TypeID, root.Struct, signedPld); err != nil {
return nil, err
}
return msg, nil
}
42 changes: 17 additions & 25 deletions go/lib/infra/messenger/quic_response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ func (rw *QUICResponseWriter) SendAckReply(ctx context.Context, msg *ack.Ack) er
if err != nil {
return err
}
signedCtrlPld, err := ctrlPld.SignedPld(infra.NullSigner)
if err != nil {
return err
}
return rw.ReplyWriter.WriteReply(&rpc.Reply{SignedPld: signedCtrlPld})
return rw.sendMessage(ctrlPld)
}

func (rw *QUICResponseWriter) SendTRCReply(ctx context.Context, msg *cert_mgmt.TRC) error {
Expand All @@ -62,11 +58,7 @@ func (rw *QUICResponseWriter) SendTRCReply(ctx context.Context, msg *cert_mgmt.T
if err != nil {
return err
}
signedCtrlPld, err := ctrlPld.SignedPld(infra.NullSigner)
if err != nil {
return err
}
return rw.ReplyWriter.WriteReply(&rpc.Reply{SignedPld: signedCtrlPld})
return rw.sendMessage(ctrlPld)
}

func (rw *QUICResponseWriter) SendCertChainReply(ctx context.Context, msg *cert_mgmt.Chain) error {
Expand All @@ -79,11 +71,7 @@ func (rw *QUICResponseWriter) SendCertChainReply(ctx context.Context, msg *cert_
if err != nil {
return err
}
signedCtrlPld, err := ctrlPld.SignedPld(infra.NullSigner)
if err != nil {
return err
}
return rw.ReplyWriter.WriteReply(&rpc.Reply{SignedPld: signedCtrlPld})
return rw.sendMessage(ctrlPld)
}

func (rw *QUICResponseWriter) SendChainIssueReply(ctx context.Context,
Expand All @@ -98,11 +86,7 @@ func (rw *QUICResponseWriter) SendChainIssueReply(ctx context.Context,
if err != nil {
return err
}
signedCtrlPld, err := ctrlPld.SignedPld(infra.NullSigner)
if err != nil {
return err
}
return rw.ReplyWriter.WriteReply(&rpc.Reply{SignedPld: signedCtrlPld})
return rw.sendMessage(ctrlPld)
}

func (rw *QUICResponseWriter) SendSegReply(ctx context.Context, msg *path_mgmt.SegReply) error {
Expand All @@ -115,11 +99,7 @@ func (rw *QUICResponseWriter) SendSegReply(ctx context.Context, msg *path_mgmt.S
if err != nil {
return err
}
signedCtrlPld, err := ctrlPld.SignedPld(infra.NullSigner)
if err != nil {
return err
}
return rw.ReplyWriter.WriteReply(&rpc.Reply{SignedPld: signedCtrlPld})
return rw.sendMessage(ctrlPld)
}

func (rw *QUICResponseWriter) SendIfStateInfoReply(ctx context.Context,
Expand All @@ -128,3 +108,15 @@ func (rw *QUICResponseWriter) SendIfStateInfoReply(ctx context.Context,
// FIXME(scrye): Use only UDP because the BR doesn't support QUIC.
return common.NewBasicError("IFStateInfos responses not supported in QUIC", nil)
}

func (rw *QUICResponseWriter) sendMessage(ctrlPld *ctrl.Pld) error {
signedCtrlPld, err := ctrlPld.SignedPld(infra.NullSigner)
if err != nil {
return err
}
msg, err := signedPldToMsg(signedCtrlPld)
if err != nil {
return err
}
return rw.ReplyWriter.WriteReply(&rpc.Reply{Message: msg})
}
5 changes: 3 additions & 2 deletions go/lib/infra/rpc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//go/lib/common:go_default_library",
"//go/lib/ctrl:go_default_library",
"//go/lib/log:go_default_library",
"//go/lib/snet:go_default_library",
"//go/proto:go_default_library",
"@com_github_lucas_clemente_quic_go//:go_default_library",
"@com_zombiezen_go_capnproto2//:go_default_library",
"@com_zombiezen_go_capnproto2//pogs:go_default_library",
],
)

Expand All @@ -28,6 +26,9 @@ go_test(
deps = [
"//go/lib/ctrl:go_default_library",
"//go/lib/xtest:go_default_library",
"//go/proto:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_zombiezen_go_capnproto2//:go_default_library",
"@com_zombiezen_go_capnproto2//pogs:go_default_library",
],
)
36 changes: 2 additions & 34 deletions go/lib/infra/rpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,15 @@ import (
"net"

capnp "zombiezen.com/go/capnproto2"
"zombiezen.com/go/capnproto2/pogs"

"github.com/scionproto/scion/go/lib/ctrl"
"github.com/scionproto/scion/go/proto"
)

type Request struct {
SignedPld *ctrl.SignedPld
Message *capnp.Message
// Address records the network address that sent the request. It will
// usually be used for logging.
Address net.Addr
}

type Reply struct {
SignedPld *ctrl.SignedPld
}

func messageToSignedPayload(msg *capnp.Message) (*ctrl.SignedPld, error) {
root, err := msg.RootPtr()
if err != nil {
return nil, err
}
signedPld := &ctrl.SignedPld{}
if err := pogs.Extract(signedPld, proto.SignedCtrlPld_TypeID, root.Struct()); err != nil {
return nil, err
}
return signedPld, nil
}

func signedPldToMessage(signedPld *ctrl.SignedPld) (*capnp.Message, error) {
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
return nil, err
}
root, err := proto.NewRootSignedCtrlPld(seg)
if err != nil {
return nil, err
}
if err := pogs.Insert(proto.SignedCtrlPld_TypeID, root.Struct, signedPld); err != nil {
return nil, err
}
return msg, nil

Message *capnp.Message
}
40 changes: 10 additions & 30 deletions go/lib/infra/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/proto"
)

const (
Expand Down Expand Up @@ -64,7 +65,7 @@ func (s *Server) ListenAndServe() error {
for {
session, err := s.listener.Accept()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
if strings.Contains(err.Error(), "server closed") {
return err
}
log.Warn("[quic] server accept error", "err", err)
Expand Down Expand Up @@ -108,18 +109,14 @@ func (s *Server) handleQUICSession(session quic.Session) error {
if err != nil {
return err
}
msg, err := capnp.NewDecoder(stream).Decode()
if err != nil {
return err
}
signedPld, err := messageToSignedPayload(msg)
msg, err := proto.SafeDecode(capnp.NewDecoder(stream))
if err != nil {
return err
}
rw := &replyWriter{stream: stream}
request := &Request{
SignedPld: signedPld,
Address: session.RemoteAddr(),
Message: msg,
Address: session.RemoteAddr(),
}
go func() {
defer log.LogPanicAndExit()
Expand Down Expand Up @@ -161,19 +158,11 @@ func (c *Client) Request(ctx context.Context, request *Request, address net.Addr
stream.CancelWrite(CtxTimedOutError)
}()

msg, err := signedPldToMessage(request.SignedPld)
if err != nil {
return nil, err
}
err = capnp.NewEncoder(stream).Encode(msg)
if err != nil {
return nil, err
}
msg, err = capnp.NewDecoder(stream).Decode()
err = capnp.NewEncoder(stream).Encode(request.Message)
if err != nil {
return nil, err
}
signedPld, err := messageToSignedPayload(msg)
msg, err := proto.SafeDecode(capnp.NewDecoder(stream))
if err != nil {
return nil, err
}
Expand All @@ -184,7 +173,7 @@ func (c *Client) Request(ctx context.Context, request *Request, address net.Addr
if err := session.Close(); err != nil {
return nil, err
}
return &Reply{SignedPld: signedPld}, nil
return &Reply{Message: msg}, nil
}

func (c *Client) sendRequest() error {
Expand Down Expand Up @@ -227,22 +216,13 @@ type replyWriter struct {
}

func (rw *replyWriter) WriteReply(reply *Reply) error {
msg, err := signedPldToMessage(reply.SignedPld)
if err != nil {
return err
}

err = capnp.NewEncoder(rw.stream).Encode(msg)
if err != nil {
if err := capnp.NewEncoder(rw.stream).Encode(reply.Message); err != nil {
return err
}

err = rw.stream.Close()
if err != nil {
if err := rw.stream.Close(); err != nil {
return err
}
return nil

}

func (rw *replyWriter) Close() error {
Expand Down
Loading