46 # include <winsock2.h>
50 using namespace dca_ipr;
52 const std::string S_ToolName =
"iprsample";
53 const std::string S_ToolVersion =
"1.3";
60 "<dca-redist-folder> <ticket> <product> <input-file>\n"
61 " dca-redist-folder - the folder where the DCA is installed\n"
62 " ticket - a valid ticket\n"
63 " product - the product associated with your ticket\n"
64 " input-file - a file with sample IP addresses\n\n"
72 # define DCA_BINDIR "bin/Win32"
74 # define DCA_BINDIR "bin/linux"
81 #define DCA_INITDIR "init"
86 #define DCA_LOGDIR "./logs"
98 static void SetupInitData(
const std::string& strRedistFolder,
117 static bool StartupLibraries()
122 WORD wVersionRequested = MAKEWORD( 2, 2 );
124 int err = WSAStartup( wVersionRequested, &wsaData );
126 std::cout <<
"Error on WSAStartup (" << err <<
") occured, aborting" <<
144 static void ShutdownLibraries()
165 const std::string& strProduct,
168 aLicenseData.
ticket = strTicket;
169 aLicenseData.
product = strProduct;
193 static void PrintDbConnectionInfo(
const DbConnection& aDbConnection )
198 std::cout <<
"IPR Database Version: " << databaseInformation.
versionString
211 struct tm *expirationTime = localtime( &expirationDate );
213 std::cout <<
"License Info:" << std::endl;
214 std::cout <<
" DCA is " << ( aLicense.
isLicensed() ?
"licensed." :
215 "not licensed." ) << std::endl;
216 std::cout <<
" MaxUsers:" << aLicense.
getMaxUsers() <<
220 std::cout <<
" Ticket:" << aLicense.
getTicket() <<
222 std::cout <<
" Session:" << aLicense.
getSession() <<
226 std::cout <<
" Expiration Date:" << asctime( expirationTime ) <<
236 std::cout <<
"IBM DCA Sample: " << S_ToolName <<
" (" << S_ToolVersion <<
246 static void PrintUsage(
const char *pSampleName )
248 std::cout << pSampleName <<
" usage:" << std::endl;
264 const std::string& strIndent =
"" )
269 int myValue = aIprClassificationResult.
value;
274 std::cout << strIndent <<
"IprCategory '" << myIprCategory.
name() <<
275 "' (id=" << myCatId <<
")" << std::endl;
280 if( numOfEnums > 0 ) {
284 const IprEnum myIprEnum = aIprEnums.
byId( enumId );
285 std::cout << strIndent <<
"\t IprEnum '" << myIprEnum.
name() <<
"' (id=" <<
286 enumId <<
")" << std::endl;
290 std::cout << strIndent <<
"\tValue " << myValue << std::endl;
305 std::cout <<
"Results: None" << std::endl;
311 std::cout <<
"Results (" << numOfResults <<
") categories" << std::endl;
318 PrintResult( myIprClassificationResult, myIprCategories, myIprEnums,
"\t" );
330 void DoCallIprClassification(
const std::vector<std::string>& aIpStrings,
334 std::vector< std::string >::const_iterator I = aIpStrings.begin();
335 std::vector< std::string >::const_iterator IEnd = aIpStrings.end();
340 for( ; I != IEnd; ++I ) {
341 const std::string& strIp = *I;
343 std::cout <<
"IP Classification ('" << strIp <<
"')..." << std::endl;
345 const Ip myIp( strIp.c_str(), strIp.length() );
346 myFR = aIprClassifier.
classify( myIp, myIprClassificationResults );
352 else PrintResults( myIprClassificationResults, aIprCategoriesInfo );
364 void TestIprClassification(
const IprClassifier& aIprClassifier,
366 const std::string& strFileName )
368 std::ifstream fstream( strFileName.c_str(), std::ios::in );
369 if ( !fstream.is_open() ) {
370 std::cout <<
"Error: File '" << strFileName <<
"' could not be opened!" << std::endl;
375 std::vector< std::string > myIpStrings;
377 while( std::getline( fstream, strLine ) ) {
378 if( strLine.empty() )
382 while( strLine[strLine.length()-1] ==
'\r' ||
383 strLine[strLine.length()-1] ==
'\n' )
384 strLine.erase( strLine.length() - 1 );
386 if( strLine.empty() )
389 if( strLine[0] ==
'#' )
393 myIpStrings.push_back( strLine );
398 if( !myIpStrings.empty() )
399 DoCallIprClassification( myIpStrings, aIprClassifier, aIprCategoriesInfo );
401 std::cout <<
"Could not read any IP address from file!" << std::endl;
412 int main(
int argc,
char *argv[] )
424 std::string strRedistFolder = argv[ 1 ];
425 const std::string strTicket = argv[ 2 ];
426 const std::string strProduct = argv[ 3 ];
427 const std::string strFileName = argv[ 4 ];
429 if( strRedistFolder.empty() ||
431 strProduct.empty() ||
432 strFileName.empty() )
439 const char c = strRedistFolder[ strRedistFolder.length() - 1 ];
440 if( c !=
'/' && c !=
'\\' )
441 strRedistFolder +=
"/";
444 if( !StartupLibraries() )
463 if( myLicense.
isLicensed( IprClassification::ID ) ) {
466 IprClassification::create( myDca, myLicense );
469 SetupConnectionData( myDbConnectionData );
475 PrintDbConnectionInfo( myDbConnection );
488 TestIprClassification( myClassifier, myCategoriesInfo,
495 catch(
const ExDca& ex ) {
496 std::cerr <<
"DCA Exception occured. Details: " << ex.
getDescription()
500 catch(
const std::exception& s ) {
501 std::cerr <<
"std::exception occured. Details: " << s.what() <<
"." <<
506 std::cerr <<
"Unknown exception caught." << std::endl;
DCA_ENUM_ID_TYPE enumByValue(int value) const
Returns the id of an enum item associated with the current IprCategory.
DCA_SIZE_TYPE enumsSize() const
Returns the count of IprEnum items associated with current IprCategory.
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Definition of the Ip class.
Exception class used in the DCA.
void InitCUrl()
Initializes libcurl. Do not use any DCA function before initializing libcurl.
std::string initDir
the directory in which the DCA init files are stored
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
IprEnums getEnums() const
Returns the contained IprEnums.
DatabaseInformation getDatabaseInformation() const
Returns information about the underlying database.
DCA_RESULT_TYPE getReturnCode() const
Returns the last error code (if any).
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
Defintion of the IprClassifier class.
Container class for all IPR enumeration objects.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
DCA_CATEGORY_ID_TYPE categoryId
A IPR category id.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
Stores the connection data for a database.
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
IprCategories getCategories() const
Returns the contained IprCategories.
IprEnum byId(DCA_ENUM_ID_TYPE id) const
Returns the IprEnum with the given id.
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
const DbType DBT_Ipr
Used to specify an IP Classification database when creating a dca::DbConnection instance.
Database connection class for a local or remote database.
Package header file for the IPR (IP Reputation) Classification Package.
The result item of a IPR classification.
#define DCA_LOGDIR
Relative directory for logfile(s).
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
static void PrintToolHeader()
Prints out the name and the version of this sample.
Use a License to initialize a classification package or a toolbox package.
bool isLicensed(DCA_MODULE_ID_TYPE id=0, bool force=false) const
Checks whether the given License is valid for the given module id.
static void PrintLicenseInfo(const License &aLicense)
Prints out the information about the provided License.
DCA_SIZE_TYPE size() const
Returns the number of classification results.
std::string ticket
The ticket as provided in the license.
This header includes all header files of the DCA Base Package.
bool useLocalDatabase
Set to true to connect to a local or custom database, set to false to use a remote database.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the category.
Encapsulates the init and deinit of the DCA API.
The IprEnum class describes a single enumerable item, for example the Geo Location "Greenland".
std::string product
The product code used with the license.
Definition of the IprClassificationResults class.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the enumerable item.
Definition of a container class for IprEnum objects.
int DCA_CATEGORY_ID_TYPE
Type for category ids.
std::string logDir
the directory in which the DCA log file should be created
static void PrintUsage(const char *name)
Prints out the syntax of the sample.
std::string getLastMessage() const
Returns the last message received from our license server or if none available the last available mes...
size_t DCA_INDEX_TYPE
Type for index access (used for arrays and collections).
DbType dbType
The type of the database.
IprClassifier createClassifier(const dca::DbConnection &aDbConnection, const IprClassifierOptions &aIprClassifierOptions=IprClassifierOptions()) const
Creates a IprClassifier that is used to classify dca_ipr::Ip objects.
dca::FunctionResult classify(const Ip &input, IprClassificationResults &result) const
Classification function that takes the given input and returns the results (if any) in the provided r...
std::string getDescription() const
Returns the description for the error or warning.
IprCategory byId(DCA_CATEGORY_ID_TYPE catid) const
Returns the category with the given category id.
size_t DCA_SIZE_TYPE
Type for size (used for size of array and collections).
Definition of a container class for IprCategory objects.
std::string getTicket() const
Returns the ticket of the license as string.
Definition of the IprClassification class.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
Standard function result.
IprCategoriesInfo getCategoriesInfo() const
Retrieve the IprCategoriesInfo class.
std::string getSession() const
Returns the session of the license as string.
int value
The value related to the given category.
bool isCategorized() const
If the classification returned a result this function returns true.
The IprCategory class contains information for a single category. An IP address can be assigned to on...
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
DbConnection createDbConnection(const License &aLicense, const DbConnectionData &dbcData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a DbConnection object using the given DbConnectionData.
This structure is used to initialize the DcaInstance.
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
static void SetupLicense(const std::string &ticket, const std::string &product, LicenseData &licenseData)
Sets up the given licenseData by copying the given ticket and product strings.
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
int main(int argc, char *argv[])
The main routine.