8c6cb7ec98dfc8b9b68132e7b6bbe871e982efbf
[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    * Return the current security status (generally: UNCHECKED, BOGUS, or
108    * SECURE).
109    */
110   public int getSecurity()
111   {
112     return getSecurityStatus();
113   }
114
115   /**
116    * Return the current security status (generally: UNCHECKED, BOGUS, or
117    * SECURE).
118    */
119   public int getSecurityStatus()
120   {
121     return mSecurityStatus.getStatus();
122   }
123
124   /**
125    * Set the current security status for this SRRset. This status will be
126    * shared amongst all copies of this SRRset (created with cloneSRRset())
127    */
128   public void setSecurityStatus(int status)
129   {
130     mSecurityStatus.setStatus(status);
131   }
132
133   /**
134    * @return The total number of records (data + sigs) in the SRRset.
135    */
136   public int getNumRecords()
137   {
138     return totalSize();
139   }
140
141   /**
142    * @return true if this RRset has RRSIG records that cover data records.
143    *         (i.e., RRSIG SRRsets return false)
144    */
145   public boolean isSigned()
146   {
147     if (getType() == Type.RRSIG) return false;
148     return firstSig() != null;
149   }
150
151   /**
152    * @return The "signer" name for this SRRset, if signed, or null if not.
153    */
154   public Name getSignerName()
155   {
156     RRSIGRecord sig = (RRSIGRecord) firstSig();
157     if (sig == null) return null;
158     return sig.getSigner();
159   }
160   
161   public void setTTL(long ttl)
162   {
163     if (ttl < 0)
164     {
165       throw new IllegalArgumentException("ttl can't be less than zero, stupid! was " + ttl);
166     }
167     super.setTTL(ttl);
168   }
169 }