blob: ebe2f5aa4319212ef6a42799e9f9cc0f477f8b22 [file] [log] [blame]
Ningyuan Wangb52786a2015-12-10 14:25:15 -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
17#ifndef DHCP_CLIENT_DHCPV4_H_
18#define DHCP_CLIENT_DHCPV4_H_
19
Ningyuan Wang81edb0d2016-01-29 15:57:42 -080020#include <random>
Ningyuan Wangb52786a2015-12-10 14:25:15 -080021#include <string>
22
23#include <base/macros.h>
24#include <base/strings/stringprintf.h>
Ningyuan Wang2e8b9372016-01-25 14:15:46 -080025#include <shill/net/byte_string.h>
26#include <shill/net/io_handler_factory_container.h>
27#include <shill/net/sockets.h>
Ningyuan Wangb52786a2015-12-10 14:25:15 -080028
29#include "dhcp_client/dhcp.h"
Ningyuan Wang2e8b9372016-01-25 14:15:46 -080030#include "dhcp_client/dhcp_message.h"
Ningyuan Wangb52786a2015-12-10 14:25:15 -080031#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,
Ningyuan Wang63aa9b52016-01-19 10:24:59 -080038 const shill::ByteString& hardware_address,
Ningyuan Wangb52786a2015-12-10 14:25:15 -080039 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
Ningyuan Wang288ccd02015-12-16 16:24:43 -080048 bool Start();
Ningyuan Wangb52786a2015-12-10 14:25:15 -080049 void Stop();
50
51 private:
Ningyuan Wang2e8b9372016-01-25 14:15:46 -080052 bool CreateRawSocket();
53 bool MakeRawPacket(const DHCPMessage& message, shill::ByteString* buffer);
54 void OnReadError(const std::string& error_msg);
Ningyuan Wangbea780a2016-01-27 13:53:41 -080055 void ParseRawPacket(shill::InputData* data);
Ningyuan Wang2e8b9372016-01-25 14:15:46 -080056 bool SendRawPacket(const shill::ByteString& buffer);
Ningyuan Wangbea780a2016-01-27 13:53:41 -080057 // 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);
Ningyuan Wang2e8b9372016-01-25 14:15:46 -080064
Ningyuan Wangb52786a2015-12-10 14:25:15 -080065 // Interface parameters.
66 std::string interface_name_;
Ningyuan Wang63aa9b52016-01-19 10:24:59 -080067 shill::ByteString hardware_address_;
Ningyuan Wangb52786a2015-12-10 14:25:15 -080068 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_;
Ningyuan Wang288ccd02015-12-16 16:24:43 -080083 shill::IOHandlerFactory *io_handler_factory_;
84 std::unique_ptr<shill::IOHandler> input_handler_;
Ningyuan Wangb52786a2015-12-10 14:25:15 -080085
Ningyuan Wang679654b2016-01-08 15:43:36 -080086 // DHCP state variables.
Ningyuan Wangb52786a2015-12-10 14:25:15 -080087 State state_;
Ningyuan Wangbea780a2016-01-27 13:53:41 -080088 uint32_t server_identifier_;
89 uint32_t transaction_id_;
90 uint32_t from_;
91 uint32_t to_;
Ningyuan Wangb52786a2015-12-10 14:25:15 -080092
Ningyuan Wang288ccd02015-12-16 16:24:43 -080093 // 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
Ningyuan Wang81edb0d2016-01-29 15:57:42 -080098 std::default_random_engine random_engine_;
99
Ningyuan Wangb52786a2015-12-10 14:25:15 -0800100 DISALLOW_COPY_AND_ASSIGN(DHCPV4);
101};
102
103} // namespace dhcp_client
104
105#endif // DHCP_CLIENT_DHCPV4_H_