| 1 |
#ifndef _PROTO_DETOUR |
| 2 |
#define _PROTO_DETOUR |
| 3 |
|
| 4 |
#include "protoChannel.h" |
| 5 |
#include "protoAddress.h" |
| 6 |
|
| 7 |
/** |
| 8 |
* @class ProtoDetour |
| 9 |
* |
| 10 |
* @brief Inbound/outbound packet _interception_ class. Platform |
| 11 |
* implementations vary ... but generally leverages system firewall interfaces. |
| 12 |
* A Win32 version based around NDIS intermediate driver is in progress. |
| 13 |
*/ |
| 14 |
|
| 15 |
class ProtoDetour : public ProtoChannel |
| 16 |
{ |
| 17 |
public: |
| 18 |
static ProtoDetour* Create(); |
| 19 |
virtual ~ProtoDetour() {} |
| 20 |
|
| 21 |
enum HookFlag |
| 22 |
{ |
| 23 |
INPUT = 0x01, // packets coming in |
| 24 |
OUTPUT = 0x02, // locally generated packets |
| 25 |
FORWARD = 0x04, // packets we might forward |
| 26 |
INJECT = 0x80 // "inject-only" option (create a raw socket for send-only) |
| 27 |
}; |
| 28 |
|
| 29 |
enum Direction |
| 30 |
{ |
| 31 |
UNSPECIFIED, |
| 32 |
INBOUND, |
| 33 |
OUTBOUND |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
virtual bool Open(int hookFlags = 0, |
| 38 |
const ProtoAddress& srcFilterAddr = PROTO_ADDR_NONE, |
| 39 |
unsigned int srcFilterMask = 0, |
| 40 |
const ProtoAddress& dstFilterAddr = PROTO_ADDR_NONE, |
| 41 |
unsigned int dstFilterMask = 0, |
| 42 |
int dscpValue = -1) |
| 43 |
{ |
| 44 |
return ProtoChannel::Open(); |
| 45 |
} |
| 46 |
virtual bool IsOpen() {return ProtoChannel::IsOpen();} |
| 47 |
// ProtoCap::Close() should also be called at the _beginning_ of |
| 48 |
// any derived implementations' Close() method |
| 49 |
virtual void Close() {ProtoChannel::Close();} |
| 50 |
|
| 51 |
virtual bool Recv(char* buffer, |
| 52 |
unsigned int& numBytes, |
| 53 |
Direction* direction = NULL, // optionally learn INBOUND/OUTBOUND direction of pkt |
| 54 |
ProtoAddress* srcMac = NULL, // optionally learn previous hop source MAC addr |
| 55 |
unsigned int* ifIndex = NULL) = 0; // optionally learn which iface (INBOUND only) |
| 56 |
|
| 57 |
virtual bool Allow(const char* buffer, unsigned int numBytes) = 0; |
| 58 |
virtual bool Drop() = 0; |
| 59 |
virtual bool Inject(const char* buffer, unsigned int numBytes) = 0; |
| 60 |
|
| 61 |
virtual bool SetMulticastInterface(const char* interfaceName) {return false;} |
| 62 |
|
| 63 |
void SetUserData(const void* userData) |
| 64 |
{user_data = userData;} |
| 65 |
const void* GetUserData() const |
| 66 |
{return user_data;} |
| 67 |
|
| 68 |
protected: |
| 69 |
ProtoDetour() : user_data(NULL) |
| 70 |
{ |
| 71 |
// Enable input notification by default |
| 72 |
StartInputNotification(); |
| 73 |
// Our default handler "allows" all packets received |
| 74 |
// (Note that ProtoChannel::SetNotifier() _must_ be set _and_ a "running" notifier |
| 75 |
// must be in place for this default listener to be automatically invoked! |
| 76 |
SetListener(this, &ProtoDetour::DefaultEventHandler); |
| 77 |
} |
| 78 |
|
| 79 |
private: |
| 80 |
void DefaultEventHandler(ProtoChannel& theChannel, |
| 81 |
Notification theNotification) |
| 82 |
{ |
| 83 |
if (NOTIFY_INPUT == theNotification) |
| 84 |
{ |
| 85 |
char buffer[8192]; |
| 86 |
unsigned int numBytes = 8192; |
| 87 |
while (Recv(buffer, numBytes)) |
| 88 |
{ |
| 89 |
if (0 != numBytes) |
| 90 |
Allow(buffer, numBytes); |
| 91 |
else |
| 92 |
break; |
| 93 |
numBytes = 8192; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
const void* user_data; |
| 99 |
}; // end class ProtoDetour |
| 100 |
|
| 101 |
#endif // _PROTO_DETOUR |