Ping a IP subnet

Share your advanced PureBasic knowledge/code with the community.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Ping a IP subnet

Post 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
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

hey fweil, winsock has api's that convert the byte sequence for network addresses...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Yes functions exist but here is mine and it's fast :lol:
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post 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!
~I see one problem with your reasoning: the fact is thats not a chicken~
Post Reply