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)29 SharedFD StartIpv4(const std::string& host, int port) {
30 return SharedFD::SocketClient(host, port, SOCK_STREAM);
31 }
32
StartIpv6(const std::string & host,int port)33 SharedFD StartIpv6(const std::string& host, int port) {
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);
50 }
51
52 }
53
TcpClient(std::string host,int port)54 TcpClient::TcpClient(std::string host, int port) : host_(std::move(host)), port_(port) {}
55
Start()56 SharedFD TcpClient::Start() {
57 SharedFD client;
58
59 if (IsIpv6(host_)) {
60 client = StartIpv6(host_, port_);
61 } else {
62 client = StartIpv4(host_, port_);
63 }
64
65 if (client->IsOpen()) {
66 last_failure_reason_ = 0;
67 LOG(DEBUG) << "Connected to socket:" << host_ << ":" << port_;
68 return client;
69 } else {
70 // Don't log if the previous connection failed with the same error
71 if (last_failure_reason_ != client->GetErrno()) {
72 last_failure_reason_ = client->GetErrno();
73 LOG(ERROR) << "Unable to connect to tcp server: " << client->StrError();
74 }
75 }
76
77 return client;
78 }
79
Describe() const80 std::string TcpClient::Describe() const {
81 return fmt::format("tcp: {}:{}", host_, port_);
82 }
83
VsockClient(int id,int port)84 VsockClient::VsockClient(int id, int port) : id_(id), port_(port) {}
85
Start()86 SharedFD VsockClient::Start() {
87 auto vsock_socket = SharedFD::VsockClient(id_, port_, SOCK_STREAM);
88
89 if (vsock_socket->IsOpen()) {
90 last_failure_reason_ = 0;
91 LOG(DEBUG) << "Connected to vsock:" << id_ << ":" << port_;
92 } else {
93 // Don't log if the previous connection failed with the same error
94 if (last_failure_reason_ != vsock_socket->GetErrno()) {
95 last_failure_reason_ = vsock_socket->GetErrno();
96 LOG(ERROR) << "Unable to connect to vsock server: "
97 << vsock_socket->StrError();
98 }
99 }
100 return vsock_socket;
101 }
102
Describe() const103 std::string VsockClient::Describe() const {
104 return fmt::format("vsock: {}:{}", id_, port_);
105 }
106
107 }
108 }