#ifndef NET_SOCKET_UNIX_DOMAIN_SERVER_SOCKET_POSIX_H_
#define NET_SOCKET_UNIX_DOMAIN_SERVER_SOCKET_POSIX_H_
#include <stdint.h>
#include <sys/types.h>
#include <memory>
#include <string>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_export.h"
#include "net/socket/server_socket.h"
#include "net/socket/socket_descriptor.h"
namespace net {
class SocketPosix;
class NET_EXPORT UnixDomainServerSocket : public ServerSocket {
public:
struct NET_EXPORT Credentials {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_OHOS)
pid_t process_id;
#endif
uid_t user_id;
gid_t group_id;
};
using AuthCallback = base::RepeatingCallback<bool(const Credentials&)>;
UnixDomainServerSocket(const AuthCallback& auth_callack,
bool use_abstract_namespace);
UnixDomainServerSocket(const UnixDomainServerSocket&) = delete;
UnixDomainServerSocket& operator=(const UnixDomainServerSocket&) = delete;
~UnixDomainServerSocket() override;
static bool GetPeerCredentials(SocketDescriptor socket_fd,
Credentials* credentials);
int Listen(const IPEndPoint& address,
int backlog,
absl::optional<bool> ipv6_only) override;
int ListenWithAddressAndPort(const std::string& address_string,
uint16_t port,
int backlog) override;
int GetLocalAddress(IPEndPoint* address) const override;
int Accept(std::unique_ptr<StreamSocket>* socket,
CompletionOnceCallback callback) override;
int BindAndListen(const std::string& socket_path, int backlog);
int AcceptSocketDescriptor(SocketDescriptor* socket_descriptor,
CompletionOnceCallback callback);
private:
int DoAccept();
void AcceptCompleted(int rv);
bool AuthenticateAndGetStreamSocket();
void SetSocketResult(std::unique_ptr<SocketPosix> accepted_socket);
void RunCallback(int rv);
void CancelCallback();
std::unique_ptr<SocketPosix> listen_socket_;
const AuthCallback auth_callback_;
CompletionOnceCallback callback_;
const bool use_abstract_namespace_;
std::unique_ptr<SocketPosix> accept_socket_;
struct SocketDestination {
raw_ptr<std::unique_ptr<StreamSocket>> stream = nullptr;
raw_ptr<SocketDescriptor> descriptor = nullptr;
};
SocketDestination out_socket_;
};
}
#endif