blob: 5f9d7d4c69ab7a88ebde71741855cee45e9f1511 [file] [log] [blame]
Benny Prijono91169ac2007-02-22 02:09:23 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2005 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 */
19#include <pjlib-util.h>
20#include <pjlib.h>
Benny Prijonod0a35852007-02-23 01:07:54 +000021#include "stun_session.h"
22
23#include <conio.h>
Benny Prijono91169ac2007-02-22 02:09:23 +000024
25
26#define THIS_FILE "client_main.c"
Benny Prijonod0a35852007-02-23 01:07:54 +000027
28
29static my_perror(const char *title, pj_status_t status)
30{
31 char errmsg[PJ_ERR_MSG_SIZE];
32 pj_strerror(status, errmsg, sizeof(errmsg));
33
34 PJ_LOG(3,(THIS_FILE, "%s: %s", title, errmsg));
35}
36
37static pj_status_t on_send_msg(pj_stun_tx_data *tdata,
38 const void *pkt,
39 pj_size_t pkt_size,
40 unsigned addr_len,
41 const pj_sockaddr_t *dst_addr)
42{
43 pj_sock_t sock;
44 pj_ssize_t len;
45 pj_status_t status;
46
47 sock = (pj_sock_t) pj_stun_session_get_user_data(tdata->sess);
48
49 len = pkt_size;
50 status = pj_sock_sendto(sock, pkt, &len, 0, dst_addr, addr_len);
51
52 if (status != PJ_SUCCESS)
53 my_perror("Error sending packet", status);
54
55 return status;
56}
57
58static void on_bind_response(pj_stun_session *sess,
59 pj_status_t status,
60 pj_stun_tx_data *request,
61 const pj_stun_msg *response)
62{
63 my_perror("on_bind_response()", status);
64}
65
66int main()
67{
68 pj_stun_endpoint *endpt = NULL;
69 pj_pool_t *pool = NULL;
70 pj_caching_pool cp;
71 pj_timer_heap_t *th = NULL;
72 pj_stun_session *sess;
73 pj_sock_t sock = PJ_INVALID_SOCKET;
74 pj_sockaddr_in addr;
75 pj_stun_session_cb stun_cb;
76 pj_stun_tx_data *tdata;
77 pj_str_t s;
78 pj_status_t status;
79
80 status = pj_init();
81 status = pjlib_util_init();
82
83 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
84
85 pool = pj_pool_create(&cp.factory, NULL, 1000, 1000, NULL);
86
87 status = pj_timer_heap_create(pool, 1000, &th);
88 pj_assert(status == PJ_SUCCESS);
89
90 status = pj_stun_endpoint_create(&cp.factory, 0, NULL, th, &endpt);
91 pj_assert(status == PJ_SUCCESS);
92
93 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
94 pj_assert(status == PJ_SUCCESS);
95
96 status = pj_sockaddr_in_init(&addr, pj_cstr(&s, "127.0.0.1"), PJ_STUN_PORT);
97 pj_assert(status == PJ_SUCCESS);
98
99 pj_memset(&stun_cb, 0, sizeof(stun_cb));
100 stun_cb.on_send_msg = &on_send_msg;
101 stun_cb.on_bind_response = &on_bind_response;
102
103 status = pj_stun_session_create(endpt, NULL, &stun_cb, &sess);
104 pj_assert(status == PJ_SUCCESS);
105
106 pj_stun_session_set_user_data(sess, (void*)sock);
107
108 status = pj_stun_session_create_bind_req(sess, &tdata);
109 pj_assert(status == PJ_SUCCESS);
110
111 status = pj_stun_session_send_msg(sess, 0, sizeof(addr), &addr, tdata);
112 pj_assert(status == PJ_SUCCESS);
113
114 while (1) {
115 pj_fd_set_t rset;
116 int n;
117 pj_time_val timeout;
118
119 if (kbhit()) {
120 if (_getch()==27)
121 break;
122 }
123
124 PJ_FD_ZERO(&rset);
125 PJ_FD_SET(sock, &rset);
126
127 timeout.sec = 0; timeout.msec = 100;
128
129 n = pj_sock_select(FD_SETSIZE, &rset, NULL, NULL, &timeout);
130
131 if (PJ_FD_ISSET(sock, &rset)) {
132 char pkt[512];
133 pj_ssize_t len;
134
135 len = sizeof(pkt);
136 status = pj_sock_recv(sock, pkt, &len, 0);
137 if (status == PJ_SUCCESS) {
138 pj_stun_session_on_rx_pkt(sess, pkt, len, NULL);
139 }
140 }
141
142 pj_timer_heap_poll(th, NULL);
143 }
144
145on_return:
146 if (sock != PJ_INVALID_SOCKET)
147 pj_sock_close(sock);
148 if (endpt)
149 pj_stun_endpoint_destroy(endpt);
150 if (th)
151 pj_timer_heap_destroy(th);
152 if (pool)
153 pj_pool_release(pool);
154 pj_caching_pool_destroy(&cp);
155
156 return 0;
157}
158
Benny Prijono91169ac2007-02-22 02:09:23 +0000159