blob: a8a5c274d1f764b1fb83732419f08dcf23fee6c7 [file] [log] [blame]
Benny Prijono7cd16222007-03-05 00:58:24 +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 "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;
102 usage->worker = pj_pool_calloc(pool, si->thread_cnt,
103 sizeof(struct worker));
104 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;
111 usage->send_key = pj_pool_calloc(pool, usage->send_count,
112 sizeof(pj_ioqueue_op_key_t));
113 for (i=0; i<usage->send_count; ++i) {
114 pj_ioqueue_op_key_init(&usage->send_key[i],
115 sizeof(usage->send_key[i]));
116 }
117
118 for (i=0; i<si->thread_cnt; ++i) {
119 pj_ssize_t size;
120
121 size = sizeof(usage->worker[i].readbuf);
122 usage->worker[i].src_addr_len = sizeof(usage->worker[i].src_addr);
123 status = pj_ioqueue_recvfrom(usage->key, &usage->worker[i].read_key,
124 usage->worker[i].readbuf, &size,
125 PJ_IOQUEUE_ALWAYS_ASYNC,
126 &usage->worker[i].src_addr,
127 &usage->worker[i].src_addr_len);
128 if (status != PJ_EPENDING)
129 goto on_error;
130 }
131
Benny Prijonoe6315582007-03-08 18:58:04 +0000132 pj_stun_server_register_usage(srv, usage);
133
Benny Prijonod5971742007-03-10 23:15:36 +0000134 /* Only after everything has been initialized we copy the callback,
135 * to prevent callback from being called when we encounter error
136 * during initialiation (decendant would not expect this).
137 */
138 pj_memcpy(&usage->cb, cb, sizeof(*cb));
139
Benny Prijono7cd16222007-03-05 00:58:24 +0000140 *p_usage = usage;
141 return PJ_SUCCESS;
142
143on_error:
144 pj_stun_usage_destroy(usage);
145 return status;
146}
147
148
149/**
150 * Destroy usage.
151 */
152PJ_DEF(pj_status_t) pj_stun_usage_destroy(pj_stun_usage *usage)
153{
Benny Prijonoe6315582007-03-08 18:58:04 +0000154 pj_stun_server_unregister_usage(usage->srv, usage);
155 if (usage->cb.on_destroy)
156 (*usage->cb.on_destroy)(usage);
157
Benny Prijono7cd16222007-03-05 00:58:24 +0000158 if (usage->key) {
159 pj_ioqueue_unregister(usage->key);
160 usage->key = NULL;
161 usage->sock = PJ_INVALID_SOCKET;
162 } else if (usage->sock != 0 && usage->sock != PJ_INVALID_SOCKET) {
163 pj_sock_close(usage->sock);
164 usage->sock = PJ_INVALID_SOCKET;
165 }
166
167 if (usage->mutex) {
168 pj_mutex_destroy(usage->mutex);
169 usage->mutex = NULL;
170 }
171
172 if (usage->pool) {
173 pj_pool_t *pool = usage->pool;
174 usage->pool = NULL;
175 pj_pool_release(pool);
176 }
177
178 return PJ_SUCCESS;
179}
180
181
182/**
183 * Set user data.
184 */
185PJ_DEF(pj_status_t) pj_stun_usage_set_user_data( pj_stun_usage *usage,
186 void *user_data)
187{
188 usage->user_data = user_data;
189 return PJ_SUCCESS;
190}
191
192/**
193 * Get user data.
194 */
195PJ_DEF(void*) pj_stun_usage_get_user_data(pj_stun_usage *usage)
196{
197 return usage->user_data;
198}
199
200
201/**
202 * Send with the usage.
203 */
204PJ_DEF(pj_status_t) pj_stun_usage_sendto( pj_stun_usage *usage,
205 const void *pkt,
206 pj_size_t pkt_size,
207 unsigned flags,
208 const pj_sockaddr_t *dst_addr,
209 unsigned addr_len)
210{
211 pj_ssize_t size = pkt_size;
212 unsigned i, count = usage->send_count, index;
213
214 pj_mutex_lock(usage->mutex);
215 for (i=0, ++usage->send_index; i<count; ++i, ++usage->send_index) {
216 if (usage->send_index >= usage->send_count)
217 usage->send_index = 0;
218
219 if (pj_ioqueue_is_pending(usage->key, &usage->send_key[usage->send_index])==0) {
220 break;
221 }
222 }
223
224 if (i==count) {
225 pj_mutex_unlock(usage->mutex);
226 return PJ_EBUSY;
227 }
228
229 index = usage->send_index;
230 pj_mutex_unlock(usage->mutex);
231
232 return pj_ioqueue_sendto(usage->key, &usage->send_key[index],
233 pkt, &size, flags,
234 dst_addr, addr_len);
235}
236
237
238static void on_read_complete(pj_ioqueue_key_t *key,
239 pj_ioqueue_op_key_t *op_key,
240 pj_ssize_t bytes_read)
241{
242 enum { MAX_LOOP = 10 };
243 pj_stun_usage *usage = pj_ioqueue_get_user_data(key);
244 struct worker *worker = (struct worker*) op_key;
245 unsigned count;
246 pj_status_t status;
247
248 for (count=0; !usage->quitting; ++count) {
249 unsigned flags;
250
251 if (bytes_read > 0) {
252 (*usage->cb.on_rx_data)(usage, worker->readbuf, bytes_read,
253 &worker->src_addr, worker->src_addr_len);
254 } else if (bytes_read < 0) {
255 pj_stun_perror(usage->pool->obj_name, "recv() error", -bytes_read);
256 }
257
258 if (usage->quitting)
259 break;
260
261 bytes_read = sizeof(worker->readbuf);
262 flags = (count >= MAX_LOOP) ? PJ_IOQUEUE_ALWAYS_ASYNC : 0;
263 worker->src_addr_len = sizeof(worker->src_addr);
264 status = pj_ioqueue_recvfrom(usage->key, &worker->read_key,
265 worker->readbuf, &bytes_read, flags,
266 &worker->src_addr, &worker->src_addr_len);
267 if (status == PJ_EPENDING)
268 break;
269 }
270}
271