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

