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
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 #include "asterisk.h"
00211
00212 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 426599 $")
00213
00214 #include <signal.h>
00215 #include <sys/signal.h>
00216 #include <regex.h>
00217 #include <inttypes.h>
00218
00219 #include "asterisk/network.h"
00220 #include "asterisk/paths.h"
00221 #include "asterisk/lock.h"
00222 #include "asterisk/config.h"
00223 #include "asterisk/module.h"
00224 #include "asterisk/pbx.h"
00225 #include "asterisk/sched.h"
00226 #include "asterisk/io.h"
00227 #include "asterisk/rtp_engine.h"
00228 #include "asterisk/udptl.h"
00229 #include "asterisk/acl.h"
00230 #include "asterisk/manager.h"
00231 #include "asterisk/callerid.h"
00232 #include "asterisk/cli.h"
00233 #include "asterisk/musiconhold.h"
00234 #include "asterisk/dsp.h"
00235 #include "asterisk/features.h"
00236 #include "asterisk/srv.h"
00237 #include "asterisk/astdb.h"
00238 #include "asterisk/causes.h"
00239 #include "asterisk/utils.h"
00240 #include "asterisk/file.h"
00241 #include "asterisk/astobj2.h"
00242 #include "asterisk/dnsmgr.h"
00243 #include "asterisk/devicestate.h"
00244 #include "asterisk/monitor.h"
00245 #include "asterisk/netsock2.h"
00246 #include "asterisk/localtime.h"
00247 #include "asterisk/abstract_jb.h"
00248 #include "asterisk/threadstorage.h"
00249 #include "asterisk/translate.h"
00250 #include "asterisk/ast_version.h"
00251 #include "asterisk/event.h"
00252 #include "asterisk/cel.h"
00253 #include "asterisk/data.h"
00254 #include "asterisk/aoc.h"
00255 #include "sip/include/sip.h"
00256 #include "sip/include/globals.h"
00257 #include "sip/include/config_parser.h"
00258 #include "sip/include/reqresp_parser.h"
00259 #include "sip/include/sip_utils.h"
00260 #include "sip/include/srtp.h"
00261 #include "sip/include/sdp_crypto.h"
00262 #include "asterisk/ccss.h"
00263 #include "asterisk/xml.h"
00264 #include "sip/include/dialog.h"
00265 #include "sip/include/dialplan_functions.h"
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 static int min_expiry = DEFAULT_MIN_EXPIRY;
00547 static int max_expiry = DEFAULT_MAX_EXPIRY;
00548 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00549 static int mwi_expiry = DEFAULT_MWI_EXPIRY;
00550
00551 static int unauth_sessions = 0;
00552 static int authlimit = DEFAULT_AUTHLIMIT;
00553 static int authtimeout = DEFAULT_AUTHTIMEOUT;
00554
00555
00556
00557 static struct ast_jb_conf default_jbconf =
00558 {
00559 .flags = 0,
00560 .max_size = 200,
00561 .resync_threshold = 1000,
00562 .impl = "fixed",
00563 .target_extra = 40,
00564 };
00565 static struct ast_jb_conf global_jbconf;
00566
00567 static const char config[] = "sip.conf";
00568 static const char notify_config[] = "sip_notify.conf";
00569
00570
00571
00572 static const struct invstate2stringtable {
00573 const enum invitestates state;
00574 const char *desc;
00575 } invitestate2string[] = {
00576 {INV_NONE, "None" },
00577 {INV_CALLING, "Calling (Trying)"},
00578 {INV_PROCEEDING, "Proceeding "},
00579 {INV_EARLY_MEDIA, "Early media"},
00580 {INV_COMPLETED, "Completed (done)"},
00581 {INV_CONFIRMED, "Confirmed (up)"},
00582 {INV_TERMINATED, "Done"},
00583 {INV_CANCELLED, "Cancelled"}
00584 };
00585
00586
00587
00588
00589
00590
00591 static const struct cfsubscription_types {
00592 enum subscriptiontype type;
00593 const char * const event;
00594 const char * const mediatype;
00595 const char * const text;
00596 } subscription_types[] = {
00597 { NONE, "-", "unknown", "unknown" },
00598
00599 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00600 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00601 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00602 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00603 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00604 };
00605
00606
00607
00608
00609
00610
00611 static const struct cfsip_methods {
00612 enum sipmethod id;
00613 int need_rtp;
00614 char * const text;
00615 enum can_create_dialog can_create;
00616 } sip_methods[] = {
00617 { SIP_UNKNOWN, RTP, "-UNKNOWN-",CAN_CREATE_DIALOG },
00618 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00619 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00620 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00621 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00622 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00623 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00624 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00625 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00626 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00627 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE",CAN_CREATE_DIALOG },
00628 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00629 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00630 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00631 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00632 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG },
00633 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00634 };
00635
00636
00637
00638
00639
00640
00641
00642
00643 static const struct sip_reasons {
00644 enum AST_REDIRECTING_REASON code;
00645 char * const text;
00646 } sip_reason_table[] = {
00647 { AST_REDIRECTING_REASON_UNKNOWN, "unknown" },
00648 { AST_REDIRECTING_REASON_USER_BUSY, "user-busy" },
00649 { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer" },
00650 { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable" },
00651 { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional" },
00652 { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day" },
00653 { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb" },
00654 { AST_REDIRECTING_REASON_DEFLECTION, "deflection" },
00655 { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me" },
00656 { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service" },
00657 { AST_REDIRECTING_REASON_AWAY, "away" },
00658 { AST_REDIRECTING_REASON_CALL_FWD_DTE, "unknown"}
00659 };
00660
00661
00662
00663
00664
00665
00666
00667 static char default_language[MAX_LANGUAGE];
00668 static char default_callerid[AST_MAX_EXTENSION];
00669 static char default_mwi_from[80];
00670 static char default_fromdomain[AST_MAX_EXTENSION];
00671 static int default_fromdomainport;
00672 static char default_notifymime[AST_MAX_EXTENSION];
00673 static char default_vmexten[AST_MAX_EXTENSION];
00674 static int default_qualify;
00675 static char default_mohinterpret[MAX_MUSICCLASS];
00676 static char default_mohsuggest[MAX_MUSICCLASS];
00677
00678 static char default_parkinglot[AST_MAX_CONTEXT];
00679 static char default_engine[256];
00680 static int default_maxcallbitrate;
00681 static struct ast_codec_pref default_prefs;
00682 static unsigned int default_transports;
00683 static unsigned int default_primary_transport;
00684
00685
00686 static struct sip_settings sip_cfg;
00687
00688
00689
00690 #define SIP_PEDANTIC_DECODE(str) \
00691 if (sip_cfg.pedanticsipchecking && !ast_strlen_zero(str)) { \
00692 ast_uri_decode(str); \
00693 } \
00694
00695 static unsigned int chan_idx;
00696 static int global_match_auth_username;
00697
00698 static int global_relaxdtmf;
00699 static int global_prematuremediafilter;
00700 static int global_rtptimeout;
00701 static int global_rtpholdtimeout;
00702 static int global_rtpkeepalive;
00703 static int global_reg_timeout;
00704 static int global_regattempts_max;
00705 static int global_reg_retry_403;
00706 static int global_shrinkcallerid;
00707 static int global_callcounter;
00708
00709
00710 static unsigned int global_tos_sip;
00711 static unsigned int global_tos_audio;
00712 static unsigned int global_tos_video;
00713 static unsigned int global_tos_text;
00714 static unsigned int global_cos_sip;
00715 static unsigned int global_cos_audio;
00716 static unsigned int global_cos_video;
00717 static unsigned int global_cos_text;
00718 static unsigned int recordhistory;
00719 static unsigned int dumphistory;
00720 static char global_useragent[AST_MAX_EXTENSION];
00721 static char global_sdpsession[AST_MAX_EXTENSION];
00722 static char global_sdpowner[AST_MAX_EXTENSION];
00723 static int global_authfailureevents;
00724 static int global_t1;
00725 static int global_t1min;
00726 static int global_timer_b;
00727 static unsigned int global_autoframing;
00728 static int global_qualifyfreq;
00729 static int global_qualify_gap;
00730 static int global_qualify_peers;
00731
00732 static enum st_mode global_st_mode;
00733 static enum st_refresher_param global_st_refresher;
00734 static int global_min_se;
00735 static int global_max_se;
00736
00737 static int global_store_sip_cause;
00738
00739 static int global_dynamic_exclude_static = 0;
00740
00741
00742
00743
00744
00745
00746
00747
00748 static int can_parse_xml;
00749
00750
00751
00752
00753 static int speerobjs = 0;
00754 static int rpeerobjs = 0;
00755 static int apeerobjs = 0;
00756 static int regobjs = 0;
00757
00758
00759 static struct ast_flags global_flags[3] = {{0}};
00760 static unsigned int global_t38_maxdatagram;
00761
00762 static struct ast_event_sub *network_change_event_subscription;
00763 static int network_change_event_sched_id = -1;
00764
00765 static char used_context[AST_MAX_CONTEXT];
00766
00767 AST_MUTEX_DEFINE_STATIC(netlock);
00768
00769
00770
00771 AST_MUTEX_DEFINE_STATIC(monlock);
00772
00773 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00774
00775
00776
00777 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00778
00779 static int sip_reloading = FALSE;
00780 static enum channelreloadreason sip_reloadreason;
00781
00782 struct sched_context *sched;
00783 static struct io_context *io;
00784 static int *sipsock_read_id;
00785 struct sip_pkt;
00786 static AST_LIST_HEAD_STATIC(domain_list, domain);
00787
00788 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00789
00790 static enum sip_debug_e sipdebug;
00791
00792
00793
00794
00795
00796 static int sipdebug_text;
00797
00798 static const struct _map_x_s referstatusstrings[] = {
00799 { REFER_IDLE, "<none>" },
00800 { REFER_SENT, "Request sent" },
00801 { REFER_RECEIVED, "Request received" },
00802 { REFER_CONFIRMED, "Confirmed" },
00803 { REFER_ACCEPTED, "Accepted" },
00804 { REFER_RINGING, "Target ringing" },
00805 { REFER_200OK, "Done" },
00806 { REFER_FAILED, "Failed" },
00807 { REFER_NOAUTH, "Failed - auth failure" },
00808 { -1, NULL}
00809 };
00810
00811
00812 #ifdef LOW_MEMORY
00813 static const int HASH_PEER_SIZE = 17;
00814 static const int HASH_DIALOG_SIZE = 17;
00815 #else
00816 static const int HASH_PEER_SIZE = 563;
00817 static const int HASH_DIALOG_SIZE = 563;
00818 #endif
00819
00820 static const struct {
00821 enum ast_cc_service_type service;
00822 const char *service_string;
00823 } sip_cc_service_map [] = {
00824 [AST_CC_NONE] = { AST_CC_NONE, "" },
00825 [AST_CC_CCBS] = { AST_CC_CCBS, "BS" },
00826 [AST_CC_CCNR] = { AST_CC_CCNR, "NR" },
00827 [AST_CC_CCNL] = { AST_CC_CCNL, "NL" },
00828 };
00829
00830 static enum ast_cc_service_type service_string_to_service_type(const char * const service_string)
00831 {
00832 enum ast_cc_service_type service;
00833 for (service = AST_CC_CCBS; service <= AST_CC_CCNL; ++service) {
00834 if (!strcasecmp(service_string, sip_cc_service_map[service].service_string)) {
00835 return service;
00836 }
00837 }
00838 return AST_CC_NONE;
00839 }
00840
00841 static const struct {
00842 enum sip_cc_notify_state state;
00843 const char *state_string;
00844 } sip_cc_notify_state_map [] = {
00845 [CC_QUEUED] = {CC_QUEUED, "cc-state: queued"},
00846 [CC_READY] = {CC_READY, "cc-state: ready"},
00847 };
00848
00849 AST_LIST_HEAD_STATIC(epa_static_data_list, epa_backend);
00850
00851 static int sip_epa_register(const struct epa_static_data *static_data)
00852 {
00853 struct epa_backend *backend = ast_calloc(1, sizeof(*backend));
00854
00855 if (!backend) {
00856 return -1;
00857 }
00858
00859 backend->static_data = static_data;
00860
00861 AST_LIST_LOCK(&epa_static_data_list);
00862 AST_LIST_INSERT_TAIL(&epa_static_data_list, backend, next);
00863 AST_LIST_UNLOCK(&epa_static_data_list);
00864 return 0;
00865 }
00866
00867 static void sip_epa_unregister_all(void)
00868 {
00869 struct epa_backend *backend;
00870
00871 AST_LIST_LOCK(&epa_static_data_list);
00872 while ((backend = AST_LIST_REMOVE_HEAD(&epa_static_data_list, next))) {
00873 ast_free(backend);
00874 }
00875 AST_LIST_UNLOCK(&epa_static_data_list);
00876 }
00877
00878 static void cc_handle_publish_error(struct sip_pvt *pvt, const int resp, struct sip_request *req, struct sip_epa_entry *epa_entry);
00879
00880 static void cc_epa_destructor(void *data)
00881 {
00882 struct sip_epa_entry *epa_entry = data;
00883 struct cc_epa_entry *cc_entry = epa_entry->instance_data;
00884 ast_free(cc_entry);
00885 }
00886
00887 static const struct epa_static_data cc_epa_static_data = {
00888 .event = CALL_COMPLETION,
00889 .name = "call-completion",
00890 .handle_error = cc_handle_publish_error,
00891 .destructor = cc_epa_destructor,
00892 };
00893
00894 static const struct epa_static_data *find_static_data(const char * const event_package)
00895 {
00896 const struct epa_backend *backend = NULL;
00897
00898 AST_LIST_LOCK(&epa_static_data_list);
00899 AST_LIST_TRAVERSE(&epa_static_data_list, backend, next) {
00900 if (!strcmp(backend->static_data->name, event_package)) {
00901 break;
00902 }
00903 }
00904 AST_LIST_UNLOCK(&epa_static_data_list);
00905 return backend ? backend->static_data : NULL;
00906 }
00907
00908 static struct sip_epa_entry *create_epa_entry (const char * const event_package, const char * const destination)
00909 {
00910 struct sip_epa_entry *epa_entry;
00911 const struct epa_static_data *static_data;
00912
00913 if (!(static_data = find_static_data(event_package))) {
00914 return NULL;
00915 }
00916
00917 if (!(epa_entry = ao2_t_alloc(sizeof(*epa_entry), static_data->destructor, "Allocate new EPA entry"))) {
00918 return NULL;
00919 }
00920
00921 epa_entry->static_data = static_data;
00922 ast_copy_string(epa_entry->destination, destination, sizeof(epa_entry->destination));
00923 return epa_entry;
00924 }
00925
00926
00927
00928
00929 static int esc_etag_counter;
00930 static const int DEFAULT_PUBLISH_EXPIRES = 3600;
00931
00932 #ifdef HAVE_LIBXML2
00933 static int cc_esc_publish_handler(struct sip_pvt *pvt, struct sip_request *req, struct event_state_compositor *esc, struct sip_esc_entry *esc_entry);
00934
00935 static const struct sip_esc_publish_callbacks cc_esc_publish_callbacks = {
00936 .initial_handler = cc_esc_publish_handler,
00937 .modify_handler = cc_esc_publish_handler,
00938 };
00939 #endif
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953 static struct event_state_compositor {
00954 enum subscriptiontype event;
00955 const char * name;
00956 const struct sip_esc_publish_callbacks *callbacks;
00957 struct ao2_container *compositor;
00958 } event_state_compositors [] = {
00959 #ifdef HAVE_LIBXML2
00960 {CALL_COMPLETION, "call-completion", &cc_esc_publish_callbacks},
00961 #endif
00962 };
00963
00964 static const int ESC_MAX_BUCKETS = 37;
00965
00966 static void esc_entry_destructor(void *obj)
00967 {
00968 struct sip_esc_entry *esc_entry = obj;
00969 if (esc_entry->sched_id > -1) {
00970 AST_SCHED_DEL(sched, esc_entry->sched_id);
00971 }
00972 }
00973
00974 static int esc_hash_fn(const void *obj, const int flags)
00975 {
00976 const struct sip_esc_entry *entry = obj;
00977 return ast_str_hash(entry->entity_tag);
00978 }
00979
00980 static int esc_cmp_fn(void *obj, void *arg, int flags)
00981 {
00982 struct sip_esc_entry *entry1 = obj;
00983 struct sip_esc_entry *entry2 = arg;
00984
00985 return (!strcmp(entry1->entity_tag, entry2->entity_tag)) ? (CMP_MATCH | CMP_STOP) : 0;
00986 }
00987
00988 static struct event_state_compositor *get_esc(const char * const event_package) {
00989 int i;
00990 for (i = 0; i < ARRAY_LEN(event_state_compositors); i++) {
00991 if (!strcasecmp(event_package, event_state_compositors[i].name)) {
00992 return &event_state_compositors[i];
00993 }
00994 }
00995 return NULL;
00996 }
00997
00998 static struct sip_esc_entry *get_esc_entry(const char * entity_tag, struct event_state_compositor *esc) {
00999 struct sip_esc_entry *entry;
01000 struct sip_esc_entry finder;
01001
01002 ast_copy_string(finder.entity_tag, entity_tag, sizeof(finder.entity_tag));
01003
01004 entry = ao2_find(esc->compositor, &finder, OBJ_POINTER);
01005
01006 return entry;
01007 }
01008
01009 static int publish_expire(const void *data)
01010 {
01011 struct sip_esc_entry *esc_entry = (struct sip_esc_entry *) data;
01012 struct event_state_compositor *esc = get_esc(esc_entry->event);
01013
01014 ast_assert(esc != NULL);
01015
01016 ao2_unlink(esc->compositor, esc_entry);
01017 ao2_ref(esc_entry, -1);
01018 return 0;
01019 }
01020
01021 static void create_new_sip_etag(struct sip_esc_entry *esc_entry, int is_linked)
01022 {
01023 int new_etag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
01024 struct event_state_compositor *esc = get_esc(esc_entry->event);
01025
01026 ast_assert(esc != NULL);
01027 if (is_linked) {
01028 ao2_unlink(esc->compositor, esc_entry);
01029 }
01030 snprintf(esc_entry->entity_tag, sizeof(esc_entry->entity_tag), "%d", new_etag);
01031 ao2_link(esc->compositor, esc_entry);
01032 }
01033
01034 static struct sip_esc_entry *create_esc_entry(struct event_state_compositor *esc, struct sip_request *req, const int expires)
01035 {
01036 struct sip_esc_entry *esc_entry;
01037 int expires_ms;
01038
01039 if (!(esc_entry = ao2_alloc(sizeof(*esc_entry), esc_entry_destructor))) {
01040 return NULL;
01041 }
01042
01043 esc_entry->event = esc->name;
01044
01045 expires_ms = expires * 1000;
01046
01047 ao2_ref(esc_entry, +1);
01048 esc_entry->sched_id = ast_sched_add(sched, expires_ms, publish_expire, esc_entry);
01049
01050
01051 create_new_sip_etag(esc_entry, 0);
01052
01053 return esc_entry;
01054 }
01055
01056 static int initialize_escs(void)
01057 {
01058 int i, res = 0;
01059 for (i = 0; i < ARRAY_LEN(event_state_compositors); i++) {
01060 if (!((event_state_compositors[i].compositor) =
01061 ao2_container_alloc(ESC_MAX_BUCKETS, esc_hash_fn, esc_cmp_fn))) {
01062 res = -1;
01063 }
01064 }
01065 return res;
01066 }
01067
01068 static void destroy_escs(void)
01069 {
01070 int i;
01071 for (i = 0; i < ARRAY_LEN(event_state_compositors); i++) {
01072 ao2_ref(event_state_compositors[i].compositor, -1);
01073 }
01074 }
01075
01076
01077
01078
01079
01080 struct ao2_container *dialogs_to_destroy;
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090 static struct ao2_container *dialogs;
01091 #define sip_pvt_lock(x) ao2_lock(x)
01092 #define sip_pvt_trylock(x) ao2_trylock(x)
01093 #define sip_pvt_unlock(x) ao2_unlock(x)
01094
01095
01096 static struct ao2_container *threadt;
01097
01098
01099 static struct ao2_container *peers;
01100 static struct ao2_container *peers_by_ip;
01101
01102
01103 static struct sip_peer *bogus_peer;
01104
01105 #define BOGUS_PEER_MD5SECRET "intentionally_invalid_md5_string"
01106
01107
01108 static struct ast_register_list {
01109 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01110 int recheck;
01111 } regl;
01112
01113
01114 static struct ast_subscription_mwi_list {
01115 ASTOBJ_CONTAINER_COMPONENTS(struct sip_subscription_mwi);
01116 } submwil;
01117 static int temp_pvt_init(void *);
01118 static void temp_pvt_cleanup(void *);
01119
01120
01121 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01122
01123
01124 static struct sip_auth_container *authl = NULL;
01125
01126 AST_MUTEX_DEFINE_STATIC(authl_lock);
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144 static int sipsock = -1;
01145
01146 struct ast_sockaddr bindaddr;
01147
01148
01149
01150
01151
01152
01153
01154 static struct ast_sockaddr internip;
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171 static struct ast_sockaddr externaddr;
01172 static struct ast_sockaddr media_address;
01173
01174 static char externhost[MAXHOSTNAMELEN];
01175 static time_t externexpire;
01176 static int externrefresh = 10;
01177 static uint16_t externtcpport;
01178 static uint16_t externtlsport;
01179
01180
01181
01182
01183
01184
01185
01186 static struct ast_ha *localaddr;
01187
01188 static int ourport_tcp;
01189 static int ourport_tls;
01190 static struct ast_sockaddr debugaddr;
01191
01192 static struct ast_config *notify_types = NULL;
01193
01194
01195
01196 #define UNLINK(element, head, prev) do { \
01197 if (prev) \
01198 (prev)->next = (element)->next; \
01199 else \
01200 (head) = (element)->next; \
01201 } while (0)
01202
01203 struct show_peers_context;
01204
01205
01206
01207
01208
01209
01210 static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
01211 static int sip_devicestate(void *data);
01212 static int sip_sendtext(struct ast_channel *ast, const char *text);
01213 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01214 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen);
01215 static int sip_hangup(struct ast_channel *ast);
01216 static int sip_answer(struct ast_channel *ast);
01217 static struct ast_frame *sip_read(struct ast_channel *ast);
01218 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01219 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01220 static int sip_transfer(struct ast_channel *ast, const char *dest);
01221 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01222 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01223 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01224 static int sip_setoption(struct ast_channel *chan, int option, void *data, int datalen);
01225 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen);
01226 static const char *sip_get_callid(struct ast_channel *chan);
01227
01228 static int handle_request_do(struct sip_request *req, struct ast_sockaddr *addr);
01229 static int sip_standard_port(enum sip_transport type, int port);
01230 static int sip_prepare_socket(struct sip_pvt *p);
01231 static int get_address_family_filter(unsigned int transport);
01232
01233
01234 static int sipsock_read(int *id, int fd, short events, void *ignore);
01235 static int __sip_xmit(struct sip_pvt *p, struct ast_str *data);
01236 static int __sip_reliable_xmit(struct sip_pvt *p, uint32_t seqno, int resp, struct ast_str *data, int fatal, int sipmethod);
01237 static void add_cc_call_info_to_response(struct sip_pvt *p, struct sip_request *resp);
01238 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01239 static int retrans_pkt(const void *data);
01240 static int transmit_response_using_temp(ast_string_field callid, struct ast_sockaddr *addr, int useglobal_nat, const int intended_method, const struct sip_request *req, const char *msg);
01241 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01242 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01243 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01244 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp, int rpid);
01245 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01246 static int transmit_response_with_auth(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *rand, enum xmittype reliable, const char *header, int stale);
01247 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp);
01248 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01249 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable);
01250 static int transmit_request(struct sip_pvt *p, int sipmethod, uint32_t seqno, enum xmittype reliable, int newbranch);
01251 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, uint32_t seqno, enum xmittype reliable, int newbranch);
01252 static int transmit_publish(struct sip_epa_entry *epa_entry, enum sip_publish_type publish_type, const char * const explicit_uri);
01253 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init, const char * const explicit_uri);
01254 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp);
01255 static int transmit_info_with_aoc(struct sip_pvt *p, struct ast_aoc_decoded *decoded);
01256 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01257 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01258 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01259 static int transmit_refer(struct sip_pvt *p, const char *dest);
01260 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, const char *vmexten);
01261 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01262 static int transmit_cc_notify(struct ast_cc_agent *agent, struct sip_pvt *subscription, enum sip_cc_notify_state state);
01263 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01264 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, uint32_t seqno);
01265 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, uint32_t seqno);
01266 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01267 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01268 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req, char **name, char **number, int set_call_forward);
01269 static int sip_send_mwi_to_peer(struct sip_peer *peer, int cache_only);
01270
01271
01272 static int __sip_autodestruct(const void *data);
01273 static void *registry_unref(struct sip_registry *reg, char *tag);
01274 static int update_call_counter(struct sip_pvt *fup, int event);
01275 static int auto_congest(const void *arg);
01276 static struct sip_pvt *find_call(struct sip_request *req, struct ast_sockaddr *addr, const int intended_method);
01277 static void free_old_route(struct sip_route *route);
01278 static void list_route(struct sip_route *route);
01279 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards, int resp);
01280 static enum check_auth_result register_verify(struct sip_pvt *p, struct ast_sockaddr *addr,
01281 struct sip_request *req, const char *uri);
01282 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01283 static void check_pendings(struct sip_pvt *p);
01284 static void *sip_park_thread(void *stuff);
01285 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, uint32_t seqno, const char *park_exten, const char *park_context);
01286
01287 static void *sip_pickup_thread(void *stuff);
01288 static int sip_pickup(struct ast_channel *chan);
01289
01290 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01291 static int is_method_allowed(unsigned int *allowed_methods, enum sipmethod method);
01292
01293
01294 static void try_suggested_sip_codec(struct sip_pvt *p);
01295 static const char *get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01296 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value);
01297 static int find_sdp(struct sip_request *req);
01298 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action);
01299 static int process_sdp_o(const char *o, struct sip_pvt *p);
01300 static int process_sdp_c(const char *c, struct ast_sockaddr *addr);
01301 static int process_sdp_a_sendonly(const char *a, int *sendonly);
01302 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec);
01303 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newvideortp, int *last_rtpmap_codec);
01304 static int process_sdp_a_text(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newtextrtp, char *red_fmtp, int *red_num_gen, int *red_data_pt, int *last_rtpmap_codec);
01305 static int process_sdp_a_image(const char *a, struct sip_pvt *p);
01306 static void add_codec_to_sdp(const struct sip_pvt *p, format_t codec,
01307 struct ast_str **m_buf, struct ast_str **a_buf,
01308 int debug, int *min_packet_size);
01309 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format,
01310 struct ast_str **m_buf, struct ast_str **a_buf,
01311 int debug);
01312 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38);
01313 static void do_setnat(struct sip_pvt *p);
01314 static void stop_media_flows(struct sip_pvt *p);
01315
01316
01317 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01318 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01319 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01320 const char *secret, const char *md5secret, int sipmethod,
01321 const char *uri, enum xmittype reliable, int ignore);
01322 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01323 int sipmethod, const char *uri, enum xmittype reliable,
01324 struct ast_sockaddr *addr, struct sip_peer **authpeer);
01325 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, const char *uri, enum xmittype reliable, struct ast_sockaddr *addr);
01326
01327
01328 static int check_sip_domain(const char *domain, char *context, size_t len);
01329 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01330 static void clear_sip_domains(void);
01331
01332
01333 static void add_realm_authentication(struct sip_auth_container **credentials, const char *configuration, int lineno);
01334 static struct sip_auth *find_realm_authentication(struct sip_auth_container *credentials, const char *realm);
01335
01336
01337 static void check_rtp_timeout(struct sip_pvt *dialog, time_t t);
01338 static int reload_config(enum channelreloadreason reason);
01339 static void add_diversion_header(struct sip_request *req, struct sip_pvt *pvt);
01340 static int expire_register(const void *data);
01341 static void *do_monitor(void *data);
01342 static int restart_monitor(void);
01343 static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer);
01344 static struct ast_variable *copy_vars(struct ast_variable *src);
01345 static int dialog_find_multiple(void *obj, void *arg, int flags);
01346 static struct ast_channel *sip_pvt_lock_full(struct sip_pvt *pvt);
01347
01348 static int sip_refer_allocate(struct sip_pvt *p);
01349 static int sip_notify_allocate(struct sip_pvt *p);
01350 static void ast_quiet_chan(struct ast_channel *chan);
01351 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01352 static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context);
01353
01354
01355 static int cb_extensionstate(char *context, char* exten, int state, void *data);
01356 static int sip_devicestate(void *data);
01357 static int sip_poke_noanswer(const void *data);
01358 static int sip_poke_peer(struct sip_peer *peer, int force);
01359 static void sip_poke_all_peers(void);
01360 static void sip_peer_hold(struct sip_pvt *p, int hold);
01361 static void mwi_event_cb(const struct ast_event *, void *);
01362 static void network_change_event_cb(const struct ast_event *, void *);
01363
01364
01365 static const char *sip_nat_mode(const struct sip_pvt *p);
01366 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01367 static char *transfermode2str(enum transfermodes mode) attribute_const;
01368 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01369 static char *sip_show_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01370 static char * _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01371 static struct sip_peer *_sip_show_peers_one(int fd, struct mansession *s, struct show_peers_context *cont, struct sip_peer *peer);
01372 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01373 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01374 static void print_group(int fd, ast_group_t group, int crlf);
01375 static const char *dtmfmode2str(int mode) attribute_const;
01376 static int str2dtmfmode(const char *str) attribute_unused;
01377 static const char *insecure2str(int mode) attribute_const;
01378 static const char *allowoverlap2str(int mode) attribute_const;
01379 static void cleanup_stale_contexts(char *new, char *old);
01380 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01381 static const char *domain_mode_to_text(const enum domain_mode mode);
01382 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01383 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01384 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01385 static char *_sip_qualify_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01386 static char *sip_qualify_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01387 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01388 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01389 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01390 static char *sip_show_mwi(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01391 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01392 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01393 static char *complete_sip_peer(const char *word, int state, int flags2);
01394 static char *complete_sip_registered_peer(const char *word, int state, int flags2);
01395 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state);
01396 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01397 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state);
01398 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01399 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01400 static char *sip_show_channelstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01401 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01402 static char *sip_do_debug_ip(int fd, const char *arg);
01403 static char *sip_do_debug_peer(int fd, const char *arg);
01404 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01405 static char *sip_cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01406 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01407 static int sip_dtmfmode(struct ast_channel *chan, const char *data);
01408 static int sip_addheader(struct ast_channel *chan, const char *data);
01409 static int sip_do_reload(enum channelreloadreason reason);
01410 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01411 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
01412 const char *name, int flag, int family);
01413 static int ast_sockaddr_resolve_first(struct ast_sockaddr *addr,
01414 const char *name, int flag);
01415 static int ast_sockaddr_resolve_first_transport(struct ast_sockaddr *addr,
01416 const char *name, int flag, unsigned int transport);
01417
01418
01419
01420
01421
01422 static void sip_dump_history(struct sip_pvt *dialog);
01423 static inline int sip_debug_test_addr(const struct ast_sockaddr *addr);
01424 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01425 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01426 static void sip_dump_history(struct sip_pvt *dialog);
01427
01428
01429 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only);
01430 static int update_call_counter(struct sip_pvt *fup, int event);
01431 static void sip_destroy_peer(struct sip_peer *peer);
01432 static void sip_destroy_peer_fn(void *peer);
01433 static void set_peer_defaults(struct sip_peer *peer);
01434 static struct sip_peer *temp_peer(const char *name);
01435 static void register_peer_exten(struct sip_peer *peer, int onoff);
01436 static struct sip_peer *find_peer(const char *peer, struct ast_sockaddr *addr, int realtime, int forcenamematch, int devstate_only, int transport);
01437 static int sip_poke_peer_s(const void *data);
01438 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01439 static void reg_source_db(struct sip_peer *peer);
01440 static void destroy_association(struct sip_peer *peer);
01441 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
01442 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01443 static void set_socket_transport(struct sip_socket *socket, int transport);
01444
01445
01446 static void realtime_update_peer(const char *peername, struct ast_sockaddr *addr, const char *username, const char *fullcontact, const char *useragent, int expirey, unsigned short deprecated_username, int lastms);
01447 static void update_peer(struct sip_peer *p, int expire);
01448 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *config);
01449 static const char *get_name_from_variable(const struct ast_variable *var);
01450 static struct sip_peer *realtime_peer(const char *peername, struct ast_sockaddr *sin, int devstate_only, int which_objects);
01451 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01452
01453
01454 static void ast_sip_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us, struct sip_pvt *p);
01455 static void sip_registry_destroy(struct sip_registry *reg);
01456 static int sip_register(const char *value, int lineno);
01457 static const char *regstate2str(enum sipregistrystate regstate) attribute_const;
01458 static int sip_reregister(const void *data);
01459 static int __sip_do_register(struct sip_registry *r);
01460 static int sip_reg_timeout(const void *data);
01461 static void sip_send_all_registers(void);
01462 static int sip_reinvite_retry(const void *data);
01463
01464
01465 static void append_date(struct sip_request *req);
01466 static int determine_firstline_parts(struct sip_request *req);
01467 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01468 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01469 static int find_sip_method(const char *msg);
01470 static unsigned int parse_allowed_methods(struct sip_request *req);
01471 static unsigned int set_pvt_allowed_methods(struct sip_pvt *pvt, struct sip_request *req);
01472 static int parse_request(struct sip_request *req);
01473 static const char *get_header(const struct sip_request *req, const char *name);
01474 static const char *referstatus2str(enum referstatus rstatus) attribute_pure;
01475 static int method_match(enum sipmethod id, const char *name);
01476 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
01477 static const char *find_alias(const char *name, const char *_default);
01478 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
01479 static void lws2sws(struct ast_str *msgbuf);
01480 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
01481 static char *remove_uri_parameters(char *uri);
01482 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
01483 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
01484 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
01485 static int set_address_from_contact(struct sip_pvt *pvt);
01486 static void check_via(struct sip_pvt *p, const struct sip_request *req);
01487 static int get_rpid(struct sip_pvt *p, struct sip_request *oreq);
01488 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq, char **name, char **number, int *reason);
01489 static enum sip_get_dest_result get_destination(struct sip_pvt *p, struct sip_request *oreq, int *cc_recall_core_id);
01490 static int get_msg_text(char *buf, int len, struct sip_request *req);
01491 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
01492 static void update_connectedline(struct sip_pvt *p, const void *data, size_t datalen);
01493 static void update_redirecting(struct sip_pvt *p, const void *data, size_t datalen);
01494 static int get_domain(const char *str, char *domain, int len);
01495 static void get_realm(struct sip_pvt *p, const struct sip_request *req);
01496
01497
01498 static void *_sip_tcp_helper_thread(struct ast_tcptls_session_instance *tcptls_session);
01499 static void *sip_tcp_worker_fn(void *);
01500
01501
01502 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
01503 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
01504 static void deinit_req(struct sip_request *req);
01505 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, uint32_t seqno, int newbranch);
01506 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, const char * const explicit_uri);
01507 static int init_resp(struct sip_request *resp, const char *msg);
01508 static inline int resp_needs_contact(const char *msg, enum sipmethod method);
01509 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
01510 static const struct ast_sockaddr *sip_real_dst(const struct sip_pvt *p);
01511 static void build_via(struct sip_pvt *p);
01512 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
01513 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct ast_sockaddr *addr, int newdialog);
01514 static char *generate_random_string(char *buf, size_t size);
01515 static void build_callid_pvt(struct sip_pvt *pvt);
01516 static void change_callid_pvt(struct sip_pvt *pvt, const char *callid);
01517 static void build_callid_registry(struct sip_registry *reg, const struct ast_sockaddr *ourip, const char *fromdomain);
01518 static void build_localtag_registry(struct sip_registry *reg);
01519 static void make_our_tag(struct sip_pvt *pvt);
01520 static int add_header(struct sip_request *req, const char *var, const char *value);
01521 static int add_header_max_forwards(struct sip_pvt *dialog, struct sip_request *req);
01522 static int add_content(struct sip_request *req, const char *line);
01523 static int finalize_content(struct sip_request *req);
01524 static int add_text(struct sip_request *req, const char *text);
01525 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode);
01526 static int add_rpid(struct sip_request *req, struct sip_pvt *p);
01527 static int add_vidupdate(struct sip_request *req);
01528 static void add_route(struct sip_request *req, struct sip_route *route);
01529 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01530 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01531 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
01532 static void set_destination(struct sip_pvt *p, char *uri);
01533 static void append_date(struct sip_request *req);
01534 static void build_contact(struct sip_pvt *p);
01535
01536
01537 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int *recount, int *nounlock);
01538 static int handle_request_update(struct sip_pvt *p, struct sip_request *req);
01539 static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock);
01540 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, int *nounlock);
01541 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
01542 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *sin, const char *e);
01543 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
01544 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
01545 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, const char *e);
01546 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
01547 static int handle_request_options(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e);
01548 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, struct ast_sockaddr *addr, int *nounlock);
01549 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, const char *e);
01550 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, uint32_t seqno, int *nounlock);
01551
01552
01553 static void handle_response_publish(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
01554 static void handle_response_invite(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
01555 static void handle_response_notify(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
01556 static void handle_response_refer(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
01557 static void handle_response_subscribe(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
01558 static int handle_response_register(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
01559 static void handle_response(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
01560
01561
01562 static int setup_srtp(struct sip_srtp **srtp);
01563 static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct sip_srtp **srtp, const char *a);
01564
01565
01566 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
01567 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
01568 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
01569 static void change_t38_state(struct sip_pvt *p, int state);
01570
01571
01572 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp);
01573 static int proc_session_timer(const void *vp);
01574 static void stop_session_timer(struct sip_pvt *p);
01575 static void start_session_timer(struct sip_pvt *p);
01576 static void restart_session_timer(struct sip_pvt *p);
01577 static const char *strefresherparam2str(enum st_refresher r);
01578 static int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher_param *const p_ref);
01579 static int parse_minse(const char *p_hdrval, int *const p_interval);
01580 static int st_get_se(struct sip_pvt *, int max);
01581 static enum st_refresher st_get_refresher(struct sip_pvt *);
01582 static enum st_mode st_get_mode(struct sip_pvt *, int no_cached);
01583 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p);
01584
01585
01586 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, format_t codecs, int nat_active);
01587
01588
01589 static int sip_subscribe_mwi(const char *value, int lineno);
01590 static void sip_subscribe_mwi_destroy(struct sip_subscription_mwi *mwi);
01591 static void sip_send_all_mwi_subscriptions(void);
01592 static int sip_subscribe_mwi_do(const void *data);
01593 static int __sip_subscribe_mwi_do(struct sip_subscription_mwi *mwi);
01594
01595
01596 const struct ast_channel_tech sip_tech = {
01597 .type = "SIP",
01598 .description = "Session Initiation Protocol (SIP)",
01599 .capabilities = AST_FORMAT_AUDIO_MASK,
01600 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01601 .requester = sip_request_call,
01602 .devicestate = sip_devicestate,
01603 .call = sip_call,
01604 .send_html = sip_sendhtml,
01605 .hangup = sip_hangup,
01606 .answer = sip_answer,
01607 .read = sip_read,
01608 .write = sip_write,
01609 .write_video = sip_write,
01610 .write_text = sip_write,
01611 .indicate = sip_indicate,
01612 .transfer = sip_transfer,
01613 .fixup = sip_fixup,
01614 .send_digit_begin = sip_senddigit_begin,
01615 .send_digit_end = sip_senddigit_end,
01616 .bridge = ast_rtp_instance_bridge,
01617 .early_bridge = ast_rtp_instance_early_bridge,
01618 .send_text = sip_sendtext,
01619 .func_channel_read = sip_acf_channel_read,
01620 .setoption = sip_setoption,
01621 .queryoption = sip_queryoption,
01622 .get_pvt_uniqueid = sip_get_callid,
01623 };
01624
01625
01626
01627
01628
01629
01630
01631 struct ast_channel_tech sip_tech_info;
01632
01633 static int sip_cc_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
01634 static int sip_cc_agent_start_offer_timer(struct ast_cc_agent *agent);
01635 static int sip_cc_agent_stop_offer_timer(struct ast_cc_agent *agent);
01636 static void sip_cc_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
01637 static int sip_cc_agent_status_request(struct ast_cc_agent *agent);
01638 static int sip_cc_agent_start_monitoring(struct ast_cc_agent *agent);
01639 static int sip_cc_agent_recall(struct ast_cc_agent *agent);
01640 static void sip_cc_agent_destructor(struct ast_cc_agent *agent);
01641
01642 static struct ast_cc_agent_callbacks sip_cc_agent_callbacks = {
01643 .type = "SIP",
01644 .init = sip_cc_agent_init,
01645 .start_offer_timer = sip_cc_agent_start_offer_timer,
01646 .stop_offer_timer = sip_cc_agent_stop_offer_timer,
01647 .respond = sip_cc_agent_respond,
01648 .status_request = sip_cc_agent_status_request,
01649 .start_monitoring = sip_cc_agent_start_monitoring,
01650 .callee_available = sip_cc_agent_recall,
01651 .destructor = sip_cc_agent_destructor,
01652 };
01653
01654 static int find_by_notify_uri_helper(void *obj, void *arg, int flags)
01655 {
01656 struct ast_cc_agent *agent = obj;
01657 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01658 const char *uri = arg;
01659
01660 return !sip_uri_cmp(agent_pvt->notify_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
01661 }
01662
01663 static struct ast_cc_agent *find_sip_cc_agent_by_notify_uri(const char * const uri)
01664 {
01665 struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_notify_uri_helper, (char *)uri, "SIP");
01666 return agent;
01667 }
01668
01669 static int find_by_subscribe_uri_helper(void *obj, void *arg, int flags)
01670 {
01671 struct ast_cc_agent *agent = obj;
01672 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01673 const char *uri = arg;
01674
01675 return !sip_uri_cmp(agent_pvt->subscribe_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
01676 }
01677
01678 static struct ast_cc_agent *find_sip_cc_agent_by_subscribe_uri(const char * const uri)
01679 {
01680 struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_subscribe_uri_helper, (char *)uri, "SIP");
01681 return agent;
01682 }
01683
01684 static int find_by_callid_helper(void *obj, void *arg, int flags)
01685 {
01686 struct ast_cc_agent *agent = obj;
01687 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01688 struct sip_pvt *call_pvt = arg;
01689
01690 return !strcmp(agent_pvt->original_callid, call_pvt->callid) ? CMP_MATCH | CMP_STOP : 0;
01691 }
01692
01693 static struct ast_cc_agent *find_sip_cc_agent_by_original_callid(struct sip_pvt *pvt)
01694 {
01695 struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_callid_helper, pvt, "SIP");
01696 return agent;
01697 }
01698
01699 static int sip_cc_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan)
01700 {
01701 struct sip_cc_agent_pvt *agent_pvt = ast_calloc(1, sizeof(*agent_pvt));
01702 struct sip_pvt *call_pvt = chan->tech_pvt;
01703
01704 if (!agent_pvt) {
01705 return -1;
01706 }
01707
01708 ast_assert(!strcmp(chan->tech->type, "SIP"));
01709
01710 ast_copy_string(agent_pvt->original_callid, call_pvt->callid, sizeof(agent_pvt->original_callid));
01711 ast_copy_string(agent_pvt->original_exten, call_pvt->exten, sizeof(agent_pvt->original_exten));
01712 agent_pvt->offer_timer_id = -1;
01713 agent->private_data = agent_pvt;
01714 sip_pvt_lock(call_pvt);
01715 ast_set_flag(&call_pvt->flags[0], SIP_OFFER_CC);
01716 sip_pvt_unlock(call_pvt);
01717 return 0;
01718 }
01719
01720 static int sip_offer_timer_expire(const void *data)
01721 {
01722 struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
01723 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01724
01725 agent_pvt->offer_timer_id = -1;
01726
01727 return ast_cc_failed(agent->core_id, "SIP agent %s's offer timer expired", agent->device_name);
01728 }
01729
01730 static int sip_cc_agent_start_offer_timer(struct ast_cc_agent *agent)
01731 {
01732 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01733 int when;
01734
01735 when = ast_get_cc_offer_timer(agent->cc_params) * 1000;
01736 agent_pvt->offer_timer_id = ast_sched_add(sched, when, sip_offer_timer_expire, agent);
01737 return 0;
01738 }
01739
01740 static int sip_cc_agent_stop_offer_timer(struct ast_cc_agent *agent)
01741 {
01742 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01743
01744 AST_SCHED_DEL(sched, agent_pvt->offer_timer_id);
01745 return 0;
01746 }
01747
01748 static void sip_cc_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
01749 {
01750 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01751
01752 sip_pvt_lock(agent_pvt->subscribe_pvt);
01753 ast_set_flag(&agent_pvt->subscribe_pvt->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
01754 if (reason == AST_CC_AGENT_RESPONSE_SUCCESS || !ast_strlen_zero(agent_pvt->notify_uri)) {
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767 transmit_response(agent_pvt->subscribe_pvt, "200 OK", &agent_pvt->subscribe_pvt->initreq);
01768 transmit_cc_notify(agent, agent_pvt->subscribe_pvt, CC_QUEUED);
01769 } else {
01770 transmit_response(agent_pvt->subscribe_pvt, "500 Internal Error", &agent_pvt->subscribe_pvt->initreq);
01771 }
01772 sip_pvt_unlock(agent_pvt->subscribe_pvt);
01773 agent_pvt->is_available = TRUE;
01774 }
01775
01776 static int sip_cc_agent_status_request(struct ast_cc_agent *agent)
01777 {
01778 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01779 enum ast_device_state state = agent_pvt->is_available ? AST_DEVICE_NOT_INUSE : AST_DEVICE_INUSE;
01780 return ast_cc_agent_status_response(agent->core_id, state);
01781 }
01782
01783 static int sip_cc_agent_start_monitoring(struct ast_cc_agent *agent)
01784 {
01785
01786
01787
01788
01789 return 0;
01790 }
01791
01792 static int sip_cc_agent_recall(struct ast_cc_agent *agent)
01793 {
01794 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01795
01796
01797
01798
01799 if (!agent_pvt->is_available) {
01800 return ast_cc_agent_caller_busy(agent->core_id, "Caller %s is busy, reporting to the core",
01801 agent->device_name);
01802 }
01803
01804
01805
01806 sip_pvt_lock(agent_pvt->subscribe_pvt);
01807 transmit_cc_notify(agent, agent_pvt->subscribe_pvt, CC_READY);
01808 sip_pvt_unlock(agent_pvt->subscribe_pvt);
01809 return 0;
01810 }
01811
01812 static void sip_cc_agent_destructor(struct ast_cc_agent *agent)
01813 {
01814 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01815
01816 if (!agent_pvt) {
01817
01818 return;
01819 }
01820
01821 sip_cc_agent_stop_offer_timer(agent);
01822 if (agent_pvt->subscribe_pvt) {
01823 sip_pvt_lock(agent_pvt->subscribe_pvt);
01824 if (!ast_test_flag(&agent_pvt->subscribe_pvt->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
01825
01826
01827
01828 transmit_response(agent_pvt->subscribe_pvt, "500 Internal Server Error", &agent_pvt->subscribe_pvt->initreq);
01829 }
01830 sip_pvt_unlock(agent_pvt->subscribe_pvt);
01831 agent_pvt->subscribe_pvt = dialog_unref(agent_pvt->subscribe_pvt, "SIP CC agent destructor: Remove ref to subscription");
01832 }
01833 ast_free(agent_pvt);
01834 }
01835
01836 struct ao2_container *sip_monitor_instances;
01837
01838 static int sip_monitor_instance_hash_fn(const void *obj, const int flags)
01839 {
01840 const struct sip_monitor_instance *monitor_instance = obj;
01841 return monitor_instance->core_id;
01842 }
01843
01844 static int sip_monitor_instance_cmp_fn(void *obj, void *arg, int flags)
01845 {
01846 struct sip_monitor_instance *monitor_instance1 = obj;
01847 struct sip_monitor_instance *monitor_instance2 = arg;
01848
01849 return monitor_instance1->core_id == monitor_instance2->core_id ? CMP_MATCH | CMP_STOP : 0;
01850 }
01851
01852 static void sip_monitor_instance_destructor(void *data)
01853 {
01854 struct sip_monitor_instance *monitor_instance = data;
01855 if (monitor_instance->subscription_pvt) {
01856 sip_pvt_lock(monitor_instance->subscription_pvt);
01857 monitor_instance->subscription_pvt->expiry = 0;
01858 transmit_invite(monitor_instance->subscription_pvt, SIP_SUBSCRIBE, FALSE, 0, monitor_instance->subscribe_uri);
01859 sip_pvt_unlock(monitor_instance->subscription_pvt);
01860 dialog_unref(monitor_instance->subscription_pvt, "Unref monitor instance ref of subscription pvt");
01861 }
01862 if (monitor_instance->suspension_entry) {
01863 monitor_instance->suspension_entry->body[0] = '\0';
01864 transmit_publish(monitor_instance->suspension_entry, SIP_PUBLISH_REMOVE ,monitor_instance->notify_uri);
01865 ao2_t_ref(monitor_instance->suspension_entry, -1, "Decrementing suspension entry refcount in sip_monitor_instance_destructor");
01866 }
01867 ast_string_field_free_memory(monitor_instance);
01868 }
01869
01870 static struct sip_monitor_instance *sip_monitor_instance_init(int core_id, const char * const subscribe_uri, const char * const peername, const char * const device_name)
01871 {
01872 struct sip_monitor_instance *monitor_instance = ao2_alloc(sizeof(*monitor_instance), sip_monitor_instance_destructor);
01873
01874 if (!monitor_instance) {
01875 return NULL;
01876 }
01877
01878 if (ast_string_field_init(monitor_instance, 256)) {
01879 ao2_ref(monitor_instance, -1);
01880 return NULL;
01881 }
01882
01883 ast_string_field_set(monitor_instance, subscribe_uri, subscribe_uri);
01884 ast_string_field_set(monitor_instance, peername, peername);
01885 ast_string_field_set(monitor_instance, device_name, device_name);
01886 monitor_instance->core_id = core_id;
01887 ao2_link(sip_monitor_instances, monitor_instance);
01888 return monitor_instance;
01889 }
01890
01891 static int find_sip_monitor_instance_by_subscription_pvt(void *obj, void *arg, int flags)
01892 {
01893 struct sip_monitor_instance *monitor_instance = obj;
01894 return monitor_instance->subscription_pvt == arg ? CMP_MATCH | CMP_STOP : 0;
01895 }
01896
01897 static int find_sip_monitor_instance_by_suspension_entry(void *obj, void *arg, int flags)
01898 {
01899 struct sip_monitor_instance *monitor_instance = obj;
01900 return monitor_instance->suspension_entry == arg ? CMP_MATCH | CMP_STOP : 0;
01901 }
01902
01903 static int sip_cc_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
01904 static int sip_cc_monitor_suspend(struct ast_cc_monitor *monitor);
01905 static int sip_cc_monitor_unsuspend(struct ast_cc_monitor *monitor);
01906 static int sip_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
01907 static void sip_cc_monitor_destructor(void *private_data);
01908
01909 static struct ast_cc_monitor_callbacks sip_cc_monitor_callbacks = {
01910 .type = "SIP",
01911 .request_cc = sip_cc_monitor_request_cc,
01912 .suspend = sip_cc_monitor_suspend,
01913 .unsuspend = sip_cc_monitor_unsuspend,
01914 .cancel_available_timer = sip_cc_monitor_cancel_available_timer,
01915 .destructor = sip_cc_monitor_destructor,
01916 };
01917
01918 static int sip_cc_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
01919 {
01920 struct sip_monitor_instance *monitor_instance = monitor->private_data;
01921 enum ast_cc_service_type service = monitor->service_offered;
01922 int when;
01923
01924 if (!monitor_instance) {
01925 return -1;
01926 }
01927
01928 if (!(monitor_instance->subscription_pvt = sip_alloc(NULL, NULL, 0, SIP_SUBSCRIBE, NULL))) {
01929 return -1;
01930 }
01931
01932 when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) :
01933 ast_get_ccnr_available_timer(monitor->interface->config_params);
01934
01935 sip_pvt_lock(monitor_instance->subscription_pvt);
01936 ast_set_flag(&monitor_instance->subscription_pvt->flags[0], SIP_OUTGOING);
01937 create_addr(monitor_instance->subscription_pvt, monitor_instance->peername, 0, 1);
01938 ast_sip_ouraddrfor(&monitor_instance->subscription_pvt->sa, &monitor_instance->subscription_pvt->ourip, monitor_instance->subscription_pvt);
01939 monitor_instance->subscription_pvt->subscribed = CALL_COMPLETION;
01940 monitor_instance->subscription_pvt->expiry = when;
01941
01942 transmit_invite(monitor_instance->subscription_pvt, SIP_SUBSCRIBE, FALSE, 2, monitor_instance->subscribe_uri);
01943 sip_pvt_unlock(monitor_instance->subscription_pvt);
01944
01945 ao2_t_ref(monitor, +1, "Adding a ref to the monitor for the scheduler");
01946 *available_timer_id = ast_sched_add(sched, when * 1000, ast_cc_available_timer_expire, monitor);
01947 return 0;
01948 }
01949
01950 static int construct_pidf_body(enum sip_cc_publish_state state, char *pidf_body, size_t size, const char *presentity)
01951 {
01952 struct ast_str *body = ast_str_alloca(size);
01953 char tuple_id[32];
01954
01955 generate_random_string(tuple_id, sizeof(tuple_id));
01956
01957
01958
01959
01960 ast_str_append(&body, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
01961
01962
01963
01964
01965
01966
01967 ast_str_append(&body, 0, "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"%s\">\n", presentity);
01968 ast_str_append(&body, 0, "<tuple id=\"%s\">\n", tuple_id);
01969 ast_str_append(&body, 0, "<status><basic>%s</basic></status>\n", state == CC_OPEN ? "open" : "closed");
01970 ast_str_append(&body, 0, "</tuple>\n");
01971 ast_str_append(&body, 0, "</presence>\n");
01972 ast_copy_string(pidf_body, ast_str_buffer(body), size);
01973 return 0;
01974 }
01975
01976 static int sip_cc_monitor_suspend(struct ast_cc_monitor *monitor)
01977 {
01978 struct sip_monitor_instance *monitor_instance = monitor->private_data;
01979 enum sip_publish_type publish_type;
01980 struct cc_epa_entry *cc_entry;
01981
01982 if (!monitor_instance) {
01983 return -1;
01984 }
01985
01986 if (!monitor_instance->suspension_entry) {
01987
01988 if (!(monitor_instance->suspension_entry = create_epa_entry("call-completion", monitor_instance->peername))) {
01989 ast_log(LOG_WARNING, "Unable to allocate sip EPA entry for call-completion\n");
01990 ao2_ref(monitor_instance, -1);
01991 return -1;
01992 }
01993 if (!(cc_entry = ast_calloc(1, sizeof(*cc_entry)))) {
01994 ast_log(LOG_WARNING, "Unable to allocate space for instance data of EPA entry for call-completion\n");
01995 ao2_ref(monitor_instance, -1);
01996 return -1;
01997 }
01998 cc_entry->core_id = monitor->core_id;
01999 monitor_instance->suspension_entry->instance_data = cc_entry;
02000 publish_type = SIP_PUBLISH_INITIAL;
02001 } else {
02002 publish_type = SIP_PUBLISH_MODIFY;
02003 cc_entry = monitor_instance->suspension_entry->instance_data;
02004 }
02005
02006 cc_entry->current_state = CC_CLOSED;
02007
02008 if (ast_strlen_zero(monitor_instance->notify_uri)) {
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021 return 0;
02022 }
02023 construct_pidf_body(CC_CLOSED, monitor_instance->suspension_entry->body, sizeof(monitor_instance->suspension_entry->body), monitor_instance->peername);
02024 return transmit_publish(monitor_instance->suspension_entry, publish_type, monitor_instance->notify_uri);
02025 }
02026
02027 static int sip_cc_monitor_unsuspend(struct ast_cc_monitor *monitor)
02028 {
02029 struct sip_monitor_instance *monitor_instance = monitor->private_data;
02030 struct cc_epa_entry *cc_entry;
02031
02032 if (!monitor_instance) {
02033 return -1;
02034 }
02035
02036 ast_assert(monitor_instance->suspension_entry != NULL);
02037
02038 cc_entry = monitor_instance->suspension_entry->instance_data;
02039 cc_entry->current_state = CC_OPEN;
02040 if (ast_strlen_zero(monitor_instance->notify_uri)) {
02041
02042
02043
02044
02045 return 0;
02046 }
02047 construct_pidf_body(CC_OPEN, monitor_instance->suspension_entry->body, sizeof(monitor_instance->suspension_entry->body), monitor_instance->peername);
02048 return transmit_publish(monitor_instance->suspension_entry, SIP_PUBLISH_MODIFY, monitor_instance->notify_uri);
02049 }
02050
02051 static int sip_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
02052 {
02053 if (*sched_id != -1) {
02054 AST_SCHED_DEL(sched, *sched_id);
02055 ao2_t_ref(monitor, -1, "Removing scheduler's reference to the monitor");
02056 }
02057 return 0;
02058 }
02059
02060 static void sip_cc_monitor_destructor(void *private_data)
02061 {
02062 struct sip_monitor_instance *monitor_instance = private_data;
02063 ao2_unlink(sip_monitor_instances, monitor_instance);
02064 ast_module_unref(ast_module_info->self);
02065 }
02066
02067 static int sip_get_cc_information(struct sip_request *req, char *subscribe_uri, size_t size, enum ast_cc_service_type *service)
02068 {
02069 char *call_info = ast_strdupa(get_header(req, "Call-Info"));
02070 char *uri;
02071 char *purpose;
02072 char *service_str;
02073 static const char cc_purpose[] = "purpose=call-completion";
02074 static const int cc_purpose_len = sizeof(cc_purpose) - 1;
02075
02076 if (ast_strlen_zero(call_info)) {
02077
02078 return -1;
02079 }
02080
02081 uri = strsep(&call_info, ";");
02082
02083 while ((purpose = strsep(&call_info, ";"))) {
02084 if (!strncmp(purpose, cc_purpose, cc_purpose_len)) {
02085 break;
02086 }
02087 }
02088 if (!purpose) {
02089
02090 return -1;
02091 }
02092
02093
02094 while ((service_str = strsep(&call_info, ";"))) {
02095 if (!strncmp(service_str, "m=", 2)) {
02096 break;
02097 }
02098 }
02099 if (!service_str) {
02100
02101
02102
02103 service_str = "BS";
02104 } else {
02105
02106
02107
02108 strsep(&service_str, "=");
02109 }
02110
02111 if ((*service = service_string_to_service_type(service_str)) == AST_CC_NONE) {
02112
02113 return -1;
02114 }
02115
02116 ast_copy_string(subscribe_uri, get_in_brackets(uri), size);
02117
02118 return 0;
02119 }
02120
02121
02122
02123
02124
02125
02126
02127
02128
02129
02130
02131
02132
02133
02134
02135
02136
02137
02138 static void sip_handle_cc(struct sip_pvt *pvt, struct sip_request *req, enum ast_cc_service_type service)
02139 {
02140 enum ast_cc_monitor_policies monitor_policy = ast_get_cc_monitor_policy(pvt->cc_params);
02141 int core_id;
02142 char interface_name[AST_CHANNEL_NAME];
02143
02144 if (monitor_policy == AST_CC_MONITOR_NEVER) {
02145
02146 return;
02147 }
02148
02149 if ((core_id = ast_cc_get_current_core_id(pvt->owner)) == -1) {
02150
02151 return;
02152 }
02153
02154 ast_channel_get_device_name(pvt->owner, interface_name, sizeof(interface_name));
02155
02156 if (monitor_policy == AST_CC_MONITOR_ALWAYS || monitor_policy == AST_CC_MONITOR_NATIVE) {
02157 char subscribe_uri[SIPBUFSIZE];
02158 char device_name[AST_CHANNEL_NAME];
02159 enum ast_cc_service_type offered_service;
02160 struct sip_monitor_instance *monitor_instance;
02161 if (sip_get_cc_information(req, subscribe_uri, sizeof(subscribe_uri), &offered_service)) {
02162
02163
02164
02165
02166 goto generic;
02167 }
02168 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
02169 if (!(monitor_instance = sip_monitor_instance_init(core_id, subscribe_uri, pvt->peername, device_name))) {
02170
02171 goto generic;
02172 }
02173
02174
02175
02176
02177 ast_module_ref(ast_module_info->self);
02178 ast_queue_cc_frame(pvt->owner, "SIP", pvt->dialstring, offered_service, monitor_instance);
02179 ao2_ref(monitor_instance, -1);
02180 return;
02181 }
02182
02183 generic:
02184 if (monitor_policy == AST_CC_MONITOR_GENERIC || monitor_policy == AST_CC_MONITOR_ALWAYS) {
02185 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE, interface_name, service, NULL);
02186 }
02187 }
02188
02189
02190 static struct ast_tls_config sip_tls_cfg;
02191
02192
02193 static struct ast_tls_config default_tls_cfg;
02194
02195
02196 static struct ast_tcptls_session_args sip_tcp_desc = {
02197 .accept_fd = -1,
02198 .master = AST_PTHREADT_NULL,
02199 .tls_cfg = NULL,
02200 .poll_timeout = -1,
02201 .name = "SIP TCP server",
02202 .accept_fn = ast_tcptls_server_root,
02203 .worker_fn = sip_tcp_worker_fn,
02204 };
02205
02206
02207 static struct ast_tcptls_session_args sip_tls_desc = {
02208 .accept_fd = -1,
02209 .master = AST_PTHREADT_NULL,
02210 .tls_cfg = &sip_tls_cfg,
02211 .poll_timeout = -1,
02212 .name = "SIP TLS server",
02213 .accept_fn = ast_tcptls_server_root,
02214 .worker_fn = sip_tcp_worker_fn,
02215 };
02216
02217
02218
02219 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
02220
02221 struct sip_pvt *dialog_ref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
02222 {
02223 if (p)
02224 #ifdef REF_DEBUG
02225 __ao2_ref_debug(p, 1, tag, file, line, func);
02226 #else
02227 ao2_ref(p, 1);
02228 #endif
02229 else
02230 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
02231 return p;
02232 }
02233
02234 struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
02235 {
02236 if (p)
02237 #ifdef REF_DEBUG
02238 __ao2_ref_debug(p, -1, tag, file, line, func);
02239 #else
02240 ao2_ref(p, -1);
02241 #endif
02242 return NULL;
02243 }
02244
02245
02246
02247
02248 static const char *map_x_s(const struct _map_x_s *table, int x, const char *errorstring)
02249 {
02250 const struct _map_x_s *cur;
02251
02252 for (cur = table; cur->s; cur++)
02253 if (cur->x == x)
02254 return cur->s;
02255 return errorstring;
02256 }
02257
02258
02259
02260
02261 static int map_s_x(const struct _map_x_s *table, const char *s, int errorvalue)
02262 {
02263 const struct _map_x_s *cur;
02264
02265 for (cur = table; cur->s; cur++)
02266 if (!strcasecmp(cur->s, s))
02267 return cur->x;
02268 return errorvalue;
02269 }
02270
02271 static enum AST_REDIRECTING_REASON sip_reason_str_to_code(const char *text)
02272 {
02273 enum AST_REDIRECTING_REASON ast = AST_REDIRECTING_REASON_UNKNOWN;
02274 int i;
02275
02276 for (i = 0; i < ARRAY_LEN(sip_reason_table); ++i) {
02277 if (!strcasecmp(text, sip_reason_table[i].text)) {
02278 ast = sip_reason_table[i].code;
02279 break;
02280 }
02281 }
02282
02283 return ast;
02284 }
02285
02286 static const char *sip_reason_code_to_str(enum AST_REDIRECTING_REASON code)
02287 {
02288 if (code >= 0 && code < ARRAY_LEN(sip_reason_table)) {
02289 return sip_reason_table[code].text;
02290 }
02291
02292 return "unknown";
02293 }
02294
02295
02296
02297
02298
02299
02300
02301
02302 #define check_request_transport(peer, tmpl) ({ \
02303 int ret = 0; \
02304 if (peer->socket.type == tmpl->socket.type) \
02305 ; \
02306 else if (!(peer->transports & tmpl->socket.type)) {\
02307 ast_log(LOG_ERROR, \
02308 "'%s' is not a valid transport for '%s'. we only use '%s'! ending call.\n", \
02309 get_transport(tmpl->socket.type), peer->name, get_transport_list(peer->transports) \
02310 ); \
02311 ret = 1; \
02312 } else if (peer->socket.type & SIP_TRANSPORT_TLS) { \
02313 ast_log(LOG_WARNING, \
02314 "peer '%s' HAS NOT USED (OR SWITCHED TO) TLS in favor of '%s' (but this was allowed in sip.conf)!\n", \
02315 peer->name, get_transport(tmpl->socket.type) \
02316 ); \
02317 } else { \
02318 ast_debug(1, \
02319 "peer '%s' has contacted us over %s even though we prefer %s.\n", \
02320 peer->name, get_transport(tmpl->socket.type), get_transport(peer->socket.type) \
02321 ); \
02322 }\
02323 (ret); \
02324 })
02325
02326
02327
02328
02329 static struct ast_variable *copy_vars(struct ast_variable *src)
02330 {
02331 struct ast_variable *res = NULL, *tmp, *v = NULL;
02332
02333 for (v = src ; v ; v = v->next) {
02334 if ((tmp = ast_variable_new(v->name, v->value, v->file))) {
02335 tmp->next = res;
02336 res = tmp;
02337 }
02338 }
02339 return res;
02340 }
02341
02342 static void tcptls_packet_destructor(void *obj)
02343 {
02344 struct tcptls_packet *packet = obj;
02345
02346 ast_free(packet->data);
02347 }
02348
02349 static void sip_tcptls_client_args_destructor(void *obj)
02350 {
02351 struct ast_tcptls_session_args *args = obj;
02352 if (args->tls_cfg) {
02353 ast_free(args->tls_cfg->certfile);
02354 ast_free(args->tls_cfg->pvtfile);
02355 ast_free(args->tls_cfg->cipher);
02356 ast_free(args->tls_cfg->cafile);
02357 ast_free(args->tls_cfg->capath);
02358
02359 ast_ssl_teardown(args->tls_cfg);
02360 }
02361 ast_free(args->tls_cfg);
02362 ast_free((char *) args->name);
02363 }
02364
02365 static void sip_threadinfo_destructor(void *obj)
02366 {
02367 struct sip_threadinfo *th = obj;
02368 struct tcptls_packet *packet;
02369 if (th->alert_pipe[1] > -1) {
02370 close(th->alert_pipe[0]);
02371 }
02372 if (th->alert_pipe[1] > -1) {
02373 close(th->alert_pipe[1]);
02374 }
02375 th->alert_pipe[0] = th->alert_pipe[1] = -1;
02376
02377 while ((packet = AST_LIST_REMOVE_HEAD(&th->packet_q, entry))) {
02378 ao2_t_ref(packet, -1, "thread destruction, removing packet from frame queue");
02379 }
02380
02381 if (th->tcptls_session) {
02382 ao2_t_ref(th->tcptls_session, -1, "remove tcptls_session for sip_threadinfo object");
02383 }
02384 }
02385
02386
02387 static struct sip_threadinfo *sip_threadinfo_create(struct ast_tcptls_session_instance *tcptls_session, int transport)
02388 {
02389 struct sip_threadinfo *th;
02390
02391 if (!tcptls_session || !(th = ao2_alloc(sizeof(*th), sip_threadinfo_destructor))) {
02392 return NULL;
02393 }
02394
02395 th->alert_pipe[0] = th->alert_pipe[1] = -1;
02396
02397 if (pipe(th->alert_pipe) == -1) {
02398 ao2_t_ref(th, -1, "Failed to open alert pipe on sip_threadinfo");
02399 ast_log(LOG_ERROR, "Could not create sip alert pipe in tcptls thread, error %s\n", strerror(errno));
02400 return NULL;
02401 }
02402 ao2_t_ref(tcptls_session, +1, "tcptls_session ref for sip_threadinfo object");
02403 th->tcptls_session = tcptls_session;
02404 th->type = transport ? transport : (tcptls_session->ssl ? SIP_TRANSPORT_TLS: SIP_TRANSPORT_TCP);
02405 ao2_t_link(threadt, th, "Adding new tcptls helper thread");
02406 ao2_t_ref(th, -1, "Decrementing threadinfo ref from alloc, only table ref remains");
02407 return th;
02408 }
02409
02410
02411 static int sip_tcptls_write(struct ast_tcptls_session_instance *tcptls_session, const void *buf, size_t len)
02412 {
02413 int res = len;
02414 struct sip_threadinfo *th = NULL;
02415 struct tcptls_packet *packet = NULL;
02416 struct sip_threadinfo tmp = {
02417 .tcptls_session = tcptls_session,
02418 };
02419 enum sip_tcptls_alert alert = TCPTLS_ALERT_DATA;
02420
02421 if (!tcptls_session) {
02422 return XMIT_ERROR;
02423 }
02424
02425 ast_mutex_lock(&tcptls_session->lock);
02426
02427 if ((tcptls_session->fd == -1) ||
02428 !(th = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread")) ||
02429 !(packet = ao2_alloc(sizeof(*packet), tcptls_packet_destructor)) ||
02430 !(packet->data = ast_str_create(len))) {
02431 goto tcptls_write_setup_error;
02432 }
02433
02434
02435 ast_str_set(&packet->data, 0, "%s", (char *) buf);
02436 packet->len = len;
02437
02438
02439
02440
02441 ao2_lock(th);
02442 if (write(th->alert_pipe[1], &alert, sizeof(alert)) == -1) {
02443 ast_log(LOG_ERROR, "write() to alert pipe failed: %s\n", strerror(errno));
02444 ao2_t_ref(packet, -1, "could not write to alert pipe, remove packet");
02445 packet = NULL;
02446 res = XMIT_ERROR;
02447 } else {
02448 AST_LIST_INSERT_TAIL(&th->packet_q, packet, entry);
02449 }
02450 ao2_unlock(th);
02451
02452 ast_mutex_unlock(&tcptls_session->lock);
02453 ao2_t_ref(th, -1, "In sip_tcptls_write, unref threadinfo object after finding it");
02454 return res;
02455
02456 tcptls_write_setup_error:
02457 if (th) {
02458 ao2_t_ref(th, -1, "In sip_tcptls_write, unref threadinfo obj, could not create packet");
02459 }
02460 if (packet) {
02461 ao2_t_ref(packet, -1, "could not allocate packet's data");
02462 }
02463 ast_mutex_unlock(&tcptls_session->lock);
02464
02465 return XMIT_ERROR;
02466 }
02467
02468
02469 static void *sip_tcp_worker_fn(void *data)
02470 {
02471 struct ast_tcptls_session_instance *tcptls_session = data;
02472
02473 return _sip_tcp_helper_thread(tcptls_session);
02474 }
02475
02476
02477
02478
02479
02480
02481
02482
02483 static int sip_check_authtimeout(time_t start)
02484 {
02485 int timeout;
02486 time_t now;
02487 if(time(&now) == -1) {
02488 ast_log(LOG_ERROR, "error executing time(): %s\n", strerror(errno));
02489 return -1;
02490 }
02491
02492 timeout = (authtimeout - (now - start)) * 1000;
02493 if (timeout < 0) {
02494
02495 return 0;
02496 }
02497
02498 return timeout;
02499 }
02500
02501
02502
02503
02504 enum message_integrity {
02505
02506
02507
02508
02509 MESSAGE_INVALID,
02510
02511
02512
02513 MESSAGE_FRAGMENT,
02514
02515
02516
02517
02518 MESSAGE_FRAGMENT_COMPLETE,
02519
02520
02521
02522 MESSAGE_COMPLETE,
02523 };
02524
02525
02526
02527
02528
02529
02530
02531
02532 static int read_raw_content_length(const char *message)
02533 {
02534 char *content_length_str;
02535 int content_length = -1;
02536
02537 struct ast_str *msg_copy;
02538 char *msg;
02539
02540
02541 if (!(msg_copy = ast_str_create(strlen(message) + 1))) {
02542 return -1;
02543 }
02544 ast_str_set(&msg_copy, 0, "%s", message);
02545
02546 if (sip_cfg.pedanticsipchecking) {
02547 lws2sws(msg_copy);
02548 }
02549
02550 msg = ast_str_buffer(msg_copy);
02551
02552
02553 if ((content_length_str = strcasestr(msg, "\nContent-Length:"))) {
02554 content_length_str += sizeof("\nContent-Length:") - 1;
02555 } else if ((content_length_str = strcasestr(msg, "\nl:"))) {
02556 content_length_str += sizeof("\nl:") - 1;
02557 } else {
02558
02559
02560
02561
02562
02563 goto done;
02564 }
02565
02566
02567 if (!strchr(content_length_str, '\n')) {
02568 goto done;
02569 }
02570
02571 if (sscanf(content_length_str, "%30d", &content_length) != 1) {
02572 content_length = -1;
02573 }
02574
02575 done:
02576 ast_free(msg_copy);
02577 return content_length;
02578 }
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
02591
02592 static enum message_integrity check_message_integrity(struct ast_str **request, struct ast_str **overflow)
02593 {
02594 char *message = ast_str_buffer(*request);
02595 char *body;
02596 int content_length;
02597 int message_len = ast_str_strlen(*request);
02598 int body_len;
02599
02600
02601
02602
02603
02604
02605 body = strstr(message, "\r\n\r\n");
02606 if (!body) {
02607
02608
02609
02610 return MESSAGE_FRAGMENT;
02611 }
02612 body += sizeof("\r\n\r\n") - 1;
02613 body_len = message_len - (body - message);
02614
02615 body[-1] = '\0';
02616 content_length = read_raw_content_length(message);
02617 body[-1] = '\n';
02618
02619 if (content_length < 0) {
02620 return MESSAGE_INVALID;
02621 } else if (content_length == 0) {
02622
02623
02624
02625
02626 if (body_len == 0) {
02627 return MESSAGE_COMPLETE;
02628 } else {
02629 ast_str_append(overflow, 0, "%s", body);
02630 ast_str_truncate(*request, message_len - body_len);
02631 return MESSAGE_FRAGMENT_COMPLETE;
02632 }
02633 }
02634
02635
02636
02637 if (body_len < content_length) {
02638
02639 return MESSAGE_FRAGMENT;
02640 } else if (body_len > content_length) {
02641
02642
02643
02644 ast_str_append(overflow, 0, "%s", body + content_length);
02645 ast_str_truncate(*request, message_len - (body_len - content_length));
02646 return MESSAGE_FRAGMENT_COMPLETE;
02647 } else {
02648
02649 return MESSAGE_COMPLETE;
02650 }
02651 }
02652
02653
02654
02655
02656
02657
02658
02659
02660
02661 static int sip_tcptls_read(struct sip_request *req, struct ast_tcptls_session_instance *tcptls_session,
02662 int authenticated, time_t start)
02663 {
02664 enum message_integrity message_integrity = MESSAGE_FRAGMENT;
02665
02666 while (message_integrity == MESSAGE_FRAGMENT) {
02667 size_t datalen;
02668
02669 if (ast_str_strlen(tcptls_session->overflow_buf) == 0) {
02670 char readbuf[4097];
02671 int timeout;
02672 int res;
02673 if (!tcptls_session->client && !authenticated) {
02674 if ((timeout = sip_check_authtimeout(start)) < 0) {
02675 return -1;
02676 }
02677
02678 if (timeout == 0) {
02679 ast_debug(2, "SIP TCP/TLS server timed out\n");
02680 return -1;
02681 }
02682 } else {
02683 timeout = -1;
02684 }
02685 res = ast_wait_for_input(tcptls_session->fd, timeout);
02686 if (res < 0) {
02687 ast_debug(2, "SIP TCP/TLS server :: ast_wait_for_input returned %d\n", res);
02688 return -1;
02689 } else if (res == 0) {
02690 ast_debug(2, "SIP TCP/TLS server timed out\n");
02691 return -1;
02692 }
02693
02694 res = ast_tcptls_server_read(tcptls_session, readbuf, sizeof(readbuf) - 1);
02695 if (res < 0) {
02696 if (errno == EAGAIN || errno == EINTR) {
02697 continue;
02698 }
02699 ast_debug(2, "SIP TCP/TLS server error when receiving data\n");
02700 return -1;
02701 } else if (res == 0) {
02702 ast_debug(2, "SIP TCP/TLS server has shut down\n");
02703 return -1;
02704 }
02705 readbuf[res] = '\0';
02706 ast_str_append(&req->data, 0, "%s", readbuf);
02707 } else {
02708 ast_str_append(&req->data, 0, "%s", ast_str_buffer(tcptls_session->overflow_buf));
02709 ast_str_reset(tcptls_session->overflow_buf);
02710 }
02711
02712 datalen = ast_str_strlen(req->data);
02713 if (datalen > SIP_MAX_PACKET_SIZE) {
02714 ast_log(LOG_WARNING, "Rejecting TCP/TLS packet from '%s' because way too large: %zu\n",
02715 ast_sockaddr_stringify(&tcptls_session->remote_address), datalen);
02716 return -1;
02717 }
02718
02719 message_integrity = check_message_integrity(&req->data, &tcptls_session->overflow_buf);
02720 }
02721
02722 return 0;
02723 }
02724
02725
02726
02727
02728 static void *_sip_tcp_helper_thread(struct ast_tcptls_session_instance *tcptls_session)
02729 {
02730 int res, timeout = -1, authenticated = 0, flags;
02731 time_t start;
02732 struct sip_request req = { 0, } , reqcpy = { 0, };
02733 struct sip_threadinfo *me = NULL;
02734 char buf[1024] = "";
02735 struct pollfd fds[2] = { { 0 }, { 0 }, };
02736 struct ast_tcptls_session_args *ca = NULL;
02737
02738
02739
02740
02741
02742
02743
02744
02745
02746
02747
02748
02749 if (!tcptls_session->client) {
02750 if (ast_atomic_fetchadd_int(&unauth_sessions, +1) >= authlimit) {
02751
02752 goto cleanup;
02753 }
02754
02755 if ((flags = fcntl(tcptls_session->fd, F_GETFL)) == -1) {
02756 ast_log(LOG_ERROR, "error setting socket to non blocking mode, fcntl() failed: %s\n", strerror(errno));
02757 goto cleanup;
02758 }
02759
02760 flags |= O_NONBLOCK;
02761 if (fcntl(tcptls_session->fd, F_SETFL, flags) == -1) {
02762 ast_log(LOG_ERROR, "error setting socket to non blocking mode, fcntl() failed: %s\n", strerror(errno));
02763 goto cleanup;
02764 }
02765
02766 if (!(me = sip_threadinfo_create(tcptls_session, tcptls_session->ssl ? SIP_TRANSPORT_TLS : SIP_TRANSPORT_TCP))) {
02767 goto cleanup;
02768 }
02769 ao2_t_ref(me, +1, "Adding threadinfo ref for tcp_helper_thread");
02770 } else {
02771 struct sip_threadinfo tmp = {
02772 .tcptls_session = tcptls_session,
02773 };
02774
02775 if ((!(ca = tcptls_session->parent)) ||
02776 (!(me = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread"))) ||
02777 (!(tcptls_session = ast_tcptls_client_start(tcptls_session)))) {
02778 goto cleanup;
02779 }
02780 }
02781
02782 flags = 1;
02783 if (setsockopt(tcptls_session->fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags))) {
02784 ast_log(LOG_ERROR, "error enabling TCP keep-alives on sip socket: %s\n", strerror(errno));
02785 goto cleanup;
02786 }
02787
02788 me->threadid = pthread_self();
02789 ast_debug(2, "Starting thread for %s server\n", tcptls_session->ssl ? "TLS" : "TCP");
02790
02791
02792 fds[0].fd = tcptls_session->fd;
02793 fds[1].fd = me->alert_pipe[0];
02794 fds[0].events = fds[1].events = POLLIN | POLLPRI;
02795
02796 if (!(req.data = ast_str_create(SIP_MIN_PACKET))) {
02797 goto cleanup;
02798 }
02799 if (!(reqcpy.data = ast_str_create(SIP_MIN_PACKET))) {
02800 goto cleanup;
02801 }
02802
02803 if(time(&start) == -1) {
02804 ast_log(LOG_ERROR, "error executing time(): %s\n", strerror(errno));
02805 goto cleanup;
02806 }
02807
02808
02809
02810
02811
02812 ast_tcptls_stream_set_exclusive_input(tcptls_session->stream_cookie, 0);
02813
02814 ast_tcptls_stream_set_timeout_sequence(tcptls_session->stream_cookie, ast_tvnow(),
02815 tcptls_session->client ? -1 : (authtimeout * 1000));
02816
02817 for (;;) {
02818 struct ast_str *str_save;
02819
02820 if (!tcptls_session->client && req.authenticated && !authenticated) {
02821 authenticated = 1;
02822 ast_tcptls_stream_set_timeout_disable(tcptls_session->stream_cookie);
02823 ast_atomic_fetchadd_int(&unauth_sessions, -1);
02824 }
02825
02826
02827 if (!tcptls_session->client && !authenticated ) {
02828 if ((timeout = sip_check_authtimeout(start)) < 0) {
02829 goto cleanup;
02830 }
02831
02832 if (timeout == 0) {
02833 ast_debug(2, "SIP %s server timed out\n", tcptls_session->ssl ? "TLS": "TCP");
02834 goto cleanup;
02835 }
02836 } else {
02837 timeout = -1;
02838 }
02839
02840 if (ast_str_strlen(tcptls_session->overflow_buf) == 0) {
02841 res = ast_poll(fds, 2, timeout);
02842 if (res < 0) {
02843 ast_debug(2, "SIP %s server :: ast_wait_for_input returned %d\n", tcptls_session->ssl ? "TLS": "TCP", res);
02844 goto cleanup;
02845 } else if (res == 0) {
02846
02847 ast_debug(2, "SIP %s server timed out\n", tcptls_session->ssl ? "TLS": "TCP");
02848 goto cleanup;
02849 }
02850 }
02851
02852
02853
02854
02855
02856 if (fds[0].revents || (ast_str_strlen(tcptls_session->overflow_buf) > 0)) {
02857 fds[0].revents = 0;
02858
02859
02860 str_save = req.data;
02861 memset(&req, 0, sizeof(req));
02862 req.data = str_save;
02863 ast_str_reset(req.data);
02864
02865 str_save = reqcpy.data;
02866 memset(&reqcpy, 0, sizeof(reqcpy));
02867 reqcpy.data = str_save;
02868 ast_str_reset(reqcpy.data);
02869
02870 memset(buf, 0, sizeof(buf));
02871
02872 if (tcptls_session->ssl) {
02873 set_socket_transport(&req.socket, SIP_TRANSPORT_TLS);
02874 req.socket.port = htons(ourport_tls);
02875 } else {
02876 set_socket_transport(&req.socket, SIP_TRANSPORT_TCP);
02877 req.socket.port = htons(ourport_tcp);
02878 }
02879 req.socket.fd = tcptls_session->fd;
02880
02881 res = sip_tcptls_read(&req, tcptls_session, authenticated, start);
02882 if (res < 0) {
02883 goto cleanup;
02884 }
02885
02886 req.socket.tcptls_session = tcptls_session;
02887 handle_request_do(&req, &tcptls_session->remote_address);
02888 }
02889
02890 if (fds[1].revents) {
02891 enum sip_tcptls_alert alert;
02892 struct tcptls_packet *packet;
02893
02894 fds[1].revents = 0;
02895
02896 if (read(me->alert_pipe[0], &alert, sizeof(alert)) == -1) {
02897 ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
02898 continue;
02899 }
02900
02901 switch (alert) {
02902 case TCPTLS_ALERT_STOP:
02903 goto cleanup;
02904 case TCPTLS_ALERT_DATA:
02905 ao2_lock(me);
02906 if (!(packet = AST_LIST_REMOVE_HEAD(&me->packet_q, entry))) {
02907 ast_log(LOG_WARNING, "TCPTLS thread alert_pipe indicated packet should be sent, but frame_q is empty\n");
02908 }
02909 ao2_unlock(me);
02910
02911 if (packet) {
02912 if (ast_tcptls_server_write(tcptls_session, ast_str_buffer(packet->data), packet->len) == -1) {
02913 ast_log(LOG_WARNING, "Failure to write to tcp/tls socket\n");
02914 }
02915 ao2_t_ref(packet, -1, "tcptls packet sent, this is no longer needed");
02916 }
02917 break;
02918 default:
02919 ast_log(LOG_ERROR, "Unknown tcptls thread alert '%u'\n", alert);
02920 }
02921 }
02922 }
02923
02924 ast_debug(2, "Shutting down thread for %s server\n", tcptls_session->ssl ? "TLS" : "TCP");
02925
02926 cleanup:
02927 if (tcptls_session && !tcptls_session->client && !authenticated) {
02928 ast_atomic_fetchadd_int(&unauth_sessions, -1);
02929 }
02930
02931 if (me) {
02932 ao2_t_unlink(threadt, me, "Removing tcptls helper thread, thread is closing");
02933 ao2_t_ref(me, -1, "Removing tcp_helper_threads threadinfo ref");
02934 }
02935 deinit_req(&reqcpy);
02936 deinit_req(&req);
02937
02938
02939 if (ca) {
02940 ao2_t_ref(ca, -1, "closing tcptls thread, getting rid of client tcptls_session arguments");
02941 }
02942
02943 if (tcptls_session) {
02944 ast_mutex_lock(&tcptls_session->lock);
02945 ast_tcptls_close_session_file(tcptls_session);
02946 tcptls_session->parent = NULL;
02947 ast_mutex_unlock(&tcptls_session->lock);
02948
02949 ao2_ref(tcptls_session, -1);
02950 tcptls_session = NULL;
02951 }
02952 return NULL;
02953 }
02954
02955 #ifdef REF_DEBUG
02956 #define ref_peer(arg1,arg2) _ref_peer((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
02957 #define unref_peer(arg1,arg2) _unref_peer((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
02958 static struct sip_peer *_ref_peer(struct sip_peer *peer, char *tag, char *file, int line, const char *func)
02959 {
02960 if (peer)
02961 __ao2_ref_debug(peer, 1, tag, file, line, func);
02962 else
02963 ast_log(LOG_ERROR, "Attempt to Ref a null peer pointer\n");
02964 return peer;
02965 }
02966
02967 static struct sip_peer *_unref_peer(struct sip_peer *peer, char *tag, char *file, int line, const char *func)
02968 {
02969 if (peer)
02970 __ao2_ref_debug(peer, -1, tag, file, line, func);
02971 return NULL;
02972 }
02973 #else
02974
02975
02976
02977
02978
02979 static void *unref_peer(struct sip_peer *peer, char *tag)
02980 {
02981 ao2_t_ref(peer, -1, tag);
02982 return NULL;
02983 }
02984
02985 static struct sip_peer *ref_peer(struct sip_peer *peer, char *tag)
02986 {
02987 ao2_t_ref(peer, 1, tag);
02988 return peer;
02989 }
02990 #endif
02991
02992 static void peer_sched_cleanup(struct sip_peer *peer)
02993 {
02994 if (peer->pokeexpire != -1) {
02995 AST_SCHED_DEL_UNREF(sched, peer->pokeexpire,
02996 unref_peer(peer, "removing poke peer ref"));
02997 }
02998 if (peer->expire != -1) {
02999 AST_SCHED_DEL_UNREF(sched, peer->expire,
03000 unref_peer(peer, "remove register expire ref"));
03001 }
03002 }
03003
03004 typedef enum {
03005 SIP_PEERS_MARKED,
03006 SIP_PEERS_ALL,
03007 } peer_unlink_flag_t;
03008
03009
03010
03011 static int match_and_cleanup_peer_sched(void *peerobj, void *arg, int flags)
03012 {
03013 struct sip_peer *peer = peerobj;
03014 peer_unlink_flag_t which = *(peer_unlink_flag_t *)arg;
03015
03016 if (which == SIP_PEERS_ALL || peer->the_mark) {
03017 peer_sched_cleanup(peer);
03018 if (peer->dnsmgr) {
03019 ast_dnsmgr_release(peer->dnsmgr);
03020 peer->dnsmgr = NULL;
03021 unref_peer(peer, "Release peer from dnsmgr");
03022 }
03023 return CMP_MATCH;
03024 }
03025 return 0;
03026 }
03027
03028 static void unlink_peers_from_tables(peer_unlink_flag_t flag)
03029 {
03030 ao2_t_callback(peers, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE,
03031 match_and_cleanup_peer_sched, &flag, "initiating callback to remove marked peers");
03032 ao2_t_callback(peers_by_ip, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE,
03033 match_and_cleanup_peer_sched, &flag, "initiating callback to remove marked peers");
03034 }
03035
03036
03037 static void unlink_marked_peers_from_tables(void)
03038 {
03039 unlink_peers_from_tables(SIP_PEERS_MARKED);
03040 }
03041
03042 static void unlink_all_peers_from_tables(void)
03043 {
03044 unlink_peers_from_tables(SIP_PEERS_ALL);
03045 }
03046
03047
03048
03049
03050
03051
03052
03053
03054
03055
03056
03057
03058 static void ref_proxy(struct sip_pvt *pvt, struct sip_proxy *proxy)
03059 {
03060 struct sip_proxy *old_obproxy = pvt->outboundproxy;
03061
03062
03063
03064 if (proxy && proxy != &sip_cfg.outboundproxy) {
03065 ao2_ref(proxy, +1);
03066 }
03067 pvt->outboundproxy = proxy;
03068 if (old_obproxy && old_obproxy != &sip_cfg.outboundproxy) {
03069 ao2_ref(old_obproxy, -1);
03070 }
03071 }
03072
03073
03074
03075
03076
03077
03078
03079
03080 void dialog_unlink_all(struct sip_pvt *dialog)
03081 {
03082 struct sip_pkt *cp;
03083 struct ast_channel *owner;
03084
03085 dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
03086
03087 ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
03088
03089
03090 owner = sip_pvt_lock_full(dialog);
03091 if (owner) {
03092 ast_debug(1, "Detaching from channel %s\n", owner->name);
03093 owner->tech_pvt = dialog_unref(owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
03094 ast_channel_unlock(owner);
03095 ast_channel_unref(owner);
03096 dialog->owner = NULL;
03097 }
03098 sip_pvt_unlock(dialog);
03099
03100 if (dialog->registry) {
03101 if (dialog->registry->call == dialog) {
03102 dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
03103 }
03104 dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
03105 }
03106 if (dialog->stateid != -1) {
03107 ast_extension_state_del(dialog->stateid, cb_extensionstate);
03108 dialog->stateid = -1;
03109 }
03110
03111 if (dialog->relatedpeer && dialog->relatedpeer->mwipvt == dialog) {
03112 dialog->relatedpeer->mwipvt = dialog_unref(dialog->relatedpeer->mwipvt, "delete ->relatedpeer->mwipvt");
03113 }
03114 if (dialog->relatedpeer && dialog->relatedpeer->call == dialog) {
03115 dialog->relatedpeer->call = dialog_unref(dialog->relatedpeer->call, "unset the relatedpeer->call field in tandem with relatedpeer field itself");
03116 }
03117
03118
03119 while((cp = dialog->packets)) {
03120 dialog->packets = dialog->packets->next;
03121 AST_SCHED_DEL(sched, cp->retransid);
03122 dialog_unref(cp->owner, "remove all current packets in this dialog, and the pointer to the dialog too as part of __sip_destroy");
03123 if (cp->data) {
03124 ast_free(cp->data);
03125 }
03126 ast_free(cp);
03127 }
03128
03129 AST_SCHED_DEL_UNREF(sched, dialog->waitid, dialog_unref(dialog, "when you delete the waitid sched, you should dec the refcount for the stored dialog ptr"));
03130
03131 AST_SCHED_DEL_UNREF(sched, dialog->initid, dialog_unref(dialog, "when you delete the initid sched, you should dec the refcount for the stored dialog ptr"));
03132
03133 if (dialog->autokillid > -1) {
03134 AST_SCHED_DEL_UNREF(sched, dialog->autokillid, dialog_unref(dialog, "when you delete the autokillid sched, you should dec the refcount for the stored dialog ptr"));
03135 }
03136
03137 if (dialog->request_queue_sched_id > -1) {
03138 AST_SCHED_DEL_UNREF(sched, dialog->request_queue_sched_id, dialog_unref(dialog, "when you delete the request_queue_sched_id sched, you should dec the refcount for the stored dialog ptr"));
03139 }
03140
03141 AST_SCHED_DEL_UNREF(sched, dialog->provisional_keepalive_sched_id, dialog_unref(dialog, "when you delete the provisional_keepalive_sched_id, you should dec the refcount for the stored dialog ptr"));
03142
03143 if (dialog->t38id > -1) {
03144 AST_SCHED_DEL_UNREF(sched, dialog->t38id, dialog_unref(dialog, "when you delete the t38id sched, you should dec the refcount for the stored dialog ptr"));
03145 }
03146
03147 if (dialog->stimer) {
03148 stop_session_timer(dialog);
03149 }
03150
03151 dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
03152 }
03153
03154 void *registry_unref(struct sip_registry *reg, char *tag)
03155 {
03156 ast_debug(3, "SIP Registry %s: refcount now %u\n", reg->hostname, reg->refcount - 1);
03157 ASTOBJ_UNREF(reg, sip_registry_destroy);
03158 return NULL;
03159 }
03160
03161
03162 static struct sip_registry *registry_addref(struct sip_registry *reg, char *tag)
03163 {
03164 ast_debug(3, "SIP Registry %s: refcount now %u\n", reg->hostname, reg->refcount + 1);
03165 return ASTOBJ_REF(reg);
03166 }
03167
03168
03169 static struct ast_udptl_protocol sip_udptl = {
03170 .type = "SIP",
03171 .get_udptl_info = sip_get_udptl_peer,
03172 .set_udptl_peer = sip_set_udptl_peer,
03173 };
03174
03175 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
03176 __attribute__((format(printf, 2, 3)));
03177
03178
03179
03180 static const char *referstatus2str(enum referstatus rstatus)
03181 {
03182 return map_x_s(referstatusstrings, rstatus, "");
03183 }
03184
03185 static inline void pvt_set_needdestroy(struct sip_pvt *pvt, const char *reason)
03186 {
03187 if (pvt->final_destruction_scheduled) {
03188 return;
03189 }
03190 append_history(pvt, "NeedDestroy", "Setting needdestroy because %s", reason);
03191 pvt->needdestroy = 1;
03192 }
03193
03194
03195
03196
03197 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
03198 {
03199 if (p->initreq.headers) {
03200 ast_debug(1, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
03201 } else {
03202 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
03203 }
03204
03205 copy_request(&p->initreq, req);
03206 parse_request(&p->initreq);
03207 if (req->debug) {
03208 ast_verbose("Initreq: %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
03209 }
03210 }
03211
03212
03213 static void sip_alreadygone(struct sip_pvt *dialog)
03214 {
03215 ast_debug(3, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
03216 dialog->alreadygone = 1;
03217 }
03218
03219
03220 static int proxy_update(struct sip_proxy *proxy)
03221 {
03222
03223
03224 if (!ast_sockaddr_parse(&proxy->ip, proxy->name, 0)) {
03225
03226
03227 proxy->ip.ss.ss_family = get_address_family_filter(SIP_TRANSPORT_UDP);
03228 if (ast_get_ip_or_srv(&proxy->ip, proxy->name, sip_cfg.srvlookup ? "_sip._udp" : NULL) < 0) {
03229 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", proxy->name);
03230 return FALSE;
03231 }
03232
03233 }
03234
03235 ast_sockaddr_set_port(&proxy->ip, proxy->port);
03236
03237 proxy->last_dnsupdate = time(NULL);
03238 return TRUE;
03239 }
03240
03241
03242
03243
03244 static struct sip_proxy *proxy_from_config(const char *proxy, int sipconf_lineno, struct sip_proxy *dest)
03245 {
03246 char *mutable_proxy, *sep, *name;
03247 int allocated = 0;
03248
03249 if (!dest) {
03250 dest = ao2_alloc(sizeof(struct sip_proxy), NULL);
03251 if (!dest) {
03252 ast_log(LOG_WARNING, "Unable to allocate config storage for proxy\n");
03253 return NULL;
03254 }
03255 allocated = 1;
03256 }
03257
03258
03259 mutable_proxy = ast_skip_blanks(ast_strdupa(proxy));
03260 sep = strchr(mutable_proxy, ',');
03261 if (sep) {
03262 *sep++ = '\0';
03263 dest->force = !strncasecmp(ast_skip_blanks(sep), "force", 5);
03264 } else {
03265 dest->force = FALSE;
03266 }
03267
03268 sip_parse_host(mutable_proxy, sipconf_lineno, &name, &dest->port, &dest->transport);
03269
03270
03271 if (ast_strlen_zero(name)) {
03272 if (allocated) {
03273 ao2_ref(dest, -1);
03274 } else {
03275 dest->name[0] = '\0';
03276 }
03277 return NULL;
03278 }
03279 ast_copy_string(dest->name, name, sizeof(dest->name));
03280
03281
03282 proxy_update(dest);
03283
03284 return dest;
03285 }
03286
03287
03288
03289
03290
03291 unsigned int port_str2int(const char *pt, unsigned int standard)
03292 {
03293 int port = standard;
03294 if (ast_strlen_zero(pt) || (sscanf(pt, "%30d", &port) != 1) || (port < 1) || (port > 65535)) {
03295 port = standard;
03296 }
03297
03298 return port;
03299 }
03300
03301
03302 static struct sip_proxy *obproxy_get(struct sip_pvt *dialog, struct sip_peer *peer)
03303 {
03304 if (dialog && dialog->options && dialog->options->outboundproxy) {
03305 if (sipdebug) {
03306 ast_debug(1, "OBPROXY: Applying dialplan set OBproxy to this call\n");
03307 }
03308 append_history(dialog, "OBproxy", "Using dialplan obproxy %s", dialog->options->outboundproxy->name);
03309 return dialog->options->outboundproxy;
03310 }
03311 if (peer && peer->outboundproxy) {
03312 if (sipdebug) {
03313 ast_debug(1, "OBPROXY: Applying peer OBproxy to this call\n");
03314 }
03315 append_history(dialog, "OBproxy", "Using peer obproxy %s", peer->outboundproxy->name);
03316 return peer->outboundproxy;
03317 }
03318 if (sip_cfg.outboundproxy.name[0]) {
03319 if (sipdebug) {
03320 ast_debug(1, "OBPROXY: Applying global OBproxy to this call\n");
03321 }
03322 append_history(dialog, "OBproxy", "Using global obproxy %s", sip_cfg.outboundproxy.name);
03323 return &sip_cfg.outboundproxy;
03324 }
03325 if (sipdebug) {
03326 ast_debug(1, "OBPROXY: Not applying OBproxy to this call\n");
03327 }
03328 return NULL;
03329 }
03330
03331
03332
03333
03334
03335
03336
03337 static int method_match(enum sipmethod id, const char *name)
03338 {
03339 int len = strlen(sip_methods[id].text);
03340 int l_name = name ? strlen(name) : 0;
03341
03342 return (l_name >= len && name && name[len] < 33 &&
03343 !strncasecmp(sip_methods[id].text, name, len));
03344 }
03345
03346
03347 static int find_sip_method(const char *msg)
03348 {
03349 int i, res = 0;
03350
03351 if (ast_strlen_zero(msg)) {
03352 return 0;
03353 }
03354 for (i = 1; i < ARRAY_LEN(sip_methods) && !res; i++) {
03355 if (method_match(i, msg)) {
03356 res = sip_methods[i].id;
03357 }
03358 }
03359 return res;
03360 }
03361
03362
03363 static inline int sip_debug_test_addr(const struct ast_sockaddr *addr)
03364 {
03365
03366 if (!sipdebug) {
03367 return 0;
03368 }
03369
03370
03371 if (ast_sockaddr_isnull(&debugaddr)) {
03372 return 1;
03373 }
03374
03375
03376
03377
03378 if (ast_sockaddr_port(&debugaddr)) {
03379 return !ast_sockaddr_cmp(&debugaddr, addr);
03380 } else {
03381 return !ast_sockaddr_cmp_addr(&debugaddr, addr);
03382 }
03383 }
03384
03385
03386 static const struct ast_sockaddr *sip_real_dst(const struct sip_pvt *p)
03387 {
03388 if (p->outboundproxy) {
03389 return &p->outboundproxy->ip;
03390 }
03391
03392 return ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT) || ast_test_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT) ? &p->recv : &p->sa;
03393 }
03394
03395
03396 static const char *sip_nat_mode(const struct sip_pvt *p)
03397 {
03398 return ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT) ? "NAT" : "no NAT";
03399 }
03400
03401
03402 static inline int sip_debug_test_pvt(struct sip_pvt *p)
03403 {
03404 if (!sipdebug) {
03405 return 0;
03406 }
03407 return sip_debug_test_addr(sip_real_dst(p));
03408 }
03409
03410
03411 static int get_transport_str2enum(const char *transport)
03412 {
03413 int res = 0;
03414
03415 if (ast_strlen_zero(transport)) {
03416 return res;
03417 }
03418
03419 if (!strcasecmp(transport, "udp")) {
03420 res |= SIP_TRANSPORT_UDP;
03421 }
03422 if (!strcasecmp(transport, "tcp")) {
03423 res |= SIP_TRANSPORT_TCP;
03424 }
03425 if (!strcasecmp(transport, "tls")) {
03426 res |= SIP_TRANSPORT_TLS;
03427 }
03428
03429 return res;
03430 }
03431
03432
03433 static inline const char *get_transport_list(unsigned int transports) {
03434 switch (transports) {
03435 case SIP_TRANSPORT_UDP:
03436 return "UDP";
03437 case SIP_TRANSPORT_TCP:
03438 return "TCP";
03439 case SIP_TRANSPORT_TLS:
03440 return "TLS";
03441 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TCP:
03442 return "TCP,UDP";
03443 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TLS:
03444 return "TLS,UDP";
03445 case SIP_TRANSPORT_TCP | SIP_TRANSPORT_TLS:
03446 return "TLS,TCP";
03447 default:
03448 return transports ?
03449 "TLS,TCP,UDP" : "UNKNOWN";
03450 }
03451 }
03452
03453
03454 static inline const char *get_transport(enum sip_transport t)
03455 {
03456 switch (t) {
03457 case SIP_TRANSPORT_UDP:
03458 return "UDP";
03459 case SIP_TRANSPORT_TCP:
03460 return "TCP";
03461 case SIP_TRANSPORT_TLS:
03462 return "TLS";
03463 }
03464
03465 return "UNKNOWN";
03466 }
03467
03468
03469 static inline const char *get_srv_protocol(enum sip_transport t)
03470 {
03471 switch (t) {
03472 case SIP_TRANSPORT_UDP:
03473 return "udp";
03474 case SIP_TRANSPORT_TLS:
03475 case SIP_TRANSPORT_TCP:
03476 return "tcp";
03477 }
03478
03479 return "udp";
03480 }
03481
03482
03483 static inline const char *get_srv_service(enum sip_transport t)
03484 {
03485 switch (t) {
03486 case SIP_TRANSPORT_TCP:
03487 case SIP_TRANSPORT_UDP:
03488 return "sip";
03489 case SIP_TRANSPORT_TLS:
03490 return "sips";
03491 }
03492 return "sip";
03493 }
03494
03495
03496
03497
03498
03499
03500
03501 static inline const char *get_transport_pvt(struct sip_pvt *p)
03502 {
03503 if (p->outboundproxy && p->outboundproxy->transport) {
03504 set_socket_transport(&p->socket, p->outboundproxy->transport);
03505 }
03506
03507 return get_transport(p->socket.type);
03508 }
03509
03510
03511
03512
03513
03514
03515
03516
03517
03518
03519
03520
03521 static int __sip_xmit(struct sip_pvt *p, struct ast_str *data)
03522 {
03523 int res = 0;
03524 const struct ast_sockaddr *dst = sip_real_dst(p);
03525
03526 ast_debug(2, "Trying to put '%.11s' onto %s socket destined for %s\n", ast_str_buffer(data), get_transport_pvt(p), ast_sockaddr_stringify(dst));
03527
03528 if (sip_prepare_socket(p) < 0) {
03529 return XMIT_ERROR;
03530 }
03531
03532 if (p->socket.type == SIP_TRANSPORT_UDP) {
03533 res = ast_sendto(p->socket.fd, ast_str_buffer(data), ast_str_strlen(data), 0, dst);
03534 } else if (p->socket.tcptls_session) {
03535 res = sip_tcptls_write(p->socket.tcptls_session, ast_str_buffer(data), ast_str_strlen(data));
03536 } else {
03537 ast_debug(2, "Socket type is TCP but no tcptls_session is present to write to\n");
03538 return XMIT_ERROR;
03539 }
03540
03541 if (res == -1) {
03542 switch (errno) {
03543 case EBADF:
03544 case EHOSTUNREACH:
03545 case ENETDOWN:
03546 case ENETUNREACH:
03547 case ECONNREFUSED:
03548 res = XMIT_ERROR;
03549 }
03550 }
03551 if (res != ast_str_strlen(data)) {
03552 ast_log(LOG_WARNING, "sip_xmit of %p (len %zu) to %s returned %d: %s\n", data, ast_str_strlen(data), ast_sockaddr_stringify(dst), res, strerror(errno));
03553 }
03554
03555 return res;
03556 }
03557
03558
03559 static void build_via(struct sip_pvt *p)
03560 {
03561
03562 const char *rport = (ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT) || ast_test_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT)) ? ";rport" : "";
03563
03564
03565 snprintf(p->via, sizeof(p->via), "SIP/2.0/%s %s;branch=z9hG4bK%08x%s",
03566 get_transport_pvt(p),
03567 ast_sockaddr_stringify_remote(&p->ourip),
03568 (unsigned)p->branch, rport);
03569 }
03570
03571
03572
03573
03574
03575
03576
03577
03578 static void ast_sip_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us, struct sip_pvt *p)
03579 {
03580 struct ast_sockaddr theirs;
03581
03582
03583
03584
03585
03586
03587
03588
03589
03590
03591
03592
03593
03594 int want_remap = 0;
03595
03596 ast_sockaddr_copy(us, &internip);
03597
03598 ast_ouraddrfor(them, us);
03599 ast_sockaddr_copy(&theirs, them);
03600
03601 if (ast_sockaddr_is_ipv6(&theirs)) {
03602 if (localaddr && !ast_sockaddr_isnull(&externaddr) && !ast_sockaddr_is_any(&bindaddr)) {
03603 ast_log(LOG_WARNING, "Address remapping activated in sip.conf "
03604 "but we're using IPv6, which doesn't need it. Please "
03605 "remove \"localnet\" and/or \"externaddr\" settings.\n");
03606 }
03607 } else {
03608 want_remap = localaddr &&
03609 !ast_sockaddr_isnull(&externaddr) &&
03610 ast_apply_ha(localaddr, &theirs) == AST_SENSE_ALLOW ;
03611 }
03612
03613 if (want_remap &&
03614 (!sip_cfg.matchexternaddrlocally || !ast_apply_ha(localaddr, us)) ) {
03615
03616 if (externexpire && time(NULL) >= externexpire) {
03617 if (ast_sockaddr_resolve_first(&externaddr, externhost, 0)) {
03618 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
03619 }
03620 externexpire = time(NULL) + externrefresh;
03621 }
03622 if (!ast_sockaddr_isnull(&externaddr)) {
03623 ast_sockaddr_copy(us, &externaddr);
03624 switch (p->socket.type) {
03625 case SIP_TRANSPORT_TCP:
03626 if (!externtcpport && ast_sockaddr_port(&externaddr)) {
03627
03628 externtcpport = ast_sockaddr_port(&externaddr);
03629 }
03630 ast_sockaddr_set_port(us, externtcpport);
03631 break;
03632 case SIP_TRANSPORT_TLS:
03633 ast_sockaddr_set_port(us, externtlsport);
03634 break;
03635 case SIP_TRANSPORT_UDP:
03636 if (!ast_sockaddr_port(&externaddr)) {
03637 ast_sockaddr_set_port(us, ast_sockaddr_port(&bindaddr));
03638 }
03639 break;
03640 default:
03641 break;
03642 }
03643 }
03644 ast_debug(1, "Target address %s is not local, substituting externaddr\n",
03645 ast_sockaddr_stringify(them));
03646 } else {
03647
03648 switch (p->socket.type) {
03649 case SIP_TRANSPORT_TCP:
03650 if (!ast_sockaddr_is_any(&sip_tcp_desc.local_address)) {
03651 ast_sockaddr_copy(us,
03652 &sip_tcp_desc.local_address);
03653 } else {
03654 ast_sockaddr_set_port(us,
03655 ast_sockaddr_port(&sip_tcp_desc.local_address));
03656 }
03657 break;
03658 case SIP_TRANSPORT_TLS:
03659 if (!ast_sockaddr_is_any(&sip_tls_desc.local_address)) {
03660 ast_sockaddr_copy(us,
03661 &sip_tls_desc.local_address);
03662 } else {
03663 ast_sockaddr_set_port(us,
03664 ast_sockaddr_port(&sip_tls_desc.local_address));
03665 }
03666 break;
03667 case SIP_TRANSPORT_UDP:
03668
03669 default:
03670 if (!ast_sockaddr_is_any(&bindaddr)) {
03671 ast_sockaddr_copy(us, &bindaddr);
03672 }
03673 if (!ast_sockaddr_port(us)) {
03674 ast_sockaddr_set_port(us, ast_sockaddr_port(&bindaddr));
03675 }
03676 }
03677 }
03678 ast_debug(3, "Setting SIP_TRANSPORT_%s with address %s\n", get_transport(p->socket.type), ast_sockaddr_stringify(us));
03679 }
03680
03681
03682 static __attribute__((format(printf, 2, 0))) void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
03683 {
03684 char buf[80], *c = buf;
03685 struct sip_history *hist;
03686 int l;
03687
03688 vsnprintf(buf, sizeof(buf), fmt, ap);
03689 strsep(&c, "\r\n");
03690 l = strlen(buf) + 1;
03691 if (!(hist = ast_calloc(1, sizeof(*hist) + l))) {
03692 return;
03693 }
03694 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
03695 ast_free(hist);
03696 return;
03697 }
03698 memcpy(hist->event, buf, l);
03699 if (p->history_entries == MAX_HISTORY_ENTRIES) {
03700 struct sip_history *oldest;
03701 oldest = AST_LIST_REMOVE_HEAD(p->history, list);
03702 p->history_entries--;
03703 ast_free(oldest);
03704 }
03705 AST_LIST_INSERT_TAIL(p->history, hist, list);
03706 p->history_entries++;
03707 }
03708
03709
03710 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
03711 {
03712 va_list ap;
03713
03714 if (!p) {
03715 return;
03716 }
03717
03718 if (!p->do_history && !recordhistory && !dumphistory) {
03719 return;
03720 }
03721
03722 va_start(ap, fmt);
03723 append_history_va(p, fmt, ap);
03724 va_end(ap);
03725
03726 return;
03727 }
03728
03729
03730 static int retrans_pkt(const void *data)
03731 {
03732 struct sip_pkt *pkt = (struct sip_pkt *)data, *prev, *cur = NULL;
03733 int reschedule = DEFAULT_RETRANS;
03734 int xmitres = 0;
03735
03736 int64_t diff = pkt->retrans_stop_time - ast_tvdiff_ms(ast_tvnow(), pkt->time_sent);
03737
03738
03739
03740
03741 if ((diff <= 0) || (diff > pkt->retrans_stop_time)) {
03742 pkt->retrans_stop = 1;
03743 }
03744
03745
03746 sip_pvt_lock(pkt->owner);
03747
03748 if (!pkt->retrans_stop) {
03749 pkt->retrans++;
03750 if (!pkt->timer_t1) {
03751 if (sipdebug) {
03752 ast_debug(4, "SIP TIMER: Not rescheduling id #%d:%s (Method %d) (No timer T1)\n",
03753 pkt->retransid,
03754 sip_methods[pkt->method].text,
03755 pkt->method);
03756 }
03757 } else {
03758 int siptimer_a;
03759
03760 if (sipdebug) {
03761 ast_debug(4, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n",
03762 pkt->retransid,
03763 pkt->retrans,
03764 sip_methods[pkt->method].text,
03765 pkt->method);
03766 }
03767 if (!pkt->timer_a) {
03768 pkt->timer_a = 2 ;
03769 } else {
03770 pkt->timer_a = 2 * pkt->timer_a;
03771 }
03772
03773
03774 siptimer_a = pkt->timer_t1 * pkt->timer_a;
03775 if (pkt->method != SIP_INVITE && siptimer_a > 4000) {
03776 siptimer_a = 4000;
03777 }
03778
03779
03780 reschedule = siptimer_a;
03781 ast_debug(4, "** SIP timers: Rescheduling retransmission %d to %d ms (t1 %d ms (Retrans id #%d)) \n",
03782 pkt->retrans + 1,
03783 siptimer_a,
03784 pkt->timer_t1,
03785 pkt->retransid);
03786 }
03787
03788 if (sip_debug_test_pvt(pkt->owner)) {
03789 const struct ast_sockaddr *dst = sip_real_dst(pkt->owner);
03790 ast_verbose("Retransmitting #%d (%s) to %s:\n%s\n---\n",
03791 pkt->retrans, sip_nat_mode(pkt->owner),
03792 ast_sockaddr_stringify(dst),
03793 ast_str_buffer(pkt->data));
03794 }
03795
03796 append_history(pkt->owner, "ReTx", "%d %s", reschedule, ast_str_buffer(pkt->data));
03797 xmitres = __sip_xmit(pkt->owner, pkt->data);
03798
03799
03800
03801
03802
03803
03804 if (xmitres != XMIT_ERROR) {
03805 if (reschedule >= diff) {
03806 pkt->retrans_stop = 1;
03807 reschedule = diff;
03808 }
03809 sip_pvt_unlock(pkt->owner);
03810 return reschedule;
03811 }
03812 }
03813
03814
03815
03816
03817 pkt->retransid = -1;
03818
03819 if (pkt->method != SIP_OPTIONS && xmitres == 0) {
03820 if (pkt->is_fatal || sipdebug) {
03821 ast_log(LOG_WARNING, "Retransmission timeout reached on transmission %s for seqno %u (%s %s) -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions\n"
03822 "Packet timed out after %dms with no response\n",
03823 pkt->owner->callid,
03824 pkt->seqno,
03825 pkt->is_fatal ? "Critical" : "Non-critical",
03826 pkt->is_resp ? "Response" : "Request",
03827 (int) ast_tvdiff_ms(ast_tvnow(), pkt->time_sent));
03828 }
03829 } else if (pkt->method == SIP_OPTIONS && sipdebug) {
03830 ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s) -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions\n", pkt->owner->callid);
03831 }
03832
03833 if (xmitres == XMIT_ERROR) {
03834 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission on Call ID %s\n", pkt->owner->callid);
03835 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03836 } else {
03837 append_history(pkt->owner, "MaxRetries", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03838 }
03839
03840 if (pkt->is_fatal) {
03841 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
03842 sip_pvt_unlock(pkt->owner);
03843 usleep(1);
03844 sip_pvt_lock(pkt->owner);
03845 }
03846 if (pkt->owner->owner && !pkt->owner->owner->hangupcause) {
03847 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
03848 }
03849 if (pkt->owner->owner) {
03850 ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet (see https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions).\n", pkt->owner->callid);
03851
03852 if (pkt->is_resp &&
03853 (pkt->response_code >= 200) &&
03854 (pkt->response_code < 300) &&
03855 pkt->owner->pendinginvite &&
03856 ast_test_flag(&pkt->owner->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
03857
03858
03859
03860
03861
03862
03863
03864 pkt->owner->invitestate = INV_TERMINATED;
03865 pkt->owner->pendinginvite = 0;
03866 } else {
03867
03868 sip_alreadygone(pkt->owner);
03869 }
03870 ast_queue_hangup_with_cause(pkt->owner->owner, AST_CAUSE_NO_USER_RESPONSE);
03871 ast_channel_unlock(pkt->owner->owner);
03872 } else {
03873
03874
03875
03876 if (pkt->method != SIP_OPTIONS && pkt->method != SIP_REGISTER) {
03877 pvt_set_needdestroy(pkt->owner, "no response to critical packet");
03878 sip_alreadygone(pkt->owner);
03879 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
03880 }
03881 }
03882 } else if (pkt->owner->pendinginvite == pkt->seqno) {
03883 ast_log(LOG_WARNING, "Timeout on %s on non-critical invite transaction.\n", pkt->owner->callid);
03884 pkt->owner->invitestate = INV_TERMINATED;
03885 pkt->owner->pendinginvite = 0;
03886 check_pendings(pkt->owner);
03887 }
03888
03889 if (pkt->method == SIP_BYE) {
03890
03891 sip_alreadygone(pkt->owner);
03892 if (pkt->owner->owner) {
03893 ast_channel_unlock(pkt->owner->owner);
03894 }
03895 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
03896 pvt_set_needdestroy(pkt->owner, "no response to BYE");
03897 }
03898
03899
03900 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
03901 if (cur == pkt) {
03902 UNLINK(cur, pkt->owner->packets, prev);
03903 sip_pvt_unlock(pkt->owner);
03904 if (pkt->owner) {
03905 pkt->owner = dialog_unref(pkt->owner,"pkt is being freed, its dialog ref is dead now");
03906 }
03907 if (pkt->data) {
03908 ast_free(pkt->data);
03909 }
03910 pkt->data = NULL;
03911 ast_free(pkt);
03912 return 0;
03913 }
03914 }
03915
03916 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
03917 sip_pvt_unlock(pkt->owner);
03918 return 0;
03919 }
03920
03921
03922
03923
03924
03925
03926 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, uint32_t seqno, int resp, struct ast_str *data, int fatal, int sipmethod)
03927 {
03928 struct sip_pkt *pkt = NULL;
03929 int siptimer_a = DEFAULT_RETRANS;
03930 int xmitres = 0;
03931 unsigned respid;
03932
03933 if (sipmethod == SIP_INVITE) {
03934
03935 p->pendinginvite = seqno;
03936 }
03937
03938
03939
03940
03941 if (!(p->socket.type & SIP_TRANSPORT_UDP)) {
03942 xmitres = __sip_xmit(p, data);
03943 if (xmitres == XMIT_ERROR) {
03944 append_history(p, "XmitErr", "%s", fatal ? "(Critical)" : "(Non-critical)");
03945 return AST_FAILURE;
03946 } else {
03947 return AST_SUCCESS;
03948 }
03949 }
03950
03951 if (!(pkt = ast_calloc(1, sizeof(*pkt)))) {
03952 return AST_FAILURE;
03953 }
03954
03955 if (!(pkt->data = ast_str_create(ast_str_strlen(data)))) {
03956 ast_free(pkt);
03957 return AST_FAILURE;
03958 }
03959 ast_str_set(&pkt->data, 0, "%s%s", ast_str_buffer(data), "\0");
03960
03961 pkt->method = sipmethod;
03962 pkt->seqno = seqno;
03963 pkt->is_resp = resp;
03964 pkt->is_fatal = fatal;
03965 pkt->owner = dialog_ref(p, "__sip_reliable_xmit: setting pkt->owner");
03966 pkt->next = p->packets;
03967 p->packets = pkt;
03968 if (resp) {
03969
03970 if (sscanf(ast_str_buffer(pkt->data), "SIP/2.0 %30u", &respid) == 1) {
03971 pkt->response_code = respid;
03972 }
03973 }
03974 pkt->timer_t1 = p->timer_t1;
03975 pkt->retransid = -1;
03976 if (pkt->timer_t1) {
03977 siptimer_a = pkt->timer_t1;
03978 }
03979
03980 pkt->time_sent = ast_tvnow();
03981 pkt->retrans_stop_time = 64 * (pkt->timer_t1 ? pkt->timer_t1 : DEFAULT_TIMER_T1);
03982
03983
03984 AST_SCHED_REPLACE_VARIABLE(pkt->retransid, sched, siptimer_a, retrans_pkt, pkt, 1);
03985 if (sipdebug) {
03986 ast_debug(4, "*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->retransid);
03987 }
03988
03989 xmitres = __sip_xmit(pkt->owner, pkt->data);
03990
03991 if (xmitres == XMIT_ERROR) {
03992 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03993 ast_log(LOG_ERROR, "Serious Network Trouble; __sip_xmit returns error for pkt data\n");
03994 AST_SCHED_DEL(sched, pkt->retransid);
03995 p->packets = pkt->next;
03996 pkt->owner = dialog_unref(pkt->owner,"pkt is being freed, its dialog ref is dead now");
03997 ast_free(pkt->data);
03998 ast_free(pkt);
03999 return AST_FAILURE;
04000 } else {
04001
04002
04003
04004
04005 if (monitor_thread != AST_PTHREADT_NULL) {
04006 pthread_kill(monitor_thread, SIGURG);
04007 }
04008 return AST_SUCCESS;
04009 }
04010 }
04011
04012
04013
04014
04015
04016
04017 static int __sip_autodestruct(const void *data)
04018 {
04019 struct sip_pvt *p = (struct sip_pvt *)data;
04020 struct ast_channel *owner;
04021
04022
04023 if (p->subscribed && p->subscribed != MWI_NOTIFICATION && p->subscribed != CALL_COMPLETION) {
04024 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
04025 p->subscribed = NONE;
04026 append_history(p, "Subscribestatus", "timeout");
04027 ast_debug(3, "Re-scheduled destruction of SIP subscription %s\n", p->callid ? p->callid : "<unknown>");
04028 return 10000;
04029 }
04030
04031
04032 if (p->packets) {
04033 if (!p->needdestroy) {
04034 char method_str[31];
04035 ast_debug(3, "Re-scheduled destruction of SIP call %s\n", p->callid ? p->callid : "<unknown>");
04036 append_history(p, "ReliableXmit", "timeout");
04037 if (sscanf(p->lastmsg, "Tx: %30s", method_str) == 1 || sscanf(p->lastmsg, "Rx: %30s", method_str) == 1) {
04038 if (p->ongoing_reinvite || method_match(SIP_CANCEL, method_str) || method_match(SIP_BYE, method_str)) {
04039 pvt_set_needdestroy(p, "autodestruct");
04040 }
04041 }
04042 return 10000;
04043 } else {
04044
04045 __sip_pretend_ack(p);
04046 }
04047 }
04048
04049
04050 p->autokillid = -1;
04051
04052
04053
04054
04055 owner = sip_pvt_lock_full(p);
04056 if (owner) {
04057 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner %s in place (Method: %s). Rescheduling destruction for 10000 ms\n", p->callid, owner->name, sip_methods[p->method].text);
04058 ast_queue_hangup_with_cause(owner, AST_CAUSE_PROTOCOL_ERROR);
04059 ast_channel_unlock(owner);
04060 ast_channel_unref(owner);
04061 sip_pvt_unlock(p);
04062 return 10000;
04063 } else if (p->refer && !p->alreadygone) {
04064 ast_debug(3, "Finally hanging up channel after transfer: %s\n", p->callid);
04065 stop_media_flows(p);
04066 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
04067 append_history(p, "ReferBYE", "Sending BYE on transferer call leg %s", p->callid);
04068 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
04069 } else {
04070 append_history(p, "AutoDestroy", "%s", p->callid);
04071 ast_debug(3, "Auto destroying SIP dialog '%s'\n", p->callid);
04072 sip_pvt_unlock(p);
04073 dialog_unlink_all(p);
04074 sip_pvt_lock(p);
04075
04076
04077
04078 }
04079
04080 sip_pvt_unlock(p);
04081
04082 dialog_unref(p, "The ref to a dialog passed to this sched callback is going out of scope; unref it.");
04083
04084 return 0;
04085 }
04086
04087
04088
04089
04090 void sip_scheddestroy_final(struct sip_pvt *p, int ms)
04091 {
04092 if (p->final_destruction_scheduled) {
04093 return;
04094 }
04095
04096 sip_scheddestroy(p, ms);
04097 if (p->autokillid != -1) {
04098 p->final_destruction_scheduled = 1;
04099 }
04100 }
04101
04102
04103 void sip_scheddestroy(struct sip_pvt *p, int ms)
04104 {
04105 if (p->final_destruction_scheduled) {
04106 return;
04107 }
04108
04109 if (ms < 0) {
04110 if (p->timer_t1 == 0) {
04111 p->timer_t1 = global_t1;
04112 }
04113 if (p->timer_b == 0) {
04114 p->timer_b = global_timer_b;
04115 }
04116 ms = p->timer_t1 * 64;
04117 }
04118 if (sip_debug_test_pvt(p)) {
04119 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
04120 }
04121 if (sip_cancel_destroy(p)) {
04122 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
04123 }
04124
04125 if (p->do_history) {
04126 append_history(p, "SchedDestroy", "%d ms", ms);
04127 }
04128 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, dialog_ref(p, "setting ref as passing into ast_sched_add for __sip_autodestruct"));
04129
04130 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_schedid > 0) {
04131 stop_session_timer(p);
04132 }
04133 }
04134
04135
04136
04137
04138
04139 int sip_cancel_destroy(struct sip_pvt *p)
04140 {
04141 if (p->final_destruction_scheduled) {
04142 return 0;
04143 }
04144
04145 if (p->autokillid > -1) {
04146 append_history(p, "CancelDestroy", "");
04147 AST_SCHED_DEL_UNREF(sched, p->autokillid, dialog_unref(p, "remove ref for autokillid"));
04148 }
04149 return 0;
04150 }
04151
04152
04153
04154 int __sip_ack(struct sip_pvt *p, uint32_t seqno, int resp, int sipmethod)
04155 {
04156 struct sip_pkt *cur, *prev = NULL;
04157 const char *msg = "Not Found";
04158 int res = FALSE;
04159
04160
04161
04162
04163
04164
04165 if (p->outboundproxy && !p->outboundproxy->force) {
04166 ref_proxy(p, NULL);
04167 }
04168
04169 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
04170 if (cur->seqno != seqno || cur->is_resp != resp) {
04171 continue;
04172 }
04173 if (cur->is_resp || cur->method == sipmethod) {
04174 res = TRUE;
04175 msg = "Found";
04176 if (!resp && (seqno == p->pendinginvite)) {
04177 ast_debug(1, "Acked pending invite %u\n", p->pendinginvite);
04178 p->pendinginvite = 0;
04179 }
04180 if (cur->retransid > -1) {
04181 if (sipdebug)
04182 ast_debug(4, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
04183 }
04184
04185
04186
04187
04188
04189
04190
04191
04192
04193
04194
04195
04196
04197
04198
04199
04200 while (cur->retransid > -1 && ast_sched_del(sched, cur->retransid)) {
04201 sip_pvt_unlock(p);
04202 usleep(1);
04203 sip_pvt_lock(p);
04204 }
04205 UNLINK(cur, p->packets, prev);
04206 dialog_unref(cur->owner, "unref pkt cur->owner dialog from sip ack before freeing pkt");
04207 if (cur->data) {
04208 ast_free(cur->data);
04209 }
04210 ast_free(cur);
04211 break;
04212 }
04213 }
04214 ast_debug(1, "Stopping retransmission on '%s' of %s %u: Match %s\n",
04215 p->callid, resp ? "Response" : "Request", seqno, msg);
04216 return res;
04217 }
04218
04219
04220
04221 void __sip_pretend_ack(struct sip_pvt *p)
04222 {
04223 struct sip_pkt *cur = NULL;
04224
04225 while (p->packets) {
04226 int method;
04227 if (cur == p->packets) {
04228 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
04229 return;
04230 }
04231 cur = p->packets;
04232 method = (cur->method) ? cur->method : find_sip_method(ast_str_buffer(cur->data));
04233 __sip_ack(p, cur->seqno, cur->is_resp, method);
04234 }
04235 }
04236
04237
04238 int __sip_semi_ack(struct sip_pvt *p, uint32_t seqno, int resp, int sipmethod)
04239 {
04240 struct sip_pkt *cur;
04241 int res = FALSE;
04242
04243 for (cur = p->packets; cur; cur = cur->next) {
04244 if (cur->seqno == seqno && cur->is_resp == resp &&
04245 (cur->is_resp || method_match(sipmethod, ast_str_buffer(cur->data)))) {
04246
04247 if (cur->retransid > -1) {
04248 if (sipdebug)
04249 ast_debug(4, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
04250 }
04251 AST_SCHED_DEL(sched, cur->retransid);
04252 res = TRUE;
04253 break;
04254 }
04255 }
04256 ast_debug(1, "(Provisional) Stopping retransmission (but retaining packet) on '%s' %s %u: %s\n", p->callid, resp ? "Response" : "Request", seqno, res == -1 ? "Not Found" : "Found");
04257 return res;
04258 }
04259
04260
04261
04262 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
04263 {
04264 copy_request(dst, src);
04265 parse_request(dst);
04266 }
04267
04268
04269 static void add_blank(struct sip_request *req)
04270 {
04271 if (!req->lines) {
04272
04273 ast_str_append(&req->data, 0, "\r\n");
04274 }
04275 }
04276
04277 static int send_provisional_keepalive_full(struct sip_pvt *pvt, int with_sdp)
04278 {
04279 const char *msg = NULL;
04280 struct ast_channel *chan;
04281 int res = 0;
04282 int old_sched_id = pvt->provisional_keepalive_sched_id;
04283
04284 chan = sip_pvt_lock_full(pvt);
04285
04286 if (old_sched_id != pvt->provisional_keepalive_sched_id) {
04287
04288 if (chan) {
04289 ast_channel_unlock(chan);
04290 chan = ast_channel_unref(chan);
04291 }
04292 sip_pvt_unlock(pvt);
04293 dialog_unref(pvt, "dialog ref for provisional keepalive");
04294 return 0;
04295 }
04296
04297 if (!pvt->last_provisional || !strncasecmp(pvt->last_provisional, "100", 3)) {
04298 msg = "183 Session Progress";
04299 }
04300
04301 if (pvt->invitestate < INV_COMPLETED) {
04302 if (with_sdp) {
04303 transmit_response_with_sdp(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq, XMIT_UNRELIABLE, FALSE, FALSE);
04304 } else {
04305 transmit_response(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq);
04306 }
04307 res = PROVIS_KEEPALIVE_TIMEOUT;
04308 }
04309
04310 if (chan) {
04311 ast_channel_unlock(chan);
04312 chan = ast_channel_unref(chan);
04313 }
04314
04315 if (!res) {
04316 pvt->provisional_keepalive_sched_id = -1;
04317 }
04318
04319 sip_pvt_unlock(pvt);
04320
04321 if (!res) {
04322 dialog_unref(pvt, "dialog ref for provisional keepalive");
04323 }
04324 return res;
04325 }
04326
04327 static int send_provisional_keepalive(const void *data) {
04328 struct sip_pvt *pvt = (struct sip_pvt *) data;
04329
04330 return send_provisional_keepalive_full(pvt, 0);
04331 }
04332
04333 static int send_provisional_keepalive_with_sdp(const void *data) {
04334 struct sip_pvt *pvt = (void *)data;
04335
04336 return send_provisional_keepalive_full(pvt, 1);
04337 }
04338
04339 static void update_provisional_keepalive(struct sip_pvt *pvt, int with_sdp)
04340 {
04341 AST_SCHED_DEL_UNREF(sched, pvt->provisional_keepalive_sched_id, dialog_unref(pvt, "when you delete the provisional_keepalive_sched_id, you should dec the refcount for the stored dialog ptr"));
04342
04343 pvt->provisional_keepalive_sched_id = ast_sched_add(sched, PROVIS_KEEPALIVE_TIMEOUT,
04344 with_sdp ? send_provisional_keepalive_with_sdp : send_provisional_keepalive, dialog_ref(pvt, "Increment refcount to pass dialog pointer to sched callback"));
04345 }
04346
04347 static void add_required_respheader(struct sip_request *req)
04348 {
04349 struct ast_str *str;
04350 int i;
04351
04352 if (!req->reqsipoptions) {
04353 return;
04354 }
04355
04356 str = ast_str_create(32);
04357
04358 for (i = 0; i < ARRAY_LEN(sip_options); ++i) {
04359 if (!(req->reqsipoptions & sip_options[i].id)) {
04360 continue;
04361 }
04362 if (ast_str_strlen(str) > 0) {
04363 ast_str_append(&str, 0, ", ");
04364 }
04365 ast_str_append(&str, 0, "%s", sip_options[i].text);
04366 }
04367
04368 if (ast_str_strlen(str) > 0) {
04369 add_header(req, "Require", ast_str_buffer(str));
04370 }
04371
04372 ast_free(str);
04373 }
04374
04375
04376 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, uint32_t seqno)
04377 {
04378 int res;
04379
04380 finalize_content(req);
04381 add_blank(req);
04382 if (sip_debug_test_pvt(p)) {
04383 const struct ast_sockaddr *dst = sip_real_dst(p);
04384
04385 ast_verbose("\n<--- %sTransmitting (%s) to %s --->\n%s\n<------------>\n",
04386 reliable ? "Reliably " : "", sip_nat_mode(p),
04387 ast_sockaddr_stringify(dst),
04388 ast_str_buffer(req->data));
04389 }
04390 if (p->do_history) {
04391 struct sip_request tmp = { .rlPart1 = 0, };
04392 parse_copy(&tmp, req);
04393 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", ast_str_buffer(tmp.data), get_header(&tmp, "CSeq"),
04394 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? REQ_OFFSET_TO_STR(&tmp, rlPart2) : sip_methods[tmp.method].text);
04395 deinit_req(&tmp);
04396 }
04397
04398
04399 if (p->initreq.method == SIP_INVITE && reliable == XMIT_CRITICAL) {
04400 AST_SCHED_DEL_UNREF(sched, p->provisional_keepalive_sched_id, dialog_unref(p, "when you delete the provisional_keepalive_sched_id, you should dec the refcount for the stored dialog ptr"));
04401 }
04402
04403 res = (reliable) ?
04404 __sip_reliable_xmit(p, seqno, 1, req->data, (reliable == XMIT_CRITICAL), req->method) :
04405 __sip_xmit(p, req->data);
04406 deinit_req(req);
04407 if (res > 0) {
04408 return 0;
04409 }
04410 return res;
04411 }
04412
04413
04414
04415
04416
04417
04418 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, uint32_t seqno)
04419 {
04420 int res;
04421
04422
04423
04424
04425 if (p->outboundproxy) {
04426 p->sa = p->outboundproxy->ip;
04427 }
04428
04429 finalize_content(req);
04430 add_blank(req);
04431 if (sip_debug_test_pvt(p)) {
04432 if (ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT)) {
04433 ast_verbose("%sTransmitting (NAT) to %s:\n%s\n---\n", reliable ? "Reliably " : "", ast_sockaddr_stringify(&p->recv), ast_str_buffer(req->data));
04434 } else {
04435 ast_verbose("%sTransmitting (no NAT) to %s:\n%s\n---\n", reliable ? "Reliably " : "", ast_sockaddr_stringify(&p->sa), ast_str_buffer(req->data));
04436 }
04437 }
04438 if (p->do_history) {
04439 struct sip_request tmp = { .rlPart1 = 0, };
04440 parse_copy(&tmp, req);
04441 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", ast_str_buffer(tmp.data), get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
04442 deinit_req(&tmp);
04443 }
04444 res = (reliable) ?
04445 __sip_reliable_xmit(p, seqno, 0, req->data, (reliable == XMIT_CRITICAL), req->method) :
04446 __sip_xmit(p, req->data);
04447 deinit_req(req);
04448 return res;
04449 }
04450
04451 static void enable_dsp_detect(struct sip_pvt *p)
04452 {
04453 int features = 0;
04454
04455 if (p->dsp) {
04456 return;
04457 }
04458
04459 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
04460 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
04461 if (p->rtp) {
04462 ast_rtp_instance_dtmf_mode_set(p->rtp, AST_RTP_DTMF_MODE_INBAND);
04463 }
04464 features |= DSP_FEATURE_DIGIT_DETECT;
04465 }
04466
04467 if (ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_CNG)) {
04468 features |= DSP_FEATURE_FAX_DETECT;
04469 }
04470
04471 if (!features) {
04472 return;
04473 }
04474
04475 if (!(p->dsp = ast_dsp_new())) {
04476 return;
04477 }
04478
04479 ast_dsp_set_features(p->dsp, features);
04480 if (global_relaxdtmf) {
04481 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
04482 }
04483 }
04484
04485 static void disable_dsp_detect(struct sip_pvt *p)
04486 {
04487 if (p->dsp) {
04488 ast_dsp_free(p->dsp);
04489 p->dsp = NULL;
04490 }
04491 }
04492
04493
04494 static int sip_setoption(struct ast_channel *chan, int option, void *data, int datalen)
04495 {
04496 int res = -1;
04497 struct sip_pvt *p = chan->tech_pvt;
04498
04499 if (!p) {
04500 ast_log(LOG_ERROR, "Attempt to Ref a null pointer. sip private structure is gone!\n");
04501 return -1;
04502 }
04503
04504 sip_pvt_lock(p);
04505
04506 switch (option) {
04507 case AST_OPTION_FORMAT_READ:
04508 if (p->rtp) {
04509 res = ast_rtp_instance_set_read_format(p->rtp, *(int *) data);
04510 }
04511 break;
04512 case AST_OPTION_FORMAT_WRITE:
04513 if (p->rtp) {
04514 res = ast_rtp_instance_set_write_format(p->rtp, *(int *) data);
04515 }
04516 break;
04517 case AST_OPTION_MAKE_COMPATIBLE:
04518 if (p->rtp) {
04519 res = ast_rtp_instance_make_compatible(chan, p->rtp, (struct ast_channel *) data);
04520 }
04521 break;
04522 case AST_OPTION_DIGIT_DETECT:
04523 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
04524 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
04525 char *cp = (char *) data;
04526
04527 ast_debug(1, "%sabling digit detection on %s\n", *cp ? "En" : "Dis", chan->name);
04528 if (*cp) {
04529 enable_dsp_detect(p);
04530 } else {
04531 disable_dsp_detect(p);
04532 }
04533 res = 0;
04534 }
04535 break;
04536 case AST_OPTION_SECURE_SIGNALING:
04537 p->req_secure_signaling = *(unsigned int *) data;
04538 res = 0;
04539 break;
04540 case AST_OPTION_SECURE_MEDIA:
04541 ast_set2_flag(&p->flags[1], *(unsigned int *) data, SIP_PAGE2_USE_SRTP);
04542 res = 0;
04543 break;
04544 default:
04545 break;
04546 }
04547
04548 sip_pvt_unlock(p);
04549
04550 return res;
04551 }
04552
04553
04554 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen)
04555 {
04556 int res = -1;
04557 enum ast_t38_state state = T38_STATE_UNAVAILABLE;
04558 struct sip_pvt *p = (struct sip_pvt *) chan->tech_pvt;
04559 char *cp;
04560
04561 sip_pvt_lock(p);
04562
04563 switch (option) {
04564 case AST_OPTION_T38_STATE:
04565
04566 if (*datalen != sizeof(enum ast_t38_state)) {
04567 ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option. Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
04568 break;
04569 }
04570
04571
04572 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT)) {
04573 switch (p->t38.state) {
04574 case T38_LOCAL_REINVITE:
04575 case T38_PEER_REINVITE:
04576 state = T38_STATE_NEGOTIATING;
04577 break;
04578 case T38_ENABLED:
04579 state = T38_STATE_NEGOTIATED;
04580 break;
04581 default:
04582 state = T38_STATE_UNKNOWN;
04583 }
04584 }
04585
04586 *((enum ast_t38_state *) data) = state;
04587 res = 0;
04588
04589 break;
04590 case AST_OPTION_DIGIT_DETECT:
04591 cp = (char *) data;
04592 *cp = p->dsp ? 1 : 0;
04593 ast_debug(1, "Reporting digit detection %sabled on %s\n", *cp ? "en" : "dis", chan->name);
04594 break;
04595 case AST_OPTION_SECURE_SIGNALING:
04596 *((unsigned int *) data) = p->req_secure_signaling;
04597 res = 0;
04598 break;
04599 case AST_OPTION_SECURE_MEDIA:
04600 *((unsigned int *) data) = ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP) ? 1 : 0;
04601 res = 0;
04602 break;
04603 case AST_OPTION_DEVICE_NAME:
04604 if (p && p->outgoing_call) {
04605 cp = (char *) data;
04606 ast_copy_string(cp, p->dialstring, *datalen);
04607 res = 0;
04608 }
04609
04610
04611
04612 break;
04613 default:
04614 break;
04615 }
04616
04617 sip_pvt_unlock(p);
04618
04619 return res;
04620 }
04621
04622
04623
04624
04625
04626 const char *find_closing_quote(const char *start, const char *lim)
04627 {
04628 char last_char = '\0';
04629 const char *s;
04630 for (s = start; *s && s != lim; last_char = *s++) {
04631 if (*s == '"' && last_char != '\\')
04632 break;
04633 }
04634 return s;
04635 }
04636
04637
04638 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
04639 {
04640 struct sip_pvt *p = chan->tech_pvt;
04641
04642 if (subclass != AST_HTML_URL)
04643 return -1;
04644
04645 ast_string_field_build(p, url, "<%s>;mode=active", data);
04646
04647 if (sip_debug_test_pvt(p))
04648 ast_debug(1, "Send URL %s, state = %u!\n", data, chan->_state);
04649
04650 switch (chan->_state) {
04651 case AST_STATE_RING:
04652 transmit_response(p, "100 Trying", &p->initreq);
04653 break;
04654 case AST_STATE_RINGING:
04655 transmit_response(p, "180 Ringing", &p->initreq);
04656 break;
04657 case AST_STATE_UP:
04658 if (!p->pendinginvite) {
04659 transmit_reinvite_with_sdp(p, FALSE, FALSE);
04660 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04661 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04662 }
04663 break;
04664 default:
04665 ast_log(LOG_WARNING, "Don't know how to send URI when state is %u!\n", chan->_state);
04666 }
04667
04668 return 0;
04669 }
04670
04671
04672 static const char *sip_get_callid(struct ast_channel *chan)
04673 {
04674 return chan->tech_pvt ? ((struct sip_pvt *) chan->tech_pvt)->callid : "";
04675 }
04676
04677
04678
04679
04680
04681
04682 static int sip_sendtext(struct ast_channel *ast, const char *text)
04683 {
04684 struct sip_pvt *dialog = ast->tech_pvt;
04685 int debug;
04686
04687 if (!dialog) {
04688 return -1;
04689 }
04690
04691
04692 if (!text) {
04693 return 0;
04694 }
04695 if(!is_method_allowed(&dialog->allowed_methods, SIP_MESSAGE)) {
04696 ast_debug(2, "Trying to send MESSAGE to device that does not support it.\n");
04697 return(0);
04698 }
04699
04700 debug = sip_debug_test_pvt(dialog);
04701 if (debug) {
04702 ast_verbose("Sending text %s on %s\n", text, ast->name);
04703 }
04704
04705 transmit_message_with_text(dialog, text);
04706 return 0;
04707 }
04708
04709
04710
04711
04712
04713
04714 static void realtime_update_peer(const char *peername, struct ast_sockaddr *addr, const char *defaultuser, const char *fullcontact, const char *useragent, int expirey, unsigned short deprecated_username, int lastms)
04715 {
04716 char port[10];
04717 char ipaddr[INET6_ADDRSTRLEN];
04718 char regseconds[20];
04719 char *tablename = NULL;
04720 char str_lastms[20];
04721
04722 const char *sysname = ast_config_AST_SYSTEM_NAME;
04723 char *syslabel = NULL;
04724
04725 time_t nowtime = time(NULL) + expirey;
04726 const char *fc = fullcontact ? "fullcontact" : NULL;
04727
04728 int realtimeregs = ast_check_realtime("sipregs");
04729
04730 tablename = realtimeregs ? "sipregs" : "sippeers";
04731
04732
04733 snprintf(str_lastms, sizeof(str_lastms), "%d", lastms);
04734 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
04735 ast_copy_string(ipaddr, ast_sockaddr_isnull(addr) ? "" : ast_sockaddr_stringify_addr(addr), sizeof(ipaddr));
04736 ast_copy_string(port, ast_sockaddr_port(addr) ? ast_sockaddr_stringify_port(addr) : "", sizeof(port));
04737
04738 if (ast_strlen_zero(sysname))
04739 sysname = NULL;
04740 else if (sip_cfg.rtsave_sysname)
04741 syslabel = "regserver";
04742
04743
04744
04745
04746
04747
04748
04749 if (fc) {
04750 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
04751 "port", port, "regseconds", regseconds,
04752 deprecated_username ? "username" : "defaultuser", defaultuser,
04753 "useragent", useragent, "lastms", str_lastms,
04754 fc, fullcontact, syslabel, sysname, SENTINEL);
04755 } else {
04756 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
04757 "port", port, "regseconds", regseconds,
04758 "useragent", useragent, "lastms", str_lastms,
04759 deprecated_username ? "username" : "defaultuser", defaultuser,
04760 syslabel, sysname, SENTINEL);
04761 }
04762 }
04763
04764
04765 static void register_peer_exten(struct sip_peer *peer, int onoff)
04766 {
04767 char multi[256];
04768 char *stringp, *ext, *context;
04769 struct pbx_find_info q = { .stacklen = 0 };
04770
04771
04772
04773
04774
04775 if (ast_strlen_zero(sip_cfg.regcontext))
04776 return;
04777
04778 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
04779 stringp = multi;
04780 while ((ext = strsep(&stringp, "&"))) {
04781 if ((context = strchr(ext, '@'))) {
04782 *context++ = '\0';
04783 if (!ast_context_find(context)) {
04784 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
04785 continue;
04786 }
04787 } else {
04788 context = sip_cfg.regcontext;
04789 }
04790 if (onoff) {
04791 if (!ast_exists_extension(NULL, context, ext, 1, NULL)) {
04792 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
04793 ast_strdup(peer->name), ast_free_ptr, "SIP");
04794 }
04795 } else if (pbx_find_extension(NULL, NULL, &q, context, ext, 1, NULL, "", E_MATCH)) {
04796 ast_context_remove_extension(context, ext, 1, NULL);
04797 }
04798 }
04799 }
04800
04801
04802 static void destroy_mailbox(struct sip_mailbox *mailbox)
04803 {
04804 if (mailbox->event_sub)
04805 ast_event_unsubscribe(mailbox->event_sub);
04806 ast_free(mailbox);
04807 }
04808
04809
04810 static void clear_peer_mailboxes(struct sip_peer *peer)
04811 {
04812 struct sip_mailbox *mailbox;
04813
04814 while ((mailbox = AST_LIST_REMOVE_HEAD(&peer->mailboxes, entry)))
04815 destroy_mailbox(mailbox);
04816 }
04817
04818 static void sip_destroy_peer_fn(void *peer)
04819 {
04820 sip_destroy_peer(peer);
04821 }
04822
04823
04824 static void sip_destroy_peer(struct sip_peer *peer)
04825 {
04826 ast_debug(3, "Destroying SIP peer %s\n", peer->name);
04827
04828
04829
04830
04831
04832
04833 clear_peer_mailboxes(peer);
04834
04835 if (peer->outboundproxy) {
04836 ao2_ref(peer->outboundproxy, -1);
04837 peer->outboundproxy = NULL;
04838 }
04839
04840
04841 if (peer->call) {
04842 dialog_unlink_all(peer->call);
04843 peer->call = dialog_unref(peer->call, "peer->call is being unset");
04844 }
04845
04846 if (peer->mwipvt) {
04847 dialog_unlink_all(peer->mwipvt);
04848 peer->mwipvt = dialog_unref(peer->mwipvt, "unreffing peer->mwipvt");
04849 }
04850
04851 if (peer->chanvars) {
04852 ast_variables_destroy(peer->chanvars);
04853 peer->chanvars = NULL;
04854 }
04855
04856 register_peer_exten(peer, FALSE);
04857 ast_free_ha(peer->ha);
04858 ast_free_ha(peer->directmediaha);
04859 if (peer->selfdestruct)
04860 ast_atomic_fetchadd_int(&apeerobjs, -1);
04861 else if (!ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && peer->is_realtime) {
04862 ast_atomic_fetchadd_int(&rpeerobjs, -1);
04863 ast_debug(3, "-REALTIME- peer Destroyed. Name: %s. Realtime Peer objects: %d\n", peer->name, rpeerobjs);
04864 } else
04865 ast_atomic_fetchadd_int(&speerobjs, -1);
04866 if (peer->auth) {
04867 ao2_t_ref(peer->auth, -1, "Removing peer authentication");
04868 peer->auth = NULL;
04869 }
04870
04871 if (peer->socket.tcptls_session) {
04872 ao2_ref(peer->socket.tcptls_session, -1);
04873 peer->socket.tcptls_session = NULL;
04874 }
04875
04876 ast_cc_config_params_destroy(peer->cc_params);
04877
04878 ast_string_field_free_memory(peer);
04879 }
04880
04881
04882 static void update_peer(struct sip_peer *p, int expire)
04883 {
04884 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
04885 if (sip_cfg.peer_rtupdate &&
04886 (p->is_realtime || rtcachefriends)) {
04887 realtime_update_peer(p->name, &p->addr, p->username, p->fullcontact, p->useragent, expire, p->deprecated_username, p->lastms);
04888 }
04889 }
04890
04891 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *cfg)
04892 {
04893 struct ast_variable *var = NULL;
04894 struct ast_flags flags = {0};
04895 char *cat = NULL;
04896 const char *insecure;
04897 while ((cat = ast_category_browse(cfg, cat))) {
04898 insecure = ast_variable_retrieve(cfg, cat, "insecure");
04899 set_insecure_flags(&flags, insecure, -1);
04900 if (ast_test_flag(&flags, SIP_INSECURE_PORT)) {
04901 var = ast_category_root(cfg, cat);
04902 break;
04903 }
04904 }
04905 return var;
04906 }
04907
04908 static struct ast_variable *get_insecure_variable_from_sippeers(const char *column, const char *value)
04909 {
04910 struct ast_config *peerlist;
04911 struct ast_variable *var = NULL;
04912 if ((peerlist = ast_load_realtime_multientry("sippeers", column, value, "insecure LIKE", "%port%", SENTINEL))) {
04913 if ((var = get_insecure_variable_from_config(peerlist))) {
04914
04915
04916 var = ast_variables_dup(var);
04917 }
04918 ast_config_destroy(peerlist);
04919 }
04920 return var;
04921 }
04922
04923
04924
04925
04926
04927
04928 static struct ast_variable *get_insecure_variable_from_sipregs(const char *column, const char *value, struct ast_variable **var)
04929 {
04930 struct ast_variable *varregs = NULL;
04931 struct ast_config *regs, *peers;
04932 char *regscat;
04933 const char *regname;
04934
04935 if (!(regs = ast_load_realtime_multientry("sipregs", column, value, SENTINEL))) {
04936 return NULL;
04937 }
04938
04939
04940 if (!(peers = ast_load_realtime_multientry("sippeers", "insecure LIKE", "%port%", SENTINEL))) {
04941 ast_config_destroy(regs);
04942 return NULL;
04943 }
04944
04945
04946
04947 regscat = NULL;
04948 while ((regscat = ast_category_browse(regs, regscat)) && (regname = ast_variable_retrieve(regs, regscat, "name"))) {
04949 char *peerscat;
04950 const char *peername;
04951
04952 peerscat = NULL;
04953 while ((peerscat = ast_category_browse(peers, peerscat)) && (peername = ast_variable_retrieve(peers, peerscat, "name"))) {
04954 if (!strcasecmp(regname, peername)) {
04955
04956
04957 const char *insecure = ast_variable_retrieve(peers, peerscat, "insecure");
04958 struct ast_flags flags = {0};
04959 set_insecure_flags(&flags, insecure, -1);
04960 if (ast_test_flag(&flags, SIP_INSECURE_PORT)) {
04961
04962 if ((varregs = ast_variables_dup(ast_category_root(regs, regscat)))) {
04963 if (!(*var = ast_variables_dup(ast_category_root(peers, peerscat)))) {
04964 ast_variables_destroy(varregs);
04965 varregs = NULL;
04966 }
04967 }
04968 goto done;
04969 }
04970 }
04971 }
04972 }
04973
04974 done:
04975 ast_config_destroy(regs);
04976 ast_config_destroy(peers);
04977 return varregs;
04978 }
04979
04980 static const char *get_name_from_variable(const struct ast_variable *var)
04981 {
04982
04983
04984
04985
04986
04987
04988
04989
04990 const struct ast_variable *tmp;
04991 for (tmp = var; tmp; tmp = tmp->next) {
04992 if (!strcasecmp(tmp->name, "name")) {
04993 if (!ast_strlen_zero(tmp->value)) {
04994 return tmp->value;
04995 }
04996 break;
04997 }
04998 }
04999 return NULL;
05000 }
05001
05002
05003
05004 static int realtime_peer_by_name(const char *const *name, struct ast_sockaddr *addr, const char *ipaddr, struct ast_variable **var, struct ast_variable **varregs)
05005 {
05006
05007 if ((*var = ast_load_realtime("sippeers", "name", *name, "host", "dynamic", SENTINEL))) {
05008 ;
05009
05010 } else if (addr && !(*var = ast_load_realtime("sippeers", "name", *name, "host", ipaddr, SENTINEL))) {
05011 ;
05012
05013 } else if ((*var = ast_load_realtime("sippeers", "name", *name, SENTINEL))) {
05014
05015
05016
05017
05018
05019
05020 if (addr) {
05021 struct ast_variable *tmp;
05022 for (tmp = *var; tmp; tmp = tmp->next) {
05023 if (!strcasecmp(tmp->name, "host")) {
05024 struct ast_sockaddr *addrs = NULL;
05025
05026 if (ast_sockaddr_resolve(&addrs,
05027 tmp->value,
05028 PARSE_PORT_FORBID,
05029 get_address_family_filter(SIP_TRANSPORT_UDP)) <= 0 ||
05030 ast_sockaddr_cmp(&addrs[0], addr)) {
05031
05032 ast_variables_destroy(*var);
05033 *var = NULL;
05034 }
05035 ast_free(addrs);
05036 break;
05037 }
05038 }
05039 }
05040 }
05041
05042
05043 if (*var) {
05044 if (varregs) {
05045 *varregs = ast_load_realtime("sipregs", "name", *name, SENTINEL);
05046 }
05047 return 1;
05048 }
05049 return 0;
05050 }
05051
05052
05053
05054
05055
05056
05057
05058 static struct ast_variable *realtime_peer_get_sippeer_helper(const char **name, struct ast_variable **varregs) {
05059 struct ast_variable *var = NULL;
05060 const char *old_name = *name;
05061 *name = get_name_from_variable(*varregs);
05062 if (!*name || !(var = ast_load_realtime("sippeers", "name", *name, SENTINEL))) {
05063 if (!*name) {
05064 ast_log(LOG_WARNING, "Found sipreg but it has no name\n");
05065 }
05066 ast_variables_destroy(*varregs);
05067 *varregs = NULL;
05068 *name = old_name;
05069 }
05070 return var;
05071 }
05072
05073
05074
05075
05076 static int realtime_peer_by_addr(const char **name, struct ast_sockaddr *addr, const char *ipaddr, struct ast_variable **var, struct ast_variable **varregs)
05077 {
05078 char portstring[6];
05079 ast_copy_string(portstring, ast_sockaddr_stringify_port(addr), sizeof(portstring));
05080
05081
05082 *name = NULL;
05083
05084
05085 if ((*var = ast_load_realtime("sippeers", "host", ipaddr, "port", portstring, SENTINEL))) {
05086 ;
05087
05088 } else if (varregs && (*varregs = ast_load_realtime("sipregs", "ipaddr", ipaddr, "port", portstring, SENTINEL)) &&
05089 (*var = realtime_peer_get_sippeer_helper(name, varregs))) {
05090 ;
05091
05092 } else if (!varregs && (*var = ast_load_realtime("sippeers", "ipaddr", ipaddr, "port", portstring, SENTINEL))) {
05093 ;
05094
05095 } else if ((*var = get_insecure_variable_from_sippeers("host", ipaddr))) {
05096 ;
05097
05098
05099
05100
05101 } else if (varregs && (*varregs = get_insecure_variable_from_sipregs("ipaddr", ipaddr, var))) {
05102 ;
05103
05104 } else if (!varregs && (*var = get_insecure_variable_from_sippeers("ipaddr", ipaddr))) {
05105 ;
05106 }
05107
05108
05109 if (!*var) {
05110 return 0;
05111 }
05112
05113
05114
05115
05116 if (!*name && !(*name = get_name_from_variable(*var))) {
05117 ast_log(LOG_WARNING, "Found peer for IP %s but it has no name\n", ipaddr);
05118 ast_variables_destroy(*var);
05119 *var = NULL;
05120 if (varregs && *varregs) {
05121 ast_variables_destroy(*varregs);
05122 *varregs = NULL;
05123 }
05124 return 0;
05125 }
05126
05127
05128
05129
05130 if (varregs && !*varregs) {
05131 *varregs = ast_load_realtime("sipregs", "name", *name, SENTINEL);
05132 }
05133 return 1;
05134 }
05135
05136
05137
05138
05139
05140
05141
05142
05143
05144
05145 static struct sip_peer *realtime_peer(const char *newpeername, struct ast_sockaddr *addr, int devstate_only, int which_objects)
05146 {
05147 struct sip_peer *peer = NULL;
05148 struct ast_variable *var = NULL;
05149 struct ast_variable *varregs = NULL;
05150 char ipaddr[INET6_ADDRSTRLEN];
05151 int realtimeregs = ast_check_realtime("sipregs");
05152
05153 if (addr) {
05154 ast_copy_string(ipaddr, ast_sockaddr_stringify_addr(addr), sizeof(ipaddr));
05155 } else {
05156 ipaddr[0] = '\0';
05157 }
05158
05159 if (newpeername && realtime_peer_by_name(&newpeername, addr, ipaddr, &var, realtimeregs ? &varregs : NULL)) {
05160 ;
05161 } else if (addr && realtime_peer_by_addr(&newpeername, addr, ipaddr, &var, realtimeregs ? &varregs : NULL)) {
05162 ;
05163 } else {
05164 return NULL;
05165 }
05166
05167
05168
05169 if (which_objects == FINDUSERS) {
05170 struct ast_variable *tmp;
05171 for (tmp = var; tmp; tmp = tmp->next) {
05172 if (!strcasecmp(tmp->name, "type") && (!strcasecmp(tmp->value, "peer"))) {
05173 goto cleanup;
05174 }
05175 }
05176 }
05177
05178
05179 peer = build_peer(newpeername, var, varregs, TRUE, devstate_only);
05180 if (!peer) {
05181 goto cleanup;
05182 }
05183
05184
05185
05186 if (peer->type == 0) {
05187 if (which_objects == FINDUSERS) {
05188 peer->type = SIP_TYPE_USER;
05189 } else if (which_objects == FINDPEERS) {
05190 peer->type = SIP_TYPE_PEER;
05191 } else {
05192 peer->type = SIP_TYPE_PEER | SIP_TYPE_USER;
05193 }
05194 }
05195
05196 ast_debug(3, "-REALTIME- loading peer from database to memory. Name: %s. Peer objects: %d\n", peer->name, rpeerobjs);
05197
05198 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && !devstate_only) {
05199
05200 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
05201 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
05202 AST_SCHED_REPLACE_UNREF(peer->expire, sched, sip_cfg.rtautoclear * 1000, expire_register, peer,
05203 unref_peer(_data, "remove registration ref"),
05204 unref_peer(peer, "remove registration ref"),
05205 ref_peer(peer, "add registration ref"));
05206 }
05207 ao2_t_link(peers, peer, "link peer into peers table");
05208 if (!ast_sockaddr_isnull(&peer->addr)) {
05209 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
05210 }
05211 }
05212 peer->is_realtime = 1;
05213
05214 cleanup:
05215 ast_variables_destroy(var);
05216 ast_variables_destroy(varregs);
05217 return peer;
05218 }
05219
05220
05221 static int find_by_name(void *obj, void *arg, void *data, int flags)
05222 {
05223 struct sip_peer *search = obj, *match = arg;
05224 int *which_objects = data;
05225
05226
05227 if (strcmp(search->name, match->name)) {
05228 return 0;
05229 }
05230
05231 switch (*which_objects) {
05232 case FINDUSERS:
05233 if (!(search->type & SIP_TYPE_USER)) {
05234 return 0;
05235 }
05236 break;
05237 case FINDPEERS:
05238 if (!(search->type & SIP_TYPE_PEER)) {
05239 return 0;
05240 }
05241 break;
05242 case FINDALLDEVICES:
05243 break;
05244 }
05245
05246 return CMP_MATCH | CMP_STOP;
05247 }
05248
05249
05250
05251
05252
05253
05254
05255
05256
05257
05258
05259
05260
05261
05262 static struct sip_peer *find_peer(const char *peer, struct ast_sockaddr *addr, int realtime, int which_objects, int devstate_only, int transport)
05263 {
05264 struct sip_peer *p = NULL;
05265 struct sip_peer tmp_peer;
05266
05267 if (peer) {
05268 ast_copy_string(tmp_peer.name, peer, sizeof(tmp_peer.name));
05269 p = ao2_t_callback_data(peers, OBJ_POINTER, find_by_name, &tmp_peer, &which_objects, "ao2_find in peers table");
05270 } else if (addr) {
05271 ast_sockaddr_copy(&tmp_peer.addr, addr);
05272 tmp_peer.flags[0].flags = 0;
05273 tmp_peer.transports = transport;
05274 p = ao2_t_find(peers_by_ip, &tmp_peer, OBJ_POINTER, "ao2_find in peers_by_ip table");
05275 if (!p) {
05276 ast_set_flag(&tmp_peer.flags[0], SIP_INSECURE_PORT);
05277 p = ao2_t_find(peers_by_ip, &tmp_peer, OBJ_POINTER, "ao2_find in peers_by_ip table 2");
05278 if (p) {
05279 return p;
05280 }
05281 }
05282 }
05283
05284 if (!p && (realtime || devstate_only)) {
05285 p = realtime_peer(peer, addr, devstate_only, which_objects);
05286 if (p) {
05287 switch (which_objects) {
05288 case FINDUSERS:
05289 if (!(p->type & SIP_TYPE_USER)) {
05290 unref_peer(p, "Wrong type of realtime SIP endpoint");
05291 return NULL;
05292 }
05293 break;
05294 case FINDPEERS:
05295 if (!(p->type & SIP_TYPE_PEER)) {
05296 unref_peer(p, "Wrong type of realtime SIP endpoint");
05297 return NULL;
05298 }
05299 break;
05300 case FINDALLDEVICES:
05301 break;
05302 }
05303 }
05304 }
05305
05306 return p;
05307 }
05308
05309
05310 static void do_setnat(struct sip_pvt *p)
05311 {
05312 const char *mode;
05313 int natflags;
05314
05315 natflags = ast_test_flag(&p->flags[1], SIP_PAGE2_SYMMETRICRTP);
05316 mode = natflags ? "On" : "Off";
05317
05318 if (p->rtp) {
05319 ast_debug(1, "Setting NAT on RTP to %s\n", mode);
05320 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_NAT, natflags);
05321 }
05322 if (p->vrtp) {
05323 ast_debug(1, "Setting NAT on VRTP to %s\n", mode);
05324 ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_NAT, natflags);
05325 }
05326 if (p->udptl) {
05327 ast_debug(1, "Setting NAT on UDPTL to %s\n", mode);
05328 ast_udptl_setnat(p->udptl, natflags);
05329 }
05330 if (p->trtp) {
05331 ast_debug(1, "Setting NAT on TRTP to %s\n", mode);
05332 ast_rtp_instance_set_prop(p->trtp, AST_RTP_PROPERTY_NAT, natflags);
05333 }
05334 }
05335
05336
05337 static void change_t38_state(struct sip_pvt *p, int state)
05338 {
05339 int old = p->t38.state;
05340 struct ast_channel *chan = p->owner;
05341 struct ast_control_t38_parameters parameters = { .request_response = 0 };
05342
05343
05344 if (old == state)
05345 return;
05346
05347 p->t38.state = state;
05348 ast_debug(2, "T38 state changed to %u on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
05349
05350
05351 if (!chan)
05352 return;
05353
05354
05355 switch (state) {
05356 case T38_PEER_REINVITE:
05357 parameters = p->t38.their_parms;
05358 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
05359 parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
05360 ast_udptl_set_tag(p->udptl, "%s", chan->name);
05361 break;
05362 case T38_ENABLED:
05363 parameters = p->t38.their_parms;
05364 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
05365 parameters.request_response = AST_T38_NEGOTIATED;
05366 ast_udptl_set_tag(p->udptl, "%s", chan->name);
05367 break;
05368 case T38_DISABLED:
05369 if (old == T38_ENABLED) {
05370 parameters.request_response = AST_T38_TERMINATED;
05371 } else if (old == T38_LOCAL_REINVITE) {
05372 parameters.request_response = AST_T38_REFUSED;
05373 }
05374 break;
05375 case T38_LOCAL_REINVITE:
05376
05377 break;
05378 }
05379
05380
05381 if (parameters.request_response)
05382 ast_queue_control_data(chan, AST_CONTROL_T38_PARAMETERS, ¶meters, sizeof(parameters));
05383 }
05384
05385
05386 static void set_t38_capabilities(struct sip_pvt *p)
05387 {
05388 if (p->udptl) {
05389 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY) {
05390 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
05391 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL_FEC) {
05392 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
05393 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL) {
05394 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
05395 }
05396 }
05397 }
05398
05399 static void copy_socket_data(struct sip_socket *to_sock, const struct sip_socket *from_sock)
05400 {
05401 if (to_sock->tcptls_session) {
05402 ao2_ref(to_sock->tcptls_session, -1);
05403 to_sock->tcptls_session = NULL;
05404 }
05405
05406 if (from_sock->tcptls_session) {
05407 ao2_ref(from_sock->tcptls_session, +1);
05408 }
05409
05410 *to_sock = *from_sock;
05411 }
05412
05413
05414
05415
05416 static int dialog_initialize_rtp(struct sip_pvt *dialog)
05417 {
05418 struct ast_sockaddr bindaddr_tmp;
05419
05420 if (!sip_methods[dialog->method].need_rtp) {
05421 return 0;
05422 }
05423
05424 ast_sockaddr_copy(&bindaddr_tmp, &bindaddr);
05425 if (!(dialog->rtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
05426 return -1;
05427 }
05428
05429 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS) ||
05430 (ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) && (dialog->capability & AST_FORMAT_VIDEO_MASK))) {
05431 if (!(dialog->vrtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
05432 return -1;
05433 }
05434 ast_rtp_instance_set_timeout(dialog->vrtp, dialog->rtptimeout);
05435 ast_rtp_instance_set_hold_timeout(dialog->vrtp, dialog->rtpholdtimeout);
05436 ast_rtp_instance_set_keepalive(dialog->vrtp, dialog->rtpkeepalive);
05437
05438 ast_rtp_instance_set_prop(dialog->vrtp, AST_RTP_PROPERTY_RTCP, 1);
05439 ast_rtp_instance_set_qos(dialog->vrtp, global_tos_video, global_cos_video, "SIP VIDEO");
05440 }
05441
05442 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_TEXTSUPPORT)) {
05443 if (!(dialog->trtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
05444 return -1;
05445 }
05446
05447 ast_rtp_instance_set_keepalive(dialog->trtp, dialog->rtpkeepalive);
05448
05449 ast_rtp_instance_set_prop(dialog->trtp, AST_RTP_PROPERTY_RTCP, 1);
05450 }
05451
05452 ast_rtp_instance_set_timeout(dialog->rtp, dialog->rtptimeout);
05453 ast_rtp_instance_set_hold_timeout(dialog->rtp, dialog->rtpholdtimeout);
05454 ast_rtp_instance_set_keepalive(dialog->rtp, dialog->rtpkeepalive);
05455
05456 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_RTCP, 1);
05457 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
05458 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
05459
05460 ast_rtp_instance_set_qos(dialog->rtp, global_tos_audio, global_cos_audio, "SIP RTP");
05461
05462 do_setnat(dialog);
05463
05464 return 0;
05465 }
05466
05467
05468
05469
05470
05471
05472
05473
05474 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
05475 {
05476 struct sip_auth_container *credentials;
05477
05478
05479
05480
05481 if (dialog->socket.type && check_request_transport(peer, dialog))
05482 return -1;
05483 copy_socket_data(&dialog->socket, &peer->socket);
05484
05485 if (!(ast_sockaddr_isnull(&peer->addr) && ast_sockaddr_isnull(&peer->defaddr)) &&
05486 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
05487 dialog->sa = ast_sockaddr_isnull(&peer->addr) ? peer->defaddr : peer->addr;
05488 dialog->recv = dialog->sa;
05489 } else
05490 return -1;
05491
05492
05493 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
05494 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
05495 ast_copy_flags(&dialog->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
05496 dialog->capability = peer->capability;
05497 dialog->prefs = peer->prefs;
05498 dialog->amaflags = peer->amaflags;
05499
05500 ast_string_field_set(dialog, engine, peer->engine);
05501
05502 dialog->rtptimeout = peer->rtptimeout;
05503 dialog->rtpholdtimeout = peer->rtpholdtimeout;
05504 dialog->rtpkeepalive = peer->rtpkeepalive;
05505 if (dialog_initialize_rtp(dialog)) {
05506 return -1;
05507 }
05508
05509 if (dialog->rtp) {
05510 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
05511 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
05512
05513 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(dialog->rtp), dialog->rtp, &dialog->prefs);
05514 dialog->autoframing = peer->autoframing;
05515 }
05516
05517
05518 ast_string_field_set(dialog, peername, peer->name);
05519 ast_string_field_set(dialog, authname, peer->username);
05520 ast_string_field_set(dialog, username, peer->username);
05521 ast_string_field_set(dialog, peersecret, peer->secret);
05522 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
05523 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
05524 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
05525 ast_string_field_set(dialog, tohost, peer->tohost);
05526 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
05527 ast_string_field_set(dialog, accountcode, peer->accountcode);
05528 ast_string_field_set(dialog, context, peer->context);
05529 ast_string_field_set(dialog, cid_num, peer->cid_num);
05530 ast_string_field_set(dialog, cid_name, peer->cid_name);
05531 ast_string_field_set(dialog, cid_tag, peer->cid_tag);
05532 ast_string_field_set(dialog, mwi_from, peer->mwi_from);
05533 if (!ast_strlen_zero(peer->parkinglot)) {
05534 ast_string_field_set(dialog, parkinglot, peer->parkinglot);
05535 }
05536 ast_string_field_set(dialog, engine, peer->engine);
05537 ref_proxy(dialog, obproxy_get(dialog, peer));
05538 dialog->callgroup = peer->callgroup;
05539 dialog->pickupgroup = peer->pickupgroup;
05540 dialog->allowtransfer = peer->allowtransfer;
05541 dialog->jointnoncodeccapability = dialog->noncodeccapability;
05542
05543
05544 ao2_lock(peer);
05545 credentials = peer->auth;
05546 if (credentials) {
05547 ao2_t_ref(credentials, +1, "Ref peer auth for dialog");
05548 }
05549 ao2_unlock(peer);
05550 ao2_lock(dialog);
05551 if (dialog->peerauth) {
05552 ao2_t_ref(dialog->peerauth, -1, "Unref old dialog peer auth");
05553 }
05554 dialog->peerauth = credentials;
05555 ao2_unlock(dialog);
05556
05557 dialog->maxcallbitrate = peer->maxcallbitrate;
05558 dialog->disallowed_methods = peer->disallowed_methods;
05559 ast_cc_copy_config_params(dialog->cc_params, peer->cc_params);
05560 if (ast_strlen_zero(dialog->tohost))
05561 ast_string_field_set(dialog, tohost, ast_sockaddr_stringify_host_remote(&dialog->sa));
05562 if (!ast_strlen_zero(peer->fromdomain)) {
05563 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
05564 if (!dialog->initreq.headers) {
05565 char *new_callid;
05566 char *tmpcall = ast_strdupa(dialog->callid);
05567
05568 new_callid = strchr(tmpcall, '@');
05569 if (new_callid) {
05570 int callid_size;
05571
05572 *new_callid = '\0';
05573
05574
05575 callid_size = strlen(tmpcall) + strlen(peer->fromdomain) + 2;
05576 new_callid = ast_alloca(callid_size);
05577 snprintf(new_callid, callid_size, "%s@%s", tmpcall, peer->fromdomain);
05578 change_callid_pvt(dialog, new_callid);
05579 }
05580 }
05581 }
05582 if (!ast_strlen_zero(peer->fromuser))
05583 ast_string_field_set(dialog, fromuser, peer->fromuser);
05584 if (!ast_strlen_zero(peer->language))
05585 ast_string_field_set(dialog, language, peer->language);
05586
05587
05588
05589
05590 if (peer->maxms && peer->lastms)
05591 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
05592 else
05593 dialog->timer_t1 = peer->timer_t1;
05594
05595
05596
05597 if (peer->timer_b)
05598 dialog->timer_b = peer->timer_b;
05599 else
05600 dialog->timer_b = 64 * dialog->timer_t1;
05601
05602 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
05603 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
05604 dialog->noncodeccapability |= AST_RTP_DTMF;
05605 else
05606 dialog->noncodeccapability &= ~AST_RTP_DTMF;
05607 dialog->directmediaha = ast_duplicate_ha_list(peer->directmediaha);
05608 if (peer->call_limit)
05609 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
05610 if (!dialog->portinuri)
05611 dialog->portinuri = peer->portinuri;
05612 dialog->chanvars = copy_vars(peer->chanvars);
05613 if (peer->fromdomainport)
05614 dialog->fromdomainport = peer->fromdomainport;
05615 dialog->callingpres = peer->callingpres;
05616
05617 return 0;
05618 }
05619
05620
05621 static inline int default_sip_port(enum sip_transport type)
05622 {
05623 return type == SIP_TRANSPORT_TLS ? STANDARD_TLS_PORT : STANDARD_SIP_PORT;
05624 }
05625
05626
05627
05628
05629 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct ast_sockaddr *addr, int newdialog)
05630 {
05631 struct sip_peer *peer;
05632 char *peername, *peername2, *hostn;
05633 char host[MAXHOSTNAMELEN];
05634 char service[MAXHOSTNAMELEN];
05635 int srv_ret = 0;
05636 int tportno;
05637
05638 AST_DECLARE_APP_ARGS(hostport,
05639 AST_APP_ARG(host);
05640 AST_APP_ARG(port);
05641 );
05642
05643 peername = ast_strdupa(opeer);
05644 peername2 = ast_strdupa(opeer);
05645 AST_NONSTANDARD_RAW_ARGS(hostport, peername2, ':');
05646
05647 if (hostport.port)
05648 dialog->portinuri = 1;
05649
05650 dialog->timer_t1 = global_t1;
05651 dialog->timer_b = global_timer_b;
05652 peer = find_peer(peername, NULL, TRUE, FINDPEERS, FALSE, 0);
05653
05654 if (peer) {
05655 int res;
05656 if (newdialog) {
05657 set_socket_transport(&dialog->socket, 0);
05658 }
05659 res = create_addr_from_peer(dialog, peer);
05660 dialog->relatedpeer = ref_peer(peer, "create_addr: setting dialog's relatedpeer pointer");
05661 unref_peer(peer, "create_addr: unref peer from find_peer hashtab lookup");
05662 return res;
05663 } else if (ast_check_digits(peername)) {
05664
05665
05666
05667 ast_log(LOG_WARNING, "Purely numeric hostname (%s), and not a peer--rejecting!\n", peername);
05668 return -1;
05669 } else {
05670 dialog->rtptimeout = global_rtptimeout;
05671 dialog->rtpholdtimeout = global_rtpholdtimeout;
05672 dialog->rtpkeepalive = global_rtpkeepalive;
05673 if (dialog_initialize_rtp(dialog)) {
05674 return -1;
05675 }
05676 }
05677
05678 ast_string_field_set(dialog, tohost, hostport.host);
05679 dialog->allowed_methods &= ~sip_cfg.disallowed_methods;
05680
05681
05682 ref_proxy(dialog, obproxy_get(dialog, NULL));
05683
05684 if (addr) {
05685
05686 ast_sockaddr_copy(&dialog->sa, addr);
05687 } else {
05688
05689
05690
05691
05692
05693
05694 hostn = peername;
05695
05696
05697
05698 if (!hostport.port && sip_cfg.srvlookup) {
05699 snprintf(service, sizeof(service), "_%s._%s.%s",
05700 get_srv_service(dialog->socket.type),
05701 get_srv_protocol(dialog->socket.type), peername);
05702 if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno,
05703 service)) > 0) {
05704 hostn = host;
05705 }
05706 }
05707
05708 if (ast_sockaddr_resolve_first_transport(&dialog->sa, hostn, 0, dialog->socket.type ? dialog->socket.type : SIP_TRANSPORT_UDP)) {
05709 ast_log(LOG_WARNING, "No such host: %s\n", peername);
05710 return -1;
05711 }
05712
05713 if (srv_ret > 0) {
05714 ast_sockaddr_set_port(&dialog->sa, tportno);
05715 }
05716 }
05717
05718 if (!dialog->socket.type)
05719 set_socket_transport(&dialog->socket, SIP_TRANSPORT_UDP);
05720 if (!dialog->socket.port) {
05721 dialog->socket.port = htons(ast_sockaddr_port(&bindaddr));
05722 }
05723
05724 if (!ast_sockaddr_port(&dialog->sa)) {
05725 ast_sockaddr_set_port(&dialog->sa, default_sip_port(dialog->socket.type));
05726 }
05727 ast_sockaddr_copy(&dialog->recv, &dialog->sa);
05728 return 0;
05729 }
05730
05731
05732
05733
05734 static int auto_congest(const void *arg)
05735 {
05736 struct sip_pvt *p = (struct sip_pvt *)arg;
05737
05738 sip_pvt_lock(p);
05739 p->initid = -1;
05740 if (p->owner) {
05741
05742 if (!ast_channel_trylock(p->owner)) {
05743 append_history(p, "Cong", "Auto-congesting (timer)");
05744 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
05745 ast_channel_unlock(p->owner);
05746 }
05747
05748
05749 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05750 }
05751 sip_pvt_unlock(p);
05752 dialog_unref(p, "unreffing arg passed into auto_congest callback (p->initid)");
05753 return 0;
05754 }
05755
05756
05757
05758
05759 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
05760 {
05761 int res;
05762 struct sip_pvt *p = ast->tech_pvt;
05763 struct varshead *headp;
05764 struct ast_var_t *current;
05765 const char *referer = NULL;
05766 int cc_core_id;
05767 char uri[SIPBUFSIZE] = "";
05768
05769 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
05770 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
05771 return -1;
05772 }
05773
05774 if (ast_cc_is_recall(ast, &cc_core_id, "SIP")) {
05775 char device_name[AST_CHANNEL_NAME];
05776 struct ast_cc_monitor *recall_monitor;
05777 struct sip_monitor_instance *monitor_instance;
05778 ast_channel_get_device_name(ast, device_name, sizeof(device_name));
05779 if ((recall_monitor = ast_cc_get_monitor_by_recall_core_id(cc_core_id, device_name))) {
05780 monitor_instance = recall_monitor->private_data;
05781 ast_copy_string(uri, monitor_instance->notify_uri, sizeof(uri));
05782 ao2_t_ref(recall_monitor, -1, "Got the URI we need so unreffing monitor");
05783 }
05784 }
05785
05786
05787 headp=&ast->varshead;
05788 AST_LIST_TRAVERSE(headp, current, entries) {
05789
05790 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
05791 p->options->vxml_url = ast_var_value(current);
05792 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
05793 p->options->uri_options = ast_var_value(current);
05794 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
05795
05796 p->options->addsipheaders = 1;
05797 } else if (!strcasecmp(ast_var_name(current), "SIPFROMDOMAIN")) {
05798 ast_string_field_set(p, fromdomain, ast_var_value(current));
05799 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
05800
05801 p->options->transfer = 1;
05802 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
05803
05804 referer = ast_var_value(current);
05805 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
05806
05807 p->options->replaces = ast_var_value(current);
05808 } else if (!strcasecmp(ast_var_name(current), "SIP_MAX_FORWARDS")) {
05809 if (sscanf(ast_var_value(current), "%d", &(p->maxforwards)) != 1) {
05810 ast_log(LOG_WARNING, "The SIP_MAX_FORWARDS channel variable is not a valid integer.\n");
05811 }
05812 }
05813 }
05814
05815
05816 if (p->req_secure_signaling && p->socket.type != SIP_TRANSPORT_TLS) {
05817 ast_log(LOG_WARNING, "Encrypted signaling is required\n");
05818 ast->hangupcause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05819 return -1;
05820 }
05821
05822 if (ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP)) {
05823 if (ast_test_flag(&p->flags[0], SIP_REINVITE)) {
05824 ast_debug(1, "Direct media not possible when using SRTP, ignoring canreinvite setting\n");
05825 ast_clear_flag(&p->flags[0], SIP_REINVITE);
05826 }
05827
05828 if (p->rtp && !p->srtp && setup_srtp(&p->srtp) < 0) {
05829 ast_log(LOG_WARNING, "SRTP audio setup failed\n");
05830 return -1;
05831 }
05832
05833 if (p->vrtp && !p->vsrtp && setup_srtp(&p->vsrtp) < 0) {
05834 ast_log(LOG_WARNING, "SRTP video setup failed\n");
05835 return -1;
05836 }
05837
05838 if (p->trtp && !p->tsrtp && setup_srtp(&p->tsrtp) < 0) {
05839 ast_log(LOG_WARNING, "SRTP text setup failed\n");
05840 return -1;
05841 }
05842 }
05843
05844 res = 0;
05845 ast_set_flag(&p->flags[0], SIP_OUTGOING);
05846
05847
05848
05849
05850 ast_clear_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_T38);
05851
05852 if (p->options->transfer) {
05853 char buf[SIPBUFSIZE/2];
05854
05855 if (referer) {
05856 if (sipdebug)
05857 ast_debug(3, "Call for %s transferred by %s\n", p->username, referer);
05858 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
05859 } else
05860 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
05861 ast_string_field_set(p, cid_name, buf);
05862 }
05863 ast_debug(1, "Outgoing Call for %s\n", p->username);
05864
05865 res = update_call_counter(p, INC_CALL_RINGING);
05866
05867 if (res == -1) {
05868 ast->hangupcause = AST_CAUSE_USER_BUSY;
05869 return res;
05870 }
05871 p->callingpres = ast_party_id_presentation(&ast->caller.id);
05872 p->jointcapability = ast_rtp_instance_available_formats(p->rtp, p->capability, p->prefcodec);
05873 p->jointnoncodeccapability = p->noncodeccapability;
05874
05875
05876 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
05877 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
05878 res = -1;
05879 } else {
05880 int xmitres;
05881 struct ast_party_connected_line connected;
05882 struct ast_set_party_connected_line update_connected;
05883
05884 sip_pvt_lock(p);
05885
05886
05887 memset(&update_connected, 0, sizeof(update_connected));
05888 ast_party_connected_line_init(&connected);
05889 if (!ast_strlen_zero(p->cid_num)
05890 || (p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
05891 update_connected.id.number = 1;
05892 connected.id.number.valid = 1;
05893 connected.id.number.str = (char *) p->cid_num;
05894 connected.id.number.presentation = p->callingpres;
05895 }
05896 if (!ast_strlen_zero(p->cid_name)
05897 || (p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
05898 update_connected.id.name = 1;
05899 connected.id.name.valid = 1;
05900 connected.id.name.str = (char *) p->cid_name;
05901 connected.id.name.presentation = p->callingpres;
05902 }
05903 if (update_connected.id.number || update_connected.id.name) {
05904 connected.id.tag = (char *) p->cid_tag;
05905 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
05906 ast_channel_queue_connected_line_update(ast, &connected, &update_connected);
05907 }
05908
05909 xmitres = transmit_invite(p, SIP_INVITE, 1, 2, uri);
05910 if (xmitres == XMIT_ERROR) {
05911 sip_pvt_unlock(p);
05912 return -1;
05913 }
05914 p->invitestate = INV_CALLING;
05915
05916
05917 AST_SCHED_REPLACE_UNREF(p->initid, sched, p->timer_b, auto_congest, p,
05918 dialog_unref(_data, "dialog ptr dec when SCHED_REPLACE del op succeeded"),
05919 dialog_unref(p, "dialog ptr dec when SCHED_REPLACE add failed"),
05920 dialog_ref(p, "dialog ptr inc when SCHED_REPLACE add succeeded") );
05921 sip_pvt_unlock(p);
05922 }
05923 return res;
05924 }
05925
05926
05927
05928 static void sip_registry_destroy(struct sip_registry *reg)
05929 {
05930
05931 ast_debug(3, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
05932
05933 if (reg->call) {
05934
05935
05936 reg->call->registry = registry_unref(reg->call->registry, "destroy reg->call->registry");
05937 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
05938 dialog_unlink_all(reg->call);
05939 reg->call = dialog_unref(reg->call, "unref reg->call");
05940
05941 }
05942 AST_SCHED_DEL(sched, reg->expire);
05943 AST_SCHED_DEL(sched, reg->timeout);
05944
05945 ast_string_field_free_memory(reg);
05946 ast_atomic_fetchadd_int(®objs, -1);
05947 ast_free(reg);
05948 }
05949
05950
05951 static void sip_subscribe_mwi_destroy(struct sip_subscription_mwi *mwi)
05952 {
05953 if (mwi->call) {
05954 mwi->call->mwi = NULL;
05955 mwi->call = dialog_unref(mwi->call, "sip_subscription_mwi destruction");
05956 }
05957
05958 AST_SCHED_DEL(sched, mwi->resub);
05959 ast_string_field_free_memory(mwi);
05960 ast_free(mwi);
05961 }
05962
05963
05964 void __sip_destroy(struct sip_pvt *p, int lockowner, int lockdialoglist)
05965 {
05966 struct sip_request *req;
05967
05968
05969 if (p->stimer) {
05970 p->stimer->quit_flag = 1;
05971 stop_session_timer(p);
05972 ast_free(p->stimer);
05973 p->stimer = NULL;
05974 }
05975
05976 if (sip_debug_test_pvt(p))
05977 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
05978
05979 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05980 update_call_counter(p, DEC_CALL_LIMIT);
05981 ast_debug(2, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
05982 }
05983
05984
05985 if (p->owner) {
05986 if (lockowner)
05987 ast_channel_lock(p->owner);
05988 ast_debug(1, "Detaching from %s\n", p->owner->name);
05989 p->owner->tech_pvt = NULL;
05990
05991 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05992 if (lockowner)
05993 ast_channel_unlock(p->owner);
05994
05995 usleep(1);
05996 }
05997
05998
05999 if (p->relatedpeer && p->relatedpeer->mwipvt == p)
06000 p->relatedpeer->mwipvt = dialog_unref(p->relatedpeer->mwipvt, "delete ->relatedpeer->mwipvt");
06001 if (p->relatedpeer && p->relatedpeer->call == p)
06002 p->relatedpeer->call = dialog_unref(p->relatedpeer->call, "unset the relatedpeer->call field in tandem with relatedpeer field itself");
06003
06004 if (p->relatedpeer)
06005 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting a dialog relatedpeer field in sip_destroy");
06006
06007 if (p->registry) {
06008 if (p->registry->call == p)
06009 p->registry->call = dialog_unref(p->registry->call, "nulling out the registry's call dialog field in unlink_all");
06010 p->registry = registry_unref(p->registry, "delete p->registry");
06011 }
06012
06013 if (p->mwi) {
06014 p->mwi->call = NULL;
06015 p->mwi = NULL;
06016 }
06017
06018 if (dumphistory)
06019 sip_dump_history(p);
06020
06021 if (p->options) {
06022 if (p->options->outboundproxy) {
06023 ao2_ref(p->options->outboundproxy, -1);
06024 }
06025 ast_free(p->options);
06026 p->options = NULL;
06027 }
06028
06029 if (p->outboundproxy) {
06030 ref_proxy(p, NULL);
06031 }
06032
06033 if (p->notify) {
06034 ast_variables_destroy(p->notify->headers);
06035 ast_free(p->notify->content);
06036 ast_free(p->notify);
06037 p->notify = NULL;
06038 }
06039 if (p->rtp) {
06040 ast_rtp_instance_destroy(p->rtp);
06041 p->rtp = NULL;
06042 }
06043 if (p->vrtp) {
06044 ast_rtp_instance_destroy(p->vrtp);
06045 p->vrtp = NULL;
06046 }
06047 if (p->trtp) {
06048 ast_rtp_instance_destroy(p->trtp);
06049 p->trtp = NULL;
06050 }
06051 if (p->udptl) {
06052 ast_udptl_destroy(p->udptl);
06053 p->udptl = NULL;
06054 }
06055 if (p->refer) {
06056 if (p->refer->refer_call) {
06057 p->refer->refer_call = dialog_unref(p->refer->refer_call, "unref dialog p->refer->refer_call");
06058 }
06059 ast_free(p->refer);
06060 p->refer = NULL;
06061 }
06062 if (p->route) {
06063 free_old_route(p->route);
06064 p->route = NULL;
06065 }
06066 deinit_req(&p->initreq);
06067
06068
06069 if (p->history) {
06070 struct sip_history *hist;
06071 while ( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) ) {
06072 ast_free(hist);
06073 p->history_entries--;
06074 }
06075 ast_free(p->history);
06076 p->history = NULL;
06077 }
06078
06079 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
06080 ast_free(req);
06081 }
06082
06083 if (p->chanvars) {
06084 ast_variables_destroy(p->chanvars);
06085 p->chanvars = NULL;
06086 }
06087
06088 if (p->srtp) {
06089 sip_srtp_destroy(p->srtp);
06090 p->srtp = NULL;
06091 }
06092
06093 if (p->vsrtp) {
06094 sip_srtp_destroy(p->vsrtp);
06095 p->vsrtp = NULL;
06096 }
06097
06098 if (p->tsrtp) {
06099 sip_srtp_destroy(p->tsrtp);
06100 p->tsrtp = NULL;
06101 }
06102
06103 if (p->directmediaha) {
06104 ast_free_ha(p->directmediaha);
06105 p->directmediaha = NULL;
06106 }
06107
06108 ast_string_field_free_memory(p);
06109
06110 ast_cc_config_params_destroy(p->cc_params);
06111 p->cc_params = NULL;
06112
06113 if (p->epa_entry) {
06114 ao2_ref(p->epa_entry, -1);
06115 p->epa_entry = NULL;
06116 }
06117
06118 if (p->socket.tcptls_session) {
06119 ao2_ref(p->socket.tcptls_session, -1);
06120 p->socket.tcptls_session = NULL;
06121 }
06122
06123 if (p->peerauth) {
06124 ao2_t_ref(p->peerauth, -1, "Removing active peer authentication");
06125 p->peerauth = NULL;
06126 }
06127 }
06128
06129
06130
06131
06132
06133
06134
06135
06136
06137
06138
06139
06140
06141
06142
06143 static int update_call_counter(struct sip_pvt *fup, int event)
06144 {
06145 char name[256];
06146 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
06147 int outgoing = fup->outgoing_call;
06148 struct sip_peer *p = NULL;
06149
06150 ast_debug(3, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
06151
06152
06153
06154
06155 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
06156 return 0;
06157
06158 ast_copy_string(name, fup->username, sizeof(name));
06159
06160
06161 if (fup->relatedpeer) {
06162 p = ref_peer(fup->relatedpeer, "ref related peer for update_call_counter");
06163 inuse = &p->inUse;
06164 call_limit = &p->call_limit;
06165 inringing = &p->inRinging;
06166 ast_copy_string(name, fup->peername, sizeof(name));
06167 }
06168 if (!p) {
06169 ast_debug(2, "%s is not a local device, no call limit\n", name);
06170 return 0;
06171 }
06172
06173 switch(event) {
06174
06175 case DEC_CALL_LIMIT:
06176
06177 if (inuse) {
06178 sip_pvt_lock(fup);
06179 ao2_lock(p);
06180 if (*inuse > 0) {
06181 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
06182 (*inuse)--;
06183 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
06184 }
06185 } else {
06186 *inuse = 0;
06187 }
06188 ao2_unlock(p);
06189 sip_pvt_unlock(fup);
06190 }
06191
06192
06193 if (inringing) {
06194 sip_pvt_lock(fup);
06195 ao2_lock(p);
06196 if (*inringing > 0) {
06197 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
06198 (*inringing)--;
06199 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
06200 }
06201 } else {
06202 *inringing = 0;
06203 }
06204 ao2_unlock(p);
06205 sip_pvt_unlock(fup);
06206 }
06207
06208
06209 sip_pvt_lock(fup);
06210 ao2_lock(p);
06211 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && sip_cfg.notifyhold) {
06212 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
06213 ao2_unlock(p);
06214 sip_pvt_unlock(fup);
06215 sip_peer_hold(fup, FALSE);
06216 } else {
06217 ao2_unlock(p);
06218 sip_pvt_unlock(fup);
06219 }
06220 if (sipdebug)
06221 ast_debug(2, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", "peer", name, *call_limit);
06222 break;
06223
06224 case INC_CALL_RINGING:
06225 case INC_CALL_LIMIT:
06226
06227 if (*call_limit > 0 ) {
06228 if (*inuse >= *call_limit) {
06229 ast_log(LOG_NOTICE, "Call %s %s '%s' rejected due to usage limit of %d\n", outgoing ? "to" : "from", "peer", name, *call_limit);
06230 unref_peer(p, "update_call_counter: unref peer p, call limit exceeded");
06231 return -1;
06232 }
06233 }
06234 if (inringing && (event == INC_CALL_RINGING)) {
06235 sip_pvt_lock(fup);
06236 ao2_lock(p);
06237 if (!ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
06238 (*inringing)++;
06239 ast_set_flag(&fup->flags[0], SIP_INC_RINGING);
06240 }
06241 ao2_unlock(p);
06242 sip_pvt_unlock(fup);
06243 }
06244 if (inuse) {
06245 sip_pvt_lock(fup);
06246 ao2_lock(p);
06247 if (!ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
06248 (*inuse)++;
06249 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
06250 }
06251 ao2_unlock(p);
06252 sip_pvt_unlock(fup);
06253 }
06254 if (sipdebug) {
06255 ast_debug(2, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", "peer", name, *inuse, *call_limit);
06256 }
06257 break;
06258
06259 case DEC_CALL_RINGING:
06260 if (inringing) {
06261 sip_pvt_lock(fup);
06262 ao2_lock(p);
06263 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
06264 if (*inringing > 0) {
06265 (*inringing)--;
06266 }
06267 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
06268 }
06269 ao2_unlock(p);
06270 sip_pvt_unlock(fup);
06271 }
06272 break;
06273
06274 default:
06275 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
06276 }
06277
06278 if (p) {
06279 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "SIP/%s", p->name);
06280 unref_peer(p, "update_call_counter: unref_peer from call counter");
06281 }
06282 return 0;
06283 }
06284
06285
06286 static void sip_destroy_fn(void *p)
06287 {
06288 sip_destroy(p);
06289 }
06290
06291
06292
06293
06294
06295
06296 struct sip_pvt *sip_destroy(struct sip_pvt *p)
06297 {
06298 ast_debug(3, "Destroying SIP dialog %s\n", p->callid);
06299 __sip_destroy(p, TRUE, TRUE);
06300 return NULL;
06301 }
06302
06303
06304 int hangup_sip2cause(int cause)
06305 {
06306
06307
06308 switch(cause) {
06309 case 401:
06310 return AST_CAUSE_CALL_REJECTED;
06311 case 403:
06312 return AST_CAUSE_CALL_REJECTED;
06313 case 404:
06314 return AST_CAUSE_UNALLOCATED;
06315 case 405:
06316 return AST_CAUSE_INTERWORKING;
06317 case 407:
06318 return AST_CAUSE_CALL_REJECTED;
06319 case 408:
06320 return AST_CAUSE_NO_USER_RESPONSE;
06321 case 409:
06322 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
06323 case 410:
06324 return AST_CAUSE_NUMBER_CHANGED;
06325 case 411:
06326 return AST_CAUSE_INTERWORKING;
06327 case 413:
06328 return AST_CAUSE_INTERWORKING;
06329 case 414:
06330 return AST_CAUSE_INTERWORKING;
06331 case 415:
06332 return AST_CAUSE_INTERWORKING;
06333 case 420:
06334 return AST_CAUSE_NO_ROUTE_DESTINATION;
06335 case 480:
06336 return AST_CAUSE_NO_ANSWER;
06337 case 481:
06338 return AST_CAUSE_INTERWORKING;
06339 case 482:
06340 return AST_CAUSE_INTERWORKING;
06341 case 483:
06342 return AST_CAUSE_NO_ANSWER;
06343 case 484:
06344 return AST_CAUSE_INVALID_NUMBER_FORMAT;
06345 case 485:
06346 return AST_CAUSE_UNALLOCATED;
06347 case 486:
06348 return AST_CAUSE_BUSY;
06349 case 487:
06350 return AST_CAUSE_INTERWORKING;
06351 case 488:
06352 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
06353 case 491:
06354 return AST_CAUSE_INTERWORKING;
06355 case 493:
06356 return AST_CAUSE_INTERWORKING;
06357 case 500:
06358 return AST_CAUSE_FAILURE;
06359 case 501:
06360 return AST_CAUSE_FACILITY_REJECTED;
06361 case 502:
06362 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
06363 case 503:
06364 return AST_CAUSE_CONGESTION;
06365 case 504:
06366 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
06367 case 505:
06368 return AST_CAUSE_INTERWORKING;
06369 case 600:
06370 return AST_CAUSE_USER_BUSY;
06371 case 603:
06372 return AST_CAUSE_CALL_REJECTED;
06373 case 604:
06374 return AST_CAUSE_UNALLOCATED;
06375 case 606:
06376 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
06377 default:
06378 if (cause < 500 && cause >= 400) {
06379
06380 return AST_CAUSE_INTERWORKING;
06381 } else if (cause < 600 && cause >= 500) {
06382
06383 return AST_CAUSE_CONGESTION;
06384 } else if (cause < 700 && cause >= 600) {
06385
06386 return AST_CAUSE_INTERWORKING;
06387 }
06388 return AST_CAUSE_NORMAL;
06389 }
06390
06391 return 0;
06392 }
06393
06394
06395
06396
06397
06398
06399
06400
06401
06402
06403
06404
06405
06406
06407
06408
06409
06410
06411
06412
06413
06414
06415
06416
06417
06418
06419
06420
06421
06422
06423
06424
06425
06426 const char *hangup_cause2sip(int cause)
06427 {
06428 switch (cause) {
06429 case AST_CAUSE_UNALLOCATED:
06430 case AST_CAUSE_NO_ROUTE_DESTINATION:
06431 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
06432 return "404 Not Found";
06433 case AST_CAUSE_CONGESTION:
06434 case AST_CAUSE_SWITCH_CONGESTION:
06435 return "503 Service Unavailable";
06436 case AST_CAUSE_NO_USER_RESPONSE:
06437 return "408 Request Timeout";
06438 case AST_CAUSE_NO_ANSWER:
06439 case AST_CAUSE_UNREGISTERED:
06440 return "480 Temporarily unavailable";
06441 case AST_CAUSE_CALL_REJECTED:
06442 return "403 Forbidden";
06443 case AST_CAUSE_NUMBER_CHANGED:
06444 return "410 Gone";
06445 case AST_CAUSE_NORMAL_UNSPECIFIED:
06446 return "480 Temporarily unavailable";
06447 case AST_CAUSE_INVALID_NUMBER_FORMAT:
06448 return "484 Address incomplete";
06449 case AST_CAUSE_USER_BUSY:
06450 return "486 Busy here";
06451 case AST_CAUSE_FAILURE:
06452 return "500 Server internal failure";
06453 case AST_CAUSE_FACILITY_REJECTED:
06454 return "501 Not Implemented";
06455 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
06456 return "503 Service Unavailable";
06457
06458 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
06459 return "502 Bad Gateway";
06460 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
06461 return "488 Not Acceptable Here";
06462
06463 case AST_CAUSE_NOTDEFINED:
06464 default:
06465 ast_debug(1, "AST hangup cause %d (no match found in SIP)\n", cause);
06466 return NULL;
06467 }
06468
06469
06470 return 0;
06471 }
06472
06473 static int reinvite_timeout(const void *data)
06474 {
06475 struct sip_pvt *dialog = (struct sip_pvt *) data;
06476 struct ast_channel *owner = sip_pvt_lock_full(dialog);
06477 dialog->reinviteid = -1;
06478 check_pendings(dialog);
06479 if (owner) {
06480 ast_channel_unlock(owner);
06481 ast_channel_unref(owner);
06482 }
06483 ao2_unlock(dialog);
06484 dialog_unref(dialog, "unref for reinvite timeout");
06485 return 0;
06486 }
06487
06488
06489
06490 static int sip_hangup(struct ast_channel *ast)
06491 {
06492 struct sip_pvt *p = ast->tech_pvt;
06493 int needcancel = FALSE;
06494 int needdestroy = 0;
06495 struct ast_channel *oldowner = ast;
06496
06497 if (!p) {
06498 ast_debug(1, "Asked to hangup channel that was not connected\n");
06499 return 0;
06500 }
06501 if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE) || ast->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE) {
06502 ast_debug(1, "This call was answered elsewhere\n");
06503 if (ast->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE) {
06504 ast_debug(1, "####### It's the cause code, buddy. The cause code!!!\n");
06505 }
06506 append_history(p, "Cancel", "Call answered elsewhere");
06507 p->answered_elsewhere = TRUE;
06508 }
06509
06510
06511 if (p->owner)
06512 p->hangupcause = p->owner->hangupcause;
06513
06514 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
06515 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
06516 if (sipdebug)
06517 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
06518 update_call_counter(p, DEC_CALL_LIMIT);
06519 }
06520 ast_debug(4, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
06521 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
06522 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
06523 p->needdestroy = 0;
06524 if (p->owner) {
06525 p->owner->tech_pvt = dialog_unref(p->owner->tech_pvt, "unref p->owner->tech_pvt");
06526 sip_pvt_lock(p);
06527 p->owner = NULL;
06528 sip_pvt_unlock(p);
06529 }
06530 ast_module_unref(ast_module_info->self);
06531 return 0;
06532 }
06533
06534 ast_debug(1, "Hangup call %s, SIP callid %s\n", ast->name, p->callid);
06535
06536 sip_pvt_lock(p);
06537 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
06538 if (sipdebug)
06539 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
06540 update_call_counter(p, DEC_CALL_LIMIT);
06541 }
06542
06543
06544 if (p->owner != ast) {
06545 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
06546 sip_pvt_unlock(p);
06547 return 0;
06548 }
06549
06550
06551 if (p->invitestate < INV_COMPLETED && p->owner->_state != AST_STATE_UP) {
06552 needcancel = TRUE;
06553 ast_debug(4, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
06554 }
06555
06556 stop_media_flows(p);
06557
06558 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", ast_cause2str(p->hangupcause));
06559
06560
06561 disable_dsp_detect(p);
06562
06563 p->owner = NULL;
06564 ast->tech_pvt = dialog_unref(ast->tech_pvt, "unref ast->tech_pvt");
06565
06566 ast_module_unref(ast_module_info->self);
06567
06568
06569
06570
06571
06572
06573 if (p->alreadygone)
06574 needdestroy = 1;
06575 else if (p->invitestate != INV_CALLING)
06576 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
06577
06578
06579 if (!p->alreadygone && p->initreq.data && ast_str_strlen(p->initreq.data)) {
06580 if (needcancel) {
06581 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06582
06583 if (p->invitestate == INV_CALLING) {
06584
06585 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
06586
06587 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
06588 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
06589 } else {
06590 struct sip_pkt *cur;
06591
06592 for (cur = p->packets; cur; cur = cur->next) {
06593 __sip_semi_ack(p, cur->seqno, cur->is_resp, cur->method ? cur->method : find_sip_method(ast_str_buffer(cur->data)));
06594 }
06595 p->invitestate = INV_CANCELLED;
06596
06597 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
06598
06599
06600 needdestroy = 0;
06601 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
06602 }
06603 } else {
06604 const char *res;
06605 AST_SCHED_DEL_UNREF(sched, p->provisional_keepalive_sched_id, dialog_unref(p, "when you delete the provisional_keepalive_sched_id, you should dec the refcount for the stored dialog ptr"));
06606 if (p->hangupcause && (res = hangup_cause2sip(p->hangupcause)))
06607 transmit_response_reliable(p, res, &p->initreq);
06608 else
06609 transmit_response_reliable(p, "603 Declined", &p->initreq);
06610 p->invitestate = INV_TERMINATED;
06611 }
06612 } else {
06613 if (p->stimer->st_active == TRUE) {
06614 stop_session_timer(p);
06615 }
06616
06617 if (!p->pendinginvite) {
06618 struct ast_channel *bridge = ast_bridged_channel(oldowner);
06619 char quality_buf[AST_MAX_USER_FIELD], *quality;
06620
06621
06622
06623
06624 while (bridge && ast_channel_trylock(bridge)) {
06625 sip_pvt_unlock(p);
06626 do {
06627 CHANNEL_DEADLOCK_AVOIDANCE(oldowner);
06628 } while (sip_pvt_trylock(p));
06629 bridge = ast_bridged_channel(oldowner);
06630 }
06631
06632 if (p->rtp) {
06633 ast_rtp_instance_set_stats_vars(oldowner, p->rtp);
06634 }
06635
06636 if (bridge) {
06637 struct sip_pvt *q = bridge->tech_pvt;
06638
06639 if (IS_SIP_TECH(bridge->tech) && q && q->rtp) {
06640 ast_rtp_instance_set_stats_vars(bridge, q->rtp);
06641 }
06642 ast_channel_unlock(bridge);
06643 }
06644
06645
06646
06647
06648
06649 if (p->rtp && (quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
06650 if (p->do_history) {
06651 append_history(p, "RTCPaudio", "Quality:%s", quality);
06652 }
06653 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", quality);
06654 }
06655 if (p->vrtp && (quality = ast_rtp_instance_get_quality(p->vrtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
06656 if (p->do_history) {
06657 append_history(p, "RTCPvideo", "Quality:%s", quality);
06658 }
06659 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", quality);
06660 }
06661 if (p->trtp && (quality = ast_rtp_instance_get_quality(p->trtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
06662 if (p->do_history) {
06663 append_history(p, "RTCPtext", "Quality:%s", quality);
06664 }
06665 pbx_builtin_setvar_helper(oldowner, "RTPTEXTQOS", quality);
06666 }
06667
06668
06669 if (oldowner->_state == AST_STATE_UP) {
06670 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
06671 }
06672
06673 } else {
06674
06675
06676 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
06677 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
06678 AST_SCHED_DEL_UNREF(sched, p->waitid, dialog_unref(p, "when you delete the waitid sched, you should dec the refcount for the stored dialog ptr"));
06679 if (sip_cancel_destroy(p)) {
06680 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
06681 }
06682
06683
06684
06685
06686 if (p->ongoing_reinvite && p->reinviteid < 0) {
06687 p->reinviteid = ast_sched_add(sched, 32 * p->timer_t1, reinvite_timeout, dialog_ref(p, "ref for reinvite_timeout"));
06688 }
06689 }
06690 }
06691 }
06692 if (needdestroy) {
06693 pvt_set_needdestroy(p, "hangup");
06694 }
06695 sip_pvt_unlock(p);
06696 return 0;
06697 }
06698
06699
06700 static void try_suggested_sip_codec(struct sip_pvt *p)
06701 {
06702 format_t fmt;
06703 const char *codec;
06704
06705 if (p->outgoing_call) {
06706 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_OUTBOUND");
06707 } else if (!(codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_INBOUND"))) {
06708 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
06709 }
06710
06711 if (!codec)
06712 return;
06713
06714 fmt = ast_getformatbyname(codec);
06715 if (fmt) {
06716 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
06717 if (p->jointcapability & fmt) {
06718 p->jointcapability &= fmt;
06719 p->capability &= fmt;
06720 } else
06721 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
06722 } else
06723 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
06724 return;
06725 }
06726
06727
06728
06729 static int sip_answer(struct ast_channel *ast)
06730 {
06731 int res = 0;
06732 struct sip_pvt *p = ast->tech_pvt;
06733 int oldsdp = FALSE;
06734
06735 if (!p) {
06736 ast_debug(1, "Asked to answer channel %s without tech pvt; ignoring\n",
06737 ast->name);
06738 return res;
06739 }
06740 sip_pvt_lock(p);
06741 if (ast->_state != AST_STATE_UP) {
06742 try_suggested_sip_codec(p);
06743
06744 if (ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT)) {
06745 oldsdp = TRUE;
06746 }
06747
06748 ast_setstate(ast, AST_STATE_UP);
06749 ast_debug(1, "SIP answering channel: %s\n", ast->name);
06750 ast_rtp_instance_update_source(p->rtp);
06751 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL, oldsdp, TRUE);
06752 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
06753
06754
06755 if (p->stimer->st_active == TRUE) {
06756 start_session_timer(p);
06757 }
06758 }
06759 sip_pvt_unlock(p);
06760 return res;
06761 }
06762
06763
06764 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
06765 {
06766 struct sip_pvt *p = ast->tech_pvt;
06767 int res = 0;
06768
06769 switch (frame->frametype) {
06770 case AST_FRAME_VOICE:
06771 if (!(frame->subclass.codec & ast->nativeformats)) {
06772 char s1[512], s2[512], s3[512];
06773 ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s read/write = %s/%s\n",
06774 ast_getformatname(frame->subclass.codec),
06775 ast_getformatname_multiple(s1, sizeof(s1), ast->nativeformats & AST_FORMAT_AUDIO_MASK),
06776 ast_getformatname_multiple(s2, sizeof(s2), ast->readformat),
06777 ast_getformatname_multiple(s3, sizeof(s3), ast->writeformat));
06778 return 0;
06779 }
06780 if (p) {
06781 sip_pvt_lock(p);
06782 if (p->t38.state == T38_ENABLED) {
06783
06784 sip_pvt_unlock(p);
06785 break;
06786 } else if (p->rtp) {
06787
06788 if ((ast->_state != AST_STATE_UP) &&
06789 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06790 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06791 ast_rtp_instance_update_source(p->rtp);
06792 if (!global_prematuremediafilter) {
06793 p->invitestate = INV_EARLY_MEDIA;
06794 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06795 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06796 }
06797 }
06798 p->lastrtptx = time(NULL);
06799 res = ast_rtp_instance_write(p->rtp, frame);
06800 }
06801 sip_pvt_unlock(p);
06802 }
06803 break;
06804 case AST_FRAME_VIDEO:
06805 if (p) {
06806 sip_pvt_lock(p);
06807 if (p->vrtp) {
06808
06809 if ((ast->_state != AST_STATE_UP) &&
06810 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06811 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06812 p->invitestate = INV_EARLY_MEDIA;
06813 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06814 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06815 }
06816 p->lastrtptx = time(NULL);
06817 res = ast_rtp_instance_write(p->vrtp, frame);
06818 }
06819 sip_pvt_unlock(p);
06820 }
06821 break;
06822 case AST_FRAME_TEXT:
06823 if (p) {
06824 sip_pvt_lock(p);
06825 if (p->red) {
06826 ast_rtp_red_buffer(p->trtp, frame);
06827 } else {
06828 if (p->trtp) {
06829
06830 if ((ast->_state != AST_STATE_UP) &&
06831 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06832 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06833 p->invitestate = INV_EARLY_MEDIA;
06834 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06835 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06836 }
06837 p->lastrtptx = time(NULL);
06838 res = ast_rtp_instance_write(p->trtp, frame);
06839 }
06840 }
06841 sip_pvt_unlock(p);
06842 }
06843 break;
06844 case AST_FRAME_IMAGE:
06845 return 0;
06846 break;
06847 case AST_FRAME_MODEM:
06848 if (p) {
06849 sip_pvt_lock(p);
06850
06851
06852
06853
06854 if ((ast->_state == AST_STATE_UP) &&
06855 p->udptl &&
06856 (p->t38.state == T38_ENABLED)) {
06857 res = ast_udptl_write(p->udptl, frame);
06858 }
06859 sip_pvt_unlock(p);
06860 }
06861 break;
06862 default:
06863 ast_log(LOG_WARNING, "Can't send %u type frames with SIP write\n", frame->frametype);
06864 return 0;
06865 }
06866
06867 return res;
06868 }
06869
06870
06871
06872 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
06873 {
06874 int ret = -1;
06875 struct sip_pvt *p;
06876
06877 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE))
06878 ast_debug(1, "New channel is zombie\n");
06879 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE))
06880 ast_debug(1, "Old channel is zombie\n");
06881
06882 if (!newchan || !newchan->tech_pvt) {
06883 if (!newchan)
06884 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
06885 else
06886 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
06887 return -1;
06888 }
06889 p = newchan->tech_pvt;
06890
06891 sip_pvt_lock(p);
06892 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
06893 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
06894 if (p->owner != oldchan)
06895 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
06896 else {
06897 p->owner = newchan;
06898
06899
06900
06901
06902
06903
06904 sip_set_rtp_peer(newchan, NULL, NULL, 0, 0, 0);
06905 ret = 0;
06906 }
06907 ast_debug(3, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
06908
06909 sip_pvt_unlock(p);
06910 return ret;
06911 }
06912
06913 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
06914 {
06915 struct sip_pvt *p = ast->tech_pvt;
06916 int res = 0;
06917
06918 if (!p) {
06919 ast_debug(1, "Asked to begin DTMF digit on channel %s with no pvt; ignoring\n",
06920 ast->name);
06921 return res;
06922 }
06923
06924 sip_pvt_lock(p);
06925 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
06926 case SIP_DTMF_INBAND:
06927 res = -1;
06928 break;
06929 case SIP_DTMF_RFC2833:
06930 if (p->rtp)
06931 ast_rtp_instance_dtmf_begin(p->rtp, digit);
06932 break;
06933 default:
06934 break;
06935 }
06936 sip_pvt_unlock(p);
06937
06938 return res;
06939 }
06940
06941
06942
06943 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
06944 {
06945 struct sip_pvt *p = ast->tech_pvt;
06946 int res = 0;
06947
06948 if (!p) {
06949 ast_debug(1, "Asked to end DTMF digit on channel %s with no pvt; ignoring\n",
06950 ast->name);
06951 return res;
06952 }
06953
06954 sip_pvt_lock(p);
06955 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
06956 case SIP_DTMF_INFO:
06957 case SIP_DTMF_SHORTINFO:
06958 transmit_info_with_digit(p, digit, duration);
06959 break;
06960 case SIP_DTMF_RFC2833:
06961 if (p->rtp)
06962 ast_rtp_instance_dtmf_end_with_duration(p->rtp, digit, duration);
06963 break;
06964 case SIP_DTMF_INBAND:
06965 res = -1;
06966 break;
06967 }
06968 sip_pvt_unlock(p);
06969
06970 return res;
06971 }
06972
06973
06974 static int sip_transfer(struct ast_channel *ast, const char *dest)
06975 {
06976 struct sip_pvt *p = ast->tech_pvt;
06977 int res;
06978
06979 if (!p) {
06980 ast_debug(1, "Asked to transfer channel %s with no pvt; ignoring\n",
06981 ast->name);
06982 return -1;
06983 }
06984
06985 if (dest == NULL)
06986 dest = "";
06987 sip_pvt_lock(p);
06988 if (ast->_state == AST_STATE_RING)
06989 res = sip_sipredirect(p, dest);
06990 else
06991 res = transmit_refer(p, dest);
06992 sip_pvt_unlock(p);
06993 return res;
06994 }
06995
06996
06997 static int interpret_t38_parameters(struct sip_pvt *p, const struct ast_control_t38_parameters *parameters)
06998 {
06999 int res = 0;
07000
07001 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) || !p->udptl) {
07002 return -1;
07003 }
07004 switch (parameters->request_response) {
07005 case AST_T38_NEGOTIATED:
07006 case AST_T38_REQUEST_NEGOTIATE:
07007
07008 if (!parameters->max_ifp) {
07009 change_t38_state(p, T38_DISABLED);
07010 if (p->t38.state == T38_PEER_REINVITE) {
07011 AST_SCHED_DEL_UNREF(sched, p->t38id, dialog_unref(p, "when you delete the t38id sched, you should dec the refcount for the stored dialog ptr"));
07012 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
07013 }
07014 break;
07015 } else if (p->t38.state == T38_PEER_REINVITE) {
07016 AST_SCHED_DEL_UNREF(sched, p->t38id, dialog_unref(p, "when you delete the t38id sched, you should dec the refcount for the stored dialog ptr"));
07017 p->t38.our_parms = *parameters;
07018
07019
07020
07021 if (!p->t38.their_parms.fill_bit_removal) {
07022 p->t38.our_parms.fill_bit_removal = FALSE;
07023 }
07024 if (!p->t38.their_parms.transcoding_mmr) {
07025 p->t38.our_parms.transcoding_mmr = FALSE;
07026 }
07027 if (!p->t38.their_parms.transcoding_jbig) {
07028 p->t38.our_parms.transcoding_jbig = FALSE;
07029 }
07030 p->t38.our_parms.version = MIN(p->t38.our_parms.version, p->t38.their_parms.version);
07031 p->t38.our_parms.rate_management = p->t38.their_parms.rate_management;
07032 ast_udptl_set_local_max_ifp(p->udptl, p->t38.our_parms.max_ifp);
07033 change_t38_state(p, T38_ENABLED);
07034 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
07035 } else if (p->t38.state != T38_ENABLED) {
07036 p->t38.our_parms = *parameters;
07037 ast_udptl_set_local_max_ifp(p->udptl, p->t38.our_parms.max_ifp);
07038 change_t38_state(p, T38_LOCAL_REINVITE);
07039 if (!p->pendinginvite) {
07040 transmit_reinvite_with_sdp(p, TRUE, FALSE);
07041 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
07042 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
07043 }
07044 }
07045 break;
07046 case AST_T38_TERMINATED:
07047 case AST_T38_REFUSED:
07048 case AST_T38_REQUEST_TERMINATE:
07049 if (p->t38.state == T38_PEER_REINVITE) {
07050 AST_SCHED_DEL_UNREF(sched, p->t38id, dialog_unref(p, "when you delete the t38id sched, you should dec the refcount for the stored dialog ptr"));
07051 change_t38_state(p, T38_DISABLED);
07052 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
07053 } else if (p->t38.state == T38_ENABLED)
07054 transmit_reinvite_with_sdp(p, FALSE, FALSE);
07055 break;
07056 case AST_T38_REQUEST_PARMS: {
07057 struct ast_control_t38_parameters parameters = p->t38.their_parms;
07058
07059 if (p->t38.state == T38_PEER_REINVITE) {
07060 AST_SCHED_DEL_UNREF(sched, p->t38id, dialog_unref(p, "when you delete the t38id sched, you should dec the refcount for the stored dialog ptr"));
07061 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
07062 parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
07063 if (p->owner) {
07064 ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, ¶meters, sizeof(parameters));
07065 }
07066
07067
07068
07069
07070 res = AST_T38_REQUEST_PARMS;
07071 }
07072 break;
07073 }
07074 default:
07075 res = -1;
07076 break;
07077 }
07078
07079 return res;
07080 }
07081
07082
07083
07084
07085
07086
07087
07088
07089
07090
07091 static int initialize_udptl(struct sip_pvt *p)
07092 {
07093 int natflags = ast_test_flag(&p->flags[1], SIP_PAGE2_SYMMETRICRTP);
07094
07095 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT)) {
07096 return 1;
07097 }
07098
07099
07100 if (p->udptl) {
07101 return 0;
07102 }
07103
07104
07105 if ((p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &bindaddr))) {
07106 if (p->owner) {
07107 ast_channel_set_fd(p->owner, 5, ast_udptl_fd(p->udptl));
07108 }
07109
07110 ast_udptl_setqos(p->udptl, global_tos_audio, global_cos_audio);
07111 p->t38_maxdatagram = p->relatedpeer ? p->relatedpeer->t38_maxdatagram : global_t38_maxdatagram;
07112 set_t38_capabilities(p);
07113
07114 ast_debug(1, "Setting NAT on UDPTL to %s\n", natflags ? "On" : "Off");
07115 ast_udptl_setnat(p->udptl, natflags);
07116 } else {
07117 ast_log(AST_LOG_WARNING, "UDPTL creation failed - disabling T38 for this dialog\n");
07118 ast_clear_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT);
07119 return 1;
07120 }
07121
07122 return 0;
07123 }
07124
07125
07126
07127
07128
07129
07130 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
07131 {
07132 struct sip_pvt *p = ast->tech_pvt;
07133 int res = 0;
07134
07135 if (!p) {
07136 ast_debug(1, "Asked to indicate condition on channel %s with no pvt; ignoring\n",
07137 ast->name);
07138 return res;
07139 }
07140
07141 sip_pvt_lock(p);
07142 switch(condition) {
07143 case AST_CONTROL_RINGING:
07144 if (ast->_state == AST_STATE_RING) {
07145 p->invitestate = INV_EARLY_MEDIA;
07146 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
07147 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
07148
07149 transmit_provisional_response(p, "180 Ringing", &p->initreq, 0);
07150 ast_set_flag(&p->flags[0], SIP_RINGING);
07151 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
07152 break;
07153 } else {
07154
07155 }
07156 }
07157 res = -1;
07158 break;
07159 case AST_CONTROL_BUSY:
07160 if (ast->_state != AST_STATE_UP) {
07161 transmit_response_reliable(p, "486 Busy Here", &p->initreq);
07162 p->invitestate = INV_COMPLETED;
07163 sip_alreadygone(p);
07164 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
07165 break;
07166 }
07167 res = -1;
07168 break;
07169 case AST_CONTROL_CONGESTION:
07170 if (ast->_state != AST_STATE_UP) {
07171 transmit_response_reliable(p, "503 Service Unavailable", &p->initreq);
07172 p->invitestate = INV_COMPLETED;
07173 sip_alreadygone(p);
07174 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
07175 break;
07176 }
07177 res = -1;
07178 break;
07179 case AST_CONTROL_INCOMPLETE:
07180 if (ast->_state != AST_STATE_UP) {
07181 switch (ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP)) {
07182 case SIP_PAGE2_ALLOWOVERLAP_YES:
07183 transmit_response_reliable(p, "484 Address Incomplete", &p->initreq);
07184 p->invitestate = INV_COMPLETED;
07185 sip_alreadygone(p);
07186 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
07187 break;
07188 case SIP_PAGE2_ALLOWOVERLAP_DTMF:
07189
07190 break;
07191 default:
07192
07193 transmit_response_reliable(p, "404 Not Found", &p->initreq);
07194 p->invitestate = INV_COMPLETED;
07195 sip_alreadygone(p);
07196 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
07197 break;
07198 }
07199 }
07200 break;
07201 case AST_CONTROL_PROCEEDING:
07202 if ((ast->_state != AST_STATE_UP) &&
07203 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
07204 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
07205 transmit_response(p, "100 Trying", &p->initreq);
07206 p->invitestate = INV_PROCEEDING;
07207 break;
07208 }
07209 res = -1;
07210 break;
07211 case AST_CONTROL_PROGRESS:
07212 if ((ast->_state != AST_STATE_UP) &&
07213 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
07214 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
07215 p->invitestate = INV_EARLY_MEDIA;
07216 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
07217 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
07218 break;
07219 }
07220 res = -1;
07221 break;
07222 case AST_CONTROL_HOLD:
07223 ast_rtp_instance_update_source(p->rtp);
07224 ast_moh_start(ast, data, p->mohinterpret);
07225 break;
07226 case AST_CONTROL_UNHOLD:
07227 ast_rtp_instance_update_source(p->rtp);
07228 ast_moh_stop(ast);
07229 break;
07230 case AST_CONTROL_VIDUPDATE:
07231 if (p->vrtp && !p->novideo) {
07232 transmit_info_with_vidupdate(p);
07233
07234 } else
07235 res = -1;
07236 break;
07237 case AST_CONTROL_T38_PARAMETERS:
07238 res = -1;
07239 if (datalen != sizeof(struct ast_control_t38_parameters)) {
07240 ast_log(LOG_ERROR, "Invalid datalen for AST_CONTROL_T38_PARAMETERS. Expected %d, got %d\n", (int) sizeof(struct ast_control_t38_parameters), (int) datalen);
07241 } else {
07242 const struct ast_control_t38_parameters *parameters = data;
07243 if (!initialize_udptl(p)) {
07244 res = interpret_t38_parameters(p, parameters);
07245 }
07246 }
07247 break;
07248 case AST_CONTROL_SRCUPDATE:
07249 ast_rtp_instance_update_source(p->rtp);
07250 break;
07251 case AST_CONTROL_SRCCHANGE:
07252 ast_rtp_instance_change_source(p->rtp);
07253 break;
07254 case AST_CONTROL_CONNECTED_LINE:
07255 update_connectedline(p, data, datalen);
07256 break;
07257 case AST_CONTROL_REDIRECTING:
07258 update_redirecting(p, data, datalen);
07259 break;
07260 case AST_CONTROL_AOC:
07261 {
07262 struct ast_aoc_decoded *decoded = ast_aoc_decode((struct ast_aoc_encoded *) data, datalen, ast);
07263 if (!decoded) {
07264 ast_log(LOG_ERROR, "Error decoding indicated AOC data\n");
07265 res = -1;
07266 break;
07267 }
07268 switch (ast_aoc_get_msg_type(decoded)) {
07269 case AST_AOC_REQUEST:
07270 if (ast_aoc_get_termination_request(decoded)) {
07271
07272
07273
07274
07275
07276
07277
07278 ast_debug(1, "AOC-E termination request received on %s. This is not yet supported on sip. Continue with hangup \n", p->owner->name);
07279 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
07280 }
07281 break;
07282 case AST_AOC_D:
07283 case AST_AOC_E:
07284 if (ast_test_flag(&p->flags[2], SIP_PAGE3_SNOM_AOC)) {
07285 transmit_info_with_aoc(p, decoded);
07286 }
07287 break;
07288 case AST_AOC_S:
07289 default:
07290 break;
07291 }
07292 ast_aoc_destroy_decoded(decoded);
07293 }
07294 break;
07295 case AST_CONTROL_UPDATE_RTP_PEER:
07296 break;
07297 case AST_CONTROL_FLASH:
07298 res = -1;
07299 break;
07300 case -1:
07301 res = -1;
07302 break;
07303 default:
07304 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
07305 res = -1;
07306 break;
07307 }
07308 sip_pvt_unlock(p);
07309 return res;
07310 }
07311
07312
07313
07314
07315
07316
07317
07318
07319
07320
07321
07322
07323 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid)
07324 {
07325 struct ast_channel *tmp;
07326 struct ast_variable *v = NULL;
07327 format_t fmt;
07328 format_t what;
07329 format_t video;
07330 format_t text;
07331 format_t needvideo = 0;
07332 int needtext = 0;
07333 char buf[SIPBUFSIZE];
07334 char *exten;
07335
07336 {
07337 const char *my_name;
07338
07339 if (title) {
07340 my_name = title;
07341 } else {
07342 my_name = ast_strdupa(i->fromdomain);
07343 }
07344
07345 sip_pvt_unlock(i);
07346
07347 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, linkedid, i->amaflags, "SIP/%s-%08x", my_name, (unsigned)ast_atomic_fetchadd_int((int *)&chan_idx, +1));
07348 }
07349 if (!tmp) {
07350 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
07351 sip_pvt_lock(i);
07352 return NULL;
07353 }
07354 ast_channel_lock(tmp);
07355 sip_pvt_lock(i);
07356 ast_channel_cc_params_init(tmp, i->cc_params);
07357 tmp->caller.id.tag = ast_strdup(i->cid_tag);
07358
07359 tmp->tech = ( ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO || ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_SHORTINFO) ? &sip_tech_info : &sip_tech;
07360
07361
07362
07363 if (i->jointcapability) {
07364 what = i->jointcapability;
07365 video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
07366 text = i->jointcapability & AST_FORMAT_TEXT_MASK;
07367 } else if (i->capability) {
07368 what = i->capability;
07369 video = i->capability & AST_FORMAT_VIDEO_MASK;
07370 text = i->capability & AST_FORMAT_TEXT_MASK;
07371 } else {
07372 what = sip_cfg.capability;
07373 video = sip_cfg.capability & AST_FORMAT_VIDEO_MASK;
07374 text = sip_cfg.capability & AST_FORMAT_TEXT_MASK;
07375 }
07376
07377
07378 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video | text;
07379 ast_debug(3, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
07380 ast_debug(3, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
07381 ast_debug(3, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
07382 ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
07383 if (i->prefcodec)
07384 ast_debug(3, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
07385
07386
07387 fmt = ast_best_codec(tmp->nativeformats);
07388
07389
07390
07391
07392
07393 if (i->vrtp) {
07394 if (ast_test_flag(&i->flags[1], SIP_PAGE2_VIDEOSUPPORT))
07395 needvideo = AST_FORMAT_VIDEO_MASK;
07396 else if (i->prefcodec)
07397 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
07398 else
07399 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
07400 }
07401
07402 if (i->trtp) {
07403 if (i->prefcodec)
07404 needtext = i->prefcodec & AST_FORMAT_TEXT_MASK;
07405 else
07406 needtext = i->jointcapability & AST_FORMAT_TEXT_MASK;
07407 }
07408
07409 if (needvideo)
07410 ast_debug(3, "This channel can handle video! HOLLYWOOD next!\n");
07411 else
07412 ast_debug(3, "This channel will not be able to handle video.\n");
07413
07414 enable_dsp_detect(i);
07415
07416 if ((ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
07417 (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
07418 if (i->rtp) {
07419 ast_rtp_instance_dtmf_mode_set(i->rtp, AST_RTP_DTMF_MODE_INBAND);
07420 }
07421 } else if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) {
07422 if (i->rtp) {
07423 ast_rtp_instance_dtmf_mode_set(i->rtp, AST_RTP_DTMF_MODE_RFC2833);
07424 }
07425 }
07426
07427
07428
07429
07430 if (i->rtp) {
07431 ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(i->rtp, 0));
07432 ast_channel_set_fd(tmp, 1, ast_rtp_instance_fd(i->rtp, 1));
07433 ast_rtp_instance_set_write_format(i->rtp, fmt);
07434 ast_rtp_instance_set_read_format(i->rtp, fmt);
07435 }
07436 if (needvideo && i->vrtp) {
07437 ast_channel_set_fd(tmp, 2, ast_rtp_instance_fd(i->vrtp, 0));
07438 ast_channel_set_fd(tmp, 3, ast_rtp_instance_fd(i->vrtp, 1));
07439 }
07440 if (needtext && i->trtp) {
07441 ast_channel_set_fd(tmp, 4, ast_rtp_instance_fd(i->trtp, 0));
07442 }
07443 if (i->udptl) {
07444 ast_channel_set_fd(tmp, 5, ast_udptl_fd(i->udptl));
07445 }
07446
07447 if (state == AST_STATE_RING)
07448 tmp->rings = 1;
07449 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
07450
07451 tmp->writeformat = fmt;
07452 tmp->rawwriteformat = fmt;
07453
07454 tmp->readformat = fmt;
07455 tmp->rawreadformat = fmt;
07456
07457 tmp->tech_pvt = dialog_ref(i, "sip_new: set chan->tech_pvt to i");
07458
07459 tmp->callgroup = i->callgroup;
07460 tmp->pickupgroup = i->pickupgroup;
07461 tmp->caller.id.name.presentation = i->callingpres;
07462 tmp->caller.id.number.presentation = i->callingpres;
07463 if (!ast_strlen_zero(i->parkinglot))
07464 ast_string_field_set(tmp, parkinglot, i->parkinglot);
07465 if (!ast_strlen_zero(i->accountcode))
07466 ast_string_field_set(tmp, accountcode, i->accountcode);
07467 if (i->amaflags)
07468 tmp->amaflags = i->amaflags;
07469 if (!ast_strlen_zero(i->language))
07470 ast_string_field_set(tmp, language, i->language);
07471 i->owner = tmp;
07472 ast_module_ref(ast_module_info->self);
07473 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
07474
07475
07476
07477
07478 exten = ast_strdupa(i->exten);
07479 sip_pvt_unlock(i);
07480 ast_channel_unlock(tmp);
07481 if (!ast_exists_extension(NULL, i->context, i->exten, 1, i->cid_num)) {
07482 ast_uri_decode(exten);
07483 }
07484 ast_channel_lock(tmp);
07485 sip_pvt_lock(i);
07486 ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
07487
07488
07489
07490 if (!ast_strlen_zero(i->cid_num)) {
07491 tmp->caller.ani.number.valid = 1;
07492 tmp->caller.ani.number.str = ast_strdup(i->cid_num);
07493 }
07494 if (!ast_strlen_zero(i->rdnis)) {
07495 tmp->redirecting.from.number.valid = 1;
07496 tmp->redirecting.from.number.str = ast_strdup(i->rdnis);
07497 }
07498
07499 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s")) {
07500 tmp->dialed.number.str = ast_strdup(i->exten);
07501 }
07502
07503 tmp->priority = 1;
07504 if (!ast_strlen_zero(i->uri))
07505 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
07506 if (!ast_strlen_zero(i->domain))
07507 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
07508 if (!ast_strlen_zero(i->callid))
07509 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
07510 if (i->rtp)
07511 ast_jb_configure(tmp, &global_jbconf);
07512
07513 if (!i->relatedpeer) {
07514 tmp->flags |= AST_FLAG_DISABLE_DEVSTATE_CACHE;
07515 }
07516
07517 for (v = i->chanvars ; v ; v = v->next) {
07518 char valuebuf[1024];
07519 pbx_builtin_setvar_helper(tmp, v->name, ast_get_encoded_str(v->value, valuebuf, sizeof(valuebuf)));
07520 }
07521
07522 if (i->do_history)
07523 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
07524
07525
07526 if (sip_cfg.callevents)
07527 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
07528 "Channel: %s\r\nUniqueid: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\n",
07529 tmp->name, tmp->uniqueid, "SIP", i->callid, i->fullcontact);
07530
07531 return tmp;
07532 }
07533
07534
07535 static char *get_body_by_line(const char *line, const char *name, int nameLen, char delimiter)
07536 {
07537 if (!strncasecmp(line, name, nameLen) && line[nameLen] == delimiter)
07538 return ast_skip_blanks(line + nameLen + 1);
07539
07540 return "";
07541 }
07542
07543
07544
07545
07546
07547 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
07548 {
07549 int len = strlen(name);
07550
07551 while (*start < (req->sdp_start + req->sdp_count)) {
07552 const char *r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[(*start)++]), name, len, '=');
07553 if (r[0] != '\0')
07554 return r;
07555 }
07556
07557
07558 (*start)++;
07559
07560 return "";
07561 }
07562
07563
07564
07565
07566
07567
07568 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value)
07569 {
07570 char type = '\0';
07571 const char *line = NULL;
07572
07573 if (stop > (req->sdp_start + req->sdp_count)) {
07574 stop = req->sdp_start + req->sdp_count;
07575 }
07576
07577 while (*start < stop) {
07578 line = REQ_OFFSET_TO_STR(req, line[(*start)++]);
07579 if (line[1] == '=') {
07580 type = line[0];
07581 *value = ast_skip_blanks(line + 2);
07582 break;
07583 }
07584 }
07585
07586 return type;
07587 }
07588
07589
07590 static char *get_body(struct sip_request *req, char *name, char delimiter)
07591 {
07592 int x;
07593 int len = strlen(name);
07594 char *r;
07595
07596 for (x = 0; x < req->lines; x++) {
07597 r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[x]), name, len, delimiter);
07598 if (r[0] != '\0')
07599 return r;
07600 }
07601
07602 return "";
07603 }
07604
07605
07606 static const char *find_alias(const char *name, const char *_default)
07607 {
07608
07609 static const struct cfalias {
07610 char * const fullname;
07611 char * const shortname;
07612 } aliases[] = {
07613 { "Content-Type", "c" },
07614 { "Content-Encoding", "e" },
07615 { "From", "f" },
07616 { "Call-ID", "i" },
07617 { "Contact", "m" },
07618 { "Content-Length", "l" },
07619 { "Subject", "s" },
07620 { "To", "t" },
07621 { "Supported", "k" },
07622 { "Refer-To", "r" },
07623 { "Referred-By", "b" },
07624 { "Allow-Events", "u" },
07625 { "Event", "o" },
07626 { "Via", "v" },
07627 { "Accept-Contact", "a" },
07628 { "Reject-Contact", "j" },
07629 { "Request-Disposition", "d" },
07630 { "Session-Expires", "x" },
07631 { "Identity", "y" },
07632 { "Identity-Info", "n" },
07633 };
07634 int x;
07635
07636 for (x = 0; x < ARRAY_LEN(aliases); x++) {
07637 if (!strcasecmp(aliases[x].fullname, name))
07638 return aliases[x].shortname;
07639 }
07640
07641 return _default;
07642 }
07643
07644 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
07645 {
07646
07647
07648
07649
07650
07651
07652
07653
07654
07655 const char *sname = find_alias(name, NULL);
07656 int x, len = strlen(name), slen = (sname ? 1 : 0);
07657 for (x = *start; x < req->headers; x++) {
07658 const char *header = REQ_OFFSET_TO_STR(req, header[x]);
07659 int smatch = 0, match = !strncasecmp(header, name, len);
07660 if (slen) {
07661 smatch = !strncasecmp(header, sname, slen);
07662 }
07663 if (match || smatch) {
07664
07665 const char *r = header + (match ? len : slen );
07666 if (sip_cfg.pedanticsipchecking) {
07667 r = ast_skip_blanks(r);
07668 }
07669
07670 if (*r == ':') {
07671 *start = x+1;
07672 return ast_skip_blanks(r+1);
07673 }
07674 }
07675 }
07676
07677
07678 return "";
07679 }
07680
07681
07682
07683
07684 static const char *get_header(const struct sip_request *req, const char *name)
07685 {
07686 int start = 0;
07687 return __get_header(req, name, &start);
07688 }
07689
07690
07691 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
07692 {
07693
07694 struct ast_frame *f;
07695
07696 if (!p->rtp) {
07697
07698 return &ast_null_frame;
07699 }
07700
07701 switch(ast->fdno) {
07702 case 0:
07703 f = ast_rtp_instance_read(p->rtp, 0);
07704 break;
07705 case 1:
07706 f = ast_rtp_instance_read(p->rtp, 1);
07707 break;
07708 case 2:
07709 f = ast_rtp_instance_read(p->vrtp, 0);
07710 break;
07711 case 3:
07712 f = ast_rtp_instance_read(p->vrtp, 1);
07713 break;
07714 case 4:
07715 f = ast_rtp_instance_read(p->trtp, 0);
07716 if (sipdebug_text) {
07717 int i;
07718 unsigned char* arr = f->data.ptr;
07719 for (i=0; i < f->datalen; i++)
07720 ast_verbose("%c", (arr[i] > ' ' && arr[i] < '}') ? arr[i] : '.');
07721 ast_verbose(" -> ");
07722 for (i=0; i < f->datalen; i++)
07723 ast_verbose("%02X ", (unsigned)arr[i]);
07724 ast_verbose("\n");
07725 }
07726 break;
07727 case 5:
07728 f = ast_udptl_read(p->udptl);
07729 break;
07730 default:
07731 f = &ast_null_frame;
07732 }
07733
07734 if (f && (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) &&
07735 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833)) {
07736 ast_debug(1, "Ignoring DTMF (%c) RTP frame because dtmfmode is not RFC2833\n", f->subclass.integer);
07737 ast_frfree(f);
07738 return &ast_null_frame;
07739 }
07740
07741
07742 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
07743 return f;
07744
07745 if (f && f->subclass.codec != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
07746 if (!(f->subclass.codec & p->jointcapability)) {
07747 ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
07748 ast_getformatname(f->subclass.codec), p->owner->name);
07749 ast_frfree(f);
07750 return &ast_null_frame;
07751 }
07752 ast_debug(1, "Oooh, format changed to %s\n",
07753 ast_getformatname(f->subclass.codec));
07754 p->owner->nativeformats = (p->owner->nativeformats & (AST_FORMAT_VIDEO_MASK | AST_FORMAT_TEXT_MASK)) | f->subclass.codec;
07755 ast_set_read_format(p->owner, p->owner->readformat);
07756 ast_set_write_format(p->owner, p->owner->writeformat);
07757 }
07758
07759 if (f && p->dsp) {
07760 f = ast_dsp_process(p->owner, p->dsp, f);
07761 if (f && f->frametype == AST_FRAME_DTMF) {
07762 if (f->subclass.integer == 'f') {
07763 ast_debug(1, "Fax CNG detected on %s\n", ast->name);
07764 *faxdetect = 1;
07765
07766 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
07767 ast_dsp_set_features(p->dsp, DSP_FEATURE_DIGIT_DETECT);
07768 } else {
07769 ast_dsp_free(p->dsp);
07770 p->dsp = NULL;
07771 }
07772 } else {
07773 ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass.integer);
07774 }
07775 }
07776 }
07777
07778 return f;
07779 }
07780
07781
07782 static struct ast_frame *sip_read(struct ast_channel *ast)
07783 {
07784 struct ast_frame *fr;
07785 struct sip_pvt *p = ast->tech_pvt;
07786 int faxdetected = FALSE;
07787
07788 sip_pvt_lock(p);
07789 fr = sip_rtp_read(ast, p, &faxdetected);
07790 p->lastrtprx = time(NULL);
07791
07792
07793 if (faxdetected && ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_CNG)) {
07794 if (strcmp(ast->exten, "fax")) {
07795 const char *target_context = S_OR(ast->macrocontext, ast->context);
07796
07797
07798
07799
07800
07801 sip_pvt_unlock(p);
07802 ast_channel_unlock(ast);
07803 if (ast_exists_extension(ast, target_context, "fax", 1,
07804 S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, NULL))) {
07805 ast_channel_lock(ast);
07806 sip_pvt_lock(p);
07807 ast_verbose(VERBOSE_PREFIX_2 "Redirecting '%s' to fax extension due to CNG detection\n", ast->name);
07808 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
07809 if (ast_async_goto(ast, target_context, "fax", 1)) {
07810 ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
07811 }
07812 ast_frfree(fr);
07813 fr = &ast_null_frame;
07814 } else {
07815 ast_channel_lock(ast);
07816 sip_pvt_lock(p);
07817 ast_log(LOG_NOTICE, "FAX CNG detected but no fax extension\n");
07818 }
07819 }
07820 }
07821
07822
07823 if (fr && fr->frametype == AST_FRAME_VOICE && p->invitestate != INV_EARLY_MEDIA && ast->_state != AST_STATE_UP) {
07824 ast_frfree(fr);
07825 fr = &ast_null_frame;
07826 }
07827
07828 sip_pvt_unlock(p);
07829
07830 return fr;
07831 }
07832
07833
07834
07835 static char *generate_random_string(char *buf, size_t size)
07836 {
07837 long val[4];
07838 int x;
07839
07840 for (x=0; x<4; x++)
07841 val[x] = ast_random();
07842 snprintf(buf, size, "%08lx%08lx%08lx%08lx", (unsigned long)val[0], (unsigned long)val[1], (unsigned long)val[2], (unsigned long)val[3]);
07843
07844 return buf;
07845 }
07846
07847 static char *generate_uri(struct sip_pvt *pvt, char *buf, size_t size)
07848 {
07849 struct ast_str *uri = ast_str_alloca(size);
07850 ast_str_set(&uri, 0, "%s", pvt->socket.type == SIP_TRANSPORT_TLS ? "sips:" : "sip:");
07851
07852
07853
07854 ast_str_append(&uri, 0, "%s", generate_random_string(buf, size));
07855 ast_str_append(&uri, 0, "@%s", ast_sockaddr_stringify_remote(&pvt->ourip));
07856 ast_copy_string(buf, ast_str_buffer(uri), size);
07857 return buf;
07858 }
07859
07860
07861
07862
07863
07864
07865
07866
07867 static void build_callid_pvt(struct sip_pvt *pvt)
07868 {
07869 char buf[33];
07870 const char *host = S_OR(pvt->fromdomain, ast_sockaddr_stringify_remote(&pvt->ourip));
07871
07872 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
07873 }
07874
07875
07876 #define CONTAINER_UNLINK(container, obj, tag) \
07877 ({ \
07878 int found = 0; \
07879 typeof((obj)) __removed_obj; \
07880 __removed_obj = ao2_t_callback((container), \
07881 OBJ_UNLINK | OBJ_POINTER, ao2_match_by_addr, (obj), (tag)); \
07882 if (__removed_obj) { \
07883 ao2_ref(__removed_obj, -1); \
07884 found = 1; \
07885 } \
07886 found; \
07887 })
07888
07889
07890
07891
07892
07893
07894
07895
07896
07897
07898 static void change_callid_pvt(struct sip_pvt *pvt, const char *callid)
07899 {
07900 int in_dialog_container;
07901 char *oldid = ast_strdupa(pvt->callid);
07902
07903 ao2_lock(dialogs);
07904 in_dialog_container = CONTAINER_UNLINK(dialogs, pvt,
07905 "About to change the callid -- remove the old name");
07906 if (callid) {
07907 ast_string_field_set(pvt, callid, callid);
07908 } else {
07909 build_callid_pvt(pvt);
07910 }
07911 if (in_dialog_container) {
07912 ao2_t_link(dialogs, pvt, "New dialog callid -- inserted back into table");
07913 }
07914 ao2_unlock(dialogs);
07915
07916 if (strcmp(oldid, pvt->callid)) {
07917 ast_debug(1, "SIP call-id changed from '%s' to '%s'\n", oldid, pvt->callid);
07918 }
07919 }
07920
07921
07922 static void build_callid_registry(struct sip_registry *reg, const struct ast_sockaddr *ourip, const char *fromdomain)
07923 {
07924 char buf[33];
07925
07926 const char *host = S_OR(fromdomain, ast_sockaddr_stringify_host_remote(ourip));
07927
07928 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
07929 }
07930
07931
07932 static void build_localtag_registry(struct sip_registry *reg)
07933 {
07934 ast_string_field_build(reg, localtag, "as%08lx", (unsigned long)ast_random());
07935 }
07936
07937
07938 static void make_our_tag(struct sip_pvt *pvt)
07939 {
07940 ast_string_field_build(pvt, tag, "as%08lx", (unsigned long)ast_random());
07941 }
07942
07943
07944 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p)
07945 {
07946 struct sip_st_dlg *stp;
07947
07948 if (p->stimer) {
07949 ast_log(LOG_ERROR, "Session-Timer struct already allocated\n");
07950 return p->stimer;
07951 }
07952
07953 if (!(stp = ast_calloc(1, sizeof(struct sip_st_dlg))))
07954 return NULL;
07955
07956 p->stimer = stp;
07957
07958 stp->st_schedid = -1;
07959
07960 return p->stimer;
07961 }
07962
07963
07964
07965
07966
07967 struct sip_pvt *sip_alloc(ast_string_field callid, struct ast_sockaddr *addr,
07968 int useglobal_nat, const int intended_method, struct sip_request *req)
07969 {
07970 struct sip_pvt *p;
07971
07972 if (!(p = ao2_t_alloc(sizeof(*p), sip_destroy_fn, "allocate a dialog(pvt) struct")))
07973 return NULL;
07974
07975 if (ast_string_field_init(p, 512)) {
07976 ao2_t_ref(p, -1, "failed to string_field_init, drop p");
07977 return NULL;
07978 }
07979
07980 if (!(p->cc_params = ast_cc_config_params_init())) {
07981 ao2_t_ref(p, -1, "Yuck, couldn't allocate cc_params struct. Get rid o' p");
07982 return NULL;
07983 }
07984
07985
07986
07987 if (req) {
07988 struct sip_via *via;
07989 const char *cseq = get_header(req, "Cseq");
07990 uint32_t seqno;
07991
07992
07993 via = parse_via(get_header(req, "Via"));
07994 if (via) {
07995
07996
07997 if (!ast_strlen_zero(via->branch) && !strncasecmp(via->branch, "z9hG4bK", 7)) {
07998 ast_string_field_set(p, initviabranch, via->branch);
07999 ast_string_field_set(p, initviasentby, via->sent_by);
08000 }
08001 free_via(via);
08002 }
08003
08004
08005
08006 if (!ast_strlen_zero(cseq) && (sscanf(cseq, "%30u", &seqno) == 1)) {
08007 p->init_icseq = seqno;
08008 }
08009
08010 set_socket_transport(&p->socket, req->socket.type);
08011 } else {
08012 set_socket_transport(&p->socket, SIP_TRANSPORT_UDP);
08013 }
08014
08015 p->socket.fd = -1;
08016 p->method = intended_method;
08017 p->initid = -1;
08018 p->waitid = -1;
08019 p->reinviteid = -1;
08020 p->autokillid = -1;
08021 p->request_queue_sched_id = -1;
08022 p->provisional_keepalive_sched_id = -1;
08023 p->t38id = -1;
08024 p->subscribed = NONE;
08025 p->stateid = -1;
08026 p->sessionversion_remote = -1;
08027 p->session_modify = TRUE;
08028 p->stimer = NULL;
08029 p->prefs = default_prefs;
08030 p->maxforwards = sip_cfg.default_max_forwards;
08031
08032 if (intended_method != SIP_OPTIONS) {
08033 p->timer_t1 = global_t1;
08034 p->timer_b = global_timer_b;
08035 }
08036
08037 if (!addr) {
08038 p->ourip = internip;
08039 } else {
08040 ast_sockaddr_copy(&p->sa, addr);
08041 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
08042 }
08043
08044
08045 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
08046 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
08047 ast_copy_flags(&p->flags[2], &global_flags[2], SIP_PAGE3_FLAGS_TO_COPY);
08048
08049 p->do_history = recordhistory;
08050
08051 p->branch = ast_random();
08052 make_our_tag(p);
08053 p->ocseq = INITIAL_CSEQ;
08054 p->allowed_methods = UINT_MAX;
08055
08056 if (sip_methods[intended_method].need_rtp) {
08057 p->maxcallbitrate = default_maxcallbitrate;
08058 p->autoframing = global_autoframing;
08059 }
08060
08061 if (useglobal_nat && addr) {
08062
08063 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT_FORCE_RPORT);
08064 ast_sockaddr_copy(&p->recv, addr);
08065
08066 do_setnat(p);
08067 }
08068
08069 if (p->method != SIP_REGISTER) {
08070 ast_string_field_set(p, fromdomain, default_fromdomain);
08071 p->fromdomainport = default_fromdomainport;
08072 }
08073 build_via(p);
08074 if (!callid)
08075 build_callid_pvt(p);
08076 else
08077 ast_string_field_set(p, callid, callid);
08078
08079 ast_string_field_set(p, mohinterpret, default_mohinterpret);
08080 ast_string_field_set(p, mohsuggest, default_mohsuggest);
08081 p->capability = sip_cfg.capability;
08082 p->allowtransfer = sip_cfg.allowtransfer;
08083 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
08084 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
08085 p->noncodeccapability |= AST_RTP_DTMF;
08086 }
08087 ast_string_field_set(p, context, sip_cfg.default_context);
08088 ast_string_field_set(p, parkinglot, default_parkinglot);
08089 ast_string_field_set(p, engine, default_engine);
08090
08091 AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
08092
08093
08094
08095 ao2_t_link(dialogs, p, "link pvt into dialogs table");
08096
08097 ast_debug(1, "Allocating new SIP dialog for %s - %s (%s)\n", callid ? callid : p->callid, sip_methods[intended_method].text, p->rtp ? "With RTP" : "No RTP");
08098 return p;
08099 }
08100
08101
08102
08103
08104
08105
08106
08107
08108 static int addr_is_multicast(const struct ast_sockaddr *addr)
08109 {
08110 return ((ast_sockaddr_ipv4(addr) & 0xf0000000) == 0xe0000000);
08111 }
08112
08113
08114
08115
08116
08117
08118
08119
08120
08121
08122
08123
08124
08125
08126
08127
08128 static int process_via(struct sip_pvt *p, const struct sip_request *req)
08129 {
08130 struct sip_via *via = parse_via(get_header(req, "Via"));
08131
08132 if (!via) {
08133 ast_log(LOG_ERROR, "error processing via header\n");
08134 return -1;
08135 }
08136
08137 if (via->maddr) {
08138 if (ast_sockaddr_resolve_first_transport(&p->sa, via->maddr, PARSE_PORT_FORBID, p->socket.type)) {
08139 ast_log(LOG_WARNING, "Can't find address for maddr '%s'\n", via->maddr);
08140 ast_log(LOG_ERROR, "error processing via header\n");
08141 free_via(via);
08142 return -1;
08143 }
08144
08145 if (addr_is_multicast(&p->sa)) {
08146 setsockopt(sipsock, IPPROTO_IP, IP_MULTICAST_TTL, &via->ttl, sizeof(via->ttl));
08147 }
08148 }
08149
08150 ast_sockaddr_set_port(&p->sa, via->port ? via->port : STANDARD_SIP_PORT);
08151
08152 free_via(via);
08153 return 0;
08154 }
08155
08156
08157 struct match_req_args {
08158 int method;
08159 const char *callid;
08160 const char *totag;
08161 const char *fromtag;
08162 uint32_t seqno;
08163
08164
08165 const char *ruri;
08166 const char *viabranch;
08167 const char *viasentby;
08168
08169
08170 int authentication_present;
08171 };
08172
08173 enum match_req_res {
08174 SIP_REQ_MATCH,
08175 SIP_REQ_NOT_MATCH,
08176 SIP_REQ_LOOP_DETECTED,
08177 };
08178
08179
08180
08181
08182
08183
08184 static enum match_req_res match_req_to_dialog(struct sip_pvt *sip_pvt_ptr, struct match_req_args *arg)
08185 {
08186 const char *init_ruri = NULL;
08187 if (sip_pvt_ptr->initreq.headers) {
08188 init_ruri = REQ_OFFSET_TO_STR(&sip_pvt_ptr->initreq, rlPart2);
08189 }
08190
08191
08192
08193
08194 if (!ast_strlen_zero(arg->callid) && strcmp(sip_pvt_ptr->callid, arg->callid)) {
08195
08196 return SIP_REQ_NOT_MATCH;
08197 }
08198 if (arg->method == SIP_RESPONSE) {
08199
08200
08201 if (!ast_strlen_zero(sip_pvt_ptr->theirtag) && ast_test_flag(&sip_pvt_ptr->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
08202 if (ast_strlen_zero(arg->totag)) {
08203
08204 return SIP_REQ_NOT_MATCH;
08205 }
08206 if (strcmp(arg->totag, sip_pvt_ptr->theirtag)) {
08207
08208 return SIP_REQ_NOT_MATCH;
08209 }
08210 }
08211
08212 if (strcmp(arg->fromtag, sip_pvt_ptr->tag)) {
08213
08214 return SIP_REQ_NOT_MATCH;
08215 }
08216 } else {
08217
08218
08219
08220 if (!arg->authentication_present && strcmp(arg->fromtag, sip_pvt_ptr->theirtag)) {
08221
08222 return SIP_REQ_NOT_MATCH;
08223 }
08224
08225 if (!ast_strlen_zero(arg->totag) && (strcmp(arg->totag, sip_pvt_ptr->tag))) {
08226
08227 return SIP_REQ_NOT_MATCH;
08228 }
08229 }
08230
08231
08232
08233
08234
08235
08236
08237
08238
08239
08240
08241
08242
08243
08244
08245
08246
08247 if ((arg->method != SIP_RESPONSE) &&
08248 ast_strlen_zero(arg->totag) &&
08249 (sip_pvt_ptr->init_icseq == arg->seqno) &&
08250 !ast_strlen_zero(sip_pvt_ptr->initviabranch) &&
08251 init_ruri) {
08252
08253
08254 if (ast_strlen_zero(arg->viabranch) ||
08255 strcmp(arg->viabranch, sip_pvt_ptr->initviabranch) ||
08256 ast_strlen_zero(arg->viasentby) ||
08257 strcmp(arg->viasentby, sip_pvt_ptr->initviasentby)) {
08258
08259
08260
08261 if ((sip_pvt_ptr->method != arg->method)) {
08262 return SIP_REQ_NOT_MATCH;
08263 }
08264
08265
08266
08267 if (sip_uri_cmp(init_ruri, arg->ruri)) {
08268
08269 return SIP_REQ_NOT_MATCH;
08270 }
08271
08272
08273
08274
08275
08276
08277
08278
08279
08280
08281
08282
08283
08284
08285
08286
08287
08288
08289
08290 return SIP_REQ_LOOP_DETECTED;
08291 }
08292 }
08293
08294
08295
08296
08297
08298
08299 if ((arg->method != SIP_RESPONSE) &&
08300 ast_strlen_zero(arg->totag) &&
08301 arg->authentication_present &&
08302 sip_uri_cmp(init_ruri, arg->ruri)) {
08303
08304
08305 return SIP_REQ_NOT_MATCH;
08306 }
08307
08308 return SIP_REQ_MATCH;
08309 }
08310
08311
08312
08313
08314
08315
08316
08317
08318
08319
08320
08321
08322
08323
08324
08325
08326
08327
08328 static struct ast_channel *sip_pvt_lock_full(struct sip_pvt *pvt)
08329 {
08330 struct ast_channel *chan;
08331
08332
08333
08334
08335 for (;;) {
08336
08337 sip_pvt_lock(pvt);
08338 chan = pvt->owner;
08339 if (chan) {
08340
08341
08342
08343 ast_channel_ref(chan);
08344 } else {
08345
08346 return NULL;
08347 }
08348
08349
08350
08351
08352 sip_pvt_unlock(pvt);
08353
08354
08355 ast_channel_lock(chan);
08356 sip_pvt_lock(pvt);
08357
08358 if (pvt->owner == chan) {
08359
08360 break;
08361 }
08362
08363
08364
08365
08366
08367
08368 ast_channel_unlock(chan);
08369 ast_channel_unref(chan);
08370 sip_pvt_unlock(pvt);
08371 }
08372
08373
08374 return pvt->owner;
08375 }
08376
08377
08378
08379
08380
08381
08382 static struct sip_pvt *find_call(struct sip_request *req, struct ast_sockaddr *addr, const int intended_method)
08383 {
08384 char totag[128];
08385 char fromtag[128];
08386 const char *callid = get_header(req, "Call-ID");
08387 const char *from = get_header(req, "From");
08388 const char *to = get_header(req, "To");
08389 const char *cseq = get_header(req, "Cseq");
08390 struct sip_pvt *sip_pvt_ptr;
08391 uint32_t seqno;
08392
08393
08394 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
08395 ast_strlen_zero(from) || ast_strlen_zero(cseq) ||
08396 (sscanf(cseq, "%30u", &seqno) != 1)) {
08397
08398
08399 if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
08400 transmit_response_using_temp(callid, addr, 1, intended_method,
08401 req, "400 Bad Request");
08402 }
08403 return NULL;
08404 }
08405
08406 if (sip_cfg.pedanticsipchecking) {
08407
08408
08409
08410
08411
08412
08413 if (gettag(req, "To", totag, sizeof(totag)))
08414 req->has_to_tag = 1;
08415 gettag(req, "From", fromtag, sizeof(fromtag));
08416
08417 ast_debug(5, "= Looking for Call ID: %s (Checking %s) --From tag %s --To-tag %s \n", callid, req->method==SIP_RESPONSE ? "To" : "From", fromtag, totag);
08418
08419
08420 if (ast_strlen_zero(fromtag)) {
08421 ast_debug(5, "%s request has no from tag, dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
08422 return NULL;
08423 }
08424
08425 if (ast_strlen_zero(totag) && (req->method == SIP_ACK || req->method == SIP_BYE || req->method == SIP_INFO )) {
08426 ast_debug(5, "%s must have a to tag. dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
08427 return NULL;
08428 }
08429 }
08430
08431 if (!sip_cfg.pedanticsipchecking) {
08432 struct sip_pvt tmp_dialog = {
08433 .callid = callid,
08434 };
08435 sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find in dialogs");
08436 if (sip_pvt_ptr) {
08437
08438 return sip_pvt_ptr;
08439 }
08440 } else {
08441 struct sip_pvt tmp_dialog = {
08442 .callid = callid,
08443 };
08444 struct match_req_args args = { 0, };
08445 int found;
08446 struct ao2_iterator *iterator = ao2_t_callback(dialogs,
08447 OBJ_POINTER | OBJ_MULTIPLE,
08448 dialog_find_multiple,
08449 &tmp_dialog,
08450 "pedantic ao2_find in dialogs");
08451 struct sip_via *via = NULL;
08452
08453 args.method = req->method;
08454 args.callid = NULL;
08455 args.totag = totag;
08456 args.fromtag = fromtag;
08457 args.seqno = seqno;
08458
08459
08460 if (req->method != SIP_RESPONSE) {
08461 args.ruri = REQ_OFFSET_TO_STR(req, rlPart2);
08462 via = parse_via(get_header(req, "Via"));
08463 if (via) {
08464 args.viasentby = via->sent_by;
08465 args.viabranch = via->branch;
08466 }
08467 if (!ast_strlen_zero(get_header(req, "Authorization")) ||
08468 !ast_strlen_zero(get_header(req, "Proxy-Authorization"))) {
08469 args.authentication_present = 1;
08470 }
08471 }
08472
08473
08474 while (iterator && (sip_pvt_ptr = ao2_iterator_next(iterator))) {
08475 sip_pvt_lock(sip_pvt_ptr);
08476 found = match_req_to_dialog(sip_pvt_ptr, &args);
08477 sip_pvt_unlock(sip_pvt_ptr);
08478
08479 switch (found) {
08480 case SIP_REQ_MATCH:
08481 ao2_iterator_destroy(iterator);
08482 free_via(via);
08483 return sip_pvt_ptr;
08484 case SIP_REQ_LOOP_DETECTED:
08485
08486
08487 transmit_response_using_temp(callid, addr, 1, intended_method, req, "482 (Loop Detected)");
08488 dialog_unref(sip_pvt_ptr, "pvt did not match incoming SIP msg, unref from search.");
08489 ao2_iterator_destroy(iterator);
08490 free_via(via);
08491 return NULL;
08492 case SIP_REQ_NOT_MATCH:
08493 default:
08494 dialog_unref(sip_pvt_ptr, "pvt did not match incoming SIP msg, unref from search");
08495 break;
08496 }
08497 }
08498 if (iterator) {
08499 ao2_iterator_destroy(iterator);
08500 }
08501
08502 free_via(via);
08503 }
08504
08505
08506 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
08507 struct sip_pvt *p = NULL;
08508
08509 if (intended_method == SIP_REFER) {
08510
08511 transmit_response_using_temp(callid, addr, 1, intended_method, req, "603 Declined (no dialog)");
08512
08513
08514 } else if (!(p = sip_alloc(callid, addr, 1, intended_method, req))) {
08515
08516
08517
08518
08519
08520
08521
08522
08523 transmit_response_using_temp(callid, addr, 1, intended_method, req, "500 Server internal error");
08524 ast_debug(4, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
08525 }
08526 return p;
08527 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
08528
08529 transmit_response_using_temp(callid, addr, 1, intended_method, req, "501 Method Not Implemented");
08530 ast_debug(2, "Got a request with unsupported SIP method.\n");
08531 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
08532
08533 transmit_response_using_temp(callid, addr, 1, intended_method, req, "481 Call leg/transaction does not exist");
08534 ast_debug(2, "That's odd... Got a request in unknown dialog. Callid %s\n", callid ? callid : "<unknown>");
08535 }
08536
08537
08538 if (intended_method == SIP_RESPONSE)
08539 ast_debug(2, "That's odd... Got a response on a call we don't know about. Callid %s\n", callid ? callid : "<unknown>");
08540
08541 return NULL;
08542 }
08543
08544
08545 static int sip_register(const char *value, int lineno)
08546 {
08547 struct sip_registry *reg;
08548
08549 if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
08550 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
08551 return -1;
08552 }
08553
08554 ast_atomic_fetchadd_int(®objs, 1);
08555 ASTOBJ_INIT(reg);
08556
08557 if (sip_parse_register_line(reg, default_expiry, value, lineno)) {
08558 registry_unref(reg, "failure to parse, unref the reg pointer");
08559 return -1;
08560 }
08561
08562
08563 if (reg->refresh && !reg->expiry && !reg->configured_expiry) {
08564 reg->refresh = reg->expiry = reg->configured_expiry = default_expiry;
08565 }
08566
08567
08568 ASTOBJ_CONTAINER_LINK(®l, reg);
08569
08570
08571 registry_unref(reg, "unref the reg pointer");
08572
08573 return 0;
08574 }
08575
08576
08577 static int sip_subscribe_mwi(const char *value, int lineno)
08578 {
08579 struct sip_subscription_mwi *mwi;
08580 int portnum = 0;
08581 enum sip_transport transport = SIP_TRANSPORT_UDP;
08582 char buf[256] = "";
08583 char *username = NULL, *hostname = NULL, *secret = NULL, *authuser = NULL, *porta = NULL, *mailbox = NULL;
08584
08585 if (!value) {
08586 return -1;
08587 }
08588
08589 ast_copy_string(buf, value, sizeof(buf));
08590
08591 username = buf;
08592
08593 if ((hostname = strrchr(buf, '@'))) {
08594 *hostname++ = '\0';
08595 } else {
08596 return -1;
08597 }
08598
08599 if ((secret = strchr(username, ':'))) {
08600 *secret++ = '\0';
08601 if ((authuser = strchr(secret, ':'))) {
08602 *authuser++ = '\0';
08603 }
08604 }
08605
08606 if ((mailbox = strchr(hostname, '/'))) {
08607 *mailbox++ = '\0';
08608 }
08609
08610 if (ast_strlen_zero(username) || ast_strlen_zero(hostname) || ast_strlen_zero(mailbox)) {
08611 ast_log(LOG_WARNING, "Format for MWI subscription is user[:secret[:authuser]]@host[:port]/mailbox at line %d\n", lineno);
08612 return -1;
08613 }
08614
08615 if ((porta = strchr(hostname, ':'))) {
08616 *porta++ = '\0';
08617 if (!(portnum = atoi(porta))) {
08618 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
08619 return -1;
08620 }
08621 }
08622
08623 if (!(mwi = ast_calloc_with_stringfields(1, struct sip_subscription_mwi, 256))) {
08624 return -1;
08625 }
08626
08627 ASTOBJ_INIT(mwi);
08628 ast_string_field_set(mwi, username, username);
08629 if (secret) {
08630 ast_string_field_set(mwi, secret, secret);
08631 }
08632 if (authuser) {
08633 ast_string_field_set(mwi, authuser, authuser);
08634 }
08635 ast_string_field_set(mwi, hostname, hostname);
08636 ast_string_field_set(mwi, mailbox, mailbox);
08637 mwi->resub = -1;
08638 mwi->portno = portnum;
08639 mwi->transport = transport;
08640
08641 ASTOBJ_CONTAINER_LINK(&submwil, mwi);
08642 ASTOBJ_UNREF(mwi, sip_subscribe_mwi_destroy);
08643
08644 return 0;
08645 }
08646
08647 static void mark_method_allowed(unsigned int *allowed_methods, enum sipmethod method)
08648 {
08649 (*allowed_methods) |= (1 << method);
08650 }
08651
08652 static void mark_method_unallowed(unsigned int *allowed_methods, enum sipmethod method)
08653 {
08654 (*allowed_methods) &= ~(1 << method);
08655 }
08656
08657
08658 static int is_method_allowed(unsigned int *allowed_methods, enum sipmethod method)
08659 {
08660 return ((*allowed_methods) >> method) & 1;
08661 }
08662
08663 static void mark_parsed_methods(unsigned int *methods, char *methods_str)
08664 {
08665 char *method;
08666 for (method = strsep(&methods_str, ","); !ast_strlen_zero(method); method = strsep(&methods_str, ",")) {
08667 int id = find_sip_method(ast_skip_blanks(method));
08668 if (id == SIP_UNKNOWN) {
08669 continue;
08670 }
08671 mark_method_allowed(methods, id);
08672 }
08673 }
08674
08675
08676
08677
08678
08679
08680
08681
08682
08683
08684
08685
08686
08687
08688
08689
08690 static unsigned int parse_allowed_methods(struct sip_request *req)
08691 {
08692 char *allow = ast_strdupa(get_header(req, "Allow"));
08693 unsigned int allowed_methods = SIP_UNKNOWN;
08694
08695 if (ast_strlen_zero(allow)) {
08696
08697
08698
08699
08700 char *contact = ast_strdupa(get_header(req, "Contact"));
08701 char *methods = strstr(contact, ";methods=");
08702
08703 if (ast_strlen_zero(methods)) {
08704
08705
08706
08707
08708
08709
08710
08711
08712
08713
08714
08715 return UINT_MAX;
08716 }
08717 allow = ast_strip_quoted(methods + 9, "\"", "\"");
08718 }
08719 mark_parsed_methods(&allowed_methods, allow);
08720 return allowed_methods;
08721 }
08722
08723
08724
08725
08726
08727
08728
08729
08730
08731
08732 static unsigned int set_pvt_allowed_methods(struct sip_pvt *pvt, struct sip_request *req)
08733 {
08734 pvt->allowed_methods = parse_allowed_methods(req);
08735
08736 if (ast_test_flag(&pvt->flags[1], SIP_PAGE2_RPID_UPDATE)) {
08737 mark_method_allowed(&pvt->allowed_methods, SIP_UPDATE);
08738 }
08739 pvt->allowed_methods &= ~(pvt->disallowed_methods);
08740
08741 return pvt->allowed_methods;
08742 }
08743
08744
08745
08746 static void lws2sws(struct ast_str *data)
08747 {
08748 char *msgbuf = data->str;
08749 int len = ast_str_strlen(data);
08750 int h = 0, t = 0;
08751 int lws = 0;
08752
08753 for (; h < len;) {
08754
08755 if (msgbuf[h] == '\r') {
08756 h++;
08757 continue;
08758 }
08759
08760 if (msgbuf[h] == '\n') {
08761
08762 if (h + 1 == len)
08763 break;
08764
08765 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
08766
08767 h++;
08768 continue;
08769 }
08770
08771 msgbuf[t++] = msgbuf[h++];
08772 lws = 0;
08773 continue;
08774 }
08775 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
08776 if (lws) {
08777 h++;
08778 continue;
08779 }
08780 msgbuf[t++] = msgbuf[h++];
08781 lws = 1;
08782 continue;
08783 }
08784 msgbuf[t++] = msgbuf[h++];
08785 if (lws)
08786 lws = 0;
08787 }
08788 msgbuf[t] = '\0';
08789 data->used = t;
08790 }
08791
08792
08793
08794
08795 static int parse_request(struct sip_request *req)
08796 {
08797 char *c = req->data->str;
08798 ptrdiff_t *dst = req->header;
08799 int i = 0, lim = SIP_MAX_HEADERS - 1;
08800 unsigned int skipping_headers = 0;
08801 ptrdiff_t current_header_offset = 0;
08802 char *previous_header = "";
08803
08804 req->header[0] = 0;
08805 req->headers = -1;
08806 for (; *c; c++) {
08807 if (*c == '\r') {
08808 *c = '\0';
08809 } else if (*c == '\n') {
08810 *c = '\0';
08811 current_header_offset = (c + 1) - ast_str_buffer(req->data);
08812 previous_header = ast_str_buffer(req->data) + dst[i];
08813 if (skipping_headers) {
08814
08815
08816
08817 if (ast_strlen_zero(previous_header)) {
08818 skipping_headers = 0;
08819 }
08820 dst[i] = current_header_offset;
08821 continue;
08822 }
08823 if (sipdebug) {
08824 ast_debug(4, "%7s %2d [%3d]: %s\n",
08825 req->headers < 0 ? "Header" : "Body",
08826 i, (int) strlen(previous_header), previous_header);
08827 }
08828 if (ast_strlen_zero(previous_header) && req->headers < 0) {
08829 req->headers = i;
08830 dst = req->line;
08831 i = 0;
08832 lim = SIP_MAX_LINES - 1;
08833 } else {
08834 if (i++ == lim) {
08835
08836
08837
08838 if (req->headers != -1) {
08839 break;
08840 } else {
08841 req->headers = i;
08842 dst = req->line;
08843 i = 0;
08844 lim = SIP_MAX_LINES - 1;
08845 skipping_headers = 1;
08846 }
08847 }
08848 }
08849 dst[i] = current_header_offset;
08850 }
08851 }
08852
08853
08854
08855
08856
08857
08858 previous_header = ast_str_buffer(req->data) + dst[i];
08859 if ((i < lim) && !ast_strlen_zero(previous_header)) {
08860 if (sipdebug) {
08861 ast_debug(4, "%7s %2d [%3d]: %s\n",
08862 req->headers < 0 ? "Header" : "Body",
08863 i, (int) strlen(previous_header), previous_header );
08864 }
08865 i++;
08866 }
08867
08868
08869 if (req->headers >= 0) {
08870 req->lines = i;
08871 } else {
08872 req->headers = i;
08873 req->lines = 0;
08874
08875 req->line[0] = ast_str_strlen(req->data);
08876 }
08877
08878 if (*c) {
08879 ast_log(LOG_WARNING, "Too many lines, skipping <%s>\n", c);
08880 }
08881
08882
08883 return determine_firstline_parts(req);
08884 }
08885
08886
08887
08888
08889
08890
08891
08892
08893
08894 static int find_sdp(struct sip_request *req)
08895 {
08896 const char *content_type;
08897 const char *content_length;
08898 const char *search;
08899 char *boundary;
08900 unsigned int x;
08901 int boundaryisquoted = FALSE;
08902 int found_application_sdp = FALSE;
08903 int found_end_of_headers = FALSE;
08904
08905 content_length = get_header(req, "Content-Length");
08906
08907 if (!ast_strlen_zero(content_length)) {
08908 if (sscanf(content_length, "%30u", &x) != 1) {
08909 ast_log(LOG_WARNING, "Invalid Content-Length: %s\n", content_length);
08910 return 0;
08911 }
08912
08913
08914
08915 if (x == 0)
08916 return 0;
08917 }
08918
08919 content_type = get_header(req, "Content-Type");
08920
08921
08922 if (!strncasecmp(content_type, "application/sdp", 15)) {
08923 req->sdp_start = 0;
08924 req->sdp_count = req->lines;
08925 return req->lines ? 1 : 0;
08926 }
08927
08928
08929 if (strncasecmp(content_type, "multipart/mixed", 15))
08930 return 0;
08931
08932
08933 if ((search = strcasestr(content_type, ";boundary=")))
08934 search += 10;
08935 else if ((search = strcasestr(content_type, "; boundary=")))
08936 search += 11;
08937 else
08938 return 0;
08939
08940 if (ast_strlen_zero(search))
08941 return 0;
08942
08943
08944 if (*search == '\"') {
08945 search++;
08946 boundaryisquoted = TRUE;
08947 }
08948
08949
08950
08951 boundary = ast_strdupa(search - 2);
08952 boundary[0] = boundary[1] = '-';
08953
08954 if (boundaryisquoted)
08955 boundary[strlen(boundary) - 1] = '\0';
08956
08957
08958
08959
08960 for (x = 0; x < (req->lines); x++) {
08961 const char *line = REQ_OFFSET_TO_STR(req, line[x]);
08962 if (!strncasecmp(line, boundary, strlen(boundary))){
08963 if (found_application_sdp && found_end_of_headers) {
08964 req->sdp_count = (x - 1) - req->sdp_start;
08965 return 1;
08966 }
08967 found_application_sdp = FALSE;
08968 }
08969 if (!strcasecmp(line, "Content-Type: application/sdp"))
08970 found_application_sdp = TRUE;
08971
08972 if (ast_strlen_zero(line)) {
08973 if (found_application_sdp && !found_end_of_headers){
08974 req->sdp_start = x;
08975 found_end_of_headers = TRUE;
08976 }
08977 }
08978 }
08979 if (found_application_sdp && found_end_of_headers) {
08980 req->sdp_count = x - req->sdp_start;
08981 return TRUE;
08982 }
08983 return FALSE;
08984 }
08985
08986
08987 static void change_hold_state(struct sip_pvt *dialog, struct sip_request *req, int holdstate, int sendonly)
08988 {
08989 if (sip_cfg.notifyhold && (!holdstate || !ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD)))
08990 sip_peer_hold(dialog, holdstate);
08991 if (sip_cfg.callevents)
08992 manager_event(EVENT_FLAG_CALL, "Hold",
08993 "Status: %s\r\n"
08994 "Channel: %s\r\n"
08995 "Uniqueid: %s\r\n",
08996 holdstate ? "On" : "Off",
08997 dialog->owner->name,
08998 dialog->owner->uniqueid);
08999 append_history(dialog, holdstate ? "Hold" : "Unhold", "%s", ast_str_buffer(req->data));
09000 if (!holdstate) {
09001 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD);
09002 return;
09003 }
09004
09005
09006
09007 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD);
09008
09009 if (sendonly == 1)
09010 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
09011 else if (sendonly == 2)
09012 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
09013 else
09014 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
09015 return;
09016 }
09017
09018
09019 static int get_ip_and_port_from_sdp(struct sip_request *req, const enum media_type media, struct ast_sockaddr *addr)
09020 {
09021 const char *m;
09022 const char *c;
09023 int miterator = req->sdp_start;
09024 int citerator = req->sdp_start;
09025 unsigned int x = 0;
09026 unsigned int numberofports;
09027 int len;
09028 int af;
09029 char proto[4], host[258] = "";
09030
09031 c = get_sdp_iterate(&citerator, req, "c");
09032 if (sscanf(c, "IN %3s %256s", proto, host) != 2) {
09033 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
09034
09035 }
09036
09037 for (m = get_sdp_iterate(&miterator, req, "m"); !ast_strlen_zero(m); m = get_sdp_iterate(&miterator, req, "m")) {
09038 if ((media == SDP_AUDIO && ((sscanf(m, "audio %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
09039 (sscanf(m, "audio %30u RTP/AVP %n", &x, &len) == 1 && len > 0))) ||
09040 (media == SDP_VIDEO && ((sscanf(m, "video %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
09041 (sscanf(m, "video %30u RTP/AVP %n", &x, &len) == 1 && len > 0)))) {
09042
09043
09044
09045
09046 c = get_sdp_iterate(&citerator, req, "c");
09047 if (!ast_strlen_zero(c)) {
09048 sscanf(c, "IN %3s %256s", proto, host);
09049 }
09050 break;
09051 }
09052 }
09053
09054 if (!strcmp("IP4", proto)) {
09055 af = AF_INET;
09056 } else if (!strcmp("IP6", proto)) {
09057 af = AF_INET6;
09058 } else {
09059 ast_log(LOG_WARNING, "Unknown protocol '%s'.\n", proto);
09060 return -1;
09061 }
09062
09063 if (ast_strlen_zero(host) || x == 0) {
09064 ast_log(LOG_WARNING, "Failed to read an alternate host or port in SDP. Expect %s problems\n", media == SDP_AUDIO ? "audio" : "video");
09065 return -1;
09066 }
09067
09068 if (ast_sockaddr_resolve_first_af(addr, host, 0, af)) {
09069 ast_log(LOG_WARNING, "Could not look up IP address of alternate hostname. Expect %s problems\n", media == SDP_AUDIO? "audio" : "video");
09070 return -1;
09071 }
09072
09073 return 0;
09074 }
09075
09076
09077
09078
09079
09080
09081
09082 static int sockaddr_is_null_or_any(const struct ast_sockaddr *addr)
09083 {
09084 return ast_sockaddr_isnull(addr) || ast_sockaddr_is_any(addr);
09085 }
09086
09087
09088
09089
09090
09091
09092 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
09093 {
09094
09095 int start = req->sdp_start;
09096 int next = start;
09097 int iterator = start;
09098
09099
09100 char type = '\0';
09101 const char *value = NULL;
09102 const char *m = NULL;
09103 const char *nextm = NULL;
09104 int len = -1;
09105
09106
09107 struct ast_sockaddr sessionsa;
09108 struct ast_sockaddr audiosa;
09109 struct ast_sockaddr videosa;
09110 struct ast_sockaddr textsa;
09111 struct ast_sockaddr imagesa;
09112 struct ast_sockaddr *sa = NULL;
09113 struct ast_sockaddr *vsa = NULL;
09114 struct ast_sockaddr *tsa = NULL;
09115 struct ast_sockaddr *isa = NULL;
09116 int portno = -1;
09117 int vportno = -1;
09118 int tportno = -1;
09119 int udptlportno = -1;
09120
09121
09122 format_t peercapability = 0, vpeercapability = 0, tpeercapability = 0;
09123 int peernoncodeccapability = 0, vpeernoncodeccapability = 0, tpeernoncodeccapability = 0;
09124
09125 struct ast_rtp_codecs newaudiortp, newvideortp, newtextrtp;
09126 format_t newjointcapability;
09127 format_t newpeercapability;
09128 int newnoncodeccapability;
09129
09130 const char *codecs;
09131 unsigned int codec;
09132
09133
09134 int secure_audio = FALSE;
09135 int secure_video = FALSE;
09136
09137
09138 int sendonly = -1;
09139 unsigned int numberofports;
09140 int numberofmediastreams = 0;
09141 int last_rtpmap_codec = 0;
09142 int red_data_pt[10];
09143 int red_num_gen = 0;
09144 char red_fmtp[100] = "empty";
09145 int debug = sip_debug_test_pvt(p);
09146
09147
09148 char buf[SIPBUFSIZE];
09149
09150
09151
09152 if (!p->rtp) {
09153 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
09154 return -1;
09155 }
09156
09157
09158 ast_rtp_codecs_payloads_clear(&newaudiortp, NULL);
09159 ast_rtp_codecs_payloads_clear(&newvideortp, NULL);
09160 ast_rtp_codecs_payloads_clear(&newtextrtp, NULL);
09161
09162
09163 p->lastrtprx = p->lastrtptx = time(NULL);
09164
09165 memset(p->offered_media, 0, sizeof(p->offered_media));
09166
09167
09168 p->novideo = TRUE;
09169 p->notext = TRUE;
09170
09171
09172 nextm = get_sdp_iterate(&next, req, "m");
09173 if (ast_strlen_zero(nextm)) {
09174 ast_log(LOG_WARNING, "Insufficient information for SDP (m= not found)\n");
09175 return -1;
09176 }
09177
09178
09179 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
09180 int processed = FALSE;
09181 switch (type) {
09182 case 'o':
09183
09184
09185
09186 if (!process_sdp_o(value, p)) {
09187 return (p->session_modify == FALSE) ? 0 : -1;
09188 }
09189 processed = TRUE;
09190 break;
09191 case 'c':
09192 if (process_sdp_c(value, &sessionsa)) {
09193 processed = TRUE;
09194 sa = &sessionsa;
09195 vsa = sa;
09196 tsa = sa;
09197 isa = sa;
09198 }
09199 break;
09200 case 'a':
09201 if (process_sdp_a_sendonly(value, &sendonly)) {
09202 processed = TRUE;
09203 }
09204 else if (process_sdp_a_audio(value, p, &newaudiortp, &last_rtpmap_codec))
09205 processed = TRUE;
09206 else if (process_sdp_a_video(value, p, &newvideortp, &last_rtpmap_codec))
09207 processed = TRUE;
09208 else if (process_sdp_a_text(value, p, &newtextrtp, red_fmtp, &red_num_gen, red_data_pt, &last_rtpmap_codec))
09209 processed = TRUE;
09210 else if (process_sdp_a_image(value, p))
09211 processed = TRUE;
09212 break;
09213 }
09214
09215 ast_debug(3, "Processing session-level SDP %c=%s... %s\n", type, value, (processed == TRUE)? "OK." : "UNSUPPORTED OR FAILED.");
09216 }
09217
09218
09219 while (!ast_strlen_zero(nextm)) {
09220 int audio = FALSE;
09221 int video = FALSE;
09222 int image = FALSE;
09223 int text = FALSE;
09224 int processed_crypto = FALSE;
09225 char protocol[18] = {0,};
09226 unsigned int x;
09227
09228 numberofports = 0;
09229 len = -1;
09230 start = next;
09231 m = nextm;
09232 iterator = next;
09233 nextm = get_sdp_iterate(&next, req, "m");
09234
09235
09236 if (strncmp(m, "audio ", 6) == 0) {
09237 if ((sscanf(m, "audio %30u/%30u RTP/%4s %n", &x, &numberofports, protocol, &len) == 3 && len > 0) ||
09238 (sscanf(m, "audio %30u RTP/%4s %n", &x, protocol, &len) == 2 && len > 0)) {
09239 if (x == 0) {
09240 ast_log(LOG_WARNING, "Ignoring audio media offer because port number is zero\n");
09241 continue;
09242 }
09243
09244
09245 if (numberofports > 1) {
09246 ast_log(LOG_WARNING, "%u ports offered for audio media, not supported by Asterisk. Will try anyway...\n", numberofports);
09247 }
09248
09249 if (!strcmp(protocol, "SAVP")) {
09250 secure_audio = 1;
09251 } else if (strcmp(protocol, "AVP")) {
09252 ast_log(LOG_WARNING, "Unknown RTP profile in audio offer: %s\n", m);
09253 continue;
09254 }
09255
09256 if (p->offered_media[SDP_AUDIO].order_offered) {
09257 ast_log(LOG_WARNING, "Rejecting non-primary audio stream: %s\n", m);
09258 return -1;
09259 }
09260
09261 audio = TRUE;
09262 p->offered_media[SDP_AUDIO].order_offered = ++numberofmediastreams;
09263 portno = x;
09264
09265
09266 codecs = m + len;
09267 ast_copy_string(p->offered_media[SDP_AUDIO].codecs, codecs, sizeof(p->offered_media[SDP_AUDIO].codecs));
09268 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
09269 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
09270 ast_log(LOG_WARNING, "Invalid syntax in RTP audio format list: %s\n", codecs);
09271 return -1;
09272 }
09273 if (debug) {
09274 ast_verbose("Found RTP audio format %u\n", codec);
09275 }
09276
09277 ast_rtp_codecs_payloads_set_m_type(&newaudiortp, NULL, codec);
09278 }
09279 } else {
09280 ast_log(LOG_WARNING, "Rejecting audio media offer due to invalid or unsupported syntax: %s\n", m);
09281 return -1;
09282 }
09283 }
09284
09285 else if (strncmp(m, "video ", 6) == 0) {
09286 if ((sscanf(m, "video %30u/%30u RTP/%4s %n", &x, &numberofports, protocol, &len) == 3 && len > 0) ||
09287 (sscanf(m, "video %30u RTP/%4s %n", &x, protocol, &len) == 2 && len > 0)) {
09288 if (x == 0) {
09289 ast_log(LOG_WARNING, "Ignoring video media offer because port number is zero\n");
09290 continue;
09291 }
09292
09293
09294 if (numberofports > 1) {
09295 ast_log(LOG_WARNING, "%u ports offered for video media, not supported by Asterisk. Will try anyway...\n", numberofports);
09296 }
09297
09298 if (!strcmp(protocol, "SAVP")) {
09299 secure_video = 1;
09300 } else if (strcmp(protocol, "AVP")) {
09301 ast_log(LOG_WARNING, "Unknown RTP profile in video offer: %s\n", m);
09302 continue;
09303 }
09304
09305 if (p->offered_media[SDP_VIDEO].order_offered) {
09306 ast_log(LOG_WARNING, "Rejecting non-primary video stream: %s\n", m);
09307 return -1;
09308 }
09309
09310 video = TRUE;
09311 p->novideo = FALSE;
09312 p->offered_media[SDP_VIDEO].order_offered = ++numberofmediastreams;
09313 vportno = x;
09314
09315
09316 codecs = m + len;
09317 ast_copy_string(p->offered_media[SDP_VIDEO].codecs, codecs, sizeof(p->offered_media[SDP_VIDEO].codecs));
09318 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
09319 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
09320 ast_log(LOG_WARNING, "Invalid syntax in RTP video format list: %s\n", codecs);
09321 return -1;
09322 }
09323 if (debug) {
09324 ast_verbose("Found RTP video format %u\n", codec);
09325 }
09326 ast_rtp_codecs_payloads_set_m_type(&newvideortp, NULL, codec);
09327 }
09328 } else {
09329 ast_log(LOG_WARNING, "Rejecting video media offer due to invalid or unsupported syntax: %s\n", m);
09330 return -1;
09331 }
09332 }
09333
09334 else if (strncmp(m, "text ", 5) == 0) {
09335 if ((sscanf(m, "text %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
09336 (sscanf(m, "text %30u RTP/AVP %n", &x, &len) == 1 && len > 0)) {
09337 if (x == 0) {
09338 ast_log(LOG_WARNING, "Ignoring text media offer because port number is zero\n");
09339 continue;
09340 }
09341
09342
09343 if (numberofports > 1) {
09344 ast_log(LOG_WARNING, "%u ports offered for text media, not supported by Asterisk. Will try anyway...\n", numberofports);
09345 }
09346
09347 if (p->offered_media[SDP_TEXT].order_offered) {
09348 ast_log(LOG_WARNING, "Rejecting non-primary text stream: %s\n", m);
09349 return -1;
09350 }
09351
09352 text = TRUE;
09353 p->notext = FALSE;
09354 p->offered_media[SDP_TEXT].order_offered = ++numberofmediastreams;
09355 tportno = x;
09356
09357
09358 codecs = m + len;
09359 ast_copy_string(p->offered_media[SDP_TEXT].codecs, codecs, sizeof(p->offered_media[SDP_TEXT].codecs));
09360 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
09361 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
09362 ast_log(LOG_WARNING, "Invalid syntax in RTP video format list: %s\n", codecs);
09363 return -1;
09364 }
09365 if (debug) {
09366 ast_verbose("Found RTP text format %u\n", codec);
09367 }
09368 ast_rtp_codecs_payloads_set_m_type(&newtextrtp, NULL, codec);
09369 }
09370 } else {
09371 ast_log(LOG_WARNING, "Rejecting text media offer due to invalid or unsupported syntax: %s\n", m);
09372 return -1;
09373 }
09374 }
09375
09376 else if (strncmp(m, "image ", 6) == 0) {
09377 if (((sscanf(m, "image %30u udptl t38%n", &x, &len) == 1 && len > 0) ||
09378 (sscanf(m, "image %30u UDPTL t38%n", &x, &len) == 1 && len > 0))) {
09379 if (x == 0) {
09380 ast_log(LOG_WARNING, "Ignoring image media offer because port number is zero\n");
09381 continue;
09382 }
09383
09384 if (initialize_udptl(p)) {
09385 ast_log(LOG_WARNING, "Rejecting offer with image stream due to UDPTL initialization failure\n");
09386 return -1;
09387 }
09388
09389 if (p->offered_media[SDP_IMAGE].order_offered) {
09390 ast_log(LOG_WARNING, "Rejecting non-primary image stream: %s\n", m);
09391 return -1;
09392 }
09393
09394 image = TRUE;
09395 if (debug) {
09396 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
09397 }
09398
09399 p->offered_media[SDP_IMAGE].order_offered = ++numberofmediastreams;
09400 udptlportno = x;
09401
09402 if (p->t38.state != T38_ENABLED) {
09403 memset(&p->t38.their_parms, 0, sizeof(p->t38.their_parms));
09404
09405
09406
09407 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
09408 }
09409 } else if (sscanf(m, "image %30u %17s t38%n", &x, protocol, &len) == 2 && len > 0) {
09410 ast_log(LOG_WARNING, "Declining image stream due to unsupported transport: %s\n", m);
09411 continue;
09412 } else {
09413 ast_log(LOG_WARNING, "Rejecting image media offer due to invalid or unsupported syntax: %s\n", m);
09414 return -1;
09415 }
09416 } else {
09417 ast_log(LOG_WARNING, "Unsupported top-level media type in offer: %s\n", m);
09418 continue;
09419 }
09420
09421
09422 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
09423 int processed = FALSE;
09424
09425 switch (type) {
09426 case 'c':
09427 if (audio) {
09428 if (process_sdp_c(value, &audiosa)) {
09429 processed = TRUE;
09430 sa = &audiosa;
09431 }
09432 } else if (video) {
09433 if (process_sdp_c(value, &videosa)) {
09434 processed = TRUE;
09435 vsa = &videosa;
09436 }
09437 } else if (text) {
09438 if (process_sdp_c(value, &textsa)) {
09439 processed = TRUE;
09440 tsa = &textsa;
09441 }
09442 } else if (image) {
09443 if (process_sdp_c(value, &imagesa)) {
09444 processed = TRUE;
09445 isa = &imagesa;
09446 }
09447 }
09448 break;
09449 case 'a':
09450
09451 if (audio) {
09452 if (process_sdp_a_sendonly(value, &sendonly)) {
09453 processed = TRUE;
09454 } else if (!processed_crypto && process_crypto(p, p->rtp, &p->srtp, value)) {
09455 processed_crypto = TRUE;
09456 processed = TRUE;
09457 } else if (process_sdp_a_audio(value, p, &newaudiortp, &last_rtpmap_codec)) {
09458 processed = TRUE;
09459 }
09460 }
09461
09462 else if (video) {
09463 if (!processed_crypto && process_crypto(p, p->vrtp, &p->vsrtp, value)) {
09464 processed_crypto = TRUE;
09465 processed = TRUE;
09466 } else if (process_sdp_a_video(value, p, &newvideortp, &last_rtpmap_codec)) {
09467 processed = TRUE;
09468 }
09469 }
09470
09471 else if (text) {
09472 if (process_sdp_a_text(value, p, &newtextrtp, red_fmtp, &red_num_gen, red_data_pt, &last_rtpmap_codec)) {
09473 processed = TRUE;
09474 } else if (!processed_crypto && process_crypto(p, p->trtp, &p->tsrtp, value)) {
09475 processed_crypto = TRUE;
09476 processed = TRUE;
09477 }
09478 }
09479
09480 else if (image) {
09481 if (process_sdp_a_image(value, p))
09482 processed = TRUE;
09483 }
09484 break;
09485 }
09486
09487 ast_debug(3, "Processing media-level (%s) SDP %c=%s... %s\n",
09488 (audio == TRUE)? "audio" : (video == TRUE)? "video" : (text == TRUE)? "text" : "image",
09489 type, value,
09490 (processed == TRUE)? "OK." : "UNSUPPORTED OR FAILED.");
09491 }
09492
09493
09494 if (audio && secure_audio && !processed_crypto) {
09495 ast_log(LOG_WARNING, "Rejecting secure audio stream without encryption details: %s\n", m);
09496 return -1;
09497 } else if (video && secure_video && !processed_crypto) {
09498 ast_log(LOG_WARNING, "Rejecting secure video stream without encryption details: %s\n", m);
09499 return -1;
09500 }
09501 }
09502
09503
09504 if (!sa && !vsa && !tsa && !isa) {
09505 ast_log(LOG_WARNING, "Insufficient information in SDP (c=)...\n");
09506 return -1;
09507 }
09508
09509 if ((portno == -1) &&
09510 (vportno == -1) &&
09511 (tportno == -1) &&
09512 (udptlportno == -1)) {
09513 ast_log(LOG_WARNING, "Failing due to no acceptable offer found\n");
09514 return -1;
09515 }
09516
09517 if (secure_audio && !(p->srtp && (ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)))) {
09518 ast_log(LOG_WARNING, "Can't provide secure audio requested in SDP offer\n");
09519 return -1;
09520 }
09521
09522 if (!secure_audio && p->srtp) {
09523 ast_log(LOG_WARNING, "We are requesting SRTP for audio, but they responded without it!\n");
09524 return -1;
09525 }
09526
09527 if (secure_video && !(p->vsrtp && (ast_test_flag(p->vsrtp, SRTP_CRYPTO_OFFER_OK)))) {
09528 ast_log(LOG_WARNING, "Can't provide secure video requested in SDP offer\n");
09529 return -1;
09530 }
09531
09532 if (!p->novideo && !secure_video && p->vsrtp) {
09533 ast_log(LOG_WARNING, "We are requesting SRTP for video, but they responded without it!\n");
09534 return -1;
09535 }
09536
09537 if (!(secure_audio || secure_video) && ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP)) {
09538 ast_log(LOG_WARNING, "Matched device setup to use SRTP, but request was not!\n");
09539 return -1;
09540 }
09541
09542 if (udptlportno == -1) {
09543 change_t38_state(p, T38_DISABLED);
09544 }
09545
09546
09547 ast_rtp_codecs_payload_formats(&newaudiortp, &peercapability, &peernoncodeccapability);
09548 ast_rtp_codecs_payload_formats(&newvideortp, &vpeercapability, &vpeernoncodeccapability);
09549 ast_rtp_codecs_payload_formats(&newtextrtp, &tpeercapability, &tpeernoncodeccapability);
09550
09551 newjointcapability = p->capability & (peercapability | vpeercapability | tpeercapability);
09552 newpeercapability = (peercapability | vpeercapability | tpeercapability);
09553 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
09554
09555 if (debug) {
09556
09557 char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE], s5[SIPBUFSIZE];
09558
09559 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s/text=%s, combined - %s\n",
09560 ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
09561 ast_getformatname_multiple(s2, SIPBUFSIZE, peercapability),
09562 ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
09563 ast_getformatname_multiple(s4, SIPBUFSIZE, tpeercapability),
09564 ast_getformatname_multiple(s5, SIPBUFSIZE, newjointcapability));
09565 }
09566 if (debug) {
09567 struct ast_str *s1 = ast_str_alloca(SIPBUFSIZE);
09568 struct ast_str *s2 = ast_str_alloca(SIPBUFSIZE);
09569 struct ast_str *s3 = ast_str_alloca(SIPBUFSIZE);
09570
09571 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
09572 ast_rtp_lookup_mime_multiple2(s1, p->noncodeccapability, 0, 0),
09573 ast_rtp_lookup_mime_multiple2(s2, peernoncodeccapability, 0, 0),
09574 ast_rtp_lookup_mime_multiple2(s3, newnoncodeccapability, 0, 0));
09575 }
09576 if (!newjointcapability && udptlportno == -1) {
09577 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
09578
09579 return -1;
09580 }
09581
09582 if (portno != -1 || vportno != -1 || tportno != -1) {
09583
09584
09585 p->jointcapability = newjointcapability;
09586 p->peercapability = newpeercapability;
09587 p->jointnoncodeccapability = newnoncodeccapability;
09588
09589
09590 if (ast_test_flag(&p->flags[1], SIP_PAGE2_PREFERRED_CODEC)) {
09591 p->jointcapability = ast_codec_choose(&p->prefs, p->jointcapability, 1);
09592 }
09593 }
09594
09595
09596 if (p->rtp) {
09597 if (sa && portno > 0) {
09598 ast_sockaddr_set_port(sa, portno);
09599 ast_rtp_instance_set_remote_address(p->rtp, sa);
09600 if (debug) {
09601 ast_verbose("Peer audio RTP is at port %s\n",
09602 ast_sockaddr_stringify(sa));
09603 }
09604
09605 ast_rtp_codecs_payloads_copy(&newaudiortp, ast_rtp_instance_get_codecs(p->rtp), p->rtp);
09606
09607
09608 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 1);
09609
09610 if (p->owner) {
09611 ast_channel_set_fd(p->owner, 1, ast_rtp_instance_fd(p->rtp, 1));
09612 }
09613
09614 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
09615 ast_clear_flag(&p->flags[0], SIP_DTMF);
09616 if (newnoncodeccapability & AST_RTP_DTMF) {
09617
09618 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
09619
09620 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, 1);
09621 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
09622 } else {
09623 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
09624 }
09625 }
09626 } else if (udptlportno > 0) {
09627 if (debug)
09628 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session.\n");
09629
09630 if (p->owner) {
09631 ast_channel_set_fd(p->owner, 1, -1);
09632 }
09633
09634 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 0);
09635 } else {
09636 ast_rtp_instance_stop(p->rtp);
09637 if (debug)
09638 ast_verbose("Peer doesn't provide audio\n");
09639 }
09640 }
09641
09642
09643 if (p->vrtp) {
09644 if (vsa && vportno > 0) {
09645 ast_sockaddr_set_port(vsa, vportno);
09646 ast_rtp_instance_set_remote_address(p->vrtp, vsa);
09647 if (debug) {
09648 ast_verbose("Peer video RTP is at port %s\n",
09649 ast_sockaddr_stringify(vsa));
09650 }
09651 ast_rtp_codecs_payloads_copy(&newvideortp, ast_rtp_instance_get_codecs(p->vrtp), p->vrtp);
09652 } else {
09653 ast_rtp_instance_stop(p->vrtp);
09654 if (debug)
09655 ast_verbose("Peer doesn't provide video\n");
09656 }
09657 }
09658
09659
09660 if (p->trtp) {
09661 if (tsa && tportno > 0) {
09662 ast_sockaddr_set_port(tsa, tportno);
09663 ast_rtp_instance_set_remote_address(p->trtp, tsa);
09664 if (debug) {
09665 ast_verbose("Peer T.140 RTP is at port %s\n",
09666 ast_sockaddr_stringify(tsa));
09667 }
09668 if ((p->jointcapability & AST_FORMAT_T140RED)) {
09669 p->red = 1;
09670 ast_rtp_red_init(p->trtp, 300, red_data_pt, 2);
09671 } else {
09672 p->red = 0;
09673 }
09674 ast_rtp_codecs_payloads_copy(&newtextrtp, ast_rtp_instance_get_codecs(p->trtp), p->trtp);
09675 } else {
09676 ast_rtp_instance_stop(p->trtp);
09677 if (debug)
09678 ast_verbose("Peer doesn't provide T.140\n");
09679 }
09680 }
09681
09682
09683 if (p->udptl) {
09684 if (isa && udptlportno > 0) {
09685 if (ast_test_flag(&p->flags[1], SIP_PAGE2_SYMMETRICRTP) && ast_test_flag(&p->flags[1], SIP_PAGE2_UDPTL_DESTINATION)) {
09686 ast_rtp_instance_get_remote_address(p->rtp, isa);
09687 if (!ast_sockaddr_isnull(isa) && debug) {
09688 ast_debug(1, "Peer T.38 UDPTL is set behind NAT and with destination, destination address now %s\n", ast_sockaddr_stringify(isa));
09689 }
09690 }
09691 ast_sockaddr_set_port(isa, udptlportno);
09692 ast_udptl_set_peer(p->udptl, isa);
09693 if (debug)
09694 ast_debug(1,"Peer T.38 UDPTL is at port %s\n", ast_sockaddr_stringify(isa));
09695
09696
09697 if (!ast_udptl_get_far_max_datagram(p->udptl)) {
09698
09699 ast_udptl_set_far_max_datagram(p->udptl, 0);
09700 }
09701
09702
09703 if ((t38action == SDP_T38_ACCEPT) &&
09704 (p->t38.state == T38_LOCAL_REINVITE)) {
09705 change_t38_state(p, T38_ENABLED);
09706 } else if ((t38action == SDP_T38_INITIATE) &&
09707 p->owner && p->lastinvite) {
09708 change_t38_state(p, T38_PEER_REINVITE);
09709
09710 if (ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_T38)) {
09711 ast_channel_lock(p->owner);
09712 if (strcmp(p->owner->exten, "fax")) {
09713 const char *target_context = S_OR(p->owner->macrocontext, p->owner->context);
09714 ast_channel_unlock(p->owner);
09715 if (ast_exists_extension(p->owner, target_context, "fax", 1,
09716 S_COR(p->owner->caller.id.number.valid, p->owner->caller.id.number.str, NULL))) {
09717 ast_verbose(VERBOSE_PREFIX_2 "Redirecting '%s' to fax extension due to peer T.38 re-INVITE\n", p->owner->name);
09718 pbx_builtin_setvar_helper(p->owner, "FAXEXTEN", p->owner->exten);
09719 if (ast_async_goto(p->owner, target_context, "fax", 1)) {
09720 ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", p->owner->name, target_context);
09721 }
09722 } else {
09723 ast_log(LOG_NOTICE, "T.38 re-INVITE detected but no fax extension\n");
09724 }
09725 } else {
09726 ast_channel_unlock(p->owner);
09727 }
09728 }
09729 }
09730 } else {
09731 change_t38_state(p, T38_DISABLED);
09732 ast_udptl_stop(p->udptl);
09733 if (debug)
09734 ast_debug(1, "Peer doesn't provide T.38 UDPTL\n");
09735 }
09736 }
09737
09738 if ((portno == -1) && (p->t38.state != T38_DISABLED)) {
09739 ast_debug(3, "Have T.38 but no audio, accepting offer anyway\n");
09740 return 0;
09741 }
09742
09743
09744 ast_debug(2, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
09745
09746 if (!p->owner)
09747 return 0;
09748
09749 ast_debug(4, "We have an owner, now see if we need to change this call\n");
09750
09751 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
09752 if (debug) {
09753 char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
09754 ast_debug(1, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
09755 ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
09756 ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
09757 }
09758 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability) | (p->capability & tpeercapability);
09759 ast_set_read_format(p->owner, p->owner->readformat);
09760 ast_set_write_format(p->owner, p->owner->writeformat);
09761 }
09762
09763 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && (!ast_sockaddr_isnull(sa) || !ast_sockaddr_isnull(vsa) || !ast_sockaddr_isnull(tsa) || !ast_sockaddr_isnull(isa)) && (!sendonly || sendonly == -1)) {
09764 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
09765
09766 ast_queue_frame(p->owner, &ast_null_frame);
09767 change_hold_state(p, req, FALSE, sendonly);
09768 } else if ((sockaddr_is_null_or_any(sa) && sockaddr_is_null_or_any(vsa) && sockaddr_is_null_or_any(tsa) && sockaddr_is_null_or_any(isa)) || (sendonly && sendonly != -1)) {
09769 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
09770 S_OR(p->mohsuggest, NULL),
09771 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
09772 if (sendonly)
09773 ast_rtp_instance_stop(p->rtp);
09774
09775
09776 ast_queue_frame(p->owner, &ast_null_frame);
09777 change_hold_state(p, req, TRUE, sendonly);
09778 }
09779
09780 return 0;
09781 }
09782
09783 static int process_sdp_o(const char *o, struct sip_pvt *p)
09784 {
09785 char *o_copy;
09786 char *token;
09787 int64_t rua_version;
09788
09789
09790
09791
09792
09793
09794
09795
09796
09797
09798 p->session_modify = TRUE;
09799
09800 if (ast_strlen_zero(o)) {
09801 ast_log(LOG_WARNING, "SDP syntax error. SDP without an o= line\n");
09802 return FALSE;
09803 }
09804
09805 o_copy = ast_strdupa(o);
09806 token = strsep(&o_copy, " ");
09807 if (!o_copy) {
09808 ast_log(LOG_WARNING, "SDP syntax error in o= line username\n");
09809 return FALSE;
09810 }
09811 token = strsep(&o_copy, " ");
09812 if (!o_copy) {
09813 ast_log(LOG_WARNING, "SDP syntax error in o= line session-id\n");
09814 return FALSE;
09815 }
09816 token = strsep(&o_copy, " ");
09817 if (!o_copy) {
09818 ast_log(LOG_WARNING, "SDP syntax error in o= line\n");
09819 return FALSE;
09820 }
09821 if (!sscanf(token, "%30" SCNd64, &rua_version)) {
09822 ast_log(LOG_WARNING, "SDP syntax error in o= line version\n");
09823 return FALSE;
09824 }
09825
09826
09827
09828
09829
09830
09831
09832
09833
09834
09835
09836
09837
09838
09839
09840
09841
09842
09843
09844 if (ast_test_flag(&p->flags[1], SIP_PAGE2_IGNORESDPVERSION) ||
09845 (p->sessionversion_remote < 0) ||
09846 (p->sessionversion_remote < rua_version)) {
09847 p->sessionversion_remote = rua_version;
09848 } else {
09849 if (p->t38.state == T38_LOCAL_REINVITE) {
09850 p->sessionversion_remote = rua_version;
09851 ast_log(LOG_WARNING, "Call %s responded to our T.38 reinvite without changing SDP version; 'ignoresdpversion' should be set for this peer.\n", p->callid);
09852 } else {
09853 p->session_modify = FALSE;
09854 ast_debug(2, "Call %s responded to our reinvite without changing SDP version; ignoring SDP.\n", p->callid);
09855 return FALSE;
09856 }
09857 }
09858
09859 return TRUE;
09860 }
09861
09862 static int process_sdp_c(const char *c, struct ast_sockaddr *addr)
09863 {
09864 char proto[4], host[258];
09865 int af;
09866
09867
09868 if (sscanf(c, "IN %3s %255s", proto, host) == 2) {
09869 if (!strcmp("IP4", proto)) {
09870 af = AF_INET;
09871 } else if (!strcmp("IP6", proto)) {
09872 af = AF_INET6;
09873 } else {
09874 ast_log(LOG_WARNING, "Unknown protocol '%s'.\n", proto);
09875 return FALSE;
09876 }
09877 if (ast_sockaddr_resolve_first_af(addr, host, 0, af)) {
09878 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in c= line, '%s'\n", c);
09879 return FALSE;
09880 }
09881 return TRUE;
09882 } else {
09883 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
09884 return FALSE;
09885 }
09886 return FALSE;
09887 }
09888
09889 static int process_sdp_a_sendonly(const char *a, int *sendonly)
09890 {
09891 int found = FALSE;
09892
09893 if (!strcasecmp(a, "sendonly")) {
09894 if (*sendonly == -1)
09895 *sendonly = 1;
09896 found = TRUE;
09897 } else if (!strcasecmp(a, "inactive")) {
09898 if (*sendonly == -1)
09899 *sendonly = 2;
09900 found = TRUE;
09901 } else if (!strcasecmp(a, "sendrecv")) {
09902 if (*sendonly == -1)
09903 *sendonly = 0;
09904 found = TRUE;
09905 }
09906 return found;
09907 }
09908
09909 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec)
09910 {
09911 int found = FALSE;
09912 unsigned int codec;
09913 char mimeSubtype[128];
09914 char fmtp_string[64];
09915 unsigned int sample_rate;
09916 int debug = sip_debug_test_pvt(p);
09917
09918 if (!strncasecmp(a, "ptime", 5)) {
09919 char *tmp = strrchr(a, ':');
09920 long int framing = 0;
09921 if (tmp) {
09922 tmp++;
09923 framing = strtol(tmp, NULL, 10);
09924 if (framing == LONG_MIN || framing == LONG_MAX) {
09925 framing = 0;
09926 ast_debug(1, "Can't read framing from SDP: %s\n", a);
09927 }
09928 }
09929 if (framing && p->autoframing) {
09930 struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
09931 int codec_n;
09932 for (codec_n = 0; codec_n < AST_RTP_MAX_PT; codec_n++) {
09933 struct ast_rtp_payload_type format = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(p->rtp), codec_n);
09934 if (!format.asterisk_format || !format.code)
09935 continue;
09936 ast_debug(1, "Setting framing for %s to %ld\n", ast_getformatname(format.code), framing);
09937 ast_codec_pref_setsize(pref, format.code, framing);
09938 }
09939 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, pref);
09940 }
09941 found = TRUE;
09942 } else if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
09943
09944 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
09945 if (!(ast_rtp_codecs_payloads_set_rtpmap_type_rate(newaudiortp, NULL, codec, "audio", mimeSubtype,
09946 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0, sample_rate))) {
09947 if (debug)
09948 ast_verbose("Found audio description format %s for ID %u\n", mimeSubtype, codec);
09949
09950 (*last_rtpmap_codec)++;
09951 found = TRUE;
09952 } else {
09953 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09954 if (debug)
09955 ast_verbose("Found unknown media description format %s for ID %u\n", mimeSubtype, codec);
09956 }
09957 } else {
09958 if (debug)
09959 ast_verbose("Discarded description format %s for ID %u\n", mimeSubtype, codec);
09960 }
09961 } else if (sscanf(a, "fmtp: %30u %63[^\t\n]", &codec, fmtp_string) == 2) {
09962 struct ast_rtp_payload_type payload;
09963
09964 payload = ast_rtp_codecs_payload_lookup(newaudiortp, codec);
09965 if (payload.code && payload.asterisk_format) {
09966 unsigned int bit_rate;
09967
09968 switch (payload.code) {
09969 case AST_FORMAT_SIREN7:
09970 if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
09971 if (bit_rate != 32000) {
09972 ast_log(LOG_WARNING, "Got Siren7 offer at %u bps, but only 32000 bps supported; ignoring.\n", bit_rate);
09973 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09974 } else {
09975 found = TRUE;
09976 }
09977 }
09978 break;
09979 case AST_FORMAT_SIREN14:
09980 if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
09981 if (bit_rate != 48000) {
09982 ast_log(LOG_WARNING, "Got Siren14 offer at %u bps, but only 48000 bps supported; ignoring.\n", bit_rate);
09983 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09984 } else {
09985 found = TRUE;
09986 }
09987 }
09988 break;
09989 case AST_FORMAT_G719:
09990 if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
09991 if (bit_rate != 64000) {
09992 ast_log(LOG_WARNING, "Got G.719 offer at %u bps, but only 64000 bps supported; ignoring.\n", bit_rate);
09993 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09994 } else {
09995 found = TRUE;
09996 }
09997 }
09998 }
09999 }
10000 }
10001
10002 return found;
10003 }
10004
10005 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newvideortp, int *last_rtpmap_codec)
10006 {
10007 int found = FALSE;
10008 unsigned int codec;
10009 char mimeSubtype[128];
10010 unsigned int sample_rate;
10011 int debug = sip_debug_test_pvt(p);
10012
10013 if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
10014
10015 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
10016
10017 if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3)) {
10018 if (!(ast_rtp_codecs_payloads_set_rtpmap_type_rate(newvideortp, NULL, codec, "video", mimeSubtype, 0, sample_rate))) {
10019 if (debug)
10020 ast_verbose("Found video description format %s for ID %u\n", mimeSubtype, codec);
10021
10022 (*last_rtpmap_codec)++;
10023 found = TRUE;
10024 } else {
10025 ast_rtp_codecs_payloads_unset(newvideortp, NULL, codec);
10026 if (debug)
10027 ast_verbose("Found unknown media description format %s for ID %u\n", mimeSubtype, codec);
10028 }
10029 }
10030 } else {
10031 if (debug)
10032 ast_verbose("Discarded description format %s for ID %u\n", mimeSubtype, codec);
10033 }
10034 }
10035
10036 return found;
10037 }
10038
10039 static int process_sdp_a_text(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newtextrtp, char *red_fmtp, int *red_num_gen, int *red_data_pt, int *last_rtpmap_codec)
10040 {
10041 int found = FALSE;
10042 unsigned int codec;
10043 char mimeSubtype[128];
10044 unsigned int sample_rate;
10045 char *red_cp;
10046 int debug = sip_debug_test_pvt(p);
10047
10048 if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
10049
10050 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
10051 if (!strncasecmp(mimeSubtype, "T140", 4)) {
10052 if (p->trtp) {
10053
10054 ast_rtp_codecs_payloads_set_rtpmap_type_rate(newtextrtp, NULL, codec, "text", mimeSubtype, 0, sample_rate);
10055 found = TRUE;
10056 }
10057 } else if (!strncasecmp(mimeSubtype, "RED", 3)) {
10058 if (p->trtp) {
10059 ast_rtp_codecs_payloads_set_rtpmap_type_rate(newtextrtp, NULL, codec, "text", mimeSubtype, 0, sample_rate);
10060 sprintf(red_fmtp, "fmtp:%u ", codec);
10061 if (debug)
10062 ast_verbose("RED submimetype has payload type: %u\n", codec);
10063 found = TRUE;
10064 }
10065 }
10066 } else {
10067 if (debug)
10068 ast_verbose("Discarded description format %s for ID %u\n", mimeSubtype, codec);
10069 }
10070 } else if (!strncmp(a, red_fmtp, strlen(red_fmtp))) {
10071
10072 red_cp = &red_fmtp[strlen(red_fmtp)];
10073 strncpy(red_fmtp, a, 100);
10074
10075 sscanf(red_cp, "%30u", (unsigned *)&red_data_pt[*red_num_gen]);
10076 red_cp = strtok(red_cp, "/");
10077 while (red_cp && (*red_num_gen)++ < AST_RED_MAX_GENERATION) {
10078 sscanf(red_cp, "%30u", (unsigned *)&red_data_pt[*red_num_gen]);
10079 red_cp = strtok(NULL, "/");
10080 }
10081 red_cp = red_fmtp;
10082 found = TRUE;
10083 }
10084
10085 return found;
10086 }
10087
10088 static int process_sdp_a_image(const char *a, struct sip_pvt *p)
10089 {
10090 int found = FALSE;
10091 char s[256];
10092 unsigned int x;
10093 char *attrib = ast_strdupa(a);
10094 char *pos;
10095
10096 if (initialize_udptl(p)) {
10097 return found;
10098 }
10099
10100
10101
10102
10103
10104
10105 for (pos = attrib; *pos; ++pos) {
10106 *pos = tolower(*pos);
10107 }
10108
10109 if ((sscanf(attrib, "t38faxmaxbuffer:%30u", &x) == 1)) {
10110 ast_debug(3, "MaxBufferSize:%u\n", x);
10111 found = TRUE;
10112 } else if ((sscanf(attrib, "t38maxbitrate:%30u", &x) == 1) || (sscanf(attrib, "t38faxmaxrate:%30u", &x) == 1)) {
10113 ast_debug(3, "T38MaxBitRate: %u\n", x);
10114 switch (x) {
10115 case 14400:
10116 p->t38.their_parms.rate = AST_T38_RATE_14400;
10117 break;
10118 case 12000:
10119 p->t38.their_parms.rate = AST_T38_RATE_12000;
10120 break;
10121 case 9600:
10122 p->t38.their_parms.rate = AST_T38_RATE_9600;
10123 break;
10124 case 7200:
10125 p->t38.their_parms.rate = AST_T38_RATE_7200;
10126 break;
10127 case 4800:
10128 p->t38.their_parms.rate = AST_T38_RATE_4800;
10129 break;
10130 case 2400:
10131 p->t38.their_parms.rate = AST_T38_RATE_2400;
10132 break;
10133 }
10134 found = TRUE;
10135 } else if ((sscanf(attrib, "t38faxversion:%30u", &x) == 1)) {
10136 ast_debug(3, "FaxVersion: %u\n", x);
10137 p->t38.their_parms.version = x;
10138 found = TRUE;
10139 } else if ((sscanf(attrib, "t38faxmaxdatagram:%30u", &x) == 1) || (sscanf(attrib, "t38maxdatagram:%30u", &x) == 1)) {
10140
10141 if (((signed int) p->t38_maxdatagram >= 0) && ((unsigned int) p->t38_maxdatagram > x)) {
10142 ast_debug(1, "Overriding T38FaxMaxDatagram '%u' with '%u'\n", x, p->t38_maxdatagram);
10143 x = p->t38_maxdatagram;
10144 }
10145 ast_debug(3, "FaxMaxDatagram: %u\n", x);
10146 ast_udptl_set_far_max_datagram(p->udptl, x);
10147 found = TRUE;
10148 } else if ((strncmp(attrib, "t38faxfillbitremoval", 20) == 0)) {
10149 if (sscanf(attrib, "t38faxfillbitremoval:%30u", &x) == 1) {
10150 ast_debug(3, "FillBitRemoval: %u\n", x);
10151 if (x == 1) {
10152 p->t38.their_parms.fill_bit_removal = TRUE;
10153 }
10154 } else {
10155 ast_debug(3, "FillBitRemoval\n");
10156 p->t38.their_parms.fill_bit_removal = TRUE;
10157 }
10158 found = TRUE;
10159 } else if ((strncmp(attrib, "t38faxtranscodingmmr", 20) == 0)) {
10160 if (sscanf(attrib, "t38faxtranscodingmmr:%30u", &x) == 1) {
10161 ast_debug(3, "Transcoding MMR: %u\n", x);
10162 if (x == 1) {
10163 p->t38.their_parms.transcoding_mmr = TRUE;
10164 }
10165 } else {
10166 ast_debug(3, "Transcoding MMR\n");
10167 p->t38.their_parms.transcoding_mmr = TRUE;
10168 }
10169 found = TRUE;
10170 } else if ((strncmp(attrib, "t38faxtranscodingjbig", 21) == 0)) {
10171 if (sscanf(attrib, "t38faxtranscodingjbig:%30u", &x) == 1) {
10172 ast_debug(3, "Transcoding JBIG: %u\n", x);
10173 if (x == 1) {
10174 p->t38.their_parms.transcoding_jbig = TRUE;
10175 }
10176 } else {
10177 ast_debug(3, "Transcoding JBIG\n");
10178 p->t38.their_parms.transcoding_jbig = TRUE;
10179 }
10180 found = TRUE;
10181 } else if ((sscanf(attrib, "t38faxratemanagement:%255s", s) == 1)) {
10182 ast_debug(3, "RateManagement: %s\n", s);
10183 if (!strcasecmp(s, "localTCF"))
10184 p->t38.their_parms.rate_management = AST_T38_RATE_MANAGEMENT_LOCAL_TCF;
10185 else if (!strcasecmp(s, "transferredTCF"))
10186 p->t38.their_parms.rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF;
10187 found = TRUE;
10188 } else if ((sscanf(attrib, "t38faxudpec:%255s", s) == 1)) {
10189 ast_debug(3, "UDP EC: %s\n", s);
10190 if (!strcasecmp(s, "t38UDPRedundancy")) {
10191 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
10192 } else if (!strcasecmp(s, "t38UDPFEC")) {
10193 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
10194 } else {
10195 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
10196 }
10197 found = TRUE;
10198 }
10199
10200 return found;
10201 }
10202
10203
10204
10205
10206 static int add_supported_header(struct sip_pvt *pvt, struct sip_request *req)
10207 {
10208 int res;
10209 if (st_get_mode(pvt, 0) != SESSION_TIMER_MODE_REFUSE) {
10210 res = add_header(req, "Supported", "replaces, timer");
10211 } else {
10212 res = add_header(req, "Supported", "replaces");
10213 }
10214 return res;
10215 }
10216
10217
10218 static int add_header(struct sip_request *req, const char *var, const char *value)
10219 {
10220 if (req->headers == SIP_MAX_HEADERS) {
10221 ast_log(LOG_WARNING, "Out of SIP header space\n");
10222 return -1;
10223 }
10224
10225 if (req->lines) {
10226 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
10227 return -1;
10228 }
10229
10230 if (sip_cfg.compactheaders) {
10231 var = find_alias(var, var);
10232 }
10233
10234 ast_str_append(&req->data, 0, "%s: %s\r\n", var, value);
10235 req->header[req->headers] = ast_str_strlen(req->data);
10236
10237 req->headers++;
10238
10239 return 0;
10240 }
10241
10242
10243
10244
10245
10246 static int add_header_max_forwards(struct sip_pvt *dialog, struct sip_request *req)
10247 {
10248 char clen[10];
10249
10250 snprintf(clen, sizeof(clen), "%d", dialog->maxforwards);
10251
10252 return add_header(req, "Max-Forwards", clen);
10253 }
10254
10255
10256 static int finalize_content(struct sip_request *req)
10257 {
10258 char clen[10];
10259
10260 if (req->lines) {
10261 ast_log(LOG_WARNING, "finalize_content() called on a message that has already been finalized\n");
10262 return -1;
10263 }
10264
10265 snprintf(clen, sizeof(clen), "%zu", ast_str_strlen(req->content));
10266 add_header(req, "Content-Length", clen);
10267
10268 if (ast_str_strlen(req->content)) {
10269 ast_str_append(&req->data, 0, "\r\n%s", ast_str_buffer(req->content));
10270 }
10271 req->lines = ast_str_strlen(req->content) ? 1 : 0;
10272 return 0;
10273 }
10274
10275
10276 static int add_content(struct sip_request *req, const char *line)
10277 {
10278 if (req->lines) {
10279 ast_log(LOG_WARNING, "Can't add more content when the content has been finalized\n");
10280 return -1;
10281 }
10282
10283 ast_str_append(&req->content, 0, "%s", line);
10284 return 0;
10285 }
10286
10287
10288 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
10289 {
10290 const char *tmp = get_header(orig, field);
10291
10292 if (!ast_strlen_zero(tmp))
10293 return add_header(req, field, tmp);
10294 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
10295 return -1;
10296 }
10297
10298
10299 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
10300 {
10301 int start = 0;
10302 int copied = 0;
10303 for (;;) {
10304 const char *tmp = __get_header(orig, field, &start);
10305
10306 if (ast_strlen_zero(tmp))
10307 break;
10308
10309 add_header(req, field, tmp);
10310 copied++;
10311 }
10312 return copied ? 0 : -1;
10313 }
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
10324 {
10325 int copied = 0;
10326 int start = 0;
10327
10328 for (;;) {
10329 char new[512];
10330 const char *oh = __get_header(orig, field, &start);
10331
10332 if (ast_strlen_zero(oh))
10333 break;
10334
10335 if (!copied) {
10336 char leftmost[512], *others, *rport;
10337
10338
10339 ast_copy_string(leftmost, oh, sizeof(leftmost));
10340 others = strchr(leftmost, ',');
10341 if (others)
10342 *others++ = '\0';
10343
10344
10345 rport = strstr(leftmost, ";rport");
10346 if (rport && *(rport+6) == '=')
10347 rport = NULL;
10348
10349 if (((ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT)) || (rport && ast_test_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT)))) {
10350
10351 char *end;
10352
10353 rport = strstr(leftmost, ";rport");
10354
10355 if (rport) {
10356 end = strchr(rport + 1, ';');
10357 if (end)
10358 memmove(rport, end, strlen(end) + 1);
10359 else
10360 *rport = '\0';
10361 }
10362
10363
10364 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
10365 leftmost, ast_sockaddr_stringify_addr_remote(&p->recv),
10366 ast_sockaddr_port(&p->recv),
10367 others ? "," : "", others ? others : "");
10368 } else {
10369
10370 snprintf(new, sizeof(new), "%s;received=%s%s%s",
10371 leftmost, ast_sockaddr_stringify_addr_remote(&p->recv),
10372 others ? "," : "", others ? others : "");
10373 }
10374 oh = new;
10375 }
10376 add_header(req, field, oh);
10377 copied++;
10378 }
10379 if (!copied) {
10380 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
10381 return -1;
10382 }
10383 return 0;
10384 }
10385
10386
10387 static void add_route(struct sip_request *req, struct sip_route *route)
10388 {
10389 char r[SIPBUFSIZE*2], *p;
10390 int n, rem = sizeof(r);
10391
10392 if (!route)
10393 return;
10394
10395 p = r;
10396 for (;route ; route = route->next) {
10397 n = strlen(route->hop);
10398 if (rem < n+3)
10399 break;
10400 if (p != r) {
10401 *p++ = ',';
10402 --rem;
10403 }
10404 *p++ = '<';
10405 ast_copy_string(p, route->hop, rem);
10406 p += n;
10407 *p++ = '>';
10408 rem -= (n+2);
10409 }
10410 *p = '\0';
10411 add_header(req, "Route", r);
10412 }
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423 static void set_destination(struct sip_pvt *p, char *uri)
10424 {
10425 char *h, *maddr, hostname[256];
10426 int hn;
10427 int debug=sip_debug_test_pvt(p);
10428 int tls_on = FALSE;
10429
10430 if (debug)
10431 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
10432
10433
10434 h = strchr(uri, '@');
10435 if (h)
10436 ++h;
10437 else {
10438 h = uri;
10439 if (!strncasecmp(h, "sip:", 4)) {
10440 h += 4;
10441 } else if (!strncasecmp(h, "sips:", 5)) {
10442 h += 5;
10443 tls_on = TRUE;
10444 }
10445 }
10446 hn = strcspn(h, ";>") + 1;
10447 if (hn > sizeof(hostname))
10448 hn = sizeof(hostname);
10449 ast_copy_string(hostname, h, hn);
10450
10451 h += hn - 1;
10452
10453
10454
10455 if (ast_sockaddr_resolve_first_transport(&p->sa, hostname, 0, p->socket.type)) {
10456 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
10457 return;
10458 }
10459
10460
10461 maddr = strstr(h, "maddr=");
10462 if (maddr) {
10463 int port;
10464
10465 maddr += 6;
10466 hn = strspn(maddr, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
10467 "0123456789-.:[]") + 1;
10468 if (hn > sizeof(hostname))
10469 hn = sizeof(hostname);
10470 ast_copy_string(hostname, maddr, hn);
10471
10472 port = ast_sockaddr_port(&p->sa);
10473
10474
10475
10476 if (ast_sockaddr_resolve_first_transport(&p->sa, hostname, PARSE_PORT_FORBID, p->socket.type)) {
10477 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
10478 return;
10479 }
10480
10481 ast_sockaddr_set_port(&p->sa, port);
10482 }
10483
10484 if (!ast_sockaddr_port(&p->sa)) {
10485 ast_sockaddr_set_port(&p->sa, tls_on ?
10486 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
10487 }
10488
10489 if (debug) {
10490 ast_verbose("set_destination: set destination to %s\n",
10491 ast_sockaddr_stringify(&p->sa));
10492 }
10493 }
10494
10495
10496 static int init_resp(struct sip_request *resp, const char *msg)
10497 {
10498
10499 memset(resp, 0, sizeof(*resp));
10500 resp->method = SIP_RESPONSE;
10501 if (!(resp->data = ast_str_create(SIP_MIN_PACKET)))
10502 goto e_return;
10503 if (!(resp->content = ast_str_create(SIP_MIN_PACKET)))
10504 goto e_free_data;
10505 resp->header[0] = 0;
10506 ast_str_set(&resp->data, 0, "SIP/2.0 %s\r\n", msg);
10507 resp->headers++;
10508 return 0;
10509
10510 e_free_data:
10511 ast_free(resp->data);
10512 resp->data = NULL;
10513 e_return:
10514 return -1;
10515 }
10516
10517
10518 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
10519 {
10520
10521 memset(req, 0, sizeof(*req));
10522 if (!(req->data = ast_str_create(SIP_MIN_PACKET)))
10523 goto e_return;
10524 if (!(req->content = ast_str_create(SIP_MIN_PACKET)))
10525 goto e_free_data;
10526 req->method = sipmethod;
10527 req->header[0] = 0;
10528 ast_str_set(&req->data, 0, "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
10529 req->headers++;
10530 return 0;
10531
10532 e_free_data:
10533 ast_free(req->data);
10534 req->data = NULL;
10535 e_return:
10536 return -1;
10537 }
10538
10539
10540 static void deinit_req(struct sip_request *req)
10541 {
10542 if (req->data) {
10543 ast_free(req->data);
10544 req->data = NULL;
10545 }
10546 if (req->content) {
10547 ast_free(req->content);
10548 req->content = NULL;
10549 }
10550 }
10551
10552
10553
10554 static inline int resp_needs_contact(const char *msg, enum sipmethod method) {
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570 switch (method) {
10571
10572 case SIP_INVITE:
10573 case SIP_UPDATE:
10574 case SIP_SUBSCRIBE:
10575 case SIP_NOTIFY:
10576 if ((msg[0] >= '1' && msg[0] <= '3') || !strncmp(msg, "485", 3))
10577 return 1;
10578 break;
10579
10580
10581 case SIP_REGISTER:
10582 case SIP_OPTIONS:
10583 if (msg[0] == '2' || msg[0] == '3' || !strncmp(msg, "485", 3))
10584 return 1;
10585 break;
10586
10587
10588 case SIP_BYE:
10589 case SIP_PRACK:
10590 case SIP_MESSAGE:
10591 case SIP_PUBLISH:
10592 if (msg[0] == '3' || !strncmp(msg, "485", 3))
10593 return 1;
10594 break;
10595
10596
10597 case SIP_REFER:
10598 if (msg[0] >= '2' && msg[0] <= '6')
10599 return 1;
10600 break;
10601
10602
10603 case SIP_ACK:
10604 case SIP_CANCEL:
10605 case SIP_INFO:
10606 case SIP_PING:
10607 default:
10608 return 0;
10609 }
10610 return 0;
10611 }
10612
10613
10614 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
10615 {
10616 char newto[256];
10617 const char *ot;
10618
10619 init_resp(resp, msg);
10620 copy_via_headers(p, resp, req, "Via");
10621 if (msg[0] == '1' || msg[0] == '2')
10622 copy_all_header(resp, req, "Record-Route");
10623 copy_header(resp, req, "From");
10624 ot = get_header(req, "To");
10625 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
10626
10627
10628 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
10629 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
10630 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
10631 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
10632 else
10633 ast_copy_string(newto, ot, sizeof(newto));
10634 ot = newto;
10635 }
10636 add_header(resp, "To", ot);
10637 copy_header(resp, req, "Call-ID");
10638 copy_header(resp, req, "CSeq");
10639 if (!ast_strlen_zero(global_useragent))
10640 add_header(resp, "Server", global_useragent);
10641 add_header(resp, "Allow", ALLOWED_METHODS);
10642 add_supported_header(p, resp);
10643
10644
10645 if (p->method == SIP_INVITE && p->stimer && p->stimer->st_active == TRUE) {
10646 char se_hdr[256];
10647 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
10648 p->stimer->st_ref == SESSION_TIMER_REFRESHER_US ? "uas" : "uac");
10649 add_header(resp, "Session-Expires", se_hdr);
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660 if (p->stimer->st_ref == SESSION_TIMER_REFRESHER_THEM ||
10661 (p->stimer->st_ref == SESSION_TIMER_REFRESHER_US &&
10662 p->stimer->st_active_peer_ua == TRUE)) {
10663 resp->reqsipoptions |= SIP_OPT_TIMER;
10664 }
10665 }
10666
10667 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_PUBLISH)) {
10668
10669
10670 char tmp[256];
10671
10672 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
10673 add_header(resp, "Expires", tmp);
10674 if (p->expiry) {
10675 char contact[SIPBUFSIZE];
10676 const char *contact_uri = p->method == SIP_SUBSCRIBE ? p->our_contact : p->fullcontact;
10677 char *brackets = strchr(contact_uri, '<');
10678 snprintf(contact, sizeof(contact), "%s%s%s;expires=%d", brackets ? "" : "<", contact_uri, brackets ? "" : ">", p->expiry);
10679 add_header(resp, "Contact", contact);
10680 }
10681 } else if (!ast_strlen_zero(p->our_contact) && resp_needs_contact(msg, p->method)) {
10682 add_header(resp, "Contact", p->our_contact);
10683 }
10684
10685 if (!ast_strlen_zero(p->url)) {
10686 add_header(resp, "Access-URL", p->url);
10687 ast_string_field_set(p, url, NULL);
10688 }
10689
10690
10691
10692
10693
10694
10695
10696 p->sa = p->recv;
10697
10698 if (process_via(p, req)) {
10699 ast_log(LOG_WARNING, "error processing via header, will send response to originating address\n");
10700 }
10701
10702 return 0;
10703 }
10704
10705
10706 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, uint32_t seqno, int newbranch)
10707 {
10708 struct sip_request *orig = &p->initreq;
10709 char stripped[80];
10710 char tmp[80];
10711 char newto[256];
10712 const char *c;
10713 const char *ot, *of;
10714 int is_strict = FALSE;
10715 int is_outbound = ast_test_flag(&p->flags[0], SIP_OUTGOING);
10716
10717 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
10718
10719 if (!seqno) {
10720 p->ocseq++;
10721 seqno = p->ocseq;
10722 }
10723
10724
10725 if (sipmethod == SIP_CANCEL) {
10726 p->branch = p->invite_branch;
10727 build_via(p);
10728 } else if (newbranch && (sipmethod == SIP_INVITE)) {
10729 p->branch ^= ast_random();
10730 p->invite_branch = p->branch;
10731 build_via(p);
10732 } else if (newbranch) {
10733 p->branch ^= ast_random();
10734 build_via(p);
10735 }
10736
10737
10738 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop, ";lr") == NULL) {
10739 is_strict = TRUE;
10740 if (sipdebug)
10741 ast_debug(1, "Strict routing enforced for session %s\n", p->callid);
10742 }
10743
10744 if (sipmethod == SIP_CANCEL)
10745 c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
10746 else if (sipmethod == SIP_ACK) {
10747
10748
10749 if (!ast_strlen_zero(p->okcontacturi))
10750 c = is_strict ? p->route->hop : p->okcontacturi;
10751 else
10752 c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
10753 } else if (!ast_strlen_zero(p->okcontacturi))
10754 c = is_strict ? p->route->hop : p->okcontacturi;
10755 else if (!ast_strlen_zero(p->uri))
10756 c = p->uri;
10757 else {
10758 char *n;
10759
10760 ast_copy_string(stripped, get_header(orig, is_outbound ? "To" : "From"),
10761 sizeof(stripped));
10762 n = get_in_brackets(stripped);
10763 c = remove_uri_parameters(n);
10764 }
10765 init_req(req, sipmethod, c);
10766
10767 snprintf(tmp, sizeof(tmp), "%u %s", seqno, sip_methods[sipmethod].text);
10768
10769 add_header(req, "Via", p->via);
10770
10771
10772
10773
10774
10775 if (p->route &&
10776 !(sipmethod == SIP_CANCEL ||
10777 (sipmethod == SIP_ACK && (p->invitestate == INV_COMPLETED || p->invitestate == INV_CANCELLED)))) {
10778 set_destination(p, p->route->hop);
10779 add_route(req, is_strict ? p->route->next : p->route);
10780 }
10781 add_header_max_forwards(p, req);
10782
10783 ot = get_header(orig, "To");
10784 of = get_header(orig, "From");
10785
10786
10787
10788 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
10789
10790
10791 if (is_outbound && !ast_strlen_zero(p->theirtag))
10792 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
10793 else if (!is_outbound)
10794 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
10795 else
10796 snprintf(newto, sizeof(newto), "%s", ot);
10797 ot = newto;
10798 }
10799
10800 if (is_outbound) {
10801 add_header(req, "From", of);
10802 add_header(req, "To", ot);
10803 } else {
10804 add_header(req, "From", ot);
10805 add_header(req, "To", of);
10806 }
10807
10808 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
10809 add_header(req, "Contact", p->our_contact);
10810
10811 copy_header(req, orig, "Call-ID");
10812 add_header(req, "CSeq", tmp);
10813
10814 if (!ast_strlen_zero(global_useragent))
10815 add_header(req, "User-Agent", global_useragent);
10816
10817 if (!ast_strlen_zero(p->url)) {
10818 add_header(req, "Access-URL", p->url);
10819 ast_string_field_set(p, url, NULL);
10820 }
10821
10822
10823
10824
10825
10826
10827
10828
10829 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE
10830 && sipmethod == SIP_INVITE) {
10831 char se_hdr[256];
10832 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
10833 p->stimer->st_ref == SESSION_TIMER_REFRESHER_US ? "uac" : "uas");
10834 add_header(req, "Session-Expires", se_hdr);
10835 snprintf(se_hdr, sizeof(se_hdr), "%d", st_get_se(p, FALSE));
10836 add_header(req, "Min-SE", se_hdr);
10837 }
10838
10839 return 0;
10840 }
10841
10842
10843 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
10844 {
10845 struct sip_request resp;
10846 uint32_t seqno = 0;
10847
10848 if (reliable && (sscanf(get_header(req, "CSeq"), "%30u ", &seqno) != 1)) {
10849 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
10850 return -1;
10851 }
10852 respprep(&resp, p, msg, req);
10853
10854 if (ast_test_flag(&p->flags[0], SIP_SENDRPID)
10855 && ast_test_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND)
10856 && (!strncmp(msg, "180", 3) || !strncmp(msg, "183", 3))) {
10857 ast_clear_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND);
10858 add_rpid(&resp, p);
10859 }
10860 if (ast_test_flag(&p->flags[0], SIP_OFFER_CC)) {
10861 add_cc_call_info_to_response(p, &resp);
10862 }
10863
10864
10865 if (!strncmp(msg, "302", 3)) {
10866 add_diversion_header(&resp, p);
10867 }
10868
10869
10870
10871 if (p->method == SIP_INVITE && msg[0] != '1') {
10872 char buf[20];
10873
10874 if (ast_test_flag(&p->flags[1], SIP_PAGE2_Q850_REASON)) {
10875 int hangupcause = 0;
10876
10877 if (p->owner && p->owner->hangupcause) {
10878 hangupcause = p->owner->hangupcause;
10879 } else if (p->hangupcause) {
10880 hangupcause = p->hangupcause;
10881 } else {
10882 int respcode;
10883 if (sscanf(msg, "%30d ", &respcode))
10884 hangupcause = hangup_sip2cause(respcode);
10885 }
10886
10887 if (hangupcause) {
10888 sprintf(buf, "Q.850;cause=%i", hangupcause & 0x7f);
10889 add_header(&resp, "Reason", buf);
10890 }
10891 }
10892
10893 if (p->owner && p->owner->hangupcause) {
10894 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
10895 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
10896 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
10897 }
10898 }
10899 return send_response(p, &resp, reliable, seqno);
10900 }
10901
10902 static int transmit_response_with_sip_etag(struct sip_pvt *p, const char *msg, const struct sip_request *req, struct sip_esc_entry *esc_entry, int need_new_etag)
10903 {
10904 struct sip_request resp;
10905
10906 if (need_new_etag) {
10907 create_new_sip_etag(esc_entry, 1);
10908 }
10909 respprep(&resp, p, msg, req);
10910 add_header(&resp, "SIP-ETag", esc_entry->entity_tag);
10911
10912 return send_response(p, &resp, 0, 0);
10913 }
10914
10915 static int temp_pvt_init(void *data)
10916 {
10917 struct sip_pvt *p = data;
10918
10919 p->do_history = 0;
10920 return ast_string_field_init(p, 512);
10921 }
10922
10923 static void temp_pvt_cleanup(void *data)
10924 {
10925 struct sip_pvt *p = data;
10926
10927 ast_string_field_free_memory(p);
10928
10929 ast_free(data);
10930 }
10931
10932
10933 static int transmit_response_using_temp(ast_string_field callid, struct ast_sockaddr *addr, int useglobal_nat, const int intended_method, const struct sip_request *req, const char *msg)
10934 {
10935 struct sip_pvt *p = NULL;
10936
10937 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
10938 ast_log(LOG_ERROR, "Failed to get temporary pvt\n");
10939 return -1;
10940 }
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950 p->method = intended_method;
10951
10952 if (!addr) {
10953 ast_sockaddr_copy(&p->ourip, &internip);
10954 } else {
10955 ast_sockaddr_copy(&p->sa, addr);
10956 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
10957 }
10958
10959 p->branch = ast_random();
10960 make_our_tag(p);
10961 p->ocseq = INITIAL_CSEQ;
10962
10963 if (useglobal_nat && addr) {
10964 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT_FORCE_RPORT);
10965 ast_sockaddr_copy(&p->recv, addr);
10966 do_setnat(p);
10967 }
10968
10969 ast_string_field_set(p, fromdomain, default_fromdomain);
10970 p->fromdomainport = default_fromdomainport;
10971 build_via(p);
10972 ast_string_field_set(p, callid, callid);
10973
10974 copy_socket_data(&p->socket, &req->socket);
10975
10976
10977 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
10978
10979
10980 ast_string_field_init(p, 0);
10981
10982 return 0;
10983 }
10984
10985
10986 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
10987 {
10988 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
10989 }
10990
10991
10992 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
10993 {
10994 struct sip_request resp;
10995 respprep(&resp, p, msg, req);
10996 append_date(&resp);
10997 add_header(&resp, "Unsupported", unsupported);
10998 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
10999 }
11000
11001
11002 static int transmit_response_with_minse(struct sip_pvt *p, const char *msg, const struct sip_request *req, int minse_int)
11003 {
11004 struct sip_request resp;
11005 char minse_str[20];
11006
11007 respprep(&resp, p, msg, req);
11008 append_date(&resp);
11009
11010 snprintf(minse_str, sizeof(minse_str), "%d", minse_int);
11011 add_header(&resp, "Min-SE", minse_str);
11012 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
11013 }
11014
11015
11016
11017
11018
11019 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
11020 {
11021 return __transmit_response(p, msg, req, req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL);
11022 }
11023
11024
11025 static void append_date(struct sip_request *req)
11026 {
11027 char tmpdat[256];
11028 struct tm tm;
11029 time_t t = time(NULL);
11030
11031 gmtime_r(&t, &tm);
11032 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
11033 add_header(req, "Date", tmpdat);
11034 }
11035
11036
11037 static int transmit_response_with_retry_after(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *seconds)
11038 {
11039 struct sip_request resp;
11040 respprep(&resp, p, msg, req);
11041 add_header(&resp, "Retry-After", seconds);
11042 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
11043 }
11044
11045
11046 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
11047 {
11048 struct sip_request resp;
11049 respprep(&resp, p, msg, req);
11050 append_date(&resp);
11051 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
11052 }
11053
11054
11055 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
11056 {
11057 struct sip_request resp;
11058 respprep(&resp, p, msg, req);
11059 add_header(&resp, "Accept", "application/sdp");
11060 return send_response(p, &resp, reliable, 0);
11061 }
11062
11063
11064 static int transmit_response_with_minexpires(struct sip_pvt *p, const char *msg, const struct sip_request *req)
11065 {
11066 struct sip_request resp;
11067 char tmp[32];
11068
11069 snprintf(tmp, sizeof(tmp), "%d", min_expiry);
11070 respprep(&resp, p, msg, req);
11071 add_header(&resp, "Min-Expires", tmp);
11072 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
11073 }
11074
11075
11076 static int transmit_response_with_auth(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *randdata, enum xmittype reliable, const char *header, int stale)
11077 {
11078 struct sip_request resp;
11079 char tmp[512];
11080 uint32_t seqno = 0;
11081
11082 if (reliable && (sscanf(get_header(req, "CSeq"), "%30u ", &seqno) != 1)) {
11083 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
11084 return -1;
11085 }
11086
11087 get_realm(p, req);
11088
11089
11090
11091 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", p->realm, randdata, stale ? ", stale=true" : "");
11092 respprep(&resp, p, msg, req);
11093 add_header(&resp, header, tmp);
11094 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
11095 return send_response(p, &resp, reliable, seqno);
11096 }
11097
11098
11099
11100
11101
11102
11103
11104 static int get_domain(const char *str, char *domain, int len)
11105 {
11106 char tmpf[256];
11107 char *a, *from;
11108
11109 *domain = '\0';
11110 ast_copy_string(tmpf, str, sizeof(tmpf));
11111 from = get_in_brackets(tmpf);
11112 if (!ast_strlen_zero(from)) {
11113 if (strncasecmp(from, "sip:", 4)) {
11114 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
11115 return -1;
11116 }
11117 from += 4;
11118 } else
11119 from = NULL;
11120
11121 if (from) {
11122 int bracket = 0;
11123
11124
11125 if ((a = strchr(from, ';')))
11126 *a = '\0';
11127
11128 for (a = from; *a != '\0'; ++a) {
11129 if (*a == ':' && bracket == 0) {
11130 *a = '\0';
11131 break;
11132 } else if (*a == '[') {
11133 ++bracket;
11134 } else if (*a == ']') {
11135 --bracket;
11136 }
11137 }
11138 if ((a = strchr(from, '@'))) {
11139 *a = '\0';
11140 ast_copy_string(domain, a + 1, len);
11141 } else
11142 ast_copy_string(domain, from, len);
11143 }
11144
11145 return ast_strlen_zero(domain);
11146 }
11147
11148
11149
11150
11151
11152 static void get_realm(struct sip_pvt *p, const struct sip_request *req)
11153 {
11154 char domain[MAXHOSTNAMELEN];
11155
11156 if (!ast_strlen_zero(p->realm))
11157 return;
11158
11159 if (sip_cfg.domainsasrealm &&
11160 !AST_LIST_EMPTY(&domain_list))
11161 {
11162
11163 if (!get_domain(get_header(req, "From"), domain, sizeof(domain))) {
11164 if (check_sip_domain(domain, NULL, 0)) {
11165 ast_string_field_set(p, realm, domain);
11166 return;
11167 }
11168 }
11169
11170 if (!get_domain(get_header(req, "To"), domain, sizeof(domain))) {
11171 if (check_sip_domain(domain, NULL, 0)) {
11172 ast_string_field_set(p, realm, domain);
11173 return;
11174 }
11175 }
11176 }
11177
11178
11179 ast_string_field_set(p, realm, sip_cfg.realm);
11180 }
11181
11182
11183
11184
11185
11186
11187
11188
11189 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp)
11190 {
11191 int res;
11192
11193 if (!(res = with_sdp ? transmit_response_with_sdp(p, msg, req, XMIT_UNRELIABLE, FALSE, FALSE) : transmit_response(p, msg, req))) {
11194 p->last_provisional = msg;
11195 update_provisional_keepalive(p, with_sdp);
11196 }
11197
11198 return res;
11199 }
11200
11201
11202 static int add_text(struct sip_request *req, const char *text)
11203 {
11204
11205 add_header(req, "Content-Type", "text/plain;charset=UTF-8");
11206 add_content(req, text);
11207 return 0;
11208 }
11209
11210
11211
11212
11213
11214 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode)
11215 {
11216 char tmp[256];
11217 int event;
11218 if (mode) {
11219
11220 if ('0' <= digit && digit <= '9') {
11221 event = digit - '0';
11222 } else if (digit == '*') {
11223 event = 10;
11224 } else if (digit == '#') {
11225 event = 11;
11226 } else if ('A' <= digit && digit <= 'D') {
11227 event = 12 + digit - 'A';
11228 } else if ('a' <= digit && digit <= 'd') {
11229 event = 12 + digit - 'a';
11230 } else {
11231
11232 event = 0;
11233 }
11234 snprintf(tmp, sizeof(tmp), "%d\r\n", event);
11235 add_header(req, "Content-Type", "application/dtmf");
11236 add_content(req, tmp);
11237 } else {
11238
11239 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
11240 add_header(req, "Content-Type", "application/dtmf-relay");
11241 add_content(req, tmp);
11242 }
11243 return 0;
11244 }
11245
11246
11247
11248
11249
11250 static int add_rpid(struct sip_request *req, struct sip_pvt *p)
11251 {
11252 struct ast_str *tmp = ast_str_alloca(256);
11253 char tmp2[256];
11254 char lid_name_buf[128];
11255 char *lid_num;
11256 char *lid_name;
11257 int lid_pres;
11258 const char *fromdomain;
11259 const char *privacy = NULL;
11260 const char *screen = NULL;
11261 const char *anonymous_string = "\"Anonymous\" <sip:anonymous@anonymous.invalid>";
11262
11263 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID)) {
11264 return 0;
11265 }
11266
11267 if (!p->owner) {
11268 return 0;
11269 }
11270 lid_num = S_COR(p->owner->connected.id.number.valid,
11271 p->owner->connected.id.number.str,
11272 NULL);
11273 if (!lid_num) {
11274 return 0;
11275 }
11276 lid_name = S_COR(p->owner->connected.id.name.valid,
11277 p->owner->connected.id.name.str,
11278 NULL);
11279 if (!lid_name) {
11280 lid_name = lid_num;
11281 }
11282 ast_escape_quoted(lid_name, lid_name_buf, sizeof(lid_name_buf));
11283 lid_pres = ast_party_id_presentation(&p->owner->connected.id);
11284
11285 if (((lid_pres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) &&
11286 (ast_test_flag(&p->flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND) == SIP_PAGE2_TRUST_ID_OUTBOUND_NO)) {
11287
11288 return 0;
11289 }
11290
11291 fromdomain = p->fromdomain;
11292 if (!fromdomain ||
11293 ((ast_test_flag(&p->flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND) == SIP_PAGE2_TRUST_ID_OUTBOUND_YES) &&
11294 !strcmp("anonymous.invalid", fromdomain))) {
11295
11296
11297 fromdomain = ast_sockaddr_stringify_host_remote(&p->ourip);
11298 }
11299
11300 lid_num = ast_uri_encode(lid_num, tmp2, sizeof(tmp2), 0);
11301
11302 if (ast_test_flag(&p->flags[0], SIP_SENDRPID_PAI)) {
11303 if (ast_test_flag(&p->flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND) != SIP_PAGE2_TRUST_ID_OUTBOUND_LEGACY) {
11304
11305
11306 ast_str_set(&tmp, -1, "\"%s\" <sip:%s@%s>", lid_name_buf, lid_num, fromdomain);
11307 if ((lid_pres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
11308 add_header(req, "Privacy", "id");
11309 }
11310 } else {
11311
11312
11313 if ((lid_pres & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
11314 ast_str_set(&tmp, -1, "\"%s\" <sip:%s@%s>", lid_name_buf, lid_num, fromdomain);
11315 } else {
11316 ast_str_set(&tmp, -1, "%s", anonymous_string);
11317 }
11318 }
11319 add_header(req, "P-Asserted-Identity", ast_str_buffer(tmp));
11320 } else {
11321 ast_str_set(&tmp, -1, "\"%s\" <sip:%s@%s>;party=%s", lid_name_buf, lid_num, fromdomain, p->outgoing_call ? "calling" : "called");
11322
11323 switch (lid_pres) {
11324 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
11325 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
11326 privacy = "off";
11327 screen = "no";
11328 break;
11329 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
11330 case AST_PRES_ALLOWED_NETWORK_NUMBER:
11331 privacy = "off";
11332 screen = "yes";
11333 break;
11334 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
11335 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
11336 privacy = "full";
11337 screen = "no";
11338 break;
11339 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
11340 case AST_PRES_PROHIB_NETWORK_NUMBER:
11341 privacy = "full";
11342 screen = "yes";
11343 break;
11344 case AST_PRES_NUMBER_NOT_AVAILABLE:
11345 break;
11346 default:
11347 if ((lid_pres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
11348 privacy = "full";
11349 }
11350 else
11351 privacy = "off";
11352 screen = "no";
11353 break;
11354 }
11355
11356 if (!ast_strlen_zero(privacy) && !ast_strlen_zero(screen)) {
11357 ast_str_append(&tmp, -1, ";privacy=%s;screen=%s", privacy, screen);
11358 }
11359
11360 add_header(req, "Remote-Party-ID", ast_str_buffer(tmp));
11361 }
11362 return 0;
11363 }
11364
11365
11366
11367 static int add_vidupdate(struct sip_request *req)
11368 {
11369 const char *xml_is_a_huge_waste_of_space =
11370 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
11371 " <media_control>\r\n"
11372 " <vc_primitive>\r\n"
11373 " <to_encoder>\r\n"
11374 " <picture_fast_update>\r\n"
11375 " </picture_fast_update>\r\n"
11376 " </to_encoder>\r\n"
11377 " </vc_primitive>\r\n"
11378 " </media_control>\r\n";
11379 add_header(req, "Content-Type", "application/media_control+xml");
11380 add_content(req, xml_is_a_huge_waste_of_space);
11381 return 0;
11382 }
11383
11384
11385 static void add_codec_to_sdp(const struct sip_pvt *p, format_t codec,
11386 struct ast_str **m_buf, struct ast_str **a_buf,
11387 int debug, int *min_packet_size)
11388 {
11389 int rtp_code;
11390 struct ast_format_list fmt;
11391
11392
11393 if (debug)
11394 ast_verbose("Adding codec 0x%" PRIx64 " (%s) to SDP\n", (uint64_t)codec, ast_getformatname(codec));
11395 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 1, codec)) == -1)
11396 return;
11397
11398 if (p->rtp) {
11399 struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
11400 fmt = ast_codec_pref_getsize(pref, codec);
11401 } else
11402 return;
11403 ast_str_append(m_buf, 0, " %d", rtp_code);
11404 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%u\r\n", rtp_code,
11405 ast_rtp_lookup_mime_subtype2(1, codec,
11406 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
11407 ast_rtp_lookup_sample_rate2(1, codec));
11408
11409 switch (codec) {
11410 case AST_FORMAT_G729A:
11411
11412 ast_str_append(a_buf, 0, "a=fmtp:%d annexb=no\r\n", rtp_code);
11413 break;
11414 case AST_FORMAT_G723_1:
11415
11416 ast_str_append(a_buf, 0, "a=fmtp:%d annexa=no\r\n", rtp_code);
11417 break;
11418 case AST_FORMAT_ILBC:
11419
11420 ast_str_append(a_buf, 0, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
11421 break;
11422 case AST_FORMAT_SIREN7:
11423
11424 ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=32000\r\n", rtp_code);
11425 break;
11426 case AST_FORMAT_SIREN14:
11427
11428 ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=48000\r\n", rtp_code);
11429 break;
11430 case AST_FORMAT_G719:
11431
11432 ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=64000\r\n", rtp_code);
11433 break;
11434 }
11435
11436 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
11437 *min_packet_size = fmt.cur_ms;
11438
11439
11440 if ((*min_packet_size)==0 && fmt.cur_ms)
11441 *min_packet_size = fmt.cur_ms;
11442 }
11443
11444
11445
11446 static void add_vcodec_to_sdp(const struct sip_pvt *p, format_t codec,
11447 struct ast_str **m_buf, struct ast_str **a_buf,
11448 int debug, int *min_packet_size)
11449 {
11450 int rtp_code;
11451
11452 if (!p->vrtp)
11453 return;
11454
11455 if (debug)
11456 ast_verbose("Adding video codec 0x%" PRIx64 " (%s) to SDP\n", (uint64_t)codec, ast_getformatname(codec));
11457
11458 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->vrtp), 1, codec)) == -1)
11459 return;
11460
11461 ast_str_append(m_buf, 0, " %d", rtp_code);
11462 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%u\r\n", rtp_code,
11463 ast_rtp_lookup_mime_subtype2(1, codec, 0),
11464 ast_rtp_lookup_sample_rate2(1, codec));
11465
11466 }
11467
11468
11469 static void add_tcodec_to_sdp(const struct sip_pvt *p, int codec,
11470 struct ast_str **m_buf, struct ast_str **a_buf,
11471 int debug, int *min_packet_size)
11472 {
11473 int rtp_code;
11474
11475 if (!p->trtp)
11476 return;
11477
11478 if (debug)
11479 ast_verbose("Adding text codec 0x%x (%s) to SDP\n", (unsigned)codec, ast_getformatname(codec));
11480
11481 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, codec)) == -1)
11482 return;
11483
11484 ast_str_append(m_buf, 0, " %d", rtp_code);
11485 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%u\r\n", rtp_code,
11486 ast_rtp_lookup_mime_subtype2(1, codec, 0),
11487 ast_rtp_lookup_sample_rate2(1, codec));
11488
11489
11490 if (codec == AST_FORMAT_T140RED) {
11491 int t140code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, AST_FORMAT_T140);
11492 ast_str_append(a_buf, 0, "a=fmtp:%d %d/%d/%d\r\n", rtp_code,
11493 t140code,
11494 t140code,
11495 t140code);
11496
11497 }
11498 }
11499
11500
11501
11502 static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
11503 {
11504 switch (rate) {
11505 case AST_T38_RATE_2400:
11506 return 2400;
11507 case AST_T38_RATE_4800:
11508 return 4800;
11509 case AST_T38_RATE_7200:
11510 return 7200;
11511 case AST_T38_RATE_9600:
11512 return 9600;
11513 case AST_T38_RATE_12000:
11514 return 12000;
11515 case AST_T38_RATE_14400:
11516 return 14400;
11517 default:
11518 return 0;
11519 }
11520 }
11521
11522
11523 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format,
11524 struct ast_str **m_buf, struct ast_str **a_buf,
11525 int debug)
11526 {
11527 int rtp_code;
11528
11529 if (debug)
11530 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", (unsigned)format, ast_rtp_lookup_mime_subtype2(0, format, 0));
11531 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 0, format)) == -1)
11532 return;
11533
11534 ast_str_append(m_buf, 0, " %d", rtp_code);
11535 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%u\r\n", rtp_code,
11536 ast_rtp_lookup_mime_subtype2(0, format, 0),
11537 ast_rtp_lookup_sample_rate2(0, format));
11538 if (format == AST_RTP_DTMF)
11539 ast_str_append(a_buf, 0, "a=fmtp:%d 0-16\r\n", rtp_code);
11540 }
11541
11542
11543
11544
11545 static void get_our_media_address(struct sip_pvt *p, int needvideo, int needtext,
11546 struct ast_sockaddr *addr, struct ast_sockaddr *vaddr,
11547 struct ast_sockaddr *taddr, struct ast_sockaddr *dest,
11548 struct ast_sockaddr *vdest, struct ast_sockaddr *tdest)
11549 {
11550 int use_externip = 0;
11551
11552
11553 ast_rtp_instance_get_local_address(p->rtp, addr);
11554 if (p->vrtp) {
11555 ast_rtp_instance_get_local_address(p->vrtp, vaddr);
11556 }
11557 if (p->trtp) {
11558 ast_rtp_instance_get_local_address(p->trtp, taddr);
11559 }
11560
11561
11562
11563
11564 use_externip = ast_sockaddr_cmp_addr(&p->ourip, addr);
11565
11566
11567
11568 if (!ast_sockaddr_isnull(&p->redirip)) {
11569 ast_sockaddr_copy(dest, &p->redirip);
11570 } else {
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582 ast_sockaddr_copy(dest,
11583 !ast_sockaddr_isnull(&media_address) ? &media_address :
11584 !ast_sockaddr_is_any(addr) && !use_externip ? addr :
11585 &p->ourip);
11586 ast_sockaddr_set_port(dest, ast_sockaddr_port(addr));
11587 }
11588
11589 if (needvideo) {
11590
11591 if (!ast_sockaddr_isnull(&p->vredirip)) {
11592 ast_sockaddr_copy(vdest, &p->vredirip);
11593 } else {
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605 ast_sockaddr_copy(vdest,
11606 !ast_sockaddr_isnull(&media_address) ? &media_address :
11607 !ast_sockaddr_is_any(vaddr) && !use_externip ? vaddr :
11608 &p->ourip);
11609 ast_sockaddr_set_port(vdest, ast_sockaddr_port(vaddr));
11610 }
11611 }
11612
11613 if (needtext) {
11614
11615 if (!ast_sockaddr_isnull(&p->tredirip)) {
11616 ast_sockaddr_copy(tdest, &p->tredirip);
11617 } else {
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629 ast_sockaddr_copy(tdest,
11630 !ast_sockaddr_isnull(&media_address) ? &media_address :
11631 !ast_sockaddr_is_any(taddr) && !use_externip ? taddr :
11632 &p->ourip);
11633 ast_sockaddr_set_port(tdest, ast_sockaddr_port(taddr));
11634 }
11635 }
11636 }
11637
11638 static void get_crypto_attrib(struct sip_srtp *srtp, const char **a_crypto)
11639 {
11640
11641 if (srtp) {
11642 if (!srtp->crypto) {
11643 srtp->crypto = sdp_crypto_setup();
11644 }
11645 if (srtp->crypto && (sdp_crypto_offer(srtp->crypto) >= 0)) {
11646 *a_crypto = sdp_crypto_attrib(srtp->crypto);
11647 }
11648
11649 if (!*a_crypto) {
11650 ast_log(LOG_WARNING, "No SRTP key management enabled\n");
11651 }
11652 }
11653 }
11654
11655
11656
11657
11658
11659
11660
11661 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
11662 {
11663 format_t alreadysent = 0;
11664 int doing_directmedia = FALSE;
11665
11666 struct ast_sockaddr addr = { {0,} };
11667 struct ast_sockaddr vaddr = { {0,} };
11668 struct ast_sockaddr taddr = { {0,} };
11669 struct ast_sockaddr udptladdr = { {0,} };
11670 struct ast_sockaddr dest = { {0,} };
11671 struct ast_sockaddr vdest = { {0,} };
11672 struct ast_sockaddr tdest = { {0,} };
11673 struct ast_sockaddr udptldest = { {0,} };
11674
11675
11676 char *version = "v=0\r\n";
11677 char subject[256];
11678 char owner[256];
11679 char connection[256];
11680 char *session_time = "t=0 0\r\n";
11681 char bandwidth[256] = "";
11682 char *hold = "";
11683 struct ast_str *m_audio = ast_str_alloca(256);
11684 struct ast_str *m_video = ast_str_alloca(256);
11685 struct ast_str *m_text = ast_str_alloca(256);
11686 struct ast_str *m_modem = ast_str_alloca(256);
11687 struct ast_str *a_audio = ast_str_alloca(1024);
11688 struct ast_str *a_video = ast_str_alloca(1024);
11689 struct ast_str *a_text = ast_str_alloca(1024);
11690 struct ast_str *a_modem = ast_str_alloca(1024);
11691 const char *a_crypto = NULL;
11692 const char *v_a_crypto = NULL;
11693 const char *t_a_crypto = NULL;
11694
11695 format_t x;
11696 format_t capability = 0;
11697 int needaudio = FALSE;
11698 int needvideo = FALSE;
11699 int needtext = FALSE;
11700 int debug = sip_debug_test_pvt(p);
11701 int min_audio_packet_size = 0;
11702 int min_video_packet_size = 0;
11703 int min_text_packet_size = 0;
11704
11705 char codecbuf[SIPBUFSIZE];
11706 char buf[SIPBUFSIZE];
11707 char dummy_answer[256];
11708
11709
11710 snprintf(subject, sizeof(subject), "s=%s\r\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
11711
11712 if (!p->rtp) {
11713 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
11714 return AST_FAILURE;
11715 }
11716
11717
11718
11719
11720 if (!p->sessionid) {
11721 p->sessionid = (int)ast_random();
11722 p->sessionversion = p->sessionid;
11723 } else {
11724 if (oldsdp == FALSE)
11725 p->sessionversion++;
11726 }
11727
11728 if (add_audio) {
11729 doing_directmedia = (!ast_sockaddr_isnull(&p->redirip) && p->redircodecs) ? TRUE : FALSE;
11730
11731 if ((p->jointcapability & AST_FORMAT_VIDEO_MASK) && !p->novideo) {
11732 if (doing_directmedia && !(p->jointcapability & AST_FORMAT_VIDEO_MASK & p->redircodecs)) {
11733 ast_debug(2, "This call needs video offers, but caller probably did not offer it!\n");
11734 } else if (p->vrtp) {
11735 needvideo = TRUE;
11736 ast_debug(2, "This call needs video offers!\n");
11737 } else {
11738 ast_debug(2, "This call needs video offers, but there's no video support enabled!\n");
11739 }
11740 }
11741
11742 if ((p->jointcapability & AST_FORMAT_TEXT_MASK) && !p->notext) {
11743 if (sipdebug_text)
11744 ast_verbose("We think we can do text\n");
11745 if (p->trtp) {
11746 if (sipdebug_text) {
11747 ast_verbose("And we have a text rtp object\n");
11748 }
11749 needtext = TRUE;
11750 ast_debug(2, "This call needs text offers! \n");
11751 } else {
11752 ast_debug(2, "This call needs text offers, but there's no text support enabled ! \n");
11753 }
11754 }
11755 }
11756
11757 get_our_media_address(p, needvideo, needtext, &addr, &vaddr, &taddr, &dest, &vdest, &tdest);
11758
11759 snprintf(owner, sizeof(owner), "o=%s %d %d IN %s %s\r\n",
11760 ast_strlen_zero(global_sdpowner) ? "-" : global_sdpowner,
11761 p->sessionid, p->sessionversion,
11762 (ast_sockaddr_is_ipv6(&dest) && !ast_sockaddr_is_ipv4_mapped(&dest)) ?
11763 "IP6" : "IP4",
11764 ast_sockaddr_stringify_addr_remote(&dest));
11765
11766 snprintf(connection, sizeof(connection), "c=IN %s %s\r\n",
11767 (ast_sockaddr_is_ipv6(&dest) && !ast_sockaddr_is_ipv4_mapped(&dest)) ?
11768 "IP6" : "IP4",
11769 ast_sockaddr_stringify_addr_remote(&dest));
11770
11771 if (add_audio) {
11772 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR) {
11773 hold = "a=recvonly\r\n";
11774 doing_directmedia = FALSE;
11775 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE) {
11776 hold = "a=inactive\r\n";
11777 doing_directmedia = FALSE;
11778 } else {
11779 hold = "a=sendrecv\r\n";
11780 }
11781
11782 capability = p->jointcapability;
11783
11784
11785 ast_debug(1, "** Our capability: %s Video flag: %s Text flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability),
11786 p->novideo ? "True" : "False", p->notext ? "True" : "False");
11787 ast_debug(1, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
11788
11789 if (doing_directmedia) {
11790 capability &= p->redircodecs;
11791 ast_debug(1, "** Our native-bridge filtered capablity: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability));
11792 }
11793
11794
11795 if (capability & AST_FORMAT_AUDIO_MASK)
11796 needaudio = TRUE;
11797
11798 if (debug) {
11799 ast_verbose("Audio is at %s\n", ast_sockaddr_stringify_port(&addr));
11800 }
11801
11802
11803
11804 if (needvideo) {
11805 get_crypto_attrib(p->vsrtp, &v_a_crypto);
11806 ast_str_append(&m_video, 0, "m=video %d RTP/%s", ast_sockaddr_port(&vdest),
11807 v_a_crypto ? "SAVP" : "AVP");
11808
11809
11810 if (p->maxcallbitrate)
11811 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
11812 if (debug) {
11813 ast_verbose("Video is at %s\n", ast_sockaddr_stringify(&vdest));
11814 }
11815 }
11816
11817
11818
11819 if (needtext) {
11820 if (sipdebug_text)
11821 ast_verbose("Lets set up the text sdp\n");
11822 get_crypto_attrib(p->tsrtp, &t_a_crypto);
11823 ast_str_append(&m_text, 0, "m=text %d RTP/%s", ast_sockaddr_port(&tdest),
11824 t_a_crypto ? "SAVP" : "AVP");
11825 if (debug) {
11826 ast_verbose("Text is at %s\n", ast_sockaddr_stringify(&taddr));
11827 }
11828 }
11829
11830
11831
11832
11833
11834
11835 get_crypto_attrib(p->srtp, &a_crypto);
11836 ast_str_append(&m_audio, 0, "m=audio %d RTP/%s", ast_sockaddr_port(&dest),
11837 a_crypto ? "SAVP" : "AVP");
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848 if ((capability & p->prefcodec) & AST_FORMAT_AUDIO_MASK) {
11849 format_t codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
11850
11851 add_codec_to_sdp(p, codec, &m_audio, &a_audio, debug, &min_audio_packet_size);
11852 alreadysent |= codec;
11853 }
11854
11855
11856 for (x = 0; x < 64; x++) {
11857 format_t codec;
11858
11859 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
11860 break;
11861
11862 if (!(capability & codec))
11863 continue;
11864
11865 if (alreadysent & codec)
11866 continue;
11867
11868 add_codec_to_sdp(p, codec, &m_audio, &a_audio, debug, &min_audio_packet_size);
11869 alreadysent |= codec;
11870 }
11871
11872
11873 for (x = 1ULL; x <= (needtext ? AST_FORMAT_TEXT_MASK : (needvideo ? AST_FORMAT_VIDEO_MASK : AST_FORMAT_AUDIO_MASK)); x <<= 1) {
11874 if (!(capability & x))
11875 continue;
11876
11877 if (alreadysent & x)
11878 continue;
11879
11880 if (x & AST_FORMAT_AUDIO_MASK)
11881 add_codec_to_sdp(p, x, &m_audio, &a_audio, debug, &min_audio_packet_size);
11882 else if (x & AST_FORMAT_VIDEO_MASK)
11883 add_vcodec_to_sdp(p, x, &m_video, &a_video, debug, &min_video_packet_size);
11884 else if (x & AST_FORMAT_TEXT_MASK)
11885 add_tcodec_to_sdp(p, x, &m_text, &a_text, debug, &min_text_packet_size);
11886 }
11887
11888
11889 for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
11890 if (!(p->jointnoncodeccapability & x))
11891 continue;
11892
11893 add_noncodec_to_sdp(p, x, &m_audio, &a_audio, debug);
11894 }
11895
11896 ast_debug(3, "-- Done with adding codecs to SDP\n");
11897
11898 if (!p->owner || p->owner->timingfd == -1) {
11899 ast_str_append(&a_audio, 0, "a=silenceSupp:off - - - -\r\n");
11900 }
11901
11902 if (min_audio_packet_size)
11903 ast_str_append(&a_audio, 0, "a=ptime:%d\r\n", min_audio_packet_size);
11904
11905
11906 if (min_video_packet_size)
11907 ast_str_append(&a_video, 0, "a=ptime:%d\r\n", min_video_packet_size);
11908
11909
11910 if (min_text_packet_size)
11911 ast_str_append(&a_text, 0, "a=ptime:%d\r\n", min_text_packet_size);
11912
11913 if (ast_str_size(m_audio) - ast_str_strlen(m_audio) < 2 || ast_str_size(m_video) - ast_str_strlen(m_video) < 2 ||
11914 ast_str_size(m_text) - ast_str_strlen(m_text) < 2 || ast_str_size(a_text) - ast_str_strlen(a_text) < 2 ||
11915 ast_str_size(a_audio) - ast_str_strlen(a_audio) < 2 || ast_str_size(a_video) - ast_str_strlen(a_video) < 2)
11916 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
11917 }
11918
11919 if (add_t38) {
11920
11921 ast_udptl_get_us(p->udptl, &udptladdr);
11922
11923
11924 ast_sockaddr_copy(&udptldest, &p->ourip);
11925 ast_sockaddr_set_port(&udptldest, ast_sockaddr_port(&udptladdr));
11926
11927 if (debug) {
11928 ast_debug(1, "T.38 UDPTL is at %s port %d\n", ast_sockaddr_stringify_addr(&p->ourip), ast_sockaddr_port(&udptladdr));
11929 }
11930
11931
11932
11933
11934 ast_str_append(&m_modem, 0, "m=image %d udptl t38\r\n", ast_sockaddr_port(&udptldest));
11935
11936 if (ast_sockaddr_cmp(&udptldest, &dest)) {
11937 ast_str_append(&m_modem, 0, "c=IN %s %s\r\n",
11938 (ast_sockaddr_is_ipv6(&udptldest) && !ast_sockaddr_is_ipv4_mapped(&udptldest)) ?
11939 "IP6" : "IP4", ast_sockaddr_stringify_addr_remote(&udptldest));
11940 }
11941
11942 ast_str_append(&a_modem, 0, "a=T38FaxVersion:%u\r\n", p->t38.our_parms.version);
11943 ast_str_append(&a_modem, 0, "a=T38MaxBitRate:%u\r\n", t38_get_rate(p->t38.our_parms.rate));
11944 if (p->t38.our_parms.fill_bit_removal) {
11945 ast_str_append(&a_modem, 0, "a=T38FaxFillBitRemoval\r\n");
11946 }
11947 if (p->t38.our_parms.transcoding_mmr) {
11948 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingMMR\r\n");
11949 }
11950 if (p->t38.our_parms.transcoding_jbig) {
11951 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingJBIG\r\n");
11952 }
11953 switch (p->t38.our_parms.rate_management) {
11954 case AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF:
11955 ast_str_append(&a_modem, 0, "a=T38FaxRateManagement:transferredTCF\r\n");
11956 break;
11957 case AST_T38_RATE_MANAGEMENT_LOCAL_TCF:
11958 ast_str_append(&a_modem, 0, "a=T38FaxRateManagement:localTCF\r\n");
11959 break;
11960 }
11961 ast_str_append(&a_modem, 0, "a=T38FaxMaxDatagram:%u\r\n", ast_udptl_get_local_max_datagram(p->udptl));
11962 switch (ast_udptl_get_error_correction_scheme(p->udptl)) {
11963 case UDPTL_ERROR_CORRECTION_NONE:
11964 break;
11965 case UDPTL_ERROR_CORRECTION_FEC:
11966 ast_str_append(&a_modem, 0, "a=T38FaxUdpEC:t38UDPFEC\r\n");
11967 break;
11968 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
11969 ast_str_append(&a_modem, 0, "a=T38FaxUdpEC:t38UDPRedundancy\r\n");
11970 break;
11971 }
11972 }
11973
11974 if (needaudio)
11975 ast_str_append(&m_audio, 0, "\r\n");
11976 if (needvideo)
11977 ast_str_append(&m_video, 0, "\r\n");
11978 if (needtext)
11979 ast_str_append(&m_text, 0, "\r\n");
11980
11981 add_header(resp, "Content-Type", "application/sdp");
11982 add_content(resp, version);
11983 add_content(resp, owner);
11984 add_content(resp, subject);
11985 add_content(resp, connection);
11986
11987 if (needvideo) {
11988 add_content(resp, bandwidth);
11989 }
11990 add_content(resp, session_time);
11991
11992 if (p->offered_media[SDP_AUDIO].order_offered ||
11993 p->offered_media[SDP_VIDEO].order_offered ||
11994 p->offered_media[SDP_TEXT].order_offered ||
11995 p->offered_media[SDP_IMAGE].order_offered) {
11996 int i;
11997
11998 for (i = 1; i <= 3; i++) {
11999 if (p->offered_media[SDP_AUDIO].order_offered == i) {
12000 if (needaudio) {
12001 add_content(resp, ast_str_buffer(m_audio));
12002 add_content(resp, ast_str_buffer(a_audio));
12003 add_content(resp, hold);
12004 if (a_crypto) {
12005 add_content(resp, a_crypto);
12006 }
12007 } else {
12008 snprintf(dummy_answer, sizeof(dummy_answer), "m=audio 0 RTP/AVP %s\r\n", p->offered_media[SDP_AUDIO].codecs);
12009 add_content(resp, dummy_answer);
12010 }
12011 } else if (p->offered_media[SDP_VIDEO].order_offered == i) {
12012 if (needvideo) {
12013 add_content(resp, ast_str_buffer(m_video));
12014 add_content(resp, ast_str_buffer(a_video));
12015 add_content(resp, hold);
12016 if (v_a_crypto) {
12017 add_content(resp, v_a_crypto);
12018 }
12019 } else {
12020 snprintf(dummy_answer, sizeof(dummy_answer), "m=video 0 RTP/AVP %s\r\n", p->offered_media[SDP_VIDEO].codecs);
12021 add_content(resp, dummy_answer);
12022 }
12023 } else if (p->offered_media[SDP_TEXT].order_offered == i) {
12024 if (needtext) {
12025 add_content(resp, ast_str_buffer(m_text));
12026 add_content(resp, ast_str_buffer(a_text));
12027 add_content(resp, hold);
12028 if (t_a_crypto) {
12029 add_content(resp, t_a_crypto);
12030 }
12031 } else {
12032 snprintf(dummy_answer, sizeof(dummy_answer), "m=text 0 RTP/AVP %s\r\n", p->offered_media[SDP_TEXT].codecs);
12033 add_content(resp, dummy_answer);
12034 }
12035 } else if (p->offered_media[SDP_IMAGE].order_offered == i) {
12036 if (add_t38) {
12037 add_content(resp, ast_str_buffer(m_modem));
12038 add_content(resp, ast_str_buffer(a_modem));
12039 } else {
12040 add_content(resp, "m=image 0 udptl t38\r\n");
12041 }
12042 }
12043 }
12044 } else {
12045
12046 if (needaudio) {
12047 add_content(resp, ast_str_buffer(m_audio));
12048 add_content(resp, ast_str_buffer(a_audio));
12049 add_content(resp, hold);
12050 if (a_crypto) {
12051 add_content(resp, a_crypto);
12052 }
12053 }
12054 if (needvideo) {
12055 add_content(resp, ast_str_buffer(m_video));
12056 add_content(resp, ast_str_buffer(a_video));
12057 add_content(resp, hold);
12058 if (v_a_crypto) {
12059 add_content(resp, v_a_crypto);
12060 }
12061 }
12062 if (needtext) {
12063 add_content(resp, ast_str_buffer(m_text));
12064 add_content(resp, ast_str_buffer(a_text));
12065 add_content(resp, hold);
12066 if (t_a_crypto) {
12067 add_content(resp, t_a_crypto);
12068 }
12069 }
12070 if (add_t38) {
12071 add_content(resp, ast_str_buffer(m_modem));
12072 add_content(resp, ast_str_buffer(a_modem));
12073 }
12074 }
12075
12076
12077 p->lastrtprx = p->lastrtptx = time(NULL);
12078
12079 ast_debug(3, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
12080
12081 return AST_SUCCESS;
12082 }
12083
12084
12085 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
12086 {
12087 struct sip_request resp;
12088 uint32_t seqno;
12089
12090 if (sscanf(get_header(req, "CSeq"), "%30u ", &seqno) != 1) {
12091 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
12092 return -1;
12093 }
12094 respprep(&resp, p, msg, req);
12095 if (p->udptl) {
12096 add_sdp(&resp, p, 0, 0, 1);
12097 } else
12098 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
12099 if (retrans && !p->pendinginvite)
12100 p->pendinginvite = seqno;
12101 return send_response(p, &resp, retrans, seqno);
12102 }
12103
12104
12105 static void copy_request(struct sip_request *dst, const struct sip_request *src)
12106 {
12107
12108
12109
12110 struct ast_str *duplicate = dst->data;
12111 struct ast_str *duplicate_content = dst->content;
12112
12113
12114
12115 *dst = *src;
12116 dst->data = duplicate;
12117 dst->content = duplicate_content;
12118
12119
12120 if (!dst->data && !(dst->data = ast_str_create(ast_str_strlen(src->data) + 1))) {
12121 return;
12122 }
12123 ast_str_copy_string(&dst->data, src->data);
12124
12125
12126 if (src->content) {
12127 if (!dst->content && !(dst->content = ast_str_create(ast_str_strlen(src->content) + 1))) {
12128 return;
12129 }
12130 ast_str_copy_string(&dst->content, src->content);
12131 }
12132 }
12133
12134 static void add_cc_call_info_to_response(struct sip_pvt *p, struct sip_request *resp)
12135 {
12136 char uri[SIPBUFSIZE];
12137 struct ast_str *header = ast_str_alloca(SIPBUFSIZE);
12138 struct ast_cc_agent *agent = find_sip_cc_agent_by_original_callid(p);
12139 struct sip_cc_agent_pvt *agent_pvt;
12140
12141 if (!agent) {
12142
12143
12144
12145 ast_log(LOG_WARNING, "Can't find SIP CC agent for call '%s' even though OFFER_CC flag was set?\n", p->callid);
12146 return;
12147 }
12148
12149 agent_pvt = agent->private_data;
12150
12151 if (!ast_strlen_zero(agent_pvt->subscribe_uri)) {
12152 ast_copy_string(uri, agent_pvt->subscribe_uri, sizeof(uri));
12153 } else {
12154 generate_uri(p, uri, sizeof(uri));
12155 ast_copy_string(agent_pvt->subscribe_uri, uri, sizeof(agent_pvt->subscribe_uri));
12156 }
12157
12158
12159
12160
12161 ast_str_set(&header, 0, "<%s>;purpose=call-completion;m=%s", uri, "NR");
12162 add_header(resp, "Call-Info", ast_str_buffer(header));
12163 ao2_ref(agent, -1);
12164 }
12165
12166
12167
12168
12169 static int transmit_response_with_sdp(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable, int oldsdp, int rpid)
12170 {
12171 struct sip_request resp;
12172 uint32_t seqno;
12173 if (sscanf(get_header(req, "CSeq"), "%30u ", &seqno) != 1) {
12174 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
12175 return -1;
12176 }
12177 respprep(&resp, p, msg, req);
12178 if (rpid == TRUE) {
12179 add_rpid(&resp, p);
12180 }
12181 if (ast_test_flag(&p->flags[0], SIP_OFFER_CC)) {
12182 add_cc_call_info_to_response(p, &resp);
12183 }
12184 if (p->rtp) {
12185 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
12186 ast_debug(1, "Setting framing from config on incoming call\n");
12187 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
12188 }
12189 ast_rtp_instance_activate(p->rtp);
12190 try_suggested_sip_codec(p);
12191 if (p->t38.state == T38_ENABLED) {
12192 add_sdp(&resp, p, oldsdp, TRUE, TRUE);
12193 } else {
12194 add_sdp(&resp, p, oldsdp, TRUE, FALSE);
12195 }
12196 } else
12197 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
12198 if (reliable && !p->pendinginvite)
12199 p->pendinginvite = seqno;
12200 add_required_respheader(&resp);
12201 return send_response(p, &resp, reliable, seqno);
12202 }
12203
12204
12205 static int determine_firstline_parts(struct sip_request *req)
12206 {
12207 char *e = ast_skip_blanks(req->data->str);
12208 char *local_rlPart1;
12209
12210 if (!*e)
12211 return -1;
12212 req->rlPart1 = e - req->data->str;
12213 local_rlPart1 = e;
12214 e = ast_skip_nonblanks(e);
12215 if (*e)
12216 *e++ = '\0';
12217
12218 e = ast_skip_blanks(e);
12219 if ( !*e )
12220 return -1;
12221 ast_trim_blanks(e);
12222
12223 if (!strcasecmp(local_rlPart1, "SIP/2.0") ) {
12224 if (strlen(e) < 3)
12225 return -1;
12226 req->rlPart2 = e - req->data->str;
12227 } else {
12228 if ( *e == '<' ) {
12229 ast_debug(3, "Oops. Bogus uri in <> %s\n", e);
12230 e++;
12231 if (!*e)
12232 return -1;
12233 }
12234 req->rlPart2 = e - req->data->str;
12235 e = ast_skip_nonblanks(e);
12236 if (*e)
12237 *e++ = '\0';
12238 e = ast_skip_blanks(e);
12239 if (strcasecmp(e, "SIP/2.0") ) {
12240 ast_debug(3, "Skipping packet - Bad request protocol %s\n", e);
12241 return -1;
12242 }
12243 }
12244 return 1;
12245 }
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp)
12261 {
12262 struct sip_request req;
12263
12264 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
12265
12266 add_header(&req, "Allow", ALLOWED_METHODS);
12267 add_supported_header(p, &req);
12268 if (sipdebug) {
12269 if (oldsdp == TRUE)
12270 add_header(&req, "X-asterisk-Info", "SIP re-invite (Session-Timers)");
12271 else
12272 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
12273 }
12274
12275 if (ast_test_flag(&p->flags[0], SIP_SENDRPID))
12276 add_rpid(&req, p);
12277
12278 if (p->do_history) {
12279 append_history(p, "ReInv", "Re-invite sent");
12280 }
12281 memset(p->offered_media, 0, sizeof(p->offered_media));
12282
12283 try_suggested_sip_codec(p);
12284 if (t38version) {
12285 add_sdp(&req, p, oldsdp, FALSE, TRUE);
12286 } else {
12287 add_sdp(&req, p, oldsdp, TRUE, FALSE);
12288 }
12289
12290
12291 initialize_initreq(p, &req);
12292 p->lastinvite = p->ocseq;
12293 ast_set_flag(&p->flags[0], SIP_OUTGOING);
12294 p->ongoing_reinvite = 1;
12295 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
12296 }
12297
12298
12299 static char *remove_uri_parameters(char *uri)
12300 {
12301 char *atsign;
12302 atsign = strchr(uri, '@');
12303 if (!atsign) {
12304 atsign = uri;
12305 }
12306 atsign = strchr(atsign, ';');
12307 if (atsign)
12308 *atsign = '\0';
12309 return uri;
12310 }
12311
12312
12313 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
12314 {
12315 char stripped[SIPBUFSIZE];
12316 char *c;
12317
12318 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
12319 c = get_in_brackets(stripped);
12320
12321 c = remove_uri_parameters(c);
12322 if (!ast_strlen_zero(c)) {
12323 ast_string_field_set(p, uri, c);
12324 }
12325
12326 }
12327
12328
12329 static void build_contact(struct sip_pvt *p)
12330 {
12331 char tmp[SIPBUFSIZE];
12332 char *user = ast_uri_encode(p->exten, tmp, sizeof(tmp), 0);
12333
12334 if (p->socket.type == SIP_TRANSPORT_UDP) {
12335 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", user,
12336 ast_strlen_zero(user) ? "" : "@", ast_sockaddr_stringify_remote(&p->ourip));
12337 } else {
12338 ast_string_field_build(p, our_contact, "<sip:%s%s%s;transport=%s>", user,
12339 ast_strlen_zero(user) ? "" : "@", ast_sockaddr_stringify_remote(&p->ourip),
12340 get_transport(p->socket.type));
12341 }
12342 }
12343
12344
12345 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, const char * const explicit_uri)
12346 {
12347 struct ast_str *invite = ast_str_alloca(256);
12348 char from[256];
12349 char to[256];
12350 char tmp_n[SIPBUFSIZE/2];
12351 char tmp_l[SIPBUFSIZE/2];
12352 const char *l = NULL;
12353 const char *n = NULL;
12354 const char *d = NULL;
12355 const char *urioptions = "";
12356 int ourport;
12357
12358 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
12359 const char *s = p->username;
12360
12361
12362
12363
12364
12365
12366 if (*s == '+')
12367 s++;
12368 for (; *s; s++) {
12369 if (!strchr(AST_DIGIT_ANYNUM, *s) )
12370 break;
12371 }
12372
12373 if (!*s)
12374 urioptions = ";user=phone";
12375 }
12376
12377
12378 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
12379
12380 if (ast_strlen_zero(p->fromdomain)) {
12381 d = ast_sockaddr_stringify_host_remote(&p->ourip);
12382 }
12383 if (p->owner) {
12384 if ((ast_party_id_presentation(&p->owner->connected.id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
12385 l = p->owner->connected.id.number.valid ? p->owner->connected.id.number.str : NULL;
12386 n = p->owner->connected.id.name.valid ? p->owner->connected.id.name.str : NULL;
12387 } else {
12388
12389
12390 l = "anonymous";
12391 n = CALLERID_UNKNOWN;
12392 d = FROMDOMAIN_INVALID;
12393 }
12394 }
12395
12396
12397
12398
12399
12400
12401
12402 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->mwi_from)) {
12403 l = p->mwi_from;
12404 }
12405
12406 if (ast_strlen_zero(l))
12407 l = default_callerid;
12408 if (ast_strlen_zero(n))
12409 n = l;
12410
12411 if (!ast_strlen_zero(p->fromuser))
12412 l = p->fromuser;
12413 else
12414 ast_string_field_set(p, fromuser, l);
12415
12416
12417 if (!ast_strlen_zero(p->fromname))
12418 n = p->fromname;
12419 else
12420 ast_string_field_set(p, fromname, n);
12421
12422
12423 if (!ast_strlen_zero(p->fromdomain))
12424 d = p->fromdomain;
12425 else
12426 ast_string_field_set(p, fromdomain, d);
12427
12428 ast_copy_string(tmp_l, l, sizeof(tmp_l));
12429 if (sip_cfg.pedanticsipchecking) {
12430 ast_escape_quoted(n, tmp_n, sizeof(tmp_n));
12431 n = tmp_n;
12432 ast_uri_encode(l, tmp_l, sizeof(tmp_l), 0);
12433 }
12434
12435 ourport = (p->fromdomainport && (p->fromdomainport != STANDARD_SIP_PORT)) ? p->fromdomainport : ast_sockaddr_port(&p->ourip);
12436 if (!sip_standard_port(p->socket.type, ourport)) {
12437 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, tmp_l, d, ourport, p->tag);
12438 } else {
12439 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, tmp_l, d, p->tag);
12440 }
12441
12442 if (!ast_strlen_zero(explicit_uri)) {
12443 ast_str_set(&invite, 0, "%s", explicit_uri);
12444 } else {
12445
12446 if (!ast_strlen_zero(p->fullcontact)) {
12447
12448 ast_str_append(&invite, 0, "%s", p->fullcontact);
12449 } else {
12450
12451 ast_str_append(&invite, 0, "sip:");
12452 if (!ast_strlen_zero(p->username)) {
12453 n = p->username;
12454 if (sip_cfg.pedanticsipchecking) {
12455 ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
12456 n = tmp_n;
12457 }
12458 ast_str_append(&invite, 0, "%s@", n);
12459 }
12460 ast_str_append(&invite, 0, "%s", p->tohost);
12461 if (p->portinuri) {
12462 ast_str_append(&invite, 0, ":%d", ast_sockaddr_port(&p->sa));
12463 }
12464 ast_str_append(&invite, 0, "%s", urioptions);
12465 }
12466 }
12467
12468
12469 if (p->options && !ast_strlen_zero(p->options->uri_options))
12470 ast_str_append(&invite, 0, ";%s", p->options->uri_options);
12471
12472
12473
12474
12475 ast_string_field_set(p, uri, ast_str_buffer(invite));
12476
12477 if (!ast_strlen_zero(p->todnid)) {
12478
12479 if (!strchr(p->todnid, '@')) {
12480
12481 snprintf(to, sizeof(to), "<sip:%s@%s>%s%s", p->todnid, p->tohost, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
12482 } else {
12483 snprintf(to, sizeof(to), "<sip:%s>%s%s", p->todnid, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
12484 }
12485 } else {
12486 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
12487
12488 snprintf(to, sizeof(to), "<%s%s>;tag=%s", (strncasecmp(p->uri, "sip:", 4) ? "sip:" : ""), p->uri, p->theirtag);
12489 } else if (p->options && p->options->vxml_url) {
12490
12491 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
12492 } else {
12493 snprintf(to, sizeof(to), "<%s>", p->uri);
12494 }
12495 }
12496
12497 init_req(req, sipmethod, p->uri);
12498
12499 snprintf(tmp_n, sizeof(tmp_n), "%u %s", ++p->ocseq, sip_methods[sipmethod].text);
12500
12501 add_header(req, "Via", p->via);
12502 add_header_max_forwards(p, req);
12503
12504
12505
12506
12507 add_route(req, p->route);
12508
12509 add_header(req, "From", from);
12510 add_header(req, "To", to);
12511 ast_string_field_set(p, exten, l);
12512 build_contact(p);
12513 add_header(req, "Contact", p->our_contact);
12514 add_header(req, "Call-ID", p->callid);
12515 add_header(req, "CSeq", tmp_n);
12516 if (!ast_strlen_zero(global_useragent)) {
12517 add_header(req, "User-Agent", global_useragent);
12518 }
12519 }
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529 static void add_diversion_header(struct sip_request *req, struct sip_pvt *pvt)
12530 {
12531 const char *diverting_number;
12532 const char *diverting_name;
12533 const char *reason;
12534 char header_text[256];
12535
12536 if (!pvt->owner) {
12537 return;
12538 }
12539
12540 diverting_number = pvt->owner->redirecting.from.number.str;
12541 if (!pvt->owner->redirecting.from.number.valid
12542 || ast_strlen_zero(diverting_number)) {
12543 return;
12544 }
12545
12546 reason = sip_reason_code_to_str(pvt->owner->redirecting.reason);
12547
12548
12549 diverting_name = pvt->owner->redirecting.from.name.str;
12550 if (!pvt->owner->redirecting.from.name.valid
12551 || ast_strlen_zero(diverting_name)) {
12552 snprintf(header_text, sizeof(header_text), "<sip:%s@%s>;reason=%s", diverting_number,
12553 ast_sockaddr_stringify_host_remote(&pvt->ourip), reason);
12554 } else {
12555 char diverting_name_buf[128];
12556
12557 ast_escape_quoted(diverting_name, diverting_name_buf, sizeof(diverting_name_buf));
12558 snprintf(header_text, sizeof(header_text), "\"%s\" <sip:%s@%s>;reason=%s",
12559 diverting_name_buf, diverting_number,
12560 ast_sockaddr_stringify_host_remote(&pvt->ourip), reason);
12561 }
12562
12563 add_header(req, "Diversion", header_text);
12564 }
12565
12566 static int transmit_publish(struct sip_epa_entry *epa_entry, enum sip_publish_type publish_type, const char * const explicit_uri)
12567 {
12568 struct sip_pvt *pvt;
12569 int expires;
12570
12571 epa_entry->publish_type = publish_type;
12572
12573 if (!(pvt = sip_alloc(NULL, NULL, 0, SIP_PUBLISH, NULL))) {
12574 return -1;
12575 }
12576
12577 sip_pvt_lock(pvt);
12578
12579 if (create_addr(pvt, epa_entry->destination, NULL, TRUE)) {
12580 sip_pvt_unlock(pvt);
12581 dialog_unlink_all(pvt);
12582 dialog_unref(pvt, "create_addr failed in transmit_publish. Unref dialog");
12583 return -1;
12584 }
12585 ast_sip_ouraddrfor(&pvt->sa, &pvt->ourip, pvt);
12586 ast_set_flag(&pvt->flags[0], SIP_OUTGOING);
12587 expires = (publish_type == SIP_PUBLISH_REMOVE) ? 0 : DEFAULT_PUBLISH_EXPIRES;
12588 pvt->expiry = expires;
12589
12590
12591 ao2_ref(epa_entry, +1);
12592 pvt->epa_entry = epa_entry;
12593
12594 transmit_invite(pvt, SIP_PUBLISH, FALSE, 2, explicit_uri);
12595 sip_pvt_unlock(pvt);
12596 sip_scheddestroy(pvt, DEFAULT_TRANS_TIMEOUT);
12597 dialog_unref(pvt, "Done with the sip_pvt allocated for transmitting PUBLISH");
12598 return 0;
12599 }
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init, const char * const explicit_uri)
12611 {
12612 struct sip_request req;
12613 struct ast_variable *var;
12614
12615 if (init) {
12616 p->branch ^= ast_random();
12617 p->invite_branch = p->branch;
12618 build_via(p);
12619 }
12620 if (init > 1) {
12621 initreqprep(&req, p, sipmethod, explicit_uri);
12622 } else {
12623
12624 reqprep(&req, p, sipmethod, 0, init ? 0 : 1);
12625 }
12626
12627 if (p->options && p->options->auth) {
12628 add_header(&req, p->options->authheader, p->options->auth);
12629 }
12630 append_date(&req);
12631 if (sipmethod == SIP_REFER) {
12632 if (p->refer) {
12633 char buf[SIPBUFSIZE];
12634 if (!ast_strlen_zero(p->refer->refer_to)) {
12635 add_header(&req, "Refer-To", p->refer->refer_to);
12636 }
12637 if (!ast_strlen_zero(p->refer->referred_by)) {
12638 snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
12639 add_header(&req, "Referred-By", buf);
12640 }
12641 }
12642 } else if (sipmethod == SIP_SUBSCRIBE) {
12643 char buf[SIPBUFSIZE];
12644 if (p->subscribed == MWI_NOTIFICATION) {
12645 add_header(&req, "Event", "message-summary");
12646 add_header(&req, "Accept", "application/simple-message-summary");
12647 } else if (p->subscribed == CALL_COMPLETION) {
12648 add_header(&req, "Event", "call-completion");
12649 add_header(&req, "Accept", "application/call-completion");
12650 }
12651 snprintf(buf, sizeof(buf), "%d", p->expiry);
12652 add_header(&req, "Expires", buf);
12653 }
12654
12655
12656
12657 if (p->options && !ast_strlen_zero(p->options->replaces)) {
12658 add_header(&req, "Replaces", p->options->replaces);
12659 add_header(&req, "Require", "replaces");
12660 }
12661
12662
12663 if (st_get_mode(p, 0) == SESSION_TIMER_MODE_ORIGINATE
12664 || (st_get_mode(p, 0) == SESSION_TIMER_MODE_ACCEPT
12665 && st_get_se(p, FALSE) != DEFAULT_MIN_SE)) {
12666 char i2astr[10];
12667
12668 if (!p->stimer->st_interval) {
12669 p->stimer->st_interval = st_get_se(p, TRUE);
12670 }
12671
12672 p->stimer->st_active = TRUE;
12673 if (st_get_mode(p, 0) == SESSION_TIMER_MODE_ORIGINATE) {
12674 snprintf(i2astr, sizeof(i2astr), "%d", p->stimer->st_interval);
12675 add_header(&req, "Session-Expires", i2astr);
12676 }
12677
12678 snprintf(i2astr, sizeof(i2astr), "%d", st_get_se(p, FALSE));
12679 add_header(&req, "Min-SE", i2astr);
12680 }
12681
12682 add_header(&req, "Allow", ALLOWED_METHODS);
12683 add_supported_header(p, &req);
12684
12685 if (p->options && p->options->addsipheaders && p->owner) {
12686 struct ast_channel *chan = p->owner;
12687 struct varshead *headp;
12688
12689 ast_channel_lock(chan);
12690
12691 headp = &chan->varshead;
12692
12693 if (!headp) {
12694 ast_log(LOG_WARNING, "No Headp for the channel...ooops!\n");
12695 } else {
12696 const struct ast_var_t *current;
12697 AST_LIST_TRAVERSE(headp, current, entries) {
12698
12699 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
12700 char *content, *end;
12701 const char *header = ast_var_value(current);
12702 char *headdup = ast_strdupa(header);
12703
12704
12705 if (*headdup == '"') {
12706 headdup++;
12707 }
12708 if ((content = strchr(headdup, ':'))) {
12709 *content++ = '\0';
12710 content = ast_skip_blanks(content);
12711
12712 end = content + strlen(content) -1;
12713 if (*end == '"') {
12714 *end = '\0';
12715 }
12716
12717 add_header(&req, headdup, content);
12718 if (sipdebug) {
12719 ast_debug(1, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
12720 }
12721 }
12722 }
12723 }
12724 }
12725
12726 ast_channel_unlock(chan);
12727 }
12728 if ((sipmethod == SIP_INVITE || sipmethod == SIP_UPDATE) && ast_test_flag(&p->flags[0], SIP_SENDRPID))
12729 add_rpid(&req, p);
12730 if (sipmethod == SIP_INVITE) {
12731 add_diversion_header(&req, p);
12732 }
12733 if (sdp) {
12734 memset(p->offered_media, 0, sizeof(p->offered_media));
12735 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
12736 ast_debug(1, "T38 is in state %u on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
12737 add_sdp(&req, p, FALSE, FALSE, TRUE);
12738 } else if (p->rtp) {
12739 try_suggested_sip_codec(p);
12740 add_sdp(&req, p, FALSE, TRUE, FALSE);
12741 }
12742 } else if (p->notify) {
12743 for (var = p->notify->headers; var; var = var->next) {
12744 add_header(&req, var->name, var->value);
12745 }
12746 if (ast_str_strlen(p->notify->content)) {
12747 add_content(&req, ast_str_buffer(p->notify->content));
12748 }
12749 } else if (sipmethod == SIP_PUBLISH) {
12750 char expires[SIPBUFSIZE];
12751
12752 switch (p->epa_entry->static_data->event) {
12753 case CALL_COMPLETION:
12754 snprintf(expires, sizeof(expires), "%d", p->expiry);
12755 add_header(&req, "Event", "call-completion");
12756 add_header(&req, "Expires", expires);
12757 if (p->epa_entry->publish_type != SIP_PUBLISH_INITIAL) {
12758 add_header(&req, "SIP-If-Match", p->epa_entry->entity_tag);
12759 }
12760
12761 if (!ast_strlen_zero(p->epa_entry->body)) {
12762 add_header(&req, "Content-Type", "application/pidf+xml");
12763 add_content(&req, p->epa_entry->body);
12764 }
12765 default:
12766 break;
12767 }
12768 }
12769
12770 if (!p->initreq.headers || init > 2) {
12771 initialize_initreq(p, &req);
12772 }
12773 if (sipmethod == SIP_INVITE || sipmethod == SIP_SUBSCRIBE) {
12774 p->lastinvite = p->ocseq;
12775 }
12776 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
12777 }
12778
12779
12780 static int sip_subscribe_mwi_do(const void *data)
12781 {
12782 struct sip_subscription_mwi *mwi = (struct sip_subscription_mwi*)data;
12783
12784 if (!mwi) {
12785 return -1;
12786 }
12787
12788 mwi->resub = -1;
12789 __sip_subscribe_mwi_do(mwi);
12790 ASTOBJ_UNREF(mwi, sip_subscribe_mwi_destroy);
12791
12792 return 0;
12793 }
12794
12795 static void on_dns_update_registry(struct ast_sockaddr *old, struct ast_sockaddr *new, void *data)
12796 {
12797 struct sip_registry *reg = data;
12798 const char *old_str;
12799
12800
12801 if (ast_sockaddr_isnull(new)) {
12802 ast_debug(1, "Empty sockaddr change...ignoring!\n");
12803 return;
12804 }
12805
12806 if (!ast_sockaddr_port(new)) {
12807 ast_sockaddr_set_port(new, reg->portno);
12808 }
12809
12810 old_str = ast_strdupa(ast_sockaddr_stringify(old));
12811
12812 ast_debug(1, "Changing registry %s from %s to %s\n", S_OR(reg->peername, reg->hostname), old_str, ast_sockaddr_stringify(new));
12813 ast_sockaddr_copy(®->us, new);
12814 }
12815
12816 static void on_dns_update_peer(struct ast_sockaddr *old, struct ast_sockaddr *new, void *data)
12817 {
12818 struct sip_peer *peer = data;
12819 const char *old_str;
12820
12821
12822 if (ast_sockaddr_isnull(new)) {
12823 ast_debug(1, "Empty sockaddr change...ignoring!\n");
12824 return;
12825 }
12826
12827 if (!ast_sockaddr_isnull(&peer->addr)) {
12828 ao2_unlink(peers_by_ip, peer);
12829 }
12830
12831 if (!ast_sockaddr_port(new)) {
12832 ast_sockaddr_set_port(new, default_sip_port(peer->socket.type));
12833 }
12834
12835 old_str = ast_strdupa(ast_sockaddr_stringify(old));
12836 ast_debug(1, "Changing peer %s address from %s to %s\n", peer->name, old_str, ast_sockaddr_stringify(new));
12837
12838 ao2_lock(peer);
12839 ast_sockaddr_copy(&peer->addr, new);
12840 ao2_unlock(peer);
12841
12842 ao2_link(peers_by_ip, peer);
12843 }
12844
12845 static void on_dns_update_mwi(struct ast_sockaddr *old, struct ast_sockaddr *new, void *data)
12846 {
12847 struct sip_subscription_mwi *mwi = data;
12848 const char *old_str;
12849
12850
12851 if (ast_sockaddr_isnull(new)) {
12852 ast_debug(1, "Empty sockaddr change...ignoring!\n");
12853 return;
12854 }
12855
12856 old_str = ast_strdupa(ast_sockaddr_stringify(old));
12857 ast_debug(1, "Changing mwi %s from %s to %s\n", mwi->hostname, old_str, ast_sockaddr_stringify(new));
12858 ast_sockaddr_copy(&mwi->us, new);
12859 }
12860
12861
12862 static int __sip_subscribe_mwi_do(struct sip_subscription_mwi *mwi)
12863 {
12864
12865 if (!mwi->dnsmgr) {
12866 char transport[MAXHOSTNAMELEN];
12867 struct sip_subscription_mwi *saved;
12868 snprintf(transport, sizeof(transport), "_%s._%s", get_srv_service(mwi->transport), get_srv_protocol(mwi->transport));
12869
12870 mwi->us.ss.ss_family = get_address_family_filter(mwi->transport);
12871 saved = ASTOBJ_REF(mwi);
12872 ast_dnsmgr_lookup_cb(mwi->hostname, &mwi->us, &mwi->dnsmgr, sip_cfg.srvlookup ? transport : NULL, on_dns_update_mwi, saved);
12873 if (!mwi->dnsmgr) {
12874 ASTOBJ_UNREF(saved, sip_subscribe_mwi_destroy);
12875 }
12876 }
12877
12878
12879 if (mwi->call) {
12880 transmit_invite(mwi->call, SIP_SUBSCRIBE, 0, 0, NULL);
12881 return 0;
12882 }
12883
12884
12885 if (!(mwi->call = sip_alloc(NULL, NULL, 0, SIP_SUBSCRIBE, NULL))) {
12886 return -1;
12887 }
12888
12889 ref_proxy(mwi->call, obproxy_get(mwi->call, NULL));
12890
12891 if (!ast_sockaddr_port(&mwi->us) && mwi->portno) {
12892 ast_sockaddr_set_port(&mwi->us, mwi->portno);
12893 }
12894
12895
12896 if (create_addr(mwi->call, mwi->hostname, &mwi->us, 0)) {
12897 dialog_unlink_all(mwi->call);
12898 mwi->call = dialog_unref(mwi->call, "unref dialog after unlink_all");
12899 return 0;
12900 }
12901
12902 mwi->call->expiry = mwi_expiry;
12903
12904 if (!mwi->dnsmgr && mwi->portno) {
12905 ast_sockaddr_set_port(&mwi->call->sa, mwi->portno);
12906 ast_sockaddr_set_port(&mwi->call->recv, mwi->portno);
12907 } else {
12908 mwi->portno = ast_sockaddr_port(&mwi->call->sa);
12909 }
12910
12911
12912 if (!ast_strlen_zero(mwi->authuser)) {
12913 ast_string_field_set(mwi->call, peername, mwi->authuser);
12914 ast_string_field_set(mwi->call, authname, mwi->authuser);
12915 ast_string_field_set(mwi->call, fromuser, mwi->authuser);
12916 } else {
12917 ast_string_field_set(mwi->call, peername, mwi->username);
12918 ast_string_field_set(mwi->call, authname, mwi->username);
12919 ast_string_field_set(mwi->call, fromuser, mwi->username);
12920 }
12921 ast_string_field_set(mwi->call, username, mwi->username);
12922 if (!ast_strlen_zero(mwi->secret)) {
12923 ast_string_field_set(mwi->call, peersecret, mwi->secret);
12924 }
12925 set_socket_transport(&mwi->call->socket, mwi->transport);
12926 mwi->call->socket.port = htons(mwi->portno);
12927 ast_sip_ouraddrfor(&mwi->call->sa, &mwi->call->ourip, mwi->call);
12928 build_contact(mwi->call);
12929 build_via(mwi->call);
12930
12931
12932 change_callid_pvt(mwi->call, NULL);
12933
12934 ast_set_flag(&mwi->call->flags[0], SIP_OUTGOING);
12935
12936
12937 mwi->call->mwi = ASTOBJ_REF(mwi);
12938
12939 mwi->call->subscribed = MWI_NOTIFICATION;
12940
12941
12942 transmit_invite(mwi->call, SIP_SUBSCRIBE, 0, 2, NULL);
12943
12944 return 0;
12945 }
12946
12947
12948 static int find_calling_channel(void *obj, void *arg, void *data, int flags)
12949 {
12950 struct ast_channel *c = obj;
12951 struct sip_pvt *p = data;
12952 int res;
12953
12954 ast_channel_lock(c);
12955
12956 res = (c->pbx &&
12957 (!strcasecmp(c->macroexten, p->exten) || !strcasecmp(c->exten, p->exten)) &&
12958 (sip_cfg.notifycid == IGNORE_CONTEXT || !strcasecmp(c->context, p->context)));
12959
12960 ast_channel_unlock(c);
12961
12962 return res ? CMP_MATCH | CMP_STOP : 0;
12963 }
12964
12965
12966 static void state_notify_build_xml(int state, int full, const char *exten, const char *context, struct ast_str **tmp, struct sip_pvt *p, int subscribed, const char *mfrom, const char *mto)
12967 {
12968 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
12969 const char *statestring = "terminated";
12970 const char *pidfstate = "--";
12971 const char *pidfnote= "Ready";
12972 char hint[AST_MAX_EXTENSION];
12973
12974 switch (state) {
12975 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
12976 statestring = (sip_cfg.notifyringing) ? "early" : "confirmed";
12977 local_state = NOTIFY_INUSE;
12978 pidfstate = "busy";
12979 pidfnote = "Ringing";
12980 break;
12981 case AST_EXTENSION_RINGING:
12982 statestring = "early";
12983 local_state = NOTIFY_INUSE;
12984 pidfstate = "busy";
12985 pidfnote = "Ringing";
12986 break;
12987 case AST_EXTENSION_INUSE:
12988 statestring = "confirmed";
12989 local_state = NOTIFY_INUSE;
12990 pidfstate = "busy";
12991 pidfnote = "On the phone";
12992 break;
12993 case AST_EXTENSION_BUSY:
12994 statestring = "confirmed";
12995 local_state = NOTIFY_CLOSED;
12996 pidfstate = "busy";
12997 pidfnote = "On the phone";
12998 break;
12999 case AST_EXTENSION_UNAVAILABLE:
13000 statestring = "terminated";
13001 local_state = NOTIFY_CLOSED;
13002 pidfstate = "away";
13003 pidfnote = "Unavailable";
13004 break;
13005 case AST_EXTENSION_ONHOLD:
13006 statestring = "confirmed";
13007 local_state = NOTIFY_CLOSED;
13008 pidfstate = "busy";
13009 pidfnote = "On hold";
13010 break;
13011 case AST_EXTENSION_NOT_INUSE:
13012 default:
13013
13014 break;
13015 }
13016
13017
13018 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, context, exten)) {
13019 char *hint2 = hint, *individual_hint = NULL;
13020 int hint_count = 0, unavailable_count = 0;
13021
13022 while ((individual_hint = strsep(&hint2, "&"))) {
13023 hint_count++;
13024
13025 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE)
13026 unavailable_count++;
13027 }
13028
13029
13030
13031
13032 if (hint_count > 0 && hint_count == unavailable_count) {
13033 local_state = NOTIFY_CLOSED;
13034 pidfstate = "away";
13035 pidfnote = "Not online";
13036 }
13037 }
13038
13039 switch (subscribed) {
13040 case XPIDF_XML:
13041 case CPIM_PIDF_XML:
13042 ast_str_append(tmp, 0,
13043 "<?xml version=\"1.0\"?>\n"
13044 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
13045 "<presence>\n");
13046 ast_str_append(tmp, 0, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
13047 ast_str_append(tmp, 0, "<atom id=\"%s\">\n", exten);
13048 ast_str_append(tmp, 0, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
13049 ast_str_append(tmp, 0, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
13050 ast_str_append(tmp, 0, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
13051 ast_str_append(tmp, 0, "</address>\n</atom>\n</presence>\n");
13052 break;
13053 case PIDF_XML:
13054 ast_str_append(tmp, 0,
13055 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
13056 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \nxmlns:pp=\"urn:ietf:params:xml:ns:pidf:person\"\nxmlns:es=\"urn:ietf:params:xml:ns:pidf:rpid:status:rpid-status\"\nxmlns:ep=\"urn:ietf:params:xml:ns:pidf:rpid:rpid-person\"\nentity=\"%s\">\n", mfrom);
13057 ast_str_append(tmp, 0, "<pp:person><status>\n");
13058 if (pidfstate[0] != '-') {
13059 ast_str_append(tmp, 0, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
13060 }
13061 ast_str_append(tmp, 0, "</status></pp:person>\n");
13062 ast_str_append(tmp, 0, "<note>%s</note>\n", pidfnote);
13063 ast_str_append(tmp, 0, "<tuple id=\"%s\">\n", exten);
13064 ast_str_append(tmp, 0, "<contact priority=\"1\">%s</contact>\n", mto);
13065 if (pidfstate[0] == 'b')
13066 ast_str_append(tmp, 0, "<status><basic>open</basic></status>\n");
13067 else
13068 ast_str_append(tmp, 0, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
13069 ast_str_append(tmp, 0, "</tuple>\n</presence>\n");
13070 break;
13071 case DIALOG_INFO_XML:
13072 ast_str_append(tmp, 0, "<?xml version=\"1.0\"?>\n");
13073 ast_str_append(tmp, 0, "<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" version=\"%u\" state=\"%s\" entity=\"%s\">\n", p->dialogver, full ? "full" : "partial", mto);
13074
13075 if ((state & AST_EXTENSION_RINGING) && sip_cfg.notifyringing) {
13076
13077 char local_display[AST_MAX_EXTENSION * 2];
13078 char remote_display[AST_MAX_EXTENSION * 2];
13079 char *local_target = ast_strdupa(mto);
13080
13081
13082
13083
13084
13085
13086
13087
13088 char *remote_target = ast_strdupa(mto);
13089
13090 ast_xml_escape(exten, local_display, sizeof(local_display));
13091 ast_xml_escape(exten, remote_display, sizeof(remote_display));
13092
13093
13094
13095
13096 if (sip_cfg.notifycid) {
13097 struct ast_channel *caller;
13098
13099 if ((caller = ast_channel_callback(find_calling_channel, NULL, p, 0))) {
13100 static char *anonymous = "anonymous";
13101 static char *invalid = "anonymous.invalid";
13102 char *cid_num;
13103 char *connected_num;
13104 int need;
13105 int cid_num_restricted, connected_num_restricted;
13106
13107 ast_channel_lock(caller);
13108
13109 cid_num_restricted = (caller->caller.id.number.presentation &
13110 AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED;
13111 cid_num = S_COR(caller->caller.id.number.valid,
13112 S_COR(cid_num_restricted, anonymous,
13113 caller->caller.id.number.str), "");
13114
13115 need = strlen(cid_num) + (cid_num_restricted ? strlen(invalid) :
13116 strlen(p->fromdomain)) + sizeof("sip:@");
13117
13118 remote_target = ast_alloca(need);
13119 snprintf(remote_target, need, "sip:%s@%s", cid_num,
13120 cid_num_restricted ? invalid : p->fromdomain);
13121
13122 ast_xml_escape(S_COR(caller->caller.id.name.valid,
13123 S_COR((caller->caller.id.name.presentation &
13124 AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED, anonymous,
13125 caller->caller.id.name.str), ""),
13126 remote_display, sizeof(remote_display));
13127
13128 connected_num_restricted = (caller->connected.id.number.presentation &
13129 AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED;
13130 connected_num = S_COR(caller->connected.id.number.valid,
13131 S_COR(connected_num_restricted, anonymous,
13132 caller->connected.id.number.str), "");
13133
13134 need = strlen(connected_num) + (connected_num_restricted ? strlen(invalid) :
13135 strlen(p->fromdomain)) + sizeof("sip:@");
13136 local_target = ast_alloca(need);
13137
13138 snprintf(local_target, need, "sip:%s@%s", connected_num,
13139 connected_num_restricted ? invalid : p->fromdomain);
13140
13141 ast_xml_escape(S_COR(caller->connected.id.name.valid,
13142 S_COR((caller->connected.id.name.presentation &
13143 AST_PRES_RESTRICTION) == AST_PRES_RESTRICTED, anonymous,
13144 caller->connected.id.name.str), ""),
13145 local_display, sizeof(local_display));
13146
13147 ast_channel_unlock(caller);
13148 caller = ast_channel_unref(caller);
13149 }
13150
13151
13152
13153 if (sip_cfg.pedanticsipchecking) {
13154 ast_str_append(tmp, 0, "<dialog id=\"%s\" call-id=\"pickup-%s\" local-tag=\"%s\" remote-tag=\"%s\" direction=\"recipient\">\n",
13155 exten, p->callid, p->theirtag, p->tag);
13156 } else {
13157 ast_str_append(tmp, 0, "<dialog id=\"%s\" call-id=\"pickup-%s\" direction=\"recipient\">\n",
13158 exten, p->callid);
13159 }
13160 ast_str_append(tmp, 0,
13161 "<remote>\n"
13162
13163
13164 "<identity display=\"%s\">%s</identity>\n"
13165 "<target uri=\"%s\"/>\n"
13166 "</remote>\n"
13167 "<local>\n"
13168 "<identity display=\"%s\">%s</identity>\n"
13169 "<target uri=\"%s\"/>\n"
13170 "</local>\n",
13171 remote_display, remote_target, remote_target, local_display, local_target, local_target);
13172 } else {
13173 ast_str_append(tmp, 0, "<dialog id=\"%s\" direction=\"recipient\">\n", exten);
13174 }
13175
13176 } else {
13177 ast_str_append(tmp, 0, "<dialog id=\"%s\">\n", exten);
13178 }
13179 ast_str_append(tmp, 0, "<state>%s</state>\n", statestring);
13180 if (state == AST_EXTENSION_ONHOLD) {
13181 ast_str_append(tmp, 0, "<local>\n<target uri=\"%s\">\n"
13182 "<param pname=\"+sip.rendering\" pvalue=\"no\"/>\n"
13183 "</target>\n</local>\n", mto);
13184 }
13185 ast_str_append(tmp, 0, "</dialog>\n</dialog-info>\n");
13186 break;
13187 case NONE:
13188 default:
13189 break;
13190 }
13191 }
13192
13193 static int transmit_cc_notify(struct ast_cc_agent *agent, struct sip_pvt *subscription, enum sip_cc_notify_state state)
13194 {
13195 struct sip_request req;
13196 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
13197 char uri[SIPBUFSIZE];
13198 char state_str[64];
13199 char subscription_state_hdr[64];
13200
13201 if (state < CC_QUEUED || state > CC_READY) {
13202 ast_log(LOG_WARNING, "Invalid state provided for transmit_cc_notify (%u)\n", state);
13203 return -1;
13204 }
13205
13206 reqprep(&req, subscription, SIP_NOTIFY, 0, TRUE);
13207 snprintf(state_str, sizeof(state_str), "%s\r\n", sip_cc_notify_state_map[state].state_string);
13208 add_header(&req, "Event", "call-completion");
13209 add_header(&req, "Content-Type", "application/call-completion");
13210 snprintf(subscription_state_hdr, sizeof(subscription_state_hdr), "active;expires=%d", subscription->expiry);
13211 add_header(&req, "Subscription-State", subscription_state_hdr);
13212 if (state == CC_READY) {
13213 generate_uri(subscription, agent_pvt->notify_uri, sizeof(agent_pvt->notify_uri));
13214 snprintf(uri, sizeof(uri) - 1, "cc-URI: %s\r\n", agent_pvt->notify_uri);
13215 }
13216 add_content(&req, state_str);
13217 if (state == CC_READY) {
13218 add_content(&req, uri);
13219 }
13220 return send_request(subscription, &req, XMIT_RELIABLE, subscription->ocseq);
13221 }
13222
13223
13224 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
13225 {
13226 struct ast_str *tmp = ast_str_alloca(4000);
13227 char from[256], to[256];
13228 char *c, *mfrom, *mto;
13229 struct sip_request req;
13230 const struct cfsubscription_types *subscriptiontype;
13231
13232
13233 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
13234 return 0;
13235 }
13236
13237 memset(from, 0, sizeof(from));
13238 memset(to, 0, sizeof(to));
13239
13240 subscriptiontype = find_subscription_type(p->subscribed);
13241
13242 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
13243 c = get_in_brackets(from);
13244 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
13245 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
13246 return -1;
13247 }
13248
13249 mfrom = remove_uri_parameters(c);
13250
13251 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
13252 c = get_in_brackets(to);
13253 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
13254 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
13255 return -1;
13256 }
13257 mto = remove_uri_parameters(c);
13258
13259 reqprep(&req, p, SIP_NOTIFY, 0, 1);
13260
13261 switch(state) {
13262 case AST_EXTENSION_DEACTIVATED:
13263 if (timeout)
13264 add_header(&req, "Subscription-State", "terminated;reason=timeout");
13265 else {
13266 add_header(&req, "Subscription-State", "terminated;reason=probation");
13267 add_header(&req, "Retry-After", "60");
13268 }
13269 break;
13270 case AST_EXTENSION_REMOVED:
13271 add_header(&req, "Subscription-State", "terminated;reason=noresource");
13272 break;
13273 default:
13274 if (p->expiry)
13275 add_header(&req, "Subscription-State", "active");
13276 else
13277 add_header(&req, "Subscription-State", "terminated;reason=timeout");
13278 }
13279
13280 switch (p->subscribed) {
13281 case XPIDF_XML:
13282 case CPIM_PIDF_XML:
13283 add_header(&req, "Event", subscriptiontype->event);
13284 state_notify_build_xml(state, full, p->exten, p->context, &tmp, p, p->subscribed, mfrom, mto);
13285 add_header(&req, "Content-Type", subscriptiontype->mediatype);
13286 p->dialogver++;
13287 break;
13288 case PIDF_XML:
13289 add_header(&req, "Event", subscriptiontype->event);
13290 state_notify_build_xml(state, full, p->exten, p->context, &tmp, p, p->subscribed, mfrom, mto);
13291 add_header(&req, "Content-Type", subscriptiontype->mediatype);
13292 p->dialogver++;
13293 break;
13294 case DIALOG_INFO_XML:
13295 add_header(&req, "Event", subscriptiontype->event);
13296 state_notify_build_xml(state, full, p->exten, p->context, &tmp, p, p->subscribed, mfrom, mto);
13297 add_header(&req, "Content-Type", subscriptiontype->mediatype);
13298 p->dialogver++;
13299 break;
13300 case NONE:
13301 default:
13302 break;
13303 }
13304
13305 add_content(&req, ast_str_buffer(tmp));
13306
13307 p->pendinginvite = p->ocseq;
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
13323 }
13324
13325
13326
13327
13328
13329
13330
13331 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, const char *vmexten)
13332 {
13333 struct sip_request req;
13334 struct ast_str *out = ast_str_alloca(500);
13335 int ourport = (p->fromdomainport && (p->fromdomainport != STANDARD_SIP_PORT)) ? p->fromdomainport : ast_sockaddr_port(&p->ourip);
13336 const char *domain;
13337 const char *exten = S_OR(vmexten, default_vmexten);
13338
13339 initreqprep(&req, p, SIP_NOTIFY, NULL);
13340 add_header(&req, "Event", "message-summary");
13341 add_header(&req, "Content-Type", default_notifymime);
13342 ast_str_append(&out, 0, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
13343
13344
13345 domain = S_OR(p->fromdomain, ast_sockaddr_stringify_host_remote(&p->ourip));
13346
13347 if (!sip_standard_port(p->socket.type, ourport)) {
13348 if (p->socket.type == SIP_TRANSPORT_UDP) {
13349 ast_str_append(&out, 0, "Message-Account: sip:%s@%s:%d\r\n", exten, domain, ourport);
13350 } else {
13351 ast_str_append(&out, 0, "Message-Account: sip:%s@%s:%d;transport=%s\r\n", exten, domain, ourport, get_transport(p->socket.type));
13352 }
13353 } else {
13354 if (p->socket.type == SIP_TRANSPORT_UDP) {
13355 ast_str_append(&out, 0, "Message-Account: sip:%s@%s\r\n", exten, domain);
13356 } else {
13357 ast_str_append(&out, 0, "Message-Account: sip:%s@%s;transport=%s\r\n", exten, domain, get_transport(p->socket.type));
13358 }
13359 }
13360
13361
13362
13363 ast_str_append(&out, 0, "Voice-Message: %d/%d%s\r\n",
13364 newmsgs, oldmsgs, (ast_test_flag(&p->flags[1], SIP_PAGE2_BUGGY_MWI) ? "" : " (0/0)"));
13365
13366 if (p->subscribed) {
13367 if (p->expiry) {
13368 add_header(&req, "Subscription-State", "active");
13369 } else {
13370 add_header(&req, "Subscription-State", "terminated;reason=timeout");
13371 }
13372 }
13373
13374 add_content(&req, ast_str_buffer(out));
13375
13376 if (!p->initreq.headers) {
13377 initialize_initreq(p, &req);
13378 }
13379 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
13380 }
13381
13382
13383 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
13384 {
13385 struct sip_request req;
13386 char tmp[SIPBUFSIZE/2];
13387
13388 reqprep(&req, p, SIP_NOTIFY, 0, 1);
13389 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
13390 add_header(&req, "Event", tmp);
13391 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
13392 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
13393 add_header(&req, "Allow", ALLOWED_METHODS);
13394 add_supported_header(p, &req);
13395
13396 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
13397 add_content(&req, tmp);
13398
13399 if (!p->initreq.headers) {
13400 initialize_initreq(p, &req);
13401 }
13402
13403 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
13404 }
13405
13406 static int manager_sipnotify(struct mansession *s, const struct message *m)
13407 {
13408 const char *channame = astman_get_header(m, "Channel");
13409 struct ast_variable *vars = astman_get_variables_order(m, ORDER_NATURAL);
13410 struct sip_pvt *p;
13411 struct ast_variable *header, *var;
13412
13413 if (ast_strlen_zero(channame)) {
13414 astman_send_error(s, m, "SIPNotify requires a channel name");
13415 return 0;
13416 }
13417
13418 if (!strncasecmp(channame, "sip/", 4)) {
13419 channame += 4;
13420 }
13421
13422 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
13423 astman_send_error(s, m, "Unable to build sip pvt data for notify (memory/socket error)");
13424 return 0;
13425 }
13426
13427 if (create_addr(p, channame, NULL, 0)) {
13428
13429 dialog_unlink_all(p);
13430 dialog_unref(p, "unref dialog inside for loop" );
13431
13432 astman_send_error(s, m, "Could not create address");
13433 return 0;
13434 }
13435
13436
13437 ast_set_flag(&p->flags[0], SIP_OUTGOING);
13438 sip_notify_allocate(p);
13439
13440 p->notify->headers = header = ast_variable_new("Subscription-State", "terminated", "");
13441
13442 for (var = vars; var; var = var->next) {
13443 if (!strcasecmp(var->name, "Content")) {
13444 if (ast_str_strlen(p->notify->content))
13445 ast_str_append(&p->notify->content, 0, "\r\n");
13446 ast_str_append(&p->notify->content, 0, "%s", var->value);
13447 } else if (!strcasecmp(var->name, "Content-Length")) {
13448 ast_log(LOG_WARNING, "it is not necessary to specify Content-Length, ignoring\n");
13449 } else {
13450 header->next = ast_variable_new(var->name, var->value, "");
13451 header = header->next;
13452 }
13453 }
13454
13455 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
13456 transmit_invite(p, SIP_NOTIFY, 0, 2, NULL);
13457 dialog_unref(p, "bump down the count of p since we're done with it.");
13458
13459 astman_send_ack(s, m, "Notify Sent");
13460 ast_variables_destroy(vars);
13461 return 0;
13462 }
13463
13464
13465
13466 static void update_redirecting(struct sip_pvt *p, const void *data, size_t datalen)
13467 {
13468 struct sip_request resp;
13469
13470 if (p->owner->_state == AST_STATE_UP || ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
13471 return;
13472 }
13473
13474 respprep(&resp, p, "181 Call is being forwarded", &p->initreq);
13475 add_diversion_header(&resp, p);
13476 send_response(p, &resp, XMIT_UNRELIABLE, 0);
13477 }
13478
13479
13480 static void update_connectedline(struct sip_pvt *p, const void *data, size_t datalen)
13481 {
13482
13483 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID)) {
13484 return;
13485 }
13486 if (!p->owner->connected.id.number.valid
13487 || ast_strlen_zero(p->owner->connected.id.number.str)) {
13488 return;
13489 }
13490
13491 append_history(p, "ConnectedLine", "%s party is now %s <%s>",
13492 ast_test_flag(&p->flags[0], SIP_OUTGOING) ? "Calling" : "Called",
13493 S_COR(p->owner->connected.id.name.valid, p->owner->connected.id.name.str, ""),
13494 S_COR(p->owner->connected.id.number.valid, p->owner->connected.id.number.str, ""));
13495
13496 if (p->owner->_state == AST_STATE_UP || ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
13497 struct sip_request req;
13498
13499 if (!p->pendinginvite && (p->invitestate == INV_CONFIRMED || p->invitestate == INV_TERMINATED)) {
13500 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
13501
13502 add_header(&req, "Allow", ALLOWED_METHODS);
13503 add_supported_header(p, &req);
13504 add_rpid(&req, p);
13505 add_sdp(&req, p, FALSE, TRUE, FALSE);
13506
13507 initialize_initreq(p, &req);
13508 p->lastinvite = p->ocseq;
13509 ast_set_flag(&p->flags[0], SIP_OUTGOING);
13510 p->invitestate = INV_CALLING;
13511 send_request(p, &req, XMIT_CRITICAL, p->ocseq);
13512 } else if ((is_method_allowed(&p->allowed_methods, SIP_UPDATE)) && (!ast_strlen_zero(p->okcontacturi))) {
13513 reqprep(&req, p, SIP_UPDATE, 0, 1);
13514 add_rpid(&req, p);
13515 add_header(&req, "X-Asterisk-rpid-update", "Yes");
13516 send_request(p, &req, XMIT_CRITICAL, p->ocseq);
13517 } else {
13518
13519 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
13520 }
13521 } else {
13522 ast_set_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND);
13523 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPID_IMMEDIATE)) {
13524 struct sip_request resp;
13525
13526 if ((p->owner->_state == AST_STATE_RING) && !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT)) {
13527 ast_clear_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND);
13528 respprep(&resp, p, "180 Ringing", &p->initreq);
13529 add_rpid(&resp, p);
13530 send_response(p, &resp, XMIT_UNRELIABLE, 0);
13531 ast_set_flag(&p->flags[0], SIP_RINGING);
13532 } else if (p->owner->_state == AST_STATE_RINGING) {
13533 ast_clear_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND);
13534 respprep(&resp, p, "183 Session Progress", &p->initreq);
13535 add_rpid(&resp, p);
13536 send_response(p, &resp, XMIT_UNRELIABLE, 0);
13537 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
13538 } else {
13539 ast_debug(1, "Unable able to send update to '%s' in state '%s'\n", p->owner->name, ast_state2str(p->owner->_state));
13540 }
13541 }
13542 }
13543 }
13544
13545 static const struct _map_x_s regstatestrings[] = {
13546 { REG_STATE_FAILED, "Failed" },
13547 { REG_STATE_UNREGISTERED, "Unregistered"},
13548 { REG_STATE_REGSENT, "Request Sent"},
13549 { REG_STATE_AUTHSENT, "Auth. Sent"},
13550 { REG_STATE_REGISTERED, "Registered"},
13551 { REG_STATE_REJECTED, "Rejected"},
13552 { REG_STATE_TIMEOUT, "Timeout"},
13553 { REG_STATE_NOAUTH, "No Authentication"},
13554 { -1, NULL }
13555 };
13556
13557
13558 static const char *regstate2str(enum sipregistrystate regstate)
13559 {
13560 return map_x_s(regstatestrings, regstate, "Unknown");
13561 }
13562
13563
13564
13565
13566
13567
13568
13569 static int sip_reregister(const void *data)
13570 {
13571
13572 struct sip_registry *r = (struct sip_registry *) data;
13573
13574
13575 if (!r) {
13576 return 0;
13577 }
13578
13579 if (r->call && r->call->do_history) {
13580 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
13581 }
13582
13583
13584 if (sipdebug) {
13585 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
13586 }
13587
13588 r->expire = -1;
13589 r->expiry = r->configured_expiry;
13590 __sip_do_register(r);
13591 registry_unref(r, "unref the re-register scheduled event");
13592 return 0;
13593 }
13594
13595
13596
13597
13598 static int __sip_do_register(struct sip_registry *r)
13599 {
13600 int res;
13601
13602 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
13603 return res;
13604 }
13605
13606
13607
13608
13609
13610
13611
13612 static int sip_reg_timeout(const void *data)
13613 {
13614
13615
13616 struct sip_registry *r = (struct sip_registry *)data;
13617 struct sip_pvt *p;
13618
13619
13620 if (!r) {
13621 return 0;
13622 }
13623
13624 if (r->dnsmgr) {
13625
13626 ast_dnsmgr_refresh(r->dnsmgr);
13627 }
13628
13629
13630
13631
13632
13633 if (r->call) {
13634
13635
13636 p = r->call;
13637 sip_pvt_lock(p);
13638 pvt_set_needdestroy(p, "registration timeout");
13639
13640 __sip_pretend_ack(p);
13641 sip_pvt_unlock(p);
13642
13643
13644
13645 if (p->registry) {
13646 p->registry = registry_unref(p->registry, "p->registry unreffed");
13647 }
13648 r->call = dialog_unref(r->call, "unrefing r->call");
13649 }
13650
13651 r->timeout = -1;
13652 if (global_regattempts_max && r->regattempts >= global_regattempts_max) {
13653
13654
13655
13656 ast_log(LOG_NOTICE, " -- Last Registration Attempt #%d failed, Giving up forever trying to register '%s@%s'\n", r->regattempts, r->username, r->hostname);
13657 r->regstate = REG_STATE_FAILED;
13658 } else {
13659 r->regstate = REG_STATE_UNREGISTERED;
13660 transmit_register(r, SIP_REGISTER, NULL, NULL);
13661 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
13662 }
13663 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nUsername: %s\r\nDomain: %s\r\nStatus: %s\r\n", r->username, r->hostname, regstate2str(r->regstate));
13664 registry_unref(r, "unreffing registry_unref r");
13665 return 0;
13666 }
13667
13668 static const char *sip_sanitized_host(const char *host)
13669 {
13670 struct ast_sockaddr addr = { { 0, 0, }, };
13671
13672
13673
13674
13675 if (!ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID)) {
13676 return host;
13677 }
13678 return ast_sockaddr_stringify_host_remote(&addr);
13679 }
13680
13681
13682
13683
13684 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
13685 {
13686 struct sip_request req;
13687 char from[256];
13688 char to[256];
13689 char tmp[80];
13690 char addr[80];
13691 struct sip_pvt *p;
13692 struct sip_peer *peer = NULL;
13693 int res;
13694 int portno = 0;
13695
13696
13697 if (r == NULL || ((auth == NULL) && (r->regstate == REG_STATE_REGSENT || r->regstate == REG_STATE_AUTHSENT))) {
13698 if (r) {
13699 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
13700 }
13701 return 0;
13702 }
13703
13704 if (r->dnsmgr == NULL) {
13705 char transport[MAXHOSTNAMELEN];
13706 peer = find_peer(r->hostname, NULL, TRUE, FINDPEERS, FALSE, 0);
13707 snprintf(transport, sizeof(transport), "_%s._%s",get_srv_service(r->transport), get_srv_protocol(r->transport));
13708 r->us.ss.ss_family = get_address_family_filter(r->transport);
13709
13710
13711
13712
13713
13714 if (!obproxy_get(r->call, peer)) {
13715 registry_addref(r, "add reg ref for dnsmgr");
13716 ast_dnsmgr_lookup_cb(peer ? peer->tohost : r->hostname, &r->us, &r->dnsmgr, sip_cfg.srvlookup ? transport : NULL, on_dns_update_registry, r);
13717 if (!r->dnsmgr) {
13718
13719 registry_unref(r, "remove reg ref, dnsmgr disabled");
13720 }
13721 }
13722 if (peer) {
13723 peer = unref_peer(peer, "removing peer ref for dnsmgr_lookup");
13724 }
13725 }
13726
13727 if (r->call) {
13728 if (!auth) {
13729 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
13730 return 0;
13731 } else {
13732 p = dialog_ref(r->call, "getting a copy of the r->call dialog in transmit_register");
13733 ast_string_field_set(p, theirtag, NULL);
13734 }
13735 } else {
13736
13737 if (!r->callid_valid) {
13738 build_callid_registry(r, &internip, default_fromdomain);
13739 build_localtag_registry(r);
13740 r->callid_valid = TRUE;
13741 }
13742
13743 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER, NULL))) {
13744 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
13745 return 0;
13746 }
13747
13748
13749 ast_string_field_set(p, tag, r->localtag);
13750
13751 if (p->do_history) {
13752 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
13753 }
13754
13755 p->socket.type = r->transport;
13756
13757
13758 if (!ast_sockaddr_isnull(&r->us)) {
13759 if (!ast_sockaddr_port(&r->us) && r->portno) {
13760 ast_sockaddr_set_port(&r->us, r->portno);
13761 }
13762
13763
13764
13765
13766
13767 if ((peer = find_peer(S_OR(r->peername, r->hostname), NULL, TRUE, FINDPEERS, FALSE, 0))) {
13768 if (ast_sockaddr_cmp(&peer->addr, &r->us)) {
13769 on_dns_update_peer(&peer->addr, &r->us, peer);
13770 }
13771 peer = unref_peer(peer, "unref after find_peer");
13772 }
13773 }
13774
13775
13776 if (create_addr(p, S_OR(r->peername, r->hostname), &r->us, 0)) {
13777
13778
13779 dialog_unlink_all(p);
13780 p = dialog_unref(p, "unref dialog after unlink_all");
13781 if (r->timeout > -1) {
13782 AST_SCHED_REPLACE_UNREF(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r,
13783 registry_unref(_data, "del for REPLACE of registry ptr"),
13784 registry_unref(r, "object ptr dec when SCHED_REPLACE add failed"),
13785 registry_addref(r,"add for REPLACE registry ptr"));
13786 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
13787 } else {
13788 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, registry_addref(r, "add for REPLACE registry ptr"));
13789 ast_log(LOG_WARNING, "Probably a DNS error for registration to %s@%s, trying REGISTER again (after %d seconds)\n", r->username, r->hostname, global_reg_timeout);
13790 }
13791 r->regattempts++;
13792 return 0;
13793 }
13794
13795
13796 ast_string_field_set(r, callid, p->callid);
13797
13798 if (!r->dnsmgr && r->portno) {
13799 ast_sockaddr_set_port(&p->sa, r->portno);
13800 ast_sockaddr_set_port(&p->recv, r->portno);
13801 }
13802 if (!ast_strlen_zero(p->fromdomain)) {
13803 portno = (p->fromdomainport) ? p->fromdomainport : STANDARD_SIP_PORT;
13804 } else if (!ast_strlen_zero(r->regdomain)) {
13805 portno = (r->regdomainport) ? r->regdomainport : STANDARD_SIP_PORT;
13806 } else {
13807 portno = ast_sockaddr_port(&p->sa);
13808 }
13809
13810 ast_set_flag(&p->flags[0], SIP_OUTGOING);
13811 r->call = dialog_ref(p, "copying dialog into registry r->call");
13812 p->registry = registry_addref(r, "transmit_register: addref to p->registry in transmit_register");
13813 if (!ast_strlen_zero(r->secret)) {
13814 ast_string_field_set(p, peersecret, r->secret);
13815 }
13816 if (!ast_strlen_zero(r->md5secret))
13817 ast_string_field_set(p, peermd5secret, r->md5secret);
13818
13819
13820 if (!ast_strlen_zero(r->authuser)) {
13821 ast_string_field_set(p, peername, r->authuser);
13822 ast_string_field_set(p, authname, r->authuser);
13823 } else if (!ast_strlen_zero(r->username)) {
13824 ast_string_field_set(p, peername, r->username);
13825 ast_string_field_set(p, authname, r->username);
13826 ast_string_field_set(p, fromuser, r->username);
13827 }
13828 if (!ast_strlen_zero(r->username)) {
13829 ast_string_field_set(p, username, r->username);
13830 }
13831
13832 if (!ast_strlen_zero(r->callback)) {
13833 ast_string_field_set(p, exten, r->callback);
13834 }
13835
13836
13837 set_socket_transport(&p->socket, r->transport);
13838 if (r->transport == SIP_TRANSPORT_TLS || r->transport == SIP_TRANSPORT_TCP) {
13839 p->socket.port =
13840 htons(ast_sockaddr_port(&sip_tcp_desc.local_address));
13841 }
13842
13843
13844
13845
13846
13847
13848 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
13849 build_contact(p);
13850 }
13851
13852
13853 if (auth == NULL) {
13854 if (r->timeout > -1) {
13855 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
13856 }
13857 AST_SCHED_REPLACE_UNREF(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r,
13858 registry_unref(_data,"reg ptr unrefed from del in SCHED_REPLACE"),
13859 registry_unref(r,"reg ptr unrefed from add failure in SCHED_REPLACE"),
13860 registry_addref(r,"reg ptr reffed from add in SCHED_REPLACE"));
13861 ast_debug(1, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
13862 }
13863
13864 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, S_OR(r->regdomain, sip_sanitized_host(p->tohost)), p->tag);
13865 if (!ast_strlen_zero(p->theirtag)) {
13866 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, S_OR(r->regdomain, sip_sanitized_host(p->tohost)), p->theirtag);
13867 } else {
13868 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, S_OR(r->regdomain, sip_sanitized_host(p->tohost)));
13869 }
13870
13871
13872
13873 if (portno && portno != STANDARD_SIP_PORT) {
13874 snprintf(addr, sizeof(addr), "sip:%s:%d", S_OR(p->fromdomain,S_OR(r->regdomain, sip_sanitized_host(r->hostname))), portno);
13875 } else {
13876 snprintf(addr, sizeof(addr), "sip:%s", S_OR(p->fromdomain,S_OR(r->regdomain, sip_sanitized_host(r->hostname))));
13877 }
13878
13879 ast_string_field_set(p, uri, addr);
13880
13881 p->branch ^= ast_random();
13882
13883 init_req(&req, sipmethod, addr);
13884
13885
13886 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
13887 p->ocseq = r->ocseq;
13888
13889 build_via(p);
13890 add_header(&req, "Via", p->via);
13891 add_header_max_forwards(p, &req);
13892 add_header(&req, "From", from);
13893 add_header(&req, "To", to);
13894 add_header(&req, "Call-ID", p->callid);
13895 add_header(&req, "CSeq", tmp);
13896 if (!ast_strlen_zero(global_useragent))
13897 add_header(&req, "User-Agent", global_useragent);
13898
13899 if (auth) {
13900 add_header(&req, authheader, auth);
13901 } else if (!ast_strlen_zero(r->nonce)) {
13902 char digest[1024];
13903
13904
13905
13906
13907
13908
13909 if (sipdebug) {
13910 ast_debug(1, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
13911 }
13912 ast_string_field_set(p, realm, r->realm);
13913 ast_string_field_set(p, nonce, r->nonce);
13914 ast_string_field_set(p, domain, r->authdomain);
13915 ast_string_field_set(p, opaque, r->opaque);
13916 ast_string_field_set(p, qop, r->qop);
13917 p->noncecount = ++r->noncecount;
13918
13919 memset(digest, 0, sizeof(digest));
13920 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
13921 add_header(&req, "Authorization", digest);
13922 } else {
13923 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
13924 }
13925 }
13926
13927 snprintf(tmp, sizeof(tmp), "%d", r->expiry);
13928 add_header(&req, "Expires", tmp);
13929 add_header(&req, "Contact", p->our_contact);
13930
13931 initialize_initreq(p, &req);
13932 if (sip_debug_test_pvt(p)) {
13933 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
13934 }
13935 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
13936 r->regattempts++;
13937 ast_debug(4, "REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
13938 res = send_request(p, &req, XMIT_CRITICAL, p->ocseq);
13939 dialog_unref(p, "p is finished here at the end of transmit_register");
13940 return res;
13941 }
13942
13943
13944 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
13945 {
13946 struct sip_request req;
13947
13948 reqprep(&req, p, SIP_MESSAGE, 0, 1);
13949 add_text(&req, text);
13950 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
13951 }
13952
13953
13954 static int sip_refer_allocate(struct sip_pvt *p)
13955 {
13956 p->refer = ast_calloc(1, sizeof(struct sip_refer));
13957 return p->refer ? 1 : 0;
13958 }
13959
13960
13961 static int sip_notify_allocate(struct sip_pvt *p)
13962 {
13963 p->notify = ast_calloc(1, sizeof(struct sip_notify));
13964 if (p->notify) {
13965 p->notify->content = ast_str_create(128);
13966 }
13967 return p->notify ? 1 : 0;
13968 }
13969
13970
13971
13972
13973
13974
13975 static int transmit_refer(struct sip_pvt *p, const char *dest)
13976 {
13977 struct sip_request req = {
13978 .headers = 0,
13979 };
13980 char from[256];
13981 const char *of;
13982 char *c;
13983 char referto[256];
13984 int use_tls=FALSE;
13985
13986 if (sipdebug) {
13987 ast_debug(1, "SIP transfer of %s to %s\n", p->callid, dest);
13988 }
13989
13990
13991 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
13992 of = get_header(&p->initreq, "To");
13993 } else {
13994 of = get_header(&p->initreq, "From");
13995 }
13996
13997 ast_copy_string(from, of, sizeof(from));
13998 of = get_in_brackets(from);
13999 ast_string_field_set(p, from, of);
14000 if (!strncasecmp(of, "sip:", 4)) {
14001 of += 4;
14002 } else if (!strncasecmp(of, "sips:", 5)) {
14003 of += 5;
14004 use_tls = TRUE;
14005 } else {
14006 ast_log(LOG_NOTICE, "From address missing 'sip(s):', assuming sip:\n");
14007 }
14008
14009 if (strchr(dest, '@')) {
14010 c = NULL;
14011 } else if ((c = strchr(of, '@'))) {
14012 *c++ = '\0';
14013 }
14014 if (c) {
14015 snprintf(referto, sizeof(referto), "<sip%s:%s@%s>", use_tls ? "s" : "", dest, c);
14016 } else {
14017 snprintf(referto, sizeof(referto), "<sip%s:%s>", use_tls ? "s" : "", dest);
14018 }
14019
14020
14021 sip_refer_allocate(p);
14022 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
14023 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
14024 p->refer->status = REFER_SENT;
14025
14026 reqprep(&req, p, SIP_REFER, 0, 1);
14027
14028 add_header(&req, "Refer-To", referto);
14029 add_header(&req, "Allow", ALLOWED_METHODS);
14030 add_supported_header(p, &req);
14031 if (!ast_strlen_zero(p->our_contact)) {
14032 add_header(&req, "Referred-By", p->our_contact);
14033 }
14034
14035 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045 }
14046
14047
14048 static int transmit_info_with_aoc(struct sip_pvt *p, struct ast_aoc_decoded *decoded)
14049 {
14050 struct sip_request req;
14051 struct ast_str *str = ast_str_alloca(512);
14052 const struct ast_aoc_unit_entry *unit_entry = ast_aoc_get_unit_info(decoded, 0);
14053 enum ast_aoc_charge_type charging = ast_aoc_get_charge_type(decoded);
14054
14055 reqprep(&req, p, SIP_INFO, 0, 1);
14056
14057 if (ast_aoc_get_msg_type(decoded) == AST_AOC_D) {
14058 ast_str_append(&str, 0, "type=active;");
14059 } else if (ast_aoc_get_msg_type(decoded) == AST_AOC_E) {
14060 ast_str_append(&str, 0, "type=terminated;");
14061 } else {
14062
14063 return -1;
14064 }
14065
14066 switch (charging) {
14067 case AST_AOC_CHARGE_FREE:
14068 ast_str_append(&str, 0, "free-of-charge;");
14069 break;
14070 case AST_AOC_CHARGE_CURRENCY:
14071 ast_str_append(&str, 0, "charging;");
14072 ast_str_append(&str, 0, "charging-info=currency;");
14073 ast_str_append(&str, 0, "amount=%u;", ast_aoc_get_currency_amount(decoded));
14074 ast_str_append(&str, 0, "multiplier=%s;", ast_aoc_get_currency_multiplier_decimal(decoded));
14075 if (!ast_strlen_zero(ast_aoc_get_currency_name(decoded))) {
14076 ast_str_append(&str, 0, "currency=%s;", ast_aoc_get_currency_name(decoded));
14077 }
14078 break;
14079 case AST_AOC_CHARGE_UNIT:
14080 ast_str_append(&str, 0, "charging;");
14081 ast_str_append(&str, 0, "charging-info=pulse;");
14082 if (unit_entry) {
14083 ast_str_append(&str, 0, "recorded-units=%u;", unit_entry->amount);
14084 }
14085 break;
14086 default:
14087 ast_str_append(&str, 0, "not-available;");
14088 };
14089
14090 add_header(&req, "AOC", ast_str_buffer(str));
14091
14092 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
14093 }
14094
14095
14096 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
14097 {
14098 struct sip_request req;
14099
14100 reqprep(&req, p, SIP_INFO, 0, 1);
14101 add_digit(&req, digit, duration, (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_SHORTINFO));
14102 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
14103 }
14104
14105
14106 static int transmit_info_with_vidupdate(struct sip_pvt *p)
14107 {
14108 struct sip_request req;
14109
14110 reqprep(&req, p, SIP_INFO, 0, 1);
14111 add_vidupdate(&req);
14112 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
14113 }
14114
14115
14116
14117
14118 static int transmit_request(struct sip_pvt *p, int sipmethod, uint32_t seqno, enum xmittype reliable, int newbranch)
14119 {
14120 struct sip_request resp;
14121
14122 reqprep(&resp, p, sipmethod, seqno, newbranch);
14123 if (sipmethod == SIP_CANCEL && p->answered_elsewhere) {
14124 add_header(&resp, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
14125 }
14126
14127 if (sipmethod == SIP_ACK) {
14128 p->invitestate = INV_CONFIRMED;
14129 }
14130
14131 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
14132 }
14133
14134
14135 static void auth_headers(enum sip_auth_type code, char **header, char **respheader)
14136 {
14137 if (code == WWW_AUTH) {
14138 *header = "WWW-Authenticate";
14139 *respheader = "Authorization";
14140 } else if (code == PROXY_AUTH) {
14141 *header = "Proxy-Authenticate";
14142 *respheader = "Proxy-Authorization";
14143 } else {
14144 ast_verbose("-- wrong response code %u\n", code);
14145 *header = *respheader = "Invalid";
14146 }
14147 }
14148
14149
14150 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, uint32_t seqno, enum xmittype reliable, int newbranch)
14151 {
14152 struct sip_request resp;
14153
14154 reqprep(&resp, p, sipmethod, seqno, newbranch);
14155 if (!ast_strlen_zero(p->realm)) {
14156 char digest[1024];
14157
14158 memset(digest, 0, sizeof(digest));
14159 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
14160 char *dummy, *response;
14161 enum sip_auth_type code = p->options ? p->options->auth_type : PROXY_AUTH;
14162 auth_headers(code, &dummy, &response);
14163 add_header(&resp, response, digest);
14164 } else {
14165 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
14166 }
14167 }
14168
14169
14170 if (sipmethod == SIP_BYE) {
14171 char buf[20];
14172
14173 if (ast_test_flag(&p->flags[1], SIP_PAGE2_Q850_REASON) && p->hangupcause) {
14174 sprintf(buf, "Q.850;cause=%i", p->hangupcause & 0x7f);
14175 add_header(&resp, "Reason", buf);
14176 }
14177
14178 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->hangupcause));
14179 snprintf(buf, sizeof(buf), "%d", p->hangupcause);
14180 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
14181 }
14182
14183 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
14184 }
14185
14186
14187 static void destroy_association(struct sip_peer *peer)
14188 {
14189 int realtimeregs = ast_check_realtime("sipregs");
14190 char *tablename = (realtimeregs) ? "sipregs" : "sippeers";
14191
14192 if (!sip_cfg.ignore_regexpire) {
14193 if (peer->rt_fromcontact && sip_cfg.peer_rtupdate) {
14194 ast_update_realtime(tablename, "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "regserver", "", "useragent", "", "lastms", "0", SENTINEL);
14195 } else {
14196 ast_db_del("SIP/Registry", peer->name);
14197 ast_db_del("SIP/PeerMethods", peer->name);
14198 }
14199 }
14200 }
14201
14202 static void set_socket_transport(struct sip_socket *socket, int transport)
14203 {
14204
14205 if (socket->type != transport) {
14206 socket->fd = -1;
14207 socket->type = transport;
14208 if (socket->tcptls_session) {
14209 ao2_ref(socket->tcptls_session, -1);
14210 socket->tcptls_session = NULL;
14211 }
14212 }
14213 }
14214
14215
14216 static int expire_register(const void *data)
14217 {
14218 struct sip_peer *peer = (struct sip_peer *)data;
14219
14220 if (!peer) {
14221 return 0;
14222 }
14223
14224 peer->expire = -1;
14225 peer->portinuri = 0;
14226
14227 destroy_association(peer);
14228 set_socket_transport(&peer->socket, peer->default_outbound_transport);
14229
14230 if (peer->socket.tcptls_session) {
14231 ao2_ref(peer->socket.tcptls_session, -1);
14232 peer->socket.tcptls_session = NULL;
14233 }
14234
14235 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
14236 register_peer_exten(peer, FALSE);
14237 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "SIP/%s", peer->name);
14238
14239
14240
14241
14242 if (peer->is_realtime) {
14243 ast_debug(3, "-REALTIME- peer expired registration. Name: %s. Realtime peer objects now %d\n", peer->name, rpeerobjs);
14244 }
14245
14246 if (peer->selfdestruct ||
14247 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
14248 ao2_t_unlink(peers, peer, "ao2_unlink of peer from peers table");
14249 }
14250 if (!ast_sockaddr_isnull(&peer->addr)) {
14251
14252
14253
14254 ao2_t_unlink(peers_by_ip, peer, "ao2_unlink of peer from peers_by_ip table");
14255 }
14256
14257
14258
14259 memset(&peer->addr, 0, sizeof(peer->addr));
14260
14261 unref_peer(peer, "removing peer ref for expire_register");
14262
14263 return 0;
14264 }
14265
14266
14267 static int sip_poke_peer_s(const void *data)
14268 {
14269 struct sip_peer *peer = (struct sip_peer *)data;
14270 struct sip_peer *foundpeer;
14271
14272 peer->pokeexpire = -1;
14273
14274 foundpeer = ao2_find(peers, peer, OBJ_POINTER);
14275 if (!foundpeer) {
14276 unref_peer(peer, "removing poke peer ref");
14277 return 0;
14278 } else if (foundpeer->name != peer->name) {
14279 unref_peer(foundpeer, "removing above peer ref");
14280 unref_peer(peer, "removing poke peer ref");
14281 return 0;
14282 }
14283
14284 unref_peer(foundpeer, "removing above peer ref");
14285 sip_poke_peer(peer, 0);
14286 unref_peer(peer, "removing poke peer ref");
14287
14288 return 0;
14289 }
14290
14291
14292 static void reg_source_db(struct sip_peer *peer)
14293 {
14294 char data[256];
14295 struct ast_sockaddr sa;
14296 int expire;
14297 char full_addr[128];
14298 AST_DECLARE_APP_ARGS(args,
14299 AST_APP_ARG(addr);
14300 AST_APP_ARG(port);
14301 AST_APP_ARG(expiry_str);
14302 AST_APP_ARG(username);
14303 AST_APP_ARG(contact);
14304 );
14305
14306
14307 if (peer->rt_fromcontact && sip_cfg.peer_rtupdate) {
14308 return;
14309 }
14310 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data))) {
14311 return;
14312 }
14313
14314 AST_NONSTANDARD_RAW_ARGS(args, data, ':');
14315
14316 snprintf(full_addr, sizeof(full_addr), "%s:%s", args.addr, args.port);
14317
14318 if (!ast_sockaddr_parse(&sa, full_addr, 0)) {
14319 return;
14320 }
14321
14322 if (args.expiry_str) {
14323 expire = atoi(args.expiry_str);
14324 } else {
14325 return;
14326 }
14327
14328 if (args.username) {
14329 ast_string_field_set(peer, username, args.username);
14330 }
14331 if (args.contact) {
14332 ast_string_field_set(peer, fullcontact, args.contact);
14333 }
14334
14335 ast_debug(2, "SIP Seeding peer from astdb: '%s' at %s@%s for %d\n",
14336 peer->name, peer->username, ast_sockaddr_stringify_host(&sa), expire);
14337
14338 ast_sockaddr_copy(&peer->addr, &sa);
14339 if (peer->maxms) {
14340
14341 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched,
14342 ast_random() % ((peer->qualifyfreq) ? peer->qualifyfreq : global_qualifyfreq) + 1,
14343 sip_poke_peer_s, peer,
14344 unref_peer(_data, "removing poke peer ref"),
14345 unref_peer(peer, "removing poke peer ref"),
14346 ref_peer(peer, "adding poke peer ref"));
14347 }
14348 AST_SCHED_REPLACE_UNREF(peer->expire, sched, (expire + 10) * 1000, expire_register, peer,
14349 unref_peer(_data, "remove registration ref"),
14350 unref_peer(peer, "remove registration ref"),
14351 ref_peer(peer, "add registration ref"));
14352 register_peer_exten(peer, TRUE);
14353 }
14354
14355
14356 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
14357 {
14358 char contact[SIPBUFSIZE];
14359 char *c;
14360
14361
14362 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
14363 c = get_in_brackets(contact);
14364
14365
14366 ast_string_field_set(pvt, fullcontact, c);
14367
14368
14369 ast_string_field_set(pvt, okcontacturi, c);
14370
14371
14372
14373 return TRUE;
14374 }
14375
14376
14377
14378
14379
14380
14381
14382 static int parse_uri_legacy_check(char *uri, const char *scheme, char **user, char **pass, char **hostport, char **transport)
14383 {
14384 int ret = parse_uri(uri, scheme, user, pass, hostport, transport);
14385 if (sip_cfg.legacy_useroption_parsing) {
14386 char *p;
14387 if ((p = strchr(uri, (int)';'))) {
14388 *p = '\0';
14389 }
14390 }
14391 return ret;
14392 }
14393
14394 static int __set_address_from_contact(const char *fullcontact, struct ast_sockaddr *addr, int tcp)
14395 {
14396 char *hostport, *transport;
14397 char contact_buf[256];
14398 char *contact;
14399
14400
14401 ast_copy_string(contact_buf, fullcontact, sizeof(contact_buf));
14402 contact = contact_buf;
14403
14404
14405
14406
14407
14408
14409
14410
14411 if (parse_uri_legacy_check(contact, "sip:,sips:", &contact, NULL, &hostport,
14412 &transport)) {
14413 ast_log(LOG_WARNING, "Invalid contact uri %s (missing sip: or sips:), attempting to use anyway\n", fullcontact);
14414 }
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425 if (ast_strlen_zero(hostport)) {
14426 ast_log(LOG_WARNING, "Invalid URI: parse_uri failed to acquire hostport\n");
14427 return -1;
14428 }
14429
14430 if (ast_sockaddr_resolve_first_transport(addr, hostport, 0, get_transport_str2enum(transport))) {
14431 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't "
14432 "resolve in DNS) : '%s'\n", hostport);
14433 return -1;
14434 }
14435
14436
14437 if (!ast_sockaddr_port(addr)) {
14438 ast_sockaddr_set_port(addr,
14439 (get_transport_str2enum(transport) ==
14440 SIP_TRANSPORT_TLS ||
14441 !strncasecmp(fullcontact, "sips", 4)) ?
14442 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
14443 }
14444
14445 return 0;
14446 }
14447
14448
14449 static int set_address_from_contact(struct sip_pvt *pvt)
14450 {
14451 if (ast_test_flag(&pvt->flags[0], SIP_NAT_FORCE_RPORT)) {
14452
14453
14454
14455 pvt->sa = pvt->recv;
14456 return 0;
14457 }
14458
14459 return __set_address_from_contact(pvt->fullcontact, &pvt->sa, pvt->socket.type == SIP_TRANSPORT_TLS ? 1 : 0);
14460 }
14461
14462
14463 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
14464 {
14465 char contact[SIPBUFSIZE];
14466 char data[SIPBUFSIZE];
14467 const char *expires = get_header(req, "Expires");
14468 int expire = atoi(expires);
14469 char *curi = NULL, *hostport = NULL, *transport = NULL;
14470 int transport_type;
14471 const char *useragent;
14472 struct ast_sockaddr oldsin, testsa;
14473 char *firstcuri = NULL;
14474 int start = 0;
14475 int wildcard_found = 0;
14476 int single_binding_found = 0;
14477
14478 ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
14479
14480 if (ast_strlen_zero(expires)) {
14481 char *s = strcasestr(contact, ";expires=");
14482 if (s) {
14483 expires = strsep(&s, ";");
14484 if (sscanf(expires + 9, "%30d", &expire) != 1) {
14485 expire = default_expiry;
14486 }
14487 } else {
14488
14489 expire = default_expiry;
14490 }
14491 }
14492
14493 if (expire > max_expiry) {
14494 expire = max_expiry;
14495 }
14496 if (expire < min_expiry && expire != 0) {
14497 expire = min_expiry;
14498 }
14499 pvt->expiry = expire;
14500
14501 copy_socket_data(&pvt->socket, &req->socket);
14502
14503 do {
14504
14505 curi = contact;
14506 if (strchr(contact, '<') == NULL)
14507 strsep(&curi, ";");
14508 curi = get_in_brackets(contact);
14509 if (!firstcuri) {
14510 firstcuri = ast_strdupa(curi);
14511 }
14512
14513 if (!strcasecmp(curi, "*")) {
14514 wildcard_found = 1;
14515 } else {
14516 single_binding_found = 1;
14517 }
14518
14519 if (wildcard_found && (ast_strlen_zero(expires) || expire != 0 || single_binding_found)) {
14520
14521
14522 return PARSE_REGISTER_FAILED;
14523 }
14524
14525 ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
14526 } while (!ast_strlen_zero(contact));
14527 curi = firstcuri;
14528
14529
14530
14531
14532
14533 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
14534
14535 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact)) {
14536 pvt->expiry = ast_sched_when(sched, peer->expire);
14537 }
14538 return PARSE_REGISTER_QUERY;
14539 } else if (!strcasecmp(curi, "*") || !expire) {
14540
14541 AST_SCHED_DEL_UNREF(sched, peer->expire,
14542 unref_peer(peer, "remove register expire ref"));
14543 ast_verb(3, "Unregistered SIP '%s'\n", peer->name);
14544 expire_register(ref_peer(peer,"add ref for explicit expire_register"));
14545 return PARSE_REGISTER_UPDATE;
14546 }
14547
14548
14549 ast_string_field_set(peer, fullcontact, curi);
14550
14551
14552 ast_string_field_build(pvt, our_contact, "<%s>", curi);
14553
14554
14555 if (ast_strlen_zero(curi) || parse_uri_legacy_check(curi, "sip:,sips:", &curi, NULL, &hostport, &transport)) {
14556 ast_log(LOG_NOTICE, "Not a valid SIP contact (missing sip:/sips:) trying to use anyway\n");
14557 }
14558
14559
14560 if (!(transport_type = get_transport_str2enum(transport))) {
14561 transport_type = pvt->socket.type;
14562 }
14563
14564
14565
14566
14567 if ((peer->socket.type != transport_type) && (peer->transports & transport_type)) {
14568 set_socket_transport(&peer->socket, transport_type);
14569 }
14570
14571 oldsin = peer->addr;
14572
14573
14574 if (!ast_sockaddr_isnull(&peer->addr) && (!peer->is_realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS))) {
14575 ao2_t_unlink(peers_by_ip, peer, "ao2_unlink of peer from peers_by_ip table");
14576 }
14577
14578 if (!ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) && !ast_test_flag(&peer->flags[0], SIP_NAT_RPORT_PRESENT)) {
14579
14580 ast_debug(1, "Store REGISTER's Contact header for call routing.\n");
14581
14582
14583 if (ast_sockaddr_resolve_first_transport(&testsa, hostport, 0, peer->socket.type)) {
14584 ast_log(LOG_WARNING, "Invalid hostport '%s'\n", hostport);
14585 ast_string_field_set(peer, fullcontact, "");
14586 ast_string_field_set(pvt, our_contact, "");
14587 return PARSE_REGISTER_FAILED;
14588 }
14589
14590
14591
14592 peer->portinuri = ast_sockaddr_port(&testsa) ? TRUE : FALSE;
14593
14594 if (!ast_sockaddr_port(&testsa)) {
14595 ast_sockaddr_set_port(&testsa, default_sip_port(transport_type));
14596 }
14597
14598 ast_sockaddr_copy(&peer->addr, &testsa);
14599 } else {
14600
14601
14602 ast_debug(1, "Store REGISTER's src-IP:port for call routing.\n");
14603 peer->addr = pvt->recv;
14604 }
14605
14606
14607 if (ast_apply_ha(sip_cfg.contact_ha, &peer->addr) != AST_SENSE_ALLOW ||
14608 ast_apply_ha(peer->contactha, &peer->addr) != AST_SENSE_ALLOW) {
14609 ast_log(LOG_WARNING, "Domain '%s' disallowed by contact ACL (violating IP %s)\n", hostport,
14610 ast_sockaddr_stringify_addr(&peer->addr));
14611 ast_string_field_set(peer, fullcontact, "");
14612 ast_string_field_set(pvt, our_contact, "");
14613 return PARSE_REGISTER_DENIED;
14614 }
14615
14616
14617
14618
14619 if ((peer->socket.type == pvt->socket.type) &&
14620 !ast_sockaddr_cmp(&peer->addr, &pvt->recv)) {
14621 copy_socket_data(&peer->socket, &pvt->socket);
14622 }
14623
14624
14625 if (!peer->is_realtime || ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
14626 ao2_t_link(peers_by_ip, peer, "ao2_link into peers_by_ip table");
14627 }
14628
14629
14630 peer->sipoptions = pvt->sipoptions;
14631
14632 if (!ast_strlen_zero(curi) && ast_strlen_zero(peer->username)) {
14633 ast_string_field_set(peer, username, curi);
14634 }
14635
14636 AST_SCHED_DEL_UNREF(sched, peer->expire,
14637 unref_peer(peer, "remove register expire ref"));
14638
14639 if (peer->is_realtime && !ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
14640 peer->expire = -1;
14641 } else {
14642 peer->expire = ast_sched_add(sched, (expire + 10) * 1000, expire_register,
14643 ref_peer(peer, "add registration ref"));
14644 if (peer->expire == -1) {
14645 unref_peer(peer, "remote registration ref");
14646 }
14647 }
14648 snprintf(data, sizeof(data), "%s:%d:%s:%s", ast_sockaddr_stringify(&peer->addr),
14649 expire, peer->username, peer->fullcontact);
14650
14651 if (!peer->rt_fromcontact || !sip_cfg.peer_rtupdate)
14652 ast_db_put("SIP/Registry", peer->name, data);
14653 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\nAddress: %s\r\n", peer->name, ast_sockaddr_stringify(&peer->addr));
14654
14655
14656 if (VERBOSITY_ATLEAST(2) && ast_sockaddr_cmp(&peer->addr, &oldsin)) {
14657 ast_verbose(VERBOSE_PREFIX_3 "Registered SIP '%s' at %s\n", peer->name,
14658 ast_sockaddr_stringify(&peer->addr));
14659 }
14660 sip_pvt_unlock(pvt);
14661 sip_poke_peer(peer, 0);
14662 sip_pvt_lock(pvt);
14663 register_peer_exten(peer, 1);
14664
14665
14666 useragent = get_header(req, "User-Agent");
14667 if (strcasecmp(useragent, peer->useragent)) {
14668 ast_string_field_set(peer, useragent, useragent);
14669 ast_verb(4, "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
14670 }
14671 return PARSE_REGISTER_UPDATE;
14672 }
14673
14674
14675 static void free_old_route(struct sip_route *route)
14676 {
14677 struct sip_route *next;
14678
14679 while (route) {
14680 next = route->next;
14681 ast_free(route);
14682 route = next;
14683 }
14684 }
14685
14686
14687 static void list_route(struct sip_route *route)
14688 {
14689 if (!route) {
14690 ast_verbose("list_route: no route\n");
14691 } else {
14692 for (;route; route = route->next)
14693 ast_verbose("list_route: hop: <%s>\n", route->hop);
14694 }
14695 }
14696
14697
14698
14699 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards, int resp)
14700 {
14701 struct sip_route *thishop, *head, *tail;
14702 int start = 0;
14703 int len;
14704 const char *rr, *c;
14705
14706
14707 if (p->route && p->route_persistent) {
14708 ast_debug(1, "build_route: Retaining previous route: <%s>\n", p->route->hop);
14709 return;
14710 }
14711
14712 if (p->route) {
14713 free_old_route(p->route);
14714 p->route = NULL;
14715 }
14716
14717
14718
14719 if ((resp < 100) || (resp > 199)) {
14720 p->route_persistent = 1;
14721 }
14722
14723
14724
14725
14726
14727
14728 head = NULL;
14729 tail = head;
14730
14731 for (;;) {
14732
14733 int len = 0;
14734 const char *uri;
14735 rr = __get_header(req, "Record-Route", &start);
14736 if (*rr == '\0') {
14737 break;
14738 }
14739 while (!get_in_brackets_const(rr, &uri, &len)) {
14740 len++;
14741 rr = strchr(rr, ',');
14742 if(rr >= uri && rr < (uri + len)) {
14743
14744 const char *next_br = strchr(rr, '<');
14745 if (next_br && next_br < (uri + len)) {
14746 rr++;
14747 continue;
14748 }
14749 continue;
14750 }
14751 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
14752 ast_copy_string(thishop->hop, uri, len);
14753 ast_debug(2, "build_route: Record-Route hop: <%s>\n", thishop->hop);
14754
14755 if (backwards) {
14756
14757 thishop->next = head;
14758 head = thishop;
14759
14760 if (!tail) {
14761 tail = thishop;
14762 }
14763 } else {
14764 thishop->next = NULL;
14765
14766 if (tail) {
14767 tail->next = thishop;
14768 } else {
14769 head = thishop;
14770 }
14771 tail = thishop;
14772 }
14773 }
14774 rr = strchr(uri + len, ',');
14775 if (rr == NULL) {
14776
14777 break;
14778 }
14779
14780 rr++;
14781 }
14782 }
14783
14784
14785 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop, ";lr") == NULL) ) {
14786
14787
14788 char *contact = ast_strdupa(get_header(req, "Contact"));
14789 if (!ast_strlen_zero(contact)) {
14790 ast_debug(2, "build_route: Contact hop: %s\n", contact);
14791
14792 c = get_in_brackets(contact);
14793 len = strlen(c) + 1;
14794 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
14795
14796 ast_copy_string(thishop->hop, c, len);
14797 thishop->next = NULL;
14798
14799 if (tail) {
14800 tail->next = thishop;
14801 } else {
14802 head = thishop;
14803 }
14804 }
14805 }
14806 }
14807
14808
14809 p->route = head;
14810
14811
14812 if (sip_debug_test_pvt(p)) {
14813 list_route(p->route);
14814 }
14815 }
14816
14817
14818
14819
14820
14821
14822
14823 static void set_nonce_randdata(struct sip_pvt *p, int forceupdate)
14824 {
14825 if (p->stalenonce || forceupdate || ast_strlen_zero(p->randdata)) {
14826 ast_string_field_build(p, randdata, "%08lx", (unsigned long)ast_random());
14827 p->stalenonce = 0;
14828 }
14829 }
14830
14831 AST_THREADSTORAGE(check_auth_buf);
14832 #define CHECK_AUTH_BUF_INITLEN 256
14833
14834
14835
14836
14837
14838
14839 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
14840 const char *secret, const char *md5secret, int sipmethod,
14841 const char *uri, enum xmittype reliable, int ignore)
14842 {
14843 const char *response;
14844 char *reqheader, *respheader;
14845 const char *authtoken;
14846 char a1_hash[256];
14847 char resp_hash[256]="";
14848 char *c;
14849 int is_bogus_peer = 0;
14850 int wrongnonce = FALSE;
14851 int good_response;
14852 const char *usednonce = p->randdata;
14853 struct ast_str *buf;
14854 int res;
14855
14856
14857 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
14858 struct x {
14859 const char *key;
14860 const char *s;
14861 } *i, keys[] = {
14862 [K_RESP] = { "response=", "" },
14863 [K_URI] = { "uri=", "" },
14864 [K_USER] = { "username=", "" },
14865 [K_NONCE] = { "nonce=", "" },
14866 [K_LAST] = { NULL, NULL}
14867 };
14868
14869
14870 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
14871 return AUTH_SUCCESSFUL;
14872
14873
14874
14875 response = "401 Unauthorized";
14876
14877
14878
14879
14880
14881 auth_headers(WWW_AUTH, &respheader, &reqheader);
14882
14883 authtoken = get_header(req, reqheader);
14884 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
14885
14886
14887 if (!reliable) {
14888
14889
14890 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
14891
14892 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14893 }
14894 return AUTH_CHALLENGE_SENT;
14895 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
14896
14897 set_nonce_randdata(p, 1);
14898 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
14899
14900 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
14901 return AUTH_CHALLENGE_SENT;
14902 }
14903
14904
14905
14906
14907
14908
14909 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
14910 return AUTH_SECRET_FAILED;
14911 }
14912
14913
14914 res = ast_str_set(&buf, 0, "%s", authtoken);
14915
14916 if (res == AST_DYNSTR_BUILD_FAILED) {
14917 return AUTH_SECRET_FAILED;
14918 }
14919
14920 c = buf->str;
14921
14922 while(c && *(c = ast_skip_blanks(c)) ) {
14923 for (i = keys; i->key != NULL; i++) {
14924 const char *separator = ",";
14925
14926 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
14927 continue;
14928 }
14929
14930 c += strlen(i->key);
14931 if (*c == '"') {
14932 c++;
14933 separator = "\"";
14934 }
14935 i->s = c;
14936 strsep(&c, separator);
14937 break;
14938 }
14939 if (i->key == NULL) {
14940 strsep(&c, " ,");
14941 }
14942 }
14943
14944
14945
14946 if (md5secret && strcmp(md5secret, BOGUS_PEER_MD5SECRET) == 0) {
14947 is_bogus_peer = 1;
14948 }
14949
14950
14951 if (strcmp(username, keys[K_USER].s) && !is_bogus_peer) {
14952 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
14953 username, keys[K_USER].s);
14954
14955 return AUTH_USERNAME_MISMATCH;
14956 }
14957
14958
14959
14960 if (strcasecmp(p->randdata, keys[K_NONCE].s) || p->stalenonce) {
14961 wrongnonce = TRUE;
14962 usednonce = keys[K_NONCE].s;
14963 } else {
14964 p->stalenonce = 1;
14965 }
14966
14967 if (!ast_strlen_zero(md5secret)) {
14968 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
14969 } else {
14970 char a1[256];
14971
14972 snprintf(a1, sizeof(a1), "%s:%s:%s", username, p->realm, secret);
14973 ast_md5_hash(a1_hash, a1);
14974 }
14975
14976
14977 {
14978 char a2[256];
14979 char a2_hash[256];
14980 char resp[256];
14981
14982 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
14983 S_OR(keys[K_URI].s, uri));
14984 ast_md5_hash(a2_hash, a2);
14985 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
14986 ast_md5_hash(resp_hash, resp);
14987 }
14988
14989 good_response = keys[K_RESP].s &&
14990 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash)) &&
14991 !is_bogus_peer;
14992 if (wrongnonce) {
14993 if (good_response) {
14994 if (sipdebug)
14995 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "From"));
14996
14997 set_nonce_randdata(p, 0);
14998 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
14999 } else {
15000
15001 if (!req->ignore) {
15002 if (sipdebug) {
15003 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
15004 }
15005 set_nonce_randdata(p, 1);
15006 } else {
15007 if (sipdebug) {
15008 ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", get_header(req, "To"));
15009 }
15010 }
15011 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
15012 }
15013
15014
15015 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15016 return AUTH_CHALLENGE_SENT;
15017 }
15018 if (good_response) {
15019 append_history(p, "AuthOK", "Auth challenge successful for %s", username);
15020 return AUTH_SUCCESSFUL;
15021 }
15022
15023
15024
15025
15026
15027
15028 return AUTH_SECRET_FAILED;
15029 }
15030
15031
15032 static void sip_peer_hold(struct sip_pvt *p, int hold)
15033 {
15034 if (!p->relatedpeer) {
15035 return;
15036 }
15037
15038
15039 ast_atomic_fetchadd_int(&p->relatedpeer->onHold, (hold ? +1 : -1));
15040
15041
15042 ast_devstate_changed(AST_DEVICE_UNKNOWN, (p->owner->flags & AST_FLAG_DISABLE_DEVSTATE_CACHE ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE),
15043 "SIP/%s", p->relatedpeer->name);
15044
15045 return;
15046 }
15047
15048
15049 static void mwi_event_cb(const struct ast_event *event, void *userdata)
15050 {
15051 struct sip_peer *peer = userdata;
15052
15053 sip_send_mwi_to_peer(peer, 0);
15054 }
15055
15056 static void network_change_event_subscribe(void)
15057 {
15058 if (!network_change_event_subscription) {
15059 network_change_event_subscription = ast_event_subscribe(AST_EVENT_NETWORK_CHANGE,
15060 network_change_event_cb, "SIP Network Change", NULL, AST_EVENT_IE_END);
15061 }
15062 }
15063
15064 static void network_change_event_unsubscribe(void)
15065 {
15066 if (network_change_event_subscription) {
15067 network_change_event_subscription = ast_event_unsubscribe(network_change_event_subscription);
15068 }
15069 }
15070
15071 static int network_change_event_sched_cb(const void *data)
15072 {
15073 network_change_event_sched_id = -1;
15074 sip_send_all_registers();
15075 sip_send_all_mwi_subscriptions();
15076 return 0;
15077 }
15078
15079 static void network_change_event_cb(const struct ast_event *event, void *userdata)
15080 {
15081 ast_debug(1, "SIP, got a network change event, renewing all SIP registrations.\n");
15082 if (network_change_event_sched_id == -1) {
15083 network_change_event_sched_id = ast_sched_add(sched, 1000, network_change_event_sched_cb, NULL);
15084 }
15085 }
15086
15087 static void cb_extensionstate_destroy(int id, void *data)
15088 {
15089 struct sip_pvt *p = data;
15090
15091 dialog_unref(p, "the extensionstate containing this dialog ptr was destroyed");
15092 }
15093
15094
15095
15096
15097 static int cb_extensionstate(char *context, char* exten, int state, void *data)
15098 {
15099 struct sip_pvt *p = data;
15100
15101 sip_pvt_lock(p);
15102
15103 switch(state) {
15104 case AST_EXTENSION_DEACTIVATED:
15105 case AST_EXTENSION_REMOVED:
15106 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15107 ast_verb(2, "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
15108 p->subscribed = NONE;
15109 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
15110 break;
15111 default:
15112 p->laststate = state;
15113 break;
15114 }
15115 if (p->subscribed != NONE) {
15116 if (!p->pendinginvite) {
15117 transmit_state_notify(p, state, 1, FALSE);
15118 } else {
15119
15120
15121 ast_set_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
15122 }
15123 }
15124 ast_verb(2, "Extension Changed %s[%s] new state %s for Notify User %s %s\n", exten, context, ast_extension_state2str(state), p->username,
15125 ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE) ? "(queued)" : "");
15126
15127 sip_pvt_unlock(p);
15128
15129 return 0;
15130 }
15131
15132
15133
15134
15135 static void transmit_fake_auth_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable)
15136 {
15137
15138
15139 const char *response = "401 Unauthorized";
15140 const char *reqheader = "Authorization";
15141 const char *respheader = "WWW-Authenticate";
15142 const char *authtoken;
15143 struct ast_str *buf;
15144 char *c;
15145
15146
15147 enum keys { K_NONCE, K_LAST };
15148 struct x {
15149 const char *key;
15150 const char *s;
15151 } *i, keys[] = {
15152 [K_NONCE] = { "nonce=", "" },
15153 [K_LAST] = { NULL, NULL}
15154 };
15155
15156 authtoken = get_header(req, reqheader);
15157 if (req->ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
15158
15159
15160 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
15161
15162 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15163 return;
15164 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
15165
15166 set_nonce_randdata(p, 1);
15167 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
15168
15169 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15170 return;
15171 }
15172
15173 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
15174 __transmit_response(p, "403 Forbidden", &p->initreq, reliable);
15175 return;
15176 }
15177
15178
15179 if (ast_str_set(&buf, 0, "%s", authtoken) == AST_DYNSTR_BUILD_FAILED) {
15180 __transmit_response(p, "403 Forbidden", &p->initreq, reliable);
15181 return;
15182 }
15183
15184 c = buf->str;
15185
15186 while (c && *(c = ast_skip_blanks(c))) {
15187 for (i = keys; i->key != NULL; i++) {
15188 const char *separator = ",";
15189
15190 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
15191 continue;
15192 }
15193
15194 c += strlen(i->key);
15195 if (*c == '"') {
15196 c++;
15197 separator = "\"";
15198 }
15199 i->s = c;
15200 strsep(&c, separator);
15201 break;
15202 }
15203 if (i->key == NULL) {
15204 strsep(&c, " ,");
15205 }
15206 }
15207
15208
15209 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
15210 if (!req->ignore) {
15211 set_nonce_randdata(p, 1);
15212 }
15213 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
15214
15215
15216 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15217 } else {
15218 __transmit_response(p, "403 Forbidden", &p->initreq, reliable);
15219 }
15220 }
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236 static char *terminate_uri(char *uri)
15237 {
15238 char *t = uri;
15239 while (*t && *t > ' ' && *t != ';') {
15240 t++;
15241 }
15242 *t = '\0';
15243 return uri;
15244 }
15245
15246
15247
15248
15249
15250
15251
15252 static void extract_host_from_hostport(char **hostport)
15253 {
15254 char *dont_care;
15255 ast_sockaddr_split_hostport(*hostport, hostport, &dont_care, PARSE_PORT_IGNORE);
15256 }
15257
15258
15259
15260 static void update_peer_lastmsgssent(struct sip_peer *peer, int value, int locked)
15261 {
15262 if (!locked) {
15263 ao2_lock(peer);
15264 }
15265 peer->lastmsgssent = value;
15266 if (!locked) {
15267 ao2_unlock(peer);
15268 }
15269 }
15270
15271
15272
15273
15274
15275
15276
15277 static enum check_auth_result register_verify(struct sip_pvt *p, struct ast_sockaddr *addr,
15278 struct sip_request *req, const char *uri)
15279 {
15280 enum check_auth_result res = AUTH_NOT_FOUND;
15281 struct sip_peer *peer;
15282 char tmp[256];
15283 char *c, *name, *unused_password, *domain;
15284 char *uri2 = ast_strdupa(uri);
15285 int send_mwi = 0;
15286
15287 terminate_uri(uri2);
15288
15289 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
15290
15291 c = get_in_brackets(tmp);
15292 c = remove_uri_parameters(c);
15293
15294 if (parse_uri_legacy_check(c, "sip:,sips:", &name, &unused_password, &domain, NULL)) {
15295 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_sockaddr_stringify_addr(addr));
15296 return -1;
15297 }
15298
15299 SIP_PEDANTIC_DECODE(name);
15300 SIP_PEDANTIC_DECODE(domain);
15301
15302 extract_host_from_hostport(&domain);
15303
15304 if (ast_strlen_zero(domain)) {
15305
15306 transmit_response(p, "404 Not found", &p->initreq);
15307 return AUTH_UNKNOWN_DOMAIN;
15308 }
15309
15310 if (ast_strlen_zero(name)) {
15311
15312
15313
15314
15315
15316
15317
15318 name = domain;
15319 }
15320
15321
15322
15323
15324
15325 if (!AST_LIST_EMPTY(&domain_list)) {
15326 if (!check_sip_domain(domain, NULL, 0)) {
15327 if (sip_cfg.alwaysauthreject) {
15328 transmit_fake_auth_response(p, &p->initreq, XMIT_UNRELIABLE);
15329 } else {
15330 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
15331 }
15332 return AUTH_UNKNOWN_DOMAIN;
15333 }
15334 }
15335
15336 ast_string_field_set(p, exten, name);
15337 build_contact(p);
15338 if (req->ignore) {
15339
15340 const char *expires = get_header(req, "Expires");
15341 int expire = atoi(expires);
15342
15343 if (ast_strlen_zero(expires)) {
15344 if ((expires = strcasestr(get_header(req, "Contact"), ";expires="))) {
15345 expire = atoi(expires + 9);
15346 }
15347 }
15348 if (!ast_strlen_zero(expires) && expire == 0) {
15349 transmit_response_with_date(p, "200 OK", req);
15350 return 0;
15351 }
15352 }
15353 peer = find_peer(name, NULL, TRUE, FINDPEERS, FALSE, 0);
15354
15355
15356
15357 if (!peer && sip_cfg.alwaysauthreject && !sip_cfg.autocreatepeer) {
15358 peer = bogus_peer;
15359 ref_peer(peer, "register_verify: ref the bogus_peer");
15360 }
15361
15362 if (!(peer && ast_apply_ha(peer->ha, addr))) {
15363
15364 if (peer) {
15365 unref_peer(peer, "register_verify: unref_peer: from find_peer operation");
15366 peer = NULL;
15367 res = AUTH_ACL_FAILED;
15368 } else {
15369 res = AUTH_NOT_FOUND;
15370 }
15371 }
15372
15373 if (peer) {
15374 ao2_lock(peer);
15375 if (!peer->host_dynamic) {
15376 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
15377 res = AUTH_PEER_NOT_DYNAMIC;
15378 } else {
15379 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT_FORCE_RPORT);
15380 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri2, XMIT_UNRELIABLE, req->ignore))) {
15381 if (sip_cancel_destroy(p))
15382 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15383
15384 if (check_request_transport(peer, req)) {
15385 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
15386 transmit_response_with_date(p, "403 Forbidden", req);
15387 res = AUTH_BAD_TRANSPORT;
15388 } else {
15389
15390
15391
15392 switch (parse_register_contact(p, peer, req)) {
15393 case PARSE_REGISTER_DENIED:
15394 ast_log(LOG_WARNING, "Registration denied because of contact ACL\n");
15395 transmit_response_with_date(p, "603 Denied", req);
15396 res = 0;
15397 break;
15398 case PARSE_REGISTER_FAILED:
15399 ast_log(LOG_WARNING, "Failed to parse contact info\n");
15400 transmit_response_with_date(p, "400 Bad Request", req);
15401 res = 0;
15402 break;
15403 case PARSE_REGISTER_QUERY:
15404 ast_string_field_set(p, fullcontact, peer->fullcontact);
15405 transmit_response_with_date(p, "200 OK", req);
15406 res = 0;
15407 break;
15408 case PARSE_REGISTER_UPDATE:
15409 ast_string_field_set(p, fullcontact, peer->fullcontact);
15410
15411 if (p->expiry != 0) {
15412 update_peer(peer, p->expiry);
15413 }
15414
15415 transmit_response_with_date(p, "200 OK", req);
15416 send_mwi = 1;
15417 res = 0;
15418 break;
15419 }
15420 }
15421
15422 }
15423 }
15424 ao2_unlock(peer);
15425 }
15426 if (!peer && sip_cfg.autocreatepeer) {
15427
15428 peer = temp_peer(name);
15429 if (peer) {
15430 ao2_t_link(peers, peer, "link peer into peer table");
15431 if (!ast_sockaddr_isnull(&peer->addr)) {
15432 ao2_t_link(peers_by_ip, peer, "link peer into peers-by-ip table");
15433 }
15434 ao2_lock(peer);
15435 if (sip_cancel_destroy(p))
15436 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
15437 switch (parse_register_contact(p, peer, req)) {
15438 case PARSE_REGISTER_DENIED:
15439 ast_log(LOG_WARNING, "Registration denied because of contact ACL\n");
15440 transmit_response_with_date(p, "403 Forbidden", req);
15441 res = 0;
15442 break;
15443 case PARSE_REGISTER_FAILED:
15444 ast_log(LOG_WARNING, "Failed to parse contact info\n");
15445 transmit_response_with_date(p, "400 Bad Request", req);
15446 res = 0;
15447 break;
15448 case PARSE_REGISTER_QUERY:
15449 ast_string_field_set(p, fullcontact, peer->fullcontact);
15450 transmit_response_with_date(p, "200 OK", req);
15451 send_mwi = 1;
15452 res = 0;
15453 break;
15454 case PARSE_REGISTER_UPDATE:
15455 ast_string_field_set(p, fullcontact, peer->fullcontact);
15456
15457 transmit_response_with_date(p, "200 OK", req);
15458 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Registered\r\nAddress: %s\r\n", peer->name, ast_sockaddr_stringify(addr));
15459 send_mwi = 1;
15460 res = 0;
15461 break;
15462 }
15463 ao2_unlock(peer);
15464 }
15465 }
15466 if (!res) {
15467 if (send_mwi) {
15468 sip_pvt_unlock(p);
15469 sip_send_mwi_to_peer(peer, 0);
15470 sip_pvt_lock(p);
15471 } else {
15472 update_peer_lastmsgssent(peer, -1, 0);
15473 }
15474 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "SIP/%s", peer->name);
15475 }
15476 if (res < 0) {
15477 switch (res) {
15478 case AUTH_SECRET_FAILED:
15479
15480 transmit_response(p, "403 Forbidden", &p->initreq);
15481 if (global_authfailureevents) {
15482 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
15483 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
15484 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
15485 "ChannelType: SIP\r\n"
15486 "Peer: SIP/%s\r\n"
15487 "PeerStatus: Rejected\r\n"
15488 "Cause: AUTH_SECRET_FAILED\r\n"
15489 "Address: %s\r\n"
15490 "Port: %s\r\n",
15491 name, peer_addr, peer_port);
15492 }
15493 break;
15494 case AUTH_USERNAME_MISMATCH:
15495
15496
15497
15498
15499 case AUTH_NOT_FOUND:
15500 case AUTH_PEER_NOT_DYNAMIC:
15501 case AUTH_ACL_FAILED:
15502 if (sip_cfg.alwaysauthreject) {
15503 transmit_fake_auth_response(p, &p->initreq, XMIT_UNRELIABLE);
15504 if (global_authfailureevents) {
15505 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
15506 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
15507 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
15508 "ChannelType: SIP\r\n"
15509 "Peer: SIP/%s\r\n"
15510 "PeerStatus: Rejected\r\n"
15511 "Cause: %s\r\n"
15512 "Address: %s\r\n"
15513 "Port: %s\r\n",
15514 name,
15515 res == AUTH_PEER_NOT_DYNAMIC ? "AUTH_PEER_NOT_DYNAMIC" : "URI_NOT_FOUND",
15516 peer_addr, peer_port);
15517 }
15518 } else {
15519
15520 if (res == AUTH_PEER_NOT_DYNAMIC) {
15521 transmit_response(p, "403 Forbidden", &p->initreq);
15522 if (global_authfailureevents) {
15523 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
15524 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
15525 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
15526 "ChannelType: SIP\r\n"
15527 "Peer: SIP/%s\r\n"
15528 "PeerStatus: Rejected\r\n"
15529 "Cause: AUTH_PEER_NOT_DYNAMIC\r\n"
15530 "Address: %s\r\n"
15531 "Port: %s\r\n",
15532 name, peer_addr, peer_port);
15533 }
15534 } else {
15535 transmit_response(p, "404 Not found", &p->initreq);
15536 if (global_authfailureevents) {
15537 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
15538 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
15539 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
15540 "ChannelType: SIP\r\n"
15541 "Peer: SIP/%s\r\n"
15542 "PeerStatus: Rejected\r\n"
15543 "Cause: %s\r\n"
15544 "Address: %s\r\n"
15545 "Port: %s\r\n",
15546 name,
15547 (res == AUTH_USERNAME_MISMATCH) ? "AUTH_USERNAME_MISMATCH" : "URI_NOT_FOUND",
15548 peer_addr, peer_port);
15549 }
15550 }
15551 }
15552 break;
15553 case AUTH_BAD_TRANSPORT:
15554 default:
15555 break;
15556 }
15557 }
15558 if (peer) {
15559 unref_peer(peer, "register_verify: unref_peer: tossing stack peer pointer at end of func");
15560 }
15561
15562 return res;
15563 }
15564
15565
15566 static void sip_set_redirstr(struct sip_pvt *p, char *reason) {
15567
15568 if (!strcmp(reason, "unknown")) {
15569 ast_string_field_set(p, redircause, "UNKNOWN");
15570 } else if (!strcmp(reason, "user-busy")) {
15571 ast_string_field_set(p, redircause, "BUSY");
15572 } else if (!strcmp(reason, "no-answer")) {
15573 ast_string_field_set(p, redircause, "NOANSWER");
15574 } else if (!strcmp(reason, "unavailable")) {
15575 ast_string_field_set(p, redircause, "UNREACHABLE");
15576 } else if (!strcmp(reason, "unconditional")) {
15577 ast_string_field_set(p, redircause, "UNCONDITIONAL");
15578 } else if (!strcmp(reason, "time-of-day")) {
15579 ast_string_field_set(p, redircause, "UNKNOWN");
15580 } else if (!strcmp(reason, "do-not-disturb")) {
15581 ast_string_field_set(p, redircause, "UNKNOWN");
15582 } else if (!strcmp(reason, "deflection")) {
15583 ast_string_field_set(p, redircause, "UNKNOWN");
15584 } else if (!strcmp(reason, "follow-me")) {
15585 ast_string_field_set(p, redircause, "UNKNOWN");
15586 } else if (!strcmp(reason, "out-of-service")) {
15587 ast_string_field_set(p, redircause, "UNREACHABLE");
15588 } else if (!strcmp(reason, "away")) {
15589 ast_string_field_set(p, redircause, "UNREACHABLE");
15590 } else {
15591 ast_string_field_set(p, redircause, "UNKNOWN");
15592 }
15593 }
15594
15595
15596
15597
15598
15599 static int get_pai(struct sip_pvt *p, struct sip_request *req)
15600 {
15601 char pai[256];
15602 char privacy[64];
15603 char *cid_num = NULL;
15604 char *cid_name = NULL;
15605 char emptyname[1] = "";
15606 int callingpres = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
15607 char *uri = NULL;
15608 int is_anonymous = 0, do_update = 1, no_name = 0;
15609
15610 ast_copy_string(pai, get_header(req, "P-Asserted-Identity"), sizeof(pai));
15611
15612 if (ast_strlen_zero(pai)) {
15613 return 0;
15614 }
15615
15616
15617 if (get_name_and_number(pai, &cid_name, &cid_num)) {
15618 return 0;
15619 }
15620
15621 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(cid_num)) {
15622 ast_shrink_phone_number(cid_num);
15623 }
15624
15625 uri = get_in_brackets(pai);
15626 if (!strncasecmp(uri, "sip:anonymous@anonymous.invalid", 31)) {
15627 callingpres = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
15628
15629
15630
15631 ast_free(cid_num);
15632 is_anonymous = 1;
15633 cid_num = (char *)p->cid_num;
15634 }
15635
15636 ast_copy_string(privacy, get_header(req, "Privacy"), sizeof(privacy));
15637 if (!ast_strlen_zero(privacy) && !strncmp(privacy, "id", 2)) {
15638 callingpres = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
15639 }
15640 if (!cid_name) {
15641 no_name = 1;
15642 cid_name = (char *)emptyname;
15643 }
15644
15645 if (!strcasecmp(p->cid_num, cid_num) && !strcasecmp(p->cid_name, cid_name) && p->callingpres == callingpres) {
15646 do_update = 0;
15647 } else {
15648
15649 ast_string_field_set(p, cid_num, cid_num);
15650 ast_string_field_set(p, cid_name, cid_name);
15651 p->callingpres = callingpres;
15652
15653 if (p->owner) {
15654 ast_set_callerid(p->owner, cid_num, cid_name, NULL);
15655 p->owner->caller.id.name.presentation = callingpres;
15656 p->owner->caller.id.number.presentation = callingpres;
15657 }
15658 }
15659
15660
15661 if (!is_anonymous) {
15662 ast_free(cid_num);
15663 }
15664 if (!no_name) {
15665 ast_free(cid_name);
15666 }
15667
15668 return do_update;
15669 }
15670
15671
15672
15673
15674
15675 static int get_rpid(struct sip_pvt *p, struct sip_request *oreq)
15676 {
15677 char tmp[256];
15678 struct sip_request *req;
15679 char *cid_num = "";
15680 char *cid_name = "";
15681 int callingpres = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
15682 char *privacy = "";
15683 char *screen = "";
15684 char *start, *end;
15685
15686 if (!ast_test_flag(&p->flags[0], SIP_TRUSTRPID))
15687 return 0;
15688 req = oreq;
15689 if (!req)
15690 req = &p->initreq;
15691 ast_copy_string(tmp, get_header(req, "Remote-Party-ID"), sizeof(tmp));
15692 if (ast_strlen_zero(tmp)) {
15693 return get_pai(p, req);
15694 }
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704 start = tmp;
15705
15706 if (*start == '"') {
15707 *start++ = '\0';
15708 end = strchr(start, '"');
15709 if (!end)
15710 return 0;
15711 *end++ = '\0';
15712 cid_name = start;
15713 start = ast_skip_blanks(end);
15714
15715 } else {
15716 cid_name = start;
15717 start = end = strchr(start, '<');
15718 if (!start) {
15719 return 0;
15720 }
15721
15722 while (--end >= cid_name && *end < 33) {
15723 *end = '\0';
15724 }
15725 }
15726
15727 if (*start != '<')
15728 return 0;
15729 *start++ = '\0';
15730 end = strchr(start, '@');
15731 if (!end)
15732 return 0;
15733 *end++ = '\0';
15734 if (strncasecmp(start, "sip:", 4))
15735 return 0;
15736 cid_num = start + 4;
15737 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(cid_num))
15738 ast_shrink_phone_number(cid_num);
15739 start = end;
15740
15741 end = strchr(start, '>');
15742 if (!end)
15743 return 0;
15744 *end++ = '\0';
15745 if (*end) {
15746 start = end;
15747 if (*start != ';')
15748 return 0;
15749 *start++ = '\0';
15750 while (!ast_strlen_zero(start)) {
15751 end = strchr(start, ';');
15752 if (end)
15753 *end++ = '\0';
15754 if (!strncasecmp(start, "privacy=", 8))
15755 privacy = start + 8;
15756 else if (!strncasecmp(start, "screen=", 7))
15757 screen = start + 7;
15758 start = end;
15759 }
15760
15761 if (!strcasecmp(privacy, "full")) {
15762 if (!strcasecmp(screen, "yes"))
15763 callingpres = AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
15764 else if (!strcasecmp(screen, "no"))
15765 callingpres = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
15766 } else {
15767 if (!strcasecmp(screen, "yes"))
15768 callingpres = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
15769 else if (!strcasecmp(screen, "no"))
15770 callingpres = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
15771 }
15772 }
15773
15774
15775 if (!strcasecmp(p->cid_num, cid_num) && !strcasecmp(p->cid_name, cid_name) && p->callingpres == callingpres)
15776 return 0;
15777
15778 ast_string_field_set(p, cid_num, cid_num);
15779 ast_string_field_set(p, cid_name, cid_name);
15780 p->callingpres = callingpres;
15781
15782 if (p->owner) {
15783 ast_set_callerid(p->owner, cid_num, cid_name, NULL);
15784 p->owner->caller.id.name.presentation = callingpres;
15785 p->owner->caller.id.number.presentation = callingpres;
15786 }
15787
15788 return 1;
15789 }
15790
15791
15792 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq, char **name, char **number, int *reason)
15793 {
15794 char tmp[256], *exten, *rexten, *rdomain, *rname = NULL;
15795 char *params, *reason_param = NULL;
15796 struct sip_request *req;
15797
15798 req = oreq ? oreq : &p->initreq;
15799
15800 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
15801 if (ast_strlen_zero(tmp))
15802 return -1;
15803
15804 if ((params = strchr(tmp, '>'))) {
15805 params = strchr(params, ';');
15806 }
15807
15808 exten = get_in_brackets(tmp);
15809 if (!strncasecmp(exten, "sip:", 4)) {
15810 exten += 4;
15811 } else if (!strncasecmp(exten, "sips:", 5)) {
15812 exten += 5;
15813 } else {
15814 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", exten);
15815 return -1;
15816 }
15817
15818
15819 if (params) {
15820 *params = '\0';
15821 params++;
15822 while (*params == ';' || *params == ' ')
15823 params++;
15824
15825 if ((reason_param = strcasestr(params, "reason="))) {
15826 char *end;
15827 reason_param+=7;
15828 if ((end = strchr(reason_param, ';'))) {
15829 *end = '\0';
15830 }
15831
15832 if (*reason_param == '"')
15833 reason_param = ast_strip_quoted(reason_param, "\"", "\"");
15834 if (!ast_strlen_zero(reason_param)) {
15835 sip_set_redirstr(p, reason_param);
15836 if (p->owner) {
15837 pbx_builtin_setvar_helper(p->owner, "__PRIREDIRECTREASON", p->redircause);
15838 pbx_builtin_setvar_helper(p->owner, "__SIPREDIRECTREASON", reason_param);
15839 }
15840 }
15841 }
15842 }
15843
15844 rdomain = exten;
15845 rexten = strsep(&rdomain, "@");
15846 if (p->owner)
15847 pbx_builtin_setvar_helper(p->owner, "__SIPRDNISDOMAIN", rdomain);
15848
15849 if (sip_debug_test_pvt(p))
15850 ast_verbose("RDNIS for this call is %s (reason %s)\n", exten, S_OR(reason_param, ""));
15851
15852
15853
15854 if (*tmp == '\"') {
15855 char *end_quote;
15856 rname = tmp + 1;
15857 end_quote = strchr(rname, '\"');
15858 if (end_quote) {
15859 *end_quote = '\0';
15860 }
15861 }
15862
15863 if (number) {
15864 *number = ast_strdup(rexten);
15865 }
15866
15867 if (name && rname) {
15868 *name = ast_strdup(rname);
15869 }
15870
15871 if (reason && !ast_strlen_zero(reason_param)) {
15872 *reason = sip_reason_str_to_code(reason_param);
15873 }
15874
15875 return 0;
15876 }
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892 static enum sip_get_dest_result get_destination(struct sip_pvt *p, struct sip_request *oreq, int *cc_recall_core_id)
15893 {
15894 char tmp[256] = "", *uri, *unused_password, *domain;
15895 RAII_VAR(char *, tmpf, NULL, ast_free);
15896 char *from = NULL;
15897 struct sip_request *req;
15898 char *decoded_uri;
15899
15900 req = oreq;
15901 if (!req) {
15902 req = &p->initreq;
15903 }
15904
15905
15906 if (req->rlPart2)
15907 ast_copy_string(tmp, REQ_OFFSET_TO_STR(req, rlPart2), sizeof(tmp));
15908
15909 uri = ast_strdupa(get_in_brackets(tmp));
15910
15911 if (parse_uri_legacy_check(uri, "sip:,sips:", &uri, &unused_password, &domain, NULL)) {
15912 ast_log(LOG_WARNING, "Not a SIP header (%s)?\n", uri);
15913 return SIP_GET_DEST_INVALID_URI;
15914 }
15915
15916 SIP_PEDANTIC_DECODE(domain);
15917 SIP_PEDANTIC_DECODE(uri);
15918
15919 extract_host_from_hostport(&domain);
15920
15921 if (ast_strlen_zero(uri)) {
15922
15923
15924
15925
15926
15927 uri = "s";
15928 }
15929
15930 ast_string_field_set(p, domain, domain);
15931
15932
15933
15934
15935
15936 tmpf = ast_strdup(get_header(req, "From"));
15937 if (!ast_strlen_zero(tmpf)) {
15938 from = get_in_brackets(tmpf);
15939 if (parse_uri_legacy_check(from, "sip:,sips:", &from, NULL, &domain, NULL)) {
15940 ast_log(LOG_WARNING, "Not a SIP header (%s)?\n", from);
15941 return SIP_GET_DEST_INVALID_URI;
15942 }
15943
15944 SIP_PEDANTIC_DECODE(from);
15945 SIP_PEDANTIC_DECODE(domain);
15946
15947 extract_host_from_hostport(&domain);
15948
15949 ast_string_field_set(p, fromdomain, domain);
15950 }
15951
15952 if (!AST_LIST_EMPTY(&domain_list)) {
15953 char domain_context[AST_MAX_EXTENSION];
15954
15955 domain_context[0] = '\0';
15956 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
15957 if (!sip_cfg.allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
15958 ast_debug(1, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
15959 return SIP_GET_DEST_REFUSED;
15960 }
15961 }
15962
15963
15964 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_HAVEPEERCONTEXT) && !ast_strlen_zero(domain_context)) {
15965 ast_string_field_set(p, context, domain_context);
15966 }
15967 }
15968
15969
15970 if (req->method == SIP_SUBSCRIBE && !ast_strlen_zero(p->subscribecontext)) {
15971 ast_string_field_set(p, context, p->subscribecontext);
15972 }
15973
15974 if (sip_debug_test_pvt(p)) {
15975 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
15976 }
15977
15978
15979
15980 decoded_uri = ast_strdupa(uri);
15981 ast_uri_decode(decoded_uri);
15982
15983
15984 if (req->method == SIP_SUBSCRIBE) {
15985 char hint[AST_MAX_EXTENSION];
15986 int which = 0;
15987 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, uri) ||
15988 (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, decoded_uri) && (which = 1))) {
15989 if (!oreq) {
15990 ast_string_field_set(p, exten, which ? decoded_uri : uri);
15991 }
15992 return SIP_GET_DEST_EXTEN_FOUND;
15993 } else {
15994 return SIP_GET_DEST_EXTEN_NOT_FOUND;
15995 }
15996 } else {
15997 struct ast_cc_agent *agent;
15998
15999
16000
16001 if (ast_exists_extension(NULL, p->context, uri, 1, S_OR(p->cid_num, from))) {
16002 if (!oreq) {
16003 ast_string_field_set(p, exten, uri);
16004 }
16005 return SIP_GET_DEST_EXTEN_FOUND;
16006 }
16007 if (ast_exists_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from))
16008 || !strcmp(decoded_uri, ast_pickup_ext())) {
16009 if (!oreq) {
16010 ast_string_field_set(p, exten, decoded_uri);
16011 }
16012 return SIP_GET_DEST_EXTEN_FOUND;
16013 }
16014 if ((agent = find_sip_cc_agent_by_notify_uri(tmp))) {
16015 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
16016
16017
16018
16019 ast_string_field_set(p, exten, agent_pvt->original_exten);
16020
16021
16022
16023 ast_cc_agent_recalling(agent->core_id, "SIP caller %s is attempting recall",
16024 agent->device_name);
16025 if (cc_recall_core_id) {
16026 *cc_recall_core_id = agent->core_id;
16027 }
16028 ao2_ref(agent, -1);
16029 return SIP_GET_DEST_EXTEN_FOUND;
16030 }
16031 }
16032
16033 if (ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP)
16034 && (ast_canmatch_extension(NULL, p->context, uri, 1, S_OR(p->cid_num, from))
16035 || ast_canmatch_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from))
16036 || !strncmp(decoded_uri, ast_pickup_ext(), strlen(decoded_uri)))) {
16037
16038 return SIP_GET_DEST_EXTEN_MATCHMORE;
16039 }
16040
16041 return SIP_GET_DEST_EXTEN_NOT_FOUND;
16042 }
16043
16044
16045
16046
16047 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
16048 {
16049 struct sip_pvt *sip_pvt_ptr;
16050 struct sip_pvt tmp_dialog = {
16051 .callid = callid,
16052 };
16053
16054 if (totag) {
16055 ast_debug(4, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
16056 }
16057
16058
16059
16060 sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find of dialog in dialogs table");
16061 if (sip_pvt_ptr) {
16062
16063 sip_pvt_lock(sip_pvt_ptr);
16064 if (sip_cfg.pedanticsipchecking) {
16065 unsigned char frommismatch = 0, tomismatch = 0;
16066
16067 if (ast_strlen_zero(fromtag)) {
16068 sip_pvt_unlock(sip_pvt_ptr);
16069 ast_debug(4, "Matched %s call for callid=%s - no from tag specified, pedantic check fails\n",
16070 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid);
16071 return NULL;
16072 }
16073
16074 if (ast_strlen_zero(totag)) {
16075 sip_pvt_unlock(sip_pvt_ptr);
16076 ast_debug(4, "Matched %s call for callid=%s - no to tag specified, pedantic check fails\n",
16077 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid);
16078 return NULL;
16079 }
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094 frommismatch = !!strcmp(fromtag, sip_pvt_ptr->theirtag);
16095 tomismatch = !!strcmp(totag, sip_pvt_ptr->tag);
16096
16097
16098
16099
16100 if ((frommismatch && ast_test_flag(&sip_pvt_ptr->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) || tomismatch) {
16101 sip_pvt_unlock(sip_pvt_ptr);
16102 if (frommismatch) {
16103 ast_debug(4, "Matched %s call for callid=%s - pedantic from tag check fails; their tag is %s our tag is %s\n",
16104 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid,
16105 fromtag, sip_pvt_ptr->theirtag);
16106 }
16107 if (tomismatch) {
16108 ast_debug(4, "Matched %s call for callid=%s - pedantic to tag check fails; their tag is %s our tag is %s\n",
16109 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid,
16110 totag, sip_pvt_ptr->tag);
16111 }
16112 return NULL;
16113 }
16114 }
16115
16116 if (totag)
16117 ast_debug(4, "Matched %s call - their tag is %s Our tag is %s\n",
16118 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING",
16119 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
16120
16121
16122 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
16123 sip_pvt_unlock(sip_pvt_ptr);
16124 usleep(1);
16125 sip_pvt_lock(sip_pvt_ptr);
16126 }
16127 }
16128
16129 return sip_pvt_ptr;
16130 }
16131
16132
16133
16134
16135
16136
16137
16138
16139 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
16140 {
16141
16142 const char *p_referred_by = NULL;
16143 char *h_refer_to = NULL;
16144 char *h_referred_by = NULL;
16145 char *refer_to;
16146 const char *p_refer_to;
16147 char *referred_by_uri = NULL;
16148 char *ptr;
16149 struct sip_request *req = NULL;
16150 const char *transfer_context = NULL;
16151 struct sip_refer *referdata;
16152
16153
16154 req = outgoing_req;
16155 referdata = transferer->refer;
16156
16157 if (!req) {
16158 req = &transferer->initreq;
16159 }
16160
16161 p_refer_to = get_header(req, "Refer-To");
16162 if (ast_strlen_zero(p_refer_to)) {
16163 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
16164 return -2;
16165 }
16166 h_refer_to = ast_strdupa(p_refer_to);
16167 refer_to = get_in_brackets(h_refer_to);
16168 if (!strncasecmp(refer_to, "sip:", 4)) {
16169 refer_to += 4;
16170 } else if (!strncasecmp(refer_to, "sips:", 5)) {
16171 refer_to += 5;
16172 } else {
16173 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
16174 return -3;
16175 }
16176
16177
16178 p_referred_by = get_header(req, "Referred-By");
16179
16180
16181 if (transferer->owner) {
16182 struct ast_channel *peer = ast_bridged_channel(transferer->owner);
16183 if (peer) {
16184 pbx_builtin_setvar_helper(peer, "SIPREFERRINGCONTEXT", transferer->context);
16185 pbx_builtin_setvar_helper(peer, "SIPREFERREDBYHDR", p_referred_by);
16186 }
16187 }
16188
16189 if (!ast_strlen_zero(p_referred_by)) {
16190 char *lessthan;
16191 h_referred_by = ast_strdupa(p_referred_by);
16192
16193
16194 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
16195 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
16196 *(lessthan - 1) = '\0';
16197 }
16198
16199 referred_by_uri = get_in_brackets(h_referred_by);
16200
16201 if (!strncasecmp(referred_by_uri, "sip:", 4)) {
16202 referred_by_uri += 4;
16203 } else if (!strncasecmp(referred_by_uri, "sips:", 5)) {
16204 referred_by_uri += 5;
16205 } else {
16206 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
16207 referred_by_uri = NULL;
16208 }
16209 }
16210
16211
16212 if ((ptr = strcasestr(refer_to, "replaces="))) {
16213 char *to = NULL, *from = NULL;
16214
16215
16216 referdata->attendedtransfer = 1;
16217 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
16218 ast_uri_decode(referdata->replaces_callid);
16219 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
16220 *ptr++ = '\0';
16221 }
16222
16223 if (ptr) {
16224
16225 to = strcasestr(ptr, "to-tag=");
16226 from = strcasestr(ptr, "from-tag=");
16227 }
16228
16229
16230 if (to) {
16231 ptr = to + 7;
16232 if ((to = strchr(ptr, '&'))) {
16233 *to = '\0';
16234 }
16235 if ((to = strchr(ptr, ';'))) {
16236 *to = '\0';
16237 }
16238 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
16239 }
16240
16241 if (from) {
16242 ptr = from + 9;
16243 if ((to = strchr(ptr, '&'))) {
16244 *to = '\0';
16245 }
16246 if ((to = strchr(ptr, ';'))) {
16247 *to = '\0';
16248 }
16249 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
16250 }
16251
16252 if (!strcmp(referdata->replaces_callid, transferer->callid) &&
16253 (!sip_cfg.pedanticsipchecking ||
16254 (!strcmp(referdata->replaces_callid_fromtag, transferer->theirtag) &&
16255 !strcmp(referdata->replaces_callid_totag, transferer->tag)))) {
16256 ast_log(LOG_WARNING, "Got an attempt to replace own Call-ID on %s\n", transferer->callid);
16257 return -4;
16258 }
16259
16260 if (!sip_cfg.pedanticsipchecking) {
16261 ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
16262 } else {
16263 ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s F-tag: %s T-tag: %s\n", referdata->replaces_callid, referdata->replaces_callid_fromtag ? referdata->replaces_callid_fromtag : "<none>", referdata->replaces_callid_totag ? referdata->replaces_callid_totag : "<none>" );
16264 }
16265 }
16266
16267 if ((ptr = strchr(refer_to, '@'))) {
16268 char *urioption = NULL, *domain;
16269 int bracket = 0;
16270 *ptr++ = '\0';
16271
16272 if ((urioption = strchr(ptr, ';'))) {
16273 *urioption++ = '\0';
16274 }
16275
16276 domain = ptr;
16277
16278
16279 for (; *ptr != '\0'; ++ptr) {
16280 if (*ptr == ':' && bracket == 0) {
16281 *ptr = '\0';
16282 break;
16283 } else if (*ptr == '[') {
16284 ++bracket;
16285 } else if (*ptr == ']') {
16286 --bracket;
16287 }
16288 }
16289
16290 SIP_PEDANTIC_DECODE(domain);
16291 SIP_PEDANTIC_DECODE(urioption);
16292
16293
16294 ast_copy_string(referdata->refer_to_domain, domain, sizeof(referdata->refer_to_domain));
16295 if (urioption) {
16296 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
16297 }
16298 }
16299
16300 if ((ptr = strchr(refer_to, ';')))
16301 *ptr = '\0';
16302
16303 SIP_PEDANTIC_DECODE(refer_to);
16304 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
16305
16306 if (referred_by_uri) {
16307 if ((ptr = strchr(referred_by_uri, ';')))
16308 *ptr = '\0';
16309 SIP_PEDANTIC_DECODE(referred_by_uri);
16310 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
16311 } else {
16312 referdata->referred_by[0] = '\0';
16313 }
16314
16315
16316 if (transferer->owner) {
16317
16318 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
16319 if (ast_strlen_zero(transfer_context)) {
16320 transfer_context = transferer->owner->macrocontext;
16321 }
16322 }
16323 if (ast_strlen_zero(transfer_context)) {
16324 transfer_context = S_OR(transferer->context, sip_cfg.default_context);
16325 }
16326
16327 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
16328
16329
16330 if (referdata->attendedtransfer || ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
16331 if (sip_debug_test_pvt(transferer)) {
16332 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
16333 }
16334
16335 return 0;
16336 }
16337 if (sip_debug_test_pvt(transferer))
16338 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
16339
16340
16341 return -1;
16342 }
16343
16344
16345
16346
16347
16348 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
16349 {
16350 char tmp[256] = "", *c, *a;
16351 struct sip_request *req = oreq ? oreq : &p->initreq;
16352 struct sip_refer *referdata = NULL;
16353 const char *transfer_context = NULL;
16354
16355 if (!p->refer && !sip_refer_allocate(p))
16356 return -1;
16357
16358 referdata = p->refer;
16359
16360 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
16361 c = get_in_brackets(tmp);
16362
16363 if (parse_uri_legacy_check(c, "sip:,sips:", &c, NULL, &a, NULL)) {
16364 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
16365 return -1;
16366 }
16367
16368 SIP_PEDANTIC_DECODE(c);
16369 SIP_PEDANTIC_DECODE(a);
16370
16371 if (!ast_strlen_zero(a)) {
16372 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
16373 }
16374
16375 if (sip_debug_test_pvt(p))
16376 ast_verbose("Looking for %s in %s\n", c, p->context);
16377
16378
16379 if (p->owner) {
16380
16381 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
16382 if (ast_strlen_zero(transfer_context)) {
16383 transfer_context = p->owner->macrocontext;
16384 }
16385 }
16386 if (ast_strlen_zero(transfer_context)) {
16387 transfer_context = S_OR(p->context, sip_cfg.default_context);
16388 }
16389
16390 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
16391
16392 ast_debug(1, "SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
16393 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
16394 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
16395 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
16396
16397 ast_string_field_set(p, context, transfer_context);
16398 return 0;
16399 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
16400 return 1;
16401 }
16402
16403 return -1;
16404 }
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416 static attribute_unused void check_via_response(struct sip_pvt *p, struct sip_request *req)
16417 {
16418 char via[256];
16419 char *cur, *opts;
16420
16421 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
16422
16423
16424 opts = strchr(via, ',');
16425 if (opts)
16426 *opts = '\0';
16427
16428
16429 opts = strchr(via, ';');
16430 if (!opts)
16431 return;
16432 *opts++ = '\0';
16433 while ( (cur = strsep(&opts, ";")) ) {
16434 if (!strncmp(cur, "rport=", 6)) {
16435 int port = strtol(cur+6, NULL, 10);
16436
16437 ast_sockaddr_set_port(&p->ourip, port);
16438 } else if (!strncmp(cur, "received=", 9)) {
16439 if (ast_parse_arg(cur + 9, PARSE_ADDR, &p->ourip))
16440 ;
16441 }
16442 }
16443 }
16444
16445
16446 static void check_via(struct sip_pvt *p, const struct sip_request *req)
16447 {
16448 char via[512];
16449 char *c, *maddr;
16450 struct ast_sockaddr tmp = { { 0, } };
16451 uint16_t port;
16452
16453 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
16454
16455
16456 c = strchr(via, ',');
16457 if (c)
16458 *c = '\0';
16459
16460
16461 c = strstr(via, ";rport");
16462 if (c && (c[6] != '=')) {
16463 ast_set_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT);
16464 ast_set_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT);
16465 }
16466
16467
16468 maddr = strstr(via, "maddr=");
16469 if (maddr) {
16470 maddr += 6;
16471 c = maddr + strspn(maddr, "abcdefghijklmnopqrstuvwxyz"
16472 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.:[]");
16473 *c = '\0';
16474 }
16475
16476 c = strchr(via, ';');
16477 if (c)
16478 *c = '\0';
16479
16480 c = strchr(via, ' ');
16481 if (c) {
16482 *c = '\0';
16483 c = ast_skip_blanks(c+1);
16484 if (strcasecmp(via, "SIP/2.0/UDP") && strcasecmp(via, "SIP/2.0/TCP") && strcasecmp(via, "SIP/2.0/TLS")) {
16485 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
16486 return;
16487 }
16488
16489 if (maddr && ast_sockaddr_resolve_first(&p->sa, maddr, 0)) {
16490 p->sa = p->recv;
16491 }
16492
16493 if (ast_sockaddr_resolve_first(&tmp, c, 0)) {
16494 ast_log(LOG_WARNING, "Could not resolve socket address for '%s'\n", c);
16495 port = STANDARD_SIP_PORT;
16496 } else if (!(port = ast_sockaddr_port(&tmp))) {
16497 port = STANDARD_SIP_PORT;
16498 }
16499
16500 ast_sockaddr_set_port(&p->sa, port);
16501
16502 if (sip_debug_test_pvt(p)) {
16503 ast_verbose("Sending to %s (%s)\n",
16504 ast_sockaddr_stringify(sip_real_dst(p)),
16505 sip_nat_mode(p));
16506 }
16507 }
16508 }
16509
16510
16511 static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
16512 struct sip_request *req, int sipmethod, struct ast_sockaddr *addr,
16513 struct sip_peer **authpeer,
16514 enum xmittype reliable, char *calleridname, char *uri2)
16515 {
16516 enum check_auth_result res;
16517 int debug = sip_debug_test_addr(addr);
16518 struct sip_peer *peer;
16519
16520 if (sipmethod == SIP_SUBSCRIBE) {
16521
16522
16523
16524 peer = find_peer(of, NULL, TRUE, FINDALLDEVICES, FALSE, 0);
16525 } else {
16526
16527 peer = find_peer(of, NULL, TRUE, FINDUSERS, FALSE, 0);
16528
16529
16530 if (!peer) {
16531 peer = find_peer(NULL, &p->recv, TRUE, FINDPEERS, FALSE, p->socket.type);
16532 }
16533 }
16534
16535 if (!peer) {
16536 if (debug) {
16537 ast_verbose("No matching peer for '%s' from '%s'\n",
16538 of, ast_sockaddr_stringify(&p->recv));
16539 }
16540
16541
16542
16543
16544 if (sip_cfg.allowguest || !sip_cfg.alwaysauthreject) {
16545 return AUTH_DONT_KNOW;
16546 }
16547
16548
16549
16550
16551 peer = bogus_peer;
16552 ref_peer(peer, "ref_peer: check_peer_ok: must ref bogus_peer so unreffing it does not fail");
16553 }
16554
16555 if (!ast_apply_ha(peer->ha, addr)) {
16556 ast_debug(2, "Found peer '%s' for '%s', but fails host access\n", peer->name, of);
16557 unref_peer(peer, "unref_peer: check_peer_ok: from find_peer call, early return of AUTH_ACL_FAILED");
16558 return AUTH_ACL_FAILED;
16559 }
16560 if (debug && peer != bogus_peer) {
16561 ast_verbose("Found peer '%s' for '%s' from %s\n",
16562 peer->name, of, ast_sockaddr_stringify(&p->recv));
16563 }
16564
16565
16566
16567 if (p->rtp) {
16568 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &peer->prefs);
16569 p->autoframing = peer->autoframing;
16570 }
16571
16572
16573 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
16574 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
16575 ast_copy_flags(&p->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
16576
16577 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && p->udptl) {
16578 p->t38_maxdatagram = peer->t38_maxdatagram;
16579 set_t38_capabilities(p);
16580 }
16581
16582
16583
16584 if (p->sipoptions)
16585 peer->sipoptions = p->sipoptions;
16586
16587 do_setnat(p);
16588
16589 ast_string_field_set(p, peersecret, peer->secret);
16590 ast_string_field_set(p, peermd5secret, peer->md5secret);
16591 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
16592 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
16593 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
16594 if (!ast_strlen_zero(peer->parkinglot)) {
16595 ast_string_field_set(p, parkinglot, peer->parkinglot);
16596 }
16597 ast_string_field_set(p, engine, peer->engine);
16598 p->disallowed_methods = peer->disallowed_methods;
16599 set_pvt_allowed_methods(p, req);
16600 ast_cc_copy_config_params(p->cc_params, peer->cc_params);
16601 if (peer->callingpres)
16602 p->callingpres = peer->callingpres;
16603 if (peer->maxms && peer->lastms)
16604 p->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
16605 else
16606 p->timer_t1 = peer->timer_t1;
16607
16608
16609 if (peer->timer_b)
16610 p->timer_b = peer->timer_b;
16611 else
16612 p->timer_b = 64 * p->timer_t1;
16613
16614 p->allowtransfer = peer->allowtransfer;
16615
16616 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
16617
16618 ast_string_field_set(p, peersecret, NULL);
16619 ast_string_field_set(p, peermd5secret, NULL);
16620 }
16621 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, req->ignore))) {
16622 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
16623 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
16624 ast_copy_flags(&p->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
16625
16626 if (peer->call_limit)
16627 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
16628 ast_string_field_set(p, peername, peer->name);
16629 ast_string_field_set(p, authname, peer->name);
16630
16631 if (sipmethod == SIP_INVITE) {
16632
16633 ast_variables_destroy(p->chanvars);
16634 p->chanvars = copy_vars(peer->chanvars);
16635 }
16636
16637 if (authpeer) {
16638 ao2_t_ref(peer, 1, "copy pointer into (*authpeer)");
16639 (*authpeer) = peer;
16640 }
16641
16642 if (!ast_strlen_zero(peer->username)) {
16643 ast_string_field_set(p, username, peer->username);
16644
16645
16646 ast_string_field_set(p, authname, peer->username);
16647 }
16648 if (!get_rpid(p, req)) {
16649 if (!ast_strlen_zero(peer->cid_num)) {
16650 char *tmp = ast_strdupa(peer->cid_num);
16651 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
16652 ast_shrink_phone_number(tmp);
16653 ast_string_field_set(p, cid_num, tmp);
16654 }
16655 if (!ast_strlen_zero(peer->cid_name))
16656 ast_string_field_set(p, cid_name, peer->cid_name);
16657 if (peer->callingpres)
16658 p->callingpres = peer->callingpres;
16659 }
16660 if (!ast_strlen_zero(peer->cid_tag)) {
16661 ast_string_field_set(p, cid_tag, peer->cid_tag);
16662 }
16663 ast_string_field_set(p, fullcontact, peer->fullcontact);
16664
16665 if (!ast_strlen_zero(peer->context)) {
16666 ast_string_field_set(p, context, peer->context);
16667 }
16668 if (!ast_strlen_zero(peer->mwi_from)) {
16669 ast_string_field_set(p, mwi_from, peer->mwi_from);
16670 }
16671
16672 ast_string_field_set(p, peersecret, peer->secret);
16673 ast_string_field_set(p, peermd5secret, peer->md5secret);
16674 ast_string_field_set(p, language, peer->language);
16675 ast_string_field_set(p, accountcode, peer->accountcode);
16676 p->amaflags = peer->amaflags;
16677 p->callgroup = peer->callgroup;
16678 p->pickupgroup = peer->pickupgroup;
16679 p->capability = peer->capability;
16680 p->prefs = peer->prefs;
16681 p->jointcapability = peer->capability;
16682 if (peer->maxforwards > 0) {
16683 p->maxforwards = peer->maxforwards;
16684 }
16685 if (p->peercapability)
16686 p->jointcapability &= p->peercapability;
16687 p->maxcallbitrate = peer->maxcallbitrate;
16688 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
16689 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
16690 p->noncodeccapability |= AST_RTP_DTMF;
16691 else
16692 p->noncodeccapability &= ~AST_RTP_DTMF;
16693 p->jointnoncodeccapability = p->noncodeccapability;
16694 p->rtptimeout = peer->rtptimeout;
16695 p->rtpholdtimeout = peer->rtpholdtimeout;
16696 p->rtpkeepalive = peer->rtpkeepalive;
16697 if (!dialog_initialize_rtp(p)) {
16698 if (p->rtp) {
16699 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &peer->prefs);
16700 p->autoframing = peer->autoframing;
16701 }
16702 } else {
16703 res = AUTH_RTP_FAILED;
16704 }
16705 }
16706 unref_peer(peer, "check_peer_ok: unref_peer: tossing temp ptr to peer from find_peer");
16707 return res;
16708 }
16709
16710
16711
16712
16713
16714
16715
16716 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
16717 int sipmethod, const char *uri, enum xmittype reliable,
16718 struct ast_sockaddr *addr, struct sip_peer **authpeer)
16719 {
16720 char *of, *name, *unused_password, *domain;
16721 RAII_VAR(char *, ofbuf, NULL, ast_free);
16722 RAII_VAR(char *, namebuf, NULL, ast_free);
16723 enum check_auth_result res = AUTH_DONT_KNOW;
16724 char calleridname[256];
16725 char *uri2 = ast_strdupa(uri);
16726
16727 terminate_uri(uri2);
16728
16729 ofbuf = ast_strdup(get_header(req, "From"));
16730
16731
16732
16733 if (!(of = (char *) get_calleridname(ofbuf, calleridname, sizeof(calleridname)))) {
16734 ast_log(LOG_ERROR, "FROM header can not be parsed\n");
16735 return res;
16736 }
16737
16738 if (calleridname[0]) {
16739 ast_string_field_set(p, cid_name, calleridname);
16740 }
16741
16742 if (ast_strlen_zero(p->exten)) {
16743 char *t = uri2;
16744 if (!strncasecmp(t, "sip:", 4))
16745 t+= 4;
16746 else if (!strncasecmp(t, "sips:", 5))
16747 t += 5;
16748 ast_string_field_set(p, exten, t);
16749 t = strchr(p->exten, '@');
16750 if (t)
16751 *t = '\0';
16752
16753 if (ast_strlen_zero(p->our_contact))
16754 build_contact(p);
16755 }
16756
16757 of = get_in_brackets(of);
16758
16759
16760 ast_string_field_set(p, from, of);
16761
16762 if (parse_uri_legacy_check(of, "sip:,sips:", &name, &unused_password, &domain, NULL)) {
16763 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
16764 }
16765
16766 SIP_PEDANTIC_DECODE(name);
16767 SIP_PEDANTIC_DECODE(domain);
16768
16769 extract_host_from_hostport(&domain);
16770
16771 if (ast_strlen_zero(domain)) {
16772
16773 ast_log(LOG_ERROR, "Empty domain name in FROM header\n");
16774 return res;
16775 }
16776
16777 if (ast_strlen_zero(name)) {
16778
16779
16780
16781 name = domain;
16782 } else {
16783
16784 char *tmp = ast_strdupa(name);
16785
16786
16787
16788 tmp = strsep(&tmp, ";");
16789 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp)) {
16790 ast_shrink_phone_number(tmp);
16791 }
16792 ast_string_field_set(p, cid_num, tmp);
16793 }
16794
16795 if (global_match_auth_username) {
16796
16797
16798
16799
16800
16801
16802
16803
16804 const char *hdr = get_header(req, "Authorization");
16805 if (ast_strlen_zero(hdr)) {
16806 hdr = get_header(req, "Proxy-Authorization");
16807 }
16808
16809 if (!ast_strlen_zero(hdr) && (hdr = strstr(hdr, "username=\""))) {
16810 namebuf = name = ast_strdup(hdr + strlen("username=\""));
16811 name = strsep(&name, "\"");
16812 }
16813 }
16814
16815 res = check_peer_ok(p, name, req, sipmethod, addr,
16816 authpeer, reliable, calleridname, uri2);
16817 if (res != AUTH_DONT_KNOW) {
16818 return res;
16819 }
16820
16821
16822 if (sip_cfg.allowguest) {
16823
16824 get_rpid(p, req);
16825 p->rtptimeout = global_rtptimeout;
16826 p->rtpholdtimeout = global_rtpholdtimeout;
16827 p->rtpkeepalive = global_rtpkeepalive;
16828 if (!dialog_initialize_rtp(p)) {
16829 res = AUTH_SUCCESSFUL;
16830 } else {
16831 res = AUTH_RTP_FAILED;
16832 }
16833 } else {
16834 res = AUTH_SECRET_FAILED;
16835 }
16836
16837 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT)) {
16838 ast_set_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT);
16839 }
16840
16841 return res;
16842 }
16843
16844
16845
16846
16847 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, const char *uri, enum xmittype reliable, struct ast_sockaddr *addr)
16848 {
16849 return check_user_full(p, req, sipmethod, uri, reliable, addr, NULL);
16850 }
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860 static int get_msg_text(char *buf, int len, struct sip_request *req)
16861 {
16862 int x;
16863 int linelen;
16864
16865 buf[0] = '\0';
16866 --len;
16867 for (x = 0; len && x < req->lines; ++x) {
16868 const char *line = REQ_OFFSET_TO_STR(req, line[x]);
16869 strncat(buf, line, len);
16870 linelen = strlen(buf);
16871 buf += linelen;
16872 len -= linelen;
16873 if (len) {
16874 strcat(buf, "\n");
16875 ++buf;
16876 --len;
16877 }
16878 }
16879 return 0;
16880 }
16881
16882
16883
16884
16885
16886 static void receive_message(struct sip_pvt *p, struct sip_request *req)
16887 {
16888 char buf[1400];
16889 char *bufp;
16890 struct ast_frame f;
16891 const char *content_type = get_header(req, "Content-Type");
16892
16893 if (strncmp(content_type, "text/plain", strlen("text/plain"))) {
16894 transmit_response(p, "415 Unsupported Media Type", req);
16895 if (!p->owner)
16896 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16897 return;
16898 }
16899
16900 if (get_msg_text(buf, sizeof(buf), req)) {
16901 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
16902 transmit_response(p, "500 Internal Server Error", req);
16903 if (!p->owner) {
16904 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16905 }
16906 return;
16907 }
16908
16909
16910
16911 bufp = buf + strlen(buf);
16912 while (--bufp >= buf && *bufp == '\n') {
16913 *bufp = '\0';
16914 }
16915
16916 if (p->owner) {
16917 if (sip_debug_test_pvt(p))
16918 ast_verbose("SIP Text message received: '%s'\n", buf);
16919 memset(&f, 0, sizeof(f));
16920 f.frametype = AST_FRAME_TEXT;
16921 f.subclass.integer = 0;
16922 f.offset = 0;
16923 f.data.ptr = buf;
16924 f.datalen = strlen(buf) + 1;
16925 ast_queue_frame(p->owner, &f);
16926 transmit_response(p, "202 Accepted", req);
16927 return;
16928 }
16929
16930
16931 ast_log(LOG_WARNING, "Received message to %s from %s, dropped it...\n Content-Type:%s\n Message: %s\n", get_header(req, "To"), get_header(req, "From"), content_type, buf);
16932 transmit_response(p, "405 Method Not Allowed", req);
16933 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
16934 return;
16935 }
16936
16937
16938 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16939 {
16940 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
16941 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
16942 char ilimits[40];
16943 char iused[40];
16944 int showall = FALSE;
16945 struct ao2_iterator i;
16946 struct sip_peer *peer;
16947
16948 switch (cmd) {
16949 case CLI_INIT:
16950 e->command = "sip show inuse";
16951 e->usage =
16952 "Usage: sip show inuse [all]\n"
16953 " List all SIP devices usage counters and limits.\n"
16954 " Add option \"all\" to show all devices, not only those with a limit.\n";
16955 return NULL;
16956 case CLI_GENERATE:
16957 return NULL;
16958 }
16959
16960 if (a->argc < 3)
16961 return CLI_SHOWUSAGE;
16962
16963 if (a->argc == 4 && !strcmp(a->argv[3], "all"))
16964 showall = TRUE;
16965
16966 ast_cli(a->fd, FORMAT, "* Peer name", "In use", "Limit");
16967
16968 i = ao2_iterator_init(peers, 0);
16969 while ((peer = ao2_t_iterator_next(&i, "iterate thru peer table"))) {
16970 ao2_lock(peer);
16971 if (peer->call_limit)
16972 snprintf(ilimits, sizeof(ilimits), "%d", peer->call_limit);
16973 else
16974 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
16975 snprintf(iused, sizeof(iused), "%d/%d/%d", peer->inUse, peer->inRinging, peer->onHold);
16976 if (showall || peer->call_limit)
16977 ast_cli(a->fd, FORMAT2, peer->name, iused, ilimits);
16978 ao2_unlock(peer);
16979 unref_peer(peer, "toss iterator pointer");
16980 }
16981 ao2_iterator_destroy(&i);
16982
16983 return CLI_SUCCESS;
16984 #undef FORMAT
16985 #undef FORMAT2
16986 }
16987
16988
16989
16990 static char *transfermode2str(enum transfermodes mode)
16991 {
16992 if (mode == TRANSFER_OPENFORALL)
16993 return "open";
16994 else if (mode == TRANSFER_CLOSED)
16995 return "closed";
16996 return "strict";
16997 }
16998
16999
17000
17001
17002
17003
17004
17005 static const struct _map_x_s stmodes[] = {
17006 { SESSION_TIMER_MODE_ACCEPT, "Accept"},
17007 { SESSION_TIMER_MODE_ORIGINATE, "Originate"},
17008 { SESSION_TIMER_MODE_REFUSE, "Refuse"},
17009 { -1, NULL},
17010 };
17011
17012 static const char *stmode2str(enum st_mode m)
17013 {
17014 return map_x_s(stmodes, m, "Unknown");
17015 }
17016
17017 static enum st_mode str2stmode(const char *s)
17018 {
17019 return map_s_x(stmodes, s, -1);
17020 }
17021
17022
17023 static const struct _map_x_s strefresher_params[] = {
17024 { SESSION_TIMER_REFRESHER_PARAM_UNKNOWN, "unknown" },
17025 { SESSION_TIMER_REFRESHER_PARAM_UAC, "uac" },
17026 { SESSION_TIMER_REFRESHER_PARAM_UAS, "uas" },
17027 { -1, NULL },
17028 };
17029
17030 static const struct _map_x_s strefreshers[] = {
17031 { SESSION_TIMER_REFRESHER_AUTO, "auto" },
17032 { SESSION_TIMER_REFRESHER_US, "us" },
17033 { SESSION_TIMER_REFRESHER_THEM, "them" },
17034 { -1, NULL },
17035 };
17036
17037 static const char *strefresherparam2str(enum st_refresher r)
17038 {
17039 return map_x_s(strefresher_params, r, "Unknown");
17040 }
17041
17042 static enum st_refresher str2strefresherparam(const char *s)
17043 {
17044 return map_s_x(strefresher_params, s, -1);
17045 }
17046
17047 static const char *strefresher2str(enum st_refresher r)
17048 {
17049 return map_x_s(strefreshers, r, "Unknown");
17050 }
17051
17052 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
17053 {
17054 int res = 0;
17055 if (peer->maxms) {
17056 if (peer->lastms < 0) {
17057 ast_copy_string(status, "UNREACHABLE", statuslen);
17058 } else if (peer->lastms > peer->maxms) {
17059 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
17060 res = 1;
17061 } else if (peer->lastms) {
17062 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
17063 res = 1;
17064 } else {
17065 ast_copy_string(status, "UNKNOWN", statuslen);
17066 }
17067 } else {
17068 ast_copy_string(status, "Unmonitored", statuslen);
17069
17070 res = -1;
17071 }
17072 return res;
17073 }
17074
17075
17076 static char *sip_show_tcp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17077 {
17078 struct sip_threadinfo *th;
17079 struct ao2_iterator i;
17080
17081 #define FORMAT2 "%-47.47s %9.9s %6.6s\n"
17082 #define FORMAT "%-47.47s %-9.9s %-6.6s\n"
17083
17084 switch (cmd) {
17085 case CLI_INIT:
17086 e->command = "sip show tcp";
17087 e->usage =
17088 "Usage: sip show tcp\n"
17089 " Lists all active TCP/TLS sessions.\n";
17090 return NULL;
17091 case CLI_GENERATE:
17092 return NULL;
17093 }
17094
17095 if (a->argc != 3)
17096 return CLI_SHOWUSAGE;
17097
17098 ast_cli(a->fd, FORMAT2, "Address", "Transport", "Type");
17099
17100 i = ao2_iterator_init(threadt, 0);
17101 while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
17102 ast_cli(a->fd, FORMAT,
17103 ast_sockaddr_stringify(&th->tcptls_session->remote_address),
17104 get_transport(th->type),
17105 (th->tcptls_session->client ? "Client" : "Server"));
17106 ao2_t_ref(th, -1, "decrement ref from iterator");
17107 }
17108 ao2_iterator_destroy(&i);
17109
17110 return CLI_SUCCESS;
17111 #undef FORMAT
17112 #undef FORMAT2
17113 }
17114
17115
17116 static char *sip_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17117 {
17118 regex_t regexbuf;
17119 int havepattern = FALSE;
17120 struct ao2_iterator user_iter;
17121 struct sip_peer *user;
17122
17123 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
17124
17125 switch (cmd) {
17126 case CLI_INIT:
17127 e->command = "sip show users";
17128 e->usage =
17129 "Usage: sip show users [like <pattern>]\n"
17130 " Lists all known SIP users.\n"
17131 " Optional regular expression pattern is used to filter the user list.\n";
17132 return NULL;
17133 case CLI_GENERATE:
17134 return NULL;
17135 }
17136
17137 switch (a->argc) {
17138 case 5:
17139 if (!strcasecmp(a->argv[3], "like")) {
17140 if (regcomp(®exbuf, a->argv[4], REG_EXTENDED | REG_NOSUB))
17141 return CLI_SHOWUSAGE;
17142 havepattern = TRUE;
17143 } else
17144 return CLI_SHOWUSAGE;
17145 case 3:
17146 break;
17147 default:
17148 return CLI_SHOWUSAGE;
17149 }
17150
17151 ast_cli(a->fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "Forcerport");
17152
17153 user_iter = ao2_iterator_init(peers, 0);
17154 while ((user = ao2_t_iterator_next(&user_iter, "iterate thru peers table"))) {
17155 ao2_lock(user);
17156 if (!(user->type & SIP_TYPE_USER)) {
17157 ao2_unlock(user);
17158 unref_peer(user, "sip show users");
17159 continue;
17160 }
17161
17162 if (havepattern && regexec(®exbuf, user->name, 0, NULL, 0)) {
17163 ao2_unlock(user);
17164 unref_peer(user, "sip show users");
17165 continue;
17166 }
17167
17168 ast_cli(a->fd, FORMAT, user->name,
17169 user->secret,
17170 user->accountcode,
17171 user->context,
17172 AST_CLI_YESNO(user->ha != NULL),
17173 AST_CLI_YESNO(ast_test_flag(&user->flags[0], SIP_NAT_FORCE_RPORT)));
17174 ao2_unlock(user);
17175 unref_peer(user, "sip show users");
17176 }
17177 ao2_iterator_destroy(&user_iter);
17178
17179 if (havepattern)
17180 regfree(®exbuf);
17181
17182 return CLI_SUCCESS;
17183 #undef FORMAT
17184 }
17185
17186
17187 static int manager_show_registry(struct mansession *s, const struct message *m)
17188 {
17189 const char *id = astman_get_header(m, "ActionID");
17190 char idtext[256] = "";
17191 int total = 0;
17192
17193 if (!ast_strlen_zero(id))
17194 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
17195
17196 astman_send_listack(s, m, "Registrations will follow", "start");
17197
17198 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
17199 ASTOBJ_RDLOCK(iterator);
17200 astman_append(s,
17201 "Event: RegistryEntry\r\n"
17202 "%s"
17203 "Host: %s\r\n"
17204 "Port: %d\r\n"
17205 "Username: %s\r\n"
17206 "Domain: %s\r\n"
17207 "DomainPort: %d\r\n"
17208 "Refresh: %d\r\n"
17209 "State: %s\r\n"
17210 "RegistrationTime: %ld\r\n"
17211 "\r\n",
17212 idtext,
17213 iterator->hostname,
17214 iterator->portno ? iterator->portno : STANDARD_SIP_PORT,
17215 iterator->username,
17216 S_OR(iterator->regdomain,iterator->hostname),
17217 iterator->regdomainport ? iterator->regdomainport : STANDARD_SIP_PORT,
17218 iterator->refresh,
17219 regstate2str(iterator->regstate),
17220 (long) iterator->regtime.tv_sec);
17221 ASTOBJ_UNLOCK(iterator);
17222 total++;
17223 } while(0));
17224
17225 astman_append(s,
17226 "Event: RegistrationsComplete\r\n"
17227 "EventList: Complete\r\n"
17228 "ListItems: %d\r\n"
17229 "%s"
17230 "\r\n", total, idtext);
17231
17232 return 0;
17233 }
17234
17235
17236
17237 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
17238 {
17239 const char *id = astman_get_header(m, "ActionID");
17240 const char *a[] = {"sip", "show", "peers"};
17241 char idtext[256] = "";
17242 int total = 0;
17243
17244 if (!ast_strlen_zero(id))
17245 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
17246
17247 astman_send_listack(s, m, "Peer status list will follow", "start");
17248
17249 _sip_show_peers(-1, &total, s, m, 3, a);
17250
17251 astman_append(s,
17252 "Event: PeerlistComplete\r\n"
17253 "EventList: Complete\r\n"
17254 "ListItems: %d\r\n"
17255 "%s"
17256 "\r\n", total, idtext);
17257 return 0;
17258 }
17259
17260
17261 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17262 {
17263 switch (cmd) {
17264 case CLI_INIT:
17265 e->command = "sip show peers";
17266 e->usage =
17267 "Usage: sip show peers [like <pattern>]\n"
17268 " Lists all known SIP peers.\n"
17269 " Optional regular expression pattern is used to filter the peer list.\n";
17270 return NULL;
17271 case CLI_GENERATE:
17272 return NULL;
17273 }
17274
17275 return _sip_show_peers(a->fd, NULL, NULL, NULL, a->argc, (const char **) a->argv);
17276 }
17277
17278 int peercomparefunc(const void *a, const void *b);
17279
17280 int peercomparefunc(const void *a, const void *b)
17281 {
17282 struct sip_peer **ap = (struct sip_peer **)a;
17283 struct sip_peer **bp = (struct sip_peer **)b;
17284 return strcmp((*ap)->name, (*bp)->name);
17285 }
17286
17287
17288 #define PEERS_FORMAT2 "%-25.25s %-39.39s %-3.3s %-10.10s %-3.3s %-8s %-10s %s\n"
17289
17290
17291 struct show_peers_context {
17292 regex_t regexbuf;
17293 int havepattern;
17294 char idtext[256];
17295 int realtimepeers;
17296 int peers_mon_online;
17297 int peers_mon_offline;
17298 int peers_unmon_offline;
17299 int peers_unmon_online;
17300 };
17301
17302
17303 static char *_sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
17304 {
17305 struct show_peers_context cont = {
17306 .havepattern = FALSE,
17307 .idtext = "",
17308
17309 .peers_mon_online = 0,
17310 .peers_mon_offline = 0,
17311 .peers_unmon_online = 0,
17312 .peers_unmon_offline = 0,
17313 };
17314 struct sip_peer *peer;
17315 struct ao2_iterator* it_peers;
17316
17317 int total_peers = 0;
17318 const char *id;
17319 struct sip_peer **peerarray;
17320 int k;
17321
17322 cont.realtimepeers = ast_check_realtime("sippeers");
17323
17324 if (s) {
17325 id = astman_get_header(m, "ActionID");
17326 if (!ast_strlen_zero(id)) {
17327 snprintf(cont.idtext, sizeof(cont.idtext), "ActionID: %s\r\n", id);
17328 }
17329 }
17330
17331 switch (argc) {
17332 case 5:
17333 if (!strcasecmp(argv[3], "like")) {
17334 if (regcomp(&cont.regexbuf, argv[4], REG_EXTENDED | REG_NOSUB))
17335 return CLI_SHOWUSAGE;
17336 cont.havepattern = TRUE;
17337 } else
17338 return CLI_SHOWUSAGE;
17339 case 3:
17340 break;
17341 default:
17342 return CLI_SHOWUSAGE;
17343 }
17344
17345 if (!s) {
17346
17347 ast_cli(fd, PEERS_FORMAT2, "Name/username", "Host", "Dyn", "Forcerport", "ACL", "Port", "Status", (cont.realtimepeers ? "Realtime" : ""));
17348 }
17349
17350 ao2_lock(peers);
17351 if (!(it_peers = ao2_callback(peers, OBJ_MULTIPLE, NULL, NULL))) {
17352 ast_log(AST_LOG_ERROR, "Unable to create iterator for peers container for sip show peers\n");
17353 ao2_unlock(peers);
17354 return CLI_FAILURE;
17355 }
17356 if (!(peerarray = ast_calloc(sizeof(struct sip_peer *), ao2_container_count(peers)))) {
17357 ast_log(AST_LOG_ERROR, "Unable to allocate peer array for sip show peers\n");
17358 ao2_iterator_destroy(it_peers);
17359 ao2_unlock(peers);
17360 return CLI_FAILURE;
17361 }
17362 ao2_unlock(peers);
17363
17364 while ((peer = ao2_t_iterator_next(it_peers, "iterate thru peers table"))) {
17365 ao2_lock(peer);
17366
17367 if (!(peer->type & SIP_TYPE_PEER)) {
17368 ao2_unlock(peer);
17369 unref_peer(peer, "unref peer because it's actually a user");
17370 continue;
17371 }
17372
17373 if (cont.havepattern && regexec(&cont.regexbuf, peer->name, 0, NULL, 0)) {
17374 ao2_unlock(peer);
17375 unref_peer(peer, "toss iterator peer ptr before continue");
17376 continue;
17377 }
17378
17379 peerarray[total_peers++] = peer;
17380 ao2_unlock(peer);
17381 }
17382 ao2_iterator_destroy(it_peers);
17383
17384 qsort(peerarray, total_peers, sizeof(struct sip_peer *), peercomparefunc);
17385
17386 for(k = 0; k < total_peers; k++) {
17387 peerarray[k] = _sip_show_peers_one(fd, s, &cont, peerarray[k]);
17388 }
17389
17390 if (!s) {
17391 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
17392 total_peers, cont.peers_mon_online, cont.peers_mon_offline, cont.peers_unmon_online, cont.peers_unmon_offline);
17393 }
17394
17395 if (cont.havepattern) {
17396 regfree(&cont.regexbuf);
17397 }
17398
17399 if (total) {
17400 *total = total_peers;
17401 }
17402
17403 ast_free(peerarray);
17404
17405 return CLI_SUCCESS;
17406 }
17407
17408
17409 static struct sip_peer *_sip_show_peers_one(int fd, struct mansession *s, struct show_peers_context *cont, struct sip_peer *peer)
17410 {
17411
17412
17413
17414
17415 char name[256];
17416 char status[20] = "";
17417 char pstatus;
17418
17419
17420
17421
17422
17423
17424 char *tmp_port;
17425 char *tmp_host;
17426
17427 tmp_port = ast_sockaddr_isnull(&peer->addr) ?
17428 "0" : ast_strdupa(ast_sockaddr_stringify_port(&peer->addr));
17429
17430 tmp_host = ast_sockaddr_isnull(&peer->addr) ?
17431 "(Unspecified)" : ast_strdupa(ast_sockaddr_stringify_addr(&peer->addr));
17432
17433 ao2_lock(peer);
17434 if (cont->havepattern && regexec(&cont->regexbuf, peer->name, 0, NULL, 0)) {
17435 ao2_unlock(peer);
17436 return unref_peer(peer, "toss iterator peer ptr no match");
17437 }
17438
17439 if (!ast_strlen_zero(peer->username) && !s) {
17440 snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
17441 } else {
17442 ast_copy_string(name, peer->name, sizeof(name));
17443 }
17444
17445 pstatus = peer_status(peer, status, sizeof(status));
17446 if (pstatus == 1) {
17447 cont->peers_mon_online++;
17448 } else if (pstatus == 0) {
17449 cont->peers_mon_offline++;
17450 } else {
17451 if (ast_sockaddr_isnull(&peer->addr) ||
17452 !ast_sockaddr_port(&peer->addr)) {
17453 cont->peers_unmon_offline++;
17454 } else {
17455 cont->peers_unmon_online++;
17456 }
17457 }
17458
17459 if (!s) {
17460 ast_cli(fd, PEERS_FORMAT2, name,
17461 tmp_host,
17462 peer->host_dynamic ? " D " : " ",
17463 ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) ? " N " : " ",
17464 peer->ha ? " A " : " ",
17465 tmp_port, status,
17466 cont->realtimepeers ? (peer->is_realtime ? "Cached RT":"") : "");
17467 } else {
17468
17469 astman_append(s,
17470 "Event: PeerEntry\r\n%s"
17471 "Channeltype: SIP\r\n"
17472 "ObjectName: %s\r\n"
17473 "ChanObjectType: peer\r\n"
17474 "IPaddress: %s\r\n"
17475 "IPport: %s\r\n"
17476 "Dynamic: %s\r\n"
17477 "Forcerport: %s\r\n"
17478 "VideoSupport: %s\r\n"
17479 "TextSupport: %s\r\n"
17480 "ACL: %s\r\n"
17481 "Status: %s\r\n"
17482 "RealtimeDevice: %s\r\n\r\n",
17483 cont->idtext,
17484 peer->name,
17485 ast_sockaddr_isnull(&peer->addr) ? "-none-" : tmp_host,
17486 ast_sockaddr_isnull(&peer->addr) ? "0" : tmp_port,
17487 peer->host_dynamic ? "yes" : "no",
17488 ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) ? "yes" : "no",
17489 ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
17490 ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT) ? "yes" : "no",
17491 peer->ha ? "yes" : "no",
17492 status,
17493 cont->realtimepeers ? (peer->is_realtime ? "yes":"no") : "no");
17494 }
17495 ao2_unlock(peer);
17496
17497 return unref_peer(peer, "toss iterator peer ptr");
17498 }
17499 #undef PEERS_FORMAT2
17500
17501 static int peer_dump_func(void *userobj, void *arg, int flags)
17502 {
17503 struct sip_peer *peer = userobj;
17504 int refc = ao2_t_ref(userobj, 0, "");
17505 struct ast_cli_args *a = (struct ast_cli_args *) arg;
17506
17507 ast_cli(a->fd, "name: %s\ntype: peer\nobjflags: %d\nrefcount: %d\n\n",
17508 peer->name, 0, refc);
17509 return 0;
17510 }
17511
17512 static int dialog_dump_func(void *userobj, void *arg, int flags)
17513 {
17514 struct sip_pvt *pvt = userobj;
17515 int refc = ao2_t_ref(userobj, 0, "");
17516 struct ast_cli_args *a = (struct ast_cli_args *) arg;
17517
17518 ast_cli(a->fd, "name: %s\ntype: dialog\nobjflags: %d\nrefcount: %d\n\n",
17519 pvt->callid, 0, refc);
17520 return 0;
17521 }
17522
17523
17524
17525 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17526 {
17527 char tmp[256];
17528
17529 switch (cmd) {
17530 case CLI_INIT:
17531 e->command = "sip show objects";
17532 e->usage =
17533 "Usage: sip show objects\n"
17534 " Lists status of known SIP objects\n";
17535 return NULL;
17536 case CLI_GENERATE:
17537 return NULL;
17538 }
17539
17540 if (a->argc != 3)
17541 return CLI_SHOWUSAGE;
17542 ast_cli(a->fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
17543 ao2_t_callback(peers, OBJ_NODATA, peer_dump_func, a, "initiate ao2_callback to dump peers");
17544 ast_cli(a->fd, "-= Peer objects by IP =-\n\n");
17545 ao2_t_callback(peers_by_ip, OBJ_NODATA, peer_dump_func, a, "initiate ao2_callback to dump peers_by_ip");
17546 ast_cli(a->fd, "-= Registry objects: %d =-\n\n", regobjs);
17547 ASTOBJ_CONTAINER_DUMP(a->fd, tmp, sizeof(tmp), ®l);
17548 ast_cli(a->fd, "-= Dialog objects:\n\n");
17549 ao2_t_callback(dialogs, OBJ_NODATA, dialog_dump_func, a, "initiate ao2_callback to dump dialogs");
17550 return CLI_SUCCESS;
17551 }
17552
17553 static void print_group(int fd, ast_group_t group, int crlf)
17554 {
17555 char buf[256];
17556 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
17557 }
17558
17559
17560 static const struct _map_x_s dtmfstr[] = {
17561 { SIP_DTMF_RFC2833, "rfc2833" },
17562 { SIP_DTMF_INFO, "info" },
17563 { SIP_DTMF_SHORTINFO, "shortinfo" },
17564 { SIP_DTMF_INBAND, "inband" },
17565 { SIP_DTMF_AUTO, "auto" },
17566 { -1, NULL },
17567 };
17568
17569
17570 static const char *dtmfmode2str(int mode)
17571 {
17572 return map_x_s(dtmfstr, mode, "<error>");
17573 }
17574
17575
17576 static int str2dtmfmode(const char *str)
17577 {
17578 return map_s_x(dtmfstr, str, -1);
17579 }
17580
17581 static const struct _map_x_s insecurestr[] = {
17582 { SIP_INSECURE_PORT, "port" },
17583 { SIP_INSECURE_INVITE, "invite" },
17584 { SIP_INSECURE_PORT | SIP_INSECURE_INVITE, "port,invite" },
17585 { 0, "no" },
17586 { -1, NULL },
17587 };
17588
17589
17590 static const char *insecure2str(int mode)
17591 {
17592 return map_x_s(insecurestr, mode, "<error>");
17593 }
17594
17595 static const struct _map_x_s allowoverlapstr[] = {
17596 { SIP_PAGE2_ALLOWOVERLAP_YES, "Yes" },
17597 { SIP_PAGE2_ALLOWOVERLAP_DTMF, "DTMF" },
17598 { SIP_PAGE2_ALLOWOVERLAP_NO, "No" },
17599 { -1, NULL },
17600 };
17601
17602
17603 static const char *allowoverlap2str(int mode)
17604 {
17605 return map_x_s(allowoverlapstr, mode, "<error>");
17606 }
17607
17608 static const struct _map_x_s trust_id_outboundstr[] = {
17609 { SIP_PAGE2_TRUST_ID_OUTBOUND_LEGACY, "Legacy" },
17610 { SIP_PAGE2_TRUST_ID_OUTBOUND_NO, "No" },
17611 { SIP_PAGE2_TRUST_ID_OUTBOUND_YES, "Yes" },
17612 { -1, NULL },
17613 };
17614
17615 static const char *trust_id_outbound2str(int mode)
17616 {
17617 return map_x_s(trust_id_outboundstr, mode, "<error>");
17618 }
17619
17620
17621
17622
17623 static void cleanup_stale_contexts(char *new, char *old)
17624 {
17625 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
17626
17627 while ((oldcontext = strsep(&old, "&"))) {
17628 stalecontext = '\0';
17629 ast_copy_string(newlist, new, sizeof(newlist));
17630 stringp = newlist;
17631 while ((newcontext = strsep(&stringp, "&"))) {
17632 if (!strcmp(newcontext, oldcontext)) {
17633
17634 stalecontext = '\0';
17635 break;
17636 } else if (strcmp(newcontext, oldcontext)) {
17637 stalecontext = oldcontext;
17638 }
17639
17640 }
17641 if (stalecontext)
17642 ast_context_destroy(ast_context_find(stalecontext), "SIP");
17643 }
17644 }
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656 static int dialog_needdestroy(void *dialogobj, void *arg, int flags)
17657 {
17658 struct sip_pvt *dialog = dialogobj;
17659 time_t *t = arg;
17660
17661 if (sip_pvt_trylock(dialog)) {
17662
17663
17664 return 0;
17665 }
17666
17667
17668 check_rtp_timeout(dialog, *t);
17669
17670
17671 if (dialog->rtp && ast_rtp_instance_get_bridged(dialog->rtp)) {
17672 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
17673 sip_pvt_unlock(dialog);
17674 return 0;
17675 }
17676
17677 if (dialog->vrtp && ast_rtp_instance_get_bridged(dialog->vrtp)) {
17678 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
17679 sip_pvt_unlock(dialog);
17680 return 0;
17681 }
17682
17683
17684
17685
17686 if (dialog->needdestroy && !dialog->packets && !dialog->owner) {
17687
17688 if (dialog->rtp && ast_rtp_instance_get_bridged(dialog->rtp)) {
17689 ast_debug(2, "Bridge still active. Delaying destruction of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
17690 sip_pvt_unlock(dialog);
17691 return 0;
17692 }
17693
17694 if (dialog->vrtp && ast_rtp_instance_get_bridged(dialog->vrtp)) {
17695 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
17696 sip_pvt_unlock(dialog);
17697 return 0;
17698 }
17699
17700 sip_pvt_unlock(dialog);
17701
17702
17703 ao2_t_link(dialogs_to_destroy, dialog, "Link dialog for destruction");
17704 return 0;
17705 }
17706
17707 sip_pvt_unlock(dialog);
17708
17709 return 0;
17710 }
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722 static int dialog_unlink_callback(void *obj, void *arg, int flags)
17723 {
17724 struct sip_pvt *dialog = obj;
17725
17726 dialog_unlink_all(dialog);
17727
17728 return CMP_MATCH;
17729 }
17730
17731
17732
17733 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17734 {
17735 struct sip_peer *peer, *pi;
17736 int prunepeer = FALSE;
17737 int multi = FALSE;
17738 const char *name = NULL;
17739 regex_t regexbuf;
17740 int havepattern = 0;
17741 struct ao2_iterator i;
17742 static const char * const choices[] = { "all", "like", NULL };
17743 char *cmplt;
17744
17745 if (cmd == CLI_INIT) {
17746 e->command = "sip prune realtime [peer|all]";
17747 e->usage =
17748 "Usage: sip prune realtime [peer [<name>|all|like <pattern>]|all]\n"
17749 " Prunes object(s) from the cache.\n"
17750 " Optional regular expression pattern is used to filter the objects.\n";
17751 return NULL;
17752 } else if (cmd == CLI_GENERATE) {
17753 if (a->pos == 4 && !strcasecmp(a->argv[3], "peer")) {
17754 cmplt = ast_cli_complete(a->word, choices, a->n);
17755 if (!cmplt)
17756 cmplt = complete_sip_peer(a->word, a->n - sizeof(choices), SIP_PAGE2_RTCACHEFRIENDS);
17757 return cmplt;
17758 }
17759 if (a->pos == 5 && !strcasecmp(a->argv[4], "like"))
17760 return complete_sip_peer(a->word, a->n, SIP_PAGE2_RTCACHEFRIENDS);
17761 return NULL;
17762 }
17763 switch (a->argc) {
17764 case 4:
17765 name = a->argv[3];
17766
17767 if (!strcasecmp(name, "peer") || !strcasecmp(name, "like"))
17768 return CLI_SHOWUSAGE;
17769 prunepeer = TRUE;
17770 if (!strcasecmp(name, "all")) {
17771 multi = TRUE;
17772 name = NULL;
17773 }
17774
17775 break;
17776 case 5:
17777
17778 name = a->argv[4];
17779 if (!strcasecmp(a->argv[3], "peer"))
17780 prunepeer = TRUE;
17781 else if (!strcasecmp(a->argv[3], "like")) {
17782 prunepeer = TRUE;
17783 multi = TRUE;
17784 } else
17785 return CLI_SHOWUSAGE;
17786 if (!strcasecmp(name, "like"))
17787 return CLI_SHOWUSAGE;
17788 if (!multi && !strcasecmp(name, "all")) {
17789 multi = TRUE;
17790 name = NULL;
17791 }
17792 break;
17793 case 6:
17794 name = a->argv[5];
17795 multi = TRUE;
17796
17797 if (strcasecmp(a->argv[4], "like"))
17798 return CLI_SHOWUSAGE;
17799 if (!strcasecmp(a->argv[3], "peer")) {
17800 prunepeer = TRUE;
17801 } else
17802 return CLI_SHOWUSAGE;
17803 break;
17804 default:
17805 return CLI_SHOWUSAGE;
17806 }
17807
17808 if (multi && name) {
17809 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB)) {
17810 return CLI_SHOWUSAGE;
17811 }
17812 havepattern = 1;
17813 }
17814
17815 if (multi) {
17816 if (prunepeer) {
17817 int pruned = 0;
17818
17819 i = ao2_iterator_init(peers, 0);
17820 while ((pi = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
17821 ao2_lock(pi);
17822 if (name && regexec(®exbuf, pi->name, 0, NULL, 0)) {
17823 ao2_unlock(pi);
17824 unref_peer(pi, "toss iterator peer ptr before continue");
17825 continue;
17826 };
17827 if (ast_test_flag(&pi->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
17828 pi->the_mark = 1;
17829 pruned++;
17830 }
17831 ao2_unlock(pi);
17832 unref_peer(pi, "toss iterator peer ptr");
17833 }
17834 ao2_iterator_destroy(&i);
17835 if (pruned) {
17836 unlink_marked_peers_from_tables();
17837 ast_cli(a->fd, "%d peers pruned.\n", pruned);
17838 } else
17839 ast_cli(a->fd, "No peers found to prune.\n");
17840 }
17841 } else {
17842 if (prunepeer) {
17843 struct sip_peer tmp;
17844 ast_copy_string(tmp.name, name, sizeof(tmp.name));
17845 if ((peer = ao2_t_find(peers, &tmp, OBJ_POINTER | OBJ_UNLINK, "finding to unlink from peers"))) {
17846 if (!ast_sockaddr_isnull(&peer->addr)) {
17847 ao2_t_unlink(peers_by_ip, peer, "unlinking peer from peers_by_ip also");
17848 }
17849 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
17850 ast_cli(a->fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
17851
17852 ao2_t_link(peers, peer, "link peer into peer table");
17853 if (!ast_sockaddr_isnull(&peer->addr)) {
17854 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
17855 }
17856 } else
17857 ast_cli(a->fd, "Peer '%s' pruned.\n", name);
17858 unref_peer(peer, "sip_prune_realtime: unref_peer: tossing temp peer ptr");
17859 } else
17860 ast_cli(a->fd, "Peer '%s' not found.\n", name);
17861 }
17862 }
17863
17864 if (havepattern) {
17865 regfree(®exbuf);
17866 }
17867
17868 return CLI_SUCCESS;
17869 }
17870
17871
17872 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
17873 {
17874 int x;
17875 format_t codec;
17876
17877 for(x = 0; x < 64 ; x++) {
17878 codec = ast_codec_pref_index(pref, x);
17879 if (!codec)
17880 break;
17881 ast_cli(fd, "%s", ast_getformatname(codec));
17882 ast_cli(fd, ":%d", pref->framing[x]);
17883 if (x < 31 && ast_codec_pref_index(pref, x + 1))
17884 ast_cli(fd, ",");
17885 }
17886 if (!x)
17887 ast_cli(fd, "none");
17888 }
17889
17890
17891 static const char *domain_mode_to_text(const enum domain_mode mode)
17892 {
17893 switch (mode) {
17894 case SIP_DOMAIN_AUTO:
17895 return "[Automatic]";
17896 case SIP_DOMAIN_CONFIG:
17897 return "[Configured]";
17898 }
17899
17900 return "";
17901 }
17902
17903
17904 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17905 {
17906 struct domain *d;
17907 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
17908
17909 switch (cmd) {
17910 case CLI_INIT:
17911 e->command = "sip show domains";
17912 e->usage =
17913 "Usage: sip show domains\n"
17914 " Lists all configured SIP local domains.\n"
17915 " Asterisk only responds to SIP messages to local domains.\n";
17916 return NULL;
17917 case CLI_GENERATE:
17918 return NULL;
17919 }
17920
17921 if (AST_LIST_EMPTY(&domain_list)) {
17922 ast_cli(a->fd, "SIP Domain support not enabled.\n\n");
17923 return CLI_SUCCESS;
17924 } else {
17925 ast_cli(a->fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
17926 AST_LIST_LOCK(&domain_list);
17927 AST_LIST_TRAVERSE(&domain_list, d, list)
17928 ast_cli(a->fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
17929 domain_mode_to_text(d->mode));
17930 AST_LIST_UNLOCK(&domain_list);
17931 ast_cli(a->fd, "\n");
17932 return CLI_SUCCESS;
17933 }
17934 }
17935 #undef FORMAT
17936
17937
17938 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
17939 {
17940 const char *a[4];
17941 const char *peer;
17942
17943 peer = astman_get_header(m, "Peer");
17944 if (ast_strlen_zero(peer)) {
17945 astman_send_error(s, m, "Peer: <name> missing.");
17946 return 0;
17947 }
17948 a[0] = "sip";
17949 a[1] = "show";
17950 a[2] = "peer";
17951 a[3] = peer;
17952
17953 _sip_show_peer(1, -1, s, m, 4, a);
17954 astman_append(s, "\r\n" );
17955 return 0;
17956 }
17957
17958
17959 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17960 {
17961 switch (cmd) {
17962 case CLI_INIT:
17963 e->command = "sip show peer";
17964 e->usage =
17965 "Usage: sip show peer <name> [load]\n"
17966 " Shows all details on one SIP peer and the current status.\n"
17967 " Option \"load\" forces lookup of peer in realtime storage.\n";
17968 return NULL;
17969 case CLI_GENERATE:
17970 return complete_sip_show_peer(a->line, a->word, a->pos, a->n);
17971 }
17972 return _sip_show_peer(0, a->fd, NULL, NULL, a->argc, (const char **) a->argv);
17973 }
17974
17975
17976 static char *_sip_qualify_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
17977 {
17978 struct sip_peer *peer;
17979 int load_realtime;
17980
17981 if (argc < 4)
17982 return CLI_SHOWUSAGE;
17983
17984 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
17985 if ((peer = find_peer(argv[3], NULL, load_realtime, FINDPEERS, FALSE, 0))) {
17986 sip_poke_peer(peer, 1);
17987 unref_peer(peer, "qualify: done with peer");
17988 } else if (type == 0) {
17989 ast_cli(fd, "Peer '%s' not found\n", argv[3]);
17990 } else {
17991 astman_send_error(s, m, "Peer not found");
17992 }
17993 return CLI_SUCCESS;
17994 }
17995
17996
17997 static int manager_sip_qualify_peer(struct mansession *s, const struct message *m)
17998 {
17999 const char *a[4];
18000 const char *peer;
18001
18002 peer = astman_get_header(m, "Peer");
18003 if (ast_strlen_zero(peer)) {
18004 astman_send_error(s, m, "Peer: <name> missing.");
18005 return 0;
18006 }
18007 a[0] = "sip";
18008 a[1] = "qualify";
18009 a[2] = "peer";
18010 a[3] = peer;
18011
18012 _sip_qualify_peer(1, -1, s, m, 4, a);
18013 astman_append(s, "\r\n\r\n" );
18014 return 0;
18015 }
18016
18017
18018 static char *sip_qualify_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18019 {
18020 switch (cmd) {
18021 case CLI_INIT:
18022 e->command = "sip qualify peer";
18023 e->usage =
18024 "Usage: sip qualify peer <name> [load]\n"
18025 " Requests a response from one SIP peer and the current status.\n"
18026 " Option \"load\" forces lookup of peer in realtime storage.\n";
18027 return NULL;
18028 case CLI_GENERATE:
18029 return complete_sip_show_peer(a->line, a->word, a->pos, a->n);
18030 }
18031 return _sip_qualify_peer(0, a->fd, NULL, NULL, a->argc, (const char **) a->argv);
18032 }
18033
18034
18035 static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer)
18036 {
18037 struct sip_mailbox *mailbox;
18038
18039 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
18040 ast_str_append(mailbox_str, 0, "%s%s%s%s",
18041 mailbox->mailbox,
18042 ast_strlen_zero(mailbox->context) ? "" : "@",
18043 S_OR(mailbox->context, ""),
18044 AST_LIST_NEXT(mailbox, entry) ? "," : "");
18045 }
18046 }
18047
18048 static struct _map_x_s faxecmodes[] = {
18049 { SIP_PAGE2_T38SUPPORT_UDPTL, "None"},
18050 { SIP_PAGE2_T38SUPPORT_UDPTL_FEC, "FEC"},
18051 { SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY, "Redundancy"},
18052 { -1, NULL},
18053 };
18054
18055 static const char *faxec2str(int faxec)
18056 {
18057 return map_x_s(faxecmodes, faxec, "Unknown");
18058 }
18059
18060
18061 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
18062 {
18063 char status[30] = "";
18064 char cbuf[256];
18065 struct sip_peer *peer;
18066 char codec_buf[512];
18067 struct ast_codec_pref *pref;
18068 struct ast_variable *v;
18069 int x = 0, load_realtime;
18070 format_t codec = 0;
18071 int realtimepeers;
18072
18073 realtimepeers = ast_check_realtime("sippeers");
18074
18075 if (argc < 4)
18076 return CLI_SHOWUSAGE;
18077
18078 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
18079 peer = find_peer(argv[3], NULL, load_realtime, FINDPEERS, FALSE, 0);
18080
18081 if (s) {
18082 if (peer) {
18083 const char *id = astman_get_header(m, "ActionID");
18084
18085 astman_append(s, "Response: Success\r\n");
18086 if (!ast_strlen_zero(id))
18087 astman_append(s, "ActionID: %s\r\n", id);
18088 } else {
18089 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.", argv[3]);
18090 astman_send_error(s, m, cbuf);
18091 return CLI_SUCCESS;
18092 }
18093 }
18094 if (peer && type==0 ) {
18095 struct ast_str *mailbox_str = ast_str_alloca(512);
18096 struct sip_auth_container *credentials;
18097
18098 ao2_lock(peer);
18099 credentials = peer->auth;
18100 if (credentials) {
18101 ao2_t_ref(credentials, +1, "Ref peer auth for show");
18102 }
18103 ao2_unlock(peer);
18104
18105 ast_cli(fd, "\n\n");
18106 ast_cli(fd, " * Name : %s\n", peer->name);
18107 if (realtimepeers) {
18108 ast_cli(fd, " Realtime peer: %s\n", peer->is_realtime ? "Yes, cached" : "No");
18109 }
18110 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
18111 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
18112 ast_cli(fd, " Remote Secret: %s\n", ast_strlen_zero(peer->remotesecret)?"<Not set>":"<Set>");
18113 if (credentials) {
18114 struct sip_auth *auth;
18115
18116 AST_LIST_TRAVERSE(&credentials->list, auth, node) {
18117 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s %s\n",
18118 auth->realm,
18119 auth->username,
18120 !ast_strlen_zero(auth->secret)
18121 ? "<Secret set>"
18122 : (!ast_strlen_zero(auth->md5secret)
18123 ? "<MD5secret set>" : "<Not set>"));
18124 }
18125 ao2_t_ref(credentials, -1, "Unref peer auth for show");
18126 }
18127 ast_cli(fd, " Context : %s\n", peer->context);
18128 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
18129 ast_cli(fd, " Language : %s\n", peer->language);
18130 if (!ast_strlen_zero(peer->accountcode))
18131 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
18132 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
18133 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
18134 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
18135 if (!ast_strlen_zero(peer->fromuser))
18136 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
18137 if (!ast_strlen_zero(peer->fromdomain))
18138 ast_cli(fd, " FromDomain : %s Port %d\n", peer->fromdomain, (peer->fromdomainport) ? peer->fromdomainport : STANDARD_SIP_PORT);
18139 ast_cli(fd, " Callgroup : ");
18140 print_group(fd, peer->callgroup, 0);
18141 ast_cli(fd, " Pickupgroup : ");
18142 print_group(fd, peer->pickupgroup, 0);
18143 peer_mailboxes_to_str(&mailbox_str, peer);
18144 ast_cli(fd, " MOH Suggest : %s\n", peer->mohsuggest);
18145 ast_cli(fd, " Mailbox : %s\n", ast_str_buffer(mailbox_str));
18146 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
18147 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
18148 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
18149 ast_cli(fd, " Max forwards : %d\n", peer->maxforwards);
18150 if (peer->busy_level)
18151 ast_cli(fd, " Busy level : %d\n", peer->busy_level);
18152 ast_cli(fd, " Dynamic : %s\n", AST_CLI_YESNO(peer->host_dynamic));
18153 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
18154 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
18155 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
18156 ast_cli(fd, " Insecure : %s\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
18157 ast_cli(fd, " Force rport : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT)));
18158 ast_cli(fd, " ACL : %s\n", AST_CLI_YESNO(peer->ha != NULL));
18159 ast_cli(fd, " DirectMedACL : %s\n", AST_CLI_YESNO(peer->directmediaha != NULL));
18160 ast_cli(fd, " T.38 support : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
18161 ast_cli(fd, " T.38 EC mode : %s\n", faxec2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
18162 ast_cli(fd, " T.38 MaxDtgrm: %u\n", peer->t38_maxdatagram);
18163 ast_cli(fd, " DirectMedia : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_DIRECT_MEDIA)));
18164 ast_cli(fd, " PromiscRedir : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)));
18165 ast_cli(fd, " User=Phone : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)));
18166 ast_cli(fd, " Video Support: %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT) || ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS)));
18167 ast_cli(fd, " Text Support : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)));
18168 ast_cli(fd, " Ign SDP ver : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_IGNORESDPVERSION)));
18169 ast_cli(fd, " Trust RPID : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_TRUSTRPID)));
18170 ast_cli(fd, " Send RPID : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_SENDRPID)));
18171 ast_cli(fd, " TrustIDOutbnd: %s\n", trust_id_outbound2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND)));
18172 ast_cli(fd, " Subscriptions: %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
18173 ast_cli(fd, " Overlap dial : %s\n", allowoverlap2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP)));
18174 if (peer->outboundproxy)
18175 ast_cli(fd, " Outb. proxy : %s %s\n", ast_strlen_zero(peer->outboundproxy->name) ? "<not set>" : peer->outboundproxy->name,
18176 peer->outboundproxy->force ? "(forced)" : "");
18177
18178
18179 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
18180 ast_cli(fd, " Timer T1 : %d\n", peer->timer_t1);
18181 ast_cli(fd, " Timer B : %d\n", peer->timer_b);
18182 ast_cli(fd, " ToHost : %s\n", peer->tohost);
18183 ast_cli(fd, " Addr->IP : %s\n", ast_sockaddr_stringify(&peer->addr));
18184 ast_cli(fd, " Defaddr->IP : %s\n", ast_sockaddr_stringify(&peer->defaddr));
18185 ast_cli(fd, " Prim.Transp. : %s\n", get_transport(peer->socket.type));
18186 ast_cli(fd, " Allowed.Trsp : %s\n", get_transport_list(peer->transports));
18187 if (!ast_strlen_zero(sip_cfg.regcontext))
18188 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
18189 ast_cli(fd, " Def. Username: %s\n", peer->username);
18190 ast_cli(fd, " SIP Options : ");
18191 if (peer->sipoptions) {
18192 int lastoption = -1;
18193 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
18194 if (sip_options[x].id != lastoption) {
18195 if (peer->sipoptions & sip_options[x].id)
18196 ast_cli(fd, "%s ", sip_options[x].text);
18197 lastoption = x;
18198 }
18199 }
18200 } else
18201 ast_cli(fd, "(none)");
18202
18203 ast_cli(fd, "\n");
18204 ast_cli(fd, " Codecs : ");
18205 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
18206 ast_cli(fd, "%s\n", codec_buf);
18207 ast_cli(fd, " Codec Order : (");
18208 print_codec_to_cli(fd, &peer->prefs);
18209 ast_cli(fd, ")\n");
18210
18211 ast_cli(fd, " Auto-Framing : %s\n", AST_CLI_YESNO(peer->autoframing));
18212 ast_cli(fd, " Status : ");
18213 peer_status(peer, status, sizeof(status));
18214 ast_cli(fd, "%s\n", status);
18215 ast_cli(fd, " Useragent : %s\n", peer->useragent);
18216 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
18217 ast_cli(fd, " Qualify Freq : %d ms\n", peer->qualifyfreq);
18218 if (peer->chanvars) {
18219 ast_cli(fd, " Variables :\n");
18220 for (v = peer->chanvars ; v ; v = v->next)
18221 ast_cli(fd, " %s = %s\n", v->name, v->value);
18222 }
18223
18224 ast_cli(fd, " Sess-Timers : %s\n", stmode2str(peer->stimer.st_mode_oper));
18225 ast_cli(fd, " Sess-Refresh : %s\n", strefresherparam2str(peer->stimer.st_ref));
18226 ast_cli(fd, " Sess-Expires : %d secs\n", peer->stimer.st_max_se);
18227 ast_cli(fd, " Min-Sess : %d secs\n", peer->stimer.st_min_se);
18228 ast_cli(fd, " RTP Engine : %s\n", peer->engine);
18229 ast_cli(fd, " Parkinglot : %s\n", peer->parkinglot);
18230 ast_cli(fd, " Use Reason : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_Q850_REASON)));
18231 ast_cli(fd, " Encryption : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP)));
18232 ast_cli(fd, "\n");
18233 peer = unref_peer(peer, "sip_show_peer: unref_peer: done with peer ptr");
18234 } else if (peer && type == 1) {
18235 char buffer[256];
18236 struct ast_str *mailbox_str = ast_str_alloca(512);
18237 astman_append(s, "Channeltype: SIP\r\n");
18238 astman_append(s, "ObjectName: %s\r\n", peer->name);
18239 astman_append(s, "ChanObjectType: peer\r\n");
18240 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
18241 astman_append(s, "RemoteSecretExist: %s\r\n", ast_strlen_zero(peer->remotesecret)?"N":"Y");
18242 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
18243 astman_append(s, "Context: %s\r\n", peer->context);
18244 astman_append(s, "Language: %s\r\n", peer->language);
18245 if (!ast_strlen_zero(peer->accountcode))
18246 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
18247 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
18248 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
18249 if (!ast_strlen_zero(peer->fromuser))
18250 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
18251 if (!ast_strlen_zero(peer->fromdomain))
18252 astman_append(s, "SIP-FromDomain: %s\r\nSip-FromDomain-Port: %d\r\n", peer->fromdomain, (peer->fromdomainport) ? peer->fromdomainport : STANDARD_SIP_PORT);
18253 astman_append(s, "Callgroup: ");
18254 astman_append(s, "%s\r\n", ast_print_group(buffer, sizeof(buffer), peer->callgroup));
18255 astman_append(s, "Pickupgroup: ");
18256 astman_append(s, "%s\r\n", ast_print_group(buffer, sizeof(buffer), peer->pickupgroup));
18257 astman_append(s, "MOHSuggest: %s\r\n", peer->mohsuggest);
18258 peer_mailboxes_to_str(&mailbox_str, peer);
18259 astman_append(s, "VoiceMailbox: %s\r\n", ast_str_buffer(mailbox_str));
18260 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
18261 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
18262 astman_append(s, "Maxforwards: %d\r\n", peer->maxforwards);
18263 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
18264 astman_append(s, "Busy-level: %d\r\n", peer->busy_level);
18265 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
18266 astman_append(s, "Dynamic: %s\r\n", peer->host_dynamic?"Y":"N");
18267 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
18268 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched, peer->expire));
18269 astman_append(s, "SIP-AuthInsecure: %s\r\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
18270 astman_append(s, "SIP-Forcerport: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT)?"Y":"N"));
18271 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
18272 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_DIRECT_MEDIA)?"Y":"N"));
18273 astman_append(s, "SIP-DirectMedia: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_DIRECT_MEDIA)?"Y":"N"));
18274 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
18275 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
18276 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
18277 astman_append(s, "SIP-TextSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)?"Y":"N"));
18278 astman_append(s, "SIP-T.38Support: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)?"Y":"N"));
18279 astman_append(s, "SIP-T.38EC: %s\r\n", faxec2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
18280 astman_append(s, "SIP-T.38MaxDtgrm: %u\r\n", peer->t38_maxdatagram);
18281 astman_append(s, "SIP-Sess-Timers: %s\r\n", stmode2str(peer->stimer.st_mode_oper));
18282 astman_append(s, "SIP-Sess-Refresh: %s\r\n", strefresherparam2str(peer->stimer.st_ref));
18283 astman_append(s, "SIP-Sess-Expires: %d\r\n", peer->stimer.st_max_se);
18284 astman_append(s, "SIP-Sess-Min: %d\r\n", peer->stimer.st_min_se);
18285 astman_append(s, "SIP-RTP-Engine: %s\r\n", peer->engine);
18286 astman_append(s, "SIP-Encryption: %s\r\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP) ? "Y" : "N");
18287
18288
18289 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
18290 astman_append(s, "ToHost: %s\r\n", peer->tohost);
18291 astman_append(s, "Address-IP: %s\r\nAddress-Port: %d\r\n", ast_sockaddr_stringify_addr(&peer->addr), ast_sockaddr_port(&peer->addr));
18292 astman_append(s, "Default-addr-IP: %s\r\nDefault-addr-port: %d\r\n", ast_sockaddr_stringify_addr(&peer->defaddr), ast_sockaddr_port(&peer->defaddr));
18293 astman_append(s, "Default-Username: %s\r\n", peer->username);
18294 if (!ast_strlen_zero(sip_cfg.regcontext))
18295 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
18296 astman_append(s, "Codecs: ");
18297 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
18298 astman_append(s, "%s\r\n", codec_buf);
18299 astman_append(s, "CodecOrder: ");
18300 pref = &peer->prefs;
18301 for(x = 0; x < 64 ; x++) {
18302 codec = ast_codec_pref_index(pref, x);
18303 if (!codec)
18304 break;
18305 astman_append(s, "%s", ast_getformatname(codec));
18306 if (x < 63 && ast_codec_pref_index(pref, x+1))
18307 astman_append(s, ",");
18308 }
18309
18310 astman_append(s, "\r\n");
18311 astman_append(s, "Status: ");
18312 peer_status(peer, status, sizeof(status));
18313 astman_append(s, "%s\r\n", status);
18314 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
18315 astman_append(s, "Reg-Contact: %s\r\n", peer->fullcontact);
18316 astman_append(s, "QualifyFreq: %d ms\r\n", peer->qualifyfreq);
18317 astman_append(s, "Parkinglot: %s\r\n", peer->parkinglot);
18318 if (peer->chanvars) {
18319 for (v = peer->chanvars ; v ; v = v->next) {
18320 astman_append(s, "ChanVariable: %s=%s\r\n", v->name, v->value);
18321 }
18322 }
18323 astman_append(s, "SIP-Use-Reason-Header: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_Q850_REASON)) ? "Y" : "N");
18324
18325 peer = unref_peer(peer, "sip_show_peer: unref_peer: done with peer");
18326
18327 } else {
18328 ast_cli(fd, "Peer %s not found.\n", argv[3]);
18329 ast_cli(fd, "\n");
18330 }
18331
18332 return CLI_SUCCESS;
18333 }
18334
18335
18336 static char *complete_sip_user(const char *word, int state)
18337 {
18338 char *result = NULL;
18339 int wordlen = strlen(word);
18340 int which = 0;
18341 struct ao2_iterator user_iter;
18342 struct sip_peer *user;
18343
18344 user_iter = ao2_iterator_init(peers, 0);
18345 while ((user = ao2_t_iterator_next(&user_iter, "iterate thru peers table"))) {
18346 ao2_lock(user);
18347 if (!(user->type & SIP_TYPE_USER)) {
18348 ao2_unlock(user);
18349 unref_peer(user, "complete sip user");
18350 continue;
18351 }
18352
18353 if (!strncasecmp(word, user->name, wordlen) && ++which > state) {
18354 result = ast_strdup(user->name);
18355 }
18356 ao2_unlock(user);
18357 unref_peer(user, "complete sip user");
18358 if (result) {
18359 break;
18360 }
18361 }
18362 ao2_iterator_destroy(&user_iter);
18363 return result;
18364 }
18365
18366 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
18367 {
18368 if (pos == 3)
18369 return complete_sip_user(word, state);
18370
18371 return NULL;
18372 }
18373
18374
18375 static char *sip_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18376 {
18377 char cbuf[256];
18378 struct sip_peer *user;
18379 struct ast_variable *v;
18380 int load_realtime;
18381
18382 switch (cmd) {
18383 case CLI_INIT:
18384 e->command = "sip show user";
18385 e->usage =
18386 "Usage: sip show user <name> [load]\n"
18387 " Shows all details on one SIP user and the current status.\n"
18388 " Option \"load\" forces lookup of peer in realtime storage.\n";
18389 return NULL;
18390 case CLI_GENERATE:
18391 return complete_sip_show_user(a->line, a->word, a->pos, a->n);
18392 }
18393
18394 if (a->argc < 4)
18395 return CLI_SHOWUSAGE;
18396
18397
18398 load_realtime = (a->argc == 5 && !strcmp(a->argv[4], "load")) ? TRUE : FALSE;
18399
18400 if ((user = find_peer(a->argv[3], NULL, load_realtime, FINDUSERS, FALSE, 0))) {
18401 ao2_lock(user);
18402 ast_cli(a->fd, "\n\n");
18403 ast_cli(a->fd, " * Name : %s\n", user->name);
18404 ast_cli(a->fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
18405 ast_cli(a->fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
18406 ast_cli(a->fd, " Context : %s\n", user->context);
18407 ast_cli(a->fd, " Language : %s\n", user->language);
18408 if (!ast_strlen_zero(user->accountcode))
18409 ast_cli(a->fd, " Accountcode : %s\n", user->accountcode);
18410 ast_cli(a->fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
18411 ast_cli(a->fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
18412 ast_cli(a->fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
18413 ast_cli(a->fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
18414 ast_cli(a->fd, " Call limit : %d\n", user->call_limit);
18415 ast_cli(a->fd, " Callgroup : ");
18416 print_group(a->fd, user->callgroup, 0);
18417 ast_cli(a->fd, " Pickupgroup : ");
18418 print_group(a->fd, user->pickupgroup, 0);
18419 ast_cli(a->fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
18420 ast_cli(a->fd, " ACL : %s\n", AST_CLI_YESNO(user->ha != NULL));
18421 ast_cli(a->fd, " Sess-Timers : %s\n", stmode2str(user->stimer.st_mode_oper));
18422 ast_cli(a->fd, " Sess-Refresh : %s\n", strefresherparam2str(user->stimer.st_ref));
18423 ast_cli(a->fd, " Sess-Expires : %d secs\n", user->stimer.st_max_se);
18424 ast_cli(a->fd, " Sess-Min-SE : %d secs\n", user->stimer.st_min_se);
18425 ast_cli(a->fd, " RTP Engine : %s\n", user->engine);
18426
18427 ast_cli(a->fd, " Codec Order : (");
18428 print_codec_to_cli(a->fd, &user->prefs);
18429 ast_cli(a->fd, ")\n");
18430
18431 ast_cli(a->fd, " Auto-Framing: %s \n", AST_CLI_YESNO(user->autoframing));
18432 if (user->chanvars) {
18433 ast_cli(a->fd, " Variables :\n");
18434 for (v = user->chanvars ; v ; v = v->next)
18435 ast_cli(a->fd, " %s = %s\n", v->name, v->value);
18436 }
18437
18438 ast_cli(a->fd, "\n");
18439
18440 ao2_unlock(user);
18441 unref_peer(user, "sip show user");
18442 } else {
18443 ast_cli(a->fd, "User %s not found.\n", a->argv[3]);
18444 ast_cli(a->fd, "\n");
18445 }
18446
18447 return CLI_SUCCESS;
18448 }
18449
18450
18451 static char *sip_show_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18452 {
18453 struct ast_str *cbuf;
18454 struct ast_cb_names cbnames = {9, { "retrans_pkt",
18455 "__sip_autodestruct",
18456 "expire_register",
18457 "auto_congest",
18458 "sip_reg_timeout",
18459 "sip_poke_peer_s",
18460 "sip_poke_noanswer",
18461 "sip_reregister",
18462 "sip_reinvite_retry"},
18463 { retrans_pkt,
18464 __sip_autodestruct,
18465 expire_register,
18466 auto_congest,
18467 sip_reg_timeout,
18468 sip_poke_peer_s,
18469 sip_poke_noanswer,
18470 sip_reregister,
18471 sip_reinvite_retry}};
18472
18473 switch (cmd) {
18474 case CLI_INIT:
18475 e->command = "sip show sched";
18476 e->usage =
18477 "Usage: sip show sched\n"
18478 " Shows stats on what's in the sched queue at the moment\n";
18479 return NULL;
18480 case CLI_GENERATE:
18481 return NULL;
18482 }
18483
18484 cbuf = ast_str_alloca(2048);
18485
18486 ast_cli(a->fd, "\n");
18487 ast_sched_report(sched, &cbuf, &cbnames);
18488 ast_cli(a->fd, "%s", ast_str_buffer(cbuf));
18489
18490 return CLI_SUCCESS;
18491 }
18492
18493
18494 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18495 {
18496 #define FORMAT2 "%-39.39s %-6.6s %-12.12s %8.8s %-20.20s %-25.25s\n"
18497 #define FORMAT "%-39.39s %-6.6s %-12.12s %8d %-20.20s %-25.25s\n"
18498 char host[80];
18499 char user[80];
18500 char tmpdat[256];
18501 struct ast_tm tm;
18502 int counter = 0;
18503
18504 switch (cmd) {
18505 case CLI_INIT:
18506 e->command = "sip show registry";
18507 e->usage =
18508 "Usage: sip show registry\n"
18509 " Lists all registration requests and status.\n";
18510 return NULL;
18511 case CLI_GENERATE:
18512 return NULL;
18513 }
18514
18515 if (a->argc != 3)
18516 return CLI_SHOWUSAGE;
18517 ast_cli(a->fd, FORMAT2, "Host", "dnsmgr", "Username", "Refresh", "State", "Reg.Time");
18518
18519 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
18520 ASTOBJ_RDLOCK(iterator);
18521 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
18522 snprintf(user, sizeof(user), "%s", iterator->username);
18523 if (!ast_strlen_zero(iterator->regdomain)) {
18524 snprintf(tmpdat, sizeof(tmpdat), "%s", user);
18525 snprintf(user, sizeof(user), "%s@%s", tmpdat, iterator->regdomain);}
18526 if (iterator->regdomainport) {
18527 snprintf(tmpdat, sizeof(tmpdat), "%s", user);
18528 snprintf(user, sizeof(user), "%s:%d", tmpdat, iterator->regdomainport);}
18529 if (iterator->regtime.tv_sec) {
18530 ast_localtime(&iterator->regtime, &tm, NULL);
18531 ast_strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
18532 } else
18533 tmpdat[0] = '\0';
18534 ast_cli(a->fd, FORMAT, host, (iterator->dnsmgr) ? "Y" : "N", user, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
18535 ASTOBJ_UNLOCK(iterator);
18536 counter++;
18537 } while(0));
18538 ast_cli(a->fd, "%d SIP registrations.\n", counter);
18539 return CLI_SUCCESS;
18540 #undef FORMAT
18541 #undef FORMAT2
18542 }
18543
18544
18545
18546
18547
18548 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18549 {
18550 struct sip_peer *peer;
18551 int load_realtime = 0;
18552
18553 switch (cmd) {
18554 case CLI_INIT:
18555 e->command = "sip unregister";
18556 e->usage =
18557 "Usage: sip unregister <peer>\n"
18558 " Unregister (force expiration) a SIP peer from the registry\n";
18559 return NULL;
18560 case CLI_GENERATE:
18561 return complete_sip_unregister(a->line, a->word, a->pos, a->n);
18562 }
18563
18564 if (a->argc != 3)
18565 return CLI_SHOWUSAGE;
18566
18567 if ((peer = find_peer(a->argv[2], NULL, load_realtime, FINDPEERS, TRUE, 0))) {
18568 if (peer->expire > 0) {
18569 AST_SCHED_DEL_UNREF(sched, peer->expire,
18570 unref_peer(peer, "remove register expire ref"));
18571 expire_register(ref_peer(peer, "ref for expire_register"));
18572 ast_cli(a->fd, "Unregistered peer \'%s\'\n\n", a->argv[2]);
18573 } else {
18574 ast_cli(a->fd, "Peer %s not registered\n", a->argv[2]);
18575 }
18576 unref_peer(peer, "sip_unregister: unref_peer via sip_unregister: done with peer from find_peer call");
18577 } else {
18578 ast_cli(a->fd, "Peer unknown: \'%s\'. Not unregistered.\n", a->argv[2]);
18579 }
18580
18581 return CLI_SUCCESS;
18582 }
18583
18584
18585 static int show_chanstats_cb(void *__cur, void *__arg, int flags)
18586 {
18587 #define FORMAT2 "%-15.15s %-11.11s %-8.8s %-10.10s %-10.10s ( %%) %-6.6s %-10.10s %-10.10s ( %%) %-6.6s\n"
18588 #define FORMAT "%-15.15s %-11.11s %-8.8s %-10.10u%-1.1s %-10.10u (%5.2f%%) %-6.4lf %-10.10u%-1.1s %-10.10u (%5.2f%%) %-6.4lf\n"
18589 struct sip_pvt *cur = __cur;
18590 struct ast_rtp_instance_stats stats;
18591 char durbuf[10];
18592 int duration;
18593 int durh, durm, durs;
18594 struct ast_channel *c;
18595 struct __show_chan_arg *arg = __arg;
18596 int fd = arg->fd;
18597
18598 sip_pvt_lock(cur);
18599 c = cur->owner;
18600
18601 if (cur->subscribed != NONE) {
18602
18603 sip_pvt_unlock(cur);
18604 return 0;
18605 }
18606
18607 if (!cur->rtp) {
18608 if (sipdebug) {
18609 ast_cli(fd, "%-15.15s %-11.11s (inv state: %s) -- %s\n",
18610 ast_sockaddr_stringify_addr(&cur->sa), cur->callid,
18611 invitestate2string[cur->invitestate].desc,
18612 "-- No RTP active");
18613 }
18614 sip_pvt_unlock(cur);
18615 return 0;
18616 }
18617
18618 if (ast_rtp_instance_get_stats(cur->rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
18619 sip_pvt_unlock(cur);
18620 ast_log(LOG_WARNING, "Could not get RTP stats.\n");
18621 return 0;
18622 }
18623
18624 if (c && c->cdr && !ast_tvzero(c->cdr->start)) {
18625 duration = (int)(ast_tvdiff_ms(ast_tvnow(), c->cdr->start) / 1000);
18626 durh = duration / 3600;
18627 durm = (duration % 3600) / 60;
18628 durs = duration % 60;
18629 snprintf(durbuf, sizeof(durbuf), "%02d:%02d:%02d", durh, durm, durs);
18630 } else {
18631 durbuf[0] = '\0';
18632 }
18633
18634 ast_cli(fd, FORMAT,
18635 ast_sockaddr_stringify_addr(&cur->sa),
18636 cur->callid,
18637 durbuf,
18638 stats.rxcount > (unsigned int) 100000 ? (unsigned int) (stats.rxcount)/(unsigned int) 1000 : stats.rxcount,
18639 stats.rxcount > (unsigned int) 100000 ? "K":" ",
18640 stats.rxploss,
18641 (stats.rxcount + stats.rxploss) > 0 ? (double) stats.rxploss / (stats.rxcount + stats.rxploss) * 100 : 0,
18642 stats.rxjitter,
18643 stats.txcount > (unsigned int) 100000 ? (unsigned int) (stats.txcount)/(unsigned int) 1000 : stats.txcount,
18644 stats.txcount > (unsigned int) 100000 ? "K":" ",
18645 stats.txploss,
18646 stats.txcount > 0 ? (double) stats.txploss / stats.txcount * 100 : 0,
18647 stats.txjitter
18648 );
18649 arg->numchans++;
18650 sip_pvt_unlock(cur);
18651
18652 return 0;
18653 }
18654
18655
18656 static char *sip_show_channelstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18657 {
18658 struct __show_chan_arg arg = { .fd = a->fd, .numchans = 0 };
18659
18660 switch (cmd) {
18661 case CLI_INIT:
18662 e->command = "sip show channelstats";
18663 e->usage =
18664 "Usage: sip show channelstats\n"
18665 " Lists all currently active SIP channel's RTCP statistics.\n"
18666 " Note that calls in the much optimized RTP P2P bridge mode will not show any packets here.";
18667 return NULL;
18668 case CLI_GENERATE:
18669 return NULL;
18670 }
18671
18672 if (a->argc != 3)
18673 return CLI_SHOWUSAGE;
18674
18675 ast_cli(a->fd, FORMAT2, "Peer", "Call ID", "Duration", "Recv: Pack", "Lost", "Jitter", "Send: Pack", "Lost", "Jitter");
18676
18677 ao2_t_callback(dialogs, OBJ_NODATA, show_chanstats_cb, &arg, "callback to sip show chanstats");
18678 ast_cli(a->fd, "%d active SIP channel%s\n", arg.numchans, (arg.numchans != 1) ? "s" : "");
18679 return CLI_SUCCESS;
18680 }
18681 #undef FORMAT
18682 #undef FORMAT2
18683
18684
18685 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18686 {
18687 int realtimepeers;
18688 int realtimeregs;
18689 char codec_buf[SIPBUFSIZE];
18690 const char *msg;
18691 struct sip_auth_container *credentials;
18692
18693 switch (cmd) {
18694 case CLI_INIT:
18695 e->command = "sip show settings";
18696 e->usage =
18697 "Usage: sip show settings\n"
18698 " Provides detailed list of the configuration of the SIP channel.\n";
18699 return NULL;
18700 case CLI_GENERATE:
18701 return NULL;
18702 }
18703
18704 if (a->argc != 3)
18705 return CLI_SHOWUSAGE;
18706
18707 realtimepeers = ast_check_realtime("sippeers");
18708 realtimeregs = ast_check_realtime("sipregs");
18709
18710 ast_mutex_lock(&authl_lock);
18711 credentials = authl;
18712 if (credentials) {
18713 ao2_t_ref(credentials, +1, "Ref global auth for show");
18714 }
18715 ast_mutex_unlock(&authl_lock);
18716
18717 ast_cli(a->fd, "\n\nGlobal Settings:\n");
18718 ast_cli(a->fd, "----------------\n");
18719 ast_cli(a->fd, " UDP Bindaddress: %s\n", ast_sockaddr_stringify(&bindaddr));
18720 if (ast_sockaddr_is_ipv6(&bindaddr) && ast_sockaddr_is_any(&bindaddr)) {
18721 ast_cli(a->fd, " ** Additional Info:\n");
18722 ast_cli(a->fd, " [::] may include IPv4 in addition to IPv6, if such a feature is enabled in the OS.\n");
18723 }
18724 ast_cli(a->fd, " TCP SIP Bindaddress: %s\n",
18725 sip_cfg.tcp_enabled != FALSE ?
18726 ast_sockaddr_stringify(&sip_tcp_desc.local_address) :
18727 "Disabled");
18728 ast_cli(a->fd, " TLS SIP Bindaddress: %s\n",
18729 default_tls_cfg.enabled != FALSE ?
18730 ast_sockaddr_stringify(&sip_tls_desc.local_address) :
18731 "Disabled");
18732 ast_cli(a->fd, " Videosupport: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT)));
18733 ast_cli(a->fd, " Textsupport: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT)));
18734 ast_cli(a->fd, " Ignore SDP sess. ver.: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION)));
18735 ast_cli(a->fd, " AutoCreate Peer: %s\n", AST_CLI_YESNO(sip_cfg.autocreatepeer));
18736 ast_cli(a->fd, " Match Auth Username: %s\n", AST_CLI_YESNO(global_match_auth_username));
18737 ast_cli(a->fd, " Allow unknown access: %s\n", AST_CLI_YESNO(sip_cfg.allowguest));
18738 ast_cli(a->fd, " Allow subscriptions: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
18739 ast_cli(a->fd, " Allow overlap dialing: %s\n", allowoverlap2str(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP)));
18740 ast_cli(a->fd, " Allow promisc. redir: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_PROMISCREDIR)));
18741 ast_cli(a->fd, " Enable call counters: %s\n", AST_CLI_YESNO(global_callcounter));
18742 ast_cli(a->fd, " SIP domain support: %s\n", AST_CLI_YESNO(!AST_LIST_EMPTY(&domain_list)));
18743 ast_cli(a->fd, " Realm. auth: %s\n", AST_CLI_YESNO(credentials != NULL));
18744 if (credentials) {
18745 struct sip_auth *auth;
18746
18747 AST_LIST_TRAVERSE(&credentials->list, auth, node) {
18748 ast_cli(a->fd, " Realm. auth entry: Realm %-15.15s User %-10.20s %s\n",
18749 auth->realm,
18750 auth->username,
18751 !ast_strlen_zero(auth->secret)
18752 ? "<Secret set>"
18753 : (!ast_strlen_zero(auth->md5secret)
18754 ? "<MD5secret set>" : "<Not set>"));
18755 }
18756 ao2_t_ref(credentials, -1, "Unref global auth for show");
18757 }
18758 ast_cli(a->fd, " Our auth realm %s\n", sip_cfg.realm);
18759 ast_cli(a->fd, " Use domains as realms: %s\n", AST_CLI_YESNO(sip_cfg.domainsasrealm));
18760 ast_cli(a->fd, " Call to non-local dom.: %s\n", AST_CLI_YESNO(sip_cfg.allow_external_domains));
18761 ast_cli(a->fd, " URI user is phone no: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_USEREQPHONE)));
18762 ast_cli(a->fd, " Always auth rejects: %s\n", AST_CLI_YESNO(sip_cfg.alwaysauthreject));
18763 ast_cli(a->fd, " Direct RTP setup: %s\n", AST_CLI_YESNO(sip_cfg.directrtpsetup));
18764 ast_cli(a->fd, " User Agent: %s\n", global_useragent);
18765 ast_cli(a->fd, " SDP Session Name: %s\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
18766 ast_cli(a->fd, " SDP Owner Name: %s\n", ast_strlen_zero(global_sdpowner) ? "-" : global_sdpowner);
18767 ast_cli(a->fd, " Reg. context: %s\n", S_OR(sip_cfg.regcontext, "(not set)"));
18768 ast_cli(a->fd, " Regexten on Qualify: %s\n", AST_CLI_YESNO(sip_cfg.regextenonqualify));
18769 ast_cli(a->fd, " Legacy userfield parse: %s\n", AST_CLI_YESNO(sip_cfg.legacy_useroption_parsing));
18770 ast_cli(a->fd, " Caller ID: %s\n", default_callerid);
18771 if ((default_fromdomainport) && (default_fromdomainport != STANDARD_SIP_PORT)) {
18772 ast_cli(a->fd, " From: Domain: %s:%d\n", default_fromdomain, default_fromdomainport);
18773 } else {
18774 ast_cli(a->fd, " From: Domain: %s\n", default_fromdomain);
18775 }
18776 ast_cli(a->fd, " Record SIP history: %s\n", AST_CLI_ONOFF(recordhistory));
18777 ast_cli(a->fd, " Call Events: %s\n", AST_CLI_ONOFF(sip_cfg.callevents));
18778 ast_cli(a->fd, " Auth. Failure Events: %s\n", AST_CLI_ONOFF(global_authfailureevents));
18779
18780 ast_cli(a->fd, " T.38 support: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT)));
18781 ast_cli(a->fd, " T.38 EC mode: %s\n", faxec2str(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT)));
18782 ast_cli(a->fd, " T.38 MaxDtgrm: %u\n", global_t38_maxdatagram);
18783 if (!realtimepeers && !realtimeregs)
18784 ast_cli(a->fd, " SIP realtime: Disabled\n" );
18785 else
18786 ast_cli(a->fd, " SIP realtime: Enabled\n" );
18787 ast_cli(a->fd, " Qualify Freq : %d ms\n", global_qualifyfreq);
18788 ast_cli(a->fd, " Q.850 Reason header: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_Q850_REASON)));
18789 ast_cli(a->fd, " Store SIP_CAUSE: %s\n", AST_CLI_YESNO(global_store_sip_cause));
18790 ast_cli(a->fd, "\nNetwork QoS Settings:\n");
18791 ast_cli(a->fd, "---------------------------\n");
18792 ast_cli(a->fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
18793 ast_cli(a->fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
18794 ast_cli(a->fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
18795 ast_cli(a->fd, " IP ToS RTP text: %s\n", ast_tos2str(global_tos_text));
18796 ast_cli(a->fd, " 802.1p CoS SIP: %u\n", global_cos_sip);
18797 ast_cli(a->fd, " 802.1p CoS RTP audio: %u\n", global_cos_audio);
18798 ast_cli(a->fd, " 802.1p CoS RTP video: %u\n", global_cos_video);
18799 ast_cli(a->fd, " 802.1p CoS RTP text: %u\n", global_cos_text);
18800 ast_cli(a->fd, " Jitterbuffer enabled: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_ENABLED)));
18801 if (ast_test_flag(&global_jbconf, AST_JB_ENABLED)) {
18802 ast_cli(a->fd, " Jitterbuffer forced: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_FORCED)));
18803 ast_cli(a->fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
18804 ast_cli(a->fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
18805 ast_cli(a->fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
18806 if (!strcasecmp(global_jbconf.impl, "adaptive")) {
18807 ast_cli(a->fd, " Jitterbuffer tgt extra: %ld\n", global_jbconf.target_extra);
18808 }
18809 ast_cli(a->fd, " Jitterbuffer log: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_LOG)));
18810 }
18811
18812 ast_cli(a->fd, "\nNetwork Settings:\n");
18813 ast_cli(a->fd, "---------------------------\n");
18814
18815 if (localaddr == NULL)
18816 msg = "Disabled, no localnet list";
18817 else if (ast_sockaddr_isnull(&externaddr))
18818 msg = "Disabled";
18819 else if (!ast_strlen_zero(externhost))
18820 msg = "Enabled using externhost";
18821 else
18822 msg = "Enabled using externaddr";
18823 ast_cli(a->fd, " SIP address remapping: %s\n", msg);
18824 ast_cli(a->fd, " Externhost: %s\n", S_OR(externhost, "<none>"));
18825 ast_cli(a->fd, " Externaddr: %s\n", ast_sockaddr_stringify(&externaddr));
18826 ast_cli(a->fd, " Externrefresh: %d\n", externrefresh);
18827 {
18828 struct ast_ha *d;
18829 const char *prefix = "Localnet:";
18830
18831 for (d = localaddr; d ; prefix = "", d = d->next) {
18832 const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&d->addr));
18833 const char *mask = ast_strdupa(ast_sockaddr_stringify_addr(&d->netmask));
18834 ast_cli(a->fd, " %-24s%s/%s\n", prefix, addr, mask);
18835 }
18836 }
18837 ast_cli(a->fd, "\nGlobal Signalling Settings:\n");
18838 ast_cli(a->fd, "---------------------------\n");
18839 ast_cli(a->fd, " Codecs: ");
18840 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, sip_cfg.capability);
18841 ast_cli(a->fd, "%s\n", codec_buf);
18842 ast_cli(a->fd, " Codec Order: ");
18843 print_codec_to_cli(a->fd, &default_prefs);
18844 ast_cli(a->fd, "\n");
18845 ast_cli(a->fd, " Relax DTMF: %s\n", AST_CLI_YESNO(global_relaxdtmf));
18846 ast_cli(a->fd, " RFC2833 Compensation: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE)));
18847 ast_cli(a->fd, " Symmetric RTP: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_SYMMETRICRTP)));
18848 ast_cli(a->fd, " Compact SIP headers: %s\n", AST_CLI_YESNO(sip_cfg.compactheaders));
18849 ast_cli(a->fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
18850 ast_cli(a->fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
18851 ast_cli(a->fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
18852 ast_cli(a->fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
18853 ast_cli(a->fd, " DNS SRV lookup: %s\n", AST_CLI_YESNO(sip_cfg.srvlookup));
18854 ast_cli(a->fd, " Pedantic SIP support: %s\n", AST_CLI_YESNO(sip_cfg.pedanticsipchecking));
18855 ast_cli(a->fd, " Reg. min duration %d secs\n", min_expiry);
18856 ast_cli(a->fd, " Reg. max duration: %d secs\n", max_expiry);
18857 ast_cli(a->fd, " Reg. default duration: %d secs\n", default_expiry);
18858 ast_cli(a->fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
18859 ast_cli(a->fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
18860 ast_cli(a->fd, " Outbound reg. retry 403:%d\n", global_reg_retry_403);
18861 ast_cli(a->fd, " Notify ringing state: %s\n", AST_CLI_YESNO(sip_cfg.notifyringing));
18862 if (sip_cfg.notifyringing) {
18863 ast_cli(a->fd, " Include CID: %s%s\n",
18864 AST_CLI_YESNO(sip_cfg.notifycid),
18865 sip_cfg.notifycid == IGNORE_CONTEXT ? " (Ignoring context)" : "");
18866 }
18867 ast_cli(a->fd, " Notify hold state: %s\n", AST_CLI_YESNO(sip_cfg.notifyhold));
18868 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(sip_cfg.allowtransfer));
18869 ast_cli(a->fd, " Max Call Bitrate: %d kbps\n", default_maxcallbitrate);
18870 ast_cli(a->fd, " Auto-Framing: %s\n", AST_CLI_YESNO(global_autoframing));
18871 ast_cli(a->fd, " Outb. proxy: %s %s\n", ast_strlen_zero(sip_cfg.outboundproxy.name) ? "<not set>" : sip_cfg.outboundproxy.name,
18872 sip_cfg.outboundproxy.force ? "(forced)" : "");
18873 ast_cli(a->fd, " Session Timers: %s\n", stmode2str(global_st_mode));
18874 ast_cli(a->fd, " Session Refresher: %s\n", strefresherparam2str(global_st_refresher));
18875 ast_cli(a->fd, " Session Expires: %d secs\n", global_max_se);
18876 ast_cli(a->fd, " Session Min-SE: %d secs\n", global_min_se);
18877 ast_cli(a->fd, " Timer T1: %d\n", global_t1);
18878 ast_cli(a->fd, " Timer T1 minimum: %d\n", global_t1min);
18879 ast_cli(a->fd, " Timer B: %d\n", global_timer_b);
18880 ast_cli(a->fd, " No premature media: %s\n", AST_CLI_YESNO(global_prematuremediafilter));
18881 ast_cli(a->fd, " Max forwards: %d\n", sip_cfg.default_max_forwards);
18882
18883 ast_cli(a->fd, "\nDefault Settings:\n");
18884 ast_cli(a->fd, "-----------------\n");
18885 ast_cli(a->fd, " Allowed transports: %s\n", get_transport_list(default_transports));
18886 ast_cli(a->fd, " Outbound transport: %s\n", get_transport(default_primary_transport));
18887 ast_cli(a->fd, " Context: %s\n", sip_cfg.default_context);
18888 ast_cli(a->fd, " Force rport: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_NAT_FORCE_RPORT)));
18889 ast_cli(a->fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
18890 ast_cli(a->fd, " Qualify: %d\n", default_qualify);
18891 ast_cli(a->fd, " Use ClientCode: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_USECLIENTCODE)));
18892 ast_cli(a->fd, " Progress inband: %s\n", (ast_test_flag(&global_flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER) ? "Never" : (AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_NO)));
18893 ast_cli(a->fd, " Language: %s\n", default_language);
18894 ast_cli(a->fd, " MOH Interpret: %s\n", default_mohinterpret);
18895 ast_cli(a->fd, " MOH Suggest: %s\n", default_mohsuggest);
18896 ast_cli(a->fd, " Voice Mail Extension: %s\n", default_vmexten);
18897
18898
18899 if (realtimepeers || realtimeregs) {
18900 ast_cli(a->fd, "\nRealtime SIP Settings:\n");
18901 ast_cli(a->fd, "----------------------\n");
18902 ast_cli(a->fd, " Realtime Peers: %s\n", AST_CLI_YESNO(realtimepeers));
18903 ast_cli(a->fd, " Realtime Regs: %s\n", AST_CLI_YESNO(realtimeregs));
18904 ast_cli(a->fd, " Cache Friends: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)));
18905 ast_cli(a->fd, " Update: %s\n", AST_CLI_YESNO(sip_cfg.peer_rtupdate));
18906 ast_cli(a->fd, " Ignore Reg. Expire: %s\n", AST_CLI_YESNO(sip_cfg.ignore_regexpire));
18907 ast_cli(a->fd, " Save sys. name: %s\n", AST_CLI_YESNO(sip_cfg.rtsave_sysname));
18908 ast_cli(a->fd, " Auto Clear: %d (%s)\n", sip_cfg.rtautoclear, ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR) ? "Enabled" : "Disabled");
18909 }
18910 ast_cli(a->fd, "\n----\n");
18911 return CLI_SUCCESS;
18912 }
18913
18914 static char *sip_show_mwi(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18915 {
18916 #define FORMAT "%-30.30s %-12.12s %-10.10s %-10.10s\n"
18917 char host[80];
18918
18919 switch (cmd) {
18920 case CLI_INIT:
18921 e->command = "sip show mwi";
18922 e->usage =
18923 "Usage: sip show mwi\n"
18924 " Provides a list of MWI subscriptions and status.\n";
18925 return NULL;
18926 case CLI_GENERATE:
18927 return NULL;
18928 }
18929
18930 ast_cli(a->fd, FORMAT, "Host", "Username", "Mailbox", "Subscribed");
18931
18932 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
18933 ASTOBJ_RDLOCK(iterator);
18934 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
18935 ast_cli(a->fd, FORMAT, host, iterator->username, iterator->mailbox, AST_CLI_YESNO(iterator->subscribed));
18936 ASTOBJ_UNLOCK(iterator);
18937 } while(0));
18938
18939 return CLI_SUCCESS;
18940 #undef FORMAT
18941 }
18942
18943
18944
18945 static const char *subscription_type2str(enum subscriptiontype subtype)
18946 {
18947 int i;
18948
18949 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
18950 if (subscription_types[i].type == subtype) {
18951 return subscription_types[i].text;
18952 }
18953 }
18954 return subscription_types[0].text;
18955 }
18956
18957
18958 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
18959 {
18960 int i;
18961
18962 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
18963 if (subscription_types[i].type == subtype) {
18964 return &subscription_types[i];
18965 }
18966 }
18967 return &subscription_types[0];
18968 }
18969
18970
18971
18972
18973
18974
18975
18976
18977 #define FORMAT4 "%-15.15s %-15.15s %-15.15s %-15.15s %-13.13s %-15.15s %-10.10s %-6.6d\n"
18978 #define FORMAT3 "%-15.15s %-15.15s %-15.15s %-15.15s %-13.13s %-15.15s %-10.10s %-6.6s\n"
18979 #define FORMAT2 "%-15.15s %-15.15s %-15.15s %-15.15s %-7.7s %-15.15s %-10.10s %-10.10s\n"
18980 #define FORMAT "%-15.15s %-15.15s %-15.15s %-15.15s %-3.3s %-3.3s %-15.15s %-10.10s %-10.10s\n"
18981
18982
18983 static int show_channels_cb(void *__cur, void *__arg, int flags)
18984 {
18985 struct sip_pvt *cur = __cur;
18986 struct __show_chan_arg *arg = __arg;
18987 const struct ast_sockaddr *dst;
18988
18989 sip_pvt_lock(cur);
18990 dst = sip_real_dst(cur);
18991
18992
18993 if (cur->subscribed == NONE && !arg->subscriptions) {
18994
18995 const char *referstatus = cur->refer ? referstatus2str(cur->refer->status) : "";
18996 char formatbuf[SIPBUFSIZE/2];
18997
18998 ast_cli(arg->fd, FORMAT, ast_sockaddr_stringify_addr(dst),
18999 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
19000 cur->callid,
19001 ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
19002 AST_CLI_YESNO(ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD)),
19003 cur->needdestroy ? "(d)" : "",
19004 cur->lastmsg ,
19005 referstatus,
19006 cur->relatedpeer ? cur->relatedpeer->name : "<guest>"
19007 );
19008 arg->numchans++;
19009 }
19010 if (cur->subscribed != NONE && arg->subscriptions) {
19011 struct ast_str *mailbox_str = ast_str_alloca(512);
19012 if (cur->subscribed == MWI_NOTIFICATION && cur->relatedpeer)
19013 peer_mailboxes_to_str(&mailbox_str, cur->relatedpeer);
19014 ast_cli(arg->fd, FORMAT4, ast_sockaddr_stringify_addr(dst),
19015 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
19016 cur->callid,
19017
19018 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
19019 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
19020 subscription_type2str(cur->subscribed),
19021 cur->subscribed == MWI_NOTIFICATION ? S_OR(ast_str_buffer(mailbox_str), "<none>") : "<none>",
19022 cur->expiry
19023 );
19024 arg->numchans++;
19025 }
19026 sip_pvt_unlock(cur);
19027 return 0;
19028 }
19029
19030
19031
19032
19033
19034
19035 static char *sip_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
19036 {
19037 struct __show_chan_arg arg = { .fd = a->fd, .numchans = 0 };
19038
19039
19040 if (cmd == CLI_INIT) {
19041 e->command = "sip show {channels|subscriptions}";
19042 e->usage =
19043 "Usage: sip show channels\n"
19044 " Lists all currently active SIP calls (dialogs).\n"
19045 "Usage: sip show subscriptions\n"
19046 " Lists active SIP subscriptions.\n";
19047 return NULL;
19048 } else if (cmd == CLI_GENERATE)
19049 return NULL;
19050
19051 if (a->argc != e->args)
19052 return CLI_SHOWUSAGE;
19053 arg.subscriptions = !strcasecmp(a->argv[e->args - 1], "subscriptions");
19054 if (!arg.subscriptions)
19055 ast_cli(arg.fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Format", "Hold", "Last Message", "Expiry", "Peer");
19056 else
19057 ast_cli(arg.fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox", "Expiry");
19058
19059
19060 ao2_t_callback(dialogs, OBJ_NODATA, show_channels_cb, &arg, "callback to show channels");
19061
19062
19063 ast_cli(arg.fd, "%d active SIP %s%s\n", arg.numchans,
19064 (arg.subscriptions ? "subscription" : "dialog"),
19065 ESS(arg.numchans));
19066 return CLI_SUCCESS;
19067 #undef FORMAT
19068 #undef FORMAT2
19069 #undef FORMAT3
19070 }
19071
19072
19073
19074
19075
19076
19077 static char *complete_sipch(const char *line, const char *word, int pos, int state)
19078 {
19079 int which=0;
19080 struct sip_pvt *cur;
19081 char *c = NULL;
19082 int wordlen = strlen(word);
19083 struct ao2_iterator i;
19084
19085 if (pos != 3) {
19086 return NULL;
19087 }
19088
19089 i = ao2_iterator_init(dialogs, 0);
19090 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
19091 sip_pvt_lock(cur);
19092 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
19093 c = ast_strdup(cur->callid);
19094 sip_pvt_unlock(cur);
19095 dialog_unref(cur, "drop ref in iterator loop break");
19096 break;
19097 }
19098 sip_pvt_unlock(cur);
19099 dialog_unref(cur, "drop ref in iterator loop");
19100 }
19101 ao2_iterator_destroy(&i);
19102 return c;
19103 }
19104
19105
19106
19107 static char *complete_sip_peer(const char *word, int state, int flags2)
19108 {
19109 char *result = NULL;
19110 int wordlen = strlen(word);
19111 int which = 0;
19112 struct ao2_iterator i = ao2_iterator_init(peers, 0);
19113 struct sip_peer *peer;
19114
19115 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
19116
19117 if (!strncasecmp(word, peer->name, wordlen) &&
19118 (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
19119 ++which > state)
19120 result = ast_strdup(peer->name);
19121 unref_peer(peer, "toss iterator peer ptr before break");
19122 if (result) {
19123 break;
19124 }
19125 }
19126 ao2_iterator_destroy(&i);
19127 return result;
19128 }
19129
19130
19131 static char *complete_sip_registered_peer(const char *word, int state, int flags2)
19132 {
19133 char *result = NULL;
19134 int wordlen = strlen(word);
19135 int which = 0;
19136 struct ao2_iterator i;
19137 struct sip_peer *peer;
19138
19139 i = ao2_iterator_init(peers, 0);
19140 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
19141 if (!strncasecmp(word, peer->name, wordlen) &&
19142 (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
19143 ++which > state && peer->expire > 0)
19144 result = ast_strdup(peer->name);
19145 if (result) {
19146 unref_peer(peer, "toss iterator peer ptr before break");
19147 break;
19148 }
19149 unref_peer(peer, "toss iterator peer ptr");
19150 }
19151 ao2_iterator_destroy(&i);
19152 return result;
19153 }
19154
19155
19156 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state)
19157 {
19158 if (pos == 3)
19159 return complete_sipch(line, word, pos, state);
19160
19161 return NULL;
19162 }
19163
19164
19165 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
19166 {
19167 if (pos == 3) {
19168 return complete_sip_peer(word, state, 0);
19169 }
19170
19171 return NULL;
19172 }
19173
19174
19175 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state)
19176 {
19177 if (pos == 2)
19178 return complete_sip_registered_peer(word, state, 0);
19179
19180 return NULL;
19181 }
19182
19183
19184 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
19185 {
19186 char *c = NULL;
19187
19188 if (pos == 2) {
19189 int which = 0;
19190 char *cat = NULL;
19191 int wordlen = strlen(word);
19192
19193
19194
19195 if (!notify_types)
19196 return NULL;
19197
19198 while ( (cat = ast_category_browse(notify_types, cat)) ) {
19199 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
19200 c = ast_strdup(cat);
19201 break;
19202 }
19203 }
19204 return c;
19205 }
19206
19207 if (pos > 2)
19208 return complete_sip_peer(word, state, 0);
19209
19210 return NULL;
19211 }
19212
19213 static const char *transport2str(enum sip_transport transport)
19214 {
19215 switch (transport) {
19216 case SIP_TRANSPORT_TLS:
19217 return "TLS";
19218 case SIP_TRANSPORT_UDP:
19219 return "UDP";
19220 case SIP_TRANSPORT_TCP:
19221 return "TCP";
19222 }
19223
19224 return "Undefined";
19225 }
19226
19227
19228 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
19229 {
19230 struct sip_pvt *cur;
19231 size_t len;
19232 int found = 0;
19233 struct ao2_iterator i;
19234
19235 switch (cmd) {
19236 case CLI_INIT:
19237 e->command = "sip show channel";
19238 e->usage =
19239 "Usage: sip show channel <call-id>\n"
19240 " Provides detailed status on a given SIP dialog (identified by SIP call-id).\n";
19241 return NULL;
19242 case CLI_GENERATE:
19243 return complete_sipch(a->line, a->word, a->pos, a->n);
19244 }
19245
19246 if (a->argc != 4)
19247 return CLI_SHOWUSAGE;
19248 len = strlen(a->argv[3]);
19249
19250 i = ao2_iterator_init(dialogs, 0);
19251 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
19252 sip_pvt_lock(cur);
19253
19254 if (!strncasecmp(cur->callid, a->argv[3], len)) {
19255 char formatbuf[SIPBUFSIZE/2];
19256 ast_cli(a->fd, "\n");
19257 if (cur->subscribed != NONE)
19258 ast_cli(a->fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
19259 else
19260 ast_cli(a->fd, " * SIP Call\n");
19261 ast_cli(a->fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
19262 ast_cli(a->fd, " Call-ID: %s\n", cur->callid);
19263 ast_cli(a->fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
19264 ast_cli(a->fd, " Our Codec Capability: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->capability));
19265 ast_cli(a->fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
19266 ast_cli(a->fd, " Their Codec Capability: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->peercapability));
19267 ast_cli(a->fd, " Joint Codec Capability: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->jointcapability));
19268 ast_cli(a->fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
19269 ast_cli(a->fd, " T.38 support %s\n", AST_CLI_YESNO(cur->udptl != NULL));
19270 ast_cli(a->fd, " Video support %s\n", AST_CLI_YESNO(cur->vrtp != NULL));
19271 ast_cli(a->fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
19272 ast_cli(a->fd, " Theoretical Address: %s\n", ast_sockaddr_stringify(&cur->sa));
19273 ast_cli(a->fd, " Received Address: %s\n", ast_sockaddr_stringify(&cur->recv));
19274 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
19275 ast_cli(a->fd, " Force rport: %s\n", AST_CLI_YESNO(ast_test_flag(&cur->flags[0], SIP_NAT_FORCE_RPORT)));
19276 if (ast_sockaddr_isnull(&cur->redirip)) {
19277 ast_cli(a->fd,
19278 " Audio IP: %s (local)\n",
19279 ast_sockaddr_stringify_addr(&cur->ourip));
19280 } else {
19281 ast_cli(a->fd,
19282 " Audio IP: %s (Outside bridge)\n",
19283 ast_sockaddr_stringify_addr(&cur->redirip));
19284 }
19285 ast_cli(a->fd, " Our Tag: %s\n", cur->tag);
19286 ast_cli(a->fd, " Their Tag: %s\n", cur->theirtag);
19287 ast_cli(a->fd, " SIP User agent: %s\n", cur->useragent);
19288 if (!ast_strlen_zero(cur->username))
19289 ast_cli(a->fd, " Username: %s\n", cur->username);
19290 if (!ast_strlen_zero(cur->peername))
19291 ast_cli(a->fd, " Peername: %s\n", cur->peername);
19292 if (!ast_strlen_zero(cur->uri))
19293 ast_cli(a->fd, " Original uri: %s\n", cur->uri);
19294 if (!ast_strlen_zero(cur->cid_num))
19295 ast_cli(a->fd, " Caller-ID: %s\n", cur->cid_num);
19296 ast_cli(a->fd, " Need Destroy: %s\n", AST_CLI_YESNO(cur->needdestroy));
19297 ast_cli(a->fd, " Last Message: %s\n", cur->lastmsg);
19298 ast_cli(a->fd, " Promiscuous Redir: %s\n", AST_CLI_YESNO(ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR)));
19299 ast_cli(a->fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
19300 ast_cli(a->fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
19301 ast_cli(a->fd, " SIP Options: ");
19302 if (cur->sipoptions) {
19303 int x;
19304 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
19305 if (cur->sipoptions & sip_options[x].id)
19306 ast_cli(a->fd, "%s ", sip_options[x].text);
19307 }
19308 ast_cli(a->fd, "\n");
19309 } else
19310 ast_cli(a->fd, "(none)\n");
19311
19312 if (!cur->stimer)
19313 ast_cli(a->fd, " Session-Timer: Uninitiallized\n");
19314 else {
19315 ast_cli(a->fd, " Session-Timer: %s\n", cur->stimer->st_active ? "Active" : "Inactive");
19316 if (cur->stimer->st_active == TRUE) {
19317 ast_cli(a->fd, " S-Timer Interval: %d\n", cur->stimer->st_interval);
19318 ast_cli(a->fd, " S-Timer Refresher: %s\n", strefresher2str(cur->stimer->st_ref));
19319 ast_cli(a->fd, " S-Timer Sched Id: %d\n", cur->stimer->st_schedid);
19320 ast_cli(a->fd, " S-Timer Peer Sts: %s\n", cur->stimer->st_active_peer_ua ? "Active" : "Inactive");
19321 ast_cli(a->fd, " S-Timer Cached Min-SE: %d\n", cur->stimer->st_cached_min_se);
19322 ast_cli(a->fd, " S-Timer Cached SE: %d\n", cur->stimer->st_cached_max_se);
19323 ast_cli(a->fd, " S-Timer Cached Ref: %s\n", strefresherparam2str(cur->stimer->st_cached_ref));
19324 ast_cli(a->fd, " S-Timer Cached Mode: %s\n", stmode2str(cur->stimer->st_cached_mode));
19325 }
19326 }
19327
19328
19329 ast_cli(a->fd, " Transport: %s\n", transport2str(cur->socket.type));
19330 ast_cli(a->fd, " Media: %s\n", cur->srtp ? "SRTP" : cur->rtp ? "RTP" : "None");
19331
19332 ast_cli(a->fd, "\n\n");
19333
19334 found++;
19335 }
19336
19337 sip_pvt_unlock(cur);
19338
19339 ao2_t_ref(cur, -1, "toss dialog ptr set by iterator_next");
19340 }
19341 ao2_iterator_destroy(&i);
19342
19343 if (!found)
19344 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
19345
19346 return CLI_SUCCESS;
19347 }
19348
19349
19350 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
19351 {
19352 struct sip_pvt *cur;
19353 size_t len;
19354 int found = 0;
19355 struct ao2_iterator i;
19356
19357 switch (cmd) {
19358 case CLI_INIT:
19359 e->command = "sip show history";
19360 e->usage =
19361 "Usage: sip show history <call-id>\n"
19362 " Provides detailed dialog history on a given SIP call (specified by call-id).\n";
19363 return NULL;
19364 case CLI_GENERATE:
19365 return complete_sip_show_history(a->line, a->word, a->pos, a->n);
19366 }
19367
19368 if (a->argc != 4)
19369 return CLI_SHOWUSAGE;
19370
19371 if (!recordhistory)
19372 ast_cli(a->fd, "\n***Note: History recording is currently DISABLED. Use 'sip set history on' to ENABLE.\n");
19373
19374 len = strlen(a->argv[3]);
19375
19376 i = ao2_iterator_init(dialogs, 0);
19377 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
19378 sip_pvt_lock(cur);
19379 if (!strncasecmp(cur->callid, a->argv[3], len)) {
19380 struct sip_history *hist;
19381 int x = 0;
19382
19383 ast_cli(a->fd, "\n");
19384 if (cur->subscribed != NONE)
19385 ast_cli(a->fd, " * Subscription\n");
19386 else
19387 ast_cli(a->fd, " * SIP Call\n");
19388 if (cur->history)
19389 AST_LIST_TRAVERSE(cur->history, hist, list)
19390 ast_cli(a->fd, "%d. %s\n", ++x, hist->event);
19391 if (x == 0)
19392 ast_cli(a->fd, "Call '%s' has no history\n", cur->callid);
19393 found++;
19394 }
19395 sip_pvt_unlock(cur);
19396 ao2_t_ref(cur, -1, "toss dialog ptr from iterator_next");
19397 }
19398 ao2_iterator_destroy(&i);
19399
19400 if (!found)
19401 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
19402
19403 return CLI_SUCCESS;
19404 }
19405
19406
19407 static void sip_dump_history(struct sip_pvt *dialog)
19408 {
19409 int x = 0;
19410 struct sip_history *hist;
19411 static int errmsg = 0;
19412
19413 if (!dialog)
19414 return;
19415
19416 if (!option_debug && !sipdebug) {
19417 if (!errmsg) {
19418 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
19419 errmsg = 1;
19420 }
19421 return;
19422 }
19423
19424 ast_debug(1, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
19425 if (dialog->subscribed)
19426 ast_debug(1, " * Subscription\n");
19427 else
19428 ast_debug(1, " * SIP Call\n");
19429 if (dialog->history)
19430 AST_LIST_TRAVERSE(dialog->history, hist, list)
19431 ast_debug(1, " %-3.3d. %s\n", ++x, hist->event);
19432 if (!x)
19433 ast_debug(1, "Call '%s' has no history\n", dialog->callid);
19434 ast_debug(1, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
19435 }
19436
19437
19438
19439 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
19440 {
19441 char buf[1024] = "";
19442 unsigned int event;
19443 const char *c = get_header(req, "Content-Type");
19444
19445
19446 if (!strcasecmp(c, "application/dtmf-relay") ||
19447 !strcasecmp(c, "application/vnd.nortelnetworks.digits") ||
19448 !strcasecmp(c, "application/dtmf")) {
19449 unsigned int duration = 0;
19450
19451 if (!p->owner) {
19452 transmit_response(p, "481 Call leg/transaction does not exist", req);
19453 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19454 return;
19455 }
19456
19457
19458
19459 if (strcasecmp(c, "application/dtmf")) {
19460 const char *msg_body;
19461
19462 if ( ast_strlen_zero(msg_body = get_body(req, "Signal", '='))
19463 && ast_strlen_zero(msg_body = get_body(req, "d", '='))) {
19464 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal for INFO message on "
19465 "call %s\n", p->callid);
19466 transmit_response(p, "200 OK", req);
19467 return;
19468 }
19469 ast_copy_string(buf, msg_body, sizeof(buf));
19470
19471 if (!ast_strlen_zero((msg_body = get_body(req, "Duration", '=')))) {
19472 sscanf(msg_body, "%30u", &duration);
19473 }
19474 } else {
19475
19476 get_msg_text(buf, sizeof(buf), req);
19477 }
19478
19479
19480 if (ast_strlen_zero(buf)) {
19481 transmit_response(p, "200 OK", req);
19482 return;
19483 }
19484
19485 if (!duration) {
19486 duration = 100;
19487 }
19488
19489 if (buf[0] == '*') {
19490 event = 10;
19491 } else if (buf[0] == '#') {
19492 event = 11;
19493 } else if (buf[0] == '!') {
19494 event = 16;
19495 } else if ('A' <= buf[0] && buf[0] <= 'D') {
19496 event = 12 + buf[0] - 'A';
19497 } else if ('a' <= buf[0] && buf[0] <= 'd') {
19498 event = 12 + buf[0] - 'a';
19499 } else if ((sscanf(buf, "%30u", &event) != 1) || event > 16) {
19500 ast_log(AST_LOG_WARNING, "Unable to convert DTMF event signal code to a valid "
19501 "value for INFO message on call %s\n", p->callid);
19502 transmit_response(p, "200 OK", req);
19503 return;
19504 }
19505
19506 if (event == 16) {
19507
19508 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
19509 ast_queue_frame(p->owner, &f);
19510 if (sipdebug) {
19511 ast_verbose("* DTMF-relay event received: FLASH\n");
19512 }
19513 } else {
19514
19515 struct ast_frame f = { AST_FRAME_DTMF, };
19516 if (event < 10) {
19517 f.subclass.integer = '0' + event;
19518 } else if (event == 10) {
19519 f.subclass.integer = '*';
19520 } else if (event == 11) {
19521 f.subclass.integer = '#';
19522 } else {
19523 f.subclass.integer = 'A' + (event - 12);
19524 }
19525 f.len = duration;
19526 ast_queue_frame(p->owner, &f);
19527 if (sipdebug) {
19528 ast_verbose("* DTMF-relay event received: %c\n", (int) f.subclass.integer);
19529 }
19530 }
19531 transmit_response(p, "200 OK", req);
19532 return;
19533 } else if (!strcasecmp(c, "application/media_control+xml")) {
19534
19535 if (p->owner)
19536 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
19537 transmit_response(p, "200 OK", req);
19538 return;
19539 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
19540
19541 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
19542 if (p->owner && p->owner->cdr)
19543 ast_cdr_setuserfield(p->owner, c);
19544 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
19545 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
19546 transmit_response(p, "200 OK", req);
19547 } else {
19548 transmit_response(p, "403 Forbidden", req);
19549 }
19550 return;
19551 } else if (!ast_strlen_zero(c = get_header(req, "Record"))) {
19552
19553
19554
19555
19556
19557
19558
19559 struct ast_call_feature *feat;
19560 int j;
19561 struct ast_frame f = { AST_FRAME_DTMF, };
19562
19563 if (!p->owner) {
19564 transmit_response(p, "481 Call leg/transaction does not exist", req);
19565 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19566 return;
19567 }
19568
19569
19570 ast_rdlock_call_features();
19571 feat = ast_find_call_feature("automon");
19572 if (!feat || ast_strlen_zero(feat->exten)) {
19573 ast_log(LOG_WARNING, "Recording requested, but no One Touch Monitor registered. (See features.conf)\n");
19574
19575 transmit_response(p, "403 Forbidden", req);
19576 ast_unlock_call_features();
19577 return;
19578 }
19579
19580 f.len = 100;
19581 for (j=0; j < strlen(feat->exten); j++) {
19582 f.subclass.integer = feat->exten[j];
19583 ast_queue_frame(p->owner, &f);
19584 if (sipdebug)
19585 ast_verbose("* DTMF-relay event faked: %c\n", f.subclass.integer);
19586 }
19587 ast_unlock_call_features();
19588
19589 ast_debug(1, "Got a Request to Record the channel, state %s\n", c);
19590 transmit_response(p, "200 OK", req);
19591 return;
19592 } else if (ast_strlen_zero(c = get_header(req, "Content-Length")) || !strcasecmp(c, "0")) {
19593
19594 transmit_response(p, "200 OK", req);
19595 return;
19596 }
19597
19598
19599
19600
19601 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
19602 transmit_response(p, "415 Unsupported media type", req);
19603 return;
19604 }
19605
19606
19607 static char *sip_do_debug_ip(int fd, const char *arg)
19608 {
19609 if (ast_sockaddr_resolve_first_af(&debugaddr, arg, 0, 0)) {
19610 return CLI_SHOWUSAGE;
19611 }
19612
19613 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_sockaddr_stringify_addr(&debugaddr));
19614 sipdebug |= sip_debug_console;
19615
19616 return CLI_SUCCESS;
19617 }
19618
19619
19620 static char *sip_do_debug_peer(int fd, const char *arg)
19621 {
19622 struct sip_peer *peer = find_peer(arg, NULL, TRUE, FINDPEERS, FALSE, 0);
19623 if (!peer)
19624 ast_cli(fd, "No such peer '%s'\n", arg);
19625 else if (ast_sockaddr_isnull(&peer->addr))
19626 ast_cli(fd, "Unable to get IP address of peer '%s'\n", arg);
19627 else {
19628 ast_sockaddr_copy(&debugaddr, &peer->addr);
19629 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_sockaddr_stringify_addr(&debugaddr));
19630 sipdebug |= sip_debug_console;
19631 }
19632 if (peer)
19633 unref_peer(peer, "sip_do_debug_peer: unref_peer, from find_peer call");
19634 return CLI_SUCCESS;
19635 }
19636
19637
19638 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
19639 {
19640 int oldsipdebug = sipdebug & sip_debug_console;
19641 const char *what;
19642
19643 if (cmd == CLI_INIT) {
19644 e->command = "sip set debug {on|off|ip|peer}";
19645 e->usage =
19646 "Usage: sip set debug {off|on|ip addr[:port]|peer peername}\n"
19647 " Globally disables dumping of SIP packets,\n"
19648 " or enables it either globally or for a (single)\n"
19649 " IP address or registered peer.\n";
19650 return NULL;
19651 } else if (cmd == CLI_GENERATE) {
19652 if (a->pos == 4 && !strcasecmp(a->argv[3], "peer"))
19653 return complete_sip_peer(a->word, a->n, 0);
19654 return NULL;
19655 }
19656
19657 what = a->argv[e->args-1];
19658 if (a->argc == e->args) {
19659 if (!strcasecmp(what, "on")) {
19660 sipdebug |= sip_debug_console;
19661 sipdebug_text = 1;
19662 memset(&debugaddr, 0, sizeof(debugaddr));
19663 ast_cli(a->fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
19664 return CLI_SUCCESS;
19665 } else if (!strcasecmp(what, "off")) {
19666 sipdebug &= ~sip_debug_console;
19667 sipdebug_text = 0;
19668 ast_cli(a->fd, "SIP Debugging Disabled\n");
19669 return CLI_SUCCESS;
19670 }
19671 } else if (a->argc == e->args +1) {
19672 if (!strcasecmp(what, "ip"))
19673 return sip_do_debug_ip(a->fd, a->argv[e->args]);
19674 else if (!strcasecmp(what, "peer"))
19675 return sip_do_debug_peer(a->fd, a->argv[e->args]);
19676 }
19677 return CLI_SHOWUSAGE;
19678 }
19679
19680
19681 static char *sip_cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
19682 {
19683 struct ast_variable *varlist;
19684 int i;
19685
19686 switch (cmd) {
19687 case CLI_INIT:
19688 e->command = "sip notify";
19689 e->usage =
19690 "Usage: sip notify <type> <peer> [<peer>...]\n"
19691 " Send a NOTIFY message to a SIP peer or peers\n"
19692 " Message types are defined in sip_notify.conf\n";
19693 return NULL;
19694 case CLI_GENERATE:
19695 return complete_sipnotify(a->line, a->word, a->pos, a->n);
19696 }
19697
19698 if (a->argc < 4)
19699 return CLI_SHOWUSAGE;
19700
19701 if (!notify_types) {
19702 ast_cli(a->fd, "No %s file found, or no types listed there\n", notify_config);
19703 return CLI_FAILURE;
19704 }
19705
19706 varlist = ast_variable_browse(notify_types, a->argv[2]);
19707
19708 if (!varlist) {
19709 ast_cli(a->fd, "Unable to find notify type '%s'\n", a->argv[2]);
19710 return CLI_FAILURE;
19711 }
19712
19713 for (i = 3; i < a->argc; i++) {
19714 struct sip_pvt *p;
19715 char buf[512];
19716 struct ast_variable *header, *var;
19717
19718 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
19719 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
19720 return CLI_FAILURE;
19721 }
19722
19723 if (create_addr(p, a->argv[i], NULL, 1)) {
19724
19725 dialog_unlink_all(p);
19726 dialog_unref(p, "unref dialog inside for loop" );
19727
19728 ast_cli(a->fd, "Could not create address for '%s'\n", a->argv[i]);
19729 continue;
19730 }
19731
19732
19733 ast_set_flag(&p->flags[0], SIP_OUTGOING);
19734 sip_notify_allocate(p);
19735 p->notify->headers = header = ast_variable_new("Subscription-State", "terminated", "");
19736
19737 for (var = varlist; var; var = var->next) {
19738 ast_copy_string(buf, var->value, sizeof(buf));
19739 ast_unescape_semicolon(buf);
19740
19741 if (!strcasecmp(var->name, "Content")) {
19742 if (ast_str_strlen(p->notify->content))
19743 ast_str_append(&p->notify->content, 0, "\r\n");
19744 ast_str_append(&p->notify->content, 0, "%s", buf);
19745 } else if (!strcasecmp(var->name, "Content-Length")) {
19746 ast_log(LOG_WARNING, "it is not necessary to specify Content-Length in sip_notify.conf, ignoring\n");
19747 } else {
19748 header->next = ast_variable_new(var->name, buf, "");
19749 header = header->next;
19750 }
19751 }
19752
19753
19754 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
19755 build_via(p);
19756
19757 change_callid_pvt(p, NULL);
19758
19759 ast_cli(a->fd, "Sending NOTIFY of type '%s' to '%s'\n", a->argv[2], a->argv[i]);
19760 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
19761 transmit_invite(p, SIP_NOTIFY, 0, 2, NULL);
19762 dialog_unref(p, "bump down the count of p since we're done with it.");
19763 }
19764
19765 return CLI_SUCCESS;
19766 }
19767
19768
19769 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
19770 {
19771 switch (cmd) {
19772 case CLI_INIT:
19773 e->command = "sip set history {on|off}";
19774 e->usage =
19775 "Usage: sip set history {on|off}\n"
19776 " Enables/Disables recording of SIP dialog history for debugging purposes.\n"
19777 " Use 'sip show history' to view the history of a call number.\n";
19778 return NULL;
19779 case CLI_GENERATE:
19780 return NULL;
19781 }
19782
19783 if (a->argc != e->args)
19784 return CLI_SHOWUSAGE;
19785
19786 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
19787 recordhistory = TRUE;
19788 ast_cli(a->fd, "SIP History Recording Enabled (use 'sip show history')\n");
19789 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
19790 recordhistory = FALSE;
19791 ast_cli(a->fd, "SIP History Recording Disabled\n");
19792 } else {
19793 return CLI_SHOWUSAGE;
19794 }
19795 return CLI_SUCCESS;
19796 }
19797
19798
19799 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code)
19800 {
19801 char *header, *respheader;
19802 char digest[1024];
19803
19804 p->authtries++;
19805 auth_headers(code, &header, &respheader);
19806 memset(digest, 0, sizeof(digest));
19807 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
19808
19809
19810 if (sip_debug_test_pvt(p) && p->registry)
19811 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
19812
19813 return -1;
19814 }
19815 if (p->do_history)
19816 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
19817 if (sip_debug_test_pvt(p) && p->registry)
19818 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
19819 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
19820 }
19821
19822
19823 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code, int sipmethod, int init)
19824 {
19825 char *header, *respheader;
19826 char digest[1024];
19827
19828 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
19829 return -2;
19830
19831 p->authtries++;
19832 auth_headers(code, &header, &respheader);
19833 ast_debug(2, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
19834 memset(digest, 0, sizeof(digest));
19835 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
19836
19837 return -1;
19838 }
19839
19840 p->options->auth = digest;
19841 p->options->authheader = respheader;
19842 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init, NULL);
19843 }
19844
19845
19846
19847
19848
19849 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
19850 {
19851 char tmp[512];
19852 char *c;
19853 char oldnonce[256];
19854
19855
19856 const struct x {
19857 const char *key;
19858 const ast_string_field *field;
19859 } *i, keys[] = {
19860 { "realm=", &p->realm },
19861 { "nonce=", &p->nonce },
19862 { "opaque=", &p->opaque },
19863 { "qop=", &p->qop },
19864 { "domain=", &p->domain },
19865 { NULL, 0 },
19866 };
19867
19868 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
19869 if (ast_strlen_zero(tmp))
19870 return -1;
19871 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
19872 ast_log(LOG_WARNING, "missing Digest.\n");
19873 return -1;
19874 }
19875 c = tmp + strlen("Digest ");
19876 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
19877 while (c && *(c = ast_skip_blanks(c))) {
19878 for (i = keys; i->key != NULL; i++) {
19879 char *src, *separator;
19880 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
19881 continue;
19882
19883 c += strlen(i->key);
19884 if (*c == '"') {
19885 src = ++c;
19886 separator = "\"";
19887 } else {
19888 src = c;
19889 separator = ",";
19890 }
19891 strsep(&c, separator);
19892 ast_string_field_ptr_set(p, i->field, src);
19893 break;
19894 }
19895 if (i->key == NULL)
19896 strsep(&c, ",");
19897 }
19898
19899 if (strcmp(p->nonce, oldnonce))
19900 p->noncecount = 0;
19901
19902
19903 if (p->registry) {
19904 struct sip_registry *r = p->registry;
19905
19906 if (strcmp(r->nonce, p->nonce)) {
19907 ast_string_field_set(r, realm, p->realm);
19908 ast_string_field_set(r, nonce, p->nonce);
19909 ast_string_field_set(r, authdomain, p->domain);
19910 ast_string_field_set(r, opaque, p->opaque);
19911 ast_string_field_set(r, qop, p->qop);
19912 r->noncecount = 0;
19913 }
19914 }
19915 return build_reply_digest(p, sipmethod, digest, digest_len);
19916 }
19917
19918
19919
19920
19921
19922
19923 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
19924 {
19925 char a1[256];
19926 char a2[256];
19927 char a1_hash[256];
19928 char a2_hash[256];
19929 char resp[256];
19930 char resp_hash[256];
19931 char uri[256];
19932 char opaque[256] = "";
19933 char cnonce[80];
19934 const char *username;
19935 const char *secret;
19936 const char *md5secret;
19937 struct sip_auth *auth;
19938 struct sip_auth_container *credentials;
19939
19940 if (!ast_strlen_zero(p->domain))
19941 snprintf(uri, sizeof(uri), "%s:%s", p->socket.type == SIP_TRANSPORT_TLS ? "sips" : "sip", p->domain);
19942 else if (!ast_strlen_zero(p->uri))
19943 ast_copy_string(uri, p->uri, sizeof(uri));
19944 else
19945 snprintf(uri, sizeof(uri), "%s:%s@%s", p->socket.type == SIP_TRANSPORT_TLS ? "sips" : "sip", p->username, ast_sockaddr_stringify_host_remote(&p->sa));
19946
19947 snprintf(cnonce, sizeof(cnonce), "%08lx", (unsigned long)ast_random());
19948
19949
19950 ao2_lock(p);
19951 credentials = p->peerauth;
19952 if (credentials) {
19953 ao2_t_ref(credentials, +1, "Ref peer auth for digest");
19954 }
19955 ao2_unlock(p);
19956 auth = find_realm_authentication(credentials, p->realm);
19957 if (!auth) {
19958
19959 if (credentials) {
19960 ao2_t_ref(credentials, -1, "Unref peer auth for digest");
19961 }
19962 ast_mutex_lock(&authl_lock);
19963 credentials = authl;
19964 if (credentials) {
19965 ao2_t_ref(credentials, +1, "Ref global auth for digest");
19966 }
19967 ast_mutex_unlock(&authl_lock);
19968 auth = find_realm_authentication(credentials, p->realm);
19969 }
19970
19971 if (auth) {
19972 ast_debug(3, "use realm [%s] from peer [%s][%s]\n", auth->username, p->peername, p->username);
19973 username = auth->username;
19974 secret = auth->secret;
19975 md5secret = auth->md5secret;
19976 if (sipdebug)
19977 ast_debug(1, "Using realm %s authentication for call %s\n", p->realm, p->callid);
19978 } else {
19979
19980 username = p->authname;
19981 secret = p->relatedpeer
19982 && !ast_strlen_zero(p->relatedpeer->remotesecret)
19983 ? p->relatedpeer->remotesecret : p->peersecret;
19984 md5secret = p->peermd5secret;
19985 }
19986 if (ast_strlen_zero(username)) {
19987
19988 if (credentials) {
19989 ao2_t_ref(credentials, -1, "Unref auth for digest");
19990 }
19991 return -1;
19992 }
19993
19994
19995 snprintf(a1, sizeof(a1), "%s:%s:%s", username, p->realm, secret);
19996 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[method].text, uri);
19997 if (!ast_strlen_zero(md5secret))
19998 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
19999 else
20000 ast_md5_hash(a1_hash, a1);
20001 ast_md5_hash(a2_hash, a2);
20002
20003 p->noncecount++;
20004 if (!ast_strlen_zero(p->qop))
20005 snprintf(resp, sizeof(resp), "%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, (unsigned)p->noncecount, cnonce, "auth", a2_hash);
20006 else
20007 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, p->nonce, a2_hash);
20008 ast_md5_hash(resp_hash, resp);
20009
20010
20011 if (!ast_strlen_zero(p->opaque)) {
20012 snprintf(opaque, sizeof(opaque), ", opaque=\"%s\"", p->opaque);
20013 }
20014
20015
20016 if (!ast_strlen_zero(p->qop))
20017 snprintf(digest, digest_len, "Digest username=\"%s\", realm=\"%s\", algorithm=MD5, uri=\"%s\", nonce=\"%s\", response=\"%s\"%s, qop=auth, cnonce=\"%s\", nc=%08x", username, p->realm, uri, p->nonce, resp_hash, opaque, cnonce, (unsigned)p->noncecount);
20018 else
20019 snprintf(digest, digest_len, "Digest username=\"%s\", realm=\"%s\", algorithm=MD5, uri=\"%s\", nonce=\"%s\", response=\"%s\"%s", username, p->realm, uri, p->nonce, resp_hash, opaque);
20020
20021 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
20022
20023 if (credentials) {
20024 ao2_t_ref(credentials, -1, "Unref auth for digest");
20025 }
20026 return 0;
20027 }
20028
20029
20030 static int func_header_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
20031 {
20032 struct sip_pvt *p;
20033 const char *content = NULL;
20034 AST_DECLARE_APP_ARGS(args,
20035 AST_APP_ARG(header);
20036 AST_APP_ARG(number);
20037 );
20038 int i, number, start = 0;
20039
20040 if (!chan) {
20041 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
20042 return -1;
20043 }
20044
20045 if (ast_strlen_zero(data)) {
20046 ast_log(LOG_WARNING, "This function requires a header name.\n");
20047 return -1;
20048 }
20049
20050 ast_channel_lock(chan);
20051 if (!IS_SIP_TECH(chan->tech)) {
20052 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
20053 ast_channel_unlock(chan);
20054 return -1;
20055 }
20056
20057 AST_STANDARD_APP_ARGS(args, data);
20058 if (!args.number) {
20059 number = 1;
20060 } else {
20061 sscanf(args.number, "%30d", &number);
20062 if (number < 1)
20063 number = 1;
20064 }
20065
20066 p = chan->tech_pvt;
20067
20068
20069 if (!p) {
20070 ast_channel_unlock(chan);
20071 return -1;
20072 }
20073
20074 for (i = 0; i < number; i++)
20075 content = __get_header(&p->initreq, args.header, &start);
20076
20077 if (ast_strlen_zero(content)) {
20078 ast_channel_unlock(chan);
20079 return -1;
20080 }
20081
20082 ast_copy_string(buf, content, len);
20083 ast_channel_unlock(chan);
20084
20085 return 0;
20086 }
20087
20088 static struct ast_custom_function sip_header_function = {
20089 .name = "SIP_HEADER",
20090 .read = func_header_read,
20091 };
20092
20093
20094 static int func_check_sipdomain(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
20095 {
20096 if (ast_strlen_zero(data)) {
20097 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
20098 return -1;
20099 }
20100 if (check_sip_domain(data, NULL, 0))
20101 ast_copy_string(buf, data, len);
20102 else
20103 buf[0] = '\0';
20104 return 0;
20105 }
20106
20107 static struct ast_custom_function checksipdomain_function = {
20108 .name = "CHECKSIPDOMAIN",
20109 .read = func_check_sipdomain,
20110 };
20111
20112
20113 static int function_sippeer(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
20114 {
20115 struct sip_peer *peer;
20116 char *colname;
20117
20118 if ((colname = strchr(data, ':'))) {
20119 static int deprecation_warning = 0;
20120 *colname++ = '\0';
20121 if (deprecation_warning++ % 10 == 0)
20122 ast_log(LOG_WARNING, "SIPPEER(): usage of ':' to separate arguments is deprecated. Please use ',' instead.\n");
20123 } else if ((colname = strchr(data, ',')))
20124 *colname++ = '\0';
20125 else
20126 colname = "ip";
20127
20128 if (!(peer = find_peer(data, NULL, TRUE, FINDPEERS, FALSE, 0)))
20129 return -1;
20130
20131 if (!strcasecmp(colname, "ip")) {
20132 ast_copy_string(buf, ast_sockaddr_stringify_addr(&peer->addr), len);
20133 } else if (!strcasecmp(colname, "port")) {
20134 snprintf(buf, len, "%d", ast_sockaddr_port(&peer->addr));
20135 } else if (!strcasecmp(colname, "status")) {
20136 peer_status(peer, buf, len);
20137 } else if (!strcasecmp(colname, "language")) {
20138 ast_copy_string(buf, peer->language, len);
20139 } else if (!strcasecmp(colname, "regexten")) {
20140 ast_copy_string(buf, peer->regexten, len);
20141 } else if (!strcasecmp(colname, "limit")) {
20142 snprintf(buf, len, "%d", peer->call_limit);
20143 } else if (!strcasecmp(colname, "busylevel")) {
20144 snprintf(buf, len, "%d", peer->busy_level);
20145 } else if (!strcasecmp(colname, "curcalls")) {
20146 snprintf(buf, len, "%d", peer->inUse);
20147 } else if (!strcasecmp(colname, "maxforwards")) {
20148 snprintf(buf, len, "%d", peer->maxforwards);
20149 } else if (!strcasecmp(colname, "accountcode")) {
20150 ast_copy_string(buf, peer->accountcode, len);
20151 } else if (!strcasecmp(colname, "callgroup")) {
20152 ast_print_group(buf, len, peer->callgroup);
20153 } else if (!strcasecmp(colname, "pickupgroup")) {
20154 ast_print_group(buf, len, peer->pickupgroup);
20155 } else if (!strcasecmp(colname, "useragent")) {
20156 ast_copy_string(buf, peer->useragent, len);
20157 } else if (!strcasecmp(colname, "mailbox")) {
20158 struct ast_str *mailbox_str = ast_str_alloca(512);
20159 peer_mailboxes_to_str(&mailbox_str, peer);
20160 ast_copy_string(buf, ast_str_buffer(mailbox_str), len);
20161 } else if (!strcasecmp(colname, "context")) {
20162 ast_copy_string(buf, peer->context, len);
20163 } else if (!strcasecmp(colname, "expire")) {
20164 snprintf(buf, len, "%d", peer->expire);
20165 } else if (!strcasecmp(colname, "dynamic")) {
20166 ast_copy_string(buf, peer->host_dynamic ? "yes" : "no", len);
20167 } else if (!strcasecmp(colname, "callerid_name")) {
20168 ast_copy_string(buf, peer->cid_name, len);
20169 } else if (!strcasecmp(colname, "callerid_num")) {
20170 ast_copy_string(buf, peer->cid_num, len);
20171 } else if (!strcasecmp(colname, "codecs")) {
20172 ast_getformatname_multiple(buf, len -1, peer->capability);
20173 } else if (!strcasecmp(colname, "encryption")) {
20174 snprintf(buf, len, "%u", ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP));
20175 } else if (!strncasecmp(colname, "chanvar[", 8)) {
20176 char *chanvar=colname + 8;
20177 struct ast_variable *v;
20178
20179 chanvar = strsep(&chanvar, "]");
20180 for (v = peer->chanvars ; v ; v = v->next) {
20181 if (!strcasecmp(v->name, chanvar)) {
20182 ast_copy_string(buf, v->value, len);
20183 }
20184 }
20185 } else if (!strncasecmp(colname, "codec[", 6)) {
20186 char *codecnum;
20187 format_t codec = 0;
20188
20189 codecnum = colname + 6;
20190 codecnum = strsep(&codecnum, "]");
20191 if((codec = ast_codec_pref_index(&peer->prefs, atoi(codecnum)))) {
20192 ast_copy_string(buf, ast_getformatname(codec), len);
20193 } else {
20194 buf[0] = '\0';
20195 }
20196 } else {
20197 buf[0] = '\0';
20198 }
20199
20200 unref_peer(peer, "unref_peer from function_sippeer, just before return");
20201
20202 return 0;
20203 }
20204
20205
20206 static struct ast_custom_function sippeer_function = {
20207 .name = "SIPPEER",
20208 .read = function_sippeer,
20209 };
20210
20211
20212 static int function_sipchaninfo_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
20213 {
20214 struct sip_pvt *p;
20215 static int deprecated = 0;
20216
20217 *buf = 0;
20218
20219 if (!chan) {
20220 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
20221 return -1;
20222 }
20223
20224 if (!data) {
20225 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
20226 return -1;
20227 }
20228
20229 ast_channel_lock(chan);
20230 if (!IS_SIP_TECH(chan->tech)) {
20231 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
20232 ast_channel_unlock(chan);
20233 return -1;
20234 }
20235
20236 if (deprecated++ % 20 == 0) {
20237
20238 ast_log(LOG_WARNING, "SIPCHANINFO() is deprecated. Please transition to using CHANNEL().\n");
20239 }
20240
20241 p = chan->tech_pvt;
20242
20243
20244 if (!p) {
20245 ast_channel_unlock(chan);
20246 return -1;
20247 }
20248
20249 if (!strcasecmp(data, "peerip")) {
20250 ast_copy_string(buf, ast_sockaddr_stringify_addr(&p->sa), len);
20251 } else if (!strcasecmp(data, "recvip")) {
20252 ast_copy_string(buf, ast_sockaddr_stringify_addr(&p->recv), len);
20253 } else if (!strcasecmp(data, "from")) {
20254 ast_copy_string(buf, p->from, len);
20255 } else if (!strcasecmp(data, "uri")) {
20256 ast_copy_string(buf, p->uri, len);
20257 } else if (!strcasecmp(data, "useragent")) {
20258 ast_copy_string(buf, p->useragent, len);
20259 } else if (!strcasecmp(data, "peername")) {
20260 ast_copy_string(buf, p->peername, len);
20261 } else if (!strcasecmp(data, "t38passthrough")) {
20262 if (p->t38.state == T38_DISABLED) {
20263 ast_copy_string(buf, "0", len);
20264 } else {
20265 ast_copy_string(buf, "1", len);
20266 }
20267 } else {
20268 ast_channel_unlock(chan);
20269 return -1;
20270 }
20271 ast_channel_unlock(chan);
20272
20273 return 0;
20274 }
20275
20276
20277 static struct ast_custom_function sipchaninfo_function = {
20278 .name = "SIPCHANINFO",
20279 .read = function_sipchaninfo_read,
20280 };
20281
20282
20283
20284
20285 static void change_redirecting_information(struct sip_pvt *p, struct sip_request *req,
20286 struct ast_party_redirecting *redirecting,
20287 struct ast_set_party_redirecting *update_redirecting, int set_call_forward)
20288 {
20289 char *redirecting_from_name = NULL;
20290 char *redirecting_from_number = NULL;
20291 char *redirecting_to_name = NULL;
20292 char *redirecting_to_number = NULL;
20293 int reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
20294 int is_response = req->method == SIP_RESPONSE;
20295 int res = 0;
20296
20297 res = get_rdnis(p, req, &redirecting_from_name, &redirecting_from_number, &reason);
20298 if (res == -1) {
20299 if (is_response) {
20300 get_name_and_number(get_header(req, "TO"), &redirecting_from_name, &redirecting_from_number);
20301 } else {
20302 return;
20303 }
20304 }
20305
20306
20307
20308
20309
20310 if (is_response) {
20311 parse_moved_contact(p, req, &redirecting_to_name, &redirecting_to_number, set_call_forward);
20312 } else {
20313 get_name_and_number(get_header(req, "TO"), &redirecting_to_name, &redirecting_to_number);
20314 }
20315
20316 if (!ast_strlen_zero(redirecting_from_number)) {
20317 ast_debug(3, "Got redirecting from number %s\n", redirecting_from_number);
20318 update_redirecting->from.number = 1;
20319 redirecting->from.number.valid = 1;
20320 ast_free(redirecting->from.number.str);
20321 redirecting->from.number.str = redirecting_from_number;
20322 }
20323 if (!ast_strlen_zero(redirecting_from_name)) {
20324 ast_debug(3, "Got redirecting from name %s\n", redirecting_from_name);
20325 update_redirecting->from.name = 1;
20326 redirecting->from.name.valid = 1;
20327 ast_free(redirecting->from.name.str);
20328 redirecting->from.name.str = redirecting_from_name;
20329 }
20330 if (!ast_strlen_zero(p->cid_tag)) {
20331 ast_free(redirecting->from.tag);
20332 redirecting->from.tag = ast_strdup(p->cid_tag);
20333 ast_free(redirecting->to.tag);
20334 redirecting->to.tag = ast_strdup(p->cid_tag);
20335 }
20336 if (!ast_strlen_zero(redirecting_to_number)) {
20337 ast_debug(3, "Got redirecting to number %s\n", redirecting_to_number);
20338 update_redirecting->to.number = 1;
20339 redirecting->to.number.valid = 1;
20340 ast_free(redirecting->to.number.str);
20341 redirecting->to.number.str = redirecting_to_number;
20342 }
20343 if (!ast_strlen_zero(redirecting_to_name)) {
20344 ast_debug(3, "Got redirecting to name %s\n", redirecting_from_number);
20345 update_redirecting->to.name = 1;
20346 redirecting->to.name.valid = 1;
20347 ast_free(redirecting->to.name.str);
20348 redirecting->to.name.str = redirecting_to_name;
20349 }
20350 redirecting->reason = reason;
20351 }
20352
20353
20354
20355
20356
20357
20358
20359 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req, char **name, char **number, int set_call_forward)
20360 {
20361 char contact[SIPBUFSIZE];
20362 char *contact_name = NULL;
20363 char *contact_number = NULL;
20364 char *separator, *trans;
20365 char *domain;
20366 enum sip_transport transport = SIP_TRANSPORT_UDP;
20367
20368 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
20369 if ((separator = strchr(contact, ',')))
20370 *separator = '\0';
20371
20372 contact_number = get_in_brackets(contact);
20373 if ((trans = strcasestr(contact_number, ";transport="))) {
20374 trans += 11;
20375
20376 if ((separator = strchr(trans, ';')))
20377 *separator = '\0';
20378
20379 if (!strncasecmp(trans, "tcp", 3))
20380 transport = SIP_TRANSPORT_TCP;
20381 else if (!strncasecmp(trans, "tls", 3))
20382 transport = SIP_TRANSPORT_TLS;
20383 else {
20384 if (strncasecmp(trans, "udp", 3))
20385 ast_debug(1, "received contact with an invalid transport, '%s'\n", contact_number);
20386
20387 transport = SIP_TRANSPORT_UDP;
20388 }
20389 }
20390 contact_number = remove_uri_parameters(contact_number);
20391
20392 if (p->socket.tcptls_session) {
20393 ao2_ref(p->socket.tcptls_session, -1);
20394 p->socket.tcptls_session = NULL;
20395 }
20396
20397 set_socket_transport(&p->socket, transport);
20398
20399 if (set_call_forward && ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
20400 char *host = NULL;
20401 if (!strncasecmp(contact_number, "sip:", 4))
20402 contact_number += 4;
20403 else if (!strncasecmp(contact_number, "sips:", 5))
20404 contact_number += 5;
20405 separator = strchr(contact_number, '/');
20406 if (separator)
20407 *separator = '\0';
20408 if ((host = strchr(contact_number, '@'))) {
20409 *host++ = '\0';
20410 ast_debug(2, "Found promiscuous redirection to 'SIP/%s::::%s@%s'\n", contact_number, get_transport(transport), host);
20411 if (p->owner)
20412 ast_string_field_build(p->owner, call_forward, "SIP/%s::::%s@%s", contact_number, get_transport(transport), host);
20413 } else {
20414 ast_debug(2, "Found promiscuous redirection to 'SIP/::::%s@%s'\n", get_transport(transport), contact_number);
20415 if (p->owner)
20416 ast_string_field_build(p->owner, call_forward, "SIP/::::%s@%s", get_transport(transport), contact_number);
20417 }
20418 } else {
20419 separator = strchr(contact, '@');
20420 if (separator) {
20421 *separator++ = '\0';
20422 domain = separator;
20423 } else {
20424
20425 domain = contact;
20426 }
20427 separator = strchr(contact, '/');
20428 if (separator)
20429 *separator = '\0';
20430
20431 if (!strncasecmp(contact_number, "sip:", 4))
20432 contact_number += 4;
20433 else if (!strncasecmp(contact_number, "sips:", 5))
20434 contact_number += 5;
20435 separator = strchr(contact_number, ';');
20436 if (separator)
20437 *separator = '\0';
20438 ast_uri_decode(contact_number);
20439 if (set_call_forward) {
20440 ast_debug(2, "Received 302 Redirect to extension '%s' (domain %s)\n", contact_number, domain);
20441 if (p->owner) {
20442 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
20443 ast_string_field_set(p->owner, call_forward, contact_number);
20444 }
20445 }
20446 }
20447
20448
20449
20450 if (*contact == '\"') {
20451 contact_name = contact + 1;
20452 if (!(separator = (char *)find_closing_quote(contact_name, NULL))) {
20453 ast_log(LOG_NOTICE, "No closing quote on name in Contact header? %s\n", contact);
20454 }
20455 *separator = '\0';
20456 }
20457
20458 if (name && !ast_strlen_zero(contact_name)) {
20459 *name = ast_strdup(contact_name);
20460 }
20461 if (number) {
20462 *number = ast_strdup(contact_number);
20463 }
20464 }
20465
20466
20467
20468
20469
20470
20471 static void check_pendings(struct sip_pvt *p)
20472 {
20473 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
20474 if (p->reinviteid > -1) {
20475
20476 return;
20477 } else if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA) {
20478
20479 p->invitestate = INV_CANCELLED;
20480 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
20481
20482 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
20483 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
20484 }
20485
20486
20487 } else {
20488
20489
20490
20491 if (p->pendinginvite && !p->ongoing_reinvite)
20492 return;
20493
20494 if (p->owner) {
20495 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
20496 }
20497
20498 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
20499 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
20500 }
20501 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20502 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
20503
20504 if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
20505 ast_debug(2, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
20506 } else {
20507 ast_debug(2, "Sending pending reinvite on '%s'\n", p->callid);
20508
20509 transmit_reinvite_with_sdp(p, (p->t38.state == T38_LOCAL_REINVITE ? TRUE : FALSE), FALSE);
20510 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
20511 }
20512 }
20513 }
20514
20515
20516
20517
20518
20519 static int sip_reinvite_retry(const void *data)
20520 {
20521 struct sip_pvt *p = (struct sip_pvt *) data;
20522 struct ast_channel *owner;
20523
20524 sip_pvt_lock(p);
20525 while ((owner = p->owner) && ast_channel_trylock(owner)) {
20526 sip_pvt_unlock(p);
20527 usleep(1);
20528 sip_pvt_lock(p);
20529 }
20530 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
20531 p->waitid = -1;
20532 check_pendings(p);
20533 sip_pvt_unlock(p);
20534 if (owner) {
20535 ast_channel_unlock(owner);
20536 }
20537 dialog_unref(p, "unref the dialog ptr from sip_reinvite_retry, because it held a dialog ptr");
20538 return 0;
20539 }
20540
20541
20542
20543
20544
20545
20546 static void handle_response_update(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
20547 {
20548 if (p->options) {
20549 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
20550 }
20551 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, SIP_UPDATE, 1)) {
20552 ast_log(LOG_NOTICE, "Failed to authenticate on UPDATE to '%s'\n", get_header(&p->initreq, "From"));
20553 }
20554 }
20555
20556 static void cc_handle_publish_error(struct sip_pvt *pvt, const int resp, struct sip_request *req, struct sip_epa_entry *epa_entry)
20557 {
20558 struct cc_epa_entry *cc_entry = epa_entry->instance_data;
20559 struct sip_monitor_instance *monitor_instance = ao2_callback(sip_monitor_instances, 0,
20560 find_sip_monitor_instance_by_suspension_entry, epa_entry);
20561 const char *min_expires;
20562
20563 if (!monitor_instance) {
20564 ast_log(LOG_WARNING, "Can't find monitor_instance corresponding to epa_entry %p.\n", epa_entry);
20565 return;
20566 }
20567
20568 if (resp != 423) {
20569 ast_cc_monitor_failed(cc_entry->core_id, monitor_instance->device_name,
20570 "Received error response to our PUBLISH");
20571 ao2_ref(monitor_instance, -1);
20572 return;
20573 }
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583 min_expires = get_header(req, "Min-Expires");
20584 if (ast_strlen_zero(min_expires)) {
20585 pvt->expiry *= 2;
20586 if (pvt->expiry < 0) {
20587
20588 ast_cc_monitor_failed(cc_entry->core_id, monitor_instance->device_name,
20589 "PUBLISH expiry overflowed");
20590 ao2_ref(monitor_instance, -1);
20591 return;
20592 }
20593 } else if (sscanf(min_expires, "%d", &pvt->expiry) != 1) {
20594 ast_cc_monitor_failed(cc_entry->core_id, monitor_instance->device_name,
20595 "Min-Expires has non-numeric value");
20596 ao2_ref(monitor_instance, -1);
20597 return;
20598 }
20599
20600
20601
20602 transmit_invite(pvt, SIP_PUBLISH, FALSE, 0, NULL);
20603 ao2_ref(monitor_instance, -1);
20604 }
20605
20606 static void handle_response_publish(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
20607 {
20608 struct sip_epa_entry *epa_entry = p->epa_entry;
20609 const char *etag = get_header(req, "Sip-ETag");
20610
20611 ast_assert(epa_entry != NULL);
20612
20613 if (resp == 401 || resp == 407) {
20614 ast_string_field_set(p, theirtag, NULL);
20615 if (p->options) {
20616 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
20617 }
20618 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, SIP_PUBLISH, 0)) {
20619 ast_log(LOG_NOTICE, "Failed to authenticate on PUBLISH to '%s'\n", get_header(&p->initreq, "From"));
20620 pvt_set_needdestroy(p, "Failed to authenticate on PUBLISH");
20621 sip_alreadygone(p);
20622 }
20623 return;
20624 }
20625
20626 if (resp == 501 || resp == 405) {
20627 mark_method_unallowed(&p->allowed_methods, SIP_PUBLISH);
20628 }
20629
20630 if (resp == 200) {
20631 p->authtries = 0;
20632
20633
20634
20635 if (!ast_strlen_zero(etag)) {
20636 ast_copy_string(epa_entry->entity_tag, etag, sizeof(epa_entry->entity_tag));
20637 }
20638
20639
20640
20641
20642 if (epa_entry->static_data->handle_ok) {
20643 epa_entry->static_data->handle_ok(p, req, epa_entry);
20644 }
20645 } else {
20646
20647
20648
20649
20650 if (epa_entry->static_data->handle_error) {
20651 epa_entry->static_data->handle_error(p, resp, req, epa_entry);
20652 }
20653 }
20654 }
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667 static void sip_queue_hangup_cause(struct sip_pvt *p, int cause)
20668 {
20669 struct ast_channel *owner = p->owner;
20670 const char *name = ast_strdupa(owner->name);
20671
20672
20673 ast_channel_ref(owner);
20674 ast_channel_unlock(owner);
20675 sip_pvt_unlock(p);
20676 ast_set_hangupsource(owner, name, 0);
20677 if (cause) {
20678 ast_queue_hangup_with_cause(owner, cause);
20679 } else {
20680 ast_queue_hangup(owner);
20681 }
20682 ast_channel_unref(owner);
20683
20684
20685 owner = sip_pvt_lock_full(p);
20686 if (owner) {
20687 ast_channel_unref(owner);
20688 }
20689 }
20690
20691
20692 static void handle_response_invite(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
20693 {
20694 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
20695 int res = 0;
20696 int xmitres = 0;
20697 int reinvite = ast_test_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
20698 char *p_hdrval;
20699 int rtn;
20700 struct ast_party_connected_line connected;
20701 struct ast_set_party_connected_line update_connected;
20702
20703 if (reinvite)
20704 ast_debug(4, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
20705 else
20706 ast_debug(4, "SIP response %d to standard invite\n", resp);
20707
20708 if (p->alreadygone) {
20709 ast_debug(1, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
20710 return;
20711 }
20712
20713
20714
20715 AST_SCHED_DEL_UNREF(sched, p->initid, dialog_unref(p, "when you delete the initid sched, you should dec the refcount for the stored dialog ptr"));
20716
20717
20718
20719
20720 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 181 && resp != 182 && resp != 183)
20721 resp = 183;
20722
20723
20724 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
20725 p->invitestate = INV_PROCEEDING;
20726
20727
20728 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
20729 p->invitestate = INV_COMPLETED;
20730
20731 if ((resp >= 200 && reinvite)) {
20732 p->ongoing_reinvite = 0;
20733 if (p->reinviteid > -1) {
20734 AST_SCHED_DEL_UNREF(sched, p->reinviteid, dialog_unref(p, "unref dialog for reinvite timeout because of a final response"));
20735 }
20736 }
20737
20738
20739 if ((resp == 200 || resp >= 300) && p->pendinginvite && seqno == p->pendinginvite) {
20740 p->pendinginvite = 0;
20741 }
20742
20743
20744
20745
20746 if (!reinvite) {
20747 set_pvt_allowed_methods(p, req);
20748 }
20749
20750 switch (resp) {
20751 case 100:
20752 case 101:
20753 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
20754 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20755 check_pendings(p);
20756 break;
20757
20758 case 180:
20759 case 182:
20760 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
20761 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20762
20763
20764
20765 parse_ok_contact(p, req);
20766 if (!reinvite) {
20767 build_route(p, req, 1, resp);
20768 }
20769 if (!req->ignore && p->owner) {
20770 if (get_rpid(p, req)) {
20771
20772 ast_party_connected_line_init(&connected);
20773 memset(&update_connected, 0, sizeof(update_connected));
20774
20775 update_connected.id.number = 1;
20776 connected.id.number.valid = 1;
20777 connected.id.number.str = (char *) p->cid_num;
20778 connected.id.number.presentation = p->callingpres;
20779
20780 update_connected.id.name = 1;
20781 connected.id.name.valid = 1;
20782 connected.id.name.str = (char *) p->cid_name;
20783 connected.id.name.presentation = p->callingpres;
20784
20785 connected.id.tag = (char *) p->cid_tag;
20786 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
20787 ast_channel_queue_connected_line_update(p->owner, &connected,
20788 &update_connected);
20789 }
20790 sip_handle_cc(p, req, AST_CC_CCNR);
20791 ast_queue_control(p->owner, AST_CONTROL_RINGING);
20792 if (p->owner->_state != AST_STATE_UP) {
20793 ast_setstate(p->owner, AST_STATE_RINGING);
20794 }
20795 }
20796 if (find_sdp(req)) {
20797 if (p->invitestate != INV_CANCELLED)
20798 p->invitestate = INV_EARLY_MEDIA;
20799 res = process_sdp(p, req, SDP_T38_NONE);
20800 if (!req->ignore && p->owner) {
20801
20802 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
20803 }
20804 ast_rtp_instance_activate(p->rtp);
20805 }
20806 check_pendings(p);
20807 break;
20808
20809 case 181:
20810 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
20811 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20812
20813
20814
20815 parse_ok_contact(p, req);
20816 if (!reinvite) {
20817 build_route(p, req, 1, resp);
20818 }
20819 if (!req->ignore && p->owner) {
20820 struct ast_party_redirecting redirecting;
20821 struct ast_set_party_redirecting update_redirecting;
20822
20823 ast_party_redirecting_init(&redirecting);
20824 memset(&update_redirecting, 0, sizeof(update_redirecting));
20825 change_redirecting_information(p, req, &redirecting, &update_redirecting,
20826 FALSE);
20827 ast_channel_queue_redirecting_update(p->owner, &redirecting,
20828 &update_redirecting);
20829 ast_party_redirecting_free(&redirecting);
20830 sip_handle_cc(p, req, AST_CC_CCNR);
20831 }
20832 check_pendings(p);
20833 break;
20834
20835 case 183:
20836 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
20837 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20838
20839
20840
20841 parse_ok_contact(p, req);
20842 if (!reinvite) {
20843 build_route(p, req, 1, resp);
20844 }
20845 if (!req->ignore && p->owner) {
20846 if (get_rpid(p, req)) {
20847
20848 ast_party_connected_line_init(&connected);
20849 memset(&update_connected, 0, sizeof(update_connected));
20850
20851 update_connected.id.number = 1;
20852 connected.id.number.valid = 1;
20853 connected.id.number.str = (char *) p->cid_num;
20854 connected.id.number.presentation = p->callingpres;
20855
20856 update_connected.id.name = 1;
20857 connected.id.name.valid = 1;
20858 connected.id.name.str = (char *) p->cid_name;
20859 connected.id.name.presentation = p->callingpres;
20860
20861 connected.id.tag = (char *) p->cid_tag;
20862 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
20863 ast_channel_queue_connected_line_update(p->owner, &connected,
20864 &update_connected);
20865 }
20866 sip_handle_cc(p, req, AST_CC_CCNR);
20867 }
20868 if (find_sdp(req)) {
20869 if (p->invitestate != INV_CANCELLED)
20870 p->invitestate = INV_EARLY_MEDIA;
20871 res = process_sdp(p, req, SDP_T38_NONE);
20872 if (!req->ignore && p->owner) {
20873
20874 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
20875 }
20876 ast_rtp_instance_activate(p->rtp);
20877 } else {
20878
20879
20880
20881
20882 if (!req->ignore && p->owner) {
20883 ast_queue_control(p->owner, AST_CONTROL_RINGING);
20884 }
20885 }
20886 check_pendings(p);
20887 break;
20888
20889 case 200:
20890 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
20891 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20892 p->authtries = 0;
20893 if (find_sdp(req)) {
20894 if ((res = process_sdp(p, req, SDP_T38_ACCEPT)) && !req->ignore) {
20895 if (!reinvite) {
20896
20897
20898 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
20899 p->hangupcause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
20900 if (p->owner) {
20901 p->owner->hangupcause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
20902 sip_queue_hangup_cause(p, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
20903 }
20904 }
20905 }
20906 ast_rtp_instance_activate(p->rtp);
20907 } else if (!reinvite) {
20908 struct ast_sockaddr remote_address = {{0,}};
20909
20910 ast_rtp_instance_get_remote_address(p->rtp, &remote_address);
20911 if (ast_sockaddr_isnull(&remote_address) || (!ast_strlen_zero(p->theirprovtag) && strcmp(p->theirtag, p->theirprovtag))) {
20912 ast_log(LOG_WARNING, "Received response: \"200 OK\" from '%s' without SDP\n", p->relatedpeer->name);
20913 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
20914 ast_rtp_instance_activate(p->rtp);
20915 }
20916 }
20917
20918 if (!req->ignore && p->owner) {
20919 int rpid_changed;
20920
20921 rpid_changed = get_rpid(p, req);
20922 if (rpid_changed || !reinvite) {
20923
20924 ast_party_connected_line_init(&connected);
20925 memset(&update_connected, 0, sizeof(update_connected));
20926 if (rpid_changed
20927 || !ast_strlen_zero(p->cid_num)
20928 || (p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
20929 update_connected.id.number = 1;
20930 connected.id.number.valid = 1;
20931 connected.id.number.str = (char *) p->cid_num;
20932 connected.id.number.presentation = p->callingpres;
20933 }
20934 if (rpid_changed
20935 || !ast_strlen_zero(p->cid_name)
20936 || (p->callingpres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
20937 update_connected.id.name = 1;
20938 connected.id.name.valid = 1;
20939 connected.id.name.str = (char *) p->cid_name;
20940 connected.id.name.presentation = p->callingpres;
20941 }
20942 if (update_connected.id.number || update_connected.id.name) {
20943 connected.id.tag = (char *) p->cid_tag;
20944 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
20945 ast_channel_queue_connected_line_update(p->owner, &connected,
20946 &update_connected);
20947 }
20948 }
20949 }
20950
20951
20952
20953
20954 if (outgoing) {
20955 update_call_counter(p, DEC_CALL_RINGING);
20956 parse_ok_contact(p, req);
20957
20958 if (!reinvite) {
20959 build_route(p, req, 1, resp);
20960 }
20961 if(set_address_from_contact(p)) {
20962
20963
20964 if (!p->route && !req->ignore)
20965 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
20966 }
20967
20968 }
20969
20970 if (!req->ignore && p->owner) {
20971 if (!reinvite && !res) {
20972 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
20973 if (sip_cfg.callevents)
20974 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
20975 "Channel: %s\r\nChanneltype: %s\r\nUniqueid: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
20976 p->owner->name, "SIP", p->owner->uniqueid, p->callid, p->fullcontact, p->peername);
20977 } else {
20978 if (p->t38.state == T38_DISABLED) {
20979 ast_queue_control(p->owner, AST_CONTROL_UPDATE_RTP_PEER);
20980 } else {
20981 ast_queue_frame(p->owner, &ast_null_frame);
20982 }
20983 }
20984 } else {
20985
20986
20987
20988 if (!req->ignore)
20989 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
20990 }
20991
20992
20993 if (st_get_mode(p, 0) != SESSION_TIMER_MODE_REFUSE) {
20994 p_hdrval = (char*)get_header(req, "Session-Expires");
20995 if (!ast_strlen_zero(p_hdrval)) {
20996
20997 enum st_refresher_param st_ref_param;
20998 int tmp_st_interval = 0;
20999 rtn = parse_session_expires(p_hdrval, &tmp_st_interval, &st_ref_param);
21000 if (rtn != 0) {
21001 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
21002 } else if (tmp_st_interval < st_get_se(p, FALSE)) {
21003 ast_log(LOG_WARNING, "Got Session-Expires less than local Min-SE in 200 OK, tearing down call\n");
21004 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
21005 }
21006 if (st_ref_param == SESSION_TIMER_REFRESHER_PARAM_UAC) {
21007 p->stimer->st_ref = SESSION_TIMER_REFRESHER_US;
21008 } else if (st_ref_param == SESSION_TIMER_REFRESHER_PARAM_UAS) {
21009 p->stimer->st_ref = SESSION_TIMER_REFRESHER_THEM;
21010 } else {
21011 ast_log(LOG_WARNING, "Unknown refresher on %s\n", p->callid);
21012 }
21013 if (tmp_st_interval) {
21014 p->stimer->st_interval = tmp_st_interval;
21015 }
21016 p->stimer->st_active = TRUE;
21017 p->stimer->st_active_peer_ua = TRUE;
21018 start_session_timer(p);
21019 } else {
21020
21021 if (st_get_mode(p, 0) == SESSION_TIMER_MODE_ORIGINATE) {
21022 p->stimer->st_ref = SESSION_TIMER_REFRESHER_US;
21023 p->stimer->st_active_peer_ua = FALSE;
21024 start_session_timer(p);
21025 }
21026 }
21027 }
21028
21029
21030
21031 p->invitestate = INV_TERMINATED;
21032 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
21033 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
21034 check_pendings(p);
21035 break;
21036
21037 case 407:
21038 case 401:
21039
21040 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21041 if (p->options)
21042 p->options->auth_type = resp;
21043
21044
21045 ast_string_field_set(p, theirtag, NULL);
21046 if (!req->ignore) {
21047 if (p->authtries < MAX_AUTHTRIES)
21048 p->invitestate = INV_CALLING;
21049 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, SIP_INVITE, 1)) {
21050 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
21051 pvt_set_needdestroy(p, "failed to authenticate on INVITE");
21052 sip_alreadygone(p);
21053 if (p->owner)
21054 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
21055 }
21056 }
21057 break;
21058
21059 case 403:
21060
21061 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21062 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
21063 if (!req->ignore && p->owner) {
21064 sip_queue_hangup_cause(p, hangup_sip2cause(resp));
21065 }
21066 break;
21067
21068 case 414:
21069 case 493:
21070 case 404:
21071 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21072 if (p->owner && !req->ignore) {
21073 sip_queue_hangup_cause(p, hangup_sip2cause(resp));
21074 }
21075 break;
21076
21077 case 481:
21078
21079 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
21080 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21081 if (p->owner) {
21082 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(resp));
21083 }
21084 break;
21085
21086 case 422:
21087 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21088 ast_string_field_set(p, theirtag, NULL);
21089 proc_422_rsp(p, req);
21090 break;
21091
21092 case 428:
21093 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21094 append_history(p, "Identity", "SIP identity is required. Not supported by Asterisk.");
21095 ast_log(LOG_WARNING, "SIP identity required by proxy. SIP dialog '%s'. Giving up.\n", p->callid);
21096 if (p->owner && !req->ignore) {
21097 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(resp));
21098 }
21099 break;
21100
21101 case 487:
21102
21103
21104
21105 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21106 if (p->owner && !req->ignore) {
21107 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_NORMAL_CLEARING);
21108 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
21109 } else if (!req->ignore) {
21110 update_call_counter(p, DEC_CALL_LIMIT);
21111 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
21112 }
21113 check_pendings(p);
21114 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21115 break;
21116 case 415:
21117 case 488:
21118 case 606:
21119 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21120 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
21121 change_t38_state(p, T38_DISABLED);
21122
21123
21124
21125
21126 transmit_reinvite_with_sdp(p, FALSE, FALSE);
21127 } else {
21128
21129 if (p->owner && !req->ignore) {
21130 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(resp));
21131 }
21132 }
21133 break;
21134 case 491:
21135 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21136 if (p->owner && !req->ignore) {
21137 if (p->owner->_state != AST_STATE_UP) {
21138 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(resp));
21139 } else {
21140
21141
21142
21143 int wait;
21144
21145
21146 if (p->outgoing_call) {
21147 wait = 2100 + ast_random() % 2000;
21148 } else {
21149 wait = ast_random() % 2000;
21150 }
21151 p->waitid = ast_sched_add(sched, wait, sip_reinvite_retry, dialog_ref(p, "passing dialog ptr into sched structure based on waitid for sip_reinvite_retry."));
21152 ast_debug(2, "Reinvite race. Scheduled sip_reinvite_retry in %d secs in handle_response_invite (waitid %d, dialog '%s')\n",
21153 wait, p->waitid, p->callid);
21154 }
21155 }
21156 break;
21157
21158 case 408:
21159 case 405:
21160 case 501:
21161 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
21162 if (p->owner) {
21163 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(resp));
21164 }
21165 break;
21166 }
21167 if (xmitres == XMIT_ERROR)
21168 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
21169 }
21170
21171
21172
21173
21174 static void handle_response_notify(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
21175 {
21176 switch (resp) {
21177 case 200:
21178
21179 if (p->owner) {
21180 if (p->refer) {
21181 ast_log(LOG_NOTICE, "Got OK on REFER Notify message\n");
21182 } else {
21183 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
21184 }
21185 } else {
21186 if (p->subscribed == NONE && !p->refer) {
21187 ast_debug(4, "Got 200 accepted on NOTIFY %s\n", p->callid);
21188 pvt_set_needdestroy(p, "received 200 response");
21189 }
21190 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
21191
21192 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
21193 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
21194 }
21195 }
21196 break;
21197 case 401:
21198 case 407:
21199 if (!p->notify) {
21200 break;
21201 }
21202 ast_string_field_set(p, theirtag, NULL);
21203 if (ast_strlen_zero(p->authname)) {
21204 ast_log(LOG_WARNING, "Asked to authenticate NOTIFY to %s but we have no matching peer or realm auth!\n", ast_sockaddr_stringify(&p->recv));
21205 pvt_set_needdestroy(p, "unable to authenticate NOTIFY");
21206 }
21207 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_NOTIFY, 0)) {
21208 ast_log(LOG_NOTICE, "Failed to authenticate on NOTIFY to '%s'\n", get_header(&p->initreq, "From"));
21209 pvt_set_needdestroy(p, "failed to authenticate NOTIFY");
21210 }
21211 break;
21212 case 481:
21213 pvt_set_needdestroy(p, "Received 481 response for NOTIFY");
21214 break;
21215 }
21216 }
21217
21218
21219 static void handle_response_subscribe(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
21220 {
21221 if (p->subscribed == CALL_COMPLETION) {
21222 struct sip_monitor_instance *monitor_instance;
21223
21224 if (resp < 300) {
21225 return;
21226 }
21227
21228
21229 monitor_instance = ao2_callback(sip_monitor_instances, 0,
21230 find_sip_monitor_instance_by_subscription_pvt, p);
21231 if (monitor_instance) {
21232 ast_cc_monitor_failed(monitor_instance->core_id,
21233 monitor_instance->device_name,
21234 "Received error response to our SUBSCRIBE");
21235 }
21236 return;
21237 }
21238
21239 if (p->subscribed != MWI_NOTIFICATION) {
21240 return;
21241 }
21242 if (!p->mwi) {
21243 return;
21244 }
21245
21246 switch (resp) {
21247 case 200:
21248 ast_debug(3, "Got 200 OK on subscription for MWI\n");
21249 set_pvt_allowed_methods(p, req);
21250 if (p->options) {
21251 if (p->options->outboundproxy) {
21252 ao2_ref(p->options->outboundproxy, -1);
21253 }
21254 ast_free(p->options);
21255 p->options = NULL;
21256 }
21257 p->mwi->subscribed = 1;
21258 if ((p->mwi->resub = ast_sched_add(sched, mwi_expiry * 1000, sip_subscribe_mwi_do, ASTOBJ_REF(p->mwi))) < 0) {
21259 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
21260 }
21261 break;
21262 case 401:
21263 case 407:
21264 ast_string_field_set(p, theirtag, NULL);
21265 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_SUBSCRIBE, 0)) {
21266 ast_log(LOG_NOTICE, "Failed to authenticate on SUBSCRIBE to '%s'\n", get_header(&p->initreq, "From"));
21267 p->mwi->call = NULL;
21268 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
21269 pvt_set_needdestroy(p, "failed to authenticate SUBSCRIBE");
21270 }
21271 break;
21272 case 403:
21273 transmit_response_with_date(p, "200 OK", req);
21274 ast_log(LOG_WARNING, "Authentication failed while trying to subscribe for MWI.\n");
21275 p->mwi->call = NULL;
21276 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
21277 pvt_set_needdestroy(p, "received 403 response");
21278 sip_alreadygone(p);
21279 break;
21280 case 404:
21281 ast_log(LOG_WARNING, "Subscription failed for MWI. The remote side said that a mailbox may not have been configured.\n");
21282 p->mwi->call = NULL;
21283 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
21284 pvt_set_needdestroy(p, "received 404 response");
21285 break;
21286 case 481:
21287 ast_log(LOG_WARNING, "Subscription failed for MWI. The remote side said that our dialog did not exist.\n");
21288 p->mwi->call = NULL;
21289 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
21290 pvt_set_needdestroy(p, "received 481 response");
21291 break;
21292
21293 case 400:
21294 case 414:
21295 case 493:
21296 case 500:
21297 case 501:
21298 ast_log(LOG_WARNING, "Subscription failed for MWI. The remote side may have suffered a heart attack.\n");
21299 p->mwi->call = NULL;
21300 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
21301 pvt_set_needdestroy(p, "received serious error (500/501/493/414/400) response");
21302 break;
21303 }
21304 }
21305
21306
21307
21308
21309 static void handle_response_refer(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
21310 {
21311 enum ast_control_transfer message = AST_TRANSFER_FAILED;
21312
21313
21314 if (!p->refer)
21315 return;
21316
21317 switch (resp) {
21318 case 202:
21319
21320
21321 p->refer->status = REFER_ACCEPTED;
21322
21323 ast_debug(3, "Got 202 accepted on transfer\n");
21324
21325 break;
21326
21327 case 401:
21328 case 407:
21329 if (ast_strlen_zero(p->authname)) {
21330 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s but we have no matching peer or realm auth!\n",
21331 ast_sockaddr_stringify(&p->recv));
21332 if (p->owner) {
21333 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
21334 }
21335 pvt_set_needdestroy(p, "unable to authenticate REFER");
21336 }
21337 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_REFER, 0)) {
21338 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
21339 p->refer->status = REFER_NOAUTH;
21340 if (p->owner) {
21341 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
21342 }
21343 pvt_set_needdestroy(p, "failed to authenticate REFER");
21344 }
21345 break;
21346
21347 case 405:
21348
21349
21350 ast_log(LOG_NOTICE, "SIP transfer to %s failed, REFER not allowed. \n", p->refer->refer_to);
21351 pvt_set_needdestroy(p, "received 405 response");
21352 p->refer->status = REFER_FAILED;
21353 if (p->owner) {
21354 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
21355 }
21356 break;
21357
21358 case 481:
21359
21360
21361
21362
21363 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
21364 if (p->owner)
21365 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
21366 pvt_set_needdestroy(p, "received 481 response");
21367 break;
21368
21369 case 500:
21370 case 501:
21371
21372
21373 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
21374 pvt_set_needdestroy(p, "received 500/501 response");
21375 p->refer->status = REFER_FAILED;
21376 if (p->owner) {
21377 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
21378 }
21379 break;
21380 case 603:
21381 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
21382 p->refer->status = REFER_FAILED;
21383 pvt_set_needdestroy(p, "received 603 response");
21384 if (p->owner) {
21385 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
21386 }
21387 break;
21388 default:
21389
21390
21391
21392
21393 if (resp < 299) {
21394 ast_log(LOG_WARNING, "SIP transfer to %s had unxpected 2xx response (%d), confusion is possible. \n", p->refer->refer_to, resp);
21395 } else {
21396 ast_log(LOG_WARNING, "SIP transfer to %s with response (%d). \n", p->refer->refer_to, resp);
21397 }
21398
21399 p->refer->status = REFER_FAILED;
21400 pvt_set_needdestroy(p, "received failure response");
21401 if (p->owner) {
21402 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
21403 }
21404 break;
21405 }
21406 }
21407
21408
21409 static int handle_response_register(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
21410 {
21411 int expires, expires_ms;
21412 struct sip_registry *r;
21413 r=p->registry;
21414
21415 switch (resp) {
21416 case 401:
21417 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
21418 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
21419 pvt_set_needdestroy(p, "failed to authenticate REGISTER");
21420 }
21421 break;
21422 case 403:
21423 if (global_reg_retry_403) {
21424 ast_log(LOG_NOTICE, "Treating 403 response to REGISTER as non-fatal for %s@%s\n",
21425 p->registry->username, p->registry->hostname);
21426 ast_string_field_set(r, nonce, "");
21427 ast_string_field_set(p, nonce, "");
21428 break;
21429 }
21430 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
21431 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 403"));
21432 r->regstate = REG_STATE_NOAUTH;
21433 pvt_set_needdestroy(p, "received 403 response");
21434 break;
21435 case 404:
21436 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username, p->registry->hostname);
21437 pvt_set_needdestroy(p, "received 404 response");
21438 if (r->call)
21439 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 404");
21440 r->regstate = REG_STATE_REJECTED;
21441 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 404"));
21442 break;
21443 case 407:
21444 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
21445 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
21446 pvt_set_needdestroy(p, "failed to authenticate REGISTER");
21447 }
21448 break;
21449 case 408:
21450
21451 if (r) {
21452 r->regattempts = 0;
21453 } else {
21454 ast_log(LOG_WARNING, "Got a 408 response to our REGISTER on call %s after we had destroyed the registry object\n", p->callid);
21455 }
21456 break;
21457 case 423:
21458 r->expiry = atoi(get_header(req, "Min-Expires"));
21459 ast_log(LOG_WARNING, "Got 423 Interval too brief for service %s@%s, minimum is %d seconds\n", p->registry->username, p->registry->hostname, r->expiry);
21460 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 423"));
21461 if (r->call) {
21462 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 423");
21463 pvt_set_needdestroy(p, "received 423 response");
21464 }
21465 if (r->expiry > max_expiry) {
21466 ast_log(LOG_WARNING, "Required expiration time from %s@%s is too high, giving up\n", p->registry->username, p->registry->hostname);
21467 r->expiry = r->configured_expiry;
21468 r->regstate = REG_STATE_REJECTED;
21469 } else {
21470 r->regstate = REG_STATE_UNREGISTERED;
21471 transmit_register(r, SIP_REGISTER, NULL, NULL);
21472 }
21473 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nUsername: %s\r\nDomain: %s\r\nStatus: %s\r\n", r->username, r->hostname, regstate2str(r->regstate));
21474 break;
21475 case 400:
21476 case 414:
21477 case 493:
21478 case 479:
21479 ast_log(LOG_WARNING, "Got error %d on register to %s@%s, giving up (check config)\n", resp, p->registry->username, p->registry->hostname);
21480 pvt_set_needdestroy(p, "received 4xx response");
21481 if (r->call)
21482 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 4xx");
21483 r->regstate = REG_STATE_REJECTED;
21484 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 479"));
21485 break;
21486 case 200:
21487 if (!r) {
21488 ast_log(LOG_WARNING, "Got 200 OK on REGISTER, but there isn't a registry entry for '%s' (we probably already got the OK)\n", S_OR(p->peername, p->username));
21489 pvt_set_needdestroy(p, "received erroneous 200 response");
21490 return 0;
21491 }
21492
21493 r->regstate = REG_STATE_REGISTERED;
21494 r->regtime = ast_tvnow();
21495 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nUsername: %s\r\nDomain: %s\r\nStatus: %s\r\n", r->username, r->hostname, regstate2str(r->regstate));
21496 r->regattempts = 0;
21497 ast_debug(1, "Registration successful\n");
21498 if (r->timeout > -1) {
21499 ast_debug(1, "Cancelling timeout %d\n", r->timeout);
21500 }
21501 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 200"));
21502 if (r->call)
21503 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 200");
21504 p->registry = registry_unref(p->registry, "unref registry entry p->registry");
21505
21506
21507 pvt_set_needdestroy(p, "Registration successfull");
21508
21509
21510
21511
21512
21513 expires = 0;
21514
21515
21516 if (!ast_strlen_zero(get_header(req, "Contact"))) {
21517 const char *contact = NULL;
21518 const char *tmptmp = NULL;
21519 int start = 0;
21520 for(;;) {
21521 contact = __get_header(req, "Contact", &start);
21522
21523 if(!ast_strlen_zero(contact)) {
21524 if( (tmptmp=strstr(contact, p->our_contact))) {
21525 contact=tmptmp;
21526 break;
21527 }
21528 } else
21529 break;
21530 }
21531 tmptmp = strcasestr(contact, "expires=");
21532 if (tmptmp) {
21533 if (sscanf(tmptmp + 8, "%30d", &expires) != 1) {
21534 expires = 0;
21535 }
21536 }
21537
21538 }
21539 if (!expires)
21540 expires=atoi(get_header(req, "expires"));
21541 if (!expires)
21542 expires=default_expiry;
21543
21544 expires_ms = expires * 1000;
21545 if (expires <= EXPIRY_GUARD_LIMIT)
21546 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT), EXPIRY_GUARD_MIN);
21547 else
21548 expires_ms -= EXPIRY_GUARD_SECS * 1000;
21549 if (sipdebug)
21550 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
21551
21552 r->refresh= (int) expires_ms / 1000;
21553
21554
21555 AST_SCHED_REPLACE_UNREF(r->expire, sched, expires_ms, sip_reregister, r,
21556 registry_unref(_data,"unref in REPLACE del fail"),
21557 registry_unref(r,"unref in REPLACE add fail"),
21558 registry_addref(r,"The Addition side of REPLACE"));
21559 }
21560 return 1;
21561 }
21562
21563
21564 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
21565 {
21566 struct sip_peer *peer = p->relatedpeer ;
21567 int statechanged, is_reachable, was_reachable;
21568 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
21569
21570
21571
21572
21573
21574
21575 if (pingtime < 1) {
21576 pingtime = 1;
21577 }
21578
21579 if (!peer->maxms) {
21580 pvt_set_needdestroy(p, "got OPTIONS response but qualify is not enabled");
21581 return;
21582 }
21583
21584
21585
21586
21587
21588 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
21589 is_reachable = pingtime <= peer->maxms;
21590 statechanged = peer->lastms == 0
21591 || was_reachable != is_reachable;
21592
21593 peer->lastms = pingtime;
21594 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
21595 if (statechanged) {
21596 const char *s = is_reachable ? "Reachable" : "Lagged";
21597 char str_lastms[20];
21598 snprintf(str_lastms, sizeof(str_lastms), "%d", pingtime);
21599
21600 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
21601 peer->name, s, pingtime, peer->maxms);
21602 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "SIP/%s", peer->name);
21603 if (sip_cfg.peer_rtupdate) {
21604 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", str_lastms, SENTINEL);
21605 }
21606 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
21607 "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
21608 peer->name, s, pingtime);
21609 if (is_reachable && sip_cfg.regextenonqualify)
21610 register_peer_exten(peer, TRUE);
21611 }
21612
21613 pvt_set_needdestroy(p, "got OPTIONS response");
21614
21615
21616 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched,
21617 is_reachable ? peer->qualifyfreq : DEFAULT_FREQ_NOTOK,
21618 sip_poke_peer_s, peer,
21619 unref_peer(_data, "removing poke peer ref"),
21620 unref_peer(peer, "removing poke peer ref"),
21621 ref_peer(peer, "adding poke peer ref"));
21622 }
21623
21624
21625
21626
21627
21628
21629
21630
21631 static void handle_response_info(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
21632 {
21633 int sipmethod = SIP_INFO;
21634
21635 switch (resp) {
21636 case 401:
21637 case 407:
21638 ast_log(LOG_WARNING, "Host '%s' requests authentication (%d) for '%s'\n",
21639 ast_sockaddr_stringify(&p->sa), resp, sip_methods[sipmethod].text);
21640 break;
21641 case 405:
21642 case 501:
21643 mark_method_unallowed(&p->allowed_methods, sipmethod);
21644 if (p->relatedpeer) {
21645 mark_method_allowed(&p->relatedpeer->disallowed_methods, sipmethod);
21646 }
21647 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n",
21648 ast_sockaddr_stringify(&p->sa), sip_methods[sipmethod].text);
21649 break;
21650 default:
21651 if (300 <= resp && resp < 700) {
21652 ast_verb(3, "Got SIP %s response %d \"%s\" back from host '%s'\n",
21653 sip_methods[sipmethod].text, resp, rest, ast_sockaddr_stringify(&p->sa));
21654 }
21655 break;
21656 }
21657 }
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667 static void handle_response_message(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
21668 {
21669 int sipmethod = SIP_MESSAGE;
21670
21671
21672
21673 switch (resp) {
21674 case 401:
21675 case 407:
21676 ast_log(LOG_WARNING, "Host '%s' requests authentication (%d) for '%s'\n",
21677 ast_sockaddr_stringify(&p->sa), resp, sip_methods[sipmethod].text);
21678 break;
21679 case 405:
21680 case 501:
21681 mark_method_unallowed(&p->allowed_methods, sipmethod);
21682 if (p->relatedpeer) {
21683 mark_method_allowed(&p->relatedpeer->disallowed_methods, sipmethod);
21684 }
21685 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n",
21686 ast_sockaddr_stringify(&p->sa), sip_methods[sipmethod].text);
21687 break;
21688 default:
21689 if (100 <= resp && resp < 200) {
21690
21691 } else if (200 <= resp && resp < 300) {
21692 p->authtries = 0;
21693 } else if (300 <= resp && resp < 700) {
21694 ast_verb(3, "Got SIP %s response %d \"%s\" back from host '%s'\n",
21695 sip_methods[sipmethod].text, resp, rest, ast_sockaddr_stringify(&p->sa));
21696 }
21697 break;
21698 }
21699 }
21700
21701
21702 static void stop_media_flows(struct sip_pvt *p)
21703 {
21704
21705 if (p->rtp)
21706 ast_rtp_instance_stop(p->rtp);
21707 if (p->vrtp)
21708 ast_rtp_instance_stop(p->vrtp);
21709 if (p->trtp)
21710 ast_rtp_instance_stop(p->trtp);
21711 if (p->udptl)
21712 ast_udptl_stop(p->udptl);
21713 }
21714
21715
21716
21717 static void handle_response(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno)
21718 {
21719 struct ast_channel *owner;
21720 int sipmethod;
21721 const char *c = get_header(req, "Cseq");
21722
21723 char *c_copy = ast_strdupa(c);
21724
21725 const char *msg = ast_skip_blanks(ast_skip_nonblanks(c_copy));
21726
21727 if (!msg)
21728 msg = "";
21729
21730 sipmethod = find_sip_method(msg);
21731
21732 owner = p->owner;
21733 if (owner) {
21734 const char *rp = NULL, *rh = NULL;
21735
21736 owner->hangupcause = 0;
21737 if (ast_test_flag(&p->flags[1], SIP_PAGE2_Q850_REASON) && (rh = get_header(req, "Reason"))) {
21738 rh = ast_skip_blanks(rh);
21739 if (!strncasecmp(rh, "Q.850", 5)) {
21740 rp = strstr(rh, "cause=");
21741 if (rp && sscanf(rp + 6, "%30d", &owner->hangupcause) == 1) {
21742 owner->hangupcause &= 0x7f;
21743 if (req->debug)
21744 ast_verbose("Using Reason header for cause code: %d\n", owner->hangupcause);
21745 }
21746 }
21747 }
21748
21749 if (!owner->hangupcause)
21750 owner->hangupcause = hangup_sip2cause(resp);
21751 }
21752
21753 if (p->socket.type == SIP_TRANSPORT_UDP) {
21754 int ack_res = FALSE;
21755
21756
21757 if ((resp >= 100) && (resp <= 199)) {
21758
21759 if (sipmethod == SIP_INVITE) {
21760 ack_res = __sip_semi_ack(p, seqno, 0, sipmethod);
21761 }
21762 } else {
21763 ack_res = __sip_ack(p, seqno, 0, sipmethod);
21764 }
21765
21766 if (ack_res == FALSE) {
21767
21768 if (sipmethod == SIP_INVITE && resp >= 200) {
21769 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, resp < 300 ? TRUE: FALSE);
21770 }
21771
21772 append_history(p, "Ignore", "Ignoring this retransmit\n");
21773 return;
21774 }
21775 }
21776
21777
21778 if (!p->owner && sipmethod == SIP_NOTIFY && p->pendinginvite) {
21779 p->pendinginvite = 0;
21780 }
21781
21782
21783 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
21784 char tag[128];
21785
21786 gettag(req, "To", tag, sizeof(tag));
21787 ast_string_field_set(p, theirtag, tag);
21788 } else {
21789
21790 ast_string_field_set(p, theirprovtag, p->theirtag);
21791 }
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808 if ((resp == 404 || resp == 408 || resp == 481) && sipmethod == SIP_BYE) {
21809 pvt_set_needdestroy(p, "received 4XX response to a BYE");
21810 return;
21811 }
21812
21813 if (p->relatedpeer && sipmethod == SIP_OPTIONS) {
21814
21815
21816
21817 if (resp != 100)
21818 handle_response_peerpoke(p, resp, req);
21819 } else if (sipmethod == SIP_REFER && resp >= 200) {
21820 handle_response_refer(p, resp, rest, req, seqno);
21821 } else if (sipmethod == SIP_PUBLISH) {
21822
21823
21824
21825 handle_response_publish(p, resp, rest, req, seqno);
21826 } else if (sipmethod == SIP_INFO) {
21827
21828 handle_response_info(p, resp, rest, req, seqno);
21829 } else if (sipmethod == SIP_MESSAGE) {
21830
21831 handle_response_message(p, resp, rest, req, seqno);
21832 } else if (sipmethod == SIP_NOTIFY) {
21833
21834 handle_response_notify(p, resp, rest, req, seqno);
21835 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
21836 switch(resp) {
21837 case 100:
21838 case 101:
21839 case 183:
21840 case 180:
21841 case 182:
21842 case 181:
21843 if (sipmethod == SIP_INVITE)
21844 handle_response_invite(p, resp, rest, req, seqno);
21845 break;
21846 case 200:
21847 p->authtries = 0;
21848 if (sipmethod == SIP_INVITE) {
21849 handle_response_invite(p, resp, rest, req, seqno);
21850 } else if (sipmethod == SIP_REGISTER) {
21851 handle_response_register(p, resp, rest, req, seqno);
21852 } else if (sipmethod == SIP_SUBSCRIBE) {
21853 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
21854 handle_response_subscribe(p, resp, rest, req, seqno);
21855 } else if (sipmethod == SIP_BYE) {
21856 pvt_set_needdestroy(p, "received 200 response");
21857 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
21858 }
21859 break;
21860 case 401:
21861 case 407:
21862 if (sipmethod == SIP_INVITE)
21863 handle_response_invite(p, resp, rest, req, seqno);
21864 else if (sipmethod == SIP_SUBSCRIBE)
21865 handle_response_subscribe(p, resp, rest, req, seqno);
21866 else if (p->registry && sipmethod == SIP_REGISTER)
21867 handle_response_register(p, resp, rest, req, seqno);
21868 else if (sipmethod == SIP_UPDATE) {
21869 handle_response_update(p, resp, rest, req, seqno);
21870 } else if (sipmethod == SIP_BYE) {
21871 if (p->options)
21872 p->options->auth_type = resp;
21873 if (ast_strlen_zero(p->authname)) {
21874 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s but we have no matching peer!\n",
21875 msg, ast_sockaddr_stringify(&p->recv));
21876 pvt_set_needdestroy(p, "unable to authenticate BYE");
21877 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, sipmethod, 0)) {
21878 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
21879 pvt_set_needdestroy(p, "failed to authenticate BYE");
21880 }
21881 } else {
21882 ast_log(LOG_WARNING, "Got authentication request (%d) on %s to '%s'\n", resp, sip_methods[sipmethod].text, get_header(req, "To"));
21883 pvt_set_needdestroy(p, "received 407 response");
21884 }
21885 break;
21886 case 403:
21887 if (sipmethod == SIP_INVITE)
21888 handle_response_invite(p, resp, rest, req, seqno);
21889 else if (sipmethod == SIP_SUBSCRIBE)
21890 handle_response_subscribe(p, resp, rest, req, seqno);
21891 else if (p->registry && sipmethod == SIP_REGISTER)
21892 handle_response_register(p, resp, rest, req, seqno);
21893 else {
21894 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
21895 pvt_set_needdestroy(p, "received 403 response");
21896 }
21897 break;
21898 case 400:
21899 case 414:
21900 case 493:
21901 case 404:
21902 if (p->registry && sipmethod == SIP_REGISTER)
21903 handle_response_register(p, resp, rest, req, seqno);
21904 else if (sipmethod == SIP_INVITE)
21905 handle_response_invite(p, resp, rest, req, seqno);
21906 else if (sipmethod == SIP_SUBSCRIBE)
21907 handle_response_subscribe(p, resp, rest, req, seqno);
21908 else if (owner)
21909 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
21910 break;
21911 case 423:
21912 if (sipmethod == SIP_REGISTER)
21913 handle_response_register(p, resp, rest, req, seqno);
21914 break;
21915 case 408:
21916 if (sipmethod == SIP_INVITE)
21917 handle_response_invite(p, resp, rest, req, seqno);
21918 else if (sipmethod == SIP_REGISTER)
21919 handle_response_register(p, resp, rest, req, seqno);
21920 else if (sipmethod == SIP_BYE) {
21921 pvt_set_needdestroy(p, "received 408 response");
21922 ast_debug(4, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
21923 } else {
21924 if (owner)
21925 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
21926 pvt_set_needdestroy(p, "received 408 response");
21927 }
21928 break;
21929
21930 case 428:
21931 case 422:
21932 if (sipmethod == SIP_INVITE) {
21933 handle_response_invite(p, resp, rest, req, seqno);
21934 }
21935 break;
21936
21937 case 481:
21938 if (sipmethod == SIP_INVITE) {
21939 handle_response_invite(p, resp, rest, req, seqno);
21940 } else if (sipmethod == SIP_SUBSCRIBE) {
21941 handle_response_subscribe(p, resp, rest, req, seqno);
21942 } else if (sipmethod == SIP_BYE) {
21943
21944
21945 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
21946 } else if (sipmethod == SIP_CANCEL) {
21947
21948
21949 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
21950 } else {
21951 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
21952
21953 }
21954 break;
21955 case 487:
21956 if (sipmethod == SIP_INVITE)
21957 handle_response_invite(p, resp, rest, req, seqno);
21958 break;
21959 case 415:
21960 case 488:
21961 case 606:
21962 if (sipmethod == SIP_INVITE)
21963 handle_response_invite(p, resp, rest, req, seqno);
21964 break;
21965 case 491:
21966 if (sipmethod == SIP_INVITE)
21967 handle_response_invite(p, resp, rest, req, seqno);
21968 else {
21969 ast_debug(1, "Got 491 on %s, unsupported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
21970 pvt_set_needdestroy(p, "received 491 response");
21971 }
21972 break;
21973 case 405:
21974 case 501:
21975 mark_method_unallowed(&p->allowed_methods, sipmethod);
21976 if (p->relatedpeer) {
21977 mark_method_allowed(&p->relatedpeer->disallowed_methods, sipmethod);
21978 }
21979 if (sipmethod == SIP_INVITE)
21980 handle_response_invite(p, resp, rest, req, seqno);
21981 else
21982 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_sockaddr_stringify(&p->sa), msg);
21983 break;
21984 default:
21985 if ((resp >= 300) && (resp < 700)) {
21986
21987 if ((resp != 487))
21988 ast_verb(3, "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_sockaddr_stringify(&p->sa));
21989
21990 if (sipmethod == SIP_INVITE)
21991 stop_media_flows(p);
21992
21993
21994 switch(resp) {
21995 case 300:
21996 case 301:
21997 case 302:
21998 case 305:
21999 if (p->owner) {
22000 struct ast_party_redirecting redirecting;
22001 struct ast_set_party_redirecting update_redirecting;
22002
22003 ast_party_redirecting_init(&redirecting);
22004 memset(&update_redirecting, 0, sizeof(update_redirecting));
22005 change_redirecting_information(p, req, &redirecting,
22006 &update_redirecting, TRUE);
22007 ast_channel_set_redirecting(p->owner, &redirecting,
22008 &update_redirecting);
22009 ast_party_redirecting_free(&redirecting);
22010 }
22011
22012 case 486:
22013 case 600:
22014 case 603:
22015 if (p->owner) {
22016 sip_handle_cc(p, req, AST_CC_CCBS);
22017 ast_queue_control(p->owner, AST_CONTROL_BUSY);
22018 }
22019 break;
22020 case 482:
22021 case 480:
22022 case 404:
22023 case 410:
22024 case 400:
22025 case 500:
22026 if (sipmethod == SIP_SUBSCRIBE) {
22027 handle_response_subscribe(p, resp, rest, req, seqno);
22028 break;
22029 }
22030
22031 case 502:
22032 case 503:
22033 case 504:
22034 if (owner)
22035 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
22036 break;
22037 case 484:
22038 if (owner && sipmethod != SIP_BYE) {
22039 switch (ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP)) {
22040 case SIP_PAGE2_ALLOWOVERLAP_YES:
22041 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(resp));
22042 break;
22043 default:
22044 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(404));
22045 break;
22046 }
22047 }
22048 break;
22049 default:
22050
22051 if (owner && sipmethod != SIP_BYE)
22052 ast_queue_hangup_with_cause(p->owner, hangup_sip2cause(resp));
22053 break;
22054 }
22055
22056 if (sipmethod == SIP_INVITE)
22057 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
22058 sip_alreadygone(p);
22059 if (!p->owner) {
22060 pvt_set_needdestroy(p, "transaction completed");
22061 }
22062 } else if ((resp >= 100) && (resp < 200)) {
22063 if (sipmethod == SIP_INVITE) {
22064 if (!req->ignore && sip_cancel_destroy(p))
22065 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
22066 if (find_sdp(req))
22067 process_sdp(p, req, SDP_T38_NONE);
22068 if (p->owner) {
22069
22070 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
22071 }
22072 }
22073 } else
22074 ast_log(LOG_NOTICE, "Don't know how to handle a %d %s response from %s\n", resp, rest, p->owner ? p->owner->name : ast_sockaddr_stringify(&p->sa));
22075 }
22076 } else {
22077
22078
22079 if (req->debug)
22080 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
22081
22082 if (sipmethod == SIP_INVITE && resp == 200) {
22083
22084
22085 char tag[128];
22086
22087 gettag(req, "To", tag, sizeof(tag));
22088 ast_string_field_set(p, theirtag, tag);
22089 }
22090
22091 switch(resp) {
22092 case 200:
22093 if (sipmethod == SIP_INVITE) {
22094 handle_response_invite(p, resp, rest, req, seqno);
22095 } else if (sipmethod == SIP_CANCEL) {
22096 ast_debug(1, "Got 200 OK on CANCEL\n");
22097
22098
22099 } else if (sipmethod == SIP_BYE) {
22100 pvt_set_needdestroy(p, "transaction completed");
22101 }
22102 break;
22103 case 401:
22104 case 407:
22105 if (sipmethod == SIP_INVITE)
22106 handle_response_invite(p, resp, rest, req, seqno);
22107 else if (sipmethod == SIP_BYE) {
22108 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, sipmethod, 0)) {
22109 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
22110 pvt_set_needdestroy(p, "failed to authenticate BYE");
22111 }
22112 }
22113 break;
22114 case 481:
22115 if (sipmethod == SIP_INVITE) {
22116
22117 handle_response_invite(p, resp, rest, req, seqno);
22118 } else if (sipmethod == SIP_BYE) {
22119 pvt_set_needdestroy(p, "received 481 response");
22120 } else if (sipdebug) {
22121 ast_debug(1, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
22122 }
22123 break;
22124 case 501:
22125 if (sipmethod == SIP_INVITE)
22126 handle_response_invite(p, resp, rest, req, seqno);
22127 break;
22128 default:
22129 if ((resp >= 100) && (resp < 200)) {
22130 if (sipmethod == SIP_INVITE) {
22131 if (!req->ignore && sip_cancel_destroy(p))
22132 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
22133 }
22134 }
22135 if ((resp >= 300) && (resp < 700)) {
22136 if ((resp != 487))
22137 ast_verb(3, "Incoming call: Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_sockaddr_stringify(&p->sa));
22138 switch(resp) {
22139 case 415:
22140 case 488:
22141 case 603:
22142 case 500:
22143 case 502:
22144 case 503:
22145 case 504:
22146
22147
22148 if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
22149 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
22150 break;
22151 }
22152 }
22153 break;
22154 }
22155 }
22156 }
22157
22158
22159
22160
22161
22162
22163
22164 static void *sip_park_thread(void *stuff)
22165 {
22166 struct ast_channel *transferee, *transferer;
22167 struct sip_pvt *transferer_pvt;
22168 struct sip_dual *d;
22169 int ext;
22170 int res;
22171
22172 d = stuff;
22173 transferee = d->chan1;
22174 transferer = d->chan2;
22175 transferer_pvt = transferer->tech_pvt;
22176
22177 ast_debug(4, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
22178
22179 res = ast_park_call_exten(transferee, transferer, d->park_exten, d->park_context, 0, &ext);
22180
22181 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
22182 if (res) {
22183 transmit_message_with_text(transferer_pvt, "Unable to park call.\n");
22184 } else {
22185
22186 sprintf(buf, "Call parked on extension '%d'", ext);
22187 transmit_message_with_text(transferer_pvt, buf);
22188 }
22189 #endif
22190
22191
22192
22193 ast_set_flag(&transferer_pvt->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
22194 if (!res) {
22195
22196 append_history(transferer_pvt, "SIPpark", "Parked call on %d", ext);
22197 transmit_notify_with_sipfrag(transferer_pvt, d->seqno, "200 OK", TRUE);
22198 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
22199 ast_debug(1, "SIP Call parked on extension '%d'\n", ext);
22200 } else {
22201 transmit_notify_with_sipfrag(transferer_pvt, d->seqno, "503 Service Unavailable", TRUE);
22202 append_history(transferer_pvt, "SIPpark", "Parking failed\n");
22203 ast_log(AST_LOG_NOTICE, "SIP Call parked failed for %s\n", transferee->name);
22204 ast_hangup(transferee);
22205 }
22206 ast_hangup(transferer);
22207 deinit_req(&d->req);
22208 ast_free(d->park_exten);
22209 ast_free(d->park_context);
22210 ast_free(d);
22211 return NULL;
22212 }
22213
22214
22215 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, uint32_t seqno, const char *park_exten, const char *park_context)
22216 {
22217 struct sip_dual *d;
22218 struct ast_channel *transferee, *transferer;
22219 pthread_t th;
22220
22221 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->linkedid, chan1->amaflags, "Parking/%s", chan1->name);
22222 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->linkedid, chan2->amaflags, "SIPPeer/%s", chan2->name);
22223 d = ast_calloc(1, sizeof(*d));
22224 if (!transferee || !transferer || !d) {
22225 if (transferee) {
22226 ast_hangup(transferee);
22227 }
22228 if (transferer) {
22229 ast_hangup(transferer);
22230 }
22231 ast_free(d);
22232 return -1;
22233 }
22234 d->park_exten = ast_strdup(park_exten);
22235 d->park_context = ast_strdup(park_context);
22236 if (!d->park_exten || !d->park_context) {
22237 ast_hangup(transferee);
22238 ast_hangup(transferer);
22239 ast_free(d->park_exten);
22240 ast_free(d->park_context);
22241 ast_free(d);
22242 return -1;
22243 }
22244
22245
22246 transferee->readformat = chan1->readformat;
22247 transferee->writeformat = chan1->writeformat;
22248
22249
22250 if (ast_channel_masquerade(transferee, chan1)) {
22251 ast_hangup(transferee);
22252 ast_hangup(transferer);
22253 ast_free(d->park_exten);
22254 ast_free(d->park_context);
22255 ast_free(d);
22256 return -1;
22257 }
22258
22259
22260 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
22261 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
22262 transferee->priority = chan1->priority;
22263
22264 ast_do_masquerade(transferee);
22265
22266
22267
22268
22269
22270 transferer->readformat = chan2->readformat;
22271 transferer->writeformat = chan2->writeformat;
22272 ast_string_field_set(transferer, parkinglot, chan2->parkinglot);
22273
22274
22275 if (ast_channel_masquerade(transferer, chan2)) {
22276 ast_hangup(transferee);
22277 ast_hangup(transferer);
22278 ast_free(d->park_exten);
22279 ast_free(d->park_context);
22280 ast_free(d);
22281 return -1;
22282 }
22283
22284
22285 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
22286 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
22287 transferer->priority = chan2->priority;
22288
22289 ast_do_masquerade(transferer);
22290
22291
22292 copy_request(&d->req, req);
22293 d->chan1 = transferee;
22294 d->chan2 = transferer;
22295 d->seqno = seqno;
22296 if (ast_pthread_create_detached_background(&th, NULL, sip_park_thread, d) < 0) {
22297
22298 ast_hangup(transferer);
22299 ast_hangup(transferee);
22300 deinit_req(&d->req);
22301 ast_free(d->park_exten);
22302 ast_free(d->park_context);
22303 ast_free(d);
22304
22305 return -1;
22306 }
22307 return 0;
22308 }
22309
22310
22311
22312
22313
22314 static void *sip_pickup_thread(void *stuff)
22315 {
22316 struct ast_channel *chan;
22317 chan = stuff;
22318
22319 if (ast_pickup_call(chan)) {
22320 chan->hangupcause = AST_CAUSE_CALL_REJECTED;
22321 } else {
22322 chan->hangupcause = AST_CAUSE_NORMAL_CLEARING;
22323 }
22324 ast_hangup(chan);
22325 ast_channel_unref(chan);
22326 chan = NULL;
22327 return NULL;
22328 }
22329
22330
22331
22332
22333 static int sip_pickup(struct ast_channel *chan)
22334 {
22335 pthread_t threadid;
22336
22337 ast_channel_ref(chan);
22338
22339 if (ast_pthread_create_detached_background(&threadid, NULL, sip_pickup_thread, chan)) {
22340 ast_debug(1, "Unable to start Group pickup thread on channel %s\n", chan->name);
22341 ast_channel_unref(chan);
22342 return -1;
22343 }
22344 ast_debug(1, "Started Group pickup thread on channel %s\n", chan->name);
22345 return 0;
22346 }
22347
22348
22349
22350
22351
22352 static void ast_quiet_chan(struct ast_channel *chan)
22353 {
22354 if (chan && chan->_state == AST_STATE_UP) {
22355 if (ast_test_flag(chan, AST_FLAG_MOH))
22356 ast_moh_stop(chan);
22357 else if (chan->generatordata)
22358 ast_deactivate_generator(chan);
22359 }
22360 }
22361
22362
22363
22364 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
22365 {
22366 int res = 0;
22367 struct ast_channel *peera = NULL,
22368 *peerb = NULL,
22369 *peerc = NULL,
22370 *peerd = NULL;
22371
22372
22373
22374
22375 ast_debug(4, "Sip transfer:--------------------\n");
22376 if (transferer->chan1)
22377 ast_debug(4, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
22378 else
22379 ast_debug(4, "-- No transferer first channel - odd??? \n");
22380 if (target->chan1)
22381 ast_debug(4, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
22382 else
22383 ast_debug(4, "-- No target first channel ---\n");
22384 if (transferer->chan2)
22385 ast_debug(4, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
22386 else
22387 ast_debug(4, "-- No bridged call to transferee\n");
22388 if (target->chan2)
22389 ast_debug(4, "-- Bridged call to transfer target: %s State %s\n", target->chan2 ? target->chan2->name : "<none>", target->chan2 ? ast_state2str(target->chan2->_state) : "(none)");
22390 else
22391 ast_debug(4, "-- No target second channel ---\n");
22392 ast_debug(4, "-- END Sip transfer:--------------------\n");
22393 if (transferer->chan2) {
22394 peera = transferer->chan1;
22395 peerb = target->chan1;
22396 peerc = transferer->chan2;
22397 peerd = target->chan2;
22398 ast_debug(3, "SIP transfer: Four channels to handle\n");
22399 } else if (target->chan2) {
22400 peera = target->chan1;
22401 peerb = transferer->chan1;
22402 peerc = target->chan2;
22403 peerd = transferer->chan2;
22404 ast_debug(3, "SIP transfer: Three channels to handle\n");
22405 }
22406
22407 if (peera && peerb && peerc && (peerb != peerc)) {
22408 ast_quiet_chan(peera);
22409
22410
22411
22412 ast_quiet_chan(peerc);
22413 if (peerd)
22414 ast_quiet_chan(peerd);
22415
22416 ast_debug(4, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
22417 if (ast_channel_masquerade(peerb, peerc)) {
22418 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
22419 res = -1;
22420 } else
22421 ast_debug(4, "SIP transfer: Succeeded to masquerade channels.\n");
22422 return res;
22423 } else {
22424 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
22425 if (transferer->chan1)
22426 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
22427 if (target->chan1)
22428 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
22429 return -1;
22430 }
22431 return 0;
22432 }
22433
22434
22435
22436
22437
22438
22439 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
22440 {
22441 const char *thetag;
22442
22443 if (!tagbuf)
22444 return NULL;
22445 tagbuf[0] = '\0';
22446 thetag = get_header(req, header);
22447 thetag = strcasestr(thetag, ";tag=");
22448 if (thetag) {
22449 thetag += 5;
22450 ast_copy_string(tagbuf, thetag, tagbufsize);
22451 return strsep(&tagbuf, ";");
22452 }
22453 return NULL;
22454 }
22455
22456 static int handle_cc_notify(struct sip_pvt *pvt, struct sip_request *req)
22457 {
22458 struct sip_monitor_instance *monitor_instance = ao2_callback(sip_monitor_instances, 0,
22459 find_sip_monitor_instance_by_subscription_pvt, pvt);
22460 const char *status = get_body(req, "cc-state", ':');
22461 struct cc_epa_entry *cc_entry;
22462 char *uri;
22463
22464 if (!monitor_instance) {
22465 transmit_response(pvt, "400 Bad Request", req);
22466 return -1;
22467 }
22468
22469 if (ast_strlen_zero(status)) {
22470 ao2_ref(monitor_instance, -1);
22471 transmit_response(pvt, "400 Bad Request", req);
22472 return -1;
22473 }
22474
22475 if (!strcmp(status, "queued")) {
22476
22477
22478
22479
22480 ast_cc_monitor_request_acked(monitor_instance->core_id, "SIP endpoint %s accepted request", monitor_instance->device_name);
22481 transmit_response(pvt, "200 OK", req);
22482 ao2_ref(monitor_instance, -1);
22483 return 0;
22484 }
22485
22486
22487 uri = get_body(req, "cc-URI", ':');
22488 if (ast_strlen_zero(uri)) {
22489 uri = get_in_brackets((char *)get_header(req, "From"));
22490 }
22491
22492 ast_string_field_set(monitor_instance, notify_uri, uri);
22493 if (monitor_instance->suspension_entry) {
22494 cc_entry = monitor_instance->suspension_entry->instance_data;
22495 if (cc_entry->current_state == CC_CLOSED) {
22496
22497
22498
22499
22500
22501 construct_pidf_body(CC_CLOSED, monitor_instance->suspension_entry->body,
22502 sizeof(monitor_instance->suspension_entry->body), monitor_instance->peername);
22503 transmit_publish(monitor_instance->suspension_entry, SIP_PUBLISH_INITIAL, monitor_instance->notify_uri);
22504 } else {
22505 ast_cc_monitor_callee_available(monitor_instance->core_id, "SIP monitored callee has become available");
22506 }
22507 } else {
22508 ast_cc_monitor_callee_available(monitor_instance->core_id, "SIP monitored callee has become available");
22509 }
22510 ao2_ref(monitor_instance, -1);
22511 transmit_response(pvt, "200 OK", req);
22512
22513 return 0;
22514 }
22515
22516
22517 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, const char *e)
22518 {
22519
22520
22521 int res = 0;
22522 const char *event = get_header(req, "Event");
22523 char *sep;
22524
22525 if( (sep = strchr(event, ';')) ) {
22526 *sep++ = '\0';
22527 }
22528
22529 if (sipdebug)
22530 ast_debug(2, "Got NOTIFY Event: %s\n", event);
22531
22532 if (!strcmp(event, "refer")) {
22533
22534
22535
22536
22537
22538 char buf[1024];
22539 char *cmd, *code;
22540 int respcode;
22541 int success = TRUE;
22542
22543
22544
22545
22546
22547
22548
22549
22550 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
22551
22552 transmit_response(p, "400 Bad request", req);
22553 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22554 return -1;
22555 }
22556
22557
22558 if (get_msg_text(buf, sizeof(buf), req)) {
22559 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
22560 transmit_response(p, "400 Bad request", req);
22561 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22562 return -1;
22563 }
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585 ast_debug(3, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
22586 cmd = ast_skip_blanks(buf);
22587 code = cmd;
22588
22589 while(*code && (*code > 32)) {
22590 code++;
22591 }
22592 *code++ = '\0';
22593 code = ast_skip_blanks(code);
22594 sep = code;
22595 sep++;
22596 while(*sep && (*sep > 32)) {
22597 sep++;
22598 }
22599 *sep++ = '\0';
22600 respcode = atoi(code);
22601 switch (respcode) {
22602 case 200:
22603
22604 break;
22605 case 301:
22606 case 302:
22607
22608 success = FALSE;
22609 break;
22610 case 503:
22611 case 603:
22612
22613 success = FALSE;
22614 break;
22615 case 0:
22616
22617 ast_log(LOG_NOTICE, "Error parsing sipfrag in NOTIFY in response to REFER.\n");
22618 success = FALSE;
22619 break;
22620 default:
22621 if (respcode < 200) {
22622
22623 success = -1;
22624 } else {
22625 ast_log(LOG_NOTICE, "Got unknown code '%d' in NOTIFY in response to REFER.\n", respcode);
22626 success = FALSE;
22627 }
22628 break;
22629 }
22630 if (success == FALSE) {
22631 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
22632 }
22633
22634 if (p->owner && success != -1) {
22635 enum ast_control_transfer message = success ? AST_TRANSFER_SUCCESS : AST_TRANSFER_FAILED;
22636 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
22637 }
22638
22639 transmit_response(p, "200 OK", req);
22640 } else if (!strcmp(event, "message-summary")) {
22641 const char *mailbox = NULL;
22642 char *c = ast_strdupa(get_body(req, "Voice-Message", ':'));
22643
22644 if (!p->mwi) {
22645 struct sip_peer *peer = find_peer(NULL, &p->recv, TRUE, FINDPEERS, FALSE, p->socket.type);
22646
22647 if (peer) {
22648 mailbox = ast_strdupa(peer->unsolicited_mailbox);
22649 unref_peer(peer, "removing unsolicited mwi ref");
22650 }
22651 } else {
22652 mailbox = p->mwi->mailbox;
22653 }
22654
22655 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(c)) {
22656 char *old = strsep(&c, " ");
22657 char *new = strsep(&old, "/");
22658 struct ast_event *event;
22659
22660 if ((event = ast_event_new(AST_EVENT_MWI,
22661 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
22662 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, "SIP_Remote",
22663 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, atoi(new),
22664 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, atoi(old),
22665 AST_EVENT_IE_END))) {
22666 ast_event_queue_and_cache(event);
22667 }
22668 transmit_response(p, "200 OK", req);
22669 } else {
22670 transmit_response(p, "489 Bad event", req);
22671 res = -1;
22672 }
22673 } else if (!strcmp(event, "keep-alive")) {
22674
22675
22676 transmit_response(p, "200 OK", req);
22677 } else if (!strcmp(event, "call-completion")) {
22678 res = handle_cc_notify(p, req);
22679 } else {
22680
22681 transmit_response(p, "489 Bad event", req);
22682 res = -1;
22683 }
22684
22685 if (!p->lastinvite)
22686 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22687
22688 return res;
22689 }
22690
22691
22692
22693
22694 static int handle_request_options(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e)
22695 {
22696 const char *msg;
22697 enum sip_get_dest_result gotdest;
22698 int res;
22699
22700 if (p->lastinvite) {
22701
22702 transmit_response_with_allow(p, "200 OK", req, 0);
22703 return 0;
22704 }
22705
22706 if (sip_cfg.auth_options_requests) {
22707
22708 copy_request(&p->initreq, req);
22709 set_pvt_allowed_methods(p, req);
22710 res = check_user(p, req, SIP_OPTIONS, e, XMIT_UNRELIABLE, addr);
22711 if (res == AUTH_CHALLENGE_SENT) {
22712 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22713 return 0;
22714 }
22715 if (res < 0) {
22716 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
22717 transmit_response(p, "403 Forbidden", req);
22718 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22719 return 0;
22720 }
22721 }
22722
22723
22724 gotdest = get_destination(p, req, NULL);
22725 build_contact(p);
22726
22727 if (ast_strlen_zero(p->context))
22728 ast_string_field_set(p, context, sip_cfg.default_context);
22729
22730 if (ast_shutting_down()) {
22731 msg = "503 Unavailable";
22732 } else {
22733 msg = "404 Not Found";
22734 switch (gotdest) {
22735 case SIP_GET_DEST_INVALID_URI:
22736 msg = "416 Unsupported URI scheme";
22737 break;
22738 case SIP_GET_DEST_EXTEN_MATCHMORE:
22739 case SIP_GET_DEST_REFUSED:
22740 case SIP_GET_DEST_EXTEN_NOT_FOUND:
22741
22742 break;
22743 case SIP_GET_DEST_EXTEN_FOUND:
22744 msg = "200 OK";
22745 break;
22746 }
22747 }
22748 transmit_response_with_allow(p, msg, req, 0);
22749
22750
22751
22752 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22753
22754 return 0;
22755 }
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, struct ast_sockaddr *addr, int *nounlock)
22770 {
22771 int earlyreplace = 0;
22772 int oneleggedreplace = 0;
22773 struct ast_channel *c = p->owner;
22774 struct ast_channel *replacecall = p->refer->refer_call->owner;
22775 struct ast_channel *targetcall;
22776
22777
22778 if (replacecall->_state == AST_STATE_RING)
22779 earlyreplace = 1;
22780
22781
22782 if (!(targetcall = ast_bridged_channel(replacecall))) {
22783
22784 if (!earlyreplace) {
22785 ast_debug(2, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
22786 oneleggedreplace = 1;
22787 }
22788 }
22789 if (targetcall && targetcall->_state == AST_STATE_RINGING)
22790 ast_debug(4, "SIP transfer: Target channel is in ringing state\n");
22791
22792 if (targetcall)
22793 ast_debug(4, "SIP transfer: Invite Replace incoming channel should bridge to channel %s while hanging up channel %s\n", targetcall->name, replacecall->name);
22794 else
22795 ast_debug(4, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
22796
22797 if (req->ignore) {
22798 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
22799
22800
22801
22802 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE, FALSE);
22803
22804 if (c) {
22805 *nounlock = 1;
22806 ast_channel_unlock(c);
22807 }
22808 ast_channel_unlock(replacecall);
22809 sip_pvt_unlock(p->refer->refer_call);
22810 return 1;
22811 }
22812 if (!c) {
22813
22814 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
22815 transmit_response_reliable(p, "503 Service Unavailable", req);
22816 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
22817 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22818 ast_channel_unlock(replacecall);
22819 sip_pvt_unlock(p->refer->refer_call);
22820 return 1;
22821 }
22822 append_history(p, "Xfer", "INVITE/Replace received");
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834 transmit_response(p, "100 Trying", req);
22835 ast_setstate(c, AST_STATE_RING);
22836
22837
22838
22839
22840
22841 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE, FALSE);
22842
22843
22844 if (earlyreplace || oneleggedreplace) {
22845
22846 ast_cel_report_event(replacecall, AST_CEL_PICKUP, NULL, NULL, c);
22847 ast_setstate(c, AST_STATE_UP);
22848 ast_cel_report_event(c, AST_CEL_ANSWER, NULL, NULL, NULL);
22849 } else {
22850 ast_setstate(c, AST_STATE_UP);
22851 }
22852
22853
22854 ast_quiet_chan(replacecall);
22855 ast_quiet_chan(targetcall);
22856 ast_debug(4, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
22857
22858
22859 if (! earlyreplace && ! oneleggedreplace )
22860 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
22861
22862
22863 if(ast_channel_masquerade(replacecall, c))
22864 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
22865 else
22866 ast_debug(4, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
22867
22868
22869
22870
22871
22872 ast_channel_ref(c);
22873 ast_channel_unlock(replacecall);
22874 ast_channel_unlock(c);
22875 sip_pvt_unlock(p->refer->refer_call);
22876 sip_pvt_unlock(p);
22877 if (ast_do_masquerade(replacecall)) {
22878 ast_log(LOG_WARNING, "Failed to perform masquerade with INVITE replaces\n");
22879 }
22880 if (earlyreplace || oneleggedreplace ) {
22881 ast_channel_lock(c);
22882 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
22883 ast_channel_unlock(c);
22884 }
22885
22886
22887 c->tech_pvt = dialog_unref(c->tech_pvt, "unref dialog c->tech_pvt");
22888
22889
22890 ast_hangup(c);
22891
22892 *nounlock = 1;
22893
22894 sip_pvt_lock(p);
22895 ast_channel_unref(c);
22896 return 0;
22897 }
22898
22899
22900 static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context)
22901 {
22902 struct ast_str *str = ast_str_alloca(AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2);
22903 struct ast_app *pickup = pbx_findapp("Pickup");
22904
22905 if (!pickup) {
22906 ast_log(LOG_ERROR, "Unable to perform pickup: Application 'Pickup' not loaded (app_directed_pickup.so).\n");
22907 return -1;
22908 }
22909
22910 ast_str_set(&str, 0, "%s@%s", extension, sip_cfg.notifycid == IGNORE_CONTEXT ? "PICKUPMARK" : context);
22911
22912 ast_debug(2, "About to call Pickup(%s)\n", ast_str_buffer(str));
22913
22914
22915
22916
22917 pbx_exec(channel, pickup, ast_str_buffer(str));
22918
22919 return 0;
22920 }
22921
22922
22923 static int sip_t38_abort(const void *data)
22924 {
22925 struct sip_pvt *p = (struct sip_pvt *) data;
22926
22927 sip_pvt_lock(p);
22928
22929
22930
22931
22932
22933 if (p->t38id != -1) {
22934 change_t38_state(p, T38_DISABLED);
22935 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
22936 p->t38id = -1;
22937 dialog_unref(p, "unref the dialog ptr from sip_t38_abort, because it held a dialog ptr");
22938 }
22939 sip_pvt_unlock(p);
22940 return 0;
22941 }
22942
22943
22944
22945
22946
22947
22948
22949
22950
22951
22952
22953
22954 static int handle_request_update(struct sip_pvt *p, struct sip_request *req)
22955 {
22956 if (ast_strlen_zero(get_header(req, "X-Asterisk-rpid-update"))) {
22957 transmit_response(p, "501 Method Not Implemented", req);
22958 return 0;
22959 }
22960 if (!p->owner) {
22961 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
22962 return 0;
22963 }
22964 if (get_rpid(p, req)) {
22965 struct ast_party_connected_line connected;
22966 struct ast_set_party_connected_line update_connected;
22967
22968 ast_party_connected_line_init(&connected);
22969 memset(&update_connected, 0, sizeof(update_connected));
22970
22971 update_connected.id.number = 1;
22972 connected.id.number.valid = 1;
22973 connected.id.number.str = (char *) p->cid_num;
22974 connected.id.number.presentation = p->callingpres;
22975
22976 update_connected.id.name = 1;
22977 connected.id.name.valid = 1;
22978 connected.id.name.str = (char *) p->cid_name;
22979 connected.id.name.presentation = p->callingpres;
22980
22981 connected.id.tag = (char *) p->cid_tag;
22982 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
22983 ast_channel_queue_connected_line_update(p->owner, &connected, &update_connected);
22984 }
22985 transmit_response(p, "200 OK", req);
22986 return 0;
22987 }
22988
22989
22990
22991
22992
22993
22994
22995 static int handle_request_invite_st(struct sip_pvt *p, struct sip_request *req,
22996 const char *required, int reinvite)
22997 {
22998 const char *p_uac_se_hdr;
22999 const char *p_uac_min_se;
23000 int uac_max_se = -1;
23001 int uac_min_se = -1;
23002 int st_active = FALSE;
23003 int st_interval = 0;
23004 enum st_refresher tmp_st_ref = SESSION_TIMER_REFRESHER_AUTO;
23005 int dlg_min_se = -1;
23006 int dlg_max_se = global_max_se;
23007 int rtn;
23008
23009
23010 if ((p->sipoptions & SIP_OPT_TIMER)) {
23011 enum st_refresher_param st_ref_param = SESSION_TIMER_REFRESHER_PARAM_UNKNOWN;
23012
23013
23014
23015 ast_debug(2, "Incoming INVITE with 'timer' option supported\n");
23016
23017
23018 if (!p->stimer) {
23019 sip_st_alloc(p);
23020 }
23021
23022
23023 p_uac_se_hdr = get_header(req, "Session-Expires");
23024 if (!ast_strlen_zero(p_uac_se_hdr)) {
23025 ast_debug(2, "INVITE also has \"Session-Expires\" header.\n");
23026 rtn = parse_session_expires(p_uac_se_hdr, &uac_max_se, &st_ref_param);
23027 tmp_st_ref = (st_ref_param == SESSION_TIMER_REFRESHER_PARAM_UAC) ? SESSION_TIMER_REFRESHER_THEM : SESSION_TIMER_REFRESHER_US;
23028 if (rtn != 0) {
23029 transmit_response_reliable(p, "400 Session-Expires Invalid Syntax", req);
23030 return -1;
23031 }
23032 }
23033
23034
23035 p_uac_min_se = get_header(req, "Min-SE");
23036 if (!ast_strlen_zero(p_uac_min_se)) {
23037 ast_debug(2, "INVITE also has \"Min-SE\" header.\n");
23038 rtn = parse_minse(p_uac_min_se, &uac_min_se);
23039 if (rtn != 0) {
23040 transmit_response_reliable(p, "400 Min-SE Invalid Syntax", req);
23041 return -1;
23042 }
23043 }
23044
23045 dlg_min_se = st_get_se(p, FALSE);
23046 switch (st_get_mode(p, 1)) {
23047 case SESSION_TIMER_MODE_ACCEPT:
23048 case SESSION_TIMER_MODE_ORIGINATE:
23049 if (uac_max_se > 0 && uac_max_se < dlg_min_se) {
23050 transmit_response_with_minse(p, "422 Session Interval Too Small", req, dlg_min_se);
23051 return -1;
23052 }
23053
23054 p->stimer->st_active_peer_ua = TRUE;
23055 st_active = TRUE;
23056 if (st_ref_param == SESSION_TIMER_REFRESHER_PARAM_UNKNOWN) {
23057 tmp_st_ref = st_get_refresher(p);
23058 }
23059
23060 dlg_max_se = st_get_se(p, TRUE);
23061 if (uac_max_se > 0) {
23062 if (dlg_max_se >= uac_min_se) {
23063 st_interval = (uac_max_se < dlg_max_se) ? uac_max_se : dlg_max_se;
23064 } else {
23065 st_interval = uac_max_se;
23066 }
23067 } else if (uac_min_se > 0) {
23068 st_interval = MAX(dlg_max_se, uac_min_se);
23069 } else {
23070 st_interval = dlg_max_se;
23071 }
23072 break;
23073
23074 case SESSION_TIMER_MODE_REFUSE:
23075 if (p->reqsipoptions & SIP_OPT_TIMER) {
23076 transmit_response_with_unsupported(p, "420 Option Disabled", req, required);
23077 ast_log(LOG_WARNING, "Received SIP INVITE with supported but disabled option: %s\n", required);
23078 return -1;
23079 }
23080 break;
23081
23082 default:
23083 ast_log(LOG_ERROR, "Internal Error %u at %s:%d\n", st_get_mode(p, 1), __FILE__, __LINE__);
23084 break;
23085 }
23086 } else {
23087
23088
23089
23090 switch (st_get_mode(p, 1)) {
23091 case SESSION_TIMER_MODE_ORIGINATE:
23092 st_active = TRUE;
23093 st_interval = st_get_se(p, TRUE);
23094 tmp_st_ref = SESSION_TIMER_REFRESHER_US;
23095 p->stimer->st_active_peer_ua = (p->sipoptions & SIP_OPT_TIMER) ? TRUE : FALSE;
23096 break;
23097
23098 default:
23099 break;
23100 }
23101 }
23102
23103 if (reinvite == 0) {
23104
23105 if (st_active == TRUE) {
23106 p->stimer->st_active = TRUE;
23107 p->stimer->st_interval = st_interval;
23108 p->stimer->st_ref = tmp_st_ref;
23109 }
23110 } else {
23111 if (p->stimer->st_active == TRUE) {
23112
23113
23114
23115 ast_debug (2, "Restarting session-timers on a refresh - %s\n", p->callid);
23116
23117
23118 if (st_interval > 0) {
23119 p->stimer->st_interval = st_interval;
23120 p->stimer->st_ref = tmp_st_ref;
23121 }
23122 }
23123 }
23124
23125 return 0;
23126 }
23127
23128
23129
23130
23131
23132
23133
23134
23135 static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock)
23136 {
23137 int res = 1;
23138 int gotdest;
23139 const char *p_replaces;
23140 char *replace_id = NULL;
23141 int refer_locked = 0;
23142 const char *required;
23143 unsigned int required_profile = 0;
23144 struct ast_channel *c = NULL;
23145 struct sip_peer *authpeer = NULL;
23146 int reinvite = 0;
23147 struct ast_party_redirecting redirecting;
23148 struct ast_set_party_redirecting update_redirecting;
23149 int supported_start = 0;
23150 int require_start = 0;
23151 char unsupported[256] = { 0, };
23152 struct {
23153 char exten[AST_MAX_EXTENSION];
23154 char context[AST_MAX_CONTEXT];
23155 } pickup = {
23156 .exten = "",
23157 };
23158
23159
23160 if (!p->sipoptions) {
23161 const char *supported = NULL;
23162 do {
23163 supported = __get_header(req, "Supported", &supported_start);
23164 if (!ast_strlen_zero(supported)) {
23165 p->sipoptions |= parse_sip_options(supported, NULL, 0);
23166 }
23167 } while (!ast_strlen_zero(supported));
23168 }
23169
23170
23171 do {
23172 required = __get_header(req, "Require", &require_start);
23173 if (!ast_strlen_zero(required)) {
23174 required_profile |= parse_sip_options(required, unsupported, ARRAY_LEN(unsupported));
23175 }
23176 } while (!ast_strlen_zero(required));
23177
23178
23179
23180 if (!ast_strlen_zero(unsupported)) {
23181 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, unsupported);
23182 ast_log(LOG_WARNING, "Received SIP INVITE with unsupported required extension: required:%s unsupported:%s\n", required, unsupported);
23183 p->invitestate = INV_COMPLETED;
23184 if (!p->lastinvite) {
23185 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23186 }
23187 res = -1;
23188 goto request_invite_cleanup;
23189 }
23190
23191
23192
23193
23194 p->sipoptions |= required_profile;
23195 p->reqsipoptions = required_profile;
23196
23197
23198 if (ast_test_flag(&p->flags[0], SIP_OUTGOING) && p->owner && (p->invitestate != INV_TERMINATED && p->invitestate != INV_CONFIRMED) && p->owner->_state != AST_STATE_UP) {
23199
23200
23201
23202
23203
23204 int different;
23205 const char *initial_rlPart2 = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
23206 const char *this_rlPart2 = REQ_OFFSET_TO_STR(req, rlPart2);
23207 if (sip_cfg.pedanticsipchecking)
23208 different = sip_uri_cmp(initial_rlPart2, this_rlPart2);
23209 else
23210 different = strcmp(initial_rlPart2, this_rlPart2);
23211 if (!different) {
23212 transmit_response(p, "482 Loop Detected", req);
23213 p->invitestate = INV_COMPLETED;
23214 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23215 res = 0;
23216 goto request_invite_cleanup;
23217 } else {
23218
23219
23220
23221
23222
23223
23224
23225 char *uri = ast_strdupa(this_rlPart2);
23226 char *at = strchr(uri, '@');
23227 char *peerorhost;
23228 ast_debug(2, "Potential spiral detected. Original RURI was %s, new RURI is %s\n", initial_rlPart2, this_rlPart2);
23229 transmit_response(p, "100 Trying", req);
23230 if (at) {
23231 *at = '\0';
23232 }
23233
23234 if ((peerorhost = strchr(uri, ':'))) {
23235 *peerorhost++ = '\0';
23236 }
23237 ast_string_field_set(p, theirtag, NULL);
23238
23239
23240 ast_string_field_set(p->owner, call_forward, peerorhost);
23241 ast_queue_control(p->owner, AST_CONTROL_BUSY);
23242 res = 0;
23243 goto request_invite_cleanup;
23244 }
23245 }
23246
23247 if (!req->ignore && p->pendinginvite) {
23248 if (!ast_test_flag(&p->flags[0], SIP_OUTGOING) && (p->invitestate == INV_COMPLETED || p->invitestate == INV_TERMINATED)) {
23249
23250
23251
23252
23253
23254
23255
23256
23257
23258
23259 __sip_ack(p, p->pendinginvite, 1, 0);
23260 } else {
23261
23262 p->glareinvite = seqno;
23263 if (p->rtp && find_sdp(req)) {
23264 struct ast_sockaddr addr;
23265 if (get_ip_and_port_from_sdp(req, SDP_AUDIO, &addr)) {
23266 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Audio may not work properly on this call.\n");
23267 } else {
23268 ast_rtp_instance_set_alt_remote_address(p->rtp, &addr);
23269 }
23270 if (p->vrtp) {
23271 if (get_ip_and_port_from_sdp(req, SDP_VIDEO, &addr)) {
23272 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Video may not work properly on this call.\n");
23273 } else {
23274 ast_rtp_instance_set_alt_remote_address(p->vrtp, &addr);
23275 }
23276 }
23277 }
23278 transmit_response_reliable(p, "491 Request Pending", req);
23279 check_via(p, req);
23280 ast_debug(1, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
23281
23282 res = 0;
23283 goto request_invite_cleanup;
23284 }
23285 }
23286
23287 p_replaces = get_header(req, "Replaces");
23288 if (!ast_strlen_zero(p_replaces)) {
23289
23290 char *ptr;
23291 char *fromtag = NULL;
23292 char *totag = NULL;
23293 char *start, *to;
23294 int error = 0;
23295
23296 if (p->owner) {
23297 ast_debug(3, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
23298 transmit_response_reliable(p, "400 Bad request", req);
23299 check_via(p, req);
23300 copy_request(&p->initreq, req);
23301
23302 res = -1;
23303 goto request_invite_cleanup;
23304 }
23305
23306 if (sipdebug)
23307 ast_debug(3, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
23308
23309 replace_id = ast_strdupa(p_replaces);
23310 ast_uri_decode(replace_id);
23311
23312 if (!p->refer && !sip_refer_allocate(p)) {
23313 transmit_response_reliable(p, "500 Server Internal Error", req);
23314 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
23315 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23316 p->invitestate = INV_COMPLETED;
23317 check_via(p, req);
23318 copy_request(&p->initreq, req);
23319 res = -1;
23320 goto request_invite_cleanup;
23321 }
23322
23323
23324
23325
23326
23327
23328
23329
23330
23331
23332 replace_id = ast_skip_blanks(replace_id);
23333
23334 start = replace_id;
23335 while ( (ptr = strsep(&start, ";")) ) {
23336 ptr = ast_skip_blanks(ptr);
23337 if ( (to = strcasestr(ptr, "to-tag=") ) )
23338 totag = to + 7;
23339 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
23340 fromtag = to + 9;
23341 fromtag = strsep(&fromtag, "&");
23342 }
23343 }
23344
23345 if (sipdebug)
23346 ast_debug(4, "Invite/replaces: Will use Replace-Call-ID : %s Fromtag: %s Totag: %s\n",
23347 replace_id,
23348 fromtag ? fromtag : "<no from tag>",
23349 totag ? totag : "<no to tag>");
23350
23351
23352
23353
23354
23355 if (strncmp(replace_id, "pickup-", 7) == 0) {
23356 struct sip_pvt *subscription = NULL;
23357 replace_id += 7;
23358
23359 if ((subscription = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
23360 ast_log(LOG_NOTICE, "Unable to find subscription with call-id: %s\n", replace_id);
23361 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
23362 error = 1;
23363 } else {
23364 ast_log(LOG_NOTICE, "Trying to pick up %s@%s\n", subscription->exten, subscription->context);
23365 ast_copy_string(pickup.exten, subscription->exten, sizeof(pickup.exten));
23366 ast_copy_string(pickup.context, subscription->context, sizeof(pickup.context));
23367 sip_pvt_unlock(subscription);
23368 if (subscription->owner) {
23369 ast_channel_unlock(subscription->owner);
23370 }
23371 subscription = dialog_unref(subscription, "unref dialog subscription");
23372 }
23373 }
23374
23375
23376 if (!error && ast_strlen_zero(pickup.exten) && (p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
23377 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
23378 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
23379 error = 1;
23380 } else {
23381 refer_locked = 1;
23382 }
23383
23384
23385
23386
23387
23388 if (p->refer->refer_call == p) {
23389 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
23390 transmit_response_reliable(p, "400 Bad request", req);
23391 error = 1;
23392 }
23393
23394 if (!error && ast_strlen_zero(pickup.exten) && !p->refer->refer_call->owner) {
23395
23396 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
23397
23398 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replace)", req);
23399 error = 1;
23400 }
23401
23402 if (!error && ast_strlen_zero(pickup.exten) && p->refer->refer_call->owner->_state != AST_STATE_RINGING && p->refer->refer_call->owner->_state != AST_STATE_RING && p->refer->refer_call->owner->_state != AST_STATE_UP) {
23403 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
23404 transmit_response_reliable(p, "603 Declined (Replaces)", req);
23405 error = 1;
23406 }
23407
23408 if (error) {
23409 append_history(p, "Xfer", "INVITE/Replace Failed.");
23410 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23411 sip_pvt_unlock(p);
23412 if (p->refer->refer_call) {
23413 sip_pvt_unlock(p->refer->refer_call);
23414 if (p->refer->refer_call->owner) {
23415 ast_channel_unlock(p->refer->refer_call->owner);
23416 }
23417 p->refer->refer_call = dialog_unref(p->refer->refer_call, "unref dialog p->refer->refer_call");
23418 }
23419 refer_locked = 0;
23420 p->invitestate = INV_COMPLETED;
23421 check_via(p, req);
23422 copy_request(&p->initreq, req);
23423 res = -1;
23424 goto request_invite_cleanup;
23425 }
23426 }
23427
23428
23429
23430
23431 if (!req->ignore) {
23432 int newcall = (p->initreq.headers ? TRUE : FALSE);
23433
23434 if (sip_cancel_destroy(p))
23435 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
23436
23437 p->pendinginvite = seqno;
23438 check_via(p, req);
23439
23440 copy_request(&p->initreq, req);
23441 if (sipdebug)
23442 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
23443 if (!p->owner) {
23444 if (debug)
23445 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
23446 if (newcall)
23447 append_history(p, "Invite", "New call: %s", p->callid);
23448 parse_ok_contact(p, req);
23449 } else {
23450 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
23451 if (get_rpid(p, req)) {
23452 struct ast_party_connected_line connected;
23453 struct ast_set_party_connected_line update_connected;
23454
23455 ast_party_connected_line_init(&connected);
23456 memset(&update_connected, 0, sizeof(update_connected));
23457
23458 update_connected.id.number = 1;
23459 connected.id.number.valid = 1;
23460 connected.id.number.str = (char *) p->cid_num;
23461 connected.id.number.presentation = p->callingpres;
23462
23463 update_connected.id.name = 1;
23464 connected.id.name.valid = 1;
23465 connected.id.name.str = (char *) p->cid_name;
23466 connected.id.name.presentation = p->callingpres;
23467
23468 connected.id.tag = (char *) p->cid_tag;
23469 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
23470 ast_channel_queue_connected_line_update(p->owner, &connected,
23471 &update_connected);
23472 }
23473
23474 if (find_sdp(req)) {
23475 if (process_sdp(p, req, SDP_T38_INITIATE)) {
23476 if (!ast_strlen_zero(get_header(req, "Content-Encoding"))) {
23477
23478
23479
23480 transmit_response_reliable(p, "415 Unsupported Media type", req);
23481 } else {
23482 transmit_response_reliable(p, "488 Not acceptable here", req);
23483 }
23484 if (!p->lastinvite)
23485 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23486 res = -1;
23487 goto request_invite_cleanup;
23488 }
23489 ast_queue_control(p->owner, AST_CONTROL_SRCUPDATE);
23490 } else {
23491 p->jointcapability = p->capability;
23492 ast_debug(1, "Hm.... No sdp for the moment\n");
23493
23494
23495
23496 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
23497 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
23498
23499 ast_queue_frame(p->owner, &ast_null_frame);
23500 change_hold_state(p, req, FALSE, 0);
23501 }
23502 }
23503 if (p->do_history)
23504 append_history(p, "ReInv", "Re-invite received");
23505 }
23506 } else if (debug)
23507 ast_verbose("Ignoring this INVITE request\n");
23508
23509 if (!p->lastinvite && !req->ignore && !p->owner) {
23510
23511
23512 int cc_recall_core_id = -1;
23513 set_pvt_allowed_methods(p, req);
23514 res = check_user_full(p, req, SIP_INVITE, e, XMIT_RELIABLE, addr, &authpeer);
23515 if (res == AUTH_CHALLENGE_SENT) {
23516 p->invitestate = INV_COMPLETED;
23517 res = 0;
23518 goto request_invite_cleanup;
23519 }
23520 if (res < 0) {
23521 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
23522 transmit_response_reliable(p, "403 Forbidden", req);
23523 p->invitestate = INV_COMPLETED;
23524 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23525 res = 0;
23526 goto request_invite_cleanup;
23527 }
23528
23529
23530 if (p->relatedpeer) {
23531 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting the relatedpeer field in the dialog, before it is set to something else.");
23532 }
23533 if (authpeer) {
23534 p->relatedpeer = ref_peer(authpeer, "setting dialog's relatedpeer pointer");
23535 }
23536
23537 req->authenticated = 1;
23538
23539
23540 if (find_sdp(req)) {
23541 if (process_sdp(p, req, SDP_T38_INITIATE)) {
23542
23543
23544
23545 if (!ast_strlen_zero(get_header(req, "Content-Encoding"))) {
23546 transmit_response_reliable(p, "415 Unsupported Media type", req);
23547 } else {
23548
23549 transmit_response_reliable(p, "488 Not acceptable here", req);
23550 }
23551 p->invitestate = INV_COMPLETED;
23552 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23553 ast_debug(1, "No compatible codecs for this SIP call.\n");
23554 res = -1;
23555 goto request_invite_cleanup;
23556 }
23557 } else {
23558 p->jointcapability = p->capability;
23559 ast_debug(2, "No SDP in Invite, third party call control\n");
23560 }
23561
23562
23563 if (ast_strlen_zero(p->context))
23564 ast_string_field_set(p, context, sip_cfg.default_context);
23565
23566
23567
23568 ast_debug(1, "Checking SIP call limits for device %s\n", p->username);
23569 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
23570 if (res < 0) {
23571 ast_log(LOG_NOTICE, "Failed to place call for device %s, too many calls\n", p->username);
23572 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
23573 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23574 p->invitestate = INV_COMPLETED;
23575 }
23576 res = 0;
23577 goto request_invite_cleanup;
23578 }
23579 gotdest = get_destination(p, NULL, &cc_recall_core_id);
23580 extract_uri(p, req);
23581 build_contact(p);
23582
23583 if (p->rtp) {
23584 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
23585 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
23586 }
23587
23588 if (!replace_id && (gotdest != SIP_GET_DEST_EXTEN_FOUND)) {
23589 switch(gotdest) {
23590 case SIP_GET_DEST_INVALID_URI:
23591 transmit_response_reliable(p, "416 Unsupported URI scheme", req);
23592 break;
23593 case SIP_GET_DEST_EXTEN_MATCHMORE:
23594 if (ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP)
23595 == SIP_PAGE2_ALLOWOVERLAP_YES) {
23596 transmit_response_reliable(p, "484 Address Incomplete", req);
23597 break;
23598 }
23599
23600
23601
23602
23603
23604
23605
23606
23607
23608 case SIP_GET_DEST_EXTEN_NOT_FOUND:
23609 {
23610 char *decoded_exten = ast_strdupa(p->exten);
23611 transmit_response_reliable(p, "404 Not Found", req);
23612 ast_uri_decode(decoded_exten);
23613 ast_log(LOG_NOTICE, "Call from '%s' (%s) to extension"
23614 " '%s' rejected because extension not found in context '%s'.\n",
23615 S_OR(p->username, p->peername), ast_sockaddr_stringify(&p->recv), decoded_exten, p->context);
23616 }
23617 break;
23618 case SIP_GET_DEST_REFUSED:
23619 default:
23620 transmit_response_reliable(p, "403 Forbidden", req);
23621 }
23622
23623 p->invitestate = INV_COMPLETED;
23624 update_call_counter(p, DEC_CALL_LIMIT);
23625 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23626 res = 0;
23627 goto request_invite_cleanup;
23628 } else {
23629
23630
23631
23632 if (ast_strlen_zero(p->exten))
23633 ast_string_field_set(p, exten, "s");
23634
23635
23636 make_our_tag(p);
23637
23638 if (handle_request_invite_st(p, req, required, reinvite)) {
23639 p->invitestate = INV_COMPLETED;
23640 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23641 res = -1;
23642 goto request_invite_cleanup;
23643 }
23644
23645
23646
23647 c = sip_new(p, AST_STATE_DOWN, S_OR(p->peername, NULL), NULL);
23648 if (cc_recall_core_id != -1) {
23649 ast_setup_cc_recall_datastore(c, cc_recall_core_id);
23650 ast_cc_agent_set_interfaces_chanvar(c);
23651 }
23652 *recount = 1;
23653
23654
23655 build_route(p, req, 0, 0);
23656
23657 if (c) {
23658 ast_party_redirecting_init(&redirecting);
23659 memset(&update_redirecting, 0, sizeof(update_redirecting));
23660 change_redirecting_information(p, req, &redirecting, &update_redirecting,
23661 FALSE);
23662 ast_channel_set_redirecting(c, &redirecting, &update_redirecting);
23663 ast_party_redirecting_free(&redirecting);
23664 }
23665 }
23666 } else {
23667 ast_party_redirecting_init(&redirecting);
23668 memset(&update_redirecting, 0, sizeof(update_redirecting));
23669 if (sipdebug) {
23670 if (!req->ignore)
23671 ast_debug(2, "Got a SIP re-invite for call %s\n", p->callid);
23672 else
23673 ast_debug(2, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
23674 }
23675 if (!req->ignore)
23676 reinvite = 1;
23677
23678 if (handle_request_invite_st(p, req, required, reinvite)) {
23679 p->invitestate = INV_COMPLETED;
23680 if (!p->lastinvite) {
23681 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23682 }
23683 res = -1;
23684 goto request_invite_cleanup;
23685 }
23686
23687 c = p->owner;
23688 change_redirecting_information(p, req, &redirecting, &update_redirecting, FALSE);
23689 if (c) {
23690 ast_channel_set_redirecting(c, &redirecting, &update_redirecting);
23691 }
23692 ast_party_redirecting_free(&redirecting);
23693 }
23694
23695 if (reinvite && p->stimer->st_active == TRUE) {
23696 restart_session_timer(p);
23697 }
23698
23699 if (!req->ignore && p)
23700 p->lastinvite = seqno;
23701
23702 if (c && replace_id) {
23703 if (!ast_strlen_zero(pickup.exten)) {
23704 append_history(p, "Xfer", "INVITE/Replace received");
23705
23706
23707 transmit_response(p, "100 Trying", req);
23708 p->invitestate = INV_PROCEEDING;
23709 ast_setstate(c, AST_STATE_RING);
23710
23711
23712 ast_channel_unlock(c);
23713 *nounlock = 1;
23714
23715
23716
23717
23718
23719
23720 sip_pvt_unlock(p);
23721 do_magic_pickup(c, pickup.exten, pickup.context);
23722
23723 ast_hangup(c);
23724 sip_pvt_lock(p);
23725
23726 res = 0;
23727 goto request_invite_cleanup;
23728 } else {
23729
23730 if (sipdebug)
23731 ast_debug(4, "Sending this call to the invite/replcaes handler %s\n", p->callid);
23732 res = handle_invite_replaces(p, req, debug, seqno, addr, nounlock);
23733 refer_locked = 0;
23734 goto request_invite_cleanup;
23735 }
23736 }
23737
23738
23739 if (c) {
23740 enum ast_channel_state c_state = c->_state;
23741
23742 if (c_state != AST_STATE_UP && reinvite &&
23743 (p->invitestate == INV_TERMINATED || p->invitestate == INV_CONFIRMED)) {
23744
23745
23746
23747
23748
23749
23750
23751
23752
23753 c_state = AST_STATE_UP;
23754 }
23755
23756 switch(c_state) {
23757 case AST_STATE_DOWN:
23758 ast_debug(2, "%s: New call is still down.... Trying... \n", c->name);
23759 transmit_provisional_response(p, "100 Trying", req, 0);
23760 p->invitestate = INV_PROCEEDING;
23761 ast_setstate(c, AST_STATE_RING);
23762 if (strcmp(p->exten, ast_pickup_ext())) {
23763 enum ast_pbx_result result;
23764
23765 result = ast_pbx_start(c);
23766
23767 switch(result) {
23768 case AST_PBX_FAILED:
23769 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
23770 p->invitestate = INV_COMPLETED;
23771 transmit_response_reliable(p, "503 Unavailable", req);
23772 break;
23773 case AST_PBX_CALL_LIMIT:
23774 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
23775 p->invitestate = INV_COMPLETED;
23776 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
23777 break;
23778 case AST_PBX_SUCCESS:
23779
23780 break;
23781 }
23782
23783 if (result) {
23784
23785
23786 ast_channel_unlock(c);
23787 *nounlock = 1;
23788 sip_pvt_unlock(p);
23789 ast_hangup(c);
23790 sip_pvt_lock(p);
23791 c = NULL;
23792 }
23793 } else {
23794 if (sip_pickup(c)) {
23795 ast_log(LOG_WARNING, "Failed to start Group pickup by %s\n", c->name);
23796 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
23797 sip_alreadygone(p);
23798 c->hangupcause = AST_CAUSE_FAILURE;
23799
23800
23801 ast_channel_unlock(c);
23802 *nounlock = 1;
23803
23804 p->invitestate = INV_COMPLETED;
23805 sip_pvt_unlock(p);
23806 ast_hangup(c);
23807 sip_pvt_lock(p);
23808 c = NULL;
23809 }
23810 }
23811 break;
23812 case AST_STATE_RING:
23813 transmit_provisional_response(p, "100 Trying", req, 0);
23814 p->invitestate = INV_PROCEEDING;
23815 break;
23816 case AST_STATE_RINGING:
23817 transmit_provisional_response(p, "180 Ringing", req, 0);
23818 p->invitestate = INV_PROCEEDING;
23819 break;
23820 case AST_STATE_UP:
23821 ast_debug(2, "%s: This call is UP.... \n", c->name);
23822
23823 transmit_response(p, "100 Trying", req);
23824
23825 if (p->t38.state == T38_PEER_REINVITE) {
23826 if (p->t38id > -1) {
23827
23828 AST_SCHED_DEL_UNREF(sched, p->t38id, dialog_unref(p, "remove ref for t38id"));
23829 }
23830 p->t38id = ast_sched_add(sched, 5000, sip_t38_abort, dialog_ref(p, "passing dialog ptr into sched structure based on t38id for sip_t38_abort."));
23831 } else if (p->t38.state == T38_ENABLED) {
23832 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
23833 transmit_response_with_t38_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)));
23834 } else if (p->t38.state == T38_DISABLED) {
23835
23836 if (p->srtp && !ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)) {
23837 ast_log(LOG_WARNING, "Target does not support required crypto\n");
23838 transmit_response_reliable(p, "488 Not Acceptable Here (crypto)", req);
23839 } else {
23840 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
23841 transmit_response_with_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)), p->session_modify == TRUE ? FALSE : TRUE, FALSE);
23842 ast_queue_control(p->owner, AST_CONTROL_UPDATE_RTP_PEER);
23843 }
23844 }
23845
23846 p->invitestate = INV_TERMINATED;
23847 break;
23848 default:
23849 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %u\n", c->_state);
23850 transmit_response(p, "100 Trying", req);
23851 break;
23852 }
23853 } else {
23854 if (!req->ignore && p && (p->autokillid == -1)) {
23855 const char *msg;
23856
23857 if (!p->jointcapability)
23858 msg = "488 Not Acceptable Here (codec error)";
23859 else {
23860 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
23861 msg = "503 Unavailable";
23862 }
23863 transmit_response_reliable(p, msg, req);
23864 p->invitestate = INV_COMPLETED;
23865 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23866 }
23867 }
23868
23869 request_invite_cleanup:
23870
23871 if (refer_locked && p->refer && p->refer->refer_call) {
23872 sip_pvt_unlock(p->refer->refer_call);
23873 if (p->refer->refer_call->owner) {
23874 ast_channel_unlock(p->refer->refer_call->owner);
23875 }
23876 p->refer->refer_call = dialog_unref(p->refer->refer_call, "unref dialog p->refer->refer_call");
23877 }
23878 if (authpeer) {
23879 authpeer = unref_peer(authpeer, "unref_peer, from handle_request_invite authpeer");
23880 }
23881
23882 return res;
23883 }
23884
23885
23886
23887
23888
23889
23890
23891
23892
23893
23894
23895
23896
23897
23898 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, uint32_t seqno, int *nounlock)
23899 {
23900 struct sip_dual target;
23901
23902 int res = 0;
23903 struct sip_pvt *targetcall_pvt;
23904 struct ast_party_connected_line connected_to_transferee;
23905 struct ast_party_connected_line connected_to_target;
23906 char transferer_linkedid[32];
23907 struct ast_channel *chans[2];
23908
23909
23910 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
23911 transferer->refer->replaces_callid_fromtag))) {
23912 if (transferer->refer->localtransfer) {
23913
23914
23915
23916 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
23917 append_history(transferer, "Xfer", "Refer failed");
23918 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
23919 transferer->refer->status = REFER_FAILED;
23920 return -1;
23921 }
23922
23923 ast_debug(3, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
23924 return 0;
23925 }
23926
23927
23928 append_history(transferer, "Xfer", "Refer accepted");
23929 if (!targetcall_pvt->owner) {
23930 ast_debug(4, "SIP attended transfer: Error: No owner of target call\n");
23931
23932 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
23933 append_history(transferer, "Xfer", "Refer failed");
23934 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
23935 transferer->refer->status = REFER_FAILED;
23936 sip_pvt_unlock(targetcall_pvt);
23937 if (targetcall_pvt)
23938 ao2_t_ref(targetcall_pvt, -1, "Drop targetcall_pvt pointer");
23939 return -1;
23940 }
23941
23942
23943 target.chan1 = ast_channel_ref(targetcall_pvt->owner);
23944 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
23945 if (target.chan2) {
23946 ast_channel_ref(target.chan2);
23947 }
23948
23949 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
23950
23951 if (target.chan2)
23952 ast_debug(4, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
23953 else if (target.chan1->_state != AST_STATE_RING)
23954 ast_debug(4, "SIP attended transfer: Error: No target channel\n");
23955 else
23956 ast_debug(4, "SIP attended transfer: Attempting transfer in ringing state\n");
23957 }
23958
23959
23960 if (sipdebug) {
23961 if (current->chan2)
23962 ast_debug(4, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
23963 else
23964 ast_debug(4, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
23965 }
23966
23967 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
23968
23969 ast_copy_string(transferer_linkedid, transferer->owner->linkedid, sizeof(transferer_linkedid));
23970
23971
23972 chans[0] = transferer->owner;
23973 chans[1] = target.chan1;
23974 ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
23975 "TransferMethod: SIP\r\n"
23976 "TransferType: Attended\r\n"
23977 "Channel: %s\r\n"
23978 "Uniqueid: %s\r\n"
23979 "SIP-Callid: %s\r\n"
23980 "TargetChannel: %s\r\n"
23981 "TargetUniqueid: %s\r\n",
23982 transferer->owner->name,
23983 transferer->owner->uniqueid,
23984 transferer->callid,
23985 target.chan1->name,
23986 target.chan1->uniqueid);
23987 ast_party_connected_line_init(&connected_to_transferee);
23988 ast_party_connected_line_init(&connected_to_target);
23989
23990 ast_party_connected_line_copy(&connected_to_transferee, ¤t->chan1->connected);
23991
23992 ast_party_connected_line_copy(&connected_to_target, &target.chan1->connected);
23993 connected_to_target.source = connected_to_transferee.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
23994 res = attempt_transfer(current, &target);
23995 if (res) {
23996
23997 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
23998 append_history(transferer, "Xfer", "Refer failed");
23999 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
24000
24001 sip_pvt_unlock(targetcall_pvt);
24002 ast_channel_unlock(target.chan1);
24003 } else {
24004
24005 const char *xfersound = pbx_builtin_getvar_helper(target.chan1, "ATTENDED_TRANSFER_COMPLETE_SOUND");
24006
24007
24008 ast_cel_report_event(target.chan1, AST_CEL_ATTENDEDTRANSFER, NULL, transferer_linkedid, target.chan2);
24009
24010
24011 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
24012 append_history(transferer, "Xfer", "Refer succeeded");
24013 transferer->refer->status = REFER_200OK;
24014 if (target.chan2 && !ast_strlen_zero(xfersound) && ast_streamfile(target.chan2, xfersound, target.chan2->language) >= 0) {
24015 ast_waitstream(target.chan2, "");
24016 }
24017
24018
24019
24020
24021
24022
24023
24024
24025
24026
24027
24028 ast_channel_ref(current->chan1);
24029 ast_channel_unlock(current->chan1);
24030 ast_channel_unlock(target.chan1);
24031 *nounlock = 1;
24032 sip_pvt_unlock(targetcall_pvt);
24033 sip_pvt_unlock(transferer);
24034
24035 ast_do_masquerade(target.chan1);
24036
24037 if (target.chan2) {
24038 ast_indicate(target.chan2, AST_CONTROL_UNHOLD);
24039 }
24040
24041 if (current->chan2 && current->chan2->_state == AST_STATE_RING) {
24042 ast_indicate(target.chan1, AST_CONTROL_RINGING);
24043 }
24044
24045 if (target.chan2) {
24046 ast_channel_queue_connected_line_update(target.chan1, &connected_to_transferee, NULL);
24047 ast_channel_queue_connected_line_update(target.chan2, &connected_to_target, NULL);
24048 } else {
24049
24050
24051
24052
24053
24054
24055
24056 struct ast_control_read_action_payload *frame_payload;
24057 int payload_size;
24058 int frame_size;
24059 unsigned char connected_line_data[1024];
24060 payload_size = ast_connected_line_build_data(connected_line_data,
24061 sizeof(connected_line_data), &connected_to_target, NULL);
24062 frame_size = payload_size + sizeof(*frame_payload);
24063 if (payload_size != -1) {
24064 frame_payload = ast_alloca(frame_size);
24065 frame_payload->payload_size = payload_size;
24066 memcpy(frame_payload->payload, connected_line_data, payload_size);
24067 frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
24068 ast_queue_control_data(target.chan1, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
24069 }
24070
24071
24072
24073
24074 ast_channel_queue_connected_line_update(target.chan1, &connected_to_transferee, NULL);
24075
24076 }
24077 sip_pvt_lock(transferer);
24078
24079 ast_channel_unref(current->chan1);
24080 }
24081
24082
24083 ast_party_connected_line_free(&connected_to_target);
24084 ast_party_connected_line_free(&connected_to_transferee);
24085 ast_channel_unref(target.chan1);
24086 if (target.chan2) {
24087 ast_channel_unref(target.chan2);
24088 }
24089 if (targetcall_pvt)
24090 ao2_t_ref(targetcall_pvt, -1, "drop targetcall_pvt");
24091 return 1;
24092 }
24093
24094
24095
24096
24097
24098
24099
24100
24101
24102
24103
24104
24105
24106
24107
24108
24109
24110
24111
24112
24113
24114
24115
24116
24117
24118
24119
24120
24121
24122
24123
24124
24125
24126
24127
24128
24129
24130
24131
24132
24133
24134
24135
24136
24137
24138
24139
24140
24141
24142
24143
24144
24145
24146
24147
24148
24149
24150
24151
24152
24153
24154
24155
24156
24157
24158 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, uint32_t seqno, int *nounlock)
24159 {
24160
24161
24162
24163
24164 struct sip_dual current = { 0, };
24165 struct ast_channel *chans[2] = { 0, };
24166 char *refer_to = NULL;
24167 char *refer_to_domain = NULL;
24168 char *refer_to_context = NULL;
24169 char *referred_by = NULL;
24170 char *callid = NULL;
24171 int localtransfer = 0;
24172 int attendedtransfer = 0;
24173 int res = 0;
24174
24175 if (req->debug) {
24176 ast_verbose("Call %s got a SIP call transfer from %s: (REFER)!\n",
24177 p->callid,
24178 ast_test_flag(&p->flags[0], SIP_OUTGOING) ? "callee" : "caller");
24179 }
24180
24181 if (!p->owner) {
24182
24183
24184 ast_debug(3, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
24185 transmit_response(p, "603 Declined (No dialog)", req);
24186 if (!req->ignore) {
24187 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
24188 sip_alreadygone(p);
24189 pvt_set_needdestroy(p, "outside of dialog");
24190 }
24191 res = 0;
24192 goto handle_refer_cleanup;
24193 }
24194
24195
24196 if (p->allowtransfer == TRANSFER_CLOSED ) {
24197
24198 transmit_response(p, "603 Declined (policy)", req);
24199 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
24200
24201 res = 0;
24202 goto handle_refer_cleanup;
24203 }
24204
24205 if (!req->ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
24206
24207 transmit_response(p, "491 Request pending", req);
24208 append_history(p, "Xfer", "Refer failed. Request pending.");
24209 res = 0;
24210 goto handle_refer_cleanup;
24211 }
24212
24213
24214 if (!p->refer && !sip_refer_allocate(p)) {
24215 transmit_response(p, "500 Internal Server Error", req);
24216 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
24217 res = -3;
24218 goto handle_refer_cleanup;
24219 }
24220
24221 res = get_refer_info(p, req);
24222
24223 p->refer->status = REFER_SENT;
24224
24225 if (res != 0) {
24226 switch (res) {
24227 case -2:
24228 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
24229 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
24230 if (req->debug) {
24231 ast_debug(1, "SIP transfer to black hole can't be handled (no refer-to: )\n");
24232 }
24233 break;
24234 case -3:
24235 transmit_response(p, "603 Declined (Non sip: uri)", req);
24236 append_history(p, "Xfer", "Refer failed. Non SIP uri");
24237 if (req->debug) {
24238 ast_debug(1, "SIP transfer to non-SIP uri denied\n");
24239 }
24240 break;
24241 default:
24242
24243 transmit_response(p, "202 Accepted", req);
24244 append_history(p, "Xfer", "Refer failed. Bad extension.");
24245 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
24246 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
24247 if (req->debug) {
24248 ast_debug(1, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
24249 }
24250 break;
24251 }
24252 res = 0;
24253 goto handle_refer_cleanup;
24254 }
24255 if (ast_strlen_zero(p->context)) {
24256 ast_string_field_set(p, context, sip_cfg.default_context);
24257 }
24258
24259
24260 if (sip_cfg.allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
24261 p->refer->localtransfer = 1;
24262 if (sipdebug) {
24263 ast_debug(3, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
24264 }
24265 } else if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
24266
24267 p->refer->localtransfer = 1;
24268 } else if (sipdebug) {
24269 ast_debug(3, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
24270 }
24271
24272
24273
24274 if (req->ignore) {
24275 goto handle_refer_cleanup;
24276 }
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303 chans[0] = current.chan1 = p->owner;
24304
24305
24306 chans[1] = current.chan2 = ast_bridged_channel(current.chan1);
24307
24308 ast_channel_ref(current.chan1);
24309 if (current.chan2) {
24310 ast_channel_ref(current.chan2);
24311 }
24312
24313 if (sipdebug) {
24314 ast_debug(3, "SIP %s transfer: Transferer channel %s, transferee channel %s\n",
24315 p->refer->attendedtransfer ? "attended" : "blind",
24316 current.chan1->name,
24317 current.chan2 ? current.chan2->name : "<none>");
24318 }
24319
24320 if (!current.chan2 && !p->refer->attendedtransfer) {
24321
24322
24323
24324 if (sipdebug) {
24325 ast_debug(3, "Refused SIP transfer on non-bridged channel.\n");
24326 }
24327 p->refer->status = REFER_FAILED;
24328 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
24329 transmit_response(p, "603 Declined", req);
24330 res = -1;
24331 goto handle_refer_cleanup;
24332 }
24333
24334 if (current.chan2) {
24335 if (sipdebug) {
24336 ast_debug(4, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
24337 }
24338 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
24339 }
24340
24341 ast_set_flag(&p->flags[0], SIP_GOTREFER);
24342
24343
24344 transmit_response(p, "202 Accepted", req);
24345
24346
24347 if (p->refer->attendedtransfer) {
24348
24349 if ((res = local_attended_transfer(p, ¤t, req, seqno, nounlock))) {
24350 goto handle_refer_cleanup;
24351 }
24352
24353 if (sipdebug) {
24354 ast_debug(4, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
24355 }
24356
24357 }
24358
24359
24360 refer_to = ast_strdupa(p->refer->refer_to);
24361 refer_to_domain = ast_strdupa(p->refer->refer_to_domain);
24362 refer_to_context = ast_strdupa(p->refer->refer_to_context);
24363 referred_by = ast_strdupa(p->refer->referred_by);
24364 callid = ast_strdupa(p->callid);
24365 localtransfer = p->refer->localtransfer;
24366 attendedtransfer = p->refer->attendedtransfer;
24367
24368 if (!*nounlock) {
24369 ast_channel_unlock(p->owner);
24370 *nounlock = 1;
24371 }
24372 sip_pvt_unlock(p);
24373
24374
24375 if (localtransfer && ast_parking_ext_valid(refer_to, current.chan1, refer_to_context)) {
24376 sip_pvt_lock(p);
24377 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
24378 p->refer->status = REFER_200OK;
24379 append_history(p, "Xfer", "REFER to call parking.");
24380 sip_pvt_unlock(p);
24381
24382 ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
24383 "TransferMethod: SIP\r\n"
24384 "TransferType: Blind\r\n"
24385 "Channel: %s\r\n"
24386 "Uniqueid: %s\r\n"
24387 "SIP-Callid: %s\r\n"
24388 "TargetChannel: %s\r\n"
24389 "TargetUniqueid: %s\r\n"
24390 "TransferExten: %s\r\n"
24391 "Transfer2Parking: Yes\r\n",
24392 current.chan1->name,
24393 current.chan1->uniqueid,
24394 callid,
24395 current.chan2->name,
24396 current.chan2->uniqueid,
24397 refer_to);
24398
24399 if (sipdebug) {
24400 ast_debug(4, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
24401 }
24402
24403
24404 if (sip_park(current.chan2, current.chan1, req, seqno, refer_to, refer_to_context)) {
24405 sip_pvt_lock(p);
24406 transmit_notify_with_sipfrag(p, seqno, "500 Internal Server Error", TRUE);
24407 } else {
24408 sip_pvt_lock(p);
24409 }
24410 goto handle_refer_cleanup;
24411 }
24412
24413
24414
24415
24416 if (current.chan1 && current.chan2) {
24417 ast_debug(3, "chan1->name: %s\n", current.chan1->name);
24418 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
24419 }
24420
24421 if (current.chan2) {
24422 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
24423 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", refer_to_domain);
24424 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
24425
24426 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
24427
24428 if (!ast_strlen_zero(referred_by)) {
24429 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", referred_by);
24430 }
24431 }
24432
24433 sip_pvt_lock(p);
24434
24435 if (!ast_strlen_zero(p->refer->replaces_callid)) {
24436 char tempheader[SIPBUFSIZE];
24437 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
24438 p->refer->replaces_callid_totag ? ";to-tag=" : "",
24439 p->refer->replaces_callid_totag,
24440 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
24441 p->refer->replaces_callid_fromtag);
24442
24443 if (current.chan2) {
24444 sip_pvt_unlock(p);
24445 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
24446 sip_pvt_lock(p);
24447 }
24448 }
24449
24450
24451
24452
24453 if (!p->refer->attendedtransfer) {
24454 transmit_notify_with_sipfrag(p, seqno, "180 Ringing", FALSE);
24455 }
24456
24457
24458
24459
24460
24461 if (!current.chan2) {
24462
24463
24464
24465
24466
24467
24468
24469 p->refer->status = REFER_FAILED;
24470 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
24471 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
24472 append_history(p, "Xfer", "Refer failed (only bridged calls).");
24473 res = -1;
24474 goto handle_refer_cleanup;
24475 }
24476 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
24477
24478
24479
24480
24481
24482
24483
24484 sip_pvt_unlock(p);
24485 ast_indicate(current.chan2, AST_CONTROL_UNHOLD);
24486 res = ast_async_goto(current.chan2, refer_to_context, refer_to, 1);
24487
24488 if (!res) {
24489 ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
24490 "TransferMethod: SIP\r\n"
24491 "TransferType: Blind\r\n"
24492 "Channel: %s\r\n"
24493 "Uniqueid: %s\r\n"
24494 "SIP-Callid: %s\r\n"
24495 "TargetChannel: %s\r\n"
24496 "TargetUniqueid: %s\r\n"
24497 "TransferExten: %s\r\n"
24498 "TransferContext: %s\r\n",
24499 current.chan1->name,
24500 current.chan1->uniqueid,
24501 callid,
24502 current.chan2->name,
24503 current.chan2->uniqueid,
24504 refer_to,
24505 refer_to_context);
24506
24507 ast_debug(3, "%s transfer succeeded. Telling transferer.\n", attendedtransfer? "Attended" : "Blind");
24508
24509
24510 ast_channel_lock(current.chan1);
24511 ast_cel_report_event(current.chan1, p->refer->attendedtransfer? AST_CEL_ATTENDEDTRANSFER : AST_CEL_BLINDTRANSFER, NULL, p->refer->attendedtransfer ? NULL : p->refer->refer_to, current.chan2);
24512 ast_channel_unlock(current.chan1);
24513
24514 sip_pvt_lock(p);
24515 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
24516 if (p->refer->localtransfer) {
24517 p->refer->status = REFER_200OK;
24518 }
24519 if (p->owner) {
24520 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
24521 }
24522 append_history(p, "Xfer", "Refer succeeded.");
24523 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
24524
24525
24526 res = 0;
24527 } else {
24528 sip_pvt_lock(p);
24529 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
24530 ast_debug(3, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
24531 append_history(p, "Xfer", "Refer failed.");
24532
24533 p->refer->status = REFER_FAILED;
24534 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
24535 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
24536 res = -1;
24537 }
24538
24539 handle_refer_cleanup:
24540 if (current.chan1) {
24541 ast_channel_unref(current.chan1);
24542 }
24543 if (current.chan2) {
24544 ast_channel_unref(current.chan2);
24545 }
24546
24547
24548 return res;
24549 }
24550
24551
24552 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
24553 {
24554
24555 check_via(p, req);
24556 sip_alreadygone(p);
24557
24558 if (p->owner && p->owner->_state == AST_STATE_UP) {
24559
24560 transmit_response(p, "200 OK", req);
24561 ast_debug(1, "Got CANCEL on an answered call. Ignoring... \n");
24562 return 0;
24563 }
24564
24565
24566
24567
24568
24569
24570 if (p->invitestate == INV_TERMINATED || p->invitestate == INV_COMPLETED) {
24571 __sip_pretend_ack(p);
24572 }
24573 if (p->invitestate != INV_TERMINATED)
24574 p->invitestate = INV_CANCELLED;
24575
24576 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
24577 update_call_counter(p, DEC_CALL_LIMIT);
24578
24579 stop_media_flows(p);
24580 if (p->owner) {
24581 sip_queue_hangup_cause(p, 0);
24582 } else {
24583 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
24584 }
24585 if (ast_str_strlen(p->initreq.data) > 0) {
24586 struct sip_pkt *pkt, *prev_pkt;
24587
24588
24589
24590
24591
24592
24593
24594
24595
24596
24597
24598 for (pkt = p->packets, prev_pkt = NULL; pkt; prev_pkt = pkt, pkt = pkt->next) {
24599 if (pkt->seqno == p->lastinvite && pkt->response_code == 487) {
24600 AST_SCHED_DEL(sched, pkt->retransid);
24601 UNLINK(pkt, p->packets, prev_pkt);
24602 dialog_unref(pkt->owner, "unref packet->owner from dialog");
24603 if (pkt->data) {
24604 ast_free(pkt->data);
24605 }
24606 ast_free(pkt);
24607 break;
24608 }
24609 }
24610 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
24611 transmit_response(p, "200 OK", req);
24612 return 1;
24613 } else {
24614 transmit_response(p, "481 Call Leg Does Not Exist", req);
24615 return 0;
24616 }
24617 }
24618
24619
24620 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
24621 {
24622 struct ast_channel *c=NULL;
24623 int res;
24624 struct ast_channel *bridged_to;
24625 const char *required;
24626
24627
24628 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !req->ignore) {
24629 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
24630 }
24631
24632 __sip_pretend_ack(p);
24633
24634 p->invitestate = INV_TERMINATED;
24635
24636 copy_request(&p->initreq, req);
24637 if (sipdebug)
24638 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
24639 check_via(p, req);
24640 sip_alreadygone(p);
24641
24642
24643 if (p->do_history || p->owner) {
24644 char quality_buf[AST_MAX_USER_FIELD], *quality;
24645 struct ast_channel *bridge = p->owner ? ast_bridged_channel(p->owner) : NULL;
24646
24647
24648
24649
24650 while (bridge && ast_channel_trylock(bridge)) {
24651 ast_channel_unlock(p->owner);
24652 do {
24653
24654 sip_pvt_unlock(p);
24655 usleep(1);
24656 sip_pvt_lock(p);
24657 } while (p->owner && ast_channel_trylock(p->owner));
24658 bridge = p->owner ? ast_bridged_channel(p->owner) : NULL;
24659 }
24660
24661
24662 if (p->rtp && (quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
24663 if (p->do_history) {
24664 append_history(p, "RTCPaudio", "Quality:%s", quality);
24665
24666 if ((quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
24667 append_history(p, "RTCPaudioJitter", "Quality:%s", quality);
24668 }
24669 if ((quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
24670 append_history(p, "RTCPaudioLoss", "Quality:%s", quality);
24671 }
24672 if ((quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
24673 append_history(p, "RTCPaudioRTT", "Quality:%s", quality);
24674 }
24675 }
24676
24677 if (p->owner) {
24678 ast_rtp_instance_set_stats_vars(p->owner, p->rtp);
24679 }
24680
24681 }
24682
24683 if (bridge) {
24684 struct sip_pvt *q = bridge->tech_pvt;
24685
24686 if (IS_SIP_TECH(bridge->tech) && q && q->rtp) {
24687 ast_rtp_instance_set_stats_vars(bridge, q->rtp);
24688 }
24689 ast_channel_unlock(bridge);
24690 }
24691
24692 if (p->vrtp && (quality = ast_rtp_instance_get_quality(p->vrtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
24693 if (p->do_history) {
24694 append_history(p, "RTCPvideo", "Quality:%s", quality);
24695 }
24696 if (p->owner) {
24697 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", quality);
24698 }
24699 }
24700 if (p->trtp && (quality = ast_rtp_instance_get_quality(p->trtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
24701 if (p->do_history) {
24702 append_history(p, "RTCPtext", "Quality:%s", quality);
24703 }
24704 if (p->owner) {
24705 pbx_builtin_setvar_helper(p->owner, "RTPTEXTQOS", quality);
24706 }
24707 }
24708 }
24709
24710 stop_media_flows(p);
24711 if (p->stimer) {
24712 stop_session_timer(p);
24713 }
24714
24715 if (!ast_strlen_zero(get_header(req, "Also"))) {
24716 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
24717 ast_sockaddr_stringify(&p->recv));
24718 if (ast_strlen_zero(p->context))
24719 ast_string_field_set(p, context, sip_cfg.default_context);
24720 res = get_also_info(p, req);
24721 if (!res) {
24722 c = p->owner;
24723 if (c) {
24724 bridged_to = ast_bridged_channel(c);
24725 if (bridged_to) {
24726
24727 ast_queue_control(c, AST_CONTROL_UNHOLD);
24728 ast_channel_unlock(c);
24729 ast_async_goto(bridged_to, p->context, p->refer->refer_to, 1);
24730 ast_channel_lock(c);
24731 } else
24732 ast_queue_hangup(p->owner);
24733 }
24734 } else {
24735 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_sockaddr_stringify(&p->recv));
24736 if (p->owner)
24737 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
24738 }
24739 } else if (p->owner) {
24740 sip_queue_hangup_cause(p, 0);
24741 sip_scheddestroy_final(p, DEFAULT_TRANS_TIMEOUT);
24742 ast_debug(3, "Received bye, issuing owner hangup\n");
24743 } else {
24744 sip_scheddestroy_final(p, DEFAULT_TRANS_TIMEOUT);
24745 ast_debug(3, "Received bye, no owner, selfdestruct soon.\n");
24746 }
24747 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
24748
24749
24750 required = get_header(req, "Require");
24751 if (!ast_strlen_zero(required)) {
24752 char unsupported[256] = { 0, };
24753 parse_sip_options(required, unsupported, ARRAY_LEN(unsupported));
24754
24755
24756 if (!ast_strlen_zero(unsupported)) {
24757 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, unsupported);
24758 ast_log(LOG_WARNING, "Received SIP BYE with unsupported required extension: required:%s unsupported:%s\n", required, unsupported);
24759 } else {
24760 transmit_response(p, "200 OK", req);
24761 }
24762 } else {
24763 transmit_response(p, "200 OK", req);
24764 }
24765
24766
24767
24768 AST_SCHED_DEL_UNREF(sched, p->waitid, dialog_unref(p, "decrement refcount from sip_destroy because waitid won't be scheduled"));
24769
24770 return 1;
24771 }
24772
24773
24774 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
24775 {
24776 if (!req->ignore) {
24777 if (req->debug)
24778 ast_verbose("Receiving message!\n");
24779 receive_message(p, req);
24780 } else
24781 transmit_response(p, "202 Accepted", req);
24782 return 1;
24783 }
24784
24785 static enum sip_publish_type determine_sip_publish_type(struct sip_request *req, const char * const event, const char * const etag, const char * const expires, int *expires_int)
24786 {
24787 int etag_present = !ast_strlen_zero(etag);
24788 int body_present = req->lines > 0;
24789
24790 ast_assert(expires_int != NULL);
24791
24792 if (ast_strlen_zero(expires)) {
24793
24794
24795
24796
24797 *expires_int = DEFAULT_PUBLISH_EXPIRES;
24798 } else if (sscanf(expires, "%30d", expires_int) != 1) {
24799 return SIP_PUBLISH_UNKNOWN;
24800 }
24801
24802 if (*expires_int == 0) {
24803 return SIP_PUBLISH_REMOVE;
24804 } else if (!etag_present && body_present) {
24805 return SIP_PUBLISH_INITIAL;
24806 } else if (etag_present && !body_present) {
24807 return SIP_PUBLISH_REFRESH;
24808 } else if (etag_present && body_present) {
24809 return SIP_PUBLISH_MODIFY;
24810 }
24811
24812 return SIP_PUBLISH_UNKNOWN;
24813 }
24814
24815 #ifdef HAVE_LIBXML2
24816 static void get_pidf_body(struct sip_request *req, char *pidf_body, size_t size)
24817 {
24818 int i;
24819 struct ast_str *str = ast_str_alloca(size);
24820 for (i = 0; i < req->lines; ++i) {
24821 ast_str_append(&str, 0, "%s", REQ_OFFSET_TO_STR(req, line[i]));
24822 }
24823 ast_copy_string(pidf_body, ast_str_buffer(str), size);
24824 }
24825
24826 static int pidf_validate_tuple(struct ast_xml_node *tuple_node)
24827 {
24828 const char *id;
24829 int status_found = FALSE;
24830 struct ast_xml_node *tuple_children;
24831 struct ast_xml_node *tuple_children_iterator;
24832
24833 if (!(id = ast_xml_get_attribute(tuple_node, "id"))) {
24834 ast_log(LOG_WARNING, "Tuple XML element has no attribute 'id'\n");
24835 return FALSE;
24836 }
24837
24838 ast_xml_free_attr(id);
24839
24840 if (!(tuple_children = ast_xml_node_get_children(tuple_node))) {
24841
24842 ast_log(LOG_WARNING, "Tuple XML element has no child elements\n");
24843 return FALSE;
24844 }
24845 for (tuple_children_iterator = tuple_children; tuple_children_iterator;
24846 tuple_children_iterator = ast_xml_node_get_next(tuple_children_iterator)) {
24847
24848
24849
24850
24851 if (strcmp(ast_xml_node_get_name(tuple_children_iterator), "status")) {
24852
24853 continue;
24854 }
24855 if (status_found == TRUE) {
24856
24857 ast_log(LOG_WARNING, "Multiple status elements found in tuple. Only one allowed\n");
24858 return FALSE;
24859 }
24860 status_found = TRUE;
24861 }
24862 return status_found;
24863 }
24864
24865
24866 static int pidf_validate_presence(struct ast_xml_doc *doc)
24867 {
24868 struct ast_xml_node *presence_node = ast_xml_get_root(doc);
24869 struct ast_xml_node *child_nodes;
24870 struct ast_xml_node *node_iterator;
24871 struct ast_xml_ns *ns;
24872 const char *entity;
24873 const char *namespace;
24874 const char presence_namespace[] = "urn:ietf:params:xml:ns:pidf";
24875
24876 if (!presence_node) {
24877 ast_log(LOG_WARNING, "Unable to retrieve root node of the XML document\n");
24878 return FALSE;
24879 }
24880
24881
24882
24883 if (strcmp(ast_xml_node_get_name(presence_node), "presence")) {
24884 ast_log(LOG_WARNING, "Root node of PIDF document is not 'presence'. Invalid\n");
24885 return FALSE;
24886 }
24887
24888
24889
24890
24891 if (!(entity = ast_xml_get_attribute(presence_node, "entity"))) {
24892 ast_log(LOG_WARNING, "Presence element of PIDF document has no 'entity' attribute\n");
24893 return FALSE;
24894 }
24895
24896 ast_xml_free_attr(entity);
24897
24898 if (!(ns = ast_xml_find_namespace(doc, presence_node, NULL))) {
24899 ast_log(LOG_WARNING, "Couldn't find default namespace...\n");
24900 return FALSE;
24901 }
24902
24903 namespace = ast_xml_get_ns_href(ns);
24904 if (ast_strlen_zero(namespace) || strcmp(namespace, presence_namespace)) {
24905 ast_log(LOG_WARNING, "PIDF document has invalid namespace value %s\n", namespace);
24906 return FALSE;
24907 }
24908
24909 if (!(child_nodes = ast_xml_node_get_children(presence_node))) {
24910 ast_log(LOG_WARNING, "PIDF document has no elements as children of 'presence'. Invalid\n");
24911 return FALSE;
24912 }
24913
24914
24915
24916
24917
24918
24919
24920
24921
24922 for (node_iterator = child_nodes; node_iterator;
24923 node_iterator = ast_xml_node_get_next(node_iterator)) {
24924 if (strcmp(ast_xml_node_get_name(node_iterator), "tuple")) {
24925
24926 continue;
24927 }
24928 if (pidf_validate_tuple(node_iterator) == FALSE) {
24929 ast_log(LOG_WARNING, "Unable to validate tuple\n");
24930 return FALSE;
24931 }
24932 }
24933
24934 return TRUE;
24935 }
24936
24937
24938
24939
24940
24941
24942
24943
24944
24945
24946
24947
24948
24949
24950
24951
24952 static int sip_pidf_validate(struct sip_request *req, struct ast_xml_doc **pidf_doc)
24953 {
24954 struct ast_xml_doc *doc;
24955 int content_length;
24956 const char *content_length_str = get_header(req, "Content-Length");
24957 const char *content_type = get_header(req, "Content-Type");
24958 char pidf_body[SIPBUFSIZE];
24959 int res;
24960
24961 if (ast_strlen_zero(content_type) || strcmp(content_type, "application/pidf+xml")) {
24962 ast_log(LOG_WARNING, "Content type is not PIDF\n");
24963 return FALSE;
24964 }
24965
24966 if (ast_strlen_zero(content_length_str)) {
24967 ast_log(LOG_WARNING, "No content length. Can't determine bounds of PIDF document\n");
24968 return FALSE;
24969 }
24970
24971 if (sscanf(content_length_str, "%30d", &content_length) != 1) {
24972 ast_log(LOG_WARNING, "Invalid content length provided\n");
24973 return FALSE;
24974 }
24975
24976 if (content_length > sizeof(pidf_body)) {
24977 ast_log(LOG_WARNING, "Content length of PIDF document truncated to %d bytes\n", (int) sizeof(pidf_body));
24978 content_length = sizeof(pidf_body);
24979 }
24980
24981 get_pidf_body(req, pidf_body, content_length);
24982
24983 if (!(doc = ast_xml_read_memory(pidf_body, content_length))) {
24984 ast_log(LOG_WARNING, "Unable to open XML PIDF document. Is it malformed?\n");
24985 return FALSE;
24986 }
24987
24988 res = pidf_validate_presence(doc);
24989 if (res == TRUE) {
24990 *pidf_doc = doc;
24991 } else {
24992 ast_xml_close(doc);
24993 }
24994 return res;
24995 }
24996
24997 static int cc_esc_publish_handler(struct sip_pvt *pvt, struct sip_request *req, struct event_state_compositor *esc, struct sip_esc_entry *esc_entry)
24998 {
24999 const char *uri = REQ_OFFSET_TO_STR(req, rlPart2);
25000 struct ast_cc_agent *agent;
25001 struct sip_cc_agent_pvt *agent_pvt;
25002 struct ast_xml_doc *pidf_doc = NULL;
25003 const char *basic_status = NULL;
25004 struct ast_xml_node *presence_node;
25005 struct ast_xml_node *presence_children;
25006 struct ast_xml_node *tuple_node;
25007 struct ast_xml_node *tuple_children;
25008 struct ast_xml_node *status_node;
25009 struct ast_xml_node *status_children;
25010 struct ast_xml_node *basic_node;
25011 int res = 0;
25012
25013 if (!((agent = find_sip_cc_agent_by_notify_uri(uri)) || (agent = find_sip_cc_agent_by_subscribe_uri(uri)))) {
25014 ast_log(LOG_WARNING, "Could not find agent using uri '%s'\n", uri);
25015 transmit_response(pvt, "412 Conditional Request Failed", req);
25016 return -1;
25017 }
25018
25019 agent_pvt = agent->private_data;
25020
25021 if (sip_pidf_validate(req, &pidf_doc) == FALSE) {
25022 res = -1;
25023 goto cc_publish_cleanup;
25024 }
25025
25026
25027
25028
25029
25030
25031
25032 presence_node = ast_xml_get_root(pidf_doc);
25033 if (!(presence_children = ast_xml_node_get_children(presence_node))) {
25034 ast_log(LOG_WARNING, "No tuples within presence element.\n");
25035 res = -1;
25036 goto cc_publish_cleanup;
25037 }
25038
25039 if (!(tuple_node = ast_xml_find_element(presence_children, "tuple", NULL, NULL))) {
25040 ast_log(LOG_NOTICE, "Couldn't find tuple node?\n");
25041 res = -1;
25042 goto cc_publish_cleanup;
25043 }
25044
25045
25046
25047
25048 tuple_children = ast_xml_node_get_children(tuple_node);
25049
25050 status_node = ast_xml_find_element(tuple_children, "status", NULL, NULL);
25051
25052 if (!(status_children = ast_xml_node_get_children(status_node))) {
25053 ast_log(LOG_WARNING, "No basic elements within status element.\n");
25054 res = -1;
25055 goto cc_publish_cleanup;
25056 }
25057
25058 if (!(basic_node = ast_xml_find_element(status_children, "basic", NULL, NULL))) {
25059 ast_log(LOG_WARNING, "Couldn't find basic node?\n");
25060 res = -1;
25061 goto cc_publish_cleanup;
25062 }
25063
25064 basic_status = ast_xml_get_text(basic_node);
25065
25066 if (ast_strlen_zero(basic_status)) {
25067 ast_log(LOG_NOTICE, "NOthing in basic node?\n");
25068 res = -1;
25069 goto cc_publish_cleanup;
25070 }
25071
25072 if (!strcmp(basic_status, "open")) {
25073 agent_pvt->is_available = TRUE;
25074 ast_cc_agent_caller_available(agent->core_id, "Received PUBLISH stating SIP caller %s is available",
25075 agent->device_name);
25076 } else if (!strcmp(basic_status, "closed")) {
25077 agent_pvt->is_available = FALSE;
25078 ast_cc_agent_caller_busy(agent->core_id, "Received PUBLISH stating SIP caller %s is busy",
25079 agent->device_name);
25080 } else {
25081 ast_log(LOG_NOTICE, "Invalid content in basic element: %s\n", basic_status);
25082 }
25083
25084 cc_publish_cleanup:
25085 if (basic_status) {
25086 ast_xml_free_text(basic_status);
25087 }
25088 if (pidf_doc) {
25089 ast_xml_close(pidf_doc);
25090 }
25091 ao2_ref(agent, -1);
25092 if (res) {
25093 transmit_response(pvt, "400 Bad Request", req);
25094 }
25095 return res;
25096 }
25097
25098 #endif
25099
25100 static int handle_sip_publish_initial(struct sip_pvt *p, struct sip_request *req, struct event_state_compositor *esc, const int expires)
25101 {
25102 struct sip_esc_entry *esc_entry = create_esc_entry(esc, req, expires);
25103 int res = 0;
25104
25105 if (!esc_entry) {
25106 transmit_response(p, "503 Internal Server Failure", req);
25107 return -1;
25108 }
25109
25110 if (esc->callbacks->initial_handler) {
25111 res = esc->callbacks->initial_handler(p, req, esc, esc_entry);
25112 }
25113
25114 if (!res) {
25115 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 0);
25116 }
25117
25118 ao2_ref(esc_entry, -1);
25119 return res;
25120 }
25121
25122 static int handle_sip_publish_refresh(struct sip_pvt *p, struct sip_request *req, struct event_state_compositor *esc, const char * const etag, const int expires)
25123 {
25124 struct sip_esc_entry *esc_entry = get_esc_entry(etag, esc);
25125 int expires_ms = expires * 1000;
25126 int res = 0;
25127
25128 if (!esc_entry) {
25129 transmit_response(p, "412 Conditional Request Failed", req);
25130 return -1;
25131 }
25132
25133 AST_SCHED_REPLACE_UNREF(esc_entry->sched_id, sched, expires_ms, publish_expire, esc_entry,
25134 ao2_ref(_data, -1),
25135 ao2_ref(esc_entry, -1),
25136 ao2_ref(esc_entry, +1));
25137
25138 if (esc->callbacks->refresh_handler) {
25139 res = esc->callbacks->refresh_handler(p, req, esc, esc_entry);
25140 }
25141
25142 if (!res) {
25143 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 1);
25144 }
25145
25146 ao2_ref(esc_entry, -1);
25147 return res;
25148 }
25149
25150 static int handle_sip_publish_modify(struct sip_pvt *p, struct sip_request *req, struct event_state_compositor *esc, const char * const etag, const int expires)
25151 {
25152 struct sip_esc_entry *esc_entry = get_esc_entry(etag, esc);
25153 int expires_ms = expires * 1000;
25154 int res = 0;
25155
25156 if (!esc_entry) {
25157 transmit_response(p, "412 Conditional Request Failed", req);
25158 return -1;
25159 }
25160
25161 AST_SCHED_REPLACE_UNREF(esc_entry->sched_id, sched, expires_ms, publish_expire, esc_entry,
25162 ao2_ref(_data, -1),
25163 ao2_ref(esc_entry, -1),
25164 ao2_ref(esc_entry, +1));
25165
25166 if (esc->callbacks->modify_handler) {
25167 res = esc->callbacks->modify_handler(p, req, esc, esc_entry);
25168 }
25169
25170 if (!res) {
25171 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 1);
25172 }
25173
25174 ao2_ref(esc_entry, -1);
25175 return res;
25176 }
25177
25178 static int handle_sip_publish_remove(struct sip_pvt *p, struct sip_request *req, struct event_state_compositor *esc, const char * const etag)
25179 {
25180 struct sip_esc_entry *esc_entry = get_esc_entry(etag, esc);
25181 int res = 0;
25182
25183 if (!esc_entry) {
25184 transmit_response(p, "412 Conditional Request Failed", req);
25185 return -1;
25186 }
25187
25188 AST_SCHED_DEL(sched, esc_entry->sched_id);
25189
25190 ao2_ref(esc_entry, -1);
25191
25192 if (esc->callbacks->remove_handler) {
25193 res = esc->callbacks->remove_handler(p, req, esc, esc_entry);
25194 }
25195
25196 if (!res) {
25197 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 1);
25198 }
25199
25200
25201 ao2_unlink(esc->compositor, esc_entry);
25202 ao2_ref(esc_entry, -1);
25203 return res;
25204 }
25205
25206 static int handle_request_publish(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const uint32_t seqno, const char *uri)
25207 {
25208 const char *etag = get_header(req, "SIP-If-Match");
25209 const char *event = get_header(req, "Event");
25210 struct event_state_compositor *esc;
25211 enum sip_publish_type publish_type;
25212 const char *expires_str = get_header(req, "Expires");
25213 int expires_int;
25214 int auth_result;
25215 int handler_result = -1;
25216
25217 if (ast_strlen_zero(event)) {
25218 transmit_response(p, "489 Bad Event", req);
25219 pvt_set_needdestroy(p, "missing Event: header");
25220 return -1;
25221 }
25222
25223 if (!(esc = get_esc(event))) {
25224 transmit_response(p, "489 Bad Event", req);
25225 pvt_set_needdestroy(p, "unknown event package in publish");
25226 return -1;
25227 }
25228
25229 auth_result = check_user(p, req, SIP_PUBLISH, uri, XMIT_UNRELIABLE, addr);
25230 if (auth_result == AUTH_CHALLENGE_SENT) {
25231 p->lastinvite = seqno;
25232 return 0;
25233 } else if (auth_result < 0) {
25234 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
25235 transmit_response(p, "403 Forbidden", req);
25236 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
25237 ast_string_field_set(p, theirtag, NULL);
25238 return 0;
25239 } else if (auth_result == AUTH_SUCCESSFUL && p->lastinvite) {
25240
25241 __sip_ack(p, p->lastinvite, 1, 0);
25242 }
25243
25244 publish_type = determine_sip_publish_type(req, event, etag, expires_str, &expires_int);
25245
25246 if (expires_int > max_expiry) {
25247 expires_int = max_expiry;
25248 } else if (expires_int < min_expiry && expires_int > 0) {
25249 transmit_response_with_minexpires(p, "423 Interval too small", req);
25250 pvt_set_needdestroy(p, "Expires is less that the min expires allowed.");
25251 return 0;
25252 }
25253 p->expiry = expires_int;
25254
25255
25256
25257
25258 switch (publish_type) {
25259 case SIP_PUBLISH_UNKNOWN:
25260 transmit_response(p, "400 Bad Request", req);
25261 break;
25262 case SIP_PUBLISH_INITIAL:
25263 handler_result = handle_sip_publish_initial(p, req, esc, expires_int);
25264 break;
25265 case SIP_PUBLISH_REFRESH:
25266 handler_result = handle_sip_publish_refresh(p, req, esc, etag, expires_int);
25267 break;
25268 case SIP_PUBLISH_MODIFY:
25269 handler_result = handle_sip_publish_modify(p, req, esc, etag, expires_int);
25270 break;
25271 case SIP_PUBLISH_REMOVE:
25272 handler_result = handle_sip_publish_remove(p, req, esc, etag);
25273 break;
25274 default:
25275 transmit_response(p, "400 Impossible Condition", req);
25276 break;
25277 }
25278 if (!handler_result && p->expiry > 0) {
25279 sip_scheddestroy(p, (p->expiry + 10) * 1000);
25280 } else {
25281 pvt_set_needdestroy(p, "forcing expiration");
25282 }
25283
25284 return handler_result;
25285 }
25286
25287
25288
25289
25290
25291
25292
25293 static void add_peer_mwi_subs(struct sip_peer *peer)
25294 {
25295 struct sip_mailbox *mailbox;
25296
25297 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
25298 if (mailbox->event_sub) {
25299 ast_event_unsubscribe(mailbox->event_sub);
25300 }
25301
25302 mailbox->event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, "SIP mbox event", peer,
25303 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
25304 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
25305 AST_EVENT_IE_END);
25306 }
25307 }
25308
25309 static int handle_cc_subscribe(struct sip_pvt *p, struct sip_request *req)
25310 {
25311 const char *uri = REQ_OFFSET_TO_STR(req, rlPart2);
25312 char *param_separator;
25313 struct ast_cc_agent *agent;
25314 struct sip_cc_agent_pvt *agent_pvt;
25315 const char *expires_str = get_header(req, "Expires");
25316 int expires = -1;
25317
25318 if (!ast_strlen_zero(expires_str)) {
25319 sscanf(expires_str, "%d", &expires);
25320 }
25321
25322 if ((param_separator = strchr(uri, ';'))) {
25323 *param_separator = '\0';
25324 }
25325
25326 p->subscribed = CALL_COMPLETION;
25327
25328 if (!(agent = find_sip_cc_agent_by_subscribe_uri(uri))) {
25329 if (!expires) {
25330
25331
25332
25333
25334
25335
25336 transmit_response(p, "200 OK", req);
25337 return 0;
25338 }
25339 ast_log(LOG_WARNING, "Invalid URI '%s' in CC subscribe\n", uri);
25340 transmit_response(p, "404 Not Found", req);
25341 return -1;
25342 }
25343
25344 agent_pvt = agent->private_data;
25345
25346 if (!expires) {
25347
25348
25349
25350 ast_cc_failed(agent->core_id, "CC is being canceled by %s", agent->device_name);
25351 transmit_response(p, "200 OK", req);
25352 ao2_ref(agent, -1);
25353 return 0;
25354 }
25355
25356 agent_pvt->subscribe_pvt = dialog_ref(p, "SIP CC agent gains reference to subscription dialog");
25357 ast_cc_agent_accept_request(agent->core_id, "SIP caller %s has requested CC via SUBSCRIBE",
25358 agent->device_name);
25359
25360
25361
25362
25363 ao2_ref(agent, -1);
25364 return 0;
25365 }
25366
25367
25368 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, uint32_t seqno, const char *e)
25369 {
25370 int gotdest = 0;
25371 int res = 0;
25372 int firststate;
25373 struct sip_peer *authpeer = NULL;
25374 const char *eventheader = get_header(req, "Event");
25375 int resubscribe = (p->subscribed != NONE) && !req->ignore;
25376 char *event_end;
25377 ptrdiff_t event_len = 0;
25378
25379 if (p->initreq.headers) {
25380
25381 if (p->initreq.method != SIP_SUBSCRIBE) {
25382
25383
25384 transmit_response(p, "403 Forbidden (within dialog)", req);
25385
25386 ast_debug(1, "Got a subscription within the context of another call, can't handle that - %s (Method %s)\n", p->callid, sip_methods[p->initreq.method].text);
25387 return 0;
25388 } else if (req->debug) {
25389 if (resubscribe)
25390 ast_debug(1, "Got a re-subscribe on existing subscription %s\n", p->callid);
25391 else
25392 ast_debug(1, "Got a new subscription %s (possibly with auth) or retransmission\n", p->callid);
25393 }
25394 }
25395
25396
25397
25398
25399 if (!sip_cfg.allowsubscribe) {
25400 transmit_response(p, "403 Forbidden (policy)", req);
25401 pvt_set_needdestroy(p, "forbidden");
25402 return 0;
25403 }
25404
25405 if (!req->ignore && !resubscribe) {
25406 const char *to = get_header(req, "To");
25407 char totag[128];
25408 set_pvt_allowed_methods(p, req);
25409
25410
25411 if (!ast_strlen_zero(to) && gettag(req, "To", totag, sizeof(totag))) {
25412 if (req->debug)
25413 ast_verbose("Received resubscription for a dialog we no longer know about. Telling remote side to subscribe again.\n");
25414 transmit_response(p, "481 Subscription does not exist", req);
25415 pvt_set_needdestroy(p, "subscription does not exist");
25416 return 0;
25417 }
25418
25419
25420 if (req->debug)
25421 ast_verbose("Creating new subscription\n");
25422
25423 copy_request(&p->initreq, req);
25424 if (sipdebug)
25425 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
25426 check_via(p, req);
25427 build_route(p, req, 0, 0);
25428 } else if (req->debug && req->ignore)
25429 ast_verbose("Ignoring this SUBSCRIBE request\n");
25430
25431
25432 if (ast_strlen_zero(eventheader)) {
25433 transmit_response(p, "489 Bad Event", req);
25434 ast_debug(2, "Received SIP subscribe for unknown event package: <none>\n");
25435 pvt_set_needdestroy(p, "unknown event package in subscribe");
25436 return 0;
25437 }
25438
25439 event_end = strchr(eventheader, ';');
25440 if (event_end) {
25441 event_len = event_end - eventheader;
25442 }
25443
25444
25445
25446
25447 if (p->subscribed == NONE || resubscribe) {
25448 res = check_user_full(p, req, SIP_SUBSCRIBE, e, XMIT_UNRELIABLE, addr, &authpeer);
25449
25450
25451 if (res == AUTH_CHALLENGE_SENT)
25452 return 0;
25453 if (res != AUTH_SUCCESSFUL) {
25454 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
25455 transmit_response(p, "403 Forbidden", req);
25456
25457 pvt_set_needdestroy(p, "authentication failed");
25458 return 0;
25459 }
25460 }
25461
25462
25463
25464
25465
25466
25467 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
25468 transmit_response(p, "403 Forbidden (policy)", req);
25469 pvt_set_needdestroy(p, "subscription not allowed");
25470 if (authpeer) {
25471 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 1)");
25472 }
25473 return 0;
25474 }
25475
25476 if (strncmp(eventheader, "message-summary", MAX(event_len, 15)) && strncmp(eventheader, "call-completion", MAX(event_len, 15))) {
25477
25478 gotdest = get_destination(p, NULL, NULL);
25479 }
25480
25481
25482 parse_ok_contact(p, req);
25483
25484 build_contact(p);
25485 if (gotdest != SIP_GET_DEST_EXTEN_FOUND) {
25486 if (gotdest == SIP_GET_DEST_INVALID_URI) {
25487 transmit_response(p, "416 Unsupported URI scheme", req);
25488 } else {
25489 transmit_response(p, "404 Not Found", req);
25490 }
25491 pvt_set_needdestroy(p, "subscription target not found");
25492 if (authpeer) {
25493 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 2)");
25494 }
25495 return 0;
25496 }
25497
25498
25499 if (ast_strlen_zero(p->tag))
25500 make_our_tag(p);
25501
25502 if (!strncmp(eventheader, "presence", MAX(event_len, 8)) || !strncmp(eventheader, "dialog", MAX(event_len, 6))) {
25503 unsigned int pidf_xml;
25504 const char *accept;
25505 int start = 0;
25506 enum subscriptiontype subscribed = NONE;
25507 const char *unknown_acceptheader = NULL;
25508
25509
25510 accept = __get_header(req, "Accept", &start);
25511 while ((subscribed == NONE) && !ast_strlen_zero(accept)) {
25512 pidf_xml = strstr(accept, "application/pidf+xml") ? 1 : 0;
25513
25514
25515
25516 if (pidf_xml && strstr(p->useragent, "Polycom")) {
25517 subscribed = XPIDF_XML;
25518 } else if (pidf_xml) {
25519 subscribed = PIDF_XML;
25520 } else if (strstr(accept, "application/dialog-info+xml")) {
25521 subscribed = DIALOG_INFO_XML;
25522
25523 } else if (strstr(accept, "application/cpim-pidf+xml")) {
25524 subscribed = CPIM_PIDF_XML;
25525 } else if (strstr(accept, "application/xpidf+xml")) {
25526 subscribed = XPIDF_XML;
25527 } else {
25528 unknown_acceptheader = accept;
25529 }
25530
25531 accept = __get_header(req, "Accept", &start);
25532 }
25533
25534 if (!start) {
25535 if (p->subscribed == NONE) {
25536 transmit_response(p, "489 Bad Event", req);
25537 ast_log(LOG_WARNING,"SUBSCRIBE failure: no Accept header: pvt: "
25538 "stateid: %d, laststate: %d, dialogver: %u, subscribecont: "
25539 "'%s', subscribeuri: '%s'\n",
25540 p->stateid,
25541 p->laststate,
25542 p->dialogver,
25543 p->subscribecontext,
25544 p->subscribeuri);
25545 pvt_set_needdestroy(p, "no Accept header");
25546 if (authpeer) {
25547 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 2)");
25548 }
25549 return 0;
25550 }
25551
25552
25553 } else if (subscribed == NONE) {
25554
25555 char mybuf[200];
25556 if (!ast_strlen_zero(unknown_acceptheader)) {
25557 snprintf(mybuf, sizeof(mybuf), "489 Bad Event (format %s)", unknown_acceptheader);
25558 } else {
25559 snprintf(mybuf, sizeof(mybuf), "489 Bad Event");
25560 }
25561 transmit_response(p, mybuf, req);
25562 ast_log(LOG_WARNING,"SUBSCRIBE failure: unrecognized format:"
25563 "'%s' pvt: subscribed: %d, stateid: %d, laststate: %d,"
25564 "dialogver: %u, subscribecont: '%s', subscribeuri: '%s'\n",
25565 unknown_acceptheader,
25566 (int)p->subscribed,
25567 p->stateid,
25568 p->laststate,
25569 p->dialogver,
25570 p->subscribecontext,
25571 p->subscribeuri);
25572 pvt_set_needdestroy(p, "unrecognized format");
25573 if (authpeer) {
25574 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 2)");
25575 }
25576 return 0;
25577 } else {
25578 p->subscribed = subscribed;
25579 }
25580 } else if (!strncmp(eventheader, "message-summary", MAX(event_len, 15))) {
25581 int start = 0;
25582 int found_supported = 0;
25583 const char *acceptheader;
25584
25585 acceptheader = __get_header(req, "Accept", &start);
25586 while (!found_supported && !ast_strlen_zero(acceptheader)) {
25587 found_supported = strcmp(acceptheader, "application/simple-message-summary") ? 0 : 1;
25588 if (!found_supported && (option_debug > 2)) {
25589 ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
25590 }
25591 acceptheader = __get_header(req, "Accept", &start);
25592 }
25593 if (start && !found_supported) {
25594
25595 transmit_response(p, "406 Not Acceptable", req);
25596 ast_debug(2, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
25597 pvt_set_needdestroy(p, "unknown format");
25598 if (authpeer) {
25599 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 3)");
25600 }
25601 return 0;
25602 }
25603
25604
25605
25606
25607
25608 if (!authpeer || AST_LIST_EMPTY(&authpeer->mailboxes)) {
25609 if (!authpeer) {
25610 transmit_response(p, "404 Not found", req);
25611 } else {
25612 transmit_response(p, "404 Not found (no mailbox)", req);
25613 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", S_OR(authpeer->name, ""));
25614 }
25615 pvt_set_needdestroy(p, "received 404 response");
25616 if (authpeer) {
25617 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 3)");
25618 }
25619 return 0;
25620 }
25621
25622 p->subscribed = MWI_NOTIFICATION;
25623 if (ast_test_flag(&authpeer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY)) {
25624 ao2_unlock(p);
25625 add_peer_mwi_subs(authpeer);
25626 ao2_lock(p);
25627 }
25628 if (authpeer->mwipvt != p) {
25629
25630 if (authpeer->mwipvt) {
25631 dialog_unlink_all(authpeer->mwipvt);
25632 authpeer->mwipvt = dialog_unref(authpeer->mwipvt, "unref dialog authpeer->mwipvt");
25633 }
25634 authpeer->mwipvt = dialog_ref(p, "setting peers' mwipvt to p");
25635 }
25636 if (p->relatedpeer != authpeer) {
25637 if (p->relatedpeer) {
25638 unref_peer(p->relatedpeer, "Unref previously stored relatedpeer ptr");
25639 }
25640 p->relatedpeer = ref_peer(authpeer, "setting dialog's relatedpeer pointer");
25641 }
25642
25643 } else if (!strncmp(eventheader, "call-completion", MAX(event_len, 15))) {
25644 handle_cc_subscribe(p, req);
25645 } else {
25646 transmit_response(p, "489 Bad Event", req);
25647 ast_debug(2, "Received SIP subscribe for unknown event package: %s\n", eventheader);
25648 pvt_set_needdestroy(p, "unknown event package");
25649 if (authpeer) {
25650 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 5)");
25651 }
25652 return 0;
25653 }
25654
25655
25656 if (p->subscribed != MWI_NOTIFICATION && p->subscribed != CALL_COMPLETION && !resubscribe) {
25657 if (p->stateid != -1) {
25658 ast_extension_state_del(p->stateid, cb_extensionstate);
25659 }
25660 dialog_ref(p, "copying dialog ptr into extension state struct");
25661 p->stateid = ast_extension_state_add_destroy(p->context, p->exten,
25662 cb_extensionstate, cb_extensionstate_destroy, p);
25663 if (p->stateid == -1) {
25664 dialog_unref(p, "copying dialog ptr into extension state struct failed");
25665 }
25666 }
25667
25668 if (!req->ignore) {
25669 p->lastinvite = seqno;
25670 }
25671 if (!p->needdestroy) {
25672 p->expiry = atoi(get_header(req, "Expires"));
25673
25674
25675 if (p->expiry > max_expiry) {
25676 p->expiry = max_expiry;
25677 } else if (p->expiry < min_expiry && p->expiry > 0) {
25678 transmit_response_with_minexpires(p, "423 Interval too small", req);
25679 ast_log(LOG_WARNING, "Received subscription for extension \"%s\" context \"%s\" "
25680 "with Expire header less that 'minexpire' limit. Received \"Expire: %d\" min is %d\n",
25681 p->exten, p->context, p->expiry, min_expiry);
25682 pvt_set_needdestroy(p, "Expires is less that the min expires allowed.");
25683 if (authpeer) {
25684 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 6)");
25685 }
25686 return 0;
25687 }
25688
25689 if (sipdebug) {
25690 const char *action = p->expiry > 0 ? "Adding" : "Removing";
25691 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer) {
25692 ast_debug(2, "%s subscription for mailbox notification - peer %s\n",
25693 action, p->relatedpeer->name);
25694 } else if (p->subscribed == CALL_COMPLETION) {
25695 ast_debug(2, "%s CC subscription for peer %s\n", action, p->username);
25696 } else {
25697 ast_debug(2, "%s subscription for extension %s context %s for peer %s\n",
25698 action, p->exten, p->context, p->username);
25699 }
25700 }
25701 if (p->autokillid > -1 && sip_cancel_destroy(p))
25702 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
25703 if (p->expiry > 0)
25704 sip_scheddestroy(p, (p->expiry + 10) * 1000);
25705
25706 if (p->subscribed == MWI_NOTIFICATION) {
25707 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
25708 transmit_response(p, "200 OK", req);
25709 if (p->relatedpeer) {
25710 struct sip_peer *peer = p->relatedpeer;
25711 ref_peer(peer, "ensure a peer ref is held during MWI sending");
25712 ao2_unlock(p);
25713 sip_send_mwi_to_peer(peer, 0);
25714 ao2_lock(p);
25715 unref_peer(peer, "release a peer ref now that MWI is sent");
25716 }
25717 } else if (p->subscribed != CALL_COMPLETION) {
25718 sip_pvt_unlock(p);
25719 firststate = ast_extension_state(NULL, p->context, p->exten);
25720 sip_pvt_lock(p);
25721
25722 if (firststate < 0) {
25723 ast_log(LOG_NOTICE, "Got SUBSCRIBE for extension %s@%s from %s, but there is no hint for that extension.\n", p->exten, p->context, ast_sockaddr_stringify(&p->sa));
25724 transmit_response(p, "404 Not found", req);
25725 pvt_set_needdestroy(p, "no extension for SUBSCRIBE");
25726 if (authpeer) {
25727 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 6)");
25728 }
25729 return 0;
25730 }
25731 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
25732 transmit_response(p, "200 OK", req);
25733 transmit_state_notify(p, firststate, 1, FALSE);
25734 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
25735
25736 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
25737
25738
25739 }
25740 if (!p->expiry) {
25741 pvt_set_needdestroy(p, "forcing expiration");
25742 }
25743 }
25744
25745 if (authpeer) {
25746 unref_peer(authpeer, "unref pointer into (*authpeer)");
25747 }
25748 return 1;
25749 }
25750
25751
25752 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e)
25753 {
25754 enum check_auth_result res;
25755
25756
25757
25758 if (p->initreq.headers && p->initreq.method != SIP_REGISTER) {
25759 ast_log(LOG_WARNING, "Ignoring spurious REGISTER with Call-ID: %s\n", p->callid);
25760 return -1;
25761 }
25762
25763
25764 copy_request(&p->initreq, req);
25765 if (sipdebug)
25766 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
25767 check_via(p, req);
25768 if ((res = register_verify(p, addr, req, e)) < 0) {
25769 const char *reason;
25770
25771 switch (res) {
25772 case AUTH_SECRET_FAILED:
25773 reason = "Wrong password";
25774 break;
25775 case AUTH_USERNAME_MISMATCH:
25776 reason = "Username/auth name mismatch";
25777 break;
25778 case AUTH_NOT_FOUND:
25779 reason = "No matching peer found";
25780 break;
25781 case AUTH_UNKNOWN_DOMAIN:
25782 reason = "Not a local domain";
25783 break;
25784 case AUTH_PEER_NOT_DYNAMIC:
25785 reason = "Peer is not supposed to register";
25786 break;
25787 case AUTH_ACL_FAILED:
25788 reason = "Device does not match ACL";
25789 break;
25790 case AUTH_BAD_TRANSPORT:
25791 reason = "Device not configured to use this transport type";
25792 break;
25793 default:
25794 reason = "Unknown failure";
25795 break;
25796 }
25797 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
25798 get_header(req, "To"), ast_sockaddr_stringify(addr),
25799 reason);
25800 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
25801 } else {
25802 req->authenticated = 1;
25803 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
25804 }
25805
25806 if (res < 1) {
25807
25808
25809 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
25810 }
25811 return res;
25812 }
25813
25814
25815
25816
25817
25818
25819
25820
25821 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int *recount, int *nounlock)
25822 {
25823
25824
25825 const char *cmd;
25826 const char *cseq;
25827 const char *useragent;
25828 const char *via;
25829 const char *callid;
25830 int via_pos = 0;
25831 uint32_t seqno;
25832 int len;
25833 int respid;
25834 int res = 0;
25835 int debug = sip_debug_test_pvt(p);
25836 const char *e;
25837 int error = 0;
25838 int oldmethod = p->method;
25839 int acked = 0;
25840
25841
25842
25843
25844 cseq = get_header(req, "Cseq");
25845 cmd = REQ_OFFSET_TO_STR(req, header[0]);
25846
25847 via = __get_header(req, "Via", &via_pos);
25848
25849 callid = get_header(req, "Call-ID");
25850
25851
25852 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq) || ast_strlen_zero(via)) {
25853 ast_log(LOG_ERROR, "Dropping this SIP message with Call-ID '%s', it's incomplete.\n", callid);
25854 error = 1;
25855 }
25856 if (!error && sscanf(cseq, "%30u%n", &seqno, &len) != 1) {
25857 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
25858 error = 1;
25859 }
25860 if (error) {
25861 if (!p->initreq.headers) {
25862 pvt_set_needdestroy(p, "no headers");
25863 }
25864 return -1;
25865 }
25866
25867
25868 cmd = REQ_OFFSET_TO_STR(req, rlPart1);
25869 e = ast_skip_blanks(REQ_OFFSET_TO_STR(req, rlPart2));
25870
25871
25872 useragent = get_header(req, "User-Agent");
25873 if (!ast_strlen_zero(useragent))
25874 ast_string_field_set(p, useragent, useragent);
25875
25876
25877 if (req->method == SIP_RESPONSE) {
25878
25879
25880
25881
25882
25883
25884 if (ast_strlen_zero(e)) {
25885 return 0;
25886 }
25887 if (sscanf(e, "%30d %n", &respid, &len) != 1) {
25888 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
25889 return 0;
25890 }
25891 if (respid <= 0) {
25892 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
25893 return 0;
25894 }
25895
25896
25897
25898 if (!ast_strlen_zero(__get_header(req, "via", &via_pos))) {
25899 ast_log(LOG_WARNING, "Misrouted SIP response '%s' with Call-ID '%s', too many vias\n", e, callid);
25900 return 0;
25901 }
25902 if (p->ocseq && (p->ocseq < seqno)) {
25903 ast_debug(1, "Ignoring out of order response %u (expecting %u)\n", seqno, p->ocseq);
25904 return -1;
25905 } else {
25906 char causevar[256], causeval[256];
25907
25908 if ((respid == 200) || ((respid >= 300) && (respid <= 399))) {
25909 extract_uri(p, req);
25910 }
25911
25912 handle_response(p, respid, e + len, req, seqno);
25913
25914 if (global_store_sip_cause && p->owner) {
25915 struct ast_channel *owner = p->owner;
25916
25917 snprintf(causevar, sizeof(causevar), "MASTER_CHANNEL(HASH(SIP_CAUSE,%s))", owner->name);
25918 snprintf(causeval, sizeof(causeval), "SIP %s", REQ_OFFSET_TO_STR(req, rlPart2));
25919
25920 ast_channel_ref(owner);
25921 sip_pvt_unlock(p);
25922 ast_channel_unlock(owner);
25923 *nounlock = 1;
25924 pbx_builtin_setvar_helper(owner, causevar, causeval);
25925 ast_channel_unref(owner);
25926 sip_pvt_lock(p);
25927 }
25928 }
25929 return 0;
25930 }
25931
25932
25933
25934
25935
25936 p->method = req->method;
25937 ast_debug(4, "**** Received %s (%u) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
25938
25939 if (p->icseq && (p->icseq > seqno) ) {
25940 if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
25941 ast_debug(2, "Got CANCEL or ACK on INVITE with transactions in between.\n");
25942 } else {
25943 ast_debug(1, "Ignoring too old SIP packet packet %u (expecting >= %u)\n", seqno, p->icseq);
25944 if (req->method == SIP_INVITE) {
25945 unsigned int ran = (ast_random() % 10) + 1;
25946 char seconds[4];
25947 snprintf(seconds, sizeof(seconds), "%u", ran);
25948 transmit_response_with_retry_after(p, "500 Server error", req, seconds);
25949 } else if (req->method != SIP_ACK) {
25950 transmit_response(p, "500 Server error", req);
25951 }
25952 return -1;
25953 }
25954 } else if (p->icseq &&
25955 p->icseq == seqno &&
25956 req->method != SIP_ACK &&
25957 (p->method != SIP_CANCEL || p->alreadygone)) {
25958
25959
25960
25961 req->ignore = 1;
25962 ast_debug(3, "Ignoring SIP message because of retransmit (%s Seqno %u, ours %u)\n", sip_methods[p->method].text, p->icseq, seqno);
25963 }
25964
25965
25966
25967 if (!p->pendinginvite && (req->method == SIP_CANCEL)) {
25968 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
25969 return res;
25970 }
25971
25972 if (seqno >= p->icseq)
25973
25974
25975
25976 p->icseq = seqno;
25977
25978
25979 if (ast_strlen_zero(p->theirtag)) {
25980 char tag[128];
25981
25982 gettag(req, "From", tag, sizeof(tag));
25983 ast_string_field_set(p, theirtag, tag);
25984 }
25985 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
25986
25987 if (sip_cfg.pedanticsipchecking) {
25988
25989
25990
25991
25992 if (!p->initreq.headers && req->has_to_tag) {
25993
25994 if (!req->ignore && req->method == SIP_INVITE) {
25995
25996
25997
25998
25999
26000
26001 char totag[128];
26002 gettag(req, "To", totag, sizeof(totag));
26003 ast_string_field_set(p, tag, totag);
26004 p->pendinginvite = p->icseq;
26005 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
26006
26007 return res;
26008 } else if (req->method != SIP_ACK) {
26009 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
26010 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
26011 return res;
26012 }
26013
26014 }
26015 }
26016
26017 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY || p->method == SIP_PUBLISH)) {
26018 transmit_response(p, "400 Bad request", req);
26019 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
26020 return -1;
26021 }
26022
26023
26024 switch (p->method) {
26025 case SIP_OPTIONS:
26026 res = handle_request_options(p, req, addr, e);
26027 break;
26028 case SIP_INVITE:
26029 res = handle_request_invite(p, req, debug, seqno, addr, recount, e, nounlock);
26030 break;
26031 case SIP_REFER:
26032 res = handle_request_refer(p, req, debug, seqno, nounlock);
26033 break;
26034 case SIP_CANCEL:
26035 res = handle_request_cancel(p, req);
26036 break;
26037 case SIP_BYE:
26038 res = handle_request_bye(p, req);
26039 break;
26040 case SIP_MESSAGE:
26041 res = handle_request_message(p, req);
26042 break;
26043 case SIP_PUBLISH:
26044 res = handle_request_publish(p, req, addr, seqno, e);
26045 break;
26046 case SIP_SUBSCRIBE:
26047 res = handle_request_subscribe(p, req, addr, seqno, e);
26048 break;
26049 case SIP_REGISTER:
26050 res = handle_request_register(p, req, addr, e);
26051 break;
26052 case SIP_INFO:
26053 if (req->debug)
26054 ast_verbose("Receiving INFO!\n");
26055 if (!req->ignore)
26056 handle_request_info(p, req);
26057 else
26058 transmit_response(p, "200 OK", req);
26059 break;
26060 case SIP_NOTIFY:
26061 res = handle_request_notify(p, req, addr, seqno, e);
26062 break;
26063 case SIP_UPDATE:
26064 res = handle_request_update(p, req);
26065 break;
26066 case SIP_ACK:
26067
26068 if (seqno == p->pendinginvite) {
26069 p->invitestate = INV_TERMINATED;
26070 p->pendinginvite = 0;
26071 acked = __sip_ack(p, seqno, 1 , 0);
26072 if (p->owner && find_sdp(req)) {
26073 if (process_sdp(p, req, SDP_T38_NONE)) {
26074 return -1;
26075 }
26076 if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
26077 ast_queue_control(p->owner, AST_CONTROL_SRCCHANGE);
26078 }
26079 }
26080 check_pendings(p);
26081 } else if (p->glareinvite == seqno) {
26082
26083 p->glareinvite = 0;
26084 acked = __sip_ack(p, seqno, 1, 0);
26085 }
26086 if (!acked) {
26087
26088
26089 p->method = oldmethod;
26090 }
26091 if (!p->lastinvite && ast_strlen_zero(p->randdata)) {
26092 pvt_set_needdestroy(p, "unmatched ACK");
26093 }
26094 break;
26095 default:
26096 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
26097 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
26098 cmd, ast_sockaddr_stringify(&p->sa));
26099
26100 if (!p->initreq.headers) {
26101 pvt_set_needdestroy(p, "unimplemented method");
26102 }
26103 break;
26104 }
26105 return res;
26106 }
26107
26108
26109
26110
26111
26112
26113 static int sipsock_read(int *id, int fd, short events, void *ignore)
26114 {
26115 struct sip_request req;
26116 struct ast_sockaddr addr;
26117 int res;
26118 static char readbuf[65535];
26119
26120 memset(&req, 0, sizeof(req));
26121 res = ast_recvfrom(fd, readbuf, sizeof(readbuf) - 1, 0, &addr);
26122 if (res < 0) {
26123 #if !defined(__FreeBSD__)
26124 if (errno == EAGAIN)
26125 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
26126 else
26127 #endif
26128 if (errno != ECONNREFUSED)
26129 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
26130 return 1;
26131 }
26132
26133 readbuf[res] = '\0';
26134
26135 if (!(req.data = ast_str_create(SIP_MIN_PACKET))) {
26136 return 1;
26137 }
26138
26139 if (ast_str_set(&req.data, 0, "%s", readbuf) == AST_DYNSTR_BUILD_FAILED) {
26140 return -1;
26141 }
26142
26143 req.socket.fd = sipsock;
26144 set_socket_transport(&req.socket, SIP_TRANSPORT_UDP);
26145 req.socket.tcptls_session = NULL;
26146 req.socket.port = htons(ast_sockaddr_port(&bindaddr));
26147
26148 handle_request_do(&req, &addr);
26149 deinit_req(&req);
26150
26151 return 1;
26152 }
26153
26154
26155
26156
26157
26158 static int handle_request_do(struct sip_request *req, struct ast_sockaddr *addr)
26159 {
26160 struct sip_pvt *p;
26161 struct ast_channel *owner_chan_ref = NULL;
26162 int recount = 0;
26163 int nounlock = 0;
26164
26165 if (sip_debug_test_addr(addr))
26166 req->debug = 1;
26167 if (sip_cfg.pedanticsipchecking)
26168 lws2sws(req->data);
26169 if (req->debug) {
26170 ast_verbose("\n<--- SIP read from %s:%s --->\n%s\n<------------->\n",
26171 get_transport(req->socket.type), ast_sockaddr_stringify(addr), ast_str_buffer(req->data));
26172 }
26173
26174 if (parse_request(req) == -1) {
26175 ast_str_reset(req->data);
26176 return 1;
26177 }
26178 req->method = find_sip_method(REQ_OFFSET_TO_STR(req, rlPart1));
26179
26180 if (req->debug)
26181 ast_verbose("--- (%d headers %d lines)%s ---\n", req->headers, req->lines, (req->headers + req->lines == 0) ? " Nat keepalive" : "");
26182
26183 if (req->headers < 2) {
26184 ast_str_reset(req->data);
26185 return 1;
26186 }
26187
26188
26189 ast_mutex_lock(&netlock);
26190
26191
26192 p = find_call(req, addr, req->method);
26193 if (p == NULL) {
26194 ast_debug(1, "Invalid SIP message - rejected , no callid, len %zu\n", ast_str_strlen(req->data));
26195 ast_mutex_unlock(&netlock);
26196 return 1;
26197 }
26198
26199
26200
26201 owner_chan_ref = sip_pvt_lock_full(p);
26202
26203 copy_socket_data(&p->socket, &req->socket);
26204
26205 ast_sockaddr_copy(&p->recv, addr);
26206
26207
26208 if (p->owner) {
26209 req->authenticated = 1;
26210 }
26211
26212 if (p->do_history)
26213 append_history(p, "Rx", "%s / %s / %s", ast_str_buffer(req->data), get_header(req, "CSeq"), REQ_OFFSET_TO_STR(req, rlPart2));
26214
26215 if (handle_incoming(p, req, addr, &recount, &nounlock) == -1) {
26216
26217 ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
26218 }
26219
26220 if (recount) {
26221 ast_update_use_count();
26222 }
26223
26224 if (p->owner && !nounlock) {
26225 ast_channel_unlock(p->owner);
26226 }
26227 if (owner_chan_ref) {
26228 ast_channel_unref(owner_chan_ref);
26229 }
26230 sip_pvt_unlock(p);
26231 ao2_t_ref(p, -1, "throw away dialog ptr from find_call at end of routine");
26232 ast_mutex_unlock(&netlock);
26233
26234 return 1;
26235 }
26236
26237
26238
26239
26240
26241
26242
26243 static int sip_standard_port(enum sip_transport type, int port)
26244 {
26245 if (type & SIP_TRANSPORT_TLS)
26246 return port == STANDARD_TLS_PORT;
26247 else
26248 return port == STANDARD_SIP_PORT;
26249 }
26250
26251 static int threadinfo_locate_cb(void *obj, void *arg, int flags)
26252 {
26253 struct sip_threadinfo *th = obj;
26254 struct ast_sockaddr *s = arg;
26255
26256 if (!ast_sockaddr_cmp(s, &th->tcptls_session->remote_address)) {
26257 return CMP_MATCH | CMP_STOP;
26258 }
26259
26260 return 0;
26261 }
26262
26263
26264
26265
26266
26267
26268 static struct ast_tcptls_session_instance *sip_tcp_locate(struct ast_sockaddr *s)
26269 {
26270 struct sip_threadinfo *th;
26271 struct ast_tcptls_session_instance *tcptls_instance = NULL;
26272
26273 if ((th = ao2_callback(threadt, 0, threadinfo_locate_cb, s))) {
26274 tcptls_instance = (ao2_ref(th->tcptls_session, +1), th->tcptls_session);
26275 ao2_t_ref(th, -1, "decrement ref from callback");
26276 }
26277
26278 return tcptls_instance;
26279 }
26280
26281
26282
26283
26284
26285
26286 int get_address_family_filter(unsigned int transport)
26287 {
26288 const struct ast_sockaddr *addr = NULL;
26289
26290 if ((transport == SIP_TRANSPORT_UDP) || !transport) {
26291 addr = &bindaddr;
26292 }
26293 else if (transport == SIP_TRANSPORT_TCP) {
26294 addr = &sip_tcp_desc.local_address;
26295 }
26296 else if (transport == SIP_TRANSPORT_TLS) {
26297 addr = &sip_tls_desc.local_address;
26298 }
26299
26300 if (ast_sockaddr_is_ipv6(addr) && ast_sockaddr_is_any(addr)) {
26301 return 0;
26302 }
26303
26304 return addr->ss.ss_family;
26305 }
26306
26307
26308 static int sip_prepare_socket(struct sip_pvt *p)
26309 {
26310 struct sip_socket *s = &p->socket;
26311 static const char name[] = "SIP socket";
26312 struct sip_threadinfo *th = NULL;
26313 struct ast_tcptls_session_instance *tcptls_session;
26314 struct ast_tcptls_session_args *ca;
26315 struct ast_sockaddr sa_tmp;
26316 pthread_t launched;
26317
26318
26319 if ((s->fd != -1) && (s->type == SIP_TRANSPORT_UDP)) {
26320 return s->fd;
26321 }
26322 if ((s->type & (SIP_TRANSPORT_TCP | SIP_TRANSPORT_TLS)) &&
26323 (s->tcptls_session) &&
26324 (s->tcptls_session->fd != -1)) {
26325 return s->tcptls_session->fd;
26326 }
26327
26328
26329
26330
26331 if (p->outboundproxy && p->outboundproxy->transport) {
26332 s->type = p->outboundproxy->transport;
26333 }
26334
26335 if (s->type == SIP_TRANSPORT_UDP) {
26336 s->fd = sipsock;
26337 return s->fd;
26338 }
26339
26340
26341
26342
26343
26344
26345
26346
26347
26348
26349
26350 ast_sockaddr_copy(&sa_tmp, sip_real_dst(p));
26351 if ((tcptls_session = sip_tcp_locate(&sa_tmp))) {
26352 s->fd = tcptls_session->fd;
26353 if (s->tcptls_session) {
26354 ao2_ref(s->tcptls_session, -1);
26355 s->tcptls_session = NULL;
26356 }
26357 s->tcptls_session = tcptls_session;
26358 return s->fd;
26359
26360 } else if (s->tcptls_session) {
26361 return s->fd;
26362 }
26363
26364
26365
26366 if (!(ca = ao2_alloc(sizeof(*ca), sip_tcptls_client_args_destructor)) ||
26367 !(ca->name = ast_strdup(name))) {
26368 goto create_tcptls_session_fail;
26369 }
26370 ca->accept_fd = -1;
26371 ast_sockaddr_copy(&ca->remote_address,sip_real_dst(p));
26372
26373 if (s->type == SIP_TRANSPORT_TLS) {
26374 if (!(ca->tls_cfg = ast_calloc(1, sizeof(*ca->tls_cfg)))) {
26375 goto create_tcptls_session_fail;
26376 }
26377 memcpy(ca->tls_cfg, &default_tls_cfg, sizeof(*ca->tls_cfg));
26378
26379 if (!(ca->tls_cfg->certfile = ast_strdup(default_tls_cfg.certfile)) ||
26380 !(ca->tls_cfg->pvtfile = ast_strdup(default_tls_cfg.pvtfile)) ||
26381 !(ca->tls_cfg->cipher = ast_strdup(default_tls_cfg.cipher)) ||
26382 !(ca->tls_cfg->cafile = ast_strdup(default_tls_cfg.cafile)) ||
26383 !(ca->tls_cfg->capath = ast_strdup(default_tls_cfg.capath))) {
26384
26385 goto create_tcptls_session_fail;
26386 }
26387
26388
26389 if (!ast_strlen_zero(p->tohost)) {
26390 ast_copy_string(ca->hostname, p->tohost, sizeof(ca->hostname));
26391 }
26392 }
26393
26394
26395 if (!(s->tcptls_session = ast_tcptls_client_create(ca))) {
26396 goto create_tcptls_session_fail;
26397 }
26398
26399 s->fd = s->tcptls_session->fd;
26400
26401
26402
26403
26404
26405 if (!(th = sip_threadinfo_create(s->tcptls_session, s->type))) {
26406 goto create_tcptls_session_fail;
26407 }
26408
26409
26410 ao2_ref(s->tcptls_session, +1);
26411
26412 if (ast_pthread_create_detached_background(&launched, NULL, sip_tcp_worker_fn, s->tcptls_session)) {
26413 ast_debug(1, "Unable to launch '%s'.", ca->name);
26414 ao2_ref(s->tcptls_session, -1);
26415 goto create_tcptls_session_fail;
26416 }
26417
26418 return s->fd;
26419
26420 create_tcptls_session_fail:
26421 if (ca) {
26422 ao2_t_ref(ca, -1, "failed to create client, getting rid of client tcptls_session arguments");
26423 }
26424 if (s->tcptls_session) {
26425 ast_tcptls_close_session_file(s->tcptls_session);
26426 s->fd = -1;
26427 ao2_ref(s->tcptls_session, -1);
26428 s->tcptls_session = NULL;
26429 }
26430 if (th) {
26431 ao2_t_unlink(threadt, th, "Removing tcptls thread info object, thread failed to open");
26432 }
26433
26434 return -1;
26435 }
26436
26437
26438
26439
26440
26441 static int get_cached_mwi(struct sip_peer *peer, int *new, int *old)
26442 {
26443 struct sip_mailbox *mailbox;
26444 int in_cache;
26445
26446 in_cache = 0;
26447 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
26448 struct ast_event *event;
26449 event = ast_event_get_cached(AST_EVENT_MWI,
26450 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
26451 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
26452 AST_EVENT_IE_END);
26453 if (!event)
26454 continue;
26455 *new += ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
26456 *old += ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
26457 ast_event_destroy(event);
26458 in_cache = 1;
26459 }
26460
26461 return in_cache;
26462 }
26463
26464
26465
26466
26467 static int sip_send_mwi_to_peer(struct sip_peer *peer, int cache_only)
26468 {
26469
26470 struct sip_pvt *p;
26471 int newmsgs = 0, oldmsgs = 0;
26472 const char *vmexten = NULL;
26473
26474 ao2_lock(peer);
26475
26476 if (peer->vmexten) {
26477 vmexten = ast_strdupa(peer->vmexten);
26478 }
26479
26480 if (ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY) && !peer->mwipvt) {
26481 update_peer_lastmsgssent(peer, -1, 1);
26482 ao2_unlock(peer);
26483 return 0;
26484 }
26485
26486
26487 if (ast_sockaddr_isnull(&peer->addr) && ast_sockaddr_isnull(&peer->defaddr)) {
26488 update_peer_lastmsgssent(peer, -1, 1);
26489 ao2_unlock(peer);
26490 return 0;
26491 }
26492
26493
26494 if (!get_cached_mwi(peer, &newmsgs, &oldmsgs) && !cache_only) {
26495
26496 struct ast_str *mailbox_str = ast_str_alloca(512);
26497 peer_mailboxes_to_str(&mailbox_str, peer);
26498 ao2_unlock(peer);
26499
26500 if (!ast_str_strlen(mailbox_str)) {
26501 update_peer_lastmsgssent(peer, -1, 0);
26502 return 0;
26503 }
26504 ast_app_inboxcount(ast_str_buffer(mailbox_str), &newmsgs, &oldmsgs);
26505 ao2_lock(peer);
26506 }
26507
26508 if (peer->mwipvt) {
26509
26510 p = dialog_ref(peer->mwipvt, "sip_send_mwi_to_peer: Setting dialog ptr p from peer->mwipvt");
26511 ao2_unlock(peer);
26512 } else {
26513 ao2_unlock(peer);
26514
26515 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
26516 update_peer_lastmsgssent(peer, -1, 0);
26517 return -1;
26518 }
26519
26520
26521
26522
26523
26524 set_socket_transport(&p->socket, 0);
26525 if (create_addr_from_peer(p, peer)) {
26526
26527 dialog_unlink_all(p);
26528 dialog_unref(p, "unref dialog p just created via sip_alloc");
26529 update_peer_lastmsgssent(peer, -1, 0);
26530 return 0;
26531 }
26532
26533 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
26534 build_via(p);
26535
26536 ao2_lock(peer);
26537 if (!ast_strlen_zero(peer->mwi_from)) {
26538 ast_string_field_set(p, mwi_from, peer->mwi_from);
26539 } else if (!ast_strlen_zero(default_mwi_from)) {
26540 ast_string_field_set(p, mwi_from, default_mwi_from);
26541 }
26542 ao2_unlock(peer);
26543
26544
26545 change_callid_pvt(p, NULL);
26546
26547
26548 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
26549 }
26550
26551
26552
26553 sip_pvt_lock(p);
26554
26555
26556 ast_set_flag(&p->flags[0], SIP_OUTGOING);
26557
26558 transmit_notify_with_mwi(p, newmsgs, oldmsgs, vmexten);
26559 sip_pvt_unlock(p);
26560 dialog_unref(p, "unref dialog ptr p just before it goes out of scope at the end of sip_send_mwi_to_peer.");
26561
26562 update_peer_lastmsgssent(peer, ((newmsgs > 0x7fff ? 0x7fff0000 : (newmsgs << 16)) | (oldmsgs > 0xffff ? 0xffff : oldmsgs)), 0);
26563
26564 return 0;
26565 }
26566
26567
26568 static void check_rtp_timeout(struct sip_pvt *dialog, time_t t)
26569 {
26570
26571 if (!dialog->rtp || !dialog->owner)
26572 return;
26573
26574
26575 if (dialog->owner->_state != AST_STATE_UP || !ast_sockaddr_isnull(&dialog->redirip))
26576 return;
26577
26578
26579 if (dialog->t38.state == T38_ENABLED)
26580 return;
26581
26582
26583 if (!ast_rtp_instance_get_keepalive(dialog->rtp) && !ast_rtp_instance_get_timeout(dialog->rtp) && !ast_rtp_instance_get_hold_timeout(dialog->rtp)) {
26584 return;
26585 }
26586
26587
26588 if (dialog->lastrtptx && ast_rtp_instance_get_keepalive(dialog->rtp) &&
26589 (t > dialog->lastrtptx + ast_rtp_instance_get_keepalive(dialog->rtp))) {
26590
26591 dialog->lastrtptx = time(NULL);
26592 ast_rtp_instance_sendcng(dialog->rtp, 0);
26593 }
26594
26595
26596
26597
26598
26599
26600
26601
26602 if (dialog->lastrtprx && (ast_rtp_instance_get_timeout(dialog->rtp) || ast_rtp_instance_get_hold_timeout(dialog->rtp)) && (t > dialog->lastrtprx + ast_rtp_instance_get_timeout(dialog->rtp))) {
26603 if (!ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD) || (ast_rtp_instance_get_hold_timeout(dialog->rtp) && (t > dialog->lastrtprx + ast_rtp_instance_get_hold_timeout(dialog->rtp)))) {
26604
26605 if (ast_rtp_instance_get_timeout(dialog->rtp)) {
26606 if (!dialog->owner || ast_channel_trylock(dialog->owner)) {
26607
26608
26609
26610
26611 return;
26612 }
26613 ast_log(LOG_NOTICE, "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
26614 dialog->owner->name, (long) (t - dialog->lastrtprx));
26615
26616 ast_softhangup_nolock(dialog->owner, AST_SOFTHANGUP_DEV);
26617 ast_channel_unlock(dialog->owner);
26618
26619
26620
26621
26622 ast_rtp_instance_set_timeout(dialog->rtp, 0);
26623 ast_rtp_instance_set_hold_timeout(dialog->rtp, 0);
26624 if (dialog->vrtp) {
26625 ast_rtp_instance_set_timeout(dialog->vrtp, 0);
26626 ast_rtp_instance_set_hold_timeout(dialog->vrtp, 0);
26627 }
26628 }
26629 }
26630 }
26631 }
26632
26633
26634
26635
26636
26637 static void *do_monitor(void *data)
26638 {
26639 int res;
26640 time_t t;
26641 int reloading;
26642
26643
26644 if (sipsock > -1)
26645 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
26646
26647
26648 for(;;) {
26649
26650 ast_mutex_lock(&sip_reload_lock);
26651 reloading = sip_reloading;
26652 sip_reloading = FALSE;
26653 ast_mutex_unlock(&sip_reload_lock);
26654 if (reloading) {
26655 ast_verb(1, "Reloading SIP\n");
26656 sip_do_reload(sip_reloadreason);
26657
26658
26659 if (sipsock > -1) {
26660 if (sipsock_read_id)
26661 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
26662 else
26663 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
26664 } else if (sipsock_read_id) {
26665 ast_io_remove(io, sipsock_read_id);
26666 sipsock_read_id = NULL;
26667 }
26668 }
26669
26670
26671 t = time(NULL);
26672
26673
26674
26675
26676
26677
26678
26679
26680
26681
26682 ao2_t_callback(dialogs, OBJ_NODATA | OBJ_MULTIPLE, dialog_needdestroy, &t,
26683 "callback to monitor dialog status");
26684 if (ao2_container_count(dialogs_to_destroy)) {
26685
26686 ao2_t_callback(dialogs_to_destroy, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE,
26687 dialog_unlink_callback, NULL, "callback to dialog_unlink_all");
26688 }
26689
26690
26691
26692
26693
26694
26695
26696 pthread_testcancel();
26697
26698 res = ast_sched_wait(sched);
26699 if ((res < 0) || (res > 1000))
26700 res = 1000;
26701 res = ast_io_wait(io, res);
26702 if (res > 20)
26703 ast_debug(1, "chan_sip: ast_io_wait ran %d all at once\n", res);
26704 ast_mutex_lock(&monlock);
26705 res = ast_sched_runq(sched);
26706 if (res >= 20)
26707 ast_debug(1, "chan_sip: ast_sched_runq ran %d all at once\n", res);
26708 if (global_store_sip_cause && res >= 100)
26709 ast_log(LOG_WARNING, "scheduler delays detected, setting 'storesipcause' to 'no' in %s will improve performance\n", config);
26710 ast_mutex_unlock(&monlock);
26711 }
26712
26713
26714 return NULL;
26715 }
26716
26717
26718 static int restart_monitor(void)
26719 {
26720
26721 if (monitor_thread == AST_PTHREADT_STOP)
26722 return 0;
26723 ast_mutex_lock(&monlock);
26724 if (monitor_thread == pthread_self()) {
26725 ast_mutex_unlock(&monlock);
26726 ast_log(LOG_WARNING, "Cannot kill myself\n");
26727 return -1;
26728 }
26729 if (monitor_thread != AST_PTHREADT_NULL) {
26730
26731 pthread_kill(monitor_thread, SIGURG);
26732 } else {
26733
26734 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
26735 ast_mutex_unlock(&monlock);
26736 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
26737 return -1;
26738 }
26739 }
26740 ast_mutex_unlock(&monlock);
26741 return 0;
26742 }
26743
26744
26745
26746 static void restart_session_timer(struct sip_pvt *p)
26747 {
26748 if (p->stimer->st_active == TRUE) {
26749 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
26750 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
26751 dialog_unref(p, "Removing session timer ref"));
26752 start_session_timer(p);
26753 }
26754 }
26755
26756
26757
26758 static void stop_session_timer(struct sip_pvt *p)
26759 {
26760 if (p->stimer->st_active == TRUE) {
26761 p->stimer->st_active = FALSE;
26762 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
26763 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
26764 dialog_unref(p, "removing session timer ref"));
26765 }
26766 }
26767
26768
26769
26770 static void start_session_timer(struct sip_pvt *p)
26771 {
26772 unsigned int timeout_ms;
26773
26774 if (p->stimer->st_schedid > -1) {
26775
26776 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
26777 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
26778 dialog_unref(p, "unref stimer->st_schedid from dialog"));
26779 }
26780
26781
26782
26783
26784
26785
26786
26787
26788
26789
26790 timeout_ms = (1000 * p->stimer->st_interval);
26791 if (p->stimer->st_ref == SESSION_TIMER_REFRESHER_US) {
26792 timeout_ms /= 2;
26793 } else {
26794 timeout_ms -= MIN(timeout_ms / 3, 32000);
26795 }
26796
26797 p->stimer->st_schedid = ast_sched_add(sched, timeout_ms, proc_session_timer,
26798 dialog_ref(p, "adding session timer ref"));
26799
26800 if (p->stimer->st_schedid < 0) {
26801 dialog_unref(p, "removing session timer ref");
26802 ast_log(LOG_ERROR, "ast_sched_add failed - %s\n", p->callid);
26803 } else {
26804 p->stimer->st_active = TRUE;
26805 ast_debug(2, "Session timer started: %d - %s %ums\n", p->stimer->st_schedid, p->callid, timeout_ms);
26806 }
26807 }
26808
26809
26810
26811 static int proc_session_timer(const void *vp)
26812 {
26813 struct sip_pvt *p = (struct sip_pvt *) vp;
26814 int res = 0;
26815
26816 if (!p->stimer) {
26817 ast_log(LOG_WARNING, "Null stimer in proc_session_timer - %s\n", p->callid);
26818 goto return_unref;
26819 }
26820
26821 ast_debug(2, "Session timer expired: %d - %s\n", p->stimer->st_schedid, p->callid);
26822
26823 if (!p->owner) {
26824 goto return_unref;
26825 }
26826
26827 if ((p->stimer->st_active != TRUE) || (p->owner->_state != AST_STATE_UP)) {
26828 goto return_unref;
26829 }
26830
26831 if (p->stimer->st_ref == SESSION_TIMER_REFRESHER_US) {
26832 res = 1;
26833 if (T38_ENABLED == p->t38.state) {
26834 transmit_reinvite_with_sdp(p, TRUE, TRUE);
26835 } else {
26836 transmit_reinvite_with_sdp(p, FALSE, TRUE);
26837 }
26838 } else {
26839 if (p->stimer->quit_flag) {
26840 goto return_unref;
26841 }
26842 ast_log(LOG_WARNING, "Session-Timer expired - %s\n", p->callid);
26843 sip_pvt_lock(p);
26844 while (p->owner && ast_channel_trylock(p->owner)) {
26845 sip_pvt_unlock(p);
26846 usleep(1);
26847 if (p->stimer && p->stimer->quit_flag) {
26848 goto return_unref;
26849 }
26850 sip_pvt_lock(p);
26851 }
26852
26853 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
26854 ast_channel_unlock(p->owner);
26855 sip_pvt_unlock(p);
26856 }
26857
26858 return_unref:
26859 if (!res) {
26860
26861 if (p->stimer) {
26862 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
26863
26864
26865 p->stimer->st_schedid = -1;
26866
26867
26868 stop_session_timer(p);
26869 }
26870
26871
26872
26873 dialog_unref(p, "removing session timer ref");
26874 }
26875
26876 return res;
26877 }
26878
26879
26880
26881 int parse_minse (const char *p_hdrval, int *const p_interval)
26882 {
26883 if (ast_strlen_zero(p_hdrval)) {
26884 ast_log(LOG_WARNING, "Null Min-SE header\n");
26885 return -1;
26886 }
26887
26888 *p_interval = 0;
26889 p_hdrval = ast_skip_blanks(p_hdrval);
26890 if (!sscanf(p_hdrval, "%30d", p_interval)) {
26891 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
26892 return -1;
26893 }
26894
26895 ast_debug(2, "Received Min-SE: %d\n", *p_interval);
26896 return 0;
26897 }
26898
26899
26900
26901 int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher_param *const p_ref)
26902 {
26903 char *p_token;
26904 int ref_idx;
26905 char *p_se_hdr;
26906
26907 if (ast_strlen_zero(p_hdrval)) {
26908 ast_log(LOG_WARNING, "Null Session-Expires header\n");
26909 return -1;
26910 }
26911
26912 *p_ref = SESSION_TIMER_REFRESHER_PARAM_UNKNOWN;
26913 *p_interval = 0;
26914
26915 p_se_hdr = ast_strdupa(p_hdrval);
26916 p_se_hdr = ast_skip_blanks(p_se_hdr);
26917
26918 while ((p_token = strsep(&p_se_hdr, ";"))) {
26919 p_token = ast_skip_blanks(p_token);
26920 if (!sscanf(p_token, "%30d", p_interval)) {
26921 ast_log(LOG_WARNING, "Parsing of Session-Expires failed\n");
26922 return -1;
26923 }
26924
26925 ast_debug(2, "Session-Expires: %d\n", *p_interval);
26926
26927 if (!p_se_hdr)
26928 continue;
26929
26930 p_se_hdr = ast_skip_blanks(p_se_hdr);
26931 ref_idx = strlen("refresher=");
26932 if (!strncasecmp(p_se_hdr, "refresher=", ref_idx)) {
26933 p_se_hdr += ref_idx;
26934 p_se_hdr = ast_skip_blanks(p_se_hdr);
26935
26936 if (!strncasecmp(p_se_hdr, "uac", strlen("uac"))) {
26937 *p_ref = SESSION_TIMER_REFRESHER_PARAM_UAC;
26938 ast_debug(2, "Refresher: UAC\n");
26939 } else if (!strncasecmp(p_se_hdr, "uas", strlen("uas"))) {
26940 *p_ref = SESSION_TIMER_REFRESHER_PARAM_UAS;
26941 ast_debug(2, "Refresher: UAS\n");
26942 } else {
26943 ast_log(LOG_WARNING, "Invalid refresher value %s\n", p_se_hdr);
26944 return -1;
26945 }
26946 break;
26947 }
26948 }
26949 return 0;
26950 }
26951
26952
26953
26954
26955
26956
26957
26958
26959
26960 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp)
26961 {
26962 int rtn;
26963 const char *p_hdrval;
26964 int minse;
26965
26966 p_hdrval = get_header(rsp, "Min-SE");
26967 if (ast_strlen_zero(p_hdrval)) {
26968 ast_log(LOG_WARNING, "422 response without a Min-SE header %s\n", p_hdrval);
26969 return;
26970 }
26971 rtn = parse_minse(p_hdrval, &minse);
26972 if (rtn != 0) {
26973 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
26974 return;
26975 }
26976 p->stimer->st_cached_min_se = minse;
26977 if (p->stimer->st_interval < minse) {
26978 p->stimer->st_interval = minse;
26979 }
26980 transmit_invite(p, SIP_INVITE, 1, 2, NULL);
26981 }
26982
26983
26984
26985
26986
26987
26988 int st_get_se(struct sip_pvt *p, int max)
26989 {
26990 if (max == TRUE) {
26991 if (p->stimer->st_cached_max_se) {
26992 return p->stimer->st_cached_max_se;
26993 }
26994 if (p->relatedpeer) {
26995 p->stimer->st_cached_max_se = p->relatedpeer->stimer.st_max_se;
26996 return (p->stimer->st_cached_max_se);
26997 }
26998 p->stimer->st_cached_max_se = global_max_se;
26999 return (p->stimer->st_cached_max_se);
27000 }
27001
27002 if (p->stimer->st_cached_min_se) {
27003 return p->stimer->st_cached_min_se;
27004 }
27005 if (p->relatedpeer) {
27006 p->stimer->st_cached_min_se = p->relatedpeer->stimer.st_min_se;
27007 return (p->stimer->st_cached_min_se);
27008 }
27009 p->stimer->st_cached_min_se = global_min_se;
27010 return (p->stimer->st_cached_min_se);
27011 }
27012
27013
27014
27015
27016
27017
27018
27019
27020 enum st_refresher st_get_refresher(struct sip_pvt *p)
27021 {
27022 if (p->stimer->st_cached_ref != SESSION_TIMER_REFRESHER_AUTO) {
27023 return p->stimer->st_cached_ref;
27024 }
27025
27026 if (p->relatedpeer) {
27027 p->stimer->st_cached_ref = (p->relatedpeer->stimer.st_ref == SESSION_TIMER_REFRESHER_PARAM_UAC) ? SESSION_TIMER_REFRESHER_THEM : SESSION_TIMER_REFRESHER_US;
27028 return p->stimer->st_cached_ref;
27029 }
27030
27031 p->stimer->st_cached_ref = (global_st_refresher == SESSION_TIMER_REFRESHER_PARAM_UAC) ? SESSION_TIMER_REFRESHER_THEM : SESSION_TIMER_REFRESHER_US;
27032 return p->stimer->st_cached_ref;
27033 }
27034
27035
27036
27037
27038
27039
27040
27041
27042 enum st_mode st_get_mode(struct sip_pvt *p, int no_cached)
27043 {
27044 if (!p->stimer) {
27045 sip_st_alloc(p);
27046 if (!p->stimer) {
27047 return SESSION_TIMER_MODE_INVALID;
27048 }
27049 }
27050
27051 if (!no_cached && p->stimer->st_cached_mode != SESSION_TIMER_MODE_INVALID)
27052 return p->stimer->st_cached_mode;
27053
27054 if (p->relatedpeer) {
27055 p->stimer->st_cached_mode = p->relatedpeer->stimer.st_mode_oper;
27056 return p->stimer->st_cached_mode;
27057 }
27058
27059 p->stimer->st_cached_mode = global_st_mode;
27060 return global_st_mode;
27061 }
27062
27063
27064
27065 static int sip_poke_noanswer(const void *data)
27066 {
27067 struct sip_peer *peer = (struct sip_peer *)data;
27068
27069 peer->pokeexpire = -1;
27070
27071 if (peer->lastms > -1) {
27072 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
27073 if (sip_cfg.peer_rtupdate) {
27074 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", "-1", SENTINEL);
27075 }
27076 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
27077 if (sip_cfg.regextenonqualify) {
27078 register_peer_exten(peer, FALSE);
27079 }
27080 }
27081
27082 if (peer->call) {
27083 dialog_unlink_all(peer->call);
27084 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
27085
27086 }
27087
27088
27089 if (peer->lastms > -1) {
27090 peer->lastms = -1;
27091 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "SIP/%s", peer->name);
27092 }
27093
27094
27095 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched,
27096 DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer,
27097 unref_peer(_data, "removing poke peer ref"),
27098 unref_peer(peer, "removing poke peer ref"),
27099 ref_peer(peer, "adding poke peer ref"));
27100
27101
27102 unref_peer(peer, "release peer poke noanswer ref");
27103
27104 return 0;
27105 }
27106
27107
27108
27109
27110
27111
27112
27113
27114
27115 static int sip_poke_peer(struct sip_peer *peer, int force)
27116 {
27117 struct sip_pvt *p;
27118 int xmitres = 0;
27119
27120 if ((!peer->maxms && !force) || ast_sockaddr_isnull(&peer->addr)) {
27121
27122
27123 AST_SCHED_DEL_UNREF(sched, peer->pokeexpire,
27124 unref_peer(peer, "removing poke peer ref"));
27125
27126 peer->lastms = 0;
27127 if (peer->call) {
27128 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
27129 }
27130 return 0;
27131 }
27132 if (peer->call) {
27133 if (sipdebug) {
27134 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
27135 }
27136 dialog_unlink_all(peer->call);
27137 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
27138
27139 }
27140 if (!(p = sip_alloc(NULL, NULL, 0, SIP_OPTIONS, NULL))) {
27141 return -1;
27142 }
27143 peer->call = dialog_ref(p, "copy sip alloc from p to peer->call");
27144
27145 p->sa = peer->addr;
27146 p->recv = peer->addr;
27147 copy_socket_data(&p->socket, &peer->socket);
27148 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
27149 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
27150 ast_copy_flags(&p->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
27151
27152
27153 ref_proxy(p, obproxy_get(p, peer));
27154
27155
27156 if (!ast_strlen_zero(peer->fullcontact)) {
27157 ast_string_field_set(p, fullcontact, peer->fullcontact);
27158 }
27159
27160 if (!ast_strlen_zero(peer->fromuser)) {
27161 ast_string_field_set(p, fromuser, peer->fromuser);
27162 }
27163
27164 if (!ast_strlen_zero(peer->tohost)) {
27165 ast_string_field_set(p, tohost, peer->tohost);
27166 } else {
27167 ast_string_field_set(p, tohost, ast_sockaddr_stringify_host_remote(&peer->addr));
27168 }
27169
27170
27171 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
27172 build_via(p);
27173
27174
27175 change_callid_pvt(p, NULL);
27176
27177 AST_SCHED_DEL_UNREF(sched, peer->pokeexpire,
27178 unref_peer(peer, "removing poke peer ref"));
27179
27180 if (p->relatedpeer)
27181 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting the relatedpeer field in the dialog, before it is set to something else.");
27182 p->relatedpeer = ref_peer(peer, "setting the relatedpeer field in the dialog");
27183 ast_set_flag(&p->flags[0], SIP_OUTGOING);
27184 #ifdef VOCAL_DATA_HACK
27185 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
27186 xmitres = transmit_invite(p, SIP_INVITE, 0, 2, NULL);
27187 #else
27188 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2, NULL);
27189 #endif
27190 peer->ps = ast_tvnow();
27191 if (xmitres == XMIT_ERROR) {
27192
27193 sip_poke_noanswer(ref_peer(peer, "add ref for peerexpire (fake, for sip_poke_noanswer to remove)"));
27194 } else if (!force) {
27195 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, peer->maxms * 2, sip_poke_noanswer, peer,
27196 unref_peer(_data, "removing poke peer ref"),
27197 unref_peer(peer, "removing poke peer ref"),
27198 ref_peer(peer, "adding poke peer ref"));
27199 }
27200 dialog_unref(p, "unref dialog at end of sip_poke_peer, obtained from sip_alloc, just before it goes out of scope");
27201 return 0;
27202 }
27203
27204
27205
27206
27207
27208
27209
27210
27211
27212
27213
27214
27215
27216
27217
27218
27219
27220
27221
27222
27223
27224
27225
27226
27227
27228
27229
27230
27231
27232
27233
27234
27235
27236
27237 static int sip_devicestate(void *data)
27238 {
27239 char *host;
27240 char *tmp;
27241 struct sip_peer *p;
27242
27243 int res = AST_DEVICE_INVALID;
27244
27245
27246 host = ast_strdupa(data ? data : "");
27247 if ((tmp = strchr(host, '@')))
27248 host = tmp + 1;
27249
27250 ast_debug(3, "Checking device state for peer %s\n", host);
27251
27252
27253
27254
27255
27256
27257
27258
27259 if ((p = find_peer(host, NULL, FALSE, FINDALLDEVICES, TRUE, 0))) {
27260 if (!(ast_sockaddr_isnull(&p->addr) && ast_sockaddr_isnull(&p->defaddr))) {
27261
27262
27263
27264
27265
27266
27267
27268
27269
27270
27271
27272 if (p->onHold)
27273
27274 res = AST_DEVICE_ONHOLD;
27275 else if (p->inRinging) {
27276 if (p->inRinging == p->inUse)
27277 res = AST_DEVICE_RINGING;
27278 else
27279 res = AST_DEVICE_RINGINUSE;
27280 } else if (p->call_limit && (p->inUse == p->call_limit))
27281
27282 res = AST_DEVICE_BUSY;
27283 else if (p->call_limit && p->busy_level && p->inUse >= p->busy_level)
27284
27285 res = AST_DEVICE_BUSY;
27286 else if (p->call_limit && p->inUse)
27287
27288 res = AST_DEVICE_INUSE;
27289 else if (p->maxms && ((p->lastms > p->maxms) || (p->lastms < 0)))
27290
27291 res = AST_DEVICE_UNAVAILABLE;
27292 else
27293 res = AST_DEVICE_NOT_INUSE;
27294 } else {
27295
27296 res = AST_DEVICE_UNAVAILABLE;
27297 }
27298 unref_peer(p, "unref_peer, from sip_devicestate, release ref from find_peer");
27299 }
27300
27301 return res;
27302 }
27303
27304
27305
27306
27307
27308
27309
27310
27311
27312
27313
27314
27315
27316
27317
27318
27319 static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
27320 {
27321 struct sip_pvt *p;
27322 struct ast_channel *tmpc = NULL;
27323 char *ext = NULL, *host;
27324 char tmp[256];
27325 char *dest = data;
27326 char *dnid;
27327 char *secret = NULL;
27328 char *md5secret = NULL;
27329 char *authname = NULL;
27330 char *trans = NULL;
27331 char dialstring[256];
27332 char *remote_address;
27333 enum sip_transport transport = 0;
27334 format_t oldformat = format;
27335 AST_DECLARE_APP_ARGS(args,
27336 AST_APP_ARG(peerorhost);
27337 AST_APP_ARG(exten);
27338 AST_APP_ARG(remote_address);
27339 );
27340
27341
27342
27343
27344
27345
27346
27347
27348 format &= AST_FORMAT_AUDIO_MASK;
27349 if (!format) {
27350 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format %s while capability is %s\n", ast_getformatname(oldformat), ast_getformatname(sip_cfg.capability));
27351 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
27352 return NULL;
27353 }
27354 ast_debug(1, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
27355
27356 if (ast_strlen_zero(dest)) {
27357 ast_log(LOG_ERROR, "Unable to create channel with empty destination.\n");
27358 *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
27359 return NULL;
27360 }
27361
27362 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE, NULL))) {
27363 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", dest);
27364 *cause = AST_CAUSE_SWITCH_CONGESTION;
27365 return NULL;
27366 }
27367
27368 p->outgoing_call = TRUE;
27369
27370 snprintf(dialstring, sizeof(dialstring), "%s/%s", type, dest);
27371 ast_string_field_set(p, dialstring, dialstring);
27372
27373 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
27374 dialog_unlink_all(p);
27375 dialog_unref(p, "unref dialog p from mem fail");
27376
27377 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
27378 *cause = AST_CAUSE_SWITCH_CONGESTION;
27379 return NULL;
27380 }
27381
27382
27383 ast_copy_string(tmp, dest, sizeof(tmp));
27384
27385
27386 dnid = strchr(tmp, '!');
27387 if (dnid != NULL) {
27388 *dnid++ = '\0';
27389 ast_string_field_set(p, todnid, dnid);
27390 }
27391
27392
27393 AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
27394
27395
27396 host = strchr(args.peerorhost, '@');
27397 if (host) {
27398 *host++ = '\0';
27399 ext = args.peerorhost;
27400 secret = strchr(ext, ':');
27401 }
27402 if (secret) {
27403 *secret++ = '\0';
27404 md5secret = strchr(secret, ':');
27405 }
27406 if (md5secret) {
27407 *md5secret++ = '\0';
27408 authname = strchr(md5secret, ':');
27409 }
27410 if (authname) {
27411 *authname++ = '\0';
27412 trans = strchr(authname, ':');
27413 }
27414 if (trans) {
27415 *trans++ = '\0';
27416 if (!strcasecmp(trans, "tcp"))
27417 transport = SIP_TRANSPORT_TCP;
27418 else if (!strcasecmp(trans, "tls"))
27419 transport = SIP_TRANSPORT_TLS;
27420 else {
27421 if (strcasecmp(trans, "udp"))
27422 ast_log(LOG_WARNING, "'%s' is not a valid transport option to Dial() for SIP calls, using udp by default.\n", trans);
27423 transport = SIP_TRANSPORT_UDP;
27424 }
27425 } else {
27426 transport = SIP_TRANSPORT_UDP;
27427 }
27428
27429 if (!host) {
27430 ext = args.exten;
27431 host = args.peerorhost;
27432 remote_address = args.remote_address;
27433 } else {
27434 remote_address = args.remote_address;
27435 if (!ast_strlen_zero(args.exten)) {
27436 ast_log(LOG_NOTICE, "Conflicting extension values given. Using '%s' and not '%s'\n", ext, args.exten);
27437 }
27438 }
27439
27440 if (!ast_strlen_zero(remote_address)) {
27441 p->options->outboundproxy = proxy_from_config(remote_address, 0, NULL);
27442 if (!p->options->outboundproxy) {
27443 ast_log(LOG_WARNING, "Unable to parse outboundproxy %s. We will not use this remote IP address\n", remote_address);
27444 }
27445 }
27446
27447 set_socket_transport(&p->socket, transport);
27448
27449
27450
27451
27452
27453
27454 if (create_addr(p, host, NULL, 1)) {
27455 *cause = AST_CAUSE_UNREGISTERED;
27456 ast_debug(3, "Cant create SIP call - target device not registered\n");
27457 dialog_unlink_all(p);
27458 dialog_unref(p, "unref dialog p UNREGISTERED");
27459
27460 return NULL;
27461 }
27462 if (ast_strlen_zero(p->peername) && ext)
27463 ast_string_field_set(p, peername, ext);
27464
27465 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
27466 build_via(p);
27467
27468
27469 change_callid_pvt(p, NULL);
27470
27471
27472
27473
27474
27475
27476 if (ext) {
27477 ast_string_field_set(p, username, ext);
27478 ast_string_field_set(p, fullcontact, NULL);
27479 }
27480 if (secret && !ast_strlen_zero(secret))
27481 ast_string_field_set(p, peersecret, secret);
27482
27483 if (md5secret && !ast_strlen_zero(md5secret))
27484 ast_string_field_set(p, peermd5secret, md5secret);
27485
27486 if (authname && !ast_strlen_zero(authname))
27487 ast_string_field_set(p, authname, authname);
27488 #if 0
27489 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
27490 #endif
27491 p->prefcodec = oldformat;
27492 p->jointcapability = oldformat & p->capability;
27493 sip_pvt_lock(p);
27494 tmpc = sip_new(p, AST_STATE_DOWN, host, requestor ? requestor->linkedid : NULL);
27495 if (sip_cfg.callevents)
27496 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
27497 "Channel: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
27498 p->owner ? p->owner->name : "", "SIP", p->callid, p->fullcontact, p->peername);
27499 sip_pvt_unlock(p);
27500 if (!tmpc) {
27501 dialog_unlink_all(p);
27502
27503 } else {
27504 ast_channel_unlock(tmpc);
27505 }
27506 dialog_unref(p, "toss pvt ptr at end of sip_request_call");
27507 ast_update_use_count();
27508 restart_monitor();
27509 return tmpc;
27510 }
27511
27512
27513 static void set_insecure_flags (struct ast_flags *flags, const char *value, int lineno)
27514 {
27515 if (ast_strlen_zero(value))
27516 return;
27517
27518 if (!ast_false(value)) {
27519 char buf[64];
27520 char *word, *next;
27521
27522 ast_copy_string(buf, value, sizeof(buf));
27523 next = buf;
27524 while ((word = strsep(&next, ","))) {
27525 if (!strcasecmp(word, "port"))
27526 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
27527 else if (!strcasecmp(word, "invite"))
27528 ast_set_flag(&flags[0], SIP_INSECURE_INVITE);
27529 else
27530 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
27531 }
27532 }
27533 }
27534
27535
27536
27537
27538
27539 static int handle_t38_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v,
27540 unsigned int *maxdatagram)
27541 {
27542 int res = 1;
27543
27544 if (!strcasecmp(v->name, "t38pt_udptl")) {
27545 char *buf = ast_strdupa(v->value);
27546 char *word, *next = buf;
27547
27548 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT);
27549
27550 while ((word = strsep(&next, ","))) {
27551 if (ast_true(word) || !strcasecmp(word, "fec")) {
27552 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
27553 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL_FEC);
27554 } else if (!strcasecmp(word, "redundancy")) {
27555 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
27556 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY);
27557 } else if (!strcasecmp(word, "none")) {
27558 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
27559 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL);
27560 } else if (!strncasecmp(word, "maxdatagram=", 12)) {
27561 if (sscanf(&word[12], "%30u", maxdatagram) != 1) {
27562 ast_log(LOG_WARNING, "Invalid maxdatagram '%s' at line %d of %s\n", v->value, v->lineno, config);
27563 *maxdatagram = global_t38_maxdatagram;
27564 }
27565 }
27566 }
27567 } else if (!strcasecmp(v->name, "t38pt_usertpsource")) {
27568 ast_set_flag(&mask[1], SIP_PAGE2_UDPTL_DESTINATION);
27569 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_UDPTL_DESTINATION);
27570 } else {
27571 res = 0;
27572 }
27573
27574 return res;
27575 }
27576
27577
27578
27579
27580
27581
27582
27583
27584 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
27585 {
27586 int res = 1;
27587
27588 if (!strcasecmp(v->name, "trustrpid")) {
27589 ast_set_flag(&mask[0], SIP_TRUSTRPID);
27590 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
27591 } else if (!strcasecmp(v->name, "sendrpid")) {
27592 ast_set_flag(&mask[0], SIP_SENDRPID);
27593 if (!strcasecmp(v->value, "pai")) {
27594 ast_set_flag(&flags[0], SIP_SENDRPID_PAI);
27595 } else if (!strcasecmp(v->value, "rpid")) {
27596 ast_set_flag(&flags[0], SIP_SENDRPID_RPID);
27597 } else if (ast_true(v->value)) {
27598 ast_set_flag(&flags[0], SIP_SENDRPID_RPID);
27599 }
27600 } else if (!strcasecmp(v->name, "rpid_update")) {
27601 ast_set_flag(&mask[1], SIP_PAGE2_RPID_UPDATE);
27602 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RPID_UPDATE);
27603 } else if (!strcasecmp(v->name, "rpid_immediate")) {
27604 ast_set_flag(&mask[1], SIP_PAGE2_RPID_IMMEDIATE);
27605 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RPID_IMMEDIATE);
27606 } else if (!strcasecmp(v->name, "trust_id_outbound")) {
27607 ast_set_flag(&mask[1], SIP_PAGE2_TRUST_ID_OUTBOUND);
27608 ast_clear_flag(&flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND);
27609 if (!strcasecmp(v->value, "legacy")) {
27610 ast_set_flag(&flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND_LEGACY);
27611 } else if (ast_true(v->value)) {
27612 ast_set_flag(&flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND_YES);
27613 } else if (ast_false(v->value)) {
27614 ast_set_flag(&flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND_NO);
27615 } else {
27616 ast_log(LOG_WARNING, "Unknown trust_id_outbound mode '%s' on line %d, using legacy\n", v->value, v->lineno);
27617 ast_set_flag(&flags[1], SIP_PAGE2_TRUST_ID_OUTBOUND_LEGACY);
27618 }
27619 } else if (!strcasecmp(v->name, "g726nonstandard")) {
27620 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
27621 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
27622 } else if (!strcasecmp(v->name, "useclientcode")) {
27623 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
27624 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
27625 } else if (!strcasecmp(v->name, "dtmfmode")) {
27626 ast_set_flag(&mask[0], SIP_DTMF);
27627 ast_clear_flag(&flags[0], SIP_DTMF);
27628 if (!strcasecmp(v->value, "inband"))
27629 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
27630 else if (!strcasecmp(v->value, "rfc2833"))
27631 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
27632 else if (!strcasecmp(v->value, "info"))
27633 ast_set_flag(&flags[0], SIP_DTMF_INFO);
27634 else if (!strcasecmp(v->value, "shortinfo"))
27635 ast_set_flag(&flags[0], SIP_DTMF_SHORTINFO);
27636 else if (!strcasecmp(v->value, "auto"))
27637 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
27638 else {
27639 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
27640 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
27641 }
27642 } else if (!strcasecmp(v->name, "nat")) {
27643 ast_set_flag(&mask[0], SIP_NAT_FORCE_RPORT);
27644 ast_set_flag(&mask[1], SIP_PAGE2_SYMMETRICRTP);
27645 if (!strcasecmp(v->value, "yes")) {
27646 ast_set_flag(&flags[0], SIP_NAT_FORCE_RPORT);
27647 ast_set_flag(&flags[1], SIP_PAGE2_SYMMETRICRTP);
27648 } else if (!strcasecmp(v->value, "force_rport")) {
27649 ast_set_flag(&flags[0], SIP_NAT_FORCE_RPORT);
27650 } else if (!strcasecmp(v->value, "comedia")) {
27651 ast_set_flag(&flags[1], SIP_PAGE2_SYMMETRICRTP);
27652 }
27653 } else if (!strcasecmp(v->name, "directmedia") || !strcasecmp(v->name, "canreinvite")) {
27654 ast_set_flag(&mask[0], SIP_REINVITE);
27655 ast_clear_flag(&flags[0], SIP_REINVITE);
27656 if (ast_true(v->value)) {
27657 ast_set_flag(&flags[0], SIP_DIRECT_MEDIA | SIP_DIRECT_MEDIA_NAT);
27658 } else if (!ast_false(v->value)) {
27659 char buf[64];
27660 char *word, *next = buf;
27661
27662 ast_copy_string(buf, v->value, sizeof(buf));
27663 while ((word = strsep(&next, ","))) {
27664 if (!strcasecmp(word, "update")) {
27665 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_DIRECT_MEDIA);
27666 } else if (!strcasecmp(word, "nonat")) {
27667 ast_set_flag(&flags[0], SIP_DIRECT_MEDIA);
27668 ast_clear_flag(&flags[0], SIP_DIRECT_MEDIA_NAT);
27669 } else if (!strcasecmp(word, "outgoing")) {
27670 ast_set_flag(&flags[0], SIP_DIRECT_MEDIA);
27671 ast_set_flag(&mask[2], SIP_PAGE3_DIRECT_MEDIA_OUTGOING);
27672 ast_set_flag(&flags[2], SIP_PAGE3_DIRECT_MEDIA_OUTGOING);
27673 } else {
27674 ast_log(LOG_WARNING, "Unknown directmedia mode '%s' on line %d\n", v->value, v->lineno);
27675 }
27676 }
27677 }
27678 } else if (!strcasecmp(v->name, "insecure")) {
27679 ast_set_flag(&mask[0], SIP_INSECURE);
27680 ast_clear_flag(&flags[0], SIP_INSECURE);
27681 set_insecure_flags(&flags[0], v->value, v->lineno);
27682 } else if (!strcasecmp(v->name, "progressinband")) {
27683 ast_set_flag(&mask[0], SIP_PROG_INBAND);
27684 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
27685 if (ast_true(v->value))
27686 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
27687 else if (strcasecmp(v->value, "never"))
27688 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
27689 } else if (!strcasecmp(v->name, "promiscredir")) {
27690 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
27691 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
27692 } else if (!strcasecmp(v->name, "videosupport")) {
27693 if (!strcasecmp(v->value, "always")) {
27694 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
27695 ast_set_flag(&flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
27696 } else {
27697 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
27698 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
27699 }
27700 } else if (!strcasecmp(v->name, "textsupport")) {
27701 ast_set_flag(&mask[1], SIP_PAGE2_TEXTSUPPORT);
27702 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_TEXTSUPPORT);
27703 res = 1;
27704 } else if (!strcasecmp(v->name, "allowoverlap")) {
27705 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
27706 ast_clear_flag(&flags[1], SIP_PAGE2_ALLOWOVERLAP);
27707 if (ast_true(v->value)) {
27708 ast_set_flag(&flags[1], SIP_PAGE2_ALLOWOVERLAP_YES);
27709 } else if (!strcasecmp(v->value, "dtmf")){
27710 ast_set_flag(&flags[1], SIP_PAGE2_ALLOWOVERLAP_DTMF);
27711 }
27712 } else if (!strcasecmp(v->name, "allowsubscribe")) {
27713 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
27714 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
27715 } else if (!strcasecmp(v->name, "ignoresdpversion")) {
27716 ast_set_flag(&mask[1], SIP_PAGE2_IGNORESDPVERSION);
27717 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_IGNORESDPVERSION);
27718 } else if (!strcasecmp(v->name, "faxdetect")) {
27719 ast_set_flag(&mask[1], SIP_PAGE2_FAX_DETECT);
27720 if (ast_true(v->value)) {
27721 ast_set_flag(&flags[1], SIP_PAGE2_FAX_DETECT_BOTH);
27722 } else if (ast_false(v->value)) {
27723 ast_clear_flag(&flags[1], SIP_PAGE2_FAX_DETECT_BOTH);
27724 } else {
27725 char *buf = ast_strdupa(v->value);
27726 char *word, *next = buf;
27727
27728 while ((word = strsep(&next, ","))) {
27729 if (!strcasecmp(word, "cng")) {
27730 ast_set_flag(&flags[1], SIP_PAGE2_FAX_DETECT_CNG);
27731 } else if (!strcasecmp(word, "t38")) {
27732 ast_set_flag(&flags[1], SIP_PAGE2_FAX_DETECT_T38);
27733 } else {
27734 ast_log(LOG_WARNING, "Unknown faxdetect mode '%s' on line %d.\n", word, v->lineno);
27735 }
27736 }
27737 }
27738 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
27739 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
27740 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
27741 } else if (!strcasecmp(v->name, "buggymwi")) {
27742 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
27743 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
27744 } else
27745 res = 0;
27746
27747 return res;
27748 }
27749
27750
27751 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
27752 {
27753 struct domain *d;
27754
27755 if (ast_strlen_zero(domain)) {
27756 ast_log(LOG_WARNING, "Zero length domain.\n");
27757 return 1;
27758 }
27759
27760 if (!(d = ast_calloc(1, sizeof(*d))))
27761 return 0;
27762
27763 ast_copy_string(d->domain, domain, sizeof(d->domain));
27764
27765 if (!ast_strlen_zero(context))
27766 ast_copy_string(d->context, context, sizeof(d->context));
27767
27768 d->mode = mode;
27769
27770 AST_LIST_LOCK(&domain_list);
27771 AST_LIST_INSERT_TAIL(&domain_list, d, list);
27772 AST_LIST_UNLOCK(&domain_list);
27773
27774 if (sipdebug)
27775 ast_debug(1, "Added local SIP domain '%s'\n", domain);
27776
27777 return 1;
27778 }
27779
27780
27781 static int check_sip_domain(const char *domain, char *context, size_t len)
27782 {
27783 struct domain *d;
27784 int result = 0;
27785
27786 AST_LIST_LOCK(&domain_list);
27787 AST_LIST_TRAVERSE(&domain_list, d, list) {
27788 if (strcasecmp(d->domain, domain)) {
27789 continue;
27790 }
27791
27792 if (len && !ast_strlen_zero(d->context))
27793 ast_copy_string(context, d->context, len);
27794
27795 result = 1;
27796 break;
27797 }
27798 AST_LIST_UNLOCK(&domain_list);
27799
27800 return result;
27801 }
27802
27803
27804 static void clear_sip_domains(void)
27805 {
27806 struct domain *d;
27807
27808 AST_LIST_LOCK(&domain_list);
27809 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
27810 ast_free(d);
27811 AST_LIST_UNLOCK(&domain_list);
27812 }
27813
27814
27815
27816
27817
27818
27819
27820
27821
27822 static void destroy_realm_authentication(void *obj)
27823 {
27824 struct sip_auth_container *credentials = obj;
27825 struct sip_auth *auth;
27826
27827 while ((auth = AST_LIST_REMOVE_HEAD(&credentials->list, node))) {
27828 ast_free(auth);
27829 }
27830 }
27831
27832
27833
27834
27835
27836
27837
27838
27839
27840
27841
27842 static void add_realm_authentication(struct sip_auth_container **credentials, const char *configuration, int lineno)
27843 {
27844 char *authcopy;
27845 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
27846 struct sip_auth *auth;
27847
27848 if (ast_strlen_zero(configuration)) {
27849
27850 return;
27851 }
27852
27853 ast_debug(1, "Auth config :: %s\n", configuration);
27854
27855 authcopy = ast_strdupa(configuration);
27856 username = authcopy;
27857
27858
27859 realm = strrchr(username, '@');
27860 if (realm)
27861 *realm++ = '\0';
27862 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
27863 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
27864 return;
27865 }
27866
27867
27868 if ((secret = strchr(username, ':'))) {
27869 *secret++ = '\0';
27870 } else if ((md5secret = strchr(username, '#'))) {
27871 *md5secret++ = '\0';
27872 }
27873
27874
27875 if (!*credentials) {
27876 *credentials = ao2_t_alloc(sizeof(**credentials), destroy_realm_authentication,
27877 "Create realm auth container.");
27878 if (!*credentials) {
27879
27880 return;
27881 }
27882 }
27883
27884
27885 auth = ast_calloc(1, sizeof(*auth));
27886 if (!auth) {
27887 return;
27888 }
27889 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
27890 ast_copy_string(auth->username, username, sizeof(auth->username));
27891 if (secret)
27892 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
27893 if (md5secret)
27894 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
27895
27896
27897 AST_LIST_INSERT_TAIL(&(*credentials)->list, auth, node);
27898
27899 ast_verb(3, "Added authentication for realm %s\n", realm);
27900 }
27901
27902
27903
27904
27905
27906
27907
27908
27909
27910
27911 static struct sip_auth *find_realm_authentication(struct sip_auth_container *credentials, const char *realm)
27912 {
27913 struct sip_auth *auth;
27914
27915 if (credentials) {
27916 AST_LIST_TRAVERSE(&credentials->list, auth, node) {
27917 if (!strcasecmp(auth->realm, realm)) {
27918 break;
27919 }
27920 }
27921 } else {
27922 auth = NULL;
27923 }
27924
27925 return auth;
27926 }
27927
27928
27929
27930
27931 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
27932 {
27933 struct ast_variable *tmpvar = NULL;
27934 char *varname = ast_strdupa(buf), *varval = NULL;
27935
27936 if ((varval = strchr(varname, '='))) {
27937 *varval++ = '\0';
27938 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
27939 tmpvar->next = list;
27940 list = tmpvar;
27941 }
27942 }
27943 return list;
27944 }
27945
27946
27947 static void set_peer_defaults(struct sip_peer *peer)
27948 {
27949 if (peer->expire == 0) {
27950
27951
27952
27953 peer->expire = -1;
27954 peer->pokeexpire = -1;
27955 set_socket_transport(&peer->socket, SIP_TRANSPORT_UDP);
27956 }
27957 peer->type = SIP_TYPE_PEER;
27958 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
27959 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
27960 ast_copy_flags(&peer->flags[2], &global_flags[2], SIP_PAGE3_FLAGS_TO_COPY);
27961 ast_string_field_set(peer, context, sip_cfg.default_context);
27962 ast_string_field_set(peer, subscribecontext, sip_cfg.default_subscribecontext);
27963 ast_string_field_set(peer, language, default_language);
27964 ast_string_field_set(peer, mohinterpret, default_mohinterpret);
27965 ast_string_field_set(peer, mohsuggest, default_mohsuggest);
27966 ast_string_field_set(peer, engine, default_engine);
27967 ast_sockaddr_setnull(&peer->addr);
27968 ast_sockaddr_setnull(&peer->defaddr);
27969 peer->capability = sip_cfg.capability;
27970 peer->maxcallbitrate = default_maxcallbitrate;
27971 peer->rtptimeout = global_rtptimeout;
27972 peer->rtpholdtimeout = global_rtpholdtimeout;
27973 peer->rtpkeepalive = global_rtpkeepalive;
27974 peer->allowtransfer = sip_cfg.allowtransfer;
27975 peer->autoframing = global_autoframing;
27976 peer->t38_maxdatagram = global_t38_maxdatagram;
27977 peer->qualifyfreq = global_qualifyfreq;
27978 if (global_callcounter)
27979 peer->call_limit=INT_MAX;
27980 ast_string_field_set(peer, vmexten, default_vmexten);
27981 ast_string_field_set(peer, secret, "");
27982 ast_string_field_set(peer, remotesecret, "");
27983 ast_string_field_set(peer, md5secret, "");
27984 ast_string_field_set(peer, cid_num, "");
27985 ast_string_field_set(peer, cid_name, "");
27986 ast_string_field_set(peer, cid_tag, "");
27987 ast_string_field_set(peer, fromdomain, "");
27988 ast_string_field_set(peer, fromuser, "");
27989 ast_string_field_set(peer, regexten, "");
27990 peer->callgroup = 0;
27991 peer->pickupgroup = 0;
27992 peer->maxms = default_qualify;
27993 peer->prefs = default_prefs;
27994 peer->stimer.st_mode_oper = global_st_mode;
27995 peer->stimer.st_ref = global_st_refresher;
27996 peer->stimer.st_min_se = global_min_se;
27997 peer->stimer.st_max_se = global_max_se;
27998 peer->timer_t1 = global_t1;
27999 peer->timer_b = global_timer_b;
28000 clear_peer_mailboxes(peer);
28001 peer->disallowed_methods = sip_cfg.disallowed_methods;
28002 peer->transports = default_transports;
28003 peer->default_outbound_transport = default_primary_transport;
28004 if (peer->outboundproxy) {
28005 ao2_ref(peer->outboundproxy, -1);
28006 peer->outboundproxy = NULL;
28007 }
28008 }
28009
28010
28011 static struct sip_peer *temp_peer(const char *name)
28012 {
28013 struct sip_peer *peer;
28014
28015 if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct")))
28016 return NULL;
28017
28018 if (ast_string_field_init(peer, 512)) {
28019 ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
28020 return NULL;
28021 }
28022
28023 if (!(peer->cc_params = ast_cc_config_params_init())) {
28024 ao2_t_ref(peer, -1, "failed to allocate cc_params for peer");
28025 return NULL;
28026 }
28027
28028 ast_atomic_fetchadd_int(&apeerobjs, 1);
28029 set_peer_defaults(peer);
28030
28031 ast_copy_string(peer->name, name, sizeof(peer->name));
28032
28033 peer->selfdestruct = TRUE;
28034 peer->host_dynamic = TRUE;
28035 peer->prefs = default_prefs;
28036 reg_source_db(peer);
28037
28038 return peer;
28039 }
28040
28041
28042 static void add_peer_mailboxes(struct sip_peer *peer, const char *value)
28043 {
28044 char *next, *mbox, *context;
28045
28046 next = ast_strdupa(value);
28047
28048 while ((mbox = context = strsep(&next, ","))) {
28049 struct sip_mailbox *mailbox;
28050 int duplicate = 0;
28051
28052 mbox = ast_strip(mbox);
28053 strsep(&context, "@");
28054
28055 if (ast_strlen_zero(mbox)) {
28056 continue;
28057 }
28058
28059
28060 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
28061 if (!strcmp(mailbox->mailbox, mbox) && !strcmp(S_OR(mailbox->context, ""), S_OR(context, ""))) {
28062 duplicate = 1;
28063 break;
28064 }
28065 }
28066 if (duplicate) {
28067 continue;
28068 }
28069
28070 if (!(mailbox = ast_calloc(1, sizeof(*mailbox) + strlen(mbox) + strlen(S_OR(context, ""))))) {
28071 continue;
28072 }
28073
28074 if (!ast_strlen_zero(context)) {
28075 mailbox->context = mailbox->mailbox + strlen(mbox) + 1;
28076 strcpy(mailbox->context, context);
28077 }
28078 strcpy(mailbox->mailbox, mbox);
28079
28080 AST_LIST_INSERT_TAIL(&peer->mailboxes, mailbox, entry);
28081 }
28082 }
28083
28084
28085 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only)
28086 {
28087 struct sip_peer *peer = NULL;
28088 struct ast_ha *oldha = NULL;
28089 struct ast_ha *olddirectmediaha = NULL;
28090 int found = 0;
28091 int firstpass = 1;
28092 uint16_t port = 0;
28093 int format = 0;
28094 int timerb_set = 0, timert1_set = 0;
28095 time_t regseconds = 0;
28096 struct ast_flags peerflags[3] = {{(0)}};
28097 struct ast_flags mask[3] = {{(0)}};
28098 char callback[256] = "";
28099 struct sip_peer tmp_peer;
28100 const char *srvlookup = NULL;
28101 static int deprecation_warning = 1;
28102 int alt_fullcontact = alt ? 1 : 0, headercount = 0;
28103 struct ast_str *fullcontact = ast_str_alloca(512);
28104
28105 if (!realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
28106
28107
28108
28109
28110
28111 ast_copy_string(tmp_peer.name, name, sizeof(tmp_peer.name));
28112 peer = ao2_t_find(peers, &tmp_peer, OBJ_POINTER | OBJ_UNLINK, "find and unlink peer from peers table");
28113 }
28114
28115 if (peer) {
28116
28117 found++;
28118
28119
28120 if (!ast_sockaddr_isnull(&peer->addr)) {
28121 ao2_t_unlink(peers_by_ip, peer, "ao2_unlink peer from peers_by_ip table");
28122 }
28123 if (!(peer->the_mark))
28124 firstpass = 0;
28125 } else {
28126 if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct")))
28127 return NULL;
28128
28129 if (ast_string_field_init(peer, 512)) {
28130 ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
28131 return NULL;
28132 }
28133
28134 if (!(peer->cc_params = ast_cc_config_params_init())) {
28135 ao2_t_ref(peer, -1, "failed to allocate cc_params for peer");
28136 return NULL;
28137 }
28138
28139 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
28140 ast_atomic_fetchadd_int(&rpeerobjs, 1);
28141 ast_debug(3, "-REALTIME- peer built. Name: %s. Peer objects: %d\n", name, rpeerobjs);
28142 } else
28143 ast_atomic_fetchadd_int(&speerobjs, 1);
28144 }
28145
28146
28147 if (firstpass) {
28148 peer->lastmsgssent = -1;
28149 oldha = peer->ha;
28150 peer->ha = NULL;
28151 olddirectmediaha = peer->directmediaha;
28152 peer->directmediaha = NULL;
28153 set_peer_defaults(peer);
28154 peer->type = 0;
28155 }
28156
28157
28158 ast_copy_string(peer->name, name, sizeof(peer->name));
28159
28160
28161 if (peer->chanvars) {
28162 ast_variables_destroy(peer->chanvars);
28163 peer->chanvars = NULL;
28164
28165 }
28166
28167 if (found)
28168 peer->portinuri = 0;
28169
28170
28171 ao2_lock(peer);
28172 if (peer->auth) {
28173 ao2_t_ref(peer->auth, -1, "Removing old peer authentication");
28174 peer->auth = NULL;
28175 }
28176 ao2_unlock(peer);
28177
28178
28179 peer->default_outbound_transport = 0;
28180 peer->transports = 0;
28181
28182 if (!devstate_only) {
28183 struct sip_mailbox *mailbox;
28184 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
28185 mailbox->delme = 1;
28186 }
28187 }
28188
28189 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
28190 if (!devstate_only) {
28191 if (handle_common_options(&peerflags[0], &mask[0], v)) {
28192 continue;
28193 }
28194 if (handle_t38_options(&peerflags[0], &mask[0], v, &peer->t38_maxdatagram)) {
28195 continue;
28196 }
28197 if (!strcasecmp(v->name, "transport")) {
28198 char *val = ast_strdupa(v->value);
28199 char *trans;
28200
28201 peer->transports = peer->default_outbound_transport = 0;
28202 while ((trans = strsep(&val, ","))) {
28203 trans = ast_skip_blanks(trans);
28204
28205 if (!strncasecmp(trans, "udp", 3)) {
28206 peer->transports |= SIP_TRANSPORT_UDP;
28207 } else if (sip_cfg.tcp_enabled && !strncasecmp(trans, "tcp", 3)) {
28208 peer->transports |= SIP_TRANSPORT_TCP;
28209 } else if (default_tls_cfg.enabled && !strncasecmp(trans, "tls", 3)) {
28210 peer->transports |= SIP_TRANSPORT_TLS;
28211 } else if (!strncasecmp(trans, "tcp", 3) || !strncasecmp(trans, "tls", 3)) {
28212 ast_log(LOG_WARNING, "'%.3s' is not a valid transport type when %.3senable=no. If no other is specified, the defaults from general will be used.\n", trans, trans);
28213 } else {
28214 ast_log(LOG_NOTICE, "'%s' is not a valid transport type. if no other is specified, the defaults from general will be used.\n", trans);
28215 }
28216
28217 if (!peer->default_outbound_transport) {
28218 peer->default_outbound_transport = peer->transports;
28219 }
28220 }
28221 } else if (realtime && !strcasecmp(v->name, "regseconds")) {
28222 ast_get_time_t(v->value, ®seconds, 0, NULL);
28223 } else if (realtime && !strcasecmp(v->name, "name")) {
28224 ast_copy_string(peer->name, v->value, sizeof(peer->name));
28225 } else if (realtime && !strcasecmp(v->name, "useragent")) {
28226 ast_string_field_set(peer, useragent, v->value);
28227 } else if (!strcasecmp(v->name, "type")) {
28228 if (!strcasecmp(v->value, "peer")) {
28229 peer->type |= SIP_TYPE_PEER;
28230 } else if (!strcasecmp(v->value, "user")) {
28231 peer->type |= SIP_TYPE_USER;
28232 } else if (!strcasecmp(v->value, "friend")) {
28233 peer->type = SIP_TYPE_USER | SIP_TYPE_PEER;
28234 }
28235 } else if (!strcasecmp(v->name, "remotesecret")) {
28236 ast_string_field_set(peer, remotesecret, v->value);
28237 } else if (!strcasecmp(v->name, "secret")) {
28238 ast_string_field_set(peer, secret, v->value);
28239 } else if (!strcasecmp(v->name, "md5secret")) {
28240 ast_string_field_set(peer, md5secret, v->value);
28241 } else if (!strcasecmp(v->name, "auth")) {
28242 add_realm_authentication(&peer->auth, v->value, v->lineno);
28243 } else if (!strcasecmp(v->name, "callerid")) {
28244 char cid_name[80] = { '\0' }, cid_num[80] = { '\0' };
28245
28246 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
28247 ast_string_field_set(peer, cid_name, cid_name);
28248 ast_string_field_set(peer, cid_num, cid_num);
28249 } else if (!strcasecmp(v->name, "mwi_from")) {
28250 ast_string_field_set(peer, mwi_from, v->value);
28251 } else if (!strcasecmp(v->name, "fullname")) {
28252 ast_string_field_set(peer, cid_name, v->value);
28253 } else if (!strcasecmp(v->name, "trunkname")) {
28254
28255 ast_string_field_set(peer, cid_name, "");
28256 } else if (!strcasecmp(v->name, "cid_number")) {
28257 ast_string_field_set(peer, cid_num, v->value);
28258 } else if (!strcasecmp(v->name, "cid_tag")) {
28259 ast_string_field_set(peer, cid_tag, v->value);
28260 } else if (!strcasecmp(v->name, "context")) {
28261 ast_string_field_set(peer, context, v->value);
28262 ast_set_flag(&peer->flags[1], SIP_PAGE2_HAVEPEERCONTEXT);
28263 } else if (!strcasecmp(v->name, "subscribecontext")) {
28264 ast_string_field_set(peer, subscribecontext, v->value);
28265 } else if (!strcasecmp(v->name, "fromdomain")) {
28266 char *fromdomainport;
28267 ast_string_field_set(peer, fromdomain, v->value);
28268 if ((fromdomainport = strchr(peer->fromdomain, ':'))) {
28269 *fromdomainport++ = '\0';
28270 if (!(peer->fromdomainport = port_str2int(fromdomainport, 0))) {
28271 ast_log(LOG_NOTICE, "'%s' is not a valid port number for fromdomain.\n",fromdomainport);
28272 }
28273 } else {
28274 peer->fromdomainport = STANDARD_SIP_PORT;
28275 }
28276 } else if (!strcasecmp(v->name, "usereqphone")) {
28277 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
28278 } else if (!strcasecmp(v->name, "fromuser")) {
28279 ast_string_field_set(peer, fromuser, v->value);
28280 } else if (!strcasecmp(v->name, "outboundproxy")) {
28281 struct sip_proxy *proxy;
28282 if (ast_strlen_zero(v->value)) {
28283 ast_log(LOG_WARNING, "no value given for outbound proxy on line %d of sip.conf\n", v->lineno);
28284 continue;
28285 }
28286 proxy = proxy_from_config(v->value, v->lineno, peer->outboundproxy);
28287 if (!proxy) {
28288 ast_log(LOG_WARNING, "failure parsing the outbound proxy on line %d of sip.conf.\n", v->lineno);
28289 continue;
28290 }
28291 peer->outboundproxy = proxy;
28292 } else if (!strcasecmp(v->name, "host")) {
28293 if (!strcasecmp(v->value, "dynamic")) {
28294
28295 if ((!found && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) || !peer->host_dynamic) {
28296
28297
28298 ast_sockaddr_setnull(&peer->addr);
28299 }
28300 peer->host_dynamic = TRUE;
28301 } else {
28302
28303 AST_SCHED_DEL_UNREF(sched, peer->expire,
28304 unref_peer(peer, "removing register expire ref"));
28305 peer->host_dynamic = FALSE;
28306 srvlookup = v->value;
28307 }
28308 } else if (!strcasecmp(v->name, "defaultip")) {
28309 if (!ast_strlen_zero(v->value) && ast_get_ip(&peer->defaddr, v->value)) {
28310 unref_peer(peer, "unref_peer: from build_peer defaultip");
28311 return NULL;
28312 }
28313 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
28314 int ha_error = 0;
28315 if (!ast_strlen_zero(v->value)) {
28316 peer->ha = ast_append_ha(v->name, v->value, peer->ha, &ha_error);
28317 }
28318 if (ha_error) {
28319 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
28320 }
28321 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
28322 int ha_error = 0;
28323 if (!ast_strlen_zero(v->value)) {
28324 peer->contactha = ast_append_ha(v->name + 7, v->value, peer->contactha, &ha_error);
28325 }
28326 if (ha_error) {
28327 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
28328 }
28329 } else if (!strcasecmp(v->name, "directmediapermit") || !strcasecmp(v->name, "directmediadeny")) {
28330 int ha_error = 0;
28331 peer->directmediaha = ast_append_ha(v->name + 11, v->value, peer->directmediaha, &ha_error);
28332 if (ha_error) {
28333 ast_log(LOG_ERROR, "Bad directmedia ACL entry in configuration line %d : %s\n", v->lineno, v->value);
28334 }
28335 } else if (!strcasecmp(v->name, "port")) {
28336 peer->portinuri = 1;
28337 if (!(port = port_str2int(v->value, 0))) {
28338 if (realtime) {
28339
28340 peer->portinuri = 0;
28341 } else {
28342 ast_log(LOG_WARNING, "Invalid peer port configuration at line %d : %s\n", v->lineno, v->value);
28343 }
28344 }
28345 } else if (!strcasecmp(v->name, "callingpres")) {
28346 peer->callingpres = ast_parse_caller_presentation(v->value);
28347 if (peer->callingpres == -1) {
28348 peer->callingpres = atoi(v->value);
28349 }
28350 } else if (!strcasecmp(v->name, "username") || !strcmp(v->name, "defaultuser")) {
28351 ast_string_field_set(peer, username, v->value);
28352 if (!strcasecmp(v->name, "username")) {
28353 if (deprecation_warning) {
28354 ast_log(LOG_NOTICE, "The 'username' field for sip peers has been deprecated in favor of the term 'defaultuser'\n");
28355 deprecation_warning = 0;
28356 }
28357 peer->deprecated_username = 1;
28358 }
28359 } else if (!strcasecmp(v->name, "language")) {
28360 ast_string_field_set(peer, language, v->value);
28361 } else if (!strcasecmp(v->name, "regexten")) {
28362 ast_string_field_set(peer, regexten, v->value);
28363 } else if (!strcasecmp(v->name, "callbackextension")) {
28364 ast_copy_string(callback, v->value, sizeof(callback));
28365 } else if (!strcasecmp(v->name, "amaflags")) {
28366 format = ast_cdr_amaflags2int(v->value);
28367 if (format < 0) {
28368 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
28369 } else {
28370 peer->amaflags = format;
28371 }
28372 } else if (!strcasecmp(v->name, "maxforwards")) {
28373 if (sscanf(v->value, "%30d", &peer->maxforwards) != 1
28374 || peer->maxforwards < 1 || 255 < peer->maxforwards) {
28375 ast_log(LOG_WARNING, "'%s' is not a valid maxforwards value at line %d. Using default.\n", v->value, v->lineno);
28376 peer->maxforwards = sip_cfg.default_max_forwards;
28377 }
28378 } else if (!strcasecmp(v->name, "accountcode")) {
28379 ast_string_field_set(peer, accountcode, v->value);
28380 } else if (!strcasecmp(v->name, "mohinterpret")) {
28381 ast_string_field_set(peer, mohinterpret, v->value);
28382 } else if (!strcasecmp(v->name, "mohsuggest")) {
28383 ast_string_field_set(peer, mohsuggest, v->value);
28384 } else if (!strcasecmp(v->name, "parkinglot")) {
28385 ast_string_field_set(peer, parkinglot, v->value);
28386 } else if (!strcasecmp(v->name, "rtp_engine")) {
28387 ast_string_field_set(peer, engine, v->value);
28388 } else if (!strcasecmp(v->name, "mailbox")) {
28389 add_peer_mailboxes(peer, v->value);
28390 } else if (!strcasecmp(v->name, "hasvoicemail")) {
28391
28392
28393 if (ast_true(v->value) && AST_LIST_EMPTY(&peer->mailboxes)) {
28394 add_peer_mailboxes(peer, name);
28395 }
28396 } else if (!strcasecmp(v->name, "subscribemwi")) {
28397 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
28398 } else if (!strcasecmp(v->name, "vmexten")) {
28399 ast_string_field_set(peer, vmexten, v->value);
28400 } else if (!strcasecmp(v->name, "callgroup")) {
28401 peer->callgroup = ast_get_group(v->value);
28402 } else if (!strcasecmp(v->name, "allowtransfer")) {
28403 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
28404 } else if (!strcasecmp(v->name, "pickupgroup")) {
28405 peer->pickupgroup = ast_get_group(v->value);
28406 } else if (!strcasecmp(v->name, "allow")) {
28407 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, TRUE);
28408 if (error) {
28409 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
28410 }
28411 } else if (!strcasecmp(v->name, "disallow")) {
28412 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, FALSE);
28413 if (error) {
28414 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
28415 }
28416 } else if (!strcasecmp(v->name, "preferred_codec_only")) {
28417 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
28418 } else if (!strcasecmp(v->name, "autoframing")) {
28419 peer->autoframing = ast_true(v->value);
28420 } else if (!strcasecmp(v->name, "rtptimeout")) {
28421 if ((sscanf(v->value, "%30d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
28422 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
28423 peer->rtptimeout = global_rtptimeout;
28424 }
28425 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
28426 if ((sscanf(v->value, "%30d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
28427 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
28428 peer->rtpholdtimeout = global_rtpholdtimeout;
28429 }
28430 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
28431 if ((sscanf(v->value, "%30d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
28432 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
28433 peer->rtpkeepalive = global_rtpkeepalive;
28434 }
28435 } else if (!strcasecmp(v->name, "timert1")) {
28436 if ((sscanf(v->value, "%30d", &peer->timer_t1) != 1) || (peer->timer_t1 < 200) || (peer->timer_t1 < global_t1min)) {
28437 ast_log(LOG_WARNING, "'%s' is not a valid T1 time at line %d. Using default.\n", v->value, v->lineno);
28438 peer->timer_t1 = global_t1min;
28439 }
28440 timert1_set = 1;
28441 } else if (!strcasecmp(v->name, "timerb")) {
28442 if ((sscanf(v->value, "%30d", &peer->timer_b) != 1) || (peer->timer_b < 200)) {
28443 ast_log(LOG_WARNING, "'%s' is not a valid Timer B time at line %d. Using default.\n", v->value, v->lineno);
28444 peer->timer_b = global_timer_b;
28445 }
28446 timerb_set = 1;
28447 } else if (!strcasecmp(v->name, "setvar")) {
28448 peer->chanvars = add_var(v->value, peer->chanvars);
28449 } else if (!strcasecmp(v->name, "header")) {
28450 char tmp[4096];
28451 snprintf(tmp, sizeof(tmp), "__SIPADDHEADERpre%2d=%s", ++headercount, v->value);
28452 peer->chanvars = add_var(tmp, peer->chanvars);
28453 } else if (!strcasecmp(v->name, "qualifyfreq")) {
28454 int i;
28455 if (sscanf(v->value, "%30d", &i) == 1) {
28456 peer->qualifyfreq = i * 1000;
28457 } else {
28458 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
28459 peer->qualifyfreq = global_qualifyfreq;
28460 }
28461 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
28462 peer->maxcallbitrate = atoi(v->value);
28463 if (peer->maxcallbitrate < 0) {
28464 peer->maxcallbitrate = default_maxcallbitrate;
28465 }
28466 } else if (!strcasecmp(v->name, "session-timers")) {
28467 int i = (int) str2stmode(v->value);
28468 if (i < 0) {
28469 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
28470 peer->stimer.st_mode_oper = global_st_mode;
28471 } else {
28472 peer->stimer.st_mode_oper = i;
28473 }
28474 } else if (!strcasecmp(v->name, "session-expires")) {
28475 if (sscanf(v->value, "%30d", &peer->stimer.st_max_se) != 1) {
28476 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
28477 peer->stimer.st_max_se = global_max_se;
28478 }
28479 } else if (!strcasecmp(v->name, "session-minse")) {
28480 if (sscanf(v->value, "%30d", &peer->stimer.st_min_se) != 1) {
28481 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
28482 peer->stimer.st_min_se = global_min_se;
28483 }
28484 if (peer->stimer.st_min_se < DEFAULT_MIN_SE) {
28485 ast_log(LOG_WARNING, "session-minse '%s' at line %d of %s is not allowed to be < %d secs\n", v->value, v->lineno, config, DEFAULT_MIN_SE);
28486 peer->stimer.st_min_se = global_min_se;
28487 }
28488 } else if (!strcasecmp(v->name, "session-refresher")) {
28489 int i = (int) str2strefresherparam(v->value);
28490 if (i < 0) {
28491 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
28492 peer->stimer.st_ref = global_st_refresher;
28493 } else {
28494 peer->stimer.st_ref = i;
28495 }
28496 } else if (!strcasecmp(v->name, "disallowed_methods")) {
28497 char *disallow = ast_strdupa(v->value);
28498 mark_parsed_methods(&peer->disallowed_methods, disallow);
28499 } else if (!strcasecmp(v->name, "unsolicited_mailbox")) {
28500 ast_string_field_set(peer, unsolicited_mailbox, v->value);
28501 } else if (!strcasecmp(v->name, "use_q850_reason")) {
28502 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_Q850_REASON);
28503 } else if (!strcasecmp(v->name, "encryption")) {
28504 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_USE_SRTP);
28505 } else if (!strcasecmp(v->name, "snom_aoc_enabled")) {
28506 ast_set2_flag(&peer->flags[2], ast_true(v->value), SIP_PAGE3_SNOM_AOC);
28507 }
28508 }
28509
28510
28511 if (realtime && !strcasecmp(v->name, "lastms")) {
28512 sscanf(v->value, "%30d", &peer->lastms);
28513 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
28514 ast_sockaddr_parse(&peer->addr, v->value, PARSE_PORT_FORBID);
28515 } else if (realtime && !strcasecmp(v->name, "fullcontact")) {
28516 if (alt_fullcontact && !alt) {
28517
28518
28519
28520
28521
28522 alt_fullcontact = 0;
28523 ast_str_reset(fullcontact);
28524 }
28525
28526 if (ast_str_strlen(fullcontact) > 0) {
28527 ast_str_append(&fullcontact, 0, ";%s", v->value);
28528 } else {
28529 ast_str_set(&fullcontact, 0, "%s", v->value);
28530 }
28531 } else if (!strcasecmp(v->name, "qualify")) {
28532 if (!strcasecmp(v->value, "no")) {
28533 peer->maxms = 0;
28534 } else if (!strcasecmp(v->value, "yes")) {
28535 peer->maxms = default_qualify ? default_qualify : DEFAULT_MAXMS;
28536 } else if (sscanf(v->value, "%30d", &peer->maxms) != 1) {
28537 ast_log(LOG_WARNING, "Qualification of peer '%s' should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", peer->name, v->lineno);
28538 peer->maxms = 0;
28539 }
28540 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && peer->maxms > 0) {
28541
28542
28543
28544
28545 ast_log(LOG_WARNING, "Qualify is incompatible with dynamic uncached realtime. Please either turn rtcachefriends on or turn qualify off on peer '%s'\n", peer->name);
28546 peer->maxms = 0;
28547 }
28548 } else if (!strcasecmp(v->name, "callcounter")) {
28549 peer->call_limit = ast_true(v->value) ? INT_MAX : 0;
28550 } else if (!strcasecmp(v->name, "call-limit")) {
28551 peer->call_limit = atoi(v->value);
28552 if (peer->call_limit < 0) {
28553 peer->call_limit = 0;
28554 }
28555 } else if (!strcasecmp(v->name, "busylevel")) {
28556 peer->busy_level = atoi(v->value);
28557 if (peer->busy_level < 0) {
28558 peer->busy_level = 0;
28559 }
28560 } else if (ast_cc_is_config_param(v->name)) {
28561 ast_cc_set_param(peer->cc_params, v->name, v->value);
28562 }
28563 }
28564
28565 if (!devstate_only) {
28566 struct sip_mailbox *mailbox;
28567 AST_LIST_TRAVERSE_SAFE_BEGIN(&peer->mailboxes, mailbox, entry) {
28568 if (mailbox->delme) {
28569 AST_LIST_REMOVE_CURRENT(entry);
28570 destroy_mailbox(mailbox);
28571 }
28572 }
28573 AST_LIST_TRAVERSE_SAFE_END;
28574 }
28575
28576 if (!can_parse_xml && (ast_get_cc_agent_policy(peer->cc_params) == AST_CC_AGENT_NATIVE)) {
28577 ast_log(LOG_WARNING, "Peer %s has a cc_agent_policy of 'native' but required libxml2 dependency is not installed. Changing policy to 'never'\n", peer->name);
28578 ast_set_cc_agent_policy(peer->cc_params, AST_CC_AGENT_NEVER);
28579 }
28580
28581
28582
28583 if (peer->timer_b < peer->timer_t1 * 64) {
28584 if (timerb_set && timert1_set) {
28585 ast_log(LOG_WARNING, "Timer B has been set lower than recommended for peer %s (%d < 64 * Timer-T1=%d)\n", peer->name, peer->timer_b, peer->timer_t1);
28586 } else if (timerb_set) {
28587 if ((peer->timer_t1 = peer->timer_b / 64) < global_t1min) {
28588 ast_log(LOG_WARNING, "Timer B has been set lower than recommended (%d < 64 * timert1=%d). (RFC 3261, 17.1.1.2)\n", peer->timer_b, peer->timer_t1);
28589 peer->timer_t1 = global_t1min;
28590 peer->timer_b = peer->timer_t1 * 64;
28591 }
28592 peer->timer_t1 = peer->timer_b / 64;
28593 } else {
28594 peer->timer_b = peer->timer_t1 * 64;
28595 }
28596 }
28597
28598 if (!peer->default_outbound_transport) {
28599
28600 peer->transports = default_transports;
28601
28602 peer->default_outbound_transport = default_primary_transport;
28603 }
28604
28605
28606
28607
28608
28609 if (((peer->socket.type != peer->default_outbound_transport) && (peer->expire == -1)) ||
28610 !(peer->socket.type & peer->transports) || !(peer->socket.type)) {
28611
28612 set_socket_transport(&peer->socket, peer->default_outbound_transport);
28613 }
28614
28615 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
28616 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
28617 ast_copy_flags(&peer->flags[2], &peerflags[2], mask[2].flags);
28618
28619 if (ast_str_strlen(fullcontact)) {
28620 ast_string_field_set(peer, fullcontact, ast_str_buffer(fullcontact));
28621 peer->rt_fromcontact = TRUE;
28622
28623
28624
28625
28626
28627
28628
28629
28630 if (!ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) || ast_sockaddr_isnull(&peer->addr)) {
28631 __set_address_from_contact(ast_str_buffer(fullcontact), &peer->addr, 0);
28632 }
28633 }
28634
28635 if (srvlookup && peer->dnsmgr == NULL) {
28636 char transport[MAXHOSTNAMELEN];
28637 char _srvlookup[MAXHOSTNAMELEN];
28638 char *params;
28639
28640 ast_copy_string(_srvlookup, srvlookup, sizeof(_srvlookup));
28641 if ((params = strchr(_srvlookup, ';'))) {
28642 *params++ = '\0';
28643 }
28644
28645 snprintf(transport, sizeof(transport), "_%s._%s", get_srv_service(peer->socket.type), get_srv_protocol(peer->socket.type));
28646
28647 peer->addr.ss.ss_family = get_address_family_filter(peer->socket.type);
28648 if (ast_dnsmgr_lookup_cb(_srvlookup, &peer->addr, &peer->dnsmgr, sip_cfg.srvlookup && !peer->portinuri ? transport : NULL,
28649 on_dns_update_peer, ref_peer(peer, "Store peer on dnsmgr"))) {
28650 ast_log(LOG_ERROR, "srvlookup failed for host: %s, on peer %s, removing peer\n", _srvlookup, peer->name);
28651 unref_peer(peer, "dnsmgr lookup failed, getting rid of peer dnsmgr ref");
28652 unref_peer(peer, "getting rid of a peer pointer");
28653 return NULL;
28654 }
28655 if (!peer->dnsmgr) {
28656
28657 unref_peer(peer, "dnsmgr disabled, unref peer");
28658 }
28659
28660 ast_string_field_set(peer, tohost, srvlookup);
28661
28662 if (global_dynamic_exclude_static && !ast_sockaddr_isnull(&peer->addr)) {
28663 int ha_error = 0;
28664 sip_cfg.contact_ha = ast_append_ha("deny", ast_sockaddr_stringify_addr(&peer->addr),
28665 sip_cfg.contact_ha, &ha_error);
28666 if (ha_error) {
28667 ast_log(LOG_ERROR, "Bad or unresolved host/IP entry in configuration for peer %s, cannot add to contact ACL\n", peer->name);
28668 }
28669 }
28670 } else if (peer->dnsmgr && !peer->host_dynamic) {
28671
28672 ast_dnsmgr_refresh(peer->dnsmgr);
28673 }
28674
28675 if (port && !realtime && peer->host_dynamic) {
28676 ast_sockaddr_set_port(&peer->defaddr, port);
28677 } else if (port) {
28678 ast_sockaddr_set_port(&peer->addr, port);
28679 }
28680
28681 if (ast_sockaddr_port(&peer->addr) == 0) {
28682 ast_sockaddr_set_port(&peer->addr,
28683 (peer->socket.type & SIP_TRANSPORT_TLS) ?
28684 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
28685 }
28686 if (ast_sockaddr_port(&peer->defaddr) == 0) {
28687 ast_sockaddr_set_port(&peer->defaddr,
28688 (peer->socket.type & SIP_TRANSPORT_TLS) ?
28689 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
28690 }
28691 if (!peer->socket.port) {
28692 peer->socket.port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
28693 }
28694
28695 if (realtime) {
28696 int enablepoke = 1;
28697
28698 if (!sip_cfg.ignore_regexpire && peer->host_dynamic) {
28699 time_t nowtime = time(NULL);
28700
28701 if ((nowtime - regseconds) > 0) {
28702 destroy_association(peer);
28703 memset(&peer->addr, 0, sizeof(peer->addr));
28704 peer->lastms = -1;
28705 enablepoke = 0;
28706 ast_debug(1, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
28707 }
28708 }
28709
28710
28711 if (!devstate_only && enablepoke) {
28712 sip_poke_peer(peer, 0);
28713 }
28714 }
28715
28716 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
28717 sip_cfg.allowsubscribe = TRUE;
28718 }
28719
28720 if (peer->host_dynamic && (!peer->is_realtime || !sip_cfg.peer_rtupdate)) {
28721 reg_source_db(peer);
28722 }
28723
28724
28725
28726 if (!devstate_only && !ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
28727 !AST_LIST_EMPTY(&peer->mailboxes)) {
28728 add_peer_mwi_subs(peer);
28729
28730
28731
28732 sip_send_mwi_to_peer(peer, 1);
28733 }
28734
28735 peer->the_mark = 0;
28736
28737 ast_free_ha(oldha);
28738 ast_free_ha(olddirectmediaha);
28739 if (!ast_strlen_zero(callback)) {
28740 char *reg_string;
28741 if (ast_asprintf(®_string, "%s?%s:%s@%s/%s", peer->name, peer->username, !ast_strlen_zero(peer->remotesecret) ? peer->remotesecret : peer->secret, peer->tohost, callback) >= 0) {
28742 sip_register(reg_string, 0);
28743 ast_free(reg_string);
28744 }
28745 }
28746 return peer;
28747 }
28748
28749 static int peer_markall_func(void *device, void *arg, int flags)
28750 {
28751 struct sip_peer *peer = device;
28752 peer->the_mark = 1;
28753 return 0;
28754 }
28755
28756 static void display_nat_warning(const char *cat, int reason, struct ast_flags *flags) {
28757 int global_nat, specific_nat;
28758
28759 if (reason == CHANNEL_MODULE_LOAD && (specific_nat = ast_test_flag(&flags[0], SIP_NAT_FORCE_RPORT)) != (global_nat = ast_test_flag(&global_flags[0], SIP_NAT_FORCE_RPORT))) {
28760 ast_log(LOG_WARNING, "!!! PLEASE NOTE: Setting 'nat' for a peer/user that differs from the global setting can make\n");
28761 ast_log(LOG_WARNING, "!!! the name of that peer/user discoverable by an attacker. Replies for non-existent peers/users\n");
28762 ast_log(LOG_WARNING, "!!! will be sent to a different port than replies for an existing peer/user. If at all possible,\n");
28763 ast_log(LOG_WARNING, "!!! use the global 'nat' setting and do not set 'nat' per peer/user.\n");
28764 ast_log(LOG_WARNING, "!!! (config category='%s' global force_rport='%s' peer/user force_rport='%s')\n", cat, AST_CLI_YESNO(global_nat), AST_CLI_YESNO(specific_nat));
28765 }
28766 }
28767
28768 static void cleanup_all_regs(void)
28769 {
28770
28771
28772 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
28773 ASTOBJ_WRLOCK(iterator);
28774 if (iterator->call) {
28775 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
28776
28777 dialog_unlink_all(iterator->call);
28778 iterator->call = dialog_unref(iterator->call, "remove iterator->call from registry traversal");
28779 }
28780 if (iterator->expire > -1) {
28781 AST_SCHED_DEL_UNREF(sched, iterator->expire, registry_unref(iterator, "reg ptr unref from reload config"));
28782 }
28783 if (iterator->timeout > -1) {
28784 AST_SCHED_DEL_UNREF(sched, iterator->timeout, registry_unref(iterator, "reg ptr unref from reload config"));
28785 }
28786 if (iterator->dnsmgr) {
28787 ast_dnsmgr_release(iterator->dnsmgr);
28788 iterator->dnsmgr = NULL;
28789 registry_unref(iterator, "reg ptr unref from dnsmgr");
28790 }
28791 ASTOBJ_UNLOCK(iterator);
28792 } while(0));
28793 }
28794
28795
28796
28797
28798
28799
28800
28801 static int reload_config(enum channelreloadreason reason)
28802 {
28803 struct ast_config *cfg, *ucfg;
28804 struct ast_variable *v;
28805 struct sip_peer *peer;
28806 char *cat, *stringp, *context, *oldregcontext;
28807 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
28808 struct ast_flags mask[3] = {{0}};
28809 struct ast_flags setflags[3] = {{0}};
28810 struct ast_flags config_flags = { reason == CHANNEL_MODULE_LOAD ? 0 : ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? 0 : CONFIG_FLAG_FILEUNCHANGED };
28811 int auto_sip_domains = FALSE;
28812 struct ast_sockaddr old_bindaddr = bindaddr;
28813 int registry_count = 0, peer_count = 0, timerb_set = 0, timert1_set = 0;
28814 int subscribe_network_change = 1;
28815 time_t run_start, run_end;
28816 int bindport = 0;
28817
28818 run_start = time(0);
28819 ast_unload_realtime("sipregs");
28820 ast_unload_realtime("sippeers");
28821 cfg = ast_config_load(config, config_flags);
28822
28823
28824 if (!cfg) {
28825 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
28826 return -1;
28827 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
28828 ucfg = ast_config_load("users.conf", config_flags);
28829 if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
28830 return 1;
28831 } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
28832 ast_log(LOG_ERROR, "Contents of users.conf are invalid and cannot be parsed\n");
28833 return 1;
28834 }
28835
28836 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
28837 if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
28838 ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed\n", config);
28839 ast_config_destroy(ucfg);
28840 return 1;
28841 }
28842 if (!cfg) {
28843
28844 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
28845 return -1;
28846 }
28847 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
28848 ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed\n", config);
28849 return 1;
28850 } else {
28851 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
28852 if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
28853 ast_log(LOG_ERROR, "Contents of users.conf are invalid and cannot be parsed\n");
28854 ast_config_destroy(cfg);
28855 return 1;
28856 }
28857 }
28858
28859 ast_free_ha(sip_cfg.contact_ha);
28860 sip_cfg.contact_ha = NULL;
28861
28862 default_tls_cfg.enabled = FALSE;
28863
28864 if (reason != CHANNEL_MODULE_LOAD) {
28865 ast_debug(4, "--------------- SIP reload started\n");
28866
28867 clear_sip_domains();
28868 ast_mutex_lock(&authl_lock);
28869 if (authl) {
28870 ao2_t_ref(authl, -1, "Removing old global authentication");
28871 authl = NULL;
28872 }
28873 ast_mutex_unlock(&authl_lock);
28874
28875
28876 cleanup_all_regs();
28877
28878 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
28879 ast_debug(4, "--------------- Done destroying registry list\n");
28880 ao2_t_callback(peers, OBJ_NODATA, peer_markall_func, NULL, "callback to mark all peers");
28881 }
28882
28883
28884 if (reason != CHANNEL_MODULE_LOAD) {
28885 ast_free(default_tls_cfg.certfile);
28886 ast_free(default_tls_cfg.pvtfile);
28887 ast_free(default_tls_cfg.cipher);
28888 ast_free(default_tls_cfg.cafile);
28889 ast_free(default_tls_cfg.capath);
28890 }
28891 default_tls_cfg.certfile = ast_strdup(AST_CERTFILE);
28892 default_tls_cfg.pvtfile = ast_strdup("");
28893 default_tls_cfg.cipher = ast_strdup("");
28894 default_tls_cfg.cafile = ast_strdup("");
28895 default_tls_cfg.capath = ast_strdup("");
28896
28897
28898 ast_copy_string(oldcontexts, sip_cfg.regcontext, sizeof(oldcontexts));
28899 oldregcontext = oldcontexts;
28900
28901
28902
28903 sipdebug &= sip_debug_console;
28904 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
28905 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
28906 ast_clear_flag(&global_flags[2], AST_FLAGS_ALL);
28907
28908
28909 ast_sockaddr_parse(&bindaddr, "0.0.0.0:0", 0);
28910 memset(&internip, 0, sizeof(internip));
28911
28912
28913 ast_free_ha(localaddr);
28914 memset(&localaddr, 0, sizeof(localaddr));
28915 memset(&externaddr, 0, sizeof(externaddr));
28916 memset(&media_address, 0, sizeof(media_address));
28917 memset(&default_prefs, 0 , sizeof(default_prefs));
28918 memset(&sip_cfg.outboundproxy, 0, sizeof(struct sip_proxy));
28919 sip_cfg.outboundproxy.force = FALSE;
28920 default_transports = SIP_TRANSPORT_UDP;
28921 default_primary_transport = SIP_TRANSPORT_UDP;
28922 ourport_tcp = STANDARD_SIP_PORT;
28923 ourport_tls = STANDARD_TLS_PORT;
28924 externtcpport = STANDARD_SIP_PORT;
28925 externtlsport = STANDARD_TLS_PORT;
28926 sip_cfg.srvlookup = DEFAULT_SRVLOOKUP;
28927 global_tos_sip = DEFAULT_TOS_SIP;
28928 global_tos_audio = DEFAULT_TOS_AUDIO;
28929 global_tos_video = DEFAULT_TOS_VIDEO;
28930 global_tos_text = DEFAULT_TOS_TEXT;
28931 global_cos_sip = DEFAULT_COS_SIP;
28932 global_cos_audio = DEFAULT_COS_AUDIO;
28933 global_cos_video = DEFAULT_COS_VIDEO;
28934 global_cos_text = DEFAULT_COS_TEXT;
28935
28936 externhost[0] = '\0';
28937 externexpire = 0;
28938 externrefresh = 10;
28939
28940
28941 sip_cfg.allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
28942 sip_cfg.regcontext[0] = '\0';
28943 sip_cfg.capability = DEFAULT_CAPABILITY;
28944 sip_cfg.regextenonqualify = DEFAULT_REGEXTENONQUALIFY;
28945 sip_cfg.legacy_useroption_parsing = DEFAULT_LEGACY_USEROPTION_PARSING;
28946 sip_cfg.notifyringing = DEFAULT_NOTIFYRINGING;
28947 sip_cfg.notifycid = DEFAULT_NOTIFYCID;
28948 sip_cfg.notifyhold = FALSE;
28949 sip_cfg.directrtpsetup = FALSE;
28950 sip_cfg.alwaysauthreject = DEFAULT_ALWAYSAUTHREJECT;
28951 sip_cfg.auth_options_requests = DEFAULT_AUTH_OPTIONS;
28952 sip_cfg.allowsubscribe = FALSE;
28953 sip_cfg.disallowed_methods = SIP_UNKNOWN;
28954 sip_cfg.contact_ha = NULL;
28955 snprintf(global_useragent, sizeof(global_useragent), "%s %s", DEFAULT_USERAGENT, ast_get_version());
28956 snprintf(global_sdpsession, sizeof(global_sdpsession), "%s %s", DEFAULT_SDPSESSION, ast_get_version());
28957 snprintf(global_sdpowner, sizeof(global_sdpowner), "%s", DEFAULT_SDPOWNER);
28958 global_prematuremediafilter = TRUE;
28959 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
28960 ast_copy_string(sip_cfg.realm, S_OR(ast_config_AST_SYSTEM_NAME, DEFAULT_REALM), sizeof(sip_cfg.realm));
28961 sip_cfg.domainsasrealm = DEFAULT_DOMAINSASREALM;
28962 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
28963 ast_copy_string(default_mwi_from, DEFAULT_MWI_FROM, sizeof(default_mwi_from));
28964 sip_cfg.compactheaders = DEFAULT_COMPACTHEADERS;
28965 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
28966 global_regattempts_max = 0;
28967 global_reg_retry_403 = 0;
28968 sip_cfg.pedanticsipchecking = DEFAULT_PEDANTIC;
28969 sip_cfg.autocreatepeer = DEFAULT_AUTOCREATEPEER;
28970 global_autoframing = 0;
28971 sip_cfg.allowguest = DEFAULT_ALLOWGUEST;
28972 global_callcounter = DEFAULT_CALLCOUNTER;
28973 global_match_auth_username = FALSE;
28974 global_rtptimeout = 0;
28975 global_rtpholdtimeout = 0;
28976 global_rtpkeepalive = DEFAULT_RTPKEEPALIVE;
28977 sip_cfg.allowtransfer = TRANSFER_OPENFORALL;
28978 sip_cfg.rtautoclear = 120;
28979 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
28980 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP_YES);
28981 sip_cfg.peer_rtupdate = TRUE;
28982 global_dynamic_exclude_static = 0;
28983 sip_cfg.tcp_enabled = FALSE;
28984
28985
28986 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
28987 global_st_refresher = SESSION_TIMER_REFRESHER_PARAM_UAS;
28988 global_min_se = DEFAULT_MIN_SE;
28989 global_max_se = DEFAULT_MAX_SE;
28990
28991
28992 global_qualify_gap = DEFAULT_QUALIFY_GAP;
28993 global_qualify_peers = DEFAULT_QUALIFY_PEERS;
28994
28995
28996 ast_copy_string(sip_cfg.default_context, DEFAULT_CONTEXT, sizeof(sip_cfg.default_context));
28997 sip_cfg.default_subscribecontext[0] = '\0';
28998 sip_cfg.default_max_forwards = DEFAULT_MAX_FORWARDS;
28999 default_language[0] = '\0';
29000 default_fromdomain[0] = '\0';
29001 default_fromdomainport = 0;
29002 default_qualify = DEFAULT_QUALIFY;
29003 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
29004 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
29005 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
29006 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
29007 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
29008 ast_set_flag(&global_flags[0], SIP_DIRECT_MEDIA);
29009 ast_set_flag(&global_flags[0], SIP_NAT_FORCE_RPORT);
29010 ast_copy_string(default_engine, DEFAULT_ENGINE, sizeof(default_engine));
29011 ast_copy_string(default_parkinglot, DEFAULT_PARKINGLOT, sizeof(default_parkinglot));
29012
29013
29014 dumphistory = FALSE;
29015 recordhistory = FALSE;
29016 sipdebug &= ~sip_debug_config;
29017
29018
29019 global_relaxdtmf = FALSE;
29020 sip_cfg.callevents = DEFAULT_CALLEVENTS;
29021 global_authfailureevents = FALSE;
29022 global_t1 = DEFAULT_TIMER_T1;
29023 global_timer_b = 64 * DEFAULT_TIMER_T1;
29024 global_t1min = DEFAULT_T1MIN;
29025 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
29026 global_t38_maxdatagram = -1;
29027 global_shrinkcallerid = 1;
29028 authlimit = DEFAULT_AUTHLIMIT;
29029 authtimeout = DEFAULT_AUTHTIMEOUT;
29030 global_store_sip_cause = DEFAULT_STORE_SIP_CAUSE;
29031
29032 sip_cfg.matchexternaddrlocally = DEFAULT_MATCHEXTERNADDRLOCALLY;
29033
29034
29035 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
29036
29037 ast_clear_flag(&global_flags[1], SIP_PAGE2_FAX_DETECT);
29038 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT | SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
29039 ast_clear_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT);
29040 ast_clear_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION);
29041
29042
29043
29044 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
29045 if (handle_common_options(&setflags[0], &mask[0], v)) {
29046 continue;
29047 }
29048 if (handle_t38_options(&setflags[0], &mask[0], v, &global_t38_maxdatagram)) {
29049 continue;
29050 }
29051
29052 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
29053 continue;
29054
29055
29056 if (!strcasecmp(v->name, "tlsverifyclient")) {
29057 ast_log(LOG_WARNING, "Ignoring unsupported option 'tlsverifyclient'\n");
29058 continue;
29059 } else if (!ast_tls_read_conf(&default_tls_cfg, &sip_tls_desc, v->name, v->value)) {
29060 continue;
29061 }
29062
29063 if (!strcasecmp(v->name, "context")) {
29064 ast_copy_string(sip_cfg.default_context, v->value, sizeof(sip_cfg.default_context));
29065 } else if (!strcasecmp(v->name, "subscribecontext")) {
29066 ast_copy_string(sip_cfg.default_subscribecontext, v->value, sizeof(sip_cfg.default_subscribecontext));
29067 } else if (!strcasecmp(v->name, "callcounter")) {
29068 global_callcounter = ast_true(v->value) ? 1 : 0;
29069 } else if (!strcasecmp(v->name, "allowguest")) {
29070 sip_cfg.allowguest = ast_true(v->value) ? 1 : 0;
29071 } else if (!strcasecmp(v->name, "realm")) {
29072 ast_copy_string(sip_cfg.realm, v->value, sizeof(sip_cfg.realm));
29073 } else if (!strcasecmp(v->name, "domainsasrealm")) {
29074 sip_cfg.domainsasrealm = ast_true(v->value);
29075 } else if (!strcasecmp(v->name, "useragent")) {
29076 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
29077 ast_debug(1, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
29078 } else if (!strcasecmp(v->name, "sdpsession")) {
29079 ast_copy_string(global_sdpsession, v->value, sizeof(global_sdpsession));
29080 } else if (!strcasecmp(v->name, "sdpowner")) {
29081
29082 if (!strstr(v->value, " ")) {
29083 ast_copy_string(global_sdpowner, v->value, sizeof(global_sdpowner));
29084 } else {
29085 ast_log(LOG_WARNING, "'%s' must not contain spaces at line %d. Using default.\n", v->value, v->lineno);
29086 }
29087 } else if (!strcasecmp(v->name, "allowtransfer")) {
29088 sip_cfg.allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
29089 } else if (!strcasecmp(v->name, "rtcachefriends")) {
29090 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
29091 } else if (!strcasecmp(v->name, "rtsavesysname")) {
29092 sip_cfg.rtsave_sysname = ast_true(v->value);
29093 } else if (!strcasecmp(v->name, "rtupdate")) {
29094 sip_cfg.peer_rtupdate = ast_true(v->value);
29095 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
29096 sip_cfg.ignore_regexpire = ast_true(v->value);
29097 } else if (!strcasecmp(v->name, "timert1")) {
29098
29099
29100
29101 global_t1 = atoi(v->value);
29102 } else if (!strcasecmp(v->name, "timerb")) {
29103 int tmp = atoi(v->value);
29104 if (tmp < 500) {
29105 global_timer_b = global_t1 * 64;
29106 ast_log(LOG_WARNING, "Invalid value for timerb ('%s'). Setting to default ('%d').\n", v->value, global_timer_b);
29107 }
29108 timerb_set = 1;
29109 } else if (!strcasecmp(v->name, "t1min")) {
29110 global_t1min = atoi(v->value);
29111 } else if (!strcasecmp(v->name, "transport")) {
29112 char *val = ast_strdupa(v->value);
29113 char *trans;
29114
29115 default_transports = default_primary_transport = 0;
29116 while ((trans = strsep(&val, ","))) {
29117 trans = ast_skip_blanks(trans);
29118
29119 if (!strncasecmp(trans, "udp", 3)) {
29120 default_transports |= SIP_TRANSPORT_UDP;
29121 } else if (!strncasecmp(trans, "tcp", 3)) {
29122 default_transports |= SIP_TRANSPORT_TCP;
29123 } else if (!strncasecmp(trans, "tls", 3)) {
29124 default_transports |= SIP_TRANSPORT_TLS;
29125 } else {
29126 ast_log(LOG_NOTICE, "'%s' is not a valid transport type. if no other is specified, udp will be used.\n", trans);
29127 }
29128 if (default_primary_transport == 0) {
29129 default_primary_transport = default_transports;
29130 }
29131 }
29132 } else if (!strcasecmp(v->name, "tcpenable")) {
29133 if (!ast_false(v->value)) {
29134 ast_debug(2, "Enabling TCP socket for listening\n");
29135 sip_cfg.tcp_enabled = TRUE;
29136 }
29137 } else if (!strcasecmp(v->name, "tcpbindaddr")) {
29138 if (ast_parse_arg(v->value, PARSE_ADDR,
29139 &sip_tcp_desc.local_address)) {
29140 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n",
29141 v->name, v->value, v->lineno, config);
29142 }
29143 ast_debug(2, "Setting TCP socket address to %s\n",
29144 ast_sockaddr_stringify(&sip_tcp_desc.local_address));
29145 } else if (!strcasecmp(v->name, "dynamic_exclude_static") || !strcasecmp(v->name, "dynamic_excludes_static")) {
29146 global_dynamic_exclude_static = ast_true(v->value);
29147 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
29148 int ha_error = 0;
29149 sip_cfg.contact_ha = ast_append_ha(v->name + 7, v->value, sip_cfg.contact_ha, &ha_error);
29150 if (ha_error) {
29151 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
29152 }
29153 } else if (!strcasecmp(v->name, "rtautoclear")) {
29154 int i = atoi(v->value);
29155 if (i > 0) {
29156 sip_cfg.rtautoclear = i;
29157 } else {
29158 i = 0;
29159 }
29160 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
29161 } else if (!strcasecmp(v->name, "usereqphone")) {
29162 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
29163 } else if (!strcasecmp(v->name, "prematuremedia")) {
29164 global_prematuremediafilter = ast_true(v->value);
29165 } else if (!strcasecmp(v->name, "relaxdtmf")) {
29166 global_relaxdtmf = ast_true(v->value);
29167 } else if (!strcasecmp(v->name, "vmexten")) {
29168 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
29169 } else if (!strcasecmp(v->name, "rtptimeout")) {
29170 if ((sscanf(v->value, "%30d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
29171 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
29172 global_rtptimeout = 0;
29173 }
29174 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
29175 if ((sscanf(v->value, "%30d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
29176 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
29177 global_rtpholdtimeout = 0;
29178 }
29179 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
29180 if ((sscanf(v->value, "%30d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
29181 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
29182 global_rtpkeepalive = DEFAULT_RTPKEEPALIVE;
29183 }
29184 } else if (!strcasecmp(v->name, "compactheaders")) {
29185 sip_cfg.compactheaders = ast_true(v->value);
29186 } else if (!strcasecmp(v->name, "notifymimetype")) {
29187 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
29188 } else if (!strcasecmp(v->name, "directrtpsetup")) {
29189 sip_cfg.directrtpsetup = ast_true(v->value);
29190 } else if (!strcasecmp(v->name, "notifyringing")) {
29191 sip_cfg.notifyringing = ast_true(v->value);
29192 } else if (!strcasecmp(v->name, "notifyhold")) {
29193 sip_cfg.notifyhold = ast_true(v->value);
29194 } else if (!strcasecmp(v->name, "notifycid")) {
29195 if (!strcasecmp(v->value, "ignore-context")) {
29196 sip_cfg.notifycid = IGNORE_CONTEXT;
29197 } else {
29198 sip_cfg.notifycid = ast_true(v->value) ? ENABLED : DISABLED;
29199 }
29200 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
29201 sip_cfg.alwaysauthreject = ast_true(v->value);
29202 } else if (!strcasecmp(v->name, "auth_options_requests")) {
29203 if (ast_true(v->value)) {
29204 sip_cfg.auth_options_requests = 1;
29205 }
29206 } else if (!strcasecmp(v->name, "mohinterpret")) {
29207 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
29208 } else if (!strcasecmp(v->name, "mohsuggest")) {
29209 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
29210 } else if (!strcasecmp(v->name, "language")) {
29211 ast_copy_string(default_language, v->value, sizeof(default_language));
29212 } else if (!strcasecmp(v->name, "regcontext")) {
29213 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
29214 stringp = newcontexts;
29215
29216 cleanup_stale_contexts(stringp, oldregcontext);
29217
29218 while ((context = strsep(&stringp, "&"))) {
29219 ast_copy_string(used_context, context, sizeof(used_context));
29220 ast_context_find_or_create(NULL, NULL, context, "SIP");
29221 }
29222 ast_copy_string(sip_cfg.regcontext, v->value, sizeof(sip_cfg.regcontext));
29223 } else if (!strcasecmp(v->name, "regextenonqualify")) {
29224 sip_cfg.regextenonqualify = ast_true(v->value);
29225 } else if (!strcasecmp(v->name, "legacy_useroption_parsing")) {
29226 sip_cfg.legacy_useroption_parsing = ast_true(v->value);
29227 } else if (!strcasecmp(v->name, "callerid")) {
29228 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
29229 } else if (!strcasecmp(v->name, "mwi_from")) {
29230 ast_copy_string(default_mwi_from, v->value, sizeof(default_mwi_from));
29231 } else if (!strcasecmp(v->name, "fromdomain")) {
29232 char *fromdomainport;
29233 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
29234 if ((fromdomainport = strchr(default_fromdomain, ':'))) {
29235 *fromdomainport++ = '\0';
29236 if (!(default_fromdomainport = port_str2int(fromdomainport, 0))) {
29237 ast_log(LOG_NOTICE, "'%s' is not a valid port number for fromdomain.\n",fromdomainport);
29238 }
29239 } else {
29240 default_fromdomainport = STANDARD_SIP_PORT;
29241 }
29242 } else if (!strcasecmp(v->name, "outboundproxy")) {
29243 struct sip_proxy *proxy;
29244 if (ast_strlen_zero(v->value)) {
29245 ast_log(LOG_WARNING, "no value given for outbound proxy on line %d of sip.conf\n", v->lineno);
29246 continue;
29247 }
29248 proxy = proxy_from_config(v->value, v->lineno, &sip_cfg.outboundproxy);
29249 if (!proxy) {
29250 ast_log(LOG_WARNING, "failure parsing the outbound proxy on line %d of sip.conf.\n", v->lineno);
29251 continue;
29252 }
29253 } else if (!strcasecmp(v->name, "autocreatepeer")) {
29254 sip_cfg.autocreatepeer = ast_true(v->value);
29255 } else if (!strcasecmp(v->name, "match_auth_username")) {
29256 global_match_auth_username = ast_true(v->value);
29257 } else if (!strcasecmp(v->name, "srvlookup")) {
29258 sip_cfg.srvlookup = ast_true(v->value);
29259 } else if (!strcasecmp(v->name, "pedantic")) {
29260 sip_cfg.pedanticsipchecking = ast_true(v->value);
29261 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
29262 max_expiry = atoi(v->value);
29263 if (max_expiry < 1) {
29264 max_expiry = DEFAULT_MAX_EXPIRY;
29265 }
29266 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
29267 min_expiry = atoi(v->value);
29268 if (min_expiry < 1) {
29269 min_expiry = DEFAULT_MIN_EXPIRY;
29270 }
29271 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
29272 default_expiry = atoi(v->value);
29273 if (default_expiry < 1) {
29274 default_expiry = DEFAULT_DEFAULT_EXPIRY;
29275 }
29276 } else if (!strcasecmp(v->name, "mwiexpiry") || !strcasecmp(v->name, "mwiexpirey")) {
29277 mwi_expiry = atoi(v->value);
29278 if (mwi_expiry < 1) {
29279 mwi_expiry = DEFAULT_MWI_EXPIRY;
29280 }
29281 } else if (!strcasecmp(v->name, "tcpauthtimeout")) {
29282 if (ast_parse_arg(v->value, PARSE_INT32|PARSE_DEFAULT|PARSE_IN_RANGE,
29283 &authtimeout, DEFAULT_AUTHTIMEOUT, 1, INT_MAX)) {
29284 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n",
29285 v->name, v->value, v->lineno, config);
29286 }
29287 } else if (!strcasecmp(v->name, "tcpauthlimit")) {
29288 if (ast_parse_arg(v->value, PARSE_INT32|PARSE_DEFAULT|PARSE_IN_RANGE,
29289 &authlimit, DEFAULT_AUTHLIMIT, 1, INT_MAX)) {
29290 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n",
29291 v->name, v->value, v->lineno, config);
29292 }
29293 } else if (!strcasecmp(v->name, "sipdebug")) {
29294 if (ast_true(v->value))
29295 sipdebug |= sip_debug_config;
29296 } else if (!strcasecmp(v->name, "dumphistory")) {
29297 dumphistory = ast_true(v->value);
29298 } else if (!strcasecmp(v->name, "recordhistory")) {
29299 recordhistory = ast_true(v->value);
29300 } else if (!strcasecmp(v->name, "registertimeout")) {
29301 global_reg_timeout = atoi(v->value);
29302 if (global_reg_timeout < 1) {
29303 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
29304 }
29305 } else if (!strcasecmp(v->name, "registerattempts")) {
29306 global_regattempts_max = atoi(v->value);
29307 } else if (!strcasecmp(v->name, "register_retry_403")) {
29308 global_reg_retry_403 = ast_true(v->value);
29309 } else if (!strcasecmp(v->name, "bindaddr") || !strcasecmp(v->name, "udpbindaddr")) {
29310 if (ast_parse_arg(v->value, PARSE_ADDR, &bindaddr)) {
29311 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
29312 }
29313 } else if (!strcasecmp(v->name, "localnet")) {
29314 struct ast_ha *na;
29315 int ha_error = 0;
29316
29317 if (!(na = ast_append_ha("d", v->value, localaddr, &ha_error))) {
29318 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
29319 } else {
29320 localaddr = na;
29321 }
29322 if (ha_error) {
29323 ast_log(LOG_ERROR, "Bad localnet configuration value line %d : %s\n", v->lineno, v->value);
29324 }
29325 } else if (!strcasecmp(v->name, "media_address")) {
29326 if (ast_parse_arg(v->value, PARSE_ADDR, &media_address))
29327 ast_log(LOG_WARNING, "Invalid address for media_address keyword: %s\n", v->value);
29328 } else if (!strcasecmp(v->name, "externaddr") || !strcasecmp(v->name, "externip")) {
29329 if (ast_parse_arg(v->value, PARSE_ADDR, &externaddr)) {
29330 ast_log(LOG_WARNING,
29331 "Invalid address for externaddr keyword: %s\n",
29332 v->value);
29333 }
29334 externexpire = 0;
29335 } else if (!strcasecmp(v->name, "externhost")) {
29336 ast_copy_string(externhost, v->value, sizeof(externhost));
29337 if (ast_sockaddr_resolve_first(&externaddr, externhost, 0)) {
29338 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
29339 }
29340 externexpire = time(NULL);
29341 } else if (!strcasecmp(v->name, "externrefresh")) {
29342 if (sscanf(v->value, "%30d", &externrefresh) != 1) {
29343 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
29344 externrefresh = 10;
29345 }
29346 } else if (!strcasecmp(v->name, "externtcpport")) {
29347 if (!(externtcpport = port_str2int(v->value, 0))) {
29348 ast_log(LOG_WARNING, "Invalid externtcpport value, must be a positive integer between 1 and 65535 at line %d\n", v->lineno);
29349 externtcpport = 0;
29350 }
29351 } else if (!strcasecmp(v->name, "externtlsport")) {
29352 if (!(externtlsport = port_str2int(v->value, STANDARD_TLS_PORT))) {
29353 ast_log(LOG_WARNING, "Invalid externtlsport value, must be a positive integer between 1 and 65535 at line %d\n", v->lineno);
29354 }
29355 } else if (!strcasecmp(v->name, "allow")) {
29356 int error = ast_parse_allow_disallow(&default_prefs, &sip_cfg.capability, v->value, TRUE);
29357 if (error) {
29358 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
29359 }
29360 } else if (!strcasecmp(v->name, "disallow")) {
29361 int error = ast_parse_allow_disallow(&default_prefs, &sip_cfg.capability, v->value, FALSE);
29362 if (error) {
29363 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
29364 }
29365 } else if (!strcasecmp(v->name, "preferred_codec_only")) {
29366 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
29367 } else if (!strcasecmp(v->name, "autoframing")) {
29368 global_autoframing = ast_true(v->value);
29369 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
29370 sip_cfg.allow_external_domains = ast_true(v->value);
29371 } else if (!strcasecmp(v->name, "autodomain")) {
29372 auto_sip_domains = ast_true(v->value);
29373 } else if (!strcasecmp(v->name, "domain")) {
29374 char *domain = ast_strdupa(v->value);
29375 char *cntx = strchr(domain, ',');
29376
29377 if (cntx) {
29378 *cntx++ = '\0';
29379 }
29380
29381 if (ast_strlen_zero(cntx)) {
29382 ast_debug(1, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
29383 }
29384 if (ast_strlen_zero(domain)) {
29385 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
29386 } else {
29387 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, cntx ? ast_strip(cntx) : "");
29388 }
29389 } else if (!strcasecmp(v->name, "register")) {
29390 if (sip_register(v->value, v->lineno) == 0) {
29391 registry_count++;
29392 }
29393 } else if (!strcasecmp(v->name, "mwi")) {
29394 sip_subscribe_mwi(v->value, v->lineno);
29395 } else if (!strcasecmp(v->name, "tos_sip")) {
29396 if (ast_str2tos(v->value, &global_tos_sip)) {
29397 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, refer to QoS documentation\n", v->lineno);
29398 }
29399 } else if (!strcasecmp(v->name, "tos_audio")) {
29400 if (ast_str2tos(v->value, &global_tos_audio)) {
29401 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
29402 }
29403 } else if (!strcasecmp(v->name, "tos_video")) {
29404 if (ast_str2tos(v->value, &global_tos_video)) {
29405 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, refer to QoS documentation\n", v->lineno);
29406 }
29407 } else if (!strcasecmp(v->name, "tos_text")) {
29408 if (ast_str2tos(v->value, &global_tos_text)) {
29409 ast_log(LOG_WARNING, "Invalid tos_text value at line %d, refer to QoS documentation\n", v->lineno);
29410 }
29411 } else if (!strcasecmp(v->name, "cos_sip")) {
29412 if (ast_str2cos(v->value, &global_cos_sip)) {
29413 ast_log(LOG_WARNING, "Invalid cos_sip value at line %d, refer to QoS documentation\n", v->lineno);
29414 }
29415 } else if (!strcasecmp(v->name, "cos_audio")) {
29416 if (ast_str2cos(v->value, &global_cos_audio)) {
29417 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
29418 }
29419 } else if (!strcasecmp(v->name, "cos_video")) {
29420 if (ast_str2cos(v->value, &global_cos_video)) {
29421 ast_log(LOG_WARNING, "Invalid cos_video value at line %d, refer to QoS documentation\n", v->lineno);
29422 }
29423 } else if (!strcasecmp(v->name, "cos_text")) {
29424 if (ast_str2cos(v->value, &global_cos_text)) {
29425 ast_log(LOG_WARNING, "Invalid cos_text value at line %d, refer to QoS documentation\n", v->lineno);
29426 }
29427 } else if (!strcasecmp(v->name, "bindport")) {
29428 if (sscanf(v->value, "%5d", &bindport) != 1) {
29429 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
29430 }
29431 } else if (!strcasecmp(v->name, "qualify")) {
29432 if (!strcasecmp(v->value, "no")) {
29433 default_qualify = 0;
29434 } else if (!strcasecmp(v->value, "yes")) {
29435 default_qualify = DEFAULT_MAXMS;
29436 } else if (sscanf(v->value, "%30d", &default_qualify) != 1) {
29437 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
29438 default_qualify = 0;
29439 }
29440 } else if (!strcasecmp(v->name, "qualifyfreq")) {
29441 int i;
29442 if (sscanf(v->value, "%30d", &i) == 1) {
29443 global_qualifyfreq = i * 1000;
29444 } else {
29445 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
29446 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
29447 }
29448 } else if (!strcasecmp(v->name, "callevents")) {
29449 sip_cfg.callevents = ast_true(v->value);
29450 } else if (!strcasecmp(v->name, "authfailureevents")) {
29451 global_authfailureevents = ast_true(v->value);
29452 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
29453 default_maxcallbitrate = atoi(v->value);
29454 if (default_maxcallbitrate < 0)
29455 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
29456 } else if (!strcasecmp(v->name, "matchexternaddrlocally") || !strcasecmp(v->name, "matchexterniplocally")) {
29457 sip_cfg.matchexternaddrlocally = ast_true(v->value);
29458 } else if (!strcasecmp(v->name, "session-timers")) {
29459 int i = (int) str2stmode(v->value);
29460 if (i < 0) {
29461 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
29462 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
29463 } else {
29464 global_st_mode = i;
29465 }
29466 } else if (!strcasecmp(v->name, "session-expires")) {
29467 if (sscanf(v->value, "%30d", &global_max_se) != 1) {
29468 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
29469 global_max_se = DEFAULT_MAX_SE;
29470 }
29471 } else if (!strcasecmp(v->name, "session-minse")) {
29472 if (sscanf(v->value, "%30d", &global_min_se) != 1) {
29473 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
29474 global_min_se = DEFAULT_MIN_SE;
29475 }
29476 if (global_min_se < DEFAULT_MIN_SE) {
29477 ast_log(LOG_WARNING, "session-minse '%s' at line %d of %s is not allowed to be < %d secs\n", v->value, v->lineno, config, DEFAULT_MIN_SE);
29478 global_min_se = DEFAULT_MIN_SE;
29479 }
29480 } else if (!strcasecmp(v->name, "session-refresher")) {
29481 int i = (int) str2strefresherparam(v->value);
29482 if (i < 0) {
29483 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
29484 global_st_refresher = SESSION_TIMER_REFRESHER_PARAM_UAS;
29485 } else {
29486 global_st_refresher = i;
29487 }
29488 } else if (!strcasecmp(v->name, "storesipcause")) {
29489 global_store_sip_cause = ast_true(v->value);
29490 } else if (!strcasecmp(v->name, "qualifygap")) {
29491 if (sscanf(v->value, "%30d", &global_qualify_gap) != 1) {
29492 ast_log(LOG_WARNING, "Invalid qualifygap '%s' at line %d of %s\n", v->value, v->lineno, config);
29493 global_qualify_gap = DEFAULT_QUALIFY_GAP;
29494 }
29495 } else if (!strcasecmp(v->name, "qualifypeers")) {
29496 if (sscanf(v->value, "%30d", &global_qualify_peers) != 1) {
29497 ast_log(LOG_WARNING, "Invalid pokepeers '%s' at line %d of %s\n", v->value, v->lineno, config);
29498 global_qualify_peers = DEFAULT_QUALIFY_PEERS;
29499 }
29500 } else if (!strcasecmp(v->name, "disallowed_methods")) {
29501 char *disallow = ast_strdupa(v->value);
29502 mark_parsed_methods(&sip_cfg.disallowed_methods, disallow);
29503 } else if (!strcasecmp(v->name, "shrinkcallerid")) {
29504 if (ast_true(v->value)) {
29505 global_shrinkcallerid = 1;
29506 } else if (ast_false(v->value)) {
29507 global_shrinkcallerid = 0;
29508 } else {
29509 ast_log(LOG_WARNING, "shrinkcallerid value %s is not valid at line %d.\n", v->value, v->lineno);
29510 }
29511 } else if (!strcasecmp(v->name, "use_q850_reason")) {
29512 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_Q850_REASON);
29513 } else if (!strcasecmp(v->name, "maxforwards")) {
29514 if (sscanf(v->value, "%30d", &sip_cfg.default_max_forwards) != 1
29515 || sip_cfg.default_max_forwards < 1 || 255 < sip_cfg.default_max_forwards) {
29516 ast_log(LOG_WARNING, "'%s' is not a valid maxforwards value at line %d. Using default.\n", v->value, v->lineno);
29517 sip_cfg.default_max_forwards = DEFAULT_MAX_FORWARDS;
29518 }
29519 } else if (!strcasecmp(v->name, "subscribe_network_change_event")) {
29520 if (ast_true(v->value)) {
29521 subscribe_network_change = 1;
29522 } else if (ast_false(v->value)) {
29523 subscribe_network_change = 0;
29524 } else {
29525 ast_log(LOG_WARNING, "subscribe_network_change_event value %s is not valid at line %d.\n", v->value, v->lineno);
29526 }
29527 } else if (!strcasecmp(v->name, "snom_aoc_enabled")) {
29528 ast_set2_flag(&global_flags[2], ast_true(v->value), SIP_PAGE3_SNOM_AOC);
29529 } else if (!strcasecmp(v->name, "parkinglot")) {
29530 ast_copy_string(default_parkinglot, v->value, sizeof(default_parkinglot));
29531 }
29532 }
29533
29534
29535 ast_copy_flags(&global_flags[0], &setflags[0], mask[0].flags);
29536 ast_copy_flags(&global_flags[1], &setflags[1], mask[1].flags);
29537 ast_copy_flags(&global_flags[2], &setflags[2], mask[2].flags);
29538
29539 if (subscribe_network_change) {
29540 network_change_event_subscribe();
29541 } else {
29542 network_change_event_unsubscribe();
29543 }
29544
29545 if (global_t1 < global_t1min) {
29546 ast_log(LOG_WARNING, "'t1min' (%d) cannot be greater than 't1timer' (%d). Resetting 't1timer' to the value of 't1min'\n", global_t1min, global_t1);
29547 global_t1 = global_t1min;
29548 }
29549
29550 if (global_timer_b < global_t1 * 64) {
29551 if (timerb_set && timert1_set) {
29552 ast_log(LOG_WARNING, "Timer B has been set lower than recommended (%d < 64 * timert1=%d). (RFC 3261, 17.1.1.2)\n", global_timer_b, global_t1);
29553 } else if (timerb_set) {
29554 if ((global_t1 = global_timer_b / 64) < global_t1min) {
29555 ast_log(LOG_WARNING, "Timer B has been set lower than recommended (%d < 64 * timert1=%d). (RFC 3261, 17.1.1.2)\n", global_timer_b, global_t1);
29556 global_t1 = global_t1min;
29557 global_timer_b = global_t1 * 64;
29558 }
29559 } else {
29560 global_timer_b = global_t1 * 64;
29561 }
29562 }
29563 if (!sip_cfg.allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
29564 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
29565 sip_cfg.allow_external_domains = 1;
29566 }
29567
29568 if (!sip_cfg.tcp_enabled && (default_transports & SIP_TRANSPORT_TCP)) {
29569 ast_log(LOG_WARNING, "Cannot use 'tcp' transport with tcpenable=no. Removing from available transports.\n");
29570 default_primary_transport &= ~SIP_TRANSPORT_TCP;
29571 default_transports &= ~SIP_TRANSPORT_TCP;
29572 }
29573 if (!default_tls_cfg.enabled && (default_transports & SIP_TRANSPORT_TLS)) {
29574 ast_log(LOG_WARNING, "Cannot use 'tls' transport with tlsenable=no. Removing from available transports.\n");
29575 default_primary_transport &= ~SIP_TRANSPORT_TLS;
29576 default_transports &= ~SIP_TRANSPORT_TLS;
29577 }
29578 if (!default_transports) {
29579 ast_log(LOG_WARNING, "No valid transports available, falling back to 'udp'.\n");
29580 default_transports = default_primary_transport = SIP_TRANSPORT_UDP;
29581 } else if (!default_primary_transport) {
29582 ast_log(LOG_WARNING, "No valid default transport. Selecting 'udp' as default.\n");
29583 default_primary_transport = SIP_TRANSPORT_UDP;
29584 }
29585
29586
29587 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
29588
29589 if (!strcasecmp(v->name, "auth")) {
29590 add_realm_authentication(&authl, v->value, v->lineno);
29591 }
29592 }
29593
29594 if (bindport) {
29595 if (ast_sockaddr_port(&bindaddr)) {
29596 ast_log(LOG_WARNING, "bindport is also specified in bindaddr. "
29597 "Using %d.\n", bindport);
29598 }
29599 ast_sockaddr_set_port(&bindaddr, bindport);
29600 }
29601
29602 if (!ast_sockaddr_port(&bindaddr)) {
29603 ast_sockaddr_set_port(&bindaddr, STANDARD_SIP_PORT);
29604 }
29605
29606
29607 ast_sockaddr_copy(&internip, &bindaddr);
29608 if (ast_find_ourip(&internip, &bindaddr, 0)) {
29609 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
29610 ast_config_destroy(cfg);
29611 return 0;
29612 }
29613
29614 ast_mutex_lock(&netlock);
29615 if ((sipsock > -1) && (ast_sockaddr_cmp(&old_bindaddr, &bindaddr))) {
29616 close(sipsock);
29617 sipsock = -1;
29618 }
29619 if (sipsock < 0) {
29620 sipsock = socket(ast_sockaddr_is_ipv6(&bindaddr) ?
29621 AF_INET6 : AF_INET, SOCK_DGRAM, 0);
29622 if (sipsock < 0) {
29623 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
29624 ast_config_destroy(cfg);
29625 ast_mutex_unlock(&netlock);
29626 return -1;
29627 } else {
29628
29629 const int reuseFlag = 1;
29630
29631 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
29632 (const char*)&reuseFlag,
29633 sizeof reuseFlag);
29634
29635 ast_enable_packet_fragmentation(sipsock);
29636
29637 if (ast_bind(sipsock, &bindaddr) < 0) {
29638 ast_log(LOG_WARNING, "Failed to bind to %s: %s\n",
29639 ast_sockaddr_stringify(&bindaddr), strerror(errno));
29640 close(sipsock);
29641 sipsock = -1;
29642 } else {
29643 ast_verb(2, "SIP Listening on %s\n", ast_sockaddr_stringify(&bindaddr));
29644 ast_set_qos(sipsock, global_tos_sip, global_cos_sip, "SIP");
29645 }
29646 }
29647 } else {
29648 ast_set_qos(sipsock, global_tos_sip, global_cos_sip, "SIP");
29649 }
29650 ast_mutex_unlock(&netlock);
29651
29652
29653 if (sip_cfg.tcp_enabled) {
29654 if (ast_sockaddr_isnull(&sip_tcp_desc.local_address)) {
29655 ast_sockaddr_copy(&sip_tcp_desc.local_address, &bindaddr);
29656 }
29657 if (!ast_sockaddr_port(&sip_tcp_desc.local_address)) {
29658 ast_sockaddr_set_port(&sip_tcp_desc.local_address, STANDARD_SIP_PORT);
29659 }
29660 } else {
29661 ast_sockaddr_setnull(&sip_tcp_desc.local_address);
29662 }
29663 ast_tcptls_server_start(&sip_tcp_desc);
29664 if (sip_cfg.tcp_enabled && sip_tcp_desc.accept_fd == -1) {
29665
29666 ast_log(LOG_ERROR, "SIP TCP Server start failed. Not listening on TCP socket.\n");
29667 } else {
29668 ast_debug(2, "SIP TCP server started\n");
29669 }
29670
29671
29672 memcpy(sip_tls_desc.tls_cfg, &default_tls_cfg, sizeof(default_tls_cfg));
29673
29674 if (ast_ssl_setup(sip_tls_desc.tls_cfg)) {
29675 if (ast_sockaddr_isnull(&sip_tls_desc.local_address)) {
29676 ast_sockaddr_copy(&sip_tls_desc.local_address, &bindaddr);
29677 ast_sockaddr_set_port(&sip_tls_desc.local_address,
29678 STANDARD_TLS_PORT);
29679 }
29680 if (!ast_sockaddr_port(&sip_tls_desc.local_address)) {
29681 ast_sockaddr_set_port(&sip_tls_desc.local_address,
29682 STANDARD_TLS_PORT);
29683 }
29684 ast_tcptls_server_start(&sip_tls_desc);
29685 if (default_tls_cfg.enabled && sip_tls_desc.accept_fd == -1) {
29686 ast_log(LOG_ERROR, "TLS Server start failed. Not listening on TLS socket.\n");
29687 sip_tls_desc.tls_cfg = NULL;
29688 }
29689 } else if (sip_tls_desc.tls_cfg->enabled) {
29690 sip_tls_desc.tls_cfg = NULL;
29691 ast_log(LOG_WARNING, "SIP TLS server did not load because of errors.\n");
29692 }
29693
29694 if (ucfg) {
29695 struct ast_variable *gen;
29696 int genhassip, genregistersip;
29697 const char *hassip, *registersip;
29698
29699 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
29700 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
29701 gen = ast_variable_browse(ucfg, "general");
29702 cat = ast_category_browse(ucfg, NULL);
29703 while (cat) {
29704 if (strcasecmp(cat, "general")) {
29705 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
29706 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
29707 if (ast_true(hassip) || (!hassip && genhassip)) {
29708 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0, 0);
29709 if (peer) {
29710
29711 peer->type = SIP_TYPE_USER | SIP_TYPE_PEER;
29712 ao2_t_link(peers, peer, "link peer into peer table");
29713 if ((peer->type & SIP_TYPE_PEER) && !ast_sockaddr_isnull(&peer->addr)) {
29714 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
29715 }
29716
29717 unref_peer(peer, "unref_peer: from reload_config");
29718 peer_count++;
29719 }
29720 }
29721 if (ast_true(registersip) || (!registersip && genregistersip)) {
29722 char tmp[256];
29723 const char *host = ast_variable_retrieve(ucfg, cat, "host");
29724 const char *username = ast_variable_retrieve(ucfg, cat, "username");
29725 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
29726 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
29727 const char *authuser = ast_variable_retrieve(ucfg, cat, "authuser");
29728 if (!host) {
29729 host = ast_variable_retrieve(ucfg, "general", "host");
29730 }
29731 if (!username) {
29732 username = ast_variable_retrieve(ucfg, "general", "username");
29733 }
29734 if (!secret) {
29735 secret = ast_variable_retrieve(ucfg, "general", "secret");
29736 }
29737 if (!contact) {
29738 contact = "s";
29739 }
29740 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
29741 if (!ast_strlen_zero(secret)) {
29742 if (!ast_strlen_zero(authuser)) {
29743 snprintf(tmp, sizeof(tmp), "%s?%s:%s:%s@%s/%s", cat, username, secret, authuser, host, contact);
29744 } else {
29745 snprintf(tmp, sizeof(tmp), "%s?%s:%s@%s/%s", cat, username, secret, host, contact);
29746 }
29747 } else if (!ast_strlen_zero(authuser)) {
29748 snprintf(tmp, sizeof(tmp), "%s?%s::%s@%s/%s", cat, username, authuser, host, contact);
29749 } else {
29750 snprintf(tmp, sizeof(tmp), "%s?%s@%s/%s", cat, username, host, contact);
29751 }
29752 if (sip_register(tmp, 0) == 0) {
29753 registry_count++;
29754 }
29755 }
29756 }
29757 }
29758 cat = ast_category_browse(ucfg, cat);
29759 }
29760 ast_config_destroy(ucfg);
29761 }
29762
29763
29764 cat = NULL;
29765 while ( (cat = ast_category_browse(cfg, cat)) ) {
29766 const char *utype;
29767 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
29768 continue;
29769 utype = ast_variable_retrieve(cfg, cat, "type");
29770 if (!utype) {
29771 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
29772 continue;
29773 } else {
29774 if (!strcasecmp(utype, "user")) {
29775 ;
29776 } else if (!strcasecmp(utype, "friend")) {
29777 ;
29778 } else if (!strcasecmp(utype, "peer")) {
29779 ;
29780 } else {
29781 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
29782 continue;
29783 }
29784 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0, 0);
29785 if (peer) {
29786 display_nat_warning(cat, reason, &peer->flags[0]);
29787 ao2_t_link(peers, peer, "link peer into peers table");
29788 if ((peer->type & SIP_TYPE_PEER) && !ast_sockaddr_isnull(&peer->addr)) {
29789 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
29790 }
29791 unref_peer(peer, "unref the result of the build_peer call. Now, the links from the tables are the only ones left.");
29792 peer_count++;
29793 }
29794 }
29795 }
29796
29797
29798
29799
29800
29801
29802 if (auto_sip_domains) {
29803 char temp[MAXHOSTNAMELEN];
29804
29805
29806 if (!ast_sockaddr_isnull(&bindaddr) && !ast_sockaddr_is_any(&bindaddr)) {
29807 add_sip_domain(ast_sockaddr_stringify_addr(&bindaddr),
29808 SIP_DOMAIN_AUTO, NULL);
29809 } else if (!ast_sockaddr_isnull(&internip) && !ast_sockaddr_is_any(&internip)) {
29810
29811 add_sip_domain(ast_sockaddr_stringify_addr(&internip),
29812 SIP_DOMAIN_AUTO, NULL);
29813 } else {
29814 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
29815 }
29816
29817
29818 if (!ast_sockaddr_isnull(&sip_tcp_desc.local_address) &&
29819 !ast_sockaddr_cmp(&bindaddr, &sip_tcp_desc.local_address)) {
29820 add_sip_domain(ast_sockaddr_stringify_addr(&sip_tcp_desc.local_address),
29821 SIP_DOMAIN_AUTO, NULL);
29822 }
29823
29824
29825 if (!ast_sockaddr_isnull(&sip_tls_desc.local_address) &&
29826 !ast_sockaddr_cmp(&bindaddr, &sip_tls_desc.local_address) &&
29827 !ast_sockaddr_cmp(&sip_tcp_desc.local_address,
29828 &sip_tls_desc.local_address)) {
29829 add_sip_domain(ast_sockaddr_stringify_addr(&sip_tcp_desc.local_address),
29830 SIP_DOMAIN_AUTO, NULL);
29831 }
29832
29833
29834 if (!ast_sockaddr_isnull(&externaddr)) {
29835 add_sip_domain(ast_sockaddr_stringify_addr(&externaddr),
29836 SIP_DOMAIN_AUTO, NULL);
29837 }
29838
29839
29840 if (!ast_strlen_zero(externhost)) {
29841 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
29842 }
29843
29844
29845 if (!gethostname(temp, sizeof(temp))) {
29846 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
29847 }
29848 }
29849
29850
29851 ast_config_destroy(cfg);
29852
29853
29854 if (notify_types)
29855 ast_config_destroy(notify_types);
29856 if ((notify_types = ast_config_load(notify_config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
29857 ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed.\n", notify_config);
29858 notify_types = NULL;
29859 }
29860
29861
29862 manager_event(EVENT_FLAG_SYSTEM, "ChannelReload", "ChannelType: SIP\r\nReloadReason: %s\r\nRegistry_Count: %d\r\nPeer_Count: %d\r\n", channelreloadreason2txt(reason), registry_count, peer_count);
29863 run_end = time(0);
29864 ast_debug(4, "SIP reload_config done...Runtime= %d sec\n", (int)(run_end-run_start));
29865
29866 return 0;
29867 }
29868
29869 static int apply_directmedia_ha(struct sip_pvt *p1, struct sip_pvt *p2, const char *op)
29870 {
29871 struct ast_sockaddr us = { { 0, }, }, them = { { 0, }, };
29872 int res = AST_SENSE_ALLOW;
29873
29874 ast_rtp_instance_get_remote_address(p1->rtp, &them);
29875 ast_rtp_instance_get_local_address(p1->rtp, &us);
29876
29877
29878
29879 if (!p2->relatedpeer) {
29880 return res;
29881 }
29882
29883 if ((res = ast_apply_ha(p2->relatedpeer->directmediaha, &them)) == AST_SENSE_DENY) {
29884 const char *us_addr = ast_strdupa(ast_sockaddr_stringify(&us));
29885 const char *them_addr = ast_strdupa(ast_sockaddr_stringify(&them));
29886
29887 ast_debug(3, "Reinvite %s to %s denied by directmedia ACL on %s\n",
29888 op, them_addr, us_addr);
29889 }
29890
29891 return res;
29892 }
29893
29894 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
29895 {
29896 struct sip_pvt *p;
29897 struct ast_channel *opp_chan;
29898 struct sip_pvt *opp;
29899 struct ast_udptl *udptl = NULL;
29900
29901 p = chan->tech_pvt;
29902 if (!p) {
29903 return NULL;
29904 }
29905
29906 if (!(opp_chan = ast_bridged_channel(chan))) {
29907 return NULL;
29908 } else if (((opp_chan->tech != &sip_tech) && (opp_chan->tech != &sip_tech_info)) ||
29909 (!(opp = opp_chan->tech_pvt))) {
29910 return NULL;
29911 }
29912
29913 sip_pvt_lock(p);
29914 while (sip_pvt_trylock(opp)) {
29915 sip_pvt_unlock(p);
29916 usleep(1);
29917 sip_pvt_lock(p);
29918 }
29919
29920 if (p->udptl && ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
29921 if (apply_directmedia_ha(p, opp, "UDPTL T.38 data")) {
29922 udptl = p->udptl;
29923 }
29924 }
29925
29926 sip_pvt_unlock(opp);
29927 sip_pvt_unlock(p);
29928 return udptl;
29929 }
29930
29931 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
29932 {
29933 struct sip_pvt *p;
29934
29935
29936 ast_channel_lock(chan);
29937 p = chan->tech_pvt;
29938 if (!p) {
29939 ast_channel_unlock(chan);
29940 return -1;
29941 }
29942 sip_pvt_lock(p);
29943 if (p->owner != chan) {
29944
29945 ast_debug(1, "The private is not owned by channel %s anymore.\n", chan->name);
29946 sip_pvt_unlock(p);
29947 ast_channel_unlock(chan);
29948 return 0;
29949 }
29950
29951 if (udptl) {
29952 ast_udptl_get_peer(udptl, &p->udptlredirip);
29953 } else {
29954 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
29955 }
29956 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
29957 if (!p->pendinginvite) {
29958 ast_debug(3, "Sending reinvite on SIP '%s' - It's UDPTL soon redirected to IP %s\n",
29959 p->callid, ast_sockaddr_stringify(udptl ? &p->udptlredirip : &p->ourip));
29960 transmit_reinvite_with_sdp(p, TRUE, FALSE);
29961 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
29962 ast_debug(3, "Deferring reinvite on SIP '%s' - It's UDPTL will be redirected to IP %s\n",
29963 p->callid, ast_sockaddr_stringify(udptl ? &p->udptlredirip : &p->ourip));
29964 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
29965 }
29966 }
29967
29968 p->lastrtprx = p->lastrtptx = time(NULL);
29969 sip_pvt_unlock(p);
29970 ast_channel_unlock(chan);
29971 return 0;
29972 }
29973
29974 static enum ast_rtp_glue_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
29975 {
29976 struct sip_pvt *p = NULL;
29977 struct ast_channel *opp_chan;
29978 struct sip_pvt *opp = NULL;
29979 enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
29980
29981 if (!(p = chan->tech_pvt)) {
29982 return AST_RTP_GLUE_RESULT_FORBID;
29983 }
29984
29985 if ((opp_chan = ast_bridged_channel(chan)) && (((opp_chan->tech != &sip_tech) && (opp_chan->tech != &sip_tech_info)) ||
29986 (!(opp = opp_chan->tech_pvt)))) {
29987 return AST_RTP_GLUE_RESULT_FORBID;
29988 }
29989
29990 sip_pvt_lock(p);
29991 while (opp && sip_pvt_trylock(opp)) {
29992 sip_pvt_unlock(p);
29993 usleep(1);
29994 sip_pvt_lock(p);
29995 }
29996
29997 if (!(p->rtp)) {
29998 if (opp) {
29999 sip_pvt_unlock(opp);
30000 }
30001 sip_pvt_unlock(p);
30002 return AST_RTP_GLUE_RESULT_FORBID;
30003 }
30004
30005 ao2_ref(p->rtp, +1);
30006 *instance = p->rtp;
30007
30008 if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
30009 res = AST_RTP_GLUE_RESULT_REMOTE;
30010 if (opp && !apply_directmedia_ha(p, opp, "audio")) {
30011 res = AST_RTP_GLUE_RESULT_FORBID;
30012 }
30013 } else if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA_NAT)) {
30014 res = AST_RTP_GLUE_RESULT_REMOTE;
30015 } else if (ast_test_flag(&global_jbconf, AST_JB_FORCED)) {
30016 res = AST_RTP_GLUE_RESULT_FORBID;
30017 }
30018
30019 if (opp) {
30020 sip_pvt_unlock(opp);
30021 }
30022
30023 if (p->srtp) {
30024 res = AST_RTP_GLUE_RESULT_FORBID;
30025 }
30026
30027 sip_pvt_unlock(p);
30028
30029 return res;
30030 }
30031
30032 static enum ast_rtp_glue_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
30033 {
30034 struct sip_pvt *p = NULL;
30035 struct ast_channel *opp_chan;
30036 struct sip_pvt *opp = NULL;
30037 enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
30038
30039 if (!(p = chan->tech_pvt)) {
30040 return AST_RTP_GLUE_RESULT_FORBID;
30041 }
30042
30043 if ((opp_chan = ast_bridged_channel(chan)) && (((opp_chan->tech != &sip_tech) && (opp_chan->tech != &sip_tech_info)) ||
30044 (!(opp = opp_chan->tech_pvt)))) {
30045 return AST_RTP_GLUE_RESULT_FORBID;
30046 }
30047
30048 sip_pvt_lock(p);
30049 while (opp && sip_pvt_trylock(opp)) {
30050 sip_pvt_unlock(p);
30051 usleep(1);
30052 sip_pvt_lock(p);
30053 }
30054
30055 if (!(p->vrtp)) {
30056 if (opp) {
30057 sip_pvt_unlock(opp);
30058 }
30059 sip_pvt_unlock(p);
30060 return AST_RTP_GLUE_RESULT_FORBID;
30061 }
30062
30063 ao2_ref(p->vrtp, +1);
30064 *instance = p->vrtp;
30065
30066 if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
30067 res = AST_RTP_GLUE_RESULT_REMOTE;
30068 if (opp && !apply_directmedia_ha(p, opp, "video")) {
30069 res = AST_RTP_GLUE_RESULT_FORBID;
30070 }
30071 }
30072
30073 if (opp) {
30074 sip_pvt_unlock(opp);
30075 }
30076 sip_pvt_unlock(p);
30077
30078 return res;
30079 }
30080
30081 static enum ast_rtp_glue_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
30082 {
30083 struct sip_pvt *p = NULL;
30084 struct ast_channel *opp_chan;
30085 struct sip_pvt *opp = NULL;
30086 enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
30087
30088 if (!(p = chan->tech_pvt)) {
30089 return AST_RTP_GLUE_RESULT_FORBID;
30090 }
30091
30092 if ((opp_chan = ast_bridged_channel(chan)) && (((opp_chan->tech != &sip_tech) && (opp_chan->tech != &sip_tech_info)) ||
30093 (!(opp = opp_chan->tech_pvt)))) {
30094 return AST_RTP_GLUE_RESULT_FORBID;
30095 }
30096
30097 sip_pvt_lock(p);
30098 while (opp && sip_pvt_trylock(opp)) {
30099 sip_pvt_unlock(p);
30100 usleep(1);
30101 sip_pvt_lock(p);
30102 }
30103
30104 if (!(p->trtp)) {
30105 if (opp) {
30106 sip_pvt_unlock(opp);
30107 }
30108 sip_pvt_unlock(p);
30109 return AST_RTP_GLUE_RESULT_FORBID;
30110 }
30111
30112 ao2_ref(p->trtp, +1);
30113 *instance = p->trtp;
30114
30115 if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
30116 res = AST_RTP_GLUE_RESULT_REMOTE;
30117 if (opp && !apply_directmedia_ha(p, opp, "text")) {
30118 res = AST_RTP_GLUE_RESULT_FORBID;
30119 }
30120 }
30121
30122 if (opp) {
30123 sip_pvt_unlock(opp);
30124 }
30125 sip_pvt_unlock(p);
30126
30127 return res;
30128 }
30129
30130 static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, format_t codecs, int nat_active)
30131 {
30132 struct sip_pvt *p;
30133 int changed = 0;
30134
30135
30136 ast_channel_lock(chan);
30137 p = chan->tech_pvt;
30138 if (!p) {
30139 ast_channel_unlock(chan);
30140 return -1;
30141 }
30142 sip_pvt_lock(p);
30143 if (p->owner != chan) {
30144
30145 ast_debug(1, "The private is not owned by channel %s anymore.\n", chan->name);
30146 sip_pvt_unlock(p);
30147 ast_channel_unlock(chan);
30148 return 0;
30149 }
30150
30151
30152 if ((instance || vinstance || tinstance) &&
30153 !ast_bridged_channel(chan) &&
30154 !sip_cfg.directrtpsetup) {
30155 sip_pvt_unlock(p);
30156 ast_channel_unlock(chan);
30157 return 0;
30158 }
30159
30160 if (p->alreadygone) {
30161
30162 sip_pvt_unlock(p);
30163 ast_channel_unlock(chan);
30164 return 0;
30165 }
30166
30167
30168
30169
30170 if (nat_active && !ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA_NAT)) {
30171 sip_pvt_unlock(p);
30172 ast_channel_unlock(chan);
30173 return 0;
30174 }
30175
30176 if (instance) {
30177 changed |= ast_rtp_instance_get_and_cmp_remote_address(instance, &p->redirip);
30178
30179 if (p->rtp) {
30180
30181 ast_channel_set_fd(chan, 1, -1);
30182
30183 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 0);
30184 }
30185 } else if (!ast_sockaddr_isnull(&p->redirip)) {
30186 memset(&p->redirip, 0, sizeof(p->redirip));
30187 changed = 1;
30188
30189 if (p->rtp) {
30190
30191
30192 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 1);
30193
30194 ast_channel_set_fd(chan, 1, ast_rtp_instance_fd(p->rtp, 1));
30195 }
30196 }
30197
30198 if (vinstance) {
30199 changed |= ast_rtp_instance_get_and_cmp_remote_address(vinstance, &p->vredirip);
30200
30201 if (p->vrtp) {
30202
30203 ast_channel_set_fd(chan, 3, -1);
30204
30205 ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_RTCP, 0);
30206 }
30207 } else if (!ast_sockaddr_isnull(&p->vredirip)) {
30208 memset(&p->vredirip, 0, sizeof(p->vredirip));
30209 changed = 1;
30210
30211 if (p->vrtp) {
30212
30213
30214 ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_RTCP, 1);
30215
30216 ast_channel_set_fd(chan, 3, ast_rtp_instance_fd(p->vrtp, 1));
30217 }
30218 }
30219
30220 if (tinstance) {
30221 changed |= ast_rtp_instance_get_and_cmp_remote_address(tinstance, &p->tredirip);
30222 } else if (!ast_sockaddr_isnull(&p->tredirip)) {
30223 memset(&p->tredirip, 0, sizeof(p->tredirip));
30224 changed = 1;
30225 }
30226 if (codecs && (p->redircodecs != codecs)) {
30227 p->redircodecs = codecs;
30228 changed = 1;
30229 }
30230
30231 if (ast_test_flag(&p->flags[2], SIP_PAGE3_DIRECT_MEDIA_OUTGOING) && !p->outgoing_call) {
30232
30233
30234
30235
30236 ast_clear_flag(&p->flags[2], SIP_PAGE3_DIRECT_MEDIA_OUTGOING);
30237 sip_pvt_unlock(p);
30238 ast_channel_unlock(chan);
30239 return 0;
30240 }
30241
30242 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
30243 if (chan->_state != AST_STATE_UP) {
30244 if (p->do_history)
30245 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
30246 ast_debug(1, "Early remote bridge setting SIP '%s' - Sending media to %s\n", p->callid, ast_sockaddr_stringify(instance ? &p->redirip : &p->ourip));
30247 } else if (!p->pendinginvite) {
30248 ast_debug(3, "Sending reinvite on SIP '%s' - It's audio soon redirected to IP %s\n", p->callid, ast_sockaddr_stringify(instance ? &p->redirip : &p->ourip));
30249 transmit_reinvite_with_sdp(p, FALSE, FALSE);
30250 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
30251 ast_debug(3, "Deferring reinvite on SIP '%s' - It's audio will be redirected to IP %s\n", p->callid, ast_sockaddr_stringify(instance ? &p->redirip : &p->ourip));
30252
30253 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
30254 }
30255 }
30256
30257 p->lastrtprx = p->lastrtptx = time(NULL);
30258 sip_pvt_unlock(p);
30259 ast_channel_unlock(chan);
30260 return 0;
30261 }
30262
30263 static format_t sip_get_codec(struct ast_channel *chan)
30264 {
30265 struct sip_pvt *p = chan->tech_pvt;
30266 return p->peercapability ? p->peercapability : p->capability;
30267 }
30268
30269 static struct ast_rtp_glue sip_rtp_glue = {
30270 .type = "SIP",
30271 .get_rtp_info = sip_get_rtp_peer,
30272 .get_vrtp_info = sip_get_vrtp_peer,
30273 .get_trtp_info = sip_get_trtp_peer,
30274 .update_peer = sip_set_rtp_peer,
30275 .get_codec = sip_get_codec,
30276 };
30277
30278 static char *app_dtmfmode = "SIPDtmfMode";
30279 static char *app_sipaddheader = "SIPAddHeader";
30280 static char *app_sipremoveheader = "SIPRemoveHeader";
30281
30282
30283 static int sip_dtmfmode(struct ast_channel *chan, const char *data)
30284 {
30285 struct sip_pvt *p;
30286 const char *mode = data;
30287
30288 if (!data) {
30289 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
30290 return 0;
30291 }
30292 ast_channel_lock(chan);
30293 if (!IS_SIP_TECH(chan->tech)) {
30294 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
30295 ast_channel_unlock(chan);
30296 return 0;
30297 }
30298 p = chan->tech_pvt;
30299 if (!p) {
30300 ast_channel_unlock(chan);
30301 return 0;
30302 }
30303 sip_pvt_lock(p);
30304 if (!strcasecmp(mode, "info")) {
30305 ast_clear_flag(&p->flags[0], SIP_DTMF);
30306 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
30307 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
30308 } else if (!strcasecmp(mode, "shortinfo")) {
30309 ast_clear_flag(&p->flags[0], SIP_DTMF);
30310 ast_set_flag(&p->flags[0], SIP_DTMF_SHORTINFO);
30311 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
30312 } else if (!strcasecmp(mode, "rfc2833")) {
30313 ast_clear_flag(&p->flags[0], SIP_DTMF);
30314 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
30315 p->jointnoncodeccapability |= AST_RTP_DTMF;
30316 } else if (!strcasecmp(mode, "inband")) {
30317 ast_clear_flag(&p->flags[0], SIP_DTMF);
30318 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
30319 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
30320 } else {
30321 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n", mode);
30322 }
30323 if (p->rtp)
30324 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
30325 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
30326 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
30327 enable_dsp_detect(p);
30328 } else {
30329 disable_dsp_detect(p);
30330 }
30331 sip_pvt_unlock(p);
30332 ast_channel_unlock(chan);
30333 return 0;
30334 }
30335
30336
30337 static int sip_addheader(struct ast_channel *chan, const char *data)
30338 {
30339 int no = 0;
30340 int ok = FALSE;
30341 char varbuf[30];
30342 const char *inbuf = data;
30343 char *subbuf;
30344
30345 if (ast_strlen_zero(inbuf)) {
30346 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
30347 return 0;
30348 }
30349 ast_channel_lock(chan);
30350
30351
30352 while (!ok && no <= 50) {
30353 no++;
30354 snprintf(varbuf, sizeof(varbuf), "__SIPADDHEADER%.2d", no);
30355
30356
30357 if ((pbx_builtin_getvar_helper(chan, (const char *) varbuf + 2) == (const char *) NULL)) {
30358 ok = TRUE;
30359 }
30360 }
30361 if (ok) {
30362 size_t len = strlen(inbuf);
30363 subbuf = ast_alloca(len + 1);
30364 ast_get_encoded_str(inbuf, subbuf, len + 1);
30365 pbx_builtin_setvar_helper(chan, varbuf, subbuf);
30366 if (sipdebug) {
30367 ast_debug(1, "SIP Header added \"%s\" as %s\n", inbuf, varbuf);
30368 }
30369 } else {
30370 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
30371 }
30372 ast_channel_unlock(chan);
30373 return 0;
30374 }
30375
30376
30377 static int sip_removeheader(struct ast_channel *chan, const char *data)
30378 {
30379 struct ast_var_t *newvariable;
30380 struct varshead *headp;
30381 int removeall = 0;
30382 char *inbuf = (char *) data;
30383
30384 if (ast_strlen_zero(inbuf)) {
30385 removeall = 1;
30386 }
30387 ast_channel_lock(chan);
30388
30389 headp=&chan->varshead;
30390 AST_LIST_TRAVERSE_SAFE_BEGIN (headp, newvariable, entries) {
30391 if (strncasecmp(ast_var_name(newvariable), "SIPADDHEADER", strlen("SIPADDHEADER")) == 0) {
30392 if (removeall || (!strncasecmp(ast_var_value(newvariable),inbuf,strlen(inbuf)))) {
30393 if (sipdebug)
30394 ast_debug(1,"removing SIP Header \"%s\" as %s\n",
30395 ast_var_value(newvariable),
30396 ast_var_name(newvariable));
30397 AST_LIST_REMOVE_CURRENT(entries);
30398 ast_var_delete(newvariable);
30399 }
30400 }
30401 }
30402 AST_LIST_TRAVERSE_SAFE_END;
30403
30404 ast_channel_unlock(chan);
30405 return 0;
30406 }
30407
30408
30409
30410
30411
30412
30413
30414 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
30415 {
30416 char *cdest;
30417 char *extension, *domain;
30418
30419 cdest = ast_strdupa(dest);
30420
30421 extension = strsep(&cdest, "@");
30422 domain = cdest;
30423 if (ast_strlen_zero(extension)) {
30424 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
30425 return 0;
30426 }
30427
30428
30429 if (!domain) {
30430 char *local_to_header;
30431 char to_header[256];
30432
30433 ast_copy_string(to_header, get_header(&p->initreq, "To"), sizeof(to_header));
30434 if (ast_strlen_zero(to_header)) {
30435 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
30436 return 0;
30437 }
30438 if (((local_to_header = strcasestr(to_header, "sip:")) || (local_to_header = strcasestr(to_header, "sips:")))
30439 && (local_to_header = strchr(local_to_header, '@'))) {
30440 char ldomain[256];
30441
30442 memset(ldomain, 0, sizeof(ldomain));
30443 local_to_header++;
30444
30445 sscanf(local_to_header, "%256[^<>; ]", ldomain);
30446 if (ast_strlen_zero(ldomain)) {
30447 ast_log(LOG_ERROR, "Can't find the host address\n");
30448 return 0;
30449 }
30450 domain = ast_strdupa(ldomain);
30451 }
30452 }
30453
30454 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s>", extension, domain);
30455 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
30456
30457 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
30458 sip_alreadygone(p);
30459
30460 if (p->owner) {
30461 enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
30462 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
30463 }
30464
30465 return 0;
30466 }
30467
30468 static int sip_is_xml_parsable(void)
30469 {
30470 #ifdef HAVE_LIBXML2
30471 return TRUE;
30472 #else
30473 return FALSE;
30474 #endif
30475 }
30476
30477
30478 static void sip_poke_all_peers(void)
30479 {
30480 int ms = 0, num = 0;
30481 struct ao2_iterator i;
30482 struct sip_peer *peer;
30483
30484 if (!speerobjs) {
30485 return;
30486 }
30487
30488 i = ao2_iterator_init(peers, 0);
30489 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
30490 ao2_lock(peer);
30491
30492 if (peer->maxms) {
30493 if (num == global_qualify_peers) {
30494 ms += global_qualify_gap;
30495 num = 0;
30496 } else {
30497 num++;
30498 }
30499 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, ms, sip_poke_peer_s, peer,
30500 unref_peer(_data, "removing poke peer ref"),
30501 unref_peer(peer, "removing poke peer ref"),
30502 ref_peer(peer, "adding poke peer ref"));
30503 }
30504 ao2_unlock(peer);
30505 unref_peer(peer, "toss iterator peer ptr");
30506 }
30507 ao2_iterator_destroy(&i);
30508 }
30509
30510
30511 static void sip_send_all_registers(void)
30512 {
30513 int ms;
30514 int regspacing;
30515 if (!regobjs)
30516 return;
30517 regspacing = default_expiry * 1000/regobjs;
30518 if (regspacing > 100) {
30519 regspacing = 100;
30520 }
30521 ms = regspacing;
30522 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
30523 ASTOBJ_WRLOCK(iterator);
30524 ms += regspacing;
30525 AST_SCHED_REPLACE_UNREF(iterator->expire, sched, ms, sip_reregister, iterator,
30526 registry_unref(_data, "REPLACE sched del decs the refcount"),
30527 registry_unref(iterator, "REPLACE sched add failure decs the refcount"),
30528 registry_addref(iterator, "REPLACE sched add incs the refcount"));
30529 ASTOBJ_UNLOCK(iterator);
30530 } while (0)
30531 );
30532 }
30533
30534
30535 static void sip_send_all_mwi_subscriptions(void)
30536 {
30537 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
30538 struct sip_subscription_mwi *saved;
30539 ASTOBJ_WRLOCK(iterator);
30540 AST_SCHED_DEL(sched, iterator->resub);
30541 saved = ASTOBJ_REF(iterator);
30542 if ((iterator->resub = ast_sched_add(sched, 1, sip_subscribe_mwi_do, saved)) < 0) {
30543 ASTOBJ_UNREF(saved, sip_subscribe_mwi_destroy);
30544 }
30545 ASTOBJ_UNLOCK(iterator);
30546 } while (0));
30547 }
30548
30549
30550 static int setup_srtp(struct sip_srtp **srtp)
30551 {
30552 if (!ast_rtp_engine_srtp_is_registered()) {
30553 ast_log(LOG_ERROR, "No SRTP module loaded, can't setup SRTP session.\n");
30554 return -1;
30555 }
30556
30557 if (!(*srtp = sip_srtp_alloc())) {
30558 return -1;
30559 }
30560
30561 return 0;
30562 }
30563
30564 static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct sip_srtp **srtp, const char *a)
30565 {
30566
30567 if (!rtp) {
30568 ast_debug(3, "Received offer with crypto line for media stream that is not enabled\n");
30569 return FALSE;
30570 }
30571
30572 if (strncasecmp(a, "crypto:", 7)) {
30573 return FALSE;
30574 }
30575 if (!*srtp) {
30576 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
30577 ast_log(LOG_WARNING, "Ignoring unexpected crypto attribute in SDP answer\n");
30578 return FALSE;
30579 }
30580
30581 if (setup_srtp(srtp) < 0) {
30582 return FALSE;
30583 }
30584 }
30585
30586 if (!(*srtp)->crypto && !((*srtp)->crypto = sdp_crypto_setup())) {
30587 return FALSE;
30588 }
30589
30590 if (sdp_crypto_process((*srtp)->crypto, a, rtp) < 0) {
30591 return FALSE;
30592 }
30593
30594 ast_set_flag(*srtp, SRTP_CRYPTO_OFFER_OK);
30595
30596 return TRUE;
30597 }
30598
30599
30600 static int sip_do_reload(enum channelreloadreason reason)
30601 {
30602 time_t start_poke, end_poke;
30603
30604 reload_config(reason);
30605 ast_sched_dump(sched);
30606
30607 start_poke = time(0);
30608
30609 unlink_marked_peers_from_tables();
30610
30611 ast_debug(4, "--------------- Done destroying pruned peers\n");
30612
30613
30614 sip_poke_all_peers();
30615
30616
30617 sip_send_all_registers();
30618
30619 sip_send_all_mwi_subscriptions();
30620
30621 end_poke = time(0);
30622
30623 ast_debug(4, "do_reload finished. peer poke/prune reg contact time = %d sec.\n", (int)(end_poke-start_poke));
30624
30625 ast_debug(4, "--------------- SIP reload done\n");
30626
30627 return 0;
30628 }
30629
30630
30631 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
30632 {
30633 static struct sip_peer *tmp_peer, *new_peer;
30634
30635 switch (cmd) {
30636 case CLI_INIT:
30637 e->command = "sip reload";
30638 e->usage =
30639 "Usage: sip reload\n"
30640 " Reloads SIP configuration from sip.conf\n";
30641 return NULL;
30642 case CLI_GENERATE:
30643 return NULL;
30644 }
30645
30646 ast_mutex_lock(&sip_reload_lock);
30647 if (sip_reloading) {
30648 ast_verbose("Previous SIP reload not yet done\n");
30649 } else {
30650 sip_reloading = TRUE;
30651 sip_reloadreason = (a && a->fd) ? CHANNEL_CLI_RELOAD : CHANNEL_MODULE_RELOAD;
30652 }
30653 ast_mutex_unlock(&sip_reload_lock);
30654 restart_monitor();
30655
30656 tmp_peer = bogus_peer;
30657
30658 if ((new_peer = temp_peer("(bogus_peer)"))) {
30659 ast_string_field_set(new_peer, md5secret, BOGUS_PEER_MD5SECRET);
30660 ast_clear_flag(&new_peer->flags[0], SIP_INSECURE);
30661 bogus_peer = new_peer;
30662 ao2_t_ref(tmp_peer, -1, "unref the old bogus_peer during reload");
30663 } else {
30664 ast_log(LOG_ERROR, "Could not update the fake authentication peer.\n");
30665
30666 }
30667
30668 return CLI_SUCCESS;
30669 }
30670
30671
30672 static int reload(void)
30673 {
30674 if (sip_reload(0, 0, NULL))
30675 return 0;
30676 return 1;
30677 }
30678
30679
30680
30681
30682
30683 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
30684 const char* name, int flag, int family)
30685 {
30686 struct ast_sockaddr *addrs;
30687 int addrs_cnt;
30688
30689 addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
30690 if (addrs_cnt <= 0) {
30691 return 1;
30692 }
30693 if (addrs_cnt > 1) {
30694 ast_debug(1, "Multiple addresses, using the first one only\n");
30695 }
30696
30697 ast_sockaddr_copy(addr, &addrs[0]);
30698
30699 ast_free(addrs);
30700 return 0;
30701 }
30702
30703
30704
30705
30706
30707 static int ast_sockaddr_resolve_first(struct ast_sockaddr *addr,
30708 const char* name, int flag)
30709 {
30710 return ast_sockaddr_resolve_first_af(addr, name, flag, get_address_family_filter(SIP_TRANSPORT_UDP));
30711 }
30712
30713
30714
30715
30716
30717 static int ast_sockaddr_resolve_first_transport(struct ast_sockaddr *addr,
30718 const char* name, int flag, unsigned int transport)
30719 {
30720 return ast_sockaddr_resolve_first_af(addr, name, flag, get_address_family_filter(transport));
30721 }
30722
30723
30724
30725
30726 static int peer_hash_cb(const void *obj, const int flags)
30727 {
30728 const struct sip_peer *peer = obj;
30729
30730 return ast_str_case_hash(peer->name);
30731 }
30732
30733
30734
30735
30736 static int peer_cmp_cb(void *obj, void *arg, int flags)
30737 {
30738 struct sip_peer *peer = obj, *peer2 = arg;
30739
30740 return !strcasecmp(peer->name, peer2->name) ? CMP_MATCH | CMP_STOP : 0;
30741 }
30742
30743
30744
30745
30746
30747
30748 static int peer_iphash_cb(const void *obj, const int flags)
30749 {
30750 const struct sip_peer *peer = obj;
30751 int ret = 0;
30752
30753 if (ast_sockaddr_isnull(&peer->addr)) {
30754 ast_log(LOG_ERROR, "Empty address\n");
30755 }
30756
30757 ret = ast_sockaddr_hash(&peer->addr);
30758
30759 if (ret < 0) {
30760 ret = -ret;
30761 }
30762
30763 return ret;
30764 }
30765
30766
30767
30768
30769
30770
30771
30772
30773
30774
30775
30776
30777
30778
30779
30780
30781
30782
30783
30784 static int peer_ipcmp_cb(void *obj, void *arg, int flags)
30785 {
30786 struct sip_peer *peer = obj, *peer2 = arg;
30787
30788 if (ast_sockaddr_cmp_addr(&peer->addr, &peer2->addr)) {
30789
30790 return 0;
30791 }
30792
30793
30794 if ((peer->transports & peer2->transports) & (SIP_TRANSPORT_TLS | SIP_TRANSPORT_TCP)) {
30795
30796 return CMP_MATCH | CMP_STOP;
30797 } else if (ast_test_flag(&peer2->flags[0], SIP_INSECURE_PORT)) {
30798
30799
30800 return ast_test_flag(&peer->flags[0], SIP_INSECURE_PORT) ?
30801 (CMP_MATCH | CMP_STOP) : 0;
30802 }
30803
30804
30805 return ast_sockaddr_port(&peer->addr) == ast_sockaddr_port(&peer2->addr) ?
30806 (CMP_MATCH | CMP_STOP) : 0;
30807 }
30808
30809
30810 static int threadt_hash_cb(const void *obj, const int flags)
30811 {
30812 const struct sip_threadinfo *th = obj;
30813
30814 return ast_sockaddr_hash(&th->tcptls_session->remote_address);
30815 }
30816
30817 static int threadt_cmp_cb(void *obj, void *arg, int flags)
30818 {
30819 struct sip_threadinfo *th = obj, *th2 = arg;
30820
30821 return (th->tcptls_session == th2->tcptls_session) ? CMP_MATCH | CMP_STOP : 0;
30822 }
30823
30824
30825
30826
30827 static int dialog_hash_cb(const void *obj, const int flags)
30828 {
30829 const struct sip_pvt *pvt = obj;
30830
30831 return ast_str_case_hash(pvt->callid);
30832 }
30833
30834
30835
30836
30837 static int dialog_find_multiple(void *obj, void *arg, int flags)
30838 {
30839 struct sip_pvt *pvt = obj, *pvt2 = arg;
30840
30841 return !strcasecmp(pvt->callid, pvt2->callid) ? CMP_MATCH : 0;
30842 }
30843
30844
30845
30846
30847 static int dialog_cmp_cb(void *obj, void *arg, int flags)
30848 {
30849 struct sip_pvt *pvt = obj, *pvt2 = arg;
30850
30851 return !strcasecmp(pvt->callid, pvt2->callid) ? CMP_MATCH | CMP_STOP : 0;
30852 }
30853
30854
30855 static struct ast_cli_entry cli_sip[] = {
30856 AST_CLI_DEFINE(sip_show_channels, "List active SIP channels or subscriptions"),
30857 AST_CLI_DEFINE(sip_show_channelstats, "List statistics for active SIP channels"),
30858 AST_CLI_DEFINE(sip_show_domains, "List our local SIP domains"),
30859 AST_CLI_DEFINE(sip_show_inuse, "List all inuse/limits"),
30860 AST_CLI_DEFINE(sip_show_objects, "List all SIP object allocations"),
30861 AST_CLI_DEFINE(sip_show_peers, "List defined SIP peers"),
30862 AST_CLI_DEFINE(sip_show_registry, "List SIP registration status"),
30863 AST_CLI_DEFINE(sip_unregister, "Unregister (force expiration) a SIP peer from the registry"),
30864 AST_CLI_DEFINE(sip_show_settings, "Show SIP global settings"),
30865 AST_CLI_DEFINE(sip_show_mwi, "Show MWI subscriptions"),
30866 AST_CLI_DEFINE(sip_cli_notify, "Send a notify packet to a SIP peer"),
30867 AST_CLI_DEFINE(sip_show_channel, "Show detailed SIP channel info"),
30868 AST_CLI_DEFINE(sip_show_history, "Show SIP dialog history"),
30869 AST_CLI_DEFINE(sip_show_peer, "Show details on specific SIP peer"),
30870 AST_CLI_DEFINE(sip_show_users, "List defined SIP users"),
30871 AST_CLI_DEFINE(sip_show_user, "Show details on specific SIP user"),
30872 AST_CLI_DEFINE(sip_qualify_peer, "Send an OPTIONS packet to a peer"),
30873 AST_CLI_DEFINE(sip_show_sched, "Present a report on the status of the scheduler queue"),
30874 AST_CLI_DEFINE(sip_prune_realtime, "Prune cached Realtime users/peers"),
30875 AST_CLI_DEFINE(sip_do_debug, "Enable/Disable SIP debugging"),
30876 AST_CLI_DEFINE(sip_set_history, "Enable/Disable SIP history"),
30877 AST_CLI_DEFINE(sip_reload, "Reload SIP configuration"),
30878 AST_CLI_DEFINE(sip_show_tcp, "List TCP Connections")
30879 };
30880
30881
30882 static void sip_register_tests(void)
30883 {
30884 sip_config_parser_register_tests();
30885 sip_request_parser_register_tests();
30886 sip_dialplan_function_register_tests();
30887 }
30888
30889
30890 static void sip_unregister_tests(void)
30891 {
30892 sip_config_parser_unregister_tests();
30893 sip_request_parser_unregister_tests();
30894 sip_dialplan_function_unregister_tests();
30895 }
30896
30897 #ifdef TEST_FRAMEWORK
30898 AST_TEST_DEFINE(test_sip_mwi_subscribe_parse)
30899 {
30900 int found = 0;
30901 enum ast_test_result_state res = AST_TEST_PASS;
30902 const char *mwi1 = "1234@mysipprovider.com/1234";
30903 const char *mwi2 = "1234:password@mysipprovider.com/1234";
30904 const char *mwi3 = "1234:password@mysipprovider.com:5061/1234";
30905 const char *mwi4 = "1234:password:authuser@mysipprovider.com/1234";
30906 const char *mwi5 = "1234:password:authuser@mysipprovider.com:5061/1234";
30907 const char *mwi6 = "1234:password";
30908
30909 switch (cmd) {
30910 case TEST_INIT:
30911 info->name = "sip_mwi_subscribe_parse_test";
30912 info->category = "/channels/chan_sip/";
30913 info->summary = "SIP MWI subscribe line parse unit test";
30914 info->description =
30915 "Tests the parsing of mwi subscription lines (e.g., mwi => from sip.conf)";
30916 return AST_TEST_NOT_RUN;
30917 case TEST_EXECUTE:
30918 break;
30919 }
30920
30921 if (sip_subscribe_mwi(mwi1, 1)) {
30922 res = AST_TEST_FAIL;
30923 } else {
30924 found = 0;
30925 res = AST_TEST_FAIL;
30926 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
30927 ASTOBJ_WRLOCK(iterator);
30928 if (
30929 !strcmp(iterator->hostname, "mysipprovider.com") &&
30930 !strcmp(iterator->username, "1234") &&
30931 !strcmp(iterator->secret, "") &&
30932 !strcmp(iterator->authuser, "") &&
30933 !strcmp(iterator->mailbox, "1234") &&
30934 iterator->portno == 0) {
30935 found = 1;
30936 res = AST_TEST_PASS;
30937 }
30938 ASTOBJ_UNLOCK(iterator);
30939 } while(0));
30940 if (!found) {
30941 ast_test_status_update(test, "sip_subscribe_mwi test 1 failed\n");
30942 }
30943 }
30944
30945 if (sip_subscribe_mwi(mwi2, 1)) {
30946 res = AST_TEST_FAIL;
30947 } else {
30948 found = 0;
30949 res = AST_TEST_FAIL;
30950 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
30951 ASTOBJ_WRLOCK(iterator);
30952 if (
30953 !strcmp(iterator->hostname, "mysipprovider.com") &&
30954 !strcmp(iterator->username, "1234") &&
30955 !strcmp(iterator->secret, "password") &&
30956 !strcmp(iterator->authuser, "") &&
30957 !strcmp(iterator->mailbox, "1234") &&
30958 iterator->portno == 0) {
30959 found = 1;
30960 res = AST_TEST_PASS;
30961 }
30962 ASTOBJ_UNLOCK(iterator);
30963 } while(0));
30964 if (!found) {
30965 ast_test_status_update(test, "sip_subscribe_mwi test 2 failed\n");
30966 }
30967 }
30968
30969 if (sip_subscribe_mwi(mwi3, 1)) {
30970 res = AST_TEST_FAIL;
30971 } else {
30972 found = 0;
30973 res = AST_TEST_FAIL;
30974 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
30975 ASTOBJ_WRLOCK(iterator);
30976 if (
30977 !strcmp(iterator->hostname, "mysipprovider.com") &&
30978 !strcmp(iterator->username, "1234") &&
30979 !strcmp(iterator->secret, "password") &&
30980 !strcmp(iterator->authuser, "") &&
30981 !strcmp(iterator->mailbox, "1234") &&
30982 iterator->portno == 5061) {
30983 found = 1;
30984 res = AST_TEST_PASS;
30985 }
30986 ASTOBJ_UNLOCK(iterator);
30987 } while(0));
30988 if (!found) {
30989 ast_test_status_update(test, "sip_subscribe_mwi test 3 failed\n");
30990 }
30991 }
30992
30993 if (sip_subscribe_mwi(mwi4, 1)) {
30994 res = AST_TEST_FAIL;
30995 } else {
30996 found = 0;
30997 res = AST_TEST_FAIL;
30998 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
30999 ASTOBJ_WRLOCK(iterator);
31000 if (
31001 !strcmp(iterator->hostname, "mysipprovider.com") &&
31002 !strcmp(iterator->username, "1234") &&
31003 !strcmp(iterator->secret, "password") &&
31004 !strcmp(iterator->authuser, "authuser") &&
31005 !strcmp(iterator->mailbox, "1234") &&
31006 iterator->portno == 0) {
31007 found = 1;
31008 res = AST_TEST_PASS;
31009 }
31010 ASTOBJ_UNLOCK(iterator);
31011 } while(0));
31012 if (!found) {
31013 ast_test_status_update(test, "sip_subscribe_mwi test 4 failed\n");
31014 }
31015 }
31016
31017 if (sip_subscribe_mwi(mwi5, 1)) {
31018 res = AST_TEST_FAIL;
31019 } else {
31020 found = 0;
31021 res = AST_TEST_FAIL;
31022 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
31023 ASTOBJ_WRLOCK(iterator);
31024 if (
31025 !strcmp(iterator->hostname, "mysipprovider.com") &&
31026 !strcmp(iterator->username, "1234") &&
31027 !strcmp(iterator->secret, "password") &&
31028 !strcmp(iterator->authuser, "authuser") &&
31029 !strcmp(iterator->mailbox, "1234") &&
31030 iterator->portno == 5061) {
31031 found = 1;
31032 res = AST_TEST_PASS;
31033 }
31034 ASTOBJ_UNLOCK(iterator);
31035 } while(0));
31036 if (!found) {
31037 ast_test_status_update(test, "sip_subscribe_mwi test 5 failed\n");
31038 }
31039 }
31040
31041 if (sip_subscribe_mwi(mwi6, 1)) {
31042 res = AST_TEST_PASS;
31043 } else {
31044 res = AST_TEST_FAIL;
31045 }
31046 return res;
31047 }
31048
31049 AST_TEST_DEFINE(test_sip_peers_get)
31050 {
31051 struct sip_peer *peer;
31052 struct ast_data *node;
31053 struct ast_data_query query = {
31054 .path = "/asterisk/channel/sip/peers",
31055 .search = "peers/peer/name=test_peer_data_provider"
31056 };
31057
31058 switch (cmd) {
31059 case TEST_INIT:
31060 info->name = "sip_peers_get_data_test";
31061 info->category = "/main/data/sip/peers/";
31062 info->summary = "SIP peers data providers unit test";
31063 info->description =
31064 "Tests whether the SIP peers data provider implementation works as expected.";
31065 return AST_TEST_NOT_RUN;
31066 case TEST_EXECUTE:
31067 break;
31068 }
31069
31070
31071 peer = build_peer("test_peer_data_provider", NULL, NULL, 0, 0);
31072 if (!peer) {
31073 return AST_TEST_FAIL;
31074 }
31075 peer->type = SIP_TYPE_USER;
31076 peer->call_limit = 10;
31077 ao2_link(peers, peer);
31078
31079
31080 node = ast_data_get(&query);
31081 if (!node) {
31082 ao2_unlink(peers, peer);
31083 ao2_ref(peer, -1);
31084 return AST_TEST_FAIL;
31085 }
31086
31087
31088 if (strcmp(ast_data_retrieve_string(node, "peer/name"), "test_peer_data_provider")) {
31089 ao2_unlink(peers, peer);
31090 ao2_ref(peer, -1);
31091 ast_data_free(node);
31092 return AST_TEST_FAIL;
31093 }
31094
31095 if (strcmp(ast_data_retrieve_string(node, "peer/type"), "user")) {
31096 ao2_unlink(peers, peer);
31097 ao2_ref(peer, -1);
31098 ast_data_free(node);
31099 return AST_TEST_FAIL;
31100 }
31101
31102 if (ast_data_retrieve_int(node, "peer/call_limit") != 10) {
31103 ao2_unlink(peers, peer);
31104 ao2_ref(peer, -1);
31105 ast_data_free(node);
31106 return AST_TEST_FAIL;
31107 }
31108
31109
31110 ast_data_free(node);
31111
31112 ao2_unlink(peers, peer);
31113 ao2_ref(peer, -1);
31114
31115 return AST_TEST_PASS;
31116 }
31117
31118
31119
31120
31121
31122
31123
31124
31125
31126
31127
31128
31129
31130
31131
31132
31133
31134
31135
31136 static int mock_tcp_loop(char *fragments[], size_t num_fragments,
31137 struct ast_str **overflow, char **messages, int *num_messages, struct ast_test* test)
31138 {
31139 struct ast_str *req_data;
31140 int i = 0;
31141 int res = 0;
31142
31143 req_data = ast_str_create(128);
31144 ast_str_reset(*overflow);
31145
31146 while (i < num_fragments || ast_str_strlen(*overflow) > 0) {
31147 enum message_integrity message_integrity = MESSAGE_FRAGMENT;
31148 ast_str_reset(req_data);
31149 while (message_integrity == MESSAGE_FRAGMENT) {
31150 if (ast_str_strlen(*overflow) > 0) {
31151 ast_str_append(&req_data, 0, "%s", ast_str_buffer(*overflow));
31152 ast_str_reset(*overflow);
31153 } else {
31154 ast_str_append(&req_data, 0, "%s", fragments[i++]);
31155 }
31156 message_integrity = check_message_integrity(&req_data, overflow);
31157 }
31158 if (strcmp(ast_str_buffer(req_data), messages[*num_messages])) {
31159 ast_test_status_update(test, "Mismatch in SIP messages.\n");
31160 ast_test_status_update(test, "Expected message:\n%s", messages[*num_messages]);
31161 ast_test_status_update(test, "Parsed message:\n%s", ast_str_buffer(req_data));
31162 res = -1;
31163 goto end;
31164 } else {
31165 ast_test_status_update(test, "Successfully read message:\n%s", ast_str_buffer(req_data));
31166 }
31167 (*num_messages)++;
31168 }
31169
31170 end:
31171 ast_free(req_data);
31172 return res;
31173 };
31174
31175 AST_TEST_DEFINE(test_tcp_message_fragmentation)
31176 {
31177
31178 char *normal[] = {
31179 "INVITE sip:bob@example.org SIP/2.0\r\n"
31180 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31181 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31182 "To: <sip:bob@example.org:5060>\r\n"
31183 "Call-ID: 12345\r\n"
31184 "CSeq: 1 INVITE\r\n"
31185 "Contact: sip:127.0.0.1:5061\r\n"
31186 "Max-Forwards: 70\r\n"
31187 "Content-Type: application/sdp\r\n"
31188 "Content-Length: 130\r\n"
31189 "\r\n"
31190 "v=0\r\n"
31191 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31192 "s=-\r\n"
31193 "c=IN IP4 127.0.0.1\r\n"
31194 "t=0 0\r\n"
31195 "m=audio 10000 RTP/AVP 0\r\n"
31196 "a=rtpmap:0 PCMU/8000\r\n"
31197 };
31198
31199
31200
31201
31202 char *fragmented[] = {
31203 "INVITE sip:bob@example.org SIP/2.0\r\n"
31204 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31205 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31206 "To: <sip:bob@example.org:5060>\r\n"
31207 "Call-ID: 12345\r\n"
31208 "CSeq: 1 INVITE\r\n"
31209 "Contact: sip:127.0.0.1:5061\r\n"
31210 "Max-Forwards: ",
31211
31212 "70\r\n"
31213 "Content-Type: application/sdp\r\n"
31214 "Content-Length: 130\r\n"
31215 "\r\n"
31216 "v=0\r\n"
31217 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31218 "s=-\r\n"
31219 "c=IN IP4 127.0.0.1\r\n"
31220 "t=0 0\r\n"
31221 "m=audio 10000 RTP/AVP 0\r\n"
31222 "a=rtpmap:0 PCMU/8000\r\n"
31223 };
31224
31225
31226
31227 char *fragmented_body[] = {
31228 "INVITE sip:bob@example.org SIP/2.0\r\n"
31229 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31230 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31231 "To: <sip:bob@example.org:5060>\r\n"
31232 "Call-ID: 12345\r\n"
31233 "CSeq: 1 INVITE\r\n"
31234 "Contact: sip:127.0.0.1:5061\r\n"
31235 "Max-Forwards: 70\r\n"
31236 "Content-Type: application/sdp\r\n"
31237 "Content-Length: 130\r\n"
31238 "\r\n",
31239
31240 "v=0\r\n"
31241 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31242 "s=-\r\n"
31243 "c=IN IP4 127.0.0.1\r\n"
31244 "t=0 0\r\n"
31245 "m=audio 10000 RTP/AVP 0\r\n"
31246 "a=rtpmap:0 PCMU/8000\r\n"
31247 };
31248
31249
31250
31251
31252 char *multi_fragment[] = {
31253 "INVITE sip:bob@example.org SIP/2.0\r\n"
31254 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31255 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31256 "To: <sip:bob@example.org:5060>\r\n"
31257 "Call-ID: 12345\r\n"
31258 "CSeq: 1 INVITE\r\n",
31259
31260 "Contact: sip:127.0.0.1:5061\r\n"
31261 "Max-Forwards: 70\r\n"
31262 "Content-Type: application/sdp\r\n"
31263 "Content-Length: 130\r\n"
31264 "\r\n"
31265 "v=0\r\n"
31266 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31267 "s=-\r\n"
31268 "c=IN IP4",
31269
31270 " 127.0.0.1\r\n"
31271 "t=0 0\r\n"
31272 "m=audio 10000 RTP/AVP 0\r\n"
31273 "a=rtpmap:0 PCMU/8000\r\n"
31274 };
31275
31276
31277
31278
31279 char *multi_message[] = {
31280 "SIP/2.0 100 Trying\r\n"
31281 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31282 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31283 "To: <sip:bob@example.org:5060>\r\n"
31284 "Call-ID: 12345\r\n"
31285 "CSeq: 1 INVITE\r\n"
31286 "Contact: <sip:bob@example.org:5060>\r\n"
31287 "Content-Length: 0\r\n"
31288 "\r\n"
31289 "SIP/2.0 180 Ringing\r\n"
31290 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31291 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31292 "To: <sip:bob@example.org:5060>\r\n"
31293 "Call-ID: 12345\r\n"
31294 "CSeq: 1 INVITE\r\n"
31295 "Contact: <sip:bob@example.org:5060>\r\n"
31296 "Content-Length: 0\r\n"
31297 "\r\n"
31298 };
31299 char *multi_message_divided[] = {
31300 "SIP/2.0 100 Trying\r\n"
31301 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31302 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31303 "To: <sip:bob@example.org:5060>\r\n"
31304 "Call-ID: 12345\r\n"
31305 "CSeq: 1 INVITE\r\n"
31306 "Contact: <sip:bob@example.org:5060>\r\n"
31307 "Content-Length: 0\r\n"
31308 "\r\n",
31309
31310 "SIP/2.0 180 Ringing\r\n"
31311 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31312 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31313 "To: <sip:bob@example.org:5060>\r\n"
31314 "Call-ID: 12345\r\n"
31315 "CSeq: 1 INVITE\r\n"
31316 "Contact: <sip:bob@example.org:5060>\r\n"
31317 "Content-Length: 0\r\n"
31318 "\r\n"
31319 };
31320
31321
31322
31323 char *multi_message_body[] = {
31324 "INVITE sip:bob@example.org SIP/2.0\r\n"
31325 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31326 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31327 "To: <sip:bob@example.org:5060>\r\n"
31328 "Call-ID: 12345\r\n"
31329 "CSeq: 1 INVITE\r\n"
31330 "Contact: sip:127.0.0.1:5061\r\n"
31331 "Max-Forwards: 70\r\n"
31332 "Content-Type: application/sdp\r\n"
31333 "Content-Length: 130\r\n"
31334 "\r\n"
31335 "v=0\r\n"
31336 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31337 "s=-\r\n"
31338 "c=IN IP4 127.0.0.1\r\n"
31339 "t=0 0\r\n"
31340 "m=audio 10000 RTP/AVP 0\r\n"
31341 "a=rtpmap:0 PCMU/8000\r\n"
31342 "INVITE sip:bob@example.org SIP/2.0\r\n"
31343 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31344 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31345 "To: <sip:bob@example.org:5060>\r\n"
31346 "Call-ID: 12345\r\n"
31347 "CSeq: 2 INVITE\r\n"
31348 "Contact: sip:127.0.0.1:5061\r\n"
31349 "Max-Forwards: 70\r\n"
31350 "Content-Type: application/sdp\r\n"
31351 "Content-Length: 130\r\n"
31352 "\r\n"
31353 "v=0\r\n"
31354 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31355 "s=-\r\n"
31356 "c=IN IP4 127.0.0.1\r\n"
31357 "t=0 0\r\n"
31358 "m=audio 10000 RTP/AVP 0\r\n"
31359 "a=rtpmap:0 PCMU/8000\r\n"
31360 };
31361 char *multi_message_body_divided[] = {
31362 "INVITE sip:bob@example.org SIP/2.0\r\n"
31363 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31364 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31365 "To: <sip:bob@example.org:5060>\r\n"
31366 "Call-ID: 12345\r\n"
31367 "CSeq: 1 INVITE\r\n"
31368 "Contact: sip:127.0.0.1:5061\r\n"
31369 "Max-Forwards: 70\r\n"
31370 "Content-Type: application/sdp\r\n"
31371 "Content-Length: 130\r\n"
31372 "\r\n"
31373 "v=0\r\n"
31374 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31375 "s=-\r\n"
31376 "c=IN IP4 127.0.0.1\r\n"
31377 "t=0 0\r\n"
31378 "m=audio 10000 RTP/AVP 0\r\n"
31379 "a=rtpmap:0 PCMU/8000\r\n",
31380
31381 "INVITE sip:bob@example.org SIP/2.0\r\n"
31382 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31383 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31384 "To: <sip:bob@example.org:5060>\r\n"
31385 "Call-ID: 12345\r\n"
31386 "CSeq: 2 INVITE\r\n"
31387 "Contact: sip:127.0.0.1:5061\r\n"
31388 "Max-Forwards: 70\r\n"
31389 "Content-Type: application/sdp\r\n"
31390 "Content-Length: 130\r\n"
31391 "\r\n"
31392 "v=0\r\n"
31393 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31394 "s=-\r\n"
31395 "c=IN IP4 127.0.0.1\r\n"
31396 "t=0 0\r\n"
31397 "m=audio 10000 RTP/AVP 0\r\n"
31398 "a=rtpmap:0 PCMU/8000\r\n"
31399 };
31400
31401
31402
31403
31404
31405 char *multi_message_in_fragments[] = {
31406 "SIP/2.0 100 Trying\r\n"
31407 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31408 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31409 "To: <sip:bob@example.org:5060>\r\n"
31410 "Call-ID: 12345\r\n"
31411 "CSeq: 1 INVI",
31412
31413 "TE\r\n"
31414 "Contact: <sip:bob@example.org:5060>\r\n"
31415 "Content-Length: 0\r\n"
31416 "\r\n"
31417 "SIP/2.0 180 Ringing\r\n"
31418 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31419 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31420 "To: <sip:bob@example.org:5060>\r\n"
31421 "Call-ID: 12345\r\n"
31422 "CSeq: 1 INVITE\r\n"
31423 "Contact: <sip:bob@example.org:5060>\r\n"
31424 "Content-Length: 0\r\n"
31425 "\r\n"
31426 };
31427
31428
31429
31430
31431 char *compact[] = {
31432 "INVITE sip:bob@example.org SIP/2.0\r\n"
31433 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31434 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31435 "To: <sip:bob@example.org:5060>\r\n"
31436 "Call-ID: 12345\r\n"
31437 "CSeq: 1 INVITE\r\n"
31438 "Contact: sip:127.0.0.1:5061\r\n"
31439 "Max-Forwards: 70\r\n"
31440 "Content-Type: application/sdp\r\n"
31441 "l:130\r\n"
31442 "\r\n"
31443 "v=0\r\n"
31444 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31445 "s=-\r\n"
31446 "c=IN IP4 127.0.0.1\r\n"
31447 "t=0 0\r\n"
31448 "m=audio 10000 RTP/AVP 0\r\n"
31449 "a=rtpmap:0 PCMU/8000\r\n"
31450 };
31451
31452
31453
31454
31455 char *faux[] = {
31456 "INVITE sip:bob@example.org SIP/2.0\r\n"
31457 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31458 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31459 "To: <sip:bob@example.org:5060>\r\n"
31460 "Call-ID: 12345\r\n"
31461 "CSeq: 1 INVITE\r\n"
31462 "Contact: sip:127.0.0.1:5061\r\n"
31463 "Max-Forwards: 70\r\n"
31464 "Content-Type: application/sdp\r\n"
31465 "DisContent-Length: 0\r\n"
31466 "MalContent-Length: 60\r\n"
31467 "Content-Length:130\r\n"
31468 "\r\n"
31469 "v=0\r\n"
31470 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31471 "s=-\r\n"
31472 "c=IN IP4 127.0.0.1\r\n"
31473 "t=0 0\r\n"
31474 "m=audio 10000 RTP/AVP 0\r\n"
31475 "a=rtpmap:0 PCMU/8000\r\n"
31476 };
31477
31478
31479
31480
31481
31482
31483 char *folded[] = {
31484 "INVITE sip:bob@example.org SIP/2.0\r\n"
31485 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31486 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31487 "To: <sip:bob@example.org:5060>\r\n"
31488 "Call-ID: 12345\r\n"
31489 "CSeq: 1 INVITE\r\n"
31490 "Contact: sip:127.0.0.1:5061\r\n"
31491 "Max-Forwards: 70\r\n"
31492 "Content-Type: application/sdp\r\n"
31493 "Content-Length: \t\r\n"
31494 "\t \r\n"
31495 " 130\t \r\n"
31496 "\r\n"
31497 "v=0\r\n"
31498 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31499 "s=-\r\n"
31500 "c=IN IP4 127.0.0.1\r\n"
31501 "t=0 0\r\n"
31502 "m=audio 10000 RTP/AVP 0\r\n"
31503 "a=rtpmap:0 PCMU/8000\r\n"
31504 };
31505
31506
31507
31508
31509
31510 char *cl_in_body[] = {
31511 "INVITE sip:bob@example.org SIP/2.0\r\n"
31512 "Via: SIP/2.0/TCP 127.0.0.1:5060;branch=[branch]\r\n"
31513 "From: sipp <sip:127.0.0.1:5061>;tag=12345\r\n"
31514 "To: <sip:bob@example.org:5060>\r\n"
31515 "Call-ID: 12345\r\n"
31516 "CSeq: 1 INVITE\r\n"
31517 "Contact: sip:127.0.0.1:5061\r\n"
31518 "Max-Forwards: 70\r\n"
31519 "Content-Type: application/sdp\r\n"
31520 "l: 149\r\n"
31521 "\r\n"
31522 "v=0\r\n"
31523 "Content-Length: 0\r\n"
31524 "o=user1 53655765 2353687637 IN IP4 127.0.0.1\r\n"
31525 "s=-\r\n"
31526 "c=IN IP4 127.0.0.1\r\n"
31527 "t=0 0\r\n"
31528 "m=audio 10000 RTP/AVP 0\r\n"
31529 "a=rtpmap:0 PCMU/8000\r\n"
31530 };
31531
31532 struct ast_str *overflow;
31533 struct {
31534 char **fragments;
31535 char **expected;
31536 int num_expected;
31537 const char *description;
31538 } tests[] = {
31539 { normal, normal, 1, "normal" },
31540 { fragmented, normal, 1, "fragmented" },
31541 { fragmented_body, normal, 1, "fragmented_body" },
31542 { multi_fragment, normal, 1, "multi_fragment" },
31543 { multi_message, multi_message_divided, 2, "multi_message" },
31544 { multi_message_body, multi_message_body_divided, 2, "multi_message_body" },
31545 { multi_message_in_fragments, multi_message_divided, 2, "multi_message_in_fragments" },
31546 { compact, compact, 1, "compact" },
31547 { faux, faux, 1, "faux" },
31548 { folded, folded, 1, "folded" },
31549 { cl_in_body, cl_in_body, 1, "cl_in_body" },
31550 };
31551 int i;
31552 enum ast_test_result_state res = AST_TEST_PASS;
31553
31554 switch (cmd) {
31555 case TEST_INIT:
31556 info->name = "sip_tcp_message_fragmentation";
31557 info->category = "/main/sip/transport/";
31558 info->summary = "SIP TCP message fragmentation test";
31559 info->description =
31560 "Tests reception of different TCP messages that have been fragmented or"
31561 "run together. This test mimicks the code that TCP reception uses.";
31562 return AST_TEST_NOT_RUN;
31563 case TEST_EXECUTE:
31564 break;
31565 }
31566 if (!sip_cfg.pedanticsipchecking) {
31567 ast_log(LOG_WARNING, "Not running test. Pedantic SIP checking is not enabled, so it is guaranteed to fail\n");
31568 return AST_TEST_NOT_RUN;
31569 }
31570
31571 overflow = ast_str_create(128);
31572 if (!overflow) {
31573 return AST_TEST_FAIL;
31574 }
31575 for (i = 0; i < ARRAY_LEN(tests); ++i) {
31576 int num_messages = 0;
31577 if (mock_tcp_loop(tests[i].fragments, ARRAY_LEN(tests[i].fragments),
31578 &overflow, tests[i].expected, &num_messages, test)) {
31579 ast_test_status_update(test, "Failed to parse message '%s'\n", tests[i].description);
31580 res = AST_TEST_FAIL;
31581 break;
31582 }
31583 if (num_messages != tests[i].num_expected) {
31584 ast_test_status_update(test, "Did not receive the expected number of messages. "
31585 "Expected %d but received %d\n", tests[i].num_expected, num_messages);
31586 res = AST_TEST_FAIL;
31587 break;
31588 }
31589 }
31590 ast_free(overflow);
31591 return res;
31592 }
31593
31594 AST_TEST_DEFINE(get_in_brackets_const_test)
31595 {
31596 const char *input;
31597 const char *start = NULL;
31598 int len = 0;
31599 int res;
31600
31601 #define CHECK_RESULTS(in, expected_res, expected_start, expected_len) do { \
31602 input = (in); \
31603 res = get_in_brackets_const(input, &start, &len); \
31604 if ((expected_res) != res) { \
31605 ast_test_status_update(test, "Unexpected result: %d != %d\n", expected_res, res); \
31606 return AST_TEST_FAIL; \
31607 } \
31608 if ((expected_start) != start) { \
31609 const char *e = expected_start ? expected_start : "(null)"; \
31610 const char *a = start ? start : "(null)"; \
31611 ast_test_status_update(test, "Unexpected start: %s != %s\n", e, a); \
31612 return AST_TEST_FAIL; \
31613 } \
31614 if ((expected_len) != len) { \
31615 ast_test_status_update(test, "Unexpected len: %d != %d\n", expected_len, len); \
31616 return AST_TEST_FAIL; \
31617 } \
31618 } while(0)
31619
31620 switch (cmd) {
31621 case TEST_INIT:
31622 info->name = __func__;
31623 info->category = "/channels/chan_sip/";
31624 info->summary = "get_in_brackets_const test";
31625 info->description =
31626 "Tests the get_in_brackets_const function";
31627 return AST_TEST_NOT_RUN;
31628 case TEST_EXECUTE:
31629 break;
31630 }
31631
31632 CHECK_RESULTS("", 1, NULL, -1);
31633 CHECK_RESULTS("normal <test>", 0, input + 8, 4);
31634 CHECK_RESULTS("\"normal\" <test>", 0, input + 10, 4);
31635 CHECK_RESULTS("not normal <test", -1, NULL, -1);
31636 CHECK_RESULTS("\"yes < really\" <test>", 0, input + 16, 4);
31637 CHECK_RESULTS("\"even > this\" <test>", 0, input + 15, 4);
31638 CHECK_RESULTS("<sip:id1@10.10.10.10;lr>", 0, input + 1, 22);
31639 CHECK_RESULTS("<sip:id1@10.10.10.10;lr>, <sip:id1@10.10.10.20;lr>", 0, input + 1, 22);
31640 CHECK_RESULTS("<sip:id1,id2@10.10.10.10;lr>", 0, input + 1, 26);
31641 CHECK_RESULTS("<sip:id1@10., <sip:id2@10.10.10.10;lr>", 0, input + 1, 36);
31642 CHECK_RESULTS("\"quoted text\" <sip:dlg1@10.10.10.10;lr>", 0, input + 15, 23);
31643
31644 return AST_TEST_PASS;
31645 }
31646
31647 #endif
31648
31649 #define DATA_EXPORT_SIP_PEER(MEMBER) \
31650 MEMBER(sip_peer, name, AST_DATA_STRING) \
31651 MEMBER(sip_peer, secret, AST_DATA_PASSWORD) \
31652 MEMBER(sip_peer, md5secret, AST_DATA_PASSWORD) \
31653 MEMBER(sip_peer, remotesecret, AST_DATA_PASSWORD) \
31654 MEMBER(sip_peer, context, AST_DATA_STRING) \
31655 MEMBER(sip_peer, subscribecontext, AST_DATA_STRING) \
31656 MEMBER(sip_peer, username, AST_DATA_STRING) \
31657 MEMBER(sip_peer, accountcode, AST_DATA_STRING) \
31658 MEMBER(sip_peer, tohost, AST_DATA_STRING) \
31659 MEMBER(sip_peer, regexten, AST_DATA_STRING) \
31660 MEMBER(sip_peer, fromuser, AST_DATA_STRING) \
31661 MEMBER(sip_peer, fromdomain, AST_DATA_STRING) \
31662 MEMBER(sip_peer, fullcontact, AST_DATA_STRING) \
31663 MEMBER(sip_peer, cid_num, AST_DATA_STRING) \
31664 MEMBER(sip_peer, cid_name, AST_DATA_STRING) \
31665 MEMBER(sip_peer, vmexten, AST_DATA_STRING) \
31666 MEMBER(sip_peer, language, AST_DATA_STRING) \
31667 MEMBER(sip_peer, mohinterpret, AST_DATA_STRING) \
31668 MEMBER(sip_peer, mohsuggest, AST_DATA_STRING) \
31669 MEMBER(sip_peer, parkinglot, AST_DATA_STRING) \
31670 MEMBER(sip_peer, useragent, AST_DATA_STRING) \
31671 MEMBER(sip_peer, mwi_from, AST_DATA_STRING) \
31672 MEMBER(sip_peer, engine, AST_DATA_STRING) \
31673 MEMBER(sip_peer, unsolicited_mailbox, AST_DATA_STRING) \
31674 MEMBER(sip_peer, is_realtime, AST_DATA_BOOLEAN) \
31675 MEMBER(sip_peer, host_dynamic, AST_DATA_BOOLEAN) \
31676 MEMBER(sip_peer, autoframing, AST_DATA_BOOLEAN) \
31677 MEMBER(sip_peer, inUse, AST_DATA_INTEGER) \
31678 MEMBER(sip_peer, inRinging, AST_DATA_INTEGER) \
31679 MEMBER(sip_peer, onHold, AST_DATA_INTEGER) \
31680 MEMBER(sip_peer, call_limit, AST_DATA_INTEGER) \
31681 MEMBER(sip_peer, t38_maxdatagram, AST_DATA_INTEGER) \
31682 MEMBER(sip_peer, maxcallbitrate, AST_DATA_INTEGER) \
31683 MEMBER(sip_peer, rtptimeout, AST_DATA_SECONDS) \
31684 MEMBER(sip_peer, rtpholdtimeout, AST_DATA_SECONDS) \
31685 MEMBER(sip_peer, rtpkeepalive, AST_DATA_SECONDS) \
31686 MEMBER(sip_peer, lastms, AST_DATA_MILLISECONDS) \
31687 MEMBER(sip_peer, maxms, AST_DATA_MILLISECONDS) \
31688 MEMBER(sip_peer, qualifyfreq, AST_DATA_MILLISECONDS) \
31689 MEMBER(sip_peer, timer_t1, AST_DATA_MILLISECONDS) \
31690 MEMBER(sip_peer, timer_b, AST_DATA_MILLISECONDS)
31691
31692 AST_DATA_STRUCTURE(sip_peer, DATA_EXPORT_SIP_PEER);
31693
31694 static int peers_data_provider_get(const struct ast_data_search *search,
31695 struct ast_data *data_root)
31696 {
31697 struct sip_peer *peer;
31698 struct ao2_iterator i;
31699 struct ast_data *data_peer, *data_peer_mailboxes = NULL, *data_peer_mailbox, *enum_node;
31700 struct ast_data *data_sip_options;
31701 int total_mailboxes, x;
31702 struct sip_mailbox *mailbox;
31703
31704 i = ao2_iterator_init(peers, 0);
31705 while ((peer = ao2_iterator_next(&i))) {
31706 ao2_lock(peer);
31707
31708 data_peer = ast_data_add_node(data_root, "peer");
31709 if (!data_peer) {
31710 ao2_unlock(peer);
31711 ao2_ref(peer, -1);
31712 continue;
31713 }
31714
31715 ast_data_add_structure(sip_peer, data_peer, peer);
31716
31717
31718 enum_node = ast_data_add_node(data_peer, "allowtransfer");
31719 if (!enum_node) {
31720 ao2_unlock(peer);
31721 ao2_ref(peer, -1);
31722 continue;
31723 }
31724 ast_data_add_str(enum_node, "text", transfermode2str(peer->allowtransfer));
31725 ast_data_add_int(enum_node, "value", peer->allowtransfer);
31726
31727
31728 ast_data_add_str(data_peer, "transports", get_transport_list(peer->transports));
31729
31730
31731 if ((peer->type & SIP_TYPE_USER) && (peer->type & SIP_TYPE_PEER)) {
31732 ast_data_add_str(data_peer, "type", "friend");
31733 } else if (peer->type & SIP_TYPE_PEER) {
31734 ast_data_add_str(data_peer, "type", "peer");
31735 } else if (peer->type & SIP_TYPE_USER) {
31736 ast_data_add_str(data_peer, "type", "user");
31737 }
31738
31739
31740 total_mailboxes = 0;
31741 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
31742 if (!total_mailboxes) {
31743 data_peer_mailboxes = ast_data_add_node(data_peer, "mailboxes");
31744 if (!data_peer_mailboxes) {
31745 break;
31746 }
31747 total_mailboxes++;
31748 }
31749
31750 data_peer_mailbox = ast_data_add_node(data_peer_mailboxes, "mailbox");
31751 if (!data_peer_mailbox) {
31752 continue;
31753 }
31754 ast_data_add_str(data_peer_mailbox, "mailbox", mailbox->mailbox);
31755 ast_data_add_str(data_peer_mailbox, "context", mailbox->context);
31756 }
31757
31758
31759 enum_node = ast_data_add_node(data_peer, "amaflags");
31760 if (!enum_node) {
31761 ao2_unlock(peer);
31762 ao2_ref(peer, -1);
31763 continue;
31764 }
31765 ast_data_add_int(enum_node, "value", peer->amaflags);
31766 ast_data_add_str(enum_node, "text", ast_cdr_flags2str(peer->amaflags));
31767
31768
31769 data_sip_options = ast_data_add_node(data_peer, "sipoptions");
31770 if (!data_sip_options) {
31771 ao2_unlock(peer);
31772 ao2_ref(peer, -1);
31773 continue;
31774 }
31775 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
31776 ast_data_add_bool(data_sip_options, sip_options[x].text, peer->sipoptions & sip_options[x].id);
31777 }
31778
31779
31780 enum_node = ast_data_add_node(data_peer, "callingpres");
31781 if (!enum_node) {
31782 ao2_unlock(peer);
31783 ao2_ref(peer, -1);
31784 continue;
31785 }
31786 ast_data_add_int(enum_node, "value", peer->callingpres);
31787 ast_data_add_str(enum_node, "text", ast_describe_caller_presentation(peer->callingpres));
31788
31789
31790 ast_data_add_codecs(data_peer, "codecs", peer->capability);
31791
31792 if (!ast_data_search_match(search, data_peer)) {
31793 ast_data_remove_node(data_root, data_peer);
31794 }
31795
31796 ao2_unlock(peer);
31797 ao2_ref(peer, -1);
31798 }
31799 ao2_iterator_destroy(&i);
31800
31801 return 0;
31802 }
31803
31804 static const struct ast_data_handler peers_data_provider = {
31805 .version = AST_DATA_HANDLER_VERSION,
31806 .get = peers_data_provider_get
31807 };
31808
31809 static const struct ast_data_entry sip_data_providers[] = {
31810 AST_DATA_ENTRY("asterisk/channel/sip/peers", &peers_data_provider),
31811 };
31812
31813
31814 static int load_module(void)
31815 {
31816 ast_verbose("SIP channel loading...\n");
31817
31818
31819
31820 peers = ao2_t_container_alloc(HASH_PEER_SIZE, peer_hash_cb, peer_cmp_cb, "allocate peers");
31821 peers_by_ip = ao2_t_container_alloc(HASH_PEER_SIZE, peer_iphash_cb, peer_ipcmp_cb, "allocate peers_by_ip");
31822 dialogs = ao2_t_container_alloc(HASH_DIALOG_SIZE, dialog_hash_cb, dialog_cmp_cb, "allocate dialogs");
31823 dialogs_to_destroy = ao2_t_container_alloc(1, NULL, NULL, "allocate dialogs_to_destroy");
31824 threadt = ao2_t_container_alloc(HASH_DIALOG_SIZE, threadt_hash_cb, threadt_cmp_cb, "allocate threadt table");
31825 if (!peers || !peers_by_ip || !dialogs || !dialogs_to_destroy || !threadt) {
31826 ast_log(LOG_ERROR, "Unable to create primary SIP container(s)\n");
31827 return AST_MODULE_LOAD_FAILURE;
31828 }
31829
31830 ASTOBJ_CONTAINER_INIT(®l);
31831 ASTOBJ_CONTAINER_INIT(&submwil);
31832
31833 if (!(sched = sched_context_create())) {
31834 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
31835 return AST_MODULE_LOAD_FAILURE;
31836 }
31837
31838 if (!(io = io_context_create())) {
31839 ast_log(LOG_ERROR, "Unable to create I/O context\n");
31840 sched_context_destroy(sched);
31841 return AST_MODULE_LOAD_FAILURE;
31842 }
31843
31844 sip_reloadreason = CHANNEL_MODULE_LOAD;
31845
31846 can_parse_xml = sip_is_xml_parsable();
31847 if (reload_config(sip_reloadreason)) {
31848 return AST_MODULE_LOAD_DECLINE;
31849 }
31850
31851
31852 if (!(bogus_peer = temp_peer("(bogus_peer)"))) {
31853 ast_log(LOG_ERROR, "Unable to create bogus_peer for authentication\n");
31854 io_context_destroy(io);
31855 sched_context_destroy(sched);
31856 return AST_MODULE_LOAD_FAILURE;
31857 }
31858
31859 ast_string_field_set(bogus_peer, md5secret, BOGUS_PEER_MD5SECRET);
31860 ast_clear_flag(&bogus_peer->flags[0], SIP_INSECURE);
31861
31862
31863
31864
31865
31866 memcpy(&sip_tech_info, &sip_tech, sizeof(sip_tech));
31867 memset((void *) &sip_tech_info.send_digit_begin, 0, sizeof(sip_tech_info.send_digit_begin));
31868
31869
31870 if (ast_channel_register(&sip_tech)) {
31871 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
31872 ao2_t_ref(bogus_peer, -1, "unref the bogus_peer");
31873 io_context_destroy(io);
31874 sched_context_destroy(sched);
31875 return AST_MODULE_LOAD_FAILURE;
31876 }
31877
31878 #ifdef TEST_FRAMEWORK
31879 AST_TEST_REGISTER(test_sip_peers_get);
31880 AST_TEST_REGISTER(test_sip_mwi_subscribe_parse);
31881 AST_TEST_REGISTER(test_tcp_message_fragmentation);
31882 AST_TEST_REGISTER(get_in_brackets_const_test);
31883 #endif
31884
31885
31886 ast_data_register_multiple(sip_data_providers, ARRAY_LEN(sip_data_providers));
31887
31888
31889 ast_cli_register_multiple(cli_sip, ARRAY_LEN(cli_sip));
31890
31891
31892 ast_udptl_proto_register(&sip_udptl);
31893
31894
31895 ast_rtp_glue_register(&sip_rtp_glue);
31896
31897
31898 ast_register_application_xml(app_dtmfmode, sip_dtmfmode);
31899 ast_register_application_xml(app_sipaddheader, sip_addheader);
31900 ast_register_application_xml(app_sipremoveheader, sip_removeheader);
31901
31902
31903 ast_custom_function_register(&sip_header_function);
31904 ast_custom_function_register(&sippeer_function);
31905 ast_custom_function_register(&sipchaninfo_function);
31906 ast_custom_function_register(&checksipdomain_function);
31907
31908
31909 ast_manager_register_xml("SIPpeers", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peers);
31910 ast_manager_register_xml("SIPshowpeer", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peer);
31911 ast_manager_register_xml("SIPqualifypeer", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_qualify_peer);
31912 ast_manager_register_xml("SIPshowregistry", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_show_registry);
31913 ast_manager_register_xml("SIPnotify", EVENT_FLAG_SYSTEM, manager_sipnotify);
31914 sip_poke_all_peers();
31915 sip_send_all_registers();
31916 sip_send_all_mwi_subscriptions();
31917 initialize_escs();
31918
31919 if (sip_epa_register(&cc_epa_static_data)) {
31920 return AST_MODULE_LOAD_DECLINE;
31921 }
31922
31923 if (sip_reqresp_parser_init() == -1) {
31924 ast_log(LOG_ERROR, "Unable to initialize the SIP request and response parser\n");
31925 return AST_MODULE_LOAD_DECLINE;
31926 }
31927
31928 if (can_parse_xml) {
31929
31930
31931
31932 if (ast_cc_agent_register(&sip_cc_agent_callbacks)) {
31933 return AST_MODULE_LOAD_DECLINE;
31934 }
31935 }
31936 if (ast_cc_monitor_register(&sip_cc_monitor_callbacks)) {
31937 return AST_MODULE_LOAD_DECLINE;
31938 }
31939 if (!(sip_monitor_instances = ao2_container_alloc(37, sip_monitor_instance_hash_fn, sip_monitor_instance_cmp_fn))) {
31940 return AST_MODULE_LOAD_DECLINE;
31941 }
31942
31943
31944 restart_monitor();
31945
31946 ast_realtime_require_field(ast_check_realtime("sipregs") ? "sipregs" : "sippeers",
31947 "name", RQ_CHAR, 10,
31948 "ipaddr", RQ_CHAR, INET6_ADDRSTRLEN - 1,
31949 "port", RQ_UINTEGER2, 5,
31950 "regseconds", RQ_INTEGER4, 11,
31951 "defaultuser", RQ_CHAR, 10,
31952 "fullcontact", RQ_CHAR, 35,
31953 "regserver", RQ_CHAR, 20,
31954 "useragent", RQ_CHAR, 20,
31955 "lastms", RQ_INTEGER4, 11,
31956 SENTINEL);
31957
31958
31959 sip_register_tests();
31960 network_change_event_subscribe();
31961
31962 return AST_MODULE_LOAD_SUCCESS;
31963 }
31964
31965
31966 static int unload_module(void)
31967 {
31968 struct sip_pvt *p;
31969 struct sip_threadinfo *th;
31970 struct ast_context *con;
31971 struct ao2_iterator i;
31972 int wait_count;
31973
31974 network_change_event_unsubscribe();
31975
31976 ast_sched_dump(sched);
31977
31978
31979 ast_channel_unregister(&sip_tech);
31980
31981
31982 ast_custom_function_unregister(&sipchaninfo_function);
31983 ast_custom_function_unregister(&sippeer_function);
31984 ast_custom_function_unregister(&sip_header_function);
31985 ast_custom_function_unregister(&checksipdomain_function);
31986
31987
31988 ast_unregister_application(app_dtmfmode);
31989 ast_unregister_application(app_sipaddheader);
31990 ast_unregister_application(app_sipremoveheader);
31991
31992 #ifdef TEST_FRAMEWORK
31993 AST_TEST_UNREGISTER(test_sip_peers_get);
31994 AST_TEST_UNREGISTER(test_sip_mwi_subscribe_parse);
31995 AST_TEST_UNREGISTER(test_tcp_message_fragmentation);
31996 AST_TEST_UNREGISTER(get_in_brackets_const_test);
31997 #endif
31998
31999 ast_data_unregister(NULL);
32000
32001
32002 ast_cli_unregister_multiple(cli_sip, ARRAY_LEN(cli_sip));
32003
32004
32005 ast_udptl_proto_unregister(&sip_udptl);
32006
32007
32008 ast_rtp_glue_unregister(&sip_rtp_glue);
32009
32010
32011 ast_manager_unregister("SIPpeers");
32012 ast_manager_unregister("SIPshowpeer");
32013 ast_manager_unregister("SIPqualifypeer");
32014 ast_manager_unregister("SIPshowregistry");
32015 ast_manager_unregister("SIPnotify");
32016
32017
32018 if (sip_tcp_desc.master) {
32019 ast_tcptls_server_stop(&sip_tcp_desc);
32020 }
32021 if (sip_tls_desc.master) {
32022 ast_tcptls_server_stop(&sip_tls_desc);
32023 }
32024 ast_ssl_teardown(sip_tls_desc.tls_cfg);
32025
32026
32027 i = ao2_iterator_init(threadt, 0);
32028 while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
32029 pthread_t thread = th->threadid;
32030 th->stop = 1;
32031 pthread_kill(thread, SIGURG);
32032 ao2_t_ref(th, -1, "decrement ref from iterator");
32033 }
32034 ao2_iterator_destroy(&i);
32035
32036
32037 i = ao2_iterator_init(dialogs, 0);
32038 while ((p = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
32039 if (p->owner)
32040 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
32041 ao2_t_ref(p, -1, "toss dialog ptr from iterator_next");
32042 }
32043 ao2_iterator_destroy(&i);
32044
32045 unlink_all_peers_from_tables();
32046
32047 ast_mutex_lock(&monlock);
32048 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
32049 pthread_t th = monitor_thread;
32050 monitor_thread = AST_PTHREADT_STOP;
32051 pthread_cancel(th);
32052 pthread_kill(th, SIGURG);
32053 ast_mutex_unlock(&monlock);
32054 pthread_join(th, NULL);
32055 } else {
32056 monitor_thread = AST_PTHREADT_STOP;
32057 ast_mutex_unlock(&monlock);
32058 }
32059
32060
32061 i = ao2_iterator_init(dialogs, 0);
32062 while ((p = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
32063 dialog_unlink_all(p);
32064 ao2_t_ref(p, -1, "throw away iterator result");
32065 }
32066 ao2_iterator_destroy(&i);
32067
32068
32069 ast_free_ha(localaddr);
32070
32071 ast_mutex_lock(&authl_lock);
32072 if (authl) {
32073 ao2_t_ref(authl, -1, "Removing global authentication");
32074 authl = NULL;
32075 }
32076 ast_mutex_unlock(&authl_lock);
32077
32078 sip_epa_unregister_all();
32079 destroy_escs();
32080
32081 ast_free(default_tls_cfg.certfile);
32082 ast_free(default_tls_cfg.pvtfile);
32083 ast_free(default_tls_cfg.cipher);
32084 ast_free(default_tls_cfg.cafile);
32085 ast_free(default_tls_cfg.capath);
32086
32087 cleanup_all_regs();
32088 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
32089 ASTOBJ_CONTAINER_DESTROY(®l);
32090
32091 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
32092 ASTOBJ_WRLOCK(iterator);
32093 if (iterator->dnsmgr) {
32094 ast_dnsmgr_release(iterator->dnsmgr);
32095 iterator->dnsmgr = NULL;
32096 ASTOBJ_UNREF(iterator, sip_subscribe_mwi_destroy);
32097 }
32098 ASTOBJ_UNLOCK(iterator);
32099 } while(0));
32100 ASTOBJ_CONTAINER_DESTROYALL(&submwil, sip_subscribe_mwi_destroy);
32101 ASTOBJ_CONTAINER_DESTROY(&submwil);
32102
32103
32104
32105
32106
32107
32108
32109
32110 wait_count = 1000;
32111 while (ao2_container_count(threadt) && --wait_count) {
32112 sched_yield();
32113 }
32114 if (!wait_count) {
32115 ast_debug(2, "TCP/TLS thread container did not become empty :(\n");
32116 }
32117
32118 ao2_t_ref(bogus_peer, -1, "unref the bogus_peer");
32119
32120 ao2_t_ref(peers, -1, "unref the peers table");
32121 ao2_t_ref(peers_by_ip, -1, "unref the peers_by_ip table");
32122 ao2_t_ref(dialogs, -1, "unref the dialogs table");
32123 ao2_t_ref(dialogs_to_destroy, -1, "unref dialogs_to_destroy");
32124 ao2_t_ref(threadt, -1, "unref the thread table");
32125 ao2_t_ref(sip_monitor_instances, -1, "unref the sip_monitor_instances table");
32126
32127 clear_sip_domains();
32128 ast_free_ha(sip_cfg.contact_ha);
32129 if (sipsock_read_id) {
32130 ast_io_remove(io, sipsock_read_id);
32131 sipsock_read_id = NULL;
32132 }
32133 close(sipsock);
32134 io_context_destroy(io);
32135 sched_context_destroy(sched);
32136 con = ast_context_find(used_context);
32137 if (con) {
32138 ast_context_destroy(con, "SIP");
32139 }
32140 ast_unload_realtime("sipregs");
32141 ast_unload_realtime("sippeers");
32142 ast_cc_monitor_unregister(&sip_cc_monitor_callbacks);
32143 ast_cc_agent_unregister(&sip_cc_agent_callbacks);
32144
32145 sip_reqresp_parser_exit();
32146 sip_unregister_tests();
32147
32148 if (notify_types) {
32149 ast_config_destroy(notify_types);
32150 notify_types = NULL;
32151 }
32152
32153 return 0;
32154 }
32155
32156 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Session Initiation Protocol (SIP)",
32157 .load = load_module,
32158 .unload = unload_module,
32159 .reload = reload,
32160 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
32161 .nonoptreq = "res_crypto,chan_local",
32162 );