~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjlib/src/pjlib-test/activesock.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: activesock.c 4238 2012-08-31 06:17:56Z nanang $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19
 
 */
20
 
#include "test.h"
21
 
#include <pjlib.h>
22
 
 
23
 
/**
24
 
 * \page page_pjlib_activesock_test Test: Active Socket
25
 
 *
26
 
 * This file is <b>pjlib-test/activesock.c</b>
27
 
 *
28
 
 * \include pjlib-test/activesock.c
29
 
 */
30
 
 
31
 
#if INCLUDE_ACTIVESOCK_TEST
32
 
 
33
 
#define THIS_FILE   "activesock.c"
34
 
 
35
 
 
36
 
/*******************************************************************
37
 
 * Simple UDP echo server.
38
 
 */
39
 
struct udp_echo_srv
40
 
{
41
 
    pj_activesock_t     *asock;
42
 
    pj_bool_t            echo_enabled;
43
 
    pj_uint16_t          port;
44
 
    pj_ioqueue_op_key_t  send_key;
45
 
    pj_status_t          status;
46
 
    unsigned             rx_cnt;
47
 
    unsigned             rx_err_cnt, tx_err_cnt;
48
 
};
49
 
 
50
 
static void udp_echo_err(const char *title, pj_status_t status)
51
 
{
52
 
    char errmsg[PJ_ERR_MSG_SIZE];
53
 
 
54
 
    pj_strerror(status, errmsg, sizeof(errmsg));
55
 
    PJ_LOG(3,(THIS_FILE, "   error: %s: %s", title, errmsg));
56
 
}
57
 
 
58
 
static pj_bool_t udp_echo_srv_on_data_recvfrom(pj_activesock_t *asock,
59
 
                                               void *data,
60
 
                                               pj_size_t size,
61
 
                                               const pj_sockaddr_t *src_addr,
62
 
                                               int addr_len,
63
 
                                               pj_status_t status)
64
 
{
65
 
    struct udp_echo_srv *srv;
66
 
    pj_ssize_t sent;
67
 
 
68
 
 
69
 
    srv = (struct udp_echo_srv*) pj_activesock_get_user_data(asock);
70
 
 
71
 
    if (status != PJ_SUCCESS) {
72
 
        srv->status = status;
73
 
        srv->rx_err_cnt++;
74
 
        udp_echo_err("recvfrom() callback", status);
75
 
        return PJ_TRUE;
76
 
    }
77
 
 
78
 
    srv->rx_cnt++;
79
 
 
80
 
    /* Send back if echo is enabled */
81
 
    if (srv->echo_enabled) {
82
 
        sent = size;
83
 
        srv->status = pj_activesock_sendto(asock, &srv->send_key, data, 
84
 
                                           &sent, 0,
85
 
                                           src_addr, addr_len);
86
 
        if (srv->status != PJ_SUCCESS) {
87
 
            srv->tx_err_cnt++;
88
 
            udp_echo_err("sendto()", status);
89
 
        }
90
 
    }
91
 
 
92
 
    return PJ_TRUE;
93
 
}
94
 
 
95
 
 
96
 
static pj_status_t udp_echo_srv_create(pj_pool_t *pool,
97
 
                                       pj_ioqueue_t *ioqueue,
98
 
                                       pj_bool_t enable_echo,
99
 
                                       struct udp_echo_srv **p_srv)
100
 
