summaryrefslogtreecommitdiffstats
path: root/tinyDEMO/common.c
blob: 92e19c727ab5e823c30894059656dd7d7774187a (plain)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
#include "common.h"

#include "invite.h"
#include "message.h"
#include "options.h"
#include "publish.h"
#include "register.h"
#include "subscribe.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* === default values === */
#define DEFAULT_REALM	"open-ims.test"
#define DEFAULT_IMPI "bob@"DEFAULT_REALM
#define DEFAULT_IMPU "sip:bob@"DEFAULT_REALM

#ifndef DEFAULT_LOCAL_IP
//#	ifdef ANDROID /* On the emulator */
//#		define DEFAULT_LOCAL_IP	"10.0.2.15"
//#		define DEFAULT_LOCAL_IP	"192.168.0.14"
//#	else
#		define DEFAULT_LOCAL_IP	TNET_SOCKET_HOST_ANY
//#	endif
#endif


extern ctx_t* ctx;

int stack_callback(const tsip_event_t *sipevent);
int session_handle_event(const tsip_event_t *sipevent);
int session_tostring(const session_t* session);

/* our SIP callback function */
int stack_callback(const tsip_event_t *_event)
{
    int ret = 0;

    if(!_event) { /* should never happen ...but who know? */
        TSK_DEBUG_WARN("Null SIP event.");
        return -1;
    }

#if 0
    tsk_safeobj_lock(ctx);
#endif

    switch(_event->type) {
    case tsip_event_register: {
        /* REGISTER */
        ret = register_handle_event(_event);
        break;
    }
    case tsip_event_invite: {
        /* INVITE */
        ret = invite_handle_event(_event);
        break;
    }
    case tsip_event_message: {
        /* MESSAGE */
        ret = message_handle_event(_event);
        break;
    }
    case tsip_event_options: {
        /* OPTIONS */
        ret = options_handle_event(_event);
        break;
    }
    case tsip_event_publish: {
        /* PUBLISH */
        ret = publish_handle_event(_event);
        break;
    }
    case tsip_event_subscribe: {
        /* SUBSCRIBE */
        ret = subscribe_handle_event(_event);
        break;
    }

    case tsip_event_dialog: {
        /* Common to all dialogs */
        ret = session_handle_event(_event);
        break;
    }

    case tsip_event_stack: {
        switch(_event->code) {
        case tsip_event_code_stack_started:
            TSK_DEBUG_INFO("Stack started");
            break;
        case tsip_event_code_stack_stopped:
            TSK_DEBUG_INFO("Stack stopped");
            break;
        case tsip_event_code_stack_failed_to_start:
            TSK_DEBUG_INFO("Stack failed to start");
            break;
        case tsip_event_code_stack_failed_to_stop:
            TSK_DEBUG_INFO("Stack failed to stop");
            break;
        }
        break;
    }

    default: {
        /* Unsupported */
        TSK_DEBUG_WARN("%d not supported as SIP event.", _event->type);
        ret = -3;
        break;
    }
    }
#if 0
    tsk_safeobj_unlock(ctx);
#endif

    return ret;
}

/*	==================================================================
	========================== Context =================================
*/

ctx_t* ctx_create()
{
    return tsk_object_new(ctx_def_t);
}

static tsk_object_t* ctx_ctor(tsk_object_t * self, va_list * app)
{
    ctx_t *ctx = self;
    if(ctx) {
        /* stack */
        ctx->stack = tsip_stack_create(stack_callback, DEFAULT_REALM, DEFAULT_IMPI, DEFAULT_IMPU, /* Mandatory parameters */
                                       TSIP_STACK_SET_LOCAL_IP(DEFAULT_LOCAL_IP), /* local IP */
                                       TSIP_STACK_SET_NULL() /* Mandatory */);

        /* SIP Sessions */
        ctx->sessions = tsk_list_create();

        /* user's parameters */
        ctx->params = tsk_list_create();

        /* init internal mutex */
        tsk_safeobj_init(ctx);
    }
    return self;
}

