blob: 164b3c25cefe525a582c09324f10ea80c0c6f811 [file] [log] [blame]
Benny Prijono3fd9fc52008-03-09 23:52:48 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijonod1e862f2008-02-21 15:54:27 +000019#include "turn.h"
Benny Prijono17d10b52008-03-14 17:56:11 +000020#include "auth.h"
21
22#define REALM "pjsip.org"
Benny Prijono708725a2008-03-09 12:55:00 +000023
Benny Prijono9e6d60a2008-03-20 16:32:06 +000024static pj_caching_pool g_cp;
25
Benny Prijono708725a2008-03-09 12:55:00 +000026int err(const char *title, pj_status_t status)
27{
28 char errmsg[PJ_ERR_MSG_SIZE];
29 pj_strerror(status, errmsg, sizeof(errmsg));
30
31 printf("%s: %s\n", title, errmsg);
32 return 1;
33}
34
Benny Prijono9e6d60a2008-03-20 16:32:06 +000035static void dump_status(pj_turn_srv *srv)
36{
37 char addr[80];
38 pj_hash_iterator_t itbuf, *it;
39 pj_time_val now;
40 unsigned i;
41
42 for (i=0; i<srv->core.lis_cnt; ++i) {
43 pj_turn_listener *lis = srv->core.listener[i];
44 printf("Server address : %s\n", lis->info);
45 }
46
47 printf("Worker threads : %d\n", srv->core.thread_cnt);
48 printf("Total mem usage: %d.%03dMB\n", g_cp.used_size / 1000000,
49 (g_cp.used_size % 1000000)/1000);
50 printf("UDP port range : %u %u %u (next/min/max)\n", srv->ports.next_udp,
51 srv->ports.min_udp, srv->ports.max_udp);
52 printf("TCP port range : %u %u %u (next/min/max)\n", srv->ports.next_tcp,
53 srv->ports.min_tcp, srv->ports.max_tcp);
54 printf("Clients # : %u\n", pj_hash_count(srv->tables.alloc));
55
56 puts("");
57
58 if (pj_hash_count(srv->tables.alloc)==0) {
59 return;
60 }
61
62 puts("# Client addr. Alloc addr. Username Lftm Expy #prm #chl");
63 puts("------------------------------------------------------------------------------");
64
65 pj_gettimeofday(&now);
66
67 it = pj_hash_first(srv->tables.alloc, &itbuf);
68 i=1;
69 while (it) {
70 pj_turn_allocation *alloc = (pj_turn_allocation*)
71 pj_hash_this(srv->tables.alloc, it);
72 printf("%-3d %-22s %-22s %-8.*s %-4d %-4d %-4d %-4d\n",
73 i,
74 alloc->info,
75 pj_sockaddr_print(&alloc->relay.hkey.addr, addr, sizeof(addr), 3),
76 (int)alloc->cred.data.static_cred.username.slen,
77 (int)alloc->cred.data.static_cred.username.ptr,
78 alloc->relay.lifetime,
79 alloc->relay.expiry.sec - now.sec,
80 pj_hash_count(alloc->peer_table),
81 pj_hash_count(alloc->ch_table));
82 it = pj_hash_next(srv->tables.alloc, it);
83 ++i;
84 }
85}
86
87static void menu(void)
88{
89 puts("");
90 puts("Menu:");
91 puts(" d Dump status");
92 puts(" q Quit");
93 printf(">> ");
94}
95
96static void console_main(pj_turn_srv *srv)
97{
98 pj_bool_t quit = PJ_FALSE;
99
100 while (!quit) {
101 char line[10];
102
103 menu();
104
105 fgets(line, sizeof(line), stdin);
106
107 switch (line[0]) {
108 case 'd':
109 dump_status(srv);
110 break;
111 case 'q':
112 quit = PJ_TRUE;
113 break;
114 }
115 }
116}
117
Benny Prijono708725a2008-03-09 12:55:00 +0000118int main()
119{
Benny Prijono708725a2008-03-09 12:55:00 +0000120 pj_turn_srv *srv;
121 pj_turn_listener *listener;
122 pj_status_t status;
123
124 status = pj_init();
125 if (status != PJ_SUCCESS)
126 return err("pj_init() error", status);
127
Benny Prijono17d10b52008-03-14 17:56:11 +0000128 pjlib_util_init();
129 pjnath_init();
130
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000131 pj_caching_pool_init(&g_cp, NULL, 0);
Benny Prijono708725a2008-03-09 12:55:00 +0000132
Benny Prijono17d10b52008-03-14 17:56:11 +0000133 pj_turn_auth_init(REALM);
134
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000135 status = pj_turn_srv_create(&g_cp.factory, &srv);
Benny Prijono708725a2008-03-09 12:55:00 +0000136 if (status != PJ_SUCCESS)
137 return err("Error creating server", status);
138
Benny Prijono17d10b52008-03-14 17:56:11 +0000139 status = pj_turn_listener_create_udp(srv, pj_AF_INET(), NULL,
140 PJ_STUN_PORT, 1, 0, &listener);
Benny Prijono708725a2008-03-09 12:55:00 +0000141 if (status != PJ_SUCCESS)
142 return err("Error creating listener", status);
143
144 status = pj_turn_srv_add_listener(srv, listener);
145 if (status != PJ_SUCCESS)
146 return err("Error adding listener", status);
147
148 puts("Server is running");
Benny Prijono708725a2008-03-09 12:55:00 +0000149
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000150 console_main(srv);
Benny Prijono708725a2008-03-09 12:55:00 +0000151
152 pj_turn_srv_destroy(srv);
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000153 pj_caching_pool_destroy(&g_cp);
Benny Prijono708725a2008-03-09 12:55:00 +0000154 pj_shutdown();
155
156 return 0;
157}
158