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
#include "protoApp.h"
#include "protoDetour.h"
#include "protoPktIP.h"
#include <stdlib.h> // for atoi()
#include <stdio.h> // for stdout/stderr printouts
#include <string.h>
#include "protoBase64.h"
/**
* @class DetourExample
*
* @brief An example using a ProtoDetour class instance to intercept
* outbound packets
*/
class DetourExample : public ProtoApp
{
public:
DetourExample();
~DetourExample();
// Overrides from ProtoApp or NsProtoSimAgent base
bool OnStartup(int argc, const char*const* argv);
bool ProcessCommands(int argc, const char*const* argv);
void OnShutdown();
private:
void DetourEventHandler(ProtoChannel& theChannel,
ProtoChannel::Notification theNotification);
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG};
static const char* const CMD_LIST[];
static CmdType GetCmdType(const char* string);
bool OnCommand(const char* cmd, const char* val);
void Usage();
ProtoDetour* detour;
bool ipv6_mode;
bool allow; // toggled variable for test
}; // end class DetourExample
const char* const DetourExample::CMD_LIST[] =
{
"-ipv6", // IPv6 test (instead of IPv4)
NULL
};
// This macro creates our ProtoApp derived application instance
PROTO_INSTANTIATE_APP(DetourExample)
DetourExample::DetourExample()
: detour(NULL), ipv6_mode(false), allow(true)
{
}
DetourExample::~DetourExample()
{
}
void DetourExample::Usage()
{
#ifdef WIN32
fprintf(stderr, "detourExample [ipv6][background]\n");
#else
fprintf(stderr, "detourExample\n");
#endif // if/else WIN32/UNIX
} // end DetourExample::Usage()
DetourExample::CmdType DetourExample::GetCmdType(const char* cmd)
{
if (!cmd) return CMD_INVALID;
unsigned int len = strlen(cmd);
bool matched = false;
CmdType type = CMD_INVALID;
const char* const* nextCmd = CMD_LIST;
while (*nextCmd)
{
if (!strncmp(cmd, *nextCmd+1, len))
{
if (matched)
{
// ambiguous command (command should match only once)
return CMD_INVALID;
}
else
{
matched = true;
if ('+' == *nextCmd[0])
type = CMD_ARG;
else
type = CMD_NOARG;
}
}
nextCmd++;
}
return type;
} // end DetourExample::GetCmdType()
bool DetourExample::OnStartup(int argc, const char*const* argv)
{
SetDebugLevel(3);
if (!ProcessCommands(argc, argv))
{
PLOG(PL_ERROR, "detourExample::OnStartup() error processing command line options\n");
return false;
}
// Create our cap_rcvr instance and initialize ...
if (!(detour = ProtoDetour::Create()))
{
PLOG(PL_ERROR, "detourExample::OnStartup() new ProtoDetour error: %s\n", GetErrorString());
return false;
}
detour->SetNotifier(static_cast<ProtoChannel::Notifier*>(&dispatcher));
detour->SetListener(this, &DetourExample::DetourEventHandler);
// Set detour filter for outbound multicast packets
ProtoAddress srcFilter;
ProtoAddress dstFilter;
unsigned int dstFilterMask;
if (ipv6_mode)
{
srcFilter.Reset(ProtoAddress::IPv6); // unspecified address
dstFilter.ResolveFromString("ff00::");
dstFilterMask = 8;
}
else
{
srcFilter.Reset(ProtoAddress::IPv4); // unspecified address
dstFilter.ResolveFromString("239.0.0.0");
dstFilterMask = 4;
}
if (!detour->Open(ProtoDetour::INPUT, srcFilter, 0, dstFilter, dstFilterMask))
{
PLOG(PL_ERROR, "detourExample::OnStartup() ProtoDetour::Open() error\n");
}
#ifdef NEVER //HAVE_SCHED
// Boost process priority for real-time operation
// (This _may_ work on Linux-only at this point)
struct sched_param schp;
memset(&schp, 0, sizeof(schp));
schp.sched_priority = sched_get_priority_max(SCHED_FIFO);
if (sched_setscheduler(0, SCHED_FIFO, &schp))
{
schp.sched_priority = sched_get_priority_max(SCHED_OTHER);
if (sched_setscheduler(0, SCHED_OTHER, &schp))
PLOG(PL_ERROR, "detourExample: Warning! Couldn't set any real-time priority: %s\n", GetErrorString());
}
#endif // HAVE_SCHED
#ifdef WIN32
#ifndef _WIN32_WCE
if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS))
PLOG(PL_ERROR, "detourExample: Warning! SetPriorityClass() error: %s\n", GetErrorString());
#endif // !_WIN32_WCE
if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL))
PLOG(PL_ERROR, "detourExample: Warning! SetThreadPriority() error: %s\n", GetErrorString());
#endif // WIN32
TRACE("startup returning control to dispatcher ...\n");
return true;
} // end DetourExample::OnStartup()
void DetourExample::OnShutdown()
{
if (NULL != detour)
{
detour->Close();
delete detour;
detour = NULL;
}
PLOG(PL_ERROR, "detourExample: Done.\n");
CloseDebugLog();
} // end DetourExample::OnShutdown()
bool DetourExample::ProcessCommands(int argc, const char*const* argv)
{
// Dispatch command-line commands to our OnCommand() method
int i = 1;
while ( i < argc)
{
// Is it a class DetourExample command?
switch (GetCmdType(argv[i]))
{
case CMD_INVALID:
{
PLOG(PL_ERROR, "DetourExample::ProcessCommands() Invalid command:%s\n",
argv[i]);
return false;
}
case CMD_NOARG:
if (!OnCommand(argv[i], NULL))
{
PLOG(PL_ERROR, "DetourExample::ProcessCommands() ProcessCommand(%s) error\n",
argv[i]);
return false;
}
i++;
break;
case CMD_ARG:
if (!OnCommand(argv[i], argv[i+1]))
{
PLOG(PL_ERROR, "DetourExample::ProcessCommands() ProcessCommand(%s, %s) error\n",
argv[i], argv[i+1]);
return false;
}
i += 2;
break;
}
}
return true;
} // end DetourExample::ProcessCommands()
bool DetourExample::OnCommand(const char* cmd, const char* val)
{
// (TBD) move command processing into Mgen class ???
CmdType type = GetCmdType(cmd);
ASSERT(CMD_INVALID != type);
size_t len = strlen(cmd);
if ((CMD_ARG == type) && !val)
{
PLOG(PL_ERROR, "DetourExample::ProcessCommand(%s) missing argument\n", cmd);
return false;
}
else if (!strncmp("ipv6", cmd, len))
{
ipv6_mode = true;
}
else
{
PLOG(PL_ERROR, "detourExample:: invalid command\n");
return false;
}
return true;
} // end DetourExample::OnCommand()
void DetourExample::DetourEventHandler(ProtoChannel& theChannel,
ProtoChannel::Notification theNotification)
{
if (ProtoChannel::NOTIFY_INPUT == theNotification)
{
UINT32 buffer[8192/4];
unsigned int numBytes = 8192;
if (detour->Recv((char*)buffer, numBytes))
{
TRACE("detour recv'd packet ...\n");
if (0 != numBytes)
{
allow = true;
if (allow)
{
TRACE("detour allowing packet len:%u\n", numBytes);
ProtoPktIP ipPkt(buffer, numBytes);
ipPkt.InitFromBuffer(numBytes);
// This some example code that "translates" the dst addr
// of certain outbound IP packets 224.0.0.251 -> 224.1.2.3
// (These are MDNS packets, by the way)
ProtoAddress dstAddr;
ipPkt.GetDstAddr(dstAddr);
ProtoAddress matchAddress;
matchAddress.ResolveFromString("224.0.0.251");
ProtoAddress goalAddress;
goalAddress.ResolveFromString("224.1.2.3");
if (dstAddr.HostIsEqual(matchAddress))
{
ipPkt.SetDstAddr(goalAddress);
// Need to update UDP checksum if UDP packet
// (yucky cross-layer UDP checksum)
ProtoPktUDP udpPkt;
if (udpPkt.InitFromPacket(ipPkt))
udpPkt.FinalizeChecksum(ipPkt);
}
// Some UDP packet manipulation code
ProtoPktUDP udpPkt;
if (udpPkt.InitFromPacket(ipPkt))// && (5000 == udpPkt.GetDstPort()))
{
if (udpPkt.ChecksumIsValid(ipPkt))
{
TRACE(" UDP with valid checksum %04x (computed: %04x)\n",
udpPkt.GetChecksum(), udpPkt.ComputeChecksum(ipPkt));
}
else
{
TRACE(" UDP with INVALID checksum %04x (computed: %04x)\n",
udpPkt.GetChecksum(), udpPkt.ComputeChecksum(ipPkt));
ProtoAddress srcAddr;
ipPkt.GetSrcAddr(srcAddr);
TRACE(" src:%s\n", srcAddr.GetHostString());
TRACE(" dst:%s\n", dstAddr.GetHostString());
ProtoPktIPv4 ipv4Pkt(ipPkt);
TRACE(" proto:%d\n", ipv4Pkt.GetProtocol());
/*
char base64Buffer[8192];
unsigned int length = ProtoBase64::Encode((char*)buffer, numBytes, base64Buffer, 8192);
TRACE("packet(base64)=\n %s\n", base64Buffer);
*/
for (unsigned int i = 0; i < numBytes; i++)
{
if (0 == i)
;
else if (0 == (i%16))
TRACE("\n");
else if (0 == (i%2))
TRACE(" ");
unsigned char* ptr = (unsigned char*)buffer;
TRACE("%02x", ptr[i]);
}
}
}
detour->Allow((char*)buffer, numBytes);
//allow = false; // uncomment this to drop every other packet
}
else
{
TRACE("detour dropping/injecting packet ...\n");
detour->Drop();
detour->Inject((char*)buffer, numBytes);
allow = true;
}
}
numBytes = 8192;
}
}
} // end DetourExample::DetourEventHandler()