/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include "common/libs/fs/shared_fd.h" #include "common/libs/utils/result.h" namespace cuttlefish { struct ControlMessage { public: static ControlMessage FromRaw(const cmsghdr*); static Result FromFileDescriptors( const std::vector&); #ifdef __linux__ static ControlMessage FromCredentials(const ucred&); #endif ControlMessage(const ControlMessage&) = delete; ControlMessage(ControlMessage&&); ~ControlMessage(); ControlMessage& operator=(const ControlMessage&) = delete; ControlMessage& operator=(ControlMessage&&); const cmsghdr* Raw() const; #ifdef __linux__ bool IsCredentials() const; Result AsCredentials() const; #endif bool IsFileDescriptors() const; Result> AsSharedFDs() const; private: friend class UnixMessageSocket; ControlMessage() = default; cmsghdr* Raw(); std::vector data_; std::vector fds_; }; struct UnixSocketMessage { std::vector data; std::vector control; bool HasFileDescriptors(); Result> FileDescriptors(); #ifdef __linux__ bool HasCredentials(); Result Credentials(); #endif }; class UnixMessageSocket { public: UnixMessageSocket(SharedFD); [[nodiscard]] Result WriteMessage(const UnixSocketMessage&); Result ReadMessage(); #ifdef __linux__ [[nodiscard]] Result EnableCredentials(bool); #endif private: SharedFD socket_; std::uint32_t max_message_size_; }; } // namespace cuttlefish