Esempio n. 1
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;
}
Esempio n. 2
0
int init_ds_bls(void)
{
	unsigned int i;
	struct ds_bl *dsbl;
	str name;
	str val;
	char *p;

	LM_DBG("Initialising ds blacklists\n");

	if (blacklists == NULL)
		return 0;

	for(i = 0; i < bl_size; i++ ) {
		LM_DBG("processing bl definition <%s>\n", blacklists[i]);
		/* get name */
		p = strchr( blacklists[i], '=');
		if (p==NULL || p==blacklists[i]) {
			LM_ERR("blacklist definition <%s> has no name", blacklists[i]);
			return -1;
		}
		name.s = blacklists[i];
		name.len = p - name.s;
		trim(&name);
		if (name.len == 0) {
			LM_ERR("empty name in blacklist definition <%s>\n", blacklists[i]);
			return -1;
		}
		LM_DBG("found list name <%.*s>\n", name.len, name.s);
		/* alloc structure */
		dsbl = shm_malloc(sizeof(*dsbl));
		if (dsbl == NULL) {
			LM_ERR("no more shme memory\n");
			return -1;
		}
		memset(dsbl, 0, sizeof(*dsbl));
		/* fill in the types */
		p++;
		do {
			if (dsbl->no_sets == DS_BL_MAX_SETS) {
				LM_ERR("too many types per rule <%s>\n", blacklists[i]);
				shm_free(dsbl);
				return -1;
			}
			val.s = p;
			p = strchr( p, ',');
			if (p == NULL) {
				val.len = strlen(val.s);
			} else {
				val.len = (int)(long)(p - val.s);
				p++;
			}
			trim(&val);
			if (val.len == 0) {
				LM_ERR("invalid types listing in <%s>\n", blacklists[i]);
				shm_free(dsbl);
				return -1;
			}
			LM_DBG("found type <%.*s>\n", val.len, val.s);
			if (str2int( &val, &dsbl->sets[dsbl->no_sets])!=0) {
				LM_ERR("nonnumerical type <%.*s>\n", val.len, val.s);
				shm_free(dsbl);
				return -1;
			}
			dsbl->no_sets++;
		} while(p != NULL);

		pkg_free(blacklists[i]);
		blacklists[i] = NULL;

		/* create backlist for it */
		dsbl->bl = create_bl_head( 313131, 0/*flags*/, NULL, NULL, &name);
		if (dsbl->bl == NULL) {
			LM_ERR("CREATE bl <%.*s> failed.\n", name.len, name.s);
			shm_free(dsbl);
			return -1;
		}

		/* link it */
		dsbl->next = dsbl_lists;
		dsbl_lists = dsbl;
	}

	pkg_free(blacklists);
	blacklists = NULL;

	return 0;
}