Ejemplo n.º 1
0
/**
 * init module function
 */
static int mod_init(void)
{
	load_tm_f  load_tm;

	LM_DBG("...\n");

	if(min_expires< 0)
		min_expires= 0;

	if(default_expires< 600)
		default_expires= 3600;

	/* import the TM auto-loading function */
	if((load_tm=(load_tm_f)find_export("load_tm", 0, 0))==NULL)
	{
		LM_ERR("can't import load_tm\n");
		return -1;
	}
	/* let the auto-loading function load all TM stuff */

	if(load_tm(&tmb)==-1)
	{
		LM_ERR("can't load tm functions\n");
		return -1;
	}

	init_db_url( db_url , 0 /*cannot be null*/);
	db_table.len = strlen(db_table.s);

	/* binding to database module  */
	if (db_bind_mod(&db_url, &pua_dbf))
	{
		LM_ERR("Database module not found\n");
		return -1;
	}
	if (!DB_CAPABILITY(pua_dbf, DB_CAP_ALL)) {
		LM_ERR("Database module does not implement all functions needed"
				" by the module\n");
		return -1;
	}

	pua_db = pua_dbf.init(&db_url);
	if (!pua_db)
	{
		LM_ERR("while connecting database\n");
		return -1;
	}
	/* verify table version  */
	if(db_check_table_version(&pua_dbf, pua_db, &db_table, PUA_TABLE_VERSION) < 0) {
		LM_ERR("error during table version check.\n");
		return -1;
	}

	if(HASH_SIZE<=1)
		HASH_SIZE= 512;
	else
		HASH_SIZE = 1<<HASH_SIZE;

	HashT= new_htable();
	if(HashT== NULL)
	{
		LM_ERR("while creating new hash table\n");
		return -1;
	}
	if(db_restore()< 0)
	{
		LM_ERR("while restoring hash_table\n");
		return -1;
	}

	if(update_period<=0)
	{
		LM_ERR("wrong clean_period\n");
		return -1;
	}
	if ( init_puacb_list() < 0)
	{
		LM_ERR("callbacks initialization failed\n");
		return -1;
	}
	pua_evlist= init_pua_evlist();
	if(pua_evlist==0)
	{
		LM_ERR("when initializing pua_evlist\n");
		return -1;
	}
	if(pua_add_events()< 0)
	{
		LM_ERR("while adding events\n");
		return -1;
	}

	register_timer("pua_clean", hashT_clean, 0, update_period-5,
		TIMER_FLAG_DELAY_ON_DELAY);

	register_timer("pua_dbupdate", db_update, 0, update_period,
		TIMER_FLAG_SKIP_ON_DELAY);


	if(pua_db)
		pua_dbf.close(pua_db);
	pua_db = NULL;

	return 0;
}
Ejemplo n.º 2
0
Archivo: pua.c Proyecto: kiryu/kamailio
/**
 * init module function
 */
