1 //
2 // Copyright (C) 2022 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 
16 #include <android-base/strings.h>
17 #include <string>
18 
19 #include "common/frontend/socket_vsock_proxy/client.h"
20 
21 namespace cuttlefish {
22 namespace socket_proxy {
23 namespace {
24 
IsIpv6(const std::string & address)25 bool IsIpv6(const std::string& address) {
26   return address.find(':') != std::string::npos;
27 }
28 
StartIpv4(const std::string & host,int port,std::chrono::seconds timeout)29 SharedFD StartIpv4(const std::string& host, int port, std::chrono::seconds timeout) {
30   return SharedFD::SocketClient(host, port, SOCK_STREAM, timeout);
31 }
32 
StartIpv6(const std::string & host,int port,std::chrono::seconds timeout)33 SharedFD StartIpv6(const std::string& host, int port, std::chrono::seconds timeout) {
34   const auto host_parsed = android::base::Tokenize(host, "%");
35   const auto host_interface_tokens_count = host_parsed.size();
36 
37   CHECK(host_interface_tokens_count == 1 || host_interface_tokens_count == 2)
38       << "Cannot parse passed host " << host << " to extract the network interface separated by %";
39 
40   std::string host_name;
41   std::string interface_name;
42   if (host_parsed.size() == 2) {
43     host_name = host_parsed[0];
44     interface_name = host_parsed[1];
45   } else {
46     host_name = host;
47   }
48 
49   return SharedFD::Socket6Client(host_name, interface_name, port, SOCK_STREAM, timeout);
50 }
51 
52 }
53 
TcpClient(std::string host,int port,std::chrono::seconds timeout)54 TcpClient::TcpClient(std::string host, int port, std::chrono::seconds timeout)
55     : host_(std::move(host)),
56       port_(port),
57       timeout_(timeout) {}
58 
Start()59 SharedFD TcpClient::Start() {
60   SharedFD client;
61 
62   if (IsIpv6(host_)) {
63     client = StartIpv6(host_, port_, timeout_);
64   } else {
65     client = StartIpv4(host_, port_, timeout_);
66   }
67 
68   if (client->IsOpen()) {
69     last_failure_reason_ = 0;
70     LOG(DEBUG) << "Connected to socket:" << host_ << ":" << port_;
71     return client;
72   } else {
73     // Don't log if the previous connection failed with the same error
74     if (last_failure_reason_ != client->GetErrno()) {
75       last_failure_reason_ = client->GetErrno();
76       LOG(ERROR) << "Unable to connect to tcp server: " << client->StrError();
77     }
78   }
79 
80   return client;
81 }
82 
Describe() const83 std::string TcpClient::Describe() const {
84   return fmt::format("tcp: {}:{}", host_, port_);
85 }
86 
VsockClient(int id,int port,bool vhost_user_vsock)87 VsockClient::VsockClient(int id, int port, bool vhost_user_vsock)
88     : id_(id), port_(port), vhost_user_vsock_(vhost_user_vsock) {}
89 
Start()90 SharedFD VsockClient::Start() {
91   auto vsock_socket =
92       SharedFD::VsockClient(id_, port_, SOCK_STREAM, vhost_user_vsock_);
93 
94   if (vsock_socket->IsOpen()) {
95     last_failure_reason_ = 0;
96     LOG(DEBUG) << "Connected to vsock:" << id_ << ":" << port_;
97   } else {
98     // Don't log if the previous connection failed with the same error
99     if (last_failure_reason_ != vsock_socket->GetErrno()) {
100       last_failure_reason_ = vsock_socket->GetErrno();
101       LOG(ERROR) << "Unable to connect to vsock server: "
102                  << vsock_socket->StrError();
103     }
104   }
105   return vsock_socket;
106 }
107 
Describe() const108 std::string VsockClient::Describe() const {
109   return fmt::format("vsock: {}:{} vhost_user: {}", id_, port_,
110                      vhost_user_vsock_);
111 }
112 
113 }
114 }