Erlang Central

Difference between revisions of "Lookup MX Records In DNS"

From ErlangCentral Wiki

 
(Using The DNS Lookup Interface)
Line 56:Line 56:
  
 
mxlookup(Domain) ->
 
mxlookup(Domain) ->
   case inet_res:nslookup(Domain,1,mx) of
+
   case inet_res:nslookup(Domain,1,?S_MX) of
 
     { error, Reply } -> Reply;
 
     { error, Reply } -> Reply;
 
     { ok, #dns_rec{ arlist = Ans } } -> findmx( Ans )
 
     { ok, #dns_rec{ arlist = Ans } } -> findmx( Ans )

Revision as of 10:42, 21 July 2006

Contents

Introduction

For the E-mail server I'm writing I had the simple requirement to lookup MX records from the DNS. Erlang provides a small interface for performing DNS queries. The modules you'll need to use are inet_db, inet_dns and inet_res.

Using The DNS Lookup Interface

Although it looks like there is a mechanism for querying the system nameservers, I couldn't figure out how it works properly so I have to start by adding a nameserver to query from:

Code snippet 1


ok = inet_db:add_ns({192,168,0,10}).

This adds the given IP to the internal database of nameservers. (Note the Erlang convention of IP addresses given as quadruples).

The next stage is to construct an appropriate query. Handily enough Erlang provides a very simple interface. The header file is "kernel/src/inet_dns.hrl" and this provides all of the macros and details required to do the lookup.

Use include_lib to add this:

Code snippet 2



-include_lib("kernel/src/inet_dns.hrl").

The query is extremely simple to achieve, just pass the domain and record type to inet_res:nslookup/3.

This returns { ok, Result } on success or {error, Reason} on failure.

Result is actually a record, named #dns_rec in inet_dns.hrl. This record has five fields, but for the purposes of this HowTo we are only interested in one, the arlist field. This field is a list of resource entries. To do the MX lookup I simply match on the record and list:

Code snippet 3


mxlookup(Domain) ->
  case inet_res:nslookup(Domain,1,?S_MX) of
    { error, Reply } -> Reply;
    { ok, #dns_rec{ arlist = Ans } } -> findmx( Ans )
  end.

Each entry in this list is a record of #dns_rr (DNS Resource Record).

findmx/1 is a function which searches through the list of resources and filters out the MX servers. We're basically interested in the host addresses (for sending email to) so findmx/1 is very simple (and naive), for this HowTo I'll simply return the first host found:

Code snippet 4


findmx( [#dns_rr{ type=?S_A, data = Addr } | T] ) -> Addr;
findmx( [H | T ]) -> findmx(T);
findmx( _Other ) -> none.

Conclusion

Extensions like handling timeouts, sorting and verifying results and so forth should be straightforward, the comments in inet_dns.hrl outline what each member of each record means.

Listing

Code listing 1 (mxlookup.erl)


-module(mxlookup).

-export([lookup/1, init/1]).

-include_lib("kernel/src/inet_dns.hrl").

init([NS | T]) ->
  ok = inet_db:add_ns(NS),
  init(T);
init([]) -> ok;
init(Nameserver) ->
  ok = inet_db:add_ns(Nameserver).

findmx( [#dns_rr{ type=?S_A, data = Addr } | _] ) -> Addr;
findmx( [_ | T ]) -> findmx(T);
findmx( _Other ) -> none.

lookup(Domain) ->
  case inet_res:nslookup(Domain,1,mx) of
    { error, Reply } -> Reply;
    { ok, #dns_rec{ arlist = Ans } } -> findmx( Ans )
  end.

Code listing 2 (testmx.erl)


-module(testmx).

-export([test/0]).

test() ->
  mxlookup:init( [{192,168,0,10}] ),
  Addr1 = mxlookup:lookup( "atomictiger.net" ),
  Addr2 = mxlookup:lookup( "trapexit.org" ),
  io:format("Atomic Tiger: ~p~nTrapexit: ~p~n",[Addr1,Addr2]).