Announcement

Collapse
No announcement yet.

Calculate the Broadcastaddress

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
    Marc Giesmann
    Member

  • Marc Giesmann
    replied
    Got it now...

    Okay, got it now.

    Its not EQV what should be used- if you use EQV you just would'nt often reach 255 on the last host-byte.

    Heres the working thing:
    Broadcast = IP-Adress OR NOT(Subnetmask)

    Code:
    FUNCTION GetSystemBroadcast(sIP AS STRING) AS STRING
       LOCAL lIP, lSUB, lBR AS LONG
    
       lIP    = ip2num(sIP)
       lSUB = ip2num(getSubNet(sIP))
       lBR = lIP OR NOT(lSUB)
    
       'MSGBOX BIN$(lIP, 32) + $CRLF + _
    '              BIN$(lSUB,32) + $CRLF + _
    '              BIN$(lBR,32  )
    
       FUNCTION = num2ip(lBR)
    END FUNCTION
    Thanks and bye...
    Marc

    Leave a comment:

  • Marc Giesmann
    Member

  • Marc Giesmann
    started a topic Calculate the Broadcastaddress

    Calculate the Broadcastaddress

    Hello, everybody.
    Just a little question about broadcasts.

    As far as I know, the broadcast-address is just the binary equivalent (EQV) to the subnetmask. But my program always returns "192.168.1.235" as my broadcasr. My (only) IP here is 192.168.1.1, and my subnet is a standard C-subnet: 255.255.255.0

    So, whats wrong here?

    Code:
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE "Win32API.inc"
    #INCLUDE "iphlpapi.inc"
    FUNCTION ip2num (BYVAL sIP AS STRING) AS LONG
          LOCAL ix AS LONG
          LOCAL nResult AS LONG
          LOCAL pb AS BYTE PTR
          pb = VARPTR(nResult) + SIZEOF(nResult) - 1
    
          FOR ix = 4 TO 1 STEP -1
             @pb = VAL(PARSE$(sIP, ".", ix))
             DECR pb
    
          NEXT
          FUNCTION = nResult
    END FUNCTION
    
    FUNCTION num2ip(BYVAL ip AS LONG) AS STRING
      LOCAL b AS BYTE PTR
    
      b = VARPTR(ip)
      FUNCTION = FORMAT$(@b[0]) & "." & FORMAT$(@b[1]) & "." & _
                 FORMAT$(@b[2]) & "." & FORMAT$(@b[3])
    END FUNCTION
    
    'Thanks to Steve Smith and William Burns- > Stolen from
    'http://www.powerbasic.com/support/pbforums/showthread.php?t=13418
    FUNCTION getSubnet(sIP AS STRING) AS STRING
        LOCAL tbl AS MIB_IPADDRTABLE PTR
        LOCAL Ret AS LONG
        LOCAL a AS LONG
        LOCAL i AS LONG
        LOCAL MyAddr AS STRING
        LOCAL sBuffer AS STRING
    
        'First, get the size result in Ret
        a = GetIpAddrTable (BYVAL 0, Ret, 0)
        sBuffer = SPACE$(Ret)
        a = GetIpAddrTable (BYVAL STRPTR(sBuffer), Ret, 0)
        tbl = STRPTR(sBuffer)
    
        FOR i = 0 TO @tbl.dwNumEntries - 1
            MyAddr = num2ip(@tbl.table(i).dwAddr)
    
            IF MyAddr = sIP THEN
             FUNCTION =  num2ip(@tbl.table(i).dwMask)
             EXIT FUNCTION
            END IF
        NEXT
    END FUNCTION
    
    FUNCTION GetSystemIP() AS STRING
       LOCAL ip AS LONG
    
       HOST ADDR TO ip&
       FUNCTION =  num2ip(ip)
    END FUNCTION
    
    FUNCTION GetSystemBroadcast(sIP AS STRING) AS STRING
        'MSGBOX BIN$(ip2num(sIP), 32)    + $CRLF + _
        'BIN$(ip2num(getSubnet(sIP)),32) + $CRLF + _
        'BIN$(ip2num(sIP) EQV ip2num(getSubnet(sIP)),32  )
    
        FUNCTION = num2ip(( ip2num(GetSystemIP()) EQV ip2num(getSubnet(GetSystemIP()))))
    END FUNCTION
    
    FUNCTION PBMAIN()
           MSGBOX "IP:" + GetSystemIP()
           MSGBOX "Subnet: " + getSubnet(GetSystemIP())
    
           MSGBOX "Broadcast: " + GetSystemBroadcast(GetSystemIP())
    END FUNCTION
    Feel free to uncomment the MSGBOX to see, If the EQV operator works properly.

    Greetings from germany,
    Marc
Working...
X