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