001: // Copyright (c) 2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.io.*;
006: import java.net.*;
007: import org.xbill.DNS.utils.*;
008:
009: /**
010: * IPsec Keying Material (RFC 4025)
011: *
012: * @author Brian Wellington
013: */
014:
015: public class IPSECKEYRecord extends Record {
016:
017: public static class Algorithm {
018: private Algorithm() {
019: }
020:
021: public static final int RSA = 1;
022: public static final int DSS = 2;
023: }
024:
025: public static class Gateway {
026: private Gateway() {
027: }
028:
029: public static final int None = 0;
030: public static final int IPv4 = 1;
031: public static final int IPv6 = 2;
032: public static final int Name = 3;
033: }
034:
035: private int precedence;
036: private int gatewayType;
037: private int algorithmType;
038: private Object gateway;
039: private byte[] key;
040:
041: IPSECKEYRecord() {
042: }
043:
044: Record getObject() {
045: return new IPSECKEYRecord();
046: }
047:
048: /**
049: * Creates an IPSECKEY Record from the given data.
050: * @param precedence The record's precedence.
051: * @param gatewayType The record's gateway type.
052: * @param algorithmType The record's algorithm type.
053: * @param gateway The record's gateway.
054: * @param key The record's public key.
055: */
056: public IPSECKEYRecord(Name name, int dclass, long ttl,
057: int precedence, int gatewayType, int algorithmType,
058: Object gateway, byte[] key) {
059: super (name, Type.IPSECKEY, dclass, ttl);
060: this .precedence = checkU8("precedence", precedence);
061: this .gatewayType = checkU8("gatewayType", gatewayType);
062: this .algorithmType = checkU8("algorithmType", algorithmType);
063: switch (gatewayType) {
064: case Gateway.None:
065: this .gateway = null;
066: break;
067: case Gateway.IPv4:
068: if (!(gateway instanceof InetAddress))
069: throw new IllegalArgumentException("\"gateway\" "
070: + "must be an IPv4 " + "address");
071: this .gateway = gateway;
072: break;
073: case Gateway.IPv6:
074: if (!(gateway instanceof Inet6Address))
075: throw new IllegalArgumentException("\"gateway\" "
076: + "must be an IPv6 " + "address");
077: this .gateway = gateway;
078: break;
079: case Gateway.Name:
080: if (!(gateway instanceof Name))
081: throw new IllegalArgumentException("\"gateway\" "
082: + "must be a DNS " + "name");
083: this .gateway = checkName("gateway", (Name) gateway);
084: break;
085: default:
086: throw new IllegalArgumentException("\"gatewayType\" "
087: + "must be between 0 and 3");
088: }
089:
090: this .key = key;
091: }
092:
093: void rrFromWire(DNSInput in) throws IOException {
094: precedence = in.readU8();
095: gatewayType = in.readU8();
096: algorithmType = in.readU8();
097: switch (gatewayType) {
098: case Gateway.None:
099: gateway = null;
100: break;
101: case Gateway.IPv4:
102: gateway = InetAddress.getByAddress(in.readByteArray(4));
103: break;
104: case Gateway.IPv6:
105: gateway = InetAddress.getByAddress(in.readByteArray(16));
106: break;
107: case Gateway.Name:
108: gateway = new Name(in);
109: break;
110: default:
111: throw new WireParseException("invalid gateway type");
112: }
113: if (in.remaining() > 0)
114: key = in.readByteArray();
115: }
116:
117: void rdataFromString(Tokenizer st, Name origin) throws IOException {
118: precedence = st.getUInt8();
119: gatewayType = st.getUInt8();
120: algorithmType = st.getUInt8();
121: switch (gatewayType) {
122: case Gateway.None:
123: String s = st.getString();
124: if (!s.equals("."))
125: throw new TextParseException("invalid gateway format");
126: gateway = null;
127: break;
128: case Gateway.IPv4:
129: gateway = st.getAddress(Address.IPv4);
130: break;
131: case Gateway.IPv6:
132: gateway = st.getAddress(Address.IPv6);
133: break;
134: case Gateway.Name:
135: gateway = st.getName(origin);
136: break;
137: default:
138: throw new WireParseException("invalid gateway type");
139: }
140: key = st.getBase64(false);
141: }
142:
143: String rrToString() {
144: StringBuffer sb = new StringBuffer();
145: sb.append(precedence);
146: sb.append(" ");
147: sb.append(gatewayType);
148: sb.append(" ");
149: sb.append(algorithmType);
150: sb.append(" ");
151: switch (gatewayType) {
152: case Gateway.None:
153: sb.append(".");
154: break;
155: case Gateway.IPv4:
156: case Gateway.IPv6:
157: InetAddress gatewayAddr = (InetAddress) gateway;
158: sb.append(gatewayAddr.getHostAddress());
159: break;
160: case Gateway.Name:
161: sb.append(gateway);
162: break;
163: }
164: if (key != null) {
165: sb.append(" ");
166: sb.append(base64.toString(key));
167: }
168: return sb.toString();
169: }
170:
171: /** Returns the record's precedence. */
172: public int getPrecedence() {
173: return precedence;
174: }
175:
176: /** Returns the record's gateway type. */
177: public int getGatewayType() {
178: return gatewayType;
179: }
180:
181: /** Returns the record's algorithm type. */
182: public int getAlgorithmType() {
183: return algorithmType;
184: }
185:
186: /** Returns the record's gateway. */
187: public Object getGateway() {
188: return gateway;
189: }
190:
191: /** Returns the record's public key */
192: public byte[] getKey() {
193: return key;
194: }
195:
196: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
197: out.writeU8(precedence);
198: out.writeU8(gatewayType);
199: out.writeU8(algorithmType);
200: switch (gatewayType) {
201: case Gateway.None:
202: break;
203: case Gateway.IPv4:
204: case Gateway.IPv6:
205: InetAddress gatewayAddr = (InetAddress) gateway;
206: out.writeByteArray(gatewayAddr.getAddress());
207: break;
208: case Gateway.Name:
209: Name gatewayName = (Name) gateway;
210: gatewayName.toWire(out, null, canonical);
211: break;
212: }
213: if (key != null)
214: out.writeByteArray(key);
215: }
216:
217: }
|