CheckIP function: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (fix spam)
m (Protected "CheckIP function": Excessive vandalism ([edit=autoconfirmed] (expires 19:15, 3 January 2011 (UTC)) [move=autoconfirmed] (expires 19:15, 3 January 2011 (UTC))))
 
(4 intermediate revisions by 4 users not shown)
(No difference)

Latest revision as of 19:15, 3 July 2010

Description

Requires: VersionCheck function.

Checks an IP to see if it is:

  • an Internet IP address (IP address used when connected to the Internet).
  • a Network IP address (IP address used by routers to identify a computer connected to LAN).
  • an automatic private IP address (IP address used when there is no DHCP server available and you connect to the Internet).
  • a LoopBack IP (IP used when not connected to the Internet to identify the computer itself).

How to use

Syntax

Push "89.165.74.146"
Call CheckIP
Pop "ResultVar"
Pop "ResultIPAddress" ;= value of "IPAddress"

Parameters

ResultVar
Variable where the type of the IP address is returned. List of IP addresses types:
  • 1 - LoopBack IP.
  • 2 - Automatic Private IP Address.
  • 3 - Network IP.
  • 4 - Internet IP.
ResultIPAddress
Variable where the "IPAddress" parameter value is returned.
IPAddress
IP address to be checked for its type.

Example

Push "127.0.0.1"
Call CheckIP
Pop $0
;$0 = "1"

Function code

; Function CheckIP
; input: IP-address on stack
; output: additional entry on stack
;         1 - LoopBack IP (localhost, indicates no connection to a LAN or to the internet).
;         2 - Automatic Private IP Address (no DHCP server).
;         3 - Network IP.
;         4 - Internet IP.
; Eg:
; Push '192.168.0.100'
; Call CheckIP
; Pop $0 ; Contains '3'
; Pop $1 ; Contains '192.168.0.100'
 
Function CheckIP
  Exch $0
  Push $1
 
  ; Check 127.x.x.x
  Push '127.0.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range1     ; IP cannot be in range of LoopBack addresses
  Push '127.255.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LoopBack      ; We found a LoopBack IP
 
  ; Check 10.x.x.x
  Range1:
  Push '10.0.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range2     ; IP cannot be in range 1
  Push '10.255.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LanIp         ; We found a LanIp
 
  ; Check 172.16.x.x to 172.31.x.x
  Range2:
  Push '172.16.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range3     ; IP cannot be in range 2
  Push '172.31.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LanIp         ; We found a LanIp
 
  ; Check 192.168.x.x
  Range3:
  Push '192.168.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range4     ; IP cannot be in range 3
  Push '192.168.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LanIp         ; We found a LanIp
 
  ; Check 169.254.x.x
  Range4:
  Push '169.254.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' InternetIp ; It should be an internet IP
  Push '169.254.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 APA           ; We found an Automatic Private IP Address
 
  Goto InternetIp           ; Remaining addresses are internet IPs
 
  LoopBack:
  StrCpy $1 1
  Goto Exit
 
  APA:
  StrCpy $1 2
  Goto Exit
 
  LanIp:
  StrCpy $1 3
  Goto Exit
 
  InternetIp:
  StrCpy $1 4
 
  Exit:
  Exch $1
  Exch 1
  Exch $0
  Exch 1
FunctionEnd