| 1 |
|
| 2 |
#include "protoApp.h" |
| 3 |
#include "protoCap.h" |
| 4 |
|
| 5 |
#include <stdlib.h> // for atoi() |
| 6 |
#include <stdio.h> // for stdout/stderr printouts |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* @class ProtoCapExample |
| 12 |
* |
| 13 |
* @brief This example uses a ProtoCap class instance to sniff for |
| 14 |
* raw packets on an interface as specified by the "listen" |
| 15 |
* command. Received packets may optionally be "forwarded" |
| 16 |
* to another (or, dangerously, the same) interface. |
| 17 |
* |
| 18 |
* Usage: |
| 19 |
* protoCapExample [listen <interfaceName>][forward <interfaceName>][background (WIN32)] |
| 20 |
*/ |
| 21 |
class ProtoCapExample : public ProtoApp |
| 22 |
{ |
| 23 |
public: |
| 24 |
ProtoCapExample(); |
| 25 |
~ProtoCapExample(); |
| 26 |
|
| 27 |
// Overrides from ProtoApp or NsProtoSimAgent base |
| 28 |
bool OnStartup(int argc, const char*const* argv); |
| 29 |
bool ProcessCommands(int argc, const char*const* argv); |
| 30 |
void OnShutdown(); |
| 31 |
|
| 32 |
private: |
| 33 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 34 |
static const char* const CMD_LIST[]; |
| 35 |
static CmdType GetCmdType(const char* string); |
| 36 |
bool OnCommand(const char* cmd, const char* val); |
| 37 |
void Usage(); |
| 38 |
|
| 39 |
void OnReceive(ProtoChannel& theChannel, |
| 40 |
ProtoChannel::Notification notifyType); |
| 41 |
|
| 42 |
bool OnStatTimeout(ProtoTimer& theTimer); |
| 43 |
|
| 44 |
char rcvr_interface[64]; |
| 45 |
char sndr_interface[64]; |
| 46 |
ProtoCap* cap_rcvr; // used for raw packet reception (and forwarding for same interface) |
| 47 |
ProtoCap* cap_sndr; // used for forwarding to different interface |
| 48 |
ProtoCap* cap_fwdr; // pointer to which cap is forwarding (NULL if not forwarding) |
| 49 |
bool forward; // "true" for forwarding, "false" for bridging |
| 50 |
|
| 51 |
// Stats |
| 52 |
unsigned int recv_count; |
| 53 |
unsigned int send_count; |
| 54 |
unsigned int serr_count; // send error count |
| 55 |
|
| 56 |
ProtoTimer stat_timer; // Stats are printed on timeout |
| 57 |
|
| 58 |
}; // end class ProtoCapExample |
| 59 |
|
| 60 |
// This macro creates our ProtoApp derived application instance |
| 61 |
PROTO_INSTANTIATE_APP(ProtoCapExample) |
| 62 |
|
| 63 |
ProtoCapExample::ProtoCapExample() |
| 64 |
: cap_rcvr(NULL), cap_sndr(NULL), cap_fwdr(NULL), |
| 65 |
forward(false), recv_count(0), send_count(0), serr_count(0) |
| 66 |
|
| 67 |
{ |
| 68 |
stat_timer.SetListener(this, &ProtoCapExample::OnStatTimeout); |
| 69 |
stat_timer.SetInterval(1.0); |
| 70 |
stat_timer.SetRepeat(-1); |
| 71 |
} |
| 72 |
|
| 73 |
ProtoCapExample::~ProtoCapExample() |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
void ProtoCapExample::Usage() |
| 78 |
{ |
| 79 |
#ifdef WIN32 |
| 80 |
fprintf(stderr, "protoCapExample [listen <interfaceName> [{forward | bridge} <interfaceName>]][background]\n"); |
| 81 |
#else |
| 82 |
fprintf(stderr, "protoCapExample [listen <interfaceName> [{forward | bridge} <interfaceName>]]\n"); |
| 83 |
#endif // if/else WIN32/UNIX |
| 84 |
} // end ProtoCapExample::Usage() |
| 85 |
|
| 86 |
|
| 87 |
const char* const ProtoCapExample::CMD_LIST[] = |
| 88 |
{ |
| 89 |
"+listen", // recv raw packets on given interface name |
| 90 |
"+forward", // resend all recvd packets out given interface with my src MAC addr |
| 91 |
"+bridge", // resend all recvd packets unaltered out given interface |
| 92 |
NULL |
| 93 |
}; |
| 94 |
|
| 95 |
ProtoCapExample::CmdType ProtoCapExample::GetCmdType(const char* cmd) |
| 96 |
{ |
| 97 |
if (!cmd) return CMD_INVALID; |
| 98 |
unsigned int len = strlen(cmd); |
| 99 |
bool matched = false; |
| 100 |
CmdType type = CMD_INVALID; |
| 101 |
const char* const* nextCmd = CMD_LIST; |
| 102 |
while (*nextCmd) |
| 103 |
{ |
| 104 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 105 |
{ |
| 106 |
if (matched) |
| 107 |
{ |
| 108 |
// ambiguous command (command should match only once) |
| 109 |
return CMD_INVALID; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
matched = true; |
| 114 |
if ('+' == *nextCmd[0]) |
| 115 |
type = CMD_ARG; |
| 116 |
else |
| 117 |
type = CMD_NOARG; |
| 118 |
} |
| 119 |
} |
| 120 |
nextCmd++; |
| 121 |
} |
| 122 |
return type; |
| 123 |
} // end ProtoCapExample::GetCmdType() |
| 124 |
|
| 125 |
|
| 126 |
bool ProtoCapExample::OnStartup(int argc, const char*const* argv) |
| 127 |
{ |
| 128 |
|
| 129 |
#ifdef _WIN32_WCE |
| 130 |
Sleep(1000); // give our WinCE debug window time to start up |
| 131 |
#endif // _WIN32_WCE |
| 132 |
|
| 133 |
// Get interface name of localAddress |
| 134 |
char ifName[256]; |
| 135 |
ProtoAddress localAddress; |
| 136 |
if (localAddress.ResolveLocalAddress()) |
| 137 |
{ |
| 138 |
ProtoSocket dummySocket(ProtoSocket::UDP); |
| 139 |
unsigned int index = dummySocket.GetInterfaceIndex(localAddress.GetHostString()); |
| 140 |
TRACE("protoCapExample: got interface index:%d for interface: %s\n", |
| 141 |
index, localAddress.GetHostString()); |
| 142 |
|
| 143 |
ifName[255] = '\0'; |
| 144 |
if (dummySocket.GetInterfaceName(index, ifName, 255)) |
| 145 |
{ |
| 146 |
TRACE(" (real interface name: \"%s\")\n", ifName); |
| 147 |
} |
| 148 |
|
| 149 |
ProtoAddress ethAddr; |
| 150 |
if (dummySocket.GetInterfaceAddress(ifName, ProtoAddress::ETH, ethAddr)) |
| 151 |
{ |
| 152 |
TRACE(" (interface MAC addr: \"%s\")\n", ethAddr.GetHostString()); |
| 153 |
} |
| 154 |
|
| 155 |
//index = dummySocket.GetInterfaceIndex(ifName); |
| 156 |
//TRACE("protoCapExample: got interface index:%d for interface: %s\n", index, ifName); |
| 157 |
|
| 158 |
/*ProtoAddress ifAddr; |
| 159 |
if (dummySocket.GetInterfaceAddress(buffer, ProtoAddress::IPv4, ifAddr)) |
| 160 |
{ |
| 161 |
TRACE("protoCapExample: got IPv4 address:%s for interface: %s\n", ifAddr.GetHostString(), buffer); |
| 162 |
} |
| 163 |
|
| 164 |
if (dummySocket.GetInterfaceAddress(buffer, ProtoAddress::IPv6, ifAddr)) |
| 165 |
{ |
| 166 |
TRACE("protoCapExample: got IPv6 address:%s for interface: %s\n", ifAddr.GetHostString(), buffer); |
| 167 |
}*/ |
| 168 |
} |
| 169 |
|
| 170 |
// Create out cap_rcvr instance and initialize ... |
| 171 |
if (!(cap_rcvr = ProtoCap::Create())) |
| 172 |
{ |
| 173 |
PLOG(PL_ERROR, "protoCapExample::OnStartup() new ProtoCap cap_rcvr error: %s\n", GetErrorString()); |
| 174 |
return false; |
| 175 |
} |
| 176 |
cap_rcvr->SetListener(this, &ProtoCapExample::OnReceive); |
| 177 |
cap_rcvr->SetNotifier(static_cast<ProtoChannel::Notifier*>(&dispatcher)); |
| 178 |
|
| 179 |
// Create out cap_sndr instance ... |
| 180 |
if (!(cap_sndr = ProtoCap::Create())) |
| 181 |
{ |
| 182 |
PLOG(PL_ERROR, "protoCapExample::OnStartup() new ProtoCap cap_sndr error: %s\n", GetErrorString()); |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
if (!ProcessCommands(argc, argv)) |
| 187 |
{ |
| 188 |
PLOG(PL_ERROR, "protoCapExample: Error! bad command line\n"); |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
#ifdef HAVE_SCHED |
| 193 |
// Boost process priority for real-time operation |
| 194 |
// (This _may_ work on Linux-only at this point) |
| 195 |
struct sched_param schp; |
| 196 |
memset(&schp, 0, sizeof(schp)); |
| 197 |
schp.sched_priority = sched_get_priority_max(SCHED_FIFO); |
| 198 |
if (sched_setscheduler(0, SCHED_FIFO, &schp)) |
| 199 |
{ |
| 200 |
schp.sched_priority = sched_get_priority_max(SCHED_OTHER); |
| 201 |
if (sched_setscheduler(0, SCHED_OTHER, &schp)) |
| 202 |
PLOG(PL_ERROR, "protoCapExample: Warning! Couldn't set any real-time priority: %s\n", GetErrorString()); |
| 203 |
} |
| 204 |
#endif // HAVE_SCHED |
| 205 |
|
| 206 |
#ifdef WIN32 |
| 207 |
#ifndef _WIN32_WCE |
| 208 |
if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) |
| 209 |
PLOG(PL_ERROR, "protoCapExample: Warning! SetPriorityClass() error: %s\n", GetErrorString()); |
| 210 |
#endif // !_WIN32_WCE |
| 211 |
if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)) |
| 212 |
PLOG(PL_ERROR, "protoCapExample: Warning! SetThreadPriority() error: %s\n", GetErrorString()); |
| 213 |
#endif // WIN32 |
| 214 |
|
| 215 |
return true; |
| 216 |
} // end ProtoCapExample::OnStartup() |
| 217 |
|
| 218 |
void ProtoCapExample::OnShutdown() |
| 219 |
{ |
| 220 |
|
| 221 |
if (NULL != cap_rcvr) |
| 222 |
{ |
| 223 |
cap_rcvr->Close(); |
| 224 |
delete cap_rcvr; |
| 225 |
cap_rcvr = NULL; |
| 226 |
} |
| 227 |
if (NULL != cap_sndr) |
| 228 |
{ |
| 229 |
cap_sndr->Close(); |
| 230 |
delete cap_sndr; |
| 231 |
cap_sndr = NULL; |
| 232 |
} |
| 233 |
PLOG(PL_ERROR, "protoCapExample: Done.\n"); |
| 234 |
CloseDebugLog(); |
| 235 |
} // end ProtoCapExample::OnShutdown() |
| 236 |
|
| 237 |
bool ProtoCapExample::ProcessCommands(int argc, const char*const* argv) |
| 238 |
{ |
| 239 |
// Dispatch command-line commands to our OnCommand() method |
| 240 |
int i = 1; |
| 241 |
while ( i < argc) |
| 242 |
{ |
| 243 |
// Is it a class ProtoCapExample command? |
| 244 |
switch (GetCmdType(argv[i])) |
| 245 |
{ |
| 246 |
case CMD_INVALID: |
| 247 |
{ |
| 248 |
PLOG(PL_ERROR, "ProtoCapExample::ProcessCommands() Invalid command:%s\n", |
| 249 |
argv[i]); |
| 250 |
return false; |
| 251 |
} |
| 252 |
case CMD_NOARG: |
| 253 |
if (!OnCommand(argv[i], NULL)) |
| 254 |
{ |
| 255 |
PLOG(PL_ERROR, "ProtoCapExample::ProcessCommands() ProcessCommand(%s) error\n", |
| 256 |
argv[i]); |
| 257 |
return false; |
| 258 |
} |
| 259 |
i++; |
| 260 |
break; |
| 261 |
case CMD_ARG: |
| 262 |
if (!OnCommand(argv[i], argv[i+1])) |
| 263 |
{ |
| 264 |
PLOG(PL_ERROR, "ProtoCapExample::ProcessCommands() ProcessCommand(%s, %s) error\n", |
| 265 |
argv[i], argv[i+1]); |
| 266 |
return false; |
| 267 |
} |
| 268 |
i += 2; |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
return true; |
| 273 |
} // end ProtoCapExample::ProcessCommands() |
| 274 |
|
| 275 |
bool ProtoCapExample::OnCommand(const char* cmd, const char* val) |
| 276 |
{ |
| 277 |
// (TBD) move command processing into Mgen class ??? |
| 278 |
CmdType type = GetCmdType(cmd); |
| 279 |
ASSERT(CMD_INVALID != type); |
| 280 |
unsigned int len = strlen(cmd); |
| 281 |
if ((CMD_ARG == type) && !val) |
| 282 |
{ |
| 283 |
PLOG(PL_ERROR, "ProtoCapExample::ProcessCommand(%s) missing argument\n", cmd); |
| 284 |
return false; |
| 285 |
} |
| 286 |
else if (!strncmp("listen", cmd, len)) |
| 287 |
{ |
| 288 |
// Make sure "val" is an interface name |
| 289 |
int ifIndex = ProtoSocket::GetInterfaceIndex(val); |
| 290 |
char ifName[256]; |
| 291 |
|
| 292 |
ifName[255] = '\0'; |
| 293 |
if (!ProtoSocket::GetInterfaceName(ifIndex, ifName, 255)) |
| 294 |
{ |
| 295 |
PLOG(PL_ERROR, "protoCapExample: invalid <interfaceName>\n"); |
| 296 |
return false; |
| 297 |
} |
| 298 |
if (!cap_rcvr->Open(ifName)) |
| 299 |
{ |
| 300 |
PLOG(PL_ERROR, "protoCapExample: ProtoCap::Open() error.\n"); |
| 301 |
return false; |
| 302 |
} |
| 303 |
strncpy(rcvr_interface, ifName, 64); |
| 304 |
rcvr_interface[62] = '\0'; |
| 305 |
TRACE("protoCapExample: listening to interface: %s...\n", ifName); |
| 306 |
if (!stat_timer.IsActive()) ActivateTimer(stat_timer); |
| 307 |
} |
| 308 |
else if (!strncmp("forward", cmd, len)) |
| 309 |
{ |
| 310 |
if (!cap_rcvr->IsOpen()) |
| 311 |
{ |
| 312 |
PLOG(PL_ERROR, "protoCapExample: command line error: must specify \"listen\" first\n"); |
| 313 |
Usage(); |
| 314 |
return false; |
| 315 |
} |
| 316 |
// Make sure "val" is an interface name |
| 317 |
int ifIndex = ProtoSocket::GetInterfaceIndex(val); |
| 318 |
char ifName[256]; |
| 319 |
ifName[255] = '\0'; |
| 320 |
if (!ProtoSocket::GetInterfaceName(ifIndex, ifName, 255)) |
| 321 |
{ |
| 322 |
PLOG(PL_ERROR, "protoCapExample: invalid <interfaceName>\n"); |
| 323 |
return false; |
| 324 |
} |
| 325 |
if (!strncmp(ifName, rcvr_interface, 64)) |
| 326 |
{ |
| 327 |
cap_fwdr = cap_rcvr; |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
if (!cap_sndr->Open(ifName)) |
| 332 |
{ |
| 333 |
PLOG(PL_ERROR, "protoCapExample: ProtoCap::Open() error.\n"); |
| 334 |
return false; |
| 335 |
} |
| 336 |
cap_fwdr = cap_sndr; |
| 337 |
} |
| 338 |
strncpy(sndr_interface, ifName, 64); |
| 339 |
sndr_interface[62] = '\0'; |
| 340 |
forward = true; |
| 341 |
TRACE("protoCapExample: forwarding to interface: %s...\n", ifName); |
| 342 |
} |
| 343 |
|
| 344 |
else if (!strncmp("bridge", cmd, len)) |
| 345 |
{ |
| 346 |
if (!cap_rcvr->IsOpen()) |
| 347 |
{ |
| 348 |
PLOG(PL_ERROR, "protoCapExample: command line error: must specify \"listen\" first\n"); |
| 349 |
Usage(); |
| 350 |
return false; |
| 351 |
} |
| 352 |
// Make sure "val" is an interface name |
| 353 |
int ifIndex = ProtoSocket::GetInterfaceIndex(val); |
| 354 |
char ifName[256]; |
| 355 |
ifName[255] = '\0'; |
| 356 |
if (!ProtoSocket::GetInterfaceName(ifIndex, ifName, 255)) |
| 357 |
{ |
| 358 |
PLOG(PL_ERROR, "protoCapExample: invalid <interfaceName>\n"); |
| 359 |
return false; |
| 360 |
} |
| 361 |
if (!strncmp(ifName, rcvr_interface, 64)) |
| 362 |
{ |
| 363 |
cap_fwdr = cap_rcvr; |
| 364 |
} |
| 365 |
else |
| 366 |
{ |
| 367 |
if (!cap_sndr->Open(ifName)) |
| 368 |
{ |
| 369 |
PLOG(PL_ERROR, "protoCapExample: ProtoCap::Open() error.\n"); |
| 370 |
return false; |
| 371 |
} |
| 372 |
cap_fwdr = cap_sndr; |
| 373 |
} |
| 374 |
strncpy(sndr_interface, ifName, 64); |
| 375 |
sndr_interface[62] = '\0'; |
| 376 |
forward = false; |
| 377 |
TRACE("protoCapExample: bridging to interface: %s...\n", ifName); |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
PLOG(PL_ERROR, "protoCapExample:: invalid command\n"); |
| 382 |
return false; |
| 383 |
} |
| 384 |
return true; |
| 385 |
} // end ProtoCapExample::OnCommand() |
| 386 |
|
| 387 |
void ProtoCapExample::OnReceive(ProtoChannel& theChannel, |
| 388 |
ProtoChannel::Notification notifyType) |
| 389 |
{ |
| 390 |
if (ProtoChannel::NOTIFY_INPUT == notifyType) |
| 391 |
{ |
| 392 |
char buffer[4096]; |
| 393 |
unsigned int numBytes; |
| 394 |
do |
| 395 |
{ |
| 396 |
numBytes = 4096; |
| 397 |
ProtoCap::Direction direction; |
| 398 |
if (!cap_rcvr->Recv(buffer, numBytes, &direction)) |
| 399 |
{ |
| 400 |
PLOG(PL_ERROR, "protoCapExample::OnReceive() ProtoCap::Recv() error\n"); |
| 401 |
break; |
| 402 |
} |
| 403 |
else if (0 != numBytes) |
| 404 |
{ |
| 405 |
recv_count++; |
| 406 |
if (cap_fwdr) |
| 407 |
{ |
| 408 |
// Linux PF_PACKET doesn't like to send 802.3 frames |
| 409 |
UINT16 type; |
| 410 |
memcpy(&type, buffer+12, 2); |
| 411 |
type = ntohs(type); |
| 412 |
if ((ProtoCap::INBOUND == direction) && (0 != numBytes) && (type > 0x05dc)) |
| 413 |
{ |
| 414 |
if (forward) |
| 415 |
{ |
| 416 |
if (cap_fwdr->Forward(buffer, numBytes)) |
| 417 |
send_count++; |
| 418 |
else |
| 419 |
serr_count++; |
| 420 |
} |
| 421 |
else // we're bridging |
| 422 |
{ |
| 423 |
if (cap_fwdr->Send(buffer, numBytes)) |
| 424 |
send_count++; |
| 425 |
else |
| 426 |
serr_count++; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
} while (0 != numBytes); |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
TRACE("ProtoCapExample::OnReceive() unhandled notifyType: %d\n", notifyType); |
| 436 |
} |
| 437 |
|
| 438 |
} // end ProtoCapExample::OnReceive() |
| 439 |
|
| 440 |
bool ProtoCapExample::OnStatTimeout(ProtoTimer& /*theTimer*/) |
| 441 |
{ |
| 442 |
TRACE("stats: rcvd>%lu sent>%lu serr>%lu\n", recv_count, send_count, serr_count); |
| 443 |
return true; |
| 444 |
} // end ProtoCapExample::OnStatTimeout() |
| 445 |
|