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 package org.apache.directory.server.kerberos.shared.io.decoder;
021
022
023 import java.util.Enumeration;
024
025 import org.apache.directory.server.kerberos.shared.messages.value.HostAddress;
026 import org.apache.directory.server.kerberos.shared.messages.value.types.HostAddrType;
027 import org.apache.directory.server.kerberos.shared.messages.value.HostAddresses;
028 import org.apache.directory.shared.asn1.der.DEREncodable;
029 import org.apache.directory.shared.asn1.der.DERInteger;
030 import org.apache.directory.shared.asn1.der.DEROctetString;
031 import org.apache.directory.shared.asn1.der.DERSequence;
032 import org.apache.directory.shared.asn1.der.DERTaggedObject;
033
034
035 /**
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev: 587242 $, $Date: 2007-10-22 23:42:40 +0300 (Mon, 22 Oct 2007) $
038 */
039 public class HostAddressDecoder
040 {
041 /**
042 * HostAddress ::= SEQUENCE {
043 * addr-type[0] INTEGER,
044 * address[1] OCTET STRING
045 * }
046 */
047 protected static HostAddress decode( DERSequence sequence )
048 {
049 HostAddrType type = HostAddrType.ADDRTYPE_INET;
050 byte[] value = null;
051
052 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
053 {
054 DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
055 int tag = object.getTagNo();
056 DEREncodable derObject = object.getObject();
057
058 switch ( tag )
059 {
060 case 0:
061 DERInteger addressType = ( DERInteger ) derObject;
062 type = HostAddrType.getTypeByOrdinal( addressType.intValue() );
063 break;
064
065 case 1:
066 DEROctetString address = ( DEROctetString ) derObject;
067 value = address.getOctets();
068 break;
069 }
070 }
071
072 return new HostAddress( type, value );
073 }
074
075
076 /**
077 * HostAddresses ::= SEQUENCE OF SEQUENCE {
078 * addr-type[0] INTEGER,
079 * address[1] OCTET STRING
080 * }
081 */
082 protected static HostAddresses decodeSequence( DERSequence sequence )
083 {
084 HostAddress[] addresses = new HostAddress[sequence.size()];
085
086 int ii = 0;
087
088 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
089 {
090 DERSequence object = ( DERSequence ) e.nextElement();
091 HostAddress address = decode( object );
092 addresses[ii] = address;
093 ii++;
094 }
095
096 return new HostAddresses( addresses );
097 }
098 }