codice:
Imports System.Net.Sockets
Imports System.Net
Imports System
Namespace SaurabhPing
Class Ping
Const Const SOCKET_ERROR As Integer = -1
Const Const ICMP_ECHO As Integer = 8
Public Shared Sub Main(ByVal argv As String())
If argv.Length = 0 Then
Console.WriteLine("Usage:Ping <hostname> /r")
Console.WriteLine("<hostname> The name of the Host who you want to ping")
Console.WriteLine("/r Ping the host continuously")
Else
If argv.Length = 1 Then
PingHost(argv(0))
Else
If argv.Length = 2 Then
If argv(1) = "/r" Then
While True
PingHost(argv(0))
End While
Else
PingHost(argv(0))
End If
Else
Console.WriteLine("Error in Arguments")
End If
End If
End If
End Sub
Public Shared Sub PingHost(ByVal host As String)
Dim serverHE As IPHostEntry
Dim fromHE As IPHostEntry
Dim nBytes As Integer = 0
Dim dwStart As Integer = 0
Dim dwStop As Integer = 0
Dim socket As Socket = New Socket (AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP)
Try
serverHE = DNS.GetHostByName(host)
Catch generatedExceptionVariable0 As Exception
Console.WriteLine("Host not found")
Return
End Try
Dim ipepServer As IPEndPoint = New IPEndPoint (serverHE.AddressList(0), 0)
Dim epServer As EndPoint = (ipepServer)
fromHE = DNS.GetHostByName(DNS.GetHostName)
Dim ipEndPointFrom As IPEndPoint = New IPEndPoint (fromHE.AddressList(0), 0)
Dim EndPointFrom As EndPoint = (ipEndPointFrom)
Dim PacketSize As Integer = 0
Dim packet As IcmpPacket = New IcmpPacket
packet.Type = ICMP_ECHO
packet.SubCode = 0
packet.CheckSum = UInt16.Parse("0")
packet.Identifier = UInt16.Parse("45")
packet.SequenceNumber = UInt16.Parse("0")
Dim PingData As Integer = 32
packet.Data = New Byte(PingData)
Dim i As Integer = 0
While i < PingData
packet.Data(i) = CType('#', Byte)
ConversionHelpers.PostIncrement (i)
End While
PacketSize = PingData + 8
Dim icmp_pkt_buffer(PacketSize - 1) As Byte
Dim Index As Int32 = 0
Index = Serialize(packet, icmp_pkt_buffer, PacketSize, PingData)
If Index = -1 Then
Console.WriteLine("Error in Making Packet")
Return
End If
Dim double_length As Double = Convert.ToDouble(Index)
Dim dtemp As Double = Math.Ceil(double_length / 2)
Dim cksum_buffer_length As Integer = Convert.ToInt32(dtemp)
Dim cksum_buffer(cksum_buffer_length - 1) As UInt16
Dim icmp_header_buffer_index As Integer = 0
Dim i As Integer = 0
While i < cksum_buffer_length
cksum_buffer(i) = BitConverter.ToUInt16(icmp_pkt_buffer, icmp_header_buffer_index)
icmp_header_buffer_index += 2
ConversionHelpers.PostIncrement (i)
End While
Dim u_cksum As UInt16 = checksum(cksum_buffer, cksum_buffer_length)
packet.CheckSum = u_cksum
Dim sendbuf(PacketSize - 1) As Byte
Index = Serialize(packet, sendbuf, PacketSize, PingData)
If Index = -1 Then
Console.WriteLine("Error in Making Packet")
Return
End If
dwStart = System.Environment.TickCount
If (nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) = SOCKET_ERROR Then
Console.WriteLine("Socket Error cannot Send Packet")
End If
Dim ReceiveBuffer(256 - 1) As Byte
nBytes = 0
Dim recd As Boolean = False
Dim timeout As Integer = 0
While Not recd
nBytes = socket.ReceiveFrom(ReceiveBuffer, 256, 0, EndPointFrom)
If nBytes = SOCKET_ERROR Then
Console.WriteLine("Host not Responding")
recd = True
' break
Else
If nBytes > 0 Then
dwStop = System.Environment.TickCount - dwStart
Console.WriteLine("Reply from " + epServer.ToString + " in " + dwStop + "MS :Bytes Received" + nBytes)
recd = True
' break
End If
End If
timeout = System.Environment.TickCount - dwStart
If timeout > 1000 Then
Console.WriteLine("Time Out")
recd = True
End If
End While
socket.Close
End Sub
Public Shared Function Serialize(ByVal packet As IcmpPacket, ByVal Buffer As Byte(), ByVal PacketSize As Int32, ByVal PingData As Int32) As Int32
Dim cbReturn As Int32 = 0
Dim Index As Integer = 0
Dim b_type(1 - 1) As Byte
b_type(0) = (packet.Type)
Dim b_code(1 - 1) As Byte
b_code(0) = (packet.SubCode)
Dim b_cksum As Byte() = BitConverter.GetBytes(packet.CheckSum)
Dim b_id As Byte() = BitConverter.GetBytes(packet.Identifier)
Dim b_seq As Byte() = BitConverter.GetBytes(packet.SequenceNumber)
Array.Copy(b_type, 0, Buffer, Index, b_type.Length)
Index += b_type.Length
Array.Copy(b_code, 0, Buffer, Index, b_code.Length)
Index += b_code.Length
Array.Copy(b_cksum, 0, Buffer, Index, b_cksum.Length)
Index += b_cksum.Length
Array.Copy(b_id, 0, Buffer, Index, b_id.Length)
Index += b_id.Length
Array.Copy(b_seq, 0, Buffer, Index, b_seq.Length)
Index += b_seq.Length
Array.Copy(packet.Data, 0, Buffer, Index, PingData)
Index += PingData
If Not (Index = PacketSize) Then
cbReturn = -1
Return cbReturn
End If
cbReturn = Index
Return cbReturn
End Function
Public Shared Function checksum(ByVal buffer As UInt16(), ByVal size As Integer) As UInt16
Dim cksum As Int32 = 0
Dim counter As Integer
counter = 0
While size > 0
Dim val As UInt16 = buffer(counter)
cksum += Convert.ToInt32(buffer(counter))
counter += 1
size -= 1
End While
cksum = (cksum >> 16) + (cksum And 65535)
cksum += (cksum >> 16)
Return CType((Not cksum), UInt16)
End Function
End Class
Public Class IcmpPacket
Public Type As Byte
Public SubCode As Byte
Public CheckSum As UInt16
Public Identifier As UInt16
Public SequenceNumber As UInt16
Public Data As Byte()
End Class
End Namespace