
/* (C) 2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

module MSLookup_mDNS_Templates {

import from DNS_Types all;
import from MSLookup_mDNS_Types all;

template MSLookup_mDNS ts_MSLookup_mDNS_query(integer id, charstring domain) := {
	dnsMessage := {
		header := {
			id := id,
			qr := DNS_QUERY,
			opCode := 0,
			aa := false,
			tc := false,
			rd := false,
			ra := false,
			z := '000'B,
			rCode := DNS_NO_ERROR,
			qdCount := 1,
			anCount := 0,
			nsCount := 0,
			arCount := 0
		},
		queries := {
			{
				qName := domain,
				qType := 255,
				qClass := DNS_IN
			}
		},
		answers := {},
		nameServerRecords := {},
		additionalRecords := {}
	},
	udpAddress := "239.192.23.42",
	udpPort := 4266
}

template MSLookup_mDNS tr_MSLookup_mDNS_query(charstring domain) := {
	dnsMessage := {
		header := {
			id := ?,
			qr := DNS_QUERY,
			opCode := 0,
			aa := false,
			tc := false,
			rd := false,
			ra := false,
			z := '000'B,
			rCode := DNS_NO_ERROR,
			qdCount := 1,
			anCount := 0,
			nsCount := 0,
			arCount := 0
		},
		queries := {
			{
				qName := domain,
				qType := 255,
				qClass := DNS_IN
			}
		},
		answers := {},
		nameServerRecords := {},
		additionalRecords := {}
	},
	udpAddress := ?,
	udpPort := ?
}

template MSLookup_mDNS ts_MSLookup_mDNS_result_IPv4(integer id, charstring domain, UInt32 ip_v4, integer port_v4) := {
	dnsMessage := {
		header := {
			id := id,
			qr := DNS_RESPONSE,
			opCode := DNS_OP_QUERY,
			aa := false,
			tc := false,
			rd := false,
			ra := false,
			z := '000'B,
			rCode := DNS_NO_ERROR,
			qdCount := 0,
			anCount := 3,
			nsCount := 0,
			arCount := 0
		},
		queries := {},
		answers := {
			{
				name := domain,
				rrType := DNS_TXT,
				rrClass := DNS_IN,
				ttl := '00000000'O,
				rdLength := 6,
				rData := {txt := {"age=0"}}
			},
			{
				name := domain,
				rrType := DNS_A,
				rrClass := DNS_IN,
				ttl := '00000000'O,
				rdLength := 4,
				rData := {a := ip_v4}
			},
			{
				name := domain,
				rrType := DNS_TXT,
				rrClass := DNS_IN,
				ttl := '00000000'O,
				rdLength := 1 + lengthof("port=" & int2str(port_v4)),
				rData := {txt := {"port=" & int2str(port_v4)}}
			}
		},
		nameServerRecords := {},
		additionalRecords := {}
	},
	udpAddress := "239.192.23.42",
	udpPort := 4266
}

template MSLookup_mDNS tr_MSLookup_mDNS_result_IPv4(integer id, charstring domain, UInt32 ip_v4, integer port_v4) := {
	dnsMessage := {
		header := {
			id := id,
			qr := DNS_RESPONSE,
			opCode := DNS_OP_QUERY,
			aa := false,
			tc := false,
			rd := false,
			ra := false,
			z := '000'B,
			rCode := DNS_NO_ERROR,
			qdCount := 0,
			anCount := 3,
			nsCount := 0,
			arCount := 0
		},
		queries := {},
		answers := {
			{
				name := domain,
				rrType := DNS_TXT,
				rrClass := DNS_IN,
				ttl := ?,
				rdLength := ?,
				rData := {txt := {pattern "age=*"}}
			},
			{
				name := domain,
				rrType := DNS_A,
				rrClass := DNS_IN,
				ttl := ?,
				rdLength := ?,
				rData := {a := ip_v4}
			},
			{
				name := domain,
				rrType := DNS_TXT,
				rrClass := DNS_IN,
				ttl := ?,
				rdLength := ?,
				rData := {txt := {"port=" & int2str(port_v4)}}
			}
		},
		nameServerRecords := {},
		additionalRecords := {}
	},
	udpAddress := ?,
	udpPort := ?
}

}
