1
/* $Id: client_main.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <pjlib-util.h>
25
#define THIS_FILE "client_main.c"
26
#define LOCAL_PORT 1998
27
#define BANDWIDTH 64 /* -1 to disable */
28
#define LIFETIME 600 /* -1 to disable */
29
#define REQ_TRANSPORT -1 /* 0: udp, 1: tcp, -1: disable */
30
#define REQ_PORT_PROPS -1 /* -1 to disable */
31
#define REQ_IP 0 /* IP address string */
33
//#define OPTIONS PJ_STUN_NO_AUTHENTICATE
39
pj_stun_sock *stun_sock;
40
pj_sockaddr mapped_addr;
48
pj_stun_config stun_config;
52
pj_dns_resolver *resolver;
55
pj_sockaddr relay_addr;
68
pj_bool_t use_fingerprint;
74
static int worker_thread(void *unused);
75
static void turn_on_rx_data(pj_turn_sock *relay,
78
const pj_sockaddr_t *peer_addr,
80
static void turn_on_state(pj_turn_sock *relay, pj_turn_state_t old_state,
81
pj_turn_state_t new_state);
82
static pj_bool_t stun_sock_on_status(pj_stun_sock *stun_sock,
85
static pj_bool_t stun_sock_on_rx_data(pj_stun_sock *stun_sock,
88
const pj_sockaddr_t *src_addr,
92
static void my_perror(const char *title, pj_status_t status)
94
char errmsg[PJ_ERR_MSG_SIZE];
95
pj_strerror(status, errmsg, sizeof(errmsg));
97
PJ_LOG(3,(THIS_FILE, "%s: %s", title, errmsg));
100
#define CHECK(expr) status=expr; \
101
if (status!=PJ_SUCCESS) { \
102
my_perror(#expr, status); \
112
CHECK( pjlib_util_init() );
113
CHECK( pjnath_init() );
115
/* Check that server is specified */
117
printf("Error: server must be specified\n");
121
pj_caching_pool_init(&g.cp, &pj_pool_factory_default_policy, 0);
123
g.pool = pj_pool_create(&g.cp.factory, "main", 1000, 1000, NULL);
125
/* Init global STUN config */
126
pj_stun_config_init(&g.stun_config, &g.cp.factory, 0, NULL, NULL);
128
/* Create global timer heap */
129
CHECK( pj_timer_heap_create(g.pool, 1000, &g.stun_config.timer_heap) );
131
/* Create global ioqueue */
132
CHECK( pj_ioqueue_create(g.pool, 16, &g.stun_config.ioqueue) );
137
for (i=0; i<(int)PJ_ARRAY_SIZE(g.peer); ++i) {
138
pj_stun_sock_cb stun_sock_cb;
139
char name[] = "peer0";
141
pj_stun_sock_cfg ss_cfg;
144
pj_bzero(&stun_sock_cb, sizeof(stun_sock_cb));
145
stun_sock_cb.on_rx_data = &stun_sock_on_rx_data;
146
stun_sock_cb.on_status = &stun_sock_on_status;
148
g.peer[i].mapped_addr.addr.sa_family = pj_AF_INET();
150
pj_stun_sock_cfg_default(&ss_cfg);
152
/* make reading the log easier */
153
ss_cfg.ka_interval = 300;
156
name[strlen(name)-1] = '0'+i;
157
status = pj_stun_sock_create(&g.stun_config, name, pj_AF_INET(),
158
&stun_sock_cb, &ss_cfg,
159
&g.peer[i], &g.peer[i].stun_sock);
160
if (status != PJ_SUCCESS) {
161
my_perror("pj_stun_sock_create()", status);
166
server = pj_str(o.stun_server);
169
server = pj_str(o.srv_addr);
170
port = (pj_uint16_t)(o.srv_port?atoi(o.srv_port):PJ_STUN_PORT);
172
status = pj_stun_sock_start(g.peer[i].stun_sock, &server,
174
if (status != PJ_SUCCESS) {
175
my_perror("pj_stun_sock_start()", status);
180
/* Start the worker thread */
181
CHECK( pj_thread_create(g.pool, "stun", &worker_thread, NULL, 0, 0, &g.thread) );
188
static int client_shutdown()
194
pj_thread_join(g.thread);
195
pj_thread_destroy(g.thread);
199
pj_turn_sock_destroy(g.relay);
202
for (i=0; i<PJ_ARRAY_SIZE(g.peer); ++i) {
203
if (g.peer[i].stun_sock) {
204
pj_stun_sock_destroy(g.peer[i].stun_sock);
205
g.peer[i].stun_sock = NULL;
208
if (g.stun_config.timer_heap) {
209
pj_timer_heap_destroy(g.stun_config.timer_heap);
210
g.stun_config.timer_heap = NULL;
212
if (g.stun_config.ioqueue) {
213
pj_ioqueue_destroy(g.stun_config.ioqueue);
214
g.stun_config.ioqueue = NULL;
217
pj_pool_release(g.pool);
220
pj_pool_factory_dump(&g.cp.factory, PJ_TRUE);
221
pj_caching_pool_destroy(&g.cp);
227
static int worker_thread(void *unused)
229
PJ_UNUSED_ARG(unused);
232
const pj_time_val delay = {0, 10};
234
/* Poll ioqueue for the TURN client */
235
pj_ioqueue_poll(g.stun_config.ioqueue, &delay);
237
/* Poll the timer heap */
238
pj_timer_heap_poll(g.stun_config.timer_heap, NULL);
245
static pj_status_t create_relay(void)
247
pj_turn_sock_cb rel_cb;
248
pj_stun_auth_cred cred;
253
PJ_LOG(1,(THIS_FILE, "Relay already created"));
257
/* Create DNS resolver if configured */
259
pj_str_t ns = pj_str(o.nameserver);
261
status = pj_dns_resolver_create(&g.cp.factory, "resolver", 0,
262
g.stun_config.timer_heap,
263
g.stun_config.ioqueue, &g.resolver);
264
if (status != PJ_SUCCESS) {
265
PJ_LOG(1,(THIS_FILE, "Error creating resolver (err=%d)", status));
269
status = pj_dns_resolver_set_ns(g.resolver, 1, &ns, NULL);
270
if (status != PJ_SUCCESS) {
271
PJ_LOG(1,(THIS_FILE, "Error configuring nameserver (err=%d)", status));
276
pj_bzero(&rel_cb, sizeof(rel_cb));
277
rel_cb.on_rx_data = &turn_on_rx_data;
278
rel_cb.on_state = &turn_on_state;
279
CHECK( pj_turn_sock_create(&g.stun_config, pj_AF_INET(),
280
(o.use_tcp? PJ_TURN_TP_TCP : PJ_TURN_TP_UDP),
285
pj_bzero(&cred, sizeof(cred));
286
cred.type = PJ_STUN_AUTH_CRED_STATIC;
287
cred.data.static_cred.realm = pj_str(o.realm);
288
cred.data.static_cred.username = pj_str(o.user_name);
289
cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
290
cred.data.static_cred.data = pj_str(o.password);
291
//cred.data.static_cred.nonce = pj_str(o.nonce);
293
PJ_LOG(2,(THIS_FILE, "Warning: no credential is set"));
296
srv = pj_str(o.srv_addr);
297
CHECK(pj_turn_sock_alloc(g.relay, /* the relay */
299
(o.srv_port?atoi(o.srv_port):PJ_STUN_PORT),/* def port */
300
g.resolver, /* resolver */
301
(o.user_name?&cred:NULL), /* credential */
302
NULL) /* alloc param */
308
static void destroy_relay(void)
311
pj_turn_sock_destroy(g.relay);
316
static void turn_on_rx_data(pj_turn_sock *relay,
319
const pj_sockaddr_t *peer_addr,
324
pj_sockaddr_print(peer_addr, addrinfo, sizeof(addrinfo), 3);
326
PJ_LOG(3,(THIS_FILE, "Client received %d bytes data from %s: %.*s",
327
pkt_len, addrinfo, pkt_len, pkt));
331
static void turn_on_state(pj_turn_sock *relay, pj_turn_state_t old_state,
332
pj_turn_state_t new_state)
334
PJ_LOG(3,(THIS_FILE, "State %s --> %s", pj_turn_state_name(old_state),
335
pj_turn_state_name(new_state)));
337
if (new_state == PJ_TURN_STATE_READY) {
338
pj_turn_session_info info;
339
pj_turn_sock_get_info(relay, &info);
340
pj_memcpy(&g.relay_addr, &info.relay_addr, sizeof(pj_sockaddr));
341
} else if (new_state > PJ_TURN_STATE_READY && g.relay) {
342
PJ_LOG(3,(THIS_FILE, "Relay shutting down.."));
347
static pj_bool_t stun_sock_on_status(pj_stun_sock *stun_sock,
351
struct peer *peer = (struct peer*) pj_stun_sock_get_user_data(stun_sock);
353
if (status == PJ_SUCCESS) {
354
PJ_LOG(4,(THIS_FILE, "peer%d: %s success", peer-g.peer,
355
pj_stun_sock_op_name(op)));
357
char errmsg[PJ_ERR_MSG_SIZE];
358
pj_strerror(status, errmsg, sizeof(errmsg));
359
PJ_LOG(1,(THIS_FILE, "peer%d: %s error: %s", peer-g.peer,
360
pj_stun_sock_op_name(op), errmsg));
364
if (op==PJ_STUN_SOCK_BINDING_OP || op==PJ_STUN_SOCK_KEEP_ALIVE_OP) {
365
pj_stun_sock_info info;
368
pj_stun_sock_get_info(stun_sock, &info);
369
cmp = pj_sockaddr_cmp(&info.mapped_addr, &peer->mapped_addr);
372
char straddr[PJ_INET6_ADDRSTRLEN+10];
374
pj_sockaddr_cp(&peer->mapped_addr, &info.mapped_addr);
375
pj_sockaddr_print(&peer->mapped_addr, straddr, sizeof(straddr), 3);
376
PJ_LOG(3,(THIS_FILE, "peer%d: STUN mapped address is %s",
377
peer-g.peer, straddr));
384
static pj_bool_t stun_sock_on_rx_data(pj_stun_sock *stun_sock,
387
const pj_sockaddr_t *src_addr,
390
struct peer *peer = (struct peer*) pj_stun_sock_get_user_data(stun_sock);
391
char straddr[PJ_INET6_ADDRSTRLEN+10];
393
((char*)pkt)[pkt_len] = '\0';
395
pj_sockaddr_print(src_addr, straddr, sizeof(straddr), 3);
396
PJ_LOG(3,(THIS_FILE, "peer%d: received %d bytes data from %s: %s",
397
peer-g.peer, pkt_len, straddr, (char*)pkt));
403
static void menu(void)
405
pj_turn_session_info info;
406
char client_state[20], relay_addr[80], peer0_addr[80], peer1_addr[80];
409
pj_turn_sock_get_info(g.relay, &info);
410
strcpy(client_state, pj_turn_state_name(info.state));
411
if (info.state >= PJ_TURN_STATE_READY)
412
pj_sockaddr_print(&info.relay_addr, relay_addr, sizeof(relay_addr), 3);
414
strcpy(relay_addr, "0.0.0.0:0");
416
strcpy(client_state, "NULL");
417
strcpy(relay_addr, "0.0.0.0:0");
420
pj_sockaddr_print(&g.peer[0].mapped_addr, peer0_addr, sizeof(peer0_addr), 3);
421
pj_sockaddr_print(&g.peer[1].mapped_addr, peer1_addr, sizeof(peer1_addr), 3);
425
puts("+=====================================================================+");
426
puts("| CLIENT | PEER-0 |");
428
printf("| State : %-12s | Address: %-21s |\n",
429
client_state, peer0_addr);
430
printf("| Relay addr: %-21s | |\n",
432
puts("| | 0 Send data to relay address |");
433
puts("| a Allocate relay | |");
434
puts("| p,pp Set permission for peer 0/1 +--------------------------------+");
435
puts("| s,ss Send data to peer 0/1 | PEER-1 |");
436
puts("| b,bb BindChannel to peer 0/1 | |");
437
printf("| x Delete allocation | Address: %-21s |\n",
439
puts("+------------------------------------+ |");
440
puts("| q Quit d Dump | 1 Send data to relay adderss |");
441
puts("+------------------------------------+--------------------------------+");
447
static void console_main(void)
456
if (fgets(input, sizeof(input), stdin) == NULL)
464
pj_pool_factory_dump(&g.cp.factory, PJ_TRUE);
467
if (g.relay == NULL) {
468
puts("Error: no relay");
476
strcpy(input, "Hello from client");
477
status = pj_turn_sock_sendto(g.relay, (const pj_uint8_t*)input,
480
pj_sockaddr_get_len(&peer->mapped_addr));
481
if (status != PJ_SUCCESS)
482
my_perror("turn_udp_sendto() failed", status);
485
if (g.relay == NULL) {
486
puts("Error: no relay");
494
status = pj_turn_sock_bind_channel(g.relay, &peer->mapped_addr,
495
pj_sockaddr_get_len(&peer->mapped_addr));
496
if (status != PJ_SUCCESS)
497
my_perror("turn_udp_bind_channel() failed", status);
500
if (g.relay == NULL) {
501
puts("Error: no relay");
509
status = pj_turn_sock_set_perm(g.relay, 1, &peer->mapped_addr, 1);
510
if (status != PJ_SUCCESS)
511
my_perror("pj_turn_sock_set_perm() failed", status);
514
if (g.relay == NULL) {
515
puts("Error: no relay");
522
if (g.relay == NULL) {
526
peer = &g.peer[input[0]-'0'];
527
sprintf(input, "Hello from peer%d", input[0]-'0');
528
pj_stun_sock_sendto(peer->stun_sock, NULL, input, strlen(input)+1, 0,
529
&g.relay_addr, pj_sockaddr_get_len(&g.relay_addr));
539
static void usage(void)
541
puts("Usage: pjturn_client TURN-SERVER [OPTIONS]");
543
puts("where TURN-SERVER is \"host[:port]\"");
545
puts("and OPTIONS:");
546
puts(" --tcp, -T Use TCP to connect to TURN server");
547
puts(" --realm, -r REALM Set realm of the credential to REALM");
548
puts(" --username, -u UID Set username of the credential to UID");
549
puts(" --password, -p PASSWD Set password of the credential to PASSWD");
550
puts(" --fingerprint, -F Use fingerprint for outgoing requests");
551
puts(" --stun-srv, -S NAME Use this STUN srv instead of TURN for Binding discovery");
552
puts(" --nameserver, -N IP Activate DNS SRV, use this DNS server");
556
int main(int argc, char *argv[])
558
struct pj_getopt_option long_options[] = {
559
{ "realm", 1, 0, 'r'},
560
{ "username", 1, 0, 'u'},
561
{ "password", 1, 0, 'p'},
562
{ "fingerprint",0, 0, 'F'},
564
{ "help", 0, 0, 'h'},
565
{ "stun-srv", 1, 0, 'S'},
566
{ "nameserver", 1, 0, 'N'}
572
while((c=pj_getopt_long(argc,argv, "r:u:p:S:N:hFT", long_options, &opt_id))!=-1) {
578
o.user_name = pj_optarg;
581
o.password = pj_optarg;
587
o.use_fingerprint = PJ_TRUE;
593
o.stun_server = pj_optarg;
596
o.nameserver = pj_optarg;
599
printf("Argument \"%s\" is not valid. Use -h to see help",
605
if (pj_optind == argc) {
606
puts("Error: TARGET is needed");
611
if ((pos=pj_ansi_strchr(argv[pj_optind], ':')) != NULL) {
612
o.srv_addr = argv[pj_optind];
616
o.srv_addr = argv[pj_optind];
619
if ((status=init()) != 0)
622
//if ((status=create_relay()) != 0)
629
return status ? 1 : 0;