/* Functions and classes which keep track of and use regexes to classify streams of application data. By Ethan Sommer and Matthew Strait , (C) 2006-2007 http://l7-filter.sf.net This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. http://www.gnu.org/licenses/gpl.txt This file is synced between the userspace source code and the test suite source code. I don't think it's worth the effort to make it a proper library. */ using namespace std; #include #include #include #include "l7-parse-patterns.h" // Returns true if the line (from a pattern file) is a comment static int is_comment(string line) { // blank lines are comments if(line.size() == 0) return 1; // lines starting with # are comments if(line[0] == '#') return 1; // lines with only whitespace are comments for(unsigned int i = 0; i < line.size(); i++) if(!isspace(line[i])) return 0; return 1; } // Extracts the protocol name from a line // This line should be exactly the name of the file without the .pat extension // However, we also allow junk after whitespace static string get_protocol_name(string line) { string name = ""; for(unsigned int i = 0; i < line.size(); i++) { if(!isspace(line[i])) name += line[i]; else break; } return name; } // Returns the given file name from the last slash to the next dot string basename(string filename) { int lastslash = filename.find_last_of('/'); int nextdot = filename.find_first_of('.', lastslash); return filename.substr(lastslash+1, nextdot - (lastslash+1)); } // Returns, e.g. "userspace pattern" if the line is "userspace pattern=.*foo" static string attribute(string line) { return line.substr(0, line.find_first_of('=')); } // Returns, e.g. ".*foo" if the line is "userspace pattern=.*foo" static string value(string line) { return line.substr(line.find_first_of('=')+1); } // parse the regexec and regcomp flags // Returns 1 on sucess, 0 if any unrecognized flags were encountered static int parseflags(int & cflags, int & eflags, string line) { string flag = ""; cflags = 0; eflags = 0; for(unsigned int i = 0; i < line.size(); i++){ if(!isspace(line[i])) flag += line[i]; if(isspace(line[i]) || i == line.size()-1){ if(flag == "REG_EXTENDED") cflags |= REG_EXTENDED; else if(flag == "REG_ICASE") cflags |= REG_ICASE; else if(flag == "REG_NOSUB") cflags |= REG_NOSUB; else if(flag == "REG_NEWLINE") cflags |= REG_NEWLINE; else if(flag == "REG_NOTBOL") eflags |= REG_NOTBOL; else if(flag == "REG_NOTEOL") eflags |= REG_NOTEOL; else{ cerr<<"Error: encountered unknown flag in pattern file " <