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
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 options.add( new DhcpMessageType( message.getMessageType() ) );
77
78 encodeOptions( options, byteBuffer );
79 }
80
81
82
83
84
85
86
87
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
99
100 writeBytes( byteBuffer, sbytes, len );
101 }
102 catch ( UnsupportedEncodingException e )
103 {
104
105 throw new RuntimeException( "No ASCII encoding", e );
106 }
107 }
108
109
110
111
112
113
114
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
134
135
136
137
138
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
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
169 message.put( ( byte ) 0xff );
170 }
171 }