aboutsummaryrefslogtreecommitdiff
path: root/plugin/bufsize/bufsize.go
blob: 00556c2bab648170ee684c6265d04123c9fa8751 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Package bufsize implements a plugin that clamps EDNS0 buffer size preventing packet fragmentation.
package bufsize

import (
	"context"

	"github.com/coredns/coredns/plugin"

	"github.com/miekg/dns"
)

// Bufsize implements bufsize plugin.
type Bufsize struct {
	Next plugin.Handler
	Size int
}

// ServeDNS implements the plugin.Handler interface.
func (buf Bufsize) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
	if option := r.IsEdns0(); option != nil && int(option.UDPSize()) > buf.Size {
		option.SetUDPSize(uint16(buf.Size))
	}
	return plugin.NextOrFailure(buf.Name(), buf.Next, ctx, w, r)
}

// Name implements the Handler interface.
func (buf Bufsize) Name() string { return "bufsize" }