summaryrefslogtreecommitdiff
path: root/tools/rootcanal/net/posix/posix_async_socket_server.cc
blob: 0615fda5138d9da04d3a906d028fc38efa899652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

// Copyright (C) 2021 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "net/posix/posix_async_socket_server.h"

#include <errno.h>       // for errno
#include <netinet/in.h>  // for sockaddr_in, INADDR_ANY
#include <string.h>      // for strerror, NULL
#include <sys/socket.h>  // for accept, bind, getsockname
#include <unistd.h>      // for close

#include <functional>   // for __base, function
#include <type_traits>  // for remove_extent_t

#include "net/posix/posix_async_socket.h"  // for PosixAsyncSocket, AsyncMan...
#include "os/log.h"                        // for LOG_INFO, LOG_ERROR
#include "osi/include/osi.h"               // for OSI_NO_INTR

namespace android {
namespace net {
class AsyncDataChannel;

PosixAsyncSocketServer::PosixAsyncSocketServer(int port, AsyncManager* am)
    : port_(port), am_(am) {
  int listen_fd = 0;
  struct sockaddr_in listen_address {};
  socklen_t sockaddr_in_size = sizeof(struct sockaddr_in);

  OSI_NO_INTR(listen_fd = socket(AF_INET, SOCK_STREAM, 0));
  if (listen_fd < 0) {
    LOG_INFO("Error creating socket for test channel.");
    return;
  }

  int enable = 1;
  if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) <
      0) {
    LOG_ERROR("setsockopt(SO_REUSEADDR) failed: %s", strerror(errno));
  }

  listen_address.sin_family = AF_INET;
  listen_address.sin_port = htons(port_);
  listen_address.sin_addr.s_addr = htonl(INADDR_ANY);

  if (bind(listen_fd, reinterpret_cast<sockaddr*>(&listen_address),
           sockaddr_in_size) < 0) {
    LOG_INFO("Error binding test channel listener socket to port: %d, %s", port,
             strerror(errno));
    close(listen_fd);
    return;
  }

  if (listen(listen_fd, 1) < 0) {
    LOG_INFO("Error listening for test channel: %s", strerror(errno));
    close(listen_fd);
    return;
  }

  struct sockaddr_in sin;
  socklen_t slen = sizeof(sin);
  if (getsockname(listen_fd, (struct sockaddr*)&sin, &slen) == -1)
    LOG_INFO("Error retrieving actual port: %s", strerror(errno));
  else
    port_ = ntohs(sin.sin_port);

  LOG_INFO("Listening on: %d (%d)", port_, listen_fd);
  server_socket_ = std::make_shared<PosixAsyncSocket>(listen_fd, am_);
}

bool PosixAsyncSocketServer::StartListening() {
  if (!server_socket_ || !callback_) {
    return false;
  }

  server_socket_->WatchForNonBlockingRead(
      [this](AsyncDataChannel* /* socket */) { AcceptSocket(); });
  return true;
}

void PosixAsyncSocketServer::Close() {
  if (server_socket_) {
    server_socket_->Close();
  }
}

bool PosixAsyncSocketServer::Connected() {
  return server_socket_ && server_socket_->Connected();
}

void PosixAsyncSocketServer::AcceptSocket() {
  int accept_fd = 0;
  OSI_NO_INTR(accept_fd = accept(server_socket_->fd(), NULL, NULL));

  if (accept_fd < 0) {
    LOG_INFO("Error accepting test channel connection errno=%d (%s).", errno,
             strerror(errno));
    return;
  }

  LOG_INFO("accept_fd = %d.", accept_fd);
  StopListening();
  callback_(std::make_shared<PosixAsyncSocket>(accept_fd, am_), this);
}

void PosixAsyncSocketServer::StopListening() { server_socket_->StopWatching(); }
}  // namespace net
}  // namespace android