blob: cf9427019cca2f547e65a6a300668193ec52dd8a [file] [log] [blame]
Ningyuan Wangb6836022015-12-02 14:39:01 -08001//
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
Ningyuan Wangb6836022015-12-02 14:39:01 -080017#include "dhcp_client/service.h"
18
Ningyuan Wangab2af972015-12-03 13:57:40 -080019#include <string>
20
Ningyuan Wangb52786a2015-12-10 14:25:15 -080021#include "dhcp_client/device_info.h"
Ningyuan Wangab2af972015-12-03 13:57:40 -080022#include "dhcp_client/manager.h"
23
24using std::string;
25
26namespace {
27const char kConstantInterfaceName[] = "interface_name";
28const char kConstantDHCPType[] = "type";
29const char kConstantNetworkIdentifier[] = "identifier";
30const char kConstantRequestHostname[] = "request_hostname";
31const char kConstantArpGateway[] = "arp_gateway";
32const char kConstantUnicastArp[] = "unicast_arp";
33const char kConstantRequestNontemporaryAddress[] = "request_na";
34const char kConstantRequestPrefixDelegation[] = "request_pf";
35}
36
Ningyuan Wangb6836022015-12-02 14:39:01 -080037namespace dhcp_client {
38
39Service::Service(Manager* manager,
40 int service_identifier,
41 EventDispatcherInterface* event_dispatcher,
42 const brillo::VariantDictionary& configs)
43 : manager_(manager),
44 identifier_(service_identifier),
45 event_dispatcher_(event_dispatcher),
Ningyuan Wangb52786a2015-12-10 14:25:15 -080046 type_(DHCP::SERVICE_TYPE_IPV4),
Ningyuan Wangab2af972015-12-03 13:57:40 -080047 request_hostname_(false),
Ningyuan Wangb6836022015-12-02 14:39:01 -080048 arp_gateway_(false),
49 unicast_arp_(false),
50 request_na_(false),
51 request_pd_(false) {
52 ParseConfigs(configs);
53}
54
55Service::~Service() {
Ningyuan Wangb52786a2015-12-10 14:25:15 -080056 Stop();
Ningyuan Wangb6836022015-12-02 14:39:01 -080057}
58
Ningyuan Wangb52786a2015-12-10 14:25:15 -080059bool Service::Start() {
60 if (!DeviceInfo::GetInstance()->GetDeviceInfo(interface_name_,
61 &hardware_address_,
62 &interface_index_)) {
63 LOG(ERROR) << "Unable to get interface information for: "
64 << interface_name_;
65 return false;
66 }
67
68 if (type_ == DHCP::SERVICE_TYPE_IPV4 ||
69 type_ == DHCP::SERVICE_TYPE_BOTH) {
70 state_machine_ipv4_.reset(new DHCPV4(interface_name_,
71 hardware_address_,
72 interface_index_,
73 network_id_,
74 request_hostname_,
75 arp_gateway_,
76 unicast_arp_,
77 event_dispatcher_));
78 }
79 if (type_ == DHCP::SERVICE_TYPE_IPV6 ||
80 type_ == DHCP::SERVICE_TYPE_BOTH) {
81 // TODO(nywang): Create DHCP state machine for IPV6.
82 }
83 if (state_machine_ipv4_) {
84 state_machine_ipv4_->Start();
85 }
86 // TODO(nywang): Start DHCP state machine for IPV6.
87 return true;
Ningyuan Wangb6836022015-12-02 14:39:01 -080088}
89
90void Service::Stop() {
Ningyuan Wangb52786a2015-12-10 14:25:15 -080091 if (state_machine_ipv4_) {
92 state_machine_ipv4_->Stop();
93 state_machine_ipv4_.reset();
94 }
95 // TODO(nywang): Stop DHCP state machine for IPV6.
Ningyuan Wangb6836022015-12-02 14:39:01 -080096}
97
98void Service::ParseConfigs(const brillo::VariantDictionary& configs) {
Ningyuan Wangab2af972015-12-03 13:57:40 -080099 for (const auto& key_and_value : configs) {
100 const std::string& key = key_and_value.first;
101 const auto& value = key_and_value.second;
102 if (key == kConstantInterfaceName && value.IsTypeCompatible<string>()) {
103 interface_name_ = value.Get<string>();
104 } else if (key == kConstantDHCPType && value.IsTypeCompatible<int32_t>()) {
Ningyuan Wangb52786a2015-12-10 14:25:15 -0800105 type_ = static_cast<DHCP::ServiceType>(value.Get<int32_t>());
Ningyuan Wangab2af972015-12-03 13:57:40 -0800106 } else if (key == kConstantNetworkIdentifier &&
107 value.IsTypeCompatible<string>()) {
108 network_id_ = value.Get<string>();
109 } else if (key == kConstantRequestHostname &&
110 value.IsTypeCompatible<bool>()) {
111 request_hostname_ = value.Get<bool>();
112 } else if (key == kConstantArpGateway && value.IsTypeCompatible<bool>()) {
113 arp_gateway_ = value.Get<bool>();
114 } else if (key == kConstantUnicastArp && value.IsTypeCompatible<bool>()) {
115 unicast_arp_ = value.Get<bool>();
116 } else if (key == kConstantRequestNontemporaryAddress &&
117 value.IsTypeCompatible<bool>()) {
118 request_na_ = value.Get<bool>();
119 } else if (key == kConstantRequestPrefixDelegation &&
120 value.IsTypeCompatible<bool>()) {
121 request_pd_ = value.Get<bool>();
122 } else {
123 LOG(ERROR) << "Invalid configuration with key: " << key;
124 }
125 }
Ningyuan Wangb6836022015-12-02 14:39:01 -0800126}
127
128} // namespace dhcp_client
129