Announcement

Collapse
No announcement yet.

Convert IP address to a long.

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

  • Convert IP address to a long.

    I looked but maybe I don't know what search term to look for.

    I need to convert an IP address (WWW.XXX.YYY.ZZZ) to a long. How???

    TIA and cheers.
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

  • #2
    iplong = ((www*256+xxx)*256+yyy)*256+zzz

    Comment


    • #3
      LONG to STRING
      Code:
       
        DIM lIP     As Long
        DIM IPAddr  As Byte Ptr
       
        '---Fill the lIP value in whatever way
       
        IPAddr = VarPtr(lIP)
        Function = Using$("#_.#_.#_.#", @IPAddr, @IPAddr[1], @IPAddr[2], @IPAddr[3])
      STRING to LONG
      Code:
        DIM sIP AS STRING
        DIM lIP AS LONG
       
        '---Fill the sIP value in whatever way
        '---Supposing sIP is in the form x.y.z.w
       
        lIP = ((Val(Parse$(sIP, ".", 4))*256+Val(Parse$(sIP, ".", 3)))*256+Val(Parse$(sIP, ".", 2)))*256+Val(Parse$(sIP, ".", 1))

      Comment


      • #4
        Or using the Winsock functions..
        Code:
        #INCLUDE "WIN32API.INC"
        Declare Function inet_addr LIB "wsock32.dll" Alias "inet_addr" (cp As Asciiz) AS Dword  ' Dotted to numeric
        Declare Function inet_ntoa LIB "wsock32.dll" Alias "inet_ntoa" (byval inn As Dword) As Dword  ' returns Asciiz Ptr
         
        Function PbMain()
         Dim szIP     As Asciiz * 16
         Dim pszIP    As Asciiz Ptr
         Dim IpAddr   As Dword 
         Dim IpName   As String 
         
          szIP = "192.168.0.100" 
          IpAddr = inet_addr (szIP)
          MsgBox "Dotted IP Add as long: " + Str$(IpAddr), ,szIP
         
          pszIP = inet_ntoa (IpAddr)
          MsgBox "Long IP Add as dotted: " + @pszIP, ,Str$(IpAddr)
         
         
          HOST ADDR "Localhost" To IpAddr                     ' PB Statement
          HOST NAME IpAddr      To IpName                     ' PB Statement
          pszIP = inet_ntoa (IpAddr)
         
          MsgBox  "HOST ADDR: " + str$(IpAddr) + $CR + _
                  "HOST NAME: " + IpName + $CR + _
                  "Dotted IP: " + @pszIP
         
        End Function
        Rgds, Dave

        Comment


        • #5
          Okay, I'm confused.....

          ....again.

          With an IP string of "www.xxx.yyy.zzz", I was under the assumption that "zzz" was the least significant byte. However, running the test program below, it appears that "www" is the least significant byte.
          Code:
              DECLARE SUB pause
          
          '   Longs can go from -2,147,483,648 to +2,147,483,647
              FUNCTION PBMAIN()
              DIM lIP     AS LONG
              DIM ip      AS LONG
              DIM test2   AS LONG
              DIM IPAddr  AS BYTE PTR
              DIM sip     AS STRING
              LOCAL start AS LONG
              LOCAL finish AS LONG
          
              start  = 1677830336 ' <----------Start of my home network. Adjust as you see fit.
              finish = start + 10
          
              FOR ip = start TO finish
              an$ = INKEY$
              IF an$ = CHR$(27) THEN EXIT FOR
              IF an$ = CHR$(32) THEN pause
          '    long to string
              IPAddr = VARPTR(ip)
              sip = USING$("#_.#_.#_.#", @IPaddr, @IPaddr[1], @IPaddr[2], @IPaddr[3])
              
              HOST NAME (ip) TO hostname$
          
          '   string 2 long
              test2 = ((VAL(PARSE$(sIP, ".", 4))*256+VAL(PARSE$(sIP, ".", 3)))*256+VAL(PARSE$(sIP, ".", 2)))*256+VAL(PARSE$(sIP, ".", 1))
              PRINT;ip,sip,hostname$
              NEXT ipaddr
              
              BEEP
              WAITKEY$
              END FUNCTION
              
          SUB pause
              WHILE INSTAT = 0 : WEND
              an$ = INKEY$
              END SUB
          Can somebody set me straight here, please?
          There are no atheists in a fox hole or the morning of a math test.
          If my flag offends you, I'll help you pack.

          Comment


          • #6
            IP addresses, port numbers, etc. are all represented internally in network byte order. In other words, the 32-bit value for an IPv4 address is big-endian.

            For example, if you have the IP address 192.168.0.1, the 32-bit value for that on a little-endian system (like a PC) would be different than on a big-endian system. To resolve this, the rule was established that all network addresses and port numbers would be specified in big-endian format. So, on a PC, those bytes need to be swapped around into network byte order, so 192.168.0.1 would be specified as the value &H0100A8C0.

            Edit: Just a side-note: when thinking about the numeric form of IPv4 addresses, it's generally useful to think in hex, not decimal.
            Last edited by Mike Stefanik; 3 Aug 2008, 01:44 AM.
            Mike Stefanik
            sockettools.com

            Comment


            • #7
              So, yet another possibility:
              Code:
              ipLong = CVL(CHR$(WWW,XXX,YYY,ZZZ))
              Added: I reversed above bytes to correct their order as per Mel and Mike.
              Last edited by John Gleason; 3 Aug 2008, 07:10 AM. Reason: reversed byte order to correct.

              Comment


              • #8
                John, that worked after I changed z,y,x,w to w,x,y,z. In this instance, as explained by Mike, the least significant byte is on the left side of the string. Not the right hand.

                Thanks for the explination, Mike. It is a bit confusing.
                There are no atheists in a fox hole or the morning of a math test.
                If my flag offends you, I'll help you pack.

                Comment


                • #9
                  Mel, I'm not sure about this, but if I read Mike Stefanik correctly &hC0A80001 is the result, and the LONG decimal of that is -1062731775, which is also what
                  Code:
                  ipLong = CVL(CHR$(ZZZ,YYY,XXX,WWW))
                  returns. Is that right Mike?

                  Added: And it turns out that is not right as Mike explains above and below so like you said Mel:
                  Code:
                  ipLong = CVL(CHR$(WWW,XXX,YYY,ZZZ))
                  which is also easier in that it allows the address to be entered as is, with no reversal of bytes. I'll note the change above too.
                  Last edited by John Gleason; 3 Aug 2008, 07:05 AM.

                  Comment


                  • #10
                    Sounds like you have your answer already, but my String2IP function is slightly different that what I have seen here, so I thought I would share it and the other functions I have created in the past to deal with IP addresses:

                    Code:
                    '==================================================
                    ' String2IP - converts a text IP address like 10.145.222.10 to a DWORD value (which is how windows handles them internally)
                    '==================================================
                    Function String2IP(sIP As String) As Dword
                       Local dIP   As Dword
                       Local pIP   As Byte Ptr
                       If ParseCount(sIP,".") > 2 Then
                          pIP = VarPtr(dIP) 'assign the Byte size pointer to the DWORD variable called dIP  (a DWORD can fit 4 byte variables inside it)
                          If pIP <> 0 Then
                             @pIP = Val(Parse$(sIP,".",1))    'assign the 1st octet of the dword variable called dIP
                             @pIP[1] = Val(Parse$(sIP,".",2)) '2nd octet (the [1] tells it we want to offset the pointer by one byte size variable)
                             @pIP[2] = Val(Parse$(sIP,".",3)) '3rd octet
                             @pIP[3] = Val(Parse$(sIP,".",4)) '4th octet
                             Function = dIP
                          End If
                       Else
                          Function = 0
                       End If
                    End Function
                    
                    '==================================================
                    ' IP2String - converts a DWORD in to the text version of a IP address (opposite of the String2IP function)
                    '==================================================
                    Function IP2String(dRawAddr As Dword) As String
                       Dim pIP As Byte Ptr
                       pIP = VarPtr(dRawAddr)     'assign the address to the pointer
                       If pIP <> 0 Then
                          Function = Using$("#_.#_.#_.#", @pIP, @pIP[1], @pIP[2], @pIP[3])  'the powerbasic using$ command is similar to the Format() command
                       End If
                    End Function
                    
                    '==================================================
                    ' IsValidIP - looks at a text to see if it is a valid IP address
                    '==================================================
                    Function IsValidIP(ByVal sIP As String) As Long
                       Local iCount As Long
                       If IsTrue Verify(sIP, "0123456789.") Then Exit Function  'the powerbasic Verify() command tells us if we have any character not in our list
                       If InStr(1,sIP,"..") Then Exit Function   'not valid if we have 2 periods together
                       If ParseCount(sIP, ".") <> 4 Then Exit Function 'not valid if we dont have exactly 4 segments
                       For iCount = 1 To 4     'now loop through each segment and make sure the number value is from 0 to 255
                          If Val(Parse$(sIP, ".", iCount)) > 255 Then Exit Function
                       Next iCount
                       Function = %TRUE  'if we made it passed all the tests it must be valid so return TRUE
                    End Function
                    
                    
                    '==================================================
                    ' IncrIpRange - used to process through a IP range (changes the IP dword to the next IP and returns FALSE if past the last IP in the range)
                    '==================================================
                    Function IncrIpRange(IP As Dword, ToIP As Dword) As Long 'increases to the next IP address
                       Local pIP   As Byte Ptr
                       Local pToIP As Byte Ptr
                       Local iRet  As Long
                       pIP = VarPtr(IP)
                       pToIP = VarPtr(ToIP)
                       If (pIP = 0) Or (pToIP = 0) Then Exit Function
                       Incr @pIP[3]         '4th octet
                       If @pIP[3] = 0 Then
                          Incr @pIP[2]      '3rd octet
                          If @pIP[2] = 0 Then
                             Incr @pIP[1]   '2nd octet
                             If @pIP[1] = 0 Then
                                Incr @pIP   '1st octet
                             End If
                          End If
                       End If
                       iRet = %TRUE   '%TRUE unless we find otherwise
                       If @pIP[3] > @pToIP[3] Then iRet = %FALSE    '4th octet is greater so %FALSE
                       If @pIP[2] < @pToIP[2] Then iRet = %TRUE     '3rd octet is lessthan so %TRUE even if 4th octet is greater
                       If @pIP[2] > @pToIP[2] Then iRet = %FALSE    '3rd octet is greater so %FALSE
                       If @pIP[1] < @pToIP[1] Then iRet = %TRUE     '2nd octet is lessthan so %TRUE even if 3rd or 4th octet is greater
                       If @pIP[1] > @pToIP[1] Then iRet = %FALSE    '2nd octet is greater so %FALSE
                       If @pIP < @pToIP Then iRet = %TRUE           '1st octet is lessthan so %TRUE even if 2nd or 3rd or 4th octet is greater
                       If @pIP > @pToIP Then iRet = %FALSE          '1st octet is greater so %FALSE
                       Function = iRet
                       'returns %FALSE if new IP is past ToIP
                    End Function
                    '==================================================
                    
                    
                    '==================================================
                    ' InIpRange - tests to see if a IP is inside a IP range
                    '==================================================
                    Function InIpRange(dIP As Dword, dFromIP As Dword, dToIP As Dword) As Long
                       Local pIP      As Byte Ptr
                       Local pFromIP  As Byte Ptr
                       Local pToIP    As Byte Ptr
                       Local iRet1    As Long
                       Local iRet2    As Long
                       pIP = VarPtr(dIP)
                       pFromIP = VarPtr(dFromIP)
                       pToIP = VarPtr(dToIP)
                       If (pIP = 0) Or (pToIP = 0) Or (pFromIP = 0) Then Exit Function
                    
                       'check if IP is above the From IP
                       iRet1 = %TRUE   '%TRUE unless we find otherwise
                       If @pIP[3] < @pFromIP[3] Then iRet1 = %FALSE    '4th octet is lessthan so %FALSE
                       If @pIP[2] > @pFromIP[2] Then iRet1 = %TRUE     '3rd octet is greater so %TRUE even if 4th octet is less
                       If @pIP[2] < @pFromIP[2] Then iRet1 = %FALSE    '3rd octet is lessthan so %FALSE
                       If @pIP[1] > @pFromIP[1] Then iRet1 = %TRUE     '2nd octet is greater so %TRUE even if 3rd or 4th octet is less
                       If @pIP[1] < @pFromIP[1] Then iRet1 = %FALSE    '2nd octet is lessthan so %FALSE
                       If @pIP > @pFromIP Then iRet1 = %TRUE           '1st octet is greater so %TRUE even if 2nd or 3rd or 4th octet is less
                       If @pIP < @pFromIP Then iRet1 = %FALSE          '1st octet is lessthan so %FALSE
                    
                       'check if IP is below the To IP
                       iRet2 = %TRUE   '%TRUE unless we find otherwise
                       If @pIP[3] > @pToIP[3] Then iRet2 = %FALSE    '4th octet is greater so %FALSE
                       If @pIP[2] < @pToIP[2] Then iRet2 = %TRUE     '3rd octet is lessthan so %TRUE even if 4th octet is greater
                       If @pIP[2] > @pToIP[2] Then iRet2 = %FALSE    '3rd octet is greater so %FALSE
                       If @pIP[1] < @pToIP[1] Then iRet2 = %TRUE     '2nd octet is lessthan so %TRUE even if 3rd or 4th octet is greater
                       If @pIP[1] > @pToIP[1] Then iRet2 = %FALSE    '2nd octet is greater so %FALSE
                       If @pIP < @pToIP Then iRet2 = %TRUE           '1st octet is lessthan so %TRUE even if 2nd or 3rd or 4th octet is greater
                       If @pIP > @pToIP Then iRet2 = %FALSE          '1st octet is greater so %FALSE
                    
                       If (iRet1 = %TRUE) And (iRet2 = %TRUE) Then Function = %TRUE
                    End Function

                    Note: the IncrIPRange() function is used to process an IP range and is used like this:
                    Code:
                    Do 
                       'Process dIpFrom address
                       '...
                       If IsFalse IncrIPRange(dIpFrom, dIpTo) Then Exit Loop  'increases dIpFrom until it passes dIpTo
                    Loop
                    Last edited by William Burns; 2 Aug 2008, 11:20 PM.
                    "I haven't lost my mind... its backed up on tape... I think??" :D

                    Comment


                    • #11
                      Originally posted by John Gleason View Post
                      Mel, I'm not sure about this, but if I read Mike Stefanik correctly &hC0A80001 is the result ...
                      Actually, I had my hex values flipped around backwards. The 32-bit integer value for the address 192.168.0.1 in network byte order (as returned by inet_addr) is &H0100A8C0. I fixed my post above and made it a bit clearer, so it doesn't confuse anyone.
                      Last edited by Mike Stefanik; 3 Aug 2008, 01:44 AM.
                      Mike Stefanik
                      sockettools.com

                      Comment

                      Working...
                      X