コード例 #1
0
ファイル: cpl.c プロジェクト: NormB/opensips
static inline int do_script_action(struct sip_msg *msg, int action)
{
	str  body = {0,0};
	str  bin  = {0,0};
	str  log  = {0,0};
	str  username = {0,0};
	str  domain   = {0,0};

	if ( get_body(msg, &body)!=0 ) {
		LM_ERR("failed to look for body!\n");
		goto error;
	}

	/* get the user name */
	if (get_dest_user( msg, &username, &domain)==-1)
		goto error;

	/* we have the script and the user */
	switch (action) {
		case STORE_SCRIPT :
			/* check the len -> it must not be 0 */
			if (body.len==0) {
				LM_ERR("0 content-len found for store\n");
				goto error_1;
			}
			/* now compile the script and place it into database */
			/* get the binary coding for the XML file */
			if ( encodeCPL( &body, &bin, &log)!=1) {
				cpl_err = &bad_cpl;
				goto error_1;
			}

			/* write both the XML and binary formats into database */
			if (write_to_db( &username, cpl_env.use_domain?&domain:0,
			&body,&bin)!=1) {
				cpl_err = &intern_err;
				goto error_1;
			}
			break;
		case REMOVE_SCRIPT:
			/* check the len -> it must be 0 */
			if (body.len!=0) {
				LM_ERR("non-0 content-len found for remove\n");
				goto error_1;
			}
			/* remove the script for the user */
			if (rmv_from_db( &username, cpl_env.use_domain?&domain:0)!=1) {
				cpl_err = &intern_err;
				goto error_1;
			}
			break;
	}

	if (log.s) pkg_free( log.s );
	return 0;
error_1:
	if (log.s) pkg_free( log.s );
error:
	return -1;
}
コード例 #2
0
ファイル: cpl_rpc.c プロジェクト: 4N7HR4X/kamailio
static void cpl_load(rpc_t* rpc, void* c)
{
	char* cpl_file;
	int cpl_file_len;
	str user;
	str xml = STR_NULL;
	str bin = STR_NULL;
	str enc_log = STR_NULL;

	DBG("DEBUG:cpl-c:cpl_load: \"LOAD_CPL\" FIFO command received!\n");

	if (rpc->scan(c, "s", &user.s) < 1) {
		rpc->fault(c, 400, "Username parameter not found");
		return;
	}
	user.len = strlen(user.s);

	DBG("DEBUG:cpl_load: user=%.*s\n", user.len, user.s);

	if (rpc->scan(c, "s", &cpl_file) < 1) {
		rpc->fault(c, 400, "CPL file name expected");
		return;
	}
	cpl_file_len = strlen(cpl_file);
	DBG("DEBUG:cpl-c:cpl_load: cpl file=%s\n", cpl_file);

	     /* check user+host */
	if (check_userhost( user.s, user.s+user.len)!=0) {
		LOG(L_ERR,"ERROR:cpl-c:cpl_load: invalid user@host [%.*s]\n",
		    user.len,user.s);
		rpc->fault(c, 400, "Bad user@host: %.*s", user.len, user.s);
		return;
	}

	/* load the xml file - this function will allocated a buff for the loading
	 * the cpl file and attach it to xml.s -> don't forget to free it! */
	if (load_file( cpl_file, &xml)!=1) {
		rpc->fault(c, 400, "Cannot read CPL file\n");
		goto error;
	}

	/* get the binary coding for the XML file */
	if (encodeCPL( &xml, &bin, &enc_log)!=1) {
		rpc->fault(c, 400, "%.*s", enc_log.len, enc_log.s);
		goto error;
	}

	/* write both the XML and binary formats into database */
	if (write_to_db(user.s, &xml, &bin)!=1) {
		rpc->fault(c, 400, "Cannot save CPL to database");
		goto error;
	}

	/* free the memory used for storing the cpl script in XML format */
	pkg_free( xml.s );

	/* everything was OK -> dump the logs into response file */
	rpc->add(c, "S", &enc_log);
	if (enc_log.s) pkg_free ( enc_log.s );
	return;
error:
	if (enc_log.s) pkg_free ( enc_log.s );
	if (xml.s) pkg_free ( xml.s );
}
コード例 #3
0
ファイル: cpl_loader.c プロジェクト: andrey-vorobiev/opensips
struct mi_root* mi_cpl_load(struct mi_root *cmd_tree, void *param)
{
	struct mi_root *rpl_tree;
	struct mi_node *cmd;
	struct sip_uri uri;
	str xml = {0,0};
	str bin = {0,0};
	str enc_log = {0,0};
	str val;
	char *file;

