diff --git a/Core/app/NetBlock/NetBlock.pro b/Core/app/NetBlock/NetBlock.pro index 50fa113c..e2f2bdbb 100644 --- a/Core/app/NetBlock/NetBlock.pro +++ b/Core/app/NetBlock/NetBlock.pro @@ -27,7 +27,6 @@ SOURCES += \ core/oui.cpp \ core/packet.cpp \ core/parser.cpp \ - core/pch.cpp \ main.cpp diff --git a/Core/app/NetBlock/core/arppacket.cpp b/Core/app/NetBlock/core/arppacket.cpp index 312d77cd..04ae2dc0 100644 --- a/Core/app/NetBlock/core/arppacket.cpp +++ b/Core/app/NetBlock/core/arppacket.cpp @@ -28,11 +28,12 @@ void ARPPacket::makeArppacket(WMac dmac, WMac smac, WMac tmac,WIp tip, WIp sip) packet.arp.tip_ = htonl(tip); } -void ARPPacket::send(int cnt) { +void ARPPacket::send(int cnt) +{ wpacket.buf_.data_ = reinterpret_cast(&packet); wpacket.buf_.size_ = sizeof(EthArp); for(int i =0; i host_list = db_connect.select_query("SELECT * FROM host"); + for(std::list::iterator iter = host_list.begin(); iter != host_list.end(); ++iter) { Host g; g.mac_ = WMac(iter->argv[1]); g.ip_ = WIp(iter->argv[2]); g.name = std::string(iter->argv[3]); + struct timeval now; gettimeofday(&now, NULL); now.tv_sec -= 60; g.last = now; + std::lock_guard lock(fs_map.m); fs_map.insert(std::pair(g.mac_, g)); } Data_List::list_free(host_list); } -void FullScan::scan(){ +void FullScan::scan() +{ ARPPacket arp_packet; WMac my_mac; WIp my_ip; @@ -48,25 +54,27 @@ void FullScan::scan(){ uint32_t endIp = (my_ip | ~mask)-1; //find all ip connected to the network - for(uint32_t ip = beginIp; ip != endIp; ++ip){ + for(uint32_t ip = beginIp; ip != endIp; ++ip) { if(WIp(ip) == gateway) { continue; } arp_packet.packet.arp.tip_ = htonl(WIp(ip)); arp_packet.send(3);//send packet } } -void FullScan::updateDB(){//update last_ip +void FullScan::updateDB()//update last_ip +{ DB_Connect& db_connect = DB_Connect::getInstance(); std::list d1; std::string query; + d1 = db_connect.select_query("SELECT * FROM host"); { std::lock_guard lock(fs_map.m); - for(std::map::iterator fs_iter = fs_map.begin(); fs_iter != fs_map.end(); ++fs_iter){ //fullscan + for(std::map::iterator fs_iter = fs_map.begin(); fs_iter != fs_map.end(); ++fs_iter) { //fullscan int tmp = 0; for(std::list::iterator data_iter = d1.begin(); data_iter != d1.end(); ++data_iter) { - if(WMac(data_iter->argv[1]) == fs_iter->first){//same mac - if(WIp(data_iter->argv[2]) != fs_iter->second.ip_){//need update + if(WMac(data_iter->argv[1]) == fs_iter->first) {//same mac + if(WIp(data_iter->argv[2]) != fs_iter->second.ip_) {//need update query = "UPDATE host SET last_ip = '"+std::string((fs_iter->second).ip_)+"' WHERE mac = '"+std::string(fs_iter->first)+"'"; db_connect.send_query(query.data()); } @@ -76,6 +84,7 @@ void FullScan::updateDB(){//update last_ip } if(tmp == 0) { //different mac -> insert + GTRACE("\n\n\nINSERT!!!!!!!!!"); query = "INSERT INTO host(mac, last_ip, name) VALUES('"+std::string(fs_iter->first)+"', '"+ std::string((fs_iter->second).ip_).data() +"', '"+ fs_iter->second.name.data() + "')"; db_connect.send_query(query.data()); } @@ -84,24 +93,28 @@ void FullScan::updateDB(){//update last_ip Data_List::list_free(d1); } -void FullScan::updateHostInfo(WMac mac_, WIp ip_, struct timeval last_) { +void FullScan::updateHostInfo(WMac mac_, WIp ip_, struct timeval last_) +{ std::lock_guard lock(fs_map.m); fs_map[mac_].ip_ = ip_; fs_map[mac_].last = last_; } -void FullScan::addHost(std::pair host) { +void FullScan::addHost(std::pair host) +{ std::lock_guard lock(fs_map.m); fs_map.insert(host); } -bool FullScan::isConnect(std::string mac) { +bool FullScan::isConnect(std::string mac) +{ WMac wmac(mac); std::lock_guard lock(fs_map.m); return fs_map[wmac].isConnected(); } -void FullScan::delHost(std::string mac) { +void FullScan::delHost(std::string mac) +{ WMac wmac(mac); std::lock_guard lock(fs_map.m); fs_map.erase(wmac); diff --git a/Core/app/NetBlock/core/netblock.cpp b/Core/app/NetBlock/core/netblock.cpp index 6ff4ed8c..05c294b8 100644 --- a/Core/app/NetBlock/core/netblock.cpp +++ b/Core/app/NetBlock/core/netblock.cpp @@ -1,11 +1,10 @@ #include "netblock.h" #include "arppacket.h" -void NetBlock::sendInfect(){//full-scan : is_connect & policy +void NetBlock::sendInfect()//full-scan : is_connect & policy +{ std::map fs_map = fs_instance.getFsMap(); - Packet& packet_instance = Packet::getInstance(); - ARPPacket infect_packet; time_t timer; @@ -16,11 +15,11 @@ void NetBlock::sendInfect(){//full-scan : is_connect & policy { std::lock_guard lock(m); for(std::map::iterator iter = nb_map.begin(); iter != nb_map.end(); ++iter) { - if(fs_map[iter->first].isConnected()){//full-scan & policy + if(fs_instance.isConnect(std::string(iter->first))){//full-scan & policy timer = time(NULL); t = localtime(&timer); - GTRACE("\n\n\n"); + GTRACE(""); GTRACE("time = %d:%d:%d",(Week)t->tm_wday,t->tm_hour,t->tm_min); GTRACE("%s",std::string(iter->first).data()); GTRACE("%s",std::string((iter->second).ip_).data()); @@ -34,17 +33,19 @@ void NetBlock::sendInfect(){//full-scan : is_connect & policy } } } - sleep(30); + std::this_thread::sleep_for(std::chrono::milliseconds(30000));//sleep 30s } } -void NetBlock::sendRecover(Host host) { +void NetBlock::sendRecover(Host host) +{ ARPPacket recover_packet; recover_packet.makeArppacket(host.mac_, recover_packet.gate_mac, host.mac_, host.ip_, recover_packet.gate_ip); recover_packet.send(3); } -void NetBlock::getBlockHostMap(){ +void NetBlock::getBlockHostMap() +{ DB_Connect& db_connect = DB_Connect::getInstance(); Host g; std::list d1,d2; @@ -65,27 +66,30 @@ void NetBlock::getBlockHostMap(){ Data_List::list_free(d2); } -void NetBlock::updateMap(){ +void NetBlock::updateMap() +{ time_t timer; struct tm* t; while(end_check) { timer = time(NULL); t = localtime(&timer); + if(t->tm_min % 10 != 0 || t->tm_sec != 0) { continue; } + GTRACE("updateMap: h: %d, m: %d, s: %d", t->tm_hour, t->tm_min, t->tm_sec); getBlockHostMap();//update NBmap for(std::map::iterator iter_old = nb_map.begin(); iter_old != nb_map.end(); ++iter_old) { - if(new_nb_map.find(iter_old->first) == new_nb_map.end()) { + if(new_nb_map.find(iter_old->first) == new_nb_map.end()) { //finish policy sendRecover(iter_old->second); } } { std::lock_guard lock(m); - if(new_nb_map.size() > 0){ + if(new_nb_map.size() > 0) { nb_map.clear(); nb_map.swap(new_nb_map); } diff --git a/Core/app/NetBlock/core/oui.cpp b/Core/app/NetBlock/core/oui.cpp index 9cbdef3c..32537f9e 100644 --- a/Core/app/NetBlock/core/oui.cpp +++ b/Core/app/NetBlock/core/oui.cpp @@ -1,5 +1,4 @@ #include "oui.h" - static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; @@ -43,7 +42,7 @@ std::string UrlEncode(const std::string &s) return escaped; } -char* oui_db(WMac mac){ +QString oui_db(WMac mac){ CURL *curl; CURLcode res; struct MemoryStruct chunk; @@ -64,6 +63,11 @@ char* oui_db(WMac mac){ fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); + GTRACE("%s",chunk.memory); + QString tmp = QString(chunk.memory); + if(tmp == "{\"errors\":{\"detail\":\"Not Found\"}}"){ + return nullptr; + } return chunk.memory; } } diff --git a/Core/app/NetBlock/core/oui.h b/Core/app/NetBlock/core/oui.h index ac127067..14c70da7 100644 --- a/Core/app/NetBlock/core/oui.h +++ b/Core/app/NetBlock/core/oui.h @@ -8,4 +8,4 @@ struct MemoryStruct { }; std::string UrlEncode(const std::string &s); -char* oui_db(WMac mac); +QString oui_db(WMac mac); diff --git a/Core/app/NetBlock/core/parser.cpp b/Core/app/NetBlock/core/parser.cpp index f6c41a4d..c6f2ca43 100644 --- a/Core/app/NetBlock/core/parser.cpp +++ b/Core/app/NetBlock/core/parser.cpp @@ -6,30 +6,26 @@ bool DHCPParser::parse(WPacket& packet) if(packet.ethHdr_->type() != WEthHdr::Ip4) { return false; } // Is ip4 packet? if(packet.ipHdr_->p() != WIpHdr::Udp) { return false; } // Is udp packet? if(packet.udpHdr_->sport() != 67 && packet.udpHdr_->dport() != 67) { return false; } // Is dhcp packet? - if(!packet.ethHdr_->dmac_.isBroadcast()) { return false; } // Is DHCP Request? + if(!packet.ethHdr_->dmac().isBroadcast()) { return false; } // Is DHCP Request? packet.dhcpHdr_ = (WDhcpHdr*)packet.udpData_.data_; - GTRACE(""); - g.mac_ = packet.dhcpHdr_->clientMac_;//get mac - gettimeofday(&g.last, NULL); + GTRACE(""); + g.mac_ = packet.dhcpHdr_->clientMac();//get mac GTRACE("%s",std::string(g.mac_).data()); - for(WDhcpHdr::Option* opt = packet.dhcpHdr_->first(); opt != nullptr; opt = opt->next()) - { - if(opt->type_ == WDhcpHdr::RequestedIpAddress)//get ip - { + gettimeofday(&g.last, NULL);//update connection + + for(WDhcpHdr::Option* opt = packet.dhcpHdr_->first(); opt != nullptr; opt = opt->next()) { + if(opt->type_ == WDhcpHdr::RequestedIpAddress) { //get ip char* buf = (char*)malloc(sizeof(char) * 4); - for(int i = 0; i < opt->len_ - 1; ++i) - { + for(int i = 0; i < opt->len_ - 1; ++i) { sprintf(buf + 4 * i, "%03d.", *(&opt->len_ + 1 + i)); } sprintf(buf + 12, "%03d", *(&opt->len_ + 4)); g.ip_ = WIp(std::string(buf)); GTRACE("%s", std::string(g.ip_).data()); - } - else if(opt->type_ == 12)//get name - { + } else if(opt->type_ == 12){ //get name char* tmp = (char*)malloc(sizeof(char) * opt->len_); for(int i = 0; i < opt->len_; ++i) { tmp[i] = *(&opt->len_ + 1 + i); @@ -39,7 +35,7 @@ bool DHCPParser::parse(WPacket& packet) } } - fs.addHost(std::pair(g.mac_, g));//insert into fs_map + fs.addHost(std::pair(g.mac_, g)); //insert into fs_map return true; } @@ -70,6 +66,7 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing WMac my_mac; WIp mask; WIp gateway; + { std::lock_guard lock(packet_instance.m); my_mac = packet_instance.intf()->mac(); @@ -78,15 +75,15 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing } if(packet.ethHdr_->type() != WEthHdr::Arp) {return false;} - if(packet.ethHdr_->smac_ == my_mac) {return false;} + if(packet.ethHdr_->smac() == my_mac) {return false;} GTRACE("mac: %s, ip: %s", std::string(packet.ethHdr_->smac()).data(), std::string(packet.arpHdr_->sip()).data()); - if((packet.arpHdr_->sip() & mask) == (gateway & mask) && packet.arpHdr_->sip() != gateway) - { + if((packet.arpHdr_->sip() & mask) == (gateway & mask) && packet.arpHdr_->sip() != gateway) { g.mac_ = packet.ethHdr_->smac();//get mac g.ip_ = packet.arpHdr_->sip(); //get ip gettimeofday(&g.last, NULL); + GTRACE(""); GTRACE("%s", std::string(g.mac_).data()); GTRACE("%s", std::string(g.ip_).data()); @@ -95,8 +92,7 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing if(iter != fs.getFsMap().end()) { GTRACE("already info mac: %s, ip: %s", std::string(g.mac_).data(), std::string(g.ip_).data()); fs.updateHostInfo(g.mac_, g.ip_, g.last); - } - else { + } else { g.name = std::string(g.ip_); // findName(); fs.addHost(std::pair(g.mac_, g)); //insert into fs_map @@ -109,8 +105,8 @@ void ARPParser::parse(WPacket& packet, std::map nb_map) { if(parse(packet)) { ARPPacket arp_packet; - GTRACE("next-parse: %d", packet.arpHdr_->op()==packet.arpHdr_->Request); - if(packet.arpHdr_->op()==packet.arpHdr_->Request){//request + GTRACE("next-parse: %d", packet.arpHdr_->op() == packet.arpHdr_->Request); + if(packet.arpHdr_->op()==packet.arpHdr_->Request) {//request //infection std::map::iterator iter; for(iter = nb_map.begin(); iter != nb_map.end(); ++iter) { diff --git a/Core/app/NetBlock/core/pch.cpp b/Core/app/NetBlock/core/pch.cpp deleted file mode 100644 index f0942e7e..00000000 --- a/Core/app/NetBlock/core/pch.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "pch.h" - -void sleepFunc(int msec) { - #ifdef __linux__ - usleep(msec * 1000); - #elif TARGET_OS_MAC - unsleep(msec * 1000); - #elif _WIN32 - sleep(msec); - #endif -} diff --git a/Core/app/NetBlock/core/pch.h b/Core/app/NetBlock/core/pch.h index 62e056cf..f030a498 100644 --- a/Core/app/NetBlock/core/pch.h +++ b/Core/app/NetBlock/core/pch.h @@ -12,7 +12,7 @@ #include #include #include - +#include #include #include @@ -33,5 +33,3 @@ struct Host { return now.tv_sec - last.tv_sec < 60; } }; - -void sleepFunc(int msec); diff --git a/Core/app/NetBlock/device/devicewidget.cpp b/Core/app/NetBlock/device/devicewidget.cpp index 72a6345d..05a3494d 100644 --- a/Core/app/NetBlock/device/devicewidget.cpp +++ b/Core/app/NetBlock/device/devicewidget.cpp @@ -60,11 +60,15 @@ void DeviceWidget::setDevInfo() dInfo tmp; tmp.host_id = stoi(iter->argv[0]); tmp.mac = QString::fromStdString(iter->argv[1]); - tmp.oui = QString(oui_db(WMac(tmp.mac.toStdString()))); + + if(tmp.oui==nullptr) + tmp.oui = oui_db(WMac(tmp.mac.toStdString())); + tmp.last_ip = QString::fromStdString(iter->argv[2]); tmp.name = QString::fromStdString(iter->argv[3]); tmp.is_connect = fs_instance.isConnect(tmp.mac.toStdString()); devices.push_back(tmp); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } Data_List::list_free(dl); diff --git a/Core/app/hostscan-test/arppacket.cpp b/Core/app/hostscan-test/arppacket.cpp index 312d77cd..04ae2dc0 100644 --- a/Core/app/hostscan-test/arppacket.cpp +++ b/Core/app/hostscan-test/arppacket.cpp @@ -28,11 +28,12 @@ void ARPPacket::makeArppacket(WMac dmac, WMac smac, WMac tmac,WIp tip, WIp sip) packet.arp.tip_ = htonl(tip); } -void ARPPacket::send(int cnt) { +void ARPPacket::send(int cnt) +{ wpacket.buf_.data_ = reinterpret_cast(&packet); wpacket.buf_.size_ = sizeof(EthArp); for(int i =0; i d1; std::string query; + d1 = db_connect.select_query("SELECT * FROM host"); { std::lock_guard lock(fs_map.m); - for(std::map::iterator fs_iter = fs_map.begin(); fs_iter != fs_map.end(); ++fs_iter){ //fullscan + for(std::map::iterator fs_iter = fs_map.begin(); fs_iter != fs_map.end(); ++fs_iter) { //fullscan int tmp = 0; for(std::list::iterator data_iter = d1.begin(); data_iter != d1.end(); ++data_iter) { - if(WMac(data_iter->argv[1]) == fs_iter->first){//same mac - if(WIp(data_iter->argv[2]) != fs_iter->second.ip_){//need update + if(WMac(data_iter->argv[1]) == fs_iter->first) {//same mac + if(WIp(data_iter->argv[2]) != fs_iter->second.ip_) {//need update query = "UPDATE host SET last_ip = '"+std::string((fs_iter->second).ip_)+"' WHERE mac = '"+std::string(fs_iter->first)+"'"; db_connect.send_query(query.data()); } @@ -81,6 +84,7 @@ void FullScan::updateDB(){//update last_ip } if(tmp == 0) { //different mac -> insert + GTRACE("\n\n\nINSERT!!!!!!!!!"); query = "INSERT INTO host(mac, last_ip, name) VALUES('"+std::string(fs_iter->first)+"', '"+ std::string((fs_iter->second).ip_).data() +"', '"+ fs_iter->second.name.data() + "')"; db_connect.send_query(query.data()); } @@ -89,24 +93,28 @@ void FullScan::updateDB(){//update last_ip Data_List::list_free(d1); } -void FullScan::updateHostInfo(WMac mac_, WIp ip_, struct timeval last_) { +void FullScan::updateHostInfo(WMac mac_, WIp ip_, struct timeval last_) +{ std::lock_guard lock(fs_map.m); fs_map[mac_].ip_ = ip_; fs_map[mac_].last = last_; } -void FullScan::addHost(std::pair host) { +void FullScan::addHost(std::pair host) +{ std::lock_guard lock(fs_map.m); fs_map.insert(host); } -bool FullScan::isConnect(std::string mac) { +bool FullScan::isConnect(std::string mac) +{ WMac wmac(mac); std::lock_guard lock(fs_map.m); return fs_map[wmac].isConnected(); } -void FullScan::delHost(std::string mac) { +void FullScan::delHost(std::string mac) +{ WMac wmac(mac); std::lock_guard lock(fs_map.m); fs_map.erase(wmac); diff --git a/Core/app/hostscan-test/hostscan-test.pro b/Core/app/hostscan-test/hostscan-test.pro index b38429b3..8d54dfc5 100644 --- a/Core/app/hostscan-test/hostscan-test.pro +++ b/Core/app/hostscan-test/hostscan-test.pro @@ -11,8 +11,7 @@ SOURCES += \ hostscan-test.cpp \ netblock.cpp \ packet.cpp \ - parser.cpp \ - pch.cpp + parser.cpp HEADERS += \ arppacket.h \ diff --git a/Core/app/hostscan-test/netblock.cpp b/Core/app/hostscan-test/netblock.cpp index 8b3a18eb..05c294b8 100644 --- a/Core/app/hostscan-test/netblock.cpp +++ b/Core/app/hostscan-test/netblock.cpp @@ -1,7 +1,8 @@ #include "netblock.h" #include "arppacket.h" -void NetBlock::sendInfect(){//full-scan : is_connect & policy +void NetBlock::sendInfect()//full-scan : is_connect & policy +{ std::map fs_map = fs_instance.getFsMap(); Packet& packet_instance = Packet::getInstance(); ARPPacket infect_packet; @@ -10,17 +11,15 @@ void NetBlock::sendInfect(){//full-scan : is_connect & policy struct tm* t; while(end_check) { - nb_map = fs_map; - gtrace("SIZE = %d",nb_map.size()); if(nb_map.size() == 0) { continue; } { std::lock_guard lock(m); for(std::map::iterator iter = nb_map.begin(); iter != nb_map.end(); ++iter) { - if(fs_map[iter->first].isConnected()){//full-scan & policy + if(fs_instance.isConnect(std::string(iter->first))){//full-scan & policy timer = time(NULL); t = localtime(&timer); - GTRACE("\n\n\n"); + GTRACE(""); GTRACE("time = %d:%d:%d",(Week)t->tm_wday,t->tm_hour,t->tm_min); GTRACE("%s",std::string(iter->first).data()); GTRACE("%s",std::string((iter->second).ip_).data()); @@ -34,17 +33,19 @@ void NetBlock::sendInfect(){//full-scan : is_connect & policy } } } - sleep(30); + std::this_thread::sleep_for(std::chrono::milliseconds(30000));//sleep 30s } } -void NetBlock::sendRecover(Host host) { +void NetBlock::sendRecover(Host host) +{ ARPPacket recover_packet; recover_packet.makeArppacket(host.mac_, recover_packet.gate_mac, host.mac_, host.ip_, recover_packet.gate_ip); recover_packet.send(3); } -void NetBlock::getBlockHostMap(){ +void NetBlock::getBlockHostMap() +{ DB_Connect& db_connect = DB_Connect::getInstance(); Host g; std::list d1,d2; @@ -65,27 +66,30 @@ void NetBlock::getBlockHostMap(){ Data_List::list_free(d2); } -void NetBlock::updateMap(){ +void NetBlock::updateMap() +{ time_t timer; struct tm* t; while(end_check) { timer = time(NULL); t = localtime(&timer); + if(t->tm_min % 10 != 0 || t->tm_sec != 0) { continue; } + GTRACE("updateMap: h: %d, m: %d, s: %d", t->tm_hour, t->tm_min, t->tm_sec); getBlockHostMap();//update NBmap for(std::map::iterator iter_old = nb_map.begin(); iter_old != nb_map.end(); ++iter_old) { - if(new_nb_map.find(iter_old->first) == new_nb_map.end()) { + if(new_nb_map.find(iter_old->first) == new_nb_map.end()) { //finish policy sendRecover(iter_old->second); } } { std::lock_guard lock(m); - if(new_nb_map.size() > 0){ + if(new_nb_map.size() > 0) { nb_map.clear(); nb_map.swap(new_nb_map); } diff --git a/Core/app/hostscan-test/parser.cpp b/Core/app/hostscan-test/parser.cpp index bbbc87ee..c6f2ca43 100644 --- a/Core/app/hostscan-test/parser.cpp +++ b/Core/app/hostscan-test/parser.cpp @@ -6,30 +6,26 @@ bool DHCPParser::parse(WPacket& packet) if(packet.ethHdr_->type() != WEthHdr::Ip4) { return false; } // Is ip4 packet? if(packet.ipHdr_->p() != WIpHdr::Udp) { return false; } // Is udp packet? if(packet.udpHdr_->sport() != 67 && packet.udpHdr_->dport() != 67) { return false; } // Is dhcp packet? - if(!packet.ethHdr_->dmac_.isBroadcast()) { return false; } // Is DHCP Request? + if(!packet.ethHdr_->dmac().isBroadcast()) { return false; } // Is DHCP Request? packet.dhcpHdr_ = (WDhcpHdr*)packet.udpData_.data_; - GTRACE(""); - g.mac_ = packet.dhcpHdr_->clientMac_;//get mac - gettimeofday(&g.last, NULL); + GTRACE(""); + g.mac_ = packet.dhcpHdr_->clientMac();//get mac GTRACE("%s",std::string(g.mac_).data()); - for(WDhcpHdr::Option* opt = packet.dhcpHdr_->first(); opt != nullptr; opt = opt->next()) - { - if(opt->type_ == WDhcpHdr::RequestedIpAddress)//get ip - { + gettimeofday(&g.last, NULL);//update connection + + for(WDhcpHdr::Option* opt = packet.dhcpHdr_->first(); opt != nullptr; opt = opt->next()) { + if(opt->type_ == WDhcpHdr::RequestedIpAddress) { //get ip char* buf = (char*)malloc(sizeof(char) * 4); - for(int i = 0; i < opt->len_ - 1; ++i) - { + for(int i = 0; i < opt->len_ - 1; ++i) { sprintf(buf + 4 * i, "%03d.", *(&opt->len_ + 1 + i)); } sprintf(buf + 12, "%03d", *(&opt->len_ + 4)); g.ip_ = WIp(std::string(buf)); GTRACE("%s", std::string(g.ip_).data()); - } - else if(opt->type_ == 12)//get name - { + } else if(opt->type_ == 12){ //get name char* tmp = (char*)malloc(sizeof(char) * opt->len_); for(int i = 0; i < opt->len_; ++i) { tmp[i] = *(&opt->len_ + 1 + i); @@ -39,7 +35,7 @@ bool DHCPParser::parse(WPacket& packet) } } - fs.addHost(std::pair(g.mac_, g));//insert into fs_map + fs.addHost(std::pair(g.mac_, g)); //insert into fs_map return true; } @@ -70,6 +66,7 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing WMac my_mac; WIp mask; WIp gateway; + { std::lock_guard lock(packet_instance.m); my_mac = packet_instance.intf()->mac(); @@ -78,25 +75,24 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing } if(packet.ethHdr_->type() != WEthHdr::Arp) {return false;} - if(packet.ethHdr_->smac_ == my_mac) {return false;} + if(packet.ethHdr_->smac() == my_mac) {return false;} GTRACE("mac: %s, ip: %s", std::string(packet.ethHdr_->smac()).data(), std::string(packet.arpHdr_->sip()).data()); - if((packet.arpHdr_->sip() & mask) == (gateway & mask) && packet.arpHdr_->sip() != gateway) - { + if((packet.arpHdr_->sip() & mask) == (gateway & mask) && packet.arpHdr_->sip() != gateway) { g.mac_ = packet.ethHdr_->smac();//get mac g.ip_ = packet.arpHdr_->sip(); //get ip gettimeofday(&g.last, NULL); + GTRACE(""); GTRACE("%s", std::string(g.mac_).data()); GTRACE("%s", std::string(g.ip_).data()); - GTRACE("%d",g.last); + std::map::iterator iter = fs.getFsMap().find(g.mac_); if(iter != fs.getFsMap().end()) { GTRACE("already info mac: %s, ip: %s", std::string(g.mac_).data(), std::string(g.ip_).data()); fs.updateHostInfo(g.mac_, g.ip_, g.last); - } - else { + } else { g.name = std::string(g.ip_); // findName(); fs.addHost(std::pair(g.mac_, g)); //insert into fs_map @@ -109,8 +105,8 @@ void ARPParser::parse(WPacket& packet, std::map nb_map) { if(parse(packet)) { ARPPacket arp_packet; - GTRACE("next-parse: %d", packet.arpHdr_->op()==packet.arpHdr_->Request); - if(packet.arpHdr_->op()==packet.arpHdr_->Request){//request + GTRACE("next-parse: %d", packet.arpHdr_->op() == packet.arpHdr_->Request); + if(packet.arpHdr_->op()==packet.arpHdr_->Request) {//request //infection std::map::iterator iter; for(iter = nb_map.begin(); iter != nb_map.end(); ++iter) { diff --git a/Core/app/hostscan-test/pch.cpp b/Core/app/hostscan-test/pch.cpp deleted file mode 100644 index f0942e7e..00000000 --- a/Core/app/hostscan-test/pch.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "pch.h" - -void sleepFunc(int msec) { - #ifdef __linux__ - usleep(msec * 1000); - #elif TARGET_OS_MAC - unsleep(msec * 1000); - #elif _WIN32 - sleep(msec); - #endif -} diff --git a/Core/app/hostscan-test/pch.h b/Core/app/hostscan-test/pch.h index ebdd449e..acd66124 100644 --- a/Core/app/hostscan-test/pch.h +++ b/Core/app/hostscan-test/pch.h @@ -36,5 +36,3 @@ struct Host { return now.tv_sec - last.tv_sec < 60; } }; - -void sleepFunc(int msec);