-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
308 lines (274 loc) · 9.65 KB
/
Program.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Network_Monitoring_Sytem
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Start());
}
}
// Abstract Network Devices class
public abstract class NetworkDevices
{
// Name of the Dvice
public string Name { get; set; }
// To store the IPs of the Device
public string IPS { get; set; }
// MAC Addresses of the Devices
public MACAddress MACAddress { get; set; }
// A list to store Different Interfaces of Same Device
public List<Interface> Interfaces { get; set; }
public NetworkDevices(string name)
{
Name = name;
Interfaces = new List<Interface>();
}
public void AddInterface(Interface iface)
{
Interfaces.Add(iface);
}
public void RemoveInterface(Interface iface)
{
Interfaces.Remove(iface);
}
public abstract void SendPacket(Packet packet, Interface sourceInterface);
}
// Server class
public class Server : NetworkDevices
{
public Server(string name) : base(name) { }
public override void SendPacket(Packet packet, Interface sourceInterface)
{
// Unable to Implement it
}
// For Capturing Packet
public string CapturePacket(Packet packet)
{
string message = "Received Packet:\r\n\r\n";
message += $"Source MAC Address: {packet.SourceMACAddress}\r\n";
message += $"Destination MAC Address: {packet.DestinationMACAddress}\r\n";
message += $"Source IP Address: {packet.SourceIPAddress}\r\n";
message += $"Destination IP Address: {packet.DestinationIPAddress}\r\n";
message += $"Data: {packet.Data}\r\n";
message = (message + "\r\n");
return message;
}
// For Polling
public string poll(Interface iface, NetworkDevices p)
{
string message = "Details:\r\n\r\n";
message += $"Name: {p.Name}\r\n";
message += $"Device MAC Address: {p.MACAddress}\r\n";
message += $"Device IP Address: {iface.IPAddress}\r\n";
message += $"Device Interface Name: {iface.Name}\r\n";
message += $"LinkUp: {iface.LinkUp}\r\n";
message = (message + "\r\n");
return message;
}
// For Pinging
public string Ping(string destiny)
{
string message = $"Network Monitoring System [Version 1.0] \r\n\r\n";
message += $"C:/Users/Admin > ping {destiny}\r\n\r\n";
message += $"Pinging {destiny} with 32 bytes of Data:\r\n";
message += $"Reply from {destiny}: bytes=32 time=10ms TTL=126\r\n";
message += $"Reply from {destiny}: bytes=32 time=27ms TTL=126\r\n";
message += $"Reply from {destiny}: bytes=32 time=10ms TTL=126\r\n";
message += $"Reply from {destiny}: bytes=32 time=10ms TTL=126\r\n\r\n";
message += $"Ping Statistics for {destiny}:\r\n";
message += $" Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\n";
message += $"Approximate round trip times in milli-seconds:\r\n";
message += $" Minimum = 10ms, Maximum = 27ms, Average = 15ms\r\n\r\n";
message += $" PING SUCCESSFULLY :)";
return message;
}
}
// Router class
public class Router : NetworkDevices
{
public Router(string name, MACAddress macaddress) : base(name)
{
MACAddress = macaddress;
}
public override void SendPacket(Packet packet, Interface sourceInterface)
{
// Implement routing logic here
// Find the destination interface based on the MAC address
var destinationInterface = Interfaces.FirstOrDefault(i =>
i.MACAddress.Address.SequenceEqual(packet.DestinationMACAddress.Address)
);
if (destinationInterface != null)
{
// Send the packet to the destination interface
destinationInterface.SendPacket(packet);
}
else
{
// If the destination interface is not found, send the packet to all interfaces except the source interface
foreach (var iface in Interfaces.Where(i => i != sourceInterface))
{
iface.SendPacket(packet);
}
}
}
public bool receivepacket(Packet packet)
{
if (packet.DestinationIPAddress == IPS)
{
return true;
}
else
{
return false;
}
}
}
// Switch class
public class Switch : NetworkDevices
{
public List<MACAddress> Table { get; set; }
public Switch(string name, MACAddress macaddress) : base(name)
{
MACAddress = macaddress;
Table = new List<MACAddress>();
}
public void LearnAddress(MACAddress address, Interface iface)
{
Table.Add(address);
}
public override void SendPacket(Packet packet, Interface sourceInterface)
{
// Implement switching logic here
// Find the destination interface based on the MAC address
var destinationInterface = Interfaces.FirstOrDefault(i =>
i.MACAddress.Address.SequenceEqual(packet.DestinationMACAddress.Address)
);
if (destinationInterface != null)
{
// Send the packet to the destination interface
destinationInterface.SendPacket(packet);
}
else
{
// If the destination interface is not found, send the packet to all interfaces except the source interface
foreach (var iface in Interfaces.Where(i => i != sourceInterface))
{
iface.SendPacket(packet);
}
}
}
public bool receivepacket(Packet packet)
{
if (packet.DestinationIPAddress == IPS)
{
return true;
}
else
{
return false;
}
}
}
// PC class
public class PC : NetworkDevices
{
public PC(string name, MACAddress macAddress) : base(name)
{
MACAddress = macAddress;
}
public override void SendPacket(Packet packet, Interface sourceInterface)
{
Switch sw = new Switch("Switch", new MACAddress(new byte[] { 0x00, 0x99, 0xBB, 0xCC, 0xDD, 0xEE }));
sw.SendPacket(packet, sourceInterface);
}
}
// Interface class
public class Interface
{
public string Name { get; set; }
public string IPAddress { get; set; }
public MACAddress MACAddress { get; set; }
public bool LinkUp { get; set; }
public Interface()
{
LinkUp = true;
}
public void SendPacket(Packet packet)
{
// Unable to Implement it
}
// Set the interface for PCs
public void set_inter(PC p, string ip)
{
Name = p.Name;
IPAddress = ip;
MACAddress = p.MACAddress;
}
// Set the interface for Switch & Router
public void set_inter1(string name, string ip, MACAddress MAC)
{
Name = name;
IPAddress = ip;
MACAddress = MAC;
}
// Receiving the packets
public bool receivepacket(Packet packet)
{
if (packet.DestinationIPAddress == IPAddress)
{
return true;
}
else
{
return false;
}
}
// Show whether the device is ON or OFF
public void SetLinkStatus(bool linkUp)
{
LinkUp = linkUp;
}
}
// Packet class
public class Packet
{
public MACAddress SourceMACAddress { get; set; }
public MACAddress DestinationMACAddress { get; set; }
public string SourceIPAddress { get; set; }
public string DestinationIPAddress { get; set; }
public string Data { get; set; }
public void set_info(Interface spc, Interface dpc, string data)
{
SourceMACAddress = spc.MACAddress;
DestinationMACAddress = dpc.MACAddress;
SourceIPAddress = spc.IPAddress;
DestinationIPAddress = dpc.IPAddress;
Data = data;
}
}
// MACAddress class
public class MACAddress
{
public byte[] Address { get; set; }
public MACAddress(byte[] address)
{
Address = address;
}
public override string ToString()
{
return BitConverter.ToString(Address).Replace("-", ":");
}
}
}