Skip to content
⌘ NSIS Forum Archive

check if the ip is correct

3 posts

Dj Nyx#

check if the ip is correct

hi!
i have a little problem..
i have to check if a IP is valid... i have this Ip in a uservar [the ip is: 192.168.2.11] and i only want to check if the IP is correct.
now i use the iptest.dll
[i hope i do ]😛
is there any bug in this following code?
    !insertmacro ReadOutIp
    StrCpy $0 $Ip
    Loop:
        Push $0
        ;Call GetNextIp
        Call CheckIp
        MessageBox MB_OK "Type of Ip $2"
        Pop $2 ; Type of current IP-address
        Pop $1 ; Current IP-address
        Pop $0 ; Remaining addresses
        StrCmp $2 '1' '' NoLoopBackIp
        MessageBox MB_OK "LoopBack IP-address: $1"
        Goto Continue
    NoLoopBackIp:
        StrCmp $2 '2' '' NoAPA
        MessageBox MB_OK "Automatic Private IP-address: $1"
        Goto Continue
    NoAPA:
        StrCmp $2 '3' '' NoLanIp
        MessageBox MB_OK "Network IP-address: $1"
        Goto Continue
    NoLanIp:
        MessageBox MB_OK "Internet IP-address: $1"
        Goto Continue
    Continue:
        ;StrCmp $0 '' ExitLoop Loop
        Goto ExitLoop
    ExitLoop: 
evilO#
Hi 🙂

Well actually there is a little bug:

As apparently you want to test only one IP address, you do NOT use the function "GetNextIP". Therefore, you don't have "Remaining adresses", and there are *two* values on the stack, and not *three*. So basically there is one "Pop" in excess, and you're not testing the right one. Here is the corrected code, it should work :

Function testIp
  Push $0
  Push $1
  StrCpy $0 $Ip
  Loop:
      Push $0
      Call CheckIp
      Pop $1 ; Type of current IP-address
      Pop $0 ; Current IP-address
      MessageBox MB_OK "Type of Ip $1"
      StrCmp $1 '1' '' NoLoopBackIp
      MessageBox MB_OK "LoopBack IP-address: $0"
      Goto Continue
  NoLoopBackIp:
      StrCmp $1 '2' '' NoAPA
      MessageBox MB_OK "Automatic Private IP-address: $0"
      Goto Continue
  NoAPA:
      StrCmp $1 '3' '' NoLanIp
      MessageBox MB_OK "Network IP-address: $0"
      Goto Continue
  NoLanIp:
      MessageBox MB_OK "Internet IP-address: $0"
      Goto Continue
  Continue:
      Goto ExitLoop
  ExitLoop:
  Pop $1
  Pop $0
FunctionEnd 
Oh, and a little test function:

Function test
  StrCpy $Ip "192.168.2.11"  ; Network IP-address
  Call testIp
  StrCpy $Ip "169.254.10.10" ; Automatic Private IP-address
  Call testIp
  StrCpy $Ip "127.0.0.1"     ; LoopBack IP-address
  Call testIp
  StrCpy $Ip "66.35.250.209" ; Internet IP-address
  Call testIp
FunctionEnd 

evilO/Olive