static int
method_session_stat_update(void *data, void *para) {
	struct session_info *s_info = (struct session_info *)data;
	pppoe_manage_t *manage = (pppoe_manage_t *)para;
	session_struct_t *sess;
	struct session_stat stat;
	char path[128], buf[1024];	
	uint32 sid;
	FILE *fp;

	if (unlikely(!s_info || !manage))
		return PPPOEERR_EINVAL;

	memset(path, 0, sizeof(path));
	snprintf(path, sizeof(path), "/proc/net/dev_pppoe/%s", s_info->ifname);

	fp = fopen(path, "r");
	if (!fp) {
		pppoe_log(LOG_WARNING, "%s open fail\n", path);
		return PPPOEERR_EOPEN;
	}

	fgets(buf, sizeof(buf), fp);

	while (fgets(buf, sizeof(buf), fp)) {
		if (5 != sscanf(buf, "%u %llu %llu %llu %llu", &sid, 
					&stat.rx_packets, &stat.rx_bytes,
					&stat.tx_packets, &stat.tx_bytes)) {
			pppoe_log(LOG_WARNING, "buf (%s) sscanf fail\n", buf);
			continue;
		}
		
		sess = manage_sess_get_by_sid(manage, sid);
		if (!sess) {
			continue;
		}

		if (sess->state > SESSION_ONLINE) {
			session_put(sess);
			continue;
		}

		sess->stat.rx_bytes = sess->bk_stat.rx_bytes + stat.rx_bytes;
		sess->stat.tx_bytes = sess->bk_stat.tx_bytes + stat.tx_bytes;

		sess->stat.rx_packets = sess->bk_stat.rx_packets + stat.rx_packets;
		sess->stat.tx_packets = sess->bk_stat.tx_packets + stat.tx_packets;
		
		session_put(sess);
	}

	fclose(fp);	
	pppoe_token_log(TOKEN_METHOD, "sessions %s stat update success\n", s_info->ifname);
	return PPPOEERR_SUCCESS;
}
Beispiel #2
0
static DBusMessage *sync_putphonebook(DBusConnection *connection,
			DBusMessage *message, void *user_data)
{
	struct session_data *session = user_data;
	struct sync_data *syncdata = session_get_data(session);
	const char *buf;
	char *buffer;

	if (dbus_message_get_args(message, NULL,
			DBUS_TYPE_STRING, &buf,
			DBUS_TYPE_INVALID) == FALSE)
		return g_dbus_create_error(message,
			ERROR_INF ".InvalidArguments", NULL);

	/* set default phonebook_path to memory internal phonebook */
	if (!syncdata->phonebook_path)
		syncdata->phonebook_path = g_strdup("telecom/pb.vcf");

	buffer = g_strdup(buf);

	if (session_put(session, buffer, syncdata->phonebook_path) < 0)
		return g_dbus_create_error(message,
				ERROR_INF ".Failed", "Failed");

	return dbus_message_new_method_return(message);
}
static inline int
session_wireless_info_set(pppoe_manage_t *manage, struct session_wireless_info *sess_info) {
	session_struct_t *sess
		= manage_sess_get_by_sid(manage, sess_info->info.sid);
	if (!sess) {
		pppoe_token_log(TOKEN_METHOD, "session %u is not exist\n", sess_info->info.sid);
		return PPPOEERR_ENOEXIST;
	}

	sess->wtp_id 		= sess_info->wtp_id;
	sess->radio_g_id 	= sess_info->radio_g_id;
	sess->bssindex 		= sess_info->bssindex;
	sess->vlan_id 		= sess_info->vlan_id;
	sess->wlan_id 		= sess_info->wlan_id;
	sess->security_id 	= sess_info->security_id;
	sess->radio_l_id 	= sess_info->radio_l_id;
	memcpy(sess->wtpmac, sess_info->wtpmac, ETH_ALEN);
	session_put(sess);

	pppoe_token_log(TOKEN_METHOD, "session %u: wtp %u radio %u wlan %u\n", 
									sess_info->info.sid, sess_info->wtp_id, 
									sess_info->radio_l_id, sess_info->wlan_id);	
	return PPPOEERR_SUCCESS;
}
Beispiel #4
0
/*  ----------------------------
 *          MONITOR CODE
 *  ---------------------------- */
