Пример #1
0
int populate_ds_bls( ds_set_t *sets)
{
	unsigned int i,k;
	struct ds_bl *dsbl;
	ds_set_p set;
	ds_dest_p dst;
	struct bl_rule *dsbl_first;
	struct bl_rule *dsbl_last;
	struct net *set_net;

	LM_DBG("Updating ds blacklists...\n");

	/* each bl list at a time */
	for(dsbl = dsbl_lists; dsbl; dsbl = dsbl->next) {
		dsbl_first = dsbl_last = NULL;
		/* each blacklisted set at a time */
		for (i = 0; i < dsbl->no_sets; i++) {
			/* search if any set matches the one above */
			for( set=sets ; set ; set = set->next) {
				if (set->id == dsbl->sets[i]) {
					LM_DBG("Set [%d] matches. Adding all destinations:\n", set->id);
					for (dst = set->dlist; dst; dst = dst->next) {
						/* and add all IPs for each destination */
						for( k=0 ; k<dst->ips_cnt ; k++ ) {
							//print_ip(NULL, &dst->ips[k], "\n");
							set_net = mk_net_bitlen( &dst->ips[k],
												 dst->ips[k].len*8);
							if (set_net == NULL) {
								LM_ERR("BUILD netmask failed.\n");
								continue;
							}
							/* add this destination to the BL */
							add_rule_to_list( &dsbl_first, &dsbl_last,
								set_net,
								NULL/*body*/,
								0/*port*/,
								PROTO_NONE/*proto*/,
								0/*flags*/);
							pkg_free(set_net);
						}
					}
				}
			}
		}

		/* the new content for the BL */
		if (dsbl->bl && add_list_to_head( dsbl->bl, dsbl_first, dsbl_last, 1, 0)
						!= 0) {
			LM_ERR("UPDATE blacklist failed.\n");
			return -1;
		}
	}

	return 0;
}
Пример #2
0
void load_program(char *file, struct rule_list *rules)
{
  FILE *filehandle = fopen(file,"r");
  if (filehandle == NULL)
    {
      perror("Error opening program file");
      exit(-1);
    }
  int pre_state, post_state;
  char pre_symbol, post_symbol, direction;
  int status = 0;
  unsigned int rule_count = 0;
  errno = 0;
  rules->prev = NULL;
  rules->next = NULL;
  rules->first = rules;
  rules->last = rules;
  while (status != EOF)
    {
      status = fscanf(filehandle,"(%d,%c)->(%d,%c,%c)\n",&pre_state,&pre_symbol,&post_state,&post_symbol,&direction);
      struct rule rule;
      rule.pre.symbol  = pre_symbol;
      rule.pre.state   = pre_state;
      rule.post.symbol = post_symbol;
      rule.post.state  = post_state;
      rule.post.move   = direction;
      add_rule_to_list(rules,rule);
      rule_count++;
    }
  if (errno !=0)
    {
      perror("Error reading program");
        fclose(filehandle);
      exit(-1);
    }
  printf("Read %d rules\n",rule_count);
  check_rule_list(rules,rule_count);
  fclose(filehandle);
}
Пример #3
0
int init_black_lists(void)
{
	struct bl_head *old_blst_heads;
	struct bl_rule *head;
	struct bl_rule *tail;
	struct bl_rule *it, *it1;
	unsigned int old_used_heads;
	unsigned int i;

	if (!no_shm) {
		LM_CRIT("called twice\n");
		return -1;
	}
	no_shm = 0;

	old_blst_heads = blst_heads;
	blst_heads = (struct bl_head*)shm_malloc(max_heads*sizeof(struct bl_head));
	if (blst_heads==NULL) {
		LM_ERR("no more shm memory!\n");
		return -1;
	}
	memset( blst_heads, 0, max_heads * sizeof(struct bl_head));
	old_used_heads = used_heads;

	used_heads = 0;
	bl_default_marker = 0;

	/*for lists already created, init locks and move them into shm */
	for( i=0 ; i<old_used_heads ; i++ ) {

		/* duplicate in shm */
		it = old_blst_heads[i].first;
		head = tail = 0;

		for( it1=it ; it ; it=it1 ) {
			if (add_rule_to_list( &head, &tail, &it->ip_net,
			&it->body, it->port, it->proto, it->flags)!=0) {
				LM_ERR("failed to clone rule!\n");
				return -1;
			}

			it1 = it->next;
			pkg_free(it);
		}

		if (create_bl_head( old_blst_heads[i].owner, old_blst_heads[i].flags,
		head, tail, &old_blst_heads[i].name )==NULL ) {
				LM_ERR("failed to clone head!\n");
				return -1;
		}

		pkg_free(old_blst_heads[i].name.s);
	}

	pkg_free(old_blst_heads);

	/* register timer routine  */
	if (register_timer( "blcore-expire", delete_expired_routine, 0, 1)<0) {
		LM_ERR("failed to register timer\n");
		return -1;
	}

	/* register MI commands */
	if (register_mi_mod( "blacklists", mi_bl_cmds)<0) {
		LM_ERR("unable to register MI cmds\n");
		return -1;
	}

	return 0;
}