• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 #include <arpa/inet.h>
18 #include <ifaddrs.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include <memory>
23 #include <sstream>
24 #include <string>
25 
26 #include <glog/logging.h>
27 
28 #include "common/libs/constants/ril.h"
29 #include "host/commands/launch/ril_config.h"
30 
31 namespace {
32 
number_of_ones(unsigned long val)33 int number_of_ones(unsigned long val) {
34   int ret = 0;
35   while (val) {
36     ret += val % 2;
37     val >>= 1;
38   }
39   return ret;
40 }
41 
42 class NetConfig {
43  public:
44   uint32_t ril_prefixlen = -1;
45   std::string ril_ipaddr;
46   std::string ril_gateway;
47   std::string ril_dns = "8.8.8.8";
48   std::string ril_broadcast;
49 
ObtainConfig(const std::string & interface)50   bool ObtainConfig(const std::string& interface) {
51     bool ret = ParseIntefaceAttributes(interface);
52     LOG(INFO) << "Network config:";
53     LOG(INFO) << "ipaddr = " << ril_ipaddr;
54     LOG(INFO) << "gateway = " << ril_gateway;
55     LOG(INFO) << "dns = " << ril_dns;
56     LOG(INFO) << "broadcast = " << ril_broadcast;
57     LOG(INFO) << "prefix length = " << ril_prefixlen;
58     return ret;
59   }
60 
61  private:
ParseIntefaceAttributes(struct ifaddrs * ifa)62   bool ParseIntefaceAttributes(struct ifaddrs* ifa) {
63     // if (ifa->ifa_addr->sa_family != AF_INET) {
64     //   LOG(ERROR) << "The " << ifa->ifa_name << " interface is not IPv4";
65     //   return false;
66     // }
67     struct sockaddr_in* sa;
68     char* addr_str;
69 
70     // Gateway
71     sa = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr);
72     addr_str = inet_ntoa(sa->sin_addr);
73     this->ril_gateway = strtok(addr_str, "\n");
74     auto gateway_s_addr = ntohl(sa->sin_addr.s_addr);
75 
76     // Broadcast
77     sa = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr);
78     addr_str = inet_ntoa(sa->sin_addr);
79     this->ril_broadcast = strtok(addr_str, "\n");
80     auto broadcast_s_addr = ntohl(sa->sin_addr.s_addr);
81 
82     // Netmask
83     sa = reinterpret_cast<sockaddr_in*>(ifa->ifa_netmask);
84     this->ril_prefixlen = number_of_ones(sa->sin_addr.s_addr);
85     auto netmask_s_addr = ntohl(sa->sin_addr.s_addr);
86 
87     // Address (Find an address in the network different than the network, the
88     // gateway and the broadcast)
89     auto network = gateway_s_addr & netmask_s_addr;
90     auto s_addr = network + 1;
91     // s_addr & ~netmask_s_addr is zero when s_addr wraps around the network
92     while (s_addr & ~netmask_s_addr) {
93       if (s_addr != gateway_s_addr && s_addr != broadcast_s_addr) {
94         break;
95       }
96       ++s_addr;
97     }
98     if (s_addr == network) {
99       LOG(ERROR) << "No available address found in interface " << ifa->ifa_name;
100       return false;
101     }
102     struct in_addr addr;
103     addr.s_addr = htonl(s_addr);
104     addr_str = inet_ntoa(addr);
105     this->ril_ipaddr = strtok(addr_str, "\n");
106     return true;
107   }
108 
ParseIntefaceAttributes(const std::string & interface)109   bool ParseIntefaceAttributes(const std::string& interface) {
110     struct ifaddrs *ifa_list{}, *ifa{};
111     bool ret = false;
112     getifaddrs(&ifa_list);
113     for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
114       if (strcmp(ifa->ifa_name, interface.c_str()) == 0 &&
115           ifa->ifa_addr->sa_family == AF_INET) {
116         ret = ParseIntefaceAttributes(ifa);
117         break;
118       }
119     }
120     freeifaddrs(ifa_list);
121     return ret;
122   }
123 };
124 
125 template <typename T>
BuildPropertyDefinition(const std::string & prop_name,const T & prop_value)126 std::string BuildPropertyDefinition(const std::string& prop_name,
127                                   const T& prop_value) {
128   std::ostringstream stream;
129   stream << prop_name << "=" << prop_value;
130   return stream.str();
131 }
132 }  // namespace
133 
ConfigureRil(vsoc::CuttlefishConfig * config)134 void ConfigureRil(vsoc::CuttlefishConfig* config) {
135   NetConfig netconfig;
136   if (!netconfig.ObtainConfig(config->mobile_bridge_name())) {
137     LOG(ERROR) << "Unable to obtain the network configuration";
138     return;
139   }
140 
141   config->add_kernel_cmdline(BuildPropertyDefinition(
142       CUTTLEFISH_RIL_ADDR_PROPERTY, netconfig.ril_ipaddr));
143   config->add_kernel_cmdline(BuildPropertyDefinition(
144       CUTTLEFISH_RIL_GATEWAY_PROPERTY, netconfig.ril_gateway));
145   config->add_kernel_cmdline(BuildPropertyDefinition(
146       CUTTLEFISH_RIL_DNS_PROPERTY, netconfig.ril_dns));
147   config->add_kernel_cmdline(BuildPropertyDefinition(
148       CUTTLEFISH_RIL_BROADCAST_PROPERTY, netconfig.ril_broadcast));
149   config->add_kernel_cmdline(BuildPropertyDefinition(
150       CUTTLEFISH_RIL_PREFIXLEN_PROPERTY, netconfig.ril_prefixlen));
151 }
152