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