001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020
021 package org.apache.directory.server.dns.protocol;
022
023
024 import org.apache.directory.server.dns.io.decoder.DnsMessageDecoder;
025 import org.apache.directory.server.i18n.I18n;
026 import org.apache.mina.core.buffer.IoBuffer;
027 import org.apache.mina.core.session.IoSession;
028 import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
029 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
030
031
032 /**
033 * A {@link CumulativeProtocolDecoder} which supports DNS operation over TCP,
034 * by reassembling split packets prior to decoding.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev: 545041 $, $Date: 2007-06-06 20:31:34 -0700 (Wed, 06 Jun 2007) $
038 */
039 public class DnsTcpDecoder extends CumulativeProtocolDecoder
040 {
041 private DnsMessageDecoder decoder = new DnsMessageDecoder();
042
043 private int maxObjectSize = 16384; // 16KB
044
045
046 /**
047 * Returns the allowed maximum size of the object to be decoded.
048 * If the size of the object to be decoded exceeds this value, this
049 * decoder will throw a {@link BufferDataException}. The default
050 * value is <tt>16384</tt> (16KB).
051 *
052 * @return The max object size.
053 */
054 public int getMaxObjectSize()
055 {
056 return maxObjectSize;
057 }
058
059
060 /**
061 * Sets the allowed maximum size of the object to be decoded.
062 * If the size of the object to be decoded exceeds this value, this
063 * decoder will throw a {@link BufferDataException}. The default
064 * value is <tt>16384</tt> (16KB).
065 *
066 * @param maxObjectSize
067 */
068 public void setMaxObjectSize( int maxObjectSize )
069 {
070 if ( maxObjectSize <= 0 )
071 {
072 throw new IllegalArgumentException( I18n.err( I18n.ERR_634, maxObjectSize ) );
073 }
074
075 this.maxObjectSize = maxObjectSize;
076 }
077
078
079 @Override
080 protected boolean doDecode( IoSession session, IoBuffer in, ProtocolDecoderOutput out ) throws Exception
081 {
082 if ( !in.prefixedDataAvailable( 2, maxObjectSize ) )
083 {
084 return false;
085 }
086
087 in.getShort();
088
089 out.write( decoder.decode( in ) );
090
091 return true;
092 }
093 }