[fpc-pascal] RE: Get Local IP using glib (eth0,eth1)
TOUZEAU DAVID
david.touzeau at fr.kaspersky.com
Fri Oct 6 11:24:40 CEST 2006
Many Thanks Jeff and Micha.
This function works perfectly without need to regex ifconfig... And i
use glibc
I develop only on linux system.
Great respects...
Just another question... (this because i came from Windows OS) how can i
list wich interface loaded without need to parse /etc/networks/...?
(wlan0,wlan1,eth0,eth1...)
Best regards...
Jeff Pohlmeyer a écrit :
>> I'm trying to write a function to read the local IP eth0/eth1
>> using glibc library...
>
>
> program ip_addr;
> {$MODE OBJFPC}
>
> uses libc;
> const
> IP_NAMESIZE = 16;
> type
> ipstr = array[0..IP_NAMESIZE-1] of char;
>
>
> function GetIPAddressOfInterface( if_name:ansistring):ansistring;
> 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;
>
>
>
>
> begin
> if (ParamCount=1)
> then WriteLn(GetIPAddressOfInterface(ParamStr(1)))
> else WriteLn('Usage:', ParamStr(0), '<interface-name>')
> end.
>
>
>
> - Jeff
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
--
David Touzeau -------------------------- Linux Ubuntu Dapper 6.0.6
FreePascal-Lazarus,perl,delphi,php icq:160018849
More information about the fpc-pascal
mailing list