Erlang Central

Difference between revisions of "Lookup MX Records In DNS"

From ErlangCentral Wiki

m
Line 116: Line 116:
  
 
[[Category:HowTo]]
 
[[Category:HowTo]]
 +
 +
 +
 +
[http://www.magical-casino.com/casino_bonus.html Casino Bonus ]
 +
[http://www.magical-casino.com/online_games.html Online casino games.]
 +
[http://www.casino-theory.com/craps-rules/internet-craps-gambling.html internet craps gambling]
 +
[http://www.casinos-go.com/online-casino-tips/index.html online casino tips]
 +
[http://www.online-casino-wiki.com/index.php/online_casino online casino]
 +
[http://www.casino-theory.com/craps-rules/internet-craps-gambling.html internet craps gambling]
 +
[http://www.slots-wiki.com/index.php/slots_tips slots tips]
 +
[http://www.gambling-online-theory.com/casinos-portal/internet-casinos-sites.html internet casinos sites]
 +
[http://www.casino-games-wiki.com/index.php/online_casino_games online casino games]
 +
[http://www.casino-theory.com/online-casino-bonus/index.html online casino bonus]

Revision as of 07:42, 4 December 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]).


Casino Bonus Online casino games. internet craps gambling online casino tips online casino internet craps gambling slots tips internet casinos sites online casino games online casino bonus