コード例 #1
0
ファイル: test.c プロジェクト: aemonfly/android-client
int check_pjlib_state(pj_stun_config *cfg, 
		      const struct pjlib_state *initial_st)
{
    struct pjlib_state current_state;
    int rc = 0;

    capture_pjlib_state(cfg, &current_state);

    if (current_state.timer_cnt > initial_st->timer_cnt) {
	PJ_LOG(3,("", "    error: possibly leaking timer"));
	rc |= ERR_TIMER_LEAK;

#if PJ_TIMER_DEBUG
	pj_timer_heap_dump(cfg->timer_heap);
#endif
    }

    if (current_state.pool_used_cnt > initial_st->pool_used_cnt) {
	PJ_LOG(3,("", "    error: possibly leaking memory"));
	PJ_LOG(3,("", "    dumping memory pool:"));
	pj_pool_factory_dump(mem, PJ_TRUE);
	rc |= ERR_MEMORY_LEAK;
    }

    return rc;
}
コード例 #2
0
static void server_main(pj_stun_server *srv)
{
    int quit = 0;

    while (!quit) {
	char line[10];

	printf("Menu:\n"
	       "  d     Dump status\n"
	       "  q     Quit\n"
	       "Choice:");

	fgets(line, sizeof(line), stdin);
	if (line[0] == 'q') {
	    quit = 1;
	} else if (line[0] == 'd') {
	    pj_stun_server_info *si = pj_stun_server_get_info(srv);
	    pj_pool_factory_dump(si->pf, PJ_TRUE);
	}
    }
}
コード例 #3
0
static int client_shutdown()
{
    unsigned i;

    if (g.thread) {
	g.quit = 1;
	pj_thread_join(g.thread);
	pj_thread_destroy(g.thread);
	g.thread = NULL;
    }
    if (g.relay) {
	pj_turn_sock_destroy(g.relay);
	g.relay = NULL;
    }
    for (i=0; i<PJ_ARRAY_SIZE(g.peer); ++i) {
	if (g.peer[i].stun_sock) {
	    pj_stun_sock_destroy(g.peer[i].stun_sock);
	    g.peer[i].stun_sock = NULL;
	}
    }
    if (g.stun_config.timer_heap) {
	pj_timer_heap_destroy(g.stun_config.timer_heap);
	g.stun_config.timer_heap = NULL;
    }
    if (g.stun_config.ioqueue) {
	pj_ioqueue_destroy(g.stun_config.ioqueue);
	g.stun_config.ioqueue = NULL;
    }
    if (g.pool) {
	pj_pool_release(g.pool);
	g.pool = NULL;
    }
    pj_pool_factory_dump(&g.cp.factory, PJ_TRUE);
    pj_caching_pool_destroy(&g.cp);

    return PJ_SUCCESS;
}
コード例 #4
0
static void console_main(void)
{
    while (!g.quit) {
	char input[32];
	struct peer *peer;
	pj_status_t status;

	menu();

	if (fgets(input, sizeof(input), stdin) == NULL)
	    break;
	
	switch (input[0]) {
	case 'a':
	    create_relay();
	    break;
	case 'd':
	    pj_pool_factory_dump(&g.cp.factory, PJ_TRUE);
	    break;
	case 's':
	    if (g.relay == NULL) {
		puts("Error: no relay");
		continue;
	    }
	    if (input[1]!='s')
		peer = &g.peer[0];
	    else
		peer = &g.peer[1];

	    strcpy(input, "Hello from client");
	    status = pj_turn_sock_sendto(g.relay, (const pj_uint8_t*)input, 
					strlen(input)+1, 
					&peer->mapped_addr, 
					pj_sockaddr_get_len(&peer->mapped_addr));
	    if (status != PJ_SUCCESS)
		my_perror("turn_udp_sendto() failed", status);
	    break;
	case 'b':
	    if (g.relay == NULL) {
		puts("Error: no relay");
		continue;
	    }
	    if (input[1]!='b')
		peer = &g.peer[0];
	    else
		peer = &g.peer[1];

	    status = pj_turn_sock_bind_channel(g.relay, &peer->mapped_addr,
					      pj_sockaddr_get_len(&peer->mapped_addr));
	    if (status != PJ_SUCCESS)
		my_perror("turn_udp_bind_channel() failed", status);
	    break;
	case 'p':
	    if (g.relay == NULL) {
		puts("Error: no relay");
		continue;
	    }
	    if (input[1]!='p')
		peer = &g.peer[0];
	    else
		peer = &g.peer[1];

	    status = pj_turn_sock_set_perm(g.relay, 1, &peer->mapped_addr, 1);
	    if (status != PJ_SUCCESS)
		my_perror("pj_turn_sock_set_perm() failed", status);
	    break;
	case 'x':
	    if (g.relay == NULL) {
		puts("Error: no relay");
		continue;
	    }
	    destroy_relay();
	    break;
	case '0':
	case '1':
	    if (g.relay == NULL) {
		puts("No relay");
		break;
	    }
	    peer = &g.peer[input[0]-'0'];
	    sprintf(input, "Hello from peer%d", input[0]-'0');
	    pj_stun_sock_sendto(peer->stun_sock, NULL, input, strlen(input)+1, 0,
				&g.relay_addr, pj_sockaddr_get_len(&g.relay_addr));
	    break;
	case 'q':
	    g.quit = PJ_TRUE;
	    break;
	}
    }
}
コード例 #5
0
int main(int argc, char *argv[])
{
    struct pj_getopt_option long_options[] = {
	{ "realm",	1, 0, 'r'},
	{ "username",	1, 0, 'u'},
	{ "password",	1, 0, 'p'},
	{ "nonce",	1, 0, 'N'},
	{ "fingerprint",0, 0, 'F'},
	{ "help",	0, 0, 'h'}
    };
    int c, opt_id;
    pj_caching_pool cp;
    pj_stun_server *srv;
    pj_stun_usage *turn;
    pj_status_t status;

    while((c=pj_getopt_long(argc,argv, "r:u:p:N:hF", long_options, &opt_id))!=-1) {
	switch (c) {
	case 'r':
	    o.realm = pj_optarg;
	    break;
	case 'u':
	    o.user_name = pj_optarg;
	    break;
	case 'p':
	    o.password = pj_optarg;
	    break;
	case 'N':
	    o.nonce = pj_optarg;
	    break;
	case 'h':
	    usage();
	    return 0;
	case 'F':
	    o.use_fingerprint = PJ_TRUE;
	    break;
	default:
	    printf("Argument \"%s\" is not valid. Use -h to see help",
		   argv[pj_optind]);
	    return 1;
	}
    }

    if (pj_optind != argc) {
	puts("Error: invalid arguments");
	return 1;
    }

    pj_init();
    pjlib_util_init();
    pjnath_init();
    pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);

    status = pj_stun_server_create(&cp.factory, 1, &srv);
    if (status != PJ_SUCCESS) {
	pj_stun_perror(THIS_FILE, "Unable to create server", status);
	return 1;
    }

    /*
    status = pj_stun_bind_usage_create(srv, NULL, 3478, NULL);
    if (status != PJ_SUCCESS) {
	pj_stun_perror(THIS_FILE, "Unable to create bind usage", status);
	return 1;
    }
    */

    status = pj_stun_turn_usage_create(srv, pj_SOCK_DGRAM(), NULL,
				       3478, o.use_fingerprint, &turn);
    if (status != PJ_SUCCESS) {
	pj_stun_perror(THIS_FILE, "Unable to create bind usage", status);
	return 1;
    }

    if (o.user_name && o.password) {
	pj_stun_auth_cred cred;
	pj_bzero(&cred, sizeof(cred));
	cred.type = PJ_STUN_AUTH_CRED_STATIC;
	cred.data.static_cred.realm = pj_str(o.realm);
	cred.data.static_cred.username = pj_str(o.user_name);
	cred.data.static_cred.data_type = 0;
	cred.data.static_cred.data = pj_str(o.password);
	cred.data.static_cred.nonce = pj_str(o.nonce);
	pj_stun_turn_usage_set_credential(turn, &cred);
    }

    server_main(srv);

    pj_stun_server_destroy(srv);
    pj_pool_factory_dump(&cp.factory, PJ_TRUE);
    pj_shutdown();
    return 0;
}