	LM_DBG("\"LOAD_CPL\" MI command received!\n");
	cmd = &cmd_tree->node;

	/* check user+host */
	if((cmd->kids==NULL) ||(cmd->kids->next==NULL) || (cmd->kids->next->next))
		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);

	val = cmd->kids->value;
	if (parse_uri( val.s, val.len, &uri)!=0){
		LM_ERR("invalid sip URI [%.*s]\n",
			val.len, val.s);
		return init_mi_tree( 400, USRHOST_ERR_S, USRHOST_ERR_LEN );
	}
	LM_DBG("user@host=%.*s@%.*s\n",
		uri.user.len,uri.user.s,uri.host.len,uri.host.s);

	/* second argument is the cpl file */
	val = cmd->kids->next->value;
	file = pkg_malloc(val.len+1);
	if (file==NULL) {
		LM_ERR("no more pkg mem\n");
		return 0;
	}
	memcpy( file, val.s, val.len);
	file[val.len]= '\0';

	/* load the xml file - this function will allocated a buff for the loading
	 * the cpl file and attach it to xml.s -> don't forget to free it! */
	if (load_file( file, &xml)!=1) {
		pkg_free(file);
		return init_mi_tree( 500, FILE_LOAD_ERR_S, FILE_LOAD_ERR_LEN );
	}
	LM_DBG("cpl file=%s loaded\n",file);
	pkg_free(file);

	/* get the binary coding for the XML file */
	if (encodeCPL( &xml, &bin, &enc_log)!=1) {
		rpl_tree = init_mi_tree( 500, CPLFILE_ERR_S, CPLFILE_ERR_LEN );
		goto error;
	}

	/* write both the XML and binary formats into database */
	if (write_to_db( &uri.user,cpl_env.use_domain?&uri.host:0, &xml, &bin)!=1){
		rpl_tree = init_mi_tree( 500, DB_SAVE_ERR_S, DB_SAVE_ERR_LEN );
		goto error;
	}

	/* everything was OK */
	rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);

