1
2 // Copyright (C) 2021 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 #include "net/posix/posix_async_socket_server.h"
16
17 #include <errno.h> // for errno
18 #include <netinet/in.h> // for sockaddr_in, INADDR_ANY
19 #include <string.h> // for strerror, NULL
20 #include <sys/socket.h> // for accept, bind, getsockname
21 #include <unistd.h> // for close
22
23 #include <functional> // for __base, function
24 #include <type_traits> // for remove_extent_t
25
26 #include "log.h"
27 #include "net/posix/posix_async_socket.h" // for PosixAsyncSocket, AsyncMan...
28
29 namespace android {
30 namespace net {
31 class AsyncDataChannel;
32
PosixAsyncSocketServer(int port,AsyncManager * am)33 PosixAsyncSocketServer::PosixAsyncSocketServer(int port, AsyncManager* am)
34 : port_(port), am_(am) {
35 int listen_fd = 0;
36 struct sockaddr_in listen_address {};
37 socklen_t sockaddr_in_size = sizeof(struct sockaddr_in);
38
39 do {
40 listen_fd = socket(AF_INET, SOCK_STREAM, 0);
41 } while (listen_fd == -1 && errno == EAGAIN);
42
43 if (listen_fd < 0) {
44 INFO("Error creating socket for test channel.");
45 return;
46 }
47
48 int enable = 1;
49 if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) <
50 0) {
51 ERROR("setsockopt(SO_REUSEADDR) failed: {}", strerror(errno));
52 }
53
54 listen_address.sin_family = AF_INET;
55 listen_address.sin_port = htons(port_);
56 listen_address.sin_addr.s_addr = htonl(INADDR_ANY);
57
58 if (bind(listen_fd, reinterpret_cast<sockaddr*>(&listen_address),
59 sockaddr_in_size) < 0) {
60 INFO("Error binding test channel listener socket to port: {}, {}", port,
61 strerror(errno));
62 close(listen_fd);
63 return;
64 }
65
66 if (listen(listen_fd, 1) < 0) {
67 INFO("Error listening for test channel: {}", strerror(errno));
68 close(listen_fd);
69 return;
70 }
71
72 struct sockaddr_in sin;
73 socklen_t slen = sizeof(sin);
74 if (getsockname(listen_fd, (struct sockaddr*)&sin, &slen) == -1) {
75 INFO("Error retrieving actual port: {}", strerror(errno));
76 } else {
77 port_ = ntohs(sin.sin_port);
78 }
79 INFO("Listening on: {} ({})", port_, listen_fd);
80 server_socket_ = std::make_shared<PosixAsyncSocket>(listen_fd, am_);
81 }
82
StartListening()83 bool PosixAsyncSocketServer::StartListening() {
84 if (!server_socket_ || !callback_) {
85 return false;
86 }
87
88 server_socket_->WatchForNonBlockingRead(
89 [this](AsyncDataChannel* /* socket */) { AcceptSocket(); });
90 return true;
91 }
92
Close()93 void PosixAsyncSocketServer::Close() {
94 if (server_socket_) {
95 server_socket_->Close();
96 }
97 }
98
Connected()99 bool PosixAsyncSocketServer::Connected() {
100 return server_socket_ && server_socket_->Connected();
101 }
102
AcceptSocket()103 void PosixAsyncSocketServer::AcceptSocket() {
104 int accept_fd = 0;
105 REPEAT_UNTIL_NO_INTR(accept_fd = accept(server_socket_->fd(), NULL, NULL));
106
107 if (accept_fd < 0) {
108 INFO("Error accepting test channel connection errno={} ({}).", errno,
109 strerror(errno));
110 return;
111 }
112
113 INFO("accept_fd = {}.", accept_fd);
114 StopListening();
115 callback_(std::make_shared<PosixAsyncSocket>(accept_fd, am_), this);
116 }
117
StopListening()118 void PosixAsyncSocketServer::StopListening() { server_socket_->StopWatching(); }
119 } // namespace net
120 } // namespace android
121