static tsk_object_t* ctx_dtor(tsk_object_t * self)
{
    ctx_t *ctx = self;
    if(ctx) {
        /* Stop the stack (as sessions are alive, you will continue to receive callbacks)*/
        tsip_stack_stop(ctx->stack);

        /* sessions : should be freed before the stack as explained on the Programmer's Guide
        * As all dialogs have been hanged up, the list should be empty ...but who know?*/
        TSK_OBJECT_SAFE_FREE(ctx->sessions);

        /* Destroy the stack */
        TSK_OBJECT_SAFE_FREE(ctx->stack);

        /* Identity */
        TSK_FREE(ctx->identity.display_name);
        TSK_FREE(ctx->identity.impu);
        TSK_FREE(ctx->identity.preferred);
        TSK_FREE(ctx->identity.impi);
        TSK_FREE(ctx->identity.password);

        /* Network */
        TSK_FREE(ctx->network.local_ip);
        TSK_FREE(ctx->network.proxy_cscf);
        TSK_FREE(ctx->network.proxy_cscf_trans);
        TSK_FREE(ctx->network.realm);

        /* Security */
        TSK_FREE(ctx->security.operator_id);

        /* Params */
        TSK_OBJECT_SAFE_FREE(ctx->params);

        /* deinit internal mutex */
        tsk_safeobj_deinit(ctx);
    }

    return self;
}

static const tsk_object_def_t ctx_def_s = {
    sizeof(ctx_t),
    ctx_ctor,
    ctx_dtor,
    tsk_null,
};
const tsk_object_def_t *ctx_def_t = &ctx_def_s;


/*	==================================================================
	========================== Stack =================================
*/

int stack_dump()
{
    const tsk_list_item_t* item;

    tsk_list_foreach(item, ctx->sessions) {
        session_tostring(item->data);
    }
    return 0;
}