{
101
 
    struct udp_echo_srv *srv;
102
 
    pj_sock_t sock_fd = PJ_INVALID_SOCKET;
103
 
    pj_sockaddr addr;
104
 
    int addr_len;
105
 
    pj_activesock_cb activesock_cb;
106
 
    pj_status_t status;
107
 
 
108
 
    srv = PJ_POOL_ZALLOC_T(pool, struct udp_echo_srv);
109
 
    srv->echo_enabled = enable_echo;
110
 
 
111
 
    pj_sockaddr_in_init(&addr.ipv4, NULL, 0);
112
 
    addr_len = sizeof(addr);
113
 
 
114
 
    pj_bzero(&activesock_cb, sizeof(activesock_cb));
115
 
    activesock_cb.on_data_recvfrom = &udp_echo_srv_on_data_recvfrom;
116
 
 
117
 
    status = pj_activesock_create_udp(pool, &addr, NULL, ioqueue, &activesock_cb, 
118
 
                                      srv, &srv->asock, &addr);
119
 
    if (status != PJ_SUCCESS) {
120
 
        pj_sock_close(sock_fd);
121
 
        udp_echo_err("pj_activesock_create()", status);
122
 
        return status;
123
 
    }
124
 
 
125
 
    srv->port = pj_ntohs(addr.ipv4.sin_port);
126
 
 
127
 
    pj_ioqueue_op_key_init(&srv->send_key, sizeof(srv->send_key));
128
 
 
129
 
    status = pj_activesock_start_recvfrom(srv->asock, pool, 32, 0);
130
 
    if (status != PJ_SUCCESS) {
131
 
        pj_activesock_close(srv->asock);
132
 
        udp_echo_err("pj_activesock_start_recvfrom()", status);
133
 
        return status;
134
 
    }
135
 
 
136
 
 
137
 
    *p_srv = srv;
138
 
    return PJ_SUCCESS;
139
 
}
140
 
 
141
 
static void udp_echo_srv_destroy(struct udp_echo_srv *srv)
142
 
{
143
 
    pj_activesock_close(srv->asock);
144
 
}
145
 
 
146
 
/*******************************************************************
147
 
 * UDP ping pong test (send packet back and forth between two UDP echo
148
 
 * servers.
149
 
 */
150
 
static int udp_ping_pong_test(void)
151
 
{
152
 
    pj_ioqueue_t *ioqueue = NULL;
153
 
    pj_pool_t *pool = NULL;
154
 
    struct udp_echo_srv *srv1=NULL, *srv2=NULL;
155
 
    pj_bool_t need_send = PJ_TRUE;
156
 
    unsigned data = 0;
157
 
    int count, ret;
158
 
    pj_status_t status;
159
 
 
160
 
    pool = pj_pool_create(mem, "pingpong", 512, 512, NULL);
161
 
    if (!pool)
162
 
        return -10;
163
 
 
164
 
    status = pj_ioqueue_create(pool, 4, &ioqueue);
165
 
    if (status != PJ_SUCCESS) {
166
 
        ret = -20;
167
 
        udp_echo_err("pj_ioqueue_create()", status);
168
 
        goto on_return;
169
 
    }
170
 
 
171
 
    status = udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv1);
172
 
    if (status != PJ_SUCCESS) {
173
 
        ret = -30;
174
 
        goto on_return;
175
 
    }
176
 
 
177
 
    status = udp_echo_srv_create(pool, ioqueue, PJ_TRUE, &srv2);
178
 
    if (status != PJ_SUCCESS) {
179
 
        ret = -40;
180
 
        goto on_return;
181
 
    }
182
 
 
183
 
    /* initiate the first send */
184
 
    for (count=0; count<1000; ++count) {
185
 
        unsigned last_rx1, last_rx2;
186
 
        unsigned i;
187
 
 
188
 
        if (need_send) {
189
 
            pj_str_t loopback;
190
 
            pj_sockaddr_in addr;
191
 
            pj_ssize_t sent;
192
 
 
193
 
            ++data;
194
 
 
195
 
            sent = sizeof(data);
196
 
            loopback = pj_str("127.0.0.1");
197
 
            pj_sockaddr_in_init(&addr, &loopback, srv2->port);
198
 
            status = pj_activesock_sendto(srv1->asock, &srv1->send_key,
199
 
                                          &data, &sent, 0,
200
 
                                          &addr, sizeof(addr));
201
 
            if (status != PJ_SUCCESS && status != PJ_EPENDING) {
202
 
                ret = -50;
203
 
                udp_echo_err("sendto()", status);
204
 
                goto on_return;
205
 
            }
206
 
 
207
 
            need_send = PJ_FALSE;
208
 
        }
209
 
 
210
 
        last_rx1 = srv1->rx_cnt;
211
 
        last_rx2 = srv2->rx_cnt;
212
 
 
213
 
        for (i=0; i<10 && last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt; ++i) {
214
 
            pj_time_val delay = {0, 10};
215
 
#ifdef PJ_SYMBIAN
216
 
            PJ_UNUSED_ARG(delay);
217
 
            pj_symbianos_poll(-1, 100);
218
 
#else
219
 
            pj_ioqueue_poll(ioqueue, &delay);
220
 
#endif
221
 
        }
222
 
 
223
 
        if (srv1->rx_err_cnt+srv1->tx_err_cnt != 0 ||
224
 
            srv2->rx_err_cnt+srv2->tx_err_cnt != 0)
225
 
        {
226
 
            /* Got error */
227
 
            ret = -60;
228
 
            goto on_return;
229
 
        }
230
 
 
231
 
        if (last_rx1 == srv1->rx_cnt && last_rx2 == srv2->rx_cnt) {
232
 
            /* Packet lost */
233
 
            ret = -70;
234
 
            udp_echo_err("packets have been lost", PJ_ETIMEDOUT);
235
 
            goto on_return;
236
 
        }
237
 
    }
