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  import org.apache.directory.server.i18n.I18n;
35  
36  
37  /**
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
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          {
77              options.add( new DhcpMessageType( message.getMessageType() ) );
78          }
79  
80          encodeOptions( options, byteBuffer );
81      }
82  
83  
84      /**
85       * Write a zero-terminated string to a field of len bytes.
86       * 
87       * @param byteBuffer
88       * @param serverHostname
89       * @param i
90       */
91      private void writeString( ByteBuffer byteBuffer, String string, int len )
92      {
93          if ( null == string )
94          {
95              string = "";
96          }
97  
98          try
99          {
100             byte[] sbytes = string.getBytes( "ASCII" );
101 
102             // writeBytes will automatically zero-pad and thus terminate the
103             // string.
104             writeBytes( byteBuffer, sbytes, len );
105         }
106         catch ( UnsupportedEncodingException e )
107         {
108             // should not happen
109             throw new RuntimeException( I18n.err( I18n.ERR_635 ), e );
110         }
111     }
112 
113 
114     /**
115      * Write an InetAddress to the byte buffer.
116      * 
117      * @param byteBuffer
118      * @param currentClientAddress
119      */
120     private void writeAddress( ByteBuffer byteBuffer, InetAddress currentClientAddress )
121     {
122         if ( null == currentClientAddress )
123         {
124             byte[] emptyAddress =
125                 { 0, 0, 0, 0 };
126             byteBuffer.put( emptyAddress );
127         }
128         else
129         {
130             byte[] addressBytes = currentClientAddress.getAddress();
131             byteBuffer.put( addressBytes );
132         }
133     }
134 
135 
136     /**
137      * Write an array of bytes to the buffer. Write exactly len bytes,
138      * truncating if more than len, padding if less than len bytes are
139      * available.
140      * 
141      * @param byteBuffer
142      * @param currentClientAddress
143      */
144     private void writeBytes( ByteBuffer byteBuffer, byte[] bytes, int len )
145     {
146         if ( null == bytes )
147         {
148             bytes = new byte[]
149                 {};
150         }
151 
152         byteBuffer.put( bytes, 0, Math.min( len, bytes.length ) );
153 
154         // pad as necessary
155         int remain = len - bytes.length;
156 
157         while ( remain-- > 0 )
158         {
159             byteBuffer.put( ( byte ) 0 );
160         }
161     }
162 
163     private static final byte[] VENDOR_MAGIC_COOKIE =
164         { ( byte ) 99, ( byte ) 130, ( byte ) 83, ( byte ) 99 };
165 
166 
167     public void encodeOptions( OptionsField options, ByteBuffer message )
168     {
169         message.put( VENDOR_MAGIC_COOKIE );
170 
171         for ( Iterator i = options.iterator(); i.hasNext(); )
172         {
173             DhcpOption/../../../../org/apache/directory/server/dhcp/options/DhcpOption.html#DhcpOption">DhcpOption option = ( DhcpOption ) i.next();
174             option.writeTo( message );
175         }
176 
177         // add end option
178         message.put( ( byte ) 0xff );
179     }
180 }