1package dns
2
3// NameUsed sets the RRs in the prereq section to
4// "Name is in use" RRs. RFC 2136 section 2.4.4.
5func (u *Msg) NameUsed(rr []RR) {
6	if u.Answer == nil {
7		u.Answer = make([]RR, 0, len(rr))
8	}
9	for _, r := range rr {
10		u.Answer = append(u.Answer, &ANY{Hdr: RR_Header{Name: r.Header().Name, Ttl: 0, Rrtype: TypeANY, Class: ClassANY}})
11	}
12}
13
14// NameNotUsed sets the RRs in the prereq section to
15// "Name is in not use" RRs. RFC 2136 section 2.4.5.
16func (u *Msg) NameNotUsed(rr []RR) {
17	if u.Answer == nil {
18		u.Answer = make([]RR, 0, len(rr))
19	}
20	for _, r := range rr {
21		u.Answer = append(u.Answer, &ANY{Hdr: RR_Header{Name: r.Header().Name, Ttl: 0, Rrtype: TypeANY, Class: ClassNONE}})
22	}
23}
24
25// Used sets the RRs in the prereq section to
26// "RRset exists (value dependent -- with rdata)" RRs. RFC 2136 section 2.4.2.
27func (u *Msg) Used(rr []RR) {
28	if len(u.Question) == 0 {
29		panic("dns: empty question section")
30	}
31	if u.Answer == nil {
32		u.Answer = make([]RR, 0, len(rr))
33	}
34	for _, r := range rr {
35		r.Header().Class = u.Question[0].Qclass
36		u.Answer = append(u.Answer, r)
37	}
38}
39
40// RRsetUsed sets the RRs in the prereq section to
41// "RRset exists (value independent -- no rdata)" RRs. RFC 2136 section 2.4.1.
42func (u *Msg) RRsetUsed(rr []RR) {
43	if u.Answer == nil {
44		u.Answer = make([]RR, 0, len(rr))
45	}
46	for _, r := range rr {
47		h := r.Header()
48		u.Answer = append(u.Answer, &ANY{Hdr: RR_Header{Name: h.Name, Ttl: 0, Rrtype: h.Rrtype, Class: ClassANY}})
49	}
50}
51
52// RRsetNotUsed sets the RRs in the prereq section to
53// "RRset does not exist" RRs. RFC 2136 section 2.4.3.
54func (u *Msg) RRsetNotUsed(rr []RR) {
55	if u.Answer == nil {
56		u.Answer = make([]RR, 0, len(rr))
57	}
58	for _, r := range rr {
59		h := r.Header()
60		u.Answer = append(u.Answer, &ANY{Hdr: RR_Header{Name: h.Name, Ttl: 0, Rrtype: h.Rrtype, Class: ClassNONE}})
61	}
62}
63
64// Insert creates a dynamic update packet that adds an complete RRset, see RFC 2136 section 2.5.1.
65func (u *Msg) Insert(rr []RR) {
66	if len(u.Question) == 0 {
67		panic("dns: empty question section")
68	}
69	if u.Ns == nil {
70		u.Ns = make([]RR, 0, len(rr))
71	}
72	for _, r := range rr {
73		r.Header().Class = u.Question[0].Qclass
74		u.Ns = append(u.Ns, r)
75	}
76}
77
78// RemoveRRset creates a dynamic update packet that deletes an RRset, see RFC 2136 section 2.5.2.
79func (u *Msg) RemoveRRset(rr []RR) {
80	if u.Ns == nil {
81		u.Ns = make([]RR, 0, len(rr))
82	}
83	for _, r := range rr {
84		h := r.Header()
85		u.Ns = append(u.Ns, &ANY{Hdr: RR_Header{Name: h.Name, Ttl: 0, Rrtype: h.Rrtype, Class: ClassANY}})
86	}
87}
88
89// RemoveName creates a dynamic update packet that deletes all RRsets of a name, see RFC 2136 section 2.5.3
90func (u *Msg) RemoveName(rr []RR) {
91	if u.Ns == nil {
92		u.Ns = make([]RR, 0, len(rr))
93	}
94	for _, r := range rr {
95		u.Ns = append(u.Ns, &ANY{Hdr: RR_Header{Name: r.Header().Name, Ttl: 0, Rrtype: TypeANY, Class: ClassANY}})
96	}
97}
98
99// Remove creates a dynamic update packet deletes RR from a RRSset, see RFC 2136 section 2.5.4
100func (u *Msg) Remove(rr []RR) {
101	if u.Ns == nil {
102		u.Ns = make([]RR, 0, len(rr))
103	}
104	for _, r := range rr {
105		h := r.Header()
106		h.Class = ClassNONE
107		h.Ttl = 0
108		u.Ns = append(u.Ns, r)
109	}
110}
111