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