connecting using aliased hostname

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

connecting using aliased hostname

Ulf Wiger (AL/EAB)

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), it doesn't work.
It seems to go wrong in recv_challenge, since
the other node calls himself 'n'.

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


Reply | Threaded
Open this post in threaded view
|

connecting using aliased hostname

Raimo Niskanen-3
It should work without any change to the distribution if you force
the nodename to be an aliased name; in your example start with
-sname n



ulf.wiger (Ulf Wiger AL/EAB) writes:

> 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), it doesn't work.
> It seems to go wrong in recv_challenge, since
> the other node calls himself 'n'.
>
> 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 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.

--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


Reply | Threaded
Open this post in threaded view
|

connecting using aliased hostname

Ulf Wiger-5
Den 2005-12-01 17:44:01 skrev Raimo Niskanen <raimo>:

> It should work without any change to the distribution if you force
> the nodename to be an aliased name; in your example start with
> -sname n

Yes, but in this case, I didn't want to change
the target node - just connect to it using
an alias that I had introduced on my end.

I believe that in the specific case that we
were looking at, the official hostnames are
going to be the same for several hosts (target
systems under test), and we have only the IP
addresses to tell them apart. That, and possibly
aliases that we introduce in our inetrc file.
We would of course connect to only one of the
systems at a time.

/Uffe
--
Ulf Wiger