• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
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_CLIENT_SOCKET_POSIX_H_
6 #define NET_SOCKET_UNIX_DOMAIN_CLIENT_SOCKET_POSIX_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "net/base/completion_once_callback.h"
14 #include "net/base/net_export.h"
15 #include "net/log/net_log_with_source.h"
16 #include "net/socket/socket_descriptor.h"
17 #include "net/socket/stream_socket.h"
18 #include "net/traffic_annotation/network_traffic_annotation.h"
19 
20 namespace net {
21 
22 class SocketPosix;
23 
24 // A client socket that uses unix domain socket as the transport layer.
25 class NET_EXPORT UnixDomainClientSocket : public StreamSocket {
26  public:
27   // Builds a client socket with |socket_path|. The caller should call Connect()
28   // to connect to a server socket.
29   UnixDomainClientSocket(const std::string& socket_path,
30                          bool use_abstract_namespace);
31   // Builds a client socket with SocketPosix which is already connected.
32   // UnixDomainServerSocket uses this after it accepts a connection.
33   explicit UnixDomainClientSocket(std::unique_ptr<SocketPosix> socket);
34 
35   UnixDomainClientSocket(const UnixDomainClientSocket&) = delete;
36   UnixDomainClientSocket& operator=(const UnixDomainClientSocket&) = delete;
37 
38   ~UnixDomainClientSocket() override;
39 
40   // StreamSocket implementation.
41   int Connect(CompletionOnceCallback callback) override;
42   void Disconnect() override;
43   bool IsConnected() const override;
44   bool IsConnectedAndIdle() const override;
45   int GetPeerAddress(IPEndPoint* address) const override;
46   int GetLocalAddress(IPEndPoint* address) const override;
47   const NetLogWithSource& NetLog() const override;
48   bool WasEverUsed() const override;
49   bool WasAlpnNegotiated() const override;
50   NextProto GetNegotiatedProtocol() const override;
51   bool GetSSLInfo(SSLInfo* ssl_info) override;
52   int64_t GetTotalReceivedBytes() const override;
53   void ApplySocketTag(const SocketTag& tag) override;
54 
55   // Socket implementation.
56   int Read(IOBuffer* buf,
57            int buf_len,
58            CompletionOnceCallback callback) override;
59   int Write(IOBuffer* buf,
60             int buf_len,
61             CompletionOnceCallback callback,
62             const NetworkTrafficAnnotationTag& traffic_annotation) override;
63   int SetReceiveBufferSize(int32_t size) override;
64   int SetSendBufferSize(int32_t size) override;
65 
66   // Releases ownership of underlying SocketDescriptor to caller.
67   // Internal state is reset so that this object can be used again.
68   // Socket must be connected in order to release it.
69   SocketDescriptor ReleaseConnectedSocket();
70 
71  private:
72   const std::string socket_path_;
73   const bool use_abstract_namespace_;
74   std::unique_ptr<SocketPosix> socket_;
75   // This net log is just to comply StreamSocket::NetLog(). It throws away
76   // everything.
77   NetLogWithSource net_log_;
78 };
79 
80 }  // namespace net
81 
82 #endif  // NET_SOCKET_UNIX_DOMAIN_CLIENT_SOCKET_POSIX_H_
83