Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2012 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 "shill/default_profile.h"
     18 
     19 #include <random>
     20 #include <vector>
     21 
     22 #include <base/files/file_path.h>
     23 #include <base/strings/string_number_conversions.h>
     24 #include <base/strings/stringprintf.h>
     25 #if defined(__ANDROID__)
     26 #include <dbus/service_constants.h>
     27 #else
     28 #include <chromeos/dbus/service_constants.h>
     29 #endif  // __ANDROID__
     30 
     31 #include "shill/adaptor_interfaces.h"
     32 #include "shill/connectivity_trial.h"
     33 #include "shill/control_interface.h"
     34 #include "shill/link_monitor.h"
     35 #include "shill/manager.h"
     36 #include "shill/portal_detector.h"
     37 #include "shill/resolver.h"
     38 #include "shill/store_interface.h"
     39 
     40 #if !defined(DISABLE_WIFI)
     41 #include "shill/wifi/wifi_provider.h"
     42 #endif  // DISABLE_WIFI
     43 
     44 using base::FilePath;
     45 using std::string;
     46 using std::vector;
     47 
     48 namespace shill {
     49 // static
     50 const char DefaultProfile::kDefaultId[] = "default";
     51 // static
     52 const char DefaultProfile::kStorageId[] = "global";
     53 // static
     54 const char DefaultProfile::kStorageArpGateway[] = "ArpGateway";
     55 // static
     56 const char DefaultProfile::kStorageCheckPortalList[] = "CheckPortalList";
     57 // static
     58 const char DefaultProfile::kStorageConnectionIdSalt[] = "ConnectionIdSalt";
     59 // static
     60 const char DefaultProfile::kStorageHostName[] = "HostName";
     61 // static
     62 const char DefaultProfile::kStorageIgnoredDNSSearchPaths[] =
     63     "IgnoredDNSSearchPaths";
     64 // static
     65 const char DefaultProfile::kStorageLinkMonitorTechnologies[] =
     66     "LinkMonitorTechnologies";
     67 // static
     68 const char DefaultProfile::kStorageName[] = "Name";
     69 // static
     70 const char DefaultProfile::kStorageNoAutoConnectTechnologies[] =
     71     "NoAutoConnectTechnologies";
     72 // static
     73 const char DefaultProfile::kStorageOfflineMode[] = "OfflineMode";
     74 // static
     75 const char DefaultProfile::kStoragePortalURL[] = "PortalURL";
     76 // static
     77 const char DefaultProfile::kStoragePortalCheckInterval[] =
     78     "PortalCheckInterval";
     79 // static
     80 const char DefaultProfile::kStorageProhibitedTechnologies[] =
     81     "ProhibitedTechnologies";
     82 
     83 DefaultProfile::DefaultProfile(ControlInterface* control,
     84                                Metrics* metrics,
     85                                Manager* manager,
     86                                const FilePath& storage_directory,
     87                                const string& profile_id,
     88                                const Manager::Properties& manager_props)
     89     : Profile(
     90           control, metrics, manager, Identifier(profile_id),
     91           storage_directory, true),
     92       profile_id_(profile_id),
     93       props_(manager_props),
     94       random_engine_(time(nullptr)) {
     95   PropertyStore* store = this->mutable_store();
     96   store->RegisterConstBool(kArpGatewayProperty, &manager_props.arp_gateway);
     97   store->RegisterConstString(kCheckPortalListProperty,
     98                              &manager_props.check_portal_list);
     99   store->RegisterConstString(kCountryProperty, &manager_props.country);
    100   store->RegisterConstString(kIgnoredDNSSearchPathsProperty,
    101                              &manager_props.ignored_dns_search_paths);
    102   store->RegisterConstString(kLinkMonitorTechnologiesProperty,
    103                              &manager_props.link_monitor_technologies);
    104   store->RegisterConstString(kNoAutoConnectTechnologiesProperty,
    105                              &manager_props.no_auto_connect_technologies);
    106   store->RegisterConstBool(kOfflineModeProperty, &manager_props.offline_mode);
    107   store->RegisterConstString(kPortalURLProperty, &manager_props.portal_url);
    108   store->RegisterConstInt32(kPortalCheckIntervalProperty,
    109                             &manager_props.portal_check_interval_seconds);
    110   store->RegisterConstString(kProhibitedTechnologiesProperty,
    111                              &manager_props.prohibited_technologies);
    112   set_persistent_profile_path(
    113       GetFinalStoragePath(storage_directory, Identifier(profile_id)));
    114 }
    115 
    116 DefaultProfile::~DefaultProfile() {}
    117 
    118 void DefaultProfile::LoadManagerProperties(Manager::Properties* manager_props,
    119                                            DhcpProperties* dhcp_properties) {
    120   storage()->GetBool(kStorageId, kStorageArpGateway,
    121                      &manager_props->arp_gateway);
    122   storage()->GetString(kStorageId, kStorageHostName, &manager_props->host_name);
    123   storage()->GetBool(kStorageId, kStorageOfflineMode,
    124                      &manager_props->offline_mode);
    125   if (!storage()->GetString(kStorageId,
    126                             kStorageCheckPortalList,
    127                             &manager_props->check_portal_list)) {
    128     manager_props->check_portal_list = PortalDetector::kDefaultCheckPortalList;
    129   }
    130   if (!storage()->GetInt(kStorageId, kStorageConnectionIdSalt,
    131                          &manager_props->connection_id_salt)) {
    132     manager_props->connection_id_salt =
    133         std::uniform_int_distribution<int>()(random_engine_);
    134   }
    135   if (!storage()->GetString(kStorageId,
    136                             kStorageIgnoredDNSSearchPaths,
    137                             &manager_props->ignored_dns_search_paths)) {
    138     manager_props->ignored_dns_search_paths =
    139         Resolver::kDefaultIgnoredSearchList;
    140   }
    141   if (!storage()->GetString(kStorageId,
    142                             kStorageLinkMonitorTechnologies,
    143                             &manager_props->link_monitor_technologies)) {
    144     manager_props->link_monitor_technologies =
    145         LinkMonitor::kDefaultLinkMonitorTechnologies;
    146   }
    147   if (!storage()->GetString(kStorageId,
    148                             kStorageNoAutoConnectTechnologies,
    149                             &manager_props->no_auto_connect_technologies)) {
    150     manager_props->no_auto_connect_technologies = "";
    151   }
    152   if (!storage()->GetString(kStorageId, kStoragePortalURL,
    153                             &manager_props->portal_url)) {
    154     manager_props->portal_url = ConnectivityTrial::kDefaultURL;
    155   }
    156   std::string check_interval;
    157   if (!storage()->GetString(kStorageId, kStoragePortalCheckInterval,
    158                             &check_interval) ||
    159       !base::StringToInt(check_interval,
    160                          &manager_props->portal_check_interval_seconds)) {
    161     manager_props->portal_check_interval_seconds =
    162         PortalDetector::kDefaultCheckIntervalSeconds;
    163   }
    164   if (!storage()->GetString(kStorageId,
    165                             kStorageProhibitedTechnologies,
    166                             &manager_props->prohibited_technologies)) {
    167     manager_props->prohibited_technologies = "";
    168   }
    169   dhcp_properties->Load(storage(), kStorageId);
    170 }
    171 
    172 bool DefaultProfile::ConfigureService(const ServiceRefPtr& service) {
    173   if (Profile::ConfigureService(service)) {
    174     return true;
    175   }
    176   if (service->technology() == Technology::kEthernet) {
    177     // Ethernet services should have an affinity towards the default profile,
    178     // so even if a new Ethernet service has no known configuration, accept
    179     // it anyway.
    180     UpdateService(service);
    181     service->SetProfile(this);
    182     return true;
    183   }
    184   return false;
    185 }
    186 
    187 bool DefaultProfile::Save() {
    188   storage()->SetBool(kStorageId, kStorageArpGateway, props_.arp_gateway);
    189   storage()->SetString(kStorageId, kStorageHostName, props_.host_name);
    190   storage()->SetString(kStorageId, kStorageName, GetFriendlyName());
    191   storage()->SetBool(kStorageId, kStorageOfflineMode, props_.offline_mode);
    192   storage()->SetString(kStorageId,
    193                        kStorageCheckPortalList,
    194                        props_.check_portal_list);
    195   storage()->SetInt(kStorageId, kStorageConnectionIdSalt,
    196                     props_.connection_id_salt);
    197   storage()->SetString(kStorageId,
    198                        kStorageIgnoredDNSSearchPaths,
    199                        props_.ignored_dns_search_paths);
    200   storage()->SetString(kStorageId,
    201                        kStorageLinkMonitorTechnologies,
    202                        props_.link_monitor_technologies);
    203   storage()->SetString(kStorageId,
    204                        kStorageNoAutoConnectTechnologies,
    205                        props_.no_auto_connect_technologies);
    206   storage()->SetString(kStorageId,
    207                        kStoragePortalURL,
    208                        props_.portal_url);
    209   storage()->SetString(kStorageId,
    210                        kStoragePortalCheckInterval,
    211                        base::IntToString(props_.portal_check_interval_seconds));
    212   storage()->SetString(kStorageId,
    213                        kStorageProhibitedTechnologies,
    214                        props_.prohibited_technologies);
    215   manager()->dhcp_properties().Save(storage(), kStorageId);
    216   return Profile::Save();
    217 }
    218 
    219 bool DefaultProfile::UpdateDevice(const DeviceRefPtr& device) {
    220   return device->Save(storage()) && storage()->Flush();
    221 }
    222 
    223 #if !defined(DISABLE_WIFI)
    224 bool DefaultProfile::UpdateWiFiProvider(const WiFiProvider& wifi_provider) {
    225   return wifi_provider.Save(storage()) && storage()->Flush();
    226 }
    227 #endif  // DISABLE_WIFI
    228 
    229 }  // namespace shill
    230