7358c580effe8985441e485e87eb6a0a1b996812
[strongswan.git] / src / libstrongswan / networking / streams / stream_service.c
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <library.h>
17 #include <threading/thread.h>
18 #include <threading/mutex.h>
19 #include <threading/condvar.h>
20 #include <processing/jobs/callback_job.h>
21
22 #include "stream_service.h"
23
24 #include <errno.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27
28 typedef struct private_stream_service_t private_stream_service_t;
29
30 /**
31 * Private data of an stream_service_t object.
32 */
33 struct private_stream_service_t {
34
35 /**
36 * Public stream_service_t interface.
37 */
38 stream_service_t public;
39
40 /**
41 * Underlying socket
42 */
43 int fd;
44
45 /**
46 * Accept callback
47 */
48 stream_service_cb_t cb;
49
50 /**
51 * Accept callback data
52 */
53 void *data;
54
55 /**
56 * Job priority to invoke callback with
57 */
58 job_priority_t prio;
59
60 /**
61 * Maximum number of parallel callback invocations
62 */
63 u_int cncrncy;
64
65 /**
66 * Currently active jobs
67 */
68 u_int active;
69
70 /**
71 * mutex to lock active counter
72 */
73 mutex_t *mutex;
74
75 /**
76 * Condvar to wait for callback termination
77 */
78 condvar_t *condvar;
79 };
80
81 /**
82 * Data to pass to async accept job
83 */
84 typedef struct {
85 /** callback function */
86 stream_service_cb_t cb;
87 /** callback data */
88 void *data;
89 /** accepted connection */
90 int fd;
91 /** reference to stream service */
92 private_stream_service_t *this;
93 } async_data_t;
94
95 /**
96 * Clean up accept data
97 */
98 static void destroy_async_data(async_data_t *data)
99 {
100 private_stream_service_t *this = data->this;
101
102 this->mutex->lock(this->mutex);
103 if (this->active-- == this->cncrncy)
104 {
105 /* leaving concurrency limit, restart accept()ing. */
106 this->public.on_accept(&this->public, this->cb, this->data,
107 this->prio, this->cncrncy);
108 }
109 this->condvar->signal(this->condvar);
110 this->mutex->unlock(this->mutex);
111
112 if (data->fd != -1)
113 {
114 close(data->fd);
115 }
116 free(data);
117 }
118
119 /**
120 * Async processing of accepted connection
121 */
122 static job_requeue_t accept_async(async_data_t *data)
123 {
124 stream_t *stream;
125
126 stream = stream_create_from_fd(data->fd);
127 if (stream)
128 {
129 /* FD is now owned by stream, don't close it during cleanup */
130 data->fd = -1;
131 thread_cleanup_push((void*)stream->destroy, stream);
132 thread_cleanup_pop(!data->cb(data->data, stream));
133 }
134 return JOB_REQUEUE_NONE;
135 }
136
137 /**
138 * Watcher callback function
139 */
140 static bool watch(private_stream_service_t *this, int fd, watcher_event_t event)
141 {
142 async_data_t *data;
143 bool keep = TRUE;
144
145 INIT(data,
146 .cb = this->cb,
147 .data = this->data,
148 .fd = accept(fd, NULL, NULL),
149 .this = this,
150 );
151
152 if (data->fd != -1)
153 {
154 this->mutex->lock(this->mutex);
155 if (++this->active == this->cncrncy)
156 {
157 /* concurrency limit reached, stop accept()ing new connections */
158 keep = FALSE;
159 }
160 this->mutex->unlock(this->mutex);
161
162 lib->processor->queue_job(lib->processor,
163 (job_t*)callback_job_create_with_prio((void*)accept_async, data,
164 (void*)destroy_async_data, (callback_job_cancel_t)return_false,
165 this->prio));
166 }
167 else
168 {
169 free(data);
170 }
171 return keep;
172 }
173
174 METHOD(stream_service_t, on_accept, void,
175 private_stream_service_t *this, stream_service_cb_t cb, void *data,
176 job_priority_t prio, u_int cncrncy)
177 {
178 this->mutex->lock(this->mutex);
179
180 /* wait for all callbacks to return */
181 while (this->active)
182 {
183 this->condvar->wait(this->condvar, this->mutex);
184 }
185
186 if (this->cb)
187 {
188 lib->watcher->remove(lib->watcher, this->fd);
189 }
190
191 this->cb = cb;
192 this->data = data;
193 if (prio <= JOB_PRIO_MAX)
194 {
195 this->prio = prio;
196 }
197 this->cncrncy = cncrncy;
198
199 if (this->cb)
200 {
201 lib->watcher->add(lib->watcher, this->fd,
202 WATCHER_READ, (watcher_cb_t)watch, this);
203 }
204
205 this->mutex->unlock(this->mutex);
206 }
207
208 METHOD(stream_service_t, destroy, void,
209 private_stream_service_t *this)
210 {
211 on_accept(this, NULL, NULL, this->prio, this->cncrncy);
212 close(this->fd);
213 this->mutex->destroy(this->mutex);
214 this->condvar->destroy(this->condvar);
215 free(this);
216 }
217
218 /**
219 * See header
220 */
221 stream_service_t *stream_service_create_from_fd(int fd)
222 {
223 private_stream_service_t *this;
224
225 INIT(this,
226 .public = {
227 .on_accept = _on_accept,
228 .destroy = _destroy,
229 },
230 .fd = fd,
231 .prio = JOB_PRIO_MEDIUM,
232 .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
233 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
234 );
235
236 return &this->public;
237 }