From 7b80afbe61b63fcc3d539856e4da005b614d835a Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Thu, 13 Mar 2008 15:11:29 +0000 Subject: More ticket #485: added TURN client application git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1867 74dad513-b988-da41-8d7b-12977e46ad98 --- pjnath/src/pjturn-client/client_main.c | 546 +++++++++++++++++++++++++++++++++ 1 file changed, 546 insertions(+) create mode 100644 pjnath/src/pjturn-client/client_main.c (limited to 'pjnath/src/pjturn-client') diff --git a/pjnath/src/pjturn-client/client_main.c b/pjnath/src/pjturn-client/client_main.c new file mode 100644 index 00000000..a9123319 --- /dev/null +++ b/pjnath/src/pjturn-client/client_main.c @@ -0,0 +1,546 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2007 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include +#include + + +#define THIS_FILE "client_main.c" +#define LOCAL_PORT 1998 +#define BANDWIDTH 64 /* -1 to disable */ +#define LIFETIME 600 /* -1 to disable */ +#define REQ_TRANSPORT -1 /* 0: udp, 1: tcp, -1: disable */ +#define REQ_PORT_PROPS -1 /* -1 to disable */ +#define REQ_IP 0 /* IP address string */ + +//#define OPTIONS PJ_STUN_NO_AUTHENTICATE +#define OPTIONS 0 + + +struct peer +{ + pj_sock_t sock; + pj_sockaddr addr; +}; + + +static struct global +{ + pj_caching_pool cp; + pj_pool_t *pool; + pj_stun_config stun_config; + pj_thread_t *thread; + pj_bool_t quit; + + pj_turn_udp *udp_rel; + pj_sockaddr relay_addr; + + struct peer peer[2]; +} g; + +static struct options +{ + char *srv_addr; + char *srv_port; + char *realm; + char *user_name; + char *password; + char *nonce; + pj_bool_t use_fingerprint; +} o; + + +static int worker_thread(void *unused); +static pj_status_t parse_addr(const char *input, pj_sockaddr_in *addr); +static void turn_on_rx_data(pj_turn_udp *udp_rel, + const pj_uint8_t *pkt, + unsigned pkt_len, + const pj_sockaddr_t *peer_addr, + unsigned addr_len); +static void turn_on_state(pj_turn_udp *udp_rel, pj_turn_state_t old_state, + pj_turn_state_t new_state); + + +static void my_perror(const char *title, pj_status_t status) +{ + char errmsg[PJ_ERR_MSG_SIZE]; + pj_strerror(status, errmsg, sizeof(errmsg)); + + PJ_LOG(3,(THIS_FILE, "%s: %s", title, errmsg)); +} + +#define CHECK(expr) status=expr; \ + if (status!=PJ_SUCCESS) { \ + my_perror(#expr, status); \ + return status; \ + } + +static int init() +{ + int i; + pj_status_t status; + + CHECK( pj_init() ); + CHECK( pjlib_util_init() ); + CHECK( pjnath_init() ); + + /* Check that server is specified */ + if (!o.srv_addr) { + printf("Error: server must be specified\n"); + return PJ_EINVAL; + } + + pj_caching_pool_init(&g.cp, &pj_pool_factory_default_policy, 0); + + g.pool = pj_pool_create(&g.cp.factory, "main", 1000, 1000, NULL); + + /* Init global STUN config */ + pj_stun_config_init(&g.stun_config, &g.cp.factory, 0, NULL, NULL); + + /* Create global timer heap */ + CHECK( pj_timer_heap_create(g.pool, 1000, &g.stun_config.timer_heap) ); + + /* Create global ioqueue */ + CHECK( pj_ioqueue_create(g.pool, 16, &g.stun_config.ioqueue) ); + + /* + * Create peers + */ + for (i=0; i>> "); + fflush(stdout); +} + + +static void console_main(void) +{ + while (!g.quit) { + char input[32]; + struct peer *peer; + pj_ssize_t len; + pj_status_t status; + + menu(); + + fgets(input, sizeof(input), stdin); + + switch (input[0]) { + case 'A': + create_relay(); + break; + case 'S': + if (g.udp_rel == NULL) { + puts("Error: no relay"); + continue; + } + if (input[1] != '0' && input[1] != '1') { + puts("Usage: S0 or S1"); + continue; + } + peer = &g.peer[input[1]-'0']; + strcpy(input, "Hello from client"); + status = pj_turn_udp_sendto(g.udp_rel, input, strlen(input)+1, + &peer->addr, + pj_sockaddr_get_len(&peer->addr)); + if (status != PJ_SUCCESS) + my_perror("turn_udp_sendto() failed", status); + break; + case 'B': + if (g.udp_rel == NULL) { + puts("Error: no relay"); + continue; + } + if (input[1] != '0' && input[1] != '1') { + puts("Usage: B0 or B1"); + continue; + } + peer = &g.peer[input[1]-'0']; + status = pj_turn_udp_bind_channel(g.udp_rel, &peer->addr, + pj_sockaddr_get_len(&peer->addr)); + if (status != PJ_SUCCESS) + my_perror("turn_udp_bind_channel() failed", status); + break; + case 'X': + if (g.udp_rel == NULL) { + puts("Error: no relay"); + continue; + } + destroy_relay(); + break; + case '0': + case '1': + peer = &g.peer[input[1]-'0']; + sprintf(input, "Hello from peer%d", input[0]-'0'); + len = strlen(input)+1; + pj_sock_sendto(peer->sock, input, &len, 0, &g.relay_addr, + pj_sockaddr_get_len(&g.relay_addr)); + break; + case 'q': + g.quit = PJ_TRUE; + break; + } + } +} + + +static void usage(void) +{ + puts("Usage: pjturn_client TARGET [OPTIONS]"); + puts(""); + puts("where TARGET is \"host[:port]\""); + puts(""); + puts("and OPTIONS:"); + puts(" --realm, -r Set realm of the credential"); + puts(" --username, -u Set username of the credential"); + puts(" --password, -p Set password of the credential"); + puts(" --nonce, -N Set NONCE"); + puts(" --fingerprint, -F Use fingerprint for outgoing requests"); + puts(" --help, -h"); +} + +int main(int argc, char *argv[]) +{ + struct pj_getopt_option long_options[] = { + { "realm", 1, 0, 'r'}, + { "username", 1, 0, 'u'}, + { "password", 1, 0, 'p'}, + { "nonce", 1, 0, 'N'}, + { "fingerprint",0, 0, 'F'}, + { "data", 1, 0, 'D'}, + { "help", 0, 0, 'h'} + }; + int c, opt_id; + char *pos; + pj_status_t status; + + while((c=pj_getopt_long(argc,argv, "r:u:p:N:hF", long_options, &opt_id))!=-1) { + switch (c) { + case 'r': + o.realm = pj_optarg; + break; + case 'u': + o.user_name = pj_optarg; + break; + case 'p': + o.password = pj_optarg; + break; + case 'N': + o.nonce = pj_optarg; + break; + case 'h': + usage(); + return 0; + case 'F': + o.use_fingerprint = PJ_TRUE; + break; + + default: + printf("Argument \"%s\" is not valid. Use -h to see help", + argv[pj_optind]); + return 1; + } + } + + if (pj_optind == argc) { + puts("Error: TARGET is needed"); + return 1; + } + + if ((pos=pj_ansi_strchr(argv[pj_optind], ':')) != NULL) { + o.srv_addr = argv[pj_optind]; + *pos = '\0'; + o.srv_port = pos+1; + } else { + o.srv_addr = argv[pj_optind]; + } + + if ((status=init()) != 0) + goto on_return; + + if ((status=create_relay()) != 0) + goto on_return; + + console_main(); + +on_return: + shutdown(); + return status ? 1 : 0; +} + -- cgit v1.2.3