blob: fa5f71477b44176d06f283353d42943dcc5c630f [file] [log] [blame]
Benny Prijono3fd9fc52008-03-09 23:52:48 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono3fd9fc52008-03-09 23:52:48 +00005 *
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.
10 *
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.
15 *
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
19 */
Benny Prijonod1e862f2008-02-21 15:54:27 +000020#include "turn.h"
Benny Prijono17d10b52008-03-14 17:56:11 +000021#include "auth.h"
22
Benny Prijonoe7226852008-04-13 21:48:44 +000023#define REALM "pjsip.org"
Benny Prijono24a21852008-04-14 04:04:30 +000024//#define TURN_PORT PJ_STUN_TURN_PORT
25#define TURN_PORT 34780
26#define LOG_LEVEL 4
Benny Prijonoe7226852008-04-13 21:48:44 +000027
Benny Prijono708725a2008-03-09 12:55:00 +000028
Benny Prijono9e6d60a2008-03-20 16:32:06 +000029static pj_caching_pool g_cp;
30
Benny Prijono708725a2008-03-09 12:55:00 +000031int err(const char *title, pj_status_t status)
32{
33 char errmsg[PJ_ERR_MSG_SIZE];
34 pj_strerror(status, errmsg, sizeof(errmsg));
35
36 printf("%s: %s\n", title, errmsg);
37 return 1;
38}
39
Benny Prijono9e6d60a2008-03-20 16:32:06 +000040static void dump_status(pj_turn_srv *srv)
41{
42 char addr[80];
43 pj_hash_iterator_t itbuf, *it;
44 pj_time_val now;
45 unsigned i;
46
47 for (i=0; i<srv->core.lis_cnt; ++i) {
48 pj_turn_listener *lis = srv->core.listener[i];
49 printf("Server address : %s\n", lis->info);
50 }
51
52 printf("Worker threads : %d\n", srv->core.thread_cnt);
Nanang Izzuddin838cb322008-12-18 17:52:57 +000053 printf("Total mem usage: %u.%03uMB\n", (unsigned)(g_cp.used_size / 1000000),
54 (unsigned)((g_cp.used_size % 1000000)/1000));
Benny Prijono9e6d60a2008-03-20 16:32:06 +000055 printf("UDP port range : %u %u %u (next/min/max)\n", srv->ports.next_udp,
56 srv->ports.min_udp, srv->ports.max_udp);
57 printf("TCP port range : %u %u %u (next/min/max)\n", srv->ports.next_tcp,
58 srv->ports.min_tcp, srv->ports.max_tcp);
59 printf("Clients # : %u\n", pj_hash_count(srv->tables.alloc));
60
61 puts("");
62
63 if (pj_hash_count(srv->tables.alloc)==0) {
64 return;
65 }
66
67 puts("# Client addr. Alloc addr. Username Lftm Expy #prm #chl");
68 puts("------------------------------------------------------------------------------");
69
70 pj_gettimeofday(&now);
71
72 it = pj_hash_first(srv->tables.alloc, &itbuf);
73 i=1;
74 while (it) {
75 pj_turn_allocation *alloc = (pj_turn_allocation*)
76 pj_hash_this(srv->tables.alloc, it);
Benny Prijono68854002008-03-20 19:21:27 +000077 printf("%-3d %-22s %-22s %-8.*s %-4d %-4ld %-4d %-4d\n",
Benny Prijono9e6d60a2008-03-20 16:32:06 +000078 i,
79 alloc->info,
80 pj_sockaddr_print(&alloc->relay.hkey.addr, addr, sizeof(addr), 3),
81 (int)alloc->cred.data.static_cred.username.slen,
Benny Prijono68854002008-03-20 19:21:27 +000082 alloc->cred.data.static_cred.username.ptr,
Benny Prijono9e6d60a2008-03-20 16:32:06 +000083 alloc->relay.lifetime,
84 alloc->relay.expiry.sec - now.sec,
85 pj_hash_count(alloc->peer_table),
86 pj_hash_count(alloc->ch_table));
Benny Prijono68854002008-03-20 19:21:27 +000087
Benny Prijono9e6d60a2008-03-20 16:32:06 +000088 it = pj_hash_next(srv->tables.alloc, it);
89 ++i;
90 }
91}
92
93static void menu(void)
94{
95 puts("");
96 puts("Menu:");
97 puts(" d Dump status");
98 puts(" q Quit");
99 printf(">> ");
100}
101
102static void console_main(pj_turn_srv *srv)
103{
104 pj_bool_t quit = PJ_FALSE;
105
106 while (!quit) {
107 char line[10];
108
109 menu();
110
111 fgets(line, sizeof(line), stdin);
112
113 switch (line[0]) {
114 case 'd':
115 dump_status(srv);
116 break;
117 case 'q':
118 quit = PJ_TRUE;
119 break;
120 }
121 }
122}
123
Benny Prijono708725a2008-03-09 12:55:00 +0000124int main()
125{
Benny Prijono708725a2008-03-09 12:55:00 +0000126 pj_turn_srv *srv;
127 pj_turn_listener *listener;
128 pj_status_t status;
129
130 status = pj_init();
131 if (status != PJ_SUCCESS)
132 return err("pj_init() error", status);
133
Benny Prijono17d10b52008-03-14 17:56:11 +0000134 pjlib_util_init();
135 pjnath_init();
136
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000137 pj_caching_pool_init(&g_cp, NULL, 0);
Benny Prijono708725a2008-03-09 12:55:00 +0000138
Benny Prijono17d10b52008-03-14 17:56:11 +0000139 pj_turn_auth_init(REALM);
140
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000141 status = pj_turn_srv_create(&g_cp.factory, &srv);
Benny Prijono708725a2008-03-09 12:55:00 +0000142 if (status != PJ_SUCCESS)
143 return err("Error creating server", status);
144
Benny Prijono17d10b52008-03-14 17:56:11 +0000145 status = pj_turn_listener_create_udp(srv, pj_AF_INET(), NULL,
Benny Prijonoe7226852008-04-13 21:48:44 +0000146 TURN_PORT, 1, 0, &listener);
Benny Prijono708725a2008-03-09 12:55:00 +0000147 if (status != PJ_SUCCESS)
Benny Prijono879ad1a2008-04-09 09:38:12 +0000148 return err("Error creating UDP listener", status);
149
Benny Prijono1dd54202008-07-25 10:45:34 +0000150#if PJ_HAS_TCP
Benny Prijono879ad1a2008-04-09 09:38:12 +0000151 status = pj_turn_listener_create_tcp(srv, pj_AF_INET(), NULL,
Benny Prijonoe7226852008-04-13 21:48:44 +0000152 TURN_PORT, 1, 0, &listener);
Benny Prijono879ad1a2008-04-09 09:38:12 +0000153 if (status != PJ_SUCCESS)
Benny Prijono708725a2008-03-09 12:55:00 +0000154 return err("Error creating listener", status);
Benny Prijono1dd54202008-07-25 10:45:34 +0000155#endif
Benny Prijono708725a2008-03-09 12:55:00 +0000156
157 status = pj_turn_srv_add_listener(srv, listener);
158 if (status != PJ_SUCCESS)
159 return err("Error adding listener", status);
160
161 puts("Server is running");
Benny Prijono708725a2008-03-09 12:55:00 +0000162
Benny Prijono24a21852008-04-14 04:04:30 +0000163 pj_log_set_level(LOG_LEVEL);
164
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000165 console_main(srv);
Benny Prijono708725a2008-03-09 12:55:00 +0000166
167 pj_turn_srv_destroy(srv);
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000168 pj_caching_pool_destroy(&g_cp);
Benny Prijono708725a2008-03-09 12:55:00 +0000169 pj_shutdown();
170
171 return 0;
172}
173