blob: 1526a2ef58469f902ba67d563a54dfe761b117a6 [file] [log] [blame]
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ARC_VM_BOOT_NOTIFICATION_SERVER_UTIL_H_
#define ARC_VM_BOOT_NOTIFICATION_SERVER_UTIL_H_
#include <sys/socket.h>
#include <string>
#include <utility>
#include <base/files/scoped_file.h>
#include <base/optional.h>
// Returns the length of the corresponding sockaddr_XX structure for the given
// socket family.
socklen_t GetSockLen(sa_family_t family);
// Creates a streaming socket bound to addr and starts listening on the
// socket. If successful, returns a socket in the listening state.
base::ScopedFD StartListening(sockaddr* addr);
// Waits for a client to connect to fd and returns the connected socket.
// client_addr will be populated with the address of the client.
base::ScopedFD WaitForClientConnect(int fd);
// Reads from fd until EOF (read() returns 0). If able to read successfully,
// returns read data as a string. Else, returns empty optional.
base::Optional<std::string> ReadFD(int fd);
// Locates the CID key in the given string and returns the CID value if it
// exists, and the |props| string with the CID line removed. Else, returns empty
// optional. This function expects the CID line to be at the beginning of the
// string.
base::Optional<std::pair<unsigned int, std::string>> ExtractCidValue(
const std::string& props);
// Returns the CID of the peer that is connected to |fd|, which must be a
// connected VSOCK socket.
base::Optional<unsigned int> GetPeerCid(int fd);
#endif // ARC_VM_BOOT_NOTIFICATION_SERVER_UTIL_H_