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

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // SetTargetTLSA sets the TLSA fields.
    10  func (rc *RecordConfig) SetTargetTLSA(usage, selector, matchingtype uint8, target string) error {
    11  	rc.TlsaUsage = usage
    12  	rc.TlsaSelector = selector
    13  	rc.TlsaMatchingType = matchingtype
    14  	rc.SetTarget(target)
    15  	if rc.Type == "" {
    16  		rc.Type = "TLSA"
    17  	}
    18  	if rc.Type != "TLSA" {
    19  		panic("assertion failed: SetTargetTLSA called when .Type is not TLSA")
    20  	}
    21  	return nil
    22  }
    23  
    24  // SetTargetTLSAStrings is like SetTargetTLSA but accepts strings.
    25  func (rc *RecordConfig) SetTargetTLSAStrings(usage, selector, matchingtype, target string) (err error) {
    26  	var i64usage, i64selector, i64matchingtype uint64
    27  	if i64usage, err = strconv.ParseUint(usage, 10, 8); err == nil {
    28  		if i64selector, err = strconv.ParseUint(selector, 10, 8); err == nil {
    29  			if i64matchingtype, err = strconv.ParseUint(matchingtype, 10, 8); err == nil {
    30  				return rc.SetTargetTLSA(uint8(i64usage), uint8(i64selector), uint8(i64matchingtype), target)
    31  			}
    32  		}
    33  	}
    34  	return fmt.Errorf("TLSA has value that won't fit in field: %w", err)
    35  }
    36  
    37  // SetTargetTLSAString is like SetTargetTLSA but accepts one big string.
    38  func (rc *RecordConfig) SetTargetTLSAString(s string) error {
    39  	part := strings.Fields(s)
    40  	if len(part) != 4 {
    41  		return fmt.Errorf("TLSA value does not contain 4 fields: (%#v)", s)
    42  	}
    43  	return rc.SetTargetTLSAStrings(part[0], part[1], part[2], part[3])
    44  }