001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 * 
010 * http://www.apache.org/licenses/LICENSE-2.0
011 * 
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 * 
019 */
020
021package org.apache.directory.server.dhcp.messages;
022
023
024import java.net.InetAddress;
025
026import org.apache.directory.server.dhcp.options.OptionsField;
027
028
029/**
030 * A DHCP (RFC 2131) message. Field descriptions contain the oroginal RFC field
031 * names in brackets.
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class DhcpMessage
036{
037    /**
038     * Flag value: request broadcast answer.
039     */
040    public static final int FLAG_BROADCAST = 0x01;
041
042    /**
043     * [yiaddr] 'your' (client) IP address.
044     */
045    private InetAddress assignedClientAddress;
046
047    /**
048     * [file] Boot file name, null terminated string; "generic" name or null in
049     * DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
050     */
051    private String bootFileName;
052
053    /**
054     * [ciaddr] Current client IP address; only filled in if client is in BOUND,
055     * RENEW or REBINDING state and can respond to ARP requests.
056     */
057    private InetAddress currentClientAddress;
058
059    /**
060     * [flags] Flags. (LSB is broadcast flag)
061     */
062    private short flags;
063
064    /**
065     * [hops] Client sets to zero, optionally used by relay agents when booting
066     * via a relay agent.
067     */
068    private short hopCount;
069
070    /**
071     * [op] Message op code. 1 = BOOTREQUEST, 2 = BOOTREPLY, ...
072     */
073    private byte op;
074
075    /**
076     * Operation constant: boot request (client to server).
077     * 
078     * @see #op
079     */
080    public static final byte OP_BOOTREQUEST = 1;
081
082    /**
083     * Operation constant: boot reply (server to client).
084     * 
085     * @see #op
086     */
087    public static final byte OP_BOOTREPLY = 2;
088
089    /**
090     * [siaddr] IP address of next server to use in bootstrap; returned in
091     * DHCPOFFER, DHCPACK by server.
092     */
093    private InetAddress nextServerAddress;
094
095    /**
096     * [options] Optional parameters field. See the options documents for a list
097     * of defined options.
098     */
099    private 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}