示例#1
0
void node_invite(node_t *node, char *guest) {
    gm_request_t req;
    if( node_is_online(node) ) {
        req.msg_id = GM_GROUP;
        req.gm_group.join = 1;
        ansi_copy_str(req.gm_group.owner, node->id);
        ansi_copy_str(req.gm_group.guest, guest);
        PERROR_IF_TRUE(gm_client_send(&node->gm_client, &req) < 0, "ERROR::node_invite: ");
    }
}
示例#2
0
void asterisk_acc_unpack_socket(char *buff, perm_node_t **key_list, asterisk_acc_table_t *asterisk_acc_table, opool_t *opool) {
    int i;
    json_object *new_obj, *jvalue;
    opool_item_t *item, *item2;
    perm_node_t *key_element, *value_element, *data_list;

    perm_node_t *temp, *entry;
    perm_node_t *temp2, *entry2;
    perm_node_t *data_list2;

    char id[10], pwd[10];

	new_obj = json_tokener_parse(buff);
	//printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

    for(i = 0; i < json_object_array_length(new_obj); i++)
	{ 
        item = opool_get(opool);
        EXIT_IF_TRUE(item == NULL, "Cannot get from object pool\n");
        key_element = (perm_node_t *)item->data;

        item2 = opool_get(opool);
        EXIT_IF_TRUE(item == NULL, "Cannot get from object pool\n");
        value_element = (perm_node_t *)item2->data;

        json_object *obj = json_object_array_get_idx(new_obj, i);

        json_object_object_get_ex(obj, "account", &jvalue);
        strcpy(id, json_object_get_string(jvalue));

        json_object_object_get_ex(obj, "password", &jvalue);
        strcpy(pwd, json_object_get_string(jvalue));

        ansi_copy_str(key_element->value, id);
        ansi_copy_str(value_element->value, pwd);

        data_list = (perm_node_t *)ht_get_item(asterisk_acc_table, (void *)id);
        if (data_list == NULL) {
            DL_APPEND(*key_list, key_element);
            DL_APPEND(data_list, value_element);
            ht_add_item(asterisk_acc_table, id, data_list);
        }
        else {
            DL_APPEND(data_list, value_element);
        }
    }

}
示例#3
0
文件: userver.c 项目: mocidis/userver
static void $UPROTO$_server_init_int($UPROTO$_server_t *userver, char *conn_str, pj_pool_t *pool) {
    userver->pool = pool;
    ansi_copy_str(userver->connect_str, conn_str);
    userver->is_online = 1;
    CHECK(__FILE__, pj_mutex_create_simple(pool, "", &userver->mutex));
    if (userver->on_init_done_f != NULL){
        userver->on_init_done_f(userver);
	}
}
示例#4
0
文件: riuc4.c 项目: mocidis/riuc-v1
int main(int argc, char *argv[]) {
    if (argc < 3) {
        usage(argv[0]);
    }

    /*------------ CONFIG VARIABLES ------------*/
    sqlite3 *db;
    char *sql, sql_cmd[100];
    sqlite3_stmt *stmt;

    char id[10], location[30], desc[50];
    char gm_cs[50], gmc_cs[50], adv_cs[50], gb_cs[50];
    char gm_cs_tmp[50], gmc_cs_tmp[50], adv_cs_tmp[50];

    int snd_dev_r0, snd_dev_r1, snd_dev_r2, snd_dev_r3;

    int adv_port = ADV_PORT;
    int gb_port = GB_PORT; 

    /*------------ INIT & STREAM VARIABLES ------------*/
    pj_caching_pool cp;
    pj_pool_t *pool;
    pjmedia_endpt *ep;

    endpoint_t streamers[MAX_NODE];
    endpoint_t receivers[MAX_NODE];
    adv_server_t adv_server;
    
    /*------------ OTHER VARIABLES ------------*/
    pthread_t thread;
    char *dummy, option[10];
    int i, n, input, f_quit = 0;
    int snd_dev[4];

    /*-----------------------------------------*/

    SET_LOG_LEVEL(4);

    /*------------ START ------------*/
#if 1
    SHOW_LOG(3, "Press '1': Set sound devices configure\nPress 's': Show databases\nPress 'Space': Load databases\nPress 'q': Quit\n");

    CALL_SQLITE (open (argv[2], &db));
    while(!f_quit) {
        dummy = fgets(option, sizeof(option), stdin);
        switch(option[0]) {
            case '1':
                SHOW_LOG(3, "Set device index for each radio...\n");
                for (i = 0; i < MAX_NODE; i++){
                    SHOW_LOG(3, "Radio %d: ", i);
                    dummy = fgets(option, sizeof(option), stdin);           
                    input = atoi(&option[0]);
                    n = sprintf(sql_cmd, "UPDATE riuc_config SET snd_dev_r%d =(?)", i);
                    sql_cmd[n] = '\0';
                    CALL_SQLITE (prepare_v2 (db, sql_cmd, strlen (sql_cmd) + 1, & stmt, NULL));
                    CALL_SQLITE (bind_int (stmt, 1, input));
                    CALL_SQLITE_EXPECT (step (stmt), DONE);
                }
                SHOW_LOG(3, "Config completed\n");               
                f_quit = 1;
                break;
            case 's':
                sql = "SELECT *FROM riuc_config";
                CALL_SQLITE (prepare_v2 (db, sql, strlen (sql) + 1, &stmt, NULL));

                while (sqlite3_step(stmt) == SQLITE_ROW) {
                    printf("ID: %s\n", sqlite3_column_text(stmt, 0));
                    printf("Location: %s\n", sqlite3_column_text(stmt, 1));
                    printf("Desc: %s\n", sqlite3_column_text(stmt, 2));
                    printf("GM_CS: %s\n", sqlite3_column_text(stmt, 3));
                    printf("GMC_CS: %s\n", sqlite3_column_text(stmt, 4));
                    printf("snd_dev_r0: %u\n", sqlite3_column_int(stmt, 5));
                    printf("snd_dev_r1: %u\n", sqlite3_column_int(stmt, 6));
                    printf("snd_dev_r2: %u\n", sqlite3_column_int(stmt, 7));
                    printf("snd_dev_r3: %u\n", sqlite3_column_int(stmt, 8));
                }
                break;
            case ' ':
                f_quit = 1;
                break;
            case 'q':
                return 0;
            default:
                SHOW_LOG(3, "Unknown option\n");
        }
    }
    f_quit = 0;
#endif
    /*------------ LOAD CONFIG ------------*/
    //CALL_SQLITE (open ("databases/riuc.db", &db));
    sql = "SELECT * FROM riuc_config";
    CALL_SQLITE (prepare_v2 (db, sql, strlen (sql) + 1, &stmt, NULL));

    //WARNING: MAX NUMBER OF SOUND DEV = 4
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        strcpy(id, sqlite3_column_text(stmt, 0));
        strcpy(location, sqlite3_column_text(stmt, 1));
        strcpy(desc, sqlite3_column_text(stmt, 2));
        strcpy(gm_cs, sqlite3_column_text(stmt, 3));
        strcpy(gmc_cs, sqlite3_column_text(stmt, 4));
        snd_dev_r0 = sqlite3_column_int(stmt, 5);
        snd_dev_r1 = sqlite3_column_int(stmt, 6);
        snd_dev_r2 = sqlite3_column_int(stmt, 7);
        snd_dev_r3 = sqlite3_column_int(stmt, 8);
        auto_invite = sqlite3_column_int(stmt, 9);
    }

    snd_dev[0] = snd_dev_r0;
    snd_dev[1] = snd_dev_r1;
    snd_dev[2] = snd_dev_r2;
    snd_dev[3] = snd_dev_r3;

    n = sprintf(adv_cs, "udp:0.0.0.0:%d", adv_port);
    adv_cs[n] = '\0';
    n = sprintf(gb_cs, "udp:%s:%d",GB_MIP, gb_port);
    gb_cs[n] = '\0';
    
    SHOW_LOG(3, "========= LOADED CONFIG ========\n");
    SHOW_LOG(3, "ID: %s\nDesc: %s\nGM_CS: %s\nGMC_CS: %s\nADV_CS: %s\nGB_CS: %s\nsnd_r0: %d\nsnd_r1: %d\nsnd_r2: %d\nsnd_r3: %dAuto invite: %d\n", id, desc, gm_cs, gmc_cs, adv_cs, gm_cs, snd_dev_r0, snd_dev_r1, snd_dev_r2, snd_dev_r3, auto_invite);
    SHOW_LOG(3, "================================\n");

    /*------------ INIT ------------*/
    pj_init();
    pj_caching_pool_init(&cp, NULL, 10000);
    pool = pj_pool_create(&cp.factory, "pool1", 1024, 1024, NULL);

    SHOW_LOG(2, "INIT CP AND POOL...DONE\n");

    /*------------ NODE ------------*/
