Announcement

Collapse
No announcement yet.

Checking for valid IP addresses

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checking for valid IP addresses

    Hi everyone, does anyone have an alternative to the following code. Basically I'm after the fastest way to check for a valid IP address in a string (retreived via DDT). My version is simple but seems to go and check the internet which is not always going to be appropriate.

    Regards

    Adrian Aitken

    FUNCTION CheckIPAddress(strIP AS STRING ) AS LONG

    LOCAL b AS BYTE PTR, ip AS LONG
    HOST ADDR strIP TO ip
    b = VARPTR(ip)
    IF strIP <> FORMAT$(@b[0]) + "." + FORMAT$(@b[1]) + "." + FORMAT$(@b[2]) + "." + FORMAT$(@b[3]) THEN
    FUNCTION = 1
    ELSE
    FUNCTION = 0
    END IF

    END FUNCTION


    ------------------

  • #2
    DDT only offers GUI functionality, not IP (Winsock) functionality!

    A long time ago (circa 1998/99), Dave Navarro posted some information in the Internet Programming forum about ways to stop the dialer from appearing in Win95 - that may offer some assistance for you.

    Other than that, you could drop down and use the the Winsock API directly, but it is unlikely to be any faster since the overhead is within Winsock and what it has to deal with, and HOST ADDR has to go through the same process.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Hi Lance,
      All I really wanted was a quick and reliable method of checking for a valid IP address (to do with the COMM problem as well!).
      I have resorted to using instr(x,"."), putting the value in a byte AND 255 (just incase) and then building the 4 bytes into a dot seperated string.

      Regards

      Adrian

      ------------------

      Comment


      • #4
        You can use the TALLY function to determine that there are 3 dot's in the IP.

        i.e. if TALLY(IPstring, ".") <> 3 then msgbox "Incorrect IP!"

        ------------------
        e-mail: [email protected]
        e-mail (work): [email protected]

        Comment


        • #5
          Hi Sven, nice idea but the people who'll be using the program will be engineers (not computer engineers) who might have finger trouble ie 2555 instead of 255 etc so I need to check the individual numbers as well.

          Thanks

          Adrian

          ------------------

          Comment


          • #6
            Code:
            ipString$ = trim$(ipString$)
            If tally(ipString$, ".") <> 3 then msgbox "no!"
            if len(ipString$) > 15 then msgbox "no!" 
            tempIP$ = ipString$
            tempIP$ = remove$(tempIP$, any ". ")
            if verify(tempIP$, "0123456789") <> 0 then msgbox "no!"
            Try this!



            ------------------
            e-mail: [email protected]
            e-mail (work): [email protected]

            Comment


            • #7
              Hi Sven, nice but it doesn't stop things like 192..168.0.1 or 25555.255.255.0

              Regards

              Adrian Aitken


              ------------------

              Comment


              • #8
                Your original question used HOST ADDR to validate the address, so it appears you wanted a "real" IP address validated, not just that the dotted-IP address string "fits" into the 255.255.255.255 format.

                You should be able to validate the string format quite easily using VERIFY, INSTR, PARSECOUNT and PARSE$. Something like this (off the top of my head, untested code)
                Code:
                a$ = "168.0.0.1"
                ...
                FUNCTION Valid&(BYVAL a$)
                  LOCAL x&
                  IF ISTRUE VERIFY(a$, ANY "0123456789.") THEN EXIT FUNCTION ' invalid chars
                  IF INSTR(a$, "..") THEN EXIT FUNCTION                      ' Stop invalid format of "0..0.0"
                  IF PARSECOUNT(a$, ".") <> 4 THEN EXIT FUNCTION             ' Ensure 4 values in string
                  FOR x& = 1 TO 4                                            ' Test all elements for 0-255 range
                    IF VAL(PARSE$(a$, ".", x&)) > 255 THEN EXIT FUNCTION     ' Outside the range?
                  NEXT x&
                  Valid& = -1
                END FUNCTION
                I hope this helps!


                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Lance , parsecount - thats the function I was looking for ! Thanks

                  Regards

                  Adrian Aitken

                  ------------------

                  Comment


                  • #10
                    You can also use inet_addr function. (still needs parsecount, because Winsock sees
                    1, 1.1, 1.1.1, and 1.1.1.1 all as valid IP addresses....
                    Code:
                    #Include "wsock32.inc"
                    Function ValidIPAddress(IPAddress As Asciiz) As Long
                      Function = (Inet_Addr(IPAddress) <> -1) And (ParseCount(IPAddress,".")=4)
                    End Function
                    ------------------
                    Peter.
                    mailto[email protected][email protected]</A>
                    Regards,
                    Peter

                    Comment


                    • #11
                      Doesn't a valid IP address also depend on the subnet mask of
                      the network? For instance, 204.31.85.20 is a valid IP address
                      on a Class C network, but on a Class A network it might be
                      invalid.

                      Scott


                      ------------------
                      Scott Wolfington
                      http://www.boogietools.com



                      [This message has been edited by Scott Wolfington (edited March 05, 2003).]
                      Scott Wolfington
                      [url="http://www.boogietools.com"]http://www.boogietools.com[/url]

                      Comment


                      • #12
                        Seems Adrian is more concerned with string validation than the actual validity of the IP address itself... at least, that is what I gleaned from the discussion thus far.

                        ------------------
                        Lance
                        PowerBASIC Support
                        mailto:[email protected][email protected]</A>
                        Lance
                        mailto:[email protected]

                        Comment


                        • #13
                          Lance, you're right (again!) ,
                          the IP address could be anything legal (0-255, 4 sets of digits seperated by dots), either internet valid or private (192.168.x.x etc). The routine also needs to be subnet mask enabled ie 255.255.255.0 is not a valid ip address but is a valid subnet mask.
                          I initially used HOST ADDR because it was built into PB and when I first used it I was connected to the internet so didn't realise HOST ADDR would try to connect (to a DNS server ?).

                          Regards

                          Adrian Aitken

                          ------------------

                          Comment


                          • #14
                            adrian,

                            you can use this trick to disable the dialup window temporarily:
                            http://www.powerbasic.com/support/pb...ead.php?t=1813

                            i'm using it for almost a year now on all 32 bit windows platforms and it's working great.

                            here's a function demonstrating this:

                            Code:
                            function filladdr(psin as sockaddr_in, socknum as long, bclient as long, nhost as string) as long
                            local phe as long
                            local he_list as long
                            local he as hostentstru
                            local sz as asciiz * 200
                            local zkey1 as asciiz * 200
                            local zkey2 as asciiz * 200
                            local autodial as string
                            local dtype as long
                            
                              psin.sin_family = %af_inet
                              ' if we are setting up for a listen() call (bclient = false), fill servent with our address.
                              if bclient <> 0 then
                                gethostname sz, sizeof(sz)
                              else
                                sz = nhost
                              end if
                              zkey1 = "software\microsoft\windows\currentversion\internet settings"
                              zkey2 = "enableautodial"
                              'binary value: "enableautodial"
                              dtype = %reg_binary
                              autodial = getregistryvalue(%hkey_current_user, dtype, zkey1, zkey2, ")
                              dtype = %reg_binary
                              if len(autodial) then
                                setregistryvalue %hkey_current_user, dtype, zkey1, zkey2, mkl$(0)
                              end if
                              phe = gethostbyname(sz)
                              if len(autodial) then
                                if asc(autodial) <> 0 then
                                  setregistryvalue %hkey_current_user, dtype, zkey1, zkey2, mkl$(1)
                                end if
                              end if
                              if phe <> %null then
                                copymemory byval varptr(he), byval phe, byval len(he)
                                he_list = he.h_list
                                he_list = cvl(peek$(he_list,4))
                                psin.sin_addr.s_addr = cvl(peek$(he_list,4))
                              end if
                              'set the port number
                              psin.sin_port = htons(socknum)        ' convert to network ordering
                              function = %true
                            end function

                            peter.


                            ------------------
                            [email protected]
                            [email protected]

                            Comment

                            Working...
                            X