explicit IpAddress(const char *text)                { ipset(text); }
 IpAddress(const IpAddress& other)                   { ipset(other); }
 inline IpAddress & operator = ( const IpAddress &other )
 {
     ipset(other);
     return *this;
 }
 IpAddress()                                         { ipset(NULL); }
Exemple #5
0
int main( int argc, char* argv[] )
{
	int errors = 0;


	// check the arguments
	//
	if( argc < 2 || argc > 3 )
	{
		printUsage(          );
		exit      ( ++errors );
	}



	// create the set names
	//
	std::string setName   ( argv[ 1 ] );
	std::string tmpSetName( "new_"    );
	tmpSetName.append( setName );



	// Determine the input file
	//
	std::string consensus;

	if( argc == 3 )

		consensus = argv[ 2 ];

	else

		consensus = DEFAULT_CONSENSUS;



	// Create the set of ip's
	//
	std::ifstream inputFile( consensus.c_str() );


	if( !inputFile )
	{
		std::cerr << "Error: Can't open consensus file. Do you have the right permissions?" << std::endl;
		exit( ++errors );
	}


	std::stringstream inputStream;

	inputStream << inputFile.rdbuf();

	torset::IpsetRestore ipset( inputStream, tmpSetName );
	errors += ipset.errorCode();


	// Create the restore file for ipset
	//
	std::string toRestore;

	toRestore

		.append( "create "   ).append( setName    ).append( " hash:ip,port -exist\n" )                // create set
		.append( "create "   ).append( tmpSetName ).append( " hash:ip,port -exist\n" )                // create temporary set
		.append( "flush "    ).append( tmpSetName ).append( "\n" )                                    // make sure the temporary set is empty
		.append( ipset.set() )                                                                        // add the ip's to the temporary set
		.append( "swap "     ).append( setName    ).append( " " ).append( tmpSetName ).append( "\n" ) // swap the two sets over
		.append( "destroy "  ).append( tmpSetName ).append( "\n" )                                    // delete the temporary set
	;


	// feed it all to ipset
	//
	// FILE* ipsetSTDIN = popen( "ipset restore", "w" );

	// fputs( toRestore.c_str(), ipsetSTDIN );

	// pclose( ipsetSTDIN );


	// return on stdout so people can pipe it to ipset
	//
	std::cout << toRestore << std::endl;

	exit( errors );
}