/** * $Id: parsing.h,v 1.4 2002/10/16 23:07:22 plasmahh Exp $ * $Revision: 1.4 $ * $Author: plasmahh $ * $Date: 2002/10/16 23:07:22 $ */ #ifndef __PARSING_H #define __PARSING_H #ifndef __DLLIST_H #include "dllist.h" #endif /** * This class will most likely have only static functions in order to encapsulate * most used parsing functions. What parsers are availabe, check the corresponding * class documentation. * @note Most of this functions will throw exceptions, if class specific, they are * from type x_parse. */ class parsing { private: static bool set_range ( mstring& rng , int * r1, int * r2 ); protected: public: /** * @brief Expand String to list of IPs * * This expands a string to a list of IPs. The string has to be in the following form : * 192.168.5.* (which will expand to all IPs beginning with 192.168.5.) * or * 192.168.5.4-80 (which will expand to all IPs beginning with 192.168.5. and a 4, 5, 6... 79, 80 at the end) * @note This function doesn't check for limits, which means it will even expand * "*.*.*.*" to all 256^4 = 2^32 possible IPs, which will take a long time and most likely eat up all of your memory * (unless u have something bout 40GB of RAM ;) * @param mstring& hosts which will be expanded if possible (If not,throw an exception) * @return dllist of strings, all containing the IPs which have to be scanned */ static dllist < mstring > expand_iplist ( const mstring& hosts ); }; dllist < mstring > parsing::expand_iplist ( const mstring& hosts ) { int r1pos, r2pos, r3pos; int range1_low, range1_high; int range2_low, range2_high; int range3_low, range3_high; int range4_low, range4_high; mstring range1, range2, range3, range4, dum; dllist < mstring > retlist; r1pos = hosts.lsearch("."); range1 = hosts.substring(1,r1pos-1); r2pos = hosts.lsearch(".",r1pos+1); range2 = hosts.substring (r1pos+1, r2pos-1); r3pos = hosts.lsearch(".",r2pos+1); range3 = hosts.substring (r2pos+1, r3pos-1); range4 = hosts.substring (r3pos+1, 0); if ( ! ( set_range ( range1, &range1_low, &range1_high ) && set_range ( range2, &range2_low, &range2_high ) && set_range ( range3, &range3_low, &range3_high ) && set_range ( range4, &range4_low, &range4_high ) ) ) { throw new x_parse("While expanding IP list",__FLF__); } /* cout << range1_low << ", " << range1_high << endl; cout << range2_low << ", " << range2_high << endl; cout << range3_low << ", " << range3_high << endl; cout << range4_low << ", " << range4_high << endl; cout << "==============================================" << endl; */ for ( int a=range1_low; a <= range1_high; a++ ) for ( int b=range2_low; b <= range2_high; b++ ) for ( int c=range3_low; c <= range3_high; c++ ) for ( int d=range4_low; d <= range4_high; d++ ) { //cout << a << "." << b << "." << c << "." << d << endl; dum.format ("%d.%d.%d.%d",a,b,c,d); retlist.add(dum); } return retlist; } bool parsing::set_range ( mstring& rng , int * r1, int * r2 ) { char * rest1,* rest2; int low, high; if ( rng == "*" ) // If it is a * we mus go from 0 to 255 { *r1 = 0; *r2 = 255; return true; } low = strtol(rng, &rest1,10 ); if ( low < 0 || low > 255 ) { return false; } if ( *rest1 == '\0' ) // if it is only a number, check if from 0 to 255, if not return wrong { *r1 = *r2 = low; if ( low >= 0 && low <= 255 ) { return true; } else { return false; } } // next it is possible to have something like 5-6 so check if it is negative value ;) high = strtol ( rest1, &rest2, 10); if ( *rest2 == '\0' ) { *r1 = low; *r2 = - high; if ( low >= 0 && low <=255 && -high >=0 && -high <=255 && - high >= low ) { return true; } else { return false; } } return false; } #endif /** *$Log: parsing.h,v $ *Revision 1.4 2002/10/16 23:07:22 plasmahh *fixed memory bugs in mstring *TODO : refine of exception structure * *Revision 1.3 2002/06/09 21:23:22 plasmahh *changed a few things that might be bugs * *Revision 1.2 2002/05/28 20:54:07 plasmahh *removed debug message * *Revision 1.1 2002/05/28 18:47:40 plasmahh *added more or less safe format function to the mstring class *introduced a parser class *some new cool exceptions * */