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