Benny Prijono | 3fd9fc5 | 2008-03-09 23:52:48 +0000 | [diff] [blame] | 1 | /* $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 Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 19 | #include "turn.h" |
Benny Prijono | 17d10b5 | 2008-03-14 17:56:11 +0000 | [diff] [blame] | 20 | #include "auth.h" |
| 21 | |
| 22 | #define REALM "pjsip.org" |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 23 | |
Benny Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame^] | 24 | static pj_caching_pool g_cp; |
| 25 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 26 | int 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 Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame^] | 35 | static 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 | |
| 87 | static void menu(void) |
| 88 | { |
| 89 | puts(""); |
| 90 | puts("Menu:"); |
| 91 | puts(" d Dump status"); |
| 92 | puts(" q Quit"); |
| 93 | printf(">> "); |
| 94 | } |
| 95 | |
| 96 | static 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 Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 118 | int main() |
| 119 | { |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 120 | 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 Prijono | 17d10b5 | 2008-03-14 17:56:11 +0000 | [diff] [blame] | 128 | pjlib_util_init(); |
| 129 | pjnath_init(); |
| 130 | |
Benny Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame^] | 131 | pj_caching_pool_init(&g_cp, NULL, 0); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 132 | |
Benny Prijono | 17d10b5 | 2008-03-14 17:56:11 +0000 | [diff] [blame] | 133 | pj_turn_auth_init(REALM); |
| 134 | |
Benny Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame^] | 135 | status = pj_turn_srv_create(&g_cp.factory, &srv); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 136 | if (status != PJ_SUCCESS) |
| 137 | return err("Error creating server", status); |
| 138 | |
Benny Prijono | 17d10b5 | 2008-03-14 17:56:11 +0000 | [diff] [blame] | 139 | status = pj_turn_listener_create_udp(srv, pj_AF_INET(), NULL, |
| 140 | PJ_STUN_PORT, 1, 0, &listener); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 141 | 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 Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 149 | |
Benny Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame^] | 150 | console_main(srv); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 151 | |
| 152 | pj_turn_srv_destroy(srv); |
Benny Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame^] | 153 | pj_caching_pool_destroy(&g_cp); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 154 | pj_shutdown(); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |