Announcement

Collapse
No announcement yet.

Null pointer

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

  • Null pointer

    I'm trying to enumerate all the IP addresses on a machine.
    The problem is that when I get the pointer to the list of all the ip's I can't increase it. When I increase the pointer with 4 bytes the pointer is refering to an invalid address.

    Code I use :

    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
    #INCLUDE "WSOCK32.INC"


    DECLARE FUNCTION StartWinsock()AS LONG

    FUNCTION PBMAIN()
    DIM winsck AS LONG
    DIM Hostname AS ASCIIZ * 80
    DIM ptrAsciiz AS ASCIIZ PTR
    DIM ErrNumber AS LONG
    DIM hstEnt AS hostentStru PTR
    DIM varADDR AS in_addr

    winsck = StartWinsock()
    IF winsck <> 0 THEN
    MSGBOX "Error on opening winsock. Error number: " + STR$(winsck)
    CALL wsacleanup()
    EXIT FUNCTION
    END IF

    IF (gethostname(Hostname, SIZEOF(Hostname)) = %SOCKET_ERROR) THEN
    ErrNumber = WSAGetLastError()
    MSGBOX "Error on getting local hostname. Error number: " + STR$(ErrNumber)
    CALL wsacleanup()
    EXIT FUNCTION
    END IF
    MSGBOX "The local hostname is: " + hostname

    hstEnt = gethostbyname(hostname)

    IF hstent = 0 THEN
    MSGBOX "Error on host lookup."
    CALL wsacleanup()
    EXIT FUNCTION
    END IF

    '-------- Here the enum has to start. Currently I can only
    '-------- retrieve 1 address
    '-------- C checks if pointer = 0
    ' FOR (INT i = 0; phe->h_addr_list[i] != 0; ++i) {
    ' struct in_addr ADDR;
    ' memcpy(&addr, phe->h_addr_list[i], SIZEOF(struct n_addr));
    ' cout << "Address " << i << ": " << inet_ntoa(ADDR) << endl;
    ' }

    varADDR.s_addr = @[email protected]@h_list

    ptrAsciiz = inet_ntoa(varADDR.s_addr)

    MSGBOX " Your IP adress is : " + @ptrAsciiz

    CALL wsacleanup()

    END FUNCTION

    FUNCTION StartWinsock()AS LONG
    DIM wsaData AS WSAData
    DIM lngReturn AS LONG

    lngReturn = wsastartup(257,wsaData)
    FUNCTION = lngReturn

    END FUNCTION

    Anybody who know how to check if the pointer reference is 0 (null) ?

    Thanks

  • #2
    IF ptrASCIIZ = 0 THEN MSGBOX "The pointer is NULL, NULL is the pointer!"

    Do you want to check the target byte? If so, then LEN(@ptrASCIIZ) should return zero, as long as the target address is allocated to your application.

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

    Comment


    • #3
      The problem is if I increase h_list I'm getting a reference to an illigal address. Maybe I'm doing the increase the wrong way.

      When I'm looking at the type declare the member of hostent, h_list is a long pointer adress to the list of all ip addresses. So when I increase this pointer with 4 bytes I should get the next address ?

      But if there's no other address then the pointer refers to an illigal address. So I can't call @[email protected]@h_list because this gives me a GPF. So the problem is that I don't know how to check if the pointer still points to a valid address.

      Erwin



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

      Comment


      • #4
        Hi Erwin

        try:

        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WIN32API.INC"
        #INCLUDE "WSOCK32.INC"
        
        
        DECLARE FUNCTION StartWinsock()AS LONG
        
        FUNCTION PBMAIN()
        DIM winsck AS LONG
        DIM Hostname AS ASCIIZ * 80
        DIM ptrAsciiz AS ASCIIZ PTR
        DIM ErrNumber AS LONG
        DIM hstEnt AS hostentStru PTR
        DIM varADDR AS in_addr
        DIM plAddr AS LONG PTR
        
            winsck = StartWinsock()
            IF winsck <> 0 THEN
                MSGBOX "Error on opening winsock. Error number: " + STR$(winsck)
                CALL wsacleanup()
                EXIT FUNCTION
            END IF
        
            IF (gethostname(Hostname, SIZEOF(Hostname)) = %SOCKET_ERROR) THEN
                ErrNumber = WSAGetLastError()
                MSGBOX "Error on getting local hostname. Error number: " + STR$(ErrNumber)
                CALL wsacleanup()
                EXIT FUNCTION
            END IF
            MSGBOX "The local hostname is: " + hostname
        
            hstEnt = gethostbyname(hostname)
        
            IF hstent = 0 THEN
                MSGBOX "Error on host lookup."
                CALL wsacleanup()
                EXIT FUNCTION
            END IF
        
        '-------- Here the enum has to start. Currently I can only
        '-------- retrieve 1 address
        '-------- C checks if pointer = 0
        '    FOR (INT i = 0; phe->h_addr_list[i] != 0; ++i) {
        '        struct in_addr ADDR;
        '        memcpy(&addr, phe->h_addr_list[i], SIZEOF(struct n_addr));
        '        cout << "Address " << i << ": " << inet_ntoa(ADDR) << endl;
        '    }
        
            plAddr =  @hstent.h_list
            DO WHILE @plAddr <> 0
        
                ptrAsciiz = inet_ntoa(BYVAL @@plAddr)
        
                MSGBOX " Your IP adress is : " + @ptrAsciiz
                INCR plAddr
        
        
            LOOP
        
            CALL wsacleanup()
        
        END FUNCTION
        
        FUNCTION StartWinsock()AS LONG
        DIM wsaData AS WSAData
        DIM lngReturn AS LONG
        
            lngReturn = wsastartup(MAKLNG(2,0),wsaData) 'ask for Winsock 2.0
             FUNCTION = lngReturn
        
        END FUNCTION


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

        Comment


        • #5
          hent.h_list is a pointer, to an array of pointers(which is null terminated)...
          Err..

          Anyways
          This code works for me:

          Code:
          #COMPILE EXE
          #DIM ALL
          #INCLUDE "WIN32API.INC"
          #INCLUDE "WSOCK32.INC"
          
          FUNCTION PBMAIN()
            DIM wd       AS WSAData
            DIM ret      AS LONG
            DIM hostname AS ASCIIZ * 256
            DIM hent     AS hostentStru PTR
            DIM i        AS LONG
            DIM ptrIP    AS ASCIIZ PTR
          
            ret = WSAStartup(&H101, wd)
            IF ret <> 0 THEN
              MSGBOX "Blah."
              EXIT FUNCTION
            END IF
          
            ' Set hostname to your machine name...
            hostname = "www.altavista.com"
            hent = gethostbyname(hostname)
          
            IF hent = 0 THEN
              MSGBOX "gethostbyname()... beep."
              WSACleanup
              EXIT FUNCTION
            END IF
          
            DO
              INCR i
              ptrIP = inet_ntoa(@[email protected]@h_list)
              MSGBOX "IP Address #" + FORMAT$(i) + ": " + @ptrIP
              @hent.h_list = @hent.h_list + @hent.h_len
              IF @[email protected]_list = 0 THEN EXIT DO
            LOOP
          
            WSACleanup
          END FUNCTION

          ------------------
          http://www.sublevel6.f2s.com/

          Comment


          • #6
            Thanks, problem solved ...

            Erwin van de wiel


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

            Comment


            • #7
              If this helps:


              Code:
              '------------------------------------------------------------------------------
              Global hostent          As HostEntStru Ptr
              Global lpAddr           As In_Addr
              Global ptr1             As Asciiz Ptr 
              
              
              Function Host_to_IP(hName As Asciiz) As String
              hostent = GetHostByName(hName)
              
              If hostent=0 Then
                WSAErMsg = WSAGetLastError
                Function = WSAErMsgToStr(WSAErMsg)
              Else
                lpaddr.s_addr = @[email protected]@h_list
                'Get the ip address
                ptr1 = inet_ntoa(lpaddr.s_addr)
                Function = @ptr1
              End If
              End Function
              
              '------------------------------------------------------------------------------
              
              Function IP_to_Host(haddr As Asciiz) As String
              ' IP to Host Name
              ' ascii IP to dword addr
              lpaddr.s_addr = Inet_Addr(haddr)
              ' get host by address
              hostent = GetHostByAddr(lpaddr.s_addr, 4, %PF_INET)
              ' null =  error
              If hostent = 0 Then
                WSAErMsg = WSAGetLastError
                Function = WSAErMsgToStr(Result)
              Else
                Function = @[email protected]_name
              End If
              End Function
              
              '------------------------------------------------------------------------------
              ------------------
              Scott
              mailto:[email protected][email protected]</A>

              [This message has been edited by Scott Turchin (edited May 04, 2000).]
              Scott Turchin
              MCSE, MCP+I
              http://www.tngbbs.com
              ----------------------
              True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

              Comment


              • #8
                Originally posted by Andreas_Allerdahl:
                hent.h_list is a pointer, to an array of pointers(which is null terminated)...
                Err..

                Anyways
                This code works for me:

                <font face="Courier New, Courier" size="3"><pre>
                #COMPILE EXE
                #DIM ALL
                #INCLUDE "WIN32API.INC"
                #INCLUDE "WSOCK32.INC"

                FUNCTION PBMAIN()
                DIM wd AS WSAData
                DIM ret AS LONG
                DIM hostname AS ASCIIZ * 256
                DIM hent AS hostentStru PTR
                DIM i AS LONG
                DIM ptrIP AS ASCIIZ PTR

                ret = WSAStartup(&H101, wd)
                IF ret <> 0 THEN
                MSGBOX "Blah."
                EXIT FUNCTION
                END IF

                ' Set hostname to your machine name...
                hostname = "www.altavista.com"
                hent = gethostbyname(hostname)

                IF hent = 0 THEN
                MSGBOX "gethostbyname()... beep."
                WSACleanup
                EXIT FUNCTION
                END IF

                DO
                INCR i
                ptrIP = inet_ntoa(@[email protected]@h_list)
                MSGBOX "IP Address #" + FORMAT$(i) + ": " + @ptrIP
                @hent.h_list = @hent.h_list + @hent.h_len
                IF @[email protected]_list = 0 THEN EXIT DO
                LOOP

                WSACleanup
                END FUNCTION
                </pre></font>

                I've found the line "IF @[email protected]_list = 0 THEN EXIT DO"
                will repeatably often cause a crash under circumstances where,
                for example, there are network cards active and a modem
                is also logged into another network. However
                "IF @hent.h_list = 0 THEN EXIT DO"
                seems to not cause a crash.

                ------------------
                Michael Burns
                Michael Burns

                Comment


                • #9
                  Exactly... test for a non-null pointer before looking at the pointers target - which was where this thread started all those months (years?) ago!

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

                  Comment

                  Working...
                  X