I'm coding a networking software, which has lots of functions (modules) which can be run in parallel. They share some resouces, like libnet_context
s of every type, pcap_handle
s, device info (may not change). Now the sructure is (pseudocode):
class Tools
{
Libnet link;
Libnet raw4;
Libnet raw6;
Pcap sniffer;
Pcap router;
std::mutex l_mutex; //link
std::mutex r4_mutex; //raw4
std::mutex r6_mutex; //raw6
std::mutex s_mutex; //sniffer
std::mutex r_mutex; //router
//helpers
bool GetNextHopMAC(uint src_ip, uint dst_ip, byte mac[6]);
uint HostToIP(std::string hst);
public://methods which may run in parallel
bool Init(std::string device);/* opening handles/contexts */
bool Sniff(std::string filter, std::string ouputfile);
bool SendARP(uint dst_ip, uint src_ip, byte dst_mac[6]);
bool SendARP_Loop(std::string host1, std::string host2); //spoofing
bool StartRouting(); //routes traffic
/*and more*/
}
and in main (just an example):
int main()
{
/*redirecting lazy brothers internet to learn math*/
Tools tools;
std::thread arps(&Tools::SendARP_Loop, &tools, "mybrother", "gateway");
std::thread forw(&Tools::StartRouting, &tools);
std::thread dnsh(&Tools::ReDNS, &tools, "math.com");
arps.join();
forw.join();
dnsh.join();
return 0;
};
It is working, but infinite locks can happen when a function never returns, in case of an loop, where the mutex locks the whole function (packet flooding).
What are better ways of doing this, supposing I am going to make a GUI for this too, how can I restructure the program so these methods can have different outputs (windows or text fields). Is creating a new context for the desired type (Libnet link
for a SendARP
) in methods and not having mutexes better?