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 20 #include <map> 21 #include <memory> 22 #include <set> 23 #include <string> 24 #include <vector> 25 26 #include <base/macros.h> 27 #include <shill/net/byte_string.h> 28 29 #include "dhcp_client/dhcp_options_parser.h" 30 31 namespace dhcp_client { 32 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; ParserContextParserContext47 ParserContext(DHCPOptionsParser* parser_ptr, void* output_ptr) 48 : parser(parser_ptr), 49 output(output_ptr) {} 50 }; 51 52 class DHCPMessage { 53 public: 54 DHCPMessage(); 55 ~DHCPMessage(); 56 // Initialize the data fields from a buffer with existing DHCP message. 57 // This is used for inbound DHCP message. 58 static bool InitFromBuffer(const unsigned char* buffer, 59 size_t length, 60 DHCPMessage* message); 61 static void InitRequest(DHCPMessage* message); 62 static uint16_t ComputeChecksum(const uint8_t* data, size_t len); 63 // Initialize part of the data fields for outbound DHCP message. 64 // Serialize the message to a buffer 65 bool Serialize(shill::ByteString* data) const; 66 67 // DHCP option and field setters 68 void SetClientHardwareAddress( 69 const shill::ByteString& client_hardware_address); 70 void SetClientIdentifier(const shill::ByteString& client_identifier); 71 void SetClientIPAddress(uint32_t client_ip_address); 72 void SetErrorMessage(const std::string& error_message); 73 void SetLeaseTime(uint32_t lease_time); 74 void SetMessageType(uint8_t message_type); 75 void SetParameterRequestList( 76 const std::vector<uint8_t>& parameter_request_list); 77 void SetRequestedIpAddress(uint32_t requested_ip_address); 78 void SetServerIdentifier(uint32_t server_identifier); 79 void SetTransactionID(uint32_t transaction_id); 80 void SetVendorSpecificInfo(const shill::ByteString& vendor_specific_info); 81 82 // DHCP option and field getters client_hardware_address()83 const shill::ByteString& client_hardware_address() const { 84 return client_hardware_address_; 85 } client_identifier()86 const shill::ByteString& client_identifier() const { 87 return client_identifier_; 88 } client_ip_address()89 uint32_t client_ip_address() const { return client_ip_address_; } dns_server()90 const std::vector<uint32_t>& dns_server() const { return dns_server_; } domain_name()91 const std::string& domain_name() const { return domain_name_; } error_message()92 const std::string& error_message() const { return error_message_; } lease_time()93 uint32_t lease_time() const { return lease_time_; } message_type()94 uint8_t message_type() const { return message_type_; } rebinding_time()95 uint32_t rebinding_time() const { return rebinding_time_; } renewal_time()96 uint32_t renewal_time() const { return renewal_time_; } router()97 const std::vector<uint32_t>& router() const { return router_; } server_identifier()98 uint32_t server_identifier() const { return server_identifier_; } subnet_mask()99 uint32_t subnet_mask() const { return subnet_mask_; } transaction_id()100 uint32_t transaction_id() const { return transaction_id_; } vendor_specific_info()101 const shill::ByteString& vendor_specific_info() const { 102 return vendor_specific_info_; 103 } your_ip_address()104 uint32_t your_ip_address() const { return your_ip_address_; } 105 106 private: 107 bool ParseDHCPOptions(const uint8_t* options, size_t options_length); 108 bool IsValid(); 109 bool ContainsValidOptions(const std::set<uint8_t>& options_set); 110 111 // Message type: request or reply. 112 uint8_t opcode_; 113 // Hardware address type. 114 uint8_t hardware_address_type_; 115 // Hardware address length. 116 uint8_t hardware_address_length_; 117 // Client sets to zero, optionally used by relay agents 118 // when booting via a relay agent. 119 uint8_t relay_hops_; 120 // Transaction id. 121 uint32_t transaction_id_; 122 // Elapsed time from boot in seconds. 123 uint16_t seconds_; 124 // Broadcast flag 125 uint16_t flags_; 126 // Previously allocated client IP. 127 uint32_t client_ip_address_; 128 // Client IP address. 129 uint32_t your_ip_address_; 130 // IP address of next server to use in bootstrap; 131 // returned in DHCPOFFER, DHCPACK by server. 132 // It should be zero in client's messages. 133 uint32_t next_server_ip_address_; 134 // Relay agent IP address, used in booting via a relay agent. 135 // It should be zero in client's messages. 136 uint32_t agent_ip_address_; 137 // Client's hardware address. 138 shill::ByteString client_hardware_address_; 139 // Server host name. 140 std::string servername_; 141 // Boot file name. 142 std::string bootfile_; 143 uint32_t cookie_; 144 145 // A map from DHCP Options number to corresponding callbacks. 146 std::map<uint8_t, ParserContext> options_map_; 147 148 // Fields for DHCP Options. 149 // Option 1: Subnet Mask. 150 uint32_t subnet_mask_; 151 // Option 3: Router(Default Gateway). 152 std::vector<uint32_t> router_; 153 // Option 6: Domain Name Server. 154 std::vector<uint32_t> dns_server_; 155 // Option 15: Domain Name. 156 std::string domain_name_; 157 // Option 43: Vendor Specific Information. 158 shill::ByteString vendor_specific_info_; 159 // Option 50: Requested IP Address. 160 uint32_t requested_ip_address_; 161 // Option 51: IP address lease time in unit of seconds. 162 uint32_t lease_time_; 163 // Option 53: DHCP message type. 164 uint8_t message_type_; 165 // Option 54: Server Identifier. 166 uint32_t server_identifier_; 167 // Option 55: Parameter Request List. 168 std::vector<uint8_t> parameter_request_list_; 169 // Option 56: (Error) Message. 170 std::string error_message_; 171 // Option 58: Renewal time value in unit of seconds. 172 uint32_t renewal_time_; 173 // Option 59: Rebinding time value in unit of seconds. 174 uint32_t rebinding_time_; 175 // Option 61: Client identifier. 176 shill::ByteString client_identifier_; 177 178 DISALLOW_COPY_AND_ASSIGN(DHCPMessage); 179 }; 180 181 } // namespace dhcp_client 182 183 #endif // DHCP_CLIENT_DHCP_MESSAGE_H_ 184