DEFINITION MODULE MXCheck;

        (********************************************************)
        (*                                                      *)
        (*      Extracting MX information from nameserver.      *)
        (*           This version discards everything           *)
        (*          except the MX responses themselves.         *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            03 July 1998                    *)
        (*  Last edited:        20 July 2018                    *)
        (*  Status:             OK                              *)
        (*                                                      *)
        (********************************************************)


FROM Names IMPORT (*type*) DomainName;

PROCEDURE DoMXLookup (domain: DomainName;
                      VAR (*OUT*) address: ARRAY OF CARDINAL): CARDINAL;

    (* Checks the MX records on the nameserver for the given domain,    *)
    (* and returns an array (ordered by preference) of IP addresses for *)
    (* hosts that will handle mail for that domain.  If the address     *)
    (* array is not filled, the end of the list is marked by a zero     *)
    (* address.  An empty list means that the nameserver lookup failed  *)
    (* to find an answer.  The function result is an error code:        *)
    (*      0    all OK, no error                                       *)
    (*      1    hard error, name not found                             *)
    (*      2    soft error, try again later                            *)
    (*      3    nameserver unreachable, try again later                *)
    (* The addresses are in network byte order.  Note: if no MX records *)
    (* exist, we default back to interpreting the domain as a host      *)
    (* name.                                                            *)

PROCEDURE MXMatch (domain: DomainName;  addr, Nbits: CARDINAL;
                   skipMX: BOOLEAN;  VAR (*OUT*) error: BOOLEAN): BOOLEAN;

    (* Returns TRUE iff the top Nbits bits of the 32-bit IP address     *)
    (* addr matches one of the addresses of the MX hosts for the        *)
    (* specified domain.  To avoid DNS overload, we return with a FALSE *)
    (* result and with the error parameter set if we do not have a      *)
    (* match within the first 10 results.                               *)

    (* If skipMX is TRUE we skip the MX lookup and go directly for      *)
    (* the A records for domain.                                        *)

PROCEDURE MakeMask (nbits: CARDINAL): CARDINAL;

    (* Creates a 32-bit result where the top nbits are 1, and all       *)
    (* others are zero.                                                 *)

END MXCheck.