238
 
 
239
 
    ret = 0;
240
 
 
241
 
on_return:
242
 
    if (srv2)
243
 
        udp_echo_srv_destroy(srv2);
244
 
    if (srv1)
245
 
        udp_echo_srv_destroy(srv1);
246
 
    if (ioqueue)
247
 
        pj_ioqueue_destroy(ioqueue);
248
 
    if (pool)
249
 
        pj_pool_release(pool);
250
 
    
251
 
    return ret;
252
 
}
253
 
 
254
 
 
255
 
 
256
 
#define SIGNATURE   0xdeadbeef
257
 
struct tcp_pkt
258
 
{
259
 
    pj_uint32_t signature;
260
 
    pj_uint32_t seq;
261
 
    char        fill[513];
262
 
};
263
 
 
264
 
struct tcp_state
265
 
{
266
 
    pj_bool_t   err;
267
 
    pj_bool_t   sent;
268
 
    pj_uint32_t next_recv_seq;
269
 
    pj_uint8_t  pkt[600];
270
 
};
271
 
 
272
 
struct send_key
273
 
{
274
 
    pj_ioqueue_op_key_t op_key;
275
 
};
276
 
 
277
 
 
278
 
static pj_bool_t tcp_on_data_read(pj_activesock_t *asock,
279
 
                                  void *data,
280
 
                                  pj_size_t size,
281
 
                                  pj_status_t status,
282
 
                                  pj_size_t *remainder)
283
 
{
284
 
    struct tcp_state *st = (struct tcp_state*) pj_activesock_get_user_data(asock);
285
 
    char *next = (char*) data;
286
 
 
287
 
    if (status != PJ_SUCCESS && status != PJ_EPENDING) {
288
 
        PJ_LOG(1,("", "   err: status=%d", status));
289
 
        st->err = PJ_TRUE;
290
 
        return PJ_FALSE;
291
 
    }
292
 
 
293
 
    while (size >= sizeof(struct tcp_pkt)) {
294
 
        struct tcp_pkt *tcp_pkt = (struct tcp_pkt*) next;
295
 
 
296
 
        if (tcp_pkt->signature != SIGNATURE) {
297
 
            PJ_LOG(1,("", "   err: invalid signature at seq=%d", 
298
 
                          st->next_recv_seq));
299
 
            st->err = PJ_TRUE;
300
 
            return PJ_FALSE;
301
 
        }
302
 
        if (tcp_pkt->seq != st->next_recv_seq) {
303
 
            PJ_LOG(1,("", "   err: wrong sequence"));
304
 
            st->err = PJ_TRUE;
305
 
            return PJ_FALSE;
306
 
        }
307
 
 
308
 
        st->next_recv_seq++;
309
 
        next += sizeof(struct tcp_pkt);
310
 
        size -= sizeof(struct tcp_pkt);
311
 
    }
312
 
 
313
 
    if (size) {
314
 
        pj_memmove(data, next, size);
315
 
        *remainder = size;
316
 
    }
317
 
 
318
 
    return PJ_TRUE;
319
 
}
320
 
 
321
 
