| 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 "mojo/shell/domain_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 "mojo/shell/domain_socket/net_errors.h" | |
| 13 #include "mojo/shell/domain_socket/socket_libevent.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 namespace shell { | |
| 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), socket_(socket.Pass()) { | |
| 27 } | |
| 28 | |
| 29 UnixDomainClientSocket::~UnixDomainClientSocket() { | |
| 30 Disconnect(); | |
| 31 } | |
| 32 | |
| 33 // static | |
| 34 bool UnixDomainClientSocket::FillAddress(const std::string& socket_path, | |
| 35 bool use_abstract_namespace, | |
| 36 SockaddrStorage* address) { | |
| 37 struct sockaddr_un* socket_addr = | |
| 38 reinterpret_cast<struct sockaddr_un*>(address->addr); | |
| 39 size_t path_max = address->addr_len - offsetof(struct sockaddr_un, sun_path); | |
| 40 // Non abstract namespace pathname should be null-terminated. Abstract | |
| 41 // namespace pathname must start with '\0'. So, the size is always greater | |
| 42 // than socket_path size by 1. | |
| 43 size_t path_size = socket_path.size() + 1; | |
| 44 if (path_size > path_max) | |
| 45 return false; | |
| 46 | |
| 47 memset(socket_addr, 0, address->addr_len); | |
| 48 socket_addr->sun_family = AF_UNIX; | |
| 49 address->addr_len = path_size + offsetof(struct sockaddr_un, sun_path); | |
| 50 if (!use_abstract_namespace) { | |
| 51 memcpy(socket_addr->sun_path, socket_path.c_str(), socket_path.size()); | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 #if defined(OS_ANDROID) || defined(OS_LINUX) | |
| 56 // Convert the path given into abstract socket name. It must start with | |
| 57 // the '\0' character, so we are adding it. |addr_len| must specify the | |
| 58 // length of the structure exactly, as potentially the socket name may | |
| 59 // have '\0' characters embedded (although we don't support this). | |
| 60 // Note that addr.sun_path is already zero initialized. | |
| 61 memcpy(socket_addr->sun_path + 1, socket_path.c_str(), socket_path.size()); | |
| 62 return true; | |
| 63 #else | |
| 64 return false; | |
| 65 #endif | |
| 66 } | |
| 67 | |
| 68 int UnixDomainClientSocket::Connect(const CompletionCallback& callback) { | |
| 69 DCHECK(!socket_); | |
| 70 | |
| 71 if (socket_path_.empty()) | |
| 72 return net::ERR_ADDRESS_INVALID; | |
| 73 | |
| 74 SockaddrStorage address; | |
| 75 if (!FillAddress(socket_path_, use_abstract_namespace_, &address)) | |
| 76 return net::ERR_ADDRESS_INVALID; | |
| 77 | |
| 78 socket_.reset(new SocketLibevent); | |
| 79 int rv = socket_->Open(AF_UNIX); | |
| 80 DCHECK_NE(net::ERR_IO_PENDING, rv); | |
| 81 if (rv != net::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 SocketDescriptor UnixDomainClientSocket::ReleaseConnectedSocket() { | |
| 100 DCHECK(socket_); | |
| 101 DCHECK(socket_->IsConnected()); | |
| 102 | |
| 103 SocketDescriptor socket_fd = socket_->ReleaseConnectedSocket(); | |
| 104 socket_.reset(); | |
| 105 return socket_fd; | |
| 106 } | |
| 107 | |
| 108 } // namespace shell | |
| 109 } // namespace mojo | |
| OLD | NEW |