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

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

There is a newer version: 3.6.2_1
Show newest version
// SPDX-License-Identifier: BSD-3-Clause
package org.xbill.DNS;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.xbill.DNS.DNSSEC.Algorithm;
import org.xbill.DNS.DNSSEC.Digest;

/**
 * The EDNS0 Option for Signaling Cryptographic Algorithm Understanding in DNS Security Extensions
 * (DNSSEC), RFC 6975.
 */
public class DnssecAlgorithmOption extends EDNSOption {
  private final List algCodes;

  private DnssecAlgorithmOption(int code) {
    super(code);
    switch (code) {
      case Code.DAU:
      case Code.DHU:
      case Code.N3U:
        break;
      default:
        throw new IllegalArgumentException("Invalid option code, must be one of DAU, DHU, N3U");
    }
    algCodes = new ArrayList<>();
  }

  public DnssecAlgorithmOption(int code, List algCodes) {
    this(code);
    this.algCodes.addAll(algCodes);
  }

  public DnssecAlgorithmOption(int code, int... algCodes) {
    this(code);
    if (algCodes != null) {
      for (int algCode : algCodes) {
        this.algCodes.add(algCode);
      }
    }
  }

  public List getAlgorithms() {
    return Collections.unmodifiableList(algCodes);
  }

  @Override
  void optionFromWire(DNSInput in) throws IOException {
    algCodes.clear();
    while (in.remaining() > 0) {
      algCodes.add(in.readU8());
    }
  }

  @Override
  void optionToWire(DNSOutput out) {
    algCodes.forEach(out::writeU8);
  }

  @Override
  String optionToString() {
    Function mapper;
    switch (getCode()) {
      case Code.DAU:
        mapper = Algorithm::string;
        break;
      case Code.DHU:
        mapper = Digest::string;
        break;
      case Code.N3U:
        mapper = NSEC3Record.Digest::string;
        break;
      default:
        throw new IllegalStateException("Unknown option code");
    }
    return "[" + algCodes.stream().map(mapper).collect(Collectors.joining(", ")) + "]";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy