Announcement

Collapse
No announcement yet.

Enumerating IP valid addresses

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

  • Enumerating IP valid addresses

    I posted a variant of this question on a thread earlier today, but it got lost in the shuffle and so far the messages posted after mine were orthogonal to my question.

    I realize everyone knows this, but I'll go through it to set up the problem.

    In a "HOST ADDR TO Temp" statement, you get the primary IP address of the machine placed in Temp. If you have more than one IP address assigned to the machine, you'll only get the first one with that version of the HOST ADDR statement. OK, fine. We all know that if you have two ethernet cards, or say one and have an active modem, there will be two IP addresses, and you can get them by doing:

    HOST ADDR 1 TO Temp1
    HOST ADDR 2 TO Temp2

    The "1" and "2" are the index. The index is the key.

    Here is my question: How do you enumerate the valid range of the index if you need to know ALL of the IP addresses for the machine, but your application will not have a clue ahead of time how many the machine it is running on might have?

    I did a simple experiment hoping I could simply advance the index until a trappable error occurred. No such luck.. If you exceed the index's valid range, you get a bunch of nonvalid IP addresses (as far as I can tell), and then a General Protection Fault.

    For example, in my machine right now I have two ethernet cards and a (inactive) modem.

    If I run this code (suitably inserted in the correct PB trappings):

    FOR Index& = 1 TO 10
    HOST ADDR (Index&) TO Temp
    IF ERR THEN GOTO Outloop
    #DEBUG PRINT IpToString(Temp)
    NEXT Index&
    Outloop:


    I trigger a General Protection Fault when Index&=6.

    The DEBUG window displays

    24.13.211.16
    192.168.0.1
    107.98.46.110
    101.46.109.101
    100.105.97.111

    The first two IP addressess (24.13.211.16, 192.168.0.1) are correct. That last 3 are junk as far as I can tell.

    So does anyone know how if I have no prior knowledge of how many IP addresses the machine my application will be running in should have, but I need to know ALL of them, how can I ascertain the valid range for Index& ?

    (As an aside, I do know how to enumerate & list all the valid MAC addresses in a machine, but aside from doing all that work to just to get an index range for enumerating all the IP addresses, the number of valid MAC addresses won't always match the number of valid IP addresses.)


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

  • #2
    hi michael

    you'll find what you're looking for at: http://www.powerbasic.com/support/pb...ead.php?t=3747

    cheers

    florent

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

    Comment


    • #3
      I trigger a General Protection Fault when Index&=6.
      That is an unfortunate bug in HOST ADDR which will be corrected in the next update to the compiler.

      Note that when HOST ADDR(Index&) returns zero, there are no further IP addresses to enumerate. I'm not sure if the addresses you receive are vaklid or not, but on my DEV PC I can locate only 2 IP addresses with HOST ADDR. (disclaimer, I wrote the last part of this based on my recollection of my tests, so I'll recheck that result when I get back to my DEV PC).



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

      Comment


      • #4
        If you want to index you COULD try this, it's cheap but it may work:

        HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\NetTrans\0001

        Start with 0001, go through to about 12, the key you are looking for first is:

        DriverDesc

        If it says TCP/IP Then call the same key for:

        IPAddress


        My code would loop through that in about half a second or better...

        Again it's cheap, the other method appears to work better and be the RIGHT way to do it....


        Scott

        ------------------
        Scott
        mailto:[email protected][email protected]</A>
        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


        • #5
          Thanks Florent. Your code snippet works just fine.


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

          Comment


          • #6
            Woops! Here's a puzzler. When I tested that code in the following
            manner, it worked just fine.

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

            FUNCTION EnumerateIPs () AS STRING
            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
            DIM temp AS STRING
            ret = WSAStartup(&H101, wd)
            IF ret <> 0 THEN
            EnumerateIPs=""
            EXIT FUNCTION
            END IF
            ' Set hostname to your machine name...
            HOST NAME TO hostname
            hent = gethostbyname(hostname)
            IF hent = 0 THEN
            EnumerateIPs=""
            WSACleanup
            EXIT FUNCTION
            END IF
            temp = "All IP addresses found assigned to this machine:" +CHR$(13) +CHR$(10)
            DO
            INCR i
            ptrIP = inet_ntoa(@[email protected]@h_list)
            temp = temp + @ptrIP +CHR$(13) +CHR$(10)
            @hent.h_list = @hent.h_list + @hent.h_len
            IF @[email protected]_list = 0 THEN EXIT DO
            LOOP
            WSACleanup
            EnumerateIPs=temp
            END FUNCTION

            FUNCTION PBMAIN()

            MSGBOX EnumerateIPs

            END FUNCTION


            However when I cut & pasted the subroutine "EnumerateIPs" into the
            program I want to use it in (which has the same compiler
            switches,
            #COMPILE EXE
            #DIM ALL
            #INCLUDE "WIN32API.INC"
            #INCLUDE "WSOCK32.INC"
            and has no globals with the same name as any of the local
            variables), it gives a
            Error 480: Parameter mismatch, may need ByCopy...
            ret = WSAStartup(&H101, wd)

            I tried declaring the local variables explicitly by adding LOCAL
            to their declarations in case the compiler was confused, as well
            as (on a lark) passing them using ByCopy. All I can imagine is
            that somehow its still thinking ret = WSAStartup(&H101, wd) refers
            to something else...



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

            Comment


            • #7
              Found the problem. There *WAS* a duplicate declaration of the
              data type WSAData in my applicaton. Thanks again for the solution.

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

              Comment

              Working...
              X