static int mod_init(void)
{
	LM_DBG("...\n");

	if (register_mi_mod(exports.name, mi_cmds)!=0)
	{
		LM_ERR("failed to register MI commands\n");
		return -1;
	}

	if(min_expires< 0)
		min_expires= 0;

	if(default_expires< 600)
		default_expires= 3600;

	/* load TM API */
	if(load_tm_api(&tmb)==-1)
	{
		LM_ERR("can't load tm functions\n");
		return -1;
	}

	db_url.len = db_url.s ? strlen(db_url.s) : 0;
	LM_DBG("db_url=%s/%d/%p\n", ZSW(db_url.s), db_url.len, db_url.s);
	db_table.len = db_table.s ? strlen(db_table.s) : 0;

	/* binding to database module  */
	if (db_bind_mod(&db_url, &pua_dbf))
	{
		LM_ERR("Database module not found\n");
		return -1;
	}
	if (!DB_CAPABILITY(pua_dbf, DB_CAP_ALL)) {
		LM_ERR("Database module does not implement all functions needed"
				" by the module\n");
		return -1;
	}

	pua_db = pua_dbf.init(&db_url);
	if (!pua_db)
	{
		LM_ERR("while connecting database\n");
		return -1;
	}
	/* verify table version  */
	if(db_check_table_version(&pua_dbf, pua_db, &db_table, PUA_TABLE_VERSION) < 0) {
		LM_ERR("error during table version check.\n");
		return -1;
	}

	if (dbmode != PUA_DB_ONLY)
	{ 
		if(HASH_SIZE<=1)
			HASH_SIZE= 512;
		else
			HASH_SIZE = 1<<HASH_SIZE;

		HashT= new_htable();
		if(HashT== NULL)
		{
			LM_ERR("while creating new hash table\n");
			return -1;
		}
		if(db_restore()< 0)
		{
			LM_ERR("while restoring hash_table\n");
			return -1;
		}
	} 

	if (dbmode != PUA_DB_DEFAULT && dbmode != PUA_DB_ONLY)
	{
		dbmode = PUA_DB_DEFAULT;
		LM_ERR( "Invalid dbmode-using default mode\n" );
	}

	if(update_period<0)
	{
		LM_ERR("wrong clean_period\n");
		return -1;
	}
	if ( init_puacb_list() < 0)
	{
		LM_ERR("callbacks initialization failed\n");
		return -1;
	}
	pua_evlist= init_pua_evlist();
	if(pua_evlist==0)
	{
		LM_ERR("when initializing pua_evlist\n");
		return -1;
	}
	if(pua_add_events()< 0)
	{
		LM_ERR("while adding events\n");
		return -1;
	}

	if(check_remote_contact<0 || check_remote_contact>1)
	{
		LM_ERR("bad value for check_remote_contact\n");
		return -1;
	}

	startup_time = (int) time(NULL);

	if (update_period > 0) /* probably should check > 5 here!! -croc */
		register_timer(hashT_clean, 0, update_period- 5);

	if (dbmode != PUA_DB_ONLY) 
	{        
		if (update_period > 0) 
			register_timer(db_update, 0, update_period);
	}

	if(pua_db)
		pua_dbf.close(pua_db);
	pua_db = NULL;

	outbound_proxy.len = outbound_proxy.s ? strlen(outbound_proxy.s) : 0;

	return 0;
}
Ejemplo n.º 3
0
/* exe_one - execute one instruction */
void exe_one(void)
{
    /* get the opcode */
    opcode = getcbyte(pc); pc++;

    /* execute the instruction */
    switch (opcode) {
    case OP_CALL:
		*--sp = getboperand();
		*--sp = pc;
		*--sp = (int)(top - fp);
		fp = sp;
		pc = getafield(fp[fp[2]+3],A_CODE);
		break;
    case OP_SEND:
		*--sp = getboperand();
		*--sp = pc;
		*--sp = (int)(top - fp);
		fp = sp;
		if (p2 = fp[fp[2]+3])
		    p2 = getofield(p2,O_CLASS);
		else
		    p2 = fp[fp[2]+2];
		if (p2 && (p2 = getp(p2,fp[fp[2]+1]))) {
		    pc = getafield(p2,A_CODE);
		    break;
		}
		*sp = NIL;
		/* return NIL if there is no method for this message */
    case OP_RETURN:
		if (fp == top)
		    sts = CHAIN;
		else {
		    p2 = *sp;
		    sp = fp;
		    fp = top - *sp++;
		    pc = *sp++;
		    p3 = *sp++;
		    sp += p3;
		    *sp = p2;
		}
		break;
    case OP_TSPACE:
		sp -= getboperand();
		break;
    case OP_TMP:
		p2 = getboperand();
		*sp = fp[-p2-1];
		break;
    case OP_TSET:
		p2 = getboperand();
		fp[-p2-1] = *sp;
		break;
    case OP_ARG:
		p2 = getboperand();
		if (p2 >= fp[2])
		    error("too few arguments");
		*sp = fp[p2+3];
		break;
    case OP_ASET:
		p2 = getboperand();
		if (p2 >= fp[2])
		    error("too few arguments");
		fp[p2+3] = *sp;
		break;
    case OP_BRT:
		pc = (*sp ? getwoperand() : pc+2);
		break;
    case OP_BRF:
		pc = (*sp ? pc+2 : getwoperand());
		break;
    case OP_BR:
		pc = getwoperand();
		break;
    case OP_T:
		*sp = T;
		break;
    case OP_NIL:
		*sp = NIL;
		break;
    case OP_PUSH:
		*--sp = NIL;
		break;
    case OP_NOT:
		*sp = (*sp ? NIL : T);
		break;
    case OP_ADD:
		p2 = *sp++;
		*sp += p2;
		break;
    case OP_SUB:
		p2 = *sp++;
		*sp -= p2;
		break;
    case OP_MUL:
		p2 = *sp++;
		*sp *= p2;
		break;
    case OP_DIV:
		p2 = *sp++;
		*sp = (p2 == 0 ? 0 : *sp / p2);
		break;
    case OP_REM:
		p2 = *sp++;
		*sp = (p2 == 0 ? 0 : *sp % p2);
		break;
    case OP_BAND:
		p2 = *sp++;
		*sp &= p2;
		break;
    case OP_BOR:
		p2 = *sp++;
		*sp |= p2;
		break;
    case OP_BNOT:
		*sp = ~*sp;
		break;
    case OP_LT:
		p2 = *sp++;
		*sp = (*sp < p2 ? T : NIL);
		break;
    case OP_EQ:
		p2 = *sp++;
		*sp = (*sp == p2 ? T : NIL);
		break;
    case OP_GT:
		p2 = *sp++;
		*sp = (*sp > p2 ? T : NIL);
		break;
    case OP_LIT:
		*sp = getwoperand();
		break;
    case OP_SPLIT:
		*sp = getboperand();
		break;
    case OP_SNLIT:
		*sp = -getboperand();
		break;
    case OP_VAR:
		*sp = getvalue(getwoperand());
		break;
    case OP_SVAR:
		*sp = getvalue(getboperand());
		break;
    case OP_SET:
		setvalue(getwoperand(),*sp);
		break;
    case OP_SSET:
		setvalue(getboperand(),*sp);
		break;
    case OP_GETP:
		p2 = *sp++;
		*sp = getp(*sp,p2);
		break;
    case OP_SETP:
		p3 = *sp++;
		p2 = *sp++;
		*sp = setp(*sp,p2,p3);
		break;
    case OP_PRINT:
		print(*sp);
		break;
    case OP_PNUMBER:
    		pnumber(*sp);
    		break;
    case OP_PNOUN:
		show_noun(*sp);
		break;
    case OP_TERPRI:
		trm_chr('\n');
		break;
    case OP_FINISH:
		sts = FINISH;
		break;
    case OP_CHAIN:
		sts = CHAIN;
		break;
    case OP_ABORT:
		sts = ABORT;
		break;
    case OP_EXIT:
#ifdef MAC
		macpause();
#endif
		trm_done();
		exit();
		break;
    case OP_YORN:
		trm_get(line);
		*sp = (line[0] == 'Y' || line[0] == 'y' ? T : NIL);
    		break;
    case OP_CLASS:
		*sp = getofield(*sp,O_CLASS);
		break;
    case OP_MATCH:
		p2 = *sp++;
		*sp = (match(*sp,nouns[p2-1],adjectives[p2-1]) ? T : NIL);
		break;
    case OP_SAVE:
		*sp = db_save();
		break;
    case OP_RESTORE:
		*sp = db_restore();
		break;
    case OP_RESTART:
		*sp = db_restart();
		break;
    case OP_RAND:
		*sp = getrand(*sp);
		break;
    case OP_RNDMIZE:
		setrand(time(0L));
		*sp = NIL;
		break;
    default:
	    if (opcode >= OP_XVAR && opcode < OP_XSET)
		*sp = getvalue(opcode - OP_XVAR);
	    else if (opcode >= OP_XSET && opcode < OP_XPLIT)
		setvalue(opcode - OP_XSET,*sp);
	    else if (opcode >= OP_XPLIT && opcode < OP_XNLIT)
		*sp = opcode - OP_XPLIT;
	    else if (opcode >= OP_XNLIT && opcode < 256)
		*sp = OP_XNLIT - opcode;
	    else
		trm_str("Bad opcode\n");
	    break;
    }
}