72 lines
1.3 KiB
C++
72 lines
1.3 KiB
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/ioctl.h>
|
|
#include <net/if.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#include "logging.h"
|
|
#include "module.h"
|
|
#include "netpkt.h"
|
|
|
|
NetPkt::NetPkt(const char* data, int size)
|
|
: data(data), used(size), size(0)
|
|
{
|
|
}
|
|
|
|
NetPkt::NetPkt(int size)
|
|
: used(0), size(size)
|
|
{
|
|
data = new char[size];
|
|
}
|
|
|
|
NetPkt::~NetPkt()
|
|
{
|
|
if (size > 0)
|
|
delete data;
|
|
}
|
|
|
|
int NetPkt::show(char* buf, int size)
|
|
{
|
|
return snprintf(buf, size, "(%s:%d) (%d/%d) bytes",
|
|
inet_ntoa(this->addr.sin_addr),
|
|
ntohs(this->addr.sin_port),
|
|
this->used, this->size);
|
|
}
|
|
|
|
bool NetPkt::compare(int offset, const char* buf, int len)
|
|
{
|
|
if (offset >= this->used)
|
|
return false;
|
|
|
|
/* nicht ueber das paket hinaus vergleichen */
|
|
if (offset + len >= this->used)
|
|
len = this->used - offset;
|
|
|
|
return (memcmp(this->data + offset, buf, len) == 0);
|
|
}
|
|
|
|
int NetPkt::find(int offset, const char *buf, int len)
|
|
{
|
|
if (offset >= this->used)
|
|
return -1;
|
|
|
|
void* found = memmem(this->data + offset, this->used, buf, len);
|
|
|
|
return (found == NULL) ? -1 : ((char*)found - this->data);
|
|
}
|
|
|
|
void NetPkt::setAddress(struct sockaddr_in *tmp)
|
|
{
|
|
memcpy(&addr, tmp, sizeof(addr));
|
|
}
|
|
|
|
struct sockaddr_in * NetPkt::getAddress()
|
|
{
|
|
return &addr;
|
|
}
|