Chris@47: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@47: // Licensed under the MIT License: Chris@47: // Chris@47: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@47: // of this software and associated documentation files (the "Software"), to deal Chris@47: // in the Software without restriction, including without limitation the rights Chris@47: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@47: // copies of the Software, and to permit persons to whom the Software is Chris@47: // furnished to do so, subject to the following conditions: Chris@47: // Chris@47: // The above copyright notice and this permission notice shall be included in Chris@47: // all copies or substantial portions of the Software. Chris@47: // Chris@47: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@47: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@47: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@47: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@47: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@47: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@47: // THE SOFTWARE. Chris@47: Chris@47: #ifndef CAPNP_RPC_H_ Chris@47: #define CAPNP_RPC_H_ Chris@47: Chris@47: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@47: #pragma GCC system_header Chris@47: #endif Chris@47: Chris@47: #include "capability.h" Chris@47: #include "rpc-prelude.h" Chris@47: Chris@47: namespace capnp { Chris@47: Chris@47: template Chris@47: class VatNetwork; Chris@47: template Chris@47: class SturdyRefRestorer; Chris@47: Chris@47: template Chris@47: class BootstrapFactory: public _::BootstrapFactoryBase { Chris@47: // Interface that constructs per-client bootstrap interfaces. Use this if you want each client Chris@47: // who connects to see a different bootstrap interface based on their (authenticated) VatId. Chris@47: // This allows an application to bootstrap off of the authentication performed at the VatNetwork Chris@47: // level. (Typically VatId is some sort of public key.) Chris@47: // Chris@47: // This is only useful for multi-party networks. For TwoPartyVatNetwork, there's no reason to Chris@47: // use a BootstrapFactory; just specify a single bootstrap capability in this case. Chris@47: Chris@47: public: Chris@47: virtual Capability::Client createFor(typename VatId::Reader clientId) = 0; Chris@47: // Create a bootstrap capability appropriate for exposing to the given client. VatNetwork will Chris@47: // have authenticated the client VatId before this is called. Chris@47: Chris@47: private: Chris@47: Capability::Client baseCreateFor(AnyStruct::Reader clientId) override; Chris@47: }; Chris@47: Chris@47: template Chris@47: class RpcSystem: public _::RpcSystemBase { Chris@47: // Represents the RPC system, which is the portal to objects available on the network. Chris@47: // Chris@47: // The RPC implementation sits on top of an implementation of `VatNetwork`. The `VatNetwork` Chris@47: // determines how to form connections between vats -- specifically, two-way, private, reliable, Chris@47: // sequenced datagram connections. The RPC implementation determines how to use such connections Chris@47: // to manage object references and make method calls. Chris@47: // Chris@47: // See `makeRpcServer()` and `makeRpcClient()` below for convenient syntax for setting up an Chris@47: // `RpcSystem` given a `VatNetwork`. Chris@47: // Chris@47: // See `ez-rpc.h` for an even simpler interface for setting up RPC in a typical two-party Chris@47: // client/server scenario. Chris@47: Chris@47: public: Chris@47: template Chris@47: RpcSystem( Chris@47: VatNetwork& network, Chris@47: kj::Maybe bootstrapInterface, Chris@47: kj::Maybe::Client> gateway = nullptr); Chris@47: Chris@47: template Chris@47: RpcSystem( Chris@47: VatNetwork& network, Chris@47: BootstrapFactory& bootstrapFactory, Chris@47: kj::Maybe::Client> gateway = nullptr); Chris@47: Chris@47: template Chris@47: RpcSystem( Chris@47: VatNetwork& network, Chris@47: SturdyRefRestorer& restorer); Chris@47: Chris@47: RpcSystem(RpcSystem&& other) = default; Chris@47: Chris@47: Capability::Client bootstrap(typename VatId::Reader vatId); Chris@47: // Connect to the given vat and return its bootstrap interface. Chris@47: Chris@47: Capability::Client restore(typename VatId::Reader hostId, AnyPointer::Reader objectId) Chris@47: KJ_DEPRECATED("Please transition to using a bootstrap interface instead."); Chris@47: // ** DEPRECATED ** Chris@47: // Chris@47: // Restores the given SturdyRef from the network and return the capability representing it. Chris@47: // Chris@47: // `hostId` identifies the host from which to request the ref, in the format specified by the Chris@47: // `VatNetwork` in use. `objectId` is the object ID in whatever format is expected by said host. Chris@47: // Chris@47: // This method will be removed in a future version of Cap'n Proto. Instead, please transition Chris@47: // to using bootstrap(), which is equivalent to calling restore() with a null `objectId`. Chris@47: // You may emulate the old concept of object IDs by exporting a bootstrap interface which has Chris@47: // methods that can be used to obtain other capabilities by ID. Chris@47: Chris@47: void setFlowLimit(size_t words); Chris@47: // Sets the incoming call flow limit. If more than `words` worth of call messages have not yet Chris@47: // received responses, the RpcSystem will not read further messages from the stream. This can be Chris@47: // used as a crude way to prevent a resource exhaustion attack (or bug) in which a peer makes an Chris@47: // excessive number of simultaneous calls that consume the receiver's RAM. Chris@47: // Chris@47: // There are some caveats. When over the flow limit, all messages are blocked, including returns. Chris@47: // If the outstanding calls are themselves waiting on calls going in the opposite direction, the Chris@47: // flow limit may prevent those calls from completing, leading to deadlock. However, a Chris@47: // sufficiently high limit should make this unlikely. Chris@47: // Chris@47: // Note that a call's parameter size counts against the flow limit until the call returns, even Chris@47: // if the recipient calls releaseParams() to free the parameter memory early. This is because Chris@47: // releaseParams() may simply indicate that the parameters have been forwarded to another Chris@47: // machine, but are still in-memory there. For illustration, say that Alice made a call to Bob Chris@47: // who forwarded the call to Carol. Bob has imposed a flow limit on Alice. Alice's calls are Chris@47: // being forwarded to Carol, so Bob never keeps the parameters in-memory for more than a brief Chris@47: // period. However, the flow limit counts all calls that haven't returned, even if Bob has Chris@47: // already freed the memory they consumed. You might argue that the right solution here is Chris@47: // instead for Carol to impose her own flow limit on Bob. This has a serious problem, though: Chris@47: // Bob might be forwarding requests to Carol on behalf of many different parties, not just Alice. Chris@47: // If Alice can pump enough data to hit the Bob -> Carol flow limit, then those other parties Chris@47: // will be disrupted. Thus, we can only really impose the limit on the Alice -> Bob link, which Chris@47: // only affects Alice. We need that one flow limit to limit Alice's impact on the whole system, Chris@47: // so it has to count all in-flight calls. Chris@47: // Chris@47: // In Sandstorm, flow limits are imposed by the supervisor on calls coming out of a grain, in Chris@47: // order to prevent a grain from inundating the system with in-flight calls. In practice, the Chris@47: // main time this happens is when a grain is pushing a large file download and doesn't implement Chris@47: // proper cooperative flow control. Chris@47: }; Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: Capability::Client bootstrapInterface); Chris@47: // Make an RPC server. Typical usage (e.g. in a main() function): Chris@47: // Chris@47: // MyEventLoop eventLoop; Chris@47: // kj::WaitScope waitScope(eventLoop); Chris@47: // MyNetwork network; Chris@47: // MyMainInterface::Client bootstrap = makeMain(); Chris@47: // auto server = makeRpcServer(network, bootstrap); Chris@47: // kj::NEVER_DONE.wait(waitScope); // run forever Chris@47: // Chris@47: // See also ez-rpc.h, which has simpler instructions for the common case of a two-party Chris@47: // client-server RPC connection. Chris@47: Chris@47: template , Chris@47: typename ExternalRef = _::ExternalRefFromRealmGatewayClient> Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: Capability::Client bootstrapInterface, RealmGatewayClient gateway); Chris@47: // Make an RPC server for a VatNetwork that resides in a different realm from the application. Chris@47: // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format Chris@47: // and the network's ("external") format. Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: BootstrapFactory& bootstrapFactory); Chris@47: // Make an RPC server that can serve different bootstrap interfaces to different clients via a Chris@47: // BootstrapInterface. Chris@47: Chris@47: template , Chris@47: typename ExternalRef = _::ExternalRefFromRealmGatewayClient> Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: BootstrapFactory& bootstrapFactory, RealmGatewayClient gateway); Chris@47: // Make an RPC server that can serve different bootstrap interfaces to different clients via a Chris@47: // BootstrapInterface and communicates with a different realm than the application is in via a Chris@47: // RealmGateway. Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: SturdyRefRestorer& restorer) Chris@47: KJ_DEPRECATED("Please transition to using a bootstrap interface instead."); Chris@47: // ** DEPRECATED ** Chris@47: // Chris@47: // Create an RPC server which exports multiple main interfaces by object ID. The `restorer` object Chris@47: // can be used to look up objects by ID. Chris@47: // Chris@47: // Please transition to exporting only one interface, which is known as the "bootstrap" interface. Chris@47: // For backwards-compatibility with old clients, continue to implement SturdyRefRestorer, but Chris@47: // return the new bootstrap interface when the request object ID is null. When new clients connect Chris@47: // and request the bootstrap interface, they will get that interface. Eventually, once all clients Chris@47: // are updated to request only the bootstrap interface, stop implementing SturdyRefRestorer and Chris@47: // switch to passing the bootstrap capability itself as the second parameter to `makeRpcServer()`. Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcClient( Chris@47: VatNetwork& network); Chris@47: // Make an RPC client. Typical usage (e.g. in a main() function): Chris@47: // Chris@47: // MyEventLoop eventLoop; Chris@47: // kj::WaitScope waitScope(eventLoop); Chris@47: // MyNetwork network; Chris@47: // auto client = makeRpcClient(network); Chris@47: // MyCapability::Client cap = client.restore(hostId, objId).castAs(); Chris@47: // auto response = cap.fooRequest().send().wait(waitScope); Chris@47: // handleMyResponse(response); Chris@47: // Chris@47: // See also ez-rpc.h, which has simpler instructions for the common case of a two-party Chris@47: // client-server RPC connection. Chris@47: Chris@47: template , Chris@47: typename ExternalRef = _::ExternalRefFromRealmGatewayClient> Chris@47: RpcSystem makeRpcClient( Chris@47: VatNetwork& network, Chris@47: RealmGatewayClient gateway); Chris@47: // Make an RPC client for a VatNetwork that resides in a different realm from the application. Chris@47: // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format Chris@47: // and the network's ("external") format. Chris@47: Chris@47: template Chris@47: class SturdyRefRestorer: public _::SturdyRefRestorerBase { Chris@47: // ** DEPRECATED ** Chris@47: // Chris@47: // In Cap'n Proto 0.4.x, applications could export multiple main interfaces identified by Chris@47: // object IDs. The callback used to map object IDs to objects was `SturdyRefRestorer`, as we Chris@47: // imagined this would eventually be used for restoring SturdyRefs as well. In practice, it was Chris@47: // never used for real SturdyRefs, only for exporting singleton objects under well-known names. Chris@47: // Chris@47: // The new preferred strategy is to export only a _single_ such interface, called the Chris@47: // "bootstrap interface". That interface can itself have methods for obtaining other objects, of Chris@47: // course, but that is up to the app. `SturdyRefRestorer` exists for backwards-compatibility. Chris@47: // Chris@47: // Hint: Use SturdyRefRestorer to define a server that exports services under Chris@47: // string names. Chris@47: Chris@47: public: Chris@47: virtual Capability::Client restore(typename SturdyRefObjectId::Reader ref) Chris@47: KJ_DEPRECATED( Chris@47: "Please transition to using bootstrap interfaces instead of SturdyRefRestorer.") = 0; Chris@47: // Restore the given object, returning a capability representing it. Chris@47: Chris@47: private: Chris@47: Capability::Client baseRestore(AnyPointer::Reader ref) override final; Chris@47: }; Chris@47: Chris@47: // ======================================================================================= Chris@47: // VatNetwork Chris@47: Chris@47: class OutgoingRpcMessage { Chris@47: // A message to be sent by a `VatNetwork`. Chris@47: Chris@47: public: Chris@47: virtual AnyPointer::Builder getBody() = 0; Chris@47: // Get the message body, which the caller may fill in any way it wants. (The standard RPC Chris@47: // implementation initializes it as a Message as defined in rpc.capnp.) Chris@47: Chris@47: virtual void send() = 0; Chris@47: // Send the message, or at least put it in a queue to be sent later. Note that the builder Chris@47: // returned by `getBody()` remains valid at least until the `OutgoingRpcMessage` is destroyed. Chris@47: }; Chris@47: Chris@47: class IncomingRpcMessage { Chris@47: // A message received from a `VatNetwork`. Chris@47: Chris@47: public: Chris@47: virtual AnyPointer::Reader getBody() = 0; Chris@47: // Get the message body, to be interpreted by the caller. (The standard RPC implementation Chris@47: // interprets it as a Message as defined in rpc.capnp.) Chris@47: }; Chris@47: Chris@47: template Chris@47: class VatNetwork: public _::VatNetworkBase { Chris@47: // Cap'n Proto RPC operates between vats, where a "vat" is some sort of host of objects. Chris@47: // Typically one Cap'n Proto process (in the Unix sense) is one vat. The RPC system is what Chris@47: // allows calls between objects hosted in different vats. Chris@47: // Chris@47: // The RPC implementation sits on top of an implementation of `VatNetwork`. The `VatNetwork` Chris@47: // determines how to form connections between vats -- specifically, two-way, private, reliable, Chris@47: // sequenced datagram connections. The RPC implementation determines how to use such connections Chris@47: // to manage object references and make method calls. Chris@47: // Chris@47: // The most common implementation of VatNetwork is TwoPartyVatNetwork (rpc-twoparty.h). Most Chris@47: // simple client-server apps will want to use it. (You may even want to use the EZ RPC Chris@47: // interfaces in `ez-rpc.h` and avoid all of this.) Chris@47: // Chris@47: // TODO(someday): Provide a standard implementation for the public internet. Chris@47: Chris@47: public: Chris@47: class Connection; Chris@47: Chris@47: struct ConnectionAndProvisionId { Chris@47: // Result of connecting to a vat introduced by another vat. Chris@47: Chris@47: kj::Own connection; Chris@47: // Connection to the new vat. Chris@47: Chris@47: kj::Own firstMessage; Chris@47: // An already-allocated `OutgoingRpcMessage` associated with `connection`. The RPC system will Chris@47: // construct this as an `Accept` message and send it. Chris@47: Chris@47: Orphan provisionId; Chris@47: // A `ProvisionId` already allocated inside `firstMessage`, which the RPC system will use to Chris@47: // build the `Accept` message. Chris@47: }; Chris@47: Chris@47: class Connection: public _::VatNetworkBase::Connection { Chris@47: // A two-way RPC connection. Chris@47: // Chris@47: // This object may represent a connection that doesn't exist yet, but is expected to exist Chris@47: // in the future. In this case, sent messages will automatically be queued and sent once the Chris@47: // connection is ready, so that the caller doesn't need to know the difference. Chris@47: Chris@47: public: Chris@47: // Level 0 features ---------------------------------------------- Chris@47: Chris@47: virtual typename VatId::Reader getPeerVatId() = 0; Chris@47: // Returns the connected vat's authenticated VatId. It is the VatNetwork's responsibility to Chris@47: // authenticate this, so that the caller can be assured that they are really talking to the Chris@47: // identified vat and not an imposter. Chris@47: Chris@47: virtual kj::Own newOutgoingMessage(uint firstSegmentWordSize) override = 0; Chris@47: // Allocate a new message to be sent on this connection. Chris@47: // Chris@47: // If `firstSegmentWordSize` is non-zero, it should be treated as a hint suggesting how large Chris@47: // to make the first segment. This is entirely a hint and the connection may adjust it up or Chris@47: // down. If it is zero, the connection should choose the size itself. Chris@47: Chris@47: virtual kj::Promise>> receiveIncomingMessage() override = 0; Chris@47: // Wait for a message to be received and return it. If the read stream cleanly terminates, Chris@47: // return null. If any other problem occurs, throw an exception. Chris@47: Chris@47: virtual kj::Promise shutdown() override KJ_WARN_UNUSED_RESULT = 0; Chris@47: // Waits until all outgoing messages have been sent, then shuts down the outgoing stream. The Chris@47: // returned promise resolves after shutdown is complete. Chris@47: Chris@47: private: Chris@47: AnyStruct::Reader baseGetPeerVatId() override; Chris@47: }; Chris@47: Chris@47: // Level 0 features ------------------------------------------------ Chris@47: Chris@47: virtual kj::Maybe> connect(typename VatId::Reader hostId) = 0; Chris@47: // Connect to a VatId. Note that this method immediately returns a `Connection`, even Chris@47: // if the network connection has not yet been established. Messages can be queued to this Chris@47: // connection and will be delivered once it is open. The caller must attempt to read from the Chris@47: // connection to verify that it actually succeeded; the read will fail if the connection Chris@47: // couldn't be opened. Some network implementations may actually start sending messages before Chris@47: // hearing back from the server at all, to avoid a round trip. Chris@47: // Chris@47: // Returns nullptr if `hostId` refers to the local host. Chris@47: Chris@47: virtual kj::Promise> accept() = 0; Chris@47: // Wait for the next incoming connection and return it. Chris@47: Chris@47: // Level 4 features ------------------------------------------------ Chris@47: // TODO(someday) Chris@47: Chris@47: private: Chris@47: kj::Maybe> Chris@47: baseConnect(AnyStruct::Reader hostId) override final; Chris@47: kj::Promise> baseAccept() override final; Chris@47: }; Chris@47: Chris@47: // ======================================================================================= Chris@47: // *************************************************************************************** Chris@47: // Inline implementation details start here Chris@47: // *************************************************************************************** Chris@47: // ======================================================================================= Chris@47: Chris@47: template Chris@47: Capability::Client BootstrapFactory::baseCreateFor(AnyStruct::Reader clientId) { Chris@47: return createFor(clientId.as()); Chris@47: } Chris@47: Chris@47: template Chris@47: kj::Maybe> Chris@47: VatNetwork:: Chris@47: baseConnect(AnyStruct::Reader ref) { Chris@47: auto maybe = connect(ref.as()); Chris@47: return maybe.map([](kj::Own& conn) -> kj::Own<_::VatNetworkBase::Connection> { Chris@47: return kj::mv(conn); Chris@47: }); Chris@47: } Chris@47: Chris@47: template Chris@47: kj::Promise> Chris@47: VatNetwork::baseAccept() { Chris@47: return accept().then( Chris@47: [](kj::Own&& connection) -> kj::Own<_::VatNetworkBase::Connection> { Chris@47: return kj::mv(connection); Chris@47: }); Chris@47: } Chris@47: Chris@47: template Chris@47: AnyStruct::Reader VatNetwork< Chris@47: SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>:: Chris@47: Connection::baseGetPeerVatId() { Chris@47: return getPeerVatId(); Chris@47: } Chris@47: Chris@47: template Chris@47: Capability::Client SturdyRefRestorer::baseRestore(AnyPointer::Reader ref) { Chris@47: #pragma GCC diagnostic push Chris@47: #pragma GCC diagnostic ignored "-Wdeprecated-declarations" Chris@47: return restore(ref.getAs()); Chris@47: #pragma GCC diagnostic pop Chris@47: } Chris@47: Chris@47: template Chris@47: template Chris@47: RpcSystem::RpcSystem( Chris@47: VatNetwork& network, Chris@47: kj::Maybe bootstrap, Chris@47: kj::Maybe::Client> gateway) Chris@47: : _::RpcSystemBase(network, kj::mv(bootstrap), kj::mv(gateway)) {} Chris@47: Chris@47: template Chris@47: template Chris@47: RpcSystem::RpcSystem( Chris@47: VatNetwork& network, Chris@47: BootstrapFactory& bootstrapFactory, Chris@47: kj::Maybe::Client> gateway) Chris@47: : _::RpcSystemBase(network, bootstrapFactory, kj::mv(gateway)) {} Chris@47: Chris@47: template Chris@47: template Chris@47: RpcSystem::RpcSystem( Chris@47: VatNetwork& network, Chris@47: SturdyRefRestorer& restorer) Chris@47: : _::RpcSystemBase(network, restorer) {} Chris@47: Chris@47: template Chris@47: Capability::Client RpcSystem::bootstrap(typename VatId::Reader vatId) { Chris@47: return baseBootstrap(_::PointerHelpers::getInternalReader(vatId)); Chris@47: } Chris@47: Chris@47: template Chris@47: Capability::Client RpcSystem::restore( Chris@47: typename VatId::Reader hostId, AnyPointer::Reader objectId) { Chris@47: return baseRestore(_::PointerHelpers::getInternalReader(hostId), objectId); Chris@47: } Chris@47: Chris@47: template Chris@47: inline void RpcSystem::setFlowLimit(size_t words) { Chris@47: baseSetFlowLimit(words); Chris@47: } Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: Capability::Client bootstrapInterface) { Chris@47: return RpcSystem(network, kj::mv(bootstrapInterface)); Chris@47: } Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: Capability::Client bootstrapInterface, RealmGatewayClient gateway) { Chris@47: return RpcSystem(network, kj::mv(bootstrapInterface), Chris@47: gateway.template castAs>()); Chris@47: } Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: BootstrapFactory& bootstrapFactory) { Chris@47: return RpcSystem(network, bootstrapFactory); Chris@47: } Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: BootstrapFactory& bootstrapFactory, RealmGatewayClient gateway) { Chris@47: return RpcSystem(network, bootstrapFactory, gateway.template castAs>()); Chris@47: } Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcServer( Chris@47: VatNetwork& network, Chris@47: SturdyRefRestorer& restorer) { Chris@47: return RpcSystem(network, restorer); Chris@47: } Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcClient( Chris@47: VatNetwork& network) { Chris@47: return RpcSystem(network, nullptr); Chris@47: } Chris@47: Chris@47: template Chris@47: RpcSystem makeRpcClient( Chris@47: VatNetwork& network, Chris@47: RealmGatewayClient gateway) { Chris@47: return RpcSystem(network, nullptr, gateway.template castAs>()); Chris@47: } Chris@47: Chris@47: } // namespace capnp Chris@47: Chris@47: #endif // CAPNP_RPC_H_