unit unix_iph;
interface
type
ip_address = array[0..3] of string;
pip_address = ^ip_address;
procedure normalize_ip(ip: pip_address);
procedure set_ip_data(address: ip_address; netmask: ip_address; gateway: ip_address);
procedure get_ip_data(out address: ip_address; out netmask: ip_address; out gateway: ip_address);
implementation
uses SysUtils, StrUtils;
procedure normalize_ip(ip: pip_address);
var i: integer;
begin
if (not trystrtoint(ip^[0], i)) then ip^[0] := '0';
if (not trystrtoint(ip^[1], i)) then ip^[1] := '0';
if (not trystrtoint(ip^[2], i)) then ip^[2] := '0';
if (not trystrtoint(ip^[3], i)) then ip^[3] := '0';
end;
function set_ip(ip: ip_address): string;
begin
result := ip[0] + '.' + ip[1] + '.' + ip[2] + '.' + ip[3];
end;
procedure set_ip_data(address: ip_address; netmask: ip_address; gateway: ip_address);
var
tf: textfile;
begin
assignfile(tf, '/etc/network/interfaces');
rewrite(tf);
writeln(tf, 'iface eth0 inet static');
write(tf, ' address ');
writeln(tf, set_ip(address));
write(tf, ' netmask ');
writeln(tf, set_ip(netmask));
write(tf, ' gateway ');
writeln(tf, set_ip(gateway));
closefile(tf);
SysUtils.ExecuteProcess('/etc/init.d/networking', 'restart', []);
end;
function get_ip(s: string): ip_address;
var
i, j: integer;
begin
result[0] := '';
result[1] := '';
result[2] := '';
result[3] := '';
j := 0;
for i := 1 to length(s) do
begin
if (s[i] = '.') then
begin
j := j + 1;
if (j > 3) then
begin
break;
end;
end
else
begin
result[j] := result[j] + s[i];
end;
end;
end;
procedure get_ip_data(out address: ip_address; out netmask: ip_address; out gateway: ip_address);
var
list: array[0..3] of string;
tf: textfile;
line: string;
begin
if fileexists('/etc/network/interfaces') then
begin
assignfile(tf, '/etc/network/interfaces');
reset(tf);
while not eof(tf) do
begin
readln(tf, line);
line := trim(line);
list := get_ip(rightstr(line, length(line) - 8));
case leftstr(line, 8) of
'address ':
begin
address := list;
end;
'netmask ':
begin
netmask := list;
end;
'gateway ':
begin
gateway := list;
end;
end;
end;
closefile(tf);
end;
end;
end. |