github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/tao/fd_message_channel.h (about)

     1  //  File: fd_message_channel.h
     2  //  Author: Tom Roeder <tmroeder@google.com>
     3  //
     4  //  Description: A MessageChannel that communicates over Unix file descriptors.
     5  //
     6  //  Copyright (c) 2013, Google Inc.  All rights reserved.
     7  //
     8  // Licensed under the Apache License, Version 2.0 (the "License");
     9  // you may not use this file except in compliance with the License.
    10  // You may obtain a copy of the License at
    11  //
    12  //     http://www.apache.org/licenses/LICENSE-2.0
    13  //
    14  // Unless required by applicable law or agreed to in writing, software
    15  // distributed under the License is distributed on an "AS IS" BASIS,
    16  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  // See the License for the specific language governing permissions and
    18  // limitations under the License.
    19  #ifndef TAO_FD_MESSAGE_CHANNEL_H_
    20  #define TAO_FD_MESSAGE_CHANNEL_H_
    21  
    22  #include <list>
    23  #include <string>
    24  
    25  #include "tao/message_channel.h"
    26  
    27  #include "tao/util.h"
    28  
    29  namespace tao {
    30  /// A MessageChannel that communicates with a remote endpoint using a pair of
    31  /// file descriptors. One file descriptor is used for sending messages, the
    32  /// other for receiving messages. The descriptors can be the same. On Close() or
    33  /// object destruction the file descriptors will be closed.
    34  class FDMessageChannel : public MessageChannel {
    35   public:
    36    /// Construct FDMessageChannel.
    37    /// @param readfd The file descriptor to use for receiving messages.
    38    /// @param writefd The file descriptor to use for sending messages.
    39    FDMessageChannel(int readfd, int writefd)
    40        : readfd_(readfd), writefd_(writefd) {}
    41  
    42    virtual ~FDMessageChannel() { FDClose(); }
    43  
    44    /// These methods have the same semantics as MessageChannel.
    45    /// @{
    46    virtual void Close() { FDClose(); }
    47    virtual bool IsClosed() const { return (readfd_ < 0 || writefd_ < 0); }
    48    virtual bool SendData(const void *buffer, size_t buffer_len);
    49    virtual bool SerializeToString(string *params) const;
    50    /// @}
    51  
    52    /// Attempt to deserialize a channel.
    53    /// @param params Channel parameters from SerializeToString().
    54    static FDMessageChannel *DeserializeFromString(const string &params);
    55  
    56    /// Get a list of file descriptors that should be kept open across fork/exec.
    57    /// @param[out] keep_open The list of file descriptors to preserve.
    58    virtual bool GetFileDescriptors(list<int> *keep_open) const;
    59  
    60    /// Get the read file descriptor, e.g. to use for select().
    61    virtual int GetReadFileDescriptor() { return readfd_; }
    62  
    63    /// Get the write file descriptor.
    64    virtual int GetWriteFileDescriptor() { return writefd_; }
    65  
    66   protected:
    67    /// File descriptor for writing to host Tao.
    68    int readfd_;
    69  
    70    /// File descriptor for reading from host Tao.
    71    int writefd_;
    72  
    73    /// These methods have the same semantics as MessageChannel.
    74    /// @{
    75    virtual bool ReceivePartialData(void *buffer, size_t max_recv_len,
    76                                    size_t *recv_len, bool *eof);
    77    /// @}
    78  
    79    /// A non-virtual version of Close for use in destructor.
    80    void FDClose();
    81  
    82   private:
    83    DISALLOW_COPY_AND_ASSIGN(FDMessageChannel);
    84  };
    85  
    86  }  // namespace tao
    87  
    88  #endif  // TAO_FD_MESSAGE_CHANNEL_H_