Erlang Central

Difference between revisions of "Lookup MX Records In DNS"

From ErlangCentral Wiki

(Using The DNS Lookup Interface)
Line 10: Line 10:
 
out how it works properly so I have to start by adding a nameserver to query from:
 
out how it works properly so I have to start by adding a nameserver to query from:
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
<code caption="Code snippet 1">
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code snippet 1</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
  
 
ok = inet_db:add_ns({192,168,0,10}).
 
ok = inet_db:add_ns({192,168,0,10}).
  
</pre></td></tr>
+
</code>
</table>
+
  
 
This adds the given IP to the internal database of nameservers. (Note the Erlang convention
 
This adds the given IP to the internal database of nameservers. (Note the Erlang convention
Line 29: Line 25:
 
Use include_lib to add this:
 
Use include_lib to add this:
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
<code caption="Code snippet 2">
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code snippet 2</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
 
+
  
 
-include_lib("kernel/src/inet_dns.hrl").
 
-include_lib("kernel/src/inet_dns.hrl").
  
</pre></td></tr>
+
</code>
</table>
+
  
 
The query is extremely simple to achieve, just pass the domain and record type
 
The query is extremely simple to achieve, just pass the domain and record type
Line 50: Line 41:
 
I simply match on the record and list:
 
I simply match on the record and list:
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
<code caption="Code snippet 3">
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code snippet 3</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
  
 
mxlookup(Domain) ->
 
mxlookup(Domain) ->
Line 61: Line 49:
 
   end.
 
   end.
  
</pre></td></tr>
+
</code>
</table>
+
  
 
Each entry in this list is a record of #dns_rr (DNS Resource Record).
 
Each entry in this list is a record of #dns_rr (DNS Resource Record).
Line 71: Line 58:
 
for this HowTo I'll simply return the first host found:
 
for this HowTo I'll simply return the first host found:
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
<code caption="Code snippet 4">
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code snippet 4</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
  
 
findmx( [#dns_rr{ type=?S_A, data = Addr } | T] ) -> Addr;
 
findmx( [#dns_rr{ type=?S_A, data = Addr } | T] ) -> Addr;
Line 80: Line 64:
 
findmx( _Other ) -> none.
 
findmx( _Other ) -> none.
  
</pre></td></tr>
+
</code>
</table>
+
  
 
==Conclusion==
 
==Conclusion==
Line 91: Line 74:
 
==Listing==
 
==Listing==
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
<code caption="Code listing 1 (mxlookup.erl)">
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code listing 1 (mxlookup.erl)</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
  
 
-module(mxlookup).
 
-module(mxlookup).
Line 119: Line 99:
 
   end.
 
   end.
  
</pre></td></tr>
+
</code>
</table>
+
  
<table class="ntable" width="100%" cellspacing="0" cellpadding="0" border="0">
+
<code caption="Code listing 2 (testmx.erl)">
<tr><td class="infohead" bgcolor="#7a5ada"><p class="caption">
+
            Code listing 2 (testmx.erl)</p></td></tr>
+
<tr><td bgcolor="#ddddff"><pre>
+
  
 
-module(testmx).
 
-module(testmx).
Line 137: Line 113:
 
   io:format("Atomic Tiger: ~p~nTrapexit: ~p~n",[Addr1,Addr2]).
 
   io:format("Atomic Tiger: ~p~nTrapexit: ~p~n",[Addr1,Addr2]).
  
</pre></td></tr>
+
</code>
</table>
+
  
 
[[Category:HowTo]]
 
[[Category:HowTo]]

Revision as of 11:40, 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:


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:


-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:


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:


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


-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.


-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]).