Beispiel #1
0
int tr_general(uint8_t *src_data, uint16_t* offset, uint16_t* length, uint8_t* buffer, struct ipfix_lnf_map* item_info)
{
	switch(*length)
	{
	case 1:
		readui8(buffer) = readui8(src_data + *offset);
	break;
	case 2:
		readui16(buffer) = ntohs(readui16(src_data + *offset));
	break;
	case 4:
		readui32(buffer) = ntohl(readui32(src_data + *offset));
	break;
	case 8:
		readui64(buffer) = be64toh(readui64(src_data + *offset));
	break;
	case VAR_IE_LENGTH:
		*length = real_length(src_data, offset, *length);
	default:
		//Assume data type to be octetArray -> no endian conversion
		memcpy(buffer, src_data + *offset, *length);
	}
	if(/*length check comes here*/0){
		//invalid length
		return 1;
	}
	return 0;
}
Beispiel #2
0
void store_record(struct metadata* mdata, struct lnfstore_conf *conf)
{
	static uint8_t buffer[(uint16_t)-1];
	
	static lnf_rec_t *recp = NULL;
	static lnf_file_t *lfp = NULL; 
	stack_t *smap = conf->pst;

	if( conf->profiles && !mdata->channels ){
		//Record wont be stored, it does not belong to any channel and profiling is activated
		return;
	}

	if(recp == NULL) {
		lnf_rec_init(&recp);
	} else { 
		lnf_rec_clear(recp);
	}

    uint16_t offset, length;
    offset = 0;
    struct ipfix_template *templ = mdata->record.templ;
    uint8_t *data_record = (uint8_t*) mdata->record.record;

    /* get all fields */
    for (uint16_t count = 0, index = 0; count < templ->field_count; ++count, ++index) {
		
		struct ipfix_lnf_map *item, key;
        
        /* Get Enterprise number and ID */
        key.ie = templ->fields[index].ie.id;
        length = templ->fields[index].ie.length;
        key.en = 0;

        if (key.ie & 0x8000) {
			key.ie &= 0x7fff;
            key.en = templ->fields[++index].enterprise_number;
        }

		item = bsearch(&key, tr_table, MAX_TABLE , sizeof(struct ipfix_lnf_map), ipfix_lnf_map_compare);
		
		int problem = 1;
		if(item != NULL){
			problem = item->func(data_record, &offset, &length, buffer, item);	
			lnf_rec_fset(recp, item->lnf_id, buffer);
		}
		
		if(problem){
			length = real_length(data_record, &offset, length);
		}

        offset += length;
    } //end of element processing

	//!< Decide whether close close files and create new time window
    time_t now = time(NULL);
    if(difftime(now, conf->t_vars->window_start) > conf->time_window){

		mktime_window(now, conf);
		if( conf->profiles ){
			prec_t* item = NULL;
			//Close all old files
			for(unsigned x = 0; x < smap->top; x += al4B(sizeof(prec_t))){
				item = (prec_t*)smap->data + x; 
				if(item->lfp == NULL)
					continue;
				lnf_close(item->lfp);
				item->lfp = NULL;
			}
		} else {
			lnf_close(lfp);
			lfp = NULL;
		}
    }
    
    if( conf->profiles ){
		//On stack allocation of bit array
		int ba_size = smap->top/(8*sizeof(int))+1;
		int ba[ba_size];
		memset(&ba[0], 0, ba_size*sizeof(int));
		int status = 0;
		
		prec_t *item = NULL;

		for( int i = 0; mdata->channels[i] != 0; i++ ){
			
			void* rec_prof = channel_get_profile(mdata->channels[i]);

			item = bsearch(&rec_prof, smap->data, smap->top/al4B(sizeof(prec_t)),
					al4B(sizeof(prec_t)), prec_compare);
			
			if( item == NULL ){
				//Profile is not in configuration
				prec_t entry;
				entry.address = rec_prof;
				entry.lfp = NULL;
				
				const char* prpath = profile_get_path(rec_prof);
				char* path = mkpath_string(conf, prpath);
				mkdir_hierarchy(path);
				status = lnf_open(&entry.lfp, path, LNF_WRITE | (conf->compress? LNF_COMP : 0), (char*)conf->ident);

				stack_push(smap, &entry, sizeof(prec_t));
				//Add resilience
				qsort(smap->data, smap->top/al4B(sizeof(prec_t)), 
						al4B(sizeof(prec_t)), prec_compare);
				
				free(path);
			} else if( item->lfp == NULL ){
				//Profile file is closed
				const char* prpath = profile_get_path(rec_prof);
				char* path = mkpath_string(conf, prpath);
				mkdir_hierarchy(path);
				status = lnf_open(&item->lfp, path, LNF_WRITE | (conf->compress? LNF_COMP : 0), (char*)conf->ident);
				//Get prof name and open aprop file
				free(path);
			}
		}
		void* profile = NULL;
		int i = 0;
		for(profile = channel_get_profile(mdata->channels[i=0]);
				mdata->channels[i] != 0;
				profile = channel_get_profile(mdata->channels[i++]) ){

			item = bsearch(&profile, smap->data, smap->top/al4B(sizeof(prec_t)),
					al4B(sizeof(prec_t)), prec_compare);
				
			int index = (((base_t*)item) - smap->data)/al4B(sizeof(prec_t));
			if( !GETb(ba, index) ){ //And profile is not shadow
				status = lnf_write(item->lfp, recp);
				SETb(ba, index, 1);
			}
		}
	} else {
		if( lfp == NULL ){
			int status;
			char* path = mkpath_string(conf, NULL);
			status = mkdir_hierarchy(path);
			status |= lnf_open(&lfp, path, LNF_WRITE | (conf->compress ? LNF_COMP : 0), (char*)conf->ident);
			if(status != LNF_OK) {
				MSG_ERROR(msg_module, "Failed to open file! ...at path: %s \n", path);
			}
			free(path);
		}
		lnf_write(lfp, recp);
	}
	return;
}
Beispiel #3
0
void extract_ratfuns(node_type **root)
{
     node_type *r = *root;
     Coefficient t = {special};

     switch (r->type) {
     case op2_type:
          extract_ratfuns(&(r->u.op2.operand1));
          extract_ratfuns(&(r->u.op2.operand2));

          if (r->u.op2.operand1->type != ratfun_type
              || r->u.op2.operand2->type != ratfun_type) {
               /* nothing to do here */
               return;
          }


          switch (r->u.op2.operator) {
          case '^':
               if (r->u.op2.operand2->u.coef.type != rational
                   || real_length(r->u.op2.operand2->u.coef.u.rat.num) != 1) {
                    printf("Error! "
                           "Indices must be single precision integers!\n");
                    exit(1);
               }

               ratfun_power(&r->u.op2.operand1->u.ratfun,
                            r->u.op2.operand1->u.ratfun,
                            *(r->u.op2.operand2->u.coef.u.rat.num+1));
               if (coef_neg(r->u.op2.operand2->u.coef)) {
                    invert_ratfun(&r->u.op2.operand1->u.ratfun);
               }
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '+':
               add_ratfuns(&r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand2->u.ratfun);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '-':
               sub_ratfuns(&r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand2->u.ratfun);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '*':
               mul_ratfuns(&r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand2->u.ratfun);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '/':
               div_ratfuns(&r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand1->u.ratfun,
                           r->u.op2.operand2->u.ratfun);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;
          }
          break;

     case op1_type:
          extract_ratfuns(&r->u.op1.operand);

          if (r->u.op1.operand->type != ratfun_type) {
               /* nothing to do here */
               return;
          }

          switch (r->u.op1.operator) {
          case UMINUS:
               negate_ratfun(&r->u.op1.operand->u.ratfun);
               *root = r->u.op1.operand;
               r->u.op1.operand = NULL;
               free_tree(r);
               break;
          }
          break;

     case coef_type:
          /* turn into ratfun */
          t.type = rational;
          t.u.rat = make_bigrat3(1);
          *root = add_ratfun(r->u.coef, t);
          r->u.coef.type = special;
          free_tree(r);
     case ratfun_type:
          break;
     }
}
Beispiel #4
0
void extract_polys(node_type **root)
{
     node_type *r = *root;
     Coefficient tq = {special}, tr = {special};

     switch (r->type) {
     case op2_type:
          extract_polys(&(r->u.op2.operand1));
          extract_polys(&(r->u.op2.operand2));

          if (r->u.op2.operand1->type != coef_type
              || r->u.op2.operand2->type != coef_type) {
               /* nothing to do here */
               return;
          }

          switch (r->u.op2.operator) {
          case '^':
               if (r->u.op2.operand2->u.coef.type != rational
                   || real_length(r->u.op2.operand2->u.coef.u.rat.num) != 1) {
                    printf("Error! "
                           "Indices must be single precision integers!\n");
                    exit(1);
               }
               if (coef_neg(r->u.op2.operand2->u.coef)) {
                    /* this is a rational function */
                    break;
               }

               coef_power(&r->u.op2.operand1->u.coef,
                          r->u.op2.operand1->u.coef,
                          *(r->u.op2.operand2->u.coef.u.rat.num+1));
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '+':
               add_coefficients(&r->u.op2.operand1->u.coef,
                                r->u.op2.operand1->u.coef,
                                r->u.op2.operand2->u.coef);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '-':
               sub_coefficients(&r->u.op2.operand1->u.coef,
                                r->u.op2.operand1->u.coef,
                                r->u.op2.operand2->u.coef);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '*':
               mul_coefficients(&r->u.op2.operand1->u.coef,
                                r->u.op2.operand1->u.coef,
                                r->u.op2.operand2->u.coef);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               break;

          case '/':
               /* see if division will be exact */
               polydiv_coefficients(&tq, &tr,
                                    r->u.op2.operand1->u.coef,
                                    r->u.op2.operand2->u.coef);
               if (!coef_zero(tr)) {
                    /* division is not exact so this is a ratfun */
                    /* PRINTC(tq); */
                    /* PRINTC(tr); */
                    free_coefficient(&tq);
                    free_coefficient(&tr);

                    *root = add_ratfun(r->u.op2.operand1->u.coef,
                                       r->u.op2.operand2->u.coef);
                    r->u.op2.operand1->u.coef.type = special;
                    r->u.op2.operand2->u.coef.type = special;
                    free_tree(r);
                    break;
               }

               /* division is exact */
               copy_coefficient(&r->u.op2.operand1->u.coef, tq);
               *root = r->u.op2.operand1;
               r->u.op2.operand1 = NULL;
               free_tree(r);
               free_coefficient(&tq);
               free_coefficient(&tr);
               break;
          }
          break;

     case op1_type:
          extract_polys(&r->u.op1.operand);

          if (r->u.op1.operand->type != coef_type) {
               /* nothing to do here */
               return;
          }

          switch (r->u.op1.operator) {
          case UMINUS:
               negate_coefficient(&r->u.op1.operand->u.coef);
               *root = r->u.op1.operand;
               r->u.op1.operand = NULL;
               free_tree(r);
               break;
          }
          break;

     case coef_type:
     case ratfun_type:
          break;
     }
}