;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       GetNetInfo() 
;; 
;;ACTION         Returns data by matching an IP address to a subnet list 
;; 
;;AUTHOR         Glenn Barnas  
;; 
;;VERSION        2.2  - 2008/01/20 
;; 
;;HISTORY        1.0  - 2003/01/04 - Initial Release 
;;               1.2  - 2004/02/01 - Check alternate location for config file 
;;               2.0  - 2006/11/02 - Combine GetWANSite and GetNetInfo functionality 
;;               2.1  - 2007/06/12 - Update for NoMacrosInStrings 
;;               2.2  - 2008/01/20 - Modified to permit (and ignore) comments & blank 
;;                                   lines in subnets.inf; Modified array build process 
;;                                   Modified to use InSubnet() udf 
;; 
;;SYNTAX         GetNetInfo(IP_Addr [, Field] [, File]) 
;; 
;;PARAMETERS     IP_Addr - Required - String 
;;               - An IP address of the local server 
;; 
;;               Field - Optional - Integer 
;;               - Field in subnets.inf to return. The default is field 2 - Site Name 
;; 
;;               File - Optional - String 
;;               - Defines the location of the subnets.inf file. The default is  
;;              %S_CONFIG%\subnets.inf 
;; 
;;REMARKS        Allows an application to determine physical site-specific  
;;               information based on the target system's IP address. 
;; 
;;RETURNS        The data in the specified or default field of the data file 
;; 
;;DEPENDENCIES   InSubnet() UDF & Subnets.inf file 
;;               In our application, the format of this file is: 
;;               192.168.0.0/23 <Tab> Local_SD_Server <Tab> SiteName <Tab> Description 
;;               ALL RECORDS MUST CONTAIN THE SAME NUMBER OF FIELDS! 
;; 
;;               The first field must be the network address & subnet - all 
;;               other fields are application dependent, and any number of fields  
;;               can be supported. 
;; 
;;               The file is found by default in the %S_CONFIG% folder. If it is 
;;               not there, the @SCRIPTDIR folder is checked. If the optional File 
;;               value is specified, the named file is read and no other locations 
;;               are checked. 
;;                
;; 
;;TESTED WITH    NT4, W2K, WXP, W2K3, Vista, X64 
;; 
;;EXAMPLES        
;; 
; 
Function GetNetInfo($_IP_Addr, OPTIONAL $_Field, OPTIONAL $_File)
 
  Dim $_, $_Idx, $_FH, $_Line, $_CFile
  Dim $_aZones[0], $_aData[0], $_aTmp, $_aFound
 
 
  ; Insure that the Field var is a non-zero numeric value - default is 2 
  $_Field = IIf(Val($_Field) = 0, 2, Val($_Field))
 
  ; If $_File was specified, verify the file exists. Exit with File Not Found if it doesnt. 
  ; otherwise, check for the standard file in default locations 
  If $_File
    If Exist($_File)
      $_CFile = $_File
    Else
      Exit 2	; file not found! 
    EndIf
  Else
    ; Subnet definition file should be in a std location, but could be in SCRIPTDIR 
    ; for other installations - accept SCRIPTDIR only if not in std location 
    Select
      Case Exist('%S_CONFIG%\Subnets.inf')			; standard local folder 
        $_CFile = '%S_CONFIG%\Subnets.inf'
  
      Case Exist(@SCRIPTDIR + '\Subnets.inf')			; script source folder 
        $_CFile = @SCRIPTDIR + '\Subnets.inf'
 
      Case 1
        ; FILE NOT FOUND 
        Exit 2				; exit if file was not found 
 
    EndSelect
 
  EndIf	; File 
 
  ; the subnet definition file exists - load it into a pair of arrays 
  ; trim leading spaces/blanks, ignore comments (leading "#" or ";") and blank lines 
  $_Idx = -1
  $_FH = FreeFileHandle()
  If Open($_FH, $_CFile, 2) = 0
    $_Line = LTrim(ReadLine($_FH))
    While @ERROR = 0
      ; only process if line is not blank, and not a comment 
      If $_Line <> '' And Not InStr('#;', Left($_Line, 1))
        $_aTmp = Split($_Line, Chr(9))    ; split the line into subnet and data 
        ; Verify that the Field value is valid 
        If $_Field > UBound($_aTmp)
          Exit 87                     ; invalid argument was specified 
        EndIF
 
        $_Idx = $_Idx + 1
        ReDim Preserve $_aZones[$_Idx]    ; reduce the array sizes 
        ReDim Preserve $_aData[$_Idx]
        $_aZones[$_Idx] = $_aTmp[0]       ; build the subnet array 
        $_aData[$_Idx] = $_aTmp[$_Field]  ; build the data array 
      EndIf
      $_Line = LTrim(ReadLine($_FH))    ; read the next line 
    Loop
  EndIf
  $_ = Close($_FH)                      ; close the file 
 
  For $_ = 0 to UBound($_aZones)
    If InSubnet($_IP_Addr, $_aZones[$_])
      $GetNetInfo = $_aData[$_]
      Exit 0
    EndIf
  Next
 
  Exit 1
 
EndFunction
 
; GetWanSite was deprecated - this redirects to GetNetInfo, returning the desired field 
Function GetWANSite($_IP_Addr, OPTIONAL $_Field)
  $GetWanSite = GetNetInfo($_IP_Addr, $_Field)
EndFunction
 
; GetSDSvr was deprecated - this redirects to GetNetInfo, returning field 1 
Function GetSDSvr($_IP_Addr)
  $GetSDSvr = GetNetInfo($_IP_Addr, 1)
EndFunction