int stack_config(const opts_L_t* opts)
{
    const tsk_list_item_t* item;
    const opt_t* opt;
    int ret = 0;
    tsk_param_t* param;
    tsk_bool_t pcscf_changed = tsk_false;
    tsk_bool_t stun_done = tsk_false;

    if(!opts) {
        return -1;
    }

    tsk_list_foreach(item, opts) {
        opt = item->data;

        /* Stack-level option */
        if(opt->lv != lv_none && opt->lv != lv_stack) {
            continue;
        }

        switch(opt->type) {
        case opt_amf: {
            break;
        }
        case opt_dhcpv4:
        case opt_dhcpv6: {
            tsip_stack_set(ctx->stack,
                           TSIP_STACK_SET_DISCOVERY_DHCP(tsk_true),
                           TSIP_STACK_SET_NULL());
            break;
        }
        case opt_dname: {
            break;
        }
        case opt_dns_naptr: {
            tsip_stack_set(ctx->stack,
                           TSIP_STACK_SET_DISCOVERY_NAPTR(tsk_true),
                           TSIP_STACK_SET_NULL());
            break;
        }
        case opt_header: {
            if((param = tsk_params_parse_param(opt->value, tsk_strlen(opt->value)))) {
                ret = tsip_stack_set(ctx->stack,
                                     TSIP_STACK_SET_HEADER(param->name, param->value),
                                     TSIP_STACK_SET_NULL());
                TSK_OBJECT_SAFE_FREE(param);
            }
            break;
        }
        case opt_impi: {
            tsk_strupdate(&ctx->identity.impi, opt->value);
            ret = tsip_stack_set(ctx->stack,
                                 TSIP_STACK_SET_IMPI(ctx->identity.impi),
                                 TSIP_STACK_SET_NULL());
            break;
        }
        case opt_impu: {
            tsk_strupdate(&ctx->identity.impu, opt->value);
            ret = tsip_stack_set(ctx->stack,
                                 TSIP_STACK_SET_IMPU(ctx->identity.impu),
                                 TSIP_STACK_SET_NULL());
            break;
        }
        case opt_ipv6: {
            pcscf_changed = tsk_true;
            ctx->network.ipv6 = tsk_true;
            break;
        }
        case opt_local_ip: {
            ret = tsip_stack_set(ctx->stack,
                                 TSIP_STACK_SET_LOCAL_IP(opt->value),
                                 TSIP_STACK_SET_NULL());
            break;
        }
        case opt_local_port: {
            unsigned port = (unsigned)atoi(opt->value);
            ret = tsip_stack_set(ctx->stack,
                                 TSIP_STACK_SET_LOCAL_PORT(port),
                                 TSIP_STACK_SET_NULL());
            break;
        }
        case opt_opid: {
            break;
        }
        case opt_password: {
            ret = tsip_stack_set(ctx->stack,
                                 TSIP_STACK_SET_PASSWORD(opt->value),
                                 TSIP_STACK_SET_NULL());
            break;
        }
        case opt_pcscf_ip: {
            pcscf_changed = tsk_true;
            tsk_strupdate(&ctx->network.proxy_cscf, opt->value);
            break;
        }
        case opt_pcscf_port: {
            pcscf_changed = tsk_true;
            ctx->network.proxy_cscf_port = atoi(opt->value);
            break;
        }
        case opt_pcscf_trans: {
            pcscf_changed = tsk_true;
            tsk_strupdate(&ctx->network.proxy_cscf_trans, opt->value);
            break;
        }
        case opt_realm: {
            ret = tsip_stack_set(ctx->stack,
                                 TSIP_STACK_SET_REALM(opt->value),
                                 TSIP_STACK_SET_NULL());
            break;
        }

        case opt_sigcomp_id: {
            /* add compartment */
            ret = tsip_stack_set(ctx->stack,
                                 TSIP_STACK_SET_SIGCOMP_NEW_COMPARTMENT(opt->value),
                                 TSIP_STACK_SET_NULL());
            break;
        }

        case opt_stun_ip:
        case opt_stun_pwd:
        case opt_stun_port:
        case opt_stun_usr: {
            if(!stun_done) {
                const opt_t* _opt;
                const char* ip = tsk_null, *usr = tsk_null, *pwd = tsk_null;
                unsigned port = 0;

                if((_opt = opt_get_by_type(opts, opt_stun_ip))) {
                    ip = _opt->value;
                }
                if((_opt = opt_get_by_type(opts, opt_stun_port))) {
                    port = atoi(_opt->value);
                }
                if((_opt = opt_get_by_type(opts, opt_stun_usr))) {
                    usr = _opt->value;
                }
                if((_opt = opt_get_by_type(opts, opt_stun_pwd))) {
                    pwd = _opt->value;
                }

                if(ip && port) {
                    tsip_stack_set(ctx->stack,
                                   TSIP_STACK_SET_STUN_SERVER(ip, port),
                                   TSIP_STACK_SET_NULL());
                }
                if(usr) {
                    tsip_stack_set(ctx->stack,
                                   TSIP_STACK_SET_STUN_CRED(usr, pwd),
                                   TSIP_STACK_SET_NULL());
                }

                stun_done = tsk_true;
            }
            break;
        }

        }/* switch */

    } /* foreach */

    /* whether Proxy-CSCF config has changed */
    if(pcscf_changed) {
        ret = tsip_stack_set(ctx->stack,
                             TSIP_STACK_SET_PROXY_CSCF(ctx->network.proxy_cscf, ctx->network.proxy_cscf_port, ctx->network.proxy_cscf_trans, ctx->network.ipv6 ? "ipv6" : "ipv4"),
                             TSIP_STACK_SET_NULL());
    }

    return ret;
}

int stack_run(const opts_L_t* opts)
{
    if(!ctx->stack) {
        TSK_DEBUG_ERROR("Stack is Null.");
        return -1;
    }
    else {
        return tsip_stack_start(ctx->stack);
    }
}

/*	==================================================================
	========================== Session =================================
*/

/* Find SIP session by id */
int pred_find_session_by_id(const tsk_list_item_t *item, const void* id)
{
    const session_t* session;
    if(item && item->data) {
        session = item->data;
        return (int)(tsip_ssession_get_id(session->handle)
                     - *((tsip_ssession_id_t*)id));
    }
    return -1;
}

session_t* session_create(session_type_t type, tsip_ssession_handle_t* handle)
{
    session_t* session = tsk_object_new(session_def_t, type, handle);
    if(!session) {
        TSK_DEBUG_ERROR("Failed to create new SIP session");
        return tsk_null;
    }

    switch(type) {
    case st_invite: {
        /* Enable all features (QoS, Session timers, SigComp, ...) */
        tsip_ssession_set(session->handle,
                          TSIP_SSESSION_SET_USERDATA(session),
                          /*=== MEDIA */
                          TSIP_SSESSION_SET_MEDIA(
                              // 100rel
                              TSIP_MSESSION_SET_100rel(tsk_false),
                              // Session timers
                              TSIP_MSESSION_SET_TIMERS(3600, "uac"),
                              // QoS
                              TSIP_MSESSION_SET_QOS(tmedia_qos_stype_segmented, tmedia_qos_strength_optional),
                              // close media params
                              TSIP_MSESSION_SET_NULL()
                          ),

                          TSIP_SSESSION_SET_NULL());
        break;
    }
    default:
        break;
    }
    return session;
}

const session_t* session_get_by_sid(const sessions_L_t* sessions, tsip_ssession_id_t sid)
{
    const tsk_list_item_t* item;
    if((item = tsk_list_find_item_by_pred(sessions, pred_find_session_by_id, &sid))) {
        return item->data;
    }
    else {
        return tsk_null;
    }
}


int session_tostring(const session_t* session)
{
    //char* temp = tsk_null;
    printf("== Session: ");
    if(session) {
        /* Session Id */
        printf("sid=%llu", tsip_ssession_get_id(session->handle));
        /* Type */
        printf(" type=");
        switch(session->type) {
        case st_invite:
            printf("INVITE");
            break;
        case st_message:
            printf("MESSAGE");
            break;
        case st_publish:
            printf("PUBLISH");
            break;
        case st_register:
            printf("REGISTER");
            break;
        case st_subscribe:
            printf("SUBSCRIBE");
            break;
        default:
            printf("(null)");
            break;
        }
        /* From */
        printf(" from=%s", session->from ? session->from : ctx->identity.impu);
        /* From */
        printf(" to=%s", session->to ? session->to : ctx->identity.impu);
    }
    else {
        printf("(invalid)");
    }
    printf("\n");
    return -1;
}


/* handle events -common to all sessions */
int session_handle_event(const tsip_event_t *_event)
{
    const session_t* session;

    /* Find associated session */
    if(!(session = session_get_by_sid(ctx->sessions, tsip_ssession_get_id(_event->ss)))) {
        /* Silentky ignore */
        return 0;
    }
    switch(_event->code) {
        /* === 7xx ==> errors ===  */
    case tsip_event_code_dialog_transport_error:
    case tsip_event_code_dialog_global_error:
    case tsip_event_code_dialog_message_error:
        /* do not guess that the dialog is terminated, wait for "tsip_event_code_dialog_terminated" event */
        break;

        /* === 8xx ==> success ===  */
    case tsip_event_code_dialog_request_incoming:
    case tsip_event_code_dialog_request_cancelled:
    case tsip_event_code_dialog_request_sent:
        break;

        /* === 9xx ==> Informational ===  */
    case tsip_event_code_dialog_terminated: {
        /* we no longer need the session
        * -> remove and destroy the session */
        TSK_DEBUG_INFO("Dialog Terminated --> %s", _event->phrase);
        tsk_list_remove_item_by_data(ctx->sessions, session);
        break;
    }

    case tsip_event_code_dialog_connected:
        ((session_t*)session)->connected = tsk_true;
        break;
    case tsip_event_code_dialog_terminating:
        break;
    }
    return 0;
}

