[erlang-questions] : Finding IP addresses in a PC

doug mansell doug.mansell@REDACTED
Wed Oct 4 14:25:03 CEST 2006


yes...  fmt_addr in inet_drv.c is all back-to-front!

try this one instead:


/* format address in dot notation */
static char* fmt_addr(unsigned long x, char* ptr)
{
	int i;
	for (i = 0; i < 4; i++) {
		int nb[3];
		int y = x & 0xff;
		x >>= 8;
		nb[0] = y % 10; y /= 10;
		nb[1] = y % 10; y /= 10;
		nb[2] = y % 10; y /= 10;
		switch((nb[2]>0 ? 3 : (nb[1]>0 ? 2 : 1))) {
			case 3:  *ptr++ = nb[2] + '0';
			case 2:  *ptr++ = nb[1] + '0';
			case 1:  *ptr++ = nb[0] + '0';
		}
		*ptr++ = '.';
	}
	*(ptr-1) = '\0';
	return ptr;
}


(sorry, no patch... i'm a windows developer!)  :)



On 10/4/06, Raimo Niskanen <raimo+erlang-questions@REDACTED> wrote:
> This is absolutely a bug, that we have not had time to
> investigate. We can reproduce it easily. All windows
> machines seem to exhibit this behaviour.
>
> We will see when we can find the time to correct it...
>
>
>
> On Wed, Oct 04, 2006 at 09:50:45AM +0530, Surindar Sivanesan wrote:
> > The following code might return the correct IP by calling ip:list() from erl
> > shell
> >
> >
> >
> > -module(ip).
> > -export([list/0]).
> >
> > list()->
> > IpAddress=inet:getiflist(),
> > case IpAddress of
> >  {ok,IpList}->
> >    format_ip(IpList,[]);
> >  _->
> >   io:fwrite("Error ~p in getting ip list\n",[IpAddress])
> > end.
> >
> > format_ip(Iplist,FormatedList)->
> > case Iplist of
> >  []->
> >   FormatedList;
> >  _->
> >   Ip=hd(Iplist),
> >   ReversedIp=lists:reverse(string:tokens(Ip,".")),
> >   FormatedIP=check_ip(ReversedIp,""),
> >   format_ip(tl(Iplist),FormatedList++[FormatedIP])
> > end.
> >
> > check_ip(IpList,Return)->
> > case IpList of
> >  []->
> >   Return;
> >  _->
> >   IpPart=lists:reverse(hd(IpList)),
> >   case length(IpPart) of
> >    3->
> >     NewPart=integer_to_list(list_to_integer(IpPart)),
> >     case Return of
> >      ""->
> >       check_ip(tl(IpList),Return++NewPart);
> >      _->
> >       check_ip(tl(IpList),Return++"."++NewPart)
> >     end;
> >    2->
> >     NewPart=integer_to_list(list_to_integer(IpPart++"0")),
> >     case Return of
> >      ""->
> >       check_ip(tl(IpList),Return++NewPart);
> >      _->
> >       check_ip(tl(IpList),Return++"."++NewPart)
> >     end;
> >    1->
> >     NewPart=integer_to_list(list_to_integer(IpPart++"00")),
> >     case Return of
> >      ""->
> >       check_ip(tl(IpList),Return++NewPart);
> >      _->
> >       check_ip(tl(IpList),Return++"."++NewPart)
> >     end;
> >    _->
> >     io:fwrite("Illegal IP length\n")
> >   end
> > end.
> >
> >
> > On 10/3/06, Shelton Tang <shelton.ms@REDACTED> wrote:
> > >
> > >I have one NIC with the address {192,168,0,150} on my Vista machine.
> > >If I call inet:getiflist(),  it returns
> > >["51.0.861.291","100.0.0.721"].
> > >
> > >Anybody know how to convert it to a valid format?
> > >_______________________________________________
> > >erlang-questions mailing list
> > >erlang-questions@REDACTED
> > >http://www.erlang.org/mailman/listinfo/erlang-questions
> > >
> >
> >
> >
> > --
> > with regards,
> > S.Surindar
>
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
>
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list