Examples/PcapSearch/main.cpp
/**
 * Searches all pcap files in given directory (and sub-directories if directed by the user) and output how many packets in each file matches a given
 * search criteria. This method outputs how many directories were searched, how many files were searched and how many packets were matched
 */
Searches all pcap files in given directory (and sub-directories if directed by the user) and output how many packets in each file matches a given
search criteria. This method outputs how many directories were searched, how many files were searched and how many packets were matched
Searches all pcap files in given directory ( and sub - directories if directed by the user ) and output how many packets in each file matches a given search criteria . This method outputs how many directories were searched how many files were searched and how many packets were matched
void searchDirectories(std::string directory, bool includeSubDirectories, std::string searchCriteria, std::ofstream* detailedReportFile,
		std::map<std::string, bool> extensionsToSearch,
		int& totalDirSearched, int& totalFilesSearched, int& totalPacketsFound)
{
	DIR *dir = opendir(directory.c_str());
	if (dir == NULL)
		return;
	struct dirent *entry = readdir(dir);
	std::vector<std::string> pcapList;
	while (entry != NULL)
	{
		std::string name(entry->d_name);
		std::string dirPath = directory;
		std::string dirSep = DIR_SEPARATOR;
		if (0 != directory.compare(directory.length() - dirSep.length(), dirSep.length(), dirSep)) 
			dirPath += DIR_SEPARATOR;
		dirPath += name;
		struct stat info;
		if (stat(dirPath.c_str(), &info) != 0)
		{
			entry = readdir(dir);
			continue;
		}
		if (!(info.st_mode & S_IFDIR))
		{
			if (extensionsToSearch.find(getExtension(name)) != extensionsToSearch.end())
				pcapList.push_back(dirPath);
			entry = readdir(dir);
			continue;
		}
		if (name == "." || name == "..")
		{
			entry = readdir(dir);
			continue;
		}
		if (includeSubDirectories)
			searchDirectories(dirPath, true, searchCriteria, detailedReportFile, extensionsToSearch, totalDirSearched, totalFilesSearched, totalPacketsFound);
		entry = readdir(dir);
	}
	closedir(dir);
	totalDirSearched++;
	for (std::vector<std::string>::iterator iter = pcapList.begin(); iter != pcapList.end(); iter++)
	{
		int packetsFound = searchPcap(*iter, searchCriteria, detailedReportFile);
		totalFilesSearched++;
		if (packetsFound > 0)
		{
			std::cout << packetsFound << " packets found in '" << *iter << "'" << std::endl;
			totalPacketsFound += packetsFound;
		}
	}
}
void searchDirectories ( std :: string directory , bool includeSubDirectories , std :: string searchCriteria , std :: ofstream * detailedReportFile , std :: map < std :: string , bool > extensionsToSearch , int & totalDirSearched , int & totalFilesSearched , int & totalPacketsFound ) { DIR * dir = opendir ( directory . c_str ( ) ) ; if ( dir == NULL ) return ; struct dirent * entry = readdir ( dir ) ; std :: vector < std :: string > pcapList ; while ( entry != NULL ) { std :: string name ( entry -> d_name ) ; std :: string dirPath = directory ; std :: string dirSep = DIR_SEPARATOR ; if ( 0 != directory . compare ( directory . length ( ) - dirSep . length ( ) , dirSep . length ( ) , dirSep ) ) dirPath += DIR_SEPARATOR ; dirPath += name ; struct stat info ; if ( stat ( dirPath . c_str ( ) , & info ) != 0 ) { entry = readdir ( dir ) ; continue ; } if ( ! ( info . st_mode & S_IFDIR ) ) { if ( extensionsToSearch . find ( getExtension ( name ) ) != extensionsToSearch . end ( ) ) pcapList . push_back ( dirPath ) ; entry = readdir ( dir ) ; continue ; } if ( name == " " || name == " " ) { entry = readdir ( dir ) ; continue ; } if ( includeSubDirectories ) searchDirectories ( dirPath , true , searchCriteria , detailedReportFile , extensionsToSearch , totalDirSearched , totalFilesSearched , totalPacketsFound ) ; entry = readdir ( dir ) ; } closedir ( dir ) ; totalDirSearched ++ ; for ( std :: vector < std :: string > :: iterator iter = pcapList . begin ( ) ; iter != pcapList . end ( ) ; iter ++ ) { int packetsFound = searchPcap ( * iter , searchCriteria , detailedReportFile ) ; totalFilesSearched ++ ; if ( packetsFound > 0 ) { std :: cout << packetsFound << " " << * iter << " " << std :: endl ; totalPacketsFound += packetsFound ; } } }
Searches all pcap files in given directory (and sub-directories if directed by the user) and output how many packets in each file matches a given
 search criteria.
Searches all pcap files in given directory ( and sub - directories if directed by the user ) and output how many packets in each file matches a given search criteria .
// open the directory
 // dir is null usually when user has no access permissions 
 // go over all files in this directory
 // construct directory full path
 // directory doesn't contain separator in the end
 // get file attributes
 // if the file is not a directory
 // check if the file extension matches the requested extensions to search. If it does, put the file name in a list of files
 // that should be searched (don't do the search just yet)
 // if the file is a '.' or '..' skip it
 // if we got to here it means the file is actually a directory. If required to search sub-directories, call this method recursively to search
 // inside this sub-directory
 // move to the next file
 // close dir
 // when we get to here we already covered all sub-directories and collected all the files in this directory that are required for search
 // go over each such file and search its packets to find the search criteria
 // do the actual search
 // add to total matched packets
