00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include <stdio.h>
00053 #include <string.h>
00054 #include <stdlib.h>
00055
00056 #include "../../pvar.h"
00057 #include "../../parser/parse_sst.h"
00058 #include "../../parser/parse_supported.h"
00059 #include "../../mem/mem.h"
00060 #include "../../mem/shm_mem.h"
00061 #include "../../data_lump.h"
00062 #include "../../data_lump_rpl.h"
00063 #include "../../ut.h"
00064 #include "../../dprint.h"
00065 #include "../../sr_module.h"
00066 #include "../sl/sl_api.h"
00067
00068 #include "sst_handlers.h"
00069 #include "sst_mi.h"
00070
00071
00072
00073
00074
00075
00076 #ifdef USE_CONFIRM_CALLBACK
00077 #define DLOGMSG(msg) { \
00078 if (msg->first_line.type == SIP_REQUEST) { \
00079 LM_INFO("REQUEST: %.*s\n", \
00080 msg->first_line.u.request.method.len, \
00081 msg->first_line.u.request.method.s); \
00082 } \
00083 else { \
00084 LM_INFO("RESPONSE: %d %.*s\n", \
00085 msg->first_line.u.reply.statuscode, \
00086 msg->first_line.u.reply.reason.len, \
00087 msg->first_line.u.reply.reason.s); \
00088 } \
00089 }
00090 #endif
00091
00092
00093
00094
00095
00096
00097 extern struct dlg_binds *dlg_binds;
00098
00099
00100
00101
00102
00103 typedef struct sst_msg_info_st {
00104 int supported;
00105 unsigned int min_se;
00106 unsigned int se;
00107 enum sst_refresher refresher;
00108 } sst_msg_info_t;
00109
00110
00111
00112
00113
00114 #ifdef USE_CONFIRM_CALLBACK
00115 static void sst_dialog_confirmed_CB(struct dlg_cell* did, int type,
00116 struct dlg_cb_params * params);
00117 #endif
00118 static void sst_dialog_terminate_CB(struct dlg_cell* did, int type,
00119 struct dlg_cb_params * params);
00120 static void sst_dialog_request_within_CB(struct dlg_cell* did, int type,
00121 struct dlg_cb_params * params);
00122 static void sst_dialog_response_fwded_CB(struct dlg_cell* did, int type,
00123 struct dlg_cb_params * params);
00124 static int send_response(struct sip_msg *request, int code, str *reason,
00125 char *header, int header_len);
00126 static int append_header(struct sip_msg *msg, const char *header);
00127 static int remove_header(struct sip_msg *msg, const char *header);
00128 static int set_timeout_avp(struct sip_msg *msg, unsigned int value);
00129 static int parse_msg_for_sst_info(struct sip_msg *msg, sst_msg_info_t *minfo);
00130 static int send_reject(struct sip_msg *msg, unsigned int min_se);
00131 static void setup_dialog_callbacks(struct dlg_cell *did, sst_info_t *info);
00132
00133
00134
00135
00136
00137 extern struct sl_binds slb;
00138
00139
00140
00141
00142 static pv_spec_t *timeout_avp = 0;
00143
00144
00145
00146
00147 static unsigned int sst_min_se = 0;
00148
00149
00150
00151
00152 static unsigned int sst_reject = 1;
00153
00154
00155
00156
00157
00158 static int sst_flag = 0;
00159
00160
00161 static str sst_422_rpl = str_init("Session Timer Too Small");
00162
00163
00164
00165 #define SST_SE_BUF_SIZE 80
00166 static char sst_se_buf[SST_SE_BUF_SIZE];
00167 static inline int sst_build_minse_hdr(int seval, str *sehdr)
00168 {
00169 if(sehdr==NULL)
00170 return -1;
00171
00172 sehdr->len = snprintf(sst_se_buf, SST_SE_BUF_SIZE,
00173 "Min-SE: %d\r\n", seval);
00174 sehdr->s = sst_se_buf;
00175 return 0;
00176 }
00177 static inline int sst_build_se_hdr(int seval, str *sehdr)
00178 {
00179 if(sehdr==NULL)
00180 return -1;
00181
00182 sehdr->len = snprintf(sst_se_buf, SST_SE_BUF_SIZE,
00183 "Session-Expires: %d\r\n", seval);
00184 sehdr->s = sst_se_buf;
00185 return 0;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 void sst_handler_init(pv_spec_t *timeout_avp_p, unsigned int min_se,
00199 int flag, unsigned int reject)
00200 {
00201 timeout_avp = timeout_avp_p;
00202 sst_min_se = min_se;
00203 sst_flag = 1 << flag;
00204 sst_reject = reject;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 void sst_dialog_created_CB(struct dlg_cell *did, int type,
00249 struct dlg_cb_params * params)
00250 {
00251 sst_info_t *info = NULL;
00252 sst_msg_info_t minfo;
00253 struct sip_msg* msg = params->msg;
00254
00255 memset(&minfo, 0, sizeof(sst_msg_info_t));
00256
00257
00258
00259 if ((msg->flags & sst_flag) != sst_flag) {
00260 LM_DBG("SST flag was not set for this request\n");
00261 return;
00262 }
00263
00264
00265
00266
00267 if (msg->first_line.type != SIP_REQUEST ||
00268 msg->first_line.u.request.method_value != METHOD_INVITE) {
00269 LM_WARN("dialog create callback called with a non-INVITE request.\n");
00270 return;
00271 }
00272
00273
00274
00275
00276 if (parse_msg_for_sst_info(msg, &minfo)) {
00277 LM_ERR("failed to parse sst information\n");
00278 return;
00279 }
00280
00281 info = (sst_info_t *)shm_malloc(sizeof(sst_info_t));
00282 memset(info, 0, sizeof(sst_info_t));
00283 info->requester = (minfo.se?SST_UAC:SST_UNDF);
00284 info->supported = (minfo.supported?SST_UAC:SST_UNDF);
00285 info->interval = MAX(sst_min_se, 90);
00286
00287
00288 if (minfo.se != 0) {
00289
00290
00291
00292
00293 if (minfo.se < sst_min_se) {
00294
00295
00296
00297
00298
00299 if (!minfo.supported) {
00300
00301
00302
00303
00304 str msehdr;
00305 if (minfo.min_se) {
00306
00307
00308 remove_header(msg, "Min-SE");
00309 }
00310 info->interval = MAX(sst_min_se, minfo.min_se);
00311 sst_build_minse_hdr(info->interval, &msehdr);
00312 if (append_header(msg, msehdr.s)) {
00313 LM_ERR("Could not append modified Min-SE: header\n");
00314 }
00315 }
00316 else if (sst_reject) {
00317
00318 send_reject(msg, MAX(MAX(sst_min_se, minfo.min_se), 90));
00319 shm_free(info);
00320 return;
00321 }
00322 }
00323 else {
00324
00325 info->interval = minfo.se;
00326 }
00327 }
00328 else {
00329
00330
00331
00332 str msehdr;
00333
00334 info->interval = MAX(minfo.min_se, sst_min_se);
00335
00336 if (minfo.min_se && minfo.min_se < sst_min_se) {
00337 remove_header(msg, "Min-SE");
00338 sst_build_minse_hdr(info->interval, &msehdr);
00339 if (append_header(msg, msehdr.s)) {
00340 LM_ERR("failed to append modified Min-SE: header\n");
00341
00342 }
00343 }
00344
00345 info->requester = SST_PXY;
00346 sst_build_se_hdr(info->interval, &msehdr);
00347 if (append_header(msg, msehdr.s)) {
00348 LM_ERR("failed to append Session-Expires header to proxy "
00349 "requested SST.\n");
00350 shm_free(info);
00351 return;
00352 }
00353 }
00354 setup_dialog_callbacks(did, info);
00355 set_timeout_avp(msg, info->interval);
00356 return;
00357 }
00358
00359 #ifdef USE_CONFIRM_CALLBACK
00360
00361
00362
00363 static void sst_dialog_confirmed_CB(struct dlg_cell *did, int type,
00364 struct dlg_cb_params * params)
00365 {
00366 struct sip_msg* msg = params->msg;
00367
00368 LM_DBG("confirmed dialog CB %p\n", did);
00369 DLOGMSG(msg);
00370 }
00371 #endif
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 static void sst_dialog_terminate_CB(struct dlg_cell* did, int type,
00383 struct dlg_cb_params * params)
00384 {
00385 switch (type) {
00386 case DLGCB_FAILED:
00387 LM_DBG("DID %p failed (canceled). "
00388 "Terminating session.\n", did);
00389 break;
00390 case DLGCB_EXPIRED:
00391
00392
00393
00394 LM_DBG("Terminating session.\n");
00395 break;
00396 default:
00397 LM_DBG("Terminating DID %p session\n", did);
00398 break;
00399 }
00400
00401
00402
00403 if (*(params->param)) {
00404 LM_DBG("freeing the sst_info_t from dialog %p\n", did);
00405 shm_free(*(params->param));
00406 *(params->param) = NULL;
00407 }
00408 return;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 static void sst_dialog_request_within_CB(struct dlg_cell* did, int type,
00428 struct dlg_cb_params * params)
00429 {
00430 sst_info_t *info = (sst_info_t *)*(params->param);
00431 sst_msg_info_t minfo = {0,0,0,0};
00432 struct sip_msg* msg = params->msg;
00433
00434 if (msg->first_line.type == SIP_REQUEST) {
00435 if ((msg->first_line.u.request.method_value == METHOD_INVITE ||
00436 msg->first_line.u.request.method_value == METHOD_UPDATE)) {
00437
00438 LM_DBG("Update by a REQUEST. %.*s\n",
00439 msg->first_line.u.request.method.len,
00440 msg->first_line.u.request.method.s);
00441 if (parse_msg_for_sst_info(msg, &minfo)) {
00442 LM_ERR("failed to parse sst information\n");
00443 return;
00444 }
00445
00446 set_timeout_avp(msg, minfo.se);
00447 info->interval = minfo.se;
00448 }
00449 else if (msg->first_line.u.request.method_value == METHOD_PRACK) {
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 LM_DBG("PRACK workaround applied!\n");
00462 set_timeout_avp(msg, info->interval);
00463 }
00464 }
00465 else if (msg->first_line.type == SIP_REPLY) {
00466 if ((msg->first_line.u.reply.statuscode > 199 &&
00467 msg->first_line.u.reply.statuscode < 300)) {
00468
00469
00470
00471
00472 LM_DBG("Update by a REPLY %d %.*s\n",
00473 msg->first_line.u.reply.statuscode,
00474 msg->first_line.u.reply.reason.len,
00475 msg->first_line.u.reply.reason.s);
00476 if (parse_msg_for_sst_info(msg, &minfo)) {
00477 LM_ERR("failed to parse sst information\n");
00478 return;
00479 }
00480 set_timeout_avp(msg, minfo.se);
00481 info->interval = minfo.se;
00482 }
00483 }
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 static void sst_dialog_response_fwded_CB(struct dlg_cell* did, int type,
00496 struct dlg_cb_params * params)
00497 {
00498 struct sip_msg* msg = params->msg;
00499
00500
00501
00502
00503
00504
00505 if (msg->first_line.type == SIP_REPLY) {
00506 sst_msg_info_t minfo = {0,0,0,0};
00507 sst_info_t *info = (sst_info_t *)*(params->param);
00508
00509 LM_DBG("Dialog seen REPLY %d %.*s\n",
00510 msg->first_line.u.reply.statuscode,
00511 msg->first_line.u.reply.reason.len,
00512 msg->first_line.u.reply.reason.s);
00513
00514
00515
00516
00517
00518
00519 if (msg->first_line.u.reply.statuscode == 422) {
00520 if (parse_msg_for_sst_info(msg, &minfo)) {
00521 LM_ERR("failed to parse sst information for the 422 reply\n");
00522 return;
00523 }
00524
00525 info->interval = MAX(info->interval, minfo.min_se);
00526 return;
00527 }
00528
00529
00530
00531
00532
00533 if (!msg->cseq && ((parse_headers(msg, HDR_CSEQ_F, 0) == -1) || !msg->cseq)) {
00534 LM_ERR("failed to parse CSeq\n");
00535 return;
00536 }
00537
00538
00539 if (msg->first_line.u.reply.statuscode > 199 &&
00540 msg->first_line.u.reply.statuscode < 300 &&
00541 (get_cseq(msg)->method_id == METHOD_INVITE ||
00542 get_cseq(msg)->method_id == METHOD_UPDATE)) {
00543 if (parse_msg_for_sst_info(msg, &minfo)) {
00544 LM_ERR("failed to parse sst information for the 2XX reply\n");
00545 return;
00546 }
00547
00548 if (minfo.se != 0) {
00549 if (set_timeout_avp(msg, info->interval)) {
00550 LM_ERR("failed to set the timeout AVP\n");
00551 return;
00552 }
00553 }
00554 else {
00555
00556 if (info->requester == SST_PXY || info->supported == SST_UAC) {
00557 str sehdr;
00558
00559 LM_DBG("appending the Session-Expires: header to the 2XX reply."
00560 " UAC will deal with it.\n");
00561
00562
00563
00564
00565
00566 sst_build_se_hdr(info->interval, &sehdr);
00567 if (append_header(msg, sehdr.s)) {
00568 LM_ERR("failed to append Session-Expires header\n");
00569 return;
00570 }
00571
00572 if (set_timeout_avp(msg, info->interval)) {
00573 return;
00574 }
00575 }
00576 else {
00577
00578
00579 LM_DBG("UAC and UAS do not support timers!"
00580 " No session timers for this session.\n");
00581 }
00582 }
00583 }
00584 }
00585 }
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610 int sst_check_min(struct sip_msg *msg, char *flag, char *str2)
00611 {
00612 enum parse_sst_result result;
00613 struct session_expires se = {0,0};
00614 unsigned minse = 0;
00615
00616
00617
00618
00619
00620 if (msg->first_line.type == SIP_REQUEST &&
00621 msg->first_line.u.request.method_value == METHOD_INVITE) {
00622
00623
00624
00625
00626
00627
00628 if ((result = parse_session_expires(msg, &se)) != parse_sst_success) {
00629 if (result != parse_sst_header_not_found) {
00630 LM_ERR("failed to parse Session-Expires headers.\n");
00631 return 0;
00632 }
00633
00634 LM_DBG("No Session-Expires header found. retuning false (-1)\n");
00635
00636
00637
00638 return -1;
00639 }
00640
00641
00642
00643
00644 if ((result = parse_min_se(msg, &minse)) != parse_sst_success) {
00645 if (result != parse_sst_header_not_found) {
00646
00647
00648
00649
00650 LM_ERR("failed to parse MIN-SE header.\n");
00651 return -1;
00652 }
00653
00654
00655
00656
00657 LM_DBG("No MIN-SE header found.\n");
00658 minse = 90;
00659 }
00660
00661 LM_DBG("Session-Expires: %d; MIN-SE: %d\n", se.interval, minse);
00662
00663
00664
00665
00666
00667
00668 if (sst_min_se < MIN(minse, se.interval)) {
00669
00670
00671
00672
00673 if (flag) {
00674 str msehdr;
00675 sst_build_minse_hdr(sst_min_se, &msehdr);
00676 LM_DBG("Sending 422: %.*s\n", msehdr.len, msehdr.s);
00677 if (send_response(msg, 422, &sst_422_rpl, msehdr.s, msehdr.len)){
00678 LM_ERR("Error sending 422 reply.\n");
00679 }
00680 }
00681 LM_DBG("Done returning true (1)\n");
00682 return 1;
00683 }
00684 }
00685 LM_DBG("Done returning false (-1)\n");
00686
00687
00688
00689 return -1;
00690 }
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705 static int send_response(struct sip_msg *request, int code, str *reason,
00706 char *header, int header_len)
00707 {
00708
00709 if (slb.send_reply != 0) {
00710
00711 if ((header) && (header_len)) {
00712 if (add_lump_rpl(request, header, header_len, LUMP_RPL_HDR) == 0) {
00713
00714 LM_ERR("unable to append header.\n");
00715 return -1;
00716 }
00717 }
00718
00719 if (slb.send_reply(request, code, reason) < 0) {
00720 LM_ERR("Unable to sent reply.\n");
00721 return -1;
00722 }
00723 }
00724 else {
00725 return -1;
00726 }
00727 return(0);
00728 }
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738 static int append_header(struct sip_msg *msg, const char *header)
00739 {
00740 struct lump* anchor = NULL;
00741 char *s = NULL;
00742 int len = 0;
00743
00744 LM_DBG("Appending header: %s", header);
00745
00746 if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
00747 LM_ERR("failed to parse headers in message.\n");
00748 return(1);
00749 }
00750
00751 if ((anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0)) == 0) {
00752 LM_ERR("failed to get anchor to append header\n");
00753 return(1);
00754 }
00755 len = strlen(header);
00756 if ((s = (char *)pkg_malloc(len)) == 0) {
00757 LM_ERR("No more pkg memory. (size requested = %d)\n", len);
00758 return(1);
00759 }
00760 memcpy(s, header, len);
00761 if (insert_new_lump_before(anchor, s, len, 0) == 0) {
00762 LM_ERR("failed to insert lump\n");
00763 pkg_free(s);
00764 return(1);
00765 }
00766 LM_DBG("Done appending header successfully.\n");
00767 return(0);
00768 }
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779 static int remove_header(struct sip_msg *msg, const char *header)
00780 {
00781 struct lump* anchor = NULL;
00782 struct hdr_field *hf = NULL;
00783 int cnt = 0;
00784 int len = strlen(header);
00785
00786 if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
00787 LM_ERR("failed to parse headers in message.\n");
00788 return(-1);
00789 }
00790
00791 for (hf = msg->headers; hf; hf = hf->next) {
00792 if (hf->name.len != len) {
00793 continue;
00794 }
00795 if (strncasecmp(hf->name.s, header, hf->name.len) != 0) {
00796 continue;
00797 }
00798
00799 anchor = del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
00800 if (anchor == 0) {
00801 LM_ERR("no more pkg memory\n");
00802 return -1;
00803 }
00804 cnt++;
00805 }
00806 return cnt;
00807 }
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818 static int set_timeout_avp(struct sip_msg *msg, unsigned int value)
00819 {
00820 int rtn = -1;
00821 pv_value_t pv_val;
00822 int result = 0;
00823
00824
00825 if (timeout_avp) {
00826 if ((result = pv_get_spec_value(msg, timeout_avp, &pv_val)) == 0) {
00827
00828 if (pv_val.flags & PV_VAL_INT && pv_val.ri == value) {
00829
00830 LM_DBG("Current timeout value already set to %d\n",
00831 value);
00832 rtn = 0;
00833 } else {
00834
00835 pv_val.flags = PV_VAL_INT;
00836 pv_val.ri = value;
00837 if (timeout_avp->setf(msg,&timeout_avp->pvp,EQ_T,&pv_val)!=0) {
00838 LM_ERR("failed to set new dialog timeout value\n");
00839 } else {
00840 rtn = 0;
00841 }
00842 }
00843 }
00844 else {
00845 LM_ERR("SST not reset. get avp result is %d\n", result);
00846 }
00847 }
00848 else {
00849 LM_ERR("SST needs to know the name of the dialog timeout AVP!\n");
00850 }
00851 return(rtn);
00852 }
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 static int parse_msg_for_sst_info(struct sip_msg *msg, sst_msg_info_t *minfo)
00864 {
00865 int rtn = 0;
00866 struct session_expires se = {0,0};
00867
00868 if (!msg || !minfo) {
00869 return (-1);
00870 }
00871
00872
00873
00874
00875 minfo->supported = 0;
00876 minfo->se = 0;
00877 minfo->refresher = sst_refresher_unspecified;
00878 minfo->min_se = 0;
00879
00880
00881
00882
00883
00884
00885 if ((rtn = parse_supported(msg)) == 0) {
00886 if ((((struct supported_body*)msg->supported->parsed)->supported_all
00887 & F_SUPPORTED_TIMER)) {
00888 minfo->supported = 1;
00889 }
00890 }
00891
00892
00893
00894 minfo->min_se = 0;
00895 if ((rtn = parse_min_se(msg, &minfo->min_se)) != parse_sst_success) {
00896 minfo->min_se = 0;
00897 }
00898 minfo->se = 0;
00899 if ((rtn = parse_session_expires(msg, &se)) == parse_sst_success) {
00900 minfo->se = se.interval;
00901 minfo->refresher = se.refresher;
00902 }
00903 return(0);
00904 }
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914 static int send_reject(struct sip_msg *msg, unsigned int min_se)
00915 {
00916 str msehdr;
00917
00918 sst_build_minse_hdr(min_se, &msehdr);
00919
00920 if (send_response(msg, 422, &sst_422_rpl, msehdr.s, msehdr.len)) {
00921 LM_ERR("Error sending 422 reply.\n");
00922 return(-1);
00923 }
00924 LM_DBG("Send reject reply 422 with Min-SE: %d\n", min_se);
00925 return(0);
00926 }
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936 static void setup_dialog_callbacks(struct dlg_cell *did, sst_info_t *info)
00937 {
00938
00939
00940
00941
00942 #ifdef USE_CONFIRM_CALLBACK
00943 LM_DBG("Adding callback DLGCB_CONFIRMED\n");
00944 dlg_binds->register_dlgcb(did,
00945 DLGCB_CONFIRMED, sst_dialog_confirmed_CB, info, NULL);
00946 #endif
00947
00948 LM_DBG("Adding callback "
00949 "DLGCB_FAILED|DLGCB_TERMINATED|DLGCB_EXPIRED\n");
00950 dlg_binds->register_dlgcb(did,
00951 DLGCB_FAILED|DLGCB_TERMINATED|DLGCB_EXPIRED,
00952 sst_dialog_terminate_CB, (void *)info, NULL);
00953 LM_DBG("Adding callback DLGCB_REQ_WITHIN\n");
00954
00955 dlg_binds->register_dlgcb(did, DLGCB_REQ_WITHIN,
00956 sst_dialog_request_within_CB, info, NULL);
00957
00958
00959
00960
00961
00962
00963 LM_DBG("Adding callback DLGCB_RESPONSE_FWDED\n");
00964 dlg_binds->register_dlgcb(did, DLGCB_RESPONSE_FWDED,
00965 sst_dialog_response_fwded_CB, info, NULL);
00966
00967 LM_DBG("Adding mi handler\n");
00968 dlg_binds->register_dlgcb(did, DLGCB_MI_CONTEXT,
00969 sst_dialog_mi_context_CB, info, NULL);
00970 }