Search in sources :

Example 6 with ARecord

use of org.xbill.DNS.ARecord in project nhin-d by DirectProject.

the class DNSController method refreshModelFromService.

public void refreshModelFromService(Model model) {
    // GET A RECORDS
    Collection<DNSRecord> arecords = null;
    arecords = getDnsRecords(DNSType.A.getValue());
    final Collection<DNSEntryForm> aform = new ArrayList<DNSEntryForm>();
    if (arecords != null) {
        for (Iterator<DNSRecord> iter = arecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            try {
                final ARecord newrec = (ARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                aform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsARecordResults", aform);
    // GET A4 RECORDS
    Collection<DNSRecord> a4records = null;
    a4records = getDnsRecords(DNSType.AAAA.getValue());
    final Collection<DNSEntryForm> a4form = new ArrayList<DNSEntryForm>();
    if (a4records != null) {
        for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            try {
                final AAAARecord newrec = (AAAARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                a4form.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsA4RecordResults", a4form);
    // GET CNAME RECORDS
    Collection<DNSRecord> crecords = null;
    crecords = getDnsRecords(DNSType.CNAME.getValue());
    final Collection<DNSEntryForm> cform = new ArrayList<DNSEntryForm>();
    if (crecords != null) {
        for (Iterator<DNSRecord> iter = crecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                final CNAMERecord newrec = (CNAMERecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                cform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsCnameRecordResults", cform);
    // GET MX RECORDS
    Collection<DNSRecord> mxrecords = null;
    mxrecords = getDnsRecords(DNSType.MX.getValue());
    final Collection<DNSEntryForm> mxform = new ArrayList<DNSEntryForm>();
    if (mxrecords != null) {
        for (Iterator<DNSRecord> iter = mxrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                final MXRecord newrec = (MXRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setPriority(newrec.getPriority());
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                mxform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsMxRecordResults", mxform);
    // GET Cert RECORDS
    Collection<DNSRecord> certrecords = null;
    certrecords = getDnsRecords(DNSType.CERT.getValue());
    // get the thumbprint and assign
    // create a new collection 
    final Collection<SrvRecord> form = new ArrayList<SrvRecord>();
    CertContainer cont;
    if (certrecords != null) {
        for (Iterator<DNSRecord> iter = certrecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            final SrvRecord srv = new SrvRecord();
            srv.setCreateTime(t.getCreateTime());
            srv.setData(t.getData());
            srv.setDclass(t.getDclass());
            srv.setId(t.getId());
            srv.setName(t.getName());
            srv.setTtl(t.getTtl());
            srv.setType(t.getType());
            srv.setThumb("");
            try {
                final CERTRecord newrec = (CERTRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                String thumb = "";
                byte[] certData = newrec.getCert();
                if (certData != null) {
                    // get the owner from the certificate information
                    // first transform into a certificate
                    cont = toCertContainer(certData);
                    if (cont != null && cont.getCert() != null) {
                        Certificate cert2 = new Certificate();
                        cert2.setData(certData);
                        thumb = getThumbPrint(cont.getCert());
                        srv.setThumb(thumb);
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form.add(srv);
        }
    }
    model.addAttribute("dnsCertRecordResults", form);
    // GET SRV RECORDS
    Collection<DNSRecord> srvrecords = null;
    srvrecords = getDnsRecords(DNSType.SRV.getValue());
    // create a new collection 
    final Collection<SrvRecord> form2 = new ArrayList<SrvRecord>();
    if (srvrecords != null) {
        for (Iterator<DNSRecord> iter = srvrecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            final SrvRecord srv = new SrvRecord();
            try {
                SRVRecord srv4 = (SRVRecord) SRVRecord.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                srv.setCreateTime(t.getCreateTime());
                srv.setData(t.getData());
                srv.setDclass(t.getDclass());
                srv.setId(t.getId());
                srv.setName(t.getName());
                final String name = t.getName();
                // parse the name to get service, protocol, priority , weight,
                // port
                int firstpos = name.indexOf("_");
                if (firstpos == 0) {
                    // then this can be parsed as a srv record
                    // ("_"+SrvdnsForm.getService()+"._"+SrvdnsForm.getProtocol()+"._"+SrvdnsForm.getPriority()+"._"+SrvdnsForm.getWeight()+"._"+SrvdnsForm.getPort()+"._"+SrvdnsForm.getDest()+"."+SrvdnsForm.getName()
                    int secondpos = name.indexOf("._");
                    int thirdpos = name.indexOf(".", secondpos + 2);
                    // from first to second is service
                    final String service_ = name.substring(firstpos + 1, secondpos);
                    srv.setService(service_);
                    // from second to third is protocol
                    final String protocol_ = name.substring(secondpos + 2, thirdpos);
                    ;
                    srv.setProtocol(protocol_);
                    int last2pos = name.indexOf(".", thirdpos);
                    final String name_ = name.substring(last2pos + 1, name.length());
                    srv.setName(name_);
                }
                srv.setTtl(t.getTtl());
                srv.setType(t.getType());
                srv.setPort(srv4.getPort());
                srv.setWeight(srv4.getWeight());
                srv.setPriority("" + srv4.getPriority());
                srv.setTarget("" + srv4.getTarget().toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form2.add(srv);
        }
    }
    model.addAttribute("dnsSrvRecordResults", form2);
    // GET SOA RECORDS
    Collection<DNSRecord> soarecords = null;
    soarecords = getDnsRecords(DNSType.SOA.getValue());
    final Collection<DNSEntryForm> soaform = new ArrayList<DNSEntryForm>();
    if (soarecords != null) {
        for (Iterator<DNSRecord> iter = soarecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                final SOARecord newrec = (SOARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                final DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setAdmin("" + newrec.getAdmin());
                tmp.setExpire(newrec.getExpire());
                tmp.setMinimum(newrec.getMinimum());
                tmp.setRefresh(newrec.getRefresh());
                tmp.setRetry(newrec.getRetry());
                tmp.setSerial(newrec.getSerial());
                tmp.setDest("" + newrec.getHost());
                tmp.setDomain("" + newrec.getHost());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                soaform.add(tmp);
            } catch (TextParseException e) {
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsSOARecordResults", soaform);
    // GET NS RECORDS
    Collection<DNSRecord> nsrecords = null;
    nsrecords = getDnsRecords(DNSType.NS.getValue());
    final Collection<DNSEntryForm> nsform = new ArrayList<DNSEntryForm>();
    if (nsrecords != null) {
        for (Iterator<DNSRecord> iter = nsrecords.iterator(); iter.hasNext(); ) {
            final DNSRecord t = (DNSRecord) iter.next();
            try {
                NSRecord newrec = (NSRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                nsform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsNSRecordResults", nsform);
    // *****************
    model.addAttribute("NSdnsForm", new DNSEntryForm());
    model.addAttribute("SoadnsForm", new DNSEntryForm());
    model.addAttribute("AdnsForm", new DNSEntryForm());
    model.addAttribute("AAdnsForm", new DNSEntryForm());
    model.addAttribute("CdnsForm", new DNSEntryForm());
    model.addAttribute("MXdnsForm", new DNSEntryForm());
    model.addAttribute("CertdnsForm", new DNSEntryForm());
    model.addAttribute("SrvdnsForm", new DNSEntryForm());
}
Also used : DNSRecord(org.nhindirect.config.model.DNSRecord) AAAARecord(org.xbill.DNS.AAAARecord) ArrayList(java.util.ArrayList) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) IOException(java.io.IOException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) CNAMERecord(org.xbill.DNS.CNAMERecord) SOARecord(org.xbill.DNS.SOARecord) AAAARecord(org.xbill.DNS.AAAARecord) ARecord(org.xbill.DNS.ARecord) CERTRecord(org.xbill.DNS.CERTRecord) MXRecord(org.xbill.DNS.MXRecord) NSRecord(org.xbill.DNS.NSRecord) DNSRecord(org.nhindirect.config.model.DNSRecord) SRVRecord(org.xbill.DNS.SRVRecord) SOARecord(org.xbill.DNS.SOARecord) TextParseException(org.xbill.DNS.TextParseException) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate)

Example 7 with ARecord

use of org.xbill.DNS.ARecord in project nhin-d by DirectProject.

the class MainController method refreshModelFromService.

public void refreshModelFromService(Model model) {
    // GET A RECORDS
    Collection<DNSRecord> arecords = null;
    arecords = getDnsRecords(DNSType.A.getValue());
    Collection<DNSEntryForm> aform = new ArrayList<DNSEntryForm>();
    if (arecords != null) {
        for (DNSRecord t : arecords) {
            try {
                ARecord newrec = (ARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                aform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsARecordResults", aform);
    // GET A4 RECORDS
    Collection<DNSRecord> a4records = null;
    a4records = getDnsRecords(DNSType.AAAA.getValue());
    Collection<DNSEntryForm> a4form = new ArrayList<DNSEntryForm>();
    if (a4records != null) {
        for (Iterator<DNSRecord> iter = a4records.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                AAAARecord newrec = (AAAARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getAddress());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                a4form.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsA4RecordResults", a4form);
    // GET CNAME RECORDS
    Collection<DNSRecord> crecords = null;
    crecords = getDnsRecords(DNSType.CNAME.getValue());
    Collection<DNSEntryForm> cform = new ArrayList<DNSEntryForm>();
    if (crecords != null) {
        for (Iterator<DNSRecord> iter = crecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                CNAMERecord newrec = (CNAMERecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                cform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsCnameRecordResults", cform);
    // GET MX RECORDS
    Collection<DNSRecord> mxrecords = null;
    mxrecords = getDnsRecords(DNSType.MX.getValue());
    Collection<DNSEntryForm> mxform = new ArrayList<DNSEntryForm>();
    if (mxrecords != null) {
        for (Iterator<DNSRecord> iter = mxrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                MXRecord newrec = (MXRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setPriority(newrec.getPriority());
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                mxform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsMxRecordResults", mxform);
    // GET Cert RECORDS
    Collection<DNSRecord> certrecords = null;
    certrecords = getDnsRecords(DNSType.CERT.getValue());
    // get the thumbprint and assign
    // create a new collection 
    Collection<SrvRecord> form = new ArrayList<SrvRecord>();
    CertContainer cont;
    if (certrecords != null) {
        for (Iterator<DNSRecord> iter = certrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            SrvRecord srv = new SrvRecord();
            srv.setCreateTime(t.getCreateTime());
            srv.setData(t.getData());
            srv.setDclass(t.getDclass());
            srv.setId(t.getId());
            srv.setName(t.getName());
            srv.setTtl(t.getTtl());
            srv.setType(t.getType());
            srv.setThumb("");
            try {
                CERTRecord newrec = (CERTRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                String thumb = "";
                byte[] certData = newrec.getCert();
                if (certData != null) {
                    // get the owner from the certificate information
                    // first transform into a certificate
                    cont = CertUtils.toCertContainer(certData);
                    if (cont != null && cont.getCert() != null) {
                        Certificate cert2 = new Certificate();
                        cert2.setData(certData);
                        thumb = getThumbPrint(cont.getCert());
                        srv.setThumb(thumb);
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form.add(srv);
        }
    }
    model.addAttribute("dnsCertRecordResults", form);
    // GET SRV RECORDS
    Collection<DNSRecord> srvrecords = null;
    srvrecords = getDnsRecords(DNSType.SRV.getValue());
    // create a new collection 
    Collection<SrvRecord> form2 = new ArrayList<SrvRecord>();
    if (srvrecords != null) {
        for (Iterator<DNSRecord> iter = srvrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            SrvRecord srv = new SrvRecord();
            try {
                SRVRecord srv4 = (SRVRecord) SRVRecord.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                srv.setCreateTime(t.getCreateTime());
                srv.setData(t.getData());
                srv.setDclass(t.getDclass());
                srv.setId(t.getId());
                srv.setName(t.getName());
                String name = t.getName();
                // parse the name to get service, protocol, priority , weight,
                // port
                int firstpos = name.indexOf("_");
                if (firstpos == 0) {
                    // then this can be parsed as a srv record
                    // ("_"+SrvdnsForm.getService()+"._"+SrvdnsForm.getProtocol()+"._"+SrvdnsForm.getPriority()+"._"+SrvdnsForm.getWeight()+"._"+SrvdnsForm.getPort()+"._"+SrvdnsForm.getDest()+"."+SrvdnsForm.getName()
                    int secondpos = name.indexOf("._");
                    int thirdpos = name.indexOf(".", secondpos + 2);
                    // from first to second is service
                    String service_ = name.substring(firstpos + 1, secondpos);
                    srv.setService(service_);
                    // from second to third is protocol
                    String protocol_ = name.substring(secondpos + 2, thirdpos);
                    ;
                    srv.setProtocol(protocol_);
                    int last2pos = name.indexOf(".", thirdpos);
                    String name_ = name.substring(last2pos + 1, name.length());
                    srv.setName(name_);
                }
                srv.setTtl(t.getTtl());
                srv.setType(t.getType());
                srv.setPort(srv4.getPort());
                srv.setWeight(srv4.getWeight());
                srv.setPriority("" + srv4.getPriority());
                srv.setTarget("" + srv4.getTarget().toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            form2.add(srv);
        }
    }
    model.addAttribute("dnsSrvRecordResults", form2);
    // GET SOA RECORDS
    Collection<DNSRecord> soarecords = null;
    soarecords = getDnsRecords(DNSType.SOA.getValue());
    Collection<DNSEntryForm> soaform = new ArrayList<DNSEntryForm>();
    if (soarecords != null) {
        for (Iterator<DNSRecord> iter = soarecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                SOARecord newrec = (SOARecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setAdmin("" + newrec.getAdmin());
                tmp.setExpire(newrec.getExpire());
                tmp.setMinimum(newrec.getMinimum());
                tmp.setRefresh(newrec.getRefresh());
                tmp.setRetry(newrec.getRetry());
                tmp.setSerial(newrec.getSerial());
                tmp.setDest("" + newrec.getHost());
                tmp.setDomain("" + newrec.getHost());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                soaform.add(tmp);
            } catch (TextParseException e) {
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsSOARecordResults", soaform);
    // GET NS RECORDS
    Collection<DNSRecord> nsrecords = null;
    nsrecords = getDnsRecords(DNSType.NS.getValue());
    Collection<DNSEntryForm> nsform = new ArrayList<DNSEntryForm>();
    if (nsrecords != null) {
        for (Iterator<DNSRecord> iter = nsrecords.iterator(); iter.hasNext(); ) {
            DNSRecord t = (DNSRecord) iter.next();
            try {
                NSRecord newrec = (NSRecord) Record.newRecord(Name.fromString(t.getName()), t.getType(), t.getDclass(), t.getTtl(), t.getData());
                DNSEntryForm tmp = new DNSEntryForm();
                tmp.setId(t.getId());
                tmp.setDest("" + newrec.getTarget());
                tmp.setTtl(newrec.getTTL());
                tmp.setName("" + newrec.getName());
                nsform.add(tmp);
            } catch (TextParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    model.addAttribute("dnsNSRecordResults", nsform);
    // *****************
    model.addAttribute("NSdnsForm", new DNSEntryForm());
    model.addAttribute("SoadnsForm", new DNSEntryForm());
    model.addAttribute("AdnsForm", new DNSEntryForm());
    model.addAttribute("AAdnsForm", new DNSEntryForm());
    model.addAttribute("CdnsForm", new DNSEntryForm());
    model.addAttribute("MXdnsForm", new DNSEntryForm());
    model.addAttribute("CertdnsForm", new DNSEntryForm());
    model.addAttribute("SrvdnsForm", new DNSEntryForm());
}
Also used : DNSRecord(org.nhindirect.config.model.DNSRecord) AAAARecord(org.xbill.DNS.AAAARecord) ArrayList(java.util.ArrayList) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) IOException(java.io.IOException) CertContainer(org.nhindirect.config.model.utils.CertUtils.CertContainer) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) CNAMERecord(org.xbill.DNS.CNAMERecord) SOARecord(org.xbill.DNS.SOARecord) AAAARecord(org.xbill.DNS.AAAARecord) ARecord(org.xbill.DNS.ARecord) CERTRecord(org.xbill.DNS.CERTRecord) MXRecord(org.xbill.DNS.MXRecord) NSRecord(org.xbill.DNS.NSRecord) DNSRecord(org.nhindirect.config.model.DNSRecord) SRVRecord(org.xbill.DNS.SRVRecord) SOARecord(org.xbill.DNS.SOARecord) TextParseException(org.xbill.DNS.TextParseException) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate)

Example 8 with ARecord

use of org.xbill.DNS.ARecord in project opennms by OpenNMS.

the class DnsRequisitionProvider method createRequisitionNode.

/**
 * Creates an instance of the JaxB annotated RequisionNode class.
 *
 * @param rec
 * @return a populated RequisitionNode based on defaults and data from the A
 *         record returned from a DNS zone transfer query.
 */
private RequisitionNode createRequisitionNode(DnsRequisitionRequest request, Record rec) {
    String addr = null;
    if ("A".equals(Type.string(rec.getType()))) {
        final ARecord arec = (ARecord) rec;
        addr = StringUtils.stripStart(arec.getAddress().toString(), "/");
    } else if ("AAAA".equals(Type.string(rec.getType()))) {
        final AAAARecord aaaarec = (AAAARecord) rec;
        addr = aaaarec.rdataToString();
    } else {
        throw new IllegalArgumentException("Invalid record type " + Type.string(rec.getType()) + ". A or AAAA expected.");
    }
    final RequisitionNode n = new RequisitionNode();
    final String host = rec.getName().toString();
    final String nodeLabel = StringUtils.stripEnd(StringUtils.stripStart(host, "."), ".");
    n.setBuilding(request.getForeignSource());
    switch(request.getForeignIdHashSource()) {
        case NODE_LABEL:
            n.setForeignId(computeHashCode(nodeLabel));
            LOG.debug("Generating foreignId from hash of nodelabel {}", nodeLabel);
            break;
        case IP_ADDRESS:
            n.setForeignId(computeHashCode(addr));
            LOG.debug("Generating foreignId from hash of ipAddress {}", addr);
            break;
        case NODE_LABEL_AND_IP_ADDRESS:
            n.setForeignId(computeHashCode(nodeLabel + addr));
            LOG.debug("Generating foreignId from hash of nodelabel+ipAddress {}{}", nodeLabel, addr);
            break;
        default:
            n.setForeignId(computeHashCode(nodeLabel));
            LOG.debug("Default case: Generating foreignId from hash of nodelabel {}", nodeLabel);
            break;
    }
    n.setNodeLabel(nodeLabel);
    final RequisitionInterface i = new RequisitionInterface();
    i.setDescr("DNS-" + Type.string(rec.getType()));
    i.setIpAddr(addr);
    i.setSnmpPrimary(PrimaryType.PRIMARY);
    i.setManaged(Boolean.TRUE);
    i.setStatus(Integer.valueOf(1));
    for (String service : request.getServices()) {
        service = service.trim();
        i.insertMonitoredService(new RequisitionMonitoredService(service));
        LOG.debug("Adding provisioned service {}", service);
    }
    n.putInterface(i);
    return n;
}
Also used : RequisitionNode(org.opennms.netmgt.provision.persist.requisition.RequisitionNode) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) AAAARecord(org.xbill.DNS.AAAARecord) RequisitionInterface(org.opennms.netmgt.provision.persist.requisition.RequisitionInterface) RequisitionMonitoredService(org.opennms.netmgt.provision.persist.requisition.RequisitionMonitoredService)

Example 9 with ARecord

use of org.xbill.DNS.ARecord in project opennms by OpenNMS.

the class JUnitDNSServerExecutionListener method beforeTestMethod.

/**
 * {@inheritDoc}
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
    super.beforeTestMethod(testContext);
    final JUnitDNSServer config = findTestAnnotation(JUnitDNSServer.class, testContext);
    if (config == null) {
        return;
    }
    LOG.info("initializing DNS on port {}", config.port());
    m_server = new DNSServer();
    m_server.addPort(config.port());
    for (final DNSZone dnsZone : config.zones()) {
        String name = dnsZone.name();
        if (!name.endsWith(".")) {
            name = name + ".";
        }
        final Name zoneName = Name.fromString(name, Name.root);
        LOG.debug("zoneName = {}", zoneName);
        final Zone zone = new Zone(zoneName, new Record[] { new SOARecord(zoneName, DClass.IN, DEFAULT_TTL, zoneName, Name.fromString("admin." + name), 1, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver1.opendns.com.")), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver2.opendns.com.")), new ARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v4address())), new AAAARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v6address())) });
        LOG.debug("zone = {}", zone);
        Matcher m;
        for (final DNSEntry entry : dnsZone.entries()) {
            LOG.debug("adding entry: {}", entry);
            String hostname = entry.hostname();
            final Name recordName = Name.fromString(hostname, zoneName);
            LOG.debug("name = {}", recordName);
            switch(entry.type()) {
                case "A":
                    zone.addRecord(new ARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, InetAddressUtils.addr(entry.data())));
                    break;
                case "AAAA":
                    zone.addRecord(new AAAARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, InetAddressUtils.addr(entry.data())));
                    break;
                case "CNAME":
                    zone.addRecord(new CNAMERecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "NS":
                    zone.addRecord(new NSRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "MX":
                    m = s_mxPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new MXRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Integer.valueOf(m.group(1)), Name.fromString(m.group(2))));
                    } else {
                        LOG.error("Entry data '{}' does not match MX pattern", entry.data());
                    }
                    break;
                case "PTR":
                    zone.addRecord(new PTRRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "SOA":
                    m = s_soaPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new SOARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(m.group(1)), Name.fromString(m.group(2)), Long.valueOf(m.group(3)), Long.valueOf(m.group(4)), Long.valueOf(m.group(5)), Long.valueOf(m.group(6)), Long.valueOf(m.group(7))));
                    } else {
                        LOG.error("Entry data '{}' does not match SOA pattern", entry.data());
                    }
                    break;
                case "SRV":
                    m = s_srvPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new SRVRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Integer.valueOf(m.group(1)), Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)), Name.fromString(m.group(4))));
                    } else {
                        LOG.error("Entry data '{}' does not match MX pattern", entry.data());
                    }
                    break;
                case "TXT":
                    zone.addRecord(new TXTRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, entry.data()));
                    break;
                default:
                    LOG.error("DNS entry type {} not supported.", entry.type());
                    break;
            }
        }
        m_server.addZone(zone);
    }
    LOG.debug("starting DNS server");
    m_server.start();
}
Also used : JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) AAAARecord(org.xbill.DNS.AAAARecord) Matcher(java.util.regex.Matcher) Zone(org.xbill.DNS.Zone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) Name(org.xbill.DNS.Name) CNAMERecord(org.xbill.DNS.CNAMERecord) PTRRecord(org.xbill.DNS.PTRRecord) DNSEntry(org.opennms.core.test.dns.annotations.DNSEntry) SOARecord(org.xbill.DNS.SOARecord) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) TXTRecord(org.xbill.DNS.TXTRecord) MXRecord(org.xbill.DNS.MXRecord) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) NSRecord(org.xbill.DNS.NSRecord) SOARecord(org.xbill.DNS.SOARecord) SRVRecord(org.xbill.DNS.SRVRecord)

Example 10 with ARecord

use of org.xbill.DNS.ARecord in project opennms by OpenNMS.

the class DnsUtils method resolveHostname.

/**
 * This function is used inside XSLT documents, do a string search before refactoring.
 */
public static InetAddress resolveHostname(final String hostname, final boolean preferInet6Address, final boolean throwException) throws UnknownHostException {
    InetAddress retval = null;
    // return valid A and AAAA records for "localhost".
    if ("localhost".equals(hostname)) {
        return preferInet6Address ? InetAddress.getByName("::1") : InetAddress.getByName("127.0.0.1");
    }
    try {
        // 2011-05-22 - Matt is seeing some platform-specific inconsistencies when using
        // InetAddress.getAllByName(). It seems to miss some addresses occasionally on Mac.
        // We need to use dnsjava here instead since it should be 100% reliable.
        // 
        // InetAddress[] addresses = InetAddress.getAllByName(hostname);
        // 
        List<InetAddress> v4Addresses = new ArrayList<>();
        try {
            Record[] aRecs = new Lookup(hostname, Type.A).run();
            if (aRecs != null) {
                for (Record aRec : aRecs) {
                    if (aRec instanceof ARecord) {
                        InetAddress addr = ((ARecord) aRec).getAddress();
                        if (addr instanceof Inet4Address) {
                            v4Addresses.add(addr);
                        } else {
                            // Should never happen
                            throw new UnknownHostException("Non-IPv4 address found via A record DNS lookup of host: " + hostname + ": " + addr.toString());
                        }
                    }
                }
            } else {
            // throw new UnknownHostException("No IPv4 addresses found via A record DNS lookup of host: " + hostname);
            }
        } catch (final TextParseException e) {
            final UnknownHostException ex = new UnknownHostException("Could not perform A record lookup for host: " + hostname);
            ex.initCause(e);
            throw ex;
        }
        final List<InetAddress> v6Addresses = new ArrayList<>();
        try {
            final Record[] quadARecs = new Lookup(hostname, Type.AAAA).run();
            if (quadARecs != null) {
                for (final Record quadARec : quadARecs) {
                    final InetAddress addr = ((AAAARecord) quadARec).getAddress();
                    if (addr instanceof Inet6Address) {
                        v6Addresses.add(addr);
                    } else {
                        // Should never happen
                        throw new UnknownHostException("Non-IPv6 address found via AAAA record DNS lookup of host: " + hostname + ": " + addr.toString());
                    }
                }
            } else {
            // throw new UnknownHostException("No IPv6 addresses found via AAAA record DNS lookup of host: " + hostname);
            }
        } catch (final TextParseException e) {
            final UnknownHostException ex = new UnknownHostException("Could not perform AAAA record lookup for host: " + hostname);
            ex.initCause(e);
            throw ex;
        }
        final List<InetAddress> addresses = new ArrayList<>();
        if (preferInet6Address) {
            addresses.addAll(v6Addresses);
            addresses.addAll(v4Addresses);
        } else {
            addresses.addAll(v4Addresses);
            addresses.addAll(v6Addresses);
        }
        for (final InetAddress address : addresses) {
            retval = address;
            if (!preferInet6Address && retval instanceof Inet4Address)
                break;
            if (preferInet6Address && retval instanceof Inet6Address)
                break;
        }
        if (preferInet6Address && !(retval instanceof Inet6Address)) {
            throw new UnknownHostException("No IPv6 address could be found for the hostname: " + hostname);
        }
    } catch (final UnknownHostException e) {
        if (throwException) {
            throw e;
        } else {
            // e.printStackTrace();
            return null;
        }
    }
    return retval;
}
Also used : ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) Inet4Address(java.net.Inet4Address) AAAARecord(org.xbill.DNS.AAAARecord) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) Record(org.xbill.DNS.Record) Lookup(org.xbill.DNS.Lookup) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) TextParseException(org.xbill.DNS.TextParseException)

Aggregations

ARecord (org.xbill.DNS.ARecord)12 MXRecord (org.xbill.DNS.MXRecord)7 TextParseException (org.xbill.DNS.TextParseException)7 UnknownHostException (java.net.UnknownHostException)5 ArrayList (java.util.ArrayList)5 AAAARecord (org.xbill.DNS.AAAARecord)5 NSRecord (org.xbill.DNS.NSRecord)5 Record (org.xbill.DNS.Record)5 SOARecord (org.xbill.DNS.SOARecord)5 CNAMERecord (org.xbill.DNS.CNAMERecord)4 Name (org.xbill.DNS.Name)4 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 SRVRecord (org.xbill.DNS.SRVRecord)3 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 X509Certificate (java.security.cert.X509Certificate)2 List (java.util.List)2