-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathICMPio.cs
113 lines (95 loc) · 3.94 KB
/
ICMPio.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
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Threading;
// Run these for it to be able to work on receiving computers as admin:
// netsh advfirewall firewall add rule name="pingListener IPv4" dir=in action=allow protocol=icmpv4:any,any
// netsh advfirewall firewall add rule name="pingListener IPv6" dir=in action=allow protocol=icmpv6:any,any
/* ICMP Packet sender and receiver for portless communication
Copyright (C) 2016 Lesley De Keyser
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace ICMPio.Test
{
class ICMPio
{
public delegate void onPingReceiveHandler(IPAddress remoteEndPoint, int packetSize, byte[] packetBody);
public event onPingReceiveHandler onPingReceive;
private Socket ICMPListener = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
private IPAddress ip;
public bool running = false;
public ICMPio(IPAddress localIp)
{
ip = localIp;
}
public void Start()
{
Thread threadLoop = new Thread(loop);
threadLoop.Start();
}
private void loop()
{
ICMPListener.Bind(new IPEndPoint(ip, 0));
ICMPListener.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, new byte[] { 1, 0, 0, 0 });
running = true;
while(running) {
byte[] buffer = new byte[65575]; //The max ping packet size including the header of an IPv6 address.
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
int bytesRead = ICMPListener.ReceiveFrom(buffer, ref remoteEP);
byte[] fullPacket = buffer.Take(bytesRead).ToArray(); //buffer is 66575, it'll most likely be bigger than what we actually received
byte[] body = fullPacket.Skip(fullPacket.Length - 28).ToArray(); //Take all bytes but the 28 first (28 = 20 (ip address) + 8 (icmp header packet)
byte type = fullPacket[20];
//byte code = fullPacket[21];
if (type == 0x08) //request, reply = 0x00, we only want the requests to us.
{
string remoteIP = StripPortFromEndPoint(remoteEP.ToString());
onPingReceive(IPAddress.Parse(remoteIP), bytesRead, body);
}
};
}
public void Stop()
{
running = false;
}
private string StripPortFromEndPoint(string endPoint)
{
var splitList = endPoint.Split(':');
if (splitList.Length > 2)
{
endPoint = IPAddress.Parse(endPoint).ToString();
}
else if (splitList.Length == 2)
{
endPoint = splitList[0];
}
else
{
}
return endPoint;
}
public byte[] sendPacket(IPAddress end, byte[] body)
{
Ping pingSender = new Ping();
PingReply reply = pingSender.Send(end);
if(reply.Status == IPStatus.Success)
{
return reply.Buffer;
}else
{
Exception x = new PingException(reply.Status.ToString());
return null;
}
}
}
}