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