void run_dtnperf_monitor(monitor_parameters_t * parameters)
{
	/* ------------------------
	 * variables
	 * ------------------------ */
	dtnperf_options_t * perf_opt = parameters->perf_g_opt->perf_opt;

	bp_error_t error;
	bp_endpoint_id_t local_eid;
	bp_reg_info_t reginfo;
	bp_reg_id_t regid;
	bp_bundle_object_t bundle_object;
	bp_bundle_status_report_t * status_report;
	bp_endpoint_id_t bundle_source_addr;
	bp_timestamp_t bundle_creation_timestamp;
	bp_timeval_t bundle_expiration;
	bp_endpoint_id_t relative_source_addr;
	bp_timestamp_t relative_creation_timestamp;
	HEADER_TYPE bundle_header;

	session_t * session;
	bundle_type_t bundle_type;
	struct timeval current, start;
	char * command;
	char temp[256];
	char * filename;
	int filename_len;
	char * full_filename;
	FILE * file;


	/* ------------------------
	 * initialize variables
	 * ------------------------ */
	boolean_t debug = perf_opt->debug;
	int debug_level = perf_opt->debug_level;

	dedicated_monitor = parameters->dedicated_monitor;
	bp_handle_open = FALSE;

	perf_opt->logs_dir = correct_dirname(perf_opt->logs_dir);

	status_report = NULL;
	session_list = session_list_create();

	// set out buffer size if daemon
	if (perf_opt->daemon)
	{
		setlinebuf(stdout);
		setlinebuf(stderr);
	}

	// create dir where dtnperf monitor will save logs
	// command should be: mkdir -p "logs_dir"
	if(debug && debug_level > 0)
		printf("[debug] initializing shell command...");
	command = malloc(sizeof(char) * (10 + strlen(perf_opt->logs_dir)));
	sprintf(command, "mkdir -p %s", perf_opt->logs_dir);
	if(debug && debug_level > 0)
		printf("done. Shell command = %s\n", command);

	// execute shell command
	if(debug && debug_level > 0)
		printf("[debug] executing shell command...");
	if (system(command) < 0)
	{
		perror("Error opening monitor logs dir");
		monitor_clean_exit(-1);
	}
	free(command);
	if(debug && debug_level > 0)
		printf("done\n");

	// signal handlers
	signal(SIGINT, monitor_handler);
	signal(SIGUSR1, monitor_handler);
	signal(SIGUSR2, monitor_handler);

	//open the connection to the bundle protocol router
	if(debug && debug_level > 0)
		printf("[debug] opening connection to bundle protocol router...");
	if (perf_opt->use_ip)
		error = bp_open_with_ip(perf_opt->ip_addr, perf_opt->ip_port, &handle);
	else
		error = bp_open(&handle);
	if (error != BP_SUCCESS)
	{
		fflush(stdout);
		fprintf(stderr, "fatal error opening bp handle: %s\n", bp_strerror(error));
		monitor_clean_exit(1);
	}
	else
	{
		bp_handle_open = TRUE;
	}
	if(debug && debug_level > 0)
		printf("done\n");

	//build a local eid
	if(debug && debug_level > 0)
		printf("[debug] building a local eid...");
	if (parameters->dedicated_monitor)
		sprintf(temp, "%s_%d", MON_EP_STRING, parameters->client_id);
	else
		sprintf(temp, "%s", MON_EP_STRING);
	bp_build_local_eid(handle, &local_eid, temp);
	if(debug && debug_level > 0)
		printf("done\n");
	if (debug)
		printf("local_eid = %s\n", local_eid.uri);

	// checking if there is already a registration
	if(debug && debug_level > 0)
		printf("[debug] checking for existing registration...");
	error = bp_find_registration(handle, &local_eid, &regid);
	if (error == BP_SUCCESS)
	{
		fflush(stdout);
		fprintf(stderr, "error: there is a registration with the same eid.\n");
		fprintf(stderr, "regid 0x%x\n", (unsigned int) regid);
		monitor_clean_exit(1);
	}
	if ((debug) && (debug_level > 0))
		printf(" done\n");

	//create a new registration to the local router based on this eid
	if(debug && debug_level > 0)
		printf("[debug] registering to local daemon...");
	memset(&reginfo, 0, sizeof(reginfo));
	bp_copy_eid(&reginfo.endpoint, &local_eid);
	reginfo.flags = BP_REG_DEFER;
	reginfo.regid = BP_REGID_NONE;
	reginfo.expiration = 0;
	if ((error = bp_register(handle, &reginfo, &regid)) != 0)
	{
		fflush(stdout);
		fprintf(stderr, "error creating registration: %d (%s)\n",
				error, bp_strerror(bp_errno(handle)));
		monitor_clean_exit(1);
	}
	if ((debug) && (debug_level > 0))
		printf(" done\n");
	if (debug)
		printf("regid 0x%x\n", (unsigned int) regid);

	// start expiration timer thread
	pthread_mutex_init (&mutexdata, NULL);
	pthread_create(&session_exp_timer, NULL, session_expiration_timer, (void *) parameters);

	if (!dedicated_monitor)
		printf("DTNperf Monitor started\n Waiting for bundles...\n");


	// start infinite loop
	while(1)
	{
		// reset variables
		bundle_type = NONE;

		// create a bundle object
		if ((debug) && (debug_level > 0))
			printf("[debug] initiating memory for bundles...\n");
		error = bp_bundle_create(&bundle_object);
		if (error != BP_SUCCESS)
		{
			fflush(stdout);
			fprintf(stderr, "fatal error initiating memory for bundles: %s\n", bp_strerror(error));
			monitor_clean_exit(1);
		}
		if(debug && debug_level > 0)
			printf("done\n");


		// wait until receive a bundle
		if ((debug) && (debug_level > 0))
			printf("[debug] waiting for bundles...\n");
		error = bp_bundle_receive(handle, bundle_object, BP_PAYLOAD_MEM, -1);
		if (error != BP_SUCCESS)
		{
			fflush(stdout);
			fprintf(stderr, "error getting recv reply: %d (%s)\n",
					error, bp_strerror(bp_errno(handle)));
			monitor_clean_exit(1);
		}
		if ((debug) && (debug_level > 0))
			printf(" bundle received\n");

		// mark current time
		if ((debug) && (debug_level > 0))
			printf("[debug] marking time...");
		gettimeofday(&current, NULL);
		if ((debug) && (debug_level > 0))
			printf(" done\n");

		// get SOURCE eid
		if ((debug) && (debug_level > 0))
			printf("[debug]\tgetting source eid...");
		error = bp_bundle_get_source(bundle_object, &bundle_source_addr);
		if (error != BP_SUCCESS)
		{
			fflush(stdout);
			fprintf(stderr, "error getting bundle source eid: %s\n",
					bp_strerror(error));
			monitor_clean_exit(1);
		}
		if ((debug) && (debug_level > 0))
		{
			printf(" done:\n");
			printf("\tbundle_source_addr = %s\n", bundle_source_addr.uri);
			printf("\n");
		}

		// get bundle CREATION TIMESTAMP
		if ((debug) && (debug_level > 0))
			printf("[debug]\tgetting bundle creation timestamp...");
		error = bp_bundle_get_creation_timestamp(bundle_object, &bundle_creation_timestamp);
		if (error != BP_SUCCESS)
		{
			fflush(stdout);
			fprintf(stderr, "error getting bundle creation timestamp: %s\n",
					bp_strerror(error));
			monitor_clean_exit(1);
		}
		if ((debug) && (debug_level > 0))
		{
			printf(" done:\n");
			printf("\tbundle creation timestamp:\n"
					"\tsecs = %d\n\tseqno= %d\n",
					(int)bundle_creation_timestamp.secs, (int)bundle_creation_timestamp.seqno);
			printf("\n");
		}

		// get bundle EXPIRATION TIME
		if ((debug) && (debug_level > 0))
			printf("[debug]\tgetting bundle expiration time...");
		error = bp_bundle_get_expiration(bundle_object, &bundle_expiration);
		if (error != BP_SUCCESS)
		{
			fflush(stdout);
			fprintf(stderr, "error getting bundle expiration time: %s\n",
					bp_strerror(error));
			monitor_clean_exit(1);
		}
		if ((debug) && (debug_level > 0))
		{
			printf(" done:\n");
			printf("\tbundle expiration: %lu\n", bundle_expiration);
			printf("\n");
		}

		// check if bundle is a status report
		if ((debug) && (debug_level > 0))
			printf("[debug] check if bundle is a status report...\n");
		error = bp_bundle_get_status_report(bundle_object, &status_report);
		if (error != BP_SUCCESS)
		{
			fflush(stdout);
			fprintf(stderr, "error checking if bundle is a status report: %d (%s)\n",
					error, bp_strerror(bp_errno(handle)));
			continue;
		}
		if ((debug) && (debug_level > 0))
			printf(" %s\n", status_report == NULL ? "no" : "yes");

		// check for other bundle types
		if (status_report != NULL)
			bundle_type = STATUS_REPORT;
		else
		{
			get_bundle_header_and_options(&bundle_object, & bundle_header, NULL);

			if (bundle_header == FORCE_STOP_HEADER)
				bundle_type = CLIENT_FORCE_STOP;
			else if (bundle_header == STOP_HEADER)
				bundle_type = CLIENT_STOP;
			else if (bundle_header == DSA_HEADER)
				bundle_type = SERVER_ACK;
			else // unknown bundle type
			{
				fprintf(stderr, "error: unknown bundle type\n");
				continue;
			}
		}

		// retrieve or open log file
		pthread_mutex_lock(&mutexdata);

		session = NULL;

		switch (bundle_type)
		{
		case STATUS_REPORT:
			bp_copy_eid(&relative_source_addr, &(status_report->bundle_id.source));
			relative_creation_timestamp = status_report->bundle_id.creation_ts;
			break;

		case SERVER_ACK:
			get_info_from_ack(&bundle_object, &relative_source_addr, &relative_creation_timestamp);
			break;

		case CLIENT_STOP:
		case CLIENT_FORCE_STOP:
			bp_copy_eid(&relative_source_addr, &bundle_source_addr);
			relative_creation_timestamp = bundle_creation_timestamp;
			break;

		default:
			break;
		}

		session = session_get(session_list, relative_source_addr);

		if (session == NULL) // start a new session
		{
			// mark start time
			start = current;
			char * ptr;
			filename_len = strlen(relative_source_addr.uri) - strlen("dtn://") + 15;
			filename = (char *) malloc(filename_len);
			memset(filename, 0, filename_len);
			strncpy(temp, relative_source_addr.uri, strlen(relative_source_addr.uri) + 1);
			strtok(temp, "/");
			sprintf(filename, "%lu_", relative_creation_timestamp.secs);
			strcat(filename, strtok(NULL, "/"));

			// remove .dtn suffix from the filename
			ptr = strstr(filename, ".dtn");
			if (ptr != NULL)
				ptr[0] = '\0';

			// add .csv extension
			strcat(filename, ".csv");
			full_filename = (char *) malloc(strlen(perf_opt->logs_dir) + strlen(filename) + 2);
			sprintf(full_filename, "%s/%s", perf_opt->logs_dir, filename);
			file = fopen(full_filename, "w");
			session = session_create(relative_source_addr, full_filename, file, start,
					relative_creation_timestamp.secs, bundle_expiration);
			session_put(session_list, session);
			// write header in csv log file
			fprintf(file,"Mon_RX_TIME;BSR_OR_ACK_SOURCE;BSR_OR_ACK_TIMESTAMP;BSR_OR_ACK_SEQNO;"
					"TYPE;BUNDLE_X_SOURCE;BUNDLE_X_TIMESTAMP;BUNDLE_X_SEQNO;"
					"FRAG_OFFSET;FRAG_LENGTH;");
			csv_print_status_report_timestamps_header(file);
			csv_end_line(file);
		}

		// update session infos
		session->last_bundle_time = bundle_creation_timestamp.secs;
		session->expiration = bundle_expiration;
		file = session->file;
		memcpy(&start, session->start, sizeof(struct timeval));

		if (bundle_type == STATUS_REPORT && (status_report->flags & BP_STATUS_DELIVERED))
		{
			session->delivered_count++;
		}

		pthread_mutex_unlock(&mutexdata);

		// print rx time in csv log
		csv_print_rx_time(file, current, start);

		// print bundle source in csv log
		csv_print_eid(file, bundle_source_addr);

		//print bundle creation timestamp in csv log
		csv_print_timestamp(file, bundle_creation_timestamp);

		// print bundle type in csv log
		switch (bundle_type)
		{
		case CLIENT_STOP:
			csv_print(file, "CLIENT_STOP");
			break;
		case CLIENT_FORCE_STOP:
			csv_print(file, "CLIENT_FORCE_STOP");
			break;
		case SERVER_ACK:
			csv_print(file, "SERVER_ACK");
			break;
		case STATUS_REPORT:
			csv_print(file, "STATUS_REPORT");
			break;
		default:
			csv_print(file, "UNKNOWN");
			break;
		}

		// print relative source and timestamp
		if (bundle_type == SERVER_ACK || bundle_type == STATUS_REPORT)
		{
			csv_print_eid(file, relative_source_addr);
			csv_print_timestamp(file, relative_creation_timestamp);
		}

		// print status report infos in csv log
		if (bundle_type == STATUS_REPORT)
		{
			csv_print_ulong(file, status_report->bundle_id.frag_offset);
			csv_print_ulong(file, status_report->bundle_id.orig_length);
			csv_print_status_report_timestamps(file, * status_report);
		}

		// end line in csv log
		csv_end_line(file);

		// close file
		if (bundle_type == CLIENT_STOP)
		{
			int total_to_receive;
			get_info_from_stop(&bundle_object, &total_to_receive);
			pthread_mutex_lock(&mutexdata);
			session->total_to_receive = total_to_receive;
			session->wait_after_stop = bundle_expiration;
			gettimeofday(session->stop_arrival_time, NULL);
			pthread_mutex_unlock(&mutexdata);
		}
		else if (bundle_type == CLIENT_FORCE_STOP)
		{
			printf("DTNperf monitor: received forced end session bundle\n");
			session_close(session_list, session);
		}


	} // end loop

	session_list_destroy(session_list);
	bp_close(handle);
	bp_handle_open = FALSE;
}