Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 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 | |
Ningyuan Wang | 81edb0d | 2016-01-29 15:57:42 -0800 | [diff] [blame] | 20 | #include <random> |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 21 | #include <string> |
| 22 | |
| 23 | #include <base/macros.h> |
| 24 | #include <base/strings/stringprintf.h> |
Ningyuan Wang | 2e8b937 | 2016-01-25 14:15:46 -0800 | [diff] [blame] | 25 | #include <shill/net/byte_string.h> |
| 26 | #include <shill/net/io_handler_factory_container.h> |
| 27 | #include <shill/net/sockets.h> |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 28 | |
| 29 | #include "dhcp_client/dhcp.h" |
Ningyuan Wang | 2e8b937 | 2016-01-25 14:15:46 -0800 | [diff] [blame] | 30 | #include "dhcp_client/dhcp_message.h" |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 31 | #include "dhcp_client/event_dispatcher_interface.h" |
| 32 | |
| 33 | namespace dhcp_client { |
| 34 | |
| 35 | class DHCPV4 : public DHCP { |
| 36 | public: |
| 37 | DHCPV4(const std::string& interface_name, |
Ningyuan Wang | 63aa9b5 | 2016-01-19 10:24:59 -0800 | [diff] [blame] | 38 | const shill::ByteString& hardware_address, |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 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 | |
Ningyuan Wang | 288ccd0 | 2015-12-16 16:24:43 -0800 | [diff] [blame] | 48 | bool Start(); |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 49 | void Stop(); |
| 50 | |
| 51 | private: |
Ningyuan Wang | 2e8b937 | 2016-01-25 14:15:46 -0800 | [diff] [blame] | 52 | bool CreateRawSocket(); |
| 53 | bool MakeRawPacket(const DHCPMessage& message, shill::ByteString* buffer); |
| 54 | void OnReadError(const std::string& error_msg); |
Ningyuan Wang | bea780a | 2016-01-27 13:53:41 -0800 | [diff] [blame] | 55 | void ParseRawPacket(shill::InputData* data); |
Ningyuan Wang | 2e8b937 | 2016-01-25 14:15:46 -0800 | [diff] [blame] | 56 | bool SendRawPacket(const shill::ByteString& buffer); |
Ningyuan Wang | bea780a | 2016-01-27 13:53:41 -0800 | [diff] [blame] | 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); |
Ningyuan Wang | 2e8b937 | 2016-01-25 14:15:46 -0800 | [diff] [blame] | 64 | |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 65 | // Interface parameters. |
| 66 | std::string interface_name_; |
Ningyuan Wang | 63aa9b5 | 2016-01-19 10:24:59 -0800 | [diff] [blame] | 67 | shill::ByteString hardware_address_; |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 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_; |
Ningyuan Wang | 288ccd0 | 2015-12-16 16:24:43 -0800 | [diff] [blame] | 83 | shill::IOHandlerFactory *io_handler_factory_; |
| 84 | std::unique_ptr<shill::IOHandler> input_handler_; |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 85 | |
Ningyuan Wang | 679654b | 2016-01-08 15:43:36 -0800 | [diff] [blame] | 86 | // DHCP state variables. |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 87 | State state_; |
Ningyuan Wang | bea780a | 2016-01-27 13:53:41 -0800 | [diff] [blame] | 88 | uint32_t server_identifier_; |
| 89 | uint32_t transaction_id_; |
| 90 | uint32_t from_; |
| 91 | uint32_t to_; |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 92 | |
Ningyuan Wang | 288ccd0 | 2015-12-16 16:24:43 -0800 | [diff] [blame] | 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 | |
Ningyuan Wang | 81edb0d | 2016-01-29 15:57:42 -0800 | [diff] [blame] | 98 | std::default_random_engine random_engine_; |
| 99 | |
Ningyuan Wang | b52786a | 2015-12-10 14:25:15 -0800 | [diff] [blame] | 100 | DISALLOW_COPY_AND_ASSIGN(DHCPV4); |
| 101 | }; |
| 102 | |
| 103 | } // namespace dhcp_client |
| 104 | |
| 105 | #endif // DHCP_CLIENT_DHCPV4_H_ |