Mailfromd Manual (split by section):   Section:   Chapter:FastBack: Library   Up: Library   FastForward: Using MFL Mode   Contents: Table of ContentsIndex: Concept Index

5.20 DNS Functions

The functions are implemented in two layers: primitive built-in functions which raise exceptions if the lookup fails, and library calls that are warranted to always return meaningful value without throwing exceptions.

The built-in layer is always available. The library calls become available after requesting the dns module (see Modules):

require dns
Built-in Function: string dns_getaddr (string domain)

Returns a whitespace-separated list of IP addresses (A records) for domain.

This function does not use the DNS cache.

Built-in Function: string dns_getname (string ipstr)

Returns a whitespace-separated list of domain names (PTR records) for the IPv4 address ipstr.

This function does not use the DNS cache.

Built-in Function: string getmx (string domain [, boolean ip])

Returns a whitespace-separated list of ‘MX’ names (if ip is not given or if it is 0) or ‘MXIP addresses (if ip!=0)) for domain. Within the returned string, items are sorted in order of increasing ‘MX’ priority. If domain has no ‘MX’ records, an empty string is returned. If the DNS query fails, getmx raises an appropriate exception.

Examples:

getmx("mafra.cz") ⇒ "smtp1.mafra.cz smtp2.mafra.cz relay.iol.cz"
getmx("idnes.cz") ⇒ "smtp1.mafra.cz smtp2.mafra.cz relay.iol.cz"
getmx("gnu.org")  ⇒ "mx10.gnu.org mx20.gnu.org"
getmx("org.pl") ⇒ ""

Note:

  1. The number of items returned by getmx(domain) can differ from that obtained from getmx(domain, 1), e.g.:
    getmx("aol.com")
      ⇒ mailin-01.mx.aol.com mailin-02.mx.aol.com
                mailin-03.mx.aol.com mailin-04.mx.aol.com
    getmx("aol.com", 1)
      ⇒ 64.12.137.89 64.12.137.168 64.12.137.184
                64.12.137.249 64.12.138.57 64.12.138.88
                64.12.138.120 64.12.138.185 205.188.155.89
                205.188.156.185 205.188.156.249 205.188.157.25
                205.188.157.217 205.188.158.121 205.188.159.57
                205.188.159.217
    
  2. This interface will change in future releases, when array data types are implemented.
Built-in Function: boolean primitive_hasmx (string domain)

Returns true if the domain name given by its argument has any ‘MX’ records.

If the DNS query fails, this function throws failure or temp_failure.

Library Function: boolean hasmx (string domain)

Returns true if the domain name given by its argument has any ‘MX’ records.

Otherwise, if domain has no ‘MX’s or if the DNS query fails, hasmx returns false.

Built-in Function: string primitive_hostname (string ip)

The ip argument should be a string representing an IP address in dotted-quad notation. The function returns the canonical name of the host with this IP address obtained from DNS lookup. For example

primitive_hostname (${client_addr})

returns the fully qualified domain name of the host represented by Sendmail variable ‘client_addr’.

If there is no ‘PTR’ record for ip, primitive_hostname raises the exception e_not_found.

If DNS query fails, the function raises failure or temp_failure, depending on the character of the failure.

Library Function: string hostname (string ip)

The ip argument should be a string representing an IP address in dotted-quad notation. The function returns the canonical name of the host with this IP address obtained from DNS lookup.

If there is no ‘PTR’ record for ip, or if the lookup fails, the function returns ip unchanged.

The previous mailfromd versions used the following paradigm to check if an IP address resolves:

  if hostname(ip) != ip
    ...
Built-in Function: boolean primitive_ismx (string domain, string host)

The domain argument is any valid domain name, the host is a host name or IP address.

The function returns true if host is one of the ‘MX’ records for the domain.

If domain has no ‘MX’ records, primitive_ismx raises exception e_not_found.

If DNS query fails, the function raises failure or temp_failure, depending on the character of the failure.

Library Function: boolean ismx (string domain, string host)

The domain argument is any valid domain name, the host is a host name or IP address.

The function returns true if host is one of the ‘MX’ records for the domain. Otherwise it returns false.

If domain has no ‘MX’ records, or if the DNS query fails, the function returns false.

Built-in Function: string primitive_resolve (string host, [string domain])

Reverse of primitive_hostname. The primitive_resolve function returns the IP address for the host name specified by host argument. If host has no A records, the function raises the exception e_not_found.

If DNS lookup fails, the function raises failure or temp_failure, depending on the character of the failure.

If the optional domain argument is given, it will be appended to host (with an intermediate dot), before querying the DNS. For example, the following two expressions will return the same value:

primitive_resolve("puszcza.gnu.org.ua")
primitive_resolve("puszcza", "gnu.org.ua")

There is a considerable internal difference between one-argument and two-argument forms of primitive_resolve: the former queries DNS for an ‘A’ record, whereas the latter queries it for any record matching host in the domain domain and then selects the most appropriate one. For example, the following two calls are equivalent:

primitive_hostname("213.130.0.22")
primitive_resolve("22.0.130.213", "in-addr.arpa")

This makes it possible to use primitive_resolve for querying DNS black listing domains. See match_dnsbl, for a working example of this approach. See also match_rhsbl, for another practical example of the use of the two-argument form.

Library Function: string resolve (string host, [string domain])

Reverse of hostname. The resolve function returns IP address for the host name specified by host argument. If the host name cannot be resolved, or a DNS failure occurs, the function returns ‘"0"’.

This function is entirely equivalent to primitive_resolve (see above), except that it never raises exceptions.

Built-in Function: string ptr_validate (string ip)

Tests whether the DNS reverse-mapping for ip exists and correctly points to a domain name within a particular domain.

First, it obtains all PTR records for ip. Then, for each record returned, a look up for A records is performed and IP addresses of each record are compared against ip. The function returns true if a matching A record is found.

Built-in Function: boolean primitive_hasns (string domain)

Returns ‘True’ if the domain domain has at least one ‘NS’ record. Throws exception if DNS lookup fails.

Library Function: boolean hasns (string domain)

Returns ‘True’ if the domain domain has at least one ‘NS’ record. Returns ‘False’ if there are no ‘NS’ records or if the DNS lookup fails.

Built-in Function: string getns (string domain ; boolean resolve, boolean sort)

Returns a whitespace-separated list of all the ‘NS’ records for the domain domain. Optional parameters resolve and sort control the formatting. If resolve is 0 (the default), the resulting string will contain IP addresses of the NS servers. If resolve is not 0, hostnames will be returned instead. If sort is 1, the returned items will be sorted.

If the DNS query fails, getns raises an appropriate exception.

Mailfromd Manual (split by section):   Section:   Chapter:FastBack: Library   Up: Library   FastForward: Using MFL Mode   Contents: Table of ContentsIndex: Concept Index