• 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 #include "net/socket/unix_domain_client_socket_posix.h"
6 
7 #include <sys/socket.h>
8 #include <sys/un.h>
9 
10 #include <memory>
11 #include <utility>
12 
13 #include "base/check_op.h"
14 #include "base/notreached.h"
15 #include "build/build_config.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/sockaddr_storage.h"
18 #include "net/base/sockaddr_util_posix.h"
19 #include "net/socket/socket_posix.h"
20 #include "net/traffic_annotation/network_traffic_annotation.h"
21 
22 namespace net {
23 
UnixDomainClientSocket(const std::string & socket_path,bool use_abstract_namespace)24 UnixDomainClientSocket::UnixDomainClientSocket(const std::string& socket_path,
25                                                bool use_abstract_namespace)
26     : socket_path_(socket_path),
27       use_abstract_namespace_(use_abstract_namespace) {
28 }
29 
UnixDomainClientSocket(std::unique_ptr<SocketPosix> socket)30 UnixDomainClientSocket::UnixDomainClientSocket(
31     std::unique_ptr<SocketPosix> socket)
32     : use_abstract_namespace_(false), socket_(std::move(socket)) {}
33 
~UnixDomainClientSocket()34 UnixDomainClientSocket::~UnixDomainClientSocket() {
35   Disconnect();
36 }
37 
Connect(CompletionOnceCallback callback)38 int UnixDomainClientSocket::Connect(CompletionOnceCallback callback) {
39   if (IsConnected())
40     return OK;
41 
42   SockaddrStorage address;
43   if (!FillUnixAddress(socket_path_, use_abstract_namespace_, &address))
44     return ERR_ADDRESS_INVALID;
45 
46   socket_ = std::make_unique<SocketPosix>();
47   int rv = socket_->Open(AF_UNIX);
48   DCHECK_NE(ERR_IO_PENDING, rv);
49   if (rv != OK)
50     return rv;
51 
52   return socket_->Connect(address, std::move(callback));
53 }
54 
Disconnect()55 void UnixDomainClientSocket::Disconnect() {
56   socket_.reset();
57 }
58 
IsConnected() const59 bool UnixDomainClientSocket::IsConnected() const {
60   return socket_ && socket_->IsConnected();
61 }
62 
IsConnectedAndIdle() const63 bool UnixDomainClientSocket::IsConnectedAndIdle() const {
64   return socket_ && socket_->IsConnectedAndIdle();
65 }
66 
GetPeerAddress(IPEndPoint * address) const67 int UnixDomainClientSocket::GetPeerAddress(IPEndPoint* address) const {
68   // Unix domain sockets have no valid associated addr/port;
69   // return either not connected or address invalid.
70   DCHECK(address);
71 
72   if (!IsConnected())
73     return ERR_SOCKET_NOT_CONNECTED;
74 
75   return ERR_ADDRESS_INVALID;
76 }
77 
GetLocalAddress(IPEndPoint * address) const78 int UnixDomainClientSocket::GetLocalAddress(IPEndPoint* address) const {
79   // Unix domain sockets have no valid associated addr/port;
80   // return either not connected or address invalid.
81   DCHECK(address);
82 
83   if (!socket_)
84     return ERR_SOCKET_NOT_CONNECTED;
85 
86   return ERR_ADDRESS_INVALID;
87 }
88 
NetLog() const89 const NetLogWithSource& UnixDomainClientSocket::NetLog() const {
90   return net_log_;
91 }
92 
WasEverUsed() const93 bool UnixDomainClientSocket::WasEverUsed() const {
94   return true;  // We don't care.
95 }
96 
WasAlpnNegotiated() const97 bool UnixDomainClientSocket::WasAlpnNegotiated() const {
98   return false;
99 }
100 
GetNegotiatedProtocol() const101 NextProto UnixDomainClientSocket::GetNegotiatedProtocol() const {
102   return kProtoUnknown;
103 }
104 
GetSSLInfo(SSLInfo * ssl_info)105 bool UnixDomainClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
106   return false;
107 }
108 
GetTotalReceivedBytes() const109 int64_t UnixDomainClientSocket::GetTotalReceivedBytes() const {
110   NOTIMPLEMENTED();
111   return 0;
112 }
113 
ApplySocketTag(const SocketTag & tag)114 void UnixDomainClientSocket::ApplySocketTag(const SocketTag& tag) {
115   // Ignore socket tags as Unix domain sockets are local only.
116 }
117 
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)118 int UnixDomainClientSocket::Read(IOBuffer* buf,
119                                  int buf_len,
120                                  CompletionOnceCallback callback) {
121   DCHECK(socket_);
122   return socket_->Read(buf, buf_len, std::move(callback));
123 }
124 
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag & traffic_annotation)125 int UnixDomainClientSocket::Write(
126     IOBuffer* buf,
127     int buf_len,
128     CompletionOnceCallback callback,
129     const NetworkTrafficAnnotationTag& traffic_annotation) {
130   DCHECK(socket_);
131   return socket_->Write(buf, buf_len, std::move(callback), traffic_annotation);
132 }
133 
SetReceiveBufferSize(int32_t size)134 int UnixDomainClientSocket::SetReceiveBufferSize(int32_t size) {
135   NOTIMPLEMENTED();
136   return ERR_NOT_IMPLEMENTED;
137 }
138 
SetSendBufferSize(int32_t size)139 int UnixDomainClientSocket::SetSendBufferSize(int32_t size) {
140   NOTIMPLEMENTED();
141   return ERR_NOT_IMPLEMENTED;
142 }
143 
ReleaseConnectedSocket()144 SocketDescriptor UnixDomainClientSocket::ReleaseConnectedSocket() {
145   DCHECK(socket_);
146   DCHECK(socket_->IsConnected());
147 
148   SocketDescriptor socket_fd = socket_->ReleaseConnectedSocket();
149   socket_.reset();
150   return socket_fd;
151 }
152 
153 }  // namespace net
154