inet_res.erl

Klacke klacke@REDACTED
Mon Nov 25 15:44:50 CET 2002


Folks,

I was looking at inet_res.erl because I wanted to have
gethostbyname/4 with the last arg being the searchlist
for the resolver.

While doing that, I found a couple of bugs in the code as well.
It's almost amusing, but the fact is that nobody has ever run
dns gethostbyname/3 (with a timeout) .. The code is +5 years old :-)

Here's a patch with the bugs fixed + gethostbyname/4.


/klacke



-- 
Claes Wikstrom                        -- Caps lock is nowhere and
Alteon WebSystems                     -- everything is under control          
http://www.bluetail.com/~klacke      
cellphone: +46 70 2097763
-------------- next part --------------
*** inet_res.erl.orig	2002-11-25 14:38:13.000000000 +0100
--- inet_res.erl	2002-11-25 14:28:04.000000000 +0100
***************
*** 26,37 ****
  %% (the example is for tcsh)
  
  
! -export([gethostbyname/1, gethostbyname/2, gethostbyname/3,
  	 gethostbyname_tm/3]).
  -export([gethostbyaddr/1, gethostbyaddr/2,
  	 gethostbyaddr_tm/2]).
! -export([getbyname/2, getbyname/3,
! 	 getbyname_tm/3]).
  
  -export([nslookup/3, nslookup/4]).
  -export([nnslookup/4, nnslookup/5]).
--- 26,38 ----
  %% (the example is for tcsh)
  
  
! -export([gethostbyname/1, gethostbyname/2, 
! 	 gethostbyname/3,gethostbyname/4,
  	 gethostbyname_tm/3]).
  -export([gethostbyaddr/1, gethostbyaddr/2,
  	 gethostbyaddr_tm/2]).
! -export([getbyname/2, getbyname/3,getbyname/4,
! 	 getbyname_tm/3, getbyname_tm/4]).
  
  -export([nslookup/3, nslookup/4]).
  -export([nnslookup/4, nnslookup/5]).
***************
*** 43,48 ****
--- 44,50 ----
  -include("inet_dns.hrl").
  -include("inet_int.hrl").
  
+ 
  -ifdef(DEBUG).
  -define(dbg(Fmt, Args), io:format(Fmt, Args)).
  -else.
***************
*** 117,123 ****
  gethostbyaddr(IP,Timeout) ->
      Timer = inet:start_timer(Timeout),
      Res = gethostbyaddr_tm(IP,Timer),
!     inet:stop_timer(Timeout),
      Res.    
  
  gethostbyaddr_tm({A,B,C,D}, Timer) when ?ip(A,B,C,D) ->
--- 119,125 ----
  gethostbyaddr(IP,Timeout) ->
      Timer = inet:start_timer(Timeout),
      Res = gethostbyaddr_tm(IP,Timer),
!     inet:stop_timer(Timer),
      Res.    
  
  gethostbyaddr_tm({A,B,C,D}, Timer) when ?ip(A,B,C,D) ->
***************
*** 201,219 ****
  gethostbyname(Name,Family) ->
      gethostbyname_tm(Name,Family,false).
  
  gethostbyname(Name,Family,Timeout) ->
      Timer = inet:start_timer(Timeout),    
      Res = gethostbyname_tm(Name,Family,Timer),
!     inet:stop_timer(Timeout),
      Res.
!     
! gethostbyname_tm(Name,inet,Timer) ->
!     getbyname_tm(Name,?S_A,Timer);
! gethostbyname_tm(Name,inet6,Timer) ->
!     case getbyname_tm(Name,?S_AAAA,Timer) of
  	{ok,HEnt} -> {ok,HEnt};
  	{error,nxdomain} ->
