Esempio n. 1
0
File: dns.go Progetto: jarpy/beats
// dnsResourceRecordToString converts a RR to a string.
func dnsResourceRecordToString(rr mkdns.RR) string {
	rrHeader := rr.Header()
	rrType := rrHeader.Rrtype

	var data string
	switch x := rr.(type) {
	default:
		// We don't have special handling for this type
		logp.Debug("dns", "No special handling for RR type %s", dnsTypeToString(rrType))
		unsupportedRR := new(mkdns.RFC3597)
		err := unsupportedRR.ToRFC3597(x)
		if err == nil {
			rData, err := hexStringToString(unsupportedRR.Rdata)
			data = rData
			if err != nil {
				logp.Debug("dns", "%s", err.Error())
			}
		} else {
			logp.Debug("dns", "Rdata for the unhandled RR type %s could not be fetched", dnsTypeToString(rrType))
		}
	case *mkdns.A:
		data = x.A.String()
	case *mkdns.AAAA:
		data = x.AAAA.String()
	case *mkdns.CNAME:
		data = x.Target
	case *mkdns.MX:
		data = fmt.Sprintf("preference %d, %s", x.Preference, x.Mx)
	case *mkdns.NS:
		data = x.Ns
	case *mkdns.PTR:
		data = x.Ptr
	case *mkdns.RFC3597:
		// Miekg/dns lib doesn't handle this type
		logp.Debug("dns", "Unknown RR type %s", dnsTypeToString(rrType))
		rData, err := hexStringToString(x.Rdata)
		data = rData
		if err != nil {
			logp.Debug("dns", "%s", err.Error())
		}
	case *mkdns.SOA:
		data = fmt.Sprintf("mname %s, rname %s, serial %d, refresh %d, "+
			"retry %d, expire %d, minimum %d", x.Ns, x.Mbox,
			x.Serial, x.Refresh, x.Retry, x.Expire,
			x.Minttl)
	case *mkdns.SRV:
		data = fmt.Sprintf("priority %d, weight %d, port %d, %s", x.Priority,
			x.Weight, x.Port, x.Target)
	case *mkdns.TXT:
		data = strings.Join(x.Txt, " ")
	}

	return fmt.Sprintf("%s: ttl %d, class %s, type %s, %s", rrHeader.Name,
		int(rrHeader.Ttl), dnsClassToString(rrHeader.Class),
		dnsTypeToString(rrType), data)
}
Esempio n. 2
0
func sectionToResourceRecords(section []dns.RR) []ResourceRecord {
	var a []ResourceRecord
	for _, r := range section {
		x := new(dns.RFC3597)
		x.ToRFC3597(r)
		a = append(a, ResourceRecord{r.Header().Name,
			dns.TypeToString[r.Header().Rrtype],
			dns.ClassToString[r.Header().Class],
			r.Header().Ttl,
			r.Header().Rdlength,
			x.Rdata})
	}
	return a
}
Esempio n. 3
0
func rrToMapStr(rr mkdns.RR) common.MapStr {
	mapStr := common.MapStr{}
	rrType := rr.Header().Rrtype

	switch x := rr.(type) {
	default:
		// We don't have special handling for this type
		debugf("No special handling for RR type %s", dnsTypeToString(rrType))
		unsupportedRR := new(mkdns.RFC3597)
		err := unsupportedRR.ToRFC3597(x)
		if err == nil {
			rData, err := hexStringToString(unsupportedRR.Rdata)
			mapStr["data"] = rData
			if err != nil {
				debugf("%s", err.Error())
			}
		} else {
			debugf("Rdata for the unhandled RR type %s could not be fetched", dnsTypeToString(rrType))
		}
	case *mkdns.A:
		mapStr["data"] = x.A.String()
	case *mkdns.AAAA:
		mapStr["data"] = x.AAAA.String()
	case *mkdns.CNAME:
		mapStr["data"] = x.Target
	case *mkdns.DNSKEY:
		mapStr["flags"] = strconv.Itoa(int(x.Flags))
		mapStr["protocol"] = strconv.Itoa(int(x.Protocol))
		mapStr["algorithm"] = dnsAlgorithmToString(x.Algorithm)
		mapStr["data"] = x.PublicKey
	case *mkdns.DS:
		mapStr["key_tag"] = strconv.Itoa(int(x.KeyTag))
		mapStr["algorithm"] = dnsAlgorithmToString(x.Algorithm)
		mapStr["digest_type"] = dnsHashToString(x.DigestType)
		mapStr["data"] = strings.ToUpper(x.Digest)
	case *mkdns.MX:
		mapStr["preference"] = x.Preference
		mapStr["data"] = x.Mx
	case *mkdns.NS:
		mapStr["data"] = x.Ns
	case *mkdns.NSEC:
		mapStr["type_bits"] = dnsTypeBitsMapToString(x.TypeBitMap)
		mapStr["data"] = x.NextDomain
	case *mkdns.NSEC3:
		mapStr["hash"] = dnsHashToString(x.Hash)
		mapStr["flags"] = strconv.Itoa(int(x.Flags))
		mapStr["iterations"] = strconv.Itoa(int(x.Iterations))
		mapStr["salt"] = dnsSaltToString(x.Salt)
		mapStr["type_bits"] = dnsTypeBitsMapToString(x.TypeBitMap)
		mapStr["data"] = x.NextDomain
	case *mkdns.NSEC3PARAM:
		mapStr["hash"] = dnsHashToString(x.Hash)
		mapStr["flags"] = strconv.Itoa(int(x.Flags))
		mapStr["iterations"] = strconv.Itoa(int(x.Iterations))
		mapStr["data"] = dnsSaltToString(x.Salt)
	case *mkdns.OPT: // EDNS [RFC6891]
		// OPT pseudo-RR is managed in addDnsToMapStr function
		return nil
	case *mkdns.PTR:
		mapStr["data"] = x.Ptr
	case *mkdns.RFC3597:
		// Miekg/dns lib doesn't handle this type
		debugf("Unknown RR type %s", dnsTypeToString(rrType))
		rData, err := hexStringToString(x.Rdata)
		mapStr["data"] = rData
		if err != nil {
			debugf("%s", err.Error())
		}
	case *mkdns.RRSIG:
		mapStr["type_covered"] = dnsTypeToString(x.TypeCovered)
		mapStr["algorithm"] = dnsAlgorithmToString(x.Algorithm)
		mapStr["labels"] = strconv.Itoa(int(x.Labels))
		mapStr["original_ttl"] = strconv.FormatInt(int64(x.OrigTtl), 10)
		mapStr["expiration"] = mkdns.TimeToString(x.Expiration)
		mapStr["inception"] = mkdns.TimeToString(x.Inception)
		mapStr["key_tag"] = strconv.Itoa(int(x.KeyTag))
		mapStr["signer_name"] = x.SignerName
		mapStr["data"] = x.Signature
	case *mkdns.SOA:
		mapStr["rname"] = x.Mbox
		mapStr["serial"] = x.Serial
		mapStr["refresh"] = x.Refresh
		mapStr["retry"] = x.Retry
		mapStr["expire"] = x.Expire
		mapStr["minimum"] = x.Minttl
		mapStr["data"] = x.Ns
	case *mkdns.SRV:
		mapStr["priority"] = x.Priority
		mapStr["weight"] = x.Weight
		mapStr["port"] = x.Port
		mapStr["data"] = x.Target
	case *mkdns.TXT:
		mapStr["data"] = strings.Join(x.Txt, " ")
	}

	return mapStr
}
Esempio n. 4
0
File: dns.go Progetto: jarpy/beats
// rrToMapStr converts an array of DNSResourceRecord's to an array of MapStr's.
func rrToMapStr(records []mkdns.RR) []common.MapStr {
	mapStrArray := make([]common.MapStr, len(records))
	for i, rr := range records {
		rrHeader := rr.Header()
		rrType := rrHeader.Rrtype

		mapStr := common.MapStr{
			"name":  rrHeader.Name,
			"type":  dnsTypeToString(rrType),
			"class": dnsClassToString(rrHeader.Class),
			"ttl":   strconv.FormatInt(int64(rrHeader.Ttl), 10),
		}
		mapStrArray[i] = mapStr

		switch x := rr.(type) {
		default:
			// We don't have special handling for this type
			logp.Debug("dns", "No special handling for RR type %s", dnsTypeToString(rrType))
			unsupportedRR := new(mkdns.RFC3597)
			err := unsupportedRR.ToRFC3597(x)
			if err == nil {
				rData, err := hexStringToString(unsupportedRR.Rdata)
				mapStr["data"] = rData
				if err != nil {
					logp.Debug("dns", "%s", err.Error())
				}
			} else {
				logp.Debug("dns", "Rdata for the unhandled RR type %s could not be fetched", dnsTypeToString(rrType))
			}
		case *mkdns.A:
			mapStr["data"] = x.A.String()
		case *mkdns.AAAA:
			mapStr["data"] = x.AAAA.String()
		case *mkdns.CNAME:
			mapStr["data"] = x.Target
		case *mkdns.MX:
			mapStr["preference"] = x.Preference
			mapStr["data"] = x.Mx
		case *mkdns.NS:
			mapStr["data"] = x.Ns
		case *mkdns.PTR:
			mapStr["data"] = x.Ptr
		case *mkdns.RFC3597:
			// Miekg/dns lib doesn't handle this type
			//TODO: write a test for this.
			logp.Debug("dns", "Unknown RR type %s", dnsTypeToString(rrType))
			rData, err := hexStringToString(x.Rdata)
			mapStr["data"] = rData
			if err != nil {
				logp.Debug("dns", "%s", err.Error())
			}
		case *mkdns.SOA:
			mapStr["rname"] = x.Mbox
			mapStr["serial"] = x.Serial
			mapStr["refresh"] = x.Refresh
			mapStr["retry"] = x.Retry
			mapStr["expire"] = x.Expire
			mapStr["minimum"] = x.Minttl
			mapStr["data"] = x.Ns
		case *mkdns.SRV:
			mapStr["priority"] = x.Priority
			mapStr["weight"] = x.Weight
			mapStr["port"] = x.Port
			mapStr["data"] = x.Target
		case *mkdns.TXT:
			mapStr["data"] = strings.Join(x.Txt, " ")
		}
	}

	return mapStrArray
}