unix_domain_server_socket_posix.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SOCKET_UNIX_DOMAIN_SERVER_SOCKET_POSIX_H_
  5. #define NET_SOCKET_UNIX_DOMAIN_SERVER_SOCKET_POSIX_H_
  6. #include <stdint.h>
  7. #include <sys/types.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/callback.h"
  11. #include "build/build_config.h"
  12. #include "net/base/completion_once_callback.h"
  13. #include "net/base/net_export.h"
  14. #include "net/socket/server_socket.h"
  15. #include "net/socket/socket_descriptor.h"
  16. namespace net {
  17. class SocketPosix;
  18. // A server socket that uses unix domain socket as the transport layer.
  19. // Supports abstract namespaces on Linux and Android.
  20. class NET_EXPORT UnixDomainServerSocket : public ServerSocket {
  21. public:
  22. // Credentials of a peer process connected to the socket.
  23. struct NET_EXPORT Credentials {
  24. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
  25. BUILDFLAG(IS_FUCHSIA)
  26. // Linux and Fuchsia provide more information about the connected peer
  27. // than Windows/OS X. It's useful for permission-based authorization on
  28. // Android.
  29. pid_t process_id;
  30. #endif
  31. uid_t user_id;
  32. gid_t group_id;
  33. };
  34. // Callback that returns whether the already connected client, identified by
  35. // its credentials, is allowed to keep the connection open. Note that
  36. // the socket is closed immediately in case the callback returns false.
  37. using AuthCallback = base::RepeatingCallback<bool(const Credentials&)>;
  38. UnixDomainServerSocket(const AuthCallback& auth_callack,
  39. bool use_abstract_namespace);
  40. UnixDomainServerSocket(const UnixDomainServerSocket&) = delete;
  41. UnixDomainServerSocket& operator=(const UnixDomainServerSocket&) = delete;
  42. ~UnixDomainServerSocket() override;
  43. // Gets credentials of peer to check permissions.
  44. static bool GetPeerCredentials(SocketDescriptor socket_fd,
  45. Credentials* credentials);
  46. // ServerSocket implementation.
  47. int Listen(const IPEndPoint& address, int backlog) override;
  48. int ListenWithAddressAndPort(const std::string& address_string,
  49. uint16_t port,
  50. int backlog) override;
  51. int GetLocalAddress(IPEndPoint* address) const override;
  52. int Accept(std::unique_ptr<StreamSocket>* socket,
  53. CompletionOnceCallback callback) override;
  54. // Creates a server socket, binds it to the specified |socket_path| and
  55. // starts listening for incoming connections with the specified |backlog|.
  56. int BindAndListen(const std::string& socket_path, int backlog);
  57. // Accepts an incoming connection on |listen_socket_|, but passes back
  58. // a raw SocketDescriptor instead of a StreamSocket.
  59. int AcceptSocketDescriptor(SocketDescriptor* socket_descriptor,
  60. CompletionOnceCallback callback);
  61. private:
  62. int DoAccept();
  63. void AcceptCompleted(int rv);
  64. bool AuthenticateAndGetStreamSocket();
  65. void SetSocketResult(std::unique_ptr<SocketPosix> accepted_socket);
  66. void RunCallback(int rv);
  67. void CancelCallback();
  68. std::unique_ptr<SocketPosix> listen_socket_;
  69. const AuthCallback auth_callback_;
  70. CompletionOnceCallback callback_;
  71. const bool use_abstract_namespace_;
  72. std::unique_ptr<SocketPosix> accept_socket_;
  73. struct SocketDestination {
  74. // Non-null while a call to Accept is pending.
  75. std::unique_ptr<StreamSocket>* stream = nullptr;
  76. // Non-null while a call to AcceptSocketDescriptor is pending.
  77. SocketDescriptor* descriptor = nullptr;
  78. };
  79. SocketDestination out_socket_;
  80. };
  81. } // namespace net
  82. #endif // NET_SOCKET_UNIX_DOMAIN_SERVER_SOCKET_POSIX_H_