Ejemplo n.º 1
0
static void * proc_control(void *args) {
	struct frame_manager *manager = get_frame_manager(CONTROL_MANAGER);
	struct record_manager * record_manager=get_record_manager();
	struct block_filter *filter = NULL;

	struct frame *pframe = NULL;
	struct block * pblock = NULL;
	char buffer[64];
	int length;
	char dest_addr = 0;
	char src_addr = 0;
	char operation = 0;
	char cmd = 0;
	char tmp;

	if (manager == NULL)
		return NULL;
	filter = get_frame_filter(manager);
	while (1) {
		pblock = get_block(filter, TIMEOUT_MAIN_UNIT, BLOCK_FULL);
		if (pblock != NULL) {
			set_sys_state(BIT5_MAIN_UNIT,STATE_MAINUNIT_OK);
			light_on(0);
			light_main_alarm(0);
			pframe = (struct frame*) pblock->data;
			pframe->length = pblock->data_length;
			dest_addr = destination_of_frame(pframe);
			src_addr = pframe->data[4];
			cmd = command_of_frame(pframe);
			operation = operation_of_frame(pframe);

			if (src_addr == MASTER_ADDRESS) {

				switch (dest_addr) {
				case RADIO_450M_ADDRESS:
					if ((operation == 0x03) && (cmd == 0x20)) { //向450M发送机车号
						char * cab_id = get_id();

						get_frame_real_data(pframe, buffer, &length);

						buffer[8] = 0;
						tmp = buffer[3];
						buffer[3] = 0;
						int cab_type = atoi(buffer);

						buffer[3] = tmp;

						sprintf(cab_id, "%s-%s", get_cab_code(cab_type),
								buffer + 3);

					}
					break;
				case ALL_MMI_ADDRESS:
					if ((operation == 0x03) && (cmd == 0x46)) { //关机命令

						flush_all_data(record_manager);
					}
					break;
				}

			}

			if (dest_addr == RECORD_ADDRESS) {

				switch (operation) {
				case 1: //维护信息
					// echo off
					tmp = 0;
					if (src_addr == 0x01) {
						tmp = 0x01;
					} else if ((src_addr == 0x03) || (src_addr == 0x04)) {
						tmp = 0x41;
					}
					send_frame(manager, RECORD_ADDRESS, src_addr, 1, tmp,
							pframe->data + 9, 2);

					switch (cmd) {
					case (char) 0x33: //播放控制
					{
						if (pframe->data[10] == (char) 0xff) {
							start_play(src_addr);
						} else if (pframe->data[10] == 0) {
							stop_play();
						}
					}
						break;
					case (char) 0xfa: //问询测试
						break;
					case (char) 0x34: //查询时钟
					{
						struct tm time;
						get_time(&time);

						buffer[0] = to_bcd(time.tm_year - 100); //year
						buffer[1] = to_bcd(time.tm_mon + 1);
						buffer[2] = to_bcd(time.tm_mday);
						buffer[3] = to_bcd(time.tm_hour);
						buffer[4] = to_bcd(time.tm_min);
						buffer[5] = to_bcd(time.tm_sec);
						send_frame(manager, RECORD_ADDRESS, src_addr, 1,
								(char) 0x91, buffer, 6);
					}
						break;
					case (char) 0x35: //设置时钟
						break;
					case (char) 0xa5: //查询软件版本
					{

						bcopy(version, buffer, 16);
						send_frame(manager, RECORD_ADDRESS, src_addr, 1,
								(char) 0xaa, buffer, 16);
					}
						break;

					}

					break;
				case 3: //不需要应答
					break;
				}
			}
			put_block(pblock, BLOCK_EMPTY);
		} else {
			set_sys_state(BIT5_MAIN_UNIT,STATE_MAINUNIT_FAIL);
			light_main_alarm(1);
		}
	}

	return NULL;
}
Ejemplo n.º 2
0
int store_packet(void *config, const struct ipfix_message *ipfix_msg,
		const struct ipfix_template_mgr *template_mgr)
{
	(void) template_mgr;
	std::map<uint16_t, template_table*>::iterator table;
	struct fastbit_config *conf = (struct fastbit_config *) config;
	std::map<uint16_t, template_table*> *templates = NULL;
	std::map<uint16_t, template_table*> *old_templates = NULL; /* Templates to be removed */

	std::map<std::string, std::map<uint32_t, od_info>*> *od_infos = conf->od_infos;
	std::map<std::string, std::map<uint32_t, od_info>*>::iterator exporter_it;
	std::map<uint32_t, od_info>::iterator odid_it;

	static int rcnt = 0;

	uint16_t template_id;
	uint32_t odid = ntohl(ipfix_msg->pkt_header->observation_domain_id);
	struct input_info_network *input = (struct input_info_network *) ipfix_msg->input_info;

	int rc_flows = 0;
	uint64_t rc_flows_sum = 0;

	char exporter_ip_addr_tmp[INET6_ADDRSTRLEN];
	if (input->l3_proto == 6) { /* IPv6 */
		ipv6_addr_non_canonical(exporter_ip_addr_tmp, &(input->src_addr.ipv6));
	} else { /* IPv4 */
		inet_ntop(AF_INET, &(input->src_addr.ipv4.s_addr), exporter_ip_addr_tmp, INET_ADDRSTRLEN);
	}

	/* Convert to C++ string for use in `od_infos` data structure */
	std::string exporter_ip_addr (exporter_ip_addr_tmp);

	/* Find exporter in od_infos data structure */
	if ((exporter_it = od_infos->find(exporter_ip_addr)) == od_infos->end()) {
		MSG_INFO(msg_module, "Received data for new exporter: %s", exporter_ip_addr.c_str());

		/* Add new exporter to data structure */
		std::map<uint32_t, od_info> *new_exporter = new std::map<uint32_t, od_info>;
		od_infos->insert(std::make_pair(exporter_ip_addr, new_exporter));
		exporter_it = od_infos->find(exporter_ip_addr);
	}

	/* Find ODID in template_info data structure (under exporter) */
	if ((odid_it = exporter_it->second->find(odid)) == exporter_it->second->end()) {
		MSG_INFO(msg_module, "Received new ODID for exporter %s: %u", exporter_ip_addr.c_str(), odid);

		/* Add new ODID to data structure (under exporter) */
		od_info new_odid;
		new_odid.exporter_ip_addr = exporter_ip_addr;
		new_odid.path = generate_path(conf, exporter_ip_addr, odid);

		exporter_it->second->insert(std::make_pair(odid, new_odid));
		odid_it = exporter_it->second->find(odid);
	}

	templates = &(odid_it->second.template_info);

	/* Process all datasets in message */
	int i;
	for (i = 0 ; i < MSG_MAX_DATA_COUPLES && ipfix_msg->data_couple[i].data_set; i++) {	
		if (ipfix_msg->data_couple[i].data_template == NULL) {
			/* Skip data couples without templates */
			continue;
		}

		template_id = ipfix_msg->data_couple[i].data_template->template_id;

		/* If template (ID) is unknown, add it to the template map */
		if ((table = templates->find(template_id)) == templates->end()) {
			MSG_DEBUG(msg_module, "Received new template: %hu", template_id);
			template_table *table_tmp = new template_table(template_id, conf->buff_size);
			if (table_tmp->parse_template(ipfix_msg->data_couple[i].data_template, conf) != 0) {
				/* Template cannot be parsed, skip data set */
				delete table_tmp;
				continue;
			}
			
			templates->insert(std::pair<uint16_t, template_table*>(template_id, table_tmp));
			table = templates->find(template_id);
		} else {
			/* Check template time. On reception of a new template it is crucial to rewrite the old one. */
			if (ipfix_msg->data_couple[i].data_template->first_transmission > table->second->get_first_transmission()) {
				MSG_DEBUG(msg_module, "Received new template with already used template ID: %hu", template_id);

				/* Init map for old template if necessary */
				if (old_templates == NULL) {
					old_templates = new std::map<uint16_t,template_table*>;
				}

				/* Store old template */
				old_templates->insert(std::pair<uint16_t, template_table*>(table->first, table->second));

				/* Flush data */
				flush_data(conf, exporter_ip_addr, odid, old_templates);

				/* Remove rewritten template */
				delete table->second;
				delete old_templates;
				old_templates = NULL;

				/* Remove old template from current list */
				templates->erase(table);

				/* Add the new template */
				template_table *table_tmp = new template_table(template_id, conf->buff_size);
				if (table_tmp->parse_template(ipfix_msg->data_couple[i].data_template, conf) != 0) {
					/* Template cannot be parsed; skip data set */
					delete table_tmp;
					continue;
				}

				templates->insert(std::pair<uint16_t, template_table*>(template_id, table_tmp));
				table = templates->find(template_id);
				/* New template was created; create new directory if necessary */
			}
		}

		/* Check whether data has to be flushed before storing data record */
		bool flush_records = conf->records_window > 0 && rcnt > conf->records_window;
		bool flush_time = false;
		time_t now;
		if (conf->time_window > 0) {
			time(&now);
			flush_time = difftime(now, conf->last_flush) > conf->time_window;
		}

		if (flush_records || flush_time) {
			/* Flush data for all exporters and ODIDs */
			flush_all_data(conf);

			/* Time management differs between flush policies (records vs. time) */
			if (flush_records) {
				time(&(conf->last_flush));
			} else if (flush_time) {
				while (difftime(now, conf->last_flush) > conf->time_window) {
					conf->last_flush = conf->last_flush + conf->time_window;
				}
			}

			/* Update window name and path */
			update_window_name(conf);
			odid_it->second.path = generate_path(conf, exporter_ip_addr, (*odid_it).first);

			rcnt = 0;
			conf->new_dir = true;
		}

		/* Store this data record */
		rc_flows = (*table).second->store(ipfix_msg->data_couple[i].data_set, odid_it->second.path, conf->new_dir);
		if (rc_flows >= 0) {
			rc_flows_sum += rc_flows;
			rcnt += rc_flows;
		} else {
			/* No need for showing error message here, since it is already done 
			 * by store() in case of an error */
			// MSG_ERROR(msg_module, "An error occurred during FastBit table store; no records were stored");
		}
	}

	/* We've told all tables that the directory has changed */
	conf->new_dir = false;

	if (rc_flows_sum) {
		odid_it->second.flow_watch.add_flows(rc_flows_sum);
	}

	odid_it->second.flow_watch.update_seq_no(ntohl(ipfix_msg->pkt_header->sequence_number));
	return 0;
}