-
Notifications
You must be signed in to change notification settings - Fork 99
/
Traceroute.cs
265 lines (255 loc) · 7.89 KB
/
Traceroute.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace OpenTrace
{
class TracerouteHop
{
public TracerouteHop(TracerouteResult hopData)
{
HopData = new ObservableCollection<TracerouteResult>();
HopData.Add(hopData);
}
public string No
{
get
{
return HopData[0].No;
}
}
public string IP
{
get
{
List<String> uniqueIPs = new List<string>();
foreach (TracerouteResult hop in HopData)
{
if (!uniqueIPs.Contains(hop.IP) && hop.IP != "*")
uniqueIPs.Add(hop.IP);
}
if (uniqueIPs.Count == 0) uniqueIPs.Add("*");
return String.Join(Environment.NewLine, uniqueIPs);
}
}
public string Time
{
get
{
if (UserSettings.timeRounding)
{
return String.Join(" / ", HopData.Select(d => d.Time == "*" ? d.Time : Math.Round(Convert.ToDouble(d.Time)).ToString()));
}
else
{
return String.Join(" / ", HopData.Select(d => d.Time));
}
}
}
public string Geolocation
{
get
{
List<String> uniqueGeo = new List<string>();
foreach (TracerouteResult hop in HopData)
{
if (!uniqueGeo.Contains(hop.Geolocation) && hop.IP != "*")
uniqueGeo.Add(hop.Geolocation);
}
return String.Join(Environment.NewLine, uniqueGeo);
}
}
public string Organization
{
get
{
List<String> uniqueOrg = new List<string>();
foreach (TracerouteResult hop in HopData)
{
if (!uniqueOrg.Contains(hop.Organization) && hop.IP != "*")
uniqueOrg.Add(hop.Organization);
}
return String.Join(Environment.NewLine, uniqueOrg);
}
}
public string GeolocationAndOrganization
{
get
{
List<String> uniqueGeoAndOrg = new List<string>();
foreach (TracerouteResult hop in HopData)
{
if (!uniqueGeoAndOrg.Contains(hop.Geolocation + " " + hop.Organization) && hop.IP != "*")
uniqueGeoAndOrg.Add(hop.Geolocation + " " + hop.Organization);
}
return String.Join(Environment.NewLine, uniqueGeoAndOrg);
}
}
public string Hostname
{
get
{
List<String> uniqueHostname = new List<string>();
foreach (TracerouteResult hop in HopData)
{
if (!uniqueHostname.Contains(hop.Hostname) && hop.Hostname != "" && hop.IP != "*")
uniqueHostname.Add(hop.Hostname);
}
return String.Join(Environment.NewLine, uniqueHostname);
}
}
public string AS
{
get
{
List<String> uniqueAS = new List<string>();
foreach (TracerouteResult hop in HopData)
{
if (!uniqueAS.Contains(hop.AS) && hop.AS != "" && hop.IP != "*")
uniqueAS.Add(hop.AS);
}
return String.Join(Environment.NewLine, uniqueAS);
}
}
public double StandardDeviation
{
get
{
int count = 0;
double sum = 0;
double mean = 0;
double stdDev = 0;
// Calculate the mean
foreach (TracerouteResult hop in HopData)
{
if(hop.IP != "*")
{
count++;
sum += double.Parse(hop.Time);
}
}
mean = sum / count;
// Calculate the standard deviation
foreach (TracerouteResult hop in HopData)
{
if (hop.IP != "*")
stdDev += Math.Pow(double.Parse(hop.Time) - mean, 2);
}
stdDev = Math.Sqrt(stdDev / count);
return stdDev;
}
}
public int Loss
{
get
{
int count = 0;
foreach (TracerouteResult hop in HopData)
{
if (hop.IP == "*")
count++;
}
return (int)((float)count / HopData.Count * 100);
}
}
public int Recv
{
get
{
int count = 0;
foreach (TracerouteResult hop in HopData)
{
if (hop.IP != "*")
count++;
}
return count;
}
}
public int Sent
{
get
{
return HopData.Count;
}
}
public double Last
{
get
{
if (HopData.Count > 0 && HopData[HopData.Count - 1].IP != "*")
return double.Parse(HopData[HopData.Count - 1].Time);
else
return 0;
}
}
public double Worst
{
get
{
double worst = 0;
foreach (TracerouteResult hop in HopData)
{
if (hop.IP != "*" && double.Parse(hop.Time) > worst)
worst = double.Parse(hop.Time);
}
return worst;
}
}
public double Best
{
get
{
double best = double.MaxValue;
foreach (TracerouteResult hop in HopData)
{
if (hop.IP != "*" && double.Parse(hop.Time) < best)
best = double.Parse(hop.Time);
}
if (best == double.MaxValue) best = 0;
return best;
}
}
public double Average
{
get
{
double sum = 0;
int count = 0;
foreach (TracerouteResult hop in HopData)
{
if (hop.IP != "*")
{
sum += double.Parse(hop.Time);
count++;
}
}
return sum / count;
}
}
public ObservableCollection<TracerouteResult> HopData { get; set; }
}
class TracerouteResult
{
public TracerouteResult(string no, string ip, string time, string geolocation, string ASNumber, string hostname, string organization, string latitude, string longitude)
{
No = no;
IP = ip;
Time = time;
Geolocation = geolocation;
AS = ASNumber;
Hostname = hostname;
Organization = organization;
Latitude = latitude;
Longitude = longitude;
}
public string No { get; set; }
public string IP { get; set; }
public string Time { get; set; }
public string Geolocation { get; set; }
public string AS { get; set; }
public string Hostname { get; set; }
public string Organization { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
}
}