Skip to content

Commit

Permalink
Initial source upload
Browse files Browse the repository at this point in the history
  • Loading branch information
seladb committed Oct 7, 2014
0 parents commit 488754e
Show file tree
Hide file tree
Showing 103 changed files with 7,732 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Common++/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-include ../mk/platform.mk

SOURCES := $(wildcard ./src/*.cpp)
OBJS_FILENAMES := $(patsubst ./src/%.cpp,%.o,$(SOURCES))
OBJS := $(patsubst ./src/%.cpp,./Obj/%.o,$(SOURCES))

ifdef WIN32
DEPS := -DWPCAP -DHAVE_REMOTE
endif

INCLUDES := -I"./src" \
-I"./header"

ifdef WIN32
INCLUDES += -I$(MINGW_HOME)/include/ddk \
-I$(WINPCAP_HOME)/Include
endif
ifdef LINUX
INCLUDES += -I/usr/include/netinet
endif

$(OBJS_FILENAMES):
@echo 'Building file: $(@:%.o=./src/%.cpp)'
@echo 'Invoking: GCC C++ Compiler'
$(G++) $(DEPS) $(INCLUDES) -O2 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=./Obj/%.d)" -MT"$(@:%.o=./Obj/%.d)" -o "./Obj/$@" "$(@:%.o=./src/%.cpp)"
@echo 'Finished building: $(@:%.o=./src/%.cpp)'
@echo ' '

# All Target
all: Common++.lib

# Tool invocations
Common++.lib: $(OBJS_FILENAMES)
@echo 'Building target: $@'
@echo 'Invoking: GCC Archiver'
$(AR) -r "Lib/$(LIB_PREFIX)Common++$(LIB_EXT)" $(OBJS)
@echo 'Finished successfully building target: $@'
@echo ' '

# Other Targets
clean:
$(RM) -rf ./Obj/*
$(RM) -rf ./Lib/*
@echo 'Clean finished'
74 changes: 74 additions & 0 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef PCAPPP_IPADDRESS
#define PCAPPP_IPADDRESS

#include <memory>
#include <stdint.h>
#include <string>

using namespace std;

#define MAX_ADDR_STRING_LEN 40 //xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx

class IPAddress
{
protected:
bool m_IsValid;
char m_AddressAsString[MAX_ADDR_STRING_LEN];
public:
enum AddressType {
IPv4AddressType,
IPv6AddressType
};

virtual ~IPAddress();
virtual AddressType getType() = 0;
string toString() const { return string(m_AddressAsString); }
bool isValid() { return m_IsValid; }
static auto_ptr<IPAddress> fromString(char* addressAsString);
static auto_ptr<IPAddress> fromString(string addressAsString);
};

struct in_addr;

class IPv4Address : public IPAddress
{
private:
in_addr* m_pInAddr;
void init(char* addressAsString);
public:
IPv4Address(const IPv4Address& other);
IPv4Address(uint32_t addressAsInt); //TODO: consider endianess?
IPv4Address(char* addressAsString);
IPv4Address(string addressAsString);
IPv4Address(in_addr* inAddr);
~IPv4Address();
AddressType getType() { return IPv4AddressType; }
uint32_t toInt() const;
in_addr* toInAddr() { return m_pInAddr; }
bool operator==(const IPv4Address& other) const { return toInt() == other.toInt(); }
};

struct in6_addr;

class IPv6Address : public IPAddress
{
private:
in6_addr* m_pInAddr;
void init(char* addressAsString);
public:
IPv6Address(const IPv6Address& other);
~IPv6Address();
IPv6Address(uint8_t* addressAsUintArr);
IPv6Address(char* addressAsString);
IPv6Address(string addressAsString);
AddressType getType() { return IPv6AddressType; }
uint8_t* toByteArray(int& length);
uint8_t* toByteArray();
in6_addr* toIn6Addr() { return m_pInAddr; }
void copyTo(uint8_t** arr);
void copyTo(uint8_t* arr) const;
bool operator==(const IPv6Address& other);
};


#endif /* PCAPPP_IPADDRESS */
65 changes: 65 additions & 0 deletions Common++/header/IpUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef PCAPPP_IP_UTILS
#define PCAPPP_IP_UTILS

//#include <pcap.h>
#include <stdint.h>
#ifndef WIN32
#include <in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#ifdef WIN32
/**
* convert a network format address to presentation format.
* return:
* pointer to presentation format address (`dst'), or NULL (see errno).
* author:
* Paul Vixie, 1996.
*/
const char* inet_ntop(int af, const void* src, char* dst, size_t size);

/**
* convert from presentation format (which usually means ASCII printable)
* to network format (which is usually some kind of binary format).
* return:
* 1 if the address was valid for the specified address family
* 0 if the address wasn't valid (`dst' is untouched in this case)
* -1 if some other error occurred (`dst' is untouched in this case, too)
* author:
* Paul Vixie, 1996.
*/
int inet_pton(int af, const char* src, void* dst);
#endif


/**
* extract IPv4 address from sockaddr
* @param sa - input sockaddr
* @return - in_addr
*/
in_addr* sockaddr2in_addr(struct sockaddr *sa);

/**
* extract IPv6 address from sockaddr
* @param sa - input sockaddr
* @return - in6_addr
*/
in6_addr* sockaddr2in6_addr(struct sockaddr *sa);

void sockaddr2string(struct sockaddr *sa, char* resultString);

uint32_t in_addr2int(in_addr inAddr);

struct ScalarBuffer
{
uint16_t* buffer;
size_t len;
};

//uint16_t compute_checksum(uint16_t* addr, uint32_t count, uint16_t protocol);
uint16_t compute_checksum(ScalarBuffer vec[], size_t vecSize);

#endif
88 changes: 88 additions & 0 deletions Common++/header/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef COMMONPP_LOGGER
#define COMMONPP_LOGGER

#include <stdio.h>
#include <stdint.h>

#ifndef LOG_MODULE
#define LOG_MODULE UndefinedLogModule
#endif

enum LogModule
{
UndefinedLogModule,
CommonLogModuleIpUtils,
PacketLogModuleUndefined,
PacketLogModuleIpUtils,
PacketLogModuleRawPacket,
PacketLogModulePacket,
PacketLogModuleLayer,
PacketLogModuleArpLayer,
PacketLogModuleEthLayer,
PacketLogModuleIPv4Layer,
PacketLogModuleIPv6Layer,
PacketLogModulePayloadLayer,
PacketLogModuleTcpLayer,
PacketLogModuleUdpLayer,
PacketLogModuleVlanLayer,
PacketLogModuleNumOfModules,
PcapLogModuleWinPcapLiveDevice,
PcapLogModuleRemoteDevice,
PcapLogModuleLiveDevice,
PcapLogModuleFileDevice,
NumOfLogModules
};

class LoggerPP
{
public:
enum LogLevel
{
Normal,
Debug
};

void setLogLevel(LogModule module, LogLevel level) { m_LogModulesArray[module] = level; }
void setAllModlesToLogLevel(LogLevel level) { for (int i=1; i<NumOfLogModules; i++) m_LogModulesArray[i] = level; }
inline bool isDebugEnabled(LogModule module) { return m_LogModulesArray[module] == Debug; }
inline LogLevel* getLogModulesArr() { return m_LogModulesArray; }

inline bool isErrorStringSet() { return m_ErrorString != NULL; }
inline char* getErrorString() { return m_ErrorString; }
void setErrorString(char* errString, int len) { m_ErrorString = errString; m_ErrorStringLen = len; }
inline int getErrorStringLength() { return m_ErrorStringLen; }

void supressErrors() { m_SuppressErrors = true; }
void enableErrors() { m_SuppressErrors = false; }
inline bool isSupressErrors() { return m_SuppressErrors; }

//TODO: make this singleton thread-safe
static inline LoggerPP& getInstance()
{
static LoggerPP instance;
return instance;
}
private:
char* m_ErrorString;
int m_ErrorStringLen;
bool m_SuppressErrors;
LoggerPP::LogLevel m_LogModulesArray[NumOfLogModules];
LoggerPP();
};

#define LOG_DEBUG(format, ...) do { \
if(LoggerPP::getInstance().isDebugEnabled(LOG_MODULE)) { \
printf("[%-35s: %-25s: line:%-4d] " format "\n", __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
} \
} while(0)

#define LOG_ERROR(format, ...) do { \
if (!LoggerPP::getInstance().isSupressErrors()) {\
if(LoggerPP::getInstance().isErrorStringSet()) \
snprintf(LoggerPP::getInstance().getErrorString(), LoggerPP::getInstance().getErrorStringLength(), format "\n", ## __VA_ARGS__); \
else \
fprintf(stderr, format "\n", ## __VA_ARGS__); \
} \
} while (0)

#endif /* COMMONPP_LOGGER */
39 changes: 39 additions & 0 deletions Common++/header/MacAddress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef PCAPPP_MACADDRESS
#define PCAPPP_MACADDRESS

#include <stdint.h>
#include <string>
#include <memory>

using namespace std;

class MacAddress
{
public:
MacAddress(uint8_t* addr);
MacAddress(const char* addr);
MacAddress(const string& addr);
MacAddress(uint8_t firstOctest, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet);
// copy c'tor
MacAddress(const MacAddress& other);

inline bool operator==(const MacAddress& other)
{
for (int i = 0; i < 6; i++)
if (m_Address[i] != other.m_Address[i])
return false;
return true;
}
inline bool operator!=(const MacAddress& other) {return !operator==(other);}

bool isValid() { return m_IsValid; }
string toString();
void copyTo(uint8_t** arr);
void copyTo(uint8_t* arr) const;
private:
uint8_t m_Address[6];
bool m_IsValid;
void init(const char* addr);
};

#endif /* PCAPPP_MACADDRESS */
Loading

0 comments on commit 488754e

Please sign in to comment.