1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819// Package dns defines interfaces used by maddy modules to perform DNS20// lookups.21//22// Currently, there is only Resolver interface which is implemented23// by dns.DefaultResolver(). In the future, DNSSEC-enabled stub resolver24// implementation will be added here.25package dns2627import (28 "context"29 "net"30 "strings"31)3233// Resolver is an interface that describes DNS-related methods used by maddy.34//35// It is implemented by dns.DefaultResolver(). Methods behave the same way.36type Resolver interface {37 LookupAddr(ctx context.Context, addr string) (names []string, err error)38 LookupHost(ctx context.Context, host string) (addrs []string, err error)39 LookupMX(ctx context.Context, name string) ([]*net.MX, error)40 LookupTXT(ctx context.Context, name string) ([]string, error)41 LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)42}4344// LookupAddr is a convenience wrapper for Resolver.LookupAddr.45//46// It returns the first name with trailing dot stripped.47func LookupAddr(ctx context.Context, r Resolver, ip net.IP) (string, error) {48 names, err := r.LookupAddr(ctx, ip.String())49 if err != nil || len(names) == 0 {50 return "", err51 }52 return strings.TrimRight(names[0], "."), nil53}5455func DefaultResolver() Resolver {56 if overrideServ != "" && overrideServ != "system-default" {57 override(overrideServ)58 }5960 return net.DefaultResolver61}