Debian Codegraph @

Xpjproject/pjnath/src/pjturn-srv/main.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 */
19 #include "turn.h"
20 #include "auth.h"
21
22 #define REALM "pjsip.org"
23 //#define TURN_PORT PJ_STUN_TURN_PORT
24 #define TURN_PORT 34780
25 #define LOG_LEVEL 4
26
27
28 static pj_caching_pool g_cp;
29
30 int err(const char *title, pj_status_t status)
31 {
32 char errmsg[PJ_ERR_MSG_SIZE];
33 pj_strerror(status, errmsg, sizeof(errmsg));
34
35 printf("%s: %s\n", title, errmsg);
36 return 1;
37 }
38
39 static void dump_status(pj_turn_srv *srv)
40 {
41 char addr[80];
42 pj_hash_iterator_t itbuf, *it;
43 pj_time_val now;
44 unsigned i;
45
46 for (i=0; i<srv->core.lis_cnt; ++i) {
47 pj_turn_listener *lis = srv->core.listener[i];
48 printf("Server address : %s\n", lis->info);
49 }
50
51 printf("Worker threads : %d\n", srv->core.thread_cnt);
52 /* Field used_size is deprecated by #3897 */
53 //printf("Total mem usage: %u.%03uMB\n", (unsigned)(g_cp.used_size / 1000000),
54 // (unsigned)((g_cp.used_size % 1000000)/1000));
55 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);
77 printf("%-3d %-22s %-22s %-8.*s %-4d %-4ld %-4d %-4d\n",
78 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,
82 alloc->cred.data.static_cred.username.ptr,
83 alloc->relay.lifetime,
84 alloc->relay.expiry.sec - now.sec,
85 pj_hash_count(alloc->peer_table),
86 pj_hash_count(alloc->ch_table));
87
88 it = pj_hash_next(srv->tables.alloc, it);
89 ++i;
90 }
91 }
92
93 static void menu(void)
94 {
95 puts("");
96 puts("Menu:");
97 puts(" d Dump status");
98 puts(" q Quit");
99 printf(">> ");
100 }
101
102 static 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 if (fgets(line, sizeof(line), stdin) == NULL)
112 break;
113
114 switch (line[0]) {
115 case 'd':
116 dump_status(srv);
117 break;
118 case 'q':
119 quit = PJ_TRUE;
120 break;
121 }
122 }
123 }
124
125 int main()
126 {
127 pj_turn_srv *srv;
128 pj_turn_listener *listener;
129 pj_status_t status;
130
131 status = pj_init();
132 if (status != PJ_SUCCESS)
133 return err("pj_init() error", status);
134
135 status = pjlib_util_init();
136 if (status != PJ_SUCCESS)
137 return err("pjlib_util_init() error", status);
138
139 status = pjnath_init();
140 if (status != PJ_SUCCESS)
141 return err("pjnath_init() error", status);
142
143 pj_caching_pool_init(&g_cp, NULL, 0);
144
145 pj_turn_auth_init(REALM);
146
147 status = pj_turn_srv_create(&g_cp.factory, &srv);
148 if (status != PJ_SUCCESS)
149 return err("Error creating server", status);
150
151 status = pj_turn_listener_create_udp(srv, pj_AF_INET(), NULL,
152 TURN_PORT, 1, 0, &listener);
153 if (status != PJ_SUCCESS)
154 return err("Error creating UDP listener", status);
155
156 #if PJ_HAS_TCP
157 status = pj_turn_listener_create_tcp(srv, pj_AF_INET(), NULL,
158 TURN_PORT, 1, 0, &listener);
159 if (status != PJ_SUCCESS)
160 return err("Error creating listener", status);
161 #endif
162
163 status = pj_turn_srv_add_listener(srv, listener);
164 if (status != PJ_SUCCESS)
165 return err("Error adding listener", status);
166
167 puts("Server is running");
168
169 pj_log_set_level(LOG_LEVEL);
170
171 console_main(srv);
172
173 pj_turn_srv_destroy(srv);
174 pj_caching_pool_destroy(&g_cp);
175 pj_shutdown();
176
177 return 0;
178 }
179