connecting using aliased hostname

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Thu Dec 1 15:44:49 CET 2005


We played around a little with the -kernel inetrc
option, and tried to use a user-defined alias
to connect to a node on another machine

E.g. if the official hostname is, say "gandalf",
but we want to use the alias "saruman", we 
specify in our inetrc file

{lookup, [file,native]}.
{host, {10,10,10,10}, "gandalf", ["saruman"]}.

and then start an erlang node with 

erl -kernel inetrc '"./myinetrc"' -sname n -setcookie frodo

and try net:ping(n@REDACTED), it doesn't work.
It seems to go wrong in recv_challenge, since 
the other node calls himself 'n@REDACTED'.

The patch to dist_util.erl below makes us lookup
the hostent record and proceed if the hostname
given by the other node is either the official
name (most likely) or a known alias.

I admit that it is usually not terribly useful,
and perhaps sometimes quite confusing to allow
aliased hostnames, but on occasion, I think it 
can be quite nice to have.

Say, for example that we have a node that connects
to a system under test ("sut"). We can consistently 
use the nodename thenode@REDACTED in our test program.
Wonderfully convenient!

(Ok, crummy example. Mostly, I was curious to see if
it could be made to work, after having satisfied my 
initial curiosity in establishing that it didn't in
fact work.)

/Uffe
BTW, the patch isn't quite generic. It assumes 'inet'.


*** $OTP_ROOT/lib/kernel-2.10.10/src/dist_util.erl    Tue Aug 30
05:05:28 2005
--- dist_util.erl       Thu Dec  1 15:29:31 2005
***************
*** 33,38 ****
--- 33,40 ----
  
  -include("dist_util.hrl").
  -include("dist.hrl").
+ -include("inet.hrl").
+ 
  
  -define(to_port(FSend, Socket, Data),
        case FSend(Socket, Data) of
***************
*** 324,332 ****
      send_name(HSData),
      recv_status(HSData),
      {PreOtherFlags, NodeA, VersionA, ChallengeA} =
recv_challenge(HSData),
!     if Node =/= NodeA -> ?shutdown(no_node);
!        Version =/= VersionA -> ?shutdown(no_node);
!        true -> true
      end,
      {ThisFlags, OtherFlags} = adjust_flags(PreThisFlags,
                                           PreOtherFlags),
--- 326,340 ----
      send_name(HSData),
      recv_status(HSData),
      {PreOtherFlags, NodeA, VersionA, ChallengeA} =
recv_challenge(HSData),
!     GoodNode =
!       if Node =/= NodeA ->
!               is_alias(NodeA);
!          true -> true
!       end,
!     GoodVsn = (Version == VersionA),
!     if (GoodNode and GoodVsn) -> true;
!        true ->
!           ?shutdown(no_node)
      end,
      {ThisFlags, OtherFlags} = adjust_flags(PreThisFlags,
                                           PreOtherFlags),
***************
*** 341,346 ****
--- 349,375 ----
      reset_timer(NewHSData#hs_data.timer),
      recv_challenge_ack(NewHSData, MyChallenge, MyCookie),
      connection(NewHSData).
+ 
+ is_alias(Node) ->
+     case split_node(atom_to_list(Node), $@, []) of
+       [Name|Tail] ->
+           Host = lists:append(Tail),
+           case inet:gethostbyname(Host, inet) of
+               {ok, #hostent{h_name = OfficialName,
+                             h_aliases = Aliases}} ->
+                       lists:member(Host, [OfficialName|Aliases]);
+               _ ->
+                   false
+           end;
+       _ ->
+           false
+     end.
+ 
+ 
+ split_node([Chr|T], Chr, Ack) -> [lists:reverse(Ack)|split_node(T,
Chr, [])];
+ split_node([H|T], Chr, Ack)   -> split_node(T, Chr, [H|Ack]);
+ split_node([], _, Ack)        -> [lists:reverse(Ack)].
+ 
  
  %% --------------------------------------------------------------
  %% The connection has been established.



More information about the erlang-questions mailing list