001: // Copyright (c) 2003-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.io.*;
006: import java.util.*;
007:
008: /**
009: * A helper class for constructing dynamic DNS (DDNS) update messages.
010: *
011: * @author Brian Wellington
012: */
013:
014: public class Update extends Message {
015:
016: private Name origin;
017: private int dclass;
018:
019: /**
020: * Creates an update message.
021: * @param zone The name of the zone being updated.
022: * @param dclass The class of the zone being updated.
023: */
024: public Update(Name zone, int dclass) {
025: super ();
026: if (!zone.isAbsolute())
027: throw new RelativeNameException(zone);
028: DClass.check(dclass);
029: getHeader().setOpcode(Opcode.UPDATE);
030: Record soa = Record.newRecord(zone, Type.SOA, DClass.IN);
031: addRecord(soa, Section.QUESTION);
032: this .origin = zone;
033: this .dclass = dclass;
034: }
035:
036: /**
037: * Creates an update message. The class is assumed to be IN.
038: * @param zone The name of the zone being updated.
039: */
040: public Update(Name zone) {
041: this (zone, DClass.IN);
042: }
043:
044: private void newPrereq(Record rec) {
045: addRecord(rec, Section.PREREQ);
046: }
047:
048: private void newUpdate(Record rec) {
049: addRecord(rec, Section.UPDATE);
050: }
051:
052: /**
053: * Inserts a prerequisite that the specified name exists; that is, there
054: * exist records with the given name in the zone.
055: */
056: public void present(Name name) {
057: newPrereq(Record.newRecord(name, Type.ANY, DClass.ANY, 0));
058: }
059:
060: /**
061: * Inserts a prerequisite that the specified rrset exists; that is, there
062: * exist records with the given name and type in the zone.
063: */
064: public void present(Name name, int type) {
065: newPrereq(Record.newRecord(name, type, DClass.ANY, 0));
066: }
067:
068: /**
069: * Parses a record from the string, and inserts a prerequisite that the
070: * record exists. Due to the way value-dependent prequisites work, the
071: * condition that must be met is that the set of all records with the same
072: * and type in the update message must be identical to the set of all records
073: * with that name and type on the server.
074: * @throws IOException The record could not be parsed.
075: */
076: public void present(Name name, int type, String record)
077: throws IOException {
078: newPrereq(Record.fromString(name, type, dclass, 0, record,
079: origin));
080: }
081:
082: /**
083: * Parses a record from the tokenizer, and inserts a prerequisite that the
084: * record exists. Due to the way value-dependent prequisites work, the
085: * condition that must be met is that the set of all records with the same
086: * and type in the update message must be identical to the set of all records
087: * with that name and type on the server.
088: * @throws IOException The record could not be parsed.
089: */
090: public void present(Name name, int type, Tokenizer tokenizer)
091: throws IOException {
092: newPrereq(Record.fromString(name, type, dclass, 0, tokenizer,
093: origin));
094: }
095:
096: /**
097: * Inserts a prerequisite that the specified record exists. Due to the way
098: * value-dependent prequisites work, the condition that must be met is that
099: * the set of all records with the same and type in the update message must
100: * be identical to the set of all records with that name and type on the server.
101: */
102: public void present(Record record) {
103: newPrereq(record);
104: }
105:
106: /**
107: * Inserts a prerequisite that the specified name does not exist; that is,
108: * there are no records with the given name in the zone.
109: */
110: public void absent(Name name) {
111: newPrereq(Record.newRecord(name, Type.ANY, DClass.NONE, 0));
112: }
113:
114: /**
115: * Inserts a prerequisite that the specified rrset does not exist; that is,
116: * there are no records with the given name and type in the zone.
117: */
118: public void absent(Name name, int type) {
119: newPrereq(Record.newRecord(name, type, DClass.NONE, 0));
120: }
121:
122: /**
123: * Parses a record from the string, and indicates that the record
124: * should be inserted into the zone.
125: * @throws IOException The record could not be parsed.
126: */
127: public void add(Name name, int type, long ttl, String record)
128: throws IOException {
129: newUpdate(Record.fromString(name, type, dclass, ttl, record,
130: origin));
131: }
132:
133: /**
134: * Parses a record from the tokenizer, and indicates that the record
135: * should be inserted into the zone.
136: * @throws IOException The record could not be parsed.
137: */
138: public void add(Name name, int type, long ttl, Tokenizer tokenizer)
139: throws IOException {
140: newUpdate(Record.fromString(name, type, dclass, ttl, tokenizer,
141: origin));
142: }
143:
144: /**
145: * Indicates that the record should be inserted into the zone.
146: */
147: public void add(Record record) {
148: newUpdate(record);
149: }
150:
151: /**
152: * Indicates that the records should be inserted into the zone.
153: */
154: public void add(Record[] records) {
155: for (int i = 0; i < records.length; i++)
156: add(records[i]);
157: }
158:
159: /**
160: * Indicates that all of the records in the rrset should be inserted into the
161: * zone.
162: */
163: public void add(RRset rrset) {
164: for (Iterator it = rrset.rrs(); it.hasNext();)
165: add((Record) it.next());
166: }
167:
168: /**
169: * Indicates that all records with the given name should be deleted from
170: * the zone.
171: */
172: public void delete(Name name) {
173: newUpdate(Record.newRecord(name, Type.ANY, DClass.ANY, 0));
174: }
175:
176: /**
177: * Indicates that all records with the given name and type should be deleted
178: * from the zone.
179: */
180: public void delete(Name name, int type) {
181: newUpdate(Record.newRecord(name, type, DClass.ANY, 0));
182: }
183:
184: /**
185: * Parses a record from the string, and indicates that the record
186: * should be deleted from the zone.
187: * @throws IOException The record could not be parsed.
188: */
189: public void delete(Name name, int type, String record)
190: throws IOException {
191: newUpdate(Record.fromString(name, type, DClass.NONE, 0, record,
192: origin));
193: }
194:
195: /**
196: * Parses a record from the tokenizer, and indicates that the record
197: * should be deleted from the zone.
198: * @throws IOException The record could not be parsed.
199: */
200: public void delete(Name name, int type, Tokenizer tokenizer)
201: throws IOException {
202: newUpdate(Record.fromString(name, type, DClass.NONE, 0,
203: tokenizer, origin));
204: }
205:
206: /**
207: * Indicates that the specified record should be deleted from the zone.
208: */
209: public void delete(Record record) {
210: newUpdate(record.withDClass(DClass.NONE, 0));
211: }
212:
213: /**
214: * Indicates that the records should be deleted from the zone.
215: */
216: public void delete(Record[] records) {
217: for (int i = 0; i < records.length; i++)
218: delete(records[i]);
219: }
220:
221: /**
222: * Indicates that all of the records in the rrset should be deleted from the
223: * zone.
224: */
225: public void delete(RRset rrset) {
226: for (Iterator it = rrset.rrs(); it.hasNext();)
227: delete((Record) it.next());
228: }
229:
230: /**
231: * Parses a record from the string, and indicates that the record
232: * should be inserted into the zone replacing any other records with the
233: * same name and type.
234: * @throws IOException The record could not be parsed.
235: */
236: public void replace(Name name, int type, long ttl, String record)
237: throws IOException {
238: delete(name, type);
239: add(name, type, ttl, record);
240: }
241:
242: /**
243: * Parses a record from the tokenizer, and indicates that the record
244: * should be inserted into the zone replacing any other records with the
245: * same name and type.
246: * @throws IOException The record could not be parsed.
247: */
248: public void replace(Name name, int type, long ttl,
249: Tokenizer tokenizer) throws IOException {
250: delete(name, type);
251: add(name, type, ttl, tokenizer);
252: }
253:
254: /**
255: * Indicates that the record should be inserted into the zone replacing any
256: * other records with the same name and type.
257: */
258: public void replace(Record record) {
259: delete(record.getName(), record.getType());
260: add(record);
261: }
262:
263: /**
264: * Indicates that the records should be inserted into the zone replacing any
265: * other records with the same name and type as each one.
266: */
267: public void replace(Record[] records) {
268: for (int i = 0; i < records.length; i++)
269: replace(records[i]);
270: }
271:
272: /**
273: * Indicates that all of the records in the rrset should be inserted into the
274: * zone replacing any other records with the same name and type.
275: */
276: public void replace(RRset rrset) {
277: delete(rrset.getName(), rrset.getType());
278: for (Iterator it = rrset.rrs(); it.hasNext();)
279: add((Record) it.next());
280: }
281:
282: }
|