-3

I have a few thousand pcap files that I'm trying to parse as part of a research project. They are all named as xxx.xxx.xxx.xxx_yyy.yyy.yyy.yyy.pcap where the first IP address is the one I'm trying to use as a variable in my C++ program.

Parsing the pcap files themselves is not an issue. I am passing the filename to the function as a pointer and just don't quite know how to grab that first part of the filename.

As requested, here is a bit of code:

//program.cpp//
int main(int argc, char *argv[]){
  char * inFile;
  inFile = argv[1];

  result = parsePkts(inFile);
  return 0;
}

//functions.h//
int parsePkts(char *fn){
  struct ip *ipHdr = NULL;

  ipHdr = (struct ip *)(data + sizeof(struct ether_header));

  if((ipHdr -> ip_dst.s_addr)) == xxx.xxx.xxx.xxx) {
    do stuff
  }
}

Obviously there is a lot more to the program but this is where I need to grab it. Thanks.

user2722670
  • 39
  • 1
  • 7

1 Answers1

0

if your input is const char* as filename you can split it in severals ways. You wrote that you need to split it into some parts (first part). I have little snippet to split string by char in your case '_'

void stringSplitBy(std::string str, const char *separator, std::vector<std::string> &results)
{
    size_t found = str.find_first_of(separator);
    while (found != std::string::npos) {
        if (found > 0) {
            results.push_back(str.substr(0, found));
        }
        str = str.substr(found + 1);
        found = str.find_first_of(separator);
    }
    if (str.length() > 0) {
        results.push_back(str);
    }
}

Use it in this way:

const char* inputfname = "xxx.xxx.xxx.xxx_yyy.yyy.yyy.yyy.pcap";
std::string fname = std::string(inputfname);
std::vector<std::string> results;
stringSplitBy(fname, '_', results);

You can print result:

 std::vector<std::string>::iterator it = results.begin();
 for (; it != results.end(); ++it)
 {
   std::cout<< (*it).c_str() << std::endl;
 }
Elensar
  • 128
  • 1
  • 9