Example #1
0
int ParseCommandLine(int argc, char *argv[])
{
int CurrentItem;

	CurrentItem= 1;

	// Default values
	ConfigParams.PromiscuousMode= 1;		// PCAP_OPENFLAG_PROMISCUOUS;
	ConfigParams.CaptureFileName= NULL;
	ConfigParams.AdapterName[0]= 0;
	ConfigParams.SaveFileName= NULL;
	ConfigParams.FilterString= NULL;
	ConfigParams.UseJit= false;
	ConfigParams.PrintingMode= DEFAULT;
	ConfigParams.RotateFiles= 0;
	ConfigParams.NPackets= -1;

	ConfigParams.NBackends= 1;
	strcpy(ConfigParams.DumpCodeFilename, nbNETPFLCOMPILER_DEBUG_NETIL_CODE_FILENAME);
	ConfigParams.StopAfterDumpCode= false;
	ConfigParams.DumpCode= true;
	ConfigParams.Backends[0].Id= 0;
	ConfigParams.Backends[0].Optimization= true;
	ConfigParams.Backends[1].Id= -1;
	ConfigParams.Backends[1].Optimization= true;

	ConfigParams.IPAnonFileName= NULL;
	ConfigParams.IPAnonFieldsList= NULL;

#ifdef  ENABLE_SQLITE3
	ConfigParams.SQLDatabaseFileBasename= NULL;
	ConfigParams.SQLTableName= (char*) "DefaultDump";
	ConfigParams.SQLTransactionSize= 0;
#endif

// End defaults


	while (CurrentItem < argc)
	{

		if (strcmp(argv[CurrentItem], "-h") == 0)
		{
			Usage();
			return nbFAILURE;
		}

		if (strcmp(argv[CurrentItem], "-netpdl") == 0)
		{
			ConfigParams.NetPDLFileName= argv[CurrentItem+1];
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-r") == 0)
		{
			if (ConfigParams.AdapterName[0] != 0)
			{
				printf("Cannot specify the capture interface and a capture file at the same time.\n");
				return nbFAILURE;
			}

			ConfigParams.CaptureFileName= argv[CurrentItem+1];
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-w") == 0)
		{
			ConfigParams.SaveFileName= argv[CurrentItem+1];
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-C") == 0)
		{
			ConfigParams.RotateFiles= atoi(argv[CurrentItem+1]);
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-c") == 0)
		{
			ConfigParams.NPackets= atoi(argv[CurrentItem+1]);
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-cp") == 0)
		{
			if (ConfigParams.PrintingMode != DEFAULT)
			{
				printf("Error with output specifier '-cp': another format specifier has already been specified.\n");
				return nbFAILURE;
			}
			ConfigParams.PrintingMode= CP;
			CurrentItem++;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-scp") == 0)
		{
			if (ConfigParams.PrintingMode != DEFAULT)
			{
				printf("Error with output specifier '-scp': another format specifier has already been specified.\n");
				return nbFAILURE;
			}
			ConfigParams.PrintingMode= SCP;
			CurrentItem++;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-scpt") == 0)
		{
			if (ConfigParams.PrintingMode != DEFAULT)
			{
				printf("Error with output specifier '-scpt': another format specifier has already been specified.\n");
				return nbFAILURE;
			}
			ConfigParams.PrintingMode= SCPT;
			CurrentItem++;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-scpn") == 0)
		{
			if (ConfigParams.PrintingMode != DEFAULT)
			{
				printf("Error with output specifier '-scpn': another format specifier has already been specified.\n");
				return nbFAILURE;
			}
			ConfigParams.PrintingMode= SCPN;
			CurrentItem++;
			continue;
		}

#ifdef ENABLE_SQLITE3
		if (strcmp(argv[CurrentItem], "-sqldb") == 0)
		{
			if (ConfigParams.PrintingMode != DEFAULT)
			{
				printf("Error with output specifier '-sqldb': another format specifier has already been specified.\n");
				return nbFAILURE;
			}
			ConfigParams.PrintingMode= SQLITE3;
			ConfigParams.SQLDatabaseFileBasename = argv[CurrentItem+1];
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-sqltable") == 0)
		{
			ConfigParams.SQLTableName= argv[CurrentItem+1];
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-sqltransize") == 0)
		{
			// Please note that in SQLite 3 no changes can be made to the database except within a transaction.
			// Any command that changes the database (basically, any SQL command other than SELECT)
			// will automatically start a transaction if one is not already in effect.
			// Automatically started transactions are committed when the last query finishes.
			//
			// The 'SQLTransactionSize' is used to define manually the transactions and group
			// multiple insertions in the same commit, which boosts the efficiency quite a lot.

			ConfigParams.SQLTransactionSize = atoi( argv[CurrentItem+1] );
			CurrentItem+= 2;
			continue;
		}
#endif

		if (strcmp(argv[CurrentItem], "-anonip") == 0)
		{
			ConfigParams.IPAnonFileName= argv[CurrentItem+1];
			ConfigParams.IPAnonFieldsList= argv[CurrentItem+2];
			CurrentItem+= 3;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-jit") == 0)
		{
			ConfigParams.UseJit= true;
			CurrentItem+= 1;
			continue;
		}


		if (strcmp(argv[CurrentItem], "-p") == 0)
		{
			ConfigParams.PromiscuousMode=0;
			CurrentItem+= 1;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-D") == 0)
		{
			PrintAdapters();
			return nbFAILURE;
		}

		if (strcmp(argv[CurrentItem], "-noopt") == 0)
		{
			ConfigParams.Backends[0].Optimization= false;
			CurrentItem+=1;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-backends") == 0)
		{
			PrintBackends();
			return nbFAILURE;
		}

		if (strcmp(argv[CurrentItem], "-d") == 0)
		{
		nvmBackendDescriptor *BackendList;

			CurrentItem++;

			int id= -1;

			// Get the list of available backends
			BackendList= nvmGetBackendList(&(ConfigParams.NBackends));
			// We consider also the 'netil' backend, which is not returned back by the previous function
			ConfigParams.NBackends++;

			if (CurrentItem < argc)	// we have to avoid the case in which no other parameters follow
				sscanf(argv[CurrentItem], "%d", &id);

			if (id == -1)
			{
				if (CurrentItem < argc)
				{
					id= GetBackendIdFromName(argv[CurrentItem], BackendList);
					if (id == -1)
					{
						printf("\n\tBackend '%s' is not supported. Please use the '-backends' switch to get\n", argv[CurrentItem]);
						printf("\tthe list of available backends.\n");
						return nbFAILURE;
					}
					else
						CurrentItem++;
				}
				else
				{
					printf("\n\tAfter the -d switch you need to specify the backend to use. Please use the '-backends'\n");
					printf("\tswitch to get the list of available backends.\n");
					return nbFAILURE;
				}
			}
			else
				CurrentItem++;

			if (id == 0)
			{
				// the default backend should be used
				ConfigParams.Backends[1].Id= -1;
				ConfigParams.Backends[1].Inline= 0;
				ConfigParams.Backends[1].Optimization= true;
			}

			int bid=id;

			if (id > 0)
			{
				bid= 1;
				ConfigParams.Backends[bid].Id= id;
			}

			if ((CurrentItem < argc) && strcmp(argv[CurrentItem], "noopt")==0)
			{
				ConfigParams.Backends[bid].Optimization= false;
				CurrentItem++;
			}

			if ((CurrentItem < argc) && strcmp(argv[CurrentItem], "inline")==0)
			{
				ConfigParams.Backends[bid].Inline= true;
				CurrentItem++;
			}


			if (ValidateDumpBackend(id, BackendList) == true)
			{
				ConfigParams.StopAfterDumpCode= true;
				ConfigParams.DumpCode=true;
			}
			continue;
		}

		if (strcmp(argv[CurrentItem], "-i") == 0)
		{
		char* Interface;
		int InterfaceNumber;

			if (ConfigParams.CaptureFileName != NULL)
			{
				printf("Cannot specify the capture interface and a capture file at the same time.\n");
				return nbFAILURE;
			}

			Interface= argv[CurrentItem+1];
			InterfaceNumber= atoi(Interface);

			// If we specify the interface by name, let's copy it in the proper structure
			// Otherwise, let's transform the interface index into a name
			if (InterfaceNumber == 0)
			{
				strncpy(ConfigParams.AdapterName, Interface, sizeof(ConfigParams.AdapterName));
				ConfigParams.AdapterName[sizeof(ConfigParams.AdapterName) - 1]= 0;
			}
			else
			{
				if (GetAdapterName(InterfaceNumber, ConfigParams.AdapterName, sizeof(ConfigParams.AdapterName)) == nbFAILURE)
				{
					printf("Error: the requested adapter is out of range.\n");
					return nbFAILURE;
				}
			}

			CurrentItem+=2;
			continue;
		}


		if (argv[CurrentItem][0] == '-')
		{
			printf("\n\tError: parameter '%s' is not valid.\n", argv[CurrentItem]);
			return nbFAILURE;
		}

		// We do not have any '-' switch; so, it should be the filter string
		ConfigParams.FilterString = argv[CurrentItem];
		CurrentItem += 1;
		continue;
	}

	if ((ConfigParams.CaptureFileName == NULL) && (ConfigParams.AdapterName[0] == 0))
	{
		printf("Neither the capture file nor the capturing interface has been specified.\n");
		printf("Using the first adapter as default.\n");

		if (GetAdapterName(1, ConfigParams.AdapterName, sizeof(ConfigParams.AdapterName)) == nbFAILURE)
		{
			printf("Error when trying to retrieve the name of the first network interface.\n");
			printf("    Make sure that %s is installed, and that you have the right\n", pcap_lib);
			printf("    permissions to open the network adapters.\n\n");
			return nbFAILURE;
		}
	}

	if (ConfigParams.FilterString == NULL)
	{
		printf("\n\tCommand line error: missing the filtering/extraction string\n  (e.g. 'ip extractfields(ip.src,ip.dst)').\n");
		return nbFAILURE;
	}

	if ((ConfigParams.IPAnonFileName != NULL) && (ConfigParams.IPAnonFieldsList == NULL))
	{
		printf("\n\tCommand line error: IP anonymization argument missing!\n");
		return nbFAILURE;
	}

	return nbSUCCESS;
}
Example #2
0
int ParseCommandLine(int argc, char *argv[])
{
int CurrentItem;

	CurrentItem= 1;

	// Default values
	ConfigParams.PromiscuousMode= 1;//PCAP_OPENFLAG_PROMISCUOUS;
	ConfigParams.DebugLevel= 0;
	ConfigParams.CaptureFileName= NULL;
	ConfigParams.AdapterName[0]= 0;
	ConfigParams.FilterString= NULL;
	ConfigParams.NPackets= -1;
	ConfigParams.SnapLen= 65535;
	ConfigParams.SaveFileName= NULL;
	ConfigParams.PcapDumpFile= NULL;
	ConfigParams.StopAfterDumpCode= false;
	ConfigParams.DumpCode= true;
	ConfigParams.QuietMode= false;
	ConfigParams.RotateFiles= 0;
	ConfigParams.UseJit= false;
	ConfigParams.NBackends= 1;
	ConfigParams.Backends[0].Id= 0;
	ConfigParams.Backends[0].Optimization= true;
	ConfigParams.Backends[1].Id= -1;
	ConfigParams.Backends[1].Optimization= true;

#ifdef	_DEBUG
	ConfigParams.DumpCodeFilename = nbNETPFLCOMPILER_DEBUG_NETIL_CODE_FILENAME;
	ConfigParams.DumpHIRCodeFilename = nbNETPFLCOMPILER_DEBUG_HIR_CODE_FILENAME;
	ConfigParams.DumpLIRCodeFilename = nbNETPFLCOMPILER_DEBUG_LIR_CODE_FILENAME;
	ConfigParams.DumpNetILGraphFilename = nbNETPFLCOMPILER_DEBUG_NETIL_GRAPH_FILENAME;
	ConfigParams.DumpNoCodeGraphFilename = nbNETPFLCOMPILER_DEBUG_NO_CODE_GRAPH_FILENAME;
	ConfigParams.DumpLIRNoOptGraphFilename = nbNETPFLCOMPILER_DEBUG_LIR_NOOPT_GRAPH_FILENAME;
	ConfigParams.DumpLIRGraphFilename = nbNETPFLCOMPILER_DEBUG_LIR_GRAPH_FILENAME;
	ConfigParams.DumpProtoGraphFilename = nbNETPFLCOMPILER_DEBUG_PROTOGRAH_DUMP_FILENAME;
#endif
	// End defaults

	while (CurrentItem < argc)
	{

		if (strcmp(argv[CurrentItem], "-netpdl") == 0)
		{
			ConfigParams.NetPDLFileName= argv[CurrentItem+1];
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-r") == 0)
		{
			if (ConfigParams.AdapterName[0] != 0)
			{
				printf("Cannot specify the capture interface and a capture file at the same time.\n");
				return nbFAILURE;
			}

			ConfigParams.CaptureFileName= argv[CurrentItem+1];
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-w") == 0)
		{
			ConfigParams.SaveFileName= argv[CurrentItem+1];
			CurrentItem+= 2;

			//Set quiet mode to 'true'
			ConfigParams.QuietMode= true;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-C") == 0)
		{
			ConfigParams.RotateFiles= atoi(argv[CurrentItem+1]);
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-h") == 0)
		{
			Usage();
			return nbFAILURE;
		}

		if (strcmp(argv[CurrentItem], "-jit") == 0)
		{
			ConfigParams.UseJit = true;
			CurrentItem+= 1;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-p") == 0)
		{
			ConfigParams.PromiscuousMode=0;
			CurrentItem+= 1;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-c") == 0)
		{
			ConfigParams.NPackets= atoi(argv[CurrentItem+1]);
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-s") == 0)
		{
			ConfigParams.SnapLen= atoi(argv[CurrentItem+1]);
			CurrentItem+= 2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-n") == 0)
		{
			ConfigParams.DoNotPrintNetworkNames=true;
			CurrentItem+= 1;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-q") == 0)
		{
			ConfigParams.QuietMode= true;
			CurrentItem+= 1;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-D") == 0)
		{
			PrintAdapters();
			return nbFAILURE;
		}

		if (strcmp(argv[CurrentItem], "-backends") == 0)
		{
			PrintBackends();
			return nbFAILURE;
		}

		if (strcmp(argv[CurrentItem], "-i") == 0)
		{
        char* Interface;
		int InterfaceNumber;

			if (ConfigParams.CaptureFileName != NULL)
			{
				printf("Cannot specify the capture interface and a capture file at the same time.\n");
				return nbFAILURE;
			}

			Interface= argv[CurrentItem+1];
			InterfaceNumber= atoi(Interface);

			// If we specify the interface by name, let's copy it in the proper structure
			// Otherwise, let's transform the interface index into a name
			if (InterfaceNumber == 0)
			{
				strncpy(ConfigParams.AdapterName, Interface, sizeof(ConfigParams.AdapterName));
				ConfigParams.AdapterName[sizeof(ConfigParams.AdapterName) - 1]= 0;
			}
			else
			{
				if (GetAdapterName(InterfaceNumber, ConfigParams.AdapterName, sizeof(ConfigParams.AdapterName)) == nbFAILURE)
				{
					printf("Error: the requested adapter is out of range.\n");
					return nbFAILURE;
				}
			}

			CurrentItem+=2;
			continue;
		}

		// Limit the number of optimization cycles
		if (strcmp(argv[CurrentItem], "-noopt") == 0)
		{
			ConfigParams.Backends[0].Optimization=false;
			CurrentItem+=1;
			continue;
		}

		// Debugging info levels
		if (strcmp(argv[CurrentItem], "-debug") == 0)
		{
			ConfigParams.DebugLevel= atoi(argv[CurrentItem+1]);
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-d") == 0)
		{
		nvmBackendDescriptor *BackendList;

			CurrentItem++;

			int id= -1;

			// Get the list of available backends
			BackendList= nvmGetBackendList(&(ConfigParams.NBackends));
			// We consider also the 'netil' backend, which is not returned back by the previous function
			ConfigParams.NBackends++;

			if (CurrentItem < argc)	// we have to avoid the case in which no other parameters follow
				sscanf(argv[CurrentItem], "%d", &id);

			if (id == -1)
			{
				if (CurrentItem < argc)
				{
					id= GetBackendIdFromName(argv[CurrentItem], BackendList);
					if (id == -1)
					{
						printf("\n\tBackend '%s' is not supported. Please use the '-backends' switch\n", argv[CurrentItem]);
						printf("\tto get the list of available backends.\n");
						return nbFAILURE;
					}
					else
						CurrentItem++;
				}
				else
				{
					printf("\n\tAfter the -d switch you need to specify the backend to use. Please use\n");
					printf("\tthe '-backends' switch to get the list of available backends.\n");
					return nbFAILURE;
				}
			}
			else
				CurrentItem++;

			if (id == 0)
			{
				// the default backend should be used
				ConfigParams.Backends[1].Id= -1;
				ConfigParams.Backends[1].Inline= false;
				ConfigParams.Backends[1].Optimization= true;
			}

			int bid=id;

			if (id > 0)
			{
				bid=1;
				ConfigParams.Backends[bid].Id=id;
			}

			if ((CurrentItem < argc) && strcmp(argv[CurrentItem], "noopt")==0)
			{
				ConfigParams.Backends[bid].Optimization=false;
				CurrentItem++;
			}

			if ((CurrentItem < argc) && strcmp(argv[CurrentItem], "inline")==0)
			{
				ConfigParams.Backends[bid].Inline= true;
				CurrentItem++;
			}


			if ((CurrentItem < argc) && strcmp(argv[CurrentItem], "-o") == 0)
			{
				ConfigParams.DumpCodeFilename = argv[CurrentItem+1];
				CurrentItem += 2;
			}

			if (ValidateDumpBackend(id, BackendList)==true)
			{
				ConfigParams.StopAfterDumpCode = true;
				ConfigParams.DumpCode=true;
			}
			continue;
		}

#ifdef	_DEBUG
		if (strcmp(argv[CurrentItem], "-dump_netil_code") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpCodeFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-dump_lir_code") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpLIRCodeFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-dump_hir_code") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpHIRCodeFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-dump_netil_graph") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpNetILGraphFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-dump_lir_graph") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpLIRGraphFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-dump_lir_no_opt_graph") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpLIRNoOptGraphFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-dump_no_code_graph") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpNoCodeGraphFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}

		if (strcmp(argv[CurrentItem], "-dump_proto_graph") == 0)
		{
			if (strncmp(argv[CurrentItem+1], "-", 255) != 0)
				ConfigParams.DumpProtoGraphFilename = argv[CurrentItem+1];
			CurrentItem+=2;
			continue;
		}
#endif

		if (argv[CurrentItem][0] == '-')
		{
			printf("\n\tError: parameter '%s' is not valid.\n", argv[CurrentItem]);
			return nbFAILURE;
		}

		// Current parameter is the filter string, which does not have any switch (e.g. '-something') in front
		if (ConfigParams.FilterString != NULL)
		{
			printf("\n\tError: only one filter must be defined without an option switch.\n");
			return nbFAILURE;
		}

		ConfigParams.FilterString = argv[CurrentItem];
		CurrentItem += 1;
		continue;
	}

	if ((ConfigParams.DumpCode) && (ConfigParams.StopAfterDumpCode) && ((ConfigParams.FilterString==NULL) || (ConfigParams.FilterString[0]=='\0')))
	{
		printf("\n\tNo filter specified! To dump the generated code you need to specify the filter to use.\n");
		return nbFAILURE;
	}

	if ((ConfigParams.DumpCode == false || ConfigParams.StopAfterDumpCode == false))
	{
		if ((ConfigParams.CaptureFileName == NULL) && (ConfigParams.AdapterName[0] == 0))
		{
            printf("Neither the capture file nor the capturing interface has been specified.\n");
			printf("Using the first adapter as default.\n");

			if (GetAdapterName(1, ConfigParams.AdapterName, sizeof(ConfigParams.AdapterName)) == nbFAILURE)
			{
				printf("Error when trying to retrieve the name of the first network interface.\n");
				printf("    Make sure that %s is installed, and that you have the right\n", pcap_lib);
				printf("    permissions to open the network adapters.\n\n");
				return nbFAILURE;
			}
		}
	}

	return nbSUCCESS;
}