blob: 198e0313bd04382777410027cc1c0d69eb5cd342 [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
85 pj_memcpy(&usage->cb, cb, sizeof(*cb));
86
87 usage->type = type;
88 status = pj_sock_socket(family, type, protocol, &usage->sock);
89 if (status != PJ_SUCCESS)
90 goto on_error;
91
92 status = pj_sock_bind(usage->sock, local_addr, addr_len);
93 if (status != PJ_SUCCESS)
94 goto on_error;
95
96 pj_bzero(&ioqueue_cb, sizeof(ioqueue_cb));
97 ioqueue_cb.on_read_complete = &on_read_complete;
98 status = pj_ioqueue_register_sock(usage->pool, si->ioqueue, usage->sock,
99 usage, &ioqueue_cb, &usage->key);
100 if (status != PJ_SUCCESS)
101 goto on_error;
102
103 usage->worker_cnt = si->thread_cnt;
104 usage->worker = pj_pool_calloc(pool, si->thread_cnt,
105 sizeof(struct worker));
106 for (i=0; i<si->thread_cnt; ++i) {
107 pj_ioqueue_op_key_init(&usage->worker[i].read_key,
108 sizeof(usage->worker[i].read_key));
109 usage->worker[i].index = i;
110 }
111
112 usage->send_count = usage->worker_cnt * 2;
113 usage->send_key = pj_pool_calloc(pool, usage->send_count,
114 sizeof(pj_ioqueue_op_key_t));
115 for (i=0; i<usage->send_count; ++i) {
116 pj_ioqueue_op_key_init(&usage->send_key[i],
117 sizeof(usage->send_key[i]));
118 }
119
120 for (i=0; i<si->thread_cnt; ++i) {
121 pj_ssize_t size;
122
123 size = sizeof(usage->worker[i].readbuf);
124 usage->worker[i].src_addr_len = sizeof(usage->worker[i].src_addr);
125 status = pj_ioqueue_recvfrom(usage->key, &usage->worker[i].read_key,
126 usage->worker[i].readbuf, &size,
127 PJ_IOQUEUE_ALWAYS_ASYNC,
128 &usage->worker[i].src_addr,
129 &usage->worker[i].src_addr_len);
130 if (status != PJ_EPENDING)
131 goto on_error;
132 }
133
Benny Prijonoe6315582007-03-08 18:58:04 +0000134 pj_stun_server_register_usage(srv, usage);
135
Benny Prijono7cd16222007-03-05 00:58:24 +0000136 *p_usage = usage;
137 return PJ_SUCCESS;
138
139on_error:
140 pj_stun_usage_destroy(usage);
141 return status;
142}
143
144
145/**
146 * Destroy usage.
147 */
148PJ_DEF(pj_status_t) pj_stun_usage_destroy(pj_stun_usage *usage)
149{
Benny Prijonoe6315582007-03-08 18:58:04 +0000150 pj_stun_server_unregister_usage(usage->srv, usage);
151 if (usage->cb.on_destroy)
152 (*usage->cb.on_destroy)(usage);
153
Benny Prijono7cd16222007-03-05 00:58:24 +0000154 if (usage->key) {
155 pj_ioqueue_unregister(usage->key);
156 usage->key = NULL;
157 usage->sock = PJ_INVALID_SOCKET;
158 } else if (usage->sock != 0 && usage->sock != PJ_INVALID_SOCKET) {
159 pj_sock_close(usage->sock);
160 usage->sock = PJ_INVALID_SOCKET;
161 }
162
163 if (usage->mutex) {
164 pj_mutex_destroy(usage->mutex);
165 usage->mutex = NULL;
166 }
167
168 if (usage->pool) {
169 pj_pool_t *pool = usage->pool;
170 usage->pool = NULL;
171 pj_pool_release(pool);
172 }
173
174 return PJ_SUCCESS;
175}
176
177
178/**
179 * Set user data.
180 */
181PJ_DEF(pj_status_t) pj_stun_usage_set_user_data( pj_stun_usage *usage,
182 void *user_data)
183{
184 usage->user_data = user_data;
185 return PJ_SUCCESS;
186}
187
188/**
189 * Get user data.
190 */
191PJ_DEF(void*) pj_stun_usage_get_user_data(pj_stun_usage *usage)
192{
193 return usage->user_data;
194}
195
196
197/**
198 * Send with the usage.
199 */
200PJ_DEF(pj_status_t) pj_stun_usage_sendto( pj_stun_usage *usage,
201 const void *pkt,
202 pj_size_t pkt_size,
203 unsigned flags,
204 const pj_sockaddr_t *dst_addr,
205 unsigned addr_len)
206{
207 pj_ssize_t size = pkt_size;
208 unsigned i, count = usage->send_count, index;
209
210 pj_mutex_lock(usage->mutex);
211 for (i=0, ++usage->send_index; i<count; ++i, ++usage->send_index) {
212 if (usage->send_index >= usage->send_count)
213 usage->send_index = 0;
214
215 if (pj_ioqueue_is_pending(usage->key, &usage->send_key[usage->send_index])==0) {
216 break;
217 }
218 }
219
220 if (i==count) {
221 pj_mutex_unlock(usage->mutex);
222 return PJ_EBUSY;
223 }
224
225 index = usage->send_index;
226 pj_mutex_unlock(usage->mutex);
227
228 return pj_ioqueue_sendto(usage->key, &usage->send_key[index],
229 pkt, &size, flags,
230 dst_addr, addr_len);
231}
232
233
234static void on_read_complete(pj_ioqueue_key_t *key,
235 pj_ioqueue_op_key_t *op_key,
236 pj_ssize_t bytes_read)
237{
238 enum { MAX_LOOP = 10 };
239 pj_stun_usage *usage = pj_ioqueue_get_user_data(key);
240 struct worker *worker = (struct worker*) op_key;
241 unsigned count;
242 pj_status_t status;
243
244 for (count=0; !usage->quitting; ++count) {
245 unsigned flags;
246
247 if (bytes_read > 0) {
248 (*usage->cb.on_rx_data)(usage, worker->readbuf, bytes_read,
249 &worker->src_addr, worker->src_addr_len);
250 } else if (bytes_read < 0) {
251 pj_stun_perror(usage->pool->obj_name, "recv() error", -bytes_read);
252 }
253
254 if (usage->quitting)
255 break;
256
257 bytes_read = sizeof(worker->readbuf);
258 flags = (count >= MAX_LOOP) ? PJ_IOQUEUE_ALWAYS_ASYNC : 0;
259 worker->src_addr_len = sizeof(worker->src_addr);
260 status = pj_ioqueue_recvfrom(usage->key, &worker->read_key,
261 worker->readbuf, &bytes_read, flags,
262 &worker->src_addr, &worker->src_addr_len);
263 if (status == PJ_EPENDING)
264 break;
265 }
266}
267