-
Notifications
You must be signed in to change notification settings - Fork 2
/
b2bua_socket.c
441 lines (401 loc) · 13.1 KB
/
b2bua_socket.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#define _WITH_DPRINTF 1
#include <sys/types.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#if defined(__FreeBSD__)
#include <pthread_np.h>
#endif
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "iksemel.h"
#include "b2bua_socket.h"
#include "ss_base64.h"
#include "ss_network.h"
#include "ss_lthread.h"
#include "ss_util.h"
struct b2bua_xchg_args {
int socket;
struct lthread_args *largs;
pthread_mutex_t status_mutex;
int status;
struct b2bua_slot *bslots;
/*
* The variables below should only be accessed from the
* worker (rx) thread.
*/
struct queue *inpacket_queue;
pthread_t tx_thread;
/*
* The variables below can be modified/accessed from the parent
* thread only (b2bua_acceptor_run).
*/
pthread_t rx_thread;
struct b2bua_xchg_args *next;
struct b2bua_xchg_args *prev;
};
#define B2BUA_XCHG_RUNS 0
#define B2BUA_XCHG_DEAD -1
#if !defined(INFTIM)
#define INFTIM (-1)
#endif
static void b2bua_xchg_tx(struct b2bua_xchg_args *);
static void b2bua_xchg_rx(struct b2bua_xchg_args *);
extern int pthread_timedjoin_np(pthread_t, void **,
const struct timespec *);
struct b2bua_slot *
b2bua_getslot(struct b2bua_slot *bslots, str *call_id)
{
struct b2bua_slot *bslot;
int nslots, slotnum;
int hash;
nslots = 0;
for (bslot = bslots; bslot != NULL; bslot = bslot->next) {
nslots += 1;
}
hash = hash_string(call_id, 2);
slotnum = hash % nslots;
for (bslot = bslots; slotnum > 0; slotnum--) {
bslot = bslot->next;
}
return bslot;
}
struct b2bua_slot *
b2bua_getslot_by_id(struct b2bua_slot *bslots, int id)
{
struct b2bua_slot *bslot;
for (bslot = bslots; bslot != NULL; bslot = bslot->next) {
if (bslot->id == id)
return bslot;
}
return NULL;
}
static int
b2bua_xchg_getstatus(struct b2bua_xchg_args *bargs)
{
int status;
pthread_mutex_lock(&bargs->status_mutex);
status = bargs->status;
pthread_mutex_unlock(&bargs->status_mutex);
return status;
}
void
b2bua_acceptor_run(struct lthread_args *args)
{
int n, s;
socklen_t ralen;
struct sockaddr_storage ia;
struct b2bua_xchg_args *bargs_head, *bargs, *bargs1;
char *cmd_listen_port;
bargs_head = NULL;
printf("b2bua_acceptor_run(%s)\n", args->cmd_listen_addr);
asprintf(&cmd_listen_port, "%d", args->cmd_listen_port);
n = resolve(sstosa(&ia), AF_INET, args->cmd_listen_addr, cmd_listen_port, AI_PASSIVE);
free(cmd_listen_port);
printf("resolve(%d)\n", n);
s = socket(AF_INET, SOCK_STREAM, 0);
printf("socket(%d)\n", s);
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
n = bind(s, sstosa(&ia), SS_LEN(&ia));
printf("bind(%d)\n", n);
n = listen(s, 32);
for (;;) {
for (bargs = bargs_head; bargs != NULL; bargs = bargs1) {
bargs1 = bargs->next;
if (b2bua_xchg_getstatus(bargs) != B2BUA_XCHG_RUNS) {
printf("collecting dead xchg thread\n");
pthread_join(bargs->rx_thread, NULL);
if (bargs->prev != NULL)
bargs->prev->next = bargs->next;
else
bargs_head = bargs->next;
if (bargs->next != NULL)
bargs->next->prev = bargs->prev;
free(bargs);
}
}
ralen = sizeof(ia);
n = accept(s, sstosa(&ia), &ralen);
if (n < 0)
continue;
printf("accept(%d)\n", n);
bargs = malloc(sizeof(*bargs));
if (bargs == NULL) {
fprintf(stderr, "b2bua_acceptor_run: no mem\n");
close(n);
continue;
}
memset(bargs, '\0', sizeof(*bargs));
bargs->largs = args;
bargs->socket = n;
bargs->status = B2BUA_XCHG_RUNS;
bargs->bslots = args->bslots;
pthread_mutex_init(&(bargs->status_mutex), NULL);
n = pthread_create(&bargs->rx_thread, NULL, (void *(*)(void *))&b2bua_xchg_rx, bargs);
if (n != 0) {
fprintf(stderr, "b2bua_acceptor_run: pthread_create() failed\n");
close(n);
free(bargs);
continue;
}
if (bargs_head != NULL)
bargs_head->prev = bargs;
bargs->next = bargs_head;
bargs->prev = NULL;
bargs_head = bargs;
}
}
static void
b2bua_xchg_setstatus(struct b2bua_xchg_args *bargs, int status)
{
pthread_mutex_lock(&bargs->status_mutex);
bargs->status = status;
pthread_mutex_unlock(&bargs->status_mutex);
}
#define XMPP_PROLOGUE "<?xml version='1.0'?>\n <stream:stream>\n"
static void
b2bua_xchg_tx(struct b2bua_xchg_args *bargs)
{
int i, buflen;
struct wi *wi;
char b64_databuf[11 * 1024];
char *outbuf;
send(bargs->socket, XMPP_PROLOGUE, sizeof(XMPP_PROLOGUE) - 1, MSG_NOSIGNAL);
for (;;) {
wi = queue_get_item(bargs->inpacket_queue, 1);
if (wi == NULL) {
if (b2bua_xchg_getstatus(bargs) != B2BUA_XCHG_RUNS) {
return;
}
continue;
}
#ifdef DEBUG
printf("incoming size=%d, from=%s:%d, to=%s\n", INP(wi).rsize,
INP(wi).remote_addr, INP(wi).remote_port, INP(wi).local_addr);
#endif
i = ss_b64_ntop(INP(wi).databuf, INP(wi).rsize, b64_databuf, sizeof(b64_databuf));
buflen = asprintf(&outbuf, " <incoming_packet\n" \
" src_addr=\"%s\"\n" \
" src_port=\"%d\"\n" \
" dst_addr=\"%s\"\n" \
" dst_port=\"%d\"\n" \
" rtime=\"%f\"\n" \
" msg=\"%.*s\"\n" \
" />\n", INP(wi).remote_addr, INP(wi).remote_port, INP(wi).local_addr, INP(wi).local_port, \
INP(wi).dtime, i, b64_databuf);
i = send(bargs->socket, outbuf, buflen, MSG_NOSIGNAL);
free(outbuf);
if (i < 0) {
if (b2bua_xchg_getstatus(bargs) == B2BUA_XCHG_RUNS) {
b2bua_xchg_setstatus(bargs, B2BUA_XCHG_DEAD);
shutdown(bargs->socket, SHUT_RDWR);
}
queue_put_item(wi, bargs->inpacket_queue);
return;
}
wi_free(wi);
}
}
struct wi *
wi_malloc(enum wi_type type)
{
struct wi *wi;
wi = malloc(sizeof(*wi));
if (wi == NULL)
return wi;
memset(wi, '\0', sizeof(*wi));
wi->wi_type = type;
return wi;
}
void
wi_free(struct wi *wi)
{
switch (wi->wi_type) {
case WI_OUTPACKET:
if (OUTP(wi).databuf != NULL)
free(OUTP(wi).databuf);
if (OUTP(wi).remote_addr != NULL)
free(OUTP(wi).remote_addr);
if (OUTP(wi).remote_port != NULL)
free(OUTP(wi).remote_port);
if (OUTP(wi).local_addr != NULL)
free(OUTP(wi).local_addr);
break;
case WI_INPACKET:
if (INP(wi).databuf != NULL)
free(INP(wi).databuf);
if (INP(wi).remote_addr != NULL)
free(INP(wi).remote_addr);
if (INP(wi).local_addr != NULL)
free(INP(wi).local_addr);
break;
default:
fprintf(stderr, "unknown wi_type: %d\n", wi->wi_type);
abort();
}
free(wi);
}
static int
b2bua_xchg_in_stream(struct b2bua_xchg_args *bargs, int type, iks *node)
{
iks *y;
struct wi *wi;
u_char b64_databuf[8 * 1024];
int id, i;
struct b2bua_slot *bslot;
switch(type) {
case IKS_NODE_START:
break;
case IKS_NODE_NORMAL:
if (iks_type(node) == IKS_TAG && strcmp("outgoing_packet", iks_name(node)) == 0) {
#ifdef DEBUG
printf ("Recvd : %s\n", iks_string(iks_stack(node), node));
#endif
wi = wi_malloc(WI_OUTPACKET);
if (wi == NULL) {
iks_delete(node);
return IKS_NOMEM;
}
y = iks_attrib(node);
while (y) {
if (strcmp("dst_addr", iks_name(y)) == 0) {
OUTP(wi).remote_addr = strdup(iks_cdata(y));
} else if (strcmp("dst_port", iks_name(y)) == 0) {
OUTP(wi).remote_port = strdup(iks_cdata(y));
} else if (strcmp("src_addr", iks_name(y)) == 0) {
OUTP(wi).local_addr = strdup(iks_cdata(y));
} else if (strcmp("src_port", iks_name(y)) == 0) {
OUTP(wi).local_port = strtol(iks_cdata(y), (char **)NULL, 10);
} else if (strcmp("msg", iks_name(y)) == 0) {
OUTP(wi).ssize = ss_b64_pton(iks_cdata(y), b64_databuf, sizeof(b64_databuf));
if (OUTP(wi).ssize <= 0) {
wi_free(wi);
iks_delete(node);
return IKS_BADXML;
}
OUTP(wi).databuf = malloc(OUTP(wi).ssize);
if (OUTP(wi).databuf == NULL) {
wi_free(wi);
iks_delete(node);
return IKS_NOMEM;
}
memcpy(OUTP(wi).databuf, b64_databuf, OUTP(wi).ssize);
} else {
fprintf(stderr, "unknown attribute: %s='%s'\n", iks_name(y), iks_cdata(y));
wi_free(wi);
iks_delete(node);
return IKS_BADXML;
}
y = iks_next(y);
}
if (OUTP(wi).remote_addr == NULL) {
fprintf(stderr, "'dst_addr' attribute is missing\n");
wi_free(wi);
iks_delete(node);
return IKS_BADXML;
}
if (OUTP(wi).local_addr == NULL) {
fprintf(stderr, "'src_addr' attribute is missing\n");
wi_free(wi);
iks_delete(node);
return IKS_BADXML;
}
if (OUTP(wi).remote_port == NULL) {
fprintf(stderr, "'dst_port' attribute is missing\n");
wi_free(wi);
iks_delete(node);
return IKS_BADXML;
}
if (OUTP(wi).local_port <= 0) {
fprintf(stderr, "'src_port' attribute is missing\n");
wi_free(wi);
iks_delete(node);
return IKS_BADXML;
}
if (OUTP(wi).databuf == NULL) {
fprintf(stderr, "'msg' attribute is missing\n");
wi_free(wi);
iks_delete(node);
return IKS_BADXML;
}
queue_put_item(wi, &bargs->largs->outpacket_queue);
} else if (iks_type(node) == IKS_TAG && strcmp("b2bua_slot", iks_name(node)) == 0) {
if (bargs->inpacket_queue != NULL) {
fprintf(stderr, "slot is already assigned\n");
iks_delete(node);
return IKS_OK;
}
y = iks_attrib(node);
if (strcmp("id", iks_name(y)) != 0) {
iks_delete(node);
return IKS_BADXML;
}
id = strtol(iks_cdata(y), (char **)NULL, 10);
bslot = b2bua_getslot_by_id(bargs->bslots, id);
if (bslot == NULL) {
fprintf(stderr, "unknown slot id=%d\n", id);
iks_delete(node);
return IKS_OK;
}
bargs->inpacket_queue = &bslot->inpacket_queue;
i = pthread_create(&bargs->tx_thread, NULL, (void *(*)(void *))&b2bua_xchg_tx, bargs);
if (i != 0)
abort();
printf("fd %d associated with the slot %d\n", bargs->socket, id);
}
break;
case IKS_NODE_STOP:
default:
break;
}
iks_delete(node);
return IKS_OK;
}
static void
b2bua_xchg_rx(struct b2bua_xchg_args *bargs)
{
int i;
iksparser *p;
enum iksneterror recv_stat;
struct timespec tsp;
p = iks_stream_new("b2bua:socket_server", (void *)bargs, (iksStreamHook *)b2bua_xchg_in_stream);
i = iks_connect_fd(p, bargs->socket);
printf("iks_connect_fd(%d)\n", i);
for (;;) {
recv_stat = iks_recv(p, -1);
switch (recv_stat) {
case IKS_NET_NODNS:
case IKS_NET_NOSOCK:
case IKS_NET_NOCONN:
case IKS_NET_RWERR:
case IKS_NET_NOTSUPP:
printf("b2bua_xchg_rx: socket gone\n");
if (b2bua_xchg_getstatus(bargs) == B2BUA_XCHG_RUNS) {
b2bua_xchg_setstatus(bargs, B2BUA_XCHG_DEAD);
shutdown(bargs->socket, SHUT_RDWR);
}
if (bargs->inpacket_queue != NULL) {
for (;;) {
pthread_mutex_lock(&bargs->inpacket_queue->mutex);
pthread_cond_broadcast(&bargs->inpacket_queue->cond);
pthread_mutex_unlock(&bargs->inpacket_queue->mutex);
clock_gettime(CLOCK_REALTIME, &tsp);
tsp.tv_nsec += 10000000; /* 10ms */
if (pthread_timedjoin_np(bargs->tx_thread, NULL, &tsp) != ETIMEDOUT)
break;
}
}
close(bargs->socket);
printf("b2bua_xchg_rx: socket gone 1\n");
return;
default:
break;
}
}
}