[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 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> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9 | #include <utility> |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 10 | |
| 11 | #include "base/logging.h" |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
tfarina | 3d87d7cd | 2016-01-13 02:26:59 | [diff] [blame] | 13 | #include "net/base/sockaddr_storage.h" |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 14 | #include "net/socket/socket_posix.h" |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 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 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 24 | UnixDomainClientSocket::UnixDomainClientSocket( |
| 25 | std::unique_ptr<SocketPosix> socket) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 26 | : use_abstract_namespace_(false), socket_(std::move(socket)) {} |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 27 | |
| 28 | UnixDomainClientSocket::~UnixDomainClientSocket() { |
| 29 | Disconnect(); |
| 30 | } |
| 31 | |
| 32 | // static |
| 33 | bool UnixDomainClientSocket::FillAddress(const std::string& socket_path, |
| 34 | bool use_abstract_namespace, |
| 35 | SockaddrStorage* address) { |
tfarina | 3c80dac | 2016-03-02 18:20:05 | [diff] [blame] | 36 | // Caller should provide a non-empty path for the socket address. |
| 37 | if (socket_path.empty()) |
| 38 | return false; |
| 39 | |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 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 | |
tfarina | 3c80dac | 2016-03-02 18:20:05 | [diff] [blame] | 48 | struct sockaddr_un* socket_addr = |
| 49 | reinterpret_cast<struct sockaddr_un*>(address->addr); |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 50 | memset(socket_addr, 0, address->addr_len); |
| 51 | socket_addr->sun_family = AF_UNIX; |
| 52 | address->addr_len = path_size + offsetof(struct sockaddr_un, sun_path); |
| 53 | if (!use_abstract_namespace) { |
| 54 | memcpy(socket_addr->sun_path, socket_path.c_str(), socket_path.size()); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | #if defined(OS_ANDROID) || defined(OS_LINUX) |
| 59 | // Convert the path given into abstract socket name. It must start with |
| 60 | // the '\0' character, so we are adding it. |addr_len| must specify the |
| 61 | // length of the structure exactly, as potentially the socket name may |
| 62 | // have '\0' characters embedded (although we don't support this). |
| 63 | // Note that addr.sun_path is already zero initialized. |
| 64 | memcpy(socket_addr->sun_path + 1, socket_path.c_str(), socket_path.size()); |
| 65 | return true; |
| 66 | #else |
| 67 | return false; |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | int UnixDomainClientSocket::Connect(const CompletionCallback& callback) { |
| 72 | DCHECK(!socket_); |
| 73 | |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 74 | SockaddrStorage address; |
| 75 | if (!FillAddress(socket_path_, use_abstract_namespace_, &address)) |
| 76 | return ERR_ADDRESS_INVALID; |
| 77 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 78 | socket_.reset(new SocketPosix); |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 79 | int rv = socket_->Open(AF_UNIX); |
| 80 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 81 | if (rv != OK) |
| 82 | return rv; |
| 83 | |
| 84 | return socket_->Connect(address, callback); |
| 85 | } |
| 86 | |
| 87 | void UnixDomainClientSocket::Disconnect() { |
| 88 | socket_.reset(); |
| 89 | } |
| 90 | |
| 91 | bool UnixDomainClientSocket::IsConnected() const { |
| 92 | return socket_ && socket_->IsConnected(); |
| 93 | } |
| 94 | |
| 95 | bool UnixDomainClientSocket::IsConnectedAndIdle() const { |
| 96 | return socket_ && socket_->IsConnectedAndIdle(); |
| 97 | } |
| 98 | |
| 99 | int UnixDomainClientSocket::GetPeerAddress(IPEndPoint* address) const { |
tobiasjs | ff49402 | 2015-01-08 19:40:52 | [diff] [blame] | 100 | // Unix domain sockets have no valid associated addr/port; |
| 101 | // return either not connected or address invalid. |
| 102 | DCHECK(address); |
| 103 | |
| 104 | if (!IsConnected()) |
| 105 | return ERR_SOCKET_NOT_CONNECTED; |
| 106 | |
| 107 | return ERR_ADDRESS_INVALID; |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int UnixDomainClientSocket::GetLocalAddress(IPEndPoint* address) const { |
tobiasjs | ff49402 | 2015-01-08 19:40:52 | [diff] [blame] | 111 | // Unix domain sockets have no valid associated addr/port; |
| 112 | // return either not connected or address invalid. |
| 113 | DCHECK(address); |
| 114 | |
| 115 | if (!socket_) |
| 116 | return ERR_SOCKET_NOT_CONNECTED; |
| 117 | |
| 118 | return ERR_ADDRESS_INVALID; |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 119 | } |
| 120 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame^] | 121 | const NetLogWithSource& UnixDomainClientSocket::NetLog() const { |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 122 | return net_log_; |
| 123 | } |
| 124 | |
| 125 | void UnixDomainClientSocket::SetSubresourceSpeculation() { |
| 126 | } |
| 127 | |
| 128 | void UnixDomainClientSocket::SetOmniboxSpeculation() { |
| 129 | } |
| 130 | |
| 131 | bool UnixDomainClientSocket::WasEverUsed() const { |
| 132 | return true; // We don't care. |
| 133 | } |
| 134 | |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 135 | bool UnixDomainClientSocket::WasNpnNegotiated() const { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | NextProto UnixDomainClientSocket::GetNegotiatedProtocol() const { |
| 140 | return kProtoUnknown; |
| 141 | } |
| 142 | |
| 143 | bool UnixDomainClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
| 144 | return false; |
| 145 | } |
| 146 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 147 | void UnixDomainClientSocket::GetConnectionAttempts( |
| 148 | ConnectionAttempts* out) const { |
| 149 | out->clear(); |
| 150 | } |
| 151 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 152 | int64_t UnixDomainClientSocket::GetTotalReceivedBytes() const { |
| 153 | NOTIMPLEMENTED(); |
| 154 | return 0; |
| 155 | } |
| 156 | |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 157 | int UnixDomainClientSocket::Read(IOBuffer* buf, int buf_len, |
| 158 | const CompletionCallback& callback) { |
| 159 | DCHECK(socket_); |
| 160 | return socket_->Read(buf, buf_len, callback); |
| 161 | } |
| 162 | |
| 163 | int UnixDomainClientSocket::Write(IOBuffer* buf, int buf_len, |
| 164 | const CompletionCallback& callback) { |
| 165 | DCHECK(socket_); |
| 166 | return socket_->Write(buf, buf_len, callback); |
| 167 | } |
| 168 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 169 | int UnixDomainClientSocket::SetReceiveBufferSize(int32_t size) { |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 170 | NOTIMPLEMENTED(); |
| 171 | return ERR_NOT_IMPLEMENTED; |
| 172 | } |
| 173 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 174 | int UnixDomainClientSocket::SetSendBufferSize(int32_t size) { |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 175 | NOTIMPLEMENTED(); |
| 176 | return ERR_NOT_IMPLEMENTED; |
| 177 | } |
| 178 | |
cmasone | ca100d5 | 2014-09-03 18:11:11 | [diff] [blame] | 179 | SocketDescriptor UnixDomainClientSocket::ReleaseConnectedSocket() { |
| 180 | DCHECK(socket_); |
| 181 | DCHECK(socket_->IsConnected()); |
| 182 | |
| 183 | SocketDescriptor socket_fd = socket_->ReleaseConnectedSocket(); |
| 184 | socket_.reset(); |
| 185 | return socket_fd; |
| 186 | } |
| 187 | |
[email protected] | 518c63a | 2014-07-24 03:51:23 | [diff] [blame] | 188 | } // namespace net |