static pj_bool_t tcp_on_data_sent(pj_activesock_t *asock,
322
 
                                  pj_ioqueue_op_key_t *op_key,
323
 
                                  pj_ssize_t sent)
324
 
{
325
 
    struct tcp_state *st=(struct tcp_state*)pj_activesock_get_user_data(asock);
326
 
 
327
 
    PJ_UNUSED_ARG(op_key);
328
 
 
329
 
    st->sent = 1;
330
 
 
331
 
    if (sent < 1) {
332
 
        st->err = PJ_TRUE;
333
 
        return PJ_FALSE;
334
 
    }
335
 
 
336
 
    return PJ_TRUE;
337
 
}
338
 
 
339
 
static int tcp_perf_test(void)
340
 
{
341
 
    enum { COUNT=10000 };
342
 
    pj_pool_t *pool = NULL;
343
 
    pj_ioqueue_t *ioqueue = NULL;
344
 
    pj_sock_t sock1=PJ_INVALID_SOCKET, sock2=PJ_INVALID_SOCKET;
345
 
    pj_activesock_t *asock1 = NULL, *asock2 = NULL;
346
 
    pj_activesock_cb cb;
347
 
    struct tcp_state *state1, *state2;
348
 
    unsigned i;
349
 
    pj_status_t status;
350
 
 
351
 
    pool = pj_pool_create(mem, "tcpperf", 256, 256, NULL);
352
 
 
353
 
    status = app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock1, 
354
 
                            &sock2);
355
 
    if (status != PJ_SUCCESS) {
356
 
        status = -100;
357
 
        goto on_return;
358
 
    }
359
 
 
360
 
    status = pj_ioqueue_create(pool, 4, &ioqueue);
361
 
    if (status != PJ_SUCCESS) {
362
 
        status = -110;
363
 
        goto on_return;
364
 
    }
365
 
 
366
 
    pj_bzero(&cb, sizeof(cb));
367
 
    cb.on_data_read = &tcp_on_data_read;
368
 
    cb.on_data_sent = &tcp_on_data_sent;
369
 
 
370
 
    state1 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
371
 
    status = pj_activesock_create(pool, sock1, pj_SOCK_STREAM(), NULL, ioqueue,
372
 
                                  &cb, state1, &asock1);
373
 
    if (status != PJ_SUCCESS) {
374
 
        status = -120;
375
 
        goto on_return;
376
 
    }
377
 
 
378
 
    state2 = PJ_POOL_ZALLOC_T(pool, struct tcp_state);
379
 
    status = pj_activesock_create(pool, sock2, pj_SOCK_STREAM(), NULL, ioqueue,
380
 
                                  &cb, state2, &asock2);
381
 
    if (status != PJ_SUCCESS) {
382
 
        status = -130;
383
 
        goto on_return;
384
 
    }
385
 
 
386
 
    status = pj_activesock_start_read(asock1, pool, 1000, 0);
387
 
    if (status != PJ_SUCCESS) {
388
 
        status = -140;
389
 
        goto on_return;
390
 
    }
391
 
 
392
 
    /* Send packet as quickly as possible */
