I also found a function online that I was able tou use. This one is not a s clean as the one on the synapse site, but it tells you which interface have the ip assigned:
procedure GetIPs(IPList: TStrings);
procedure GetInterfaces(inList: TStrings);
var
F : text;
s:Ansistring;
P: integer;
begin
AssignFile(f,'/proc/net/dev');
reset(f);
while not eof(f) do
begin
ReadLn(f,s);
p := pos(':',s);
if p > 0 then
begin
inList.Add(trim(copy(s,1,p-1)));
end;
end;
CloseFile(f);
end; {GetInterfaces}
function GetIPAddressOfInterface( if_name:ansistring):ansistring;
const
IP_NAMESIZE = 16;
type
ipstr = array[0..IP_NAMESIZE-1] of char;
var
ifr : ifreq;
tmp:ipstr;
sock : longint;
p:pChar;
begin
Result:='0.0.0.0';
strncpy( ifr.ifr_ifrn.ifrn_name, pChar(if_name), IF_NAMESIZE-1 );
ifr.ifr_ifru.ifru_addr.sa_family := AF_INET;
FillChar(tmp[0], IP_NAMESIZE, #0);
sock := socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if ( sock >= 0 ) then begin
if ( ioctl( sock, SIOCGIFADDR, @ifr ) >= 0 ) then begin
p:=inet_ntoa( ifr.ifr_ifru.ifru_addr.sin_addr );
if ( p <> nil ) then strncpy(tmp, p, IP_NAMESIZE-1);
if ( tmp[0] <> #0 ) then Result := tmp;
end;
libc.__close(sock);
end;
end;{GetIPAddressOfInterface}
{Main function begins}
var
I : Integer;
begin
GetInterfaces(ipList);
for i := 0 to pred(ipList.Count) do
begin
ipList[i] := ipList[i] +':'+GetIPAddressOfInterface(ipList[i]);
end;
end;
On 11/9/06, Lukas Gebauer <[EMAIL PROTECTED]> wrote:
> I need to get a list of the local ip's ( including loopbacks etc) in my
> program. In windows I call functions in IPHLPAPI.DLL to accomplish this.
>
> I could call ifconfig and grap the potput a filter that down, but that to
> me is an ugly aproach. There must be a native programming way to do this
> right?
Yes, it exists, you can get by special ioctl query. Before a two weeks I
have exactly same problem as you have now, so I have solution for you.
;-)
See http://synapse.ararat.cz/files/contrib/ipget.zip
Here you can found freepascal unit with one function what returning list
of all actual local IP addresses in comma-delimited string.
It working fine on i386 Linux and it maybe working on all Linux/unix
platforms. For other platforms you maybe must use correct constatnt
values for SIOCGIFCONF. Maybe freepascal defining this constant too with
correct value for supported platforms. If yes, use freepascals constant
instead.
--
Lukas Gebauer.
E-mail: [EMAIL PROTECTED]
WEB: http://www.ararat.cz/synapse - Synapse Delphi and Kylix TCP/IP
Library
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
synalist-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/synalist-public
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ synalist-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/synalist-public
