1 // 2 // Copyright (C) 2015 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 #ifndef SHILL_DHCP_DHCPV4_CONFIG_H_ 18 #define SHILL_DHCP_DHCPV4_CONFIG_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include <gtest/gtest_prod.h> // for FRIEND_TEST 24 25 #include "shill/dhcp/dhcp_config.h" 26 #include "shill/dhcp_properties.h" 27 28 namespace shill { 29 30 class Metrics; 31 32 // DHCPv4 client instance. 33 // |dhcp_props| may contain values for the request hostname and vendor class. 34 // If these properties have non-empty values, they will be used in the DHCP 35 // request. If the Hostname property in dhcp_props is non-empty, it asks the 36 // DHCP server to register this hostname on our behalf, for purposes of 37 // administration or creating a dynamic DNS entry. 38 class DHCPv4Config : public DHCPConfig { 39 public: 40 DHCPv4Config(ControlInterface* control_interface, 41 EventDispatcher* dispatcher, 42 DHCPProvider* provider, 43 const std::string& device_name, 44 const std::string& lease_file_suffix, 45 bool arp_gateway, 46 const DhcpProperties& dhcp_props, 47 Metrics* metrics); 48 ~DHCPv4Config() override; 49 50 // Inherited from DHCPConfig. 51 void ProcessEventSignal(const std::string& reason, 52 const KeyValueStore& configuration) override; 53 void ProcessStatusChangeSignal(const std::string& status) override; 54 55 protected: 56 // Inherited from DHCPConfig. 57 void CleanupClientState() override; 58 bool ShouldFailOnAcquisitionTimeout() override; 59 bool ShouldKeepLeaseOnDisconnect() override; 60 std::vector<std::string> GetFlags() override; 61 62 private: 63 friend class DHCPv4ConfigTest; 64 FRIEND_TEST(DHCPv4ConfigCallbackTest, ProcessEventSignalFail); 65 FRIEND_TEST(DHCPv4ConfigCallbackTest, ProcessEventSignalGatewayArp); 66 FRIEND_TEST(DHCPv4ConfigCallbackTest, ProcessEventSignalGatewayArpNak); 67 FRIEND_TEST(DHCPv4ConfigCallbackTest, ProcessEventSignalSuccess); 68 FRIEND_TEST(DHCPv4ConfigCallbackTest, ProcessEventSignalUnknown); 69 FRIEND_TEST(DHCPv4ConfigCallbackTest, StoppedDuringFailureCallback); 70 FRIEND_TEST(DHCPv4ConfigCallbackTest, StoppedDuringSuccessCallback); 71 FRIEND_TEST(DHCPv4ConfigTest, GetIPv4AddressString); 72 FRIEND_TEST(DHCPv4ConfigTest, ParseClasslessStaticRoutes); 73 FRIEND_TEST(DHCPv4ConfigTest, ParseConfiguration); 74 FRIEND_TEST(DHCPv4ConfigTest, ParseConfigurationWithMinimumMTU); 75 FRIEND_TEST(DHCPv4ConfigTest, ProcessStatusChangeSingal); 76 FRIEND_TEST(DHCPv4ConfigTest, StartWithEmptyHostname); 77 FRIEND_TEST(DHCPv4ConfigTest, StartWithHostname); 78 FRIEND_TEST(DHCPv4ConfigTest, StartWithVendorClass); 79 FRIEND_TEST(DHCPv4ConfigTest, StartWithoutArpGateway); 80 FRIEND_TEST(DHCPv4ConfigTest, StartWithoutHostname); 81 FRIEND_TEST(DHCPv4ConfigTest, StartWithoutVendorClass); 82 83 static const char kDHCPCDPathFormatPID[]; 84 85 static const char kConfigurationKeyBroadcastAddress[]; 86 static const char kConfigurationKeyClasslessStaticRoutes[]; 87 static const char kConfigurationKeyDNS[]; 88 static const char kConfigurationKeyDomainName[]; 89 static const char kConfigurationKeyDomainSearch[]; 90 static const char kConfigurationKeyHostname[]; 91 static const char kConfigurationKeyIPAddress[]; 92 static const char kConfigurationKeyLeaseTime[]; 93 static const char kConfigurationKeyMTU[]; 94 static const char kConfigurationKeyRouters[]; 95 static const char kConfigurationKeySubnetCIDR[]; 96 static const char kConfigurationKeyVendorEncapsulatedOptions[]; 97 static const char kConfigurationKeyWebProxyAutoDiscoveryUrl[]; 98 99 static const char kReasonBound[]; 100 static const char kReasonFail[]; 101 static const char kReasonGatewayArp[]; 102 static const char kReasonNak[]; 103 static const char kReasonRebind[]; 104 static const char kReasonReboot[]; 105 static const char kReasonRenew[]; 106 107 static const char kStatusArpGateway[]; 108 static const char kStatusArpSelf[]; 109 static const char kStatusBound[]; 110 static const char kStatusDiscover[]; 111 static const char kStatusIgnoreAdditionalOffer[]; 112 static const char kStatusIgnoreFailedOffer[]; 113 static const char kStatusIgnoreInvalidOffer[]; 114 static const char kStatusIgnoreNonOffer[]; 115 static const char kStatusInform[]; 116 static const char kStatusInit[]; 117 static const char kStatusNakDefer[]; 118 static const char kStatusRebind[]; 119 static const char kStatusReboot[]; 120 static const char kStatusRelease[]; 121 static const char kStatusRenew[]; 122 static const char kStatusRequest[]; 123 124 static const char kType[]; 125 126 // Parses |classless_routes| into |properties|. Sets the default gateway 127 // if one is supplied and |properties| does not already contain one. It 128 // also sets the "routes" parameter of the IPConfig properties for all 129 // routes not converted into the default gateway. Returns true on 130 // success, and false otherwise. 131 static bool ParseClasslessStaticRoutes(const std::string& classless_routes, 132 IPConfig::Properties* properties); 133 134 // Parses |configuration| into |properties|. Returns true on success, and 135 // false otherwise. 136 bool ParseConfiguration(const KeyValueStore& configuration, 137 IPConfig::Properties* properties); 138 139 // Returns the string representation of the IP address |address|, or an 140 // empty string on failure. 141 static std::string GetIPv4AddressString(unsigned int address); 142 143 // Specifies whether to supply an argument to the DHCP client to validate 144 // the acquired IP address using an ARP request to the gateway IP address. 145 bool arp_gateway_; 146 147 // Whether it is valid to retain the lease acquired via gateway ARP. 148 bool is_gateway_arp_active_; 149 150 // Hostname to be used in DHCP request. Set from DhcpProperties in 151 // constructor when present. 152 std::string hostname_; 153 154 // Vendor Class to be used in DHCP request. Set from DhcpProperties in 155 // constructor when present. 156 std::string vendor_class_; 157 158 Metrics* metrics_; 159 160 DISALLOW_COPY_AND_ASSIGN(DHCPv4Config); 161 }; 162 163 } // namespace shill 164 165 #endif // SHILL_DHCP_DHCPV4_CONFIG_H_ 166