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) : port_(port), am_(am) {
34   int listen_fd = 0;
35   struct sockaddr_in listen_address {};
36   socklen_t sockaddr_in_size = sizeof(struct sockaddr_in);
37 
38   do {
39     listen_fd = socket(AF_INET, SOCK_STREAM, 0);
40   } while (listen_fd == -1 && errno == EAGAIN);
41 
42   if (listen_fd < 0) {
43     INFO("Error creating socket for test channel.");
44     return;
45   }
46 
47   int enable = 1;
48   if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
49     ERROR("setsockopt(SO_REUSEADDR) failed: {}", strerror(errno));
50   }
51 
52   listen_address.sin_family = AF_INET;
53   listen_address.sin_port = htons(port_);
54   listen_address.sin_addr.s_addr = htonl(INADDR_ANY);
55 
56   if (bind(listen_fd, reinterpret_cast<sockaddr*>(&listen_address), sockaddr_in_size) < 0) {
57     INFO("Error binding test channel listener socket to port: {}, {}", port, strerror(errno));
58     close(listen_fd);
59     return;
60   }
61 
62   if (listen(listen_fd, 1) < 0) {
63     INFO("Error listening for test channel: {}", strerror(errno));
64     close(listen_fd);
65     return;
66   }
67 
68   struct sockaddr_in sin;
69   socklen_t slen = sizeof(sin);
70   if (getsockname(listen_fd, (struct sockaddr*)&sin, &slen) == -1) {
71     INFO("Error retrieving actual port: {}", strerror(errno));
72   } else {
73     port_ = ntohs(sin.sin_port);
74   }
75   INFO("Listening on: {} ({})", port_, listen_fd);
76   server_socket_ = std::make_shared<PosixAsyncSocket>(listen_fd, am_);
77 }
78 
StartListening()79 bool PosixAsyncSocketServer::StartListening() {
80   if (!server_socket_ || !callback_) {
81     return false;
82   }
83 
84   server_socket_->WatchForNonBlockingRead(
85           [this](AsyncDataChannel* /* socket */) { AcceptSocket(); });
86   return true;
87 }
88 
Close()89 void PosixAsyncSocketServer::Close() {
90   if (server_socket_) {
91     server_socket_->Close();
92   }
93 }
94 
Connected()95 bool PosixAsyncSocketServer::Connected() { return server_socket_ && server_socket_->Connected(); }
96 
AcceptSocket()97 void PosixAsyncSocketServer::AcceptSocket() {
98   int accept_fd = 0;
99   REPEAT_UNTIL_NO_INTR(accept_fd = accept(server_socket_->fd(), NULL, NULL));
100 
101   if (accept_fd < 0) {
102     INFO("Error accepting test channel connection errno={} ({}).", errno, strerror(errno));
103     return;
104   }
105 
106   INFO("accept_fd = {}.", accept_fd);
107   StopListening();
108   callback_(std::make_shared<PosixAsyncSocket>(accept_fd, am_), this);
109 }
110 
StopListening()111 void PosixAsyncSocketServer::StopListening() { server_socket_->StopWatching(); }
112 }  // namespace net
113 }  // namespace android
114