error:
	if (rpl_tree && enc_log.len)
		add_mi_node_child(&rpl_tree->node,MI_DUP_VALUE,"Log",3,enc_log.s,enc_log.len);
	if (enc_log.s)
		pkg_free ( enc_log.s );
	if (xml.s)
		pkg_free ( xml.s );
	return rpl_tree;
}
コード例 #4
0
ファイル: cpl.c プロジェクト: SibghatullahSheikh/kamailio
static inline int do_script_action(struct sip_msg *msg, int action)
{
	str  body = {0,0};
	str  bin  = {0,0};
	str  log  = {0,0};
	str  username = {0,0};
	str  domain   = {0,0};

	/* content-length (if present) */
	if ( !msg->content_length &&
	((parse_headers(msg,HDR_CONTENTLENGTH_F,0)==-1)||!msg->content_length)) {
		LM_ERR("no Content-Length hdr found!\n");
		goto error;
	}
	body.len = get_content_length( msg );

	/* get the user name */
	if (get_dest_user( msg, &username, &domain)==-1)
		goto error;

	/* we have the script and the user */
	switch (action) {
		case STORE_SCRIPT :
			/* check the len -> it must not be 0 */
			if (body.len==0) {
				LM_ERR("0 content-len found for store\n");
				goto error_1;
			}
			/* get the message's body */
			body.s = get_body( msg );
			if (body.s==0) {
				LM_ERR("cannot extract body from msg!\n");
				goto error_1;
			}
			/* now compile the script and place it into database */
			/* get the binary coding for the XML file */
			if ( encodeCPL( &body, &bin, &log)!=1) {
				cpl_err = &bad_cpl;
				goto error_1;
			}

			/* write both the XML and binary formats into database */
			if (write_to_db( &username, cpl_env.use_domain?&domain:0,
			&body,&bin)!=1) {
				cpl_err = &intern_err;
				goto error_1;
			}
			break;
		case REMOVE_SCRIPT:
			/* check the len -> it must be 0 */
			if (body.len!=0) {
				LM_ERR("non-0 content-len found for remove\n");
				goto error_1;
			}
			/* remove the script for the user */
			if (rmv_from_db( &username, cpl_env.use_domain?&domain:0)!=1) {
				cpl_err = &intern_err;
				goto error_1;
			}
			break;
	}

	if (log.s) pkg_free( log.s );
	return 0;
error_1:
	if (log.s) pkg_free( log.s );
error:
	return -1;
}
コード例 #5
0
ファイル: DBFetch.cpp プロジェクト: RengaPD/IuBi
void DBFetch::elabora_query(MYSQL_RES *res, MYSQL* database, DBFetch::table_positions pos,std::list<pthread_t*> &threadlist,std::list<pthread_t*>::iterator &current_thread, std::list<Device*> &devicelist,libxbee::XBee* &xbee,  libxbee::Con* &connection)
{      	
    //mysql initalizations
    MYSQL_ROW row;	// puntatore alla tupla 
    MYSQL_FIELD *field;   
    unsigned int num_fields;
    unsigned int i;
    num_fields = mysql_num_fields(res);
           
    while ((row = mysql_fetch_row(res)))/*scorre le righe, returna false quando sono finite*/
    {           
        Device* appoggio = DBFetch::search_by_mac(devicelist, row[pos.pos_mac]);   
        //così facendo sto nello heap, con annessi e connessi
            
        
        if (appoggio->is_null(*appoggio))            
        {               
            /*per evitare la mappina degli if dovrei mettere un DECORATORE
             */
            
            cout<<endl<<"il disp non è in lista"<<endl; 
            if (!strcmp(row[pos.pos_type],"0"))       
            {    
                cout<<"aggiungo un disp di tipo device";
                devicelist.push_back(new Device(row[pos.pos_mac]));
                
            }else if(!strcmp(row[pos.pos_type],"interruttore"))
            {              
                devicelist.push_back(new Light(row[pos.pos_mac]));             
            }else if(!strcmp(row[pos.pos_type],"presa"))            
            {            
                devicelist.push_back(new Powerbar(row[pos.pos_mac]));
                
            }else cout<<"elabora_query() error, device type invalid: "<<row[pos.pos_type]<<endl;            
            //altrimenti errore            
             
        }else        
        {            
                //cosa succede se c'è
            cout<<endl<<"STATOBUSY DEL DISP:"<<appoggio->check_busy()<<endl;
            cout<<"Il disp è in lista"<<endl;
            
            if(!appoggio->check_busy()) //verifico che il disp sia libero
            {
                //cout<<"il dispositivo è libero, posso lanciare il thread"<<endl;
                string triggers = DBFetch::get_triggers(row[pos.pos_state_iface],row[pos.pos_state_modif]);  
                
                
                if(strcmp(triggers.c_str(), "NC"))
                {
#ifndef MULTITHREADING   
                    appoggio->print();
                    appoggio->elaborate_triggers(triggers,xbee,connection);
                    write_to_db( database,row[pos.pos_state_iface],appoggio->getmacaddress());
#endif                    
                    
                   
#ifdef MUTITHREADING 
                    //setto il disp occupato
                    appoggio->set_acting(); 
                    //preparo l'argomento del thread
                    struct DBFetch::thd_arg *ta = new thd_arg;
                    ta->triggers = triggers;
                    ta->device = appoggio;
                    ta->state_modif = row[pos.pos_state_modif];
                    ta->xbee = xbee;
                    ta->connection = connection;
                    cout<<"elaboraquery()trigger presenti, lancio il thread:"<<endl;

                    pthread_t *thd = new pthread_t;
                    threadlist.push_back(thd);
                    
                    //faccio partire il thread (passo ta perchè non posso passare *this)
                   pthread_create(&thd, NULL, action_thread, (void *)ta);
                   
#endif                    
                }else cout<<"niente da fare con questo dispositivo"<<endl;
                
            }else cout<<"disp occupato, passo al prossimo"<<endl;              
                
                //delete *current_thread??????;          
            //qui ci va qualche verifica che il tipo sia valido o una restituzione di errore           
        }
    }    
        
}