xref: /aosp_15_r20/external/cronet/net/socket/unix_domain_client_socket_posix.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
GetNegotiatedProtocol() const97 NextProto UnixDomainClientSocket::GetNegotiatedProtocol() const {
98   return kProtoUnknown;
99 }
100 
GetSSLInfo(SSLInfo * ssl_info)101 bool UnixDomainClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
102   return false;
103 }
104 
GetTotalReceivedBytes() const105 int64_t UnixDomainClientSocket::GetTotalReceivedBytes() const {
106   NOTIMPLEMENTED();
107   return 0;
108 }
109 
ApplySocketTag(const SocketTag & tag)110 void UnixDomainClientSocket::ApplySocketTag(const SocketTag& tag) {
111   // Ignore socket tags as Unix domain sockets are local only.
112 }
113 
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)114 int UnixDomainClientSocket::Read(IOBuffer* buf,
115                                  int buf_len,
116                                  CompletionOnceCallback callback) {
117   DCHECK(socket_);
118   return socket_->Read(buf, buf_len, std::move(callback));
119 }
120 
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag & traffic_annotation)121 int UnixDomainClientSocket::Write(
122     IOBuffer* buf,
123     int buf_len,
124     CompletionOnceCallback callback,
125     const NetworkTrafficAnnotationTag& traffic_annotation) {
126   DCHECK(socket_);
127   return socket_->Write(buf, buf_len, std::move(callback), traffic_annotation);
128 }
129 
SetReceiveBufferSize(int32_t size)130 int UnixDomainClientSocket::SetReceiveBufferSize(int32_t size) {
131   NOTIMPLEMENTED();
132   return ERR_NOT_IMPLEMENTED;
133 }
134 
SetSendBufferSize(int32_t size)135 int UnixDomainClientSocket::SetSendBufferSize(int32_t size) {
136   NOTIMPLEMENTED();
137   return ERR_NOT_IMPLEMENTED;
138 }
139 
ReleaseConnectedSocket()140 SocketDescriptor UnixDomainClientSocket::ReleaseConnectedSocket() {
141   DCHECK(socket_);
142   DCHECK(socket_->IsConnected());
143 
144   SocketDescriptor socket_fd = socket_->ReleaseConnectedSocket();
145   socket_.reset();
146   return socket_fd;
147 }
148 
149 }  // namespace net
150