diff options
author | Shuyi Chen <shuyichen@google.com> | 2013-05-22 14:51:55 -0700 |
---|---|---|
committer | Shuyi Chen <shuyichen@google.com> | 2013-05-22 17:19:30 -0700 |
commit | d7955ce24d294fb2014c59d11fca184471056f44 (patch) | |
tree | e260500b0b7639127038495d46a0ad6dcbb6d96c /src/org/xbill/DNS/ClientSubnetOption.java | |
parent | 8f4ce9ea0de51fee918bffe19c434612d6bbb2d7 (diff) | |
download | smack-lollipop-mr1-cts-release.tar.gz |
Add android smack source.HEADandroid-wear-5.0.0_r1android-sdk-4.4.2_r1.0.1android-sdk-4.4.2_r1android-l-preview_r2android-cts-5.1_r9android-cts-5.1_r8android-cts-5.1_r7android-cts-5.1_r6android-cts-5.1_r5android-cts-5.1_r4android-cts-5.1_r3android-cts-5.1_r28android-cts-5.1_r27android-cts-5.1_r26android-cts-5.1_r25android-cts-5.1_r24android-cts-5.1_r23android-cts-5.1_r22android-cts-5.1_r21android-cts-5.1_r20android-cts-5.1_r2android-cts-5.1_r19android-cts-5.1_r18android-cts-5.1_r17android-cts-5.1_r16android-cts-5.1_r15android-cts-5.1_r14android-cts-5.1_r13android-cts-5.1_r10android-cts-5.1_r1android-cts-5.0_r9android-cts-5.0_r8android-cts-5.0_r7android-cts-5.0_r6android-cts-5.0_r5android-cts-5.0_r4android-cts-5.0_r3android-cts-4.4_r4android-cts-4.4_r1android-5.1.1_r9android-5.1.1_r8android-5.1.1_r7android-5.1.1_r6android-5.1.1_r5android-5.1.1_r4android-5.1.1_r38android-5.1.1_r37android-5.1.1_r36android-5.1.1_r35android-5.1.1_r34android-5.1.1_r33android-5.1.1_r30android-5.1.1_r3android-5.1.1_r29android-5.1.1_r28android-5.1.1_r26android-5.1.1_r25android-5.1.1_r24android-5.1.1_r23android-5.1.1_r22android-5.1.1_r20android-5.1.1_r2android-5.1.1_r19android-5.1.1_r18android-5.1.1_r17android-5.1.1_r16android-5.1.1_r15android-5.1.1_r14android-5.1.1_r13android-5.1.1_r12android-5.1.1_r10android-5.1.1_r1android-5.1.0_r5android-5.1.0_r4android-5.1.0_r3android-5.1.0_r1android-5.0.2_r3android-5.0.2_r1android-5.0.1_r1android-5.0.0_r7android-5.0.0_r6android-5.0.0_r5.1android-5.0.0_r5android-5.0.0_r4android-5.0.0_r3android-5.0.0_r2android-5.0.0_r1android-4.4w_r1android-4.4_r1.2.0.1android-4.4_r1.2android-4.4_r1.1.0.1android-4.4_r1.1android-4.4_r1.0.1android-4.4_r1android-4.4_r0.9android-4.4_r0.8android-4.4_r0.7android-4.4.4_r2.0.1android-4.4.4_r2android-4.4.4_r1.0.1android-4.4.4_r1android-4.4.3_r1.1.0.1android-4.4.3_r1.1android-4.4.3_r1.0.1android-4.4.3_r1android-4.4.2_r2.0.1android-4.4.2_r2android-4.4.2_r1.0.1android-4.4.2_r1android-4.4.1_r1.0.1android-4.4.1_r1android-4.3_r3.1android-4.3_r3android-4.3_r2.3android-4.3_r2.2android-4.3_r2.1android-4.3_r2android-4.3_r1.1android-4.3_r1android-4.3_r0.9.1android-4.3_r0.9android-4.3.1_r1tools_r22.2mastermainlollipop-wear-releaselollipop-releaselollipop-mr1-wfc-releaselollipop-mr1-releaselollipop-mr1-fi-releaselollipop-mr1-devlollipop-mr1-cts-releaselollipop-devlollipop-cts-releasel-previewkitkat-wearkitkat-releasekitkat-mr2.2-releasekitkat-mr2.1-releasekitkat-mr2-releasekitkat-mr1.1-releasekitkat-mr1-releasekitkat-devkitkat-cts-releasekitkat-cts-devjb-mr2.0.0-releasejb-mr2.0-releasejb-mr2-releasejb-mr2-devidea133-weekly-releaseidea133
Change-Id: I49ce97136c17173c4ae3965c694af6e7bc49897d
Diffstat (limited to 'src/org/xbill/DNS/ClientSubnetOption.java')
-rw-r--r-- | src/org/xbill/DNS/ClientSubnetOption.java | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/org/xbill/DNS/ClientSubnetOption.java b/src/org/xbill/DNS/ClientSubnetOption.java new file mode 100644 index 0000000..4a98a12 --- /dev/null +++ b/src/org/xbill/DNS/ClientSubnetOption.java @@ -0,0 +1,175 @@ +// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org) + +package org.xbill.DNS; + +import java.net.*; +import java.util.regex.*; + +/** + * The Client Subnet EDNS Option, defined in + * http://tools.ietf.org/html/draft-vandergaast-edns-client-subnet-00 + * ("Client subnet in DNS requests"). + * + * The option is used to convey information about the IP address of the + * originating client, so that an authoritative server can make decisions + * based on this address, rather than the address of the intermediate + * caching name server. + * + * The option is transmitted as part of an OPTRecord in the additional section + * of a DNS message, as defined by RFC 2671 (EDNS0). + * + * An option code has not been assigned by IANA; the value 20730 (used here) is + * also used by several other implementations. + * + * The wire format of the option contains a 2-byte length field (1 for IPv4, 2 + * for IPv6), a 1-byte source netmask, a 1-byte scope netmask, and an address + * truncated to the source netmask length (where the final octet is padded with + * bits set to 0) + * + * + * @see OPTRecord + * + * @author Brian Wellington + * @author Ming Zhou <mizhou@bnivideo.com>, Beaumaris Networks + */ +public class ClientSubnetOption extends EDNSOption { + +private static final long serialVersionUID = -3868158449890266347L; + +private int family; +private int sourceNetmask; +private int scopeNetmask; +private InetAddress address; + +ClientSubnetOption() { + super(EDNSOption.Code.CLIENT_SUBNET); +} + +private static int +checkMaskLength(String field, int family, int val) { + int max = Address.addressLength(family) * 8; + if (val < 0 || val > max) + throw new IllegalArgumentException("\"" + field + "\" " + val + + " must be in the range " + + "[0.." + max + "]"); + return val; +} + +/** + * Construct a Client Subnet option. Note that the number of significant bits in + * the address must not be greater than the supplied source netmask. + * XXX something about Java's mapped addresses + * @param sourceNetmask The length of the netmask pertaining to the query. + * In replies, it mirrors the same value as in the requests. + * @param scopeNetmask The length of the netmask pertaining to the reply. + * In requests, it MUST be set to 0. In responses, this may or may not match + * the source netmask. + * @param address The address of the client. + */ +public +ClientSubnetOption(int sourceNetmask, int scopeNetmask, InetAddress address) { + super(EDNSOption.Code.CLIENT_SUBNET); + + this.family = Address.familyOf(address); + this.sourceNetmask = checkMaskLength("source netmask", this.family, + sourceNetmask); + this.scopeNetmask = checkMaskLength("scope netmask", this.family, + scopeNetmask); + this.address = Address.truncate(address, sourceNetmask); + + if (!address.equals(this.address)) + throw new IllegalArgumentException("source netmask is not " + + "valid for address"); +} + +/** + * Construct a Client Subnet option with scope netmask set to 0. + * @param sourceNetmask The length of the netmask pertaining to the query. + * In replies, it mirrors the same value as in the requests. + * @param address The address of the client. + * @see ClientSubnetOption + */ +public +ClientSubnetOption(int sourceNetmask, InetAddress address) { + this(sourceNetmask, 0, address); +} + +/** + * Returns the family of the network address. This will be either IPv4 (1) + * or IPv6 (2). + */ +public int +getFamily() { + return family; +} + +/** Returns the source netmask. */ +public int +getSourceNetmask() { + return sourceNetmask; +} + +/** Returns the scope netmask. */ +public int +getScopeNetmask() { + return scopeNetmask; +} + +/** Returns the IP address of the client. */ +public InetAddress +getAddress() { + return address; +} + +void +optionFromWire(DNSInput in) throws WireParseException { + family = in.readU16(); + if (family != Address.IPv4 && family != Address.IPv6) + throw new WireParseException("unknown address family"); + sourceNetmask = in.readU8(); + if (sourceNetmask > Address.addressLength(family) * 8) + throw new WireParseException("invalid source netmask"); + scopeNetmask = in.readU8(); + if (scopeNetmask > Address.addressLength(family) * 8) + throw new WireParseException("invalid scope netmask"); + + // Read the truncated address + byte [] addr = in.readByteArray(); + if (addr.length != (sourceNetmask + 7) / 8) + throw new WireParseException("invalid address"); + + // Convert it to a full length address. + byte [] fulladdr = new byte[Address.addressLength(family)]; + System.arraycopy(addr, 0, fulladdr, 0, addr.length); + + try { + address = InetAddress.getByAddress(fulladdr); + } catch (UnknownHostException e) { + throw new WireParseException("invalid address", e); + } + + InetAddress tmp = Address.truncate(address, sourceNetmask); + if (!tmp.equals(address)) + throw new WireParseException("invalid padding"); +} + +void +optionToWire(DNSOutput out) { + out.writeU16(family); + out.writeU8(sourceNetmask); + out.writeU8(scopeNetmask); + out.writeByteArray(address.getAddress(), 0, (sourceNetmask + 7) / 8); +} + +String +optionToString() { + StringBuffer sb = new StringBuffer(); + sb.append(address.getHostAddress()); + sb.append("/"); + sb.append(sourceNetmask); + sb.append(", scope netmask "); + sb.append(scopeNetmask); + return sb.toString(); +} + +} |