All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xbill.DNS.ClientSubnetOption Maven / Gradle / Ivy

There is a newer version: 3.6.2_1
Show newest version
// Copyright (c) 1999-2004 Brian Wellington ([email protected])

package org.xbill.DNS;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * The Client Subnet EDNS Option, defined in  Address.addressLength(family) * 8) {
      throw new WireParseException("invalid source netmask");
    }
    scopePrefixLength = in.readU8();
    if (scopePrefixLength > Address.addressLength(family) * 8) {
      throw new WireParseException("invalid scope netmask");
    }

    // Read the truncated address
    byte[] addr = in.readByteArray();
    if (addr.length != (sourcePrefixLength + 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, sourcePrefixLength);
    if (!tmp.equals(address)) {
      throw new WireParseException("invalid padding");
    }
  }

  @Override
  void optionToWire(DNSOutput out) {
    out.writeU16(family);
    out.writeU8(sourcePrefixLength);
    out.writeU8(scopePrefixLength);
    out.writeByteArray(address.getAddress(), 0, (sourcePrefixLength + 7) / 8);
  }

  @Override
  String optionToString() {
    StringBuilder sb = new StringBuilder();
    sb.append(address.getHostAddress());
    sb.append("/");
    sb.append(sourcePrefixLength);
    sb.append(", scope netmask ");
    sb.append(scopePrefixLength);
    return sb.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy