Page 1 of 1

Ping a IP subnet

Posted: Sat May 08, 2004 8:48 pm
by fweil
Enjoy and go on for comments / suggestions ...

Code: Select all

;
; Ping and more tips
;
; FWeil : 20040508
;
; Here you will find the most optimized Ping stuff I found using Windows platforms
;
; One bad thing using IP addresses was that bytes order is reversed so that when you build an IP address
; using MakeIPAddress you can't find an easy way to insert it in a loop.
;
; A long integer has a B3B2B1B0 representation in memory as IP 32 bits addresses are B0B1B2B3 formatted.
;
; I use a fast and easy FASM call to arrange bytes for a loop requirement
;
; Also there is a StringIP function that takes the string representation to build the IP integer representation. Such a function
; could be added in a later Purebasic's update as it is useful for networking stuff
;
; By entering start and end addresses in sStartAddress and sEndAddress variables, you can then parse a subnet in a for / next loop.
;
Procedure StringIP(IPString.s)
  ProcedureReturn MakeIPAddress(Val(StringField(IPString, 1, ".")), Val(StringField(IPString, 2, ".")), Val(StringField(IPString, 3, ".")), Val(StringField(IPString, 4, ".")))
EndProcedure

Procedure BSwap(Integer.l)
  ! MOV eax, dword[esp]
  ! BSWAP eax
  ! MOV dword[esp], eax
  ProcedureReturn
EndProcedure

sStartAddress.s = "194.79.190.0"
sEndAddress.s = "194.79.190.255"

StartAddress.l = StringIP(sStartAddress)
EndAddress.l = StringIP(sEndAddress)

For iIPAddress = BSwap(StartAddress) To BSwap(EndAddress)
  #PING_TIMEOUT = 200
  Echo.ICMP_ECHO_REPLY
  EchoMessage.s = "Hello there " + Space(20)
  hFile = IcmpCreateFile_()
  lngResult = IcmpSendEcho_(hFile, BSwap(iIPAddress), EchoMessage, Len(EchoMessage), 0, Echo, SizeOf(ICMP_ECHO_REPLY), #PING_TIMEOUT)
  If lngResult = 0
      PingResult = Echo\Status * -1
      Debug IPString(BSwap(iIPAddress)) + " do not reply"
    Else
      PingResult = Echo\RoundTripTime
      Debug IPString(BSwap(iIPAddress)) + " replies"
  EndIf
  lngResult = IcmpCloseHandle_(hFile)
Next

Posted: Sat May 08, 2004 9:24 pm
by blueznl
hey fweil, winsock has api's that convert the byte sequence for network addresses...

Posted: Sat May 08, 2004 10:07 pm
by fweil
Yes functions exist but here is mine and it's fast :lol:

Posted: Tue May 11, 2004 9:29 pm
by Killswitch
Nice, I found an easy way to ping in PB, basicly all you need to do is write a .bat file (create a text file then write 'ping host$ > ip.txt) then just recover the IP from the resulting text file, very easy to code!