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  
24  import java.net.InetAddress;
25  
26  import org.apache.directory.server.dhcp.options.OptionsField;
27  
28  
29  /**
30   * A DHCP (RFC 2131) message. Field descriptions contain the oroginal RFC field
31   * names in brackets.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class DhcpMessage
36  {
37      /**
38       * Flag value: request broadcast answer.
39       */
40      public static final int FLAG_BROADCAST = 0x01;
41  
42      /**
43       * [yiaddr] 'your' (client) IP address.
44       */
45      private InetAddress assignedClientAddress;
46  
47      /**
48       * [file] Boot file name, null terminated string; "generic" name or null in
49       * DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
50       */
51      private String bootFileName;
52  
53      /**
54       * [ciaddr] Current client IP address; only filled in if client is in BOUND,
55       * RENEW or REBINDING state and can respond to ARP requests.
56       */
57      private InetAddress currentClientAddress;
58  
59      /**
60       * [flags] Flags. (LSB is broadcast flag)
61       */
62      private short flags;
63  
64      /**
65       * [hops] Client sets to zero, optionally used by relay agents when booting
66       * via a relay agent.
67       */
68      private short hopCount;
69  
70      /**
71       * [op] Message op code. 1 = BOOTREQUEST, 2 = BOOTREPLY, ...
72       */
73      private byte op;
74  
75      /**
76       * Operation constant: boot request (client to server).
77       * 
78       * @see #op
79       */
80      public static final byte OP_BOOTREQUEST = 1;
81  
82      /**
83       * Operation constant: boot reply (server to client).
84       * 
85       * @see #op
86       */
87      public static final byte OP_BOOTREPLY = 2;
88  
89      /**
90       * [siaddr] IP address of next server to use in bootstrap; returned in
91       * DHCPOFFER, DHCPACK by server.
92       */
93      private InetAddress nextServerAddress;
94  
95      /**
96       * [options] Optional parameters field. See the options documents for a list
97       * of defined options.
98       */
99      private OptionsFieldhcp/options/OptionsField.html#OptionsField">OptionsField options = new OptionsField();
100 
101     /**
102      * [giaddr] Relay agent IP address, used in booting via a relay agent.
103      */
104     private InetAddress relayAgentAddress;
105 
106     /**
107      * [secs] Filled in by client, seconds elapsed since client began address
108      * acquisition or renewal process.
109      */
110     private int seconds;
111 
112     /**
113      * [sname] Optional server host name, null terminated string.
114      */
115     private String serverHostname;
116 
117     /**
118      * [xid] Transaction ID, a random number chosen by the client, used by the
119      * client and server to associate messages and responses between a client and
120      * a server.
121      */
122     private int transactionId;
123 
124     /**
125      * The DHCP message type option.
126      */
127     private MessageType messageType;
128 
129     private HardwareAddress hardwareAddress;
130 
131 
132     /**
133      * Create a default dhcp message.
134      */
135     public DhcpMessage()
136     {
137 
138     }
139 
140 
141     /**
142      * Create a DHCP message based on the supplied values.
143      * 
144      * @param messageType
145      * @param op
146      * @param hardwareAddress
147      * @param hops
148      * @param transactionId
149      * @param seconds
150      * @param flags
151      * @param currentClientAddress
152      * @param assignedClientAddress
153      * @param nextServerAddress
154      * @param relayAgentAddress
155      * @param serverHostname
156      * @param bootFileName
157      * @param options
158      */
159     public DhcpMessage( MessageType messageType, byte op,
160         HardwareAddress hardwareAddress, short hops, int transactionId,
161         int seconds, short flags, InetAddress currentClientAddress,
162         InetAddress assignedClientAddress, InetAddress nextServerAddress,
163         InetAddress relayAgentAddress, String serverHostname,
164         String bootFileName, OptionsField options )
165     {
166         this.messageType = messageType;
167         this.op = op;
168         this.hardwareAddress = hardwareAddress;
169         this.hopCount = hops;
170         this.transactionId = transactionId;
171         this.seconds = seconds;
172         this.flags = flags;
173         this.currentClientAddress = currentClientAddress;
174         this.assignedClientAddress = assignedClientAddress;
175         this.nextServerAddress = nextServerAddress;
176         this.relayAgentAddress = relayAgentAddress;
177         this.serverHostname = serverHostname;
178         this.bootFileName = bootFileName;
179         this.options = options;
180     }
181 
182 
183     public InetAddress getAssignedClientAddress()
184     {
185         return assignedClientAddress;
186     }
187 
188 
189     public String getBootFileName()
190     {
191         return bootFileName;
192     }
193 
194 
195     public InetAddress getCurrentClientAddress()
196     {
197         return currentClientAddress;
198     }
199 
200 
201     public short getFlags()
202     {
203         return flags;
204     }
205 
206 
207     public short getHopCount()
208     {
209         return hopCount;
210     }
211 
212 
213     public MessageType getMessageType()
214     {
215         return messageType;
216     }
217 
218 
219     public InetAddress getNextServerAddress()
220     {
221         return nextServerAddress;
222     }
223 
224 
225     public OptionsField getOptions()
226     {
227         return options;
228     }
229 
230 
231     public InetAddress getRelayAgentAddress()
232     {
233         return relayAgentAddress;
234     }
235 
236 
237     public int getSeconds()
238     {
239         return seconds;
240     }
241 
242 
243     public String getServerHostname()
244     {
245         return serverHostname;
246     }
247 
248 
249     public int getTransactionId()
250     {
251         return transactionId;
252     }
253 
254 
255     public void setAssignedClientAddress( InetAddress assignedClientAddress )
256     {
257         this.assignedClientAddress = assignedClientAddress;
258     }
259 
260 
261     public void setBootFileName( String bootFileName )
262     {
263         this.bootFileName = bootFileName;
264     }
265 
266 
267     public void setCurrentClientAddress( InetAddress currentClientAddress )
268     {
269         this.currentClientAddress = currentClientAddress;
270     }
271 
272 
273     public void setFlags( short flags )
274     {
275         this.flags = flags;
276     }
277 
278 
279     public void setHopCount( short hopCount )
280     {
281         this.hopCount = hopCount;
282     }
283 
284 
285     public void setMessageType( MessageType messageType )
286     {
287         this.messageType = messageType;
288     }
289 
290 
291     public void setNextServerAddress( InetAddress nextServerAddress )
292     {
293         this.nextServerAddress = nextServerAddress;
294     }
295 
296 
297     public void setOptions( OptionsField options )
298     {
299         this.options = options;
300     }
301 
302 
303     public void setRelayAgentAddress( InetAddress relayAgentAddress )
304     {
305         this.relayAgentAddress = relayAgentAddress;
306     }
307 
308 
309     public void setSeconds( int seconds )
310     {
311         this.seconds = seconds;
312     }
313 
314 
315     public void setServerHostname( String serverHostname )
316     {
317         this.serverHostname = serverHostname;
318     }
319 
320 
321     public void setTransactionId( int transactionId )
322     {
323         this.transactionId = transactionId;
324     }
325 
326 
327     public byte getOp()
328     {
329         return op;
330     }
331 
332 
333     public void setOp( byte op )
334     {
335         this.op = op;
336     }
337 
338 
339     public HardwareAddress getHardwareAddress()
340     {
341         return hardwareAddress;
342     }
343 
344 
345     public void setHardwareAddress( HardwareAddress hardwareAddress )
346     {
347         this.hardwareAddress = hardwareAddress;
348     }
349 
350 
351     public String toString()
352     {
353         StringBuilder sb = new StringBuilder();
354         sb.append( messageType ).append( ": hwAddress=" ).append( hardwareAddress )
355             .append( ", tx=" ).append( transactionId ).append( ", options=" ).append(
356                 options );
357 
358         return sb.toString();
359     }
360 }