blob: a839ee1acd035e111a7d99f761c2df5d36ce9f64 [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 Wang3030ddf2016-01-19 11:40:09 -080022#include <set>
Ningyuan Wang48f82042016-01-04 11:28:09 -080023#include <string>
Ningyuan Wang37b081d2016-01-12 10:29:39 -080024#include <vector>
Ningyuan Wang48f82042016-01-04 11:28:09 -080025
26#include <base/macros.h>
Ningyuan Wang63aa9b52016-01-19 10:24:59 -080027#include <shill/net/byte_string.h>
Ningyuan Wang37b081d2016-01-12 10:29:39 -080028
Ningyuan Wangd4b86842016-01-13 11:28:54 -080029#include "dhcp_client/dhcp_options_parser.h"
30
Ningyuan Wang48f82042016-01-04 11:28:09 -080031namespace dhcp_client {
32
Ningyuan Wang37b081d2016-01-12 10:29:39 -080033static const uint8_t kDHCPMessageTypeDiscover = 1;
34static const uint8_t kDHCPMessageTypeOffer = 2;
35static const uint8_t kDHCPMessageTypeRequest = 3;
36static const uint8_t kDHCPMessageTypeDecline = 4;
37static const uint8_t kDHCPMessageTypeAck = 5;
38static const uint8_t kDHCPMessageTypeNak = 6;
39static const uint8_t kDHCPMessageTypeRelease = 7;
40static const uint8_t kDHCPMessageTypeInform = 8;
41
42typedef std::unique_ptr<DHCPOptionsParser> ParserPtr;
43
44struct ParserContext{
45 ParserPtr parser;
46 void* output;
47 ParserContext(DHCPOptionsParser* parser_ptr, void* output_ptr)
48 : parser(parser_ptr),
49 output(output_ptr) {}
50};
51
Ningyuan Wang48f82042016-01-04 11:28:09 -080052class DHCPMessage {
53 public:
Ningyuan Wang48f82042016-01-04 11:28:09 -080054 DHCPMessage();
55 ~DHCPMessage();
56 static bool InitFromBuffer(const unsigned char* buffer,
57 size_t length,
58 DHCPMessage* message);
Ningyuan Wang679654b2016-01-08 15:43:36 -080059 static uint16_t ComputeChecksum(const uint8_t* data, size_t len);
Ningyuan Wang37b081d2016-01-12 10:29:39 -080060
61 uint8_t message_type() const {return message_type_;}
62
63 uint32_t lease_time() const {return lease_time_;}
64
65 uint32_t rebinding_time() const {return rebinding_time_;}
66
67 uint32_t renewal_time() const {return renewal_time_;}
68
69 uint32_t server_identifier() const {return server_identifier_;}
70
71 uint32_t transaction_id() const {return transaction_id_;}
72
Ningyuan Wang3030ddf2016-01-19 11:40:09 -080073 uint32_t your_ip_address() const {return your_ip_address_;}
Ningyuan Wang37b081d2016-01-12 10:29:39 -080074
75 const std::vector<uint32_t>& dns_server() const {return dns_server_;}
76
Ningyuan Wang63aa9b52016-01-19 10:24:59 -080077 const shill::ByteString& client_hardware_address() const {
Ningyuan Wang37b081d2016-01-12 10:29:39 -080078 return client_hardware_address_;
79 }
Ningyuan Wang48f82042016-01-04 11:28:09 -080080
81 private:
Ningyuan Wang37b081d2016-01-12 10:29:39 -080082 bool ParseDHCPOptions(const uint8_t* options, size_t options_length);
Ningyuan Wang48f82042016-01-04 11:28:09 -080083 bool IsValid();
Ningyuan Wang3030ddf2016-01-19 11:40:09 -080084 bool ContainsValidOptions(const std::set<uint8_t>& options_set);
Ningyuan Wang48f82042016-01-04 11:28:09 -080085
Ningyuan Wang37b081d2016-01-12 10:29:39 -080086 // Message type: request or reply.
Ningyuan Wang48f82042016-01-04 11:28:09 -080087 uint8_t opcode_;
88 // Hardware address type.
89 uint8_t hardware_address_type_;
90 // Hardware address length.
91 uint8_t hardware_address_length_;
92 // Client sets to zero, optionally used by relay agents
93 // when booting via a relay agent.
94 uint8_t relay_hops_;
95 // Transaction id.
96 uint32_t transaction_id_;
97 // Elapsed time from boot in seconds.
98 uint16_t seconds_;
99 // Broadcast flag
100 uint16_t flags_;
101 // Previously allocated client IP.
102 uint32_t client_ip_address_;
103 // Client IP address.
104 uint32_t your_ip_address_;
105 // IP address of next server to use in bootstrap;
106 // returned in DHCPOFFER, DHCPACK by server.
107 // It should be zero in client's messages.
108 uint32_t next_server_ip_address_;
109 // Relay agent IP address, used in booting via a relay agent.
110 // It should be zero in client's messages.
111 uint32_t agent_ip_address_;
112 // Client's hardware address.
Ningyuan Wang63aa9b52016-01-19 10:24:59 -0800113 shill::ByteString client_hardware_address_;
Ningyuan Wang48f82042016-01-04 11:28:09 -0800114 // Server host name.
115 std::string servername_;
116 // Boot file name.
117 std::string bootfile_;
118 uint32_t cookie_;
119
Ningyuan Wang37b081d2016-01-12 10:29:39 -0800120 // A map from DHCP Options number to corresponding callbacks.
121 std::map<uint8_t, ParserContext> options_map_;
122
123 // Fields for DHCP Options.
124 // Option 6: Domain Name Server Option
125 std::vector<uint32_t> dns_server_;
126 // Option 51: IP address lease time in unit of seconds.
127 uint32_t lease_time_;
128 // Option 53: DHCP message type.
129 uint8_t message_type_;
130 // Option 54: Server Identifier.
131 uint32_t server_identifier_;
132 // Option 58: Renewal time value in unit of seconds.
133 uint32_t renewal_time_;
134 // Option 59: Rebinding time value in unit of seconds.
135 uint32_t rebinding_time_;
136
Ningyuan Wang48f82042016-01-04 11:28:09 -0800137 DISALLOW_COPY_AND_ASSIGN(DHCPMessage);
138};
139
140} // namespace dhcp_client
141
142#endif // DHCP_CLIENT_DHCP_MESSAGE_H_