/* handle commands -common to all sessions */
const session_t*  session_handle_cmd(cmd_type_t cmd, const opts_L_t* opts)
{
    const session_t* session = tsk_null;
    const opt_t* opt;
    const tsk_list_item_t* item;
    tsk_param_t* param;
    int ret = 0;

    /* Check if there is a session with is Id */
    if((opt = opt_get_by_type(opts, opt_sid))) {
        tsip_ssession_id_t sid = atoi(opt->value);
        session = session_get_by_sid(ctx->sessions, sid);
    }

#define TYPE_FROM_CMD(_CMD) \
	((_CMD==cmd_audio || _CMD==cmd_video || _CMD==cmd_audiovideo || _CMD==cmd_file || _CMD==cmd_large_message) ? st_invite :  \
	((_CMD==cmd_message || _CMD==cmd_sms) ? st_message : \
	(_CMD==cmd_options ? st_options : \
	(_CMD==cmd_publish ? st_publish : \
	(_CMD==cmd_register ? st_register : \
	(_CMD==cmd_subscribe ? st_subscribe : st_none))))))

    /* === Command === */
    switch(cmd) {
    case cmd_audio:
    case cmd_video:
    case cmd_audiovideo:
    case cmd_file:
    case cmd_large_message:
    case cmd_message:
    case cmd_sms:
    case cmd_options:
    case cmd_publish:
    case cmd_register:
    case cmd_subscribe: {
        if(!session) { /* Create "client-side-session" */
            session_t* _session;
            if((_session = session_client_create(TYPE_FROM_CMD(cmd))) && (session = _session)) {
                tsk_list_push_back_data(ctx->sessions, (void**)&_session);
            }
        }
        break;
    }
    default: {
        if(session) {
            /* hold, resume, refer, update, ...all in-dialog commands */
            break;
        }
        else {
            TSK_DEBUG_WARN("Session handling: Cannot handle this command [%d]", cmd);
            goto bail;
        }
    }
    } /* switch */

    if(!session) {
        TSK_DEBUG_ERROR("SIP Session is Null");
        goto bail;
    }

    /* === User Options === */
    tsk_list_foreach(item, opts) {
        opt = item->data;

        /* Session-level option? */
        if(opt->lv != lv_none && opt->lv != lv_session) {
            continue;
        }

        switch(opt->type) {
        case opt_caps: {
            if(!tsk_strnullORempty(opt->value)) {
                if((param = tsk_params_parse_param(opt->value, tsk_strlen(opt->value)))) {
                    ret = tsip_ssession_set(session->handle,
                                            TSIP_SSESSION_SET_CAPS(param->name, param->value),
                                            TSIP_SSESSION_SET_NULL());
                    TSK_OBJECT_SAFE_FREE(param);
                }
            }
            break;
        }
        case opt_expires: {
            if(!tsk_strnullORempty(opt->value)) {
                unsigned expires = atoi(opt->value);
                ret = tsip_ssession_set(session->handle,
                                        TSIP_SSESSION_SET_EXPIRES(expires),
                                        TSIP_SSESSION_SET_NULL());
            }
            break;
        }
        case opt_from: {
            /* You should use  TSIP_SSESSION_SET_OPTION(TSIP_SSESSION_OPTION_FROM, value)
            	instead of TSIP_SSESSION_SET_HEADER() to set the destination URI. */
            break;
        }
        case opt_header: {
            if((param = tsk_params_parse_param(opt->value, tsk_strlen(opt->value)))) {
                ret = tsip_ssession_set(session->handle,
                                        TSIP_SSESSION_SET_HEADER(param->name, param->value),
                                        TSIP_SSESSION_SET_NULL());
                TSK_OBJECT_SAFE_FREE(param);
            }
            break;
        }
        case opt_payload: {
            /* Will be handled by the caller */
            break;
        }
        case opt_silent: {
            /* valueless option */
            ret = tsip_ssession_set(session->handle,
                                    TSIP_SSESSION_SET_SILENT_HANGUP(tsk_true),
                                    TSIP_SSESSION_SET_NULL());
            break;
        }
        case opt_sigcomp_id: {
            /* sigcomp-id */
            ret = tsip_ssession_set(session->handle,
                                    TSIP_SSESSION_SET_SIGCOMP_COMPARTMENT(opt->value),
                                    TSIP_SSESSION_SET_NULL());
            break;
        }
        case opt_to: {
            /* You should use  TSIP_SSESSION_SET_OPTION(TSIP_SSESSION_OPTION_TO, value)
            	instead of TSIP_SSESSION_SET_HEADER() to set the destination URI. */
            if((cmd != cmd_sms) && (cmd != cmd_ect) && !tsk_strnullORempty(opt->value)) { /* SMS will use SMSC Address as Request URI */
                ret = tsip_ssession_set(session->handle,
                                        TSIP_SSESSION_SET_TO(opt->value),
                                        TSIP_SSESSION_SET_NULL());
            }
            break;
        }
        default: {
            /* will be handled by the caller */
            break;
        }
        }

    } /* foreach */

bail:
    return session;
}

