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 DHCP_CLIENT_DHCPV4_H_
18#define DHCP_CLIENT_DHCPV4_H_
19
20#include <random>
21#include <string>
22
23#include <base/macros.h>
24#include <base/strings/stringprintf.h>
25#include <shill/net/byte_string.h>
26#include <shill/net/io_handler_factory_container.h>
27#include <shill/net/sockets.h>
28
29#include "dhcp_client/dhcp.h"
30#include "dhcp_client/dhcp_message.h"
31#include "dhcp_client/event_dispatcher_interface.h"
32
33namespace dhcp_client {
34
35class DHCPV4 : public DHCP {
36 public:
37  DHCPV4(const std::string& interface_name,
38         const shill::ByteString& hardware_address,
39         unsigned int interface_index,
40         const std::string& network_id,
41         bool request_hostname,
42         bool arp_gateway,
43         bool unicast_arp,
44         EventDispatcherInterface* event_dispatcher);
45
46  virtual ~DHCPV4();
47
48  bool Start();
49  void Stop();
50
51 private:
52  bool CreateRawSocket();
53  bool MakeRawPacket(const DHCPMessage& message, shill::ByteString* buffer);
54  void OnReadError(const std::string& error_msg);
55  void ParseRawPacket(shill::InputData* data);
56  bool SendRawPacket(const shill::ByteString& buffer);
57  // Validate the IP and UDP header and return the total headers length.
58  // Return -1 if any header is invalid.
59  int ValidatePacketHeader(const unsigned char* buffer, size_t len);
60
61  void HandleOffer(const DHCPMessage& msg);
62  void HandleAck(const DHCPMessage& msg);
63  void HandleNak(const DHCPMessage& msg);
64
65  // Interface parameters.
66  std::string interface_name_;
67  shill::ByteString hardware_address_;
68  unsigned int interface_index_;
69
70  // Unique network/connection identifier,
71  // lease will persist to storage if this identifier is specified.
72  std::string network_id_;
73
74  // DHCP IPv4 configurations:
75  // Request hostname from server.
76  bool request_hostname_;
77  // ARP for default gateway.
78  bool arp_gateway_;
79  // Enable unicast ARP on renew.
80  bool unicast_arp_;
81
82  EventDispatcherInterface* event_dispatcher_;
83  shill::IOHandlerFactory *io_handler_factory_;
84  std::unique_ptr<shill::IOHandler> input_handler_;
85
86  // DHCP state variables.
87  State state_;
88  uint32_t server_identifier_;
89  uint32_t transaction_id_;
90  uint32_t from_;
91  uint32_t to_;
92
93  // Socket used for sending and receiving DHCP messages.
94  int socket_;
95  // Helper class with wrapped socket relavent functions.
96  std::unique_ptr<shill::Sockets> sockets_;
97
98  std::default_random_engine random_engine_;
99
100  DISALLOW_COPY_AND_ASSIGN(DHCPV4);
101};
102
103}  // namespace dhcp_client
104
105#endif  // DHCP_CLIENT_DHCPV4_H_
106