OLD | NEW |
(Empty) | |
| 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. |
| 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 "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" |
| 15 |
| 16 namespace net { |
| 17 |
| 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) { |
| 22 } |
| 23 |
| 24 UnixDomainClientSocket::UnixDomainClientSocket( |
| 25 scoped_ptr<SocketLibevent> socket) |
| 26 : use_abstract_namespace_(false), |
| 27 socket_(socket.Pass()) { |
| 28 } |
| 29 |
| 30 UnixDomainClientSocket::~UnixDomainClientSocket() { |
| 31 Disconnect(); |
| 32 } |
| 33 |
| 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; |
| 47 |
| 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; |
| 54 } |
| 55 |
| 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 |
| 67 } |
| 68 |
| 69 int UnixDomainClientSocket::Connect(const CompletionCallback& callback) { |
| 70 DCHECK(!socket_); |
| 71 |
| 72 if (socket_path_.empty()) |
| 73 return ERR_ADDRESS_INVALID; |
| 74 |
| 75 SockaddrStorage address; |
| 76 if (!FillAddress(socket_path_, use_abstract_namespace_, &address)) |
| 77 return ERR_ADDRESS_INVALID; |
| 78 |
| 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; |
| 84 |
| 85 return socket_->Connect(address, callback); |
| 86 } |
| 87 |
| 88 void UnixDomainClientSocket::Disconnect() { |
| 89 socket_.reset(); |
| 90 } |
| 91 |
| 92 bool UnixDomainClientSocket::IsConnected() const { |
| 93 return socket_ && socket_->IsConnected(); |
| 94 } |
| 95 |
| 96 bool UnixDomainClientSocket::IsConnectedAndIdle() const { |
| 97 return socket_ && socket_->IsConnectedAndIdle(); |
| 98 } |
| 99 |
| 100 int UnixDomainClientSocket::GetPeerAddress(IPEndPoint* address) const { |
| 101 NOTIMPLEMENTED(); |
| 102 return ERR_NOT_IMPLEMENTED; |
| 103 } |
| 104 |
| 105 int UnixDomainClientSocket::GetLocalAddress(IPEndPoint* address) const { |
| 106 NOTIMPLEMENTED(); |
| 107 return ERR_NOT_IMPLEMENTED; |
| 108 } |
| 109 |
| 110 const BoundNetLog& UnixDomainClientSocket::NetLog() const { |
| 111 return net_log_; |
| 112 } |
| 113 |
| 114 void UnixDomainClientSocket::SetSubresourceSpeculation() { |
| 115 } |
| 116 |
| 117 void UnixDomainClientSocket::SetOmniboxSpeculation() { |
| 118 } |
| 119 |
| 120 bool UnixDomainClientSocket::WasEverUsed() const { |
| 121 return true; // We don't care. |
| 122 } |
| 123 |
| 124 bool UnixDomainClientSocket::UsingTCPFastOpen() const { |
| 125 return false; |
| 126 } |
| 127 |
| 128 bool UnixDomainClientSocket::WasNpnNegotiated() const { |
| 129 return false; |
| 130 } |
| 131 |
| 132 NextProto UnixDomainClientSocket::GetNegotiatedProtocol() const { |
| 133 return kProtoUnknown; |
| 134 } |
| 135 |
| 136 bool UnixDomainClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
| 137 return false; |
| 138 } |
| 139 |
| 140 int UnixDomainClientSocket::Read(IOBuffer* buf, int buf_len, |
| 141 const CompletionCallback& callback) { |
| 142 DCHECK(socket_); |
| 143 return socket_->Read(buf, buf_len, callback); |
| 144 } |
| 145 |
| 146 int UnixDomainClientSocket::Write(IOBuffer* buf, int buf_len, |
| 147 const CompletionCallback& callback) { |
| 148 DCHECK(socket_); |
| 149 return socket_->Write(buf, buf_len, callback); |
| 150 } |
| 151 |
| 152 int UnixDomainClientSocket::SetReceiveBufferSize(int32 size) { |
| 153 NOTIMPLEMENTED(); |
| 154 return ERR_NOT_IMPLEMENTED; |
| 155 } |
| 156 |
| 157 int UnixDomainClientSocket::SetSendBufferSize(int32 size) { |
| 158 NOTIMPLEMENTED(); |
| 159 return ERR_NOT_IMPLEMENTED; |
| 160 } |
| 161 |
| 162 } // namespace net |
OLD | NEW |