hlswmaster-ng/netpkt.cpp

72 lines
1.3 KiB
C++
Raw Normal View History

2006-02-02 16:55:44 +01:00
#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"
2006-02-20 12:31:34 +01:00
#include "module.h"
2006-02-02 16:55:44 +01:00
#include "netpkt.h"
2006-03-05 02:28:19 +01:00
NetPkt::NetPkt(const char* data, int size)
: data(data), used(size), size(0)
2006-02-02 16:55:44 +01:00
{
}
2006-03-05 02:28:19 +01:00
NetPkt::NetPkt(int size)
: used(0), size(size)
2006-02-02 16:55:44 +01:00
{
2006-03-05 02:28:19 +01:00
data = new char[size];
2006-02-02 16:55:44 +01:00
}
2006-03-05 02:28:19 +01:00
NetPkt::~NetPkt()
2006-02-02 16:55:44 +01:00
{
2006-03-05 02:28:19 +01:00
if (size > 0)
delete data;
2006-02-02 16:55:44 +01:00
}
int NetPkt::show(char* buf, int size)
{
2006-03-05 02:28:19 +01:00
return snprintf(buf, size, "(%s:%d) (%d/%d) bytes",
2006-02-02 16:55:44 +01:00
inet_ntoa(this->addr.sin_addr),
ntohs(this->addr.sin_port),
2006-03-05 02:28:19 +01:00
this->used, this->size);
2006-02-20 12:31:34 +01:00
}
bool NetPkt::compare(int offset, const char* buf, int len)
{
2006-03-05 02:28:19 +01:00
if (offset >= this->used)
2006-02-20 12:31:34 +01:00
return false;
/* nicht ueber das paket hinaus vergleichen */
2006-03-05 02:28:19 +01:00
if (offset + len >= this->used)
len = this->used - offset;
2006-02-20 12:31:34 +01:00
return (memcmp(this->data + offset, buf, len) == 0);
}
2006-02-20 21:58:59 +01:00
int NetPkt::find(int offset, const char *buf, int len)
{
2006-03-05 02:28:19 +01:00
if (offset >= this->used)
2006-02-20 21:58:59 +01:00
return -1;
2006-03-05 02:28:19 +01:00
void* found = memmem(this->data + offset, this->used, buf, len);
2006-02-20 21:58:59 +01:00
return (found == NULL) ? -1 : ((char*)found - this->data);
}
2006-03-05 02:28:19 +01:00
void NetPkt::setAddress(struct sockaddr_in *tmp)
{
memcpy(&addr, tmp, sizeof(addr));
}
struct sockaddr_in * NetPkt::getAddress()
{
return &addr;
}