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/callback.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_); | |
mmenke
2014/07/10 19:07:27
include base/logging.h for DCHECK
byungchul
2014/07/11 02:56:32
Done.
| |
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); | |
mmenke
2014/07/10 19:07:27
Suggest a DCHECK_NE(ERR_IO_PENDING, rv); for docum
byungchul
2014/07/11 02:56:32
Done.
| |
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 { | |
100 NOTIMPLEMENTED(); | |
101 return ERR_NOT_IMPLEMENTED; | |
102 } | |
103 | |
104 int UnixDomainClientSocket::GetLocalAddress(IPEndPoint* address) const { | |
105 NOTIMPLEMENTED(); | |
106 return ERR_NOT_IMPLEMENTED; | |
107 } | |
108 | |
109 const BoundNetLog& UnixDomainClientSocket::NetLog() const { | |
110 return netlog_; | |
111 } | |
112 | |
113 void UnixDomainClientSocket::SetSubresourceSpeculation() { | |
114 } | |
115 | |
116 void UnixDomainClientSocket::SetOmniboxSpeculation() { | |
117 } | |
118 | |
119 bool UnixDomainClientSocket::WasEverUsed() const { | |
120 return true; // We don't care. | |
121 } | |
122 | |
123 bool UnixDomainClientSocket::UsingTCPFastOpen() const { | |
124 return false; | |
125 } | |
126 | |
127 bool UnixDomainClientSocket::WasNpnNegotiated() const { | |
128 return false; | |
129 } | |
130 | |
131 NextProto UnixDomainClientSocket::GetNegotiatedProtocol() const { | |
132 return kProtoUnknown; | |
133 } | |
134 | |
135 bool UnixDomainClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | |
136 return false; | |
137 } | |
138 | |
139 int UnixDomainClientSocket::Read(IOBuffer* buf, int buf_len, | |
140 const CompletionCallback& callback) { | |
141 DCHECK(socket_); | |
142 return socket_->Read(buf, buf_len, callback); | |
143 } | |
144 | |
145 int UnixDomainClientSocket::Write(IOBuffer* buf, int buf_len, | |
146 const CompletionCallback& callback) { | |
147 DCHECK(socket_); | |
148 return socket_->Write(buf, buf_len, callback); | |
149 } | |
150 | |
151 int UnixDomainClientSocket::SetReceiveBufferSize(int32 size) { | |
152 NOTIMPLEMENTED(); | |
153 return ERR_NOT_IMPLEMENTED; | |
154 } | |
155 | |
156 int UnixDomainClientSocket::SetSendBufferSize(int32 size) { | |
157 NOTIMPLEMENTED(); | |
158 return ERR_NOT_IMPLEMENTED; | |
159 } | |
160 | |
161 } // namespace net | |
OLD | NEW |