1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/socket/unix_domain_client_socket_posix.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <memory>
#include <utility>
#include "base/check_op.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "net/base/net_errors.h"
#include "net/base/sockaddr_storage.h"
#include "net/base/sockaddr_util_posix.h"
#include "net/socket/socket_posix.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net {
UnixDomainClientSocket::UnixDomainClientSocket(const std::string& socket_path,
bool use_abstract_namespace)
: socket_path_(socket_path),
use_abstract_namespace_(use_abstract_namespace) {
}
UnixDomainClientSocket::UnixDomainClientSocket(
std::unique_ptr<SocketPosix> socket)
: use_abstract_namespace_(false), socket_(std::move(socket)) {}
UnixDomainClientSocket::~UnixDomainClientSocket() {
Disconnect();
}
int UnixDomainClientSocket::Connect(CompletionOnceCallback callback) {
if (IsConnected())
return OK;
SockaddrStorage address;
if (!FillUnixAddress(socket_path_, use_abstract_namespace_, &address))
return ERR_ADDRESS_INVALID;
socket_ = std::make_unique<SocketPosix>();
int rv = socket_->Open(AF_UNIX);
DCHECK_NE(ERR_IO_PENDING, rv);
if (rv != OK)
return rv;
return socket_->Connect(address, std::move(callback));
}
void UnixDomainClientSocket::Disconnect() {
socket_.reset();
}
bool UnixDomainClientSocket::IsConnected() const {
return socket_ && socket_->IsConnected();
}
bool UnixDomainClientSocket::IsConnectedAndIdle() const {
return socket_ && socket_->IsConnectedAndIdle();
}
int UnixDomainClientSocket::GetPeerAddress(IPEndPoint* address) const {
// Unix domain sockets have no valid associated addr/port;
// return either not connected or address invalid.
DCHECK(address);
if (!IsConnected())
return ERR_SOCKET_NOT_CONNECTED;
return ERR_ADDRESS_INVALID;
}
int UnixDomainClientSocket::GetLocalAddress(IPEndPoint* address) const {
// Unix domain sockets have no valid associated addr/port;
// return either not connected or address invalid.
DCHECK(address);
if (!socket_)
return ERR_SOCKET_NOT_CONNECTED;
return ERR_ADDRESS_INVALID;
}
const NetLogWithSource& UnixDomainClientSocket::NetLog() const {
return net_log_;
}
bool UnixDomainClientSocket::WasEverUsed() const {
return true; // We don't care.
}
NextProto UnixDomainClientSocket::GetNegotiatedProtocol() const {
return NextProto::kProtoUnknown;
}
bool UnixDomainClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
return false;
}
int64_t UnixDomainClientSocket::GetTotalReceivedBytes() const {
NOTIMPLEMENTED();
return 0;
}
void UnixDomainClientSocket::ApplySocketTag(const SocketTag& tag) {
// Ignore socket tags as Unix domain sockets are local only.
}
int UnixDomainClientSocket::Read(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) {
DCHECK(socket_);
return socket_->Read(buf, buf_len, std::move(callback));
}
int UnixDomainClientSocket::Write(
IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag& traffic_annotation) {
DCHECK(socket_);
return socket_->Write(buf, buf_len, std::move(callback), traffic_annotation);
}
int UnixDomainClientSocket::SetReceiveBufferSize(int32_t size) {
NOTIMPLEMENTED();
return ERR_NOT_IMPLEMENTED;
}
int UnixDomainClientSocket::SetSendBufferSize(int32_t size) {
NOTIMPLEMENTED();
return ERR_NOT_IMPLEMENTED;
}
SocketDescriptor UnixDomainClientSocket::ReleaseConnectedSocket() {
DCHECK(socket_);
DCHECK(socket_->IsConnected());
SocketDescriptor socket_fd = socket_->ReleaseConnectedSocket();
socket_.reset();
return socket_fd;
}
} // namespace net
|