more progress -- still not compiling
[captive-validator.git] / src / se / rfc / unbound / SRRset.java
1 /*
2  * Copyright (c) 2005 VeriSign. All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer. 2. Redistributions in
9  * binary form must reproduce the above copyright notice, this list of
10  * conditions and the following disclaimer in the documentation and/or other
11  * materials provided with the distribution. 3. The name of the author may not
12  * be used to endorse or promote products derived from this software without
13  * specific prior written permission.
14  * 
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *  
26  */
27
28 package se.rfc.unbound;
29
30 import java.util.*;
31
32 import org.xbill.DNS.*;
33
34 /**
35  * A version of the RRset class overrides the standard security status.
36  */
37 public class SRRset extends RRset
38 {
39   private SecurityStatus mSecurityStatus;
40   
41   /** Create a new, blank SRRset. */
42   public SRRset()
43   {
44     super();
45     mSecurityStatus = new SecurityStatus();
46   }
47
48   /**
49    * Create a new SRRset from an existing RRset. This SRRset will contain that
50    * same internal Record objects as the original RRset.
51    */
52   @SuppressWarnings("unchecked") // org.xbill.DNS.RRset isn't typesafe-aware.
53 public SRRset(RRset r)
54   {
55     this();
56
57     for (Iterator i = r.rrs(); i.hasNext();)
58     {
59       addRR((Record) i.next());
60     }
61
62     for (Iterator i = r.sigs(); i.hasNext();)
63     {
64       addRR((Record) i.next());
65     }
66   }
67
68   /**
69    * Clone this SRRset, giving the copy a new TTL. The copy is independent
70    * from the original except for the security status.
71    * 
72    * @param withNewTTL The new TTL to apply to the RRset. This applies to
73    *          contained RRsig records as well.
74    * @return The cloned SRRset.
75    */
76 //  public SRRset cloneSRRset(long withNewTTL)
77 //  {
78 //    SRRset nr = new SRRset();
79 //
80 //    for (Iterator i = rrs(); i.hasNext();)
81 //    {
82 //      nr.addRR(((Record) i.next()).withTTL(withNewTTL));
83 //    }
84 //    for (Iterator i = sigs(); i.hasNext();)
85 //    {
86 //      nr.addRR(((Record) i.next()).withTTL(withNewTTL));
87 //    }
88 //
89 //    nr.mSecurityStatus = mSecurityStatus;
90 //
91 //    return nr;
92 //  }
93
94   public SRRset cloneSRRsetNoSigs()
95   {
96     SRRset nr = new SRRset();
97     for (Iterator i = rrs(); i.hasNext();)
98     {
99       // NOTE: should this clone the records as well?
100       nr.addRR((Record) i.next());
101     }
102     // Do not copy the SecurityStatus reference
103     
104     return nr;
105   }
106   
107   
108   /**
109    * Return the current security status (generally: UNCHECKED, BOGUS, or
110    * SECURE).
111    */
112   public int getSecurity()
113   {
114     return getSecurityStatus();
115   }
116
117   /**
118    * Return the current security status (generally: UNCHECKED, BOGUS, or
119    * SECURE).
120    */
121   public int getSecurityStatus()
122   {
123     return mSecurityStatus.getStatus();
124   }
125
126   /**
127    * Set the current security status for this SRRset. This status will be
128    * shared amongst all copies of this SRRset (created with cloneSRRset())
129    */
130   public void setSecurityStatus(byte status)
131   {
132     mSecurityStatus.setStatus(status);
133   }
134
135   public int totalSize() {
136       int num_sigs = 0;
137       for (Iterator i = sigs(); i.hasNext(); ) {
138           num_sigs++;
139       }
140       return size() + num_sigs;
141   }
142   
143   /**
144    * @return The total number of records (data + sigs) in the SRRset.
145    */
146   public int getNumRecords()
147   {
148     return totalSize();
149   }
150
151   public RRSIGRecord firstSig() {
152       for (Iterator i = sigs(); i.hasNext(); ) {
153           return (RRSIGRecord) i.next();
154       }
155       return null;
156   }
157   /**
158    * @return true if this RRset has RRSIG records that cover data records.
159    *         (i.e., RRSIG SRRsets return false)
160    */
161   public boolean isSigned()
162   {
163     if (getType() == Type.RRSIG) return false;
164     return firstSig() != null;
165   }
166
167   /**
168    * @return The "signer" name for this SRRset, if signed, or null if not.
169    */
170   public Name getSignerName()
171   {
172     RRSIGRecord sig = (RRSIGRecord) firstSig();
173     if (sig == null) return null;
174     return sig.getSigner();
175   }
176   
177 //  public void setTTL(long ttl)
178 //  {
179 //    if (ttl < 0)
180 //    {
181 //      throw new IllegalArgumentException("ttl can't be less than zero, stupid! was " + ttl);
182 //    }
183 //    super.setTTL(ttl);
184 //  }
185 }