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 | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame^] | 57 | |
| 58 | uint8_t message_type() const {return message_type_;} |
| 59 | |
| 60 | uint32_t lease_time() const {return lease_time_;} |
| 61 | |
| 62 | uint32_t rebinding_time() const {return rebinding_time_;} |
| 63 | |
| 64 | uint32_t renewal_time() const {return renewal_time_;} |
| 65 | |
| 66 | uint32_t server_identifier() const {return server_identifier_;} |
| 67 | |
| 68 | uint32_t transaction_id() const {return transaction_id_;} |
| 69 | |
| 70 | uint32_t your_ip_Address() const {return your_ip_address_;} |
| 71 | |
| 72 | const std::vector<uint32_t>& dns_server() const {return dns_server_;} |
| 73 | |
| 74 | const std::string& client_hardware_address() const { |
| 75 | return client_hardware_address_; |
| 76 | } |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 77 | |
| 78 | private: |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame^] | 79 | bool ParseDHCPOptions(const uint8_t* options, size_t options_length); |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 80 | bool IsValid(); |
| 81 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame^] | 82 | // Message type: request or reply. |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 83 | uint8_t opcode_; |
| 84 | // Hardware address type. |
| 85 | uint8_t hardware_address_type_; |
| 86 | // Hardware address length. |
| 87 | uint8_t hardware_address_length_; |
| 88 | // Client sets to zero, optionally used by relay agents |
| 89 | // when booting via a relay agent. |
| 90 | uint8_t relay_hops_; |
| 91 | // Transaction id. |
| 92 | uint32_t transaction_id_; |
| 93 | // Elapsed time from boot in seconds. |
| 94 | uint16_t seconds_; |
| 95 | // Broadcast flag |
| 96 | uint16_t flags_; |
| 97 | // Previously allocated client IP. |
| 98 | uint32_t client_ip_address_; |
| 99 | // Client IP address. |
| 100 | uint32_t your_ip_address_; |
| 101 | // IP address of next server to use in bootstrap; |
| 102 | // returned in DHCPOFFER, DHCPACK by server. |
| 103 | // It should be zero in client's messages. |
| 104 | uint32_t next_server_ip_address_; |
| 105 | // Relay agent IP address, used in booting via a relay agent. |
| 106 | // It should be zero in client's messages. |
| 107 | uint32_t agent_ip_address_; |
| 108 | // Client's hardware address. |
| 109 | std::string client_hardware_address_; |
| 110 | // Server host name. |
| 111 | std::string servername_; |
| 112 | // Boot file name. |
| 113 | std::string bootfile_; |
| 114 | uint32_t cookie_; |
| 115 | |
Ningyuan Wang | 37b081d | 2016-01-12 10:29:39 -0800 | [diff] [blame^] | 116 | // A map from DHCP Options number to corresponding callbacks. |
| 117 | std::map<uint8_t, ParserContext> options_map_; |
| 118 | |
| 119 | // Fields for DHCP Options. |
| 120 | // Option 6: Domain Name Server Option |
| 121 | std::vector<uint32_t> dns_server_; |
| 122 | // Option 51: IP address lease time in unit of seconds. |
| 123 | uint32_t lease_time_; |
| 124 | // Option 53: DHCP message type. |
| 125 | uint8_t message_type_; |
| 126 | // Option 54: Server Identifier. |
| 127 | uint32_t server_identifier_; |
| 128 | // Option 58: Renewal time value in unit of seconds. |
| 129 | uint32_t renewal_time_; |
| 130 | // Option 59: Rebinding time value in unit of seconds. |
| 131 | uint32_t rebinding_time_; |
| 132 | |
Ningyuan Wang | 48f8204 | 2016-01-04 11:28:09 -0800 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(DHCPMessage); |
| 134 | }; |
| 135 | |
| 136 | } // namespace dhcp_client |
| 137 | |
| 138 | #endif // DHCP_CLIENT_DHCP_MESSAGE_H_ |