forked from DebugST/STPortScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy of SYNScanner.cs
More file actions
executable file
·257 lines (238 loc) · 11 KB
/
Copy pathCopy of SYNScanner.cs
File metadata and controls
executable file
·257 lines (238 loc) · 11 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ST.Library.Network
{
public class SYNScanner : PortScanner
{
private Random m_rnd;
private Semaphore m_se;
private ProbeConfiger m_probes;
private Queue<SYNScanTaskInfo> m_que_task;
private Queue<SocketAsyncEventArgs> m_que_sae;
private TCPScanner m_tcp_scanner;
private Dictionary<uint, SYNScanTaskInfo> m_dic_task_running;
//private Dictionary<uint, uint> m_dic_uid;
private Dictionary<uint, SYNScanTaskInfo> m_dic_uid;
private Thread m_thread_timeout;
private Socket m_sock_raw;
private Socket m_sock_bind;
private uint m_uLocalIP;
private string m_strLocalIP;
private ushort m_nLocalPort;
public SYNScanner(int nMaxTask, ProbeConfiger probes) : this(nMaxTask, probes, null) { }
public SYNScanner(int nMaxTask, ProbeConfiger probes, EndPoint bindEndPoint) {
if (nMaxTask > 60000 || nMaxTask < 1) throw new ArgumentOutOfRangeException("the MaxTask must be between 1 and 30000");
m_probes = probes;
if (bindEndPoint == null) {
foreach (var v in Dns.GetHostAddresses(Dns.GetHostName())) {
if (v.IsIPv6LinkLocal || v.IsIPv6Multicast || v.IsIPv6SiteLocal) continue;
bindEndPoint = new IPEndPoint(v, 0);
}
}
m_rnd = new Random();
m_dic_uid = new Dictionary<uint, SYNScanTaskInfo>();// new Dictionary<uint, uint>();
m_dic_task_running = new Dictionary<uint, SYNScanTaskInfo>();
m_tcp_scanner = new TCPScanner(nMaxTask, probes);
m_tcp_scanner.Completed += new ScanEventHandler(m_tcp_Completed);
m_sock_bind = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_sock_bind.Bind(bindEndPoint);
bindEndPoint = m_sock_bind.LocalEndPoint;
m_strLocalIP = bindEndPoint.ToString().Split(':')[0];
m_uLocalIP = RAWDefine.IPToINT(m_strLocalIP);
m_nLocalPort = ushort.Parse(bindEndPoint.ToString().Split(':')[1]);
m_se = new Semaphore(nMaxTask, nMaxTask);
m_sock_raw = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
m_sock_raw.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
m_sock_raw.Bind(bindEndPoint);
m_sock_raw.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, null);
m_que_task = new Queue<SYNScanTaskInfo>();
m_que_sae = new Queue<SocketAsyncEventArgs>();
for (int i = 0; i < nMaxTask; i++) {
SYNScanTaskInfo ti = new SYNScanTaskInfo();
ti.TaskID = (uint)((i + 1) << 8);
ti.SYNPacket = new byte[40];
m_que_task.Enqueue(ti);
}
SocketAsyncEventArgs sae = new SocketAsyncEventArgs();
sae.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
sae.SetBuffer(new byte[65535], 0, 65535);
sae.UserToken = m_sock_raw;
if (!m_sock_raw.ReceiveAsync(sae)) IOProcessPool.QueueWork(this.ProcessRecv, sae);
m_thread_timeout = new Thread(this.CheckTimeout);
m_thread_timeout.IsBackground = true;
m_thread_timeout.Start();
}
void m_tcp_Completed(object sender, ScanEventArgs e) {
uint uid = e.TaskID;
SYNScanTaskInfo ti = null;
lock (m_dic_uid) {
if (!m_dic_uid.ContainsKey(e.TaskID)) return;
//e.TaskID = m_dic_uid[e.TaskID];
ti = m_dic_uid[e.TaskID];
e.TaskID = ti.TaskID;
m_dic_uid.Remove(uid);
if (base._IsDisposed) return;
m_que_task.Enqueue(ti);
}
//lock (m_obj_sync) {
//}
base.OnCompleted(e);
m_se.Release();
}
private SocketAsyncEventArgs PopSAE() {
lock (m_obj_sync) {
if (m_que_sae.Count != 0) return m_que_sae.Dequeue();
}
SocketAsyncEventArgs sae = new SocketAsyncEventArgs();
sae.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
sae.SetBuffer(new byte[40], 0, 40);
return sae;
}
private void PushSAE(SocketAsyncEventArgs sae) {
lock (m_obj_sync) {
if (base._IsDisposed) return;
m_que_sae.Enqueue(sae);
}
}
protected override uint OnScan(int nPort, EndPoint endPoint, int nProbes, int nTimeout, int nRetry, int nTotalTimeout, bool bUseNullProbes) {
lock (m_obj_sync) {
if (base._IsDisposed) throw new ObjectDisposedException("SYNScanner", "The scanner was disposed");
}
m_se.WaitOne();
SYNScanTaskInfo ti = this.CreateTaskInfo(nPort, endPoint, nProbes, nTimeout, nRetry, nTotalTimeout, bUseNullProbes);
lock (m_dic_task_running) {
m_dic_task_running.Add(ti.TaskID, ti);
}
this.SendData(ti);
ti.IsStarted = true;
return ti.TaskID;
}
private void SendData(SYNScanTaskInfo ti) {
SocketAsyncEventArgs sae = this.PopSAE();
Array.Copy(ti.SYNPacket, sae.Buffer, ti.SYNPacket.Length);
ti.LastTime = DateTime.Now;
sae.RemoteEndPoint = ti.EndPoint;
if (!m_sock_raw.SendToAsync(sae)) IOProcessPool.QueueWork(this.ProcessSend, sae);
}
private SYNScanTaskInfo CreateTaskInfo(int nPort, EndPoint endPoint, int nProbes, int nTimeout, int nRetry, int nTotalTimeout, bool bUseNullProbes) {
SYNScanTaskInfo ti = null;
lock (m_obj_sync) {
ti = m_que_task.Dequeue();
}
ti.Retry = nRetry;
ti.RunedRetry = 0;
ti.Port = nPort;
ti.EndPoint = endPoint;
ti.IsStarted = false;
ti.Probes = nProbes;
ti.IsUseNullProbe = bUseNullProbes;
ti.Timeout = nTimeout;
ti.TotalTimeout = nTotalTimeout;
ti.UIP = BitConverter.ToUInt32(((IPEndPoint)endPoint).Address.GetAddressBytes(), 0);
uint uTemp = 0;
lock (m_rnd) uTemp = (uint)m_rnd.Next();
uTemp &= 0xFF0000FF;
ti.TaskID = ti.TaskID & 0x00FFFF00 | uTemp;
ti.SEQ = RAWDefine.GetSynPacket(ti.SYNPacket, m_uLocalIP, ti.UIP, m_nLocalPort, (ushort)ti.Port, ti.TaskID);
return ti;
}
void IO_Completed(object sender, SocketAsyncEventArgs e) {
switch (e.LastOperation) {
case SocketAsyncOperation.SendTo:
this.ProcessSend(e);
break;
case SocketAsyncOperation.Receive:
this.ProcessRecv(e);
break;
}
}
private void ProcessSend(SocketAsyncEventArgs e) {
this.PushSAE(e);
}
private void ProcessRecv(SocketAsyncEventArgs e) {
lock (m_obj_sync) {
if (base._IsDisposed) return;
}
Socket sock = e.UserToken as Socket;
if (e.SocketError == SocketError.Success && e.BytesTransferred > 0) {
bool b = true;
uint uSIP = BitConverter.ToUInt32(e.Buffer, 16);
int nOffset = (e.Buffer[0] & 0x0F) * 4;
uint uSEQ = RAWDefine.GetACKNumber(e.Buffer, nOffset) - 1;
if (e.BytesTransferred < 40) b = false;
else if (nOffset < 20 || e.BytesTransferred - 20 < nOffset) b = false;
else if (e.Buffer[9] != RAWDefine.PROTO_TCP) b = false;
else if ((ushort)((e.Buffer[nOffset + 2] << 8) | e.Buffer[nOffset + 3]) != m_nLocalPort) b = false;
else if (e.Buffer[nOffset + 13] != 0x12) b = false;//syn + ack
else if (uSIP != m_uLocalIP) b = false;
SYNScanTaskInfo ti = null;
if (b) {
lock (m_dic_task_running) {
if (m_dic_task_running.ContainsKey(uSEQ)) {
ti = m_dic_task_running[uSEQ];
m_dic_task_running.Remove(uSEQ);
}
}
}
if (ti != null) {
uint id = m_tcp_scanner.Scan(ti.UIP, ti.Port, ti.Probes, ti.Timeout, ti.Retry, ti.TotalTimeout, ti.IsUseNullProbe);
lock (m_dic_uid) m_dic_uid.Add(id, ti);
}
}
if (!sock.ReceiveAsync(e)) IOProcessPool.QueueWork(this.ProcessRecv, e);
}
private void EndTask(SYNScanTaskInfo ti) {
ti.IsStarted = false;
lock (m_dic_task_running) {
if (!m_dic_task_running.ContainsKey(ti.TaskID)) return;
m_dic_task_running.Remove(ti.TaskID);
}
lock (m_obj_sync) {
if (base._IsDisposed) return;
m_que_task.Enqueue(ti);
}
base.OnCompleted(new ScanEventArgs(ti.TaskID, ti.EndPoint, "ACK timeout"));
m_se.Release();
}
private void CheckTimeout() {
DateTime dt = DateTime.Now;
List<SYNScanTaskInfo> lst_remove = new List<SYNScanTaskInfo>();
while (true) {
Thread.Sleep(1000);
lst_remove.Clear();
dt = DateTime.Now;
bool bDisposed = base._IsDisposed;
lock (m_dic_task_running) {
foreach (var v in m_dic_task_running) {
if (!v.Value.IsStarted) continue;
if (dt.Subtract(v.Value.StartTime).TotalMilliseconds > v.Value.TotalTimeout || bDisposed) {
lst_remove.Add(v.Value);
continue;
}
if (dt.Subtract(v.Value.LastTime).TotalMilliseconds > v.Value.Timeout) {
if (v.Value.Retry-- == 0) this.SendData(v.Value);
else lst_remove.Add(v.Value);
}
}
foreach (var v in lst_remove) {
this.EndTask(v);
}
}
if (bDisposed) break;
}
}
public override void Dispose() {
lock (m_obj_sync) {
if (base.IsDisposed) return;
base._IsDisposed = true;
}
m_tcp_scanner.Dispose();
base.CloseSocket(m_sock_bind);
}
}
}