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.dns.protocol; 22 23 24 import org.apache.directory.server.dns.io.decoder.DnsMessageDecoder; 25 import org.apache.directory.server.i18n.I18n; 26 import org.apache.mina.core.buffer.IoBuffer; 27 import org.apache.mina.core.session.IoSession; 28 import org.apache.mina.filter.codec.CumulativeProtocolDecoder; 29 import org.apache.mina.filter.codec.ProtocolDecoderOutput; 30 31 32 /** 33 * A {@link CumulativeProtocolDecoder} which supports DNS operation over TCP, 34 * by reassembling split packets prior to decoding. 35 * 36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 37 */ 38 public class DnsTcpDecoder extends CumulativeProtocolDecoder 39 { 40 private DnsMessageDecoder/decoder/DnsMessageDecoder.html#DnsMessageDecoder">DnsMessageDecoder decoder = new DnsMessageDecoder(); 41 42 private int maxObjectSize = 16384; // 16KB 43 44 45 /** 46 * Returns the allowed maximum size of the object to be decoded. 47 * 48 * @return The max object size. 49 */ 50 public int getMaxObjectSize() 51 { 52 return maxObjectSize; 53 } 54 55 56 /** 57 * Sets the allowed maximum size of the object to be decoded. 58 * If the size of the object to be decoded exceeds this value, this 59 * decoder will throw a {@link IllegalArgumentException}. The default 60 * value is <tt>16384</tt> (16KB). 61 * 62 * @param maxObjectSize 63 */ 64 public void setMaxObjectSize( int maxObjectSize ) 65 { 66 if ( maxObjectSize <= 0 ) 67 { 68 throw new IllegalArgumentException( I18n.err( I18n.ERR_634, maxObjectSize ) ); 69 } 70 71 this.maxObjectSize = maxObjectSize; 72 } 73 74 75 @Override 76 protected boolean doDecode( IoSession session, IoBuffer in, ProtocolDecoderOutput out ) throws Exception 77 { 78 if ( !in.prefixedDataAvailable( 2, maxObjectSize ) ) 79 { 80 return false; 81 } 82 83 in.getShort(); 84 85 out.write( decoder.decode( in ) ); 86 87 return true; 88 } 89 }