[fpc-pascal] RE: Get Local IP using glib (eth0,eth1)
Jeff Pohlmeyer
yetanothergeek at gmail.com
Fri Oct 6 05:01:06 CEST 2006
> > function to read the local IP eth0/eth1 using glibc library.
> program ip_addr;
> ...
> end.
Please ignore my last post -
This one is a little cleaner...
program ip_addr;
{$MODE OBJFPC}
uses libc;
function GetIPAddressOfInterface( if_name:ansistring):ansistring;
var
ifr : ifreq;
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;
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 Result := p;
end;
libc.__close(sock);
end;
end;
var
i:LongInt;
begin
if (ParamCount>0)
then for i:=1 to ParamCount
do WriteLn(GetIPAddressOfInterface(ParamStr(i)))
else WriteLn('Usage:', ParamStr(0), '<interface-name>')
end.
More information about the fpc-pascal
mailing list