blob: da12d6c1cffbb4df5812e31c05c911eb6adac754 [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 Wang679654b2016-01-08 15:43:36 -080057 static uint16_t ComputeChecksum(const uint8_t* data, size_t len);
Ningyuan Wang37b081d2016-01-12 10:29:39 -080058
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 Wang48f82042016-01-04 11:28:09 -080078
79 private:
Ningyuan Wang37b081d2016-01-12 10:29:39 -080080 bool ParseDHCPOptions(const uint8_t* options, size_t options_length);
Ningyuan Wang48f82042016-01-04 11:28:09 -080081 bool IsValid();
82
Ningyuan Wang37b081d2016-01-12 10:29:39 -080083 // Message type: request or reply.
Ningyuan Wang48f82042016-01-04 11:28:09 -080084 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 Wang37b081d2016-01-12 10:29:39 -0800117 // 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 Wang48f82042016-01-04 11:28:09 -0800134 DISALLOW_COPY_AND_ASSIGN(DHCPMessage);
135};
136
137} // namespace dhcp_client
138
139#endif // DHCP_CLIENT_DHCP_MESSAGE_H_