// Copyright (c) 1999 Brian Wellington (bwelling@xbill.org)
// Portions Copyright (c) 1999 Network Associates, Inc.

package org.xbill.DNS;

import java.io.*;
import java.util.*;
import org.xbill.DNS.utils.*;

/**
 * Start of Authority - describes properties of a zone.
 *
 * @author Brian Wellington
 */

public class SOARecord extends Record {

private static SOARecord member = new SOARecord();

private Name host, admin;
private int serial, refresh, retry, expire, minimum;

private
SOARecord() {}

private
SOARecord(Name name, short dclass, int ttl) {
	super(name, Type.SOA, dclass, ttl);
}

static SOARecord
getMember() {
	return member;
}

/**
 * Creates an SOA Record from the given data
 * @param host The primary nameserver for the zone
 * @param admin The zone administrator's address
 * @param serial The zone's serial number
 * @param refresh The amount of time until a secondary checks for a new serial
 * number
 * @param retry The amount of time between a secondary's checks for a new
 * serial number
 * @param expire The amount of time until a secondary expires a zone
 * @param minimum The minimum TTL for records in the zone
*/
public
SOARecord(Name name, short dclass, int ttl, Name host, Name admin,
	  int serial, int refresh, int retry, int expire, int minimum)
throws IOException
{
	this(name, dclass, ttl);
	this.host = host;
	this.admin = admin;
	this.serial = serial;
	this.refresh = refresh;
	this.retry = retry;
	this.expire = expire;
	this.minimum = minimum;
}

Record
rrFromWire(Name name, short type, short dclass, int ttl, int length,
	   DataByteInputStream in)
throws IOException
{
	SOARecord rec = new SOARecord(name, dclass, ttl);
	if (in == null)
		return rec;
	rec.host = new Name(in);
	rec.admin = new Name(in);
	rec.serial = in.readInt();
	rec.refresh = in.readInt();
	rec.retry = in.readInt();
	rec.expire = in.readInt();
	rec.minimum = in.readInt();
	return rec;
}

Record
rdataFromString(Name name, short dclass, int ttl, MyStringTokenizer st,
		Name origin)
throws TextParseException
{
	SOARecord rec = new SOARecord(name, dclass, ttl);
	rec.host = Name.fromString(st.nextToken(), origin);
	rec.admin = Name.fromString(st.nextToken(), origin);
	rec.serial = Integer.parseInt(st.nextToken());
	rec.refresh = TTL.parseTTL(st.nextToken());
	rec.retry = TTL.parseTTL(st.nextToken());
	rec.expire = TTL.parseTTL(st.nextToken());
	rec.minimum = TTL.parseTTL(st.nextToken());
	return rec;
}

/** Convert to a String */
public String
rdataToString() {
	StringBuffer sb = new StringBuffer();
	if (host != null) {
		sb.append(host);
		sb.append(" ");
		sb.append(admin);
		sb.append(" (\n\t\t\t\t\t");
		sb.append(serial);
		sb.append("\t; serial\n\t\t\t\t\t");
		sb.append(refresh);
		sb.append("\t; refresh\n\t\t\t\t\t");
		sb.append(retry);
		sb.append("\t; retry\n\t\t\t\t\t");
		sb.append(expire);
		sb.append("\t; expire\n\t\t\t\t\t");
		sb.append(minimum);
		sb.append(" )\t; minimum");
	}
	return sb.toString();
}

/** Returns the primary nameserver */
public Name
getHost() {  
	return host;
}       

/** Returns the zone administrator's address */
public Name
getAdmin() {  
	return admin;
}       

/** Returns the zone's serial number */
public int
getSerial() {  
	return serial;
}       

/** Returns the zone refresh interval */
public int
getRefresh() {  
	return refresh;
}       

/** Returns the zone retry interval */
public int
getRetry() {  
	return retry;
}       

/** Returns the time until a secondary expires a zone */
public int
getExpire() {  
	return expire;
}       

/** Returns the minimum TTL for records in the zone */
public int
getMinimum() {  
	return minimum;
}       

void
rrToWire(DataByteOutputStream out, Compression c) throws IOException {
	if (host == null)
		return;

	host.toWire(out, c);
	admin.toWire(out, c);
	out.writeInt(serial);
	out.writeInt(refresh);
	out.writeInt(retry);
	out.writeInt(expire);
        out.writeInt(minimum);
}

void
rrToWireCanonical(DataByteOutputStream out) throws IOException {
	if (host == null)
		return;

	host.toWireCanonical(out);
	admin.toWireCanonical(out);
	out.writeInt(serial);
	out.writeInt(refresh);
	out.writeInt(retry);
	out.writeInt(expire);
        out.writeInt(minimum);
}

}

@@ -2180,28 +2180,30 @@
 , origin);%0A%09
-rec.
+long t
 serial = Int
@@ -2203,24 +2203,22 @@
 l = 
-Integer
+Long
 .parse
-Int
+Long
 (st.
@@ -2227,24 +2227,138 @@
 xtToken());%0A
+%09if (tserial %3E 0xFFFFFFFFL)%0A%09%09throw new TextParseException(%22Invalid serial number%22);%0A%09rec.serial = (int) tserial;%0A
 %09rec.refresh
@@ -2759,24 +2759,38 @@
 ppend(serial
+ & 0xFFFFFFFFL
 );%0A%09%09sb.appe

fix signedness issues with SOA serial.