OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/unix_domain_server_socket_posix.h" | 5 #include "net/socket/unix_domain_server_socket_posix.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <sys/un.h> | 9 #include <sys/un.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/socket/socket_libevent.h" | 14 #include "net/socket/socket_libevent.h" |
15 #include "net/socket/unix_domain_client_socket_posix.h" | 15 #include "net/socket/unix_domain_client_socket_posix.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
| 19 namespace { |
| 20 |
| 21 // Intended for use as SetterCallbacks in Accept() helper methods. |
| 22 void SetStreamSocket(scoped_ptr<StreamSocket>* socket, |
| 23 scoped_ptr<SocketLibevent> accepted_socket) { |
| 24 socket->reset(new UnixDomainClientSocket(accepted_socket.Pass())); |
| 25 } |
| 26 |
| 27 void SetSocketDescriptor(SocketDescriptor* socket, |
| 28 scoped_ptr<SocketLibevent> accepted_socket) { |
| 29 *socket = accepted_socket->ReleaseConnectedSocket(); |
| 30 } |
| 31 |
| 32 } // anonymous namespace |
| 33 |
19 UnixDomainServerSocket::UnixDomainServerSocket( | 34 UnixDomainServerSocket::UnixDomainServerSocket( |
20 const AuthCallback& auth_callback, | 35 const AuthCallback& auth_callback, |
21 bool use_abstract_namespace) | 36 bool use_abstract_namespace) |
22 : auth_callback_(auth_callback), | 37 : auth_callback_(auth_callback), |
23 use_abstract_namespace_(use_abstract_namespace) { | 38 use_abstract_namespace_(use_abstract_namespace) { |
24 DCHECK(!auth_callback_.is_null()); | 39 DCHECK(!auth_callback_.is_null()); |
25 } | 40 } |
26 | 41 |
27 UnixDomainServerSocket::~UnixDomainServerSocket() { | 42 UnixDomainServerSocket::~UnixDomainServerSocket() { |
28 } | 43 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 103 } |
89 | 104 |
90 int UnixDomainServerSocket::GetLocalAddress(IPEndPoint* address) const { | 105 int UnixDomainServerSocket::GetLocalAddress(IPEndPoint* address) const { |
91 NOTIMPLEMENTED(); | 106 NOTIMPLEMENTED(); |
92 return ERR_NOT_IMPLEMENTED; | 107 return ERR_NOT_IMPLEMENTED; |
93 } | 108 } |
94 | 109 |
95 int UnixDomainServerSocket::Accept(scoped_ptr<StreamSocket>* socket, | 110 int UnixDomainServerSocket::Accept(scoped_ptr<StreamSocket>* socket, |
96 const CompletionCallback& callback) { | 111 const CompletionCallback& callback) { |
97 DCHECK(socket); | 112 DCHECK(socket); |
| 113 |
| 114 SetterCallback setter_callback = base::Bind(&SetStreamSocket, socket); |
| 115 return DoAccept(setter_callback, callback); |
| 116 } |
| 117 |
| 118 int UnixDomainServerSocket::AcceptSocketDescriptor( |
| 119 SocketDescriptor* socket, |
| 120 const CompletionCallback& callback) { |
| 121 DCHECK(socket); |
| 122 |
| 123 SetterCallback setter_callback = base::Bind(&SetSocketDescriptor, socket); |
| 124 return DoAccept(setter_callback, callback); |
| 125 } |
| 126 |
| 127 int UnixDomainServerSocket::DoAccept(const SetterCallback& setter_callback, |
| 128 const CompletionCallback& callback) { |
| 129 DCHECK(!setter_callback.is_null()); |
98 DCHECK(!callback.is_null()); | 130 DCHECK(!callback.is_null()); |
99 DCHECK(listen_socket_); | 131 DCHECK(listen_socket_); |
100 DCHECK(!accept_socket_); | 132 DCHECK(!accept_socket_); |
101 | 133 |
102 while (true) { | 134 while (true) { |
103 int rv = listen_socket_->Accept( | 135 int rv = listen_socket_->Accept( |
104 &accept_socket_, | 136 &accept_socket_, |
105 base::Bind(&UnixDomainServerSocket::AcceptCompleted, | 137 base::Bind(&UnixDomainServerSocket::AcceptCompleted, |
106 base::Unretained(this), socket, callback)); | 138 base::Unretained(this), |
| 139 setter_callback, |
| 140 callback)); |
107 if (rv != OK) | 141 if (rv != OK) |
108 return rv; | 142 return rv; |
109 if (AuthenticateAndGetStreamSocket(socket)) | 143 if (AuthenticateAndGetStreamSocket(setter_callback)) |
110 return OK; | 144 return OK; |
111 // Accept another socket because authentication error should be transparent | 145 // Accept another socket because authentication error should be transparent |
112 // to the caller. | 146 // to the caller. |
113 } | 147 } |
114 } | 148 } |
115 | 149 |
116 void UnixDomainServerSocket::AcceptCompleted(scoped_ptr<StreamSocket>* socket, | 150 void UnixDomainServerSocket::AcceptCompleted( |
117 const CompletionCallback& callback, | 151 const SetterCallback& setter_callback, |
118 int rv) { | 152 const CompletionCallback& callback, |
| 153 int rv) { |
119 if (rv != OK) { | 154 if (rv != OK) { |
120 callback.Run(rv); | 155 callback.Run(rv); |
121 return; | 156 return; |
122 } | 157 } |
123 | 158 |
124 if (AuthenticateAndGetStreamSocket(socket)) { | 159 if (AuthenticateAndGetStreamSocket(setter_callback)) { |
125 callback.Run(OK); | 160 callback.Run(OK); |
126 return; | 161 return; |
127 } | 162 } |
128 | 163 |
129 // Accept another socket because authentication error should be transparent | 164 // Accept another socket because authentication error should be transparent |
130 // to the caller. | 165 // to the caller. |
131 rv = Accept(socket, callback); | 166 rv = DoAccept(setter_callback, callback); |
132 if (rv != ERR_IO_PENDING) | 167 if (rv != ERR_IO_PENDING) |
133 callback.Run(rv); | 168 callback.Run(rv); |
134 } | 169 } |
135 | 170 |
136 bool UnixDomainServerSocket::AuthenticateAndGetStreamSocket( | 171 bool UnixDomainServerSocket::AuthenticateAndGetStreamSocket( |
137 scoped_ptr<StreamSocket>* socket) { | 172 const SetterCallback& setter_callback) { |
138 DCHECK(accept_socket_); | 173 DCHECK(accept_socket_); |
139 | 174 |
140 Credentials credentials; | 175 Credentials credentials; |
141 if (!GetPeerCredentials(accept_socket_->socket_fd(), &credentials) || | 176 if (!GetPeerCredentials(accept_socket_->socket_fd(), &credentials) || |
142 !auth_callback_.Run(credentials)) { | 177 !auth_callback_.Run(credentials)) { |
143 accept_socket_.reset(); | 178 accept_socket_.reset(); |
144 return false; | 179 return false; |
145 } | 180 } |
146 | 181 |
147 socket->reset(new UnixDomainClientSocket(accept_socket_.Pass())); | 182 setter_callback.Run(accept_socket_.Pass()); |
148 return true; | 183 return true; |
149 } | 184 } |
150 | 185 |
151 } // namespace net | 186 } // namespace net |
OLD | NEW |