Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / socket / unix_domain_client_socket_posix.cc
blob92fab6faa4189071fa01519c5825ff6b623f2307
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>
8 #include <sys/un.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"
16 namespace net {
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() {
31 Disconnect();
34 // static
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)
46 return false;
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());
53 return true;
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());
63 return true;
64 #else
65 return false;
66 #endif
69 int UnixDomainClientSocket::Connect(const CompletionCallback& callback) {
70 DCHECK(!socket_);
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);
82 if (rv != OK)
83 return rv;
85 return socket_->Connect(address, callback);
88 void UnixDomainClientSocket::Disconnect() {
89 socket_.reset();
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 {
101 NOTIMPLEMENTED();
102 return ERR_NOT_IMPLEMENTED;
105 int UnixDomainClientSocket::GetLocalAddress(IPEndPoint* address) const {
106 NOTIMPLEMENTED();
107 return ERR_NOT_IMPLEMENTED;
110 const BoundNetLog& UnixDomainClientSocket::NetLog() const {
111 return net_log_;
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 {
125 return false;
128 bool UnixDomainClientSocket::WasNpnNegotiated() const {
129 return false;
132 NextProto UnixDomainClientSocket::GetNegotiatedProtocol() const {
133 return kProtoUnknown;
136 bool UnixDomainClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
137 return false;
140 int UnixDomainClientSocket::Read(IOBuffer* buf, int buf_len,
141 const CompletionCallback& callback) {
142 DCHECK(socket_);
143 return socket_->Read(buf, buf_len, callback);
146 int UnixDomainClientSocket::Write(IOBuffer* buf, int buf_len,
147 const CompletionCallback& callback) {
148 DCHECK(socket_);
149 return socket_->Write(buf, buf_len, callback);
152 int UnixDomainClientSocket::SetReceiveBufferSize(int32 size) {
153 NOTIMPLEMENTED();
154 return ERR_NOT_IMPLEMENTED;
157 int UnixDomainClientSocket::SetSendBufferSize(int32 size) {
158 NOTIMPLEMENTED();
159 return ERR_NOT_IMPLEMENTED;
162 } // namespace net