#if 1

    init_adv_server(&adv_server, adv_cs, pool);

    for (i = 0;i < MAX_NODE; i++) {
        memset(gm_cs_tmp, 0, sizeof(gm_cs_tmp));
        memset(gmc_cs_tmp, 0, sizeof(gmc_cs_tmp));
        memset(adv_cs_tmp, 0, sizeof(adv_cs_tmp));

        ansi_copy_str(gm_cs_tmp, gm_cs);
        ansi_copy_str(adv_cs_tmp, adv_cs);
        ansi_copy_str(gmc_cs_tmp, gmc_cs);

        n = strlen(gmc_cs);

        gmc_cs_tmp[n-1] = i + 1+ '0';

        memset(&riuc_data.node[i], 0, sizeof(riuc_data.node[i]));
        riuc_data.node[i].on_leaving_server_f = &on_leaving_server;
        node_init(&riuc_data.node[i], id, location, desc, i, gm_cs_tmp, gmc_cs_tmp, pool);
        node_add_adv_server(&riuc_data.node[i], &adv_server);
    }

    SHOW_LOG(2, "INIT NODE...DONE\n");
#endif
    /*----------- GB --------------*/
#if 1
    memset(&riuc_data.gb_sender, 0, sizeof(riuc_data.gb_sender));
    n = sprintf(gb_cs, "udp:%s:%d", GB_MIP, GB_PORT);
    gb_cs[n] = '\0';
    gb_sender_create(&riuc_data.gb_sender, gb_cs);

    SHOW_LOG(2, "INIT GB SENDER...DONE\n");
