TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / android / ip_config_builder.cpp
1 /*
2  * Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #include "anbox/android/ip_config_builder.h"
19
20 #include <boost/endian/buffers.hpp>
21
22 #include <ostream>
23 #include <sstream>
24
25 namespace {
26 constexpr const char *assignment_key{"ipAssignment"};
27 constexpr const char *link_address_key{"linkAddress"};
28 constexpr const char *gateway_key{"gateway"};
29 constexpr const char *dns_key{"dns"};
30 constexpr const char *id_key{"id"};
31 constexpr const char *eos_key{"eos"};
32 constexpr const char *assignment_static{"STATIC"};
33 constexpr const char *assignment_dhcp{"DHCP"};
34 constexpr const char *assignment_unknown{"UNKNOWN"};
35
36 constexpr const std::uint32_t is_default_gateway{0};
37 constexpr const std::uint32_t gateway_is_present{1};
38
39 namespace aa = anbox::android;
40 std::string assignment_to_string(const aa::IpConfigBuilder::Assignment &value) {
41   switch (value) {
42   case anbox::android::IpConfigBuilder::Assignment::Static:
43     return assignment_static;
44     break;
45   case anbox::android::IpConfigBuilder::Assignment::DHCP:
46     return assignment_dhcp;
47     break;
48   default:
49     break;
50   }
51   return assignment_unknown;
52 }
53 }
54
55 namespace anbox {
56 namespace android {
57 std::size_t IpConfigBuilder::write(common::BinaryWriter &writer) {
58   writer.set_byte_order(common::BinaryWriter::Order::Big);
59
60   // See http://androidxref.com/7.1.1_r6/xref/frameworks/base/services/core/java/com/android/server/net/IpConfigStore.java
61   // for more details on the binary file format used here.
62
63   writer.write_uint32(static_cast<std::uint32_t>(version_));
64
65   writer.write_string_with_size(assignment_key);
66   writer.write_string_with_size(assignment_to_string(assignment_));
67
68   writer.write_string_with_size(link_address_key);
69   writer.write_string_with_size(link_.address);
70   writer.write_uint32(link_.prefix_length);
71
72   writer.write_string_with_size(gateway_key);
73   writer.write_uint32(is_default_gateway);
74   writer.write_uint32(gateway_is_present);
75   writer.write_string_with_size(gateway_);
76
77   writer.write_string_with_size(dns_key);
78   for (const auto &server : dns_servers_)
79     writer.write_string_with_size(server);
80
81   writer.write_string_with_size(id_key);
82   writer.write_uint32(id_);
83
84   writer.write_string_with_size(eos_key);
85
86   return writer.bytes_written();
87 }
88
89 void IpConfigBuilder::set_version(const Version &version) {
90   version_ = version;
91 }
92
93 void IpConfigBuilder::set_assignment(const Assignment &assignment) {
94   assignment_ = assignment;
95 }
96
97 void IpConfigBuilder::set_link_address(const std::string &address, uint32_t prefix_length) {
98   link_.address = address;
99   link_.prefix_length = prefix_length;
100 }
101
102 void IpConfigBuilder::set_gateway(const std::string &gateway) {
103   gateway_ = gateway;
104 }
105
106 void IpConfigBuilder::set_dns_servers(const std::vector<std::string> &dns_servers) {
107   dns_servers_ = dns_servers;
108 }
109
110 void IpConfigBuilder::set_id(uint32_t id) {
111   id_ = id;
112 }
113 }  // namespace android
114 }  // namespace anbox