OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef NET_SOCKET_UNIX_DOMAIN_SERVER_SOCKET_POSIX_H_ |
| 6 #define NET_SOCKET_UNIX_DOMAIN_SERVER_SOCKET_POSIX_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "net/base/net_export.h" |
| 12 #include "net/socket/server_socket.h" |
| 13 #include "net/socket/socket_descriptor.h" |
| 14 |
| 15 namespace net { |
| 16 |
| 17 // Unix Domain Server Socket Implementation. Supports abstract namespaces on |
| 18 // Linux. |
| 19 class NET_EXPORT UnixDomainServerSocket |
| 20 : public ServerSocket, |
| 21 public base::MessageLoopForIO::Watcher { |
| 22 public: |
| 23 // Callback that returns whether the already connected client, identified by |
| 24 // its process |user_id| and |group_id|, is allowed to keep the connection |
| 25 // open. Note that the socket is closed immediately in case the callback |
| 26 // returns false. |
| 27 typedef base::Callback<bool (uid_t user_id, gid_t group_id)> AuthCallback; |
| 28 |
| 29 UnixDomainServerSocket(const AuthCallback& auth_callack, |
| 30 bool use_abstract_namespace); |
| 31 virtual ~UnixDomainServerSocket(); |
| 32 |
| 33 // ServerSocket implementation. |
| 34 virtual int Listen(const IPEndPoint& address, int backlog) OVERRIDE; |
| 35 virtual int ListenWithAddressAndPort(const std::string& unix_domain_path, |
| 36 int port_unused, |
| 37 int backlog) OVERRIDE; |
| 38 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; |
| 39 virtual int Accept(scoped_ptr<StreamSocket>* socket, |
| 40 const CompletionCallback& callback) OVERRIDE; |
| 41 |
| 42 // base::MessageLoopForIO::Watcher methods. |
| 43 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 44 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| 45 |
| 46 // Creates a socket and bind to |socket_path|. |
| 47 // Only for android or for linux, |use_abstract_namespace| can be true. |
| 48 // Otherwise, returns ERR_ADDRESS_INVALID. |
| 49 static int CreateAndBind(const std::string& socket_path, |
| 50 bool use_abstract_namespace, |
| 51 SocketDescriptor* socket_fd); |
| 52 // Gets UID and GID of peer to check permissions. |
| 53 static bool GetPeerIds(SocketDescriptor socket_fd, |
| 54 uid_t* user_id, |
| 55 gid_t* group_id); |
| 56 private: |
| 57 void Close(); |
| 58 int DoAccept(scoped_ptr<StreamSocket>* socket); |
| 59 void DidCompleteAccept(); |
| 60 |
| 61 SocketDescriptor socket_fd_; |
| 62 const AuthCallback auth_callback_; |
| 63 const bool use_abstract_namespace_; |
| 64 |
| 65 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_; |
| 66 scoped_ptr<StreamSocket>* accept_socket_; |
| 67 CompletionCallback accept_callback_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocket); |
| 70 }; |
| 71 |
| 72 } // namespace net |
| 73 |
| 74 #endif // NET_SOCKET_UNIX_DOMAIN_SOCKET_POSIX_H_ |
OLD | NEW |