Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(471)

Side by Side Diff: net/socket/unix_domain_server_socket_posix.cc

Issue 348803003: Refactor tcp socket and unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix net_unittests for iOS Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_server_socket_posix.h"
6
7 #include <errno.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <unistd.h>
11
12 #include "net/base/net_errors.h"
13 #include "net/socket/socket_libevent.h"
14 #include "net/socket/unix_domain_client_socket_posix.h"
mmenke 2014/06/26 15:36:12 nit: Remove redundant include.
byungchul 2014/06/26 18:09:24 Not redundant. It's for client socket.
15
16 namespace net {
17
18 UnixDomainServerSocket::UnixDomainServerSocket(
19 const AuthCallback& auth_callback,
20 bool use_abstract_namespace)
21 : auth_callback_(auth_callback),
22 use_abstract_namespace_(use_abstract_namespace) {
23 }
24
25 UnixDomainServerSocket::~UnixDomainServerSocket() {
26 }
27
28 // static
29 bool UnixDomainServerSocket::GetPeerIds(SocketDescriptor socket,
30 uid_t* user_id,
31 gid_t* group_id) {
32 #if defined(OS_LINUX) || defined(OS_ANDROID)
33 struct ucred user_cred;
34 socklen_t len = sizeof(user_cred);
35 if (getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &user_cred, &len) < 0)
36 return false;
37 *user_id = user_cred.uid;
38 *group_id = user_cred.gid;
39 #else
40 if (getpeereid(socket, user_id, group_id) < 0)
41 return false;
42 #endif
43 return true;
44 }
45
46 int UnixDomainServerSocket::Listen(const IPEndPoint& address, int backlog) {
47 NOTIMPLEMENTED();
mmenke 2014/06/26 15:36:11 nit: Need to include base/logging.h for this.
byungchul 2014/06/26 18:09:24 Done.
48 return ERR_NOT_IMPLEMENTED;
49 }
50
51 int UnixDomainServerSocket::ListenWithAddressAndPort(
mmenke 2014/06/26 15:36:12 Both of these functions seem less than ideal. My
byungchul 2014/06/26 18:09:24 You suggested ListenWithAddressAndPort() in base s
52 const std::string& unix_domain_path,
53 int port_unused,
54 int backlog) {
55 DCHECK(!listen_socket_);
56
57 SockaddrStorage address;
58 if (!UnixDomainClientSocket::FillAddress(unix_domain_path,
59 use_abstract_namespace_,
60 &address))
61 return ERR_ADDRESS_INVALID;
mmenke 2014/06/26 15:36:11 nit: Use braces when either part of an if stateme
byungchul 2014/06/26 18:09:24 Done.
62
63 listen_socket_.reset(new SocketLibevent);
64 int rv = listen_socket_->Open(AF_UNIX);
65 if (rv != OK)
66 return rv;
67
68 rv = listen_socket_->Bind(address);
69 if (rv != OK) {
70 PLOG(ERROR)
71 << "Could not bind unix domain socket to " << unix_domain_path
72 << (use_abstract_namespace_ ? " (with abstract namespace)" : "");
73 return rv;
74 }
75
76 return listen_socket_->Listen(backlog);
77 }
78
79 int UnixDomainServerSocket::GetLocalAddress(IPEndPoint* address) const {
80 NOTIMPLEMENTED();
81 return ERR_NOT_IMPLEMENTED;
82 }
83
84 int UnixDomainServerSocket::Accept(scoped_ptr<StreamSocket>* socket,
85 const CompletionCallback& callback) {
86 DCHECK(socket);
87 DCHECK(!callback.is_null());
88 DCHECK(listen_socket_);
89 DCHECK(!accept_socket_);
90
91 while (true) {
92 int rv = listen_socket_->Accept(
93 &accept_socket_,
94 base::Bind(&UnixDomainServerSocket::AcceptCompleted,
95 base::Unretained(this), socket, callback));
96 if (rv != OK)
97 return rv;
98 if (AuthenticateAndGetStreamSocket(socket))
99 return OK;
100 }
101 }
102
103 void UnixDomainServerSocket::AcceptCompleted(scoped_ptr<StreamSocket>* socket,
104 const CompletionCallback& callback,
105 int rv) {
106 if (rv != OK) {
107 callback.Run(rv);
108 return;
109 }
110
111 if (AuthenticateAndGetStreamSocket(socket)) {
112 callback.Run(OK);
113 return;
114 }
115
116 // Accepts another socket because authentication error should be transparent
117 // to the caller.
118 rv = Accept(socket, callback);
119 if (rv != ERR_IO_PENDING)
120 callback.Run(rv);
121 }
122
123 bool UnixDomainServerSocket::AuthenticateAndGetStreamSocket(
124 scoped_ptr<StreamSocket>* socket) {
125 DCHECK(accept_socket_);
126
127 uid_t user_id;
128 gid_t group_id;
129 if (!GetPeerIds(accept_socket_->socket_fd(), &user_id, &group_id) ||
130 !auth_callback_.Run(user_id, group_id)) {
131 accept_socket_.reset();
132 return false;
133 }
134
135 socket->reset(new UnixDomainClientSocket(accept_socket_.Pass()));
136 return true;
137 }
138
139 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698