#endif
    /*----------- RIUC4 --------------*/
#if 1
    memset(riuc_data.serial_file, 0, sizeof(riuc_data.serial_file));
    strncpy(riuc_data.serial_file, argv[1], strlen(argv[1]));
    riuc4_init(&riuc_data.serial, &riuc_data.riuc4, &on_riuc4_status, pool);
    riuc4_start(&riuc_data.serial, riuc_data.serial_file);

    SHOW_LOG(2, "INIT RIUC4...DONE\n");
#if 1
    for (i = 0; i < MAX_NODE; i++) {
        riuc4_enable_rx(&riuc_data.riuc4, i);
        usleep(250*1000);
        riuc4_enable_tx(&riuc_data.riuc4, i);
        usleep(250*1000);
    }
#endif
    SHOW_LOG(2, "ENABLE TX & RX...DONE\n");
#endif
    /*----------- STREAM --------------*/
#if 1
    SHOW_LOG(3, "INIT STREAM...START\n");
    pjmedia_endpt_create(&cp.factory, NULL, 1, &ep);
#if 1
    SHOW_LOG(3, "CODEC INIT\n");
    pjmedia_codec_g711_init(ep);

    for (i = 0; i < MAX_NODE; i++) {
        SHOW_LOG(3, "NODE MEDIA CONFIG\n");
        node_media_config(&riuc_data.node[i], &streamers[i], &receivers[i]);
        SHOW_LOG(3, "SET POOL\n");
        riuc_data.node[i].streamer->pool = pool;
        riuc_data.node[i].receiver->pool = pool;
        
        SHOW_LOG(3, "SET ENDPOINT\n");
        riuc_data.node[i].receiver->ep = ep;
        riuc_data.node[i].streamer->ep = ep;

        SHOW_LOG(3, "INIT STREAMER & RECEIVER FOR NODE %d\n", i);
        streamer_init(riuc_data.node[i].streamer, riuc_data.node[i].streamer->ep, riuc_data.node[i].receiver->pool);
        receiver_init(riuc_data.node[i].receiver, riuc_data.node[i].receiver->ep, riuc_data.node[i].receiver->pool, 2);
    }
    
    SHOW_LOG(3, "CONFIG SOUND DEVICE\n");
    for (i = 0; i < MAX_NODE; i++) {
        streamer_config_dev_source(riuc_data.node[i].streamer, snd_dev[i]);
        receiver_config_dev_sink(riuc_data.node[i].receiver, snd_dev[i]);
    }

    SHOW_LOG(2, "INIT STREAM...DONE\n");
    /*---------------------------------*/
    pthread_create(&thread, NULL, auto_register, &riuc_data);
