View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   * 
19   */
20  
21  package org.apache.directory.server.dhcp.messages;
22  
23  import java.net.InetAddress;
24  
25  import org.apache.directory.server.dhcp.options.OptionsField;
26  
27  /**
28   * A DHCP (RFC 2131) message. Field descriptions contain the oroginal RFC field
29   * names in brackets.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 664295 $, $Date: 2008-06-07 09:48:16 +0200 (Sa, 07 Jun 2008) $
33   */
34  public class DhcpMessage {
35      /**
36       * Flag value: request broadcast answer.
37       */
38      public static final int FLAG_BROADCAST = 0x01;
39  
40      /**
41       * [yiaddr] 'your' (client) IP address.
42       */
43      private InetAddress assignedClientAddress;
44  
45      /**
46       * [file] Boot file name, null terminated string; "generic" name or null in
47       * DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
48       */
49      private String bootFileName;
50  
51      /**
52       * [ciaddr] Current client IP address; only filled in if client is in BOUND,
53       * RENEW or REBINDING state and can respond to ARP requests.
54       */
55      private InetAddress currentClientAddress;
56  
57      /**
58       * [flags] Flags. (LSB is broadcast flag)
59       */
60      private short flags;
61  
62      /**
63       * [hops] Client sets to zero, optionally used by relay agents when booting
64       * via a relay agent.
65       */
66      private short hopCount;
67  
68      /**
69       * [op] Message op code. 1 = BOOTREQUEST, 2 = BOOTREPLY, ...
70       */
71      private byte op;
72  
73      /**
74       * Operation constant: boot request (client to server).
75       * 
76       * @see #op
77       */
78      public static final byte OP_BOOTREQUEST = 1;
79  
80      /**
81       * Operation constant: boot reply (server to client).
82       * 
83       * @see #op
84       */
85      public static final byte OP_BOOTREPLY = 2;
86  
87      /**
88       * [siaddr] IP address of next server to use in bootstrap; returned in
89       * DHCPOFFER, DHCPACK by server.
90       */
91      private InetAddress nextServerAddress;
92  
93      /**
94       * [options] Optional parameters field. See the options documents for a list
95       * of defined options.
96       */
97      private OptionsField options = new OptionsField();
98  
99      /**
100      * [giaddr] Relay agent IP address, used in booting via a relay agent.
101      */
102     private InetAddress relayAgentAddress;
103 
104     /**
105      * [secs] Filled in by client, seconds elapsed since client began address
106      * acquisition or renewal process.
107      */
108     private int seconds;
109 
110     /**
111      * [sname] Optional server host name, null terminated string.
112      */
113     private String serverHostname;
114 
115     /**
116      * [xid] Transaction ID, a random number chosen by the client, used by the
117      * client and server to associate messages and responses between a client and
118      * a server.
119      */
120     private int transactionId;
121 
122     /**
123      * The DHCP message type option.
124      */
125     private MessageType messageType;
126 
127     private HardwareAddress hardwareAddress;
128 
129     /**
130      * Create a default dhcp message.
131      */
132     public DhcpMessage() {
133 
134     }
135 
136     /**
137      * Create a DHCP message based on the supplied values.
138      * 
139      * @param messageType
140      * @param op
141      * @param hardwareAddress
142      * @param hops
143      * @param transactionId
144      * @param seconds
145      * @param flags
146      * @param currentClientAddress
147      * @param assignedClientAddress
148      * @param nextServerAddress
149      * @param relayAgentAddress
150      * @param serverHostname
151      * @param bootFileName
152      * @param options
153      */
154     public DhcpMessage(MessageType messageType, byte op,
155             HardwareAddress hardwareAddress, short hops, int transactionId,
156             int seconds, short flags, InetAddress currentClientAddress,
157             InetAddress assignedClientAddress, InetAddress nextServerAddress,
158             InetAddress relayAgentAddress, String serverHostname,
159             String bootFileName, OptionsField options) {
160         this.messageType = messageType;
161         this.op = op;
162         this.hardwareAddress = hardwareAddress;
163         this.hopCount = hops;
164         this.transactionId = transactionId;
165         this.seconds = seconds;
166         this.flags = flags;
167         this.currentClientAddress = currentClientAddress;
168         this.assignedClientAddress = assignedClientAddress;
169         this.nextServerAddress = nextServerAddress;
170         this.relayAgentAddress = relayAgentAddress;
171         this.serverHostname = serverHostname;
172         this.bootFileName = bootFileName;
173         this.options = options;
174     }
175 
176     public InetAddress getAssignedClientAddress() {
177         return assignedClientAddress;
178     }
179 
180     public String getBootFileName() {
181         return bootFileName;
182     }
183 
184     public InetAddress getCurrentClientAddress() {
185         return currentClientAddress;
186     }
187 
188     public short getFlags() {
189         return flags;
190     }
191 
192     public short getHopCount() {
193         return hopCount;
194     }
195 
196     public MessageType getMessageType() {
197         return messageType;
198     }
199 
200     public InetAddress getNextServerAddress() {
201         return nextServerAddress;
202     }
203 
204     public OptionsField getOptions() {
205         return options;
206     }
207 
208     public InetAddress getRelayAgentAddress() {
209         return relayAgentAddress;
210     }
211 
212     public int getSeconds() {
213         return seconds;
214     }
215 
216     public String getServerHostname() {
217         return serverHostname;
218     }
219 
220     public int getTransactionId() {
221         return transactionId;
222     }
223 
224     public void setAssignedClientAddress(InetAddress assignedClientAddress) {
225         this.assignedClientAddress = assignedClientAddress;
226     }
227 
228     public void setBootFileName(String bootFileName) {
229         this.bootFileName = bootFileName;
230     }
231 
232     public void setCurrentClientAddress(InetAddress currentClientAddress) {
233         this.currentClientAddress = currentClientAddress;
234     }
235 
236     public void setFlags(short flags) {
237         this.flags = flags;
238     }
239 
240     public void setHopCount(short hopCount) {
241         this.hopCount = hopCount;
242     }
243 
244     public void setMessageType(MessageType messageType) {
245         this.messageType = messageType;
246     }
247 
248     public void setNextServerAddress(InetAddress nextServerAddress) {
249         this.nextServerAddress = nextServerAddress;
250     }
251 
252     public void setOptions(OptionsField options) {
253         this.options = options;
254     }
255 
256     public void setRelayAgentAddress(InetAddress relayAgentAddress) {
257         this.relayAgentAddress = relayAgentAddress;
258     }
259 
260     public void setSeconds(int seconds) {
261         this.seconds = seconds;
262     }
263 
264     public void setServerHostname(String serverHostname) {
265         this.serverHostname = serverHostname;
266     }
267 
268     public void setTransactionId(int transactionId) {
269         this.transactionId = transactionId;
270     }
271 
272     public byte getOp() {
273         return op;
274     }
275 
276     public void setOp(byte op) {
277         this.op = op;
278     }
279 
280     public HardwareAddress getHardwareAddress() {
281         return hardwareAddress;
282     }
283 
284     public void setHardwareAddress(HardwareAddress hardwareAddress) {
285         this.hardwareAddress = hardwareAddress;
286     }
287 
288     public String toString() {
289         StringBuilder sb = new StringBuilder();
290         sb.append(messageType).append(": hwAddress=").append(hardwareAddress)
291                 .append(", tx=").append(transactionId).append(", options=").append(
292                         options);
293 
294         return sb.toString();
295     }
296 }