-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pax.cs
399 lines (345 loc) · 14.2 KB
/
Pax.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
Pax : tool support for prototyping packet processors
Nik Sultana, Cambridge University Computer Lab, June 2016
Jonny Shipton, Cambridge University Computer Lab, July 2016
Use of this source code is governed by the Apache 2.0 license; see LICENSE.
*/
using System;
using System.Net.NetworkInformation;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PacketDotNet;
using SharpPcap;
using SharpPcap.LibPcap;
using Newtonsoft.Json;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Reflection;
using Mono.Options;
using System.Threading;
namespace Pax
{
public class Frontend {
private static void usage() {
Console.WriteLine("Required parameters: config file, and DLL containing packet handlers.");
}
private static void print_kv (string k, string v, bool secondary = false) {
var tmp = Console.ForegroundColor;
if (secondary)
Console.ForegroundColor = ConsoleColor.Gray;
else
Console.ForegroundColor = ConsoleColor.White;
Console.Write(k);
if (secondary)
Console.ForegroundColor = ConsoleColor.DarkYellow;
else
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(v);
//Console.ResetColor();
Console.ForegroundColor = tmp;
}
private static void print_heading (string s) {
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.White;
Console.Write(s);
Console.ResetColor();
Console.WriteLine();
}
private const string indent = " ";
public static int Main (string[] args) {
Console.ResetColor();
// FIXME when we load the DLL and wiring.cfg, check them against each other (e.g., that all handlers exist)
// we can resolve all the "links" (by looking at return statements) and draw a wiring diagram. -- this can be a script.
#if DEBUG
Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));
#endif
OptionSet p = new OptionSet ()
.Add ("v", _ => PaxConfig.opt_verbose = true);
args = p.Parse(args).ToArray();
if (args.Length < 2)
{
usage();
return -1;
}
// FIXME use getopts to get parameters
PaxConfig.config_filename = args[0];
PaxConfig.assembly_filename = args[1];
// FIXME currently this is redundant, but it will be used after getopts gets used in the future.
// These two parameters are essential for Pax to function.
if (String.IsNullOrEmpty(PaxConfig.config_filename) ||
String.IsNullOrEmpty(PaxConfig.assembly_filename))
{
usage();
return -1;
}
PrintIntro();
PaxConfig.assembly = Assembly.LoadFile(PaxConfig.assembly_filename);
var devices = CaptureDeviceList.Instance;
Debug.Assert (devices.Count >= 0);
if (devices.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("No capture devices found");
return -1;
}
Configure(devices);
LoadExternalHandlersFromDll();
RegisterHandlers();
print_heading("Starting");
// FIXME accept a -j parameter to limit number of threads?
foreach (var device in PaxConfig.deviceMap)
device.StartCapture();
return 0;
}
private static void PrintIntro()
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write ("✌ ");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write ("Pax v{0}", "0.1"/*FIXME const -- get value from AssemblyInfo*/);
Console.ForegroundColor = ConsoleColor.White;
Console.Write (" ☮ ");
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write ("http://www.cl.cam.ac.uk/~ns441/pax");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
if (PaxConfig.opt_verbose) {
print_kv (indent + "running as ",
(System.Environment.Is64BitProcess ? "64bit" : "32bit") + " process",
true);
print_kv (indent + "on .NET runtime v", System.Environment.Version.ToString(),
true);
print_kv (indent + "OS is ",
System.Environment.OSVersion.ToString() +
" (" +
(System.Environment.Is64BitOperatingSystem ? "64bit" : "32bit")
+ ")",
true);
print_kv (indent + "OS running for (ticks) ", System.Environment.TickCount.ToString(),
true);
print_kv (indent + "No. processors available ", System.Environment.ProcessorCount.ToString(),
true);
}
print_kv ("Using configuration file: ", PaxConfig.config_filename);
print_kv ("Using assembly file: ", PaxConfig.assembly_filename);
}
private static void Configure(CaptureDeviceList devices)
{
print_heading("Configuration");
using (JsonTextReader r = new JsonTextReader(File.OpenText(PaxConfig.config_filename))) {
JsonSerializer j = new JsonSerializer();
j.DefaultValueHandling = DefaultValueHandling.Populate;
PaxConfig.configFile = j.Deserialize<ConfigFile>(r);
PaxConfig_Lite.no_interfaces = PaxConfig.config.Count;
PaxConfig.deviceMap = new ICaptureDevice[PaxConfig_Lite.no_interfaces];
PaxConfig.interface_lead_handler = new string[PaxConfig_Lite.no_interfaces];
PaxConfig.interface_lead_handler_obj = new IPacketProcessor[PaxConfig_Lite.no_interfaces];
int idx = 0;
foreach (var i in PaxConfig.config) {
Debug.Assert (idx < PaxConfig_Lite.no_interfaces);
PaxConfig.deviceMap[idx] = null;
//FIXME not using internal_name any more to avoid more lookups. Remove completely?
//Console.Write(indent + i.internal_name);
//Console.Write(" -- ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(indent + "[");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(idx.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("] ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(i.interface_name);
PaxConfig.interface_lead_handler[idx] = i.lead_handler;
foreach (var device in devices)
{
if (device.Name == i.interface_name)
{
PaxConfig.deviceMap[idx] = device;
PaxConfig.rdeviceMap.Add(device.Name, idx);
// Configure this device's read timeout
device.Open(DeviceMode.Normal, i.read_timeout);
if (!String.IsNullOrEmpty(i.pcap_filter))
{
device.Filter = i.pcap_filter;
print_kv (indent + /*FIXME code style sucks*/ indent +
"Setting filter: ", i.pcap_filter);
}
break;
}
}
if (PaxConfig.deviceMap[idx] == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("No match for '" + i.interface_name + "'");
Environment.Exit(-1);
} else {
print_kv (indent + /*FIXME code style sucks*/ indent +
"Link layer type: ", PaxConfig.deviceMap[idx].LinkType.ToString());
}
idx++;
}
}
}
private static void LoadExternalHandlersFromDll()
{
print_heading("Scanning assembly");
// Inspect each type that implements PacketProcessor, trying to instantiate it for use
foreach (Type type in PaxConfig.assembly.GetExportedTypes()
.Where(typeof(IPacketProcessor).IsAssignableFrom))
{
// Find which network interfaces this class is handling
List<int> subscribed = new List<int>();
IPacketProcessor pp = null;
for (int idx = 0; idx < PaxConfig_Lite.no_interfaces; idx++)
{
// Does this interface have this type specified as the lead handler?
if ((!String.IsNullOrEmpty(PaxConfig.interface_lead_handler[idx])) &&
type.Name == PaxConfig.interface_lead_handler[idx])
{
// Only instantiate pp if needed
if (pp == null)
pp = InstantiatePacketProcessor(type);
if (pp == null)
// If pp is still null, then we couldn't instantiate it.
break;
subscribed.Add(idx);
PaxConfig.interface_lead_handler_obj[idx] = pp;
}
}
Console.Write (indent);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write (type);
if (PaxConfig.opt_verbose) {
// List the Pax interfaces this type implements:
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" : ");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(String.Join(", ", PacketProcessorHelper.GetUsedPaxTypes(type).Select(t => t.Name)));
}
// Print which interfaces this type is the handler for
if (subscribed.Count != 0) {
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write (" <- ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(
String.Join(", ", subscribed.ConvertAll<string>(ofs => PaxConfig.deviceMap[ofs].Name)));
}
Console.WriteLine("");
}
// FIXME add check to see if there's an interface that references a lead_handler that doesn't appear in the assembly. That should be flagged up to the user, and lead to termination of Pax.
}
private static IPacketProcessor InstantiatePacketProcessor(Type type)
{
#if MOREDEBUG
Console.WriteLine("Trying to instantiate {0}", type);
#endif
// Get the constructor arguments for this type from the config
IDictionary<string,string> arguments =
PaxConfig.configFile.handlers?.Where(handler => type.Name.Equals(handler.class_name))
.Select(intf => intf.args)
.SingleOrDefault();
if (arguments == null) arguments = new Dictionary<string,string>();
#if MOREDEBUG
Console.WriteLine(" Arguments:");
foreach (var pair in arguments)
Console.WriteLine(" {0} : {1}", pair.Key, pair.Value);
Console.WriteLine(" Public constructors:");
foreach (var ctor in type.GetConstructors(BindingFlags.Instance | BindingFlags.Public))
Console.WriteLine(" {0}", PacketProcessorHelper.ConstructorString(ctor));
#endif
// Instantiate the packet processor
IPacketProcessor pp = PacketProcessorHelper.InstantiatePacketProcessor(type, arguments);
if (pp == null)
Console.WriteLine("Couldn't instantiate {0}", type.FullName);
return pp;
}
private static void RegisterHandlers()
{
Console.ForegroundColor = ConsoleColor.Gray;
// Set up callbacks.
Task.Factory.StartNew(() =>
{
while (true)
{
var key = Console.ReadKey();
// Shutdown on ^D
if ((key.Modifiers.HasFlag(ConsoleModifiers.Control) && key.Key == ConsoleKey.D)
|| (int)key.KeyChar == 4)
{
shutdown(null, null);
}
}
});
// Shutdown on ^C -- FIXME remove handling of ^C or ^D?
Console.CancelKeyPress += new ConsoleCancelEventHandler(shutdown);
for (int idx = 0; idx < PaxConfig_Lite.no_interfaces; idx++)
{
if (PaxConfig.interface_lead_handler_obj[idx] == null)
{
var tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("No packet processor for ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(PaxConfig.deviceMap[idx].Name);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" (");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(idx);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(")");
Console.ForegroundColor = tmp;
// FIXME create a custom exception subtype for this situation.
throw new Exception("Could not instantiate '" +
PaxConfig.config[idx].lead_handler +
"' -- perhaps not a packet processor?");
} else {
var tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Registered packet processor for ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(PaxConfig.deviceMap[idx].Name);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" (");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(idx);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(")");
Console.ForegroundColor = tmp;
PaxConfig.deviceMap[idx].OnPacketArrival +=
PaxConfig.interface_lead_handler_obj[idx].packetHandler;
// If the packet processor is "active" then start it.
if (PaxConfig.interface_lead_handler_obj[idx] is IActive) {
var o = PaxConfig.interface_lead_handler_obj[idx] as IActive;
o.PreStart (PaxConfig.deviceMap[idx]);
Thread t = new Thread (new ThreadStart (o.Start));
t.Start();
}
}
}
}
//Cleanup
private static void shutdown (object sender, ConsoleCancelEventArgs args)
{
for (int idx = 0; idx < PaxConfig_Lite.no_interfaces; idx++)
{
// If the packet processor is "active" then stop it now.
if (PaxConfig.interface_lead_handler_obj[idx] is IActive) {
((IActive)PaxConfig.interface_lead_handler_obj[idx]).Stop();
}
// Set the capture timeout, as without the program can hang indefinitely.
// Cause unknown, but setting any timeout seems to fix it. Even with timeout
// of 1s, the program shuts down immediately.
PaxConfig.deviceMap[idx].StopCaptureTimeout = TimeSpan.FromSeconds(1);
PaxConfig.deviceMap[idx].Close();
}
Console.ResetColor();
Console.WriteLine ("Terminating");
}
public static void shutdown () {
shutdown(null, null);
}
}
}