#endif
#endif
    while(!f_quit) {
        dummy = fgets(option, sizeof(option), stdin);

        switch(option[0]) {
            case 'c':
                for (i = 0; i < MAX_NODE; i++){
                    SHOW_LOG(3, "Set device index for each radio...\nRadio %d: ", i);
                    dummy = fgets(option, sizeof(option), stdin);           
                    input = atoi(&option[0]);
                    n = sprintf(sql_cmd, "UPDATE riuc_config SET snd_dev_r%d =(?)", i);
                    sql_cmd[n] = '\0';
                    CALL_SQLITE (prepare_v2 (db, sql_cmd, strlen (sql_cmd) + 1, & stmt, NULL));
                    CALL_SQLITE (bind_int (stmt, 1, input));
                    CALL_SQLITE_EXPECT (step (stmt), DONE);

                    streamer_config_dev_source(riuc_data.node[i].streamer, input);
                    receiver_config_dev_sink(riuc_data.node[i].receiver, input);
                }


                SHOW_LOG(3, "Config completed\n");               
                break;
            case 's':
                sql = "SELECT *FROM riuc_config";
                CALL_SQLITE (prepare_v2 (db, sql, strlen (sql) + 1, &stmt, NULL));

                while (sqlite3_step(stmt) == SQLITE_ROW) {
                    printf("ID: %s\n", sqlite3_column_text(stmt, 0));
                    printf("Location: %s\n", sqlite3_column_text(stmt, 1));
                    printf("Desc: %s\n", sqlite3_column_text(stmt, 2));
                    printf("GM_CS: %s\n", sqlite3_column_text(stmt, 3));
                    printf("GMC_CS: %s\n", sqlite3_column_text(stmt, 4));
                    printf("snd_dev_r0: %u\n", sqlite3_column_int(stmt, 5));
                    printf("snd_dev_r1: %u\n", sqlite3_column_int(stmt, 6));
                    printf("snd_dev_r2: %u\n", sqlite3_column_int(stmt, 7));
                    printf("snd_dev_r3: %u\n", sqlite3_column_int(stmt, 8));
                }
                break;
            case 't':
                node_start_session(&riuc_data.node[0]);
                break;
            case 'y':
                node_stop_session(&riuc_data.node[0]);
                break;
            case 'j':
                node_invite(&riuc_data.node[0], "FTW");
                break;
            case 'l':
                node_repulse(&riuc_data.node[0], "FTW");
                break;
            case 'q':
                f_quit = 1;
                break;
            default:
                SHOW_LOG(3, "Unknown option\n"); 
                break;
        }   

    }
    SHOW_LOG(3, "Quiting...\n");
    //pthread_join(thread, NULL);

    sqlite3_finalize(stmt);
    sqlite3_close(db);

    return 0;
}
示例#5
0
文件: userver.c 项目: mocidis/userver
int $UPROTO$_secure_server_proc(void *param) {
    int ret;
    unsigned int len;
    
    int port;
    char *first, *second, *third;
    char cnt_str[30];

    pj_sockaddr_in caddr;
    char *caddr_str;
    pj_time_val timeout;

    pj_fd_set_t read_fds;

	char buffer[USERVER_BUFSIZE];

	$UPROTO$_request_t request;

    // For lvc parsing
    lvc_t lvc;
    int len1;
    char *val;
    uint32_t *ts;
    char sts[32];
    char plain[USERVER_BUFSIZE];
    char *pph;
    char otp[32];
    pj_str_t pjstr;
    // End for lvc parsing

    $UPROTO$_server_t *userver = ($UPROTO$_server_t *)param;
	ansi_copy_str(cnt_str, userver->connect_str);

    first = cnt_str;
    
    second = strchr(first, ':');
    EXIT_IF_TRUE(second == NULL, "Wrong connection string format\n");
    *second = '\0';
    second++;

    if(0 == strcmp(first, "udp")) {
        third = strchr(second, ':');
        EXIT_IF_TRUE(third == NULL, "Wrong connection string format\n");
        *third = '\0';
        third++;
        port = atoi(third);
        open_udp_socket(userver, second, port);
        if (userver->on_open_socket_f != NULL)
            userver->on_open_socket_f(userver);

        userver->recv_f = &udp_recvfrom;
        userver->send_f = &udp_sendto;
    }
    else if( 0 == strcmp(first, "tty") ) {
        open_tty(userver, second);
        userver->recv_f = &tty_recvfrom;
        userver->send_f = &tty_sendto;
    }
    else {
        EXIT_IF_TRUE(1, "Unsuported protocol\n");
    }

    // thread loop
    timeout.sec = 0;
    timeout.msec = 100;
    userver->is_end = 0;


    while( !userver->is_end ) {

        while (!userver->is_online) {
            SHOW_LOG(3, "Server is currently offline...\n");
            pj_thread_sleep(1000);
        }

        PJ_FD_ZERO(&read_fds);
        PJ_FD_SET(userver->fd, &read_fds);

        pj_mutex_lock(userver->mutex);
        ret = pj_sock_select(userver->fd + 1, &read_fds, NULL, NULL, &timeout); 
        pj_mutex_unlock(userver->mutex);

        EXIT_IF_TRUE(ret < 0, "Error on server socket\n");

        if( PJ_FD_ISSET(userver->fd, &read_fds) ) {
            len = sizeof(caddr);
            pj_bzero(&caddr, len);

            pj_mutex_lock(userver->mutex);
            ret = userver->recv_f(userver->fd, buffer, USERVER_BUFSIZE, (void *)&caddr, &len);
            pj_mutex_unlock(userver->mutex);
            caddr_str = pj_inet_ntoa(caddr.sin_addr);

            if( ret > 0 ) {
                buffer[ret] = '\0';

                lvc_init(&lvc, buffer, ret);

                lvc_unpack(&lvc, &len, &val);
                pj_strset(&pjstr, val, len);
                pph = userver->get_pph_f(&pjstr);
                if( pph != NULL ) {

                    lvc_unpack(&lvc, &len, &val);
                    ts = (uint32_t *)val;
                    ts2str(*ts, sts);

                    lvc_unpack(&lvc, &len, &val);
                    
                    generate_otp(otp, pph, sts);
                    do_decrypt(val, len, plain, &len1, otp);
                    if( pj_ansi_strncmp(sts, plain, len1) == 0 ) {
                        lvc_unpack(&lvc, &len, &val);
                        do_decrypt(val, len, plain, &len1, otp);
                        plain[len1] = '\0';

                        $UPROTO$_parse_request(plain, len1, &request);
                        userver->on_request_f(userver, &request, caddr_str);
                    }
                }
                //else {
                //}
            }
        }
        pj_thread_sleep(100);
    }
	return 0;
}
示例#6
0
文件: userver.c 项目: mocidis/userver
int $UPROTO$_server_proc(void *param) {
    int ret;
    unsigned int len;
    
    int port;
    char *first, *second, *third;
    char cnt_str[30];

    pj_sockaddr_in caddr;
    char *caddr_str;
    pj_time_val timeout;

    pj_fd_set_t read_fds;

	char buffer[USERVER_BUFSIZE];
	$UPROTO$_request_t request;

    $UPROTO$_server_t *userver = ($UPROTO$_server_t *)param;
	ansi_copy_str(cnt_str, userver->connect_str);

    first = cnt_str;
    
    second = strchr(first, ':');
    EXIT_IF_TRUE(second == NULL, "Wrong connection string format\n");
    *second = '\0';
    second++;

    if(0 == strcmp(first, "udp")) {
        third = strchr(second, ':');
        EXIT_IF_TRUE(third == NULL, "Wrong connection string format\n");
        *third = '\0';
        third++;
        port = atoi(third);
        open_udp_socket(userver, second, port);
        if (userver->on_open_socket_f != NULL)
            userver->on_open_socket_f(userver);

        userver->recv_f = &udp_recvfrom;
        userver->send_f = &udp_sendto;
    }
    else if( 0 == strcmp(first, "tty") ) {
        open_tty(userver, second);
        userver->recv_f = &tty_recvfrom;
        userver->send_f = &tty_sendto;
    }
    else {
        EXIT_IF_TRUE(1, "Unsuported protocol\n");
    }

    // thread loop
    timeout.sec = 0;
    timeout.msec = 100;
    userver->is_end = 0;


    while( !userver->is_end ) {

        while (!userver->is_online) {
            SHOW_LOG(3, "Server is currently offline...\n");
            //usleep(8*1000*1000);
            pj_thread_sleep(1000);
        }

        PJ_FD_ZERO(&read_fds);
        PJ_FD_SET(userver->fd, &read_fds);

        pj_mutex_lock(userver->mutex);
        ret = pj_sock_select(userver->fd + 1, &read_fds, NULL, NULL, &timeout); 
        pj_mutex_unlock(userver->mutex);

        EXIT_IF_TRUE(ret < 0, "Error on server socket\n");

        if( PJ_FD_ISSET(userver->fd, &read_fds) ) {
            len = sizeof(caddr);
            pj_bzero(&caddr, len);

            pj_mutex_lock(userver->mutex);
            ret = userver->recv_f(userver->fd, buffer, USERVER_BUFSIZE, (void *)&caddr, &len);
            pj_mutex_unlock(userver->mutex);
            caddr_str = pj_inet_ntoa(caddr.sin_addr);

            if( ret > 0 ) {
                buffer[ret] = '\0';
                SHOW_LOG(5, "Received from client: %s\n", buffer);
                $UPROTO$_parse_request(buffer, ret, &request);
                userver->on_request_f(userver, &request, caddr_str);
            }
        }
        //usleep(100*1000);
        pj_thread_sleep(100);
        // if userver->fd is ready to write. When write finish, call userver->on_sent();
        // else --> time out
    }
	return 0;
}