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(); |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 56 | // Initialize the data fields from a buffer with existing DHCP message. |
| 57 | // This is used for inbound DHCP message. |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 58 | static bool InitFromBuffer(const unsigned char* buffer, |
| 59 | size_t length, |
| 60 | DHCPMessage* message); |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 61 | static DHCPMessage InitRequest(); |
Ningyuan Wang | 679654b | 2016-01-08 15:43:36 -0800 | [diff] [blame] | 62 | static uint16_t ComputeChecksum(const uint8_t* data, size_t len); |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 63 | static uint32_t GenerateTransactionID(); |
| 64 | // Initialize part of the data fields for outbound DHCP message. |
| 65 | // Serialize the message to a buffer |
Ningyuan Wang | 2e8b937 | 2016-01-25 14:15:46 -0800 | [diff] [blame] | 66 | bool Serialize(shill::ByteString* data) const; |
Ningyuan Wang | 252c555 | 2016-01-19 15:07:32 -0800 | [diff] [blame] | 67 | |
Ningyuan Wang | 787f45d | 2016-01-28 13:03:23 -0800 | [diff] [blame] | 68 | // DHCP option and field setters |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 69 | void SetClientHardwareAddress( |
| 70 | const shill::ByteString& client_hardware_address); |
| 71 | void SetClientIdentifier(const shill::ByteString& client_identifier); |
| 72 | void SetClientIPAddress(uint32_t client_ip_address); |
Ningyuan Wang | 787f45d | 2016-01-28 13:03:23 -0800 | [diff] [blame] | 73 | void SetErrorMessage(const std::string& error_message); |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 74 | void SetLeaseTime(uint32_t lease_time); |
| 75 | void SetMessageType(uint8_t message_type); |
Ningyuan Wang | 3af3d83 | 2016-01-28 10:28:56 -0800 | [diff] [blame] | 76 | void SetParameterRequestList( |
| 77 | const std::vector<uint8_t>& parameter_request_list); |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 78 | void SetServerIdentifier(uint32_t server_identifier); |
| 79 | void SetTransactionID(uint32_t transaction_id); |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 80 | |
Ningyuan Wang | 787f45d | 2016-01-28 13:03:23 -0800 | [diff] [blame] | 81 | // DHCP option and field getters |
Ningyuan Wang | 63aa9b5 | 2016-01-19 10:24:59 -0800 | [diff] [blame] | 82 | const shill::ByteString& client_hardware_address() const { |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 83 | return client_hardware_address_; |
| 84 | } |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 85 | const shill::ByteString& client_identifier() const { |
| 86 | return client_identifier_; |
| 87 | } |
| 88 | uint32_t client_ip_address() const { return client_ip_address_; } |
Ningyuan Wang | 787f45d | 2016-01-28 13:03:23 -0800 | [diff] [blame] | 89 | const std::string& domain_name() const { return domain_name_; } |
| 90 | const std::string& error_message() const { return error_message_; } |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 91 | uint32_t lease_time() const { return lease_time_; } |
| 92 | uint8_t message_type() const { return message_type_; } |
| 93 | uint32_t rebinding_time() const { return rebinding_time_; } |
| 94 | uint32_t renewal_time() const { return renewal_time_; } |
| 95 | const std::vector<uint32_t>& router() const { return router_; } |
| 96 | uint32_t server_identifier() const { return server_identifier_; } |
| 97 | uint32_t subnet_mask() const { return subnet_mask_; } |
| 98 | uint32_t transaction_id() const { return transaction_id_; } |
| 99 | uint32_t your_ip_address() const { return your_ip_address_; } |
| 100 | const std::vector<uint32_t>& dns_server() const { return dns_server_; } |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 101 | |
| 102 | private: |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 103 | bool ParseDHCPOptions(const uint8_t* options, size_t options_length); |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 104 | bool IsValid(); |
Ningyuan Wang | 3030ddf | 2016-01-19 11:40:09 -0800 | [diff] [blame] | 105 | bool ContainsValidOptions(const std::set<uint8_t>& options_set); |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 106 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 107 | // Message type: request or reply. |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 108 | uint8_t opcode_; |
| 109 | // Hardware address type. |
| 110 | uint8_t hardware_address_type_; |
| 111 | // Hardware address length. |
| 112 | uint8_t hardware_address_length_; |
| 113 | // Client sets to zero, optionally used by relay agents |
| 114 | // when booting via a relay agent. |
| 115 | uint8_t relay_hops_; |
| 116 | // Transaction id. |
| 117 | uint32_t transaction_id_; |
| 118 | // Elapsed time from boot in seconds. |
| 119 | uint16_t seconds_; |
| 120 | // Broadcast flag |
| 121 | uint16_t flags_; |
| 122 | // Previously allocated client IP. |
| 123 | uint32_t client_ip_address_; |
| 124 | // Client IP address. |
| 125 | uint32_t your_ip_address_; |
| 126 | // IP address of next server to use in bootstrap; |
| 127 | // returned in DHCPOFFER, DHCPACK by server. |
| 128 | // It should be zero in client's messages. |
| 129 | uint32_t next_server_ip_address_; |
| 130 | // Relay agent IP address, used in booting via a relay agent. |
| 131 | // It should be zero in client's messages. |
| 132 | uint32_t agent_ip_address_; |
| 133 | // Client's hardware address. |
Ningyuan Wang | 63aa9b5 | 2016-01-19 10:24:59 -0800 | [diff] [blame] | 134 | shill::ByteString client_hardware_address_; |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 135 | // Server host name. |
| 136 | std::string servername_; |
| 137 | // Boot file name. |
| 138 | std::string bootfile_; |
| 139 | uint32_t cookie_; |
| 140 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 141 | // A map from DHCP Options number to corresponding callbacks. |
| 142 | std::map<uint8_t, ParserContext> options_map_; |
| 143 | |
| 144 | // Fields for DHCP Options. |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 145 | // Option 1: Subnet Mask. |
| 146 | uint32_t subnet_mask_; |
| 147 | // Option 3: Router(Default Gateway). |
| 148 | std::vector<uint32_t> router_; |
| 149 | // Option 6: Domain Name Server. |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 150 | std::vector<uint32_t> dns_server_; |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 151 | // Option 15: Domain Name. |
| 152 | std::string domain_name_; |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 153 | // Option 51: IP address lease time in unit of seconds. |
| 154 | uint32_t lease_time_; |
| 155 | // Option 53: DHCP message type. |
| 156 | uint8_t message_type_; |
| 157 | // Option 54: Server Identifier. |
| 158 | uint32_t server_identifier_; |
Ningyuan Wang | 3af3d83 | 2016-01-28 10:28:56 -0800 | [diff] [blame] | 159 | // Option 55: Parameter Request List. |
| 160 | std::vector<uint8_t> parameter_request_list_; |
Ningyuan Wang | 787f45d | 2016-01-28 13:03:23 -0800 | [diff] [blame] | 161 | // Option 56: (Error) Message. |
| 162 | std::string error_message_; |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 163 | // Option 58: Renewal time value in unit of seconds. |
| 164 | uint32_t renewal_time_; |
| 165 | // Option 59: Rebinding time value in unit of seconds. |
| 166 | uint32_t rebinding_time_; |
Ningyuan Wang | 599dffb | 2016-01-15 15:29:05 -0800 | [diff] [blame] | 167 | // Option 61: Client identifier. |
| 168 | shill::ByteString client_identifier_; |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame] | 169 | |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 170 | DISALLOW_COPY_AND_ASSIGN(DHCPMessage); |
| 171 | }; |
| 172 | |
| 173 | } // namespace dhcp_client |
| 174 | |
| 175 | #endif // DHCP_CLIENT_DHCP_MESSAGE_H_ |