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.io;
22  
23  
24  import java.io.UnsupportedEncodingException;
25  import java.net.InetAddress;
26  import java.nio.ByteBuffer;
27  import java.util.Iterator;
28  
29  import org.apache.directory.server.dhcp.messages.DhcpMessage;
30  import org.apache.directory.server.dhcp.messages.HardwareAddress;
31  import org.apache.directory.server.dhcp.options.DhcpOption;
32  import org.apache.directory.server.dhcp.options.OptionsField;
33  import org.apache.directory.server.dhcp.options.dhcp.DhcpMessageType;
34  
35  
36  /**
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 638228 $, $Date: 2008-03-18 07:12:41 +0100 (Di, 18 Mär 2008) $
39   */
40  public class DhcpMessageEncoder
41  {
42      /**
43       * Converts a DhcpMessage object into a byte buffer.
44       * 
45       * @param byteBuffer ByteBuffer to put DhcpMessage into
46       * @param message DhcpMessage to encode into ByteBuffer
47       */
48      public void encode( ByteBuffer byteBuffer, DhcpMessage message )
49      {
50          byteBuffer.put( message.getOp() );
51  
52          HardwareAddress hardwareAddress = message.getHardwareAddress();
53  
54          byteBuffer.put( ( byte ) ( null != hardwareAddress ? hardwareAddress.getType() : 0 ) );
55          byteBuffer.put( ( byte ) ( null != hardwareAddress ? hardwareAddress.getLength() : 0 ) );
56          byteBuffer.put( ( byte ) message.getHopCount() );
57          byteBuffer.putInt( message.getTransactionId() );
58          byteBuffer.putShort( ( short ) message.getSeconds() );
59          byteBuffer.putShort( message.getFlags() );
60  
61          writeAddress( byteBuffer, message.getCurrentClientAddress() );
62          writeAddress( byteBuffer, message.getAssignedClientAddress() );
63          writeAddress( byteBuffer, message.getNextServerAddress() );
64          writeAddress( byteBuffer, message.getRelayAgentAddress() );
65  
66          writeBytes( byteBuffer, ( null != hardwareAddress ? hardwareAddress.getAddress() : new byte[]
67              {} ), 16 );
68  
69          writeString( byteBuffer, message.getServerHostname(), 64 );
70          writeString( byteBuffer, message.getBootFileName(), 128 );
71  
72          OptionsField options = message.getOptions();
73  
74          // update message type option (if set)
75          if ( null != message.getMessageType() )
76              options.add( new DhcpMessageType( message.getMessageType() ) );
77  
78          encodeOptions( options, byteBuffer );
79      }
80  
81  
82      /**
83       * Write a zero-terminated string to a field of len bytes.
84       * 
85       * @param byteBuffer
86       * @param serverHostname
87       * @param i
88       */
89      private void writeString( ByteBuffer byteBuffer, String string, int len )
90      {
91          if ( null == string )
92              string = "";
93  
94          try
95          {
96              byte sbytes[] = string.getBytes( "ASCII" );
97  
98              // writeBytes will automatically zero-pad and thus terminate the
99              // string.
100             writeBytes( byteBuffer, sbytes, len );
101         }
102         catch ( UnsupportedEncodingException e )
103         {
104             // should not happen
105             throw new RuntimeException( "No ASCII encoding", e );
106         }
107     }
108 
109 
110     /**
111      * Write an InetAddress to the byte buffer.
112      * 
113      * @param byteBuffer
114      * @param currentClientAddress
115      */
116     private void writeAddress( ByteBuffer byteBuffer, InetAddress currentClientAddress )
117     {
118         if ( null == currentClientAddress )
119         {
120             byte emptyAddress[] =
121                 { 0, 0, 0, 0 };
122             byteBuffer.put( emptyAddress );
123         }
124         else
125         {
126             byte[] addressBytes = currentClientAddress.getAddress();
127             byteBuffer.put( addressBytes );
128         }
129     }
130 
131 
132     /**
133      * Write an array of bytes to the buffer. Write exactly len bytes,
134      * truncating if more than len, padding if less than len bytes are
135      * available.
136      * 
137      * @param byteBuffer
138      * @param currentClientAddress
139      */
140     private void writeBytes( ByteBuffer byteBuffer, byte bytes[], int len )
141     {
142         if ( null == bytes )
143             bytes = new byte[]
144                 {};
145 
146         byteBuffer.put( bytes, 0, Math.min(len, bytes.length) );
147 
148         // pad as necessary
149         int remain = len - bytes.length;
150         while ( remain-- > 0 )
151             byteBuffer.put( ( byte ) 0 );
152     }
153 
154     private static final byte[] VENDOR_MAGIC_COOKIE =
155         { ( byte ) 99, ( byte ) 130, ( byte ) 83, ( byte ) 99 };
156 
157 
158     public void encodeOptions( OptionsField options, ByteBuffer message )
159     {
160         message.put( VENDOR_MAGIC_COOKIE );
161 
162         for ( Iterator i = options.iterator(); i.hasNext(); )
163         {
164             DhcpOption option = ( DhcpOption ) i.next();
165             option.writeTo( message );
166         }
167 
168         // add end option
169         message.put( ( byte ) 0xff );
170     }
171 }