blob: faef9e35b5b4afbc1c386d50e6461d245d6a5331 [file] [log] [blame]
Ningyuan Wang48f82042016-01-04 11:28:09 -08001//
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 Wang37b081d2016-01-12 10:29:39 -080020#include <map>
21#include <memory>
Ningyuan Wang48f82042016-01-04 11:28:09 -080022#include <string>
Ningyuan Wang37b081d2016-01-12 10:29:39 -080023#include <vector>
Ningyuan Wang48f82042016-01-04 11:28:09 -080024
25#include <base/macros.h>
26
Ningyuan Wang37b081d2016-01-12 10:29:39 -080027#include <dhcp_client/dhcp_options_parser.h>
28
Ningyuan Wang48f82042016-01-04 11:28:09 -080029namespace dhcp_client {
30
Ningyuan Wang37b081d2016-01-12 10:29:39 -080031static const uint8_t kDHCPMessageTypeDiscover = 1;
32static const uint8_t kDHCPMessageTypeOffer = 2;
33static const uint8_t kDHCPMessageTypeRequest = 3;
34static const uint8_t kDHCPMessageTypeDecline = 4;
35static const uint8_t kDHCPMessageTypeAck = 5;
36static const uint8_t kDHCPMessageTypeNak = 6;
37static const uint8_t kDHCPMessageTypeRelease = 7;
38static const uint8_t kDHCPMessageTypeInform = 8;
39
40typedef std::unique_ptr<DHCPOptionsParser> ParserPtr;
41
42struct 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 Wang48f82042016-01-04 11:28:09 -080050class DHCPMessage {
51 public:
Ningyuan Wang48f82042016-01-04 11:28:09 -080052 DHCPMessage();
53 ~DHCPMessage();
54 static bool InitFromBuffer(const unsigned char* buffer,
55 size_t length,
56 DHCPMessage* message);
Ningyuan Wang37b081d2016-01-12 10:29:39 -080057
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 Wang48f82042016-01-04 11:28:09 -080077
78 private:
Ningyuan Wang37b081d2016-01-12 10:29:39 -080079 bool ParseDHCPOptions(const uint8_t* options, size_t options_length);
Ningyuan Wang48f82042016-01-04 11:28:09 -080080 bool IsValid();
81
Ningyuan Wang37b081d2016-01-12 10:29:39 -080082 // Message type: request or reply.
Ningyuan Wang48f82042016-01-04 11:28:09 -080083 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 Wang37b081d2016-01-12 10:29:39 -0800116 // 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 Wang48f82042016-01-04 11:28:09 -0800133 DISALLOW_COPY_AND_ASSIGN(DHCPMessage);
134};
135
136} // namespace dhcp_client
137
138#endif // DHCP_CLIENT_DHCP_MESSAGE_H_