github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/models/dnsrr.go (about)

     1  package models
     2  
     3  // methods that make RecordConfig meet the dns.RR interface.
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  	"strings"
     9  
    10  	"github.com/miekg/dns"
    11  )
    12  
    13  //// Header Header returns the header of an resource record.
    14  //func (rc *RecordConfig) Header() *dns.RR_Header {
    15  //	log.Fatal("Header not implemented")
    16  //	return nil
    17  //}
    18  
    19  // String returns the text representation of the resource record.
    20  func (rc *RecordConfig) String() string {
    21  	return rc.GetTargetCombined()
    22  }
    23  
    24  //// copy returns a copy of the RR
    25  //func (rc *RecordConfig) copy() dns.RR {
    26  //	log.Fatal("Copy not implemented")
    27  //	return dns.TypeToRR[dns.TypeA]()
    28  //}
    29  //
    30  //// len returns the length (in octets) of the uncompressed RR in wire format.
    31  //func (rc *RecordConfig) len() int {
    32  //	log.Fatal("len not implemented")
    33  //	return 0
    34  //}
    35  //
    36  //// pack packs an RR into wire format.
    37  //func (rc *RecordConfig) pack([]byte, int, map[string]int, bool) (int, error) {
    38  //	log.Fatal("pack not implemented")
    39  //	return 0, nil
    40  //}
    41  
    42  // Conversions
    43  
    44  // RRstoRCs converts []dns.RR to []RecordConfigs.
    45  func RRstoRCs(rrs []dns.RR, origin string) Records {
    46  	rcs := make(Records, 0, len(rrs))
    47  	for _, r := range rrs {
    48  		var rc RecordConfig
    49  		rc = RRtoRC(r, origin)
    50  		rcs = append(rcs, &rc)
    51  	}
    52  	return rcs
    53  }
    54  
    55  // RRtoRC converts dns.RR to RecordConfig
    56  func RRtoRC(rr dns.RR, origin string) RecordConfig {
    57  	// Convert's dns.RR into our native data type (RecordConfig).
    58  	// Records are translated directly with no changes.
    59  	header := rr.Header()
    60  	rc := new(RecordConfig)
    61  	rc.Type = dns.TypeToString[header.Rrtype]
    62  	rc.TTL = header.Ttl
    63  	rc.Original = rr
    64  	rc.SetLabelFromFQDN(strings.TrimSuffix(header.Name, "."), origin)
    65  	switch v := rr.(type) { // #rtype_variations
    66  	case *dns.A:
    67  		panicInvalid(rc.SetTarget(v.A.String()))
    68  	case *dns.AAAA:
    69  		panicInvalid(rc.SetTarget(v.AAAA.String()))
    70  	case *dns.CAA:
    71  		panicInvalid(rc.SetTargetCAA(v.Flag, v.Tag, v.Value))
    72  	case *dns.CNAME:
    73  		panicInvalid(rc.SetTarget(v.Target))
    74  	case *dns.MX:
    75  		panicInvalid(rc.SetTargetMX(v.Preference, v.Mx))
    76  	case *dns.NS:
    77  		panicInvalid(rc.SetTarget(v.Ns))
    78  	case *dns.PTR:
    79  		panicInvalid(rc.SetTarget(v.Ptr))
    80  	case *dns.NAPTR:
    81  		panicInvalid(rc.SetTargetNAPTR(v.Order, v.Preference, v.Flags, v.Service, v.Regexp, v.Replacement))
    82  	case *dns.SOA:
    83  		panicInvalid(rc.SetTargetSOA(v.Ns, v.Mbox, v.Serial, v.Refresh, v.Retry, v.Expire, v.Minttl))
    84  	case *dns.SRV:
    85  		panicInvalid(rc.SetTargetSRV(v.Priority, v.Weight, v.Port, v.Target))
    86  	case *dns.SSHFP:
    87  		panicInvalid(rc.SetTargetSSHFP(v.Algorithm, v.Type, v.FingerPrint))
    88  	case *dns.TLSA:
    89  		panicInvalid(rc.SetTargetTLSA(v.Usage, v.Selector, v.MatchingType, v.Certificate))
    90  	case *dns.TXT:
    91  		panicInvalid(rc.SetTargetTXTs(v.Txt))
    92  	default:
    93  		log.Fatalf("rrToRecord: Unimplemented zone record type=%s (%v)\n", rc.Type, rr)
    94  	}
    95  	return *rc
    96  }
    97  
    98  func panicInvalid(err error) {
    99  	if err != nil {
   100  		panic(fmt.Errorf("unparsable record received from BIND: %w", err))
   101  	}
   102  }