-
Notifications
You must be signed in to change notification settings - Fork 1
/
modPing.vb
131 lines (120 loc) · 5.47 KB
/
modPing.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Module modPing
' Credit for the ping module code goes to Dain Axel Muller from Planet Source Code. http://planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4311&lngWId=10
Dim tmrPingCheckTimer As System.Timers.Timer
Function Disable() As String
Unload()
My.Settings.Ping_Enable = False
My.Application.Log.WriteEntry("Ping module is disabled")
Return "Ping module disabled"
End Function
Function Enable() As String
My.Settings.Ping_Enable = True
My.Application.Log.WriteEntry("Ping module is enabled")
Load()
GetPublicIPAddress()
Return "Ping module enabled"
End Function
Function GetPublicIPAddress() As String
If My.Settings.Ping_Enable = True Then
If modGlobal.IsOnline = True Then
If My.Settings.Ping_PublicIPSource = "dyndns" Then
Dim reqUrl As String = "http://checkip.dyndns.org"
Dim req = System.Net.WebRequest.Create(reqUrl)
Dim resp As System.Net.WebResponse = req.GetResponse()
Dim sr = New System.IO.StreamReader(resp.GetResponseStream)
Dim response As String = sr.ReadToEnd().Trim()
Dim responseArray = response.Split(":")
Dim responseArray2 = responseArray(1).Split("<")
Dim strNewPubIP = responseArray2(0).Trim()
If strNewPubIP <> My.Settings.Ping_LastKnownPublicIP Then
My.Application.Log.WriteEntry("Public IP address changed from " + My.Settings.Ping_LastKnownPublicIP + " to " + strNewPubIP)
My.Settings.Ping_LastKnownPublicIP = strNewPubIP
End If
Return strNewPubIP
Else
Return "No valid public IP source set"
End If
Else
Return "Not online"
End If
Else
Return "Ping disabled"
End If
End Function
Function Load() As String
If My.Settings.Ping_Enable = True Then
My.Application.Log.WriteEntry("Loading ping module")
My.Application.Log.WriteEntry("Scheduling automatic Internet checks")
tmrPingCheckTimer = New System.Timers.Timer
AddHandler tmrPingCheckTimer.Elapsed, AddressOf PingInternet
tmrPingCheckTimer.Interval = 60000 ' 1min
tmrPingCheckTimer.Enabled = True
Return "Ping module loaded"
Else
My.Application.Log.WriteEntry("Ping module is disabled, module not loaded")
Return "Ping module is disabled, module not loaded"
End If
End Function
Private Sub PingInternet(source As Object, e As System.Timers.ElapsedEventArgs)
My.Application.Log.WriteEntry("Checking Internet connectivity")
Dim response As String = ""
response = Ping(My.Settings.Ping_InternetCheckAddress)
If response.StartsWith("Reply from") Then
If modGlobal.IsOnline = False Then
My.Application.Log.WriteEntry("System is now connected to the Internet", TraceEventType.Information)
GetPublicIPAddress()
End If
modGlobal.IsOnline = True
ElseIf response = "Ping disabled" Then
' Do nothing, Ping is disabled
Else
If modGlobal.IsOnline = True Then
My.Application.Log.WriteEntry("System is not connected to the Internet", TraceEventType.Warning)
End If
modGlobal.IsOnline = False
End If
End Sub
Public Function Ping(ByVal host As String, Optional ByVal repeat As Integer = 1) As String
If My.Settings.Ping_Enable = True Then
Try
Dim a As New System.Net.NetworkInformation.Ping
Dim b As System.Net.NetworkInformation.PingReply
Dim txtlog As String = ""
Dim c As New System.Net.NetworkInformation.PingOptions
c.DontFragment = True
c.Ttl = 64
Dim data As String = "aaaaaaaaaaaaaaaa"
Dim bt As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
Dim i As Int16
For i = 1 To repeat
b = a.Send(host, 2000, bt, c)
If b.Status = Net.NetworkInformation.IPStatus.Success Then
txtlog += "Reply from " & host & " in " & b.RoundtripTime & " ms, ttl " & b.Options.Ttl
End If
If b.Status = Net.NetworkInformation.IPStatus.DestinationHostUnreachable Then
txtlog += "Destination Host Unreachable"
End If
If b.Status = Net.NetworkInformation.IPStatus.TimedOut Then
txtlog += "Reply timed out"
End If
If i <> repeat Then
txtlog += vbCrLf
End If
Next i
Return txtlog
Catch ex As Exception
My.Application.Log.WriteException(ex)
Return "Ping error"
End Try
Else
Return "Ping disabled"
End If
End Function
Function Unload() As String
My.Application.Log.WriteEntry("Unloading ping module")
If tmrPingCheckTimer IsNot Nothing Then
tmrPingCheckTimer.Enabled = False
End If
Return "Ping module unloaded"
End Function
End Module