public static ZoneRecordsResponse ToZoneRecordsResponse(this DnsRecord record, int minimumTTL, string zoneName) { //Declare the result var response = new ZoneRecordsResponse(); //Check that the record is in SDNS format if (!record.RecordName.Contains(zoneName)) { record.RecordName = $"{record.RecordName}.{zoneName}"; } //Build up the response response.Name = record.RecordName; response.Type = record.RecordType.ToString(); //response.Ttl = record.RecordText == null ? minimumTTL : Convert.ToInt32(record.RecordText.Split('\t')[1]); // ReSharper disable once SwitchStatementMissingSomeCases switch (record.RecordType) { case DnsRecordType.MX: if (!record.RecordData.EndsWith(".") && record.RecordData.Length > 0) { response.Data = $"{record.MxPriority} {record.RecordData}."; break; } response.Data = $"{record.MxPriority} {record.RecordData}"; break; case DnsRecordType.SRV: if (!record.RecordData.EndsWith(".") && record.RecordData.Length > 0) { response.Data = $"{record.SrvPriority} {record.SrvWeight} {record.SrvPort} {record.RecordData}."; break; } response.Data = $"{record.SrvPriority} {record.SrvWeight} {record.SrvPort} {record.RecordData}"; break; case DnsRecordType.CNAME: case DnsRecordType.NS: if (!record.RecordData.EndsWith(".") && record.RecordData.Length > 0) { response.Data = $"{record.RecordData}."; break; } response.Data = $"{record.RecordData}"; break; default: response.Data = record.RecordData ?? ""; break; } //Return the response return(response); }
/// <summary> /// Method to convert <see cref="ZoneRecordsResponse"/> to <see cref="DnsRecord"/> /// </summary> /// <param name="record">DNS Record in <see cref="ZoneRecordsResponse"/> format</param> private static DnsRecord ZoneRecordResponseToDnsRecord(ZoneRecordsResponse record) { //Null checking if (record == null) { throw new ArgumentNullException(nameof(record)); } //Declare result variable var resultRecord = new DnsRecord(); //Switch on the type switch (record.Type) { case "A": resultRecord.RecordType = DnsRecordType.A; break; case "AAAA": resultRecord.RecordType = DnsRecordType.AAAA; break; case "CAA": resultRecord.RecordType = DnsRecordType.CAA; break; case "CNAME": resultRecord.RecordType = DnsRecordType.CNAME; resultRecord.RecordData = record.Data.TrimEnd('.'); break; case "MX": resultRecord.RecordType = DnsRecordType.MX; resultRecord.MxPriority = Convert.ToInt32(record.Data.Split(' ')[0]); resultRecord.RecordData = record.Data.Split(' ')[1].TrimEnd('.'); break; case "NS": resultRecord.RecordType = DnsRecordType.NS; resultRecord.RecordData = record.Data.TrimEnd('.'); break; case "SOA": resultRecord.RecordType = DnsRecordType.SOA; break; case "SRV": resultRecord.RecordType = DnsRecordType.SRV; resultRecord.SrvPriority = Convert.ToInt32(record.Data.Split(' ')[0]); resultRecord.SrvWeight = Convert.ToInt32(record.Data.Split(' ')[1]); resultRecord.SrvPort = Convert.ToInt32(record.Data.Split(' ')[2]); resultRecord.RecordData = record.Data.Split(' ')[3].TrimEnd('.'); break; case "TXT": resultRecord.RecordType = DnsRecordType.TXT; break; default: resultRecord.RecordType = DnsRecordType.Other; break; } //Build up the rest of the record //If data is already set, don't change it if (string.IsNullOrWhiteSpace(resultRecord.RecordData)) { resultRecord.RecordData = record.Data; } //Build the remaining fields of the record resultRecord.RecordName = record.Name; resultRecord.RecordText = $"{record.Name}\t{record.Ttl}\t{record.Type}\t{record.Data}"; //Return the result return(resultRecord); }