1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "core/server.h"
16
17 #include <chrono>
18 #include <memory>
19 #include <string>
20 #include <utility>
21
22 #include "backend/grpc_server.h"
23 #include "frontend/frontend_server.h"
24 #include "grpcpp/security/server_credentials.h"
25 #include "grpcpp/server.h"
26 #include "grpcpp/server_builder.h"
27 #include "netsim-daemon/src/ffi.rs.h"
28 #include "util/log.h"
29 #ifdef _WIN32
30 #include <Windows.h>
31 #else
32 #include <unistd.h>
33 #endif
34 #ifndef NETSIM_ANDROID_EMULATOR
35 #include <sys/socket.h>
36
37 // Needs to be below sys/socket.h
38 #include <linux/vm_sockets.h>
39 #endif
40 namespace netsim::server {
41
42 namespace {
43 constexpr std::chrono::seconds InactivityCheckInterval(5);
44
RunGrpcServer(int netsim_grpc_port,bool no_cli_ui,int vsock)45 std::pair<std::unique_ptr<grpc::Server>, uint32_t> RunGrpcServer(
46 int netsim_grpc_port, bool no_cli_ui, int vsock) {
47 grpc::ServerBuilder builder;
48 int selected_port;
49 builder.AddListeningPort("0.0.0.0:" + std::to_string(netsim_grpc_port),
50 grpc::InsecureServerCredentials(), &selected_port);
51 if (!no_cli_ui) {
52 static auto frontend_service = GetFrontendService();
53 builder.RegisterService(frontend_service.release());
54 }
55
56 #ifndef NETSIM_ANDROID_EMULATOR
57 if (vsock != 0) {
58 std::string vsock_uri =
59 "vsock:" + std::to_string(VMADDR_CID_ANY) + ":" + std::to_string(vsock);
60 BtsLogInfo("vsock_uri: %s", vsock_uri.c_str());
61 builder.AddListeningPort(vsock_uri, grpc::InsecureServerCredentials());
62 }
63 #endif
64
65 static auto backend_service = GetBackendService();
66 builder.RegisterService(backend_service.release());
67 builder.AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0);
68 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
69 if (server == nullptr) {
70 return std::make_pair(nullptr, static_cast<uint32_t>(selected_port));
71 }
72
73 BtsLogInfo("Grpc server listening on localhost: %s",
74 std::to_string(selected_port).c_str());
75
76 return std::make_pair(std::move(server),
77 static_cast<uint32_t>(selected_port));
78 }
79 } // namespace
80
RunGrpcServerCxx(uint32_t netsim_grpc_port,bool no_cli_ui,uint16_t vsock)81 std::unique_ptr<GrpcServer> RunGrpcServerCxx(uint32_t netsim_grpc_port,
82 bool no_cli_ui, uint16_t vsock) {
83 auto [grpc_server, port] = RunGrpcServer(netsim_grpc_port, no_cli_ui, vsock);
84 if (grpc_server == nullptr) return nullptr;
85 return std::make_unique<GrpcServer>(std::move(grpc_server), port);
86 }
87
88 } // namespace netsim::server
89