-
Notifications
You must be signed in to change notification settings - Fork 16
/
rtpp_notify.c
373 lines (316 loc) · 10.6 KB
/
rtpp_notify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (c) 2010 Sippy Software, Inc., http://www.sippysoft.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rtpp_log.h"
#include "rtpp_network.h"
#include "rtpp_notify.h"
#include "rtpp_session.h"
#include "rtpp_util.h"
struct rtpp_notify_wi
{
char *notify_buf;
int len;
struct rtpp_timeout_handler *th;
rtpp_log_t glog;
struct rtpp_notify_wi *next;
};
static pthread_t rtpp_notify_queue;
static pthread_cond_t rtpp_notify_queue_cond;
static pthread_mutex_t rtpp_notify_queue_mutex;
static pthread_mutex_t rtpp_notify_wi_free_mutex;
static int rtpp_notify_dropped_items;
static struct rtpp_notify_wi *rtpp_notify_wi_free;
static struct rtpp_notify_wi *rtpp_notify_wi_queue, *rtpp_notify_wi_queue_tail;
static struct rtpp_notify_wi *rtpp_notify_queue_get_free_item(void);
static void rtpp_notify_queue_put_item(struct rtpp_notify_wi *);
static void do_timeout_notification(struct rtpp_notify_wi *, int);
struct rtpp_notify_wi *
rtpp_notify_queue_get_free_item(void)
{
struct rtpp_notify_wi *wi;
pthread_mutex_lock(&rtpp_notify_wi_free_mutex);
if (rtpp_notify_wi_free == NULL) {
/* no free work items, allocate one now */
wi = malloc(sizeof(*wi));
if (wi == NULL)
rtpp_notify_dropped_items++;
memset(wi, '\0', sizeof(*wi));
pthread_mutex_unlock(&rtpp_notify_wi_free_mutex);
return wi;
}
wi = rtpp_notify_wi_free;
/* move up rtpp_notify_wi_free */
rtpp_notify_wi_free = rtpp_notify_wi_free->next;
pthread_mutex_unlock(&rtpp_notify_wi_free_mutex);
return wi;
}
static void
rtpp_notify_queue_return_free_item(struct rtpp_notify_wi *wi)
{
pthread_mutex_lock(&rtpp_notify_wi_free_mutex);
wi->next = rtpp_notify_wi_free;
rtpp_notify_wi_free = wi;
pthread_mutex_unlock(&rtpp_notify_wi_free_mutex);
}
static void
rtpp_notify_queue_put_item(struct rtpp_notify_wi *wi)
{
pthread_mutex_lock(&rtpp_notify_queue_mutex);
wi->next = NULL;
if (rtpp_notify_wi_queue == NULL) {
rtpp_notify_wi_queue = wi;
rtpp_notify_wi_queue_tail = wi;
} else {
rtpp_notify_wi_queue_tail->next = wi;
rtpp_notify_wi_queue_tail = wi;
}
/* notify worker thread */
pthread_cond_signal(&rtpp_notify_queue_cond);
pthread_mutex_unlock(&rtpp_notify_queue_mutex);
}
void
rtpp_notify_queue_run(void)
{
struct rtpp_notify_wi *wi;
for (;;) {
pthread_mutex_lock(&rtpp_notify_queue_mutex);
while (rtpp_notify_wi_queue == NULL) {
pthread_cond_wait(&rtpp_notify_queue_cond, &rtpp_notify_queue_mutex);
}
wi = rtpp_notify_wi_queue;
rtpp_notify_wi_queue = wi->next;
pthread_mutex_unlock(&rtpp_notify_queue_mutex);
/* main work here */
do_timeout_notification(wi, 1);
/* put wi into rtpp_notify_wi_free' tail */
rtpp_notify_queue_return_free_item(wi);
}
}
int
parse_hostport(const char *hostport, char *host, int hsize, char *port, int psize, int testonly)
{
const char *cp;
int myport;
cp = strrchr(hostport, ':');
if (cp == NULL || cp[1] == '\0' || cp == hostport) {
/* warnx("invalid tcp/udp address");*/
return -1;
}
myport = atoi(cp + 1);
if (myport <= 0 || myport > 65535) {
/*warnx("%s: invalid port", cp + 1);*/
return -1;
}
if (testonly != 0)
return 0;
if (cp - hostport + 1 > hsize || psize < 6) {
/*warnx("supplied buffers are too small");*/
return -1;
}
memcpy(host, hostport, cp - hostport);
host[cp - hostport] = '\0';
snprintf(port, psize, "%d", myport);
return 0;
}
static int
parse_timeout_sock(rtpp_log_t glog, const char *sock_name, struct rtpp_timeout_handler *timeout_handler)
{
if (strncmp("unix:", sock_name, 5) == 0) {
sock_name += 5;
timeout_handler->socket_type = PF_LOCAL;
} else if (strncmp("tcp:", sock_name, 4) == 0) {
sock_name += 4;
if (parse_hostport(sock_name, NULL, 0, NULL, 0, 1) != 0) {
rtpp_log_ewrite(RTPP_LOG_ERR, glog, "can't parse host:port in TCP address");
return -1;
}
timeout_handler->socket_type = PF_INET;
} else {
timeout_handler->socket_type = PF_LOCAL;
}
if (strlen(sock_name) == 0) {
rtpp_log_ewrite(RTPP_LOG_ERR, glog, "timeout notification socket name too short");
return -1;
}
timeout_handler->socket_name = strdup(sock_name);
if (timeout_handler->socket_name == NULL) {
rtpp_log_ewrite(RTPP_LOG_ERR, glog, "can't allocate memory");
return -1;
}
return 0;
}
struct rtpp_timeout_handler *
rtpp_notify_init(rtpp_log_t glog, const char *socket_name)
{
struct rtpp_timeout_handler *th;
rtpp_notify_wi_free = NULL;
rtpp_notify_wi_queue = NULL;
rtpp_notify_wi_queue_tail = NULL;
rtpp_notify_dropped_items = 0;
th = malloc(sizeof(*th));
if (th == NULL)
return NULL;
memset(th, '\0', sizeof(*th));
if (parse_timeout_sock(glog, socket_name, th) != 0) {
free(th);
return NULL;
}
th->fd = -1;
th->connected = 0;
pthread_cond_init(&rtpp_notify_queue_cond, NULL);
pthread_mutex_init(&rtpp_notify_queue_mutex, NULL);
pthread_mutex_init(&rtpp_notify_wi_free_mutex, NULL);
if (pthread_create(&rtpp_notify_queue, NULL, (void *(*)(void *))&rtpp_notify_queue_run, NULL) != 0) {
free(th);
return NULL;
}
return th;
}
int
rtpp_notify_schedule(struct cfg *cf, struct rtpp_session *sp)
{
struct rtpp_notify_wi *wi;
struct rtpp_timeout_handler *th = sp->timeout_data.handler;
int len;
char *notify_buf;
if (th == NULL) {
/* Not an error, just nothing to do */
return 0;
}
wi = rtpp_notify_queue_get_free_item();
if (wi == NULL)
return -1;
wi->th = th;
if (sp->timeout_data.notify_tag == NULL) {
/* two 5-digit numbers, space, \0 and \n */
len = 5 + 5 + 3;
} else {
/* string, \0 and \n */
len = strlen(sp->timeout_data.notify_tag) + 2;
}
if (wi->notify_buf == NULL) {
wi->notify_buf = malloc(len);
if (wi->notify_buf == NULL) {
rtpp_notify_queue_return_free_item(wi);
return -1;
}
} else {
notify_buf = realloc(wi->notify_buf, len);
if (notify_buf == NULL) {
rtpp_notify_queue_return_free_item(wi);
return -1;
}
wi->notify_buf = notify_buf;
}
wi->len = len;
if (sp->timeout_data.notify_tag == NULL) {
len = snprintf(wi->notify_buf, len, "%d %d\n",
sp->ports[0], sp->ports[1]);
} else {
len = snprintf(wi->notify_buf, len, "%s\n",
sp->timeout_data.notify_tag);
}
wi->glog = cf->stable.glog;
rtpp_notify_queue_put_item(wi);
return 0;
}
static void
reconnect_timeout_handler(rtpp_log_t log, struct rtpp_timeout_handler *th)
{
union {
struct sockaddr_un u;
struct sockaddr_storage i;
} remote;
char host[512], port[10];
int remote_len, n;
assert (th->socket_name != NULL && th->connected == 0);
if (th->fd == -1) {
rtpp_log_write(RTPP_LOG_DBUG, log, "connecting timeout socket");
} else {
rtpp_log_write(RTPP_LOG_DBUG, log, "reconnecting timeout socket");
close(th->fd);
}
th->fd = socket(th->socket_type, SOCK_STREAM, 0);
if (th->fd == -1) {
rtpp_log_ewrite(RTPP_LOG_ERR, log, "can't create timeout socket");
return;
}
memset(&remote, '\0', sizeof(remote));
if (th->socket_type == PF_UNIX) {
remote.u.sun_family = AF_LOCAL;
strncpy(remote.u.sun_path, th->socket_name, sizeof(remote.u.sun_path) - 1);
#if defined(HAVE_SOCKADDR_SUN_LEN)
remote.u.sun_len = strlen(remote.u.sun_path);
#endif
remote_len = sizeof(remote.u);
} else {
assert (parse_hostport(th->socket_name, host, sizeof(host), port, sizeof(port), 0) == 0);
n = resolve(sstosa(&remote.i), AF_INET, host, port, AI_PASSIVE);
if (n != 0) {
rtpp_log_write(RTPP_LOG_ERR, log, "reconnect_timeout_handler: getaddrinfo('%s:s'): %s",
host, port, gai_strerror(n));
return;
}
remote_len = SA_LEN(sstosa(&remote.i));
}
if (connect(th->fd, (struct sockaddr *)&remote, remote_len) == -1) {
rtpp_log_ewrite(RTPP_LOG_ERR, log, "can't connect to timeout socket");
} else {
th->connected = 1;
}
}
static void
do_timeout_notification(struct rtpp_notify_wi *wi, int retries)
{
int result;
if (wi->th->connected == 0) {
reconnect_timeout_handler(wi->glog, wi->th);
/* If connect fails, no notification will be sent */
if (wi->th->connected == 0) {
rtpp_log_write(RTPP_LOG_ERR, wi->glog, "unable to send timeout notification");
return;
}
}
do {
result = send(wi->th->fd, wi->notify_buf, wi->len - 1, 0);
} while (result == -1 && errno == EINTR);
if (result < 0) {
wi->th->connected = 0;
rtpp_log_ewrite(RTPP_LOG_ERR, wi->glog, "failed to send timeout notification");
if (retries > 0)
do_timeout_notification(wi, retries - 1);
}
}