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    
021    package org.apache.directory.server.dhcp.messages;
022    
023    import java.net.InetAddress;
024    
025    import org.apache.directory.server.dhcp.options.OptionsField;
026    
027    /**
028     * A DHCP (RFC 2131) message. Field descriptions contain the oroginal RFC field
029     * names in brackets.
030     *
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev: 664295 $, $Date: 2008-06-07 09:48:16 +0200 (Sat, 07 Jun 2008) $
033     */
034    public class DhcpMessage {
035        /**
036         * Flag value: request broadcast answer.
037         */
038        public static final int FLAG_BROADCAST = 0x01;
039    
040        /**
041         * [yiaddr] 'your' (client) IP address.
042         */
043        private InetAddress assignedClientAddress;
044    
045        /**
046         * [file] Boot file name, null terminated string; "generic" name or null in
047         * DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.
048         */
049        private String bootFileName;
050    
051        /**
052         * [ciaddr] Current client IP address; only filled in if client is in BOUND,
053         * RENEW or REBINDING state and can respond to ARP requests.
054         */
055        private InetAddress currentClientAddress;
056    
057        /**
058         * [flags] Flags. (LSB is broadcast flag)
059         */
060        private short flags;
061    
062        /**
063         * [hops] Client sets to zero, optionally used by relay agents when booting
064         * via a relay agent.
065         */
066        private short hopCount;
067    
068        /**
069         * [op] Message op code. 1 = BOOTREQUEST, 2 = BOOTREPLY, ...
070         */
071        private byte op;
072    
073        /**
074         * Operation constant: boot request (client to server).
075         * 
076         * @see #op
077         */
078        public static final byte OP_BOOTREQUEST = 1;
079    
080        /**
081         * Operation constant: boot reply (server to client).
082         * 
083         * @see #op
084         */
085        public static final byte OP_BOOTREPLY = 2;
086    
087        /**
088         * [siaddr] IP address of next server to use in bootstrap; returned in
089         * DHCPOFFER, DHCPACK by server.
090         */
091        private InetAddress nextServerAddress;
092    
093        /**
094         * [options] Optional parameters field. See the options documents for a list
095         * of defined options.
096         */
097        private OptionsField options = new OptionsField();
098    
099        /**
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    }