1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40 public class DhcpMessageEncoder
41 {
42
43
44
45
46
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
75 if ( null != message.getMessageType() )
76 {
77 options.add( new DhcpMessageType( message.getMessageType() ) );
78 }
79
80 encodeOptions( options, byteBuffer );
81 }
82
83
84
85
86
87
88
89
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
103
104 writeBytes( byteBuffer, sbytes, len );
105 }
106 catch ( UnsupportedEncodingException e )
107 {
108
109 throw new RuntimeException( I18n.err( I18n.ERR_635 ), e );
110 }
111 }
112
113
114
115
116
117
118
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
138
139
140
141
142
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
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
178 message.put( ( byte ) 0xff );
179 }
180 }