X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/93c7c1b8634f7dbe83b710df9908db9706cbe755..1cb912e4f90473f78d2f0f10ab5c9b2c5bd36440:/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp?ds=sidebyside diff --git a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp index 27141b91..aa10327c 100644 --- a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp @@ -28,87 +28,45 @@ $Date: 2009/03/24 11:20:15 $ $Author: faust $ */ +#include "ether_cap.h" + +#include "stg/common.h" +#include "stg/raw_ip_packet.h" +#include "stg/traffcounter.h" + +#include +#include +#include +#include +#include + #include #include #include #include #include -#include #include #include #include #include - -#include #include -#include -#include -#include -#include #include -#include -#include - -#include "ether_cap.h" -#include "common.h" -#include "raw_ip_packet.h" - -using namespace std; - //#define CAP_DEBUG 1 -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -class BPF_CAP_CREATOR +extern "C" STG::Plugin* GetPlugin() { -private: - BPF_CAP * bpfc; - -public: - BPF_CAP_CREATOR() - : bpfc(new BPF_CAP()) - { - }; - ~BPF_CAP_CREATOR() - { - delete bpfc; - }; - - BPF_CAP * GetCapturer() - { - return bpfc; - }; -}; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -BPF_CAP_CREATOR bcc; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -BASE_PLUGIN * GetPlugin() -{ -return bcc.GetCapturer(); + static BPF_CAP plugin; + return &plugin; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) +int BPF_CAP_SETTINGS::ParseSettings(const STG::ModuleSettings & s) { -//char sep[]= ", \t\n\r"; -//char *s; -string ifaces; -//char * str; -//char *p; - iface.erase(iface.begin(), iface.end()); -//PARAM_VALUE pv; -//pv.param = "WorkDir"; -//vector::const_iterator pvi; - if (s.moduleParams.empty()) { errorStr = "Parameter \'iface\' not found."; @@ -130,36 +88,10 @@ for (unsigned i = 0; i < s.moduleParams.size(); i++) } } -/*if (cf.ReadString("Iface", &ifaces, "NoIface") < 0) - { - errorStr = "Cannot read parameter \'Iface\' from " + cf.GetFileName(); - return -1; - } - -str = new char[ifaces.size() + 1]; -strcpy(str, ifaces.c_str()); -p = str; - -while((s = strtok(p, sep))) - { - printfd(__FILE__, "iface[] = %s\n", s); - p = NULL; - iface.push_back(s); - //strncpy(iface[i++], s, DEV_NAME_LEN); - //devNum++; - } - -delete[] str; -if (!ifaces.size()) - { - errorStr = "Error read parameter \'Iface\' from " + cf.GetFileName(); - return -1; - }*/ - return 0; } //----------------------------------------------------------------------------- -string BPF_CAP_SETTINGS::GetIface(unsigned int num) +std::string BPF_CAP_SETTINGS::GetIface(unsigned int num) { if (num >= iface.size()) { @@ -170,24 +102,19 @@ return iface[num]; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -const string BPF_CAP::GetVersion() const +std::string BPF_CAP::GetVersion() const { -return "bpf_cap v.1.0"; +return "cap_bpf v.1.0"; } //----------------------------------------------------------------------------- BPF_CAP::BPF_CAP() - : nonstop(false), - isRunning(false), + : isRunning(false), capSock(-1), - traffCnt(NULL) + traffCnt(NULL), + logger(STG::PluginLogger::get("cap_bpf")) { } //----------------------------------------------------------------------------- -void BPF_CAP::SetSettings(const MODULE_SETTINGS & s) -{ -settings = s; -} -//----------------------------------------------------------------------------- int BPF_CAP::ParseSettings() { int ret = capSettings.ParseSettings(settings); @@ -199,16 +126,6 @@ if (ret) return 0; } //----------------------------------------------------------------------------- -void BPF_CAP::SetTraffcounter(TRAFFCOUNTER * tc) -{ -traffCnt = tc; -} -//----------------------------------------------------------------------------- -const string & BPF_CAP::GetStrError() const -{ -return errorStr; -} -//----------------------------------------------------------------------------- int BPF_CAP::Start() { if (isRunning) @@ -220,16 +137,9 @@ if (BPFCapOpen() < 0) return -1; } -nonstop = true; +m_thread = std::jthread([this](auto token){ Run(std::move(token)); }); -if (pthread_create(&thread, NULL, Run, this) == 0) - { - return 0; - } - -errorStr = "Cannot create thread."; -printfd(__FILE__, "Cannot create thread\n"); -return -1; +return 0; } //----------------------------------------------------------------------------- int BPF_CAP::Stop() @@ -239,7 +149,7 @@ if (!isRunning) BPFCapClose(); -nonstop = false; +m_thread.request_stop(); //5 seconds to thread stops itself int i; @@ -248,71 +158,51 @@ for (i = 0; i < 25; i++) if (!isRunning) break; - usleep(200000); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now killing it if (isRunning) - { - //TODO pthread_cancel() - if (pthread_kill(thread, SIGINT)) - { - errorStr = "Cannot kill thread."; - printfd(__FILE__, "Cannot kill thread\n"); - return -1; - } - } + m_thread.detach(); +else + m_thread.join(); return 0; } //----------------------------------------------------------------------------- -bool BPF_CAP::IsRunning() -{ -return isRunning; -} -//----------------------------------------------------------------------------- -void * BPF_CAP::Run(void * d) +void BPF_CAP::Run(std::stop_token token) { -BPF_CAP * dc = (BPF_CAP *)d; -dc->isRunning = true; +sigset_t signalSet; +sigfillset(&signalSet); +pthread_sigmask(SIG_BLOCK, &signalSet, NULL); + +isRunning = true; uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96 -RAW_PACKET * rpp = (RAW_PACKET *)&hdr[14]; +STG::RawPacket * rpp = (STG::RawPacket *)&hdr[14]; memset(hdr, 0, sizeof(hdr)); rpp->dataLen = -1; char * iface; -while (dc->nonstop) +while (!token.stop_requested()) { - dc->BPFCapRead((char*)&hdr, 68 + 14, &iface); + if (BPFCapRead((char*)&hdr, 68 + 14, &iface)) + continue; if (!(hdr[12] == 0x8 && hdr[13] == 0x0)) - { continue; - } - dc->traffCnt->Process(*rpp); + traffCnt->process(*rpp); } -dc->isRunning = false; -return NULL; -} -//----------------------------------------------------------------------------- -uint16_t BPF_CAP::GetStartPosition() const -{ -return 0; -} -//----------------------------------------------------------------------------- -uint16_t BPF_CAP::GetStopPosition() const -{ -return 0; +isRunning = false; } //----------------------------------------------------------------------------- int BPF_CAP::BPFCapOpen() { -//for (int i = 0; i < settings->devNum; i++) int i = 0; BPF_DATA bd; pollfd pd; @@ -321,9 +211,9 @@ while ((bd.iface = capSettings.GetIface(i)) != "") { bpfData.push_back(bd); if (BPFCapOpen(&bpfData[i]) < 0) - { - return -1; - } + { + return -1; + } pd.events = POLLIN; pd.fd = bpfData[i].fd; @@ -334,7 +224,6 @@ while ((bd.iface = capSettings.GetIface(i)) != "") return 0; } //----------------------------------------------------------------------------- -//int BPF_CAP::BPFCapOpen(string ifaceToOpen) int BPF_CAP::BPFCapOpen(BPF_DATA * bd) { char devbpf[20]; @@ -343,57 +232,54 @@ int l = BUFF_LEN; int im = 1; struct ifreq ifr; -do { +do + { sprintf(devbpf, "/dev/bpf%d", i); i++; bd->fd = open(devbpf, O_RDONLY); - //cd[n].fd = open(devbpf, O_RDONLY); } while(bd->fd < 0 && errno == EBUSY); - //while(cd[n].fd < 0 && errno == EBUSY); -//if (cd[n].fd < 0) if (bd->fd < 0) { errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error."; + logger("Cannot open device for interface '%s': %s", bd->iface.c_str(), strerror(errno)); printfd(__FILE__, "Cannot open BPF device\n"); return -1; } -//strncpy(ifr.ifr_name, settings->iface[n], sizeof(ifr.ifr_name)); strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name)); -//if (ioctl(cd[n].fd, BIOCSBLEN, (caddr_t)&l) < 0) if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0) { - errorStr = bd->iface + " BIOCSBLEN " + string(strerror(errno)); + errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno)); + logger("ioctl (BIOCSBLEN) error for interface '%s': %s", bd->iface.c_str(), strerror(errno)); printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str()); return -1; } -//if (ioctl(cd[n].fd, BIOCSETIF, (caddr_t)&ifr) < 0 ) if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0) { - errorStr = bd->iface + " BIOCSETIF " + string(strerror(errno)); + errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno)); + logger("ioctl (BIOCSETIF) error for interface '%s': %s", bd->iface.c_str(), strerror(errno)); printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str()); return -1; } -//if (ioctl(cd[n].fd, BIOCIMMEDIATE, &im) < 0 ) if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0) { - errorStr = bd->iface + " BIOCIMMEDIATE " + string(strerror(errno)); + errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno)); + logger("ioctl (BIOCIMMEDIATE) error for interface '%s': %s", bd->iface.c_str(), strerror(errno)); printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str()); return -1; } return bd->fd; -//return 0; } //----------------------------------------------------------------------------- int BPF_CAP::BPFCapClose() { for (unsigned int i = 0; i < bpfData.size(); i++) - close(bpfData[i].fd); + close(bpfData[i].fd); return 0; } //----------------------------------------------------------------------------- @@ -405,12 +291,16 @@ for (unsigned int i = 0; i < polld.size(); i++) { if (polld[i].revents & POLLIN) { - BPFCapRead(buffer, blen, capIface, &bpfData[i]); + if (BPFCapRead(buffer, blen, capIface, &bpfData[i])) + { + polld[i].revents = 0; + continue; + } polld[i].revents = 0; return 0; } } -return 0; +return -1; } //----------------------------------------------------------------------------- int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd) @@ -420,8 +310,10 @@ if (bd->canRead) bd->r = read(bd->fd, bd->buffer, BUFF_LEN); if (bd->r < 0) { - //printfd(__FILE__, " error read\n"); - usleep(20000); + logger("read error: %s", strerror(errno)); + struct timespec ts = {0, 20000000}; + nanosleep(&ts, NULL); + return -1; } bd->p = bd->buffer; @@ -432,8 +324,6 @@ if (bd->canRead) if(bd->r > bd->sum) { memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen); - //strncpy(iface, settings->iface[n], 9); - //*iface = settings->iface[n]; bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen); bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen); @@ -449,4 +339,3 @@ if(bd->r <= bd->sum) return 0; } //----------------------------------------------------------------------------- -