int session_hangup(tsip_ssession_id_t sid)
{
    const session_t* session;
    if((session = session_get_by_sid(ctx->sessions, sid))) {
        switch(session->type) {
        case st_invite:
            tsip_api_invite_send_bye(session->handle,
                                     /* You can add your parameters */
                                     TSIP_ACTION_SET_NULL());
            break;
        case st_message:
            break;
        case st_publish:
            tsip_api_publish_send_unpublish(session->handle,
                                            /* You can add your parameters */
                                            TSIP_ACTION_SET_NULL());
            break;
        case st_register:
            tsip_api_register_send_unregister(session->handle,
                                              /* You can add your parameters */
                                              TSIP_ACTION_SET_NULL());
            break;
        case st_subscribe:
            tsip_api_subscribe_send_unsubscribe(session->handle,
                                                /* You can add your parameters */
                                                TSIP_ACTION_SET_NULL());
            break;
        default:
            TSK_DEBUG_WARN("Cannot hangup session with this type [%d]", session->type);
            return -2;
        }
        return 0;
    }
    else {
        TSK_DEBUG_WARN("Failed to find session with sid=%llu", sid);
        return -1;
    }
}

static tsk_object_t* session_ctor(tsk_object_t * self, va_list * app)
{
    session_t *session = self;
    if(session) {
        session->type = va_arg(*app, session_type_t);
        if((session->handle = va_arg(*app, tsip_ssession_handle_t*))) {
            int ret;
            /* "server-side-session" */
            if((ret = tsip_ssession_take_ownership(session->handle))) {
                TSK_DEBUG_ERROR("Failed to take ownership [%d]", ret);
            }
        }
        else {
            /* "client-side-session" */
            session->handle = tsip_ssession_create(ctx->stack,
                                                   TSIP_SSESSION_SET_NULL());
        }
    }
    return self;
}

static tsk_object_t* session_dtor(tsk_object_t * self)
{
    session_t *session = self;
    if(session) {
        TSK_OBJECT_SAFE_FREE(session->handle);

        TSK_FREE(session->to);
        TSK_FREE(session->from);
    }

    return self;
}

static int session_cmp(const tsk_object_t *_ss1, const tsk_object_t *_ss2)
{
    const session_t *ss1 = _ss1;
    const session_t *ss2 = _ss2;

    if(ss1 && ss1) {
        if(ss1->handle && ss2->handle) {
            return tsk_object_cmp(ss1->handle, ss2->handle);
        }
        else {
            return (ss2 - ss1);
        }
    }
    else if(!ss1 && !ss2) {
        return 0;
    }
    else {
        return -1;
    }
}

static const tsk_object_def_t session_def_s = {
    sizeof(session_t),
    session_ctor,
    session_dtor,
    session_cmp,
};
const tsk_object_def_t *session_def_t = &session_def_s;



tsip_action_handle_t* action_get_config(const opts_L_t* opts)
{
    const opt_t* opt;
    const tsk_list_item_t* item;
    tsip_action_handle_t* action_config = tsk_null;
    tsk_param_t* param;

    if(TSK_LIST_IS_EMPTY(opts)) {
        return tsk_null;
    }

    tsk_list_foreach(item, opts) {
        opt = item->data;

        /* action level? */
        if(opt->lv != lv_action) {
            continue;
        }

        /* create new action */
        if(!action_config && !(action_config = tsip_action_create(tsip_atype_config,
                                               TSIP_ACTION_SET_NULL()))) {
            break;
        }

        switch(opt->type) {
        case opt_header: {
            if((param = tsk_params_parse_param(opt->value, tsk_strlen(opt->value)))) {
                tsip_action_set(action_config,
                                TSIP_ACTION_SET_HEADER(param->name, param->value),
                                TSIP_ACTION_SET_NULL());
                TSK_OBJECT_SAFE_FREE(param);
            }
            break;
        }
        case opt_payload: {
            tsip_action_set(action_config,
                            TSIP_ACTION_SET_PAYLOAD(opt->value, tsk_strlen(opt->value)),
                            TSIP_ACTION_SET_NULL());
            break;
        }
        default: {
            break;
        }
        }
    }

    return action_config;
}
OpenPOWER on IntegriCloud