blob: 78903d7b670ef8144d7e5b15fbf5a1976a5a3bd6 [file] [log] [blame]
//
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef DHCP_CLIENT_DHCPV4_H_
#define DHCP_CLIENT_DHCPV4_H_
#include <random>
#include <string>
#include <base/cancelable_callback.h>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <base/strings/stringprintf.h>
#include <shill/net/byte_string.h>
#include <shill/net/io_handler_factory_container.h>
#include <shill/net/sockets.h>
#include "dhcp_client/dhcp.h"
#include "dhcp_client/dhcp_message.h"
#include "dhcp_client/event_dispatcher_interface.h"
namespace dhcp_client {
class DHCPV4 : public DHCP {
public:
DHCPV4(const std::string& interface_name,
const shill::ByteString& hardware_address,
unsigned int interface_index,
const std::string& network_id,
bool request_hostname,
bool arp_gateway,
bool unicast_arp,
EventDispatcherInterface* event_dispatcher);
virtual ~DHCPV4();
bool Start();
void Stop();
private:
bool CreateRawSocket();
bool MakeRawPacket(const DHCPMessage& message, shill::ByteString* buffer);
void OnReadError(const std::string& error_msg);
void ParseRawPacket(shill::InputData* data);
bool SendDiscover();
bool SendRequest();
bool SendRelease();
bool SendRawPacket(const shill::ByteString& buffer);
// Validate the IP and UDP header and return the total headers length.
// Return -1 if any header is invalid.
int ValidatePacketHeader(const unsigned char* buffer, size_t len);
bool ValidateOptions(const DHCPMessage& msg);
void ResetStateVariables();
const std::string IPtoString(uint32_t ip);
void HandleOffer(const DHCPMessage& msg);
void HandleAck(const DHCPMessage& msg);
void HandleNak(const DHCPMessage& msg);
// Handle the case that it is time for a renewal process.
void RenewalTask();
// Handle the case that the client didn't reiceive a timely
// renewal responese from server. Therefore it is time for
// a rebinding process.
void RebindTask();
// Interface parameters.
std::string interface_name_;
shill::ByteString hardware_address_;
unsigned int interface_index_;
// Unique network/connection identifier,
// lease will persist to storage if this identifier is specified.
std::string network_id_;
// DHCP IPv4 configurations:
// Request hostname from server.
bool request_hostname_;
// ARP for default gateway.
bool arp_gateway_;
// Enable unicast ARP on renew.
bool unicast_arp_;
EventDispatcherInterface* event_dispatcher_;
shill::IOHandlerFactory *io_handler_factory_;
std::unique_ptr<shill::IOHandler> input_handler_;
base::WeakPtrFactory<DHCPV4> weak_ptr_factory_;
// DHCP state variables.
State state_;
uint32_t server_identifier_;
uint32_t transaction_id_;
uint32_t offered_ip_address_;
uint32_t subnet_mask_;
// Client IP address.
// It can be either a bounded ip address or an INADDR_ANY constant.
uint32_t client_ip_;
// Server IP address.
// It can be either a bounded server address or an INADDR_BROADCAST constant.
uint32_t server_ip_;
// Timeout callbacks.
base::CancelableClosure renewal_task_callback_;
base::CancelableClosure rebind_task_callback_;
// Socket used for sending and receiving DHCP messages.
int socket_;
// Helper class with wrapped socket relavent functions.
std::unique_ptr<shill::Sockets> sockets_;
std::default_random_engine random_engine_;
DISALLOW_COPY_AND_ASSIGN(DHCPV4);
};
} // namespace dhcp_client
#endif // DHCP_CLIENT_DHCPV4_H_