blob: d3e455ad954505957238b9addd54059969c0ec6c [file] [log] [blame]
Benny Prijono7cd16222007-03-05 00:58:24 +00001/* $Id$ */
2/*
Benny Prijonof1428f32007-06-19 09:04:58 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono7cd16222007-03-05 00:58:24 +00004 *
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 "server.h"
20
21struct worker
22{
23 pj_ioqueue_op_key_t read_key;
24 unsigned index;
25 pj_uint8_t readbuf[4000];
26 pj_sockaddr src_addr;
27 int src_addr_len;
28};
29
30struct pj_stun_usage
31{
32 pj_pool_t *pool;
Benny Prijonoe6315582007-03-08 18:58:04 +000033 pj_stun_server *srv;
Benny Prijono7cd16222007-03-05 00:58:24 +000034 pj_mutex_t *mutex;
35 pj_stun_usage_cb cb;
36 int type;
37 pj_sock_t sock;
38 pj_ioqueue_key_t *key;
39 unsigned worker_cnt;
40 struct worker *worker;
41
42 pj_ioqueue_op_key_t *send_key;
43 unsigned send_count, send_index;
44
45 pj_bool_t quitting;
46 void *user_data;
47};
48
49
50static void on_read_complete(pj_ioqueue_key_t *key,
51 pj_ioqueue_op_key_t *op_key,
52 pj_ssize_t bytes_read);
53
54/*
55 * Create STUN usage.
56 */
57PJ_DEF(pj_status_t) pj_stun_usage_create( pj_stun_server *srv,
58 const char *name,
59 const pj_stun_usage_cb *cb,
60 int family,
61 int type,
62 int protocol,
63 const pj_sockaddr_t *local_addr,
64 int addr_len,
65 pj_stun_usage **p_usage)
66{
67 pj_stun_server_info *si;
68 pj_pool_t *pool;
69 pj_stun_usage *usage;
70 pj_ioqueue_callback ioqueue_cb;
71 unsigned i;
72 pj_status_t status;
73
74 si = pj_stun_server_get_info(srv);
75
76 pool = pj_pool_create(si->pf, name, 4000, 4000, NULL);
77 usage = PJ_POOL_ZALLOC_T(pool, pj_stun_usage);
78 usage->pool = pool;
Benny Prijonoe6315582007-03-08 18:58:04 +000079 usage->srv = srv;
Benny Prijono7cd16222007-03-05 00:58:24 +000080
81 status = pj_mutex_create_simple(pool, name, &usage->mutex);
82 if (status != PJ_SUCCESS)
83 goto on_error;
84
Benny Prijono7cd16222007-03-05 00:58:24 +000085 usage->type = type;
86 status = pj_sock_socket(family, type, protocol, &usage->sock);
87 if (status != PJ_SUCCESS)
88 goto on_error;
89
90 status = pj_sock_bind(usage->sock, local_addr, addr_len);
91 if (status != PJ_SUCCESS)
92 goto on_error;
93
94 pj_bzero(&ioqueue_cb, sizeof(ioqueue_cb));
95 ioqueue_cb.on_read_complete = &on_read_complete;
96 status = pj_ioqueue_register_sock(usage->pool, si->ioqueue, usage->sock,
97 usage, &ioqueue_cb, &usage->key);
98 if (status != PJ_SUCCESS)
99 goto on_error;
100
101 usage->worker_cnt = si->thread_cnt;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000102 usage->worker = (struct worker*) pj_pool_calloc(pool, si->thread_cnt,
103 sizeof(struct worker));
Benny Prijono7cd16222007-03-05 00:58:24 +0000104 for (i=0; i<si->thread_cnt; ++i) {
105 pj_ioqueue_op_key_init(&usage->worker[i].read_key,
106 sizeof(usage->worker[i].read_key));
107 usage->worker[i].index = i;
108 }
109
110 usage->send_count = usage->worker_cnt * 2;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000111 usage->send_key = (pj_ioqueue_op_key_t*)
112 pj_pool_calloc(pool, usage->send_count,
Benny Prijono7cd16222007-03-05 00:58:24 +0000113 sizeof(pj_ioqueue_op_key_t));
114 for (i=0; i<usage->send_count; ++i) {
115 pj_ioqueue_op_key_init(&usage->send_key[i],
116 sizeof(usage->send_key[i]));
117 }
118
119 for (i=0; i<si->thread_cnt; ++i) {
120 pj_ssize_t size;
121
122 size = sizeof(usage->worker[i].readbuf);
123 usage->worker[i].src_addr_len = sizeof(usage->worker[i].src_addr);
124 status = pj_ioqueue_recvfrom(usage->key, &usage->worker[i].read_key,
125 usage->worker[i].readbuf, &size,
126 PJ_IOQUEUE_ALWAYS_ASYNC,
127 &usage->worker[i].src_addr,
128 &usage->worker[i].src_addr_len);
129 if (status != PJ_EPENDING)
130 goto on_error;
131 }
132
Benny Prijonoe6315582007-03-08 18:58:04 +0000133 pj_stun_server_register_usage(srv, usage);
134
Benny Prijonod5971742007-03-10 23:15:36 +0000135 /* Only after everything has been initialized we copy the callback,
136 * to prevent callback from being called when we encounter error
137 * during initialiation (decendant would not expect this).
138 */
139 pj_memcpy(&usage->cb, cb, sizeof(*cb));
140
Benny Prijono7cd16222007-03-05 00:58:24 +0000141 *p_usage = usage;
142 return PJ_SUCCESS;
143
144on_error:
145 pj_stun_usage_destroy(usage);
146 return status;
147}
148
149
150/**
151 * Destroy usage.
152 */
153PJ_DEF(pj_status_t) pj_stun_usage_destroy(pj_stun_usage *usage)
154{
Benny Prijonoe6315582007-03-08 18:58:04 +0000155 pj_stun_server_unregister_usage(usage->srv, usage);
156 if (usage->cb.on_destroy)
157 (*usage->cb.on_destroy)(usage);
158
Benny Prijono7cd16222007-03-05 00:58:24 +0000159 if (usage->key) {
160 pj_ioqueue_unregister(usage->key);
161 usage->key = NULL;
162 usage->sock = PJ_INVALID_SOCKET;
163 } else if (usage->sock != 0 && usage->sock != PJ_INVALID_SOCKET) {
164 pj_sock_close(usage->sock);
165 usage->sock = PJ_INVALID_SOCKET;
166 }
167
168 if (usage->mutex) {
169 pj_mutex_destroy(usage->mutex);
170 usage->mutex = NULL;
171 }
172
173 if (usage->pool) {
174 pj_pool_t *pool = usage->pool;
175 usage->pool = NULL;
176 pj_pool_release(pool);
177 }
178
179 return PJ_SUCCESS;
180}
181
182
183/**
184 * Set user data.
185 */
186PJ_DEF(pj_status_t) pj_stun_usage_set_user_data( pj_stun_usage *usage,
187 void *user_data)
188{
189 usage->user_data = user_data;
190 return PJ_SUCCESS;
191}
192
193/**
194 * Get user data.
195 */
196PJ_DEF(void*) pj_stun_usage_get_user_data(pj_stun_usage *usage)
197{
198 return usage->user_data;
199}
200
201
202/**
203 * Send with the usage.
204 */
205PJ_DEF(pj_status_t) pj_stun_usage_sendto( pj_stun_usage *usage,
206 const void *pkt,
207 pj_size_t pkt_size,
208 unsigned flags,
209 const pj_sockaddr_t *dst_addr,
210 unsigned addr_len)
211{
212 pj_ssize_t size = pkt_size;
213 unsigned i, count = usage->send_count, index;
214
215 pj_mutex_lock(usage->mutex);
216 for (i=0, ++usage->send_index; i<count; ++i, ++usage->send_index) {
217 if (usage->send_index >= usage->send_count)
218 usage->send_index = 0;
219
220 if (pj_ioqueue_is_pending(usage->key, &usage->send_key[usage->send_index])==0) {
221 break;
222 }
223 }
224
225 if (i==count) {
226 pj_mutex_unlock(usage->mutex);
227 return PJ_EBUSY;
228 }
229
230 index = usage->send_index;
231 pj_mutex_unlock(usage->mutex);
232
233 return pj_ioqueue_sendto(usage->key, &usage->send_key[index],
234 pkt, &size, flags,
235 dst_addr, addr_len);
236}
237
238
239static void on_read_complete(pj_ioqueue_key_t *key,
240 pj_ioqueue_op_key_t *op_key,
241 pj_ssize_t bytes_read)
242{
243 enum { MAX_LOOP = 10 };
Benny Prijonoa1e69682007-05-11 15:14:34 +0000244 pj_stun_usage *usage = (pj_stun_usage*) pj_ioqueue_get_user_data(key);
Benny Prijono7cd16222007-03-05 00:58:24 +0000245 struct worker *worker = (struct worker*) op_key;
246 unsigned count;
247 pj_status_t status;
248
249 for (count=0; !usage->quitting; ++count) {
250 unsigned flags;
251
252 if (bytes_read > 0) {
253 (*usage->cb.on_rx_data)(usage, worker->readbuf, bytes_read,
254 &worker->src_addr, worker->src_addr_len);
255 } else if (bytes_read < 0) {
256 pj_stun_perror(usage->pool->obj_name, "recv() error", -bytes_read);
257 }
258
259 if (usage->quitting)
260 break;
261
262 bytes_read = sizeof(worker->readbuf);
263 flags = (count >= MAX_LOOP) ? PJ_IOQUEUE_ALWAYS_ASYNC : 0;
264 worker->src_addr_len = sizeof(worker->src_addr);
265 status = pj_ioqueue_recvfrom(usage->key, &worker->read_key,
266 worker->readbuf, &bytes_read, flags,
267 &worker->src_addr, &worker->src_addr_len);
268 if (status == PJ_EPENDING)
269 break;
270 }
271}
272