Commit 5f74c9d6 authored by 's avatar

Merging branches/automapi with trunk. A tag of the prevous state (trunk@613)...

Merging branches/automapi with trunk. A tag of the prevous state (trunk@613) has been saved in tags/mapi-before-autoconf

git-svn-id: file:///home/svn/mapi/trunk@614 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 9b724624
configure
Makefile.in
config.h.in
autom4te.cache
aclocal.m4
For the admission control daemon and library please visit
http://sourceforge.net/projects/admctrl
to get the latest version.
#include "DoS.h"
#include <qapplication.h>
#include <qlineedit.h>
#include <qcombobox.h>
#include <qtextbrowser.h>
#include <qlistview.h>
#include <qstring.h>
#include <ctype.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
typedef void* (*thread)(void*);
void victimComm(DoS* app);
void attackComm(DoS* app);
void workloadComm(DoS* app);
DoS::DoS(void)
{
this->victimSocket = this->attackSocket = this->workloadSocket = -1;
this->victim_tid = this->attack_tid = this->workload_tid = 0;
this->interval = 0;
this->listView->setColumnAlignment(0, Qt::AlignRight);
this->listView->setColumnAlignment(1, Qt::AlignRight);
this->listView->setColumnAlignment(2, Qt::AlignRight);
this->listView->setColumnAlignment(3, Qt::AlignRight);
this->listView->addColumn(QString(""), 0);
this->listView->setSorting(4, FALSE);
}
DoS::~DoS(void)
{
detectionStop();
workloadStop();
attackStop();
this->disconnect(&victimSocket);
this->disconnect(&attackSocket);
this->disconnect(&workloadSocket);
}
void DoS::message(char* msg)
{
txtMessages->append(msg);
}
void DoS::insertResult(resultItem* res)
{
int aux = res->interval;
QString a;
while (aux > 0)
{
aux = aux/10;
a += 'a';
}
QListViewItem* i = new QListViewItem(this->listView,
QString::number(res->interval),
QString::number(res->SYN_ACK),
QString::number(res->meanRate),
QString::number(res->condition),
a);
this->listView->insertItem(i);
return;
}
void DoS::detectionStart(void)
{
request req;
victim_params params;
//check connection to server
if (victimSocket != -1)
{
message("Detection Application already running");
return;
}
if (!this->connect(this->edtVictimIP->text(), (short)atoi(this->edtVictimPort->text().ascii()), &victimSocket))
return;
//construct request
if (this->cbxAlgorithm->currentItem() == 0)
req.cmd = START_ADAPTIVE;
else if (this->cbxAlgorithm->currentItem() == 1)
req.cmd = START_CUSUM;
strcpy(params.threshold, this->edtParam1->text());
strcpy(params.a, this->edtParam2->text());
strcpy(params.b, this->edtParam3->text());
strcpy(params.interval, this->edtParam4->text());
strcpy(params.device, this->edtDevice->text());
req.data_size = sizeof(victim_params);
memcpy((void*)req.data, (void*)&params, req.data_size);
//send command to server
if (send(victimSocket, (void*)&req, sizeof(request), 0) == -1)
perror("send");
//setup communication thread
this->listView->clear();
if (pthread_create(&victim_tid, NULL, (thread)victimComm, (void*)this) != 0)
fprintf(stderr, "Can not create communication thread!\n");
return;
}
void DoS::detectionStop(void)
{
request req;
//check connection to server
if (victimSocket == -1)
{
message("Detection Application is not running");
return;
}
//construct request
req.cmd = KILL_DETECTION;
//send command to server
if (send(victimSocket, (void*)&req, 4, 0) == -1)
perror("send");
//close connection to server
usleep(100000); //wait a bit for server's ack
disconnect(&victimSocket);
return;
}
void DoS::workloadStart(void)
{
request req;
attacker_params params;
//check connection to server
if (workloadSocket != -1)
{
message("Detection Application already running");
return;
}
if (!this->connect(this->edtAttackerIP->text(), (short)atoi(this->edtAttackerPort->text().ascii()), &workloadSocket))
return;
//construct request
req.cmd = START_WORKLOAD;
strcpy(params.ip, this->edtWorkloadIP->text());
strcpy(params.port, "5000");
strcpy(params.rate, this->edtWorkloadRate->text());
req.data_size = sizeof(attacker_params);
memcpy((void*)req.data, (void*)&params, req.data_size);
//send command to server
if (send(workloadSocket, (void*)&req, sizeof(request), 0) == -1)
perror("send");
//setup communication thread
if (pthread_create(&victim_tid, NULL, (thread)workloadComm, (void*)this) != 0)
fprintf(stderr, "Can not create communication thread!\n");
return;
}
void DoS::workloadStop(void)
{
request req;
//check connection to server
if (workloadSocket == -1)
{
message("Attacker is not running");
return;
}
//construct request
req.cmd = KILL_WORKLOAD;
//send command to server
if (send(workloadSocket, (void*)&req, 4, 0) == -1)
perror("send");
//close connection to server
usleep(100000); //wait a bit for server's ack
disconnect(&workloadSocket);
return;
}
void DoS::attackStart(void)
{
request req;
attacker_params params;
//check connection to server
if (attackSocket != -1)
{
message("Detection Application already running");
return;
}
if (!this->connect(this->edtAttackerIP->text(), (short)atoi(this->edtAttackerPort->text().ascii()), &attackSocket))
return;
//construct request
req.cmd = START_ATTACK;
strcpy(params.ip, this->edtAttackIP->text());
strcpy(params.port, "5394");
strcpy(params.rate, this->edtAttackRate->text());
req.data_size = sizeof(attacker_params);
memcpy((void*)req.data, (void*)&params, req.data_size);
//send command to server
if (send(attackSocket, (void*)&req, sizeof(request), 0) == -1)
perror("send");
//setup communication thread
if (pthread_create(&victim_tid, NULL, (thread)attackComm, (void*)this) != 0)
fprintf(stderr, "Can not create communication thread!\n");
return;
}
void DoS::attackStop(void)
{
request req;
//check connection to server
if (attackSocket == -1)
{
message("Attacker is not running");
return;
}
//construct request
req.cmd = KILL_ATTACK;
//send command to server
if (send(attackSocket, (void*)&req, 4, 0) == -1)
perror("send");
//close connection to server
usleep(100000); //wait a bit for server's ack
disconnect(&attackSocket);
return;
}
/**
* int DoS::connect(const char* host, short port, int* sock)
*
* Connect to specified host's port.
*
* host - hostname
* port - port number where host is listening
* sock - indirectly return value, id of the socket which has been bind to the specified host
*/
int DoS::connect(const char* host, short port, int* sock)
{
struct hostent* he;
struct sockaddr_in their_addr; //connector's address information
printf("connect()\n");
if ((he = gethostbyname(host)) == NULL) //get the host info
{
message("unknown host");
return 0;
}
if ((*sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return 0;
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(port); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);// zero the rest of the struct
if (::connect(*sock, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
{
message("Can not connect to server");
this->disconnect(sock);
return 0;
}
return 1;
}
/**
* void DoS::disconnect(int* socket)
*
* Disconnect from specified host.
*
* socket - socket which is bound to host to be disconnect from
*/
void DoS::disconnect(int* socket)
{
printf("disconnect()\n");
if (*socket == -1)
return;
::close(*socket);
*socket = -1;
return;
}
void victimComm(DoS* app)
{
request req;
resultItem* res;
int socket = app->getVictimSocket();
while (recv(socket, (void*)&req, sizeof(request), 0) != -1)
{
if (req.cmd == ERROR)
app->message("ERROR in communication with Detector");
if (req.cmd == START_DETECTION_ACK)
app->message("Detection Application Started");
else if (req.cmd == KILL_DETECTION_ACK)
{
app->setInterval(0);
app->message("Detection Application Halted");
pthread_exit(NULL);
}
else if (req.cmd == RESULTS)
{
if (req.data_size != sizeof(resultItem))
{
printf("%d == %d\n", req.data_size, sizeof(resultItem));
app->message("invalid result received from server");
continue;
}
res = (resultItem*)req.data;
app->setInterval(res->interval);
app->insertResult(res);
app->message(res->alarm);
}
}
perror("recv");
pthread_exit(NULL);
}
void workloadComm(DoS* app)
{
request req;
int socket = app->getWorkloadSocket();
while (recv(socket, (void*)&req, sizeof(request), 0) != -1)
{
if (req.cmd == START_WORKLOAD_ACK)
app->message("Workload Generator Started");
else if (req.cmd == KILL_WORKLOAD_ACK)
app->message("Workload Generator Halted");
}
perror("recv");
pthread_exit(NULL);
}
void attackComm(DoS* app)
{
request req;
int socket = app->getAttackSocket();
while (recv(socket, (void*)&req, sizeof(request), 0) != -1)
{
if (req.cmd == START_ATTACK_ACK)
{
char buf[128];
sprintf(buf, "<font color='#0000ff'>Attack Started</font> at interval #%d", app->getInterval());
app->message(buf);
}
else if (req.cmd == KILL_ATTACK_ACK)
app->message("<font color='#0000ff'>Attack Halted</font>");
}
perror("recv");
pthread_exit(NULL);
}
#ifndef DoS_H_
#define DoS_H_
#include <pthread.h>
#include "frmmain.h"
#include "../comm.h"
class DoS : public frmMain
{
Q_OBJECT
public:
DoS(void);
~DoS(void);
void message(char* msg);
void insertResult(resultItem* res);
int getVictimSocket(void) {return this->victimSocket;}
int getAttackSocket(void) {return this->attackSocket;}
int getWorkloadSocket(void) {return this->workloadSocket;}
int getInterval(void) {return this->interval;}
void setInterval(int interval) {this->interval = interval;}
public slots:
//void help(void);
void detectionStart(void);
void detectionStop(void);
void workloadStart(void);
void workloadStop(void);
void attackStart(void);
void attackStop(void);
private:
int connect(const char* host, short port, int* socket);
void disconnect(int* socket);
int interval;
int victimSocket;
int attackSocket;
int workloadSocket;
pthread_t victim_tid;
pthread_t attack_tid;
pthread_t workload_tid;
};
#endif //DoS_H_
######################################################################
# Automatically generated by qmake (1.07a) Fri Aug 20 12:45:35 2004
######################################################################
TEMPLATE = app
INCLUDEPATH += .
# Input
HEADERS += DoS.h frmmain.ui.h
INTERFACES += frmmain.ui
SOURCES += DoS.cpp main.cpp
LIBS += -lpthread
#############################################################################
# Makefile for building: DoS
# Generated by qmake (1.07a) (Qt 3.3.2) on: Mon Oct 11 16:52:05 2004
# Project: DoS.pro
# Template: app
# Command: $(QMAKE) -o Makefile DoS.pro
#############################################################################
####### Compiler, tools and options
CC = gcc
CXX = g++
LEX = flex
YACC = yacc
CFLAGS = -pipe -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -fPIC -Wall -W -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -fPIC -DQT_NO_DEBUG -DQT_SHARED -DQT_TABLET_SUPPORT
CXXFLAGS = -pipe -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -fPIC -Wall -W -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -fPIC -DQT_NO_DEBUG -DQT_SHARED -DQT_TABLET_SUPPORT
LEXFLAGS =
YACCFLAGS= -d
INCPATH = -I/usr/lib/qt3/mkspecs/default -I. -I. -I/usr/include -I$(QTDIR)/include
LINK = g++
LFLAGS =
LIBS = $(SUBLIBS) -L/usr/lib/ -L$(QTDIR)/lib/ -L/usr/X11R6/lib/ -lpthread -lqt -lXext -lX11 -lm
AR = ar cqs
RANLIB =
MOC = $(QTDIR)/bin/moc
UIC = $(QTDIR)/bin/uic
QMAKE = qmake
TAR = tar -cf
GZIP = gzip -9f
COPY = cp -f
COPY_FILE= $(COPY)
COPY_DIR = $(COPY) -r
INSTALL_FILE= $(COPY_FILE)
INSTALL_DIR = $(COPY_DIR)
DEL_FILE = rm -f
SYMLINK = ln -sf
DEL_DIR = rmdir
MOVE = mv -f
CHK_DIR_EXISTS= test -d
MKDIR = mkdir -p
####### Output directory
OBJECTS_DIR = ./
####### Files
HEADERS = DoS.h \
frmmain.ui.h
SOURCES = DoS.cpp \
main.cpp
OBJECTS = DoS.o \
main.o \
frmmain.o
FORMS = frmmain.ui
UICDECLS = frmmain.h
UICIMPLS = frmmain.cpp
SRCMOC = moc_DoS.cpp \
moc_frmmain.cpp
OBJMOC = moc_DoS.o \