! 	    case getbyname_tm(Name, ?S_A,Timer) of
  		{ok, HEnt} ->
  		    %% rewrite to a ipv4 only ipv6 address
  		    {ok,
--- 203,233 ----
  gethostbyname(Name,Family) ->
      gethostbyname_tm(Name,Family,false).
  
+ 
  gethostbyname(Name,Family,Timeout) ->
      Timer = inet:start_timer(Timeout),    
      Res = gethostbyname_tm(Name,Family,Timer),
!     inet:stop_timer(Timer),
      Res.
! gethostbyname(Name,Family,Timeout, SearchList) ->
!     Timer = inet:start_timer(Timeout),    
!     Res = gethostbyname_tm(Name,Family,Timer, SearchList),
!     inet:stop_timer(Timer),
!     Res.
! 
! 
! 
! 
! gethostbyname_tm(Name, Family, Timer) ->
!     gethostbyname_tm(Name, Family, Timer, get_searchlist()).
! 
! gethostbyname_tm(Name,inet,Timer, SearchList) ->
!     getbyname_tm(Name,?S_A,Timer, SearchList);
! gethostbyname_tm(Name,inet6,Timer,SearchList) ->
!     case getbyname_tm(Name,?S_AAAA,Timer, SearchList) of
  	{ok,HEnt} -> {ok,HEnt};
  	{error,nxdomain} ->
! 	    case getbyname_tm(Name, ?S_A,Timer, SearchList) of
  		{ok, HEnt} ->
  		    %% rewrite to a ipv4 only ipv6 address
  		    {ok,
***************
*** 232,238 ****
  	Error ->
  	    Error
      end;
! gethostbyname_tm(Name,Family,Timer) ->
      {error, einval}.
  	    
  %% --------------------------------------------------------------------------
--- 246,252 ----
  	Error ->
  	    Error
      end;
! gethostbyname_tm(Name,Family,Timer, SearchList) ->
      {error, einval}.
  	    
  %% --------------------------------------------------------------------------
***************
*** 246,259 ****
  
  getbyname(Name, Type) -> 
      getbyname_tm(Name,Type,false).
- 
  getbyname(Name, Type, Timeout) ->
      Timer = inet:start_timer(Timeout),
!     Res = getbyname_tm(Name, Type, Timer),
!     inet:stop_timer(Timeout),
      Res.
  
! getbyname_tm(Name, Type, Timer) when list(Name) ->
      case type_p(Type) of
  	true ->
  	    case inet_parse:visible_string(Name) of
--- 260,279 ----
  
  getbyname(Name, Type) -> 
      getbyname_tm(Name,Type,false).
  getbyname(Name, Type, Timeout) ->
+     getbyname(Name, Type, Timeout, get_searchlist()).
+ 
+ getbyname(Name, Type, Timeout, Slist) ->
      Timer = inet:start_timer(Timeout),
!     Res = getbyname_tm(Name, Type, Timer, Slist),
!     inet:stop_timer(Timer),
      Res.
  
! 
! getbyname_tm(Name, Type, Timer)  ->
!     getbyname_tm(Name, Type, Timer, get_searchlist()).
! 
! getbyname_tm(Name, Type, Timer, SearchList) when list(Name) ->
      case type_p(Type) of
  	true ->
  	    case inet_parse:visible_string(Name) of
***************
*** 261,275 ****
  		true ->
  		    case inet_db:getbyname(Name,Type) of
  			{ok, HEnt} -> {ok, HEnt};
! 			_ -> res_getbyname(Name,Type,Timer)
  		    end
  	    end;
  	false ->
  	    {error, formerr}
      end;
! getbyname_tm(Name,Type,Timer) when atom(Name) ->
!     getbyname_tm(atom_to_list(Name), Type,Timer);
! getbyname_tm(_, _, _) -> {error, formerr}.
  
  type_p(Type) ->
      lists:member(Type, [?S_A, ?S_AAAA, ?S_MX, ?S_NS,
--- 281,295 ----
  		true ->
  		    case inet_db:getbyname(Name,Type) of
  			{ok, HEnt} -> {ok, HEnt};
! 			_ -> res_getbyname(Name,Type,Timer, SearchList)
  		    end
  	    end;
  	false ->
  	    {error, formerr}
      end;
! getbyname_tm(Name,Type,Timer, SearchList) when atom(Name) ->
!     getbyname_tm(atom_to_list(Name), Type,Timer, SearchList);
! getbyname_tm(_, _, _,_) -> {error, formerr}.
  
  type_p(Type) ->
      lists:member(Type, [?S_A, ?S_AAAA, ?S_MX, ?S_NS,
***************
*** 278,293 ****
  		        ?S_WKS, ?S_HINFO, ?S_TXT, ?S_SRV,
  		        ?S_UINFO, ?S_UID, ?S_GID]).
  	    
! res_getbyname(Name,Type,Timer) ->
      case dots(Name) of
  	{0, Dot} ->
  	    res_getby_search(Name, Dot,
! 			     get_searchlist(),nxdomain,Type,Timer);
  	{_,Dot} ->
  	    case res_getby_query_alt(Name,Type,Timer) of
  		{error,Reason} ->
  		    res_getby_search(Name, Dot,
! 				     get_searchlist(),nxdomain,Type,Timer);
  		Other -> Other
  	    end
      end.
--- 298,313 ----
  		        ?S_WKS, ?S_HINFO, ?S_TXT, ?S_SRV,
  		        ?S_UINFO, ?S_UID, ?S_GID]).
  	    
! res_getbyname(Name,Type,Timer, SearchList) ->
      case dots(Name) of
  	{0, Dot} ->
  	    res_getby_search(Name, Dot,
! 			     SearchList,nxdomain,Type,Timer);
  	{_,Dot} ->
  	    case res_getby_query_alt(Name,Type,Timer) of
  		{error,Reason} ->
  		    res_getby_search(Name, Dot,
! 				     SearchList,nxdomain,Type,Timer);
  		Other -> Other
  	    end
      end.


More information about the erlang-questions mailing list