1616
1717#include < iostream>
1818#include < cstring>
19+ #include < thread>
1920
2021#include " Core.h"
2122
23+ void Core::executeActions () {
24+ while (!this ->stop ) {
25+ for (auto action:this ->actions )
26+ action->action (this );
27+ }
28+ }
29+
2230void Core::recvDhcp () {
2331 EthHeader *eth;
2432 Ipv4Header *ip;
@@ -69,6 +77,9 @@ void Core::recvDhcp() {
6977 continue ;
7078
7179 pktInfo.toServer = ntohs (udp->srcport ) == 68 && ntohs (udp->dstport ) == 67 ;
80+
81+ for (auto action:this ->actions )
82+ action->recvDhcpMsg (this , &pktInfo, dhcp);
7283 }
7384}
7485
@@ -87,6 +98,18 @@ int Core::sendDhcpMsg(DhcpPacket *message, unsigned short len, PacketInfo *pktIn
8798 src = 68 ;
8899 dst = 67 ;
89100 }
101+
102+ eth = eth_inject_header (buf, &this ->sock ->iaddr , &pktInfo->phisAddr , ETHTYPE_IP);
103+ ip = ip_inject_header ((unsigned char *) eth->data ,
104+ &pktInfo->ipSrc ,
105+ &pktInfo->ipDst , IPDEFIHL,
106+ ip_mkid (),
107+ (unsigned short ) UDPHDRSIZE + len,
108+ IPDEFTTL,
109+ 0x11 );
110+ udp = udp_inject_header ((unsigned char *) ip->data , src, dst, len);
111+ memcpy (udp->data , message, len);
112+ return spark_write (this ->sock , buf, ETHHDRSIZE + IPHDRSIZE + UDPHDRSIZE + len);
90113}
91114
92115void Core::openSocket (const std::string &interface) {
@@ -99,11 +122,15 @@ void Core::openSocket(const std::string &interface) {
99122
100123 this ->buf = new unsigned char [SOCK_BUFSIZE];
101124
125+ this ->thActions = std::thread (&Core::executeActions, this );
126+
102127 this ->recvDhcp ();
103128
129+ this ->thActions .join ();
130+
104131 delete[] this ->buf ;
105132}
106133
107- void Core::registerCallback ( ) {
108-
134+ void Core::registerAction (DhcpAction *action ) {
135+ this -> actions . push_front (action);
109136}
0 commit comments