/[lcore]/trunk/lcorelocalips.pas
ViewVC logotype

Diff of /trunk/lcorelocalips.pas

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 96 by beware, Wed Feb 23 06:46:34 2011 UTC revision 97 by beware, Thu Feb 24 06:24:10 2011 UTC
# Line 4  Line 4 
4    ----------------------------------------------------------------------------- }    ----------------------------------------------------------------------------- }
5    
6  {  {
7  unit to get IP addresses assigned to local interfaces.  unit to get various local system config
8    
9    
10    - get IP addresses assigned to local interfaces.
11  both IPv4 and IPv6, or one address family in isolation.  both IPv4 and IPv6, or one address family in isolation.
12  works on both windows and linux.  works on both windows and linux.
13    
# Line 17  Line 20 
20    an app that doesn't want link local IPs has to filter them out.    an app that doesn't want link local IPs has to filter them out.
21    windows XP returns only one, global scope, v6 IP, due to shortcomings.    windows XP returns only one, global scope, v6 IP, due to shortcomings.
22    
23    
24    
25    - get system DNS servers
26    
27    - get system hostname
28    
29  }  }
30    
31  unit lcorelocalips;  unit lcorelocalips;
# Line 33  Line 42 
42  function getv6localips:tbiniplist;  function getv6localips:tbiniplist;
43  {$endif}  {$endif}
44    
45    function getsystemdnsservers:tbiniplist;
46    function getsystemhostname:ansistring;
47    
48  implementation  implementation
49    
50  {$ifdef linux}  {$ifdef linux}
# Line 130  Line 142 
142  {$else}  {$else}
143    
144  uses  uses
145    sysutils,winsock,dnssync;    sysutils,windows,winsock,dnssync;
146    
147  {the following code's purpose is to determine what IP windows would come from, to reach an IP  {the following code's purpose is to determine what IP windows would come from, to reach an IP
148  it can be abused to find if there's any global v6 IPs on a local interface}  it can be abused to find if there's any global v6 IPs on a local interface}
# Line 222  Line 234 
234    
235    
236    
237    
238    
239    {$ifdef win32}
240      const
241        MAX_HOSTNAME_LEN = 132;
242        MAX_DOMAIN_NAME_LEN = 132;
243        MAX_SCOPE_ID_LEN = 260    ;
244        MAX_ADAPTER_NAME_LENGTH = 260;
245        MAX_ADAPTER_ADDRESS_LENGTH = 8;
246        MAX_ADAPTER_DESCRIPTION_LENGTH = 132;
247        ERROR_BUFFER_OVERFLOW = 111;
248        MIB_IF_TYPE_ETHERNET = 6;
249        MIB_IF_TYPE_TOKENRING = 9;
250        MIB_IF_TYPE_FDDI = 15;
251        MIB_IF_TYPE_PPP = 23;
252        MIB_IF_TYPE_LOOPBACK = 24;
253        MIB_IF_TYPE_SLIP = 28;
254    
255    
256      type
257        tip_addr_string=packed record
258          Next :pointer;
259          IpAddress : array[0..15] of ansichar;
260          ipmask    : array[0..15] of ansichar;
261          context   : dword;
262        end;
263        pip_addr_string=^tip_addr_string;
264        tFIXED_INFO=packed record
265           HostName         : array[0..MAX_HOSTNAME_LEN-1] of ansichar;
266           DomainName       : array[0..MAX_DOMAIN_NAME_LEN-1] of ansichar;
267           currentdnsserver : pip_addr_string;
268           dnsserverlist    : tip_addr_string;
269           nodetype         : longint;
270           ScopeId          : array[0..MAX_SCOPE_ID_LEN + 4] of ansichar;
271           enablerouting    : longbool;
272           enableproxy      : longbool;
273           enabledns        : longbool;
274        end;
275        pFIXED_INFO=^tFIXED_INFO;
276    
277      var
278        iphlpapi : thandle;
279        getnetworkparams : function(pFixedInfo : PFIXED_INFO;OutBufLen : plongint) : longint;stdcall;
280    
281    function callGetNetworkParams:pFIXED_INFO;
282    var
283        fixed_info : pfixed_info;
284        fixed_info_len : longint;
285    begin
286      result := nil;
287      if iphlpapi=0 then iphlpapi := loadlibrary('iphlpapi.dll');
288        if not assigned(getnetworkparams) then @getnetworkparams := getprocaddress(iphlpapi,'GetNetworkParams');
289        if not assigned(getnetworkparams) then exit;
290        fixed_info_len := 0;
291        if GetNetworkParams(nil,@fixed_info_len)<>ERROR_BUFFER_OVERFLOW then exit;
292        //fixed_info_len :=sizeof(tfixed_info);
293        getmem(fixed_info,fixed_info_len);
294        if GetNetworkParams(fixed_info,@fixed_info_len)<>0 then begin
295          freemem(fixed_info);
296          exit;
297        end;
298        result := fixed_info;
299    end;
300    
301    {$endif}
302    
303    function getsystemdnsservers:tbiniplist;
304    var
305      {$ifdef win32}
306        fixed_info : pfixed_info;
307        currentdnsserver : pip_addr_string;
308      {$else}
309        t:textfile;
310        s:ansistring;
311        a:integer;
312      {$endif}
313      ip:tbinip;
314    begin
315      //result := '';
316    
317      result := biniplist_new;
318    
319      {$ifdef win32}
320        fixed_info := callgetnetworkparams;
321        if fixed_info = nil then exit;
322    
323        currentdnsserver := @(fixed_info.dnsserverlist);
324        while assigned(currentdnsserver) do begin
325          ip := ipstrtobinf(currentdnsserver.IpAddress);
326          if (ip.family <> 0) then biniplist_add(result,ip);
327          currentdnsserver := currentdnsserver.next;
328        end;
329        freemem(fixed_info);
330      {$else}
331        filemode := 0;
332        assignfile(t,'/etc/resolv.conf');
333        {$i-}reset(t);{$i+}
334        if ioresult <> 0 then exit;
335    
336        while not eof(t) do begin
337          readln(t,s);
338          if not (copy(s,1,10) = 'nameserver') then continue;
339          s := copy(s,11,500);
340          while s <> '' do begin
341            if (s[1] = #32) or (s[1] = #9) then s := copy(s,2,500) else break;
342          end;
343          a := pos(' ',s);
344          if a <> 0 then s := copy(s,1,a-1);
345          a := pos(#9,s);
346          if a <> 0 then s := copy(s,1,a-1);
347    
348          ip := ipstrtobinf(s);
349          if (ip.family <> 0) then biniplist_add(result,ip);
350        end;
351        closefile(t);
352      {$endif}
353    end;
354    
355    
356    function getsystemhostname:ansistring;
357    var
358      {$ifdef win32}
359        fixed_info : pfixed_info;
360      {$else}
361        t:textfile;
362      {$endif}
363    begin
364      result := '';
365      {$ifdef win32}
366        fixed_info := callgetnetworkparams;
367        if fixed_info = nil then exit;
368    
369        result := fixed_info.hostname;
370        if fixed_info.domainname <> '' then result := result + '.'+fixed_info.domainname;
371    
372        freemem(fixed_info);
373      {$else}
374        filemode := 0;
375        assignfile(t,'/etc/hostname');
376        {$i-}reset(t);{$i+}
377        if ioresult <> 0 then exit;
378        readln(t,result);
379        closefile(t);
380      {$endif}
381    end;
382    
383  end.  end.

Legend:
Removed from v.96  
changed lines
  Added in v.97

No admin address has been configured">No admin address has been configured
ViewVC Help
Powered by ViewVC 1.1.22