393
 
    for (i=0; i<COUNT && !state1->err && !state2->err; ++i) {
394
 
        struct tcp_pkt *pkt;
395
 
        struct send_key send_key[2], *op_key;
396
 
        pj_ssize_t len;
397
 
 
398
 
        pkt = (struct tcp_pkt*)state2->pkt;
399
 
        pkt->signature = SIGNATURE;
400
 
        pkt->seq = i;
401
 
        pj_memset(pkt->fill, 'a', sizeof(pkt->fill));
402
 
 
403
 
        op_key = &send_key[i%2];
404
 
        pj_ioqueue_op_key_init(&op_key->op_key, sizeof(*op_key));
405
 
 
406
 
        state2->sent = PJ_FALSE;
407
 
        len = sizeof(*pkt);
408
 
        status = pj_activesock_send(asock2, &op_key->op_key, pkt, &len, 0);
409
 
        if (status == PJ_EPENDING) {
410
 
            do {
411
 
#if PJ_SYMBIAN
412
 
                pj_symbianos_poll(-1, -1);
413
 
#else
414
 
                pj_ioqueue_poll(ioqueue, NULL);
415
 
#endif
416
 
            } while (!state2->sent);
417
 
        } else {
418
 
#if PJ_SYMBIAN
419
 
                /* The Symbian socket always returns PJ_SUCCESS for TCP send,
420
 
                 * eventhough the remote end hasn't received the data yet.
421
 
                 * If we continue sending, eventually send() will block,
422
 
                 * possibly because the send buffer is full. So we need to
423
 
                 * poll the ioqueue periodically, to let receiver gets the 
424
 
                 * data.
425
 
                 */
426
 
                pj_symbianos_poll(-1, 0);
427
 
#endif
428
 
                if (status != PJ_SUCCESS) {
429
 
                    PJ_LOG(1,("", "   err: send status=%d", status));
430
 
                    status = -180;
431
 
                    break;
432
 
                } else if (status == PJ_SUCCESS) {
433
 
                    if (len != sizeof(*pkt)) {
434
 
                        PJ_LOG(1,("", "   err: shouldn't report partial sent"));
435
 
                        status = -190;
436
 
                        break;
437
 
                    }
438
 
                }
439
 
        }
440
 
 
441
 
#ifndef PJ_SYMBIAN
442
 
        for (;;) {
443
 
            pj_time_val timeout = {0, 10};
444
 
            if (pj_ioqueue_poll(ioqueue, &timeout) < 1)
445
 
                break;
446
 
        }
447
 
#endif
448
 
 
449
 
    }
450
 
 
451
 
    /* Wait until everything has been sent/received */
452
 
    if (state1->next_recv_seq < COUNT) {
453
 
#ifdef PJ_SYMBIAN
454
 
        while (pj_symbianos_poll(-1, 1000) == PJ_TRUE)
455
 
            ;
456
 
#else
457
 
        pj_time_val delay = {0, 100};
458
 
        while (pj_ioqueue_poll(ioqueue, &delay) > 0)
459
 
            ;
460
 
#endif
461
 
    }
462
 
 
463
 
    if (status == PJ_EPENDING)
464
 
        status = PJ_SUCCESS;
465
 
 
466
 
    if (status != 0)
467
 
        goto on_return;
468
 
 
469
 
    if (state1->err) {
470
 
        status = -183;
471
 
        goto on_return;
472
 
    }
473
 
    if (state2->err) {
474
 
        status = -186;
475
 
        goto on_return;
476
 
    }
477
 
    if (state1->next_recv_seq != COUNT) {
478
 
        PJ_LOG(3,("", "   err: only %u packets received, expecting %u", 
479
 
                      state1->next_recv_seq, COUNT));
480
 
        status = -195;
481
 
        goto on_return;
482
 
    }
483
 
 
484
 
on_return:
485
 
    if (asock2)
486
 
        pj_activesock_close(asock2);
487
 
    if (asock1)
488
 
        pj_activesock_close(asock1);
489
 
    if (ioqueue)
490
 
        pj_ioqueue_destroy(ioqueue);
491
 
    if (pool)
492
 
        pj_pool_release(pool);
493
 
 
494
 
    return status;
495
 
}
496
 
 
497
 
 
498
 
 
499
 
int activesock_test(void)
500
 
{
501
 
    int ret;
502
 
 
503
 
    PJ_LOG(3,("", "..udp ping/pong test"));
504
 
    ret = udp_ping_pong_test();
505
 
    if (ret != 0)
506
 
        return ret;
507
 
 
508
 
    PJ_LOG(3,("", "..tcp perf test"));
509
 
    ret = tcp_perf_test();
510
 
    if (ret != 0)
511
 
        return ret;
512
 
 
513
 
    return 0;
514
 
}
515
 
 
516
 
#else   /* INCLUDE_ACTIVESOCK_TEST */
517
 
/* To prevent warning about "translation unit is empty"
518
 
 * when this test is disabled. 
519
 
 */
520
 
int dummy_active_sock_test;
521
 
#endif  /* INCLUDE_ACTIVESOCK_TEST */
522