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.
5 #include "net/socket/unix_domain_client_socket_posix.h"
7 #include <sys/socket.h>
10 #include "base/logging.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "net/socket/socket_posix.h"
19 UnixDomainClientSocket::UnixDomainClientSocket(const std::string
& socket_path
,
20 bool use_abstract_namespace
)
21 : socket_path_(socket_path
),
22 use_abstract_namespace_(use_abstract_namespace
) {
25 UnixDomainClientSocket::UnixDomainClientSocket(scoped_ptr
<SocketPosix
> socket
)
26 : use_abstract_namespace_(false), socket_(socket
.Pass()) {}
28 UnixDomainClientSocket::~UnixDomainClientSocket() {
33 bool UnixDomainClientSocket::FillAddress(const std::string
& socket_path
,
34 bool use_abstract_namespace
,
35 SockaddrStorage
* address
) {
36 struct sockaddr_un
* socket_addr
=
37 reinterpret_cast<struct sockaddr_un
*>(address
->addr
);
38 size_t path_max
= address
->addr_len
- offsetof(struct sockaddr_un
, sun_path
);
39 // Non abstract namespace pathname should be null-terminated. Abstract
40 // namespace pathname must start with '\0'. So, the size is always greater
41 // than socket_path size by 1.
42 size_t path_size
= socket_path
.size() + 1;
43 if (path_size
> path_max
)
46 memset(socket_addr
, 0, address
->addr_len
);
47 socket_addr
->sun_family
= AF_UNIX
;
48 address
->addr_len
= path_size
+ offsetof(struct sockaddr_un
, sun_path
);
49 if (!use_abstract_namespace
) {
50 memcpy(socket_addr
->sun_path
, socket_path
.c_str(), socket_path
.size());
54 #if defined(OS_ANDROID) || defined(OS_LINUX)
55 // Convert the path given into abstract socket name. It must start with
56 // the '\0' character, so we are adding it. |addr_len| must specify the
57 // length of the structure exactly, as potentially the socket name may
58 // have '\0' characters embedded (although we don't support this).
59 // Note that addr.sun_path is already zero initialized.
60 memcpy(socket_addr
->sun_path
+ 1, socket_path
.c_str(), socket_path
.size());
67 int UnixDomainClientSocket::Connect(const CompletionCallback
& callback
) {
70 if (socket_path_
.empty())
71 return ERR_ADDRESS_INVALID
;
73 SockaddrStorage address
;
74 if (!FillAddress(socket_path_
, use_abstract_namespace_
, &address
))
75 return ERR_ADDRESS_INVALID
;
77 socket_
.reset(new SocketPosix
);
78 int rv
= socket_
->Open(AF_UNIX
);
79 DCHECK_NE(ERR_IO_PENDING
, rv
);
83 return socket_
->Connect(address
, callback
);
86 void UnixDomainClientSocket::Disconnect() {
90 bool UnixDomainClientSocket::IsConnected() const {
91 return socket_
&& socket_
->IsConnected();
94 bool UnixDomainClientSocket::IsConnectedAndIdle() const {
95 return socket_
&& socket_
->IsConnectedAndIdle();
98 int UnixDomainClientSocket::GetPeerAddress(IPEndPoint
* address
) const {
99 // Unix domain sockets have no valid associated addr/port;
100 // return either not connected or address invalid.
104 return ERR_SOCKET_NOT_CONNECTED
;
106 return ERR_ADDRESS_INVALID
;
109 int UnixDomainClientSocket::GetLocalAddress(IPEndPoint
* address
) const {
110 // Unix domain sockets have no valid associated addr/port;
111 // return either not connected or address invalid.
115 return ERR_SOCKET_NOT_CONNECTED
;
117 return ERR_ADDRESS_INVALID
;
120 const BoundNetLog
& UnixDomainClientSocket::NetLog() const {
124 void UnixDomainClientSocket::SetSubresourceSpeculation() {
127 void UnixDomainClientSocket::SetOmniboxSpeculation() {
130 bool UnixDomainClientSocket::WasEverUsed() const {
131 return true; // We don't care.
134 bool UnixDomainClientSocket::UsingTCPFastOpen() const {
138 bool UnixDomainClientSocket::WasNpnNegotiated() const {
142 NextProto
UnixDomainClientSocket::GetNegotiatedProtocol() const {
143 return kProtoUnknown
;
146 bool UnixDomainClientSocket::GetSSLInfo(SSLInfo
* ssl_info
) {
150 void UnixDomainClientSocket::GetConnectionAttempts(
151 ConnectionAttempts
* out
) const {
155 int UnixDomainClientSocket::Read(IOBuffer
* buf
, int buf_len
,
156 const CompletionCallback
& callback
) {
158 return socket_
->Read(buf
, buf_len
, callback
);
161 int UnixDomainClientSocket::Write(IOBuffer
* buf
, int buf_len
,
162 const CompletionCallback
& callback
) {
164 return socket_
->Write(buf
, buf_len
, callback
);
167 int UnixDomainClientSocket::SetReceiveBufferSize(int32 size
) {
169 return ERR_NOT_IMPLEMENTED
;
172 int UnixDomainClientSocket::SetSendBufferSize(int32 size
) {
174 return ERR_NOT_IMPLEMENTED
;
177 SocketDescriptor
UnixDomainClientSocket::ReleaseConnectedSocket() {
179 DCHECK(socket_
->IsConnected());
181 SocketDescriptor socket_fd
= socket_
->ReleaseConnectedSocket();