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/net_errors.h"
13 #include "net/base/net_util.h"
14 #include "net/socket/socket_libevent.h"
18 UnixDomainClientSocket::UnixDomainClientSocket(const std::string
& socket_path
,
19 bool use_abstract_namespace
)
20 : socket_path_(socket_path
),
21 use_abstract_namespace_(use_abstract_namespace
) {
24 UnixDomainClientSocket::UnixDomainClientSocket(
25 scoped_ptr
<SocketLibevent
> socket
)
26 : use_abstract_namespace_(false),
27 socket_(socket
.Pass()) {
30 UnixDomainClientSocket::~UnixDomainClientSocket() {
35 bool UnixDomainClientSocket::FillAddress(const std::string
& socket_path
,
36 bool use_abstract_namespace
,
37 SockaddrStorage
* address
) {
38 struct sockaddr_un
* socket_addr
=
39 reinterpret_cast<struct sockaddr_un
*>(address
->addr
);
40 size_t path_max
= address
->addr_len
- offsetof(struct sockaddr_un
, sun_path
);
41 // Non abstract namespace pathname should be null-terminated. Abstract
42 // namespace pathname must start with '\0'. So, the size is always greater
43 // than socket_path size by 1.
44 size_t path_size
= socket_path
.size() + 1;
45 if (path_size
> path_max
)
48 memset(socket_addr
, 0, address
->addr_len
);
49 socket_addr
->sun_family
= AF_UNIX
;
50 address
->addr_len
= path_size
+ offsetof(struct sockaddr_un
, sun_path
);
51 if (!use_abstract_namespace
) {
52 memcpy(socket_addr
->sun_path
, socket_path
.c_str(), socket_path
.size());
56 #if defined(OS_ANDROID) || defined(OS_LINUX)
57 // Convert the path given into abstract socket name. It must start with
58 // the '\0' character, so we are adding it. |addr_len| must specify the
59 // length of the structure exactly, as potentially the socket name may
60 // have '\0' characters embedded (although we don't support this).
61 // Note that addr.sun_path is already zero initialized.
62 memcpy(socket_addr
->sun_path
+ 1, socket_path
.c_str(), socket_path
.size());
69 int UnixDomainClientSocket::Connect(const CompletionCallback
& callback
) {
72 if (socket_path_
.empty())
73 return ERR_ADDRESS_INVALID
;
75 SockaddrStorage address
;
76 if (!FillAddress(socket_path_
, use_abstract_namespace_
, &address
))
77 return ERR_ADDRESS_INVALID
;
79 socket_
.reset(new SocketLibevent
);
80 int rv
= socket_
->Open(AF_UNIX
);
81 DCHECK_NE(ERR_IO_PENDING
, rv
);
85 return socket_
->Connect(address
, callback
);
88 void UnixDomainClientSocket::Disconnect() {
92 bool UnixDomainClientSocket::IsConnected() const {
93 return socket_
&& socket_
->IsConnected();
96 bool UnixDomainClientSocket::IsConnectedAndIdle() const {
97 return socket_
&& socket_
->IsConnectedAndIdle();
100 int UnixDomainClientSocket::GetPeerAddress(IPEndPoint
* address
) const {
102 return ERR_NOT_IMPLEMENTED
;
105 int UnixDomainClientSocket::GetLocalAddress(IPEndPoint
* address
) const {
107 return ERR_NOT_IMPLEMENTED
;
110 const BoundNetLog
& UnixDomainClientSocket::NetLog() const {
114 void UnixDomainClientSocket::SetSubresourceSpeculation() {
117 void UnixDomainClientSocket::SetOmniboxSpeculation() {
120 bool UnixDomainClientSocket::WasEverUsed() const {
121 return true; // We don't care.
124 bool UnixDomainClientSocket::UsingTCPFastOpen() const {
128 bool UnixDomainClientSocket::WasNpnNegotiated() const {
132 NextProto
UnixDomainClientSocket::GetNegotiatedProtocol() const {
133 return kProtoUnknown
;
136 bool UnixDomainClientSocket::GetSSLInfo(SSLInfo
* ssl_info
) {
140 int UnixDomainClientSocket::Read(IOBuffer
* buf
, int buf_len
,
141 const CompletionCallback
& callback
) {
143 return socket_
->Read(buf
, buf_len
, callback
);
146 int UnixDomainClientSocket::Write(IOBuffer
* buf
, int buf_len
,
147 const CompletionCallback
& callback
) {
149 return socket_
->Write(buf
, buf_len
, callback
);
152 int UnixDomainClientSocket::SetReceiveBufferSize(int32 size
) {
154 return ERR_NOT_IMPLEMENTED
;
157 int UnixDomainClientSocket::SetSendBufferSize(int32 size
) {
159 return ERR_NOT_IMPLEMENTED
;