Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -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_DHCP_MESSAGE_H_ |
| 18 | #define DHCP_CLIENT_DHCP_MESSAGE_H_ |
| 19 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <memory> |
Ningyuan Wang | 3030ddf | 2016-01-19 11:40:09 -0800 | [diff] [blame^] | 22 | #include <set> |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 23 | #include <string> |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 24 | #include <vector> |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 25 | |
| 26 | #include <base/macros.h> |
Ningyuan Wang | 63aa9b5 | 2016-01-19 10:24:59 -0800 | [diff] [blame] | 27 | #include <shill/net/byte_string.h> |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 28 | |
Ningyuan Wang | d4b8684 | 2016-01-13 11:28:54 -0800 | [diff] [blame] | 29 | #include "dhcp_client/dhcp_options_parser.h" |
| 30 | |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 31 | namespace dhcp_client { |
| 32 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 33 | static const uint8_t kDHCPMessageTypeDiscover = 1; |
| 34 | static const uint8_t kDHCPMessageTypeOffer = 2; |
| 35 | static const uint8_t kDHCPMessageTypeRequest = 3; |
| 36 | static const uint8_t kDHCPMessageTypeDecline = 4; |
| 37 | static const uint8_t kDHCPMessageTypeAck = 5; |
| 38 | static const uint8_t kDHCPMessageTypeNak = 6; |
| 39 | static const uint8_t kDHCPMessageTypeRelease = 7; |
| 40 | static const uint8_t kDHCPMessageTypeInform = 8; |
| 41 | |
| 42 | typedef std::unique_ptr<DHCPOptionsParser> ParserPtr; |
| 43 | |
| 44 | struct ParserContext{ |
| 45 | ParserPtr parser; |
| 46 | void* output; |
| 47 | ParserContext(DHCPOptionsParser* parser_ptr, void* output_ptr) |
| 48 | : parser(parser_ptr), |
| 49 | output(output_ptr) {} |
| 50 | }; |
| 51 | |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 52 | class DHCPMessage { |
| 53 | public: |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 54 | DHCPMessage(); |
| 55 | ~DHCPMessage(); |
| 56 | static bool InitFromBuffer(const unsigned char* buffer, |
| 57 | size_t length, |
| 58 | DHCPMessage* message); |
Ningyuan Wang | 679654b | 2016-01-08 15:43:36 -0800 | [diff] [blame] | 59 | static uint16_t ComputeChecksum(const uint8_t* data, size_t len); |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 60 | |
| 61 | uint8_t message_type() const {return message_type_;} |
| 62 | |
| 63 | uint32_t lease_time() const {return lease_time_;} |
| 64 | |
| 65 | uint32_t rebinding_time() const {return rebinding_time_;} |
| 66 | |
| 67 | uint32_t renewal_time() const {return renewal_time_;} |
| 68 | |
| 69 | uint32_t server_identifier() const {return server_identifier_;} |
| 70 | |
| 71 | uint32_t transaction_id() const {return transaction_id_;} |
| 72 | |
Ningyuan Wang | 3030ddf | 2016-01-19 11:40:09 -0800 | [diff] [blame^] | 73 | uint32_t your_ip_address() const {return your_ip_address_;} |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 74 | |
| 75 | const std::vector<uint32_t>& dns_server() const {return dns_server_;} |
| 76 | |
Ningyuan Wang | 63aa9b5 | 2016-01-19 10:24:59 -0800 | [diff] [blame] | 77 | const shill::ByteString& client_hardware_address() const { |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 78 | return client_hardware_address_; |
| 79 | } |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 80 | |
| 81 | private: |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 82 | bool ParseDHCPOptions(const uint8_t* options, size_t options_length); |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 83 | bool IsValid(); |
Ningyuan Wang | 3030ddf | 2016-01-19 11:40:09 -0800 | [diff] [blame^] | 84 | bool ContainsValidOptions(const std::set<uint8_t>& options_set); |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 85 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 86 | // Message type: request or reply. |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 87 | uint8_t opcode_; |
| 88 | // Hardware address type. |
| 89 | uint8_t hardware_address_type_; |
| 90 | // Hardware address length. |
| 91 | uint8_t hardware_address_length_; |
| 92 | // Client sets to zero, optionally used by relay agents |
| 93 | // when booting via a relay agent. |
| 94 | uint8_t relay_hops_; |
| 95 | // Transaction id. |
| 96 | uint32_t transaction_id_; |
| 97 | // Elapsed time from boot in seconds. |
| 98 | uint16_t seconds_; |
| 99 | // Broadcast flag |
| 100 | uint16_t flags_; |
| 101 | // Previously allocated client IP. |
| 102 | uint32_t client_ip_address_; |
| 103 | // Client IP address. |
| 104 | uint32_t your_ip_address_; |
| 105 | // IP address of next server to use in bootstrap; |
| 106 | // returned in DHCPOFFER, DHCPACK by server. |
| 107 | // It should be zero in client's messages. |
| 108 | uint32_t next_server_ip_address_; |
| 109 | // Relay agent IP address, used in booting via a relay agent. |
| 110 | // It should be zero in client's messages. |
| 111 | uint32_t agent_ip_address_; |
| 112 | // Client's hardware address. |
Ningyuan Wang | 63aa9b5 | 2016-01-19 10:24:59 -0800 | [diff] [blame] | 113 | shill::ByteString client_hardware_address_; |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 114 | // Server host name. |
| 115 | std::string servername_; |
| 116 | // Boot file name. |
| 117 | std::string bootfile_; |
| 118 | uint32_t cookie_; |
| 119 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 120 | // A map from DHCP Options number to corresponding callbacks. |
| 121 | std::map<uint8_t, ParserContext> options_map_; |
| 122 | |
| 123 | // Fields for DHCP Options. |
| 124 | // Option 6: Domain Name Server Option |
| 125 | std::vector<uint32_t> dns_server_; |
| 126 | // Option 51: IP address lease time in unit of seconds. |
| 127 | uint32_t lease_time_; |
| 128 | // Option 53: DHCP message type. |
| 129 | uint8_t message_type_; |
| 130 | // Option 54: Server Identifier. |
| 131 | uint32_t server_identifier_; |
| 132 | // Option 58: Renewal time value in unit of seconds. |
| 133 | uint32_t renewal_time_; |
| 134 | // Option 59: Rebinding time value in unit of seconds. |
| 135 | uint32_t rebinding_time_; |
| 136 | |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(DHCPMessage); |
| 138 | }; |
| 139 | |
| 140 | } // namespace dhcp_client |
| 141 | |
| 142 | #endif // DHCP_CLIENT_DHCP_MESSAGE_H_ |