library/src/network/tor/BP-Tor.cc
// thread entry point for our application handler.  This must be
// added to whichever event handler you want to hook it into at runtime
// in order for it to work. (BP_EventMgrAddProcessor)
thread entry point for our application handler.  This must be
added to whichever event handler you want to hook it into at runtime
in order for it to work.
thread entry point for our application handler . This must be added to whichever event handler you want to hook it into at runtime in order for it to work .
BP_EVENT_PROCESSOR_FUNC( BP_TorThreadCallbackEntryPoint )
{
	BP_EVENT_PROCESSOR_ENTRY;
	P_BP_TOR_EVENT_DATA tor_event_data = (P_BP_TOR_EVENT_DATA) event->event_data;
	if(!tor_event_data)
	{
		BP_EVENT_PROCESSOR_EXIT;
	}
	P_BP_TOR tor = tor_event_data->tor;
	if(!tor)
	{
		printf("\n [!!] Critical: tor session has not been passed in.");
		BP_EVENT_PROCESSOR_EXIT;
	}
	if(tor_event_data->event_type == BP_TOR_EVENT_START_TOR)
	{
		tor_event_data->status = BP_TOR_EVENT_STATUS_STARTING_UP;
		BP_TOR_EVENT_RETURN_CODE return_code;
		BP_TOR_EVENT_STATUS status;
		%%%%%%%%%%%%
		%%%%%%%%%%%%
		char *tor_params[] =
		{
				tor->tor_bin_path,
				"-f",
				tor->torrc_full_file_path,
				NULL
		};
		tor->tor_app = BP_FExec
		(
				tor->tor_bin_path,
				(char **) &tor_params,
				tor->tor_bin_envp,
				BP_TRUE
		);
		%%%%%%%%%%%%%%%
		%%%%%%%%%%%%%%%
		P_BP_CC_SESSION tor_cc_session = BP_CCNewSession
		(
				tor->tor_app,
			   "command_control_interface_ut",
			   BP_CC_SHELL_LINUX_EXECVE
		);
		tor_cc_session->display_reads_from_app_stdout = BP_TRUE;
		tor_cc_session->display_reads_from_app_stderr = BP_TRUE;
		tor_cc_session->display_writes_to_app_stdin   = BP_FALSE;
		tor_cc_session->select_child_stdout_timeout_sec  = 30;
		tor_cc_session->select_child_stdout_timeout_usec = 500;
		BP_ERROR_T ret_val = ERR_FAILURE;
		%%%%%%%%%%%%%%
		%%%%%%%%%%%%%%
		BP_CCAddCommand(tor_cc_session, "start_and_monitor_tor");
		char *tor_error_str = "Bootstrapped 100%: Done.";
		ret_val = BP_CCAddTryExpectToCommand
		(
			tor_cc_session,
			"start_and_monitor_tor",
			NULL,
			0,
			tor_error_str,
			bpstrlen(tor_error_str),
			BP_CC_MATCH_BUFF_POLICY_MEM_SEARCH
		);
		if(!BP_CCDispatcher(tor_cc_session, "start_and_monitor_tor", NULL))
		{
			printf("\n [!!] Command-and-Control Session Dispatcher failed for start_and_monitor_tor.");
			exit(0);
		}
		%%%%%%%%%%%%%%%%%%%%%
		%%%%%%%%%%%%%%%%%%%%%
		gettimeofday(&tor->service_start_time, NULL);
		printf("\n [+] TOR started and dispatcher has completed.  We are bootstrapped at 100%%");
		tor_event_data->status = BP_TOR_EVENT_STATUS_RUNNING;
	}
	if(tor_event_data->event_type == BP_TOR_EVENT_STOP_TOR)
	{
		tor_event_data->status = BP_TOR_EVENT_STATUS_STOPPING;
	    BP_BOOL still_running = BP_TRUE;
	    do
	    {
	    	still_running = BP_FExecApplicationStillRunning(tor->tor_app) ? BP_TRUE : BP_FALSE;
	    	if(still_running)
	    	{
	    		BP_FExecKillApplication(tor->tor_app);
	    		sleep(1);
	    	}
	    } while(still_running);
	    tor_event_data->status = BP_TOR_EVENT_STATUS_FINISHED;
	    printf("\n [+] Tor process has stopped completely.");
	}
	BP_EVENT_PROCESSOR_EXIT;
}
BP_EVENT_PROCESSOR_FUNC ( BP_TorThreadCallbackEntryPoint ) { BP_EVENT_PROCESSOR_ENTRY ; P_BP_TOR_EVENT_DATA tor_event_data = ( P_BP_TOR_EVENT_DATA ) event -> event_data ; if ( ! tor_event_data ) { BP_EVENT_PROCESSOR_EXIT ; } P_BP_TOR tor = tor_event_data -> tor ; if ( ! tor ) { printf ( " \n " ) ; BP_EVENT_PROCESSOR_EXIT ; } if ( tor_event_data -> event_type == BP_TOR_EVENT_START_TOR ) { tor_event_data -> status = BP_TOR_EVENT_STATUS_STARTING_UP ; BP_TOR_EVENT_RETURN_CODE return_code ; BP_TOR_EVENT_STATUS status ; char * tor_params [ ] = { tor -> tor_bin_path , " " , tor -> torrc_full_file_path , NULL } ; tor -> tor_app = BP_FExec ( tor -> tor_bin_path , ( char * * ) & tor_params , tor -> tor_bin_envp , BP_TRUE ) ; P_BP_CC_SESSION tor_cc_session = BP_CCNewSession ( tor -> tor_app , " " , BP_CC_SHELL_LINUX_EXECVE ) ; tor_cc_session -> display_reads_from_app_stdout = BP_TRUE ; tor_cc_session -> display_reads_from_app_stderr = BP_TRUE ; tor_cc_session -> display_writes_to_app_stdin = BP_FALSE ; tor_cc_session -> select_child_stdout_timeout_sec = 30 ; tor_cc_session -> select_child_stdout_timeout_usec = 500 ; BP_ERROR_T ret_val = ERR_FAILURE ; BP_CCAddCommand ( tor_cc_session , " " ) ; char * tor_error_str = " " ; ret_val = BP_CCAddTryExpectToCommand ( tor_cc_session , " " , NULL , 0 , tor_error_str , bpstrlen ( tor_error_str ) , BP_CC_MATCH_BUFF_POLICY_MEM_SEARCH ) ; if ( ! BP_CCDispatcher ( tor_cc_session , " " , NULL ) ) { printf ( " \n " ) ; exit ( 0 ) ; } gettimeofday ( & tor -> service_start_time , NULL ) ; printf ( " \n " ) ; tor_event_data -> status = BP_TOR_EVENT_STATUS_RUNNING ; } if ( tor_event_data -> event_type == BP_TOR_EVENT_STOP_TOR ) { tor_event_data -> status = BP_TOR_EVENT_STATUS_STOPPING ; BP_BOOL still_running = BP_TRUE ; do { still_running = BP_FExecApplicationStillRunning ( tor -> tor_app ) ? BP_TRUE : BP_FALSE ; if ( still_running ) { BP_FExecKillApplication ( tor -> tor_app ) ; sleep ( 1 ) ; } } while ( still_running ) ; tor_event_data -> status = BP_TOR_EVENT_STATUS_FINISHED ; printf ( " \n " ) ; } BP_EVENT_PROCESSOR_EXIT ; }
thread entry point for our application handler.
thread entry point for our application handler .
// processor entry macro // set tor event data // printf("\n HERE HERE THIS IS THE EVENT DATA POINTER: %p (event) - %p (tor event)", event, tor_event_data); // set tor handle // ensure we have tor // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Process Start Event %%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // process start event if set // set the tor running status to starting up // return code for the specified event data // tor event status // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Set Exec Parameters and Execute Binary %%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // printf("\n Tor file path?: %s - service_dir: %s", tor->torrc_full_file_path, tor->service_dir); // set argv for application. // exec the file nvp) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Create Basic CC Session To Start TOR Service %%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set cc session // display reads and writes (values can be changed inbetween // command runs to disable output for certain commands, or // enable output for others) // Set stdout polling timeout (values are additive and can be reset // between dispatched commands to change the select() responsiveness // of the app being controlled.) // declare return value // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% 2) Add Auth Try/Expect Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Add some arbitrary commands to the command orchard. // set the expected shell prompt (it should be a $ at the end of the line.  It's just // coincidence that the end of line match char is $ as well) // Add some try/expect branches to some commands // attempt to detect password field // call reset command to reset terminal (terminal gets messed up somehow) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Display CC Information After %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set service start time now that it's officially started // dbg: attempt to visualize sessions // BP_CCVisualizeSession(tor_cc_session, BP_TRUE, BP_TRUE); // tor display message // set the tor running status to starting up // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Process Stop Event %%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // process stop event if set // set event status // check if the app is still running // check if application is still running // if the application is running, attempt to kill it and sleep one sec // set event status // processor entry macro