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 #include "asterisk.h"
00210
00211 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 324704 $")
00212
00213 #include <signal.h>
00214 #include <sys/signal.h>
00215 #include <regex.h>
00216 #include <inttypes.h>
00217
00218 #include "asterisk/network.h"
00219 #include "asterisk/paths.h"
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 #include "asterisk/lock.h"
00233 #include "asterisk/config.h"
00234 #include "asterisk/module.h"
00235 #include "asterisk/pbx.h"
00236 #include "asterisk/sched.h"
00237 #include "asterisk/io.h"
00238 #include "asterisk/rtp_engine.h"
00239 #include "asterisk/udptl.h"
00240 #include "asterisk/acl.h"
00241 #include "asterisk/manager.h"
00242 #include "asterisk/callerid.h"
00243 #include "asterisk/cli.h"
00244 #include "asterisk/musiconhold.h"
00245 #include "asterisk/dsp.h"
00246 #include "asterisk/features.h"
00247 #include "asterisk/srv.h"
00248 #include "asterisk/astdb.h"
00249 #include "asterisk/causes.h"
00250 #include "asterisk/utils.h"
00251 #include "asterisk/file.h"
00252 #include "asterisk/astobj2.h"
00253 #include "asterisk/dnsmgr.h"
00254 #include "asterisk/devicestate.h"
00255 #include "asterisk/monitor.h"
00256 #include "asterisk/netsock2.h"
00257 #include "asterisk/localtime.h"
00258 #include "asterisk/abstract_jb.h"
00259 #include "asterisk/threadstorage.h"
00260 #include "asterisk/translate.h"
00261 #include "asterisk/ast_version.h"
00262 #include "asterisk/event.h"
00263 #include "asterisk/cel.h"
00264 #include "asterisk/data.h"
00265 #include "asterisk/aoc.h"
00266 #include "sip/include/sip.h"
00267 #include "sip/include/globals.h"
00268 #include "sip/include/config_parser.h"
00269 #include "sip/include/reqresp_parser.h"
00270 #include "sip/include/sip_utils.h"
00271 #include "sip/include/srtp.h"
00272 #include "sip/include/sdp_crypto.h"
00273 #include "asterisk/ccss.h"
00274 #include "asterisk/xml.h"
00275 #include "sip/include/dialog.h"
00276 #include "sip/include/dialplan_functions.h"
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
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557 static int min_expiry = DEFAULT_MIN_EXPIRY;
00558 static int max_expiry = DEFAULT_MAX_EXPIRY;
00559 static int default_expiry = DEFAULT_DEFAULT_EXPIRY;
00560 static int mwi_expiry = DEFAULT_MWI_EXPIRY;
00561
00562 static int unauth_sessions = 0;
00563 static int authlimit = DEFAULT_AUTHLIMIT;
00564 static int authtimeout = DEFAULT_AUTHTIMEOUT;
00565
00566
00567 static struct ast_jb_conf default_jbconf =
00568 {
00569 .flags = 0,
00570 .max_size = -1,
00571 .resync_threshold = -1,
00572 .impl = "",
00573 .target_extra = -1,
00574 };
00575 static struct ast_jb_conf global_jbconf;
00576
00577 static const char config[] = "sip.conf";
00578 static const char notify_config[] = "sip_notify.conf";
00579
00580
00581
00582 static const struct invstate2stringtable {
00583 const enum invitestates state;
00584 const char *desc;
00585 } invitestate2string[] = {
00586 {INV_NONE, "None" },
00587 {INV_CALLING, "Calling (Trying)"},
00588 {INV_PROCEEDING, "Proceeding "},
00589 {INV_EARLY_MEDIA, "Early media"},
00590 {INV_COMPLETED, "Completed (done)"},
00591 {INV_CONFIRMED, "Confirmed (up)"},
00592 {INV_TERMINATED, "Done"},
00593 {INV_CANCELLED, "Cancelled"}
00594 };
00595
00596
00597
00598
00599
00600
00601 static const struct cfsubscription_types {
00602 enum subscriptiontype type;
00603 const char * const event;
00604 const char * const mediatype;
00605 const char * const text;
00606 } subscription_types[] = {
00607 { NONE, "-", "unknown", "unknown" },
00608
00609 { DIALOG_INFO_XML, "dialog", "application/dialog-info+xml", "dialog-info+xml" },
00610 { CPIM_PIDF_XML, "presence", "application/cpim-pidf+xml", "cpim-pidf+xml" },
00611 { PIDF_XML, "presence", "application/pidf+xml", "pidf+xml" },
00612 { XPIDF_XML, "presence", "application/xpidf+xml", "xpidf+xml" },
00613 { MWI_NOTIFICATION, "message-summary", "application/simple-message-summary", "mwi" }
00614 };
00615
00616
00617
00618
00619
00620
00621 static const struct cfsip_methods {
00622 enum sipmethod id;
00623 int need_rtp;
00624 char * const text;
00625 enum can_create_dialog can_create;
00626 } sip_methods[] = {
00627 { SIP_UNKNOWN, RTP, "-UNKNOWN-",CAN_CREATE_DIALOG },
00628 { SIP_RESPONSE, NO_RTP, "SIP/2.0", CAN_NOT_CREATE_DIALOG },
00629 { SIP_REGISTER, NO_RTP, "REGISTER", CAN_CREATE_DIALOG },
00630 { SIP_OPTIONS, NO_RTP, "OPTIONS", CAN_CREATE_DIALOG },
00631 { SIP_NOTIFY, NO_RTP, "NOTIFY", CAN_CREATE_DIALOG },
00632 { SIP_INVITE, RTP, "INVITE", CAN_CREATE_DIALOG },
00633 { SIP_ACK, NO_RTP, "ACK", CAN_NOT_CREATE_DIALOG },
00634 { SIP_PRACK, NO_RTP, "PRACK", CAN_NOT_CREATE_DIALOG },
00635 { SIP_BYE, NO_RTP, "BYE", CAN_NOT_CREATE_DIALOG },
00636 { SIP_REFER, NO_RTP, "REFER", CAN_CREATE_DIALOG },
00637 { SIP_SUBSCRIBE, NO_RTP, "SUBSCRIBE",CAN_CREATE_DIALOG },
00638 { SIP_MESSAGE, NO_RTP, "MESSAGE", CAN_CREATE_DIALOG },
00639 { SIP_UPDATE, NO_RTP, "UPDATE", CAN_NOT_CREATE_DIALOG },
00640 { SIP_INFO, NO_RTP, "INFO", CAN_NOT_CREATE_DIALOG },
00641 { SIP_CANCEL, NO_RTP, "CANCEL", CAN_NOT_CREATE_DIALOG },
00642 { SIP_PUBLISH, NO_RTP, "PUBLISH", CAN_CREATE_DIALOG },
00643 { SIP_PING, NO_RTP, "PING", CAN_CREATE_DIALOG_UNSUPPORTED_METHOD }
00644 };
00645
00646
00647
00648
00649
00650
00651
00652
00653 static const struct sip_reasons {
00654 enum AST_REDIRECTING_REASON code;
00655 char * const text;
00656 } sip_reason_table[] = {
00657 { AST_REDIRECTING_REASON_UNKNOWN, "unknown" },
00658 { AST_REDIRECTING_REASON_USER_BUSY, "user-busy" },
00659 { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer" },
00660 { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable" },
00661 { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional" },
00662 { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day" },
00663 { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb" },
00664 { AST_REDIRECTING_REASON_DEFLECTION, "deflection" },
00665 { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me" },
00666 { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service" },
00667 { AST_REDIRECTING_REASON_AWAY, "away" },
00668 { AST_REDIRECTING_REASON_CALL_FWD_DTE, "unknown"}
00669 };
00670
00671
00672
00673
00674
00675
00676
00677 static char default_language[MAX_LANGUAGE];
00678 static char default_callerid[AST_MAX_EXTENSION];
00679 static char default_mwi_from[80];
00680 static char default_fromdomain[AST_MAX_EXTENSION];
00681 static int default_fromdomainport;
00682 static char default_notifymime[AST_MAX_EXTENSION];
00683 static char default_vmexten[AST_MAX_EXTENSION];
00684 static int default_qualify;
00685 static char default_mohinterpret[MAX_MUSICCLASS];
00686 static char default_mohsuggest[MAX_MUSICCLASS];
00687
00688 static char default_parkinglot[AST_MAX_CONTEXT];
00689 static char default_engine[256];
00690 static int default_maxcallbitrate;
00691 static struct ast_codec_pref default_prefs;
00692 static unsigned int default_transports;
00693 static unsigned int default_primary_transport;
00694
00695
00696 static struct sip_settings sip_cfg;
00697
00698
00699
00700 #define SIP_PEDANTIC_DECODE(str) \
00701 if (sip_cfg.pedanticsipchecking && !ast_strlen_zero(str)) { \
00702 ast_uri_decode(str); \
00703 } \
00704
00705 static unsigned int chan_idx;
00706 static int global_match_auth_username;
00707
00708 static int global_relaxdtmf;
00709 static int global_prematuremediafilter;
00710 static int global_rtptimeout;
00711 static int global_rtpholdtimeout;
00712 static int global_rtpkeepalive;
00713 static int global_reg_timeout;
00714 static int global_regattempts_max;
00715 static int global_shrinkcallerid;
00716 static int global_callcounter;
00717
00718
00719 static unsigned int global_tos_sip;
00720 static unsigned int global_tos_audio;
00721 static unsigned int global_tos_video;
00722 static unsigned int global_tos_text;
00723 static unsigned int global_cos_sip;
00724 static unsigned int global_cos_audio;
00725 static unsigned int global_cos_video;
00726 static unsigned int global_cos_text;
00727 static unsigned int recordhistory;
00728 static unsigned int dumphistory;
00729 static char global_useragent[AST_MAX_EXTENSION];
00730 static char global_sdpsession[AST_MAX_EXTENSION];
00731 static char global_sdpowner[AST_MAX_EXTENSION];
00732 static int global_authfailureevents;
00733 static int global_t1;
00734 static int global_t1min;
00735 static int global_timer_b;
00736 static unsigned int global_autoframing;
00737 static int global_qualifyfreq;
00738 static int global_qualify_gap;
00739 static int global_qualify_peers;
00740
00741 static enum st_mode global_st_mode;
00742 static enum st_refresher global_st_refresher;
00743 static int global_min_se;
00744 static int global_max_se;
00745
00746 static int global_dynamic_exclude_static = 0;
00747
00748
00749
00750
00751
00752
00753
00754
00755 static int can_parse_xml;
00756
00757
00758
00759
00760 static int speerobjs = 0;
00761 static int rpeerobjs = 0;
00762 static int apeerobjs = 0;
00763 static int regobjs = 0;
00764
00765
00766 static struct ast_flags global_flags[3] = {{0}};
00767 static int global_t38_maxdatagram;
00768
00769 static struct ast_event_sub *network_change_event_subscription;
00770 static int network_change_event_sched_id = -1;
00771
00772 static char used_context[AST_MAX_CONTEXT];
00773
00774 AST_MUTEX_DEFINE_STATIC(netlock);
00775
00776
00777
00778 AST_MUTEX_DEFINE_STATIC(monlock);
00779
00780 AST_MUTEX_DEFINE_STATIC(sip_reload_lock);
00781
00782
00783
00784 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00785
00786 static int sip_reloading = FALSE;
00787 static enum channelreloadreason sip_reloadreason;
00788
00789 struct sched_context *sched;
00790 static struct io_context *io;
00791 static int *sipsock_read_id;
00792 struct sip_pkt;
00793 static AST_LIST_HEAD_STATIC(domain_list, domain);
00794
00795 AST_LIST_HEAD_NOLOCK(sip_history_head, sip_history);
00796
00797 static enum sip_debug_e sipdebug;
00798
00799
00800
00801
00802
00803 static int sipdebug_text;
00804
00805 static const struct _map_x_s referstatusstrings[] = {
00806 { REFER_IDLE, "<none>" },
00807 { REFER_SENT, "Request sent" },
00808 { REFER_RECEIVED, "Request received" },
00809 { REFER_CONFIRMED, "Confirmed" },
00810 { REFER_ACCEPTED, "Accepted" },
00811 { REFER_RINGING, "Target ringing" },
00812 { REFER_200OK, "Done" },
00813 { REFER_FAILED, "Failed" },
00814 { REFER_NOAUTH, "Failed - auth failure" },
00815 { -1, NULL}
00816 };
00817
00818
00819 #ifdef LOW_MEMORY
00820 static const int HASH_PEER_SIZE = 17;
00821 static const int HASH_DIALOG_SIZE = 17;
00822 #else
00823 static const int HASH_PEER_SIZE = 563;
00824 static const int HASH_DIALOG_SIZE = 563;
00825 #endif
00826
00827 static const struct {
00828 enum ast_cc_service_type service;
00829 const char *service_string;
00830 } sip_cc_service_map [] = {
00831 [AST_CC_NONE] = { AST_CC_NONE, "" },
00832 [AST_CC_CCBS] = { AST_CC_CCBS, "BS" },
00833 [AST_CC_CCNR] = { AST_CC_CCNR, "NR" },
00834 [AST_CC_CCNL] = { AST_CC_CCNL, "NL" },
00835 };
00836
00837 static enum ast_cc_service_type service_string_to_service_type(const char * const service_string)
00838 {
00839 enum ast_cc_service_type service;
00840 for (service = AST_CC_CCBS; service <= AST_CC_CCNL; ++service) {
00841 if (!strcasecmp(service_string, sip_cc_service_map[service].service_string)) {
00842 return service;
00843 }
00844 }
00845 return AST_CC_NONE;
00846 }
00847
00848 static const struct {
00849 enum sip_cc_notify_state state;
00850 const char *state_string;
00851 } sip_cc_notify_state_map [] = {
00852 [CC_QUEUED] = {CC_QUEUED, "cc-state: queued"},
00853 [CC_READY] = {CC_READY, "cc-state: ready"},
00854 };
00855
00856 AST_LIST_HEAD_STATIC(epa_static_data_list, epa_backend);
00857
00858 static int sip_epa_register(const struct epa_static_data *static_data)
00859 {
00860 struct epa_backend *backend = ast_calloc(1, sizeof(*backend));
00861
00862 if (!backend) {
00863 return -1;
00864 }
00865
00866 backend->static_data = static_data;
00867
00868 AST_LIST_LOCK(&epa_static_data_list);
00869 AST_LIST_INSERT_TAIL(&epa_static_data_list, backend, next);
00870 AST_LIST_UNLOCK(&epa_static_data_list);
00871 return 0;
00872 }
00873
00874 static void cc_handle_publish_error(struct sip_pvt *pvt, const int resp, struct sip_request *req, struct sip_epa_entry *epa_entry);
00875
00876 static void cc_epa_destructor(void *data)
00877 {
00878 struct sip_epa_entry *epa_entry = data;
00879 struct cc_epa_entry *cc_entry = epa_entry->instance_data;
00880 ast_free(cc_entry);
00881 }
00882
00883 static const struct epa_static_data cc_epa_static_data = {
00884 .event = CALL_COMPLETION,
00885 .name = "call-completion",
00886 .handle_error = cc_handle_publish_error,
00887 .destructor = cc_epa_destructor,
00888 };
00889
00890 static const struct epa_static_data *find_static_data(const char * const event_package)
00891 {
00892 const struct epa_backend *backend = NULL;
00893
00894 AST_LIST_LOCK(&epa_static_data_list);
00895 AST_LIST_TRAVERSE(&epa_static_data_list, backend, next) {
00896 if (!strcmp(backend->static_data->name, event_package)) {
00897 break;
00898 }
00899 }
00900 AST_LIST_UNLOCK(&epa_static_data_list);
00901 return backend ? backend->static_data : NULL;
00902 }
00903
00904 static struct sip_epa_entry *create_epa_entry (const char * const event_package, const char * const destination)
00905 {
00906 struct sip_epa_entry *epa_entry;
00907 const struct epa_static_data *static_data;
00908
00909 if (!(static_data = find_static_data(event_package))) {
00910 return NULL;
00911 }
00912
00913 if (!(epa_entry = ao2_t_alloc(sizeof(*epa_entry), static_data->destructor, "Allocate new EPA entry"))) {
00914 return NULL;
00915 }
00916
00917 epa_entry->static_data = static_data;
00918 ast_copy_string(epa_entry->destination, destination, sizeof(epa_entry->destination));
00919 return epa_entry;
00920 }
00921
00922
00923
00924
00925 static int esc_etag_counter;
00926 static const int DEFAULT_PUBLISH_EXPIRES = 3600;
00927
00928 #ifdef HAVE_LIBXML2
00929 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);
00930
00931 static const struct sip_esc_publish_callbacks cc_esc_publish_callbacks = {
00932 .initial_handler = cc_esc_publish_handler,
00933 .modify_handler = cc_esc_publish_handler,
00934 };
00935 #endif
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949 static struct event_state_compositor {
00950 enum subscriptiontype event;
00951 const char * name;
00952 const struct sip_esc_publish_callbacks *callbacks;
00953 struct ao2_container *compositor;
00954 } event_state_compositors [] = {
00955 #ifdef HAVE_LIBXML2
00956 {CALL_COMPLETION, "call-completion", &cc_esc_publish_callbacks},
00957 #endif
00958 };
00959
00960 static const int ESC_MAX_BUCKETS = 37;
00961
00962 static void esc_entry_destructor(void *obj)
00963 {
00964 struct sip_esc_entry *esc_entry = obj;
00965 if (esc_entry->sched_id > -1) {
00966 AST_SCHED_DEL(sched, esc_entry->sched_id);
00967 }
00968 }
00969
00970 static int esc_hash_fn(const void *obj, const int flags)
00971 {
00972 const struct sip_esc_entry *entry = obj;
00973 return ast_str_hash(entry->entity_tag);
00974 }
00975
00976 static int esc_cmp_fn(void *obj, void *arg, int flags)
00977 {
00978 struct sip_esc_entry *entry1 = obj;
00979 struct sip_esc_entry *entry2 = arg;
00980
00981 return (!strcmp(entry1->entity_tag, entry2->entity_tag)) ? (CMP_MATCH | CMP_STOP) : 0;
00982 }
00983
00984 static struct event_state_compositor *get_esc(const char * const event_package) {
00985 int i;
00986 for (i = 0; i < ARRAY_LEN(event_state_compositors); i++) {
00987 if (!strcasecmp(event_package, event_state_compositors[i].name)) {
00988 return &event_state_compositors[i];
00989 }
00990 }
00991 return NULL;
00992 }
00993
00994 static struct sip_esc_entry *get_esc_entry(const char * entity_tag, struct event_state_compositor *esc) {
00995 struct sip_esc_entry *entry;
00996 struct sip_esc_entry finder;
00997
00998 ast_copy_string(finder.entity_tag, entity_tag, sizeof(finder.entity_tag));
00999
01000 entry = ao2_find(esc->compositor, &finder, OBJ_POINTER);
01001
01002 return entry;
01003 }
01004
01005 static int publish_expire(const void *data)
01006 {
01007 struct sip_esc_entry *esc_entry = (struct sip_esc_entry *) data;
01008 struct event_state_compositor *esc = get_esc(esc_entry->event);
01009
01010 ast_assert(esc != NULL);
01011
01012 ao2_unlink(esc->compositor, esc_entry);
01013 ao2_ref(esc_entry, -1);
01014 return 0;
01015 }
01016
01017 static void create_new_sip_etag(struct sip_esc_entry *esc_entry, int is_linked)
01018 {
01019 int new_etag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
01020 struct event_state_compositor *esc = get_esc(esc_entry->event);
01021
01022 ast_assert(esc != NULL);
01023 if (is_linked) {
01024 ao2_unlink(esc->compositor, esc_entry);
01025 }
01026 snprintf(esc_entry->entity_tag, sizeof(esc_entry->entity_tag), "%d", new_etag);
01027 ao2_link(esc->compositor, esc_entry);
01028 }
01029
01030 static struct sip_esc_entry *create_esc_entry(struct event_state_compositor *esc, struct sip_request *req, const int expires)
01031 {
01032 struct sip_esc_entry *esc_entry;
01033 int expires_ms;
01034
01035 if (!(esc_entry = ao2_alloc(sizeof(*esc_entry), esc_entry_destructor))) {
01036 return NULL;
01037 }
01038
01039 esc_entry->event = esc->name;
01040
01041 expires_ms = expires * 1000;
01042
01043 ao2_ref(esc_entry, +1);
01044 esc_entry->sched_id = ast_sched_add(sched, expires_ms, publish_expire, esc_entry);
01045
01046
01047 create_new_sip_etag(esc_entry, 0);
01048
01049 return esc_entry;
01050 }
01051
01052 static int initialize_escs(void)
01053 {
01054 int i, res = 0;
01055 for (i = 0; i < ARRAY_LEN(event_state_compositors); i++) {
01056 if (!((event_state_compositors[i].compositor) =
01057 ao2_container_alloc(ESC_MAX_BUCKETS, esc_hash_fn, esc_cmp_fn))) {
01058 res = -1;
01059 }
01060 }
01061 return res;
01062 }
01063
01064 static void destroy_escs(void)
01065 {
01066 int i;
01067 for (i = 0; i < ARRAY_LEN(event_state_compositors); i++) {
01068 ao2_ref(event_state_compositors[i].compositor, -1);
01069 }
01070 }
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080 static struct ao2_container *dialogs;
01081 #define sip_pvt_lock(x) ao2_lock(x)
01082 #define sip_pvt_trylock(x) ao2_trylock(x)
01083 #define sip_pvt_unlock(x) ao2_unlock(x)
01084
01085
01086 static struct ao2_container *threadt;
01087
01088
01089 static struct ao2_container *peers;
01090 static struct ao2_container *peers_by_ip;
01091
01092
01093 static struct ast_register_list {
01094 ASTOBJ_CONTAINER_COMPONENTS(struct sip_registry);
01095 int recheck;
01096 } regl;
01097
01098
01099 static struct ast_subscription_mwi_list {
01100 ASTOBJ_CONTAINER_COMPONENTS(struct sip_subscription_mwi);
01101 } submwil;
01102 static int temp_pvt_init(void *);
01103 static void temp_pvt_cleanup(void *);
01104
01105
01106 AST_THREADSTORAGE_CUSTOM(ts_temp_pvt, temp_pvt_init, temp_pvt_cleanup);
01107
01108
01109
01110 static struct sip_auth *authl = NULL;
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128 static int sipsock = -1;
01129
01130 struct ast_sockaddr bindaddr;
01131
01132
01133
01134
01135
01136
01137
01138 static struct ast_sockaddr internip;
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155 static struct ast_sockaddr externaddr;
01156 static struct ast_sockaddr media_address;
01157
01158 static char externhost[MAXHOSTNAMELEN];
01159 static time_t externexpire;
01160 static int externrefresh = 10;
01161 static uint16_t externtcpport;
01162 static uint16_t externtlsport;
01163
01164
01165
01166
01167
01168
01169
01170 static struct ast_ha *localaddr;
01171
01172 static int ourport_tcp;
01173 static int ourport_tls;
01174 static struct ast_sockaddr debugaddr;
01175
01176 static struct ast_config *notify_types = NULL;
01177
01178
01179
01180 #define UNLINK(element, head, prev) do { \
01181 if (prev) \
01182 (prev)->next = (element)->next; \
01183 else \
01184 (head) = (element)->next; \
01185 } while (0)
01186
01187
01188
01189
01190
01191
01192 static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
01193 static int sip_devicestate(void *data);
01194 static int sip_sendtext(struct ast_channel *ast, const char *text);
01195 static int sip_call(struct ast_channel *ast, char *dest, int timeout);
01196 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen);
01197 static int sip_hangup(struct ast_channel *ast);
01198 static int sip_answer(struct ast_channel *ast);
01199 static struct ast_frame *sip_read(struct ast_channel *ast);
01200 static int sip_write(struct ast_channel *ast, struct ast_frame *frame);
01201 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
01202 static int sip_transfer(struct ast_channel *ast, const char *dest);
01203 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01204 static int sip_senddigit_begin(struct ast_channel *ast, char digit);
01205 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
01206 static int sip_setoption(struct ast_channel *chan, int option, void *data, int datalen);
01207 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen);
01208 static const char *sip_get_callid(struct ast_channel *chan);
01209
01210 static int handle_request_do(struct sip_request *req, struct ast_sockaddr *addr);
01211 static int sip_standard_port(enum sip_transport type, int port);
01212 static int sip_prepare_socket(struct sip_pvt *p);
01213 static int get_address_family_filter(const struct ast_sockaddr *addr);
01214
01215
01216 static int sipsock_read(int *id, int fd, short events, void *ignore);
01217 static int __sip_xmit(struct sip_pvt *p, struct ast_str *data, int len);
01218 static int __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, struct ast_str *data, int len, int fatal, int sipmethod);
01219 static void add_cc_call_info_to_response(struct sip_pvt *p, struct sip_request *resp);
01220 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01221 static int retrans_pkt(const void *data);
01222 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);
01223 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01224 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01225 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req);
01226 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);
01227 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported);
01228 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);
01229 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp);
01230 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable);
01231 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable);
01232 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, enum xmittype reliable, int newbranch);
01233 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch);
01234 static int transmit_publish(struct sip_epa_entry *epa_entry, enum sip_publish_type publish_type, const char * const explicit_uri);
01235 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init, const char * const explicit_uri);
01236 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp);
01237 static int transmit_info_with_aoc(struct sip_pvt *p, struct ast_aoc_decoded *decoded);
01238 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration);
01239 static int transmit_info_with_vidupdate(struct sip_pvt *p);
01240 static int transmit_message_with_text(struct sip_pvt *p, const char *text);
01241 static int transmit_refer(struct sip_pvt *p, const char *dest);
01242 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, const char *vmexten);
01243 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate);
01244 static int transmit_cc_notify(struct ast_cc_agent *agent, struct sip_pvt *subscription, enum sip_cc_notify_state state);
01245 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader);
01246 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01247 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno);
01248 static void copy_request(struct sip_request *dst, const struct sip_request *src);
01249 static void receive_message(struct sip_pvt *p, struct sip_request *req);
01250 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req, char **name, char **number, int set_call_forward);
01251 static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only);
01252
01253
01254 static int __sip_autodestruct(const void *data);
01255 static void *registry_unref(struct sip_registry *reg, char *tag);
01256 static int update_call_counter(struct sip_pvt *fup, int event);
01257 static int auto_congest(const void *arg);
01258 static struct sip_pvt *find_call(struct sip_request *req, struct ast_sockaddr *addr, const int intended_method);
01259 static void free_old_route(struct sip_route *route);
01260 static void list_route(struct sip_route *route);
01261 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards);
01262 static enum check_auth_result register_verify(struct sip_pvt *p, struct ast_sockaddr *addr,
01263 struct sip_request *req, const char *uri);
01264 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag);
01265 static void check_pendings(struct sip_pvt *p);
01266 static void *sip_park_thread(void *stuff);
01267 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno, char *parkexten);
01268 static int sip_sipredirect(struct sip_pvt *p, const char *dest);
01269 static int is_method_allowed(unsigned int *allowed_methods, enum sipmethod method);
01270
01271
01272 static void try_suggested_sip_codec(struct sip_pvt *p);
01273 static const char *get_sdp_iterate(int* start, struct sip_request *req, const char *name);
01274 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value);
01275 static int find_sdp(struct sip_request *req);
01276 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action);
01277 static int process_sdp_o(const char *o, struct sip_pvt *p);
01278 static int process_sdp_c(const char *c, struct ast_sockaddr *addr);
01279 static int process_sdp_a_sendonly(const char *a, int *sendonly);
01280 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec);
01281 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newvideortp, int *last_rtpmap_codec);
01282 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);
01283 static int process_sdp_a_image(const char *a, struct sip_pvt *p);
01284 static void add_codec_to_sdp(const struct sip_pvt *p, format_t codec,
01285 struct ast_str **m_buf, struct ast_str **a_buf,
01286 int debug, int *min_packet_size);
01287 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format,
01288 struct ast_str **m_buf, struct ast_str **a_buf,
01289 int debug);
01290 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38);
01291 static void do_setnat(struct sip_pvt *p);
01292 static void stop_media_flows(struct sip_pvt *p);
01293
01294
01295 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len);
01296 static int build_reply_digest(struct sip_pvt *p, int method, char *digest, int digest_len);
01297 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
01298 const char *secret, const char *md5secret, int sipmethod,
01299 const char *uri, enum xmittype reliable, int ignore);
01300 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
01301 int sipmethod, const char *uri, enum xmittype reliable,
01302 struct ast_sockaddr *addr, struct sip_peer **authpeer);
01303 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, const char *uri, enum xmittype reliable, struct ast_sockaddr *addr);
01304
01305
01306 static int check_sip_domain(const char *domain, char *context, size_t len);
01307 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context);
01308 static void clear_sip_domains(void);
01309
01310
01311 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, const char *configuration, int lineno);
01312 static int clear_realm_authentication(struct sip_auth *authlist);
01313 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);
01314
01315
01316 static void check_rtp_timeout(struct sip_pvt *dialog, time_t t);
01317 static int sip_do_reload(enum channelreloadreason reason);
01318 static int reload_config(enum channelreloadreason reason);
01319 static int expire_register(const void *data);
01320 static void *do_monitor(void *data);
01321 static int restart_monitor(void);
01322 static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer);
01323 static struct ast_variable *copy_vars(struct ast_variable *src);
01324 static int dialog_find_multiple(void *obj, void *arg, int flags);
01325
01326 static int sip_refer_allocate(struct sip_pvt *p);
01327 static int sip_notify_allocate(struct sip_pvt *p);
01328 static void ast_quiet_chan(struct ast_channel *chan);
01329 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target);
01330 static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context);
01331
01332
01333 static int cb_extensionstate(char *context, char* exten, int state, void *data);
01334 static int sip_devicestate(void *data);
01335 static int sip_poke_noanswer(const void *data);
01336 static int sip_poke_peer(struct sip_peer *peer, int force);
01337 static void sip_poke_all_peers(void);
01338 static void sip_peer_hold(struct sip_pvt *p, int hold);
01339 static void mwi_event_cb(const struct ast_event *, void *);
01340 static void network_change_event_cb(const struct ast_event *, void *);
01341
01342
01343 static const char *sip_nat_mode(const struct sip_pvt *p);
01344 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01345 static char *transfermode2str(enum transfermodes mode) attribute_const;
01346 static int peer_status(struct sip_peer *peer, char *status, int statuslen);
01347 static char *sip_show_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01348 static char * _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01349 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01350 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01351 static void print_group(int fd, ast_group_t group, int crlf);
01352 static const char *dtmfmode2str(int mode) attribute_const;
01353 static int str2dtmfmode(const char *str) attribute_unused;
01354 static const char *insecure2str(int mode) attribute_const;
01355 static void cleanup_stale_contexts(char *new, char *old);
01356 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
01357 static const char *domain_mode_to_text(const enum domain_mode mode);
01358 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01359 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01360 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01361 static char *_sip_qualify_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
01362 static char *sip_qualify_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01363 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01364 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01365 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01366 static char *sip_show_mwi(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01367 static const char *subscription_type2str(enum subscriptiontype subtype) attribute_pure;
01368 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01369 static char *complete_sip_peer(const char *word, int state, int flags2);
01370 static char *complete_sip_registered_peer(const char *word, int state, int flags2);
01371 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state);
01372 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
01373 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state);
01374 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
01375 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01376 static char *sip_show_channelstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01377 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01378 static char *sip_do_debug_ip(int fd, const char *arg);
01379 static char *sip_do_debug_peer(int fd, const char *arg);
01380 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01381 static char *sip_cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01382 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01383 static int sip_dtmfmode(struct ast_channel *chan, const char *data);
01384 static int sip_addheader(struct ast_channel *chan, const char *data);
01385 static int sip_do_reload(enum channelreloadreason reason);
01386 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01387 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
01388 const char *name, int flag, int family);
01389 static int ast_sockaddr_resolve_first(struct ast_sockaddr *addr,
01390 const char *name, int flag);
01391
01392
01393
01394
01395
01396 static void sip_dump_history(struct sip_pvt *dialog);
01397 static inline int sip_debug_test_addr(const struct ast_sockaddr *addr);
01398 static inline int sip_debug_test_pvt(struct sip_pvt *p);
01399 static void append_history_full(struct sip_pvt *p, const char *fmt, ...);
01400 static void sip_dump_history(struct sip_pvt *dialog);
01401
01402
01403 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only);
01404 static int update_call_counter(struct sip_pvt *fup, int event);
01405 static void sip_destroy_peer(struct sip_peer *peer);
01406 static void sip_destroy_peer_fn(void *peer);
01407 static void set_peer_defaults(struct sip_peer *peer);
01408 static struct sip_peer *temp_peer(const char *name);
01409 static void register_peer_exten(struct sip_peer *peer, int onoff);
01410 static struct sip_peer *find_peer(const char *peer, struct ast_sockaddr *addr, int realtime, int forcenamematch, int devstate_only, int transport);
01411 static int sip_poke_peer_s(const void *data);
01412 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
01413 static void reg_source_db(struct sip_peer *peer);
01414 static void destroy_association(struct sip_peer *peer);
01415 static void set_insecure_flags(struct ast_flags *flags, const char *value, int lineno);
01416 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v);
01417 static void set_socket_transport(struct sip_socket *socket, int transport);
01418
01419
01420 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);
01421 static void update_peer(struct sip_peer *p, int expire);
01422 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *config);
01423 static const char *get_name_from_variable(struct ast_variable *var, const char *newpeername);
01424 static struct sip_peer *realtime_peer(const char *peername, struct ast_sockaddr *sin, int devstate_only, int which_objects);
01425 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
01426
01427
01428 static void ast_sip_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us, struct sip_pvt *p);
01429 static void sip_registry_destroy(struct sip_registry *reg);
01430 static int sip_register(const char *value, int lineno);
01431 static const char *regstate2str(enum sipregistrystate regstate) attribute_const;
01432 static int sip_reregister(const void *data);
01433 static int __sip_do_register(struct sip_registry *r);
01434 static int sip_reg_timeout(const void *data);
01435 static void sip_send_all_registers(void);
01436 static int sip_reinvite_retry(const void *data);
01437
01438
01439 static void append_date(struct sip_request *req);
01440 static int determine_firstline_parts(struct sip_request *req);
01441 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype);
01442 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize);
01443 static int find_sip_method(const char *msg);
01444 static unsigned int parse_allowed_methods(struct sip_request *req);
01445 static unsigned int set_pvt_allowed_methods(struct sip_pvt *pvt, struct sip_request *req);
01446 static int parse_request(struct sip_request *req);
01447 static const char *get_header(const struct sip_request *req, const char *name);
01448 static const char *referstatus2str(enum referstatus rstatus) attribute_pure;
01449 static int method_match(enum sipmethod id, const char *name);
01450 static void parse_copy(struct sip_request *dst, const struct sip_request *src);
01451 static const char *find_alias(const char *name, const char *_default);
01452 static const char *__get_header(const struct sip_request *req, const char *name, int *start);
01453 static int lws2sws(char *msgbuf, int len);
01454 static void extract_uri(struct sip_pvt *p, struct sip_request *req);
01455 static char *remove_uri_parameters(char *uri);
01456 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req);
01457 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq);
01458 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req);
01459 static int set_address_from_contact(struct sip_pvt *pvt);
01460 static void check_via(struct sip_pvt *p, struct sip_request *req);
01461 static int get_rpid(struct sip_pvt *p, struct sip_request *oreq);
01462 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq, char **name, char **number, int *reason);
01463 static enum sip_get_dest_result get_destination(struct sip_pvt *p, struct sip_request *oreq, int *cc_recall_core_id);
01464 static int get_msg_text(char *buf, int len, struct sip_request *req, int addnewline);
01465 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout);
01466 static void update_connectedline(struct sip_pvt *p, const void *data, size_t datalen);
01467 static void update_redirecting(struct sip_pvt *p, const void *data, size_t datalen);
01468 static int get_domain(const char *str, char *domain, int len);
01469 static void get_realm(struct sip_pvt *p, const struct sip_request *req);
01470
01471
01472 static void *_sip_tcp_helper_thread(struct sip_pvt *pvt, struct ast_tcptls_session_instance *tcptls_session);
01473 static void *sip_tcp_worker_fn(void *);
01474
01475
01476 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req);
01477 static int init_req(struct sip_request *req, int sipmethod, const char *recip);
01478 static void deinit_req(struct sip_request *req);
01479 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch);
01480 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, const char * const explicit_uri);
01481 static int init_resp(struct sip_request *resp, const char *msg);
01482 static inline int resp_needs_contact(const char *msg, enum sipmethod method);
01483 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req);
01484 static const struct ast_sockaddr *sip_real_dst(const struct sip_pvt *p);
01485 static void build_via(struct sip_pvt *p);
01486 static int create_addr_from_peer(struct sip_pvt *r, struct sip_peer *peer);
01487 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct ast_sockaddr *addr, int newdialog, struct ast_sockaddr *remote_address);
01488 static char *generate_random_string(char *buf, size_t size);
01489 static void build_callid_pvt(struct sip_pvt *pvt);
01490 static void build_callid_registry(struct sip_registry *reg, const struct ast_sockaddr *ourip, const char *fromdomain);
01491 static void make_our_tag(char *tagbuf, size_t len);
01492 static int add_header(struct sip_request *req, const char *var, const char *value);
01493 static int add_header_max_forwards(struct sip_pvt *dialog, struct sip_request *req);
01494 static int add_content(struct sip_request *req, const char *line);
01495 static int finalize_content(struct sip_request *req);
01496 static int add_text(struct sip_request *req, const char *text);
01497 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode);
01498 static int add_rpid(struct sip_request *req, struct sip_pvt *p);
01499 static int add_vidupdate(struct sip_request *req);
01500 static void add_route(struct sip_request *req, struct sip_route *route);
01501 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01502 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field);
01503 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field);
01504 static void set_destination(struct sip_pvt *p, char *uri);
01505 static void append_date(struct sip_request *req);
01506 static void build_contact(struct sip_pvt *p);
01507
01508
01509 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int *recount, int *nounlock);
01510 static int handle_request_update(struct sip_pvt *p, struct sip_request *req);
01511 static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock);
01512 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock);
01513 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req);
01514 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *sin, const char *e);
01515 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req);
01516 static int handle_request_message(struct sip_pvt *p, struct sip_request *req);
01517 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e);
01518 static void handle_request_info(struct sip_pvt *p, struct sip_request *req);
01519 static int handle_request_options(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e);
01520 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *nounlock);
01521 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e);
01522 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno, int *nounlock);
01523
01524
01525 static void handle_response_publish(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno);
01526 static void handle_response_invite(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno);
01527 static void handle_response_notify(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno);
01528 static void handle_response_refer(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno);
01529 static void handle_response_subscribe(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno);
01530 static int handle_response_register(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno);
01531 static void handle_response(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno);
01532
01533
01534 static int setup_srtp(struct sip_srtp **srtp);
01535 static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct sip_srtp **srtp, const char *a);
01536
01537
01538 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
01539 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan);
01540 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
01541 static void change_t38_state(struct sip_pvt *p, int state);
01542
01543
01544 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp);
01545 static int proc_session_timer(const void *vp);
01546 static void stop_session_timer(struct sip_pvt *p);
01547 static void start_session_timer(struct sip_pvt *p);
01548 static void restart_session_timer(struct sip_pvt *p);
01549 static const char *strefresher2str(enum st_refresher r);
01550 static int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher *const p_ref);
01551 static int parse_minse(const char *p_hdrval, int *const p_interval);
01552 static int st_get_se(struct sip_pvt *, int max);
01553 static enum st_refresher st_get_refresher(struct sip_pvt *);
01554 static enum st_mode st_get_mode(struct sip_pvt *);
01555 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p);
01556
01557
01558 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);
01559
01560
01561 static int sip_subscribe_mwi(const char *value, int lineno);
01562 static void sip_subscribe_mwi_destroy(struct sip_subscription_mwi *mwi);
01563 static void sip_send_all_mwi_subscriptions(void);
01564 static int sip_subscribe_mwi_do(const void *data);
01565 static int __sip_subscribe_mwi_do(struct sip_subscription_mwi *mwi);
01566
01567
01568 const struct ast_channel_tech sip_tech = {
01569 .type = "SIP",
01570 .description = "Session Initiation Protocol (SIP)",
01571 .capabilities = AST_FORMAT_AUDIO_MASK,
01572 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
01573 .requester = sip_request_call,
01574 .devicestate = sip_devicestate,
01575 .call = sip_call,
01576 .send_html = sip_sendhtml,
01577 .hangup = sip_hangup,
01578 .answer = sip_answer,
01579 .read = sip_read,
01580 .write = sip_write,
01581 .write_video = sip_write,
01582 .write_text = sip_write,
01583 .indicate = sip_indicate,
01584 .transfer = sip_transfer,
01585 .fixup = sip_fixup,
01586 .send_digit_begin = sip_senddigit_begin,
01587 .send_digit_end = sip_senddigit_end,
01588 .bridge = ast_rtp_instance_bridge,
01589 .early_bridge = ast_rtp_instance_early_bridge,
01590 .send_text = sip_sendtext,
01591 .func_channel_read = sip_acf_channel_read,
01592 .setoption = sip_setoption,
01593 .queryoption = sip_queryoption,
01594 .get_pvt_uniqueid = sip_get_callid,
01595 };
01596
01597
01598
01599
01600
01601
01602
01603 struct ast_channel_tech sip_tech_info;
01604
01605 static int sip_cc_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
01606 static int sip_cc_agent_start_offer_timer(struct ast_cc_agent *agent);
01607 static int sip_cc_agent_stop_offer_timer(struct ast_cc_agent *agent);
01608 static void sip_cc_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
01609 static int sip_cc_agent_status_request(struct ast_cc_agent *agent);
01610 static int sip_cc_agent_start_monitoring(struct ast_cc_agent *agent);
01611 static int sip_cc_agent_recall(struct ast_cc_agent *agent);
01612 static void sip_cc_agent_destructor(struct ast_cc_agent *agent);
01613
01614 static struct ast_cc_agent_callbacks sip_cc_agent_callbacks = {
01615 .type = "SIP",
01616 .init = sip_cc_agent_init,
01617 .start_offer_timer = sip_cc_agent_start_offer_timer,
01618 .stop_offer_timer = sip_cc_agent_stop_offer_timer,
01619 .respond = sip_cc_agent_respond,
01620 .status_request = sip_cc_agent_status_request,
01621 .start_monitoring = sip_cc_agent_start_monitoring,
01622 .callee_available = sip_cc_agent_recall,
01623 .destructor = sip_cc_agent_destructor,
01624 };
01625
01626 static int find_by_notify_uri_helper(void *obj, void *arg, int flags)
01627 {
01628 struct ast_cc_agent *agent = obj;
01629 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01630 const char *uri = arg;
01631
01632 return !sip_uri_cmp(agent_pvt->notify_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
01633 }
01634
01635 static struct ast_cc_agent *find_sip_cc_agent_by_notify_uri(const char * const uri)
01636 {
01637 struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_notify_uri_helper, (char *)uri, "SIP");
01638 return agent;
01639 }
01640
01641 static int find_by_subscribe_uri_helper(void *obj, void *arg, int flags)
01642 {
01643 struct ast_cc_agent *agent = obj;
01644 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01645 const char *uri = arg;
01646
01647 return !sip_uri_cmp(agent_pvt->subscribe_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
01648 }
01649
01650 static struct ast_cc_agent *find_sip_cc_agent_by_subscribe_uri(const char * const uri)
01651 {
01652 struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_subscribe_uri_helper, (char *)uri, "SIP");
01653 return agent;
01654 }
01655
01656 static int find_by_callid_helper(void *obj, void *arg, int flags)
01657 {
01658 struct ast_cc_agent *agent = obj;
01659 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01660 struct sip_pvt *call_pvt = arg;
01661
01662 return !strcmp(agent_pvt->original_callid, call_pvt->callid) ? CMP_MATCH | CMP_STOP : 0;
01663 }
01664
01665 static struct ast_cc_agent *find_sip_cc_agent_by_original_callid(struct sip_pvt *pvt)
01666 {
01667 struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_callid_helper, pvt, "SIP");
01668 return agent;
01669 }
01670
01671 static int sip_cc_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan)
01672 {
01673 struct sip_cc_agent_pvt *agent_pvt = ast_calloc(1, sizeof(*agent_pvt));
01674 struct sip_pvt *call_pvt = chan->tech_pvt;
01675
01676 if (!agent_pvt) {
01677 return -1;
01678 }
01679
01680 ast_assert(!strcmp(chan->tech->type, "SIP"));
01681
01682 ast_copy_string(agent_pvt->original_callid, call_pvt->callid, sizeof(agent_pvt->original_callid));
01683 ast_copy_string(agent_pvt->original_exten, call_pvt->exten, sizeof(agent_pvt->original_exten));
01684 agent_pvt->offer_timer_id = -1;
01685 agent->private_data = agent_pvt;
01686 sip_pvt_lock(call_pvt);
01687 ast_set_flag(&call_pvt->flags[0], SIP_OFFER_CC);
01688 sip_pvt_unlock(call_pvt);
01689 return 0;
01690 }
01691
01692 static int sip_offer_timer_expire(const void *data)
01693 {
01694 struct ast_cc_agent *agent = (struct ast_cc_agent *) data;
01695 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01696
01697 agent_pvt->offer_timer_id = -1;
01698
01699 return ast_cc_failed(agent->core_id, "SIP agent %s's offer timer expired", agent->device_name);
01700 }
01701
01702 static int sip_cc_agent_start_offer_timer(struct ast_cc_agent *agent)
01703 {
01704 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01705 int when;
01706
01707 when = ast_get_cc_offer_timer(agent->cc_params) * 1000;
01708 agent_pvt->offer_timer_id = ast_sched_add(sched, when, sip_offer_timer_expire, agent);
01709 return 0;
01710 }
01711
01712 static int sip_cc_agent_stop_offer_timer(struct ast_cc_agent *agent)
01713 {
01714 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01715
01716 AST_SCHED_DEL(sched, agent_pvt->offer_timer_id);
01717 return 0;
01718 }
01719
01720 static void sip_cc_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
01721 {
01722 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01723
01724 sip_pvt_lock(agent_pvt->subscribe_pvt);
01725 ast_set_flag(&agent_pvt->subscribe_pvt->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
01726 if (reason == AST_CC_AGENT_RESPONSE_SUCCESS || !ast_strlen_zero(agent_pvt->notify_uri)) {
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739 transmit_response(agent_pvt->subscribe_pvt, "200 OK", &agent_pvt->subscribe_pvt->initreq);
01740 transmit_cc_notify(agent, agent_pvt->subscribe_pvt, CC_QUEUED);
01741 } else {
01742 transmit_response(agent_pvt->subscribe_pvt, "500 Internal Error", &agent_pvt->subscribe_pvt->initreq);
01743 }
01744 sip_pvt_unlock(agent_pvt->subscribe_pvt);
01745 agent_pvt->is_available = TRUE;
01746 }
01747
01748 static int sip_cc_agent_status_request(struct ast_cc_agent *agent)
01749 {
01750 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01751 enum ast_device_state state = agent_pvt->is_available ? AST_DEVICE_NOT_INUSE : AST_DEVICE_INUSE;
01752 return ast_cc_agent_status_response(agent->core_id, state);
01753 }
01754
01755 static int sip_cc_agent_start_monitoring(struct ast_cc_agent *agent)
01756 {
01757
01758
01759
01760
01761 return 0;
01762 }
01763
01764 static int sip_cc_agent_recall(struct ast_cc_agent *agent)
01765 {
01766 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01767
01768
01769
01770
01771 if (!agent_pvt->is_available) {
01772 return ast_cc_agent_caller_busy(agent->core_id, "Caller %s is busy, reporting to the core",
01773 agent->device_name);
01774 }
01775
01776
01777
01778 sip_pvt_lock(agent_pvt->subscribe_pvt);
01779 transmit_cc_notify(agent, agent_pvt->subscribe_pvt, CC_READY);
01780 sip_pvt_unlock(agent_pvt->subscribe_pvt);
01781 return 0;
01782 }
01783
01784 static void sip_cc_agent_destructor(struct ast_cc_agent *agent)
01785 {
01786 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
01787
01788 if (!agent_pvt) {
01789
01790 return;
01791 }
01792
01793 sip_cc_agent_stop_offer_timer(agent);
01794 if (agent_pvt->subscribe_pvt) {
01795 sip_pvt_lock(agent_pvt->subscribe_pvt);
01796 if (!ast_test_flag(&agent_pvt->subscribe_pvt->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
01797
01798
01799
01800 transmit_response(agent_pvt->subscribe_pvt, "500 Internal Server Error", &agent_pvt->subscribe_pvt->initreq);
01801 }
01802 sip_pvt_unlock(agent_pvt->subscribe_pvt);
01803 agent_pvt->subscribe_pvt = dialog_unref(agent_pvt->subscribe_pvt, "SIP CC agent destructor: Remove ref to subscription");
01804 }
01805 ast_free(agent_pvt);
01806 }
01807
01808 struct ao2_container *sip_monitor_instances;
01809
01810 static int sip_monitor_instance_hash_fn(const void *obj, const int flags)
01811 {
01812 const struct sip_monitor_instance *monitor_instance = obj;
01813 return monitor_instance->core_id;
01814 }
01815
01816 static int sip_monitor_instance_cmp_fn(void *obj, void *arg, int flags)
01817 {
01818 struct sip_monitor_instance *monitor_instance1 = obj;
01819 struct sip_monitor_instance *monitor_instance2 = arg;
01820
01821 return monitor_instance1->core_id == monitor_instance2->core_id ? CMP_MATCH | CMP_STOP : 0;
01822 }
01823
01824 static void sip_monitor_instance_destructor(void *data)
01825 {
01826 struct sip_monitor_instance *monitor_instance = data;
01827 if (monitor_instance->subscription_pvt) {
01828 sip_pvt_lock(monitor_instance->subscription_pvt);
01829 monitor_instance->subscription_pvt->expiry = 0;
01830 transmit_invite(monitor_instance->subscription_pvt, SIP_SUBSCRIBE, FALSE, 0, monitor_instance->subscribe_uri);
01831 sip_pvt_unlock(monitor_instance->subscription_pvt);
01832 dialog_unref(monitor_instance->subscription_pvt, "Unref monitor instance ref of subscription pvt");
01833 }
01834 if (monitor_instance->suspension_entry) {
01835 monitor_instance->suspension_entry->body[0] = '\0';
01836 transmit_publish(monitor_instance->suspension_entry, SIP_PUBLISH_REMOVE ,monitor_instance->notify_uri);
01837 ao2_t_ref(monitor_instance->suspension_entry, -1, "Decrementing suspension entry refcount in sip_monitor_instance_destructor");
01838 }
01839 ast_string_field_free_memory(monitor_instance);
01840 }
01841
01842 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)
01843 {
01844 struct sip_monitor_instance *monitor_instance = ao2_alloc(sizeof(*monitor_instance), sip_monitor_instance_destructor);
01845
01846 if (!monitor_instance) {
01847 return NULL;
01848 }
01849
01850 if (ast_string_field_init(monitor_instance, 256)) {
01851 ao2_ref(monitor_instance, -1);
01852 return NULL;
01853 }
01854
01855 ast_string_field_set(monitor_instance, subscribe_uri, subscribe_uri);
01856 ast_string_field_set(monitor_instance, peername, peername);
01857 ast_string_field_set(monitor_instance, device_name, device_name);
01858 monitor_instance->core_id = core_id;
01859 ao2_link(sip_monitor_instances, monitor_instance);
01860 return monitor_instance;
01861 }
01862
01863 static int find_sip_monitor_instance_by_subscription_pvt(void *obj, void *arg, int flags)
01864 {
01865 struct sip_monitor_instance *monitor_instance = obj;
01866 return monitor_instance->subscription_pvt == arg ? CMP_MATCH | CMP_STOP : 0;
01867 }
01868
01869 static int find_sip_monitor_instance_by_suspension_entry(void *obj, void *arg, int flags)
01870 {
01871 struct sip_monitor_instance *monitor_instance = obj;
01872 return monitor_instance->suspension_entry == arg ? CMP_MATCH | CMP_STOP : 0;
01873 }
01874
01875 static int sip_cc_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
01876 static int sip_cc_monitor_suspend(struct ast_cc_monitor *monitor);
01877 static int sip_cc_monitor_unsuspend(struct ast_cc_monitor *monitor);
01878 static int sip_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
01879 static void sip_cc_monitor_destructor(void *private_data);
01880
01881 static struct ast_cc_monitor_callbacks sip_cc_monitor_callbacks = {
01882 .type = "SIP",
01883 .request_cc = sip_cc_monitor_request_cc,
01884 .suspend = sip_cc_monitor_suspend,
01885 .unsuspend = sip_cc_monitor_unsuspend,
01886 .cancel_available_timer = sip_cc_monitor_cancel_available_timer,
01887 .destructor = sip_cc_monitor_destructor,
01888 };
01889
01890 static int sip_cc_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
01891 {
01892 struct sip_monitor_instance *monitor_instance = monitor->private_data;
01893 enum ast_cc_service_type service = monitor->service_offered;
01894 int when;
01895
01896 if (!monitor_instance) {
01897 return -1;
01898 }
01899
01900 if (!(monitor_instance->subscription_pvt = sip_alloc(NULL, NULL, 0, SIP_SUBSCRIBE, NULL))) {
01901 return -1;
01902 }
01903
01904 when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) :
01905 ast_get_ccnr_available_timer(monitor->interface->config_params);
01906
01907 sip_pvt_lock(monitor_instance->subscription_pvt);
01908 create_addr(monitor_instance->subscription_pvt, monitor_instance->peername, 0, 1, NULL);
01909 ast_sip_ouraddrfor(&monitor_instance->subscription_pvt->sa, &monitor_instance->subscription_pvt->ourip, monitor_instance->subscription_pvt);
01910 monitor_instance->subscription_pvt->subscribed = CALL_COMPLETION;
01911 monitor_instance->subscription_pvt->expiry = when;
01912
01913 transmit_invite(monitor_instance->subscription_pvt, SIP_SUBSCRIBE, FALSE, 2, monitor_instance->subscribe_uri);
01914 sip_pvt_unlock(monitor_instance->subscription_pvt);
01915
01916 ao2_t_ref(monitor, +1, "Adding a ref to the monitor for the scheduler");
01917 *available_timer_id = ast_sched_add(sched, when * 1000, ast_cc_available_timer_expire, monitor);
01918 return 0;
01919 }
01920
01921 static int construct_pidf_body(enum sip_cc_publish_state state, char *pidf_body, size_t size, const char *presentity)
01922 {
01923 struct ast_str *body = ast_str_alloca(size);
01924 char tuple_id[32];
01925
01926 generate_random_string(tuple_id, sizeof(tuple_id));
01927
01928
01929
01930
01931 ast_str_append(&body, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
01932
01933
01934
01935
01936
01937
01938 ast_str_append(&body, 0, "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"%s\">\n", presentity);
01939 ast_str_append(&body, 0, "<tuple id=\"%s\">\n", tuple_id);
01940 ast_str_append(&body, 0, "<status><basic>%s</basic></status>\n", state == CC_OPEN ? "open" : "closed");
01941 ast_str_append(&body, 0, "</tuple>\n");
01942 ast_str_append(&body, 0, "</presence>\n");
01943 ast_copy_string(pidf_body, ast_str_buffer(body), size);
01944 return 0;
01945 }
01946
01947 static int sip_cc_monitor_suspend(struct ast_cc_monitor *monitor)
01948 {
01949 struct sip_monitor_instance *monitor_instance = monitor->private_data;
01950 enum sip_publish_type publish_type;
01951 struct cc_epa_entry *cc_entry;
01952
01953 if (!monitor_instance) {
01954 return -1;
01955 }
01956
01957 if (!monitor_instance->suspension_entry) {
01958
01959 if (!(monitor_instance->suspension_entry = create_epa_entry("call-completion", monitor_instance->peername))) {
01960 ast_log(LOG_WARNING, "Unable to allocate sip EPA entry for call-completion\n");
01961 ao2_ref(monitor_instance, -1);
01962 return -1;
01963 }
01964 if (!(cc_entry = ast_calloc(1, sizeof(*cc_entry)))) {
01965 ast_log(LOG_WARNING, "Unable to allocate space for instance data of EPA entry for call-completion\n");
01966 ao2_ref(monitor_instance, -1);
01967 return -1;
01968 }
01969 cc_entry->core_id = monitor->core_id;
01970 monitor_instance->suspension_entry->instance_data = cc_entry;
01971 publish_type = SIP_PUBLISH_INITIAL;
01972 } else {
01973 publish_type = SIP_PUBLISH_MODIFY;
01974 cc_entry = monitor_instance->suspension_entry->instance_data;
01975 }
01976
01977 cc_entry->current_state = CC_CLOSED;
01978
01979 if (ast_strlen_zero(monitor_instance->notify_uri)) {
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992 return 0;
01993 }
01994 construct_pidf_body(CC_CLOSED, monitor_instance->suspension_entry->body, sizeof(monitor_instance->suspension_entry->body), monitor_instance->peername);
01995 return transmit_publish(monitor_instance->suspension_entry, publish_type, monitor_instance->notify_uri);
01996 }
01997
01998 static int sip_cc_monitor_unsuspend(struct ast_cc_monitor *monitor)
01999 {
02000 struct sip_monitor_instance *monitor_instance = monitor->private_data;
02001 struct cc_epa_entry *cc_entry;
02002
02003 if (!monitor_instance) {
02004 return -1;
02005 }
02006
02007 ast_assert(monitor_instance->suspension_entry != NULL);
02008
02009 cc_entry = monitor_instance->suspension_entry->instance_data;
02010 cc_entry->current_state = CC_OPEN;
02011 if (ast_strlen_zero(monitor_instance->notify_uri)) {
02012
02013
02014
02015
02016 return 0;
02017 }
02018 construct_pidf_body(CC_OPEN, monitor_instance->suspension_entry->body, sizeof(monitor_instance->suspension_entry->body), monitor_instance->peername);
02019 return transmit_publish(monitor_instance->suspension_entry, SIP_PUBLISH_MODIFY, monitor_instance->notify_uri);
02020 }
02021
02022 static int sip_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
02023 {
02024 if (*sched_id != -1) {
02025 AST_SCHED_DEL(sched, *sched_id);
02026 ao2_t_ref(monitor, -1, "Removing scheduler's reference to the monitor");
02027 }
02028 return 0;
02029 }
02030
02031 static void sip_cc_monitor_destructor(void *private_data)
02032 {
02033 struct sip_monitor_instance *monitor_instance = private_data;
02034 ao2_unlink(sip_monitor_instances, monitor_instance);
02035 ast_module_unref(ast_module_info->self);
02036 }
02037
02038 static int sip_get_cc_information(struct sip_request *req, char *subscribe_uri, size_t size, enum ast_cc_service_type *service)
02039 {
02040 char *call_info = ast_strdupa(get_header(req, "Call-Info"));
02041 char *uri;
02042 char *purpose;
02043 char *service_str;
02044 static const char cc_purpose[] = "purpose=call-completion";
02045 static const int cc_purpose_len = sizeof(cc_purpose) - 1;
02046
02047 if (ast_strlen_zero(call_info)) {
02048
02049 return -1;
02050 }
02051
02052 uri = strsep(&call_info, ";");
02053
02054 while ((purpose = strsep(&call_info, ";"))) {
02055 if (!strncmp(purpose, cc_purpose, cc_purpose_len)) {
02056 break;
02057 }
02058 }
02059 if (!purpose) {
02060
02061 return -1;
02062 }
02063
02064
02065 while ((service_str = strsep(&call_info, ";"))) {
02066 if (!strncmp(service_str, "m=", 2)) {
02067 break;
02068 }
02069 }
02070 if (!service_str) {
02071
02072
02073
02074 service_str = "BS";
02075 } else {
02076
02077
02078
02079 strsep(&service_str, "=");
02080 }
02081
02082 if ((*service = service_string_to_service_type(service_str)) == AST_CC_NONE) {
02083
02084 return -1;
02085 }
02086
02087 ast_copy_string(subscribe_uri, get_in_brackets(uri), size);
02088
02089 return 0;
02090 }
02091
02092
02093
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103
02104
02105
02106
02107
02108
02109 static void sip_handle_cc(struct sip_pvt *pvt, struct sip_request *req, enum ast_cc_service_type service)
02110 {
02111 enum ast_cc_monitor_policies monitor_policy = ast_get_cc_monitor_policy(pvt->cc_params);
02112 int core_id;
02113 char interface_name[AST_CHANNEL_NAME];
02114
02115 if (monitor_policy == AST_CC_MONITOR_NEVER) {
02116
02117 return;
02118 }
02119
02120 if ((core_id = ast_cc_get_current_core_id(pvt->owner)) == -1) {
02121
02122 return;
02123 }
02124
02125 ast_channel_get_device_name(pvt->owner, interface_name, sizeof(interface_name));
02126
02127 if (monitor_policy == AST_CC_MONITOR_ALWAYS || monitor_policy == AST_CC_MONITOR_NATIVE) {
02128 char subscribe_uri[SIPBUFSIZE];
02129 char device_name[AST_CHANNEL_NAME];
02130 enum ast_cc_service_type offered_service;
02131 struct sip_monitor_instance *monitor_instance;
02132 if (sip_get_cc_information(req, subscribe_uri, sizeof(subscribe_uri), &offered_service)) {
02133
02134
02135
02136
02137 goto generic;
02138 }
02139 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
02140 if (!(monitor_instance = sip_monitor_instance_init(core_id, subscribe_uri, pvt->peername, device_name))) {
02141
02142 goto generic;
02143 }
02144
02145
02146
02147
02148 ast_module_ref(ast_module_info->self);
02149 ast_queue_cc_frame(pvt->owner, "SIP", pvt->dialstring, offered_service, monitor_instance);
02150 ao2_ref(monitor_instance, -1);
02151 return;
02152 }
02153
02154 generic:
02155 if (monitor_policy == AST_CC_MONITOR_GENERIC || monitor_policy == AST_CC_MONITOR_ALWAYS) {
02156 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE, interface_name, service, NULL);
02157 }
02158 }
02159
02160
02161 static struct ast_tls_config sip_tls_cfg;
02162
02163
02164 static struct ast_tls_config default_tls_cfg;
02165
02166
02167 static struct ast_tcptls_session_args sip_tcp_desc = {
02168 .accept_fd = -1,
02169 .master = AST_PTHREADT_NULL,
02170 .tls_cfg = NULL,
02171 .poll_timeout = -1,
02172 .name = "SIP TCP server",
02173 .accept_fn = ast_tcptls_server_root,
02174 .worker_fn = sip_tcp_worker_fn,
02175 };
02176
02177
02178 static struct ast_tcptls_session_args sip_tls_desc = {
02179 .accept_fd = -1,
02180 .master = AST_PTHREADT_NULL,
02181 .tls_cfg = &sip_tls_cfg,
02182 .poll_timeout = -1,
02183 .name = "SIP TLS server",
02184 .accept_fn = ast_tcptls_server_root,
02185 .worker_fn = sip_tcp_worker_fn,
02186 };
02187
02188
02189
02190 #define append_history(p, event, fmt , args... ) append_history_full(p, "%-15s " fmt, event, ## args)
02191
02192 struct sip_pvt *dialog_ref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
02193 {
02194 if (p)
02195 #ifdef REF_DEBUG
02196 __ao2_ref_debug(p, 1, tag, file, line, func);
02197 #else
02198 ao2_ref(p, 1);
02199 #endif
02200 else
02201 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
02202 return p;
02203 }
02204
02205 struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, char *tag, char *file, int line, const char *func)
02206 {
02207 if (p)
02208 #ifdef REF_DEBUG
02209 __ao2_ref_debug(p, -1, tag, file, line, func);
02210 #else
02211 ao2_ref(p, -1);
02212 #endif
02213 return NULL;
02214 }
02215
02216
02217
02218
02219 static const char *map_x_s(const struct _map_x_s *table, int x, const char *errorstring)
02220 {
02221 const struct _map_x_s *cur;
02222
02223 for (cur = table; cur->s; cur++)
02224 if (cur->x == x)
02225 return cur->s;
02226 return errorstring;
02227 }
02228
02229
02230
02231
02232 static int map_s_x(const struct _map_x_s *table, const char *s, int errorvalue)
02233 {
02234 const struct _map_x_s *cur;
02235
02236 for (cur = table; cur->s; cur++)
02237 if (!strcasecmp(cur->s, s))
02238 return cur->x;
02239 return errorvalue;
02240 }
02241
02242 static enum AST_REDIRECTING_REASON sip_reason_str_to_code(const char *text)
02243 {
02244 enum AST_REDIRECTING_REASON ast = AST_REDIRECTING_REASON_UNKNOWN;
02245 int i;
02246
02247 for (i = 0; i < ARRAY_LEN(sip_reason_table); ++i) {
02248 if (!strcasecmp(text, sip_reason_table[i].text)) {
02249 ast = sip_reason_table[i].code;
02250 break;
02251 }
02252 }
02253
02254 return ast;
02255 }
02256
02257 static const char *sip_reason_code_to_str(enum AST_REDIRECTING_REASON code)
02258 {
02259 if (code >= 0 && code < ARRAY_LEN(sip_reason_table)) {
02260 return sip_reason_table[code].text;
02261 }
02262
02263 return "unknown";
02264 }
02265
02266
02267
02268
02269
02270
02271
02272
02273 #define check_request_transport(peer, tmpl) ({ \
02274 int ret = 0; \
02275 if (peer->socket.type == tmpl->socket.type) \
02276 ; \
02277 else if (!(peer->transports & tmpl->socket.type)) {\
02278 ast_log(LOG_ERROR, \
02279 "'%s' is not a valid transport for '%s'. we only use '%s'! ending call.\n", \
02280 get_transport(tmpl->socket.type), peer->name, get_transport_list(peer->transports) \
02281 ); \
02282 ret = 1; \
02283 } else if (peer->socket.type & SIP_TRANSPORT_TLS) { \
02284 ast_log(LOG_WARNING, \
02285 "peer '%s' HAS NOT USED (OR SWITCHED TO) TLS in favor of '%s' (but this was allowed in sip.conf)!\n", \
02286 peer->name, get_transport(tmpl->socket.type) \
02287 ); \
02288 } else { \
02289 ast_debug(1, \
02290 "peer '%s' has contacted us over %s even though we prefer %s.\n", \
02291 peer->name, get_transport(tmpl->socket.type), get_transport(peer->socket.type) \
02292 ); \
02293 }\
02294 (ret); \
02295 })
02296
02297
02298
02299
02300 static struct ast_variable *copy_vars(struct ast_variable *src)
02301 {
02302 struct ast_variable *res = NULL, *tmp, *v = NULL;
02303
02304 for (v = src ; v ; v = v->next) {
02305 if ((tmp = ast_variable_new(v->name, v->value, v->file))) {
02306 tmp->next = res;
02307 res = tmp;
02308 }
02309 }
02310 return res;
02311 }
02312
02313 static void tcptls_packet_destructor(void *obj)
02314 {
02315 struct tcptls_packet *packet = obj;
02316
02317 ast_free(packet->data);
02318 }
02319
02320 static void sip_tcptls_client_args_destructor(void *obj)
02321 {
02322 struct ast_tcptls_session_args *args = obj;
02323 if (args->tls_cfg) {
02324 ast_free(args->tls_cfg->certfile);
02325 ast_free(args->tls_cfg->pvtfile);
02326 ast_free(args->tls_cfg->cipher);
02327 ast_free(args->tls_cfg->cafile);
02328 ast_free(args->tls_cfg->capath);
02329 }
02330 ast_free(args->tls_cfg);
02331 ast_free((char *) args->name);
02332 }
02333
02334 static void sip_threadinfo_destructor(void *obj)
02335 {
02336 struct sip_threadinfo *th = obj;
02337 struct tcptls_packet *packet;
02338 if (th->alert_pipe[1] > -1) {
02339 close(th->alert_pipe[0]);
02340 }
02341 if (th->alert_pipe[1] > -1) {
02342 close(th->alert_pipe[1]);
02343 }
02344 th->alert_pipe[0] = th->alert_pipe[1] = -1;
02345
02346 while ((packet = AST_LIST_REMOVE_HEAD(&th->packet_q, entry))) {
02347 ao2_t_ref(packet, -1, "thread destruction, removing packet from frame queue");
02348 }
02349
02350 if (th->tcptls_session) {
02351 ao2_t_ref(th->tcptls_session, -1, "remove tcptls_session for sip_threadinfo object");
02352 }
02353 }
02354
02355
02356 static struct sip_threadinfo *sip_threadinfo_create(struct ast_tcptls_session_instance *tcptls_session, int transport)
02357 {
02358 struct sip_threadinfo *th;
02359
02360 if (!tcptls_session || !(th = ao2_alloc(sizeof(*th), sip_threadinfo_destructor))) {
02361 return NULL;
02362 }
02363
02364 th->alert_pipe[0] = th->alert_pipe[1] = -1;
02365
02366 if (pipe(th->alert_pipe) == -1) {
02367 ao2_t_ref(th, -1, "Failed to open alert pipe on sip_threadinfo");
02368 ast_log(LOG_ERROR, "Could not create sip alert pipe in tcptls thread, error %s\n", strerror(errno));
02369 return NULL;
02370 }
02371 ao2_t_ref(tcptls_session, +1, "tcptls_session ref for sip_threadinfo object");
02372 th->tcptls_session = tcptls_session;
02373 th->type = transport ? transport : (tcptls_session->ssl ? SIP_TRANSPORT_TLS: SIP_TRANSPORT_TCP);
02374 ao2_t_link(threadt, th, "Adding new tcptls helper thread");
02375 ao2_t_ref(th, -1, "Decrementing threadinfo ref from alloc, only table ref remains");
02376 return th;
02377 }
02378
02379
02380 static int sip_tcptls_write(struct ast_tcptls_session_instance *tcptls_session, const void *buf, size_t len)
02381 {
02382 int res = len;
02383 struct sip_threadinfo *th = NULL;
02384 struct tcptls_packet *packet = NULL;
02385 struct sip_threadinfo tmp = {
02386 .tcptls_session = tcptls_session,
02387 };
02388 enum sip_tcptls_alert alert = TCPTLS_ALERT_DATA;
02389
02390 if (!tcptls_session) {
02391 return XMIT_ERROR;
02392 }
02393
02394 ast_mutex_lock(&tcptls_session->lock);
02395
02396 if ((tcptls_session->fd == -1) ||
02397 !(th = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread")) ||
02398 !(packet = ao2_alloc(sizeof(*packet), tcptls_packet_destructor)) ||
02399 !(packet->data = ast_str_create(len))) {
02400 goto tcptls_write_setup_error;
02401 }
02402
02403
02404 ast_str_set(&packet->data, 0, "%s", (char *) buf);
02405 packet->len = len;
02406
02407
02408
02409
02410 ao2_lock(th);
02411 if (write(th->alert_pipe[1], &alert, sizeof(alert)) == -1) {
02412 ast_log(LOG_ERROR, "write() to alert pipe failed: %s\n", strerror(errno));
02413 ao2_t_ref(packet, -1, "could not write to alert pipe, remove packet");
02414 packet = NULL;
02415 res = XMIT_ERROR;
02416 } else {
02417 AST_LIST_INSERT_TAIL(&th->packet_q, packet, entry);
02418 }
02419 ao2_unlock(th);
02420
02421 ast_mutex_unlock(&tcptls_session->lock);
02422 ao2_t_ref(th, -1, "In sip_tcptls_write, unref threadinfo object after finding it");
02423 return res;
02424
02425 tcptls_write_setup_error:
02426 if (th) {
02427 ao2_t_ref(th, -1, "In sip_tcptls_write, unref threadinfo obj, could not create packet");
02428 }
02429 if (packet) {
02430 ao2_t_ref(packet, -1, "could not allocate packet's data");
02431 }
02432 ast_mutex_unlock(&tcptls_session->lock);
02433
02434 return XMIT_ERROR;
02435 }
02436
02437
02438 static void *sip_tcp_worker_fn(void *data)
02439 {
02440 struct ast_tcptls_session_instance *tcptls_session = data;
02441
02442 return _sip_tcp_helper_thread(NULL, tcptls_session);
02443 }
02444
02445
02446
02447
02448
02449
02450
02451
02452 static int sip_check_authtimeout(time_t start)
02453 {
02454 int timeout;
02455 time_t now;
02456 if(time(&now) == -1) {
02457 ast_log(LOG_ERROR, "error executing time(): %s\n", strerror(errno));
02458 return -1;
02459 }
02460
02461 timeout = (authtimeout - (now - start)) * 1000;
02462 if (timeout < 0) {
02463
02464 return 0;
02465 }
02466
02467 return timeout;
02468 }
02469
02470
02471
02472
02473 static void *_sip_tcp_helper_thread(struct sip_pvt *pvt, struct ast_tcptls_session_instance *tcptls_session)
02474 {
02475 int res, cl, timeout = -1, authenticated = 0, flags, after_poll = 0, need_poll = 1;
02476 time_t start;
02477 struct sip_request req = { 0, } , reqcpy = { 0, };
02478 struct sip_threadinfo *me = NULL;
02479 char buf[1024] = "";
02480 struct pollfd fds[2] = { { 0 }, { 0 }, };
02481 struct ast_tcptls_session_args *ca = NULL;
02482
02483
02484
02485
02486
02487
02488
02489
02490
02491
02492
02493
02494 if (!tcptls_session->client) {
02495 if (ast_atomic_fetchadd_int(&unauth_sessions, +1) >= authlimit) {
02496
02497 goto cleanup;
02498 }
02499
02500 if ((flags = fcntl(tcptls_session->fd, F_GETFL)) == -1) {
02501 ast_log(LOG_ERROR, "error setting socket to non blocking mode, fcntl() failed: %s\n", strerror(errno));
02502 goto cleanup;
02503 }
02504
02505 flags |= O_NONBLOCK;
02506 if (fcntl(tcptls_session->fd, F_SETFL, flags) == -1) {
02507 ast_log(LOG_ERROR, "error setting socket to non blocking mode, fcntl() failed: %s\n", strerror(errno));
02508 goto cleanup;
02509 }
02510
02511 if (!(me = sip_threadinfo_create(tcptls_session, tcptls_session->ssl ? SIP_TRANSPORT_TLS : SIP_TRANSPORT_TCP))) {
02512 goto cleanup;
02513 }
02514 ao2_t_ref(me, +1, "Adding threadinfo ref for tcp_helper_thread");
02515 } else {
02516 struct sip_threadinfo tmp = {
02517 .tcptls_session = tcptls_session,
02518 };
02519
02520 if ((!(ca = tcptls_session->parent)) ||
02521 (!(me = ao2_t_find(threadt, &tmp, OBJ_POINTER, "ao2_find, getting sip_threadinfo in tcp helper thread"))) ||
02522 (!(tcptls_session = ast_tcptls_client_start(tcptls_session)))) {
02523 goto cleanup;
02524 }
02525 }
02526
02527 me->threadid = pthread_self();
02528 ast_debug(2, "Starting thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
02529
02530
02531 fds[0].fd = tcptls_session->fd;
02532 fds[1].fd = me->alert_pipe[0];
02533 fds[0].events = fds[1].events = POLLIN | POLLPRI;
02534
02535 if (!(req.data = ast_str_create(SIP_MIN_PACKET))) {
02536 goto cleanup;
02537 }
02538 if (!(reqcpy.data = ast_str_create(SIP_MIN_PACKET))) {
02539 goto cleanup;
02540 }
02541
02542 if(time(&start) == -1) {
02543 ast_log(LOG_ERROR, "error executing time(): %s\n", strerror(errno));
02544 goto cleanup;
02545 }
02546
02547 for (;;) {
02548 struct ast_str *str_save;
02549
02550 if (!tcptls_session->client && req.authenticated && !authenticated) {
02551 authenticated = 1;
02552 ast_atomic_fetchadd_int(&unauth_sessions, -1);
02553 }
02554
02555
02556 if (!tcptls_session->client && !authenticated ) {
02557 if ((timeout = sip_check_authtimeout(start)) < 0) {
02558 goto cleanup;
02559 }
02560
02561 if (timeout == 0) {
02562 ast_debug(2, "SIP %s server timed out\n", tcptls_session->ssl ? "SSL": "TCP");
02563 goto cleanup;
02564 }
02565 } else {
02566 timeout = -1;
02567 }
02568
02569 res = ast_poll(fds, 2, timeout);
02570 if (res < 0) {
02571 ast_debug(2, "SIP %s server :: ast_wait_for_input returned %d\n", tcptls_session->ssl ? "SSL": "TCP", res);
02572 goto cleanup;
02573 } else if (res == 0) {
02574
02575 ast_debug(2, "SIP %s server timed out\n", tcptls_session->ssl ? "SSL": "TCP");
02576 goto cleanup;
02577 }
02578
02579
02580
02581 if (fds[0].revents) {
02582 after_poll = 1;
02583
02584 fds[0].revents = 0;
02585
02586
02587 str_save = req.data;
02588 memset(&req, 0, sizeof(req));
02589 req.data = str_save;
02590 ast_str_reset(req.data);
02591
02592 str_save = reqcpy.data;
02593 memset(&reqcpy, 0, sizeof(reqcpy));
02594 reqcpy.data = str_save;
02595 ast_str_reset(reqcpy.data);
02596
02597 memset(buf, 0, sizeof(buf));
02598
02599 if (tcptls_session->ssl) {
02600 set_socket_transport(&req.socket, SIP_TRANSPORT_TLS);
02601 req.socket.port = htons(ourport_tls);
02602 } else {
02603 set_socket_transport(&req.socket, SIP_TRANSPORT_TCP);
02604 req.socket.port = htons(ourport_tcp);
02605 }
02606 req.socket.fd = tcptls_session->fd;
02607
02608
02609 while (req.len < 4 || strncmp(REQ_OFFSET_TO_STR(&req, len - 4), "\r\n\r\n", 4)) {
02610 if (!tcptls_session->client && !authenticated ) {
02611 if ((timeout = sip_check_authtimeout(start)) < 0) {
02612 goto cleanup;
02613 }
02614
02615 if (timeout == 0) {
02616 ast_debug(2, "SIP %s server timed out\n", tcptls_session->ssl ? "SSL": "TCP");
02617 goto cleanup;
02618 }
02619 } else {
02620 timeout = -1;
02621 }
02622
02623
02624
02625
02626 if (!tcptls_session->ssl || need_poll) {
02627 need_poll = 0;
02628 after_poll = 1;
02629 res = ast_wait_for_input(tcptls_session->fd, timeout);
02630 if (res < 0) {
02631 ast_debug(2, "SIP TCP server :: ast_wait_for_input returned %d\n", res);
02632 goto cleanup;
02633 } else if (res == 0) {
02634
02635 ast_debug(2, "SIP TCP server timed out\n");
02636 goto cleanup;
02637 }
02638 }
02639
02640 ast_mutex_lock(&tcptls_session->lock);
02641 if (!fgets(buf, sizeof(buf), tcptls_session->f)) {
02642 ast_mutex_unlock(&tcptls_session->lock);
02643 if (after_poll) {
02644 goto cleanup;
02645 } else {
02646 need_poll = 1;
02647 continue;
02648 }
02649 }
02650 ast_mutex_unlock(&tcptls_session->lock);
02651 after_poll = 0;
02652 if (me->stop) {
02653 goto cleanup;
02654 }
02655 ast_str_append(&req.data, 0, "%s", buf);
02656 req.len = req.data->used;
02657 }
02658 copy_request(&reqcpy, &req);
02659 parse_request(&reqcpy);
02660
02661 if (sscanf(get_header(&reqcpy, "Content-Length"), "%30d", &cl)) {
02662 while (cl > 0) {
02663 size_t bytes_read;
02664 if (!tcptls_session->client && !authenticated ) {
02665 if ((timeout = sip_check_authtimeout(start)) < 0) {
02666 goto cleanup;
02667 }
02668
02669 if (timeout == 0) {
02670 ast_debug(2, "SIP %s server timed out\n", tcptls_session->ssl ? "SSL": "TCP");
02671 goto cleanup;
02672 }
02673 } else {
02674 timeout = -1;
02675 }
02676
02677 if (!tcptls_session->ssl || need_poll) {
02678 need_poll = 0;
02679 after_poll = 1;
02680 res = ast_wait_for_input(tcptls_session->fd, timeout);
02681 if (res < 0) {
02682 ast_debug(2, "SIP TCP server :: ast_wait_for_input returned %d\n", res);
02683 goto cleanup;
02684 } else if (res == 0) {
02685
02686 ast_debug(2, "SIP TCP server timed out\n");
02687 goto cleanup;
02688 }
02689 }
02690
02691 ast_mutex_lock(&tcptls_session->lock);
02692 if (!(bytes_read = fread(buf, 1, MIN(sizeof(buf) - 1, cl), tcptls_session->f))) {
02693 ast_mutex_unlock(&tcptls_session->lock);
02694 if (after_poll) {
02695 goto cleanup;
02696 } else {
02697 need_poll = 1;
02698 continue;
02699 }
02700 }
02701 buf[bytes_read] = '\0';
02702 ast_mutex_unlock(&tcptls_session->lock);
02703 after_poll = 0;
02704 if (me->stop) {
02705 goto cleanup;
02706 }
02707 cl -= strlen(buf);
02708 ast_str_append(&req.data, 0, "%s", buf);
02709 req.len = req.data->used;
02710 }
02711 }
02712
02713
02714
02715 req.socket.tcptls_session = tcptls_session;
02716 handle_request_do(&req, &tcptls_session->remote_address);
02717 }
02718
02719 if (fds[1].revents) {
02720 enum sip_tcptls_alert alert;
02721 struct tcptls_packet *packet;
02722
02723 fds[1].revents = 0;
02724
02725 if (read(me->alert_pipe[0], &alert, sizeof(alert)) == -1) {
02726 ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
02727 continue;
02728 }
02729
02730 switch (alert) {
02731 case TCPTLS_ALERT_STOP:
02732 goto cleanup;
02733 case TCPTLS_ALERT_DATA:
02734 ao2_lock(me);
02735 if (!(packet = AST_LIST_REMOVE_HEAD(&me->packet_q, entry))) {
02736 ast_log(LOG_WARNING, "TCPTLS thread alert_pipe indicated packet should be sent, but frame_q is empty");
02737 } else if (ast_tcptls_server_write(tcptls_session, ast_str_buffer(packet->data), packet->len) == -1) {
02738 ast_log(LOG_WARNING, "Failure to write to tcp/tls socket\n");
02739 }
02740
02741 if (packet) {
02742 ao2_t_ref(packet, -1, "tcptls packet sent, this is no longer needed");
02743 }
02744 ao2_unlock(me);
02745 break;
02746 default:
02747 ast_log(LOG_ERROR, "Unknown tcptls thread alert '%d'\n", alert);
02748 }
02749 }
02750 }
02751
02752 ast_debug(2, "Shutting down thread for %s server\n", tcptls_session->ssl ? "SSL" : "TCP");
02753
02754 cleanup:
02755 if (tcptls_session && !tcptls_session->client && !authenticated) {
02756 ast_atomic_fetchadd_int(&unauth_sessions, -1);
02757 }
02758
02759 if (me) {
02760 ao2_t_unlink(threadt, me, "Removing tcptls helper thread, thread is closing");
02761 ao2_t_ref(me, -1, "Removing tcp_helper_threads threadinfo ref");
02762 }
02763 deinit_req(&reqcpy);
02764 deinit_req(&req);
02765
02766
02767 if (ca) {
02768 ao2_t_ref(ca, -1, "closing tcptls thread, getting rid of client tcptls_session arguments");
02769 }
02770
02771 if (tcptls_session) {
02772 ast_mutex_lock(&tcptls_session->lock);
02773 if (tcptls_session->f) {
02774 fclose(tcptls_session->f);
02775 tcptls_session->f = NULL;
02776 }
02777 if (tcptls_session->fd != -1) {
02778 close(tcptls_session->fd);
02779 tcptls_session->fd = -1;
02780 }
02781 tcptls_session->parent = NULL;
02782 ast_mutex_unlock(&tcptls_session->lock);
02783
02784 ao2_ref(tcptls_session, -1);
02785 tcptls_session = NULL;
02786 }
02787 return NULL;
02788 }
02789
02790
02791
02792 static int peer_is_marked(void *peerobj, void *arg, int flags)
02793 {
02794 struct sip_peer *peer = peerobj;
02795 if (peer->the_mark && peer->pokeexpire != -1) {
02796 AST_SCHED_DEL(sched, peer->pokeexpire);
02797 }
02798 return peer->the_mark ? CMP_MATCH : 0;
02799 }
02800
02801
02802
02803 static void unlink_marked_peers_from_tables(void)
02804 {
02805 ao2_t_callback(peers, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, peer_is_marked, NULL,
02806 "initiating callback to remove marked peers");
02807 ao2_t_callback(peers_by_ip, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, peer_is_marked, NULL,
02808 "initiating callback to remove marked peers");
02809 }
02810
02811
02812 static void unlink_peer_from_tables(struct sip_peer *peer)
02813 {
02814 ao2_t_unlink(peers, peer, "ao2_unlink of peer from peers table");
02815 if (!ast_sockaddr_isnull(&peer->addr)) {
02816 ao2_t_unlink(peers_by_ip, peer, "ao2_unlink of peer from peers_by_ip table");
02817 }
02818 }
02819
02820
02821
02822
02823
02824
02825 static void *unref_peer(struct sip_peer *peer, char *tag)
02826 {
02827 ao2_t_ref(peer, -1, tag);
02828 return NULL;
02829 }
02830
02831 static struct sip_peer *ref_peer(struct sip_peer *peer, char *tag)
02832 {
02833 ao2_t_ref(peer, 1, tag);
02834 return peer;
02835 }
02836
02837
02838
02839
02840
02841
02842
02843
02844
02845
02846
02847
02848 static void ref_proxy(struct sip_pvt *pvt, struct sip_proxy *proxy)
02849 {
02850 struct sip_proxy *old_obproxy = pvt->outboundproxy;
02851
02852
02853
02854 if (proxy && proxy != &sip_cfg.outboundproxy) {
02855 ao2_ref(proxy, +1);
02856 }
02857 pvt->outboundproxy = proxy;
02858 if (old_obproxy && old_obproxy != &sip_cfg.outboundproxy) {
02859 ao2_ref(old_obproxy, -1);
02860 }
02861 }
02862
02863
02864
02865
02866
02867
02868
02869
02870 void *dialog_unlink_all(struct sip_pvt *dialog, int lockowner, int lockdialoglist)
02871 {
02872 struct sip_pkt *cp;
02873
02874 dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
02875
02876 ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
02877
02878
02879 if (dialog->owner) {
02880 if (lockowner)
02881 ast_channel_lock(dialog->owner);
02882 ast_debug(1, "Detaching from channel %s\n", dialog->owner->name);
02883 dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
02884 if (lockowner) {
02885 ast_channel_unlock(dialog->owner);
02886 }
02887 }
02888 if (dialog->registry) {
02889 if (dialog->registry->call == dialog) {
02890 dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
02891 }
02892 dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
02893 }
02894 if (dialog->stateid > -1) {
02895 ast_extension_state_del(dialog->stateid, NULL);
02896 dialog_unref(dialog, "removing extension_state, should unref the associated dialog ptr that was stored there.");
02897 dialog->stateid = -1;
02898 }
02899
02900 if (dialog->relatedpeer && dialog->relatedpeer->mwipvt == dialog) {
02901 dialog->relatedpeer->mwipvt = dialog_unref(dialog->relatedpeer->mwipvt, "delete ->relatedpeer->mwipvt");
02902 }
02903 if (dialog->relatedpeer && dialog->relatedpeer->call == dialog) {
02904 dialog->relatedpeer->call = dialog_unref(dialog->relatedpeer->call, "unset the relatedpeer->call field in tandem with relatedpeer field itself");
02905 }
02906
02907
02908 while((cp = dialog->packets)) {
02909 dialog->packets = dialog->packets->next;
02910 AST_SCHED_DEL(sched, cp->retransid);
02911 dialog_unref(cp->owner, "remove all current packets in this dialog, and the pointer to the dialog too as part of __sip_destroy");
02912 if (cp->data) {
02913 ast_free(cp->data);
02914 }
02915 ast_free(cp);
02916 }
02917
02918 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"));
02919
02920 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"));
02921
02922 if (dialog->autokillid > -1) {
02923 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"));
02924 }
02925
02926 if (dialog->request_queue_sched_id > -1) {
02927 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"));
02928 }
02929
02930 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"));
02931
02932 if (dialog->t38id > -1) {
02933 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"));
02934 }
02935
02936 dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
02937 return NULL;
02938 }
02939
02940 void *registry_unref(struct sip_registry *reg, char *tag)
02941 {
02942 ast_debug(3, "SIP Registry %s: refcount now %d\n", reg->hostname, reg->refcount - 1);
02943 ASTOBJ_UNREF(reg, sip_registry_destroy);
02944 return NULL;
02945 }
02946
02947
02948 static struct sip_registry *registry_addref(struct sip_registry *reg, char *tag)
02949 {
02950 ast_debug(3, "SIP Registry %s: refcount now %d\n", reg->hostname, reg->refcount + 1);
02951 return ASTOBJ_REF(reg);
02952 }
02953
02954
02955 static struct ast_udptl_protocol sip_udptl = {
02956 type: "SIP",
02957 get_udptl_info: sip_get_udptl_peer,
02958 set_udptl_peer: sip_set_udptl_peer,
02959 };
02960
02961 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
02962 __attribute__((format(printf, 2, 3)));
02963
02964
02965
02966 static const char *referstatus2str(enum referstatus rstatus)
02967 {
02968 return map_x_s(referstatusstrings, rstatus, "");
02969 }
02970
02971 static inline void pvt_set_needdestroy(struct sip_pvt *pvt, const char *reason)
02972 {
02973 if (pvt->final_destruction_scheduled) {
02974 return;
02975 }
02976 append_history(pvt, "NeedDestroy", "Setting needdestroy because %s", reason);
02977 pvt->needdestroy = 1;
02978 }
02979
02980
02981
02982
02983 static void initialize_initreq(struct sip_pvt *p, struct sip_request *req)
02984 {
02985 if (p->initreq.headers) {
02986 ast_debug(1, "Initializing already initialized SIP dialog %s (presumably reinvite)\n", p->callid);
02987 } else {
02988 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
02989 }
02990
02991 copy_request(&p->initreq, req);
02992 parse_request(&p->initreq);
02993 if (req->debug) {
02994 ast_verbose("Initreq: %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
02995 }
02996 }
02997
02998
02999 static void sip_alreadygone(struct sip_pvt *dialog)
03000 {
03001 ast_debug(3, "Setting SIP_ALREADYGONE on dialog %s\n", dialog->callid);
03002 dialog->alreadygone = 1;
03003 }
03004
03005
03006 static int proxy_update(struct sip_proxy *proxy)
03007 {
03008
03009
03010 if (!ast_sockaddr_parse(&proxy->ip, proxy->name, 0)) {
03011
03012
03013 proxy->ip.ss.ss_family = get_address_family_filter(&bindaddr);
03014 if (ast_get_ip_or_srv(&proxy->ip, proxy->name, sip_cfg.srvlookup ? "_sip._udp" : NULL) < 0) {
03015 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", proxy->name);
03016 return FALSE;
03017 }
03018
03019 }
03020
03021 ast_sockaddr_set_port(&proxy->ip, proxy->port);
03022
03023 proxy->last_dnsupdate = time(NULL);
03024 return TRUE;
03025 }
03026
03027
03028
03029
03030
03031 unsigned int port_str2int(const char *pt, unsigned int standard)
03032 {
03033 int port = standard;
03034 if (ast_strlen_zero(pt) || (sscanf(pt, "%30d", &port) != 1) || (port < 1) || (port > 65535)) {
03035 port = standard;
03036 }
03037
03038 return port;
03039 }
03040
03041
03042 static struct sip_proxy *obproxy_get(struct sip_pvt *dialog, struct sip_peer *peer)
03043 {
03044 if (peer && peer->outboundproxy) {
03045 if (sipdebug) {
03046 ast_debug(1, "OBPROXY: Applying peer OBproxy to this call\n");
03047 }
03048 append_history(dialog, "OBproxy", "Using peer obproxy %s", peer->outboundproxy->name);
03049 return peer->outboundproxy;
03050 }
03051 if (sip_cfg.outboundproxy.name[0]) {
03052 if (sipdebug) {
03053 ast_debug(1, "OBPROXY: Applying global OBproxy to this call\n");
03054 }
03055 append_history(dialog, "OBproxy", "Using global obproxy %s", sip_cfg.outboundproxy.name);
03056 return &sip_cfg.outboundproxy;
03057 }
03058 if (sipdebug) {
03059 ast_debug(1, "OBPROXY: Not applying OBproxy to this call\n");
03060 }
03061 return NULL;
03062 }
03063
03064
03065
03066
03067
03068
03069
03070 static int method_match(enum sipmethod id, const char *name)
03071 {
03072 int len = strlen(sip_methods[id].text);
03073 int l_name = name ? strlen(name) : 0;
03074
03075 return (l_name >= len && name[len] < 33 &&
03076 !strncasecmp(sip_methods[id].text, name, len));
03077 }
03078
03079
03080 static int find_sip_method(const char *msg)
03081 {
03082 int i, res = 0;
03083
03084 if (ast_strlen_zero(msg)) {
03085 return 0;
03086 }
03087 for (i = 1; i < ARRAY_LEN(sip_methods) && !res; i++) {
03088 if (method_match(i, msg)) {
03089 res = sip_methods[i].id;
03090 }
03091 }
03092 return res;
03093 }
03094
03095
03096 static inline int sip_debug_test_addr(const struct ast_sockaddr *addr)
03097 {
03098
03099 if (!sipdebug) {
03100 return 0;
03101 }
03102
03103
03104 if (ast_sockaddr_isnull(&debugaddr)) {
03105 return 1;
03106 }
03107
03108
03109
03110
03111 if (ast_sockaddr_port(&debugaddr)) {
03112 return !ast_sockaddr_cmp(&debugaddr, addr);
03113 } else {
03114 return !ast_sockaddr_cmp_addr(&debugaddr, addr);
03115 }
03116 }
03117
03118
03119 static const struct ast_sockaddr *sip_real_dst(const struct sip_pvt *p)
03120 {
03121 if (p->outboundproxy) {
03122 return &p->outboundproxy->ip;
03123 }
03124
03125 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;
03126 }
03127
03128
03129 static const char *sip_nat_mode(const struct sip_pvt *p)
03130 {
03131 return ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT) ? "NAT" : "no NAT";
03132 }
03133
03134
03135 static inline int sip_debug_test_pvt(struct sip_pvt *p)
03136 {
03137 if (!sipdebug) {
03138 return 0;
03139 }
03140 return sip_debug_test_addr(sip_real_dst(p));
03141 }
03142
03143
03144 static int get_transport_str2enum(const char *transport)
03145 {
03146 int res = 0;
03147
03148 if (ast_strlen_zero(transport)) {
03149 return res;
03150 }
03151
03152 if (!strcasecmp(transport, "udp")) {
03153 res |= SIP_TRANSPORT_UDP;
03154 }
03155 if (!strcasecmp(transport, "tcp")) {
03156 res |= SIP_TRANSPORT_TCP;
03157 }
03158 if (!strcasecmp(transport, "tls")) {
03159 res |= SIP_TRANSPORT_TLS;
03160 }
03161
03162 return res;
03163 }
03164
03165
03166 static inline const char *get_transport_list(unsigned int transports) {
03167 switch (transports) {
03168 case SIP_TRANSPORT_UDP:
03169 return "UDP";
03170 case SIP_TRANSPORT_TCP:
03171 return "TCP";
03172 case SIP_TRANSPORT_TLS:
03173 return "TLS";
03174 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TCP:
03175 return "TCP,UDP";
03176 case SIP_TRANSPORT_UDP | SIP_TRANSPORT_TLS:
03177 return "TLS,UDP";
03178 case SIP_TRANSPORT_TCP | SIP_TRANSPORT_TLS:
03179 return "TLS,TCP";
03180 default:
03181 return transports ?
03182 "TLS,TCP,UDP" : "UNKNOWN";
03183 }
03184 }
03185
03186
03187 static inline const char *get_transport(enum sip_transport t)
03188 {
03189 switch (t) {
03190 case SIP_TRANSPORT_UDP:
03191 return "UDP";
03192 case SIP_TRANSPORT_TCP:
03193 return "TCP";
03194 case SIP_TRANSPORT_TLS:
03195 return "TLS";
03196 }
03197
03198 return "UNKNOWN";
03199 }
03200
03201
03202 static inline const char *get_srv_protocol(enum sip_transport t)
03203 {
03204 switch (t) {
03205 case SIP_TRANSPORT_UDP:
03206 return "udp";
03207 case SIP_TRANSPORT_TLS:
03208 case SIP_TRANSPORT_TCP:
03209 return "tcp";
03210 }
03211
03212 return "udp";
03213 }
03214
03215
03216 static inline const char *get_srv_service(enum sip_transport t)
03217 {
03218 switch (t) {
03219 case SIP_TRANSPORT_TCP:
03220 case SIP_TRANSPORT_UDP:
03221 return "sip";
03222 case SIP_TRANSPORT_TLS:
03223 return "sips";
03224 }
03225 return "sip";
03226 }
03227
03228
03229
03230
03231
03232
03233
03234 static inline const char *get_transport_pvt(struct sip_pvt *p)
03235 {
03236 if (p->outboundproxy && p->outboundproxy->transport) {
03237 set_socket_transport(&p->socket, p->outboundproxy->transport);
03238 }
03239
03240 return get_transport(p->socket.type);
03241 }
03242
03243
03244
03245
03246
03247
03248
03249 static int __sip_xmit(struct sip_pvt *p, struct ast_str *data, int len)
03250 {
03251 int res = 0;
03252 const struct ast_sockaddr *dst = sip_real_dst(p);
03253
03254 ast_debug(2, "Trying to put '%.11s' onto %s socket destined for %s\n", data->str, get_transport_pvt(p), ast_sockaddr_stringify(dst));
03255
03256 if (sip_prepare_socket(p) < 0) {
03257 return XMIT_ERROR;
03258 }
03259
03260 if (p->socket.type == SIP_TRANSPORT_UDP) {
03261 res = ast_sendto(p->socket.fd, data->str, len, 0, dst);
03262 } else if (p->socket.tcptls_session) {
03263 res = sip_tcptls_write(p->socket.tcptls_session, data->str, len);
03264 } else {
03265 ast_debug(2, "Socket type is TCP but no tcptls_session is present to write to\n");
03266 return XMIT_ERROR;
03267 }
03268
03269 if (res == -1) {
03270 switch (errno) {
03271 case EBADF:
03272 case EHOSTUNREACH:
03273 case ENETDOWN:
03274 case ENETUNREACH:
03275 case ECONNREFUSED:
03276 res = XMIT_ERROR;
03277 }
03278 }
03279 if (res != len) {
03280 ast_log(LOG_WARNING, "sip_xmit of %p (len %d) to %s returned %d: %s\n", data, len, ast_sockaddr_stringify(dst), res, strerror(errno));
03281 }
03282
03283 return res;
03284 }
03285
03286
03287 static void build_via(struct sip_pvt *p)
03288 {
03289
03290 const char *rport = (ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT) || ast_test_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT)) ? ";rport" : "";
03291
03292
03293 snprintf(p->via, sizeof(p->via), "SIP/2.0/%s %s;branch=z9hG4bK%08x%s",
03294 get_transport_pvt(p),
03295 ast_sockaddr_stringify(&p->ourip),
03296 (int) p->branch, rport);
03297 }
03298
03299
03300
03301
03302
03303
03304
03305
03306 static void ast_sip_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us, struct sip_pvt *p)
03307 {
03308 struct ast_sockaddr theirs;
03309
03310
03311
03312
03313
03314
03315
03316
03317
03318
03319
03320
03321
03322 int want_remap = 0;
03323
03324 ast_sockaddr_copy(us, &internip);
03325
03326 ast_ouraddrfor(them, us);
03327 ast_sockaddr_copy(&theirs, them);
03328
03329 if (ast_sockaddr_is_ipv6(&theirs)) {
03330 if (localaddr && !ast_sockaddr_isnull(&externaddr)) {
03331 ast_log(LOG_WARNING, "Address remapping activated in sip.conf "
03332 "but we're using IPv6, which doesn't need it. Please "
03333 "remove \"localnet\" and/or \"externaddr\" settings.\n");
03334 }
03335 } else {
03336 want_remap = localaddr &&
03337 !ast_sockaddr_isnull(&externaddr) &&
03338 ast_apply_ha(localaddr, &theirs) == AST_SENSE_ALLOW ;
03339 }
03340
03341 if (want_remap &&
03342 (!sip_cfg.matchexternaddrlocally || !ast_apply_ha(localaddr, us)) ) {
03343
03344 if (externexpire && time(NULL) >= externexpire) {
03345 if (ast_sockaddr_resolve_first(&externaddr, externhost, 0)) {
03346 ast_log(LOG_NOTICE, "Warning: Re-lookup of '%s' failed!\n", externhost);
03347 }
03348 externexpire = time(NULL) + externrefresh;
03349 }
03350 if (!ast_sockaddr_isnull(&externaddr)) {
03351 ast_sockaddr_copy(us, &externaddr);
03352 switch (p->socket.type) {
03353 case SIP_TRANSPORT_TCP:
03354 if (!externtcpport && ast_sockaddr_port(&externaddr)) {
03355
03356 externtcpport = ast_sockaddr_port(&externaddr);
03357 }
03358 ast_sockaddr_set_port(us, externtcpport);
03359 break;
03360 case SIP_TRANSPORT_TLS:
03361 ast_sockaddr_set_port(us, externtlsport);
03362 break;
03363 case SIP_TRANSPORT_UDP:
03364 if (!ast_sockaddr_port(&externaddr)) {
03365 ast_sockaddr_set_port(us, ast_sockaddr_port(&bindaddr));
03366 }
03367 break;
03368 default:
03369 break;
03370 }
03371 }
03372 ast_debug(1, "Target address %s is not local, substituting externaddr\n",
03373 ast_sockaddr_stringify(them));
03374 } else if (p) {
03375
03376 switch (p->socket.type) {
03377 case SIP_TRANSPORT_TCP:
03378 if (!ast_sockaddr_is_any(&sip_tcp_desc.local_address)) {
03379 ast_sockaddr_copy(us,
03380 &sip_tcp_desc.local_address);
03381 } else {
03382 ast_sockaddr_set_port(us,
03383 ast_sockaddr_port(&sip_tcp_desc.local_address));
03384 }
03385 break;
03386 case SIP_TRANSPORT_TLS:
03387 if (!ast_sockaddr_is_any(&sip_tls_desc.local_address)) {
03388 ast_sockaddr_copy(us,
03389 &sip_tls_desc.local_address);
03390 } else {
03391 ast_sockaddr_set_port(us,
03392 ast_sockaddr_port(&sip_tls_desc.local_address));
03393 }
03394 break;
03395 case SIP_TRANSPORT_UDP:
03396
03397 default:
03398 if (!ast_sockaddr_is_any(&bindaddr)) {
03399 ast_sockaddr_copy(us, &bindaddr);
03400 }
03401 if (!ast_sockaddr_port(us)) {
03402 ast_sockaddr_set_port(us, ast_sockaddr_port(&bindaddr));
03403 }
03404 }
03405 } else if (!ast_sockaddr_is_any(&bindaddr)) {
03406 ast_sockaddr_copy(us, &bindaddr);
03407 }
03408 ast_debug(3, "Setting SIP_TRANSPORT_%s with address %s\n", get_transport(p->socket.type), ast_sockaddr_stringify(us));
03409 }
03410
03411
03412 static __attribute__((format(printf, 2, 0))) void append_history_va(struct sip_pvt *p, const char *fmt, va_list ap)
03413 {
03414 char buf[80], *c = buf;
03415 struct sip_history *hist;
03416 int l;
03417
03418 vsnprintf(buf, sizeof(buf), fmt, ap);
03419 strsep(&c, "\r\n");
03420 l = strlen(buf) + 1;
03421 if (!(hist = ast_calloc(1, sizeof(*hist) + l))) {
03422 return;
03423 }
03424 if (!p->history && !(p->history = ast_calloc(1, sizeof(*p->history)))) {
03425 ast_free(hist);
03426 return;
03427 }
03428 memcpy(hist->event, buf, l);
03429 if (p->history_entries == MAX_HISTORY_ENTRIES) {
03430 struct sip_history *oldest;
03431 oldest = AST_LIST_REMOVE_HEAD(p->history, list);
03432 p->history_entries--;
03433 ast_free(oldest);
03434 }
03435 AST_LIST_INSERT_TAIL(p->history, hist, list);
03436 p->history_entries++;
03437 }
03438
03439
03440 static void append_history_full(struct sip_pvt *p, const char *fmt, ...)
03441 {
03442 va_list ap;
03443
03444 if (!p) {
03445 return;
03446 }
03447
03448 if (!p->do_history && !recordhistory && !dumphistory) {
03449 return;
03450 }
03451
03452 va_start(ap, fmt);
03453 append_history_va(p, fmt, ap);
03454 va_end(ap);
03455
03456 return;
03457 }
03458
03459
03460 static int retrans_pkt(const void *data)
03461 {
03462 struct sip_pkt *pkt = (struct sip_pkt *)data, *prev, *cur = NULL;
03463 int reschedule = DEFAULT_RETRANS;
03464 int xmitres = 0;
03465
03466 int64_t diff = pkt->retrans_stop_time - ast_tvdiff_ms(ast_tvnow(), pkt->time_sent);
03467
03468
03469
03470
03471 if ((diff <= 0) || (diff > pkt->retrans_stop_time)) {
03472 pkt->retrans_stop = 1;
03473 }
03474
03475
03476 sip_pvt_lock(pkt->owner);
03477
03478 if (!pkt->retrans_stop) {
03479 pkt->retrans++;
03480 if (!pkt->timer_t1) {
03481 if (sipdebug) {
03482 ast_debug(4, "SIP TIMER: Not rescheduling id #%d:%s (Method %d) (No timer T1)\n",
03483 pkt->retransid,
03484 sip_methods[pkt->method].text,
03485 pkt->method);
03486 }
03487 } else {
03488 int siptimer_a;
03489
03490 if (sipdebug) {
03491 ast_debug(4, "SIP TIMER: Rescheduling retransmission #%d (%d) %s - %d\n",
03492 pkt->retransid,
03493 pkt->retrans,
03494 sip_methods[pkt->method].text,
03495 pkt->method);
03496 }
03497 if (!pkt->timer_a) {
03498 pkt->timer_a = 2 ;
03499 } else {
03500 pkt->timer_a = 2 * pkt->timer_a;
03501 }
03502
03503
03504 siptimer_a = pkt->timer_t1 * pkt->timer_a;
03505 if (pkt->method != SIP_INVITE && siptimer_a > 4000) {
03506 siptimer_a = 4000;
03507 }
03508
03509
03510 reschedule = siptimer_a;
03511 ast_debug(4, "** SIP timers: Rescheduling retransmission %d to %d ms (t1 %d ms (Retrans id #%d)) \n",
03512 pkt->retrans + 1,
03513 siptimer_a,
03514 pkt->timer_t1,
03515 pkt->retransid);
03516 }
03517
03518 if (sip_debug_test_pvt(pkt->owner)) {
03519 const struct ast_sockaddr *dst = sip_real_dst(pkt->owner);
03520 ast_verbose("Retransmitting #%d (%s) to %s:\n%s\n---\n",
03521 pkt->retrans, sip_nat_mode(pkt->owner),
03522 ast_sockaddr_stringify(dst),
03523 pkt->data->str);
03524 }
03525
03526 append_history(pkt->owner, "ReTx", "%d %s", reschedule, pkt->data->str);
03527 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
03528
03529
03530
03531
03532
03533
03534 if (xmitres != XMIT_ERROR) {
03535 if (reschedule >= diff) {
03536 pkt->retrans_stop = 1;
03537 reschedule = diff;
03538 }
03539 sip_pvt_unlock(pkt->owner);
03540 return reschedule;
03541 }
03542 }
03543
03544
03545
03546
03547 pkt->retransid = -1;
03548
03549 if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
03550 if (pkt->is_fatal || sipdebug) {
03551 ast_log(LOG_WARNING, "Retransmission timeout reached on transmission %s for seqno %d (%s %s) -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions\n"
03552 "Packet timed out after %dms with no response\n",
03553 pkt->owner->callid,
03554 pkt->seqno,
03555 pkt->is_fatal ? "Critical" : "Non-critical",
03556 pkt->is_resp ? "Response" : "Request",
03557 (int) ast_tvdiff_ms(ast_tvnow(), pkt->time_sent));
03558 }
03559 } else if (pkt->method == SIP_OPTIONS && sipdebug) {
03560 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);
03561 }
03562
03563 if (xmitres == XMIT_ERROR) {
03564 ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission on Call ID %s\n", pkt->owner->callid);
03565 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03566 } else {
03567 append_history(pkt->owner, "MaxRetries", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03568 }
03569
03570 if (pkt->is_fatal) {
03571 while(pkt->owner->owner && ast_channel_trylock(pkt->owner->owner)) {
03572 sip_pvt_unlock(pkt->owner);
03573 usleep(1);
03574 sip_pvt_lock(pkt->owner);
03575 }
03576 if (pkt->owner->owner && !pkt->owner->owner->hangupcause) {
03577 pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
03578 }
03579 if (pkt->owner->owner) {
03580 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);
03581
03582 if (pkt->is_resp &&
03583 (pkt->response_code >= 200) &&
03584 (pkt->response_code < 300) &&
03585 pkt->owner->pendinginvite &&
03586 ast_test_flag(&pkt->owner->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
03587
03588
03589
03590
03591
03592
03593
03594 pkt->owner->invitestate = INV_TERMINATED;
03595 pkt->owner->pendinginvite = 0;
03596 } else {
03597
03598 sip_alreadygone(pkt->owner);
03599 }
03600 ast_queue_hangup_with_cause(pkt->owner->owner, AST_CAUSE_PROTOCOL_ERROR);
03601 ast_channel_unlock(pkt->owner->owner);
03602 } else {
03603
03604
03605
03606 if (pkt->method != SIP_OPTIONS && pkt->method != SIP_REGISTER) {
03607 pvt_set_needdestroy(pkt->owner, "no response to critical packet");
03608 sip_alreadygone(pkt->owner);
03609 append_history(pkt->owner, "DialogKill", "Killing this failed dialog immediately");
03610 }
03611 }
03612 }
03613
03614 if (pkt->method == SIP_BYE) {
03615
03616 sip_alreadygone(pkt->owner);
03617 if (pkt->owner->owner) {
03618 ast_channel_unlock(pkt->owner->owner);
03619 }
03620 append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
03621 pvt_set_needdestroy(pkt->owner, "no response to BYE");
03622 }
03623
03624
03625 for (prev = NULL, cur = pkt->owner->packets; cur; prev = cur, cur = cur->next) {
03626 if (cur == pkt) {
03627 UNLINK(cur, pkt->owner->packets, prev);
03628 sip_pvt_unlock(pkt->owner);
03629 if (pkt->owner) {
03630 pkt->owner = dialog_unref(pkt->owner,"pkt is being freed, its dialog ref is dead now");
03631 }
03632 if (pkt->data) {
03633 ast_free(pkt->data);
03634 }
03635 pkt->data = NULL;
03636 ast_free(pkt);
03637 return 0;
03638 }
03639 }
03640
03641 ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
03642 sip_pvt_unlock(pkt->owner);
03643 return 0;
03644 }
03645
03646
03647
03648
03649 static enum sip_result __sip_reliable_xmit(struct sip_pvt *p, int seqno, int resp, struct ast_str *data, int len, int fatal, int sipmethod)
03650 {
03651 struct sip_pkt *pkt = NULL;
03652 int siptimer_a = DEFAULT_RETRANS;
03653 int xmitres = 0;
03654 int respid;
03655
03656 if (sipmethod == SIP_INVITE) {
03657
03658 p->pendinginvite = seqno;
03659 }
03660
03661
03662
03663
03664 if (!(p->socket.type & SIP_TRANSPORT_UDP)) {
03665 xmitres = __sip_xmit(p, data, len);
03666 if (xmitres == XMIT_ERROR) {
03667 append_history(p, "XmitErr", "%s", fatal ? "(Critical)" : "(Non-critical)");
03668 return AST_FAILURE;
03669 } else {
03670 return AST_SUCCESS;
03671 }
03672 }
03673
03674 if (!(pkt = ast_calloc(1, sizeof(*pkt) + len + 1))) {
03675 return AST_FAILURE;
03676 }
03677
03678 if (!(pkt->data = ast_str_create(len))) {
03679 ast_free(pkt);
03680 return AST_FAILURE;
03681 }
03682 ast_str_set(&pkt->data, 0, "%s%s", data->str, "\0");
03683 pkt->packetlen = len;
03684
03685 pkt->method = sipmethod;
03686 pkt->seqno = seqno;
03687 pkt->is_resp = resp;
03688 pkt->is_fatal = fatal;
03689 pkt->owner = dialog_ref(p, "__sip_reliable_xmit: setting pkt->owner");
03690 pkt->next = p->packets;
03691 p->packets = pkt;
03692 if (resp) {
03693
03694 if (sscanf(ast_str_buffer(pkt->data), "SIP/2.0 %30u", &respid) == 1) {
03695 pkt->response_code = respid;
03696 }
03697 }
03698 pkt->timer_t1 = p->timer_t1;
03699 pkt->retransid = -1;
03700 if (pkt->timer_t1) {
03701 siptimer_a = pkt->timer_t1;
03702 }
03703
03704 pkt->time_sent = ast_tvnow();
03705 pkt->retrans_stop_time = 64 * (pkt->timer_t1 ? pkt->timer_t1 : DEFAULT_TIMER_T1);
03706
03707
03708 AST_SCHED_REPLACE_VARIABLE(pkt->retransid, sched, siptimer_a, retrans_pkt, pkt, 1);
03709 if (sipdebug) {
03710 ast_debug(4, "*** SIP TIMER: Initializing retransmit timer on packet: Id #%d\n", pkt->retransid);
03711 }
03712
03713 xmitres = __sip_xmit(pkt->owner, pkt->data, pkt->packetlen);
03714
03715 if (xmitres == XMIT_ERROR) {
03716 append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
03717 ast_log(LOG_ERROR, "Serious Network Trouble; __sip_xmit returns error for pkt data\n");
03718 AST_SCHED_DEL(sched, pkt->retransid);
03719 p->packets = pkt->next;
03720 pkt->owner = dialog_unref(pkt->owner,"pkt is being freed, its dialog ref is dead now");
03721 ast_free(pkt->data);
03722 ast_free(pkt);
03723 return AST_FAILURE;
03724 } else {
03725
03726
03727
03728
03729 if (monitor_thread != AST_PTHREADT_NULL) {
03730 pthread_kill(monitor_thread, SIGURG);
03731 }
03732 return AST_SUCCESS;
03733 }
03734 }
03735
03736
03737
03738
03739
03740
03741 static int __sip_autodestruct(const void *data)
03742 {
03743 struct sip_pvt *p = (struct sip_pvt *)data;
03744
03745
03746 if (p->subscribed && p->subscribed != MWI_NOTIFICATION && p->subscribed != CALL_COMPLETION) {
03747 transmit_state_notify(p, AST_EXTENSION_DEACTIVATED, 1, TRUE);
03748 p->subscribed = NONE;
03749 append_history(p, "Subscribestatus", "timeout");
03750 ast_debug(3, "Re-scheduled destruction of SIP subscription %s\n", p->callid ? p->callid : "<unknown>");
03751 return 10000;
03752 }
03753
03754
03755 if (p->packets) {
03756 if (!p->needdestroy) {
03757 char method_str[31];
03758 ast_debug(3, "Re-scheduled destruction of SIP call %s\n", p->callid ? p->callid : "<unknown>");
03759 append_history(p, "ReliableXmit", "timeout");
03760 if (sscanf(p->lastmsg, "Tx: %30s", method_str) == 1 || sscanf(p->lastmsg, "Rx: %30s", method_str) == 1) {
03761 if (method_match(SIP_CANCEL, method_str) || method_match(SIP_BYE, method_str)) {
03762 pvt_set_needdestroy(p, "autodestruct");
03763 }
03764 }
03765 return 10000;
03766 } else {
03767
03768 __sip_pretend_ack(p);
03769 }
03770 }
03771
03772
03773 p->autokillid = -1;
03774
03775
03776
03777
03778
03779 sip_pvt_lock(p);
03780 while (p->owner && ast_channel_trylock(p->owner)) {
03781 sip_pvt_unlock(p);
03782 sched_yield();
03783 sip_pvt_lock(p);
03784 }
03785
03786 if (p->owner) {
03787 ast_log(LOG_WARNING, "Autodestruct on dialog '%s' with owner in place (Method: %s)\n", p->callid, sip_methods[p->method].text);
03788 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
03789 ast_channel_unlock(p->owner);
03790 } else if (p->refer && !p->alreadygone) {
03791 ast_debug(3, "Finally hanging up channel after transfer: %s\n", p->callid);
03792 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
03793 append_history(p, "ReferBYE", "Sending BYE on transferer call leg %s", p->callid);
03794 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
03795 } else {
03796 append_history(p, "AutoDestroy", "%s", p->callid);
03797 ast_debug(3, "Auto destroying SIP dialog '%s'\n", p->callid);
03798 dialog_unlink_all(p, TRUE, TRUE);
03799
03800
03801
03802 }
03803
03804 sip_pvt_unlock(p);
03805
03806 dialog_unref(p, "The ref to a dialog passed to this sched callback is going out of scope; unref it.");
03807
03808 return 0;
03809 }
03810
03811
03812
03813
03814 void sip_scheddestroy_final(struct sip_pvt *p, int ms)
03815 {
03816 if (p->final_destruction_scheduled) {
03817 return;
03818 }
03819
03820 sip_scheddestroy(p, ms);
03821 if (p->autokillid != -1) {
03822 p->final_destruction_scheduled = 1;
03823 }
03824 }
03825
03826
03827 void sip_scheddestroy(struct sip_pvt *p, int ms)
03828 {
03829 if (p->final_destruction_scheduled) {
03830 return;
03831 }
03832
03833 if (ms < 0) {
03834 if (p->timer_t1 == 0) {
03835 p->timer_t1 = global_t1;
03836 }
03837 if (p->timer_b == 0) {
03838 p->timer_b = global_timer_b;
03839 }
03840 ms = p->timer_t1 * 64;
03841 }
03842 if (sip_debug_test_pvt(p)) {
03843 ast_verbose("Scheduling destruction of SIP dialog '%s' in %d ms (Method: %s)\n", p->callid, ms, sip_methods[p->method].text);
03844 }
03845 if (sip_cancel_destroy(p)) {
03846 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
03847 }
03848
03849 if (p->do_history) {
03850 append_history(p, "SchedDestroy", "%d ms", ms);
03851 }
03852 p->autokillid = ast_sched_add(sched, ms, __sip_autodestruct, dialog_ref(p, "setting ref as passing into ast_sched_add for __sip_autodestruct"));
03853
03854 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_schedid > 0) {
03855 stop_session_timer(p);
03856 }
03857 }
03858
03859
03860
03861
03862
03863 int sip_cancel_destroy(struct sip_pvt *p)
03864 {
03865 int res = 0;
03866
03867 if (p->final_destruction_scheduled) {
03868 return res;
03869 }
03870
03871 if (p->autokillid > -1) {
03872 int res3;
03873
03874 if (!(res3 = ast_sched_del(sched, p->autokillid))) {
03875 append_history(p, "CancelDestroy", "");
03876 p->autokillid = -1;
03877 dialog_unref(p, "dialog unrefd because autokillid is de-sched'd");
03878 }
03879 }
03880 return res;
03881 }
03882
03883
03884
03885 int __sip_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
03886 {
03887 struct sip_pkt *cur, *prev = NULL;
03888 const char *msg = "Not Found";
03889 int res = FALSE;
03890
03891
03892
03893
03894
03895
03896 if (p->outboundproxy && !p->outboundproxy->force){
03897 ref_proxy(p, NULL);
03898 }
03899
03900 for (cur = p->packets; cur; prev = cur, cur = cur->next) {
03901 if (cur->seqno != seqno || cur->is_resp != resp) {
03902 continue;
03903 }
03904 if (cur->is_resp || cur->method == sipmethod) {
03905 res = TRUE;
03906 msg = "Found";
03907 if (!resp && (seqno == p->pendinginvite)) {
03908 ast_debug(1, "Acked pending invite %d\n", p->pendinginvite);
03909 p->pendinginvite = 0;
03910 }
03911 if (cur->retransid > -1) {
03912 if (sipdebug)
03913 ast_debug(4, "** SIP TIMER: Cancelling retransmit of packet (reply received) Retransid #%d\n", cur->retransid);
03914 }
03915
03916
03917
03918
03919
03920
03921
03922
03923
03924
03925
03926
03927
03928
03929
03930
03931 while (cur->retransid > -1 && ast_sched_del(sched, cur->retransid)) {
03932 sip_pvt_unlock(p);
03933 usleep(1);
03934 sip_pvt_lock(p);
03935 }
03936 UNLINK(cur, p->packets, prev);
03937 dialog_unref(cur->owner, "unref pkt cur->owner dialog from sip ack before freeing pkt");
03938 if (cur->data) {
03939 ast_free(cur->data);
03940 }
03941 ast_free(cur);
03942 break;
03943 }
03944 }
03945 ast_debug(1, "Stopping retransmission on '%s' of %s %d: Match %s\n",
03946 p->callid, resp ? "Response" : "Request", seqno, msg);
03947 return res;
03948 }
03949
03950
03951
03952 void __sip_pretend_ack(struct sip_pvt *p)
03953 {
03954 struct sip_pkt *cur = NULL;
03955
03956 while (p->packets) {
03957 int method;
03958 if (cur == p->packets) {
03959 ast_log(LOG_WARNING, "Have a packet that doesn't want to give up! %s\n", sip_methods[cur->method].text);
03960 return;
03961 }
03962 cur = p->packets;
03963 method = (cur->method) ? cur->method : find_sip_method(cur->data->str);
03964 __sip_ack(p, cur->seqno, cur->is_resp, method);
03965 }
03966 }
03967
03968
03969 int __sip_semi_ack(struct sip_pvt *p, int seqno, int resp, int sipmethod)
03970 {
03971 struct sip_pkt *cur;
03972 int res = FALSE;
03973
03974 for (cur = p->packets; cur; cur = cur->next) {
03975 if (cur->seqno == seqno && cur->is_resp == resp &&
03976 (cur->is_resp || method_match(sipmethod, cur->data->str))) {
03977
03978 if (cur->retransid > -1) {
03979 if (sipdebug)
03980 ast_debug(4, "*** SIP TIMER: Cancelling retransmission #%d - %s (got response)\n", cur->retransid, sip_methods[sipmethod].text);
03981 }
03982 AST_SCHED_DEL(sched, cur->retransid);
03983 res = TRUE;
03984 break;
03985 }
03986 }
03987 ast_debug(1, "(Provisional) Stopping retransmission (but retaining packet) on '%s' %s %d: %s\n", p->callid, resp ? "Response" : "Request", seqno, res == -1 ? "Not Found" : "Found");
03988 return res;
03989 }
03990
03991
03992
03993 static void parse_copy(struct sip_request *dst, const struct sip_request *src)
03994 {
03995 copy_request(dst, src);
03996 parse_request(dst);
03997 }
03998
03999
04000 static void add_blank(struct sip_request *req)
04001 {
04002 if (!req->lines) {
04003
04004 ast_str_append(&req->data, 0, "\r\n");
04005 req->len = ast_str_strlen(req->data);
04006 }
04007 }
04008
04009 static int send_provisional_keepalive_full(struct sip_pvt *pvt, int with_sdp)
04010 {
04011 const char *msg = NULL;
04012
04013 if (!pvt->last_provisional || !strncasecmp(pvt->last_provisional, "100", 3)) {
04014 msg = "183 Session Progress";
04015 }
04016
04017 if (pvt->invitestate < INV_COMPLETED) {
04018 if (with_sdp) {
04019 transmit_response_with_sdp(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq, XMIT_UNRELIABLE, FALSE, FALSE);
04020 } else {
04021 transmit_response(pvt, S_OR(msg, pvt->last_provisional), &pvt->initreq);
04022 }
04023 return PROVIS_KEEPALIVE_TIMEOUT;
04024 }
04025
04026 return 0;
04027 }
04028
04029 static int send_provisional_keepalive(const void *data) {
04030 struct sip_pvt *pvt = (struct sip_pvt *) data;
04031
04032 return send_provisional_keepalive_full(pvt, 0);
04033 }
04034
04035 static int send_provisional_keepalive_with_sdp(const void *data) {
04036 struct sip_pvt *pvt = (void *)data;
04037
04038 return send_provisional_keepalive_full(pvt, 1);
04039 }
04040
04041 static void update_provisional_keepalive(struct sip_pvt *pvt, int with_sdp)
04042 {
04043 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"));
04044
04045 pvt->provisional_keepalive_sched_id = ast_sched_add(sched, PROVIS_KEEPALIVE_TIMEOUT,
04046 with_sdp ? send_provisional_keepalive_with_sdp : send_provisional_keepalive, dialog_ref(pvt, "Increment refcount to pass dialog pointer to sched callback"));
04047 }
04048
04049
04050 static int send_response(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
04051 {
04052 int res;
04053
04054 finalize_content(req);
04055 add_blank(req);
04056 if (sip_debug_test_pvt(p)) {
04057 const struct ast_sockaddr *dst = sip_real_dst(p);
04058
04059 ast_verbose("\n<--- %sTransmitting (%s) to %s --->\n%s\n<------------>\n",
04060 reliable ? "Reliably " : "", sip_nat_mode(p),
04061 ast_sockaddr_stringify(dst),
04062 req->data->str);
04063 }
04064 if (p->do_history) {
04065 struct sip_request tmp = { .rlPart1 = 0, };
04066 parse_copy(&tmp, req);
04067 append_history(p, reliable ? "TxRespRel" : "TxResp", "%s / %s - %s", tmp.data->str, get_header(&tmp, "CSeq"),
04068 (tmp.method == SIP_RESPONSE || tmp.method == SIP_UNKNOWN) ? REQ_OFFSET_TO_STR(&tmp, rlPart2) : sip_methods[tmp.method].text);
04069 deinit_req(&tmp);
04070 }
04071
04072
04073 if (p->initreq.method == SIP_INVITE && reliable == XMIT_CRITICAL) {
04074 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"));
04075 }
04076
04077 res = (reliable) ?
04078 __sip_reliable_xmit(p, seqno, 1, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
04079 __sip_xmit(p, req->data, req->len);
04080 deinit_req(req);
04081 if (res > 0) {
04082 return 0;
04083 }
04084 return res;
04085 }
04086
04087
04088
04089
04090 static int send_request(struct sip_pvt *p, struct sip_request *req, enum xmittype reliable, int seqno)
04091 {
04092 int res;
04093
04094
04095
04096
04097 if (p->outboundproxy) {
04098 p->sa = p->outboundproxy->ip;
04099 }
04100
04101 finalize_content(req);
04102 add_blank(req);
04103 if (sip_debug_test_pvt(p)) {
04104 if (ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT)) {
04105 ast_verbose("%sTransmitting (NAT) to %s:\n%s\n---\n", reliable ? "Reliably " : "", ast_sockaddr_stringify(&p->recv), req->data->str);
04106 } else {
04107 ast_verbose("%sTransmitting (no NAT) to %s:\n%s\n---\n", reliable ? "Reliably " : "", ast_sockaddr_stringify(&p->sa), req->data->str);
04108 }
04109 }
04110 if (p->do_history) {
04111 struct sip_request tmp = { .rlPart1 = 0, };
04112 parse_copy(&tmp, req);
04113 append_history(p, reliable ? "TxReqRel" : "TxReq", "%s / %s - %s", tmp.data->str, get_header(&tmp, "CSeq"), sip_methods[tmp.method].text);
04114 deinit_req(&tmp);
04115 }
04116 res = (reliable) ?
04117 __sip_reliable_xmit(p, seqno, 0, req->data, req->len, (reliable == XMIT_CRITICAL), req->method) :
04118 __sip_xmit(p, req->data, req->len);
04119 deinit_req(req);
04120 return res;
04121 }
04122
04123 static void enable_dsp_detect(struct sip_pvt *p)
04124 {
04125 int features = 0;
04126
04127 if (p->dsp) {
04128 return;
04129 }
04130
04131 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
04132 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
04133 if (!p->rtp || ast_rtp_instance_dtmf_mode_set(p->rtp, AST_RTP_DTMF_MODE_INBAND)) {
04134 features |= DSP_FEATURE_DIGIT_DETECT;
04135 }
04136 }
04137
04138 if (ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_CNG)) {
04139 features |= DSP_FEATURE_FAX_DETECT;
04140 }
04141
04142 if (!features) {
04143 return;
04144 }
04145
04146 if (!(p->dsp = ast_dsp_new())) {
04147 return;
04148 }
04149
04150 ast_dsp_set_features(p->dsp, features);
04151 if (global_relaxdtmf) {
04152 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
04153 }
04154 }
04155
04156 static void disable_dsp_detect(struct sip_pvt *p)
04157 {
04158 if (p->dsp) {
04159 ast_dsp_free(p->dsp);
04160 p->dsp = NULL;
04161 }
04162 }
04163
04164
04165 static int sip_setoption(struct ast_channel *chan, int option, void *data, int datalen)
04166 {
04167 int res = -1;
04168 struct sip_pvt *p = chan->tech_pvt;
04169
04170 switch (option) {
04171 case AST_OPTION_FORMAT_READ:
04172 res = ast_rtp_instance_set_read_format(p->rtp, *(int *) data);
04173 break;
04174 case AST_OPTION_FORMAT_WRITE:
04175 res = ast_rtp_instance_set_write_format(p->rtp, *(int *) data);
04176 break;
04177 case AST_OPTION_MAKE_COMPATIBLE:
04178 res = ast_rtp_instance_make_compatible(chan, p->rtp, (struct ast_channel *) data);
04179 break;
04180 case AST_OPTION_DIGIT_DETECT:
04181 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
04182 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
04183 char *cp = (char *) data;
04184
04185 ast_debug(1, "%sabling digit detection on %s\n", *cp ? "En" : "Dis", chan->name);
04186 if (*cp) {
04187 enable_dsp_detect(p);
04188 } else {
04189 disable_dsp_detect(p);
04190 }
04191 res = 0;
04192 }
04193 break;
04194 case AST_OPTION_SECURE_SIGNALING:
04195 p->req_secure_signaling = *(unsigned int *) data;
04196 res = 0;
04197 break;
04198 case AST_OPTION_SECURE_MEDIA:
04199 ast_set2_flag(&p->flags[1], *(unsigned int *) data, SIP_PAGE2_USE_SRTP);
04200 res = 0;
04201 break;
04202 default:
04203 break;
04204 }
04205
04206 return res;
04207 }
04208
04209
04210 static int sip_queryoption(struct ast_channel *chan, int option, void *data, int *datalen)
04211 {
04212 int res = -1;
04213 enum ast_t38_state state = T38_STATE_UNAVAILABLE;
04214 struct sip_pvt *p = (struct sip_pvt *) chan->tech_pvt;
04215 char *cp;
04216
04217 switch (option) {
04218 case AST_OPTION_T38_STATE:
04219
04220 if (*datalen != sizeof(enum ast_t38_state)) {
04221 ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option. Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
04222 return -1;
04223 }
04224
04225 sip_pvt_lock(p);
04226
04227
04228 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT)) {
04229 switch (p->t38.state) {
04230 case T38_LOCAL_REINVITE:
04231 case T38_PEER_REINVITE:
04232 state = T38_STATE_NEGOTIATING;
04233 break;
04234 case T38_ENABLED:
04235 state = T38_STATE_NEGOTIATED;
04236 break;
04237 default:
04238 state = T38_STATE_UNKNOWN;
04239 }
04240 }
04241
04242 sip_pvt_unlock(p);
04243
04244 *((enum ast_t38_state *) data) = state;
04245 res = 0;
04246
04247 break;
04248 case AST_OPTION_DIGIT_DETECT:
04249 cp = (char *) data;
04250 *cp = p->dsp ? 1 : 0;
04251 ast_debug(1, "Reporting digit detection %sabled on %s\n", *cp ? "en" : "dis", chan->name);
04252 break;
04253 case AST_OPTION_SECURE_SIGNALING:
04254 *((unsigned int *) data) = p->req_secure_signaling;
04255 res = 0;
04256 break;
04257 case AST_OPTION_SECURE_MEDIA:
04258 *((unsigned int *) data) = ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP) ? 1 : 0;
04259 res = 0;
04260 break;
04261 case AST_OPTION_DEVICE_NAME:
04262 if (p && p->outgoing_call) {
04263 cp = (char *) data;
04264 ast_copy_string(cp, p->dialstring, *datalen);
04265 res = 0;
04266 }
04267
04268
04269
04270 break;
04271 default:
04272 break;
04273 }
04274
04275 return res;
04276 }
04277
04278
04279
04280
04281
04282 const char *find_closing_quote(const char *start, const char *lim)
04283 {
04284 char last_char = '\0';
04285 const char *s;
04286 for (s = start; *s && s != lim; last_char = *s++) {
04287 if (*s == '"' && last_char != '\\')
04288 break;
04289 }
04290 return s;
04291 }
04292
04293
04294 static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
04295 {
04296 struct sip_pvt *p = chan->tech_pvt;
04297
04298 if (subclass != AST_HTML_URL)
04299 return -1;
04300
04301 ast_string_field_build(p, url, "<%s>;mode=active", data);
04302
04303 if (sip_debug_test_pvt(p))
04304 ast_debug(1, "Send URL %s, state = %d!\n", data, chan->_state);
04305
04306 switch (chan->_state) {
04307 case AST_STATE_RING:
04308 transmit_response(p, "100 Trying", &p->initreq);
04309 break;
04310 case AST_STATE_RINGING:
04311 transmit_response(p, "180 Ringing", &p->initreq);
04312 break;
04313 case AST_STATE_UP:
04314 if (!p->pendinginvite) {
04315 transmit_reinvite_with_sdp(p, FALSE, FALSE);
04316 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
04317 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
04318 }
04319 break;
04320 default:
04321 ast_log(LOG_WARNING, "Don't know how to send URI when state is %d!\n", chan->_state);
04322 }
04323
04324 return 0;
04325 }
04326
04327
04328 static const char *sip_get_callid(struct ast_channel *chan)
04329 {
04330 return chan->tech_pvt ? ((struct sip_pvt *) chan->tech_pvt)->callid : "";
04331 }
04332
04333
04334
04335 static int sip_sendtext(struct ast_channel *ast, const char *text)
04336 {
04337 struct sip_pvt *dialog = ast->tech_pvt;
04338 int debug = sip_debug_test_pvt(dialog);
04339
04340 if (!dialog)
04341 return -1;
04342
04343
04344 if (!text)
04345 return 0;
04346 if(!is_method_allowed(&dialog->allowed_methods, SIP_MESSAGE)) {
04347 ast_debug(2, "Trying to send MESSAGE to device that does not support it.\n");
04348 return(0);
04349 }
04350 if (debug)
04351 ast_verbose("Sending text %s on %s\n", text, ast->name);
04352 transmit_message_with_text(dialog, text);
04353 return 0;
04354 }
04355
04356
04357
04358
04359
04360
04361 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)
04362 {
04363 char port[10];
04364 char ipaddr[INET6_ADDRSTRLEN];
04365 char regseconds[20];
04366 char *tablename = NULL;
04367 char str_lastms[20];
04368
04369 const char *sysname = ast_config_AST_SYSTEM_NAME;
04370 char *syslabel = NULL;
04371
04372 time_t nowtime = time(NULL) + expirey;
04373 const char *fc = fullcontact ? "fullcontact" : NULL;
04374
04375 int realtimeregs = ast_check_realtime("sipregs");
04376
04377 tablename = realtimeregs ? "sipregs" : "sippeers";
04378
04379
04380 snprintf(str_lastms, sizeof(str_lastms), "%d", lastms);
04381 snprintf(regseconds, sizeof(regseconds), "%d", (int)nowtime);
04382 ast_copy_string(ipaddr, ast_sockaddr_stringify_addr(addr), sizeof(ipaddr));
04383 ast_copy_string(port, ast_sockaddr_stringify_port(addr), sizeof(port));
04384
04385 if (ast_strlen_zero(sysname))
04386 sysname = NULL;
04387 else if (sip_cfg.rtsave_sysname)
04388 syslabel = "regserver";
04389
04390
04391
04392
04393
04394
04395
04396 if (fc) {
04397 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
04398 "port", port, "regseconds", regseconds,
04399 deprecated_username ? "username" : "defaultuser", defaultuser,
04400 "useragent", useragent, "lastms", str_lastms,
04401 fc, fullcontact, syslabel, sysname, SENTINEL);
04402 } else {
04403 ast_update_realtime(tablename, "name", peername, "ipaddr", ipaddr,
04404 "port", port, "regseconds", regseconds,
04405 "useragent", useragent, "lastms", str_lastms,
04406 deprecated_username ? "username" : "defaultuser", defaultuser,
04407 syslabel, sysname, SENTINEL);
04408 }
04409 }
04410
04411
04412 static void register_peer_exten(struct sip_peer *peer, int onoff)
04413 {
04414 char multi[256];
04415 char *stringp, *ext, *context;
04416 struct pbx_find_info q = { .stacklen = 0 };
04417
04418
04419
04420
04421
04422 if (ast_strlen_zero(sip_cfg.regcontext))
04423 return;
04424
04425 ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
04426 stringp = multi;
04427 while ((ext = strsep(&stringp, "&"))) {
04428 if ((context = strchr(ext, '@'))) {
04429 *context++ = '\0';
04430 if (!ast_context_find(context)) {
04431 ast_log(LOG_WARNING, "Context %s must exist in regcontext= in sip.conf!\n", context);
04432 continue;
04433 }
04434 } else {
04435 context = sip_cfg.regcontext;
04436 }
04437 if (onoff) {
04438 if (!ast_exists_extension(NULL, context, ext, 1, NULL)) {
04439 ast_add_extension(context, 1, ext, 1, NULL, NULL, "Noop",
04440 ast_strdup(peer->name), ast_free_ptr, "SIP");
04441 }
04442 } else if (pbx_find_extension(NULL, NULL, &q, context, ext, 1, NULL, "", E_MATCH)) {
04443 ast_context_remove_extension(context, ext, 1, NULL);
04444 }
04445 }
04446 }
04447
04448
04449 static void destroy_mailbox(struct sip_mailbox *mailbox)
04450 {
04451 if (mailbox->event_sub)
04452 ast_event_unsubscribe(mailbox->event_sub);
04453 ast_free(mailbox);
04454 }
04455
04456
04457 static void clear_peer_mailboxes(struct sip_peer *peer)
04458 {
04459 struct sip_mailbox *mailbox;
04460
04461 while ((mailbox = AST_LIST_REMOVE_HEAD(&peer->mailboxes, entry)))
04462 destroy_mailbox(mailbox);
04463 }
04464
04465 static void sip_destroy_peer_fn(void *peer)
04466 {
04467 sip_destroy_peer(peer);
04468 }
04469
04470
04471 static void sip_destroy_peer(struct sip_peer *peer)
04472 {
04473 ast_debug(3, "Destroying SIP peer %s\n", peer->name);
04474 if (peer->outboundproxy)
04475 ao2_ref(peer->outboundproxy, -1);
04476 peer->outboundproxy = NULL;
04477
04478
04479 if (peer->call) {
04480 dialog_unlink_all(peer->call, TRUE, TRUE);
04481 peer->call = dialog_unref(peer->call, "peer->call is being unset");
04482 }
04483
04484
04485 if (peer->mwipvt) {
04486 dialog_unlink_all(peer->mwipvt, TRUE, TRUE);
04487 peer->mwipvt = dialog_unref(peer->mwipvt, "unreffing peer->mwipvt");
04488 }
04489
04490 if (peer->chanvars) {
04491 ast_variables_destroy(peer->chanvars);
04492 peer->chanvars = NULL;
04493 }
04494
04495 register_peer_exten(peer, FALSE);
04496 ast_free_ha(peer->ha);
04497 ast_free_ha(peer->directmediaha);
04498 if (peer->selfdestruct)
04499 ast_atomic_fetchadd_int(&apeerobjs, -1);
04500 else if (peer->is_realtime) {
04501 ast_atomic_fetchadd_int(&rpeerobjs, -1);
04502 ast_debug(3, "-REALTIME- peer Destroyed. Name: %s. Realtime Peer objects: %d\n", peer->name, rpeerobjs);
04503 } else
04504 ast_atomic_fetchadd_int(&speerobjs, -1);
04505 clear_realm_authentication(peer->auth);
04506 peer->auth = NULL;
04507 if (peer->dnsmgr)
04508 ast_dnsmgr_release(peer->dnsmgr);
04509 clear_peer_mailboxes(peer);
04510
04511 if (peer->socket.tcptls_session) {
04512 ao2_ref(peer->socket.tcptls_session, -1);
04513 peer->socket.tcptls_session = NULL;
04514 }
04515
04516 ast_cc_config_params_destroy(peer->cc_params);
04517
04518 ast_string_field_free_memory(peer);
04519 }
04520
04521
04522 static void update_peer(struct sip_peer *p, int expire)
04523 {
04524 int rtcachefriends = ast_test_flag(&p->flags[1], SIP_PAGE2_RTCACHEFRIENDS);
04525 if (sip_cfg.peer_rtupdate &&
04526 (p->is_realtime || rtcachefriends)) {
04527 realtime_update_peer(p->name, &p->addr, p->username, rtcachefriends ? p->fullcontact : NULL, p->useragent, expire, p->deprecated_username, p->lastms);
04528 }
04529 }
04530
04531 static struct ast_variable *get_insecure_variable_from_config(struct ast_config *cfg)
04532 {
04533 struct ast_variable *var = NULL;
04534 struct ast_flags flags = {0};
04535 char *cat = NULL;
04536 const char *insecure;
04537 while ((cat = ast_category_browse(cfg, cat))) {
04538 insecure = ast_variable_retrieve(cfg, cat, "insecure");
04539 set_insecure_flags(&flags, insecure, -1);
04540 if (ast_test_flag(&flags, SIP_INSECURE_PORT)) {
04541 var = ast_category_root(cfg, cat);
04542 break;
04543 }
04544 }
04545 return var;
04546 }
04547
04548 static const char *get_name_from_variable(struct ast_variable *var, const char *newpeername)
04549 {
04550 struct ast_variable *tmp;
04551 for (tmp = var; tmp; tmp = tmp->next) {
04552 if (!newpeername && !strcasecmp(tmp->name, "name"))
04553 newpeername = tmp->value;
04554 }
04555 return newpeername;
04556 }
04557
04558
04559
04560
04561
04562
04563
04564 static struct sip_peer *realtime_peer(const char *newpeername, struct ast_sockaddr *addr, int devstate_only, int which_objects)
04565 {
04566 struct sip_peer *peer;
04567 struct ast_variable *var = NULL;
04568 struct ast_variable *varregs = NULL;
04569 struct ast_variable *tmp;
04570 struct ast_config *peerlist = NULL;
04571 char ipaddr[INET6_ADDRSTRLEN];
04572 char portstring[6];
04573 char *cat = NULL;
04574 int realtimeregs = ast_check_realtime("sipregs");
04575
04576
04577 if (newpeername) {
04578 if (realtimeregs)
04579 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04580
04581 var = ast_load_realtime("sippeers", "name", newpeername, "host", "dynamic", SENTINEL);
04582 if (!var && addr) {
04583 var = ast_load_realtime("sippeers", "name", newpeername, "host", ast_sockaddr_stringify_addr(addr), SENTINEL);
04584 }
04585 if (!var) {
04586 var = ast_load_realtime("sippeers", "name", newpeername, SENTINEL);
04587
04588
04589
04590
04591
04592
04593 if (var && addr) {
04594 for (tmp = var; tmp; tmp = tmp->next) {
04595 if (!strcasecmp(tmp->name, "host")) {
04596 struct ast_sockaddr *addrs = NULL;
04597
04598 if (ast_sockaddr_resolve(&addrs,
04599 tmp->value,
04600 PARSE_PORT_FORBID,
04601 get_address_family_filter(&bindaddr)) <= 0 ||
04602 ast_sockaddr_cmp(&addrs[0], addr)) {
04603
04604 ast_variables_destroy(var);
04605 var = NULL;
04606 }
04607 ast_free(addrs);
04608 break;
04609 }
04610 }
04611 }
04612 }
04613 }
04614
04615 if (!var && addr) {
04616 ast_copy_string(ipaddr, ast_sockaddr_stringify_addr(addr), sizeof(ipaddr));
04617 ast_copy_string(portstring, ast_sockaddr_stringify_port(addr), sizeof(portstring));
04618 var = ast_load_realtime("sippeers", "host", ipaddr, "port", portstring, SENTINEL);
04619 if (var) {
04620 if (realtimeregs) {
04621 newpeername = get_name_from_variable(var, newpeername);
04622 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04623 }
04624 } else {
04625 if (realtimeregs)
04626 varregs = ast_load_realtime("sipregs", "ipaddr", ipaddr, "port", portstring, SENTINEL);
04627 else
04628 var = ast_load_realtime("sippeers", "ipaddr", ipaddr, "port", portstring, SENTINEL);
04629 if (varregs) {
04630 newpeername = get_name_from_variable(varregs, newpeername);
04631 var = ast_load_realtime("sippeers", "name", newpeername, SENTINEL);
04632 }
04633 }
04634 if (!var) {
04635 peerlist = ast_load_realtime_multientry("sippeers", "host", ipaddr, SENTINEL);
04636 if (peerlist) {
04637 var = get_insecure_variable_from_config(peerlist);
04638 if(var) {
04639 if (realtimeregs) {
04640 newpeername = get_name_from_variable(var, newpeername);
04641 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04642 }
04643 } else {
04644 peerlist = NULL;
04645 cat = NULL;
04646 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", ipaddr, SENTINEL);
04647 if(peerlist) {
04648 var = get_insecure_variable_from_config(peerlist);
04649 if(var) {
04650 if (realtimeregs) {
04651 newpeername = get_name_from_variable(var, newpeername);
04652 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04653 }
04654 }
04655 }
04656 }
04657 } else {
04658 if (realtimeregs) {
04659 peerlist = ast_load_realtime_multientry("sipregs", "ipaddr", ipaddr, SENTINEL);
04660 if (peerlist) {
04661 varregs = get_insecure_variable_from_config(peerlist);
04662 if (varregs) {
04663 newpeername = get_name_from_variable(varregs, newpeername);
04664 var = ast_load_realtime("sippeers", "name", newpeername, SENTINEL);
04665 }
04666 }
04667 } else {
04668 peerlist = ast_load_realtime_multientry("sippeers", "ipaddr", ipaddr, SENTINEL);
04669 if (peerlist) {
04670 var = get_insecure_variable_from_config(peerlist);
04671 if (var) {
04672 newpeername = get_name_from_variable(var, newpeername);
04673 varregs = ast_load_realtime("sipregs", "name", newpeername, SENTINEL);
04674 }
04675 }
04676 }
04677 }
04678 }
04679 }
04680
04681 if (!var) {
04682 if (peerlist)
04683 ast_config_destroy(peerlist);
04684 return NULL;
04685 }
04686
04687 for (tmp = var; tmp; tmp = tmp->next) {
04688 if (!strcasecmp(tmp->name, "type") && (!strcasecmp(tmp->value, "peer") && which_objects == FINDUSERS)) {
04689 if (peerlist) {
04690 ast_config_destroy(peerlist);
04691 } else {
04692 ast_variables_destroy(var);
04693 ast_variables_destroy(varregs);
04694 }
04695 return NULL;
04696 } else if (!newpeername && !strcasecmp(tmp->name, "name")) {
04697 newpeername = tmp->value;
04698 }
04699 }
04700
04701 if (!newpeername) {
04702 ast_log(LOG_WARNING, "Cannot Determine peer name ip=%s\n", ipaddr);
04703 if(peerlist)
04704 ast_config_destroy(peerlist);
04705 else
04706 ast_variables_destroy(var);
04707 return NULL;
04708 }
04709
04710
04711
04712 peer = build_peer(newpeername, var, varregs, TRUE, devstate_only);
04713 if (!peer) {
04714 if(peerlist)
04715 ast_config_destroy(peerlist);
04716 else {
04717 ast_variables_destroy(var);
04718 ast_variables_destroy(varregs);
04719 }
04720 return NULL;
04721 }
04722
04723 ast_debug(3, "-REALTIME- loading peer from database to memory. Name: %s. Peer objects: %d\n", peer->name, rpeerobjs);
04724
04725 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && !devstate_only) {
04726
04727 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
04728 if (ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
04729 AST_SCHED_REPLACE_UNREF(peer->expire, sched, sip_cfg.rtautoclear * 1000, expire_register, peer,
04730 unref_peer(_data, "remove registration ref"),
04731 unref_peer(peer, "remove registration ref"),
04732 ref_peer(peer, "add registration ref"));
04733 }
04734 ao2_t_link(peers, peer, "link peer into peers table");
04735 if (!ast_sockaddr_isnull(&peer->addr)) {
04736 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
04737 }
04738 }
04739 peer->is_realtime = 1;
04740 if (peerlist)
04741 ast_config_destroy(peerlist);
04742 else {
04743 ast_variables_destroy(var);
04744 ast_variables_destroy(varregs);
04745 }
04746
04747 return peer;
04748 }
04749
04750
04751 static int find_by_name(void *obj, void *arg, void *data, int flags)
04752 {
04753 struct sip_peer *search = obj, *match = arg;
04754 int *which_objects = data;
04755
04756
04757 if (strcmp(search->name, match->name)) {
04758 return 0;
04759 }
04760
04761 switch (*which_objects) {
04762 case FINDUSERS:
04763 if (!(search->type & SIP_TYPE_USER)) {
04764 return 0;
04765 }
04766 break;
04767 case FINDPEERS:
04768 if (!(search->type & SIP_TYPE_PEER)) {
04769 return 0;
04770 }
04771 break;
04772 case FINDALLDEVICES:
04773 break;
04774 }
04775
04776 return CMP_MATCH | CMP_STOP;
04777 }
04778
04779
04780
04781
04782
04783
04784
04785
04786
04787
04788
04789
04790
04791
04792 static struct sip_peer *find_peer(const char *peer, struct ast_sockaddr *addr, int realtime, int which_objects, int devstate_only, int transport)
04793 {
04794 struct sip_peer *p = NULL;
04795 struct sip_peer tmp_peer;
04796
04797 if (peer) {
04798 ast_copy_string(tmp_peer.name, peer, sizeof(tmp_peer.name));
04799 p = ao2_t_callback_data(peers, OBJ_POINTER, find_by_name, &tmp_peer, &which_objects, "ao2_find in peers table");
04800 } else if (addr) {
04801 ast_sockaddr_copy(&tmp_peer.addr, addr);
04802 tmp_peer.flags[0].flags = 0;
04803 tmp_peer.transports = transport;
04804 p = ao2_t_find(peers_by_ip, &tmp_peer, OBJ_POINTER, "ao2_find in peers_by_ip table");
04805 if (!p) {
04806 ast_set_flag(&tmp_peer.flags[0], SIP_INSECURE_PORT);
04807 p = ao2_t_find(peers_by_ip, &tmp_peer, OBJ_POINTER, "ao2_find in peers_by_ip table 2");
04808 if (p) {
04809 return p;
04810 }
04811 }
04812 }
04813
04814 if (!p && (realtime || devstate_only)) {
04815 p = realtime_peer(peer, addr, devstate_only, which_objects);
04816 if (p) {
04817 switch (which_objects) {
04818 case FINDUSERS:
04819 if (!(p->type & SIP_TYPE_USER)) {
04820 unref_peer(p, "Wrong type of realtime SIP endpoint");
04821 return NULL;
04822 }
04823 break;
04824 case FINDPEERS:
04825 if (!(p->type & SIP_TYPE_PEER)) {
04826 unref_peer(p, "Wrong type of realtime SIP endpoint");
04827 return NULL;
04828 }
04829 break;
04830 case FINDALLDEVICES:
04831 break;
04832 }
04833 }
04834 }
04835
04836 return p;
04837 }
04838
04839
04840 static void do_setnat(struct sip_pvt *p)
04841 {
04842 const char *mode;
04843 int natflags;
04844
04845 natflags = ast_test_flag(&p->flags[1], SIP_PAGE2_SYMMETRICRTP);
04846 mode = natflags ? "On" : "Off";
04847
04848 if (p->rtp) {
04849 ast_debug(1, "Setting NAT on RTP to %s\n", mode);
04850 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_NAT, natflags);
04851 }
04852 if (p->vrtp) {
04853 ast_debug(1, "Setting NAT on VRTP to %s\n", mode);
04854 ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_NAT, natflags);
04855 }
04856 if (p->udptl) {
04857 ast_debug(1, "Setting NAT on UDPTL to %s\n", mode);
04858 ast_udptl_setnat(p->udptl, natflags);
04859 }
04860 if (p->trtp) {
04861 ast_debug(1, "Setting NAT on TRTP to %s\n", mode);
04862 ast_rtp_instance_set_prop(p->trtp, AST_RTP_PROPERTY_NAT, natflags);
04863 }
04864 }
04865
04866
04867 static void change_t38_state(struct sip_pvt *p, int state)
04868 {
04869 int old = p->t38.state;
04870 struct ast_channel *chan = p->owner;
04871 struct ast_control_t38_parameters parameters = { .request_response = 0 };
04872
04873
04874 if (old == state)
04875 return;
04876
04877 p->t38.state = state;
04878 ast_debug(2, "T38 state changed to %d on channel %s\n", p->t38.state, chan ? chan->name : "<none>");
04879
04880
04881 if (!chan)
04882 return;
04883
04884
04885 switch (state) {
04886 case T38_PEER_REINVITE:
04887 parameters = p->t38.their_parms;
04888 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
04889 parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
04890 ast_udptl_set_tag(p->udptl, "SIP/%s", p->username);
04891 break;
04892 case T38_ENABLED:
04893 parameters = p->t38.their_parms;
04894 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
04895 parameters.request_response = AST_T38_NEGOTIATED;
04896 ast_udptl_set_tag(p->udptl, "SIP/%s", p->username);
04897 break;
04898 case T38_DISABLED:
04899 if (old == T38_ENABLED) {
04900 parameters.request_response = AST_T38_TERMINATED;
04901 } else if (old == T38_LOCAL_REINVITE) {
04902 parameters.request_response = AST_T38_REFUSED;
04903 }
04904 break;
04905 case T38_LOCAL_REINVITE:
04906
04907 break;
04908 }
04909
04910
04911 if (parameters.request_response)
04912 ast_queue_control_data(chan, AST_CONTROL_T38_PARAMETERS, ¶meters, sizeof(parameters));
04913 }
04914
04915
04916 static void set_t38_capabilities(struct sip_pvt *p)
04917 {
04918 if (p->udptl) {
04919 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY) {
04920 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
04921 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL_FEC) {
04922 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
04923 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) == SIP_PAGE2_T38SUPPORT_UDPTL) {
04924 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
04925 }
04926 }
04927 }
04928
04929 static void copy_socket_data(struct sip_socket *to_sock, const struct sip_socket *from_sock)
04930 {
04931 if (to_sock->tcptls_session) {
04932 ao2_ref(to_sock->tcptls_session, -1);
04933 to_sock->tcptls_session = NULL;
04934 }
04935
04936 if (from_sock->tcptls_session) {
04937 ao2_ref(from_sock->tcptls_session, +1);
04938 }
04939
04940 *to_sock = *from_sock;
04941 }
04942
04943
04944
04945
04946 static int dialog_initialize_rtp(struct sip_pvt *dialog)
04947 {
04948 struct ast_sockaddr bindaddr_tmp;
04949
04950 if (!sip_methods[dialog->method].need_rtp) {
04951 return 0;
04952 }
04953
04954 ast_sockaddr_copy(&bindaddr_tmp, &bindaddr);
04955 if (!(dialog->rtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
04956 return -1;
04957 }
04958
04959 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS) ||
04960 (ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) && (dialog->capability & AST_FORMAT_VIDEO_MASK))) {
04961 if (!(dialog->vrtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
04962 return -1;
04963 }
04964 ast_rtp_instance_set_timeout(dialog->vrtp, global_rtptimeout);
04965 ast_rtp_instance_set_hold_timeout(dialog->vrtp, global_rtpholdtimeout);
04966
04967 ast_rtp_instance_set_prop(dialog->vrtp, AST_RTP_PROPERTY_RTCP, 1);
04968 }
04969
04970 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_TEXTSUPPORT)) {
04971 if (!(dialog->trtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
04972 return -1;
04973 }
04974 ast_rtp_instance_set_timeout(dialog->trtp, global_rtptimeout);
04975 ast_rtp_instance_set_hold_timeout(dialog->trtp, global_rtpholdtimeout);
04976
04977 ast_rtp_instance_set_prop(dialog->trtp, AST_RTP_PROPERTY_RTCP, 1);
04978 }
04979
04980 ast_rtp_instance_set_timeout(dialog->rtp, global_rtptimeout);
04981 ast_rtp_instance_set_hold_timeout(dialog->rtp, global_rtpholdtimeout);
04982
04983 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_RTCP, 1);
04984 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
04985 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
04986
04987 ast_rtp_instance_set_qos(dialog->rtp, global_tos_audio, global_cos_audio, "SIP RTP");
04988
04989 do_setnat(dialog);
04990
04991 return 0;
04992 }
04993
04994
04995
04996
04997
04998
04999
05000
05001 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
05002 {
05003
05004
05005
05006 if (dialog->socket.type && check_request_transport(peer, dialog))
05007 return -1;
05008 copy_socket_data(&dialog->socket, &peer->socket);
05009
05010 if (!(ast_sockaddr_isnull(&peer->addr) && ast_sockaddr_isnull(&peer->defaddr)) &&
05011 (!peer->maxms || ((peer->lastms >= 0) && (peer->lastms <= peer->maxms)))) {
05012 dialog->sa = ast_sockaddr_isnull(&peer->addr) ? peer->defaddr : peer->addr;
05013 dialog->recv = dialog->sa;
05014 } else
05015 return -1;
05016
05017
05018 ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
05019 ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
05020 ast_copy_flags(&dialog->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
05021 dialog->capability = peer->capability;
05022 dialog->prefs = peer->prefs;
05023 if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
05024
05025 if (dialog->udptl || (!dialog->udptl && (dialog->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &bindaddr)))) {
05026 dialog->t38_maxdatagram = peer->t38_maxdatagram;
05027 set_t38_capabilities(dialog);
05028 } else {
05029
05030 ast_debug(1, "UDPTL creation failed on dialog.\n");
05031 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT);
05032 }
05033 } else if (dialog->udptl) {
05034 ast_udptl_destroy(dialog->udptl);
05035 dialog->udptl = NULL;
05036 }
05037
05038 ast_string_field_set(dialog, engine, peer->engine);
05039
05040 if (dialog_initialize_rtp(dialog)) {
05041 return -1;
05042 }
05043
05044 if (dialog->rtp) {
05045 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
05046 ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
05047 ast_rtp_instance_set_timeout(dialog->rtp, peer->rtptimeout);
05048 ast_rtp_instance_set_hold_timeout(dialog->rtp, peer->rtpholdtimeout);
05049
05050 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(dialog->rtp), dialog->rtp, &dialog->prefs);
05051 dialog->autoframing = peer->autoframing;
05052 }
05053 if (dialog->vrtp) {
05054 ast_rtp_instance_set_timeout(dialog->vrtp, peer->rtptimeout);
05055 ast_rtp_instance_set_hold_timeout(dialog->vrtp, peer->rtpholdtimeout);
05056 }
05057 if (dialog->trtp) {
05058 ast_rtp_instance_set_timeout(dialog->trtp, peer->rtptimeout);
05059 ast_rtp_instance_set_hold_timeout(dialog->trtp, peer->rtpholdtimeout);
05060 }
05061
05062
05063 ast_string_field_set(dialog, peername, peer->name);
05064 ast_string_field_set(dialog, authname, peer->username);
05065 ast_string_field_set(dialog, username, peer->username);
05066 ast_string_field_set(dialog, peersecret, peer->secret);
05067 ast_string_field_set(dialog, peermd5secret, peer->md5secret);
05068 ast_string_field_set(dialog, mohsuggest, peer->mohsuggest);
05069 ast_string_field_set(dialog, mohinterpret, peer->mohinterpret);
05070 ast_string_field_set(dialog, tohost, peer->tohost);
05071 ast_string_field_set(dialog, fullcontact, peer->fullcontact);
05072 ast_string_field_set(dialog, accountcode, peer->accountcode);
05073 ast_string_field_set(dialog, context, peer->context);
05074 ast_string_field_set(dialog, cid_num, peer->cid_num);
05075 ast_string_field_set(dialog, cid_name, peer->cid_name);
05076 ast_string_field_set(dialog, cid_tag, peer->cid_tag);
05077 ast_string_field_set(dialog, mwi_from, peer->mwi_from);
05078 if (!ast_strlen_zero(peer->parkinglot)) {
05079 ast_string_field_set(dialog, parkinglot, peer->parkinglot);
05080 }
05081 ast_string_field_set(dialog, engine, peer->engine);
05082 ref_proxy(dialog, obproxy_get(dialog, peer));
05083 dialog->callgroup = peer->callgroup;
05084 dialog->pickupgroup = peer->pickupgroup;
05085 dialog->allowtransfer = peer->allowtransfer;
05086 dialog->jointnoncodeccapability = dialog->noncodeccapability;
05087 dialog->rtptimeout = peer->rtptimeout;
05088 dialog->peerauth = peer->auth;
05089 dialog->maxcallbitrate = peer->maxcallbitrate;
05090 dialog->disallowed_methods = peer->disallowed_methods;
05091 ast_cc_copy_config_params(dialog->cc_params, peer->cc_params);
05092 if (ast_strlen_zero(dialog->tohost))
05093 ast_string_field_set(dialog, tohost, ast_sockaddr_stringify_host(&dialog->sa));
05094 if (!ast_strlen_zero(peer->fromdomain)) {
05095 ast_string_field_set(dialog, fromdomain, peer->fromdomain);
05096 if (!dialog->initreq.headers) {
05097 char *c;
05098 char *tmpcall = ast_strdupa(dialog->callid);
05099
05100 c = strchr(tmpcall, '@');
05101 if (c) {
05102 *c = '\0';
05103 ao2_t_unlink(dialogs, dialog, "About to change the callid -- remove the old name");
05104 ast_string_field_build(dialog, callid, "%s@%s", tmpcall, peer->fromdomain);
05105 ao2_t_link(dialogs, dialog, "New dialog callid -- inserted back into table");
05106 }
05107 }
05108 }
05109 if (!ast_strlen_zero(peer->fromuser))
05110 ast_string_field_set(dialog, fromuser, peer->fromuser);
05111 if (!ast_strlen_zero(peer->language))
05112 ast_string_field_set(dialog, language, peer->language);
05113
05114
05115
05116
05117 if (peer->maxms && peer->lastms)
05118 dialog->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
05119 else
05120 dialog->timer_t1 = peer->timer_t1;
05121
05122
05123
05124 if (peer->timer_b)
05125 dialog->timer_b = peer->timer_b;
05126 else
05127 dialog->timer_b = 64 * dialog->timer_t1;
05128
05129 if ((ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
05130 (ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
05131 dialog->noncodeccapability |= AST_RTP_DTMF;
05132 else
05133 dialog->noncodeccapability &= ~AST_RTP_DTMF;
05134 dialog->directmediaha = ast_duplicate_ha_list(peer->directmediaha);
05135 if (peer->call_limit)
05136 ast_set_flag(&dialog->flags[0], SIP_CALL_LIMIT);
05137 if (!dialog->portinuri)
05138 dialog->portinuri = peer->portinuri;
05139 dialog->chanvars = copy_vars(peer->chanvars);
05140 if (peer->fromdomainport)
05141 dialog->fromdomainport = peer->fromdomainport;
05142
05143 return 0;
05144 }
05145
05146
05147
05148
05149 static int create_addr(struct sip_pvt *dialog, const char *opeer, struct ast_sockaddr *addr, int newdialog, struct ast_sockaddr *remote_address)
05150 {
05151 struct sip_peer *peer;
05152 char *peername, *peername2, *hostn;
05153 char host[MAXHOSTNAMELEN];
05154 char service[MAXHOSTNAMELEN];
05155 int srv_ret = 0;
05156 int tportno;
05157
05158 AST_DECLARE_APP_ARGS(hostport,
05159 AST_APP_ARG(host);
05160 AST_APP_ARG(port);
05161 );
05162
05163 peername = ast_strdupa(opeer);
05164 peername2 = ast_strdupa(opeer);
05165 AST_NONSTANDARD_RAW_ARGS(hostport, peername2, ':');
05166
05167 if (hostport.port)
05168 dialog->portinuri = 1;
05169
05170 dialog->timer_t1 = global_t1;
05171 dialog->timer_b = global_timer_b;
05172 peer = find_peer(peername, NULL, TRUE, FINDPEERS, FALSE, 0);
05173
05174 if (peer) {
05175 int res;
05176 if (newdialog) {
05177 set_socket_transport(&dialog->socket, 0);
05178 }
05179 res = create_addr_from_peer(dialog, peer);
05180 if (!ast_sockaddr_isnull(remote_address)) {
05181 ast_sockaddr_copy(&dialog->sa, remote_address);
05182 }
05183 dialog->relatedpeer = ref_peer(peer, "create_addr: setting dialog's relatedpeer pointer");
05184 unref_peer(peer, "create_addr: unref peer from find_peer hashtab lookup");
05185 return res;
05186 }
05187
05188 if (dialog_initialize_rtp(dialog)) {
05189 return -1;
05190 }
05191
05192 ast_string_field_set(dialog, tohost, hostport.host);
05193 dialog->allowed_methods &= ~sip_cfg.disallowed_methods;
05194
05195
05196 ref_proxy(dialog, obproxy_get(dialog, NULL));
05197
05198 if (addr) {
05199
05200 ast_sockaddr_copy(&dialog->sa, addr);
05201 } else {
05202
05203
05204
05205
05206
05207
05208 hostn = peername;
05209
05210
05211
05212 if (!hostport.port && sip_cfg.srvlookup) {
05213 snprintf(service, sizeof(service), "_%s._%s.%s",
05214 get_srv_service(dialog->socket.type),
05215 get_srv_protocol(dialog->socket.type), peername);
05216 if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno,
05217 service)) > 0) {
05218 hostn = host;
05219 }
05220 }
05221
05222 if (ast_sockaddr_resolve_first(&dialog->sa, hostn, 0)) {
05223 ast_log(LOG_WARNING, "No such host: %s\n", peername);
05224 }
05225
05226 if (srv_ret > 0) {
05227 ast_sockaddr_set_port(&dialog->sa, tportno);
05228 }
05229 }
05230
05231 if (!dialog->socket.type)
05232 set_socket_transport(&dialog->socket, SIP_TRANSPORT_UDP);
05233 if (!dialog->socket.port) {
05234 dialog->socket.port = htons(ast_sockaddr_port(&bindaddr));
05235 }
05236
05237 if (!ast_sockaddr_port(&dialog->sa)) {
05238 ast_sockaddr_set_port(&dialog->sa,
05239 (dialog->socket.type == SIP_TRANSPORT_TLS) ?
05240 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
05241 }
05242 ast_sockaddr_copy(&dialog->recv, &dialog->sa);
05243 return 0;
05244 }
05245
05246
05247
05248
05249 static int auto_congest(const void *arg)
05250 {
05251 struct sip_pvt *p = (struct sip_pvt *)arg;
05252
05253 sip_pvt_lock(p);
05254 p->initid = -1;
05255 if (p->owner) {
05256
05257 if (!ast_channel_trylock(p->owner)) {
05258 append_history(p, "Cong", "Auto-congesting (timer)");
05259 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
05260 ast_channel_unlock(p->owner);
05261 }
05262
05263
05264 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05265 }
05266 sip_pvt_unlock(p);
05267 dialog_unref(p, "unreffing arg passed into auto_congest callback (p->initid)");
05268 return 0;
05269 }
05270
05271
05272
05273
05274 static int sip_call(struct ast_channel *ast, char *dest, int timeout)
05275 {
05276 int res;
05277 struct sip_pvt *p = ast->tech_pvt;
05278 struct varshead *headp;
05279 struct ast_var_t *current;
05280 const char *referer = NULL;
05281 int cc_core_id;
05282 char uri[SIPBUFSIZE] = "";
05283
05284 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
05285 ast_log(LOG_WARNING, "sip_call called on %s, neither down nor reserved\n", ast->name);
05286 return -1;
05287 }
05288
05289 if (ast_cc_is_recall(ast, &cc_core_id, "SIP")) {
05290 char device_name[AST_CHANNEL_NAME];
05291 struct ast_cc_monitor *recall_monitor;
05292 struct sip_monitor_instance *monitor_instance;
05293 ast_channel_get_device_name(ast, device_name, sizeof(device_name));
05294 if ((recall_monitor = ast_cc_get_monitor_by_recall_core_id(cc_core_id, device_name))) {
05295 monitor_instance = recall_monitor->private_data;
05296 ast_copy_string(uri, monitor_instance->notify_uri, sizeof(uri));
05297 ao2_t_ref(recall_monitor, -1, "Got the URI we need so unreffing monitor");
05298 }
05299 }
05300
05301
05302 headp=&ast->varshead;
05303 AST_LIST_TRAVERSE(headp, current, entries) {
05304
05305 if (!p->options->vxml_url && !strcasecmp(ast_var_name(current), "VXML_URL")) {
05306 p->options->vxml_url = ast_var_value(current);
05307 } else if (!p->options->uri_options && !strcasecmp(ast_var_name(current), "SIP_URI_OPTIONS")) {
05308 p->options->uri_options = ast_var_value(current);
05309 } else if (!p->options->addsipheaders && !strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
05310
05311 p->options->addsipheaders = 1;
05312 } else if (!strcasecmp(ast_var_name(current), "SIPFROMDOMAIN")) {
05313 ast_string_field_set(p, fromdomain, ast_var_value(current));
05314 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER")) {
05315
05316 p->options->transfer = 1;
05317 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REFERER")) {
05318
05319 referer = ast_var_value(current);
05320 } else if (!strcasecmp(ast_var_name(current), "SIPTRANSFER_REPLACES")) {
05321
05322 p->options->replaces = ast_var_value(current);
05323 } else if (!strcasecmp(ast_var_name(current), "SIP_MAX_FORWARDS")) {
05324 if (sscanf(ast_var_value(current), "%d", &(p->maxforwards)) != 1) {
05325 ast_log(LOG_WARNING, "The SIP_MAX_FORWARDS channel variable is not a valid integer.");
05326 }
05327 }
05328 }
05329
05330
05331 if (p->req_secure_signaling && p->socket.type != SIP_TRANSPORT_TLS) {
05332 ast_log(LOG_WARNING, "Encrypted signaling is required\n");
05333 ast->hangupcause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05334 return -1;
05335 }
05336
05337 if (ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP)) {
05338 if (ast_test_flag(&p->flags[0], SIP_REINVITE)) {
05339 ast_debug(1, "Direct media not possible when using SRTP, ignoring canreinvite setting\n");
05340 ast_clear_flag(&p->flags[0], SIP_REINVITE);
05341 }
05342
05343 if (p->rtp && !p->srtp && setup_srtp(&p->srtp) < 0) {
05344 ast_log(LOG_WARNING, "SRTP audio setup failed\n");
05345 return -1;
05346 }
05347
05348 if (p->vrtp && !p->vsrtp && setup_srtp(&p->vsrtp) < 0) {
05349 ast_log(LOG_WARNING, "SRTP video setup failed\n");
05350 return -1;
05351 }
05352
05353 if (p->trtp && !p->vsrtp && setup_srtp(&p->tsrtp) < 0) {
05354 ast_log(LOG_WARNING, "SRTP text setup failed\n");
05355 return -1;
05356 }
05357 }
05358
05359 res = 0;
05360 ast_set_flag(&p->flags[0], SIP_OUTGOING);
05361
05362
05363
05364
05365 ast_clear_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_T38);
05366
05367 if (p->options->transfer) {
05368 char buf[SIPBUFSIZE/2];
05369
05370 if (referer) {
05371 if (sipdebug)
05372 ast_debug(3, "Call for %s transfered by %s\n", p->username, referer);
05373 snprintf(buf, sizeof(buf)-1, "-> %s (via %s)", p->cid_name, referer);
05374 } else
05375 snprintf(buf, sizeof(buf)-1, "-> %s", p->cid_name);
05376 ast_string_field_set(p, cid_name, buf);
05377 }
05378 ast_debug(1, "Outgoing Call for %s\n", p->username);
05379
05380 res = update_call_counter(p, INC_CALL_RINGING);
05381
05382 if (res == -1) {
05383 ast->hangupcause = AST_CAUSE_USER_BUSY;
05384 return res;
05385 }
05386 p->callingpres = ast_party_id_presentation(&ast->caller.id);
05387 p->jointcapability = ast_rtp_instance_available_formats(p->rtp, p->capability, p->prefcodec);
05388 p->jointnoncodeccapability = p->noncodeccapability;
05389
05390
05391 if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
05392 ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
05393 res = -1;
05394 } else {
05395 int xmitres;
05396
05397 sip_pvt_lock(p);
05398 xmitres = transmit_invite(p, SIP_INVITE, 1, 2, uri);
05399 sip_pvt_unlock(p);
05400 if (xmitres == XMIT_ERROR)
05401 return -1;
05402 p->invitestate = INV_CALLING;
05403
05404
05405 AST_SCHED_REPLACE_UNREF(p->initid, sched, p->timer_b, auto_congest, p,
05406 dialog_unref(_data, "dialog ptr dec when SCHED_REPLACE del op succeeded"),
05407 dialog_unref(p, "dialog ptr dec when SCHED_REPLACE add failed"),
05408 dialog_ref(p, "dialog ptr inc when SCHED_REPLACE add succeeded") );
05409 }
05410 return res;
05411 }
05412
05413
05414
05415 static void sip_registry_destroy(struct sip_registry *reg)
05416 {
05417
05418 ast_debug(3, "Destroying registry entry for %s@%s\n", reg->username, reg->hostname);
05419
05420 if (reg->call) {
05421
05422
05423 reg->call->registry = registry_unref(reg->call->registry, "destroy reg->call->registry");
05424 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
05425 dialog_unlink_all(reg->call, TRUE, TRUE);
05426 reg->call = dialog_unref(reg->call, "unref reg->call");
05427
05428 }
05429 AST_SCHED_DEL(sched, reg->expire);
05430 AST_SCHED_DEL(sched, reg->timeout);
05431
05432 ast_string_field_free_memory(reg);
05433 ast_atomic_fetchadd_int(®objs, -1);
05434 ast_dnsmgr_release(reg->dnsmgr);
05435 ast_free(reg);
05436 }
05437
05438
05439 static void sip_subscribe_mwi_destroy(struct sip_subscription_mwi *mwi)
05440 {
05441 if (mwi->call) {
05442 mwi->call->mwi = NULL;
05443 sip_destroy(mwi->call);
05444 }
05445
05446 AST_SCHED_DEL(sched, mwi->resub);
05447 ast_string_field_free_memory(mwi);
05448 ast_dnsmgr_release(mwi->dnsmgr);
05449 ast_free(mwi);
05450 }
05451
05452
05453 void __sip_destroy(struct sip_pvt *p, int lockowner, int lockdialoglist)
05454 {
05455 struct sip_request *req;
05456
05457 if (p->stimer) {
05458 ast_free(p->stimer);
05459 p->stimer = NULL;
05460 }
05461
05462 if (sip_debug_test_pvt(p))
05463 ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
05464
05465 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05466 update_call_counter(p, DEC_CALL_LIMIT);
05467 ast_debug(2, "This call did not properly clean up call limits. Call ID %s\n", p->callid);
05468 }
05469
05470
05471 if (p->owner) {
05472 if (lockowner)
05473 ast_channel_lock(p->owner);
05474 ast_debug(1, "Detaching from %s\n", p->owner->name);
05475 p->owner->tech_pvt = NULL;
05476
05477 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05478 if (lockowner)
05479 ast_channel_unlock(p->owner);
05480
05481 usleep(1);
05482 }
05483
05484
05485 if (p->relatedpeer && p->relatedpeer->mwipvt)
05486 p->relatedpeer->mwipvt = dialog_unref(p->relatedpeer->mwipvt, "delete ->relatedpeer->mwipvt");
05487 if (p->relatedpeer && p->relatedpeer->call == p)
05488 p->relatedpeer->call = dialog_unref(p->relatedpeer->call, "unset the relatedpeer->call field in tandem with relatedpeer field itself");
05489
05490 if (p->relatedpeer)
05491 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting a dialog relatedpeer field in sip_destroy");
05492
05493 if (p->registry) {
05494 if (p->registry->call == p)
05495 p->registry->call = dialog_unref(p->registry->call, "nulling out the registry's call dialog field in unlink_all");
05496 p->registry = registry_unref(p->registry, "delete p->registry");
05497 }
05498
05499 if (p->mwi) {
05500 p->mwi->call = NULL;
05501 }
05502
05503 if (dumphistory)
05504 sip_dump_history(p);
05505
05506 if (p->options)
05507 ast_free(p->options);
05508
05509 if (p->notify) {
05510 ast_variables_destroy(p->notify->headers);
05511 ast_free(p->notify->content);
05512 ast_free(p->notify);
05513 }
05514 if (p->rtp) {
05515 ast_rtp_instance_destroy(p->rtp);
05516 }
05517 if (p->vrtp) {
05518 ast_rtp_instance_destroy(p->vrtp);
05519 }
05520 if (p->trtp) {
05521 ast_rtp_instance_destroy(p->trtp);
05522 }
05523 if (p->udptl)
05524 ast_udptl_destroy(p->udptl);
05525 if (p->refer)
05526 ast_free(p->refer);
05527 if (p->route) {
05528 free_old_route(p->route);
05529 p->route = NULL;
05530 }
05531 deinit_req(&p->initreq);
05532
05533
05534 if (p->stimer) {
05535 p->stimer->quit_flag = 1;
05536 if (p->stimer->st_active == TRUE && p->stimer->st_schedid > -1) {
05537 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
05538 dialog_unref(p, "removing session timer ref"));
05539 }
05540 ast_free(p->stimer);
05541 p->stimer = NULL;
05542 }
05543
05544
05545 if (p->history) {
05546 struct sip_history *hist;
05547 while ( (hist = AST_LIST_REMOVE_HEAD(p->history, list)) ) {
05548 ast_free(hist);
05549 p->history_entries--;
05550 }
05551 ast_free(p->history);
05552 p->history = NULL;
05553 }
05554
05555 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
05556 ast_free(req);
05557 }
05558
05559 if (p->chanvars) {
05560 ast_variables_destroy(p->chanvars);
05561 p->chanvars = NULL;
05562 }
05563
05564 if (p->srtp) {
05565 sip_srtp_destroy(p->srtp);
05566 p->srtp = NULL;
05567 }
05568
05569 if (p->vsrtp) {
05570 sip_srtp_destroy(p->vsrtp);
05571 p->vsrtp = NULL;
05572 }
05573
05574 if (p->tsrtp) {
05575 sip_srtp_destroy(p->tsrtp);
05576 p->tsrtp = NULL;
05577 }
05578
05579 if (p->directmediaha) {
05580 ast_free_ha(p->directmediaha);
05581 p->directmediaha = NULL;
05582 }
05583
05584 ast_string_field_free_memory(p);
05585
05586 ast_cc_config_params_destroy(p->cc_params);
05587
05588 if (p->epa_entry) {
05589 ao2_ref(p->epa_entry, -1);
05590 p->epa_entry = NULL;
05591 }
05592
05593 if (p->socket.tcptls_session) {
05594 ao2_ref(p->socket.tcptls_session, -1);
05595 p->socket.tcptls_session = NULL;
05596 }
05597 }
05598
05599
05600
05601
05602
05603
05604
05605
05606
05607
05608
05609
05610
05611
05612
05613 static int update_call_counter(struct sip_pvt *fup, int event)
05614 {
05615 char name[256];
05616 int *inuse = NULL, *call_limit = NULL, *inringing = NULL;
05617 int outgoing = fup->outgoing_call;
05618 struct sip_peer *p = NULL;
05619
05620 ast_debug(3, "Updating call counter for %s call\n", outgoing ? "outgoing" : "incoming");
05621
05622
05623
05624
05625 if (!ast_test_flag(&fup->flags[0], SIP_CALL_LIMIT) && !ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD))
05626 return 0;
05627
05628 ast_copy_string(name, fup->username, sizeof(name));
05629
05630
05631 if (fup->relatedpeer) {
05632 p = ref_peer(fup->relatedpeer, "ref related peer for update_call_counter");
05633 inuse = &p->inUse;
05634 call_limit = &p->call_limit;
05635 inringing = &p->inRinging;
05636 ast_copy_string(name, fup->peername, sizeof(name));
05637 }
05638 if (!p) {
05639 ast_debug(2, "%s is not a local device, no call limit\n", name);
05640 return 0;
05641 }
05642
05643 switch(event) {
05644
05645 case DEC_CALL_LIMIT:
05646
05647 if (inuse) {
05648 sip_pvt_lock(fup);
05649 ao2_lock(p);
05650 if (*inuse > 0) {
05651 if (ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
05652 (*inuse)--;
05653 ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
05654 }
05655 } else {
05656 *inuse = 0;
05657 }
05658 ao2_unlock(p);
05659 sip_pvt_unlock(fup);
05660 }
05661
05662
05663 if (inringing) {
05664 sip_pvt_lock(fup);
05665 ao2_lock(p);
05666 if (*inringing > 0) {
05667 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
05668 (*inringing)--;
05669 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
05670 }
05671 } else {
05672 *inringing = 0;
05673 }
05674 ao2_unlock(p);
05675 sip_pvt_unlock(fup);
05676 }
05677
05678
05679 sip_pvt_lock(fup);
05680 ao2_lock(p);
05681 if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && sip_cfg.notifyhold) {
05682 ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
05683 ao2_unlock(p);
05684 sip_pvt_unlock(fup);
05685 sip_peer_hold(fup, FALSE);
05686 } else {
05687 ao2_unlock(p);
05688 sip_pvt_unlock(fup);
05689 }
05690 if (sipdebug)
05691 ast_debug(2, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", "peer", name, *call_limit);
05692 break;
05693
05694 case INC_CALL_RINGING:
05695 case INC_CALL_LIMIT:
05696
05697 if (*call_limit > 0 ) {
05698 if (*inuse >= *call_limit) {
05699 ast_log(LOG_NOTICE, "Call %s %s '%s' rejected due to usage limit of %d\n", outgoing ? "to" : "from", "peer", name, *call_limit);
05700 unref_peer(p, "update_call_counter: unref peer p, call limit exceeded");
05701 return -1;
05702 }
05703 }
05704 if (inringing && (event == INC_CALL_RINGING)) {
05705 sip_pvt_lock(fup);
05706 ao2_lock(p);
05707 if (!ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
05708 (*inringing)++;
05709 ast_set_flag(&fup->flags[0], SIP_INC_RINGING);
05710 }
05711 ao2_unlock(p);
05712 sip_pvt_unlock(fup);
05713 }
05714 if (inuse) {
05715 sip_pvt_lock(fup);
05716 ao2_lock(p);
05717 if (!ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
05718 (*inuse)++;
05719 ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
05720 }
05721 ao2_unlock(p);
05722 sip_pvt_unlock(fup);
05723 }
05724 if (sipdebug) {
05725 ast_debug(2, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", "peer", name, *inuse, *call_limit);
05726 }
05727 break;
05728
05729 case DEC_CALL_RINGING:
05730 if (inringing) {
05731 sip_pvt_lock(fup);
05732 ao2_lock(p);
05733 if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
05734 if (*inringing > 0) {
05735 (*inringing)--;
05736 }
05737 ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
05738 }
05739 ao2_unlock(p);
05740 sip_pvt_unlock(fup);
05741 }
05742 break;
05743
05744 default:
05745 ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
05746 }
05747
05748 if (p) {
05749 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", p->name);
05750 unref_peer(p, "update_call_counter: unref_peer from call counter");
05751 }
05752 return 0;
05753 }
05754
05755
05756 static void sip_destroy_fn(void *p)
05757 {
05758 sip_destroy(p);
05759 }
05760
05761
05762
05763
05764
05765
05766 struct sip_pvt *sip_destroy(struct sip_pvt *p)
05767 {
05768 ast_debug(3, "Destroying SIP dialog %s\n", p->callid);
05769 __sip_destroy(p, TRUE, TRUE);
05770 return NULL;
05771 }
05772
05773
05774 int hangup_sip2cause(int cause)
05775 {
05776
05777
05778 switch(cause) {
05779 case 401:
05780 return AST_CAUSE_CALL_REJECTED;
05781 case 403:
05782 return AST_CAUSE_CALL_REJECTED;
05783 case 404:
05784 return AST_CAUSE_UNALLOCATED;
05785 case 405:
05786 return AST_CAUSE_INTERWORKING;
05787 case 407:
05788 return AST_CAUSE_CALL_REJECTED;
05789 case 408:
05790 return AST_CAUSE_NO_USER_RESPONSE;
05791 case 409:
05792 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
05793 case 410:
05794 return AST_CAUSE_NUMBER_CHANGED;
05795 case 411:
05796 return AST_CAUSE_INTERWORKING;
05797 case 413:
05798 return AST_CAUSE_INTERWORKING;
05799 case 414:
05800 return AST_CAUSE_INTERWORKING;
05801 case 415:
05802 return AST_CAUSE_INTERWORKING;
05803 case 420:
05804 return AST_CAUSE_NO_ROUTE_DESTINATION;
05805 case 480:
05806 return AST_CAUSE_NO_ANSWER;
05807 case 481:
05808 return AST_CAUSE_INTERWORKING;
05809 case 482:
05810 return AST_CAUSE_INTERWORKING;
05811 case 483:
05812 return AST_CAUSE_NO_ANSWER;
05813 case 484:
05814 return AST_CAUSE_INVALID_NUMBER_FORMAT;
05815 case 485:
05816 return AST_CAUSE_UNALLOCATED;
05817 case 486:
05818 return AST_CAUSE_BUSY;
05819 case 487:
05820 return AST_CAUSE_INTERWORKING;
05821 case 488:
05822 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05823 case 491:
05824 return AST_CAUSE_INTERWORKING;
05825 case 493:
05826 return AST_CAUSE_INTERWORKING;
05827 case 500:
05828 return AST_CAUSE_FAILURE;
05829 case 501:
05830 return AST_CAUSE_FACILITY_REJECTED;
05831 case 502:
05832 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
05833 case 503:
05834 return AST_CAUSE_CONGESTION;
05835 case 504:
05836 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
05837 case 505:
05838 return AST_CAUSE_INTERWORKING;
05839 case 600:
05840 return AST_CAUSE_USER_BUSY;
05841 case 603:
05842 return AST_CAUSE_CALL_REJECTED;
05843 case 604:
05844 return AST_CAUSE_UNALLOCATED;
05845 case 606:
05846 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
05847 default:
05848 if (cause < 500 && cause >= 400) {
05849
05850 return AST_CAUSE_INTERWORKING;
05851 } else if (cause < 600 && cause >= 500) {
05852
05853 return AST_CAUSE_CONGESTION;
05854 } else if (cause < 700 && cause >= 600) {
05855
05856 return AST_CAUSE_INTERWORKING;
05857 }
05858 return AST_CAUSE_NORMAL;
05859 }
05860
05861 return 0;
05862 }
05863
05864
05865
05866
05867
05868
05869
05870
05871
05872
05873
05874
05875
05876
05877
05878
05879
05880
05881
05882
05883
05884
05885
05886
05887
05888
05889
05890
05891
05892
05893
05894
05895
05896 const char *hangup_cause2sip(int cause)
05897 {
05898 switch (cause) {
05899 case AST_CAUSE_UNALLOCATED:
05900 case AST_CAUSE_NO_ROUTE_DESTINATION:
05901 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
05902 return "404 Not Found";
05903 case AST_CAUSE_CONGESTION:
05904 case AST_CAUSE_SWITCH_CONGESTION:
05905 return "503 Service Unavailable";
05906 case AST_CAUSE_NO_USER_RESPONSE:
05907 return "408 Request Timeout";
05908 case AST_CAUSE_NO_ANSWER:
05909 case AST_CAUSE_UNREGISTERED:
05910 return "480 Temporarily unavailable";
05911 case AST_CAUSE_CALL_REJECTED:
05912 return "403 Forbidden";
05913 case AST_CAUSE_NUMBER_CHANGED:
05914 return "410 Gone";
05915 case AST_CAUSE_NORMAL_UNSPECIFIED:
05916 return "480 Temporarily unavailable";
05917 case AST_CAUSE_INVALID_NUMBER_FORMAT:
05918 return "484 Address incomplete";
05919 case AST_CAUSE_USER_BUSY:
05920 return "486 Busy here";
05921 case AST_CAUSE_FAILURE:
05922 return "500 Server internal failure";
05923 case AST_CAUSE_FACILITY_REJECTED:
05924 return "501 Not Implemented";
05925 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
05926 return "503 Service Unavailable";
05927
05928 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
05929 return "502 Bad Gateway";
05930 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:
05931 return "488 Not Acceptable Here";
05932
05933 case AST_CAUSE_NOTDEFINED:
05934 default:
05935 ast_debug(1, "AST hangup cause %d (no match found in SIP)\n", cause);
05936 return NULL;
05937 }
05938
05939
05940 return 0;
05941 }
05942
05943
05944
05945 static int sip_hangup(struct ast_channel *ast)
05946 {
05947 struct sip_pvt *p = ast->tech_pvt;
05948 int needcancel = FALSE;
05949 int needdestroy = 0;
05950 struct ast_channel *oldowner = ast;
05951
05952 if (!p) {
05953 ast_debug(1, "Asked to hangup channel that was not connected\n");
05954 return 0;
05955 }
05956 if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE) || ast->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE) {
05957 ast_debug(1, "This call was answered elsewhere");
05958 if (ast->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE) {
05959 ast_debug(1, "####### It's the cause code, buddy. The cause code!!!\n");
05960 }
05961 append_history(p, "Cancel", "Call answered elsewhere");
05962 p->answered_elsewhere = TRUE;
05963 }
05964
05965
05966 if (p->owner)
05967 p->hangupcause = p->owner->hangupcause;
05968
05969 if (ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
05970 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05971 if (sipdebug)
05972 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
05973 update_call_counter(p, DEC_CALL_LIMIT);
05974 }
05975 ast_debug(4, "SIP Transfer: Not hanging up right now... Rescheduling hangup for %s.\n", p->callid);
05976 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
05977 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
05978 p->needdestroy = 0;
05979 p->owner->tech_pvt = dialog_unref(p->owner->tech_pvt, "unref p->owner->tech_pvt");
05980 sip_pvt_lock(p);
05981 p->owner = NULL;
05982 sip_pvt_unlock(p);
05983 ast_module_unref(ast_module_info->self);
05984 return 0;
05985 }
05986
05987 if (ast_test_flag(ast, AST_FLAG_ZOMBIE)) {
05988 if (p->refer)
05989 ast_debug(1, "SIP Transfer: Hanging up Zombie channel %s after transfer ... Call-ID: %s\n", ast->name, p->callid);
05990 else
05991 ast_debug(1, "Hanging up zombie call. Be scared.\n");
05992 } else
05993 ast_debug(1, "Hangup call %s, SIP callid %s\n", ast->name, p->callid);
05994
05995 sip_pvt_lock(p);
05996 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
05997 if (sipdebug)
05998 ast_debug(1, "update_call_counter(%s) - decrement call limit counter on hangup\n", p->username);
05999 update_call_counter(p, DEC_CALL_LIMIT);
06000 }
06001
06002
06003 if (p->owner != ast) {
06004 ast_log(LOG_WARNING, "Huh? We aren't the owner? Can't hangup call.\n");
06005 sip_pvt_unlock(p);
06006 return 0;
06007 }
06008
06009
06010 if (p->invitestate < INV_COMPLETED && p->owner->_state != AST_STATE_UP) {
06011 needcancel = TRUE;
06012 ast_debug(4, "Hanging up channel in state %s (not UP)\n", ast_state2str(ast->_state));
06013 }
06014
06015 stop_media_flows(p);
06016
06017 append_history(p, needcancel ? "Cancel" : "Hangup", "Cause %s", p->owner ? ast_cause2str(p->hangupcause) : "Unknown");
06018
06019
06020 disable_dsp_detect(p);
06021
06022 p->owner = NULL;
06023 ast->tech_pvt = dialog_unref(ast->tech_pvt, "unref ast->tech_pvt");
06024
06025 ast_module_unref(ast_module_info->self);
06026
06027
06028
06029
06030
06031
06032 if (p->alreadygone)
06033 needdestroy = 1;
06034 else if (p->invitestate != INV_CALLING)
06035 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
06036
06037
06038 if (!p->alreadygone && p->initreq.data && !ast_strlen_zero(p->initreq.data->str)) {
06039 if (needcancel) {
06040 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06041
06042 if (p->invitestate == INV_CALLING) {
06043
06044 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
06045
06046 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
06047 append_history(p, "DELAY", "Not sending cancel, waiting for timeout");
06048 } else {
06049 struct sip_pkt *cur;
06050
06051 for (cur = p->packets; cur; cur = cur->next) {
06052 __sip_semi_ack(p, cur->seqno, cur->is_resp, cur->method ? cur->method : find_sip_method(cur->data->str));
06053 }
06054 p->invitestate = INV_CANCELLED;
06055
06056 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
06057
06058
06059 needdestroy = 0;
06060 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
06061 }
06062 } else {
06063 const char *res;
06064 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"));
06065 if (p->hangupcause && (res = hangup_cause2sip(p->hangupcause)))
06066 transmit_response_reliable(p, res, &p->initreq);
06067 else
06068 transmit_response_reliable(p, "603 Declined", &p->initreq);
06069 p->invitestate = INV_TERMINATED;
06070 }
06071 } else {
06072 if (p->stimer->st_active == TRUE) {
06073 stop_session_timer(p);
06074 }
06075
06076 if (!p->pendinginvite) {
06077 struct ast_channel *bridge = ast_bridged_channel(oldowner);
06078 char quality_buf[AST_MAX_USER_FIELD], *quality;
06079
06080
06081
06082
06083 while (bridge && ast_channel_trylock(bridge)) {
06084 sip_pvt_unlock(p);
06085 do {
06086 CHANNEL_DEADLOCK_AVOIDANCE(oldowner);
06087 } while (sip_pvt_trylock(p));
06088 bridge = ast_bridged_channel(oldowner);
06089 }
06090
06091 if (p->rtp) {
06092 ast_rtp_instance_set_stats_vars(oldowner, p->rtp);
06093 }
06094
06095 if (bridge) {
06096 struct sip_pvt *q = bridge->tech_pvt;
06097
06098 if (IS_SIP_TECH(bridge->tech) && q && q->rtp) {
06099 ast_rtp_instance_set_stats_vars(bridge, q->rtp);
06100 }
06101 ast_channel_unlock(bridge);
06102 }
06103
06104 if (p->do_history || oldowner) {
06105 if (p->rtp && (quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
06106 if (p->do_history) {
06107 append_history(p, "RTCPaudio", "Quality:%s", quality);
06108 }
06109 if (oldowner) {
06110 pbx_builtin_setvar_helper(oldowner, "RTPAUDIOQOS", quality);
06111 }
06112 }
06113 if (p->vrtp && (quality = ast_rtp_instance_get_quality(p->vrtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
06114 if (p->do_history) {
06115 append_history(p, "RTCPvideo", "Quality:%s", quality);
06116 }
06117 if (oldowner) {
06118 pbx_builtin_setvar_helper(oldowner, "RTPVIDEOQOS", quality);
06119 }
06120 }
06121 if (p->trtp && (quality = ast_rtp_instance_get_quality(p->trtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
06122 if (p->do_history) {
06123 append_history(p, "RTCPtext", "Quality:%s", quality);
06124 }
06125 if (oldowner) {
06126 pbx_builtin_setvar_helper(oldowner, "RTPTEXTQOS", quality);
06127 }
06128 }
06129 }
06130
06131
06132 if (oldowner->_state == AST_STATE_UP) {
06133 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, 1);
06134 }
06135
06136 } else {
06137
06138
06139 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
06140 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
06141 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"));
06142 if (sip_cancel_destroy(p))
06143 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
06144 }
06145 }
06146 }
06147 if (needdestroy) {
06148 pvt_set_needdestroy(p, "hangup");
06149 }
06150 sip_pvt_unlock(p);
06151 return 0;
06152 }
06153
06154
06155 static void try_suggested_sip_codec(struct sip_pvt *p)
06156 {
06157 format_t fmt;
06158 const char *codec;
06159
06160 if (p->outgoing_call) {
06161 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_OUTBOUND");
06162 } else if (!(codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_INBOUND"))) {
06163 codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
06164 }
06165
06166 if (!codec)
06167 return;
06168
06169 fmt = ast_getformatbyname(codec);
06170 if (fmt) {
06171 ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
06172 if (p->jointcapability & fmt) {
06173 p->jointcapability &= fmt;
06174 p->capability &= fmt;
06175 } else
06176 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
06177 } else
06178 ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
06179 return;
06180 }
06181
06182
06183
06184 static int sip_answer(struct ast_channel *ast)
06185 {
06186 int res = 0;
06187 struct sip_pvt *p = ast->tech_pvt;
06188
06189 sip_pvt_lock(p);
06190 if (ast->_state != AST_STATE_UP) {
06191 try_suggested_sip_codec(p);
06192
06193 ast_setstate(ast, AST_STATE_UP);
06194 ast_debug(1, "SIP answering channel: %s\n", ast->name);
06195 ast_rtp_instance_update_source(p->rtp);
06196 res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL, FALSE, TRUE);
06197 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
06198 }
06199 sip_pvt_unlock(p);
06200 return res;
06201 }
06202
06203
06204 static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
06205 {
06206 struct sip_pvt *p = ast->tech_pvt;
06207 int res = 0;
06208
06209 switch (frame->frametype) {
06210 case AST_FRAME_VOICE:
06211 if (!(frame->subclass.codec & ast->nativeformats)) {
06212 char s1[512], s2[512], s3[512];
06213 ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s read/write = %s/%s\n",
06214 ast_getformatname(frame->subclass.codec),
06215 ast_getformatname_multiple(s1, sizeof(s1), ast->nativeformats & AST_FORMAT_AUDIO_MASK),
06216 ast_getformatname_multiple(s2, sizeof(s2), ast->readformat),
06217 ast_getformatname_multiple(s3, sizeof(s3), ast->writeformat));
06218 return 0;
06219 }
06220 if (p) {
06221 sip_pvt_lock(p);
06222 if (p->rtp) {
06223
06224 if ((ast->_state != AST_STATE_UP) &&
06225 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06226 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06227 ast_rtp_instance_update_source(p->rtp);
06228 if (!global_prematuremediafilter) {
06229 p->invitestate = INV_EARLY_MEDIA;
06230 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06231 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06232 }
06233 } else if (p->t38.state == T38_ENABLED) {
06234
06235 } else {
06236 p->lastrtptx = time(NULL);
06237 res = ast_rtp_instance_write(p->rtp, frame);
06238 }
06239 }
06240 sip_pvt_unlock(p);
06241 }
06242 break;
06243 case AST_FRAME_VIDEO:
06244 if (p) {
06245 sip_pvt_lock(p);
06246 if (p->vrtp) {
06247
06248 if ((ast->_state != AST_STATE_UP) &&
06249 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06250 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06251 p->invitestate = INV_EARLY_MEDIA;
06252 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06253 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06254 }
06255 p->lastrtptx = time(NULL);
06256 res = ast_rtp_instance_write(p->vrtp, frame);
06257 }
06258 sip_pvt_unlock(p);
06259 }
06260 break;
06261 case AST_FRAME_TEXT:
06262 if (p) {
06263 sip_pvt_lock(p);
06264 if (p->red) {
06265 ast_rtp_red_buffer(p->trtp, frame);
06266 } else {
06267 if (p->trtp) {
06268
06269 if ((ast->_state != AST_STATE_UP) &&
06270 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06271 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06272 p->invitestate = INV_EARLY_MEDIA;
06273 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06274 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06275 }
06276 p->lastrtptx = time(NULL);
06277 res = ast_rtp_instance_write(p->trtp, frame);
06278 }
06279 }
06280 sip_pvt_unlock(p);
06281 }
06282 break;
06283 case AST_FRAME_IMAGE:
06284 return 0;
06285 break;
06286 case AST_FRAME_MODEM:
06287 if (p) {
06288 sip_pvt_lock(p);
06289
06290
06291
06292
06293 if ((ast->_state == AST_STATE_UP) &&
06294 p->udptl &&
06295 (p->t38.state == T38_ENABLED)) {
06296 res = ast_udptl_write(p->udptl, frame);
06297 }
06298 sip_pvt_unlock(p);
06299 }
06300 break;
06301 default:
06302 ast_log(LOG_WARNING, "Can't send %d type frames with SIP write\n", frame->frametype);
06303 return 0;
06304 }
06305
06306 return res;
06307 }
06308
06309
06310
06311 static int sip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
06312 {
06313 int ret = -1;
06314 struct sip_pvt *p;
06315
06316 if (newchan && ast_test_flag(newchan, AST_FLAG_ZOMBIE))
06317 ast_debug(1, "New channel is zombie\n");
06318 if (oldchan && ast_test_flag(oldchan, AST_FLAG_ZOMBIE))
06319 ast_debug(1, "Old channel is zombie\n");
06320
06321 if (!newchan || !newchan->tech_pvt) {
06322 if (!newchan)
06323 ast_log(LOG_WARNING, "No new channel! Fixup of %s failed.\n", oldchan->name);
06324 else
06325 ast_log(LOG_WARNING, "No SIP tech_pvt! Fixup of %s failed.\n", oldchan->name);
06326 return -1;
06327 }
06328 p = newchan->tech_pvt;
06329
06330 sip_pvt_lock(p);
06331 append_history(p, "Masq", "Old channel: %s\n", oldchan->name);
06332 append_history(p, "Masq (cont)", "...new owner: %s\n", newchan->name);
06333 if (p->owner != oldchan)
06334 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
06335 else {
06336 p->owner = newchan;
06337
06338
06339
06340
06341
06342
06343 sip_set_rtp_peer(newchan, NULL, NULL, 0, 0, 0);
06344 ret = 0;
06345 }
06346 ast_debug(3, "SIP Fixup: New owner for dialogue %s: %s (Old parent: %s)\n", p->callid, p->owner->name, oldchan->name);
06347
06348 sip_pvt_unlock(p);
06349 return ret;
06350 }
06351
06352 static int sip_senddigit_begin(struct ast_channel *ast, char digit)
06353 {
06354 struct sip_pvt *p = ast->tech_pvt;
06355 int res = 0;
06356
06357 sip_pvt_lock(p);
06358 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
06359 case SIP_DTMF_INBAND:
06360 if (p->rtp && ast_rtp_instance_dtmf_mode_get(p->rtp) == AST_RTP_DTMF_MODE_INBAND) {
06361 ast_rtp_instance_dtmf_begin(p->rtp, digit);
06362 } else {
06363 res = -1;
06364 }
06365 break;
06366 case SIP_DTMF_RFC2833:
06367 if (p->rtp)
06368 ast_rtp_instance_dtmf_begin(p->rtp, digit);
06369 break;
06370 default:
06371 break;
06372 }
06373 sip_pvt_unlock(p);
06374
06375 return res;
06376 }
06377
06378
06379
06380 static int sip_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
06381 {
06382 struct sip_pvt *p = ast->tech_pvt;
06383 int res = 0;
06384
06385 sip_pvt_lock(p);
06386 switch (ast_test_flag(&p->flags[0], SIP_DTMF)) {
06387 case SIP_DTMF_INFO:
06388 case SIP_DTMF_SHORTINFO:
06389 transmit_info_with_digit(p, digit, duration);
06390 break;
06391 case SIP_DTMF_RFC2833:
06392 if (p->rtp)
06393 ast_rtp_instance_dtmf_end_with_duration(p->rtp, digit, duration);
06394 break;
06395 case SIP_DTMF_INBAND:
06396 if (p->rtp && ast_rtp_instance_dtmf_mode_get(p->rtp) == AST_RTP_DTMF_MODE_INBAND) {
06397 ast_rtp_instance_dtmf_end(p->rtp, digit);
06398 } else {
06399 res = -1;
06400 }
06401 break;
06402 }
06403 sip_pvt_unlock(p);
06404
06405 return res;
06406 }
06407
06408
06409 static int sip_transfer(struct ast_channel *ast, const char *dest)
06410 {
06411 struct sip_pvt *p = ast->tech_pvt;
06412 int res;
06413
06414 if (dest == NULL)
06415 dest = "";
06416 sip_pvt_lock(p);
06417 if (ast->_state == AST_STATE_RING)
06418 res = sip_sipredirect(p, dest);
06419 else
06420 res = transmit_refer(p, dest);
06421 sip_pvt_unlock(p);
06422 return res;
06423 }
06424
06425
06426 static int interpret_t38_parameters(struct sip_pvt *p, const struct ast_control_t38_parameters *parameters)
06427 {
06428 int res = 0;
06429
06430 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) || !p->udptl) {
06431 return -1;
06432 }
06433 switch (parameters->request_response) {
06434 case AST_T38_NEGOTIATED:
06435 case AST_T38_REQUEST_NEGOTIATE:
06436
06437 if (!parameters->max_ifp) {
06438 change_t38_state(p, T38_DISABLED);
06439 if (p->t38.state == T38_PEER_REINVITE) {
06440 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"));
06441 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
06442 }
06443 break;
06444 } else if (p->t38.state == T38_PEER_REINVITE) {
06445 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"));
06446 p->t38.our_parms = *parameters;
06447
06448
06449
06450 if (!p->t38.their_parms.fill_bit_removal) {
06451 p->t38.our_parms.fill_bit_removal = FALSE;
06452 }
06453 if (!p->t38.their_parms.transcoding_mmr) {
06454 p->t38.our_parms.transcoding_mmr = FALSE;
06455 }
06456 if (!p->t38.their_parms.transcoding_jbig) {
06457 p->t38.our_parms.transcoding_jbig = FALSE;
06458 }
06459 p->t38.our_parms.version = MIN(p->t38.our_parms.version, p->t38.their_parms.version);
06460 p->t38.our_parms.rate_management = p->t38.their_parms.rate_management;
06461 ast_udptl_set_local_max_ifp(p->udptl, p->t38.our_parms.max_ifp);
06462 change_t38_state(p, T38_ENABLED);
06463 transmit_response_with_t38_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL);
06464 } else if (p->t38.state != T38_ENABLED) {
06465 p->t38.our_parms = *parameters;
06466 ast_udptl_set_local_max_ifp(p->udptl, p->t38.our_parms.max_ifp);
06467 change_t38_state(p, T38_LOCAL_REINVITE);
06468 if (!p->pendinginvite) {
06469 transmit_reinvite_with_sdp(p, TRUE, FALSE);
06470 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
06471 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
06472 }
06473 }
06474 break;
06475 case AST_T38_TERMINATED:
06476 case AST_T38_REFUSED:
06477 case AST_T38_REQUEST_TERMINATE:
06478 if (p->t38.state == T38_PEER_REINVITE) {
06479 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"));
06480 change_t38_state(p, T38_DISABLED);
06481 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
06482 } else if (p->t38.state == T38_ENABLED)
06483 transmit_reinvite_with_sdp(p, FALSE, FALSE);
06484 break;
06485 case AST_T38_REQUEST_PARMS: {
06486 struct ast_control_t38_parameters parameters = p->t38.their_parms;
06487
06488 if (p->t38.state == T38_PEER_REINVITE) {
06489 AST_SCHED_DEL(sched, p->t38id);
06490 parameters.max_ifp = ast_udptl_get_far_max_ifp(p->udptl);
06491 parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
06492 ast_queue_control_data(p->owner, AST_CONTROL_T38_PARAMETERS, ¶meters, sizeof(parameters));
06493
06494
06495
06496
06497 res = AST_T38_REQUEST_PARMS;
06498 }
06499 break;
06500 }
06501 default:
06502 res = -1;
06503 break;
06504 }
06505
06506 return res;
06507 }
06508
06509
06510
06511
06512
06513
06514 static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
06515 {
06516 struct sip_pvt *p = ast->tech_pvt;
06517 int res = 0;
06518
06519 sip_pvt_lock(p);
06520 switch(condition) {
06521 case AST_CONTROL_RINGING:
06522 if (ast->_state == AST_STATE_RING) {
06523 p->invitestate = INV_EARLY_MEDIA;
06524 if (!ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) ||
06525 (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) == SIP_PROG_INBAND_NEVER)) {
06526
06527 transmit_provisional_response(p, "180 Ringing", &p->initreq, 0);
06528 ast_set_flag(&p->flags[0], SIP_RINGING);
06529 if (ast_test_flag(&p->flags[0], SIP_PROG_INBAND) != SIP_PROG_INBAND_YES)
06530 break;
06531 } else {
06532
06533 }
06534 }
06535 res = -1;
06536 break;
06537 case AST_CONTROL_BUSY:
06538 if (ast->_state != AST_STATE_UP) {
06539 transmit_response_reliable(p, "486 Busy Here", &p->initreq);
06540 p->invitestate = INV_COMPLETED;
06541 sip_alreadygone(p);
06542 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
06543 break;
06544 }
06545 res = -1;
06546 break;
06547 case AST_CONTROL_CONGESTION:
06548 if (ast->_state != AST_STATE_UP) {
06549 transmit_response_reliable(p, "503 Service Unavailable", &p->initreq);
06550 p->invitestate = INV_COMPLETED;
06551 sip_alreadygone(p);
06552 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
06553 break;
06554 }
06555 res = -1;
06556 break;
06557 case AST_CONTROL_PROCEEDING:
06558 if ((ast->_state != AST_STATE_UP) &&
06559 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06560 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06561 transmit_response(p, "100 Trying", &p->initreq);
06562 p->invitestate = INV_PROCEEDING;
06563 break;
06564 }
06565 res = -1;
06566 break;
06567 case AST_CONTROL_PROGRESS:
06568 if ((ast->_state != AST_STATE_UP) &&
06569 !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT) &&
06570 !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
06571 p->invitestate = INV_EARLY_MEDIA;
06572 transmit_provisional_response(p, "183 Session Progress", &p->initreq, TRUE);
06573 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
06574 break;
06575 }
06576 res = -1;
06577 break;
06578 case AST_CONTROL_HOLD:
06579 ast_rtp_instance_update_source(p->rtp);
06580 ast_moh_start(ast, data, p->mohinterpret);
06581 break;
06582 case AST_CONTROL_UNHOLD:
06583 ast_rtp_instance_update_source(p->rtp);
06584 ast_moh_stop(ast);
06585 break;
06586 case AST_CONTROL_VIDUPDATE:
06587 if (p->vrtp && !p->novideo) {
06588 transmit_info_with_vidupdate(p);
06589
06590 } else
06591 res = -1;
06592 break;
06593 case AST_CONTROL_T38_PARAMETERS:
06594 if (datalen != sizeof(struct ast_control_t38_parameters)) {
06595 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);
06596 res = -1;
06597 } else {
06598 const struct ast_control_t38_parameters *parameters = data;
06599 res = interpret_t38_parameters(p, parameters);
06600 }
06601 break;
06602 case AST_CONTROL_SRCUPDATE:
06603 ast_rtp_instance_update_source(p->rtp);
06604 break;
06605 case AST_CONTROL_SRCCHANGE:
06606 ast_rtp_instance_change_source(p->rtp);
06607 break;
06608 case AST_CONTROL_CONNECTED_LINE:
06609 update_connectedline(p, data, datalen);
06610 break;
06611 case AST_CONTROL_REDIRECTING:
06612 update_redirecting(p, data, datalen);
06613 break;
06614 case AST_CONTROL_AOC:
06615 {
06616 struct ast_aoc_decoded *decoded = ast_aoc_decode((struct ast_aoc_encoded *) data, datalen, ast);
06617 if (!decoded) {
06618 ast_log(LOG_ERROR, "Error decoding indicated AOC data\n");
06619 res = -1;
06620 break;
06621 }
06622 switch (ast_aoc_get_msg_type(decoded)) {
06623 case AST_AOC_REQUEST:
06624 if (ast_aoc_get_termination_request(decoded)) {
06625
06626
06627
06628
06629
06630
06631
06632 ast_debug(1, "AOC-E termination request received on %s. This is not yet supported on sip. Continue with hangup \n", p->owner->name);
06633 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
06634 }
06635 break;
06636 case AST_AOC_D:
06637 case AST_AOC_E:
06638 if (ast_test_flag(&p->flags[2], SIP_PAGE3_SNOM_AOC)) {
06639 transmit_info_with_aoc(p, decoded);
06640 }
06641 break;
06642 case AST_AOC_S:
06643 default:
06644 break;
06645 }
06646 ast_aoc_destroy_decoded(decoded);
06647 }
06648 break;
06649 case -1:
06650 res = -1;
06651 break;
06652 default:
06653 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
06654 res = -1;
06655 break;
06656 }
06657 sip_pvt_unlock(p);
06658 return res;
06659 }
06660
06661
06662
06663
06664
06665
06666 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid)
06667 {
06668 struct ast_channel *tmp;
06669 struct ast_variable *v = NULL;
06670 format_t fmt;
06671 format_t what;
06672 format_t video;
06673 format_t text;
06674 format_t needvideo = 0;
06675 int needtext = 0;
06676 char buf[SIPBUFSIZE];
06677 char *decoded_exten;
06678
06679 {
06680 const char *my_name;
06681
06682 if (title) {
06683 my_name = title;
06684 } else {
06685 my_name = ast_strdupa(i->fromdomain);
06686 }
06687
06688 sip_pvt_unlock(i);
06689
06690 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, ast_atomic_fetchadd_int((int *)&chan_idx, +1));
06691 }
06692 if (!tmp) {
06693 ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
06694 sip_pvt_lock(i);
06695 return NULL;
06696 }
06697 ast_channel_lock(tmp);
06698 sip_pvt_lock(i);
06699 ast_channel_cc_params_init(tmp, i->cc_params);
06700 tmp->caller.id.tag = ast_strdup(i->cid_tag);
06701 ast_channel_unlock(tmp);
06702
06703 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;
06704
06705
06706
06707 if (i->jointcapability) {
06708 what = i->jointcapability;
06709 video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
06710 text = i->jointcapability & AST_FORMAT_TEXT_MASK;
06711 } else if (i->capability) {
06712 what = i->capability;
06713 video = i->capability & AST_FORMAT_VIDEO_MASK;
06714 text = i->capability & AST_FORMAT_TEXT_MASK;
06715 } else {
06716 what = sip_cfg.capability;
06717 video = sip_cfg.capability & AST_FORMAT_VIDEO_MASK;
06718 text = sip_cfg.capability & AST_FORMAT_TEXT_MASK;
06719 }
06720
06721
06722 tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video | text;
06723 ast_debug(3, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
06724 ast_debug(3, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
06725 ast_debug(3, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
06726 ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
06727 if (i->prefcodec)
06728 ast_debug(3, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
06729
06730
06731 fmt = ast_best_codec(tmp->nativeformats);
06732
06733
06734
06735
06736
06737 if (i->vrtp) {
06738 if (ast_test_flag(&i->flags[1], SIP_PAGE2_VIDEOSUPPORT))
06739 needvideo = AST_FORMAT_VIDEO_MASK;
06740 else if (i->prefcodec)
06741 needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;
06742 else
06743 needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;
06744 }
06745
06746 if (i->trtp) {
06747 if (i->prefcodec)
06748 needtext = i->prefcodec & AST_FORMAT_TEXT_MASK;
06749 else
06750 needtext = i->jointcapability & AST_FORMAT_TEXT_MASK;
06751 }
06752
06753 if (needvideo)
06754 ast_debug(3, "This channel can handle video! HOLLYWOOD next!\n");
06755 else
06756 ast_debug(3, "This channel will not be able to handle video.\n");
06757
06758 enable_dsp_detect(i);
06759
06760 if ((ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
06761 (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
06762 if (!i->rtp || ast_rtp_instance_dtmf_mode_set(i->rtp, AST_RTP_DTMF_MODE_INBAND)) {
06763 enable_dsp_detect(i);
06764 }
06765 } else if (ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) {
06766 if (i->rtp) {
06767 ast_rtp_instance_dtmf_mode_set(i->rtp, AST_RTP_DTMF_MODE_RFC2833);
06768 }
06769 }
06770
06771
06772 if (i->rtp) {
06773 ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(i->rtp, 0));
06774 ast_channel_set_fd(tmp, 1, ast_rtp_instance_fd(i->rtp, 1));
06775 }
06776 if (needvideo && i->vrtp) {
06777 ast_channel_set_fd(tmp, 2, ast_rtp_instance_fd(i->vrtp, 0));
06778 ast_channel_set_fd(tmp, 3, ast_rtp_instance_fd(i->vrtp, 1));
06779 }
06780 if (needtext && i->trtp)
06781 ast_channel_set_fd(tmp, 4, ast_rtp_instance_fd(i->trtp, 0));
06782 if (i->udptl)
06783 ast_channel_set_fd(tmp, 5, ast_udptl_fd(i->udptl));
06784
06785 if (state == AST_STATE_RING)
06786 tmp->rings = 1;
06787 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
06788
06789 tmp->writeformat = fmt;
06790 tmp->rawwriteformat = fmt;
06791 ast_rtp_instance_set_write_format(i->rtp, fmt);
06792
06793 tmp->readformat = fmt;
06794 tmp->rawreadformat = fmt;
06795 ast_rtp_instance_set_read_format(i->rtp, fmt);
06796
06797 tmp->tech_pvt = dialog_ref(i, "sip_new: set chan->tech_pvt to i");
06798
06799 tmp->callgroup = i->callgroup;
06800 tmp->pickupgroup = i->pickupgroup;
06801 tmp->caller.id.name.presentation = i->callingpres;
06802 tmp->caller.id.number.presentation = i->callingpres;
06803 if (!ast_strlen_zero(i->parkinglot))
06804 ast_string_field_set(tmp, parkinglot, i->parkinglot);
06805 if (!ast_strlen_zero(i->accountcode))
06806 ast_string_field_set(tmp, accountcode, i->accountcode);
06807 if (i->amaflags)
06808 tmp->amaflags = i->amaflags;
06809 if (!ast_strlen_zero(i->language))
06810 ast_string_field_set(tmp, language, i->language);
06811 i->owner = tmp;
06812 ast_module_ref(ast_module_info->self);
06813 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
06814
06815
06816
06817
06818 if (ast_exists_extension(NULL, i->context, i->exten, 1, i->cid_num)) {
06819
06820 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
06821 } else {
06822 decoded_exten = ast_strdupa(i->exten);
06823 ast_uri_decode(decoded_exten);
06824 ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
06825 }
06826
06827
06828
06829 if (!ast_strlen_zero(i->cid_num)) {
06830 tmp->caller.ani.number.valid = 1;
06831 tmp->caller.ani.number.str = ast_strdup(i->cid_num);
06832 }
06833 if (!ast_strlen_zero(i->rdnis)) {
06834 tmp->redirecting.from.number.valid = 1;
06835 tmp->redirecting.from.number.str = ast_strdup(i->rdnis);
06836 }
06837
06838 if (!ast_strlen_zero(i->exten) && strcmp(i->exten, "s")) {
06839 tmp->dialed.number.str = ast_strdup(i->exten);
06840 }
06841
06842 tmp->priority = 1;
06843 if (!ast_strlen_zero(i->uri))
06844 pbx_builtin_setvar_helper(tmp, "SIPURI", i->uri);
06845 if (!ast_strlen_zero(i->domain))
06846 pbx_builtin_setvar_helper(tmp, "SIPDOMAIN", i->domain);
06847 if (!ast_strlen_zero(i->callid))
06848 pbx_builtin_setvar_helper(tmp, "SIPCALLID", i->callid);
06849 if (i->rtp)
06850 ast_jb_configure(tmp, &global_jbconf);
06851
06852
06853 for (v = i->chanvars ; v ; v = v->next) {
06854 char valuebuf[1024];
06855 pbx_builtin_setvar_helper(tmp, v->name, ast_get_encoded_str(v->value, valuebuf, sizeof(valuebuf)));
06856 }
06857
06858 if (state != AST_STATE_DOWN && ast_pbx_start(tmp)) {
06859 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
06860 tmp->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
06861 ast_hangup(tmp);
06862 tmp = NULL;
06863 }
06864
06865 if (i->do_history)
06866 append_history(i, "NewChan", "Channel %s - from %s", tmp->name, i->callid);
06867
06868
06869 if (sip_cfg.callevents)
06870 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
06871 "Channel: %s\r\nUniqueid: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\n",
06872 tmp->name, tmp->uniqueid, "SIP", i->callid, i->fullcontact);
06873
06874 return tmp;
06875 }
06876
06877
06878 static char *get_body_by_line(const char *line, const char *name, int nameLen, char delimiter)
06879 {
06880 if (!strncasecmp(line, name, nameLen) && line[nameLen] == delimiter)
06881 return ast_skip_blanks(line + nameLen + 1);
06882
06883 return "";
06884 }
06885
06886
06887
06888
06889
06890 static const char *get_sdp_iterate(int *start, struct sip_request *req, const char *name)
06891 {
06892 int len = strlen(name);
06893
06894 while (*start < (req->sdp_start + req->sdp_count)) {
06895 const char *r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[(*start)++]), name, len, '=');
06896 if (r[0] != '\0')
06897 return r;
06898 }
06899
06900
06901 (*start)++;
06902
06903 return "";
06904 }
06905
06906
06907
06908
06909
06910
06911 static char get_sdp_line(int *start, int stop, struct sip_request *req, const char **value)
06912 {
06913 char type = '\0';
06914 const char *line = NULL;
06915
06916 if (stop > (req->sdp_start + req->sdp_count)) {
06917 stop = req->sdp_start + req->sdp_count;
06918 }
06919
06920 while (*start < stop) {
06921 line = REQ_OFFSET_TO_STR(req, line[(*start)++]);
06922 if (line[1] == '=') {
06923 type = line[0];
06924 *value = ast_skip_blanks(line + 2);
06925 break;
06926 }
06927 }
06928
06929 return type;
06930 }
06931
06932
06933 static char *get_body(struct sip_request *req, char *name, char delimiter)
06934 {
06935 int x;
06936 int len = strlen(name);
06937 char *r;
06938
06939 for (x = 0; x < req->lines; x++) {
06940 r = get_body_by_line(REQ_OFFSET_TO_STR(req, line[x]), name, len, delimiter);
06941 if (r[0] != '\0')
06942 return r;
06943 }
06944
06945 return "";
06946 }
06947
06948
06949 static const char *find_alias(const char *name, const char *_default)
06950 {
06951
06952 static const struct cfalias {
06953 char * const fullname;
06954 char * const shortname;
06955 } aliases[] = {
06956 { "Content-Type", "c" },
06957 { "Content-Encoding", "e" },
06958 { "From", "f" },
06959 { "Call-ID", "i" },
06960 { "Contact", "m" },
06961 { "Content-Length", "l" },
06962 { "Subject", "s" },
06963 { "To", "t" },
06964 { "Supported", "k" },
06965 { "Refer-To", "r" },
06966 { "Referred-By", "b" },
06967 { "Allow-Events", "u" },
06968 { "Event", "o" },
06969 { "Via", "v" },
06970 { "Accept-Contact", "a" },
06971 { "Reject-Contact", "j" },
06972 { "Request-Disposition", "d" },
06973 { "Session-Expires", "x" },
06974 { "Identity", "y" },
06975 { "Identity-Info", "n" },
06976 };
06977 int x;
06978
06979 for (x = 0; x < ARRAY_LEN(aliases); x++) {
06980 if (!strcasecmp(aliases[x].fullname, name))
06981 return aliases[x].shortname;
06982 }
06983
06984 return _default;
06985 }
06986
06987 static const char *__get_header(const struct sip_request *req, const char *name, int *start)
06988 {
06989 int pass;
06990
06991
06992
06993
06994
06995
06996
06997
06998
06999
07000 for (pass = 0; name && pass < 2;pass++) {
07001 int x, len = strlen(name);
07002 for (x = *start; x < req->headers; x++) {
07003 const char *header = REQ_OFFSET_TO_STR(req, header[x]);
07004 if (!strncasecmp(header, name, len)) {
07005 const char *r = header + len;
07006 if (sip_cfg.pedanticsipchecking)
07007 r = ast_skip_blanks(r);
07008
07009 if (*r == ':') {
07010 *start = x+1;
07011 return ast_skip_blanks(r+1);
07012 }
07013 }
07014 }
07015 if (pass == 0)
07016 name = find_alias(name, NULL);
07017 }
07018
07019
07020 return "";
07021 }
07022
07023
07024
07025
07026 static const char *get_header(const struct sip_request *req, const char *name)
07027 {
07028 int start = 0;
07029 return __get_header(req, name, &start);
07030 }
07031
07032
07033 static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
07034 {
07035
07036 struct ast_frame *f;
07037
07038 if (!p->rtp) {
07039
07040 return &ast_null_frame;
07041 }
07042
07043 switch(ast->fdno) {
07044 case 0:
07045 f = ast_rtp_instance_read(p->rtp, 0);
07046 break;
07047 case 1:
07048 f = ast_rtp_instance_read(p->rtp, 1);
07049 break;
07050 case 2:
07051 f = ast_rtp_instance_read(p->vrtp, 0);
07052 break;
07053 case 3:
07054 f = ast_rtp_instance_read(p->vrtp, 1);
07055 break;
07056 case 4:
07057 f = ast_rtp_instance_read(p->trtp, 0);
07058 if (sipdebug_text) {
07059 int i;
07060 unsigned char* arr = f->data.ptr;
07061 for (i=0; i < f->datalen; i++)
07062 ast_verbose("%c", (arr[i] > ' ' && arr[i] < '}') ? arr[i] : '.');
07063 ast_verbose(" -> ");
07064 for (i=0; i < f->datalen; i++)
07065 ast_verbose("%02X ", arr[i]);
07066 ast_verbose("\n");
07067 }
07068 break;
07069 case 5:
07070 f = ast_udptl_read(p->udptl);
07071 break;
07072 default:
07073 f = &ast_null_frame;
07074 }
07075
07076 if (f && (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) &&
07077 (ast_test_flag(&p->flags[0], SIP_DTMF) != SIP_DTMF_RFC2833)) {
07078 ast_debug(1, "Ignoring DTMF (%c) RTP frame because dtmfmode is not RFC2833\n", f->subclass.integer);
07079 return &ast_null_frame;
07080 }
07081
07082
07083 if (!p->owner || (f && f->frametype != AST_FRAME_VOICE))
07084 return f;
07085
07086 if (f && f->subclass.codec != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
07087 if (!(f->subclass.codec & p->jointcapability)) {
07088 ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
07089 ast_getformatname(f->subclass.codec), p->owner->name);
07090 return &ast_null_frame;
07091 }
07092 ast_debug(1, "Oooh, format changed to %s\n",
07093 ast_getformatname(f->subclass.codec));
07094 p->owner->nativeformats = (p->owner->nativeformats & (AST_FORMAT_VIDEO_MASK | AST_FORMAT_TEXT_MASK)) | f->subclass.codec;
07095 ast_set_read_format(p->owner, p->owner->readformat);
07096 ast_set_write_format(p->owner, p->owner->writeformat);
07097 }
07098
07099 if (f && p->dsp) {
07100 f = ast_dsp_process(p->owner, p->dsp, f);
07101 if (f && f->frametype == AST_FRAME_DTMF) {
07102 if (f->subclass.integer == 'f') {
07103 ast_debug(1, "Fax CNG detected on %s\n", ast->name);
07104 *faxdetect = 1;
07105
07106 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) {
07107 ast_dsp_set_features(p->dsp, DSP_FEATURE_DIGIT_DETECT);
07108 } else {
07109 ast_dsp_free(p->dsp);
07110 p->dsp = NULL;
07111 }
07112 } else {
07113 ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass.integer);
07114 }
07115 }
07116 }
07117
07118 return f;
07119 }
07120
07121
07122 static struct ast_frame *sip_read(struct ast_channel *ast)
07123 {
07124 struct ast_frame *fr;
07125 struct sip_pvt *p = ast->tech_pvt;
07126 int faxdetected = FALSE;
07127
07128 sip_pvt_lock(p);
07129 fr = sip_rtp_read(ast, p, &faxdetected);
07130 p->lastrtprx = time(NULL);
07131
07132
07133 if (faxdetected && ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_CNG)) {
07134 ast_channel_lock(ast);
07135 if (strcmp(ast->exten, "fax")) {
07136 const char *target_context = S_OR(ast->macrocontext, ast->context);
07137 ast_channel_unlock(ast);
07138 if (ast_exists_extension(ast, target_context, "fax", 1,
07139 S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, NULL))) {
07140 ast_verbose(VERBOSE_PREFIX_2 "Redirecting '%s' to fax extension due to CNG detection\n", ast->name);
07141 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
07142 if (ast_async_goto(ast, target_context, "fax", 1)) {
07143 ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
07144 }
07145 fr = &ast_null_frame;
07146 } else {
07147 ast_log(LOG_NOTICE, "FAX CNG detected but no fax extension\n");
07148 }
07149 } else {
07150 ast_channel_unlock(ast);
07151 }
07152 }
07153
07154
07155 if (fr && fr->frametype == AST_FRAME_VOICE && p->invitestate != INV_EARLY_MEDIA && ast->_state != AST_STATE_UP) {
07156 fr = &ast_null_frame;
07157 }
07158
07159 sip_pvt_unlock(p);
07160
07161 return fr;
07162 }
07163
07164
07165
07166 static char *generate_random_string(char *buf, size_t size)
07167 {
07168 long val[4];
07169 int x;
07170
07171 for (x=0; x<4; x++)
07172 val[x] = ast_random();
07173 snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
07174
07175 return buf;
07176 }
07177
07178 static char *generate_uri(struct sip_pvt *pvt, char *buf, size_t size)
07179 {
07180 struct ast_str *uri = ast_str_alloca(size);
07181 ast_str_set(&uri, 0, "%s", pvt->socket.type == SIP_TRANSPORT_TLS ? "sips:" : "sip:");
07182
07183
07184
07185 ast_str_append(&uri, 0, "%s", generate_random_string(buf, size));
07186 ast_str_append(&uri, 0, "@%s", ast_sockaddr_stringify(&pvt->ourip));
07187 ast_copy_string(buf, ast_str_buffer(uri), size);
07188 return buf;
07189 }
07190
07191
07192 static void build_callid_pvt(struct sip_pvt *pvt)
07193 {
07194 char buf[33];
07195
07196 const char *host = S_OR(pvt->fromdomain, ast_sockaddr_stringify(&pvt->ourip));
07197
07198 ast_string_field_build(pvt, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
07199
07200 }
07201
07202
07203 static void build_callid_registry(struct sip_registry *reg, const struct ast_sockaddr *ourip, const char *fromdomain)
07204 {
07205 char buf[33];
07206
07207 const char *host = S_OR(fromdomain, ast_sockaddr_stringify_host(ourip));
07208
07209 ast_string_field_build(reg, callid, "%s@%s", generate_random_string(buf, sizeof(buf)), host);
07210 }
07211
07212
07213 static void make_our_tag(char *tagbuf, size_t len)
07214 {
07215 snprintf(tagbuf, len, "as%08lx", ast_random());
07216 }
07217
07218
07219 static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p)
07220 {
07221 struct sip_st_dlg *stp;
07222
07223 if (p->stimer) {
07224 ast_log(LOG_ERROR, "Session-Timer struct already allocated\n");
07225 return p->stimer;
07226 }
07227
07228 if (!(stp = ast_calloc(1, sizeof(struct sip_st_dlg))))
07229 return NULL;
07230
07231 p->stimer = stp;
07232
07233 stp->st_schedid = -1;
07234
07235 return p->stimer;
07236 }
07237
07238
07239
07240
07241
07242 struct sip_pvt *sip_alloc(ast_string_field callid, struct ast_sockaddr *addr,
07243 int useglobal_nat, const int intended_method, struct sip_request *req)
07244 {
07245 struct sip_pvt *p;
07246
07247 if (!(p = ao2_t_alloc(sizeof(*p), sip_destroy_fn, "allocate a dialog(pvt) struct")))
07248 return NULL;
07249
07250 if (ast_string_field_init(p, 512)) {
07251 ao2_t_ref(p, -1, "failed to string_field_init, drop p");
07252 return NULL;
07253 }
07254
07255 if (!(p->cc_params = ast_cc_config_params_init())) {
07256 ao2_t_ref(p, -1, "Yuck, couldn't allocate cc_params struct. Get rid o' p");
07257 return NULL;
07258 }
07259
07260
07261
07262 if (req) {
07263 struct sip_via *via;
07264 const char *cseq = get_header(req, "Cseq");
07265 unsigned int seqno;
07266
07267
07268 via = parse_via(get_header(req, "Via"));
07269 if (via) {
07270
07271
07272 if (!ast_strlen_zero(via->branch) && !strncasecmp(via->branch, "z9hG4bK", 7)) {
07273 ast_string_field_set(p, initviabranch, via->branch);
07274 ast_string_field_set(p, initviasentby, via->sent_by);
07275 }
07276 free_via(via);
07277 }
07278
07279
07280
07281 if (!ast_strlen_zero(cseq) && (sscanf(cseq, "%30u", &seqno) == 1)) {
07282 p->init_icseq = seqno;
07283 }
07284
07285 set_socket_transport(&p->socket, req->socket.type);
07286 } else {
07287 set_socket_transport(&p->socket, SIP_TRANSPORT_UDP);
07288 }
07289
07290 p->socket.fd = -1;
07291 p->method = intended_method;
07292 p->initid = -1;
07293 p->waitid = -1;
07294 p->autokillid = -1;
07295 p->request_queue_sched_id = -1;
07296 p->provisional_keepalive_sched_id = -1;
07297 p->t38id = -1;
07298 p->subscribed = NONE;
07299 p->stateid = -1;
07300 p->sessionversion_remote = -1;
07301 p->session_modify = TRUE;
07302 p->stimer = NULL;
07303 p->prefs = default_prefs;
07304 p->maxforwards = sip_cfg.default_max_forwards;
07305
07306 if (intended_method != SIP_OPTIONS) {
07307 p->timer_t1 = global_t1;
07308 p->timer_b = global_timer_b;
07309 }
07310
07311 if (!addr) {
07312 p->ourip = internip;
07313 } else {
07314 ast_sockaddr_copy(&p->sa, addr);
07315 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
07316 }
07317
07318
07319 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
07320 ast_copy_flags(&p->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
07321 ast_copy_flags(&p->flags[2], &global_flags[2], SIP_PAGE3_FLAGS_TO_COPY);
07322
07323 p->do_history = recordhistory;
07324
07325 p->branch = ast_random();
07326 make_our_tag(p->tag, sizeof(p->tag));
07327 p->ocseq = INITIAL_CSEQ;
07328 p->allowed_methods = UINT_MAX;
07329
07330 if (sip_methods[intended_method].need_rtp) {
07331 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT)) {
07332 if ((p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &bindaddr))) {
07333 ast_udptl_setqos(p->udptl, global_tos_audio, global_cos_audio);
07334 p->t38_maxdatagram = global_t38_maxdatagram;
07335 } else {
07336
07337 ast_log(LOG_ERROR, "UDPTL creation failed\n");
07338 ast_clear_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT);
07339 }
07340 }
07341 p->maxcallbitrate = default_maxcallbitrate;
07342 p->autoframing = global_autoframing;
07343 }
07344
07345 if (useglobal_nat && addr) {
07346
07347 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT_FORCE_RPORT);
07348 ast_sockaddr_copy(&p->recv, addr);
07349
07350 do_setnat(p);
07351 }
07352
07353 if (p->method != SIP_REGISTER) {
07354 ast_string_field_set(p, fromdomain, default_fromdomain);
07355 p->fromdomainport = default_fromdomainport;
07356 }
07357 build_via(p);
07358 if (!callid)
07359 build_callid_pvt(p);
07360 else
07361 ast_string_field_set(p, callid, callid);
07362
07363 ast_string_field_set(p, mohinterpret, default_mohinterpret);
07364 ast_string_field_set(p, mohsuggest, default_mohsuggest);
07365 p->capability = sip_cfg.capability;
07366 p->allowtransfer = sip_cfg.allowtransfer;
07367 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
07368 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
07369 p->noncodeccapability |= AST_RTP_DTMF;
07370 if (p->udptl) {
07371 p->t38_maxdatagram = global_t38_maxdatagram;
07372 set_t38_capabilities(p);
07373 }
07374 ast_string_field_set(p, context, sip_cfg.default_context);
07375 ast_string_field_set(p, parkinglot, default_parkinglot);
07376 ast_string_field_set(p, engine, default_engine);
07377
07378 AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
07379
07380
07381
07382 ao2_t_link(dialogs, p, "link pvt into dialogs table");
07383
07384 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");
07385 return p;
07386 }
07387
07388
07389
07390
07391
07392
07393
07394
07395 static int addr_is_multicast(const struct ast_sockaddr *addr)
07396 {
07397 return ((ast_sockaddr_ipv4(addr) & 0xf0000000) == 0xe0000000);
07398 }
07399
07400
07401
07402
07403
07404
07405
07406
07407
07408
07409
07410
07411
07412
07413
07414
07415 static int process_via(struct sip_pvt *p, const struct sip_request *req)
07416 {
07417 struct sip_via *via = parse_via(get_header(req, "Via"));
07418
07419 if (!via) {
07420 ast_log(LOG_ERROR, "error processing via header\n");
07421 return -1;
07422 }
07423
07424 if (via->maddr) {
07425 if (ast_sockaddr_resolve_first(&p->sa, via->maddr, PARSE_PORT_FORBID)) {
07426 ast_log(LOG_WARNING, "Can't find address for maddr '%s'\n", via->maddr);
07427 ast_log(LOG_ERROR, "error processing via header\n");
07428 free_via(via);
07429 return -1;
07430 }
07431
07432 if (addr_is_multicast(&p->sa)) {
07433 setsockopt(sipsock, IPPROTO_IP, IP_MULTICAST_TTL, &via->ttl, sizeof(via->ttl));
07434 }
07435 }
07436
07437 ast_sockaddr_set_port(&p->sa, via->port ? via->port : STANDARD_SIP_PORT);
07438
07439 free_via(via);
07440 return 0;
07441 }
07442
07443
07444 struct match_req_args {
07445 int method;
07446 const char *callid;
07447 const char *totag;
07448 const char *fromtag;
07449 unsigned int seqno;
07450
07451
07452 const char *ruri;
07453 const char *viabranch;
07454 const char *viasentby;
07455
07456
07457 int authentication_present;
07458 };
07459
07460 enum match_req_res {
07461 SIP_REQ_MATCH,
07462 SIP_REQ_NOT_MATCH,
07463 SIP_REQ_LOOP_DETECTED,
07464 };
07465
07466
07467
07468
07469
07470
07471 static enum match_req_res match_req_to_dialog(struct sip_pvt *sip_pvt_ptr, struct match_req_args *arg)
07472 {
07473 const char *init_ruri = NULL;
07474 if (sip_pvt_ptr->initreq.headers) {
07475 init_ruri = REQ_OFFSET_TO_STR(&sip_pvt_ptr->initreq, rlPart2);
07476 }
07477
07478
07479
07480
07481 if (!ast_strlen_zero(arg->callid) && strcmp(sip_pvt_ptr->callid, arg->callid)) {
07482
07483 return SIP_REQ_NOT_MATCH;
07484 }
07485 if (arg->method == SIP_RESPONSE) {
07486
07487
07488 if (!ast_strlen_zero(sip_pvt_ptr->theirtag) && ast_test_flag(&sip_pvt_ptr->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED)) {
07489 if (ast_strlen_zero(arg->totag)) {
07490
07491 return SIP_REQ_NOT_MATCH;
07492 }
07493 if (strcmp(arg->totag, sip_pvt_ptr->theirtag)) {
07494
07495 return SIP_REQ_NOT_MATCH;
07496 }
07497 }
07498
07499 if (strcmp(arg->fromtag, sip_pvt_ptr->tag)) {
07500
07501 return SIP_REQ_NOT_MATCH;
07502 }
07503 } else {
07504
07505
07506
07507 if (!arg->authentication_present && strcmp(arg->fromtag, sip_pvt_ptr->theirtag)) {
07508
07509 return SIP_REQ_NOT_MATCH;
07510 }
07511
07512 if (!ast_strlen_zero(arg->totag) && (strcmp(arg->totag, sip_pvt_ptr->tag))) {
07513
07514 return SIP_REQ_NOT_MATCH;
07515 }
07516 }
07517
07518
07519
07520
07521
07522
07523
07524
07525
07526
07527
07528
07529
07530
07531
07532
07533
07534 if ((arg->method != SIP_RESPONSE) &&
07535 ast_strlen_zero(arg->totag) &&
07536 (sip_pvt_ptr->init_icseq == arg->seqno) &&
07537 !ast_strlen_zero(sip_pvt_ptr->initviabranch) &&
07538 init_ruri) {
07539
07540
07541 if (ast_strlen_zero(arg->viabranch) ||
07542 strcmp(arg->viabranch, sip_pvt_ptr->initviabranch) ||
07543 ast_strlen_zero(arg->viasentby) ||
07544 strcmp(arg->viasentby, sip_pvt_ptr->initviasentby)) {
07545
07546
07547
07548 if ((sip_pvt_ptr->method != arg->method)) {
07549 return SIP_REQ_NOT_MATCH;
07550 }
07551
07552
07553
07554 if (sip_uri_cmp(init_ruri, arg->ruri)) {
07555
07556 return SIP_REQ_NOT_MATCH;
07557 }
07558
07559
07560
07561
07562
07563
07564
07565
07566
07567
07568
07569
07570
07571
07572
07573
07574
07575
07576
07577 return SIP_REQ_LOOP_DETECTED;
07578 }
07579 }
07580
07581
07582
07583
07584
07585
07586 if ((arg->method != SIP_RESPONSE) &&
07587 ast_strlen_zero(arg->totag) &&
07588 arg->authentication_present &&
07589 sip_uri_cmp(init_ruri, arg->ruri)) {
07590
07591
07592 return SIP_REQ_NOT_MATCH;
07593 }
07594
07595 return SIP_REQ_MATCH;
07596 }
07597
07598
07599
07600
07601
07602
07603 static struct sip_pvt *find_call(struct sip_request *req, struct ast_sockaddr *addr, const int intended_method)
07604 {
07605 char totag[128];
07606 char fromtag[128];
07607 const char *callid = get_header(req, "Call-ID");
07608 const char *from = get_header(req, "From");
07609 const char *to = get_header(req, "To");
07610 const char *cseq = get_header(req, "Cseq");
07611 struct sip_pvt *sip_pvt_ptr;
07612 unsigned int seqno;
07613
07614
07615 if (ast_strlen_zero(callid) || ast_strlen_zero(to) ||
07616 ast_strlen_zero(from) || ast_strlen_zero(cseq) ||
07617 (sscanf(cseq, "%30u", &seqno) != 1)) {
07618
07619
07620 if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
07621 transmit_response_using_temp(callid, addr, 1, intended_method,
07622 req, "400 Bad Request");
07623 }
07624 return NULL;
07625 }
07626
07627 if (sip_cfg.pedanticsipchecking) {
07628
07629
07630
07631
07632
07633
07634 if (gettag(req, "To", totag, sizeof(totag)))
07635 req->has_to_tag = 1;
07636 gettag(req, "From", fromtag, sizeof(fromtag));
07637
07638 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);
07639
07640
07641 if (ast_strlen_zero(fromtag)) {
07642 ast_debug(5, "%s request has no from tag, dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
07643 return NULL;
07644 }
07645
07646 if (ast_strlen_zero(totag) && (req->method == SIP_ACK || req->method == SIP_BYE || req->method == SIP_INFO )) {
07647 ast_debug(5, "%s must have a to tag. dropping callid: %s from: %s\n", sip_methods[req->method].text , callid, from );
07648 return NULL;
07649 }
07650 }
07651
07652 if (!sip_cfg.pedanticsipchecking) {
07653 struct sip_pvt tmp_dialog = {
07654 .callid = callid,
07655 };
07656 sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find in dialogs");
07657 if (sip_pvt_ptr) {
07658
07659 sip_pvt_lock(sip_pvt_ptr);
07660 return sip_pvt_ptr;
07661 }
07662 } else {
07663 struct sip_pvt tmp_dialog = {
07664 .callid = callid,
07665 };
07666 struct match_req_args args = { 0, };
07667 int found;
07668 struct ao2_iterator *iterator = ao2_t_callback(dialogs,
07669 OBJ_POINTER | OBJ_MULTIPLE,
07670 dialog_find_multiple,
07671 &tmp_dialog,
07672 "pedantic ao2_find in dialogs");
07673 struct sip_via *via = NULL;
07674
07675 args.method = req->method;
07676 args.callid = NULL;
07677 args.totag = totag;
07678 args.fromtag = fromtag;
07679 args.seqno = seqno;
07680
07681
07682 if (req->method != SIP_RESPONSE) {
07683 args.ruri = REQ_OFFSET_TO_STR(req, rlPart2);
07684 via = parse_via(get_header(req, "Via"));
07685 if (via) {
07686 args.viasentby = via->sent_by;
07687 args.viabranch = via->branch;
07688 }
07689 if (!ast_strlen_zero(get_header(req, "Authorization")) ||
07690 !ast_strlen_zero(get_header(req, "Proxy-Authorization"))) {
07691 args.authentication_present = 1;
07692 }
07693 }
07694
07695
07696 while (iterator && (sip_pvt_ptr = ao2_iterator_next(iterator))) {
07697 found = match_req_to_dialog(sip_pvt_ptr, &args);
07698
07699 switch (found) {
07700 case SIP_REQ_MATCH:
07701 sip_pvt_lock(sip_pvt_ptr);
07702 ao2_iterator_destroy(iterator);
07703 free_via(via);
07704 return sip_pvt_ptr;
07705 case SIP_REQ_LOOP_DETECTED:
07706
07707
07708 transmit_response_using_temp(callid, addr, 1, intended_method, req, "482 (Loop Detected)");
07709 dialog_unref(sip_pvt_ptr, "pvt did not match incoming SIP msg, unref from search.");
07710 ao2_iterator_destroy(iterator);
07711 free_via(via);
07712 return NULL;
07713 case SIP_REQ_NOT_MATCH:
07714 default:
07715 dialog_unref(sip_pvt_ptr, "pvt did not match incoming SIP msg, unref from search");
07716 }
07717 }
07718 if (iterator) {
07719 ao2_iterator_destroy(iterator);
07720 }
07721
07722 free_via(via);
07723 }
07724
07725
07726 if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
07727 struct sip_pvt *p = NULL;
07728
07729 if (intended_method == SIP_REFER) {
07730
07731 transmit_response_using_temp(callid, addr, 1, intended_method, req, "603 Declined (no dialog)");
07732 } else {
07733
07734 if ((p = sip_alloc(callid, addr, 1, intended_method, req))) {
07735
07736 sip_pvt_lock(p);
07737 } else {
07738
07739
07740
07741
07742
07743
07744
07745
07746 transmit_response_using_temp(callid, addr, 1, intended_method, req, "500 Server internal error");
07747 ast_debug(4, "Failed allocating SIP dialog, sending 500 Server internal error and giving up\n");
07748 }
07749 }
07750 return p;
07751 } else if( sip_methods[intended_method].can_create == CAN_CREATE_DIALOG_UNSUPPORTED_METHOD) {
07752
07753 transmit_response_using_temp(callid, addr, 1, intended_method, req, "501 Method Not Implemented");
07754 ast_debug(2, "Got a request with unsupported SIP method.\n");
07755 } else if (intended_method != SIP_RESPONSE && intended_method != SIP_ACK) {
07756
07757 transmit_response_using_temp(callid, addr, 1, intended_method, req, "481 Call leg/transaction does not exist");
07758 ast_debug(2, "That's odd... Got a request in unknown dialog. Callid %s\n", callid ? callid : "<unknown>");
07759 }
07760
07761
07762 if (intended_method == SIP_RESPONSE)
07763 ast_debug(2, "That's odd... Got a response on a call we don't know about. Callid %s\n", callid ? callid : "<unknown>");
07764
07765 return NULL;
07766 }
07767
07768
07769 static int sip_register(const char *value, int lineno)
07770 {
07771 struct sip_registry *reg;
07772
07773 if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
07774 ast_log(LOG_ERROR, "Out of memory. Can't allocate SIP registry entry\n");
07775 return -1;
07776 }
07777
07778 ast_atomic_fetchadd_int(®objs, 1);
07779 ASTOBJ_INIT(reg);
07780
07781 if (sip_parse_register_line(reg, default_expiry, value, lineno)) {
07782 registry_unref(reg, "failure to parse, unref the reg pointer");
07783 return -1;
07784 }
07785
07786
07787 if (reg->refresh && !reg->expiry && !reg->configured_expiry) {
07788 reg->refresh = reg->expiry = reg->configured_expiry = default_expiry;
07789 }
07790
07791
07792 ASTOBJ_CONTAINER_LINK(®l, reg);
07793
07794
07795 registry_unref(reg, "unref the reg pointer");
07796
07797 return 0;
07798 }
07799
07800
07801 static int sip_subscribe_mwi(const char *value, int lineno)
07802 {
07803 struct sip_subscription_mwi *mwi;
07804 int portnum = 0;
07805 enum sip_transport transport = SIP_TRANSPORT_UDP;
07806 char buf[256] = "";
07807 char *username = NULL, *hostname = NULL, *secret = NULL, *authuser = NULL, *porta = NULL, *mailbox = NULL, *at = NULL;
07808
07809 if (!value) {
07810 return -1;
07811 }
07812
07813 ast_copy_string(buf, value, sizeof(buf));
07814
07815 if (!(at = strstr(buf, "@"))) {
07816 return -1;
07817 }
07818
07819 if ((hostname = strrchr(buf, '@'))) {
07820 *hostname++ = '\0';
07821 username = buf;
07822 }
07823
07824 if ((secret = strchr(username, ':'))) {
07825 *secret++ = '\0';
07826 if ((authuser = strchr(secret, ':'))) {
07827 *authuser++ = '\0';
07828 }
07829 }
07830
07831 if ((mailbox = strchr(hostname, '/'))) {
07832 *mailbox++ = '\0';
07833 }
07834
07835 if (ast_strlen_zero(username) || ast_strlen_zero(hostname) || ast_strlen_zero(mailbox)) {
07836 ast_log(LOG_WARNING, "Format for MWI subscription is user[:secret[:authuser]]@host[:port]/mailbox at line %d\n", lineno);
07837 return -1;
07838 }
07839
07840 if ((porta = strchr(hostname, ':'))) {
07841 *porta++ = '\0';
07842 if (!(portnum = atoi(porta))) {
07843 ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
07844 return -1;
07845 }
07846 }
07847
07848 if (!(mwi = ast_calloc_with_stringfields(1, struct sip_subscription_mwi, 256))) {
07849 return -1;
07850 }
07851
07852 ASTOBJ_INIT(mwi);
07853 ast_string_field_set(mwi, username, username);
07854 if (secret) {
07855 ast_string_field_set(mwi, secret, secret);
07856 }
07857 if (authuser) {
07858 ast_string_field_set(mwi, authuser, authuser);
07859 }
07860 ast_string_field_set(mwi, hostname, hostname);
07861 ast_string_field_set(mwi, mailbox, mailbox);
07862 mwi->resub = -1;
07863 mwi->portno = portnum;
07864 mwi->transport = transport;
07865
07866 ASTOBJ_CONTAINER_LINK(&submwil, mwi);
07867 ASTOBJ_UNREF(mwi, sip_subscribe_mwi_destroy);
07868
07869 return 0;
07870 }
07871
07872 static void mark_method_allowed(unsigned int *allowed_methods, enum sipmethod method)
07873 {
07874 (*allowed_methods) |= (1 << method);
07875 }
07876
07877 static void mark_method_unallowed(unsigned int *allowed_methods, enum sipmethod method)
07878 {
07879 (*allowed_methods) &= ~(1 << method);
07880 }
07881
07882
07883 static int is_method_allowed(unsigned int *allowed_methods, enum sipmethod method)
07884 {
07885 return ((*allowed_methods) >> method) & 1;
07886 }
07887
07888 static void mark_parsed_methods(unsigned int *methods, char *methods_str)
07889 {
07890 char *method;
07891 for (method = strsep(&methods_str, ","); !ast_strlen_zero(method); method = strsep(&methods_str, ",")) {
07892 int id = find_sip_method(ast_skip_blanks(method));
07893 if (id == SIP_UNKNOWN) {
07894 continue;
07895 }
07896 mark_method_allowed(methods, id);
07897 }
07898 }
07899
07900
07901
07902
07903
07904
07905
07906
07907
07908
07909
07910
07911
07912
07913
07914
07915 static unsigned int parse_allowed_methods(struct sip_request *req)
07916 {
07917 char *allow = ast_strdupa(get_header(req, "Allow"));
07918 unsigned int allowed_methods = SIP_UNKNOWN;
07919
07920 if (ast_strlen_zero(allow)) {
07921
07922
07923
07924
07925 char *contact = ast_strdupa(get_header(req, "Contact"));
07926 char *methods = strstr(contact, ";methods=");
07927
07928 if (ast_strlen_zero(methods)) {
07929
07930
07931
07932
07933
07934
07935
07936
07937
07938
07939
07940 return UINT_MAX;
07941 }
07942 allow = ast_strip_quoted(methods + 9, "\"", "\"");
07943 }
07944 mark_parsed_methods(&allowed_methods, allow);
07945 return allowed_methods;
07946 }
07947
07948
07949
07950
07951
07952
07953
07954
07955
07956
07957 static unsigned int set_pvt_allowed_methods(struct sip_pvt *pvt, struct sip_request *req)
07958 {
07959 pvt->allowed_methods = parse_allowed_methods(req);
07960
07961 if (ast_test_flag(&pvt->flags[1], SIP_PAGE2_RPID_UPDATE)) {
07962 mark_method_allowed(&pvt->allowed_methods, SIP_UPDATE);
07963 }
07964 pvt->allowed_methods &= ~(pvt->disallowed_methods);
07965
07966 return pvt->allowed_methods;
07967 }
07968
07969
07970
07971 static int lws2sws(char *msgbuf, int len)
07972 {
07973 int h = 0, t = 0;
07974 int lws = 0;
07975
07976 for (; h < len;) {
07977
07978 if (msgbuf[h] == '\r') {
07979 h++;
07980 continue;
07981 }
07982
07983 if (msgbuf[h] == '\n') {
07984
07985 if (h + 1 == len)
07986 break;
07987
07988 if (msgbuf[h + 1] == ' ' || msgbuf[h + 1] == '\t') {
07989
07990 h++;
07991 continue;
07992 }
07993
07994 msgbuf[t++] = msgbuf[h++];
07995 lws = 0;
07996 continue;
07997 }
07998 if (msgbuf[h] == ' ' || msgbuf[h] == '\t') {
07999 if (lws) {
08000 h++;
08001 continue;
08002 }
08003 msgbuf[t++] = msgbuf[h++];
08004 lws = 1;
08005 continue;
08006 }
08007 msgbuf[t++] = msgbuf[h++];
08008 if (lws)
08009 lws = 0;
08010 }
08011 msgbuf[t] = '\0';
08012 return t;
08013 }
08014
08015
08016
08017
08018 static int parse_request(struct sip_request *req)
08019 {
08020 char *c = req->data->str;
08021 ptrdiff_t *dst = req->header;
08022 int i = 0, lim = SIP_MAX_HEADERS - 1;
08023 unsigned int skipping_headers = 0;
08024 ptrdiff_t current_header_offset = 0;
08025 char *previous_header = "";
08026
08027 req->header[0] = 0;
08028 req->headers = -1;
08029 for (; *c; c++) {
08030 if (*c == '\r') {
08031 *c = '\0';
08032 } else if (*c == '\n') {
08033 *c = '\0';
08034 current_header_offset = (c + 1) - req->data->str;
08035 previous_header = req->data->str + dst[i];
08036 if (skipping_headers) {
08037
08038
08039
08040 if (ast_strlen_zero(previous_header)) {
08041 skipping_headers = 0;
08042 }
08043 dst[i] = current_header_offset;
08044 continue;
08045 }
08046 if (sipdebug) {
08047 ast_debug(4, "%7s %2d [%3d]: %s\n",
08048 req->headers < 0 ? "Header" : "Body",
08049 i, (int) strlen(previous_header), previous_header);
08050 }
08051 if (ast_strlen_zero(previous_header) && req->headers < 0) {
08052 req->headers = i;
08053 dst = req->line;
08054 i = 0;
08055 lim = SIP_MAX_LINES - 1;
08056 } else {
08057 if (i++ == lim) {
08058
08059
08060
08061 if (req->headers != -1) {
08062 break;
08063 } else {
08064 req->headers = i;
08065 dst = req->line;
08066 i = 0;
08067 lim = SIP_MAX_LINES - 1;
08068 skipping_headers = 1;
08069 }
08070 }
08071 }
08072 dst[i] = current_header_offset;
08073 }
08074 }
08075
08076
08077
08078
08079
08080
08081 previous_header = req->data->str + dst[i];
08082 if ((i < lim) && !ast_strlen_zero(previous_header)) {
08083 if (sipdebug) {
08084 ast_debug(4, "%7s %2d [%3d]: %s\n",
08085 req->headers < 0 ? "Header" : "Body",
08086 i, (int) strlen(previous_header), previous_header );
08087 }
08088 i++;
08089 }
08090
08091
08092 if (req->headers >= 0) {
08093 req->lines = i;
08094 } else {
08095 req->headers = i;
08096 req->lines = 0;
08097
08098 req->line[0] = ast_str_strlen(req->data);
08099 }
08100
08101 if (*c) {
08102 ast_log(LOG_WARNING, "Too many lines, skipping <%s>\n", c);
08103 }
08104
08105
08106 return determine_firstline_parts(req);
08107 }
08108
08109
08110
08111
08112
08113
08114
08115
08116
08117 static int find_sdp(struct sip_request *req)
08118 {
08119 const char *content_type;
08120 const char *content_length;
08121 const char *search;
08122 char *boundary;
08123 unsigned int x;
08124 int boundaryisquoted = FALSE;
08125 int found_application_sdp = FALSE;
08126 int found_end_of_headers = FALSE;
08127
08128 content_length = get_header(req, "Content-Length");
08129
08130 if (!ast_strlen_zero(content_length)) {
08131 if (sscanf(content_length, "%30u", &x) != 1) {
08132 ast_log(LOG_WARNING, "Invalid Content-Length: %s\n", content_length);
08133 return 0;
08134 }
08135
08136
08137
08138 if (x == 0)
08139 return 0;
08140 }
08141
08142 content_type = get_header(req, "Content-Type");
08143
08144
08145 if (!strncasecmp(content_type, "application/sdp", 15)) {
08146 req->sdp_start = 0;
08147 req->sdp_count = req->lines;
08148 return req->lines ? 1 : 0;
08149 }
08150
08151
08152 if (strncasecmp(content_type, "multipart/mixed", 15))
08153 return 0;
08154
08155
08156 if ((search = strcasestr(content_type, ";boundary=")))
08157 search += 10;
08158 else if ((search = strcasestr(content_type, "; boundary=")))
08159 search += 11;
08160 else
08161 return 0;
08162
08163 if (ast_strlen_zero(search))
08164 return 0;
08165
08166
08167 if (*search == '\"') {
08168 search++;
08169 boundaryisquoted = TRUE;
08170 }
08171
08172
08173
08174 boundary = ast_strdupa(search - 2);
08175 boundary[0] = boundary[1] = '-';
08176
08177 if (boundaryisquoted)
08178 boundary[strlen(boundary) - 1] = '\0';
08179
08180
08181
08182
08183 for (x = 0; x < (req->lines); x++) {
08184 const char *line = REQ_OFFSET_TO_STR(req, line[x]);
08185 if (!strncasecmp(line, boundary, strlen(boundary))){
08186 if (found_application_sdp && found_end_of_headers) {
08187 req->sdp_count = (x - 1) - req->sdp_start;
08188 return 1;
08189 }
08190 found_application_sdp = FALSE;
08191 }
08192 if (!strcasecmp(line, "Content-Type: application/sdp"))
08193 found_application_sdp = TRUE;
08194
08195 if (ast_strlen_zero(line)) {
08196 if (found_application_sdp && !found_end_of_headers){
08197 req->sdp_start = x;
08198 found_end_of_headers = TRUE;
08199 }
08200 }
08201 }
08202 if (found_application_sdp && found_end_of_headers) {
08203 req->sdp_count = x - req->sdp_start;
08204 return TRUE;
08205 }
08206 return FALSE;
08207 }
08208
08209
08210 static void change_hold_state(struct sip_pvt *dialog, struct sip_request *req, int holdstate, int sendonly)
08211 {
08212 if (sip_cfg.notifyhold && (!holdstate || !ast_test_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD)))
08213 sip_peer_hold(dialog, holdstate);
08214 if (sip_cfg.callevents)
08215 manager_event(EVENT_FLAG_CALL, "Hold",
08216 "Status: %s\r\n"
08217 "Channel: %s\r\n"
08218 "Uniqueid: %s\r\n",
08219 holdstate ? "On" : "Off",
08220 dialog->owner->name,
08221 dialog->owner->uniqueid);
08222 append_history(dialog, holdstate ? "Hold" : "Unhold", "%s", req->data->str);
08223 if (!holdstate) {
08224 ast_clear_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD);
08225 return;
08226 }
08227
08228
08229 if (sendonly == 1)
08230 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ONEDIR);
08231 else if (sendonly == 2)
08232 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_INACTIVE);
08233 else
08234 ast_set_flag(&dialog->flags[1], SIP_PAGE2_CALL_ONHOLD_ACTIVE);
08235 return;
08236 }
08237
08238
08239 static int get_ip_and_port_from_sdp(struct sip_request *req, const enum media_type media, struct ast_sockaddr *addr)
08240 {
08241 const char *m;
08242 const char *c;
08243 int miterator = req->sdp_start;
08244 int citerator = req->sdp_start;
08245 int x = 0;
08246 int numberofports;
08247 int len;
08248 int af;
08249 char proto[4], host[258] = "";
08250
08251 c = get_sdp_iterate(&citerator, req, "c");
08252 if (sscanf(c, "IN %3s %256s", proto, host) != 2) {
08253 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
08254
08255 }
08256
08257 for (m = get_sdp_iterate(&miterator, req, "m"); !ast_strlen_zero(m); m = get_sdp_iterate(&miterator, req, "m")) {
08258 if ((media == SDP_AUDIO && ((sscanf(m, "audio %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
08259 (sscanf(m, "audio %30u RTP/AVP %n", &x, &len) == 1 && len > 0))) ||
08260 (media == SDP_VIDEO && ((sscanf(m, "video %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
08261 (sscanf(m, "video %30u RTP/AVP %n", &x, &len) == 1 && len > 0)))) {
08262
08263
08264
08265
08266 c = get_sdp_iterate(&citerator, req, "c");
08267 if (!ast_strlen_zero(c)) {
08268 sscanf(c, "IN %3s %256s", proto, host);
08269 }
08270 break;
08271 }
08272 }
08273
08274 if (!strcmp("IP4", proto)) {
08275 af = AF_INET;
08276 } else if (!strcmp("IP6", proto)) {
08277 af = AF_INET6;
08278 } else {
08279 ast_log(LOG_WARNING, "Unknown protocol '%s'.\n", proto);
08280 return -1;
08281 }
08282
08283 if (ast_strlen_zero(host) || x == 0) {
08284 ast_log(LOG_WARNING, "Failed to read an alternate host or port in SDP. Expect %s problems\n", media == SDP_AUDIO ? "audio" : "video");
08285 return -1;
08286 }
08287
08288 if (ast_sockaddr_resolve_first_af(addr, host, 0, af)) {
08289 ast_log(LOG_WARNING, "Could not look up IP address of alternate hostname. Expect %s problems\n", media == SDP_AUDIO? "audio" : "video");
08290 return -1;
08291 }
08292
08293 return 0;
08294 }
08295
08296
08297
08298
08299
08300
08301 static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
08302 {
08303
08304 int start = req->sdp_start;
08305 int next = start;
08306 int iterator = start;
08307
08308
08309 char type = '\0';
08310 const char *value = NULL;
08311 const char *m = NULL;
08312 const char *nextm = NULL;
08313 int len = -1;
08314
08315
08316 struct ast_sockaddr sessionsa;
08317 struct ast_sockaddr audiosa;
08318 struct ast_sockaddr videosa;
08319 struct ast_sockaddr textsa;
08320 struct ast_sockaddr imagesa;
08321 struct ast_sockaddr *sa = NULL;
08322 struct ast_sockaddr *vsa = NULL;
08323 struct ast_sockaddr *tsa = NULL;
08324 struct ast_sockaddr *isa = NULL;
08325 int portno = -1;
08326 int vportno = -1;
08327 int tportno = -1;
08328 int udptlportno = -1;
08329
08330
08331 format_t peercapability = 0, vpeercapability = 0, tpeercapability = 0;
08332 int peernoncodeccapability = 0, vpeernoncodeccapability = 0, tpeernoncodeccapability = 0;
08333
08334 struct ast_rtp_codecs newaudiortp, newvideortp, newtextrtp;
08335 format_t newjointcapability;
08336 format_t newpeercapability;
08337 int newnoncodeccapability;
08338
08339 const char *codecs;
08340 int codec;
08341
08342
08343 int secure_audio = FALSE;
08344 int secure_video = FALSE;
08345
08346
08347 int sendonly = -1;
08348 int vsendonly = -1;
08349 int numberofports;
08350 int numberofmediastreams = 0;
08351 int last_rtpmap_codec = 0;
08352 int red_data_pt[10];
08353 int red_num_gen = 0;
08354 char red_fmtp[100] = "empty";
08355 int debug = sip_debug_test_pvt(p);
08356
08357
08358 char buf[SIPBUFSIZE];
08359
08360
08361
08362 if (!p->rtp) {
08363 ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
08364 return -1;
08365 }
08366
08367
08368 ast_rtp_codecs_payloads_clear(&newaudiortp, NULL);
08369 ast_rtp_codecs_payloads_clear(&newvideortp, NULL);
08370 ast_rtp_codecs_payloads_clear(&newtextrtp, NULL);
08371
08372
08373 p->lastrtprx = p->lastrtptx = time(NULL);
08374
08375 memset(p->offered_media, 0, sizeof(p->offered_media));
08376
08377
08378
08379 p->novideo = TRUE;
08380 p->notext = TRUE;
08381
08382 if (p->vrtp) {
08383 ast_rtp_codecs_payloads_clear(&newvideortp, NULL);
08384 }
08385
08386 if (p->trtp) {
08387 ast_rtp_codecs_payloads_clear(&newtextrtp, NULL);
08388 }
08389
08390
08391 nextm = get_sdp_iterate(&next, req, "m");
08392 if (ast_strlen_zero(nextm)) {
08393 ast_log(LOG_WARNING, "Insufficient information for SDP (m= not found)\n");
08394 return -1;
08395 }
08396
08397
08398 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
08399 int processed = FALSE;
08400 switch (type) {
08401 case 'o':
08402
08403
08404
08405 if (!process_sdp_o(value, p))
08406 return (p->session_modify == FALSE) ? 0 : -1;
08407 break;
08408 case 'c':
08409 if (process_sdp_c(value, &sessionsa)) {
08410 processed = TRUE;
08411 sa = &sessionsa;
08412 vsa = sa;
08413 tsa = sa;
08414 isa = sa;
08415 }
08416 break;
08417 case 'a':
08418 if (process_sdp_a_sendonly(value, &sendonly)) {
08419 processed = TRUE;
08420 vsendonly = sendonly;
08421 }
08422 else if (process_sdp_a_audio(value, p, &newaudiortp, &last_rtpmap_codec))
08423 processed = TRUE;
08424 else if (process_sdp_a_video(value, p, &newvideortp, &last_rtpmap_codec))
08425 processed = TRUE;
08426 else if (process_sdp_a_text(value, p, &newtextrtp, red_fmtp, &red_num_gen, red_data_pt, &last_rtpmap_codec))
08427 processed = TRUE;
08428 else if (process_sdp_a_image(value, p))
08429 processed = TRUE;
08430 break;
08431 }
08432
08433 ast_debug(3, "Processing session-level SDP %c=%s... %s\n", type, value, (processed == TRUE)? "OK." : "UNSUPPORTED.");
08434 }
08435
08436
08437
08438
08439 while (!ast_strlen_zero(nextm)) {
08440 int audio = FALSE;
08441 int video = FALSE;
08442 int image = FALSE;
08443 int text = FALSE;
08444 char protocol[5] = {0,};
08445 int x;
08446
08447 numberofports = 1;
08448 len = -1;
08449 start = next;
08450 m = nextm;
08451 iterator = next;
08452 nextm = get_sdp_iterate(&next, req, "m");
08453
08454
08455 if ((sscanf(m, "audio %30u/%30u RTP/%4s %n", &x, &numberofports, protocol, &len) == 3 && len > 0) ||
08456 (sscanf(m, "audio %30u RTP/%4s %n", &x, protocol, &len) == 2 && len > 0)) {
08457 if (!strcmp(protocol, "SAVP")) {
08458 secure_audio = 1;
08459 } else if (strcmp(protocol, "AVP")) {
08460 ast_log(LOG_WARNING, "unknown SDP media protocol in offer: %s\n", protocol);
08461 continue;
08462 }
08463 audio = TRUE;
08464 p->offered_media[SDP_AUDIO].offered = TRUE;
08465 numberofmediastreams++;
08466 portno = x;
08467
08468
08469 codecs = m + len;
08470 ast_copy_string(p->offered_media[SDP_AUDIO].codecs, codecs, sizeof(p->offered_media[SDP_AUDIO].codecs));
08471 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
08472 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
08473 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
08474 return -1;
08475 }
08476 if (debug)
08477 ast_verbose("Found RTP audio format %d\n", codec);
08478
08479 ast_rtp_codecs_payloads_set_m_type(&newaudiortp, NULL, codec);
08480 }
08481
08482 } else if ((sscanf(m, "video %30u/%30u RTP/%4s %n", &x, &numberofports, protocol, &len) == 3 && len > 0) ||
08483 (sscanf(m, "video %30u RTP/%4s %n", &x, protocol, &len) == 2 && len >= 0)) {
08484 if (!strcmp(protocol, "SAVP")) {
08485 secure_video = 1;
08486 } else if (strcmp(protocol, "AVP")) {
08487 ast_log(LOG_WARNING, "unknown SDP media protocol in offer: %s\n", protocol);
08488 continue;
08489 }
08490 video = TRUE;
08491 p->novideo = FALSE;
08492 p->offered_media[SDP_VIDEO].offered = TRUE;
08493 numberofmediastreams++;
08494 vportno = x;
08495
08496
08497 codecs = m + len;
08498 ast_copy_string(p->offered_media[SDP_VIDEO].codecs, codecs, sizeof(p->offered_media[SDP_VIDEO].codecs));
08499 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
08500 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
08501 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
08502 return -1;
08503 }
08504 if (debug)
08505 ast_verbose("Found RTP video format %d\n", codec);
08506 ast_rtp_codecs_payloads_set_m_type(&newvideortp, NULL, codec);
08507 }
08508
08509 } else if ((sscanf(m, "text %30u/%30u RTP/AVP %n", &x, &numberofports, &len) == 2 && len > 0) ||
08510 (sscanf(m, "text %30u RTP/AVP %n", &x, &len) == 1 && len > 0)) {
08511 text = TRUE;
08512 p->notext = FALSE;
08513 p->offered_media[SDP_TEXT].offered = TRUE;
08514 numberofmediastreams++;
08515 tportno = x;
08516
08517
08518 codecs = m + len;
08519 ast_copy_string(p->offered_media[SDP_TEXT].codecs, codecs, sizeof(p->offered_media[SDP_TEXT].codecs));
08520 for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
08521 if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
08522 ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
08523 return -1;
08524 }
08525 if (debug)
08526 ast_verbose("Found RTP text format %d\n", codec);
08527 ast_rtp_codecs_payloads_set_m_type(&newtextrtp, NULL, codec);
08528 }
08529
08530 } else if (p->udptl && ((sscanf(m, "image %30u udptl t38%n", &x, &len) == 1 && len > 0) ||
08531 (sscanf(m, "image %30u UDPTL t38%n", &x, &len) == 1 && len > 0) )) {
08532 image = TRUE;
08533 if (debug)
08534 ast_verbose("Got T.38 offer in SDP in dialog %s\n", p->callid);
08535 p->offered_media[SDP_IMAGE].offered = TRUE;
08536 udptlportno = x;
08537 numberofmediastreams++;
08538
08539 if (p->t38.state != T38_ENABLED) {
08540 memset(&p->t38.their_parms, 0, sizeof(p->t38.their_parms));
08541
08542
08543
08544 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
08545 }
08546 } else {
08547 ast_log(LOG_WARNING, "Unsupported SDP media type in offer: %s\n", m);
08548 continue;
08549 }
08550
08551
08552 if (numberofports > 1)
08553 ast_log(LOG_WARNING, "SDP offered %d ports for media, not supported by Asterisk. Will try anyway...\n", numberofports);
08554
08555
08556 while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
08557 int processed = FALSE;
08558
08559 switch (type) {
08560 case 'c':
08561 if (audio) {
08562 if (process_sdp_c(value, &audiosa)) {
08563 processed = TRUE;
08564 sa = &audiosa;
08565 }
08566 } else if (video) {
08567 if (process_sdp_c(value, &videosa)) {
08568 processed = TRUE;
08569 vsa = &videosa;
08570 }
08571 } else if (text) {
08572 if (process_sdp_c(value, &textsa)) {
08573 processed = TRUE;
08574 tsa = &textsa;
08575 }
08576 } else if (image) {
08577 if (process_sdp_c(value, &imagesa)) {
08578 processed = TRUE;
08579 isa = &imagesa;
08580 }
08581 }
08582 break;
08583 case 'a':
08584
08585 if (audio) {
08586 if (process_sdp_a_sendonly(value, &sendonly))
08587 processed = TRUE;
08588 else if (process_crypto(p, p->rtp, &p->srtp, value))
08589 processed = TRUE;
08590 else if (process_sdp_a_audio(value, p, &newaudiortp, &last_rtpmap_codec))
08591 processed = TRUE;
08592 }
08593
08594 else if (video) {
08595 if (process_sdp_a_sendonly(value, &vsendonly))
08596 processed = TRUE;
08597 else if (process_crypto(p, p->vrtp, &p->vsrtp, value))
08598 processed = TRUE;
08599 else if (process_sdp_a_video(value, p, &newvideortp, &last_rtpmap_codec))
08600 processed = TRUE;
08601 }
08602
08603 else if (text) {
08604 if (process_sdp_a_text(value, p, &newtextrtp, red_fmtp, &red_num_gen, red_data_pt, &last_rtpmap_codec))
08605 processed = TRUE;
08606 else if (process_crypto(p, p->trtp, &p->tsrtp, value))
08607 processed = TRUE;
08608 }
08609
08610 else if (image) {
08611 if (process_sdp_a_image(value, p))
08612 processed = TRUE;
08613 }
08614 break;
08615 }
08616
08617 ast_debug(3, "Processing media-level (%s) SDP %c=%s... %s\n",
08618 (audio == TRUE)? "audio" : (video == TRUE)? "video" : "image",
08619 type, value,
08620 (processed == TRUE)? "OK." : "UNSUPPORTED.");
08621 }
08622 }
08623
08624
08625
08626 if (!sa && !vsa && !tsa && !isa) {
08627 ast_log(LOG_WARNING, "Insufficient information in SDP (c=)...\n");
08628 return -1;
08629 }
08630
08631 if (portno == -1 && vportno == -1 && udptlportno == -1 && tportno == -1) {
08632
08633
08634 ast_log(LOG_WARNING, "Failing due to no acceptable offer found\n");
08635 return -2;
08636 }
08637
08638 if (numberofmediastreams > 3) {
08639
08640 ast_log(LOG_WARNING, "Faling due to too many media streams\n");
08641 return -3;
08642 }
08643
08644 if (secure_audio && !(p->srtp && (ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)))) {
08645 ast_log(LOG_WARNING, "Can't provide secure audio requested in SDP offer\n");
08646 return -4;
08647 }
08648
08649 if (!secure_audio && p->srtp) {
08650 ast_log(LOG_WARNING, "We are requesting SRTP, but they responded without it!\n");
08651 return -4;
08652 }
08653
08654 if (secure_video && !(p->vsrtp && (ast_test_flag(p->vsrtp, SRTP_CRYPTO_OFFER_OK)))) {
08655 ast_log(LOG_WARNING, "Can't provide secure video requested in SDP offer\n");
08656 return -4;
08657 }
08658
08659 if (!p->novideo && !secure_video && p->vsrtp) {
08660 ast_log(LOG_WARNING, "We are requesting SRTP, but they responded without it!\n");
08661 return -4;
08662 }
08663
08664 if (!(secure_audio || secure_video) && ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP)) {
08665 ast_log(LOG_WARNING, "Matched device setup to use SRTP, but request was not!\n");
08666 return -4;
08667 }
08668
08669 if (udptlportno == -1) {
08670 change_t38_state(p, T38_DISABLED);
08671 }
08672
08673
08674 ast_rtp_codecs_payload_formats(&newaudiortp, &peercapability, &peernoncodeccapability);
08675 ast_rtp_codecs_payload_formats(&newvideortp, &vpeercapability, &vpeernoncodeccapability);
08676 ast_rtp_codecs_payload_formats(&newtextrtp, &tpeercapability, &tpeernoncodeccapability);
08677
08678 newjointcapability = p->capability & (peercapability | vpeercapability | tpeercapability);
08679 newpeercapability = (peercapability | vpeercapability | tpeercapability);
08680 newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
08681
08682 if (debug) {
08683
08684 char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE], s5[SIPBUFSIZE];
08685
08686 ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s/text=%s, combined - %s\n",
08687 ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
08688 ast_getformatname_multiple(s2, SIPBUFSIZE, peercapability),
08689 ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
08690 ast_getformatname_multiple(s4, SIPBUFSIZE, tpeercapability),
08691 ast_getformatname_multiple(s5, SIPBUFSIZE, newjointcapability));
08692 }
08693 if (debug) {
08694 struct ast_str *s1 = ast_str_alloca(SIPBUFSIZE);
08695 struct ast_str *s2 = ast_str_alloca(SIPBUFSIZE);
08696 struct ast_str *s3 = ast_str_alloca(SIPBUFSIZE);
08697
08698 ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
08699 ast_rtp_lookup_mime_multiple2(s1, p->noncodeccapability, 0, 0),
08700 ast_rtp_lookup_mime_multiple2(s2, peernoncodeccapability, 0, 0),
08701 ast_rtp_lookup_mime_multiple2(s3, newnoncodeccapability, 0, 0));
08702 }
08703 if (!newjointcapability && (portno != -1)) {
08704 ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
08705
08706 return -1;
08707 }
08708
08709
08710 if (p->rtp) {
08711 if (portno > 0) {
08712 ast_sockaddr_set_port(sa, portno);
08713 ast_rtp_instance_set_remote_address(p->rtp, sa);
08714 if (debug) {
08715 ast_verbose("Peer audio RTP is at port %s\n",
08716 ast_sockaddr_stringify(sa));
08717 }
08718
08719
08720 p->jointcapability = newjointcapability;
08721 p->peercapability = newpeercapability;
08722 p->jointnoncodeccapability = newnoncodeccapability;
08723
08724 if (ast_test_flag(&p->flags[1], SIP_PAGE2_PREFERRED_CODEC)) {
08725 p->jointcapability = ast_codec_choose(&p->prefs, p->jointcapability, 1);
08726 }
08727
08728 ast_rtp_codecs_payloads_copy(&newaudiortp, ast_rtp_instance_get_codecs(p->rtp), p->rtp);
08729
08730 if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
08731 ast_clear_flag(&p->flags[0], SIP_DTMF);
08732 if (newnoncodeccapability & AST_RTP_DTMF) {
08733
08734 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
08735
08736 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, 1);
08737 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
08738 } else {
08739 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
08740 }
08741 }
08742 } else if (udptlportno > 0) {
08743 if (debug)
08744 ast_verbose("Got T.38 Re-invite without audio. Keeping RTP active during T.38 session.\n");
08745 } else {
08746 ast_rtp_instance_stop(p->rtp);
08747 if (debug)
08748 ast_verbose("Peer doesn't provide audio\n");
08749 }
08750 }
08751
08752
08753 if (p->vrtp) {
08754 if (vportno > 0) {
08755 ast_sockaddr_set_port(vsa, vportno);
08756 ast_rtp_instance_set_remote_address(p->vrtp, vsa);
08757 if (debug) {
08758 ast_verbose("Peer video RTP is at port %s\n",
08759 ast_sockaddr_stringify(vsa));
08760 }
08761 ast_rtp_codecs_payloads_copy(&newvideortp, ast_rtp_instance_get_codecs(p->vrtp), p->vrtp);
08762 } else {
08763 ast_rtp_instance_stop(p->vrtp);
08764 if (debug)
08765 ast_verbose("Peer doesn't provide video\n");
08766 }
08767 }
08768
08769
08770 if (p->trtp) {
08771 if (tportno > 0) {
08772 ast_sockaddr_set_port(tsa, tportno);
08773 ast_rtp_instance_set_remote_address(p->trtp, tsa);
08774 if (debug) {
08775 ast_verbose("Peer T.140 RTP is at port %s\n",
08776 ast_sockaddr_stringify(tsa));
08777 }
08778 if ((p->jointcapability & AST_FORMAT_T140RED)) {
08779 p->red = 1;
08780 ast_rtp_red_init(p->trtp, 300, red_data_pt, 2);
08781 } else {
08782 p->red = 0;
08783 }
08784 ast_rtp_codecs_payloads_copy(&newtextrtp, ast_rtp_instance_get_codecs(p->trtp), p->trtp);
08785 } else {
08786 ast_rtp_instance_stop(p->trtp);
08787 if (debug)
08788 ast_verbose("Peer doesn't provide T.140\n");
08789 }
08790 }
08791
08792 if (p->udptl) {
08793 if (udptlportno > 0) {
08794 if (ast_test_flag(&p->flags[1], SIP_PAGE2_SYMMETRICRTP) && ast_test_flag(&p->flags[1], SIP_PAGE2_UDPTL_DESTINATION)) {
08795 ast_rtp_instance_get_remote_address(p->rtp, isa);
08796 if (!ast_sockaddr_isnull(isa) && debug) {
08797 ast_debug(1, "Peer T.38 UDPTL is set behind NAT and with destination, destination address now %s\n", ast_sockaddr_stringify(isa));
08798 }
08799 }
08800 ast_sockaddr_set_port(isa, udptlportno);
08801 ast_udptl_set_peer(p->udptl, isa);
08802 if (debug)
08803 ast_debug(1,"Peer T.38 UDPTL is at port %s\n", ast_sockaddr_stringify(isa));
08804
08805
08806 if (!ast_udptl_get_far_max_datagram(p->udptl)) {
08807
08808 ast_udptl_set_far_max_datagram(p->udptl, 0);
08809 }
08810
08811
08812 if ((t38action == SDP_T38_ACCEPT) &&
08813 (p->t38.state == T38_LOCAL_REINVITE)) {
08814 change_t38_state(p, T38_ENABLED);
08815 } else if ((t38action == SDP_T38_INITIATE) &&
08816 p->owner && p->lastinvite) {
08817 change_t38_state(p, T38_PEER_REINVITE);
08818
08819 if (ast_test_flag(&p->flags[1], SIP_PAGE2_FAX_DETECT_T38)) {
08820 ast_channel_lock(p->owner);
08821 if (strcmp(p->owner->exten, "fax")) {
08822 const char *target_context = S_OR(p->owner->macrocontext, p->owner->context);
08823 ast_channel_unlock(p->owner);
08824 if (ast_exists_extension(p->owner, target_context, "fax", 1,
08825 S_COR(p->owner->caller.id.number.valid, p->owner->caller.id.number.str, NULL))) {
08826 ast_verbose(VERBOSE_PREFIX_2 "Redirecting '%s' to fax extension due to peer T.38 re-INVITE\n", p->owner->name);
08827 pbx_builtin_setvar_helper(p->owner, "FAXEXTEN", p->owner->exten);
08828 if (ast_async_goto(p->owner, target_context, "fax", 1)) {
08829 ast_log(LOG_NOTICE, "Failed to async goto '%s' into fax of '%s'\n", p->owner->name, target_context);
08830 }
08831 } else {
08832 ast_log(LOG_NOTICE, "T.38 re-INVITE detected but no fax extension\n");
08833 }
08834 } else {
08835 ast_channel_unlock(p->owner);
08836 }
08837 }
08838 }
08839 } else {
08840 ast_udptl_stop(p->udptl);
08841 if (debug)
08842 ast_debug(1, "Peer doesn't provide T.38 UDPTL\n");
08843 }
08844 }
08845
08846 if ((portno == -1) && (p->t38.state != T38_DISABLED)) {
08847 ast_debug(3, "Have T.38 but no audio, accepting offer anyway\n");
08848 return 0;
08849 }
08850
08851
08852 ast_debug(2, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
08853
08854 if (!p->owner)
08855 return 0;
08856
08857 ast_debug(4, "We have an owner, now see if we need to change this call\n");
08858
08859 if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
08860 if (debug) {
08861 char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
08862 ast_debug(1, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
08863 ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
08864 ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
08865 }
08866 p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability) | (p->capability & tpeercapability);
08867 ast_set_read_format(p->owner, p->owner->readformat);
08868 ast_set_write_format(p->owner, p->owner->writeformat);
08869 }
08870
08871 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)) {
08872 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
08873
08874 ast_queue_frame(p->owner, &ast_null_frame);
08875 change_hold_state(p, req, FALSE, sendonly);
08876 } else if ((ast_sockaddr_isnull(sa) && ast_sockaddr_isnull(vsa) && ast_sockaddr_isnull(tsa) && ast_sockaddr_isnull(isa)) || (sendonly && sendonly != -1)) {
08877 ast_queue_control_data(p->owner, AST_CONTROL_HOLD,
08878 S_OR(p->mohsuggest, NULL),
08879 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
08880 if (sendonly)
08881 ast_rtp_instance_stop(p->rtp);
08882
08883
08884 ast_queue_frame(p->owner, &ast_null_frame);
08885 change_hold_state(p, req, TRUE, sendonly);
08886 }
08887
08888 return 0;
08889 }
08890
08891 static int process_sdp_o(const char *o, struct sip_pvt *p)
08892 {
08893 char *o_copy;
08894 char *token;
08895 int64_t rua_version;
08896
08897
08898
08899
08900
08901
08902
08903
08904
08905
08906 p->session_modify = TRUE;
08907
08908 if (ast_strlen_zero(o)) {
08909 ast_log(LOG_WARNING, "SDP syntax error. SDP without an o= line\n");
08910 return FALSE;
08911 }
08912
08913 o_copy = ast_strdupa(o);
08914 token = strsep(&o_copy, " ");
08915 if (!o_copy) {
08916 ast_log(LOG_WARNING, "SDP syntax error in o= line username\n");
08917 return FALSE;
08918 }
08919 token = strsep(&o_copy, " ");
08920 if (!o_copy) {
08921 ast_log(LOG_WARNING, "SDP syntax error in o= line session-id\n");
08922 return FALSE;
08923 }
08924 token = strsep(&o_copy, " ");
08925 if (!o_copy) {
08926 ast_log(LOG_WARNING, "SDP syntax error in o= line\n");
08927 return FALSE;
08928 }
08929 if (!sscanf(token, "%30" SCNd64, &rua_version)) {
08930 ast_log(LOG_WARNING, "SDP syntax error in o= line version\n");
08931 return FALSE;
08932 }
08933
08934
08935
08936
08937
08938
08939
08940
08941
08942
08943
08944
08945
08946
08947
08948
08949
08950
08951
08952 if (ast_test_flag(&p->flags[1], SIP_PAGE2_IGNORESDPVERSION) ||
08953 (p->sessionversion_remote < 0) ||
08954 (p->sessionversion_remote < rua_version)) {
08955 p->sessionversion_remote = rua_version;
08956 } else {
08957 if (p->t38.state == T38_LOCAL_REINVITE) {
08958 p->sessionversion_remote = rua_version;
08959 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);
08960 } else {
08961 p->session_modify = FALSE;
08962 ast_debug(2, "Call %s responded to our reinvite without changing SDP version; ignoring SDP.\n", p->callid);
08963 return FALSE;
08964 }
08965 }
08966
08967 return TRUE;
08968 }
08969
08970 static int process_sdp_c(const char *c, struct ast_sockaddr *addr)
08971 {
08972 char proto[4], host[258];
08973 int af;
08974
08975
08976 if (sscanf(c, "IN %3s %255s", proto, host) == 2) {
08977 if (!strcmp("IP4", proto)) {
08978 af = AF_INET;
08979 } else if (!strcmp("IP6", proto)) {
08980 af = AF_INET6;
08981 } else {
08982 ast_log(LOG_WARNING, "Unknown protocol '%s'.\n", proto);
08983 return FALSE;
08984 }
08985 if (ast_sockaddr_resolve_first_af(addr, host, 0, af)) {
08986 ast_log(LOG_WARNING, "Unable to lookup RTP Audio host in c= line, '%s'\n", c);
08987 return FALSE;
08988 }
08989 return TRUE;
08990 } else {
08991 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
08992 return FALSE;
08993 }
08994 return FALSE;
08995 }
08996
08997 static int process_sdp_a_sendonly(const char *a, int *sendonly)
08998 {
08999 int found = FALSE;
09000
09001 if (!strcasecmp(a, "sendonly")) {
09002 if (*sendonly == -1)
09003 *sendonly = 1;
09004 found = TRUE;
09005 } else if (!strcasecmp(a, "inactive")) {
09006 if (*sendonly == -1)
09007 *sendonly = 2;
09008 found = TRUE;
09009 } else if (!strcasecmp(a, "sendrecv")) {
09010 if (*sendonly == -1)
09011 *sendonly = 0;
09012 found = TRUE;
09013 }
09014 return found;
09015 }
09016
09017 static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec)
09018 {
09019 int found = FALSE;
09020 int codec;
09021 char mimeSubtype[128];
09022 char fmtp_string[64];
09023 unsigned int sample_rate;
09024 int debug = sip_debug_test_pvt(p);
09025
09026 if (!strncasecmp(a, "ptime", 5)) {
09027 char *tmp = strrchr(a, ':');
09028 long int framing = 0;
09029 if (tmp) {
09030 tmp++;
09031 framing = strtol(tmp, NULL, 10);
09032 if (framing == LONG_MIN || framing == LONG_MAX) {
09033 framing = 0;
09034 ast_debug(1, "Can't read framing from SDP: %s\n", a);
09035 }
09036 }
09037 if (framing && p->autoframing) {
09038 struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
09039 int codec_n;
09040 for (codec_n = 0; codec_n < AST_RTP_MAX_PT; codec_n++) {
09041 struct ast_rtp_payload_type format = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(p->rtp), codec_n);
09042 if (!format.asterisk_format || !format.code)
09043 continue;
09044 ast_debug(1, "Setting framing for %s to %ld\n", ast_getformatname(format.code), framing);
09045 ast_codec_pref_setsize(pref, format.code, framing);
09046 }
09047 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, pref);
09048 }
09049 found = TRUE;
09050 } else if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
09051
09052 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
09053 if (ast_rtp_codecs_payloads_set_rtpmap_type_rate(newaudiortp, NULL, codec, "audio", mimeSubtype,
09054 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0, sample_rate) != -1) {
09055 if (debug)
09056 ast_verbose("Found audio description format %s for ID %d\n", mimeSubtype, codec);
09057
09058 (*last_rtpmap_codec)++;
09059 found = TRUE;
09060 } else {
09061 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09062 if (debug)
09063 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
09064 }
09065 } else {
09066 if (debug)
09067 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
09068 }
09069 } else if (sscanf(a, "fmtp: %30u %63s", &codec, fmtp_string) == 2) {
09070 struct ast_rtp_payload_type payload;
09071
09072 payload = ast_rtp_codecs_payload_lookup(newaudiortp, codec);
09073 if (payload.code && payload.asterisk_format) {
09074 unsigned int bit_rate;
09075
09076 switch (payload.code) {
09077 case AST_FORMAT_SIREN7:
09078 if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
09079 if (bit_rate != 32000) {
09080 ast_log(LOG_WARNING, "Got Siren7 offer at %d bps, but only 32000 bps supported; ignoring.\n", bit_rate);
09081 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09082 } else {
09083 found = TRUE;
09084 }
09085 }
09086 break;
09087 case AST_FORMAT_SIREN14:
09088 if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
09089 if (bit_rate != 48000) {
09090 ast_log(LOG_WARNING, "Got Siren14 offer at %d bps, but only 48000 bps supported; ignoring.\n", bit_rate);
09091 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09092 } else {
09093 found = TRUE;
09094 }
09095 }
09096 break;
09097 case AST_FORMAT_G719:
09098 if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
09099 if (bit_rate != 64000) {
09100 ast_log(LOG_WARNING, "Got G.719 offer at %d bps, but only 64000 bps supported; ignoring.\n", bit_rate);
09101 ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
09102 } else {
09103 found = TRUE;
09104 }
09105 }
09106 }
09107 }
09108 }
09109
09110 return found;
09111 }
09112
09113 static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newvideortp, int *last_rtpmap_codec)
09114 {
09115 int found = FALSE;
09116 int codec;
09117 char mimeSubtype[128];
09118 unsigned int sample_rate;
09119 int debug = sip_debug_test_pvt(p);
09120
09121 if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
09122
09123 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
09124
09125 if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3)) {
09126 if (ast_rtp_codecs_payloads_set_rtpmap_type_rate(newvideortp, NULL, codec, "video", mimeSubtype, 0, sample_rate) != -1) {
09127 if (debug)
09128 ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec);
09129
09130 (*last_rtpmap_codec)++;
09131 found = TRUE;
09132 } else {
09133 ast_rtp_codecs_payloads_unset(newvideortp, NULL, codec);
09134 if (debug)
09135 ast_verbose("Found unknown media description format %s for ID %d\n", mimeSubtype, codec);
09136 }
09137 }
09138 } else {
09139 if (debug)
09140 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
09141 }
09142 }
09143
09144 return found;
09145 }
09146
09147 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)
09148 {
09149 int found = FALSE;
09150 int codec;
09151 char mimeSubtype[128];
09152 unsigned int sample_rate;
09153 char *red_cp;
09154 int debug = sip_debug_test_pvt(p);
09155
09156 if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
09157
09158 if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
09159 if (!strncasecmp(mimeSubtype, "T140", 4)) {
09160 if (p->trtp) {
09161
09162 ast_rtp_codecs_payloads_set_rtpmap_type_rate(newtextrtp, NULL, codec, "text", mimeSubtype, 0, sample_rate);
09163 found = TRUE;
09164 }
09165 } else if (!strncasecmp(mimeSubtype, "RED", 3)) {
09166 if (p->trtp) {
09167 ast_rtp_codecs_payloads_set_rtpmap_type_rate(newtextrtp, NULL, codec, "text", mimeSubtype, 0, sample_rate);
09168 sprintf(red_fmtp, "fmtp:%d ", codec);
09169 if (debug)
09170 ast_verbose("RED submimetype has payload type: %d\n", codec);
09171 found = TRUE;
09172 }
09173 }
09174 } else {
09175 if (debug)
09176 ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
09177 }
09178 } else if (!strncmp(a, red_fmtp, strlen(red_fmtp))) {
09179
09180 red_cp = &red_fmtp[strlen(red_fmtp)];
09181 strncpy(red_fmtp, a, 100);
09182
09183 sscanf(red_cp, "%30u", &red_data_pt[*red_num_gen]);
09184 red_cp = strtok(red_cp, "/");
09185 while (red_cp && (*red_num_gen)++ < AST_RED_MAX_GENERATION) {
09186 sscanf(red_cp, "%30u", &red_data_pt[*red_num_gen]);
09187 red_cp = strtok(NULL, "/");
09188 }
09189 red_cp = red_fmtp;
09190 found = TRUE;
09191 }
09192
09193 return found;
09194 }
09195
09196 static int process_sdp_a_image(const char *a, struct sip_pvt *p)
09197 {
09198 int found = FALSE;
09199 char s[256];
09200 unsigned int x;
09201
09202 if ((sscanf(a, "T38FaxMaxBuffer:%30u", &x) == 1)) {
09203 ast_debug(3, "MaxBufferSize:%d\n", x);
09204 found = TRUE;
09205 } else if ((sscanf(a, "T38MaxBitRate:%30u", &x) == 1) || (sscanf(a, "T38FaxMaxRate:%30u", &x) == 1)) {
09206 ast_debug(3, "T38MaxBitRate: %d\n", x);
09207 switch (x) {
09208 case 14400:
09209 p->t38.their_parms.rate = AST_T38_RATE_14400;
09210 break;
09211 case 12000:
09212 p->t38.their_parms.rate = AST_T38_RATE_12000;
09213 break;
09214 case 9600:
09215 p->t38.their_parms.rate = AST_T38_RATE_9600;
09216 break;
09217 case 7200:
09218 p->t38.their_parms.rate = AST_T38_RATE_7200;
09219 break;
09220 case 4800:
09221 p->t38.their_parms.rate = AST_T38_RATE_4800;
09222 break;
09223 case 2400:
09224 p->t38.their_parms.rate = AST_T38_RATE_2400;
09225 break;
09226 }
09227 found = TRUE;
09228 } else if ((sscanf(a, "T38FaxVersion:%30u", &x) == 1)) {
09229 ast_debug(3, "FaxVersion: %u\n", x);
09230 p->t38.their_parms.version = x;
09231 found = TRUE;
09232 } else if ((sscanf(a, "T38FaxMaxDatagram:%30u", &x) == 1) || (sscanf(a, "T38MaxDatagram:%30u", &x) == 1)) {
09233
09234 if (((signed int) p->t38_maxdatagram >= 0) && ((unsigned int) p->t38_maxdatagram > x)) {
09235 ast_debug(1, "Overriding T38FaxMaxDatagram '%d' with '%d'\n", x, p->t38_maxdatagram);
09236 x = p->t38_maxdatagram;
09237 }
09238 ast_debug(3, "FaxMaxDatagram: %u\n", x);
09239 ast_udptl_set_far_max_datagram(p->udptl, x);
09240 found = TRUE;
09241 } else if ((strncmp(a, "T38FaxFillBitRemoval", 20) == 0)) {
09242 if (sscanf(a, "T38FaxFillBitRemoval:%30u", &x) == 1) {
09243 ast_debug(3, "FillBitRemoval: %d\n", x);
09244 if (x == 1) {
09245 p->t38.their_parms.fill_bit_removal = TRUE;
09246 }
09247 } else {
09248 ast_debug(3, "FillBitRemoval\n");
09249 p->t38.their_parms.fill_bit_removal = TRUE;
09250 }
09251 found = TRUE;
09252 } else if ((strncmp(a, "T38FaxTranscodingMMR", 20) == 0)) {
09253 if (sscanf(a, "T38FaxTranscodingMMR:%30u", &x) == 1) {
09254 ast_debug(3, "Transcoding MMR: %d\n", x);
09255 if (x == 1) {
09256 p->t38.their_parms.transcoding_mmr = TRUE;
09257 }
09258 } else {
09259 ast_debug(3, "Transcoding MMR\n");
09260 p->t38.their_parms.transcoding_mmr = TRUE;
09261 }
09262 found = TRUE;
09263 } else if ((strncmp(a, "T38FaxTranscodingJBIG", 21) == 0)) {
09264 if (sscanf(a, "T38FaxTranscodingJBIG:%30u", &x) == 1) {
09265 ast_debug(3, "Transcoding JBIG: %d\n", x);
09266 if (x == 1) {
09267 p->t38.their_parms.transcoding_jbig = TRUE;
09268 }
09269 } else {
09270 ast_debug(3, "Transcoding JBIG\n");
09271 p->t38.their_parms.transcoding_jbig = TRUE;
09272 }
09273 found = TRUE;
09274 } else if ((sscanf(a, "T38FaxRateManagement:%255s", s) == 1)) {
09275 ast_debug(3, "RateManagement: %s\n", s);
09276 if (!strcasecmp(s, "localTCF"))
09277 p->t38.their_parms.rate_management = AST_T38_RATE_MANAGEMENT_LOCAL_TCF;
09278 else if (!strcasecmp(s, "transferredTCF"))
09279 p->t38.their_parms.rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF;
09280 found = TRUE;
09281 } else if ((sscanf(a, "T38FaxUdpEC:%255s", s) == 1)) {
09282 ast_debug(3, "UDP EC: %s\n", s);
09283 if (!strcasecmp(s, "t38UDPRedundancy")) {
09284 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_REDUNDANCY);
09285 } else if (!strcasecmp(s, "t38UDPFEC")) {
09286 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_FEC);
09287 } else {
09288 ast_udptl_set_error_correction_scheme(p->udptl, UDPTL_ERROR_CORRECTION_NONE);
09289 }
09290 found = TRUE;
09291 }
09292
09293 return found;
09294 }
09295
09296
09297
09298
09299 static int add_supported_header(struct sip_pvt *pvt, struct sip_request *req)
09300 {
09301 int res;
09302 if (st_get_mode(pvt) != SESSION_TIMER_MODE_REFUSE) {
09303 res = add_header(req, "Supported", "replaces, timer");
09304 } else {
09305 res = add_header(req, "Supported", "replaces");
09306 }
09307 return res;
09308 }
09309
09310
09311 static int add_header(struct sip_request *req, const char *var, const char *value)
09312 {
09313 if (req->headers == SIP_MAX_HEADERS) {
09314 ast_log(LOG_WARNING, "Out of SIP header space\n");
09315 return -1;
09316 }
09317
09318 if (req->lines) {
09319 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
09320 return -1;
09321 }
09322
09323 if (sip_cfg.compactheaders) {
09324 var = find_alias(var, var);
09325 }
09326
09327 ast_str_append(&req->data, 0, "%s: %s\r\n", var, value);
09328 req->header[req->headers] = req->len;
09329
09330 req->len = ast_str_strlen(req->data);
09331 req->headers++;
09332
09333 return 0;
09334 }
09335
09336
09337
09338
09339
09340 static int add_header_max_forwards(struct sip_pvt *dialog, struct sip_request *req)
09341 {
09342 char clen[10];
09343
09344 snprintf(clen, sizeof(clen), "%d", dialog->maxforwards);
09345
09346 return add_header(req, "Max-Forwards", clen);
09347 }
09348
09349
09350 static int finalize_content(struct sip_request *req)
09351 {
09352 char clen[10];
09353
09354 if (req->lines) {
09355 ast_log(LOG_WARNING, "finalize_content() called on a message that has already been finalized\n");
09356 return -1;
09357 }
09358
09359 snprintf(clen, sizeof(clen), "%zd", ast_str_strlen(req->content));
09360 add_header(req, "Content-Length", clen);
09361
09362 if (ast_str_strlen(req->content)) {
09363 ast_str_append(&req->data, 0, "\r\n%s", ast_str_buffer(req->content));
09364 req->len = ast_str_strlen(req->data);
09365 }
09366 req->lines = ast_str_strlen(req->content) ? 1 : 0;
09367 return 0;
09368 }
09369
09370
09371 static int add_content(struct sip_request *req, const char *line)
09372 {
09373 if (req->lines) {
09374 ast_log(LOG_WARNING, "Can't add more content when the content has been finalized\n");
09375 return -1;
09376 }
09377
09378 ast_str_append(&req->content, 0, "%s", line);
09379 return 0;
09380 }
09381
09382
09383 static int copy_header(struct sip_request *req, const struct sip_request *orig, const char *field)
09384 {
09385 const char *tmp = get_header(orig, field);
09386
09387 if (!ast_strlen_zero(tmp))
09388 return add_header(req, field, tmp);
09389 ast_log(LOG_NOTICE, "No field '%s' present to copy\n", field);
09390 return -1;
09391 }
09392
09393
09394 static int copy_all_header(struct sip_request *req, const struct sip_request *orig, const char *field)
09395 {
09396 int start = 0;
09397 int copied = 0;
09398 for (;;) {
09399 const char *tmp = __get_header(orig, field, &start);
09400
09401 if (ast_strlen_zero(tmp))
09402 break;
09403
09404 add_header(req, field, tmp);
09405 copied++;
09406 }
09407 return copied ? 0 : -1;
09408 }
09409
09410
09411
09412
09413
09414
09415
09416
09417
09418 static int copy_via_headers(struct sip_pvt *p, struct sip_request *req, const struct sip_request *orig, const char *field)
09419 {
09420 int copied = 0;
09421 int start = 0;
09422
09423 for (;;) {
09424 char new[512];
09425 const char *oh = __get_header(orig, field, &start);
09426
09427 if (ast_strlen_zero(oh))
09428 break;
09429
09430 if (!copied) {
09431 char leftmost[512], *others, *rport;
09432
09433
09434 ast_copy_string(leftmost, oh, sizeof(leftmost));
09435 others = strchr(leftmost, ',');
09436 if (others)
09437 *others++ = '\0';
09438
09439
09440 rport = strstr(leftmost, ";rport");
09441 if (rport && *(rport+6) == '=')
09442 rport = NULL;
09443
09444 if (((ast_test_flag(&p->flags[0], SIP_NAT_FORCE_RPORT)) || (rport && ast_test_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT)))) {
09445
09446 char *end;
09447
09448 rport = strstr(leftmost, ";rport");
09449
09450 if (rport) {
09451 end = strchr(rport + 1, ';');
09452 if (end)
09453 memmove(rport, end, strlen(end) + 1);
09454 else
09455 *rport = '\0';
09456 }
09457
09458
09459 snprintf(new, sizeof(new), "%s;received=%s;rport=%d%s%s",
09460 leftmost, ast_sockaddr_stringify_addr(&p->recv),
09461 ast_sockaddr_port(&p->recv),
09462 others ? "," : "", others ? others : "");
09463 } else {
09464
09465 snprintf(new, sizeof(new), "%s;received=%s%s%s",
09466 leftmost, ast_sockaddr_stringify_addr(&p->recv),
09467 others ? "," : "", others ? others : "");
09468 }
09469 oh = new;
09470 }
09471 add_header(req, field, oh);
09472 copied++;
09473 }
09474 if (!copied) {
09475 ast_log(LOG_NOTICE, "No header field '%s' present to copy\n", field);
09476 return -1;
09477 }
09478 return 0;
09479 }
09480
09481
09482 static void add_route(struct sip_request *req, struct sip_route *route)
09483 {
09484 char r[SIPBUFSIZE*2], *p;
09485 int n, rem = sizeof(r);
09486
09487 if (!route)
09488 return;
09489
09490 p = r;
09491 for (;route ; route = route->next) {
09492 n = strlen(route->hop);
09493 if (rem < n+3)
09494 break;
09495 if (p != r) {
09496 *p++ = ',';
09497 --rem;
09498 }
09499 *p++ = '<';
09500 ast_copy_string(p, route->hop, rem);
09501 p += n;
09502 *p++ = '>';
09503 rem -= (n+2);
09504 }
09505 *p = '\0';
09506 add_header(req, "Route", r);
09507 }
09508
09509
09510
09511
09512
09513
09514
09515
09516
09517
09518 static void set_destination(struct sip_pvt *p, char *uri)
09519 {
09520 char *h, *maddr, hostname[256];
09521 int hn;
09522 int debug=sip_debug_test_pvt(p);
09523 int tls_on = FALSE;
09524
09525 if (debug)
09526 ast_verbose("set_destination: Parsing <%s> for address/port to send to\n", uri);
09527
09528
09529 h = strchr(uri, '@');
09530 if (h)
09531 ++h;
09532 else {
09533 h = uri;
09534 if (!strncasecmp(h, "sip:", 4)) {
09535 h += 4;
09536 } else if (!strncasecmp(h, "sips:", 5)) {
09537 h += 5;
09538 tls_on = TRUE;
09539 }
09540 }
09541 hn = strcspn(h, ";>") + 1;
09542 if (hn > sizeof(hostname))
09543 hn = sizeof(hostname);
09544 ast_copy_string(hostname, h, hn);
09545
09546 h += hn - 1;
09547
09548
09549
09550 if (ast_sockaddr_resolve_first(&p->sa, hostname, 0)) {
09551 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
09552 return;
09553 }
09554
09555
09556 maddr = strstr(h, "maddr=");
09557 if (maddr) {
09558 int port;
09559
09560 maddr += 6;
09561 hn = strspn(maddr, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
09562 "0123456789-.:[]") + 1;
09563 if (hn > sizeof(hostname))
09564 hn = sizeof(hostname);
09565 ast_copy_string(hostname, maddr, hn);
09566
09567 port = ast_sockaddr_port(&p->sa);
09568
09569
09570
09571 if (ast_sockaddr_resolve_first(&p->sa, hostname, PARSE_PORT_FORBID)) {
09572 ast_log(LOG_WARNING, "Can't find address for host '%s'\n", hostname);
09573 return;
09574 }
09575
09576 ast_sockaddr_set_port(&p->sa, port);
09577 }
09578
09579 if (!ast_sockaddr_port(&p->sa)) {
09580 ast_sockaddr_set_port(&p->sa, tls_on ?
09581 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
09582 }
09583
09584 if (debug) {
09585 ast_verbose("set_destination: set destination to %s\n",
09586 ast_sockaddr_stringify(&p->sa));
09587 }
09588 }
09589
09590
09591 static int init_resp(struct sip_request *resp, const char *msg)
09592 {
09593
09594 memset(resp, 0, sizeof(*resp));
09595 resp->method = SIP_RESPONSE;
09596 if (!(resp->data = ast_str_create(SIP_MIN_PACKET)))
09597 goto e_return;
09598 if (!(resp->content = ast_str_create(SIP_MIN_PACKET)))
09599 goto e_free_data;
09600 resp->header[0] = 0;
09601 ast_str_set(&resp->data, 0, "SIP/2.0 %s\r\n", msg);
09602 resp->len = resp->data->used;
09603 resp->headers++;
09604 return 0;
09605
09606 e_free_data:
09607 ast_free(resp->data);
09608 resp->data = NULL;
09609 e_return:
09610 return -1;
09611 }
09612
09613
09614 static int init_req(struct sip_request *req, int sipmethod, const char *recip)
09615 {
09616
09617 memset(req, 0, sizeof(*req));
09618 if (!(req->data = ast_str_create(SIP_MIN_PACKET)))
09619 goto e_return;
09620 if (!(req->content = ast_str_create(SIP_MIN_PACKET)))
09621 goto e_free_data;
09622 req->method = sipmethod;
09623 req->header[0] = 0;
09624 ast_str_set(&req->data, 0, "%s %s SIP/2.0\r\n", sip_methods[sipmethod].text, recip);
09625 req->len = ast_str_strlen(req->data);
09626 req->headers++;
09627 return 0;
09628
09629 e_free_data:
09630 ast_free(req->data);
09631 req->data = NULL;
09632 e_return:
09633 return -1;
09634 }
09635
09636
09637 static void deinit_req(struct sip_request *req)
09638 {
09639 if (req->data) {
09640 ast_free(req->data);
09641 req->data = NULL;
09642 }
09643 if (req->content) {
09644 ast_free(req->content);
09645 req->content = NULL;
09646 }
09647 }
09648
09649
09650
09651 static inline int resp_needs_contact(const char *msg, enum sipmethod method) {
09652
09653
09654
09655
09656
09657
09658
09659
09660
09661
09662
09663
09664
09665
09666
09667 switch (method) {
09668
09669 case SIP_INVITE:
09670 case SIP_UPDATE:
09671 case SIP_SUBSCRIBE:
09672 case SIP_NOTIFY:
09673 if ((msg[0] >= '1' && msg[0] <= '3') || !strncmp(msg, "485", 3))
09674 return 1;
09675 break;
09676
09677
09678 case SIP_REGISTER:
09679 case SIP_OPTIONS:
09680 if (msg[0] == '2' || msg[0] == '3' || !strncmp(msg, "485", 3))
09681 return 1;
09682 break;
09683
09684
09685 case SIP_BYE:
09686 case SIP_PRACK:
09687 case SIP_MESSAGE:
09688 case SIP_PUBLISH:
09689 if (msg[0] == '3' || !strncmp(msg, "485", 3))
09690 return 1;
09691 break;
09692
09693
09694 case SIP_REFER:
09695 if (msg[0] >= '2' && msg[0] <= '6')
09696 return 1;
09697 break;
09698
09699
09700 case SIP_ACK:
09701 case SIP_CANCEL:
09702 case SIP_INFO:
09703 case SIP_PING:
09704 default:
09705 return 0;
09706 }
09707 return 0;
09708 }
09709
09710
09711 static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
09712 {
09713 char newto[256];
09714 const char *ot;
09715
09716 init_resp(resp, msg);
09717 copy_via_headers(p, resp, req, "Via");
09718 if (msg[0] == '1' || msg[0] == '2')
09719 copy_all_header(resp, req, "Record-Route");
09720 copy_header(resp, req, "From");
09721 ot = get_header(req, "To");
09722 if (!strcasestr(ot, "tag=") && strncmp(msg, "100", 3)) {
09723
09724
09725 if (!ast_strlen_zero(p->theirtag) && ast_test_flag(&p->flags[0], SIP_OUTGOING))
09726 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
09727 else if (p->tag && !ast_test_flag(&p->flags[0], SIP_OUTGOING))
09728 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
09729 else
09730 ast_copy_string(newto, ot, sizeof(newto));
09731 ot = newto;
09732 }
09733 add_header(resp, "To", ot);
09734 copy_header(resp, req, "Call-ID");
09735 copy_header(resp, req, "CSeq");
09736 if (!ast_strlen_zero(global_useragent))
09737 add_header(resp, "Server", global_useragent);
09738 add_header(resp, "Allow", ALLOWED_METHODS);
09739 add_supported_header(p, resp);
09740
09741
09742 if (p->method == SIP_INVITE && p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE) {
09743 char se_hdr[256];
09744 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
09745 strefresher2str(p->stimer->st_ref));
09746 add_header(resp, "Session-Expires", se_hdr);
09747 }
09748
09749 if (msg[0] == '2' && (p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER)) {
09750
09751
09752 char tmp[256];
09753
09754 snprintf(tmp, sizeof(tmp), "%d", p->expiry);
09755 add_header(resp, "Expires", tmp);
09756 if (p->expiry) {
09757 char contact[SIPBUFSIZE];
09758 const char *contact_uri = p->method == SIP_SUBSCRIBE ? p->our_contact : p->fullcontact;
09759 char *brackets = strchr(contact_uri, '<');
09760 snprintf(contact, sizeof(contact), "%s%s%s;expires=%d", brackets ? "" : "<", contact_uri, brackets ? "" : ">", p->expiry);
09761 add_header(resp, "Contact", contact);
09762 }
09763 } else if (!ast_strlen_zero(p->our_contact) && resp_needs_contact(msg, p->method)) {
09764 add_header(resp, "Contact", p->our_contact);
09765 }
09766
09767 if (!ast_strlen_zero(p->url)) {
09768 add_header(resp, "Access-URL", p->url);
09769 ast_string_field_set(p, url, NULL);
09770 }
09771
09772
09773
09774
09775
09776
09777
09778 p->sa = p->recv;
09779
09780 if (process_via(p, req)) {
09781 ast_log(LOG_WARNING, "error processing via header, will send response to originating address\n");
09782 }
09783
09784 return 0;
09785 }
09786
09787
09788 static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
09789 {
09790 struct sip_request *orig = &p->initreq;
09791 char stripped[80];
09792 char tmp[80];
09793 char newto[256];
09794 const char *c;
09795 const char *ot, *of;
09796 int is_strict = FALSE;
09797 int is_outbound = ast_test_flag(&p->flags[0], SIP_OUTGOING);
09798
09799 memset(req, 0, sizeof(struct sip_request));
09800
09801 snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
09802
09803 if (!seqno) {
09804 p->ocseq++;
09805 seqno = p->ocseq;
09806 }
09807
09808
09809 if (sipmethod == SIP_CANCEL) {
09810 p->branch = p->invite_branch;
09811 build_via(p);
09812 } else if (newbranch && (sipmethod == SIP_INVITE)) {
09813 p->branch ^= ast_random();
09814 p->invite_branch = p->branch;
09815 build_via(p);
09816 } else if (newbranch) {
09817 p->branch ^= ast_random();
09818 build_via(p);
09819 }
09820
09821
09822 if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop, ";lr") == NULL) {
09823 is_strict = TRUE;
09824 if (sipdebug)
09825 ast_debug(1, "Strict routing enforced for session %s\n", p->callid);
09826 }
09827
09828 if (sipmethod == SIP_CANCEL)
09829 c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
09830 else if (sipmethod == SIP_ACK) {
09831
09832
09833 if (!ast_strlen_zero(p->okcontacturi))
09834 c = is_strict ? p->route->hop : p->okcontacturi;
09835 else
09836 c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
09837 } else if (!ast_strlen_zero(p->okcontacturi))
09838 c = is_strict ? p->route->hop : p->okcontacturi;
09839 else if (!ast_strlen_zero(p->uri))
09840 c = p->uri;
09841 else {
09842 char *n;
09843
09844 ast_copy_string(stripped, get_header(orig, is_outbound ? "To" : "From"),
09845 sizeof(stripped));
09846 n = get_in_brackets(stripped);
09847 c = remove_uri_parameters(n);
09848 }
09849 init_req(req, sipmethod, c);
09850
09851 snprintf(tmp, sizeof(tmp), "%d %s", seqno, sip_methods[sipmethod].text);
09852
09853 add_header(req, "Via", p->via);
09854 if (p->route) {
09855 set_destination(p, p->route->hop);
09856 add_route(req, is_strict ? p->route->next : p->route);
09857 }
09858 add_header_max_forwards(p, req);
09859
09860 ot = get_header(orig, "To");
09861 of = get_header(orig, "From");
09862
09863
09864
09865 if (!strcasestr(ot, "tag=") && sipmethod != SIP_CANCEL) {
09866
09867
09868 if (is_outbound && !ast_strlen_zero(p->theirtag))
09869 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->theirtag);
09870 else if (!is_outbound)
09871 snprintf(newto, sizeof(newto), "%s;tag=%s", ot, p->tag);
09872 else
09873 snprintf(newto, sizeof(newto), "%s", ot);
09874 ot = newto;
09875 }
09876
09877 if (is_outbound) {
09878 add_header(req, "From", of);
09879 add_header(req, "To", ot);
09880 } else {
09881 add_header(req, "From", ot);
09882 add_header(req, "To", of);
09883 }
09884
09885 if (sipmethod != SIP_BYE && sipmethod != SIP_CANCEL && sipmethod != SIP_MESSAGE)
09886 add_header(req, "Contact", p->our_contact);
09887
09888 copy_header(req, orig, "Call-ID");
09889 add_header(req, "CSeq", tmp);
09890
09891 if (!ast_strlen_zero(global_useragent))
09892 add_header(req, "User-Agent", global_useragent);
09893
09894 if (!ast_strlen_zero(p->url)) {
09895 add_header(req, "Access-URL", p->url);
09896 ast_string_field_set(p, url, NULL);
09897 }
09898
09899
09900
09901
09902
09903
09904
09905
09906 if (p->stimer && p->stimer->st_active == TRUE && p->stimer->st_active_peer_ua == TRUE
09907 && sipmethod == SIP_INVITE) {
09908 char se_hdr[256];
09909 snprintf(se_hdr, sizeof(se_hdr), "%d;refresher=%s", p->stimer->st_interval,
09910 strefresher2str(p->stimer->st_ref));
09911 add_header(req, "Require", "timer");
09912 add_header(req, "Session-Expires", se_hdr);
09913 snprintf(se_hdr, sizeof(se_hdr), "%d", st_get_se(p, FALSE));
09914 add_header(req, "Min-SE", se_hdr);
09915 }
09916
09917 return 0;
09918 }
09919
09920
09921 static int __transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
09922 {
09923 struct sip_request resp;
09924 int seqno = 0;
09925
09926 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
09927 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
09928 return -1;
09929 }
09930 respprep(&resp, p, msg, req);
09931
09932 if (ast_test_flag(&p->flags[0], SIP_SENDRPID)
09933 && ast_test_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND)
09934 && (!strncmp(msg, "180", 3) || !strncmp(msg, "183", 3))) {
09935 ast_clear_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND);
09936 add_rpid(&resp, p);
09937 }
09938 if (ast_test_flag(&p->flags[0], SIP_OFFER_CC)) {
09939 add_cc_call_info_to_response(p, &resp);
09940 }
09941
09942
09943
09944 if (p->method == SIP_INVITE && msg[0] != '1') {
09945 char buf[20];
09946
09947 if (ast_test_flag(&p->flags[1], SIP_PAGE2_Q850_REASON)) {
09948 int hangupcause = 0;
09949
09950 if (p->owner && p->owner->hangupcause) {
09951 hangupcause = p->owner->hangupcause;
09952 } else if (p->hangupcause) {
09953 hangupcause = p->hangupcause;
09954 } else {
09955 int respcode;
09956 if (sscanf(msg, "%30d ", &respcode))
09957 hangupcause = hangup_sip2cause(respcode);
09958 }
09959
09960 if (hangupcause) {
09961 sprintf(buf, "Q.850;cause=%i", hangupcause & 0x7f);
09962 add_header(&resp, "Reason", buf);
09963 }
09964 }
09965
09966 if (p->owner && p->owner->hangupcause) {
09967 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->owner->hangupcause));
09968 snprintf(buf, sizeof(buf), "%d", p->owner->hangupcause);
09969 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
09970 }
09971 }
09972 return send_response(p, &resp, reliable, seqno);
09973 }
09974
09975 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)
09976 {
09977 struct sip_request resp;
09978
09979 if (need_new_etag) {
09980 create_new_sip_etag(esc_entry, 1);
09981 }
09982 respprep(&resp, p, msg, req);
09983 add_header(&resp, "SIP-ETag", esc_entry->entity_tag);
09984
09985 return send_response(p, &resp, 0, 0);
09986 }
09987
09988 static int temp_pvt_init(void *data)
09989 {
09990 struct sip_pvt *p = data;
09991
09992 p->do_history = 0;
09993 return ast_string_field_init(p, 512);
09994 }
09995
09996 static void temp_pvt_cleanup(void *data)
09997 {
09998 struct sip_pvt *p = data;
09999
10000 ast_string_field_free_memory(p);
10001
10002 ast_free(data);
10003 }
10004
10005
10006 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)
10007 {
10008 struct sip_pvt *p = NULL;
10009
10010 if (!(p = ast_threadstorage_get(&ts_temp_pvt, sizeof(*p)))) {
10011 ast_log(LOG_ERROR, "Failed to get temporary pvt\n");
10012 return -1;
10013 }
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023 p->method = intended_method;
10024
10025 if (!addr) {
10026 ast_sockaddr_copy(&p->ourip, &internip);
10027 } else {
10028 ast_sockaddr_copy(&p->sa, addr);
10029 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
10030 }
10031
10032 p->branch = ast_random();
10033 make_our_tag(p->tag, sizeof(p->tag));
10034 p->ocseq = INITIAL_CSEQ;
10035
10036 if (useglobal_nat && addr) {
10037 ast_copy_flags(&p->flags[0], &global_flags[0], SIP_NAT_FORCE_RPORT);
10038 ast_sockaddr_copy(&p->recv, addr);
10039 do_setnat(p);
10040 }
10041
10042 ast_string_field_set(p, fromdomain, default_fromdomain);
10043 p->fromdomainport = default_fromdomainport;
10044 build_via(p);
10045 ast_string_field_set(p, callid, callid);
10046
10047 copy_socket_data(&p->socket, &req->socket);
10048
10049
10050 __transmit_response(p, msg, req, XMIT_UNRELIABLE);
10051
10052
10053 ast_string_field_init(p, 0);
10054
10055 return 0;
10056 }
10057
10058
10059 static int transmit_response(struct sip_pvt *p, const char *msg, const struct sip_request *req)
10060 {
10061 return __transmit_response(p, msg, req, XMIT_UNRELIABLE);
10062 }
10063
10064
10065 static int transmit_response_with_unsupported(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *unsupported)
10066 {
10067 struct sip_request resp;
10068 respprep(&resp, p, msg, req);
10069 append_date(&resp);
10070 add_header(&resp, "Unsupported", unsupported);
10071 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
10072 }
10073
10074
10075 static int transmit_response_with_minse(struct sip_pvt *p, const char *msg, const struct sip_request *req, int minse_int)
10076 {
10077 struct sip_request resp;
10078 char minse_str[20];
10079
10080 respprep(&resp, p, msg, req);
10081 append_date(&resp);
10082
10083 snprintf(minse_str, sizeof(minse_str), "%d", minse_int);
10084 add_header(&resp, "Min-SE", minse_str);
10085 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
10086 }
10087
10088
10089
10090
10091
10092 static int transmit_response_reliable(struct sip_pvt *p, const char *msg, const struct sip_request *req)
10093 {
10094 return __transmit_response(p, msg, req, req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL);
10095 }
10096
10097
10098 static void append_date(struct sip_request *req)
10099 {
10100 char tmpdat[256];
10101 struct tm tm;
10102 time_t t = time(NULL);
10103
10104 gmtime_r(&t, &tm);
10105 strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T GMT", &tm);
10106 add_header(req, "Date", tmpdat);
10107 }
10108
10109
10110 static int transmit_response_with_retry_after(struct sip_pvt *p, const char *msg, const struct sip_request *req, const char *seconds)
10111 {
10112 struct sip_request resp;
10113 respprep(&resp, p, msg, req);
10114 add_header(&resp, "Retry-After", seconds);
10115 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
10116 }
10117
10118
10119 static int transmit_response_with_date(struct sip_pvt *p, const char *msg, const struct sip_request *req)
10120 {
10121 struct sip_request resp;
10122 respprep(&resp, p, msg, req);
10123 append_date(&resp);
10124 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
10125 }
10126
10127
10128 static int transmit_response_with_allow(struct sip_pvt *p, const char *msg, const struct sip_request *req, enum xmittype reliable)
10129 {
10130 struct sip_request resp;
10131 respprep(&resp, p, msg, req);
10132 add_header(&resp, "Accept", "application/sdp");
10133 return send_response(p, &resp, reliable, 0);
10134 }
10135
10136
10137 static int transmit_response_with_minexpires(struct sip_pvt *p, const char *msg, const struct sip_request *req)
10138 {
10139 struct sip_request resp;
10140 char tmp[32];
10141
10142 snprintf(tmp, sizeof(tmp), "%d", min_expiry);
10143 respprep(&resp, p, msg, req);
10144 add_header(&resp, "Min-Expires", tmp);
10145 return send_response(p, &resp, XMIT_UNRELIABLE, 0);
10146 }
10147
10148
10149 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)
10150 {
10151 struct sip_request resp;
10152 char tmp[512];
10153 int seqno = 0;
10154
10155 if (reliable && (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1)) {
10156 ast_log(LOG_WARNING, "Unable to determine sequence number from '%s'\n", get_header(req, "CSeq"));
10157 return -1;
10158 }
10159
10160 get_realm(p, req);
10161
10162
10163
10164 snprintf(tmp, sizeof(tmp), "Digest algorithm=MD5, realm=\"%s\", nonce=\"%s\"%s", p->realm, randdata, stale ? ", stale=true" : "");
10165 respprep(&resp, p, msg, req);
10166 add_header(&resp, header, tmp);
10167 append_history(p, "AuthChal", "Auth challenge sent for %s - nc %d", p->username, p->noncecount);
10168 return send_response(p, &resp, reliable, seqno);
10169 }
10170
10171
10172
10173
10174
10175
10176
10177 static int get_domain(const char *str, char *domain, int len)
10178 {
10179 char tmpf[256];
10180 char *a, *from;
10181
10182 *domain = '\0';
10183 ast_copy_string(tmpf, str, sizeof(tmpf));
10184 from = get_in_brackets(tmpf);
10185 if (!ast_strlen_zero(from)) {
10186 if (strncasecmp(from, "sip:", 4)) {
10187 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", from);
10188 return -1;
10189 }
10190 from += 4;
10191 } else
10192 from = NULL;
10193
10194 if (from) {
10195 int bracket = 0;
10196
10197
10198 if ((a = strchr(from, ';')))
10199 *a = '\0';
10200
10201 for (a = from; *a != '\0'; ++a) {
10202 if (*a == ':' && bracket == 0) {
10203 *a = '\0';
10204 break;
10205 } else if (*a == '[') {
10206 ++bracket;
10207 } else if (*a == ']') {
10208 --bracket;
10209 }
10210 }
10211 if ((a = strchr(from, '@'))) {
10212 *a = '\0';
10213 ast_copy_string(domain, a + 1, len);
10214 } else
10215 ast_copy_string(domain, from, len);
10216 }
10217
10218 return ast_strlen_zero(domain);
10219 }
10220
10221
10222
10223
10224
10225 static void get_realm(struct sip_pvt *p, const struct sip_request *req)
10226 {
10227 char domain[MAXHOSTNAMELEN];
10228
10229 if (!ast_strlen_zero(p->realm))
10230 return;
10231
10232 if (sip_cfg.domainsasrealm &&
10233 !AST_LIST_EMPTY(&domain_list))
10234 {
10235
10236 if (!get_domain(get_header(req, "From"), domain, sizeof(domain))) {
10237 if (check_sip_domain(domain, NULL, 0)) {
10238 ast_string_field_set(p, realm, domain);
10239 return;
10240 }
10241 }
10242
10243 if (!get_domain(get_header(req, "To"), domain, sizeof(domain))) {
10244 if (check_sip_domain(domain, NULL, 0)) {
10245 ast_string_field_set(p, realm, domain);
10246 return;
10247 }
10248 }
10249 }
10250
10251
10252 ast_string_field_set(p, realm, sip_cfg.realm);
10253 }
10254
10255
10256 static int transmit_provisional_response(struct sip_pvt *p, const char *msg, const struct sip_request *req, int with_sdp)
10257 {
10258 int res;
10259
10260 if (!(res = with_sdp ? transmit_response_with_sdp(p, msg, req, XMIT_UNRELIABLE, FALSE, FALSE) : transmit_response(p, msg, req))) {
10261 p->last_provisional = msg;
10262 update_provisional_keepalive(p, with_sdp);
10263 }
10264
10265 return res;
10266 }
10267
10268
10269 static int add_text(struct sip_request *req, const char *text)
10270 {
10271
10272 add_header(req, "Content-Type", "text/plain;charset=UTF-8");
10273 add_content(req, text);
10274 return 0;
10275 }
10276
10277
10278
10279
10280
10281 static int add_digit(struct sip_request *req, char digit, unsigned int duration, int mode)
10282 {
10283 char tmp[256];
10284 int event;
10285 if (mode) {
10286
10287 if (digit == '*')
10288 event = 10;
10289 else if (digit == '#')
10290 event = 11;
10291 else if ((digit >= 'A') && (digit <= 'D'))
10292 event = 12 + digit - 'A';
10293 else
10294 event = atoi(&digit);
10295 snprintf(tmp, sizeof(tmp), "%d\r\n", event);
10296 add_header(req, "Content-Type", "application/dtmf");
10297 add_content(req, tmp);
10298 } else {
10299
10300 snprintf(tmp, sizeof(tmp), "Signal=%c\r\nDuration=%u\r\n", digit, duration);
10301 add_header(req, "Content-Type", "application/dtmf-relay");
10302 add_content(req, tmp);
10303 }
10304 return 0;
10305 }
10306
10307
10308
10309
10310
10311 static int add_rpid(struct sip_request *req, struct sip_pvt *p)
10312 {
10313 struct ast_str *tmp = ast_str_alloca(256);
10314 char tmp2[256];
10315 char *lid_num = NULL;
10316 char *lid_name = NULL;
10317 int lid_pres;
10318 const char *fromdomain;
10319 const char *privacy = NULL;
10320 const char *screen = NULL;
10321 const char *anonymous_string = "\"Anonymous\" <sip:anonymous@anonymous.invalid>";
10322
10323 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID)) {
10324 return 0;
10325 }
10326
10327 if (p->owner && p->owner->connected.id.number.valid
10328 && p->owner->connected.id.number.str) {
10329 lid_num = p->owner->connected.id.number.str;
10330 }
10331 if (p->owner && p->owner->connected.id.name.valid
10332 && p->owner->connected.id.name.str) {
10333 lid_name = p->owner->connected.id.name.str;
10334 }
10335 lid_pres = (p->owner) ? ast_party_id_presentation(&p->owner->connected.id) : AST_PRES_NUMBER_NOT_AVAILABLE;
10336
10337 if (ast_strlen_zero(lid_num))
10338 return 0;
10339 if (ast_strlen_zero(lid_name))
10340 lid_name = lid_num;
10341 fromdomain = S_OR(p->fromdomain, ast_sockaddr_stringify_host(&p->ourip));
10342
10343 lid_num = ast_uri_encode(lid_num, tmp2, sizeof(tmp2), 1);
10344
10345 if (ast_test_flag(&p->flags[0], SIP_SENDRPID_PAI)) {
10346 if ((lid_pres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
10347 ast_str_set(&tmp, -1, "%s", anonymous_string);
10348 } else {
10349 ast_str_set(&tmp, -1, "\"%s\" <sip:%s@%s>", lid_name, lid_num, fromdomain);
10350 }
10351 add_header(req, "P-Asserted-Identity", ast_str_buffer(tmp));
10352 } else {
10353 ast_str_set(&tmp, -1, "\"%s\" <sip:%s@%s>;party=%s", lid_name, lid_num, fromdomain, ast_test_flag(&p->flags[0], SIP_OUTGOING) ? "calling" : "called");
10354
10355 switch (lid_pres) {
10356 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
10357 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
10358 privacy = "off";
10359 screen = "no";
10360 break;
10361 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
10362 case AST_PRES_ALLOWED_NETWORK_NUMBER:
10363 privacy = "off";
10364 screen = "yes";
10365 break;
10366 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
10367 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
10368 privacy = "full";
10369 screen = "no";
10370 break;
10371 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
10372 case AST_PRES_PROHIB_NETWORK_NUMBER:
10373 privacy = "full";
10374 screen = "yes";
10375 break;
10376 case AST_PRES_NUMBER_NOT_AVAILABLE:
10377 break;
10378 default:
10379 if ((lid_pres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
10380 privacy = "full";
10381 }
10382 else
10383 privacy = "off";
10384 screen = "no";
10385 break;
10386 }
10387
10388 if (!ast_strlen_zero(privacy) && !ast_strlen_zero(screen)) {
10389 ast_str_append(&tmp, -1, ";privacy=%s;screen=%s", privacy, screen);
10390 }
10391
10392 add_header(req, "Remote-Party-ID", ast_str_buffer(tmp));
10393 }
10394 return 0;
10395 }
10396
10397
10398
10399 static int add_vidupdate(struct sip_request *req)
10400 {
10401 const char *xml_is_a_huge_waste_of_space =
10402 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
10403 " <media_control>\r\n"
10404 " <vc_primitive>\r\n"
10405 " <to_encoder>\r\n"
10406 " <picture_fast_update>\r\n"
10407 " </picture_fast_update>\r\n"
10408 " </to_encoder>\r\n"
10409 " </vc_primitive>\r\n"
10410 " </media_control>\r\n";
10411 add_header(req, "Content-Type", "application/media_control+xml");
10412 add_content(req, xml_is_a_huge_waste_of_space);
10413 return 0;
10414 }
10415
10416
10417 static void add_codec_to_sdp(const struct sip_pvt *p, format_t codec,
10418 struct ast_str **m_buf, struct ast_str **a_buf,
10419 int debug, int *min_packet_size)
10420 {
10421 int rtp_code;
10422 struct ast_format_list fmt;
10423
10424
10425 if (debug)
10426 ast_verbose("Adding codec 0x%" PRIx64 " (%s) to SDP\n", codec, ast_getformatname(codec));
10427 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 1, codec)) == -1)
10428 return;
10429
10430 if (p->rtp) {
10431 struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
10432 fmt = ast_codec_pref_getsize(pref, codec);
10433 } else
10434 return;
10435 ast_str_append(m_buf, 0, " %d", rtp_code);
10436 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10437 ast_rtp_lookup_mime_subtype2(1, codec,
10438 ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
10439 ast_rtp_lookup_sample_rate2(1, codec));
10440
10441 switch (codec) {
10442 case AST_FORMAT_G729A:
10443
10444 ast_str_append(a_buf, 0, "a=fmtp:%d annexb=no\r\n", rtp_code);
10445 break;
10446 case AST_FORMAT_G723_1:
10447
10448 ast_str_append(a_buf, 0, "a=fmtp:%d annexa=no\r\n", rtp_code);
10449 break;
10450 case AST_FORMAT_ILBC:
10451
10452 ast_str_append(a_buf, 0, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
10453 break;
10454 case AST_FORMAT_SIREN7:
10455
10456 ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=32000\r\n", rtp_code);
10457 break;
10458 case AST_FORMAT_SIREN14:
10459
10460 ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=48000\r\n", rtp_code);
10461 break;
10462 case AST_FORMAT_G719:
10463
10464 ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=64000\r\n", rtp_code);
10465 break;
10466 }
10467
10468 if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
10469 *min_packet_size = fmt.cur_ms;
10470
10471
10472 if ((*min_packet_size)==0 && fmt.cur_ms)
10473 *min_packet_size = fmt.cur_ms;
10474 }
10475
10476
10477
10478 static void add_vcodec_to_sdp(const struct sip_pvt *p, format_t codec,
10479 struct ast_str **m_buf, struct ast_str **a_buf,
10480 int debug, int *min_packet_size)
10481 {
10482 int rtp_code;
10483
10484 if (!p->vrtp)
10485 return;
10486
10487 if (debug)
10488 ast_verbose("Adding video codec 0x%" PRIx64 " (%s) to SDP\n", codec, ast_getformatname(codec));
10489
10490 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->vrtp), 1, codec)) == -1)
10491 return;
10492
10493 ast_str_append(m_buf, 0, " %d", rtp_code);
10494 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10495 ast_rtp_lookup_mime_subtype2(1, codec, 0),
10496 ast_rtp_lookup_sample_rate2(1, codec));
10497
10498 }
10499
10500
10501 static void add_tcodec_to_sdp(const struct sip_pvt *p, int codec,
10502 struct ast_str **m_buf, struct ast_str **a_buf,
10503 int debug, int *min_packet_size)
10504 {
10505 int rtp_code;
10506
10507 if (!p->trtp)
10508 return;
10509
10510 if (debug)
10511 ast_verbose("Adding text codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
10512
10513 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, codec)) == -1)
10514 return;
10515
10516 ast_str_append(m_buf, 0, " %d", rtp_code);
10517 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10518 ast_rtp_lookup_mime_subtype2(1, codec, 0),
10519 ast_rtp_lookup_sample_rate2(1, codec));
10520
10521
10522 if (codec == AST_FORMAT_T140RED) {
10523 int t140code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, AST_FORMAT_T140);
10524 ast_str_append(a_buf, 0, "a=fmtp:%d %d/%d/%d\r\n", rtp_code,
10525 t140code,
10526 t140code,
10527 t140code);
10528
10529 }
10530 }
10531
10532
10533
10534 static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
10535 {
10536 switch (rate) {
10537 case AST_T38_RATE_2400:
10538 return 2400;
10539 case AST_T38_RATE_4800:
10540 return 4800;
10541 case AST_T38_RATE_7200:
10542 return 7200;
10543 case AST_T38_RATE_9600:
10544 return 9600;
10545 case AST_T38_RATE_12000:
10546 return 12000;
10547 case AST_T38_RATE_14400:
10548 return 14400;
10549 default:
10550 return 0;
10551 }
10552 }
10553
10554
10555 static void add_noncodec_to_sdp(const struct sip_pvt *p, int format,
10556 struct ast_str **m_buf, struct ast_str **a_buf,
10557 int debug)
10558 {
10559 int rtp_code;
10560
10561 if (debug)
10562 ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype2(0, format, 0));
10563 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 0, format)) == -1)
10564 return;
10565
10566 ast_str_append(m_buf, 0, " %d", rtp_code);
10567 ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10568 ast_rtp_lookup_mime_subtype2(0, format, 0),
10569 ast_rtp_lookup_sample_rate2(0, format));
10570 if (format == AST_RTP_DTMF)
10571 ast_str_append(a_buf, 0, "a=fmtp:%d 0-16\r\n", rtp_code);
10572 }
10573
10574
10575
10576
10577 static void get_our_media_address(struct sip_pvt *p, int needvideo, int needtext,
10578 struct ast_sockaddr *addr, struct ast_sockaddr *vaddr,
10579 struct ast_sockaddr *taddr, struct ast_sockaddr *dest,
10580 struct ast_sockaddr *vdest, struct ast_sockaddr *tdest)
10581 {
10582 int use_externip = 0;
10583
10584
10585 ast_rtp_instance_get_local_address(p->rtp, addr);
10586 if (p->vrtp) {
10587 ast_rtp_instance_get_local_address(p->vrtp, vaddr);
10588 }
10589 if (p->trtp) {
10590 ast_rtp_instance_get_local_address(p->trtp, taddr);
10591 }
10592
10593
10594
10595
10596 use_externip = ast_sockaddr_cmp_addr(&p->ourip, addr);
10597
10598
10599
10600 if (!ast_sockaddr_isnull(&p->redirip)) {
10601 ast_sockaddr_copy(dest, &p->redirip);
10602 } else {
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614 ast_sockaddr_copy(dest,
10615 !ast_sockaddr_isnull(&media_address) ? &media_address :
10616 !ast_sockaddr_is_any(addr) && !use_externip ? addr :
10617 &p->ourip);
10618 ast_sockaddr_set_port(dest, ast_sockaddr_port(addr));
10619 }
10620
10621 if (needvideo) {
10622
10623 if (!ast_sockaddr_isnull(&p->vredirip)) {
10624 ast_sockaddr_copy(vdest, &p->vredirip);
10625 } else {
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637 ast_sockaddr_copy(vdest,
10638 !ast_sockaddr_isnull(&media_address) ? &media_address :
10639 !ast_sockaddr_is_any(vaddr) && !use_externip ? vaddr :
10640 &p->ourip);
10641 ast_sockaddr_set_port(vdest, ast_sockaddr_port(vaddr));
10642 }
10643 }
10644
10645 if (needtext) {
10646
10647 if (!ast_sockaddr_isnull(&p->tredirip)) {
10648 ast_sockaddr_copy(tdest, &p->tredirip);
10649 } else {
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661 ast_sockaddr_copy(tdest,
10662 !ast_sockaddr_isnull(&media_address) ? &media_address :
10663 !ast_sockaddr_is_any(taddr) && !use_externip ? taddr :
10664 &p->ourip);
10665 ast_sockaddr_set_port(tdest, ast_sockaddr_port(taddr));
10666 }
10667 }
10668 }
10669
10670 static void get_crypto_attrib(struct sip_srtp *srtp, const char **a_crypto)
10671 {
10672
10673 if (srtp) {
10674 if (!srtp->crypto) {
10675 srtp->crypto = sdp_crypto_setup();
10676 }
10677 if (srtp->crypto && (sdp_crypto_offer(srtp->crypto) >= 0)) {
10678 *a_crypto = sdp_crypto_attrib(srtp->crypto);
10679 }
10680
10681 if (!*a_crypto) {
10682 ast_log(LOG_WARNING, "No SRTP key management enabled\n");
10683 }
10684 }
10685 }
10686
10687
10688
10689
10690
10691
10692
10693 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10694 {
10695 format_t alreadysent = 0;
10696 int doing_directmedia = FALSE;
10697
10698 struct ast_sockaddr addr = { {0,} };
10699 struct ast_sockaddr vaddr = { {0,} };
10700 struct ast_sockaddr taddr = { {0,} };
10701 struct ast_sockaddr udptladdr = { {0,} };
10702 struct ast_sockaddr dest = { {0,} };
10703 struct ast_sockaddr vdest = { {0,} };
10704 struct ast_sockaddr tdest = { {0,} };
10705 struct ast_sockaddr udptldest = { {0,} };
10706
10707
10708 char *version = "v=0\r\n";
10709 char subject[256];
10710 char owner[256];
10711 char connection[256];
10712 char *session_time = "t=0 0\r\n";
10713 char bandwidth[256] = "";
10714 char *hold = "";
10715 struct ast_str *m_audio = ast_str_alloca(256);
10716 struct ast_str *m_video = ast_str_alloca(256);
10717 struct ast_str *m_text = ast_str_alloca(256);
10718 struct ast_str *m_modem = ast_str_alloca(256);
10719 struct ast_str *a_audio = ast_str_alloca(1024);
10720 struct ast_str *a_video = ast_str_alloca(1024);
10721 struct ast_str *a_text = ast_str_alloca(1024);
10722 struct ast_str *a_modem = ast_str_alloca(1024);
10723 const char *a_crypto = NULL;
10724 const char *v_a_crypto = NULL;
10725 const char *t_a_crypto = NULL;
10726
10727 format_t x;
10728 format_t capability = 0;
10729 int needaudio = FALSE;
10730 int needvideo = FALSE;
10731 int needtext = FALSE;
10732 int debug = sip_debug_test_pvt(p);
10733 int min_audio_packet_size = 0;
10734 int min_video_packet_size = 0;
10735 int min_text_packet_size = 0;
10736
10737 char codecbuf[SIPBUFSIZE];
10738 char buf[SIPBUFSIZE];
10739 char dummy_answer[256];
10740
10741
10742 snprintf(subject, sizeof(subject), "s=%s\r\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
10743
10744 if (!p->rtp) {
10745 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
10746 return AST_FAILURE;
10747 }
10748
10749
10750
10751
10752 if (!p->sessionid) {
10753 p->sessionid = (int)ast_random();
10754 p->sessionversion = p->sessionid;
10755 } else {
10756 if (oldsdp == FALSE)
10757 p->sessionversion++;
10758 }
10759
10760 if (add_audio) {
10761 doing_directmedia = (!ast_sockaddr_isnull(&p->redirip) && p->redircodecs) ? TRUE : FALSE;
10762
10763 if ((p->jointcapability & AST_FORMAT_VIDEO_MASK) && !p->novideo) {
10764 if (p->vrtp) {
10765 needvideo = TRUE;
10766 ast_debug(2, "This call needs video offers!\n");
10767 } else
10768 ast_debug(2, "This call needs video offers, but there's no video support enabled!\n");
10769 }
10770
10771 if ((p->jointcapability & AST_FORMAT_TEXT_MASK) && !p->notext) {
10772 if (sipdebug_text)
10773 ast_verbose("We think we can do text\n");
10774 if (p->trtp) {
10775 if (sipdebug_text) {
10776 ast_verbose("And we have a text rtp object\n");
10777 }
10778 needtext = TRUE;
10779 ast_debug(2, "This call needs text offers! \n");
10780 } else {
10781 ast_debug(2, "This call needs text offers, but there's no text support enabled ! \n");
10782 }
10783 }
10784 }
10785
10786 get_our_media_address(p, needvideo, needtext, &addr, &vaddr, &taddr, &dest, &vdest, &tdest);
10787
10788 snprintf(owner, sizeof(owner), "o=%s %d %d IN %s %s\r\n",
10789 ast_strlen_zero(global_sdpowner) ? "-" : global_sdpowner,
10790 p->sessionid, p->sessionversion,
10791 (ast_sockaddr_is_ipv6(&dest) && !ast_sockaddr_is_ipv4_mapped(&dest)) ?
10792 "IP6" : "IP4",
10793 ast_sockaddr_stringify_addr(&dest));
10794
10795 snprintf(connection, sizeof(connection), "c=IN %s %s\r\n",
10796 (ast_sockaddr_is_ipv6(&dest) && !ast_sockaddr_is_ipv4_mapped(&dest)) ?
10797 "IP6" : "IP4",
10798 ast_sockaddr_stringify_addr(&dest));
10799
10800 if (add_audio) {
10801 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR) {
10802 hold = "a=recvonly\r\n";
10803 doing_directmedia = FALSE;
10804 } else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE) {
10805 hold = "a=inactive\r\n";
10806 doing_directmedia = FALSE;
10807 } else {
10808 hold = "a=sendrecv\r\n";
10809 }
10810
10811 capability = p->jointcapability;
10812
10813
10814 ast_debug(1, "** Our capability: %s Video flag: %s Text flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability),
10815 p->novideo ? "True" : "False", p->notext ? "True" : "False");
10816 ast_debug(1, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
10817
10818 if (doing_directmedia) {
10819 capability &= p->redircodecs;
10820 ast_debug(1, "** Our native-bridge filtered capablity: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability));
10821 }
10822
10823
10824 if (capability & AST_FORMAT_AUDIO_MASK)
10825 needaudio = TRUE;
10826
10827 if (debug) {
10828 ast_verbose("Audio is at %s\n", ast_sockaddr_stringify_port(&p->ourip));
10829 }
10830
10831
10832
10833 if (needvideo) {
10834 get_crypto_attrib(p->vsrtp, &v_a_crypto);
10835 ast_str_append(&m_video, 0, "m=video %d RTP/%s", ast_sockaddr_port(&vdest),
10836 v_a_crypto ? "SAVP" : "AVP");
10837
10838
10839 if (p->maxcallbitrate)
10840 snprintf(bandwidth, sizeof(bandwidth), "b=CT:%d\r\n", p->maxcallbitrate);
10841 if (debug) {
10842 ast_verbose("Video is at %s\n", ast_sockaddr_stringify(&p->ourip));
10843 }
10844 }
10845
10846
10847
10848 if (needtext) {
10849 if (sipdebug_text)
10850 ast_verbose("Lets set up the text sdp\n");
10851 get_crypto_attrib(p->tsrtp, &t_a_crypto);
10852 ast_str_append(&m_text, 0, "m=text %d RTP/%s", ast_sockaddr_port(&tdest),
10853 t_a_crypto ? "SAVP" : "AVP");
10854 if (debug) {
10855 ast_verbose("Text is at %s\n", ast_sockaddr_stringify(&p->ourip));
10856 }
10857 }
10858
10859
10860
10861
10862
10863
10864 get_crypto_attrib(p->srtp, &a_crypto);
10865 ast_str_append(&m_audio, 0, "m=audio %d RTP/%s", ast_sockaddr_port(&dest),
10866 a_crypto ? "SAVP" : "AVP");
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877 if (capability & p->prefcodec) {
10878 format_t codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
10879
10880 add_codec_to_sdp(p, codec, &m_audio, &a_audio, debug, &min_audio_packet_size);
10881 alreadysent |= codec;
10882 }
10883
10884
10885 for (x = 0; x < 64; x++) {
10886 format_t codec;
10887
10888 if (!(codec = ast_codec_pref_index(&p->prefs, x)))
10889 break;
10890
10891 if (!(capability & codec))
10892 continue;
10893
10894 if (alreadysent & codec)
10895 continue;
10896
10897 add_codec_to_sdp(p, codec, &m_audio, &a_audio, debug, &min_audio_packet_size);
10898 alreadysent |= codec;
10899 }
10900
10901
10902 for (x = 1ULL; x <= (needtext ? AST_FORMAT_TEXT_MASK : (needvideo ? AST_FORMAT_VIDEO_MASK : AST_FORMAT_AUDIO_MASK)); x <<= 1) {
10903 if (!(capability & x))
10904 continue;
10905
10906 if (alreadysent & x)
10907 continue;
10908
10909 if (x & AST_FORMAT_AUDIO_MASK)
10910 add_codec_to_sdp(p, x, &m_audio, &a_audio, debug, &min_audio_packet_size);
10911 else if (x & AST_FORMAT_VIDEO_MASK)
10912 add_vcodec_to_sdp(p, x, &m_video, &a_video, debug, &min_video_packet_size);
10913 else if (x & AST_FORMAT_TEXT_MASK)
10914 add_tcodec_to_sdp(p, x, &m_text, &a_text, debug, &min_text_packet_size);
10915 }
10916
10917
10918 for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
10919 if (!(p->jointnoncodeccapability & x))
10920 continue;
10921
10922 add_noncodec_to_sdp(p, x, &m_audio, &a_audio, debug);
10923 }
10924
10925 ast_debug(3, "-- Done with adding codecs to SDP\n");
10926
10927 if (!p->owner || !ast_internal_timing_enabled(p->owner))
10928 ast_str_append(&a_audio, 0, "a=silenceSupp:off - - - -\r\n");
10929
10930 if (min_audio_packet_size)
10931 ast_str_append(&a_audio, 0, "a=ptime:%d\r\n", min_audio_packet_size);
10932
10933
10934 if (min_video_packet_size)
10935 ast_str_append(&a_video, 0, "a=ptime:%d\r\n", min_video_packet_size);
10936
10937
10938 if (min_text_packet_size)
10939 ast_str_append(&a_text, 0, "a=ptime:%d\r\n", min_text_packet_size);
10940
10941 if (m_audio->len - m_audio->used < 2 || m_video->len - m_video->used < 2 ||
10942 m_text->len - m_text->used < 2 || a_text->len - a_text->used < 2 ||
10943 a_audio->len - a_audio->used < 2 || a_video->len - a_video->used < 2)
10944 ast_log(LOG_WARNING, "SIP SDP may be truncated due to undersized buffer!!\n");
10945 }
10946
10947 if (add_t38) {
10948
10949 ast_udptl_get_us(p->udptl, &udptladdr);
10950
10951
10952 if (!ast_sockaddr_isnull(&p->udptlredirip)) {
10953 ast_sockaddr_copy(&udptldest, &p->udptlredirip);
10954 } else {
10955 ast_sockaddr_copy(&udptldest, &p->ourip);
10956 ast_sockaddr_set_port(&udptldest, ast_sockaddr_port(&udptladdr));
10957 }
10958
10959 if (debug) {
10960 ast_debug(1, "T.38 UDPTL is at %s port %d\n", ast_sockaddr_stringify_addr(&p->ourip), ast_sockaddr_port(&udptladdr));
10961 }
10962
10963
10964
10965
10966 ast_str_append(&m_modem, 0, "m=image %d udptl t38\r\n", ast_sockaddr_port(&udptldest));
10967
10968 if (!ast_sockaddr_cmp(&udptldest, &dest)) {
10969 ast_str_append(&m_modem, 0, "c=IN %s %s\r\n",
10970 (ast_sockaddr_is_ipv6(&dest) && !ast_sockaddr_is_ipv4_mapped(&dest)) ?
10971 "IP6" : "IP4", ast_sockaddr_stringify_addr(&udptldest));
10972 }
10973
10974 ast_str_append(&a_modem, 0, "a=T38FaxVersion:%d\r\n", p->t38.our_parms.version);
10975 ast_str_append(&a_modem, 0, "a=T38MaxBitRate:%d\r\n", t38_get_rate(p->t38.our_parms.rate));
10976 if (p->t38.our_parms.fill_bit_removal) {
10977 ast_str_append(&a_modem, 0, "a=T38FaxFillBitRemoval\r\n");
10978 }
10979 if (p->t38.our_parms.transcoding_mmr) {
10980 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingMMR\r\n");
10981 }
10982 if (p->t38.our_parms.transcoding_jbig) {
10983 ast_str_append(&a_modem, 0, "a=T38FaxTranscodingJBIG\r\n");
10984 }
10985 switch (p->t38.our_parms.rate_management) {
10986 case AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF:
10987 ast_str_append(&a_modem, 0, "a=T38FaxRateManagement:transferredTCF\r\n");
10988 break;
10989 case AST_T38_RATE_MANAGEMENT_LOCAL_TCF:
10990 ast_str_append(&a_modem, 0, "a=T38FaxRateManagement:localTCF\r\n");
10991 break;
10992 }
10993 ast_str_append(&a_modem, 0, "a=T38FaxMaxDatagram:%u\r\n", ast_udptl_get_local_max_datagram(p->udptl));
10994 switch (ast_udptl_get_error_correction_scheme(p->udptl)) {
10995 case UDPTL_ERROR_CORRECTION_NONE:
10996 break;
10997 case UDPTL_ERROR_CORRECTION_FEC:
10998 ast_str_append(&a_modem, 0, "a=T38FaxUdpEC:t38UDPFEC\r\n");
10999 break;
11000 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
11001 ast_str_append(&a_modem, 0, "a=T38FaxUdpEC:t38UDPRedundancy\r\n");
11002 break;
11003 }
11004 }
11005
11006 if (needaudio)
11007 ast_str_append(&m_audio, 0, "\r\n");
11008 if (needvideo)
11009 ast_str_append(&m_video, 0, "\r\n");
11010 if (needtext)
11011 ast_str_append(&m_text, 0, "\r\n");
11012
11013 add_header(resp, "Content-Type", "application/sdp");
11014 add_content(resp, version);
11015 add_content(resp, owner);
11016 add_content(resp, subject);
11017 add_content(resp, connection);
11018 if (needvideo)
11019 add_content(resp, bandwidth);
11020 add_content(resp, session_time);
11021 if (needaudio) {
11022 add_content(resp, m_audio->str);
11023 add_content(resp, a_audio->str);
11024 add_content(resp, hold);
11025 if (a_crypto) {
11026 add_content(resp, a_crypto);
11027 }
11028 } else if (p->offered_media[SDP_AUDIO].offered) {
11029 snprintf(dummy_answer, sizeof(dummy_answer), "m=audio 0 RTP/AVP %s\r\n", p->offered_media[SDP_AUDIO].codecs);
11030 add_content(resp, dummy_answer);
11031 }
11032 if (needvideo) {
11033 add_content(resp, m_video->str);
11034 add_content(resp, a_video->str);
11035 add_content(resp, hold);
11036 if (v_a_crypto) {
11037 add_content(resp, v_a_crypto);
11038 }
11039 } else if (p->offered_media[SDP_VIDEO].offered) {
11040 snprintf(dummy_answer, sizeof(dummy_answer), "m=video 0 RTP/AVP %s\r\n", p->offered_media[SDP_VIDEO].codecs);
11041 add_content(resp, dummy_answer);
11042 }
11043 if (needtext) {
11044 add_content(resp, m_text->str);
11045 add_content(resp, a_text->str);
11046 add_content(resp, hold);
11047 if (t_a_crypto) {
11048 add_content(resp, t_a_crypto);
11049 }
11050 } else if (p->offered_media[SDP_TEXT].offered) {
11051 snprintf(dummy_answer, sizeof(dummy_answer), "m=text 0 RTP/AVP %s\r\n", p->offered_media[SDP_TEXT].codecs);
11052 add_content(resp, dummy_answer);
11053 }
11054 if (add_t38) {
11055 add_content(resp, m_modem->str);
11056 add_content(resp, a_modem->str);
11057 } else if (p->offered_media[SDP_IMAGE].offered) {
11058 add_content(resp, "m=image 0 udptl t38\r\n");
11059 }
11060
11061
11062 p->lastrtprx = p->lastrtptx = time(NULL);
11063
11064 ast_debug(3, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
11065
11066 return AST_SUCCESS;
11067 }
11068
11069
11070 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
11071 {
11072 struct sip_request resp;
11073 int seqno;
11074
11075 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
11076 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
11077 return -1;
11078 }
11079 respprep(&resp, p, msg, req);
11080 if (p->udptl) {
11081 add_sdp(&resp, p, 0, 0, 1);
11082 } else
11083 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no UDPTL session allocated. Call-ID %s\n", p->callid);
11084 if (retrans && !p->pendinginvite)
11085 p->pendinginvite = seqno;
11086 return send_response(p, &resp, retrans, seqno);
11087 }
11088
11089
11090 static void copy_request(struct sip_request *dst, const struct sip_request *src)
11091 {
11092
11093
11094
11095 struct ast_str *duplicate = dst->data;
11096 struct ast_str *duplicate_content = dst->content;
11097
11098
11099
11100 memcpy(dst, src, sizeof(*dst));
11101 dst->data = duplicate;
11102 dst->content = duplicate_content;
11103
11104
11105 if (!dst->data && !(dst->data = ast_str_create(ast_str_strlen(src->data) + 1)))
11106 return;
11107 ast_str_copy_string(&dst->data, src->data);
11108
11109
11110 if (src->content) {
11111 if (!dst->content && !(dst->content = ast_str_create(ast_str_strlen(src->content) + 1)))
11112 return;
11113 ast_str_copy_string(&dst->content, src->content);
11114 }
11115 }
11116
11117 static void add_cc_call_info_to_response(struct sip_pvt *p, struct sip_request *resp)
11118 {
11119 char uri[SIPBUFSIZE];
11120 struct ast_str *header = ast_str_alloca(SIPBUFSIZE);
11121 struct ast_cc_agent *agent = find_sip_cc_agent_by_original_callid(p);
11122 struct sip_cc_agent_pvt *agent_pvt;
11123
11124 if (!agent) {
11125
11126
11127
11128 ast_log(LOG_WARNING, "Can't find SIP CC agent for call '%s' even though OFFER_CC flag was set?\n", p->callid);
11129 return;
11130 }
11131
11132 agent_pvt = agent->private_data;
11133
11134 if (!ast_strlen_zero(agent_pvt->subscribe_uri)) {
11135 ast_copy_string(uri, agent_pvt->subscribe_uri, sizeof(uri));
11136 } else {
11137 generate_uri(p, uri, sizeof(uri));
11138 ast_copy_string(agent_pvt->subscribe_uri, uri, sizeof(agent_pvt->subscribe_uri));
11139 }
11140
11141
11142
11143
11144 ast_str_set(&header, 0, "<%s>;purpose=call-completion;m=%s", uri, "NR");
11145 add_header(resp, "Call-Info", ast_str_buffer(header));
11146 ao2_ref(agent, -1);
11147 }
11148
11149
11150
11151
11152 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)
11153 {
11154 struct sip_request resp;
11155 int seqno;
11156 if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
11157 ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
11158 return -1;
11159 }
11160 respprep(&resp, p, msg, req);
11161 if (rpid == TRUE) {
11162 add_rpid(&resp, p);
11163 }
11164 if (ast_test_flag(&p->flags[0], SIP_OFFER_CC)) {
11165 add_cc_call_info_to_response(p, &resp);
11166 }
11167 if (p->rtp) {
11168 if (!p->autoframing && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
11169 ast_debug(1, "Setting framing from config on incoming call\n");
11170 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
11171 }
11172 ast_rtp_instance_activate(p->rtp);
11173 try_suggested_sip_codec(p);
11174 if (p->t38.state == T38_ENABLED) {
11175 add_sdp(&resp, p, oldsdp, TRUE, TRUE);
11176 } else {
11177 add_sdp(&resp, p, oldsdp, TRUE, FALSE);
11178 }
11179 } else
11180 ast_log(LOG_ERROR, "Can't add SDP to response, since we have no RTP session allocated. Call-ID %s\n", p->callid);
11181 if (reliable && !p->pendinginvite)
11182 p->pendinginvite = seqno;
11183 return send_response(p, &resp, reliable, seqno);
11184 }
11185
11186
11187 static int determine_firstline_parts(struct sip_request *req)
11188 {
11189 char *e = ast_skip_blanks(req->data->str);
11190 char *local_rlPart1;
11191
11192 if (!*e)
11193 return -1;
11194 req->rlPart1 = e - req->data->str;
11195 local_rlPart1 = e;
11196 e = ast_skip_nonblanks(e);
11197 if (*e)
11198 *e++ = '\0';
11199
11200 e = ast_skip_blanks(e);
11201 if ( !*e )
11202 return -1;
11203 ast_trim_blanks(e);
11204
11205 if (!strcasecmp(local_rlPart1, "SIP/2.0") ) {
11206 if (strlen(e) < 3)
11207 return -1;
11208 req->rlPart2 = e - req->data->str;
11209 } else {
11210 if ( *e == '<' ) {
11211 ast_debug(3, "Oops. Bogus uri in <> %s\n", e);
11212 e++;
11213 if (!*e)
11214 return -1;
11215 }
11216 req->rlPart2 = e - req->data->str;
11217 e = ast_skip_nonblanks(e);
11218 if (*e)
11219 *e++ = '\0';
11220 e = ast_skip_blanks(e);
11221 if (strcasecmp(e, "SIP/2.0") ) {
11222 ast_debug(3, "Skipping packet - Bad request protocol %s\n", e);
11223 return -1;
11224 }
11225 }
11226 return 1;
11227 }
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242 static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int oldsdp)
11243 {
11244 struct sip_request req;
11245
11246 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
11247
11248 add_header(&req, "Allow", ALLOWED_METHODS);
11249 add_supported_header(p, &req);
11250 if (sipdebug) {
11251 if (oldsdp == TRUE)
11252 add_header(&req, "X-asterisk-Info", "SIP re-invite (Session-Timers)");
11253 else
11254 add_header(&req, "X-asterisk-Info", "SIP re-invite (External RTP bridge)");
11255 }
11256
11257 if (ast_test_flag(&p->flags[0], SIP_SENDRPID))
11258 add_rpid(&req, p);
11259
11260 if (p->do_history) {
11261 append_history(p, "ReInv", "Re-invite sent");
11262 }
11263 memset(p->offered_media, 0, sizeof(p->offered_media));
11264
11265 try_suggested_sip_codec(p);
11266 if (t38version) {
11267 add_sdp(&req, p, oldsdp, FALSE, TRUE);
11268 } else {
11269 add_sdp(&req, p, oldsdp, TRUE, FALSE);
11270 }
11271
11272
11273 initialize_initreq(p, &req);
11274 p->lastinvite = p->ocseq;
11275 ast_set_flag(&p->flags[0], SIP_OUTGOING);
11276
11277 return send_request(p, &req, XMIT_CRITICAL, p->ocseq);
11278 }
11279
11280
11281 static char *remove_uri_parameters(char *uri)
11282 {
11283 char *atsign;
11284 atsign = strchr(uri, '@');
11285 if (!atsign) {
11286 atsign = uri;
11287 }
11288 atsign = strchr(atsign, ';');
11289 if (atsign)
11290 *atsign = '\0';
11291 return uri;
11292 }
11293
11294
11295 static void extract_uri(struct sip_pvt *p, struct sip_request *req)
11296 {
11297 char stripped[SIPBUFSIZE];
11298 char *c;
11299
11300 ast_copy_string(stripped, get_header(req, "Contact"), sizeof(stripped));
11301 c = get_in_brackets(stripped);
11302
11303 c = remove_uri_parameters(c);
11304 if (!ast_strlen_zero(c)) {
11305 ast_string_field_set(p, uri, c);
11306 }
11307
11308 }
11309
11310
11311 static void build_contact(struct sip_pvt *p)
11312 {
11313 char tmp[SIPBUFSIZE];
11314 char *user = ast_uri_encode(p->exten, tmp, sizeof(tmp), 1);
11315
11316 if (p->socket.type == SIP_TRANSPORT_UDP) {
11317 ast_string_field_build(p, our_contact, "<sip:%s%s%s>", user,
11318 ast_strlen_zero(user) ? "" : "@", ast_sockaddr_stringify(&p->ourip));
11319 } else {
11320 ast_string_field_build(p, our_contact, "<sip:%s%s%s;transport=%s>", user,
11321 ast_strlen_zero(user) ? "" : "@", ast_sockaddr_stringify(&p->ourip),
11322 get_transport(p->socket.type));
11323 }
11324 }
11325
11326
11327 static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, const char * const explicit_uri)
11328 {
11329 struct ast_str *invite = ast_str_alloca(256);
11330 char from[256];
11331 char to[256];
11332 char tmp_n[SIPBUFSIZE/2];
11333 char tmp_l[SIPBUFSIZE/2];
11334 const char *l = NULL;
11335 const char *n = NULL;
11336 const char *d = NULL;
11337 const char *urioptions = "";
11338 int ourport;
11339
11340 if (ast_test_flag(&p->flags[0], SIP_USEREQPHONE)) {
11341 const char *s = p->username;
11342
11343
11344
11345
11346
11347
11348 if (*s == '+')
11349 s++;
11350 for (; *s; s++) {
11351 if (!strchr(AST_DIGIT_ANYNUM, *s) )
11352 break;
11353 }
11354
11355 if (!*s)
11356 urioptions = ";user=phone";
11357 }
11358
11359
11360 snprintf(p->lastmsg, sizeof(p->lastmsg), "Init: %s", sip_methods[sipmethod].text);
11361
11362 d = S_OR(p->fromdomain, ast_sockaddr_stringify_host(&p->ourip));
11363 if (p->owner) {
11364 if ((ast_party_id_presentation(&p->owner->connected.id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED) {
11365 l = p->owner->connected.id.number.valid ? p->owner->connected.id.number.str : NULL;
11366 n = p->owner->connected.id.name.valid ? p->owner->connected.id.name.str : NULL;
11367 } else if (!ast_test_flag(&p->flags[0], SIP_SENDRPID)) {
11368
11369 l = CALLERID_UNKNOWN;
11370 n = l;
11371 d = FROMDOMAIN_INVALID;
11372 }
11373 }
11374
11375
11376
11377
11378
11379
11380
11381 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->mwi_from)) {
11382 l = p->mwi_from;
11383 }
11384
11385 if (ast_strlen_zero(l))
11386 l = default_callerid;
11387 if (ast_strlen_zero(n))
11388 n = l;
11389
11390 if (!ast_strlen_zero(p->fromuser))
11391 l = p->fromuser;
11392 else
11393 ast_string_field_set(p, fromuser, l);
11394
11395
11396 if (!ast_strlen_zero(p->fromname))
11397 n = p->fromname;
11398 else
11399 ast_string_field_set(p, fromname, n);
11400
11401 if (sip_cfg.pedanticsipchecking) {
11402 ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
11403 n = tmp_n;
11404 ast_uri_encode(l, tmp_l, sizeof(tmp_l), 0);
11405 l = tmp_l;
11406 }
11407
11408 ourport = (p->fromdomainport) ? p->fromdomainport : ast_sockaddr_port(&p->ourip);
11409 if (!sip_standard_port(p->socket.type, ourport)) {
11410 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s:%d>;tag=%s", n, l, d, ourport, p->tag);
11411 } else {
11412 snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%s", n, l, d, p->tag);
11413 }
11414
11415 if (!ast_strlen_zero(explicit_uri)) {
11416 ast_str_set(&invite, 0, "%s", explicit_uri);
11417 } else {
11418
11419 if (!ast_strlen_zero(p->fullcontact)) {
11420
11421 ast_str_append(&invite, 0, "%s", p->fullcontact);
11422 } else {
11423
11424 ast_str_append(&invite, 0, "sip:");
11425 if (!ast_strlen_zero(p->username)) {
11426 n = p->username;
11427 if (sip_cfg.pedanticsipchecking) {
11428 ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
11429 n = tmp_n;
11430 }
11431 ast_str_append(&invite, 0, "%s@", n);
11432 }
11433 ast_str_append(&invite, 0, "%s", p->tohost);
11434 if (p->portinuri) {
11435 ast_str_append(&invite, 0, ":%d", ast_sockaddr_port(&p->sa));
11436 }
11437 ast_str_append(&invite, 0, "%s", urioptions);
11438 }
11439 }
11440
11441
11442 if (p->options && !ast_strlen_zero(p->options->uri_options))
11443 ast_str_append(&invite, 0, ";%s", p->options->uri_options);
11444
11445
11446
11447
11448 ast_string_field_set(p, uri, invite->str);
11449
11450 if (!ast_strlen_zero(p->todnid)) {
11451
11452 if (!strchr(p->todnid, '@')) {
11453
11454 snprintf(to, sizeof(to), "<sip:%s@%s>%s%s", p->todnid, p->tohost, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
11455 } else {
11456 snprintf(to, sizeof(to), "<sip:%s>%s%s", p->todnid, ast_strlen_zero(p->theirtag) ? "" : ";tag=", p->theirtag);
11457 }
11458 } else {
11459 if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
11460
11461 snprintf(to, sizeof(to), "<%s%s>;tag=%s", (strncasecmp(p->uri, "sip:", 4) ? "sip:" : ""), p->uri, p->theirtag);
11462 } else if (p->options && p->options->vxml_url) {
11463
11464 snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
11465 } else {
11466 snprintf(to, sizeof(to), "<%s>", p->uri);
11467 }
11468 }
11469
11470 init_req(req, sipmethod, p->uri);
11471
11472 snprintf(tmp_n, sizeof(tmp_n), "%d %s", ++p->ocseq, sip_methods[sipmethod].text);
11473
11474 add_header(req, "Via", p->via);
11475 add_header_max_forwards(p, req);
11476
11477
11478
11479
11480 add_route(req, p->route);
11481
11482 add_header(req, "From", from);
11483 add_header(req, "To", to);
11484 ast_string_field_set(p, exten, l);
11485 build_contact(p);
11486 add_header(req, "Contact", p->our_contact);
11487 add_header(req, "Call-ID", p->callid);
11488 add_header(req, "CSeq", tmp_n);
11489 if (!ast_strlen_zero(global_useragent)) {
11490 add_header(req, "User-Agent", global_useragent);
11491 }
11492 }
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502 static void add_diversion_header(struct sip_request *req, struct sip_pvt *pvt)
11503 {
11504 const char *diverting_number;
11505 const char *diverting_name;
11506 const char *reason;
11507 char header_text[256];
11508
11509 if (!pvt->owner) {
11510 return;
11511 }
11512
11513 diverting_number = pvt->owner->redirecting.from.number.str;
11514 if (!pvt->owner->redirecting.from.number.valid
11515 || ast_strlen_zero(diverting_number)) {
11516 return;
11517 }
11518
11519 reason = sip_reason_code_to_str(pvt->owner->redirecting.reason);
11520
11521
11522 diverting_name = pvt->owner->redirecting.from.name.str;
11523 if (!pvt->owner->redirecting.from.name.valid
11524 || ast_strlen_zero(diverting_name)) {
11525 snprintf(header_text, sizeof(header_text), "<sip:%s@%s>;reason=%s", diverting_number,
11526 ast_sockaddr_stringify_host(&pvt->ourip), reason);
11527 } else {
11528 snprintf(header_text, sizeof(header_text), "\"%s\" <sip:%s@%s>;reason=%s",
11529 diverting_name, diverting_number,
11530 ast_sockaddr_stringify_host(&pvt->ourip), reason);
11531 }
11532
11533 add_header(req, "Diversion", header_text);
11534 }
11535
11536 static int transmit_publish(struct sip_epa_entry *epa_entry, enum sip_publish_type publish_type, const char * const explicit_uri)
11537 {
11538 struct sip_pvt *pvt;
11539 int expires;
11540
11541 epa_entry->publish_type = publish_type;
11542
11543 if (!(pvt = sip_alloc(NULL, NULL, 0, SIP_PUBLISH, NULL))) {
11544 return -1;
11545 }
11546
11547 sip_pvt_lock(pvt);
11548
11549 if (create_addr(pvt, epa_entry->destination, NULL, TRUE, NULL)) {
11550 sip_pvt_unlock(pvt);
11551 dialog_unlink_all(pvt, TRUE, TRUE);
11552 dialog_unref(pvt, "create_addr failed in transmit_publish. Unref dialog");
11553 return -1;
11554 }
11555 ast_sip_ouraddrfor(&pvt->sa, &pvt->ourip, pvt);
11556 ast_set_flag(&pvt->flags[0], SIP_OUTGOING);
11557 expires = (publish_type == SIP_PUBLISH_REMOVE) ? 0 : DEFAULT_PUBLISH_EXPIRES;
11558 pvt->expiry = expires;
11559
11560
11561 ao2_ref(epa_entry, +1);
11562 pvt->epa_entry = epa_entry;
11563
11564 transmit_invite(pvt, SIP_PUBLISH, FALSE, 2, explicit_uri);
11565 sip_pvt_unlock(pvt);
11566 sip_scheddestroy(pvt, DEFAULT_TRANS_TIMEOUT);
11567 dialog_unref(pvt, "Done with the sip_pvt allocated for transmitting PUBLISH");
11568 return 0;
11569 }
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init, const char * const explicit_uri)
11581 {
11582 struct sip_request req;
11583 struct ast_variable *var;
11584
11585 req.method = sipmethod;
11586 if (init) {
11587 p->branch ^= ast_random();
11588 p->invite_branch = p->branch;
11589 build_via(p);
11590 }
11591 if (init > 1) {
11592 initreqprep(&req, p, sipmethod, explicit_uri);
11593 } else {
11594
11595 reqprep(&req, p, sipmethod, 0, init ? 0 : 1);
11596 }
11597
11598 if (p->options && p->options->auth) {
11599 add_header(&req, p->options->authheader, p->options->auth);
11600 }
11601 append_date(&req);
11602 if (sipmethod == SIP_REFER) {
11603 if (p->refer) {
11604 char buf[SIPBUFSIZE];
11605 if (!ast_strlen_zero(p->refer->refer_to)) {
11606 add_header(&req, "Refer-To", p->refer->refer_to);
11607 }
11608 if (!ast_strlen_zero(p->refer->referred_by)) {
11609 snprintf(buf, sizeof(buf), "%s <%s>", p->refer->referred_by_name, p->refer->referred_by);
11610 add_header(&req, "Referred-By", buf);
11611 }
11612 }
11613 } else if (sipmethod == SIP_SUBSCRIBE) {
11614 char buf[SIPBUFSIZE];
11615 if (p->subscribed == MWI_NOTIFICATION) {
11616 add_header(&req, "Event", "message-summary");
11617 add_header(&req, "Accept", "application/simple-message-summary");
11618 } else if (p->subscribed == CALL_COMPLETION) {
11619 add_header(&req, "Event", "call-completion");
11620 add_header(&req, "Accept", "application/call-completion");
11621 }
11622 snprintf(buf, sizeof(buf), "%d", p->expiry);
11623 add_header(&req, "Expires", buf);
11624 }
11625
11626
11627
11628 if (p->options && !ast_strlen_zero(p->options->replaces)) {
11629 add_header(&req, "Replaces", p->options->replaces);
11630 add_header(&req, "Require", "replaces");
11631 }
11632
11633
11634 if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
11635 char i2astr[10];
11636
11637 if (!p->stimer->st_interval) {
11638 p->stimer->st_interval = st_get_se(p, TRUE);
11639 }
11640
11641 p->stimer->st_active = TRUE;
11642
11643 snprintf(i2astr, sizeof(i2astr), "%d", p->stimer->st_interval);
11644 add_header(&req, "Session-Expires", i2astr);
11645 snprintf(i2astr, sizeof(i2astr), "%d", st_get_se(p, FALSE));
11646 add_header(&req, "Min-SE", i2astr);
11647 }
11648
11649 add_header(&req, "Allow", ALLOWED_METHODS);
11650 add_supported_header(p, &req);
11651
11652 if (p->options && p->options->addsipheaders && p->owner) {
11653 struct ast_channel *chan = p->owner;
11654 struct varshead *headp;
11655
11656 ast_channel_lock(chan);
11657
11658 headp = &chan->varshead;
11659
11660 if (!headp) {
11661 ast_log(LOG_WARNING, "No Headp for the channel...ooops!\n");
11662 } else {
11663 const struct ast_var_t *current;
11664 AST_LIST_TRAVERSE(headp, current, entries) {
11665
11666 if (!strncasecmp(ast_var_name(current), "SIPADDHEADER", strlen("SIPADDHEADER"))) {
11667 char *content, *end;
11668 const char *header = ast_var_value(current);
11669 char *headdup = ast_strdupa(header);
11670
11671
11672 if (*headdup == '"') {
11673 headdup++;
11674 }
11675 if ((content = strchr(headdup, ':'))) {
11676 *content++ = '\0';
11677 content = ast_skip_blanks(content);
11678
11679 end = content + strlen(content) -1;
11680 if (*end == '"') {
11681 *end = '\0';
11682 }
11683
11684 add_header(&req, headdup, content);
11685 if (sipdebug) {
11686 ast_debug(1, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
11687 }
11688 }
11689 }
11690 }
11691 }
11692
11693 ast_channel_unlock(chan);
11694 }
11695 if ((sipmethod == SIP_INVITE || sipmethod == SIP_UPDATE) && ast_test_flag(&p->flags[0], SIP_SENDRPID))
11696 add_rpid(&req, p);
11697 if (sipmethod == SIP_INVITE) {
11698 add_diversion_header(&req, p);
11699 }
11700 if (sdp) {
11701 memset(p->offered_media, 0, sizeof(p->offered_media));
11702 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
11703 ast_debug(1, "T38 is in state %d on channel %s\n", p->t38.state, p->owner ? p->owner->name : "<none>");
11704 add_sdp(&req, p, FALSE, FALSE, TRUE);
11705 } else if (p->rtp) {
11706 try_suggested_sip_codec(p);
11707 add_sdp(&req, p, FALSE, TRUE, FALSE);
11708 }
11709 } else if (p->notify) {
11710 for (var = p->notify->headers; var; var = var->next) {
11711 add_header(&req, var->name, var->value);
11712 }
11713 if (ast_str_strlen(p->notify->content)) {
11714 add_content(&req, ast_str_buffer(p->notify->content));
11715 }
11716 } else if (sipmethod == SIP_PUBLISH) {
11717 char expires[SIPBUFSIZE];
11718
11719 switch (p->epa_entry->static_data->event) {
11720 case CALL_COMPLETION:
11721 snprintf(expires, sizeof(expires), "%d", p->expiry);
11722 add_header(&req, "Event", "call-completion");
11723 add_header(&req, "Expires", expires);
11724 if (p->epa_entry->publish_type != SIP_PUBLISH_INITIAL) {
11725 add_header(&req, "SIP-If-Match", p->epa_entry->entity_tag);
11726 }
11727
11728 if (!ast_strlen_zero(p->epa_entry->body)) {
11729 add_header(&req, "Content-Type", "application/pidf+xml");
11730 add_content(&req, p->epa_entry->body);
11731 }
11732 default:
11733 break;
11734 }
11735 }
11736
11737 if (!p->initreq.headers || init > 2) {
11738 initialize_initreq(p, &req);
11739 }
11740 if (sipmethod == SIP_INVITE || sipmethod == SIP_SUBSCRIBE) {
11741 p->lastinvite = p->ocseq;
11742 }
11743 return send_request(p, &req, init ? XMIT_CRITICAL : XMIT_RELIABLE, p->ocseq);
11744 }
11745
11746
11747 static int sip_subscribe_mwi_do(const void *data)
11748 {
11749 struct sip_subscription_mwi *mwi = (struct sip_subscription_mwi*)data;
11750
11751 if (!mwi) {
11752 return -1;
11753 }
11754
11755 mwi->resub = -1;
11756 __sip_subscribe_mwi_do(mwi);
11757 ASTOBJ_UNREF(mwi, sip_subscribe_mwi_destroy);
11758
11759 return 0;
11760 }
11761
11762
11763 static int __sip_subscribe_mwi_do(struct sip_subscription_mwi *mwi)
11764 {
11765
11766 if (!mwi->dnsmgr) {
11767 char transport[MAXHOSTNAMELEN];
11768 snprintf(transport, sizeof(transport), "_%s._%s", get_srv_service(mwi->transport), get_srv_protocol(mwi->transport));
11769
11770 mwi->us.ss.ss_family = get_address_family_filter(&bindaddr);
11771 ast_dnsmgr_lookup(mwi->hostname, &mwi->us, &mwi->dnsmgr, sip_cfg.srvlookup ? transport : NULL);
11772 }
11773
11774
11775 if (mwi->call) {
11776 transmit_invite(mwi->call, SIP_SUBSCRIBE, 0, 0, NULL);
11777 return 0;
11778 }
11779
11780
11781 if (!(mwi->call = sip_alloc(NULL, NULL, 0, SIP_SUBSCRIBE, NULL))) {
11782 return -1;
11783 }
11784
11785 ref_proxy(mwi->call, obproxy_get(mwi->call, NULL));
11786
11787 if (!ast_sockaddr_port(&mwi->us) && mwi->portno) {
11788 ast_sockaddr_set_port(&mwi->us, mwi->portno);
11789 }
11790
11791
11792 if (create_addr(mwi->call, mwi->hostname, &mwi->us, 0, NULL)) {
11793 dialog_unlink_all(mwi->call, TRUE, TRUE);
11794 mwi->call = dialog_unref(mwi->call, "unref dialog after unlink_all");
11795 return 0;
11796 }
11797
11798 mwi->call->expiry = mwi_expiry;
11799
11800 if (!mwi->dnsmgr && mwi->portno) {
11801 ast_sockaddr_set_port(&mwi->call->sa, mwi->portno);
11802 ast_sockaddr_set_port(&mwi->call->recv, mwi->portno);
11803 } else {
11804 mwi->portno = ast_sockaddr_port(&mwi->call->sa);
11805 }
11806
11807
11808 if (!ast_strlen_zero(mwi->authuser)) {
11809 ast_string_field_set(mwi->call, peername, mwi->authuser);
11810 ast_string_field_set(mwi->call, authname, mwi->authuser);
11811 ast_string_field_set(mwi->call, fromuser, mwi->authuser);
11812 } else {
11813 ast_string_field_set(mwi->call, peername, mwi->username);
11814 ast_string_field_set(mwi->call, authname, mwi->username);
11815 ast_string_field_set(mwi->call, fromuser, mwi->username);
11816 }
11817 ast_string_field_set(mwi->call, username, mwi->username);
11818 if (!ast_strlen_zero(mwi->secret)) {
11819 ast_string_field_set(mwi->call, peersecret, mwi->secret);
11820 }
11821 set_socket_transport(&mwi->call->socket, mwi->transport);
11822 mwi->call->socket.port = htons(mwi->portno);
11823 ast_sip_ouraddrfor(&mwi->call->sa, &mwi->call->ourip, mwi->call);
11824 build_contact(mwi->call);
11825 build_via(mwi->call);
11826 build_callid_pvt(mwi->call);
11827 ast_set_flag(&mwi->call->flags[0], SIP_OUTGOING);
11828
11829
11830 mwi->call->mwi = ASTOBJ_REF(mwi);
11831
11832 mwi->call->subscribed = MWI_NOTIFICATION;
11833
11834
11835 transmit_invite(mwi->call, SIP_SUBSCRIBE, 0, 2, NULL);
11836
11837 return 0;
11838 }
11839
11840
11841 static int find_calling_channel(void *obj, void *arg, void *data, int flags)
11842 {
11843 struct ast_channel *c = obj;
11844 struct sip_pvt *p = data;
11845 int res;
11846
11847 ast_channel_lock(c);
11848
11849 res = (c->pbx &&
11850 (!strcasecmp(c->macroexten, p->exten) || !strcasecmp(c->exten, p->exten)) &&
11851 (sip_cfg.notifycid == IGNORE_CONTEXT || !strcasecmp(c->context, p->context)));
11852
11853 ast_channel_unlock(c);
11854
11855 return res ? CMP_MATCH | CMP_STOP : 0;
11856 }
11857
11858
11859 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)
11860 {
11861 enum state { NOTIFY_OPEN, NOTIFY_INUSE, NOTIFY_CLOSED } local_state = NOTIFY_OPEN;
11862 const char *statestring = "terminated";
11863 const char *pidfstate = "--";
11864 const char *pidfnote= "Ready";
11865 char hint[AST_MAX_EXTENSION];
11866
11867 switch (state) {
11868 case (AST_EXTENSION_RINGING | AST_EXTENSION_INUSE):
11869 statestring = (sip_cfg.notifyringing) ? "early" : "confirmed";
11870 local_state = NOTIFY_INUSE;
11871 pidfstate = "busy";
11872 pidfnote = "Ringing";
11873 break;
11874 case AST_EXTENSION_RINGING:
11875 statestring = "early";
11876 local_state = NOTIFY_INUSE;
11877 pidfstate = "busy";
11878 pidfnote = "Ringing";
11879 break;
11880 case AST_EXTENSION_INUSE:
11881 statestring = "confirmed";
11882 local_state = NOTIFY_INUSE;
11883 pidfstate = "busy";
11884 pidfnote = "On the phone";
11885 break;
11886 case AST_EXTENSION_BUSY:
11887 statestring = "confirmed";
11888 local_state = NOTIFY_CLOSED;
11889 pidfstate = "busy";
11890 pidfnote = "On the phone";
11891 break;
11892 case AST_EXTENSION_UNAVAILABLE:
11893 statestring = "terminated";
11894 local_state = NOTIFY_CLOSED;
11895 pidfstate = "away";
11896 pidfnote = "Unavailable";
11897 break;
11898 case AST_EXTENSION_ONHOLD:
11899 statestring = "confirmed";
11900 local_state = NOTIFY_CLOSED;
11901 pidfstate = "busy";
11902 pidfnote = "On hold";
11903 break;
11904 case AST_EXTENSION_NOT_INUSE:
11905 default:
11906
11907 break;
11908 }
11909
11910
11911 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, context, exten)) {
11912 char *hint2 = hint, *individual_hint = NULL;
11913 int hint_count = 0, unavailable_count = 0;
11914
11915 while ((individual_hint = strsep(&hint2, "&"))) {
11916 hint_count++;
11917
11918 if (ast_device_state(individual_hint) == AST_DEVICE_UNAVAILABLE)
11919 unavailable_count++;
11920 }
11921
11922
11923
11924
11925 if (hint_count > 0 && hint_count == unavailable_count) {
11926 local_state = NOTIFY_CLOSED;
11927 pidfstate = "away";
11928 pidfnote = "Not online";
11929 }
11930 }
11931
11932 switch (subscribed) {
11933 case XPIDF_XML:
11934 case CPIM_PIDF_XML:
11935 ast_str_append(tmp, 0,
11936 "<?xml version=\"1.0\"?>\n"
11937 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
11938 "<presence>\n");
11939 ast_str_append(tmp, 0, "<presentity uri=\"%s;method=SUBSCRIBE\" />\n", mfrom);
11940 ast_str_append(tmp, 0, "<atom id=\"%s\">\n", exten);
11941 ast_str_append(tmp, 0, "<address uri=\"%s;user=ip\" priority=\"0.800000\">\n", mto);
11942 ast_str_append(tmp, 0, "<status status=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "open" : (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
11943 ast_str_append(tmp, 0, "<msnsubstatus substatus=\"%s\" />\n", (local_state == NOTIFY_OPEN) ? "online" : (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
11944 ast_str_append(tmp, 0, "</address>\n</atom>\n</presence>\n");
11945 break;
11946 case PIDF_XML:
11947 ast_str_append(tmp, 0,
11948 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
11949 "<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);
11950 ast_str_append(tmp, 0, "<pp:person><status>\n");
11951 if (pidfstate[0] != '-') {
11952 ast_str_append(tmp, 0, "<ep:activities><ep:%s/></ep:activities>\n", pidfstate);
11953 }
11954 ast_str_append(tmp, 0, "</status></pp:person>\n");
11955 ast_str_append(tmp, 0, "<note>%s</note>\n", pidfnote);
11956 ast_str_append(tmp, 0, "<tuple id=\"%s\">\n", exten);
11957 ast_str_append(tmp, 0, "<contact priority=\"1\">%s</contact>\n", mto);
11958 if (pidfstate[0] == 'b')
11959 ast_str_append(tmp, 0, "<status><basic>open</basic></status>\n");
11960 else
11961 ast_str_append(tmp, 0, "<status><basic>%s</basic></status>\n", (local_state != NOTIFY_CLOSED) ? "open" : "closed");
11962 ast_str_append(tmp, 0, "</tuple>\n</presence>\n");
11963 break;
11964 case DIALOG_INFO_XML:
11965 ast_str_append(tmp, 0, "<?xml version=\"1.0\"?>");
11966 ast_str_append(tmp, 0, "<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" version=\"%d\" state=\"%s\" entity=\"%s\">", p->dialogver, full ? "full" : "partial", mto);
11967 if ((state & AST_EXTENSION_RINGING) && sip_cfg.notifyringing) {
11968 const char *local_display = exten;
11969 char *local_target = ast_strdupa(mto);
11970
11971
11972
11973
11974 if (sip_cfg.notifycid) {
11975 struct ast_channel *caller;
11976
11977 if ((caller = ast_channel_callback(find_calling_channel, NULL, p, 0))) {
11978 char *cid_num;
11979 int need;
11980
11981 ast_channel_lock(caller);
11982 cid_num = S_COR(caller->caller.id.number.valid,
11983 caller->caller.id.number.str, "");
11984 need = strlen(cid_num) + strlen(p->fromdomain) + sizeof("sip:@");
11985 local_target = alloca(need);
11986 snprintf(local_target, need, "sip:%s@%s", cid_num, p->fromdomain);
11987 local_display = ast_strdupa(S_COR(caller->caller.id.name.valid,
11988 caller->caller.id.name.str, ""));
11989 ast_channel_unlock(caller);
11990 caller = ast_channel_unref(caller);
11991 }
11992
11993
11994
11995 if (sip_cfg.pedanticsipchecking) {
11996 ast_str_append(tmp, 0, "<dialog id=\"%s\" call-id=\"pickup-%s\" local-tag=\"%s\" remote-tag=\"%s\" direction=\"recipient\">\n",
11997 exten, p->callid, p->theirtag, p->tag);
11998 } else {
11999 ast_str_append(tmp, 0, "<dialog id=\"%s\" call-id=\"pickup-%s\" direction=\"recipient\">\n",
12000 exten, p->callid);
12001 }
12002 ast_str_append(tmp, 0,
12003 "<remote>\n"
12004
12005
12006 "<identity display=\"%s\">%s</identity>\n"
12007 "<target uri=\"%s\"/>\n"
12008 "</remote>\n"
12009 "<local>\n"
12010 "<identity>%s</identity>\n"
12011 "<target uri=\"%s\"/>\n"
12012 "</local>\n",
12013 local_display, local_target, local_target, mto, mto);
12014 } else {
12015 ast_str_append(tmp, 0, "<dialog id=\"%s\" direction=\"recipient\">\n", exten);
12016 }
12017
12018 } else {
12019 ast_str_append(tmp, 0, "<dialog id=\"%s\">", exten);
12020 }
12021 ast_str_append(tmp, 0, "<state>%s</state>\n", statestring);
12022 if (state == AST_EXTENSION_ONHOLD) {
12023 ast_str_append(tmp, 0, "<local>\n<target uri=\"%s\">\n"
12024 "<param pname=\"+sip.rendering\" pvalue=\"no\"/>\n"
12025 "</target>\n</local>\n", mto);
12026 }
12027 ast_str_append(tmp, 0, "</dialog>\n</dialog-info>\n");
12028 break;
12029 case NONE:
12030 default:
12031 break;
12032 }
12033 }
12034
12035 static int transmit_cc_notify(struct ast_cc_agent *agent, struct sip_pvt *subscription, enum sip_cc_notify_state state)
12036 {
12037 struct sip_request req;
12038 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
12039 char uri[SIPBUFSIZE];
12040 char state_str[64];
12041
12042 if (state < CC_QUEUED || state > CC_READY) {
12043 ast_log(LOG_WARNING, "Invalid state provided for transmit_cc_notify (%d)\n", state);
12044 return -1;
12045 }
12046
12047 reqprep(&req, subscription, SIP_NOTIFY, 0, TRUE);
12048 snprintf(state_str, sizeof(state_str), "%s\r\n", sip_cc_notify_state_map[state].state_string);
12049 add_header(&req, "Event", "call-completion");
12050 add_header(&req, "Content-Type", "application/call-completion");
12051 if (state == CC_READY) {
12052 generate_uri(subscription, agent_pvt->notify_uri, sizeof(agent_pvt->notify_uri));
12053 snprintf(uri, sizeof(uri) - 1, "cc-URI: %s\r\n", agent_pvt->notify_uri);
12054 }
12055 add_content(&req, state_str);
12056 if (state == CC_READY) {
12057 add_content(&req, uri);
12058 }
12059 return send_request(subscription, &req, XMIT_RELIABLE, subscription->ocseq);
12060 }
12061
12062
12063 static int transmit_state_notify(struct sip_pvt *p, int state, int full, int timeout)
12064 {
12065 struct ast_str *tmp = ast_str_alloca(4000);
12066 char from[256], to[256];
12067 char *c, *mfrom, *mto;
12068 struct sip_request req;
12069 const struct cfsubscription_types *subscriptiontype;
12070
12071 memset(from, 0, sizeof(from));
12072 memset(to, 0, sizeof(to));
12073
12074 subscriptiontype = find_subscription_type(p->subscribed);
12075
12076 ast_copy_string(from, get_header(&p->initreq, "From"), sizeof(from));
12077 c = get_in_brackets(from);
12078 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
12079 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
12080 return -1;
12081 }
12082
12083 mfrom = remove_uri_parameters(c);
12084
12085 ast_copy_string(to, get_header(&p->initreq, "To"), sizeof(to));
12086 c = get_in_brackets(to);
12087 if (strncasecmp(c, "sip:", 4) && strncasecmp(c, "sips:", 5)) {
12088 ast_log(LOG_WARNING, "Huh? Not a SIP header (%s)?\n", c);
12089 return -1;
12090 }
12091 mto = remove_uri_parameters(c);
12092
12093 reqprep(&req, p, SIP_NOTIFY, 0, 1);
12094
12095 switch(state) {
12096 case AST_EXTENSION_DEACTIVATED:
12097 if (timeout)
12098 add_header(&req, "Subscription-State", "terminated;reason=timeout");
12099 else {
12100 add_header(&req, "Subscription-State", "terminated;reason=probation");
12101 add_header(&req, "Retry-After", "60");
12102 }
12103 break;
12104 case AST_EXTENSION_REMOVED:
12105 add_header(&req, "Subscription-State", "terminated;reason=noresource");
12106 break;
12107 default:
12108 if (p->expiry)
12109 add_header(&req, "Subscription-State", "active");
12110 else
12111 add_header(&req, "Subscription-State", "terminated;reason=timeout");
12112 }
12113
12114 switch (p->subscribed) {
12115 case XPIDF_XML:
12116 case CPIM_PIDF_XML:
12117 add_header(&req, "Event", subscriptiontype->event);
12118 state_notify_build_xml(state, full, p->exten, p->context, &tmp, p, p->subscribed, mfrom, mto);
12119 add_header(&req, "Content-Type", subscriptiontype->mediatype);
12120 p->dialogver++;
12121 break;
12122 case PIDF_XML:
12123 add_header(&req, "Event", subscriptiontype->event);
12124 state_notify_build_xml(state, full, p->exten, p->context, &tmp, p, p->subscribed, mfrom, mto);
12125 add_header(&req, "Content-Type", subscriptiontype->mediatype);
12126 p->dialogver++;
12127 break;
12128 case DIALOG_INFO_XML:
12129 add_header(&req, "Event", subscriptiontype->event);
12130 state_notify_build_xml(state, full, p->exten, p->context, &tmp, p, p->subscribed, mfrom, mto);
12131 add_header(&req, "Content-Type", subscriptiontype->mediatype);
12132 p->dialogver++;
12133 break;
12134 case NONE:
12135 default:
12136 break;
12137 }
12138
12139 add_content(&req, tmp->str);
12140
12141 p->pendinginvite = p->ocseq;
12142
12143 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12144 }
12145
12146
12147
12148
12149
12150
12151
12152 static int transmit_notify_with_mwi(struct sip_pvt *p, int newmsgs, int oldmsgs, const char *vmexten)
12153 {
12154 struct sip_request req;
12155 struct ast_str *out = ast_str_alloca(500);
12156 int ourport = (p->fromdomainport) ? p->fromdomainport : ast_sockaddr_port(&p->ourip);
12157 const char *domain = S_OR(p->fromdomain, ast_sockaddr_stringify_host(&p->ourip));
12158 const char *exten = S_OR(vmexten, default_vmexten);
12159
12160 initreqprep(&req, p, SIP_NOTIFY, NULL);
12161 add_header(&req, "Event", "message-summary");
12162 add_header(&req, "Content-Type", default_notifymime);
12163 ast_str_append(&out, 0, "Messages-Waiting: %s\r\n", newmsgs ? "yes" : "no");
12164
12165
12166 if (!sip_standard_port(p->socket.type, ourport)) {
12167 if (p->socket.type == SIP_TRANSPORT_UDP) {
12168 ast_str_append(&out, 0, "Message-Account: sip:%s@%s:%d\r\n", exten, domain, ourport);
12169 } else {
12170 ast_str_append(&out, 0, "Message-Account: sip:%s@%s:%d;transport=%s\r\n", exten, domain, ourport, get_transport(p->socket.type));
12171 }
12172 } else {
12173 if (p->socket.type == SIP_TRANSPORT_UDP) {
12174 ast_str_append(&out, 0, "Message-Account: sip:%s@%s\r\n", exten, domain);
12175 } else {
12176 ast_str_append(&out, 0, "Message-Account: sip:%s@%s;transport=%s\r\n", exten, domain, get_transport(p->socket.type));
12177 }
12178 }
12179
12180
12181
12182 ast_str_append(&out, 0, "Voice-Message: %d/%d%s\r\n",
12183 newmsgs, oldmsgs, (ast_test_flag(&p->flags[1], SIP_PAGE2_BUGGY_MWI) ? "" : " (0/0)"));
12184
12185 if (p->subscribed) {
12186 if (p->expiry) {
12187 add_header(&req, "Subscription-State", "active");
12188 } else {
12189 add_header(&req, "Subscription-State", "terminated;reason=timeout");
12190 }
12191 }
12192
12193 add_content(&req, out->str);
12194
12195 if (!p->initreq.headers) {
12196 initialize_initreq(p, &req);
12197 }
12198 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12199 }
12200
12201
12202 static int transmit_notify_with_sipfrag(struct sip_pvt *p, int cseq, char *message, int terminate)
12203 {
12204 struct sip_request req;
12205 char tmp[SIPBUFSIZE/2];
12206
12207 reqprep(&req, p, SIP_NOTIFY, 0, 1);
12208 snprintf(tmp, sizeof(tmp), "refer;id=%d", cseq);
12209 add_header(&req, "Event", tmp);
12210 add_header(&req, "Subscription-state", terminate ? "terminated;reason=noresource" : "active");
12211 add_header(&req, "Content-Type", "message/sipfrag;version=2.0");
12212 add_header(&req, "Allow", ALLOWED_METHODS);
12213 add_supported_header(p, &req);
12214
12215 snprintf(tmp, sizeof(tmp), "SIP/2.0 %s\r\n", message);
12216 add_content(&req, tmp);
12217
12218 if (!p->initreq.headers) {
12219 initialize_initreq(p, &req);
12220 }
12221
12222 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12223 }
12224
12225 static int manager_sipnotify(struct mansession *s, const struct message *m)
12226 {
12227 const char *channame = astman_get_header(m, "Channel");
12228 struct ast_variable *vars = astman_get_variables(m);
12229 struct sip_pvt *p;
12230 struct ast_variable *header, *var;
12231
12232 if (ast_strlen_zero(channame)) {
12233 astman_send_error(s, m, "SIPNotify requires a channel name");
12234 return 0;
12235 }
12236
12237 if (!strncasecmp(channame, "sip/", 4)) {
12238 channame += 4;
12239 }
12240
12241 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
12242 astman_send_error(s, m, "Unable to build sip pvt data for notify (memory/socket error)");
12243 return 0;
12244 }
12245
12246 if (create_addr(p, channame, NULL, 0, NULL)) {
12247
12248 dialog_unlink_all(p, TRUE, TRUE);
12249 dialog_unref(p, "unref dialog inside for loop" );
12250
12251 astman_send_error(s, m, "Could not create address");
12252 return 0;
12253 }
12254
12255
12256 ast_set_flag(&p->flags[0], SIP_OUTGOING);
12257 sip_notify_allocate(p);
12258
12259 p->notify->headers = header = ast_variable_new("Subscription-State", "terminated", "");
12260
12261 for (var = vars; var; var = var->next) {
12262 if (!strcasecmp(var->name, "Content")) {
12263 if (ast_str_strlen(p->notify->content))
12264 ast_str_append(&p->notify->content, 0, "\r\n");
12265 ast_str_append(&p->notify->content, 0, "%s", var->value);
12266 } else if (!strcasecmp(var->name, "Content-Length")) {
12267 ast_log(LOG_WARNING, "it is not necessary to specify Content-Length, ignoring");
12268 } else {
12269 header->next = ast_variable_new(var->name, var->value, "");
12270 header = header->next;
12271 }
12272 }
12273
12274 dialog_ref(p, "bump the count of p, which transmit_sip_request will decrement.");
12275 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
12276 transmit_invite(p, SIP_NOTIFY, 0, 2, NULL);
12277
12278 astman_send_ack(s, m, "Notify Sent");
12279 ast_variables_destroy(vars);
12280 return 0;
12281 }
12282
12283
12284
12285 static void update_redirecting(struct sip_pvt *p, const void *data, size_t datalen)
12286 {
12287 struct sip_request resp;
12288
12289 if (p->owner->_state == AST_STATE_UP || ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
12290 return;
12291 }
12292
12293 respprep(&resp, p, "181 Call is being forwarded", &p->initreq);
12294 add_diversion_header(&resp, p);
12295 send_response(p, &resp, XMIT_UNRELIABLE, 0);
12296 }
12297
12298
12299 static void update_connectedline(struct sip_pvt *p, const void *data, size_t datalen)
12300 {
12301
12302 if (!ast_test_flag(&p->flags[0], SIP_SENDRPID)) {
12303 return;
12304 }
12305 if (!p->owner->connected.id.number.valid
12306 || ast_strlen_zero(p->owner->connected.id.number.str)) {
12307 return;
12308 }
12309
12310 append_history(p, "ConnectedLine", "%s party is now %s <%s>",
12311 ast_test_flag(&p->flags[0], SIP_OUTGOING) ? "Calling" : "Called",
12312 S_COR(p->owner->connected.id.name.valid, p->owner->connected.id.name.str, ""),
12313 S_COR(p->owner->connected.id.number.valid, p->owner->connected.id.number.str, ""));
12314
12315 if (p->owner->_state == AST_STATE_UP || ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
12316 struct sip_request req;
12317
12318 if (p->invitestate == INV_CONFIRMED || p->invitestate == INV_TERMINATED) {
12319 reqprep(&req, p, ast_test_flag(&p->flags[0], SIP_REINVITE_UPDATE) ? SIP_UPDATE : SIP_INVITE, 0, 1);
12320
12321 add_header(&req, "Allow", ALLOWED_METHODS);
12322 add_supported_header(p, &req);
12323 add_rpid(&req, p);
12324 add_sdp(&req, p, FALSE, TRUE, FALSE);
12325
12326 initialize_initreq(p, &req);
12327 p->lastinvite = p->ocseq;
12328 ast_set_flag(&p->flags[0], SIP_OUTGOING);
12329 p->invitestate = INV_CALLING;
12330 send_request(p, &req, XMIT_CRITICAL, p->ocseq);
12331 } else if (is_method_allowed(&p->allowed_methods, SIP_UPDATE)) {
12332 reqprep(&req, p, SIP_UPDATE, 0, 1);
12333 add_rpid(&req, p);
12334 add_header(&req, "X-Asterisk-rpid-update", "Yes");
12335 send_request(p, &req, XMIT_CRITICAL, p->ocseq);
12336 } else {
12337
12338 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
12339 }
12340 } else {
12341 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPID_IMMEDIATE)) {
12342 struct sip_request resp;
12343
12344 if ((p->owner->_state == AST_STATE_RING) && !ast_test_flag(&p->flags[0], SIP_PROGRESS_SENT)) {
12345 respprep(&resp, p, "180 Ringing", &p->initreq);
12346 add_rpid(&resp, p);
12347 send_response(p, &resp, XMIT_UNRELIABLE, 0);
12348 ast_set_flag(&p->flags[0], SIP_RINGING);
12349 } else if (p->owner->_state == AST_STATE_RINGING) {
12350 respprep(&resp, p, "183 Session Progress", &p->initreq);
12351 add_rpid(&resp, p);
12352 send_response(p, &resp, XMIT_UNRELIABLE, 0);
12353 ast_set_flag(&p->flags[0], SIP_PROGRESS_SENT);
12354 } else {
12355 ast_debug(1, "Unable able to send update to '%s' in state '%s'\n", p->owner->name, ast_state2str(p->owner->_state));
12356 }
12357 } else {
12358 ast_set_flag(&p->flags[1], SIP_PAGE2_CONNECTLINEUPDATE_PEND);
12359 }
12360 }
12361 }
12362
12363 static const struct _map_x_s regstatestrings[] = {
12364 { REG_STATE_FAILED, "Failed" },
12365 { REG_STATE_UNREGISTERED, "Unregistered"},
12366 { REG_STATE_REGSENT, "Request Sent"},
12367 { REG_STATE_AUTHSENT, "Auth. Sent"},
12368 { REG_STATE_REGISTERED, "Registered"},
12369 { REG_STATE_REJECTED, "Rejected"},
12370 { REG_STATE_TIMEOUT, "Timeout"},
12371 { REG_STATE_NOAUTH, "No Authentication"},
12372 { -1, NULL }
12373 };
12374
12375
12376 static const char *regstate2str(enum sipregistrystate regstate)
12377 {
12378 return map_x_s(regstatestrings, regstate, "Unknown");
12379 }
12380
12381
12382
12383
12384
12385
12386
12387 static int sip_reregister(const void *data)
12388 {
12389
12390 struct sip_registry *r = (struct sip_registry *) data;
12391
12392
12393 if (!r) {
12394 return 0;
12395 }
12396
12397 if (r->call && r->call->do_history) {
12398 append_history(r->call, "RegistryRenew", "Account: %s@%s", r->username, r->hostname);
12399 }
12400
12401
12402 if (sipdebug) {
12403 ast_log(LOG_NOTICE, " -- Re-registration for %s@%s\n", r->username, r->hostname);
12404 }
12405
12406 r->expire = -1;
12407 r->expiry = r->configured_expiry;
12408 __sip_do_register(r);
12409 registry_unref(r, "unref the re-register scheduled event");
12410 return 0;
12411 }
12412
12413
12414
12415
12416 static int __sip_do_register(struct sip_registry *r)
12417 {
12418 int res;
12419
12420 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
12421 return res;
12422 }
12423
12424
12425
12426
12427
12428
12429
12430 static int sip_reg_timeout(const void *data)
12431 {
12432
12433
12434 struct sip_registry *r = (struct sip_registry *)data;
12435 struct sip_pvt *p;
12436 int res;
12437
12438
12439 if (!r) {
12440 return 0;
12441 }
12442
12443 if (r->dnsmgr) {
12444
12445 ast_dnsmgr_refresh(r->dnsmgr);
12446 }
12447
12448
12449
12450
12451
12452 if (r->call) {
12453
12454
12455 p = r->call;
12456 sip_pvt_lock(p);
12457 pvt_set_needdestroy(p, "registration timeout");
12458
12459 __sip_pretend_ack(p);
12460 sip_pvt_unlock(p);
12461
12462
12463
12464 if (p->registry) {
12465 p->registry = registry_unref(p->registry, "p->registry unreffed");
12466 }
12467 r->call = dialog_unref(r->call, "unrefing r->call");
12468 }
12469
12470 r->timeout = -1;
12471 if (global_regattempts_max && r->regattempts >= global_regattempts_max) {
12472
12473
12474
12475 ast_log(LOG_NOTICE, " -- Last Registration Attempt #%d failed, Giving up forever trying to register '%s@%s'\n", r->regattempts, r->username, r->hostname);
12476 r->regstate = REG_STATE_FAILED;
12477 } else {
12478 r->regstate = REG_STATE_UNREGISTERED;
12479 res = transmit_register(r, SIP_REGISTER, NULL, NULL);
12480 ast_log(LOG_NOTICE, " -- Registration for '%s@%s' timed out, trying again (Attempt #%d)\n", r->username, r->hostname, r->regattempts);
12481 }
12482 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));
12483 registry_unref(r, "unreffing registry_unref r");
12484 return 0;
12485 }
12486
12487
12488
12489
12490 static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
12491 {
12492 struct sip_request req;
12493 char from[256];
12494 char to[256];
12495 char tmp[80];
12496 char addr[80];
12497 struct sip_pvt *p;
12498 struct sip_peer *peer = NULL;
12499 int res;
12500 int portno = 0;
12501
12502
12503 if (r == NULL || ((auth == NULL) && (r->regstate == REG_STATE_REGSENT || r->regstate == REG_STATE_AUTHSENT))) {
12504 if (r) {
12505 ast_log(LOG_NOTICE, "Strange, trying to register %s@%s when registration already pending\n", r->username, r->hostname);
12506 }
12507 return 0;
12508 }
12509
12510 if (r->dnsmgr == NULL) {
12511 char transport[MAXHOSTNAMELEN];
12512 peer = find_peer(r->hostname, NULL, TRUE, FINDPEERS, FALSE, 0);
12513 snprintf(transport, sizeof(transport), "_%s._%s",get_srv_service(r->transport), get_srv_protocol(r->transport));
12514 r->us.ss.ss_family = get_address_family_filter(&bindaddr);
12515 ast_dnsmgr_lookup(peer ? peer->tohost : r->hostname, &r->us, &r->dnsmgr, sip_cfg.srvlookup ? transport : NULL);
12516 if (peer) {
12517 peer = unref_peer(peer, "removing peer ref for dnsmgr_lookup");
12518 }
12519 }
12520
12521 if (r->call) {
12522 if (!auth) {
12523 ast_log(LOG_WARNING, "Already have a REGISTER going on to %s@%s?? \n", r->username, r->hostname);
12524 return 0;
12525 } else {
12526 p = dialog_ref(r->call, "getting a copy of the r->call dialog in transmit_register");
12527 make_our_tag(p->tag, sizeof(p->tag));
12528 ast_string_field_set(p, theirtag, NULL);
12529 }
12530 } else {
12531
12532 if (!r->callid_valid) {
12533 build_callid_registry(r, &internip, default_fromdomain);
12534 r->callid_valid = TRUE;
12535 }
12536
12537 if (!(p = sip_alloc( r->callid, NULL, 0, SIP_REGISTER, NULL))) {
12538 ast_log(LOG_WARNING, "Unable to allocate registration transaction (memory or socket error)\n");
12539 return 0;
12540 }
12541
12542 if (p->do_history) {
12543 append_history(p, "RegistryInit", "Account: %s@%s", r->username, r->hostname);
12544 }
12545
12546
12547 if (!ast_sockaddr_port(&r->us) && r->portno) {
12548 ast_sockaddr_set_port(&r->us, r->portno);
12549 }
12550
12551
12552 if (create_addr(p, S_OR(r->peername, r->hostname), &r->us, 0, NULL)) {
12553
12554
12555 dialog_unlink_all(p, TRUE, TRUE);
12556 p = dialog_unref(p, "unref dialog after unlink_all");
12557 if (r->timeout > -1) {
12558 AST_SCHED_REPLACE_UNREF(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r,
12559 registry_unref(_data, "del for REPLACE of registry ptr"),
12560 registry_unref(r, "object ptr dec when SCHED_REPLACE add failed"),
12561 registry_addref(r,"add for REPLACE registry ptr"));
12562 ast_log(LOG_WARNING, "Still have a registration timeout for %s@%s (create_addr() error), %d\n", r->username, r->hostname, r->timeout);
12563 } else {
12564 r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, registry_addref(r, "add for REPLACE registry ptr"));
12565 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);
12566 }
12567 r->regattempts++;
12568 return 0;
12569 }
12570
12571
12572 ast_string_field_set(r, callid, p->callid);
12573
12574 if (!r->dnsmgr && r->portno) {
12575 ast_sockaddr_set_port(&p->sa, r->portno);
12576 ast_sockaddr_set_port(&p->recv, r->portno);
12577 }
12578 if (!ast_strlen_zero(p->fromdomain)) {
12579 portno = (p->fromdomainport) ? p->fromdomainport : STANDARD_SIP_PORT;
12580 } else if (!ast_strlen_zero(r->regdomain)) {
12581 portno = (r->regdomainport) ? r->regdomainport : STANDARD_SIP_PORT;
12582 } else {
12583 portno = ast_sockaddr_port(&p->sa);
12584 }
12585
12586 ast_set_flag(&p->flags[0], SIP_OUTGOING);
12587 r->call = dialog_ref(p, "copying dialog into registry r->call");
12588 p->registry = registry_addref(r, "transmit_register: addref to p->registry in transmit_register");
12589 if (!ast_strlen_zero(r->secret)) {
12590 ast_string_field_set(p, peersecret, r->secret);
12591 }
12592 if (!ast_strlen_zero(r->md5secret))
12593 ast_string_field_set(p, peermd5secret, r->md5secret);
12594
12595
12596 if (!ast_strlen_zero(r->authuser)) {
12597 ast_string_field_set(p, peername, r->authuser);
12598 ast_string_field_set(p, authname, r->authuser);
12599 } else if (!ast_strlen_zero(r->username)) {
12600 ast_string_field_set(p, peername, r->username);
12601 ast_string_field_set(p, authname, r->username);
12602 ast_string_field_set(p, fromuser, r->username);
12603 }
12604 if (!ast_strlen_zero(r->username)) {
12605 ast_string_field_set(p, username, r->username);
12606 }
12607
12608 if (!ast_strlen_zero(r->callback)) {
12609 ast_string_field_set(p, exten, r->callback);
12610 }
12611
12612
12613 set_socket_transport(&p->socket, r->transport);
12614 if (r->transport == SIP_TRANSPORT_TLS || r->transport == SIP_TRANSPORT_TCP) {
12615 p->socket.port =
12616 htons(ast_sockaddr_port(&sip_tcp_desc.local_address));
12617 }
12618
12619
12620
12621
12622
12623
12624 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
12625 build_contact(p);
12626 }
12627
12628
12629 if (auth == NULL) {
12630 if (r->timeout > -1) {
12631 ast_log(LOG_WARNING, "Still have a registration timeout, #%d - deleting it\n", r->timeout);
12632 }
12633 AST_SCHED_REPLACE_UNREF(r->timeout, sched, global_reg_timeout * 1000, sip_reg_timeout, r,
12634 registry_unref(_data,"reg ptr unrefed from del in SCHED_REPLACE"),
12635 registry_unref(r,"reg ptr unrefed from add failure in SCHED_REPLACE"),
12636 registry_addref(r,"reg ptr reffed from add in SCHED_REPLACE"));
12637 ast_debug(1, "Scheduled a registration timeout for %s id #%d \n", r->hostname, r->timeout);
12638 }
12639
12640 snprintf(from, sizeof(from), "<sip:%s@%s>;tag=%s", r->username, S_OR(r->regdomain,p->tohost), p->tag);
12641 if (!ast_strlen_zero(p->theirtag)) {
12642 snprintf(to, sizeof(to), "<sip:%s@%s>;tag=%s", r->username, S_OR(r->regdomain,p->tohost), p->theirtag);
12643 } else {
12644 snprintf(to, sizeof(to), "<sip:%s@%s>", r->username, S_OR(r->regdomain,p->tohost));
12645 }
12646
12647
12648
12649 if (portno && portno != STANDARD_SIP_PORT) {
12650 snprintf(addr, sizeof(addr), "sip:%s:%d", S_OR(p->fromdomain,S_OR(r->regdomain,r->hostname)), portno);
12651 } else {
12652 snprintf(addr, sizeof(addr), "sip:%s", S_OR(p->fromdomain,S_OR(r->regdomain,r->hostname)));
12653 }
12654
12655 ast_string_field_set(p, uri, addr);
12656
12657 p->branch ^= ast_random();
12658
12659 init_req(&req, sipmethod, addr);
12660
12661
12662 snprintf(tmp, sizeof(tmp), "%u %s", ++r->ocseq, sip_methods[sipmethod].text);
12663 p->ocseq = r->ocseq;
12664
12665 build_via(p);
12666 add_header(&req, "Via", p->via);
12667 add_header_max_forwards(p, &req);
12668 add_header(&req, "From", from);
12669 add_header(&req, "To", to);
12670 add_header(&req, "Call-ID", p->callid);
12671 add_header(&req, "CSeq", tmp);
12672 if (!ast_strlen_zero(global_useragent))
12673 add_header(&req, "User-Agent", global_useragent);
12674
12675 if (auth) {
12676 add_header(&req, authheader, auth);
12677 } else if (!ast_strlen_zero(r->nonce)) {
12678 char digest[1024];
12679
12680
12681
12682
12683
12684
12685 if (sipdebug) {
12686 ast_debug(1, " >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
12687 }
12688 ast_string_field_set(p, realm, r->realm);
12689 ast_string_field_set(p, nonce, r->nonce);
12690 ast_string_field_set(p, domain, r->authdomain);
12691 ast_string_field_set(p, opaque, r->opaque);
12692 ast_string_field_set(p, qop, r->qop);
12693 p->noncecount = ++r->noncecount;
12694
12695 memset(digest, 0, sizeof(digest));
12696 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
12697 add_header(&req, "Authorization", digest);
12698 } else {
12699 ast_log(LOG_NOTICE, "No authorization available for authentication of registration to %s@%s\n", r->username, r->hostname);
12700 }
12701 }
12702
12703 snprintf(tmp, sizeof(tmp), "%d", r->expiry);
12704 add_header(&req, "Expires", tmp);
12705 add_header(&req, "Contact", p->our_contact);
12706
12707 initialize_initreq(p, &req);
12708 if (sip_debug_test_pvt(p)) {
12709 ast_verbose("REGISTER %d headers, %d lines\n", p->initreq.headers, p->initreq.lines);
12710 }
12711 r->regstate = auth ? REG_STATE_AUTHSENT : REG_STATE_REGSENT;
12712 r->regattempts++;
12713 ast_debug(4, "REGISTER attempt %d to %s@%s\n", r->regattempts, r->username, r->hostname);
12714 res = send_request(p, &req, XMIT_CRITICAL, p->ocseq);
12715 dialog_unref(p, "p is finished here at the end of transmit_register");
12716 return res;
12717 }
12718
12719
12720 static int transmit_message_with_text(struct sip_pvt *p, const char *text)
12721 {
12722 struct sip_request req;
12723
12724 reqprep(&req, p, SIP_MESSAGE, 0, 1);
12725 add_text(&req, text);
12726 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12727 }
12728
12729
12730 static int sip_refer_allocate(struct sip_pvt *p)
12731 {
12732 p->refer = ast_calloc(1, sizeof(struct sip_refer));
12733 return p->refer ? 1 : 0;
12734 }
12735
12736
12737 static int sip_notify_allocate(struct sip_pvt *p)
12738 {
12739 p->notify = ast_calloc(1, sizeof(struct sip_notify));
12740 if (p->notify) {
12741 p->notify->content = ast_str_create(128);
12742 }
12743 return p->notify ? 1 : 0;
12744 }
12745
12746
12747
12748
12749
12750
12751 static int transmit_refer(struct sip_pvt *p, const char *dest)
12752 {
12753 struct sip_request req = {
12754 .headers = 0,
12755 };
12756 char from[256];
12757 const char *of;
12758 char *c;
12759 char referto[256];
12760 char *ttag, *ftag;
12761 char *theirtag = ast_strdupa(p->theirtag);
12762 int use_tls=FALSE;
12763
12764 if (sipdebug) {
12765 ast_debug(1, "SIP transfer of %s to %s\n", p->callid, dest);
12766 }
12767
12768
12769 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
12770 of = get_header(&p->initreq, "To");
12771 ttag = theirtag;
12772 ftag = p->tag;
12773 } else {
12774 of = get_header(&p->initreq, "From");
12775 ftag = theirtag;
12776 ttag = p->tag;
12777 }
12778
12779 ast_copy_string(from, of, sizeof(from));
12780 of = get_in_brackets(from);
12781 ast_string_field_set(p, from, of);
12782 if (!strncasecmp(of, "sip:", 4)) {
12783 of += 4;
12784 } else if (!strncasecmp(of, "sips:", 5)) {
12785 of += 5;
12786 use_tls = TRUE;
12787 } else {
12788 ast_log(LOG_NOTICE, "From address missing 'sip(s):', assuming sip:\n");
12789 }
12790
12791 if ((c = strchr(dest, '@'))) {
12792 c = NULL;
12793 } else if ((c = strchr(of, '@'))) {
12794 *c++ = '\0';
12795 }
12796 if (c) {
12797 snprintf(referto, sizeof(referto), "<sip%s:%s@%s>", use_tls ? "s" : "", dest, c);
12798 } else {
12799 snprintf(referto, sizeof(referto), "<sip%s:%s>", use_tls ? "s" : "", dest);
12800 }
12801
12802
12803 sip_refer_allocate(p);
12804 ast_copy_string(p->refer->refer_to, referto, sizeof(p->refer->refer_to));
12805 ast_copy_string(p->refer->referred_by, p->our_contact, sizeof(p->refer->referred_by));
12806 p->refer->status = REFER_SENT;
12807
12808 reqprep(&req, p, SIP_REFER, 0, 1);
12809
12810 add_header(&req, "Refer-To", referto);
12811 add_header(&req, "Allow", ALLOWED_METHODS);
12812 add_supported_header(p, &req);
12813 if (!ast_strlen_zero(p->our_contact)) {
12814 add_header(&req, "Referred-By", p->our_contact);
12815 }
12816
12817 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827 }
12828
12829
12830 static int transmit_info_with_aoc(struct sip_pvt *p, struct ast_aoc_decoded *decoded)
12831 {
12832 struct sip_request req;
12833 struct ast_str *str = ast_str_alloca(512);
12834 const struct ast_aoc_unit_entry *unit_entry = ast_aoc_get_unit_info(decoded, 0);
12835 enum ast_aoc_charge_type charging = ast_aoc_get_charge_type(decoded);
12836
12837 reqprep(&req, p, SIP_INFO, 0, 1);
12838
12839 if (ast_aoc_get_msg_type(decoded) == AST_AOC_D) {
12840 ast_str_append(&str, 0, "type=active;");
12841 } else if (ast_aoc_get_msg_type(decoded) == AST_AOC_E) {
12842 ast_str_append(&str, 0, "type=terminated;");
12843 } else {
12844
12845 return -1;
12846 }
12847
12848 switch (charging) {
12849 case AST_AOC_CHARGE_FREE:
12850 ast_str_append(&str, 0, "free-of-charge;");
12851 break;
12852 case AST_AOC_CHARGE_CURRENCY:
12853 ast_str_append(&str, 0, "charging;");
12854 ast_str_append(&str, 0, "charging-info=currency;");
12855 ast_str_append(&str, 0, "amount=%u;", ast_aoc_get_currency_amount(decoded));
12856 ast_str_append(&str, 0, "multiplier=%s;", ast_aoc_get_currency_multiplier_decimal(decoded));
12857 if (!ast_strlen_zero(ast_aoc_get_currency_name(decoded))) {
12858 ast_str_append(&str, 0, "currency=%s;", ast_aoc_get_currency_name(decoded));
12859 }
12860 break;
12861 case AST_AOC_CHARGE_UNIT:
12862 ast_str_append(&str, 0, "charging;");
12863 ast_str_append(&str, 0, "charging-info=pulse;");
12864 if (unit_entry) {
12865 ast_str_append(&str, 0, "recorded-units=%u;", unit_entry->amount);
12866 }
12867 break;
12868 default:
12869 ast_str_append(&str, 0, "not-available;");
12870 };
12871
12872 add_header(&req, "AOC", ast_str_buffer(str));
12873
12874 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12875 }
12876
12877
12878 static int transmit_info_with_digit(struct sip_pvt *p, const char digit, unsigned int duration)
12879 {
12880 struct sip_request req;
12881
12882 reqprep(&req, p, SIP_INFO, 0, 1);
12883 add_digit(&req, digit, duration, (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_SHORTINFO));
12884 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12885 }
12886
12887
12888 static int transmit_info_with_vidupdate(struct sip_pvt *p)
12889 {
12890 struct sip_request req;
12891
12892 reqprep(&req, p, SIP_INFO, 0, 1);
12893 add_vidupdate(&req);
12894 return send_request(p, &req, XMIT_RELIABLE, p->ocseq);
12895 }
12896
12897
12898
12899
12900 static int transmit_request(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
12901 {
12902 struct sip_request resp;
12903
12904 if (sipmethod == SIP_ACK) {
12905 p->invitestate = INV_CONFIRMED;
12906 }
12907
12908 reqprep(&resp, p, sipmethod, seqno, newbranch);
12909 if (sipmethod == SIP_CANCEL && p->answered_elsewhere) {
12910 add_header(&resp, "Reason", "SIP;cause=200;text=\"Call completed elsewhere\"");
12911 }
12912
12913 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
12914 }
12915
12916
12917 static void auth_headers(enum sip_auth_type code, char **header, char **respheader)
12918 {
12919 if (code == WWW_AUTH) {
12920 *header = "WWW-Authenticate";
12921 *respheader = "Authorization";
12922 } else if (code == PROXY_AUTH) {
12923 *header = "Proxy-Authenticate";
12924 *respheader = "Proxy-Authorization";
12925 } else {
12926 ast_verbose("-- wrong response code %d\n", code);
12927 *header = *respheader = "Invalid";
12928 }
12929 }
12930
12931
12932 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int seqno, enum xmittype reliable, int newbranch)
12933 {
12934 struct sip_request resp;
12935
12936 reqprep(&resp, p, sipmethod, seqno, newbranch);
12937 if (!ast_strlen_zero(p->realm)) {
12938 char digest[1024];
12939
12940 memset(digest, 0, sizeof(digest));
12941 if(!build_reply_digest(p, sipmethod, digest, sizeof(digest))) {
12942 char *dummy, *response;
12943 enum sip_auth_type code = p->options ? p->options->auth_type : PROXY_AUTH;
12944 auth_headers(code, &dummy, &response);
12945 add_header(&resp, response, digest);
12946 } else {
12947 ast_log(LOG_WARNING, "No authentication available for call %s\n", p->callid);
12948 }
12949 }
12950
12951
12952 if (sipmethod == SIP_BYE) {
12953 char buf[20];
12954
12955 if (ast_test_flag(&p->flags[1], SIP_PAGE2_Q850_REASON) && p->hangupcause) {
12956 sprintf(buf, "Q.850;cause=%i", p->hangupcause & 0x7f);
12957 add_header(&resp, "Reason", buf);
12958 }
12959
12960 add_header(&resp, "X-Asterisk-HangupCause", ast_cause2str(p->hangupcause));
12961 snprintf(buf, sizeof(buf), "%d", p->hangupcause);
12962 add_header(&resp, "X-Asterisk-HangupCauseCode", buf);
12963 }
12964
12965 return send_request(p, &resp, reliable, seqno ? seqno : p->ocseq);
12966 }
12967
12968
12969 static void destroy_association(struct sip_peer *peer)
12970 {
12971 int realtimeregs = ast_check_realtime("sipregs");
12972 char *tablename = (realtimeregs) ? "sipregs" : "sippeers";
12973
12974 if (!sip_cfg.ignore_regexpire) {
12975 if (peer->rt_fromcontact && sip_cfg.peer_rtupdate) {
12976 ast_update_realtime(tablename, "name", peer->name, "fullcontact", "", "ipaddr", "", "port", "", "regseconds", "0", "regserver", "", "useragent", "", "lastms", "", SENTINEL);
12977 } else {
12978 ast_db_del("SIP/Registry", peer->name);
12979 ast_db_del("SIP/PeerMethods", peer->name);
12980 }
12981 }
12982 }
12983
12984 static void set_socket_transport(struct sip_socket *socket, int transport)
12985 {
12986
12987 if (socket->type != transport) {
12988 socket->fd = -1;
12989 socket->type = transport;
12990 if (socket->tcptls_session) {
12991 ao2_ref(socket->tcptls_session, -1);
12992 socket->tcptls_session = NULL;
12993 }
12994 }
12995 }
12996
12997
12998 static int expire_register(const void *data)
12999 {
13000 struct sip_peer *peer = (struct sip_peer *)data;
13001
13002 if (!peer) {
13003 return 0;
13004 }
13005
13006 peer->expire = -1;
13007 peer->portinuri = 0;
13008
13009 destroy_association(peer);
13010 set_socket_transport(&peer->socket, peer->default_outbound_transport);
13011
13012 if (peer->socket.tcptls_session) {
13013 ao2_ref(peer->socket.tcptls_session, -1);
13014 peer->socket.tcptls_session = NULL;
13015 }
13016
13017 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
13018 register_peer_exten(peer, FALSE);
13019 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
13020
13021
13022
13023
13024 if (peer->is_realtime) {
13025 ast_debug(3, "-REALTIME- peer expired registration. Name: %s. Realtime peer objects now %d\n", peer->name, rpeerobjs);
13026 }
13027
13028 if (peer->selfdestruct ||
13029 ast_test_flag(&peer->flags[1], SIP_PAGE2_RTAUTOCLEAR)) {
13030 unlink_peer_from_tables(peer);
13031 }
13032
13033
13034
13035 memset(&peer->addr, 0, sizeof(peer->addr));
13036
13037 unref_peer(peer, "removing peer ref for expire_register");
13038
13039 return 0;
13040 }
13041
13042
13043 static int sip_poke_peer_s(const void *data)
13044 {
13045 struct sip_peer *peer = (struct sip_peer *)data;
13046 struct sip_peer *foundpeer;
13047
13048 peer->pokeexpire = -1;
13049
13050 foundpeer = ao2_find(peers, peer, OBJ_POINTER);
13051 if (!foundpeer) {
13052 unref_peer(peer, "removing poke peer ref");
13053 return 0;
13054 } else if (foundpeer->name != peer->name) {
13055 unref_peer(foundpeer, "removing above peer ref");
13056 unref_peer(peer, "removing poke peer ref");
13057 return 0;
13058 }
13059
13060 unref_peer(foundpeer, "removing above peer ref");
13061 sip_poke_peer(peer, 0);
13062 unref_peer(peer, "removing poke peer ref");
13063
13064 return 0;
13065 }
13066
13067
13068 static void reg_source_db(struct sip_peer *peer)
13069 {
13070 char data[256];
13071 struct ast_sockaddr sa;
13072 int expire;
13073 char full_addr[128];
13074 AST_DECLARE_APP_ARGS(args,
13075 AST_APP_ARG(addr);
13076 AST_APP_ARG(port);
13077 AST_APP_ARG(expiry_str);
13078 AST_APP_ARG(username);
13079 AST_APP_ARG(contact);
13080 );
13081
13082 if (peer->rt_fromcontact) {
13083 return;
13084 }
13085 if (ast_db_get("SIP/Registry", peer->name, data, sizeof(data))) {
13086 return;
13087 }
13088
13089 AST_NONSTANDARD_RAW_ARGS(args, data, ':');
13090
13091 snprintf(full_addr, sizeof(full_addr), "%s:%s", args.addr, args.port);
13092
13093 if (!ast_sockaddr_parse(&sa, full_addr, 0)) {
13094 return;
13095 }
13096
13097 if (args.expiry_str) {
13098 expire = atoi(args.expiry_str);
13099 } else {
13100 return;
13101 }
13102
13103 if (args.username) {
13104 ast_string_field_set(peer, username, args.username);
13105 }
13106 if (args.contact) {
13107 ast_string_field_set(peer, fullcontact, args.contact);
13108 }
13109
13110 ast_debug(2, "SIP Seeding peer from astdb: '%s' at %s@%s for %d\n",
13111 peer->name, peer->username, ast_sockaddr_stringify_host(&sa), expire);
13112
13113 ast_sockaddr_copy(&peer->addr, &sa);
13114 if (sipsock < 0) {
13115
13116 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, ast_random() % 5000 + 1, sip_poke_peer_s, peer,
13117 unref_peer(_data, "removing poke peer ref"),
13118 unref_peer(peer, "removing poke peer ref"),
13119 ref_peer(peer, "adding poke peer ref"));
13120 } else {
13121 sip_poke_peer(peer, 0);
13122 }
13123 AST_SCHED_REPLACE_UNREF(peer->expire, sched, (expire + 10) * 1000, expire_register, peer,
13124 unref_peer(_data, "remove registration ref"),
13125 unref_peer(peer, "remove registration ref"),
13126 ref_peer(peer, "add registration ref"));
13127 register_peer_exten(peer, TRUE);
13128 }
13129
13130
13131 static int parse_ok_contact(struct sip_pvt *pvt, struct sip_request *req)
13132 {
13133 char contact[SIPBUFSIZE];
13134 char *c;
13135
13136
13137 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
13138 c = get_in_brackets(contact);
13139
13140
13141 ast_string_field_set(pvt, fullcontact, c);
13142
13143
13144 ast_string_field_set(pvt, okcontacturi, c);
13145
13146
13147
13148 return TRUE;
13149 }
13150
13151 static int __set_address_from_contact(const char *fullcontact, struct ast_sockaddr *addr, int tcp)
13152 {
13153 char *domain, *transport;
13154 char contact_buf[256];
13155 char *contact;
13156
13157
13158 ast_copy_string(contact_buf, fullcontact, sizeof(contact_buf));
13159 contact = contact_buf;
13160
13161
13162
13163
13164
13165
13166
13167
13168 if (parse_uri(contact, "sip:,sips:", &contact, NULL, &domain,
13169 &transport)) {
13170 ast_log(LOG_WARNING, "Invalid contact uri %s (missing sip: or sips:), attempting to use anyway\n", fullcontact);
13171 }
13172
13173
13174
13175
13176
13177
13178
13179
13180 if (ast_sockaddr_resolve_first(addr, domain, 0)) {
13181 ast_log(LOG_WARNING, "Invalid host name in Contact: (can't "
13182 "resolve in DNS) : '%s'\n", domain);
13183 return -1;
13184 }
13185
13186
13187 if (!ast_sockaddr_port(addr)) {
13188 ast_sockaddr_set_port(addr,
13189 (get_transport_str2enum(transport) ==
13190 SIP_TRANSPORT_TLS ||
13191 !strncasecmp(fullcontact, "sips", 4)) ?
13192 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
13193 }
13194
13195 return 0;
13196 }
13197
13198
13199 static int set_address_from_contact(struct sip_pvt *pvt)
13200 {
13201 if (ast_test_flag(&pvt->flags[0], SIP_NAT_FORCE_RPORT)) {
13202
13203
13204
13205 pvt->sa = pvt->recv;
13206 return 0;
13207 }
13208
13209 return __set_address_from_contact(pvt->fullcontact, &pvt->sa, pvt->socket.type == SIP_TRANSPORT_TLS ? 1 : 0);
13210 }
13211
13212
13213 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
13214 {
13215 char contact[SIPBUFSIZE];
13216 char data[SIPBUFSIZE];
13217 const char *expires = get_header(req, "Expires");
13218 int expire = atoi(expires);
13219 char *curi, *domain, *transport;
13220 int transport_type;
13221 const char *useragent;
13222 struct ast_sockaddr oldsin, testsa;
13223 char *firstcuri = NULL;
13224 int start = 0;
13225 int wildcard_found = 0;
13226 int single_binding_found = 0;
13227
13228 ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
13229
13230 if (ast_strlen_zero(expires)) {
13231 char *s = strcasestr(contact, ";expires=");
13232 if (s) {
13233 expires = strsep(&s, ";");
13234 if (sscanf(expires + 9, "%30d", &expire) != 1) {
13235 expire = default_expiry;
13236 }
13237 } else {
13238
13239 expire = default_expiry;
13240 }
13241 }
13242
13243 copy_socket_data(&pvt->socket, &req->socket);
13244
13245 do {
13246
13247 curi = contact;
13248 if (strchr(contact, '<') == NULL)
13249 strsep(&curi, ";");
13250 curi = get_in_brackets(contact);
13251 if (!firstcuri) {
13252 firstcuri = ast_strdupa(curi);
13253 }
13254
13255 if (!strcasecmp(curi, "*")) {
13256 wildcard_found = 1;
13257 } else {
13258 single_binding_found = 1;
13259 }
13260
13261 if (wildcard_found && (ast_strlen_zero(expires) || expire != 0 || single_binding_found)) {
13262
13263
13264 return PARSE_REGISTER_FAILED;
13265 }
13266
13267 ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
13268 } while (!ast_strlen_zero(contact));
13269 curi = firstcuri;
13270
13271
13272
13273
13274
13275 if (ast_strlen_zero(curi) && ast_strlen_zero(expires)) {
13276
13277 if (peer->expire > -1 && !ast_strlen_zero(peer->fullcontact)) {
13278 pvt->expiry = ast_sched_when(sched, peer->expire);
13279 }
13280 return PARSE_REGISTER_QUERY;
13281 } else if (!strcasecmp(curi, "*") || !expire) {
13282
13283 memset(&peer->addr, 0, sizeof(peer->addr));
13284 set_socket_transport(&peer->socket, peer->default_outbound_transport);
13285
13286 AST_SCHED_DEL_UNREF(sched, peer->expire,
13287 unref_peer(peer, "remove register expire ref"));
13288
13289 destroy_association(peer);
13290
13291 register_peer_exten(peer, FALSE);
13292 ast_string_field_set(peer, fullcontact, "");
13293 ast_string_field_set(peer, useragent, "");
13294 peer->sipoptions = 0;
13295 peer->lastms = 0;
13296 peer->portinuri = 0;
13297 pvt->expiry = 0;
13298
13299 ast_verb(3, "Unregistered SIP '%s'\n", peer->name);
13300
13301 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unregistered\r\n", peer->name);
13302 return PARSE_REGISTER_UPDATE;
13303 }
13304
13305
13306 ast_string_field_set(peer, fullcontact, curi);
13307
13308
13309 ast_string_field_build(pvt, our_contact, "<%s>", curi);
13310
13311
13312 if (parse_uri(curi, "sip:,sips:", &curi, NULL, &domain, &transport)) {
13313 ast_log(LOG_NOTICE, "Not a valid SIP contact (missing sip:/sips:) trying to use anyway\n");
13314 }
13315
13316
13317 if (!(transport_type = get_transport_str2enum(transport))) {
13318 transport_type = pvt->socket.type;
13319 }
13320
13321
13322
13323
13324 if ((peer->socket.type != transport_type) && (peer->transports & transport_type)) {
13325 set_socket_transport(&peer->socket, transport_type);
13326 }
13327
13328 oldsin = peer->addr;
13329
13330
13331 if (!ast_sockaddr_isnull(&peer->addr)) {
13332 ao2_t_unlink(peers_by_ip, peer, "ao2_unlink of peer from peers_by_ip table");
13333 }
13334
13335 if (!ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) && !ast_test_flag(&peer->flags[0], SIP_NAT_RPORT_PRESENT)) {
13336
13337 ast_debug(1, "Store REGISTER's Contact header for call routing.\n");
13338
13339
13340 if (ast_sockaddr_resolve_first(&testsa, domain, 0)) {
13341 ast_log(LOG_WARNING, "Invalid domain '%s'\n", domain);
13342 ast_string_field_set(peer, fullcontact, "");
13343 ast_string_field_set(pvt, our_contact, "");
13344 return PARSE_REGISTER_FAILED;
13345 }
13346
13347
13348
13349 peer->portinuri = ast_sockaddr_port(&testsa) ? TRUE : FALSE;
13350
13351 if (!ast_sockaddr_port(&testsa)) {
13352 ast_sockaddr_set_port(&testsa,
13353 transport_type == SIP_TRANSPORT_TLS ?
13354 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
13355 }
13356
13357 ast_sockaddr_copy(&peer->addr, &testsa);
13358 } else {
13359
13360
13361 ast_debug(1, "Store REGISTER's src-IP:port for call routing.\n");
13362 peer->addr = pvt->recv;
13363 }
13364
13365
13366 if (ast_apply_ha(sip_cfg.contact_ha, &peer->addr) != AST_SENSE_ALLOW ||
13367 ast_apply_ha(peer->contactha, &peer->addr) != AST_SENSE_ALLOW) {
13368 ast_log(LOG_WARNING, "Domain '%s' disallowed by contact ACL (violating IP %s)\n", domain,
13369 ast_sockaddr_stringify_addr(&testsa));
13370 ast_string_field_set(peer, fullcontact, "");
13371 ast_string_field_set(pvt, our_contact, "");
13372 return PARSE_REGISTER_DENIED;
13373 }
13374
13375
13376
13377
13378 if ((peer->socket.type == pvt->socket.type) &&
13379 !ast_sockaddr_cmp(&peer->addr, &pvt->recv)) {
13380 copy_socket_data(&peer->socket, &pvt->socket);
13381 }
13382
13383
13384 ao2_t_link(peers_by_ip, peer, "ao2_link into peers_by_ip table");
13385
13386
13387 peer->sipoptions = pvt->sipoptions;
13388
13389 if (!ast_strlen_zero(curi) && ast_strlen_zero(peer->username)) {
13390 ast_string_field_set(peer, username, curi);
13391 }
13392
13393 AST_SCHED_DEL_UNREF(sched, peer->expire,
13394 unref_peer(peer, "remove register expire ref"));
13395
13396 if (expire > max_expiry) {
13397 expire = max_expiry;
13398 }
13399 if (expire < min_expiry) {
13400 expire = min_expiry;
13401 }
13402 if (peer->is_realtime && !ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
13403 peer->expire = -1;
13404 } else {
13405 peer->expire = ast_sched_add(sched, (expire + 10) * 1000, expire_register,
13406 ref_peer(peer, "add registration ref"));
13407 if (peer->expire == -1) {
13408 unref_peer(peer, "remote registration ref");
13409 }
13410 }
13411 pvt->expiry = expire;
13412 snprintf(data, sizeof(data), "%s:%d:%s:%s", ast_sockaddr_stringify(&peer->addr),
13413 expire, peer->username, peer->fullcontact);
13414
13415
13416
13417
13418 if (!peer->rt_fromcontact && (peer->socket.type & SIP_TRANSPORT_UDP))
13419 ast_db_put("SIP/Registry", peer->name, data);
13420 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));
13421
13422
13423 if (VERBOSITY_ATLEAST(2) && ast_sockaddr_cmp(&peer->addr, &oldsin)) {
13424 ast_verbose(VERBOSE_PREFIX_3 "Registered SIP '%s' at %s\n", peer->name,
13425 ast_sockaddr_stringify(&peer->addr));
13426 }
13427 sip_poke_peer(peer, 0);
13428 register_peer_exten(peer, 1);
13429
13430
13431 useragent = get_header(req, "User-Agent");
13432 if (strcasecmp(useragent, peer->useragent)) {
13433 ast_string_field_set(peer, useragent, useragent);
13434 ast_verb(4, "Saved useragent \"%s\" for peer %s\n", peer->useragent, peer->name);
13435 }
13436 return PARSE_REGISTER_UPDATE;
13437 }
13438
13439
13440 static void free_old_route(struct sip_route *route)
13441 {
13442 struct sip_route *next;
13443
13444 while (route) {
13445 next = route->next;
13446 ast_free(route);
13447 route = next;
13448 }
13449 }
13450
13451
13452 static void list_route(struct sip_route *route)
13453 {
13454 if (!route) {
13455 ast_verbose("list_route: no route\n");
13456 } else {
13457 for (;route; route = route->next)
13458 ast_verbose("list_route: hop: <%s>\n", route->hop);
13459 }
13460 }
13461
13462
13463 static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
13464 {
13465 struct sip_route *thishop, *head, *tail;
13466 int start = 0;
13467 int len;
13468 const char *rr, *contact, *c;
13469
13470
13471 if (p->route && p->route_persistent) {
13472 ast_debug(1, "build_route: Retaining previous route: <%s>\n", p->route->hop);
13473 return;
13474 }
13475
13476 if (p->route) {
13477 free_old_route(p->route);
13478 p->route = NULL;
13479 }
13480
13481
13482 p->route_persistent = 1;
13483
13484
13485
13486
13487
13488
13489 head = NULL;
13490 tail = head;
13491
13492 for (;;) {
13493
13494 rr = __get_header(req, "Record-Route", &start);
13495 if (*rr == '\0') {
13496 break;
13497 }
13498 for (; (rr = strchr(rr, '<')) ; rr += len) {
13499 ++rr;
13500 len = strcspn(rr, ">") + 1;
13501
13502 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
13503
13504 ast_copy_string(thishop->hop, rr, len);
13505 ast_debug(2, "build_route: Record-Route hop: <%s>\n", thishop->hop);
13506
13507 if (backwards) {
13508
13509 thishop->next = head;
13510 head = thishop;
13511
13512 if (!tail) {
13513 tail = thishop;
13514 }
13515 } else {
13516 thishop->next = NULL;
13517
13518 if (tail) {
13519 tail->next = thishop;
13520 } else {
13521 head = thishop;
13522 }
13523 tail = thishop;
13524 }
13525 }
13526 }
13527 }
13528
13529
13530 if (!head || (!ast_strlen_zero(head->hop) && strstr(head->hop, ";lr") == NULL) ) {
13531
13532
13533 contact = get_header(req, "Contact");
13534 if (!ast_strlen_zero(contact)) {
13535 ast_debug(2, "build_route: Contact hop: %s\n", contact);
13536
13537 c = strchr(contact, '<');
13538 if (c) {
13539
13540 ++c;
13541 len = strcspn(c, ">") + 1;
13542 } else {
13543
13544 c = contact;
13545 len = strlen(contact) + 1;
13546 }
13547 if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
13548
13549 ast_copy_string(thishop->hop, c, len);
13550 thishop->next = NULL;
13551
13552 if (tail) {
13553 tail->next = thishop;
13554 } else {
13555 head = thishop;
13556 }
13557 }
13558 }
13559 }
13560
13561
13562 p->route = head;
13563
13564
13565 if (sip_debug_test_pvt(p)) {
13566 list_route(p->route);
13567 }
13568 }
13569
13570
13571
13572
13573
13574
13575
13576 static void set_nonce_randdata(struct sip_pvt *p, int forceupdate)
13577 {
13578 if (p->stalenonce || forceupdate || ast_strlen_zero(p->randdata)) {
13579 ast_string_field_build(p, randdata, "%08lx", ast_random());
13580 p->stalenonce = 0;
13581 }
13582 }
13583
13584 AST_THREADSTORAGE(check_auth_buf);
13585 #define CHECK_AUTH_BUF_INITLEN 256
13586
13587
13588
13589
13590
13591
13592 static enum check_auth_result check_auth(struct sip_pvt *p, struct sip_request *req, const char *username,
13593 const char *secret, const char *md5secret, int sipmethod,
13594 const char *uri, enum xmittype reliable, int ignore)
13595 {
13596 const char *response;
13597 char *reqheader, *respheader;
13598 const char *authtoken;
13599 char a1_hash[256];
13600 char resp_hash[256]="";
13601 char *c;
13602 int wrongnonce = FALSE;
13603 int good_response;
13604 const char *usednonce = p->randdata;
13605 struct ast_str *buf;
13606 int res;
13607
13608
13609 enum keys { K_RESP, K_URI, K_USER, K_NONCE, K_LAST };
13610 struct x {
13611 const char *key;
13612 const char *s;
13613 } *i, keys[] = {
13614 [K_RESP] = { "response=", "" },
13615 [K_URI] = { "uri=", "" },
13616 [K_USER] = { "username=", "" },
13617 [K_NONCE] = { "nonce=", "" },
13618 [K_LAST] = { NULL, NULL}
13619 };
13620
13621
13622 if (ast_strlen_zero(secret) && ast_strlen_zero(md5secret))
13623 return AUTH_SUCCESSFUL;
13624
13625
13626
13627 response = "401 Unauthorized";
13628
13629
13630
13631
13632
13633 auth_headers(WWW_AUTH, &respheader, &reqheader);
13634
13635 authtoken = get_header(req, reqheader);
13636 if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
13637
13638
13639 if (!reliable) {
13640
13641
13642 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
13643
13644 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13645 }
13646 return AUTH_CHALLENGE_SENT;
13647 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
13648
13649 set_nonce_randdata(p, 1);
13650 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
13651
13652 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13653 return AUTH_CHALLENGE_SENT;
13654 }
13655
13656
13657
13658
13659
13660
13661 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
13662 return AUTH_SECRET_FAILED;
13663 }
13664
13665
13666 res = ast_str_set(&buf, 0, "%s", authtoken);
13667
13668 if (res == AST_DYNSTR_BUILD_FAILED) {
13669 return AUTH_SECRET_FAILED;
13670 }
13671
13672 c = buf->str;
13673
13674 while(c && *(c = ast_skip_blanks(c)) ) {
13675 for (i = keys; i->key != NULL; i++) {
13676 const char *separator = ",";
13677
13678 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
13679 continue;
13680 }
13681
13682 c += strlen(i->key);
13683 if (*c == '"') {
13684 c++;
13685 separator = "\"";
13686 }
13687 i->s = c;
13688 strsep(&c, separator);
13689 break;
13690 }
13691 if (i->key == NULL) {
13692 strsep(&c, " ,");
13693 }
13694 }
13695
13696
13697 if (strcmp(username, keys[K_USER].s)) {
13698 ast_log(LOG_WARNING, "username mismatch, have <%s>, digest has <%s>\n",
13699 username, keys[K_USER].s);
13700
13701 return AUTH_USERNAME_MISMATCH;
13702 }
13703
13704
13705
13706 if (strcasecmp(p->randdata, keys[K_NONCE].s) || p->stalenonce) {
13707 wrongnonce = TRUE;
13708 usednonce = keys[K_NONCE].s;
13709 } else {
13710 p->stalenonce = 1;
13711 }
13712
13713 if (!ast_strlen_zero(md5secret)) {
13714 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
13715 } else {
13716 char a1[256];
13717
13718 snprintf(a1, sizeof(a1), "%s:%s:%s", username, p->realm, secret);
13719 ast_md5_hash(a1_hash, a1);
13720 }
13721
13722
13723 {
13724 char a2[256];
13725 char a2_hash[256];
13726 char resp[256];
13727
13728 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[sipmethod].text,
13729 S_OR(keys[K_URI].s, uri));
13730 ast_md5_hash(a2_hash, a2);
13731 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, usednonce, a2_hash);
13732 ast_md5_hash(resp_hash, resp);
13733 }
13734
13735 good_response = keys[K_RESP].s &&
13736 !strncasecmp(keys[K_RESP].s, resp_hash, strlen(resp_hash));
13737 if (wrongnonce) {
13738 if (good_response) {
13739 if (sipdebug)
13740 ast_log(LOG_NOTICE, "Correct auth, but based on stale nonce received from '%s'\n", get_header(req, "From"));
13741
13742 set_nonce_randdata(p, 0);
13743 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, TRUE);
13744 } else {
13745
13746 if (!req->ignore) {
13747 if (sipdebug) {
13748 ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
13749 }
13750 set_nonce_randdata(p, 1);
13751 } else {
13752 if (sipdebug) {
13753 ast_log(LOG_NOTICE, "Duplicate authentication received from '%s'\n", get_header(req, "To"));
13754 }
13755 }
13756 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
13757 }
13758
13759
13760 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13761 return AUTH_CHALLENGE_SENT;
13762 }
13763 if (good_response) {
13764 append_history(p, "AuthOK", "Auth challenge successful for %s", username);
13765 return AUTH_SUCCESSFUL;
13766 }
13767
13768
13769
13770
13771
13772
13773 return AUTH_SECRET_FAILED;
13774 }
13775
13776
13777 static void sip_peer_hold(struct sip_pvt *p, int hold)
13778 {
13779 if (!p->relatedpeer) {
13780 return;
13781 }
13782
13783
13784 ast_atomic_fetchadd_int(&p->relatedpeer->onHold, (hold ? +1 : -1));
13785
13786
13787 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", p->relatedpeer->name);
13788
13789 return;
13790 }
13791
13792
13793 static void mwi_event_cb(const struct ast_event *event, void *userdata)
13794 {
13795 struct sip_peer *peer = userdata;
13796
13797 ao2_lock(peer);
13798 sip_send_mwi_to_peer(peer, event, 0);
13799 ao2_unlock(peer);
13800 }
13801
13802 static void network_change_event_subscribe(void)
13803 {
13804 if (!network_change_event_subscription) {
13805 network_change_event_subscription = ast_event_subscribe(AST_EVENT_NETWORK_CHANGE,
13806 network_change_event_cb,
13807 "SIP Network Change ",
13808 NULL, AST_EVENT_IE_END);
13809 }
13810 }
13811
13812 static void network_change_event_unsubscribe(void)
13813 {
13814 if (network_change_event_subscription) {
13815 network_change_event_subscription = ast_event_unsubscribe(network_change_event_subscription);
13816 }
13817 }
13818
13819 static int network_change_event_sched_cb(const void *data)
13820 {
13821 network_change_event_sched_id = -1;
13822 sip_send_all_registers();
13823 sip_send_all_mwi_subscriptions();
13824 return 0;
13825 }
13826
13827 static void network_change_event_cb(const struct ast_event *event, void *userdata)
13828 {
13829 ast_debug(1, "SIP, got a network change event, renewing all SIP registrations.\n");
13830 if (network_change_event_sched_id == -1) {
13831 network_change_event_sched_id = ast_sched_add(sched, 1000, network_change_event_sched_cb, NULL);
13832 }
13833 }
13834
13835
13836
13837
13838 static int cb_extensionstate(char *context, char* exten, int state, void *data)
13839 {
13840 struct sip_pvt *p = data;
13841
13842 sip_pvt_lock(p);
13843
13844 switch(state) {
13845 case AST_EXTENSION_DEACTIVATED:
13846 case AST_EXTENSION_REMOVED:
13847 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13848 ast_verb(2, "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
13849 p->stateid = -1;
13850 p->subscribed = NONE;
13851 append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
13852 break;
13853 default:
13854 p->laststate = state;
13855 break;
13856 }
13857 if (p->subscribed != NONE) {
13858 if (!p->pendinginvite) {
13859 transmit_state_notify(p, state, 1, FALSE);
13860 } else {
13861
13862
13863 ast_set_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
13864 }
13865 }
13866 ast_verb(2, "Extension Changed %s[%s] new state %s for Notify User %s %s\n", exten, context, ast_extension_state2str(state), p->username,
13867 ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE) ? "(queued)" : "");
13868
13869 sip_pvt_unlock(p);
13870
13871 return 0;
13872 }
13873
13874
13875
13876
13877 static void transmit_fake_auth_response(struct sip_pvt *p, int sipmethod, struct sip_request *req, enum xmittype reliable)
13878 {
13879
13880
13881 const char *response = "407 Proxy Authentication Required";
13882 const char *reqheader = "Proxy-Authorization";
13883 const char *respheader = "Proxy-Authenticate";
13884 const char *authtoken;
13885 struct ast_str *buf;
13886 char *c;
13887
13888
13889 enum keys { K_NONCE, K_LAST };
13890 struct x {
13891 const char *key;
13892 const char *s;
13893 } *i, keys[] = {
13894 [K_NONCE] = { "nonce=", "" },
13895 [K_LAST] = { NULL, NULL}
13896 };
13897
13898 if (sipmethod == SIP_REGISTER || sipmethod == SIP_SUBSCRIBE) {
13899 response = "401 Unauthorized";
13900 reqheader = "Authorization";
13901 respheader = "WWW-Authenticate";
13902 }
13903 authtoken = get_header(req, reqheader);
13904 if (req->ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
13905
13906
13907 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
13908
13909 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13910 return;
13911 } else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
13912
13913 set_nonce_randdata(p, 1);
13914 transmit_response_with_auth(p, response, req, p->randdata, 0, respheader, 0);
13915
13916 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13917 return;
13918 }
13919
13920 if (!(buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN))) {
13921 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
13922 return;
13923 }
13924
13925
13926 if (ast_str_set(&buf, 0, "%s", authtoken) == AST_DYNSTR_BUILD_FAILED) {
13927 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
13928 return;
13929 }
13930
13931 c = buf->str;
13932
13933 while (c && *(c = ast_skip_blanks(c))) {
13934 for (i = keys; i->key != NULL; i++) {
13935 const char *separator = ",";
13936
13937 if (strncasecmp(c, i->key, strlen(i->key)) != 0) {
13938 continue;
13939 }
13940
13941 c += strlen(i->key);
13942 if (*c == '"') {
13943 c++;
13944 separator = "\"";
13945 }
13946 i->s = c;
13947 strsep(&c, separator);
13948 break;
13949 }
13950 if (i->key == NULL) {
13951 strsep(&c, " ,");
13952 }
13953 }
13954
13955
13956 if (strcasecmp(p->randdata, keys[K_NONCE].s)) {
13957 if (!req->ignore) {
13958 set_nonce_randdata(p, 1);
13959 }
13960 transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, FALSE);
13961
13962
13963 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
13964 } else {
13965 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
13966 }
13967 }
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983 static char *terminate_uri(char *uri)
13984 {
13985 char *t = uri;
13986 while (*t && *t > ' ' && *t != ';') {
13987 t++;
13988 }
13989 *t = '\0';
13990 return uri;
13991 }
13992
13993
13994
13995
13996
13997
13998 static enum check_auth_result register_verify(struct sip_pvt *p, struct ast_sockaddr *addr,
13999 struct sip_request *req, const char *uri)
14000 {
14001 enum check_auth_result res = AUTH_NOT_FOUND;
14002 struct sip_peer *peer;
14003 char tmp[256];
14004 char *name = NULL, *c, *domain = NULL, *dummy = NULL;
14005 char *uri2 = ast_strdupa(uri);
14006
14007 terminate_uri(uri2);
14008
14009 ast_copy_string(tmp, get_header(req, "To"), sizeof(tmp));
14010
14011 c = get_in_brackets(tmp);
14012 c = remove_uri_parameters(c);
14013
14014 if (parse_uri(c, "sip:,sips:", &name, &dummy, &domain, NULL)) {
14015 ast_log(LOG_NOTICE, "Invalid to address: '%s' from %s (missing sip:) trying to use anyway...\n", c, ast_sockaddr_stringify_addr(addr));
14016 return -1;
14017 }
14018
14019 SIP_PEDANTIC_DECODE(name);
14020 SIP_PEDANTIC_DECODE(domain);
14021
14022
14023
14024
14025 if (!ast_strlen_zero(domain) && !AST_LIST_EMPTY(&domain_list)) {
14026 if (!check_sip_domain(domain, NULL, 0)) {
14027 transmit_response(p, "404 Not found (unknown domain)", &p->initreq);
14028 return AUTH_UNKNOWN_DOMAIN;
14029 }
14030 }
14031
14032 ast_string_field_set(p, exten, name);
14033 build_contact(p);
14034 if (req->ignore) {
14035
14036 const char *expires = get_header(req, "Expires");
14037 int expire = atoi(expires);
14038
14039 if (ast_strlen_zero(expires)) {
14040 if ((expires = strcasestr(get_header(req, "Contact"), ";expires="))) {
14041 expire = atoi(expires + 9);
14042 }
14043 }
14044 if (!ast_strlen_zero(expires) && expire == 0) {
14045 transmit_response_with_date(p, "200 OK", req);
14046 return 0;
14047 }
14048 }
14049 peer = find_peer(name, NULL, TRUE, FINDPEERS, FALSE, 0);
14050
14051 if (!(peer && ast_apply_ha(peer->ha, addr))) {
14052
14053 if (peer) {
14054 unref_peer(peer, "register_verify: unref_peer: from find_peer operation");
14055 peer = NULL;
14056 res = AUTH_ACL_FAILED;
14057 } else {
14058 res = AUTH_NOT_FOUND;
14059 }
14060 }
14061
14062 if (peer) {
14063 ao2_lock(peer);
14064 if (!peer->host_dynamic) {
14065 ast_log(LOG_ERROR, "Peer '%s' is trying to register, but not configured as host=dynamic\n", peer->name);
14066 res = AUTH_PEER_NOT_DYNAMIC;
14067 } else {
14068 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_NAT_FORCE_RPORT);
14069 if (ast_test_flag(&p->flags[1], SIP_PAGE2_REGISTERTRYING))
14070 transmit_response(p, "100 Trying", req);
14071 if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri2, XMIT_UNRELIABLE, req->ignore))) {
14072 if (sip_cancel_destroy(p))
14073 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
14074
14075 if (check_request_transport(peer, req)) {
14076 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
14077 transmit_response_with_date(p, "403 Forbidden", req);
14078 res = AUTH_BAD_TRANSPORT;
14079 } else {
14080
14081
14082
14083 switch (parse_register_contact(p, peer, req)) {
14084 case PARSE_REGISTER_DENIED:
14085 ast_log(LOG_WARNING, "Registration denied because of contact ACL\n");
14086 transmit_response_with_date(p, "603 Denied", req);
14087 peer->lastmsgssent = -1;
14088 res = 0;
14089 break;
14090 case PARSE_REGISTER_FAILED:
14091 ast_log(LOG_WARNING, "Failed to parse contact info\n");
14092 transmit_response_with_date(p, "400 Bad Request", req);
14093 peer->lastmsgssent = -1;
14094 res = 0;
14095 break;
14096 case PARSE_REGISTER_QUERY:
14097 ast_string_field_set(p, fullcontact, peer->fullcontact);
14098 transmit_response_with_date(p, "200 OK", req);
14099 peer->lastmsgssent = -1;
14100 res = 0;
14101 break;
14102 case PARSE_REGISTER_UPDATE:
14103 ast_string_field_set(p, fullcontact, peer->fullcontact);
14104 update_peer(peer, p->expiry);
14105
14106 transmit_response_with_date(p, "200 OK", req);
14107 if (!ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY))
14108 peer->lastmsgssent = -1;
14109 res = 0;
14110 break;
14111 }
14112 }
14113
14114 }
14115 }
14116 ao2_unlock(peer);
14117 }
14118 if (!peer && sip_cfg.autocreatepeer) {
14119
14120 peer = temp_peer(name);
14121 if (peer) {
14122 ao2_t_link(peers, peer, "link peer into peer table");
14123 if (!ast_sockaddr_isnull(&peer->addr)) {
14124 ao2_t_link(peers_by_ip, peer, "link peer into peers-by-ip table");
14125 }
14126 ao2_lock(peer);
14127 if (sip_cancel_destroy(p))
14128 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
14129 switch (parse_register_contact(p, peer, req)) {
14130 case PARSE_REGISTER_DENIED:
14131 ast_log(LOG_WARNING, "Registration denied because of contact ACL\n");
14132 transmit_response_with_date(p, "403 Forbidden (ACL)", req);
14133 peer->lastmsgssent = -1;
14134 res = 0;
14135 break;
14136 case PARSE_REGISTER_FAILED:
14137 ast_log(LOG_WARNING, "Failed to parse contact info\n");
14138 transmit_response_with_date(p, "400 Bad Request", req);
14139 peer->lastmsgssent = -1;
14140 res = 0;
14141 break;
14142 case PARSE_REGISTER_QUERY:
14143 ast_string_field_set(p, fullcontact, peer->fullcontact);
14144 transmit_response_with_date(p, "200 OK", req);
14145 peer->lastmsgssent = -1;
14146 res = 0;
14147 break;
14148 case PARSE_REGISTER_UPDATE:
14149 ast_string_field_set(p, fullcontact, peer->fullcontact);
14150
14151 transmit_response_with_date(p, "200 OK", req);
14152 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));
14153 peer->lastmsgssent = -1;
14154 res = 0;
14155 break;
14156 }
14157 ao2_unlock(peer);
14158 }
14159 }
14160 if (!peer && sip_cfg.alwaysauthreject) {
14161
14162
14163
14164 transmit_response(p, "100 Trying", req);
14165
14166 sched_yield();
14167 }
14168 if (!res) {
14169 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
14170 }
14171 if (res < 0) {
14172 switch (res) {
14173 case AUTH_SECRET_FAILED:
14174
14175 transmit_response(p, "403 Forbidden (Bad auth)", &p->initreq);
14176 if (global_authfailureevents) {
14177 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
14178 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
14179 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
14180 "ChannelType: SIP\r\n"
14181 "Peer: SIP/%s\r\n"
14182 "PeerStatus: Rejected\r\n"
14183 "Cause: AUTH_SECRET_FAILED\r\n"
14184 "Address: %s\r\n"
14185 "Port: %s\r\n",
14186 name, peer_addr, peer_port);
14187 }
14188 break;
14189 case AUTH_USERNAME_MISMATCH:
14190
14191
14192
14193
14194 case AUTH_NOT_FOUND:
14195 case AUTH_PEER_NOT_DYNAMIC:
14196 case AUTH_ACL_FAILED:
14197 if (sip_cfg.alwaysauthreject) {
14198 transmit_fake_auth_response(p, SIP_REGISTER, &p->initreq, XMIT_UNRELIABLE);
14199 if (global_authfailureevents) {
14200 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
14201 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
14202 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
14203 "ChannelType: SIP\r\n"
14204 "Peer: SIP/%s\r\n"
14205 "PeerStatus: Rejected\r\n"
14206 "Cause: %s\r\n"
14207 "Address: %s\r\n"
14208 "Port: %s\r\n",
14209 name,
14210 res == AUTH_PEER_NOT_DYNAMIC ? "AUTH_PEER_NOT_DYNAMIC" : "URI_NOT_FOUND",
14211 peer_addr, peer_port);
14212 }
14213 } else {
14214
14215 if (res == AUTH_PEER_NOT_DYNAMIC) {
14216 transmit_response(p, "403 Forbidden", &p->initreq);
14217 if (global_authfailureevents) {
14218 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
14219 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
14220 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
14221 "ChannelType: SIP\r\n"
14222 "Peer: SIP/%s\r\n"
14223 "PeerStatus: Rejected\r\n"
14224 "Cause: AUTH_PEER_NOT_DYNAMIC\r\n"
14225 "Address: %s\r\n"
14226 "Port: %s\r\n",
14227 name, peer_addr, peer_port);
14228 }
14229 } else {
14230 transmit_response(p, "404 Not found", &p->initreq);
14231 if (global_authfailureevents) {
14232 const char *peer_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
14233 const char *peer_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
14234 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
14235 "ChannelType: SIP\r\n"
14236 "Peer: SIP/%s\r\n"
14237 "PeerStatus: Rejected\r\n"
14238 "Cause: %s\r\n"
14239 "Address: %s\r\n"
14240 "Port: %s\r\n",
14241 name,
14242 (res == AUTH_USERNAME_MISMATCH) ? "AUTH_USERNAME_MISMATCH" : "URI_NOT_FOUND",
14243 peer_addr, peer_port);
14244 }
14245 }
14246 }
14247 break;
14248 case AUTH_BAD_TRANSPORT:
14249 default:
14250 break;
14251 }
14252 }
14253 if (peer) {
14254 unref_peer(peer, "register_verify: unref_peer: tossing stack peer pointer at end of func");
14255 }
14256
14257 return res;
14258 }
14259
14260
14261 static void sip_set_redirstr(struct sip_pvt *p, char *reason) {
14262
14263 if (!strcmp(reason, "unknown")) {
14264 ast_string_field_set(p, redircause, "UNKNOWN");
14265 } else if (!strcmp(reason, "user-busy")) {
14266 ast_string_field_set(p, redircause, "BUSY");
14267 } else if (!strcmp(reason, "no-answer")) {
14268 ast_string_field_set(p, redircause, "NOANSWER");
14269 } else if (!strcmp(reason, "unavailable")) {
14270 ast_string_field_set(p, redircause, "UNREACHABLE");
14271 } else if (!strcmp(reason, "unconditional")) {
14272 ast_string_field_set(p, redircause, "UNCONDITIONAL");
14273 } else if (!strcmp(reason, "time-of-day")) {
14274 ast_string_field_set(p, redircause, "UNKNOWN");
14275 } else if (!strcmp(reason, "do-not-disturb")) {
14276 ast_string_field_set(p, redircause, "UNKNOWN");
14277 } else if (!strcmp(reason, "deflection")) {
14278 ast_string_field_set(p, redircause, "UNKNOWN");
14279 } else if (!strcmp(reason, "follow-me")) {
14280 ast_string_field_set(p, redircause, "UNKNOWN");
14281 } else if (!strcmp(reason, "out-of-service")) {
14282 ast_string_field_set(p, redircause, "UNREACHABLE");
14283 } else if (!strcmp(reason, "away")) {
14284 ast_string_field_set(p, redircause, "UNREACHABLE");
14285 } else {
14286 ast_string_field_set(p, redircause, "UNKNOWN");
14287 }
14288 }
14289
14290
14291
14292
14293
14294 static int get_pai(struct sip_pvt *p, struct sip_request *req)
14295 {
14296 char pai[256];
14297 char privacy[64];
14298 char *cid_num = "";
14299 char *cid_name = "";
14300 int callingpres = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
14301 char *start = NULL, *end = NULL, *uri = NULL;
14302
14303 ast_copy_string(pai, get_header(req, "P-Asserted-Identity"), sizeof(pai));
14304
14305 if (ast_strlen_zero(pai)) {
14306 return 0;
14307 }
14308
14309 start = pai;
14310 if (*start == '"') {
14311 *start++ = '\0';
14312 end = strchr(start, '"');
14313 if (!end) {
14314 return 0;
14315 }
14316 *end++ = '\0';
14317 cid_name = start;
14318 start = ast_skip_blanks(end);
14319 }
14320
14321 if (*start != '<')
14322 return 0;
14323
14324
14325
14326
14327 uri = ast_strdupa(start);
14328 *start++ = '\0';
14329 end = strchr(start, '@');
14330 if (!end) {
14331 return 0;
14332 }
14333 *end++ = '\0';
14334 if (!strncasecmp(uri, "sip:anonymous@anonymous.invalid", 31)) {
14335 callingpres = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
14336
14337
14338
14339 cid_num = (char *)p->cid_num;
14340 } else if (!strncasecmp(start, "sip:", 4)) {
14341 cid_num = start + 4;
14342 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(cid_num))
14343 ast_shrink_phone_number(cid_num);
14344 start = end;
14345
14346 end = strchr(start, '>');
14347 if (!end) {
14348 return 0;
14349 }
14350 *end = '\0';
14351 } else {
14352 return 0;
14353 }
14354
14355 ast_copy_string(privacy, get_header(req, "Privacy"), sizeof(privacy));
14356 if (!ast_strlen_zero(privacy) && strncmp(privacy, "id", 2)) {
14357 callingpres = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
14358 }
14359
14360
14361 if (!strcasecmp(p->cid_num, cid_num) && !strcasecmp(p->cid_name, cid_name) && p->callingpres == callingpres) {
14362 return 0;
14363 }
14364
14365 ast_string_field_set(p, cid_num, cid_num);
14366 ast_string_field_set(p, cid_name, cid_name);
14367 p->callingpres = callingpres;
14368
14369 if (p->owner) {
14370 ast_set_callerid(p->owner, cid_num, cid_name, NULL);
14371 p->owner->caller.id.name.presentation = callingpres;
14372 p->owner->caller.id.number.presentation = callingpres;
14373 }
14374
14375 return 1;
14376 }
14377
14378
14379
14380
14381
14382 static int get_rpid(struct sip_pvt *p, struct sip_request *oreq)
14383 {
14384 char tmp[256];
14385 struct sip_request *req;
14386 char *cid_num = "";
14387 char *cid_name = "";
14388 int callingpres = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
14389 char *privacy = "";
14390 char *screen = "";
14391 char *start, *end;
14392
14393 if (!ast_test_flag(&p->flags[0], SIP_TRUSTRPID))
14394 return 0;
14395 req = oreq;
14396 if (!req)
14397 req = &p->initreq;
14398 ast_copy_string(tmp, get_header(req, "Remote-Party-ID"), sizeof(tmp));
14399 if (ast_strlen_zero(tmp)) {
14400 return get_pai(p, req);
14401 }
14402
14403 start = tmp;
14404 if (*start == '"') {
14405 *start++ = '\0';
14406 end = strchr(start, '"');
14407 if (!end)
14408 return 0;
14409 *end++ = '\0';
14410 cid_name = start;
14411 start = ast_skip_blanks(end);
14412 }
14413
14414 if (*start != '<')
14415 return 0;
14416 *start++ = '\0';
14417 end = strchr(start, '@');
14418 if (!end)
14419 return 0;
14420 *end++ = '\0';
14421 if (strncasecmp(start, "sip:", 4))
14422 return 0;
14423 cid_num = start + 4;
14424 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(cid_num))
14425 ast_shrink_phone_number(cid_num);
14426 start = end;
14427
14428 end = strchr(start, '>');
14429 if (!end)
14430 return 0;
14431 *end++ = '\0';
14432 if (*end) {
14433 start = end;
14434 if (*start != ';')
14435 return 0;
14436 *start++ = '\0';
14437 while (!ast_strlen_zero(start)) {
14438 end = strchr(start, ';');
14439 if (end)
14440 *end++ = '\0';
14441 if (!strncasecmp(start, "privacy=", 8))
14442 privacy = start + 8;
14443 else if (!strncasecmp(start, "screen=", 7))
14444 screen = start + 7;
14445 start = end;
14446 }
14447
14448 if (!strcasecmp(privacy, "full")) {
14449 if (!strcasecmp(screen, "yes"))
14450 callingpres = AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
14451 else if (!strcasecmp(screen, "no"))
14452 callingpres = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
14453 } else {
14454 if (!strcasecmp(screen, "yes"))
14455 callingpres = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
14456 else if (!strcasecmp(screen, "no"))
14457 callingpres = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
14458 }
14459 }
14460
14461
14462 if (!strcasecmp(p->cid_num, cid_num) && !strcasecmp(p->cid_name, cid_name) && p->callingpres == callingpres)
14463 return 0;
14464
14465 ast_string_field_set(p, cid_num, cid_num);
14466 ast_string_field_set(p, cid_name, cid_name);
14467 p->callingpres = callingpres;
14468
14469 if (p->owner) {
14470 ast_set_callerid(p->owner, cid_num, cid_name, NULL);
14471 p->owner->caller.id.name.presentation = callingpres;
14472 p->owner->caller.id.number.presentation = callingpres;
14473 }
14474
14475 return 1;
14476 }
14477
14478
14479 static int get_rdnis(struct sip_pvt *p, struct sip_request *oreq, char **name, char **number, int *reason)
14480 {
14481 char tmp[256], *exten, *rexten, *rdomain, *rname = NULL;
14482 char *params, *reason_param = NULL;
14483 struct sip_request *req;
14484
14485 req = oreq ? oreq : &p->initreq;
14486
14487 ast_copy_string(tmp, get_header(req, "Diversion"), sizeof(tmp));
14488 if (ast_strlen_zero(tmp))
14489 return -1;
14490
14491 if ((params = strchr(tmp, '>'))) {
14492 params = strchr(params, ';');
14493 }
14494
14495 exten = get_in_brackets(tmp);
14496 if (!strncasecmp(exten, "sip:", 4)) {
14497 exten += 4;
14498 } else if (!strncasecmp(exten, "sips:", 5)) {
14499 exten += 5;
14500 } else {
14501 ast_log(LOG_WARNING, "Huh? Not an RDNIS SIP header (%s)?\n", exten);
14502 return -1;
14503 }
14504
14505
14506 if (params) {
14507 *params = '\0';
14508 params++;
14509 while (*params == ';' || *params == ' ')
14510 params++;
14511
14512 if ((reason_param = strcasestr(params, "reason="))) {
14513 char *end;
14514 reason_param+=7;
14515 if ((end = strchr(reason_param, ';'))) {
14516 *end = '\0';
14517 }
14518
14519 if (*reason_param == '"')
14520 ast_strip_quoted(reason_param, "\"", "\"");
14521 if (!ast_strlen_zero(reason_param)) {
14522 sip_set_redirstr(p, reason_param);
14523 if (p->owner) {
14524 pbx_builtin_setvar_helper(p->owner, "__PRIREDIRECTREASON", p->redircause);
14525 pbx_builtin_setvar_helper(p->owner, "__SIPREDIRECTREASON", reason_param);
14526 }
14527 }
14528 }
14529 }
14530
14531 rdomain = exten;
14532 rexten = strsep(&rdomain, "@");
14533 if (p->owner)
14534 pbx_builtin_setvar_helper(p->owner, "__SIPRDNISDOMAIN", rdomain);
14535
14536 if (sip_debug_test_pvt(p))
14537 ast_verbose("RDNIS for this call is %s (reason %s)\n", exten, reason ? reason_param : "");
14538
14539
14540
14541 if (*tmp == '\"') {
14542 char *end_quote;
14543 rname = tmp + 1;
14544 end_quote = strchr(rname, '\"');
14545 *end_quote = '\0';
14546 }
14547
14548 if (number) {
14549 *number = ast_strdup(rexten);
14550 }
14551
14552 if (name && rname) {
14553 *name = ast_strdup(rname);
14554 }
14555
14556 if (reason && !ast_strlen_zero(reason_param)) {
14557 *reason = sip_reason_str_to_code(reason_param);
14558 }
14559
14560 return 0;
14561 }
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573 static enum sip_get_dest_result get_destination(struct sip_pvt *p, struct sip_request *oreq, int *cc_recall_core_id)
14574 {
14575 char tmp[256] = "", *uri, *domain, *dummy = NULL;
14576 char tmpf[256] = "", *from = NULL;
14577 struct sip_request *req;
14578 char *decoded_uri;
14579
14580 req = oreq;
14581 if (!req) {
14582 req = &p->initreq;
14583 }
14584
14585
14586 if (req->rlPart2)
14587 ast_copy_string(tmp, REQ_OFFSET_TO_STR(req, rlPart2), sizeof(tmp));
14588
14589 uri = ast_strdupa(get_in_brackets(tmp));
14590
14591 if (parse_uri(uri, "sip:,sips:", &uri, &dummy, &domain, NULL)) {
14592 ast_log(LOG_WARNING, "Not a SIP header (%s)?\n", uri);
14593 return SIP_GET_DEST_INVALID_URI;
14594 }
14595
14596 SIP_PEDANTIC_DECODE(domain);
14597 SIP_PEDANTIC_DECODE(uri);
14598
14599 ast_string_field_set(p, domain, domain);
14600
14601
14602
14603
14604
14605 ast_copy_string(tmpf, get_header(req, "From"), sizeof(tmpf));
14606 if (!ast_strlen_zero(tmpf)) {
14607 from = get_in_brackets(tmpf);
14608 if (parse_uri(from, "sip:,sips:", &from, NULL, &domain, NULL)) {
14609 ast_log(LOG_WARNING, "Not a SIP header (%s)?\n", from);
14610 return SIP_GET_DEST_INVALID_URI;
14611 }
14612
14613 SIP_PEDANTIC_DECODE(from);
14614 SIP_PEDANTIC_DECODE(domain);
14615
14616 ast_string_field_set(p, fromdomain, domain);
14617 }
14618
14619 if (!AST_LIST_EMPTY(&domain_list)) {
14620 char domain_context[AST_MAX_EXTENSION];
14621
14622 domain_context[0] = '\0';
14623 if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
14624 if (!sip_cfg.allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
14625 ast_debug(1, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
14626 return SIP_GET_DEST_REFUSED;
14627 }
14628 }
14629
14630
14631 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_HAVEPEERCONTEXT) && !ast_strlen_zero(domain_context)) {
14632 ast_string_field_set(p, context, domain_context);
14633 }
14634 }
14635
14636
14637 if (req->method == SIP_SUBSCRIBE && !ast_strlen_zero(p->subscribecontext)) {
14638 ast_string_field_set(p, context, p->subscribecontext);
14639 }
14640
14641 if (sip_debug_test_pvt(p)) {
14642 ast_verbose("Looking for %s in %s (domain %s)\n", uri, p->context, p->domain);
14643 }
14644
14645
14646
14647 decoded_uri = ast_strdupa(uri);
14648 ast_uri_decode(decoded_uri);
14649
14650
14651 if (req->method == SIP_SUBSCRIBE) {
14652 char hint[AST_MAX_EXTENSION];
14653 int which = 0;
14654 if (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, uri) ||
14655 (ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, p->context, decoded_uri) && (which = 1))) {
14656 if (!oreq) {
14657 ast_string_field_set(p, exten, which ? decoded_uri : uri);
14658 }
14659 return SIP_GET_DEST_EXTEN_FOUND;
14660 } else {
14661 return SIP_GET_DEST_EXTEN_NOT_FOUND;
14662 }
14663 } else {
14664 struct ast_cc_agent *agent;
14665 int which = 0;
14666
14667
14668
14669 if (ast_exists_extension(NULL, p->context, uri, 1, S_OR(p->cid_num, from)) ||
14670 (ast_exists_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from)) && (which = 1)) ||
14671 !strcmp(decoded_uri, ast_pickup_ext())) {
14672 if (!oreq) {
14673 ast_string_field_set(p, exten, which ? decoded_uri : uri);
14674 }
14675 return SIP_GET_DEST_EXTEN_FOUND;
14676 } else if ((agent = find_sip_cc_agent_by_notify_uri(tmp))) {
14677 struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
14678
14679
14680
14681 ast_string_field_set(p, exten, agent_pvt->original_exten);
14682
14683
14684
14685 ast_cc_agent_recalling(agent->core_id, "SIP caller %s is attempting recall",
14686 agent->device_name);
14687 if (cc_recall_core_id) {
14688 *cc_recall_core_id = agent->core_id;
14689 }
14690 ao2_ref(agent, -1);
14691 return SIP_GET_DEST_EXTEN_FOUND;
14692 }
14693 }
14694
14695
14696 if((ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP) &&
14697 ast_canmatch_extension(NULL, p->context, decoded_uri, 1, S_OR(p->cid_num, from))) ||
14698 !strncmp(decoded_uri, ast_pickup_ext(), strlen(decoded_uri))) {
14699 return SIP_GET_DEST_PICKUP_EXTEN_FOUND;
14700 }
14701
14702 return SIP_GET_DEST_EXTEN_NOT_FOUND;
14703 }
14704
14705
14706
14707
14708 static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
14709 {
14710 struct sip_pvt *sip_pvt_ptr;
14711 struct sip_pvt tmp_dialog = {
14712 .callid = callid,
14713 };
14714
14715 if (totag) {
14716 ast_debug(4, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
14717 }
14718
14719
14720
14721 sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find of dialog in dialogs table");
14722 if (sip_pvt_ptr) {
14723
14724 sip_pvt_lock(sip_pvt_ptr);
14725 if (sip_cfg.pedanticsipchecking) {
14726 unsigned char frommismatch = 0, tomismatch = 0;
14727
14728 if (ast_strlen_zero(fromtag)) {
14729 sip_pvt_unlock(sip_pvt_ptr);
14730 ast_debug(4, "Matched %s call for callid=%s - no from tag specified, pedantic check fails\n",
14731 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid);
14732 return NULL;
14733 }
14734
14735 if (ast_strlen_zero(totag)) {
14736 sip_pvt_unlock(sip_pvt_ptr);
14737 ast_debug(4, "Matched %s call for callid=%s - no to tag specified, pedantic check fails\n",
14738 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid);
14739 return NULL;
14740 }
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755 frommismatch = !!strcmp(fromtag, sip_pvt_ptr->theirtag);
14756 tomismatch = !!strcmp(totag, sip_pvt_ptr->tag);
14757
14758 if (frommismatch || tomismatch) {
14759 sip_pvt_unlock(sip_pvt_ptr);
14760 if (frommismatch) {
14761 ast_debug(4, "Matched %s call for callid=%s - pedantic from tag check fails; their tag is %s our tag is %s\n",
14762 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid,
14763 fromtag, sip_pvt_ptr->theirtag);
14764 }
14765 if (tomismatch) {
14766 ast_debug(4, "Matched %s call for callid=%s - pedantic to tag check fails; their tag is %s our tag is %s\n",
14767 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING", sip_pvt_ptr->callid,
14768 totag, sip_pvt_ptr->tag);
14769 }
14770 return NULL;
14771 }
14772 }
14773
14774 if (totag)
14775 ast_debug(4, "Matched %s call - their tag is %s Our tag is %s\n",
14776 sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING",
14777 sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
14778
14779
14780 while (sip_pvt_ptr->owner && ast_channel_trylock(sip_pvt_ptr->owner)) {
14781 sip_pvt_unlock(sip_pvt_ptr);
14782 usleep(1);
14783 sip_pvt_lock(sip_pvt_ptr);
14784 }
14785 }
14786
14787 return sip_pvt_ptr;
14788 }
14789
14790
14791
14792
14793
14794
14795
14796
14797 static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
14798 {
14799
14800 const char *p_referred_by = NULL;
14801 char *h_refer_to = NULL;
14802 char *h_referred_by = NULL;
14803 char *refer_to;
14804 const char *p_refer_to;
14805 char *referred_by_uri = NULL;
14806 char *ptr;
14807 struct sip_request *req = NULL;
14808 const char *transfer_context = NULL;
14809 struct sip_refer *referdata;
14810
14811
14812 req = outgoing_req;
14813 referdata = transferer->refer;
14814
14815 if (!req) {
14816 req = &transferer->initreq;
14817 }
14818
14819 p_refer_to = get_header(req, "Refer-To");
14820 if (ast_strlen_zero(p_refer_to)) {
14821 ast_log(LOG_WARNING, "Refer-To Header missing. Skipping transfer.\n");
14822 return -2;
14823 }
14824 h_refer_to = ast_strdupa(p_refer_to);
14825 refer_to = get_in_brackets(h_refer_to);
14826 if (!strncasecmp(refer_to, "sip:", 4)) {
14827 refer_to += 4;
14828 } else if (!strncasecmp(refer_to, "sips:", 5)) {
14829 refer_to += 5;
14830 } else {
14831 ast_log(LOG_WARNING, "Can't transfer to non-sip: URI. (Refer-to: %s)?\n", refer_to);
14832 return -3;
14833 }
14834
14835
14836 p_referred_by = get_header(req, "Referred-By");
14837
14838
14839 if (transferer->owner) {
14840 struct ast_channel *peer = ast_bridged_channel(transferer->owner);
14841 if (peer) {
14842 pbx_builtin_setvar_helper(peer, "SIPREFERRINGCONTEXT", transferer->context);
14843 pbx_builtin_setvar_helper(peer, "SIPREFERREDBYHDR", p_referred_by);
14844 }
14845 }
14846
14847 if (!ast_strlen_zero(p_referred_by)) {
14848 char *lessthan;
14849 h_referred_by = ast_strdupa(p_referred_by);
14850
14851
14852 ast_copy_string(referdata->referred_by_name, h_referred_by, sizeof(referdata->referred_by_name));
14853 if ((lessthan = strchr(referdata->referred_by_name, '<'))) {
14854 *(lessthan - 1) = '\0';
14855 }
14856
14857 referred_by_uri = get_in_brackets(h_referred_by);
14858
14859 if (!strncasecmp(referred_by_uri, "sip:", 4)) {
14860 referred_by_uri += 4;
14861 } else if (!strncasecmp(referred_by_uri, "sips:", 5)) {
14862 referred_by_uri += 5;
14863 } else {
14864 ast_log(LOG_WARNING, "Huh? Not a sip: header (Referred-by: %s). Skipping.\n", referred_by_uri);
14865 referred_by_uri = NULL;
14866 }
14867 }
14868
14869
14870 if ((ptr = strcasestr(refer_to, "replaces="))) {
14871 char *to = NULL, *from = NULL;
14872
14873
14874 referdata->attendedtransfer = 1;
14875 ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
14876 ast_uri_decode(referdata->replaces_callid);
14877 if ((ptr = strchr(referdata->replaces_callid, ';'))) {
14878 *ptr++ = '\0';
14879 }
14880
14881 if (ptr) {
14882
14883 to = strcasestr(ptr, "to-tag=");
14884 from = strcasestr(ptr, "from-tag=");
14885 }
14886
14887
14888 if (to) {
14889 ptr = to + 7;
14890 if ((to = strchr(ptr, '&'))) {
14891 *to = '\0';
14892 }
14893 if ((to = strchr(ptr, ';'))) {
14894 *to = '\0';
14895 }
14896 ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
14897 }
14898
14899 if (from) {
14900 ptr = from + 9;
14901 if ((to = strchr(ptr, '&'))) {
14902 *to = '\0';
14903 }
14904 if ((to = strchr(ptr, ';'))) {
14905 *to = '\0';
14906 }
14907 ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
14908 }
14909
14910 if (!strcmp(referdata->replaces_callid, transferer->callid) &&
14911 (!sip_cfg.pedanticsipchecking ||
14912 (!strcmp(referdata->replaces_callid_fromtag, transferer->theirtag) &&
14913 !strcmp(referdata->replaces_callid_totag, transferer->tag)))) {
14914 ast_log(LOG_WARNING, "Got an attempt to replace own Call-ID on %s\n", transferer->callid);
14915 return -4;
14916 }
14917
14918 if (!sip_cfg.pedanticsipchecking) {
14919 ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
14920 } else {
14921 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>" );
14922 }
14923 }
14924
14925 if ((ptr = strchr(refer_to, '@'))) {
14926 char *urioption = NULL, *domain;
14927 int bracket = 0;
14928 *ptr++ = '\0';
14929
14930 if ((urioption = strchr(ptr, ';'))) {
14931 *urioption++ = '\0';
14932 }
14933
14934 domain = ptr;
14935
14936
14937 for (; *ptr != '\0'; ++ptr) {
14938 if (*ptr == ':' && bracket == 0) {
14939 *ptr = '\0';
14940 break;
14941 } else if (*ptr == '[') {
14942 ++bracket;
14943 } else if (*ptr == ']') {
14944 --bracket;
14945 }
14946 }
14947
14948 SIP_PEDANTIC_DECODE(domain);
14949 SIP_PEDANTIC_DECODE(urioption);
14950
14951
14952 ast_copy_string(referdata->refer_to_domain, domain, sizeof(referdata->refer_to_domain));
14953 if (urioption) {
14954 ast_copy_string(referdata->refer_to_urioption, urioption, sizeof(referdata->refer_to_urioption));
14955 }
14956 }
14957
14958 if ((ptr = strchr(refer_to, ';')))
14959 *ptr = '\0';
14960
14961 SIP_PEDANTIC_DECODE(refer_to);
14962 ast_copy_string(referdata->refer_to, refer_to, sizeof(referdata->refer_to));
14963
14964 if (referred_by_uri) {
14965 if ((ptr = strchr(referred_by_uri, ';')))
14966 *ptr = '\0';
14967 SIP_PEDANTIC_DECODE(referred_by_uri);
14968 ast_copy_string(referdata->referred_by, referred_by_uri, sizeof(referdata->referred_by));
14969 } else {
14970 referdata->referred_by[0] = '\0';
14971 }
14972
14973
14974 if (transferer->owner)
14975 transfer_context = pbx_builtin_getvar_helper(transferer->owner, "TRANSFER_CONTEXT");
14976
14977
14978 if (ast_strlen_zero(transfer_context)) {
14979 transfer_context = S_OR(transferer->owner->macrocontext,
14980 S_OR(transferer->context, sip_cfg.default_context));
14981 }
14982
14983 ast_copy_string(referdata->refer_to_context, transfer_context, sizeof(referdata->refer_to_context));
14984
14985
14986 if (referdata->attendedtransfer || ast_exists_extension(NULL, transfer_context, refer_to, 1, NULL) ) {
14987 if (sip_debug_test_pvt(transferer)) {
14988 ast_verbose("SIP transfer to extension %s@%s by %s\n", refer_to, transfer_context, referred_by_uri);
14989 }
14990
14991 return 0;
14992 }
14993 if (sip_debug_test_pvt(transferer))
14994 ast_verbose("Failed SIP Transfer to non-existing extension %s in context %s\n n", refer_to, transfer_context);
14995
14996
14997 return -1;
14998 }
14999
15000
15001
15002
15003
15004 static int get_also_info(struct sip_pvt *p, struct sip_request *oreq)
15005 {
15006 char tmp[256] = "", *c, *a;
15007 struct sip_request *req = oreq ? oreq : &p->initreq;
15008 struct sip_refer *referdata = NULL;
15009 const char *transfer_context = NULL;
15010
15011 if (!p->refer && !sip_refer_allocate(p))
15012 return -1;
15013
15014 referdata = p->refer;
15015
15016 ast_copy_string(tmp, get_header(req, "Also"), sizeof(tmp));
15017 c = get_in_brackets(tmp);
15018
15019 if (parse_uri(c, "sip:,sips:", &c, NULL, &a, NULL)) {
15020 ast_log(LOG_WARNING, "Huh? Not a SIP header in Also: transfer (%s)?\n", c);
15021 return -1;
15022 }
15023
15024 SIP_PEDANTIC_DECODE(c);
15025 SIP_PEDANTIC_DECODE(a);
15026
15027 if (!ast_strlen_zero(a)) {
15028 ast_copy_string(referdata->refer_to_domain, a, sizeof(referdata->refer_to_domain));
15029 }
15030
15031 if (sip_debug_test_pvt(p))
15032 ast_verbose("Looking for %s in %s\n", c, p->context);
15033
15034 if (p->owner)
15035 transfer_context = pbx_builtin_getvar_helper(p->owner, "TRANSFER_CONTEXT");
15036
15037
15038 if (ast_strlen_zero(transfer_context)) {
15039 transfer_context = S_OR(p->owner->macrocontext,
15040 S_OR(p->context, sip_cfg.default_context));
15041 }
15042 if (ast_exists_extension(NULL, transfer_context, c, 1, NULL)) {
15043
15044 ast_debug(1, "SIP Bye-also transfer to Extension %s@%s \n", c, transfer_context);
15045 ast_copy_string(referdata->refer_to, c, sizeof(referdata->refer_to));
15046 ast_copy_string(referdata->referred_by, "", sizeof(referdata->referred_by));
15047 ast_copy_string(referdata->refer_contact, "", sizeof(referdata->refer_contact));
15048 referdata->refer_call = dialog_unref(referdata->refer_call, "unreffing referdata->refer_call");
15049
15050 ast_string_field_set(p, context, transfer_context);
15051 return 0;
15052 } else if (ast_canmatch_extension(NULL, p->context, c, 1, NULL)) {
15053 return 1;
15054 }
15055
15056 return -1;
15057 }
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069 static attribute_unused void check_via_response(struct sip_pvt *p, struct sip_request *req)
15070 {
15071 char via[256];
15072 char *cur, *opts;
15073
15074 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
15075
15076
15077 opts = strchr(via, ',');
15078 if (opts)
15079 *opts = '\0';
15080
15081
15082 opts = strchr(via, ';');
15083 if (!opts)
15084 return;
15085 *opts++ = '\0';
15086 while ( (cur = strsep(&opts, ";")) ) {
15087 if (!strncmp(cur, "rport=", 6)) {
15088 int port = strtol(cur+6, NULL, 10);
15089
15090 ast_sockaddr_set_port(&p->ourip, port);
15091 } else if (!strncmp(cur, "received=", 9)) {
15092 if (ast_parse_arg(cur + 9, PARSE_ADDR, &p->ourip))
15093 ;
15094 }
15095 }
15096 }
15097
15098
15099 static void check_via(struct sip_pvt *p, struct sip_request *req)
15100 {
15101 char via[512];
15102 char *c, *maddr;
15103 struct ast_sockaddr tmp;
15104 uint16_t port;
15105
15106 ast_copy_string(via, get_header(req, "Via"), sizeof(via));
15107
15108
15109 c = strchr(via, ',');
15110 if (c)
15111 *c = '\0';
15112
15113
15114 c = strstr(via, ";rport");
15115 if (c && (c[6] != '=')) {
15116 ast_set_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT);
15117 ast_set_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT);
15118 }
15119
15120
15121 maddr = strstr(via, "maddr=");
15122 if (maddr) {
15123 maddr += 6;
15124 c = maddr + strspn(maddr, "abcdefghijklmnopqrstuvwxyz"
15125 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.:[]");
15126 *c = '\0';
15127 }
15128
15129 c = strchr(via, ';');
15130 if (c)
15131 *c = '\0';
15132
15133 c = strchr(via, ' ');
15134 if (c) {
15135 *c = '\0';
15136 c = ast_skip_blanks(c+1);
15137 if (strcasecmp(via, "SIP/2.0/UDP") && strcasecmp(via, "SIP/2.0/TCP") && strcasecmp(via, "SIP/2.0/TLS")) {
15138 ast_log(LOG_WARNING, "Don't know how to respond via '%s'\n", via);
15139 return;
15140 }
15141
15142 if (maddr && ast_sockaddr_resolve_first(&p->sa, maddr, 0)) {
15143 p->sa = p->recv;
15144 }
15145
15146 ast_sockaddr_resolve_first(&tmp, c, 0);
15147 port = ast_sockaddr_port(&tmp);
15148 ast_sockaddr_set_port(&p->sa,
15149 port != 0 ? port : STANDARD_SIP_PORT);
15150
15151 if (sip_debug_test_pvt(p)) {
15152 ast_verbose("Sending to %s (%s)\n",
15153 ast_sockaddr_stringify(sip_real_dst(p)),
15154 sip_nat_mode(p));
15155 }
15156 }
15157 }
15158
15159
15160 static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
15161 struct sip_request *req, int sipmethod, struct ast_sockaddr *addr,
15162 struct sip_peer **authpeer,
15163 enum xmittype reliable, char *calleridname, char *uri2)
15164 {
15165 enum check_auth_result res;
15166 int debug = sip_debug_test_addr(addr);
15167 struct sip_peer *peer;
15168
15169 if (sipmethod == SIP_SUBSCRIBE) {
15170
15171
15172
15173 peer = find_peer(of, NULL, TRUE, FINDALLDEVICES, FALSE, 0);
15174 } else {
15175
15176 peer = find_peer(of, NULL, TRUE, FINDUSERS, FALSE, 0);
15177
15178
15179 if (!peer) {
15180 peer = find_peer(NULL, &p->recv, TRUE, FINDPEERS, FALSE, p->socket.type);
15181 }
15182 }
15183
15184 if (!peer) {
15185 if (debug) {
15186 ast_verbose("No matching peer for '%s' from '%s'\n",
15187 of, ast_sockaddr_stringify(&p->recv));
15188 }
15189 return AUTH_DONT_KNOW;
15190 }
15191
15192 if (!ast_apply_ha(peer->ha, addr)) {
15193 ast_debug(2, "Found peer '%s' for '%s', but fails host access\n", peer->name, of);
15194 unref_peer(peer, "unref_peer: check_peer_ok: from find_peer call, early return of AUTH_ACL_FAILED");
15195 return AUTH_ACL_FAILED;
15196 }
15197 if (debug)
15198 ast_verbose("Found peer '%s' for '%s' from %s\n",
15199 peer->name, of, ast_sockaddr_stringify(&p->recv));
15200
15201
15202
15203 if (p->rtp) {
15204 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &peer->prefs);
15205 p->autoframing = peer->autoframing;
15206 }
15207
15208
15209 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
15210 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
15211 ast_copy_flags(&p->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
15212
15213 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && p->udptl) {
15214 p->t38_maxdatagram = peer->t38_maxdatagram;
15215 set_t38_capabilities(p);
15216 }
15217
15218
15219
15220 if (p->sipoptions)
15221 peer->sipoptions = p->sipoptions;
15222
15223 do_setnat(p);
15224
15225 ast_string_field_set(p, peersecret, peer->secret);
15226 ast_string_field_set(p, peermd5secret, peer->md5secret);
15227 ast_string_field_set(p, subscribecontext, peer->subscribecontext);
15228 ast_string_field_set(p, mohinterpret, peer->mohinterpret);
15229 ast_string_field_set(p, mohsuggest, peer->mohsuggest);
15230 if (!ast_strlen_zero(peer->parkinglot)) {
15231 ast_string_field_set(p, parkinglot, peer->parkinglot);
15232 }
15233 ast_string_field_set(p, engine, peer->engine);
15234 p->disallowed_methods = peer->disallowed_methods;
15235 set_pvt_allowed_methods(p, req);
15236 ast_cc_copy_config_params(p->cc_params, peer->cc_params);
15237 if (peer->callingpres)
15238 p->callingpres = peer->callingpres;
15239 if (peer->maxms && peer->lastms)
15240 p->timer_t1 = peer->lastms < global_t1min ? global_t1min : peer->lastms;
15241 else
15242 p->timer_t1 = peer->timer_t1;
15243
15244
15245 if (peer->timer_b)
15246 p->timer_b = peer->timer_b;
15247 else
15248 p->timer_b = 64 * p->timer_t1;
15249
15250 if (ast_test_flag(&peer->flags[0], SIP_INSECURE_INVITE)) {
15251
15252 ast_string_field_set(p, peersecret, NULL);
15253 ast_string_field_set(p, peermd5secret, NULL);
15254 }
15255 if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri2, reliable, req->ignore))) {
15256 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
15257 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
15258 ast_copy_flags(&p->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
15259
15260 if (peer->call_limit)
15261 ast_set_flag(&p->flags[0], SIP_CALL_LIMIT);
15262 ast_string_field_set(p, peername, peer->name);
15263 ast_string_field_set(p, authname, peer->name);
15264
15265 if (sipmethod == SIP_INVITE) {
15266
15267 p->chanvars = copy_vars(peer->chanvars);
15268 }
15269
15270 if (authpeer) {
15271 ao2_t_ref(peer, 1, "copy pointer into (*authpeer)");
15272 (*authpeer) = peer;
15273 }
15274
15275 if (!ast_strlen_zero(peer->username)) {
15276 ast_string_field_set(p, username, peer->username);
15277
15278
15279 ast_string_field_set(p, authname, peer->username);
15280 }
15281 if (!get_rpid(p, req)) {
15282 if (!ast_strlen_zero(peer->cid_num)) {
15283 char *tmp = ast_strdupa(peer->cid_num);
15284 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
15285 ast_shrink_phone_number(tmp);
15286 ast_string_field_set(p, cid_num, tmp);
15287 }
15288 if (!ast_strlen_zero(peer->cid_name))
15289 ast_string_field_set(p, cid_name, peer->cid_name);
15290 if (!ast_strlen_zero(peer->cid_tag))
15291 ast_string_field_set(p, cid_tag, peer->cid_tag);
15292 if (peer->callingpres)
15293 p->callingpres = peer->callingpres;
15294 }
15295 ast_string_field_set(p, fullcontact, peer->fullcontact);
15296 if (!ast_strlen_zero(peer->context))
15297 ast_string_field_set(p, context, peer->context);
15298 ast_string_field_set(p, peersecret, peer->secret);
15299 ast_string_field_set(p, peermd5secret, peer->md5secret);
15300 ast_string_field_set(p, language, peer->language);
15301 ast_string_field_set(p, accountcode, peer->accountcode);
15302 p->amaflags = peer->amaflags;
15303 p->callgroup = peer->callgroup;
15304 p->pickupgroup = peer->pickupgroup;
15305 p->capability = peer->capability;
15306 p->prefs = peer->prefs;
15307 p->jointcapability = peer->capability;
15308 if (peer->maxforwards > 0) {
15309 p->maxforwards = peer->maxforwards;
15310 }
15311 if (p->peercapability)
15312 p->jointcapability &= p->peercapability;
15313 p->maxcallbitrate = peer->maxcallbitrate;
15314 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
15315 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
15316 p->noncodeccapability |= AST_RTP_DTMF;
15317 else
15318 p->noncodeccapability &= ~AST_RTP_DTMF;
15319 p->jointnoncodeccapability = p->noncodeccapability;
15320 if (!dialog_initialize_rtp(p)) {
15321 if (p->rtp) {
15322 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &peer->prefs);
15323 p->autoframing = peer->autoframing;
15324 }
15325 } else {
15326 res = AUTH_RTP_FAILED;
15327 }
15328 }
15329 unref_peer(peer, "check_peer_ok: unref_peer: tossing temp ptr to peer from find_peer");
15330 return res;
15331 }
15332
15333
15334
15335
15336
15337
15338
15339 static enum check_auth_result check_user_full(struct sip_pvt *p, struct sip_request *req,
15340 int sipmethod, const char *uri, enum xmittype reliable,
15341 struct ast_sockaddr *addr, struct sip_peer **authpeer)
15342 {
15343 char from[256] = { 0, };
15344 char *dummy = NULL;
15345 char *domain = NULL;
15346 char *of;
15347 enum check_auth_result res = AUTH_DONT_KNOW;
15348 char calleridname[50];
15349 char *uri2 = ast_strdupa(uri);
15350
15351 terminate_uri(uri2);
15352
15353 ast_copy_string(from, get_header(req, "From"), sizeof(from));
15354
15355 memset(calleridname, 0, sizeof(calleridname));
15356
15357
15358 if (!(of = (char *) get_calleridname(from, calleridname, sizeof(calleridname)))) {
15359 ast_log(LOG_ERROR, "FROM header can not be parsed \n");
15360 return res;
15361 }
15362
15363 if (calleridname[0])
15364 ast_string_field_set(p, cid_name, calleridname);
15365
15366 if (ast_strlen_zero(p->exten)) {
15367 char *t = uri2;
15368 if (!strncasecmp(t, "sip:", 4))
15369 t+= 4;
15370 else if (!strncasecmp(t, "sips:", 5))
15371 t += 5;
15372 ast_string_field_set(p, exten, t);
15373 t = strchr(p->exten, '@');
15374 if (t)
15375 *t = '\0';
15376
15377 if (ast_strlen_zero(p->our_contact))
15378 build_contact(p);
15379 }
15380
15381 of = get_in_brackets(of);
15382
15383
15384 ast_string_field_set(p, from, of);
15385
15386
15387 if (parse_uri(of, "sip:,sips:", &of, &dummy, &domain, NULL)) {
15388 ast_log(LOG_NOTICE, "From address missing 'sip:', using it anyway\n");
15389 }
15390
15391 SIP_PEDANTIC_DECODE(of);
15392 SIP_PEDANTIC_DECODE(domain);
15393
15394 if (ast_strlen_zero(of)) {
15395
15396
15397
15398
15399
15400
15401 of = domain;
15402 } else {
15403 char *tmp = ast_strdupa(of);
15404
15405
15406
15407 tmp = strsep(&tmp, ";");
15408 if (global_shrinkcallerid && ast_is_shrinkable_phonenumber(tmp))
15409 ast_shrink_phone_number(tmp);
15410 ast_string_field_set(p, cid_num, tmp);
15411 }
15412
15413 if (global_match_auth_username) {
15414
15415
15416
15417
15418
15419
15420
15421
15422 const char *hdr = get_header(req, "Authorization");
15423 if (ast_strlen_zero(hdr))
15424 hdr = get_header(req, "Proxy-Authorization");
15425
15426 if ( !ast_strlen_zero(hdr) && (hdr = strstr(hdr, "username=\"")) ) {
15427 ast_copy_string(from, hdr + strlen("username=\""), sizeof(from));
15428 of = from;
15429 of = strsep(&of, "\"");
15430 }
15431 }
15432
15433 res = check_peer_ok(p, of, req, sipmethod, addr,
15434 authpeer, reliable, calleridname, uri2);
15435 if (res != AUTH_DONT_KNOW)
15436 return res;
15437
15438
15439 if (sip_cfg.allowguest) {
15440 get_rpid(p, req);
15441 if (!dialog_initialize_rtp(p)) {
15442 res = AUTH_SUCCESSFUL;
15443 } else {
15444 res = AUTH_RTP_FAILED;
15445 }
15446 } else if (sip_cfg.alwaysauthreject)
15447 res = AUTH_FAKE_AUTH;
15448 else
15449 res = AUTH_SECRET_FAILED;
15450
15451
15452 if (ast_test_flag(&p->flags[1], SIP_PAGE2_RPORT_PRESENT)) {
15453 ast_set_flag(&p->flags[0], SIP_NAT_RPORT_PRESENT);
15454 }
15455
15456 return res;
15457 }
15458
15459
15460
15461
15462 static int check_user(struct sip_pvt *p, struct sip_request *req, int sipmethod, const char *uri, enum xmittype reliable, struct ast_sockaddr *addr)
15463 {
15464 return check_user_full(p, req, sipmethod, uri, reliable, addr, NULL);
15465 }
15466
15467
15468 static int get_msg_text(char *buf, int len, struct sip_request *req, int addnewline)
15469 {
15470 int x;
15471 int y;
15472
15473 buf[0] = '\0';
15474
15475 y = len - strlen(buf) - 5;
15476 if (y < 0)
15477 y = 0;
15478 for (x = 0; x < req->lines; x++) {
15479 const char *line = REQ_OFFSET_TO_STR(req, line[x]);
15480 strncat(buf, line, y);
15481 y -= strlen(line) + 1;
15482 if (y < 0)
15483 y = 0;
15484 if (y != 0 && addnewline)
15485 strcat(buf, "\n");
15486 }
15487 return 0;
15488 }
15489
15490
15491
15492
15493
15494 static void receive_message(struct sip_pvt *p, struct sip_request *req)
15495 {
15496 char buf[1400];
15497 struct ast_frame f;
15498 const char *content_type = get_header(req, "Content-Type");
15499
15500 if (strncmp(content_type, "text/plain", strlen("text/plain"))) {
15501 transmit_response(p, "415 Unsupported Media Type", req);
15502 if (!p->owner)
15503 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15504 return;
15505 }
15506
15507 if (get_msg_text(buf, sizeof(buf), req, FALSE)) {
15508 ast_log(LOG_WARNING, "Unable to retrieve text from %s\n", p->callid);
15509 transmit_response(p, "202 Accepted", req);
15510 if (!p->owner)
15511 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15512 return;
15513 }
15514
15515 if (p->owner) {
15516 if (sip_debug_test_pvt(p))
15517 ast_verbose("SIP Text message received: '%s'\n", buf);
15518 memset(&f, 0, sizeof(f));
15519 f.frametype = AST_FRAME_TEXT;
15520 f.subclass.integer = 0;
15521 f.offset = 0;
15522 f.data.ptr = buf;
15523 f.datalen = strlen(buf) + 1;
15524 ast_queue_frame(p->owner, &f);
15525 transmit_response(p, "202 Accepted", req);
15526 return;
15527 }
15528
15529
15530 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);
15531 transmit_response(p, "405 Method Not Allowed", req);
15532 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
15533 return;
15534 }
15535
15536
15537 static char *sip_show_inuse(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15538 {
15539 #define FORMAT "%-25.25s %-15.15s %-15.15s \n"
15540 #define FORMAT2 "%-25.25s %-15.15s %-15.15s \n"
15541 char ilimits[40];
15542 char iused[40];
15543 int showall = FALSE;
15544 struct ao2_iterator i;
15545 struct sip_peer *peer;
15546
15547 switch (cmd) {
15548 case CLI_INIT:
15549 e->command = "sip show inuse";
15550 e->usage =
15551 "Usage: sip show inuse [all]\n"
15552 " List all SIP devices usage counters and limits.\n"
15553 " Add option \"all\" to show all devices, not only those with a limit.\n";
15554 return NULL;
15555 case CLI_GENERATE:
15556 return NULL;
15557 }
15558
15559 if (a->argc < 3)
15560 return CLI_SHOWUSAGE;
15561
15562 if (a->argc == 4 && !strcmp(a->argv[3], "all"))
15563 showall = TRUE;
15564
15565 ast_cli(a->fd, FORMAT, "* Peer name", "In use", "Limit");
15566
15567 i = ao2_iterator_init(peers, 0);
15568 while ((peer = ao2_t_iterator_next(&i, "iterate thru peer table"))) {
15569 ao2_lock(peer);
15570 if (peer->call_limit)
15571 snprintf(ilimits, sizeof(ilimits), "%d", peer->call_limit);
15572 else
15573 ast_copy_string(ilimits, "N/A", sizeof(ilimits));
15574 snprintf(iused, sizeof(iused), "%d/%d/%d", peer->inUse, peer->inRinging, peer->onHold);
15575 if (showall || peer->call_limit)
15576 ast_cli(a->fd, FORMAT2, peer->name, iused, ilimits);
15577 ao2_unlock(peer);
15578 unref_peer(peer, "toss iterator pointer");
15579 }
15580 ao2_iterator_destroy(&i);
15581
15582 return CLI_SUCCESS;
15583 #undef FORMAT
15584 #undef FORMAT2
15585 }
15586
15587
15588
15589 static char *transfermode2str(enum transfermodes mode)
15590 {
15591 if (mode == TRANSFER_OPENFORALL)
15592 return "open";
15593 else if (mode == TRANSFER_CLOSED)
15594 return "closed";
15595 return "strict";
15596 }
15597
15598
15599
15600
15601
15602
15603
15604 static const struct _map_x_s stmodes[] = {
15605 { SESSION_TIMER_MODE_ACCEPT, "Accept"},
15606 { SESSION_TIMER_MODE_ORIGINATE, "Originate"},
15607 { SESSION_TIMER_MODE_REFUSE, "Refuse"},
15608 { -1, NULL},
15609 };
15610
15611 static const char *stmode2str(enum st_mode m)
15612 {
15613 return map_x_s(stmodes, m, "Unknown");
15614 }
15615
15616 static enum st_mode str2stmode(const char *s)
15617 {
15618 return map_s_x(stmodes, s, -1);
15619 }
15620
15621
15622 static const struct _map_x_s strefreshers[] = {
15623 { SESSION_TIMER_REFRESHER_AUTO, "auto"},
15624 { SESSION_TIMER_REFRESHER_UAC, "uac"},
15625 { SESSION_TIMER_REFRESHER_UAS, "uas"},
15626 { -1, NULL},
15627 };
15628
15629 static const char *strefresher2str(enum st_refresher r)
15630 {
15631 return map_x_s(strefreshers, r, "Unknown");
15632 }
15633
15634 static enum st_refresher str2strefresher(const char *s)
15635 {
15636 return map_s_x(strefreshers, s, -1);
15637 }
15638
15639
15640 static int peer_status(struct sip_peer *peer, char *status, int statuslen)
15641 {
15642 int res = 0;
15643 if (peer->maxms) {
15644 if (peer->lastms < 0) {
15645 ast_copy_string(status, "UNREACHABLE", statuslen);
15646 } else if (peer->lastms > peer->maxms) {
15647 snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
15648 res = 1;
15649 } else if (peer->lastms) {
15650 snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
15651 res = 1;
15652 } else {
15653 ast_copy_string(status, "UNKNOWN", statuslen);
15654 }
15655 } else {
15656 ast_copy_string(status, "Unmonitored", statuslen);
15657
15658 res = -1;
15659 }
15660 return res;
15661 }
15662
15663
15664 static char *sip_show_tcp(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15665 {
15666 struct sip_threadinfo *th;
15667 struct ao2_iterator i;
15668
15669 #define FORMAT2 "%-47.47s %9.9s %6.6s\n"
15670 #define FORMAT "%-47.47s %-9.9s %-6.6s\n"
15671
15672 switch (cmd) {
15673 case CLI_INIT:
15674 e->command = "sip show tcp";
15675 e->usage =
15676 "Usage: sip show tcp\n"
15677 " Lists all active TCP/TLS sessions.\n";
15678 return NULL;
15679 case CLI_GENERATE:
15680 return NULL;
15681 }
15682
15683 if (a->argc != 3)
15684 return CLI_SHOWUSAGE;
15685
15686 ast_cli(a->fd, FORMAT2, "Address", "Transport", "Type");
15687
15688 i = ao2_iterator_init(threadt, 0);
15689 while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
15690 ast_cli(a->fd, FORMAT,
15691 ast_sockaddr_stringify(&th->tcptls_session->remote_address),
15692 get_transport(th->type),
15693 (th->tcptls_session->client ? "Client" : "Server"));
15694 ao2_t_ref(th, -1, "decrement ref from iterator");
15695 }
15696 ao2_iterator_destroy(&i);
15697
15698 return CLI_SUCCESS;
15699 #undef FORMAT
15700 #undef FORMAT2
15701 }
15702
15703
15704 static char *sip_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15705 {
15706 regex_t regexbuf;
15707 int havepattern = FALSE;
15708 struct ao2_iterator user_iter;
15709 struct sip_peer *user;
15710
15711 #define FORMAT "%-25.25s %-15.15s %-15.15s %-15.15s %-5.5s%-10.10s\n"
15712
15713 switch (cmd) {
15714 case CLI_INIT:
15715 e->command = "sip show users";
15716 e->usage =
15717 "Usage: sip show users [like <pattern>]\n"
15718 " Lists all known SIP users.\n"
15719 " Optional regular expression pattern is used to filter the user list.\n";
15720 return NULL;
15721 case CLI_GENERATE:
15722 return NULL;
15723 }
15724
15725 switch (a->argc) {
15726 case 5:
15727 if (!strcasecmp(a->argv[3], "like")) {
15728 if (regcomp(®exbuf, a->argv[4], REG_EXTENDED | REG_NOSUB))
15729 return CLI_SHOWUSAGE;
15730 havepattern = TRUE;
15731 } else
15732 return CLI_SHOWUSAGE;
15733 case 3:
15734 break;
15735 default:
15736 return CLI_SHOWUSAGE;
15737 }
15738
15739 ast_cli(a->fd, FORMAT, "Username", "Secret", "Accountcode", "Def.Context", "ACL", "ForcerPort");
15740
15741 user_iter = ao2_iterator_init(peers, 0);
15742 while ((user = ao2_iterator_next(&user_iter))) {
15743 ao2_lock(user);
15744 if (!(user->type & SIP_TYPE_USER)) {
15745 ao2_unlock(user);
15746 unref_peer(user, "sip show users");
15747 continue;
15748 }
15749
15750 if (havepattern && regexec(®exbuf, user->name, 0, NULL, 0)) {
15751 ao2_unlock(user);
15752 unref_peer(user, "sip show users");
15753 continue;
15754 }
15755
15756 ast_cli(a->fd, FORMAT, user->name,
15757 user->secret,
15758 user->accountcode,
15759 user->context,
15760 AST_CLI_YESNO(user->ha != NULL),
15761 AST_CLI_YESNO(ast_test_flag(&user->flags[0], SIP_NAT_FORCE_RPORT)));
15762 ao2_unlock(user);
15763 unref_peer(user, "sip show users");
15764 }
15765 ao2_iterator_destroy(&user_iter);
15766
15767 if (havepattern)
15768 regfree(®exbuf);
15769
15770 return CLI_SUCCESS;
15771 #undef FORMAT
15772 }
15773
15774
15775 static int manager_show_registry(struct mansession *s, const struct message *m)
15776 {
15777 const char *id = astman_get_header(m, "ActionID");
15778 char idtext[256] = "";
15779 int total = 0;
15780
15781 if (!ast_strlen_zero(id))
15782 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
15783
15784 astman_send_listack(s, m, "Registrations will follow", "start");
15785
15786 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
15787 ASTOBJ_RDLOCK(iterator);
15788 astman_append(s,
15789 "Event: RegistryEntry\r\n"
15790 "%s"
15791 "Host: %s\r\n"
15792 "Port: %d\r\n"
15793 "Username: %s\r\n"
15794 "Domain: %s\r\n"
15795 "DomainPort: %d\r\n"
15796 "Refresh: %d\r\n"
15797 "State: %s\r\n"
15798 "RegistrationTime: %ld\r\n"
15799 "\r\n",
15800 idtext,
15801 iterator->hostname,
15802 iterator->portno ? iterator->portno : STANDARD_SIP_PORT,
15803 iterator->username,
15804 S_OR(iterator->regdomain,iterator->hostname),
15805 iterator->regdomainport ? iterator->regdomainport : STANDARD_SIP_PORT,
15806 iterator->refresh,
15807 regstate2str(iterator->regstate),
15808 (long) iterator->regtime.tv_sec);
15809 ASTOBJ_UNLOCK(iterator);
15810 total++;
15811 } while(0));
15812
15813 astman_append(s,
15814 "Event: RegistrationsComplete\r\n"
15815 "EventList: Complete\r\n"
15816 "ListItems: %d\r\n"
15817 "%s"
15818 "\r\n", total, idtext);
15819
15820 return 0;
15821 }
15822
15823
15824
15825 static int manager_sip_show_peers(struct mansession *s, const struct message *m)
15826 {
15827 const char *id = astman_get_header(m, "ActionID");
15828 const char *a[] = {"sip", "show", "peers"};
15829 char idtext[256] = "";
15830 int total = 0;
15831
15832 if (!ast_strlen_zero(id))
15833 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
15834
15835 astman_send_listack(s, m, "Peer status list will follow", "start");
15836
15837 _sip_show_peers(-1, &total, s, m, 3, a);
15838
15839 astman_append(s,
15840 "Event: PeerlistComplete\r\n"
15841 "EventList: Complete\r\n"
15842 "ListItems: %d\r\n"
15843 "%s"
15844 "\r\n", total, idtext);
15845 return 0;
15846 }
15847
15848
15849 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15850 {
15851 switch (cmd) {
15852 case CLI_INIT:
15853 e->command = "sip show peers";
15854 e->usage =
15855 "Usage: sip show peers [like <pattern>]\n"
15856 " Lists all known SIP peers.\n"
15857 " Optional regular expression pattern is used to filter the peer list.\n";
15858 return NULL;
15859 case CLI_GENERATE:
15860 return NULL;
15861 }
15862
15863 return _sip_show_peers(a->fd, NULL, NULL, NULL, a->argc, (const char **) a->argv);
15864 }
15865
15866 int peercomparefunc(const void *a, const void *b);
15867
15868 int peercomparefunc(const void *a, const void *b)
15869 {
15870 struct sip_peer **ap = (struct sip_peer **)a;
15871 struct sip_peer **bp = (struct sip_peer **)b;
15872 return strcmp((*ap)->name, (*bp)->name);
15873 }
15874
15875
15876
15877 static char *_sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
15878 {
15879 regex_t regexbuf;
15880 int havepattern = FALSE;
15881 struct sip_peer *peer;
15882 struct ao2_iterator i;
15883
15884
15885 #define FORMAT2 "%-25.25s %-39.39s %-3.3s %-10.10s %-3.3s %-8s %-10s %s\n"
15886 #define FORMAT "%-25.25s %-39.39s %-3.3s %-3.3s %-3.3s %-8d %-10s %s\n"
15887
15888 char name[256];
15889 int total_peers = 0;
15890 int peers_mon_online = 0;
15891 int peers_mon_offline = 0;
15892 int peers_unmon_offline = 0;
15893 int peers_unmon_online = 0;
15894 const char *id;
15895 char idtext[256] = "";
15896 int realtimepeers;
15897 int objcount = ao2_container_count(peers);
15898 struct sip_peer **peerarray;
15899 int k;
15900
15901
15902 realtimepeers = ast_check_realtime("sippeers");
15903 peerarray = ast_calloc(sizeof(struct sip_peer *), objcount);
15904
15905 if (s) {
15906 id = astman_get_header(m, "ActionID");
15907 if (!ast_strlen_zero(id))
15908 snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
15909 }
15910
15911 switch (argc) {
15912 case 5:
15913 if (!strcasecmp(argv[3], "like")) {
15914 if (regcomp(®exbuf, argv[4], REG_EXTENDED | REG_NOSUB))
15915 return CLI_SHOWUSAGE;
15916 havepattern = TRUE;
15917 } else
15918 return CLI_SHOWUSAGE;
15919 case 3:
15920 break;
15921 default:
15922 return CLI_SHOWUSAGE;
15923 }
15924
15925 if (!s)
15926 ast_cli(fd, FORMAT2, "Name/username", "Host", "Dyn", "Forcerport", "ACL", "Port", "Status", (realtimepeers ? "Realtime" : ""));
15927
15928
15929 i = ao2_iterator_init(peers, 0);
15930 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
15931 ao2_lock(peer);
15932
15933 if (!(peer->type & SIP_TYPE_PEER)) {
15934 ao2_unlock(peer);
15935 unref_peer(peer, "unref peer because it's actually a user");
15936 continue;
15937 }
15938
15939 if (havepattern && regexec(®exbuf, peer->name, 0, NULL, 0)) {
15940 objcount--;
15941 ao2_unlock(peer);
15942 unref_peer(peer, "toss iterator peer ptr before continue");
15943 continue;
15944 }
15945
15946 peerarray[total_peers++] = peer;
15947 ao2_unlock(peer);
15948 }
15949 ao2_iterator_destroy(&i);
15950
15951 qsort(peerarray, total_peers, sizeof(struct sip_peer *), peercomparefunc);
15952
15953 for(k=0; k < total_peers; k++) {
15954 char status[20] = "";
15955 char srch[2000];
15956 char pstatus;
15957 peer = peerarray[k];
15958
15959 ao2_lock(peer);
15960 if (havepattern && regexec(®exbuf, peer->name, 0, NULL, 0)) {
15961 ao2_unlock(peer);
15962 unref_peer(peer, "toss iterator peer ptr before continue");
15963 continue;
15964 }
15965
15966 if (!ast_strlen_zero(peer->username) && !s)
15967 snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
15968 else
15969 ast_copy_string(name, peer->name, sizeof(name));
15970
15971 pstatus = peer_status(peer, status, sizeof(status));
15972 if (pstatus == 1)
15973 peers_mon_online++;
15974 else if (pstatus == 0)
15975 peers_mon_offline++;
15976 else {
15977 if (ast_sockaddr_isnull(&peer->addr) ||
15978 !ast_sockaddr_port(&peer->addr)) {
15979 peers_unmon_offline++;
15980 } else {
15981 peers_unmon_online++;
15982 }
15983 }
15984
15985 snprintf(srch, sizeof(srch), FORMAT, name,
15986 ast_sockaddr_isnull(&peer->addr) ? "(Unspecified)" : ast_sockaddr_stringify_addr(&peer->addr),
15987 peer->host_dynamic ? " D " : " ",
15988 ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) ? " N " : " ",
15989 peer->ha ? " A " : " ",
15990 ast_sockaddr_isnull(&peer->addr) ? 0 : ast_sockaddr_port(&peer->addr), status,
15991 realtimepeers ? (peer->is_realtime ? "Cached RT":"") : "");
15992
15993 if (!s) {
15994 ast_cli(fd, FORMAT, name,
15995 ast_sockaddr_isnull(&peer->addr) ? "(Unspecified)" : ast_sockaddr_stringify_addr(&peer->addr),
15996 peer->host_dynamic ? " D " : " ",
15997 ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) ? " N " : " ",
15998 peer->ha ? " A " : " ",
15999 ast_sockaddr_isnull(&peer->addr) ? 0 : ast_sockaddr_port(&peer->addr), status,
16000 realtimepeers ? (peer->is_realtime ? "Cached RT":"") : "");
16001 } else {
16002
16003 astman_append(s,
16004 "Event: PeerEntry\r\n%s"
16005 "Channeltype: SIP\r\n"
16006 "ObjectName: %s\r\n"
16007 "ChanObjectType: peer\r\n"
16008 "IPaddress: %s\r\n"
16009 "IPport: %d\r\n"
16010 "Dynamic: %s\r\n"
16011 "Forcerport: %s\r\n"
16012 "VideoSupport: %s\r\n"
16013 "TextSupport: %s\r\n"
16014 "ACL: %s\r\n"
16015 "Status: %s\r\n"
16016 "RealtimeDevice: %s\r\n\r\n",
16017 idtext,
16018 peer->name,
16019 ast_sockaddr_isnull(&peer->addr) ? "-none-" : ast_sockaddr_stringify_fmt(&peer->addr, AST_SOCKADDR_STR_HOST),
16020 ast_sockaddr_isnull(&peer->addr) ? 0 : ast_sockaddr_port(&peer->addr),
16021 peer->host_dynamic ? "yes" : "no",
16022 ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT) ? "yes" : "no",
16023 ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT) ? "yes" : "no",
16024 ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT) ? "yes" : "no",
16025 peer->ha ? "yes" : "no",
16026 status,
16027 realtimepeers ? (peer->is_realtime ? "yes":"no") : "no");
16028 }
16029 ao2_unlock(peer);
16030 unref_peer(peer, "toss iterator peer ptr");
16031 }
16032
16033 if (!s)
16034 ast_cli(fd, "%d sip peers [Monitored: %d online, %d offline Unmonitored: %d online, %d offline]\n",
16035 total_peers, peers_mon_online, peers_mon_offline, peers_unmon_online, peers_unmon_offline);
16036
16037 if (havepattern)
16038 regfree(®exbuf);
16039
16040 if (total)
16041 *total = total_peers;
16042
16043 ast_free(peerarray);
16044
16045 return CLI_SUCCESS;
16046 #undef FORMAT
16047 #undef FORMAT2
16048 }
16049
16050 static int peer_dump_func(void *userobj, void *arg, int flags)
16051 {
16052 struct sip_peer *peer = userobj;
16053 int refc = ao2_t_ref(userobj, 0, "");
16054 struct ast_cli_args *a = (struct ast_cli_args *) arg;
16055
16056 ast_cli(a->fd, "name: %s\ntype: peer\nobjflags: %d\nrefcount: %d\n\n",
16057 peer->name, 0, refc);
16058 return 0;
16059 }
16060
16061 static int dialog_dump_func(void *userobj, void *arg, int flags)
16062 {
16063 struct sip_pvt *pvt = userobj;
16064 int refc = ao2_t_ref(userobj, 0, "");
16065 struct ast_cli_args *a = (struct ast_cli_args *) arg;
16066
16067 ast_cli(a->fd, "name: %s\ntype: dialog\nobjflags: %d\nrefcount: %d\n\n",
16068 pvt->callid, 0, refc);
16069 return 0;
16070 }
16071
16072
16073
16074 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16075 {
16076 char tmp[256];
16077
16078 switch (cmd) {
16079 case CLI_INIT:
16080 e->command = "sip show objects";
16081 e->usage =
16082 "Usage: sip show objects\n"
16083 " Lists status of known SIP objects\n";
16084 return NULL;
16085 case CLI_GENERATE:
16086 return NULL;
16087 }
16088
16089 if (a->argc != 3)
16090 return CLI_SHOWUSAGE;
16091 ast_cli(a->fd, "-= Peer objects: %d static, %d realtime, %d autocreate =-\n\n", speerobjs, rpeerobjs, apeerobjs);
16092 ao2_t_callback(peers, OBJ_NODATA, peer_dump_func, a, "initiate ao2_callback to dump peers");
16093 ast_cli(a->fd, "-= Registry objects: %d =-\n\n", regobjs);
16094 ASTOBJ_CONTAINER_DUMP(a->fd, tmp, sizeof(tmp), ®l);
16095 ast_cli(a->fd, "-= Dialog objects:\n\n");
16096 ao2_t_callback(dialogs, OBJ_NODATA, dialog_dump_func, a, "initiate ao2_callback to dump dialogs");
16097 return CLI_SUCCESS;
16098 }
16099
16100 static void print_group(int fd, ast_group_t group, int crlf)
16101 {
16102 char buf[256];
16103 ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
16104 }
16105
16106
16107 static const struct _map_x_s dtmfstr[] = {
16108 { SIP_DTMF_RFC2833, "rfc2833" },
16109 { SIP_DTMF_INFO, "info" },
16110 { SIP_DTMF_SHORTINFO, "shortinfo" },
16111 { SIP_DTMF_INBAND, "inband" },
16112 { SIP_DTMF_AUTO, "auto" },
16113 { -1, NULL },
16114 };
16115
16116
16117 static const char *dtmfmode2str(int mode)
16118 {
16119 return map_x_s(dtmfstr, mode, "<error>");
16120 }
16121
16122
16123 static int str2dtmfmode(const char *str)
16124 {
16125 return map_s_x(dtmfstr, str, -1);
16126 }
16127
16128 static const struct _map_x_s insecurestr[] = {
16129 { SIP_INSECURE_PORT, "port" },
16130 { SIP_INSECURE_INVITE, "invite" },
16131 { SIP_INSECURE_PORT | SIP_INSECURE_INVITE, "port,invite" },
16132 { 0, "no" },
16133 { -1, NULL },
16134 };
16135
16136
16137 static const char *insecure2str(int mode)
16138 {
16139 return map_x_s(insecurestr, mode, "<error>");
16140 }
16141
16142
16143
16144
16145 static void cleanup_stale_contexts(char *new, char *old)
16146 {
16147 char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
16148
16149 while ((oldcontext = strsep(&old, "&"))) {
16150 stalecontext = '\0';
16151 ast_copy_string(newlist, new, sizeof(newlist));
16152 stringp = newlist;
16153 while ((newcontext = strsep(&stringp, "&"))) {
16154 if (!strcmp(newcontext, oldcontext)) {
16155
16156 stalecontext = '\0';
16157 break;
16158 } else if (strcmp(newcontext, oldcontext)) {
16159 stalecontext = oldcontext;
16160 }
16161
16162 }
16163 if (stalecontext)
16164 ast_context_destroy(ast_context_find(stalecontext), "SIP");
16165 }
16166 }
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180 static int dialog_needdestroy(void *dialogobj, void *arg, int flags)
16181 {
16182 struct sip_pvt *dialog = dialogobj;
16183 time_t *t = arg;
16184
16185 if (sip_pvt_trylock(dialog)) {
16186
16187
16188 return 0;
16189 }
16190
16191
16192 if (dialog->rtp && ast_rtp_instance_get_bridged(dialog->rtp)) {
16193 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16194 sip_pvt_unlock(dialog);
16195 return 0;
16196 }
16197
16198 if (dialog->vrtp && ast_rtp_instance_get_bridged(dialog->vrtp)) {
16199 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16200 sip_pvt_unlock(dialog);
16201 return 0;
16202 }
16203
16204
16205 check_rtp_timeout(dialog, *t);
16206
16207
16208
16209
16210 if (dialog->needdestroy && !dialog->packets && !dialog->owner) {
16211
16212 if (dialog->rtp && ast_rtp_instance_get_bridged(dialog->rtp)) {
16213 ast_debug(2, "Bridge still active. Delaying destruction of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16214 sip_pvt_unlock(dialog);
16215 return 0;
16216 }
16217
16218 if (dialog->vrtp && ast_rtp_instance_get_bridged(dialog->vrtp)) {
16219 ast_debug(2, "Bridge still active. Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16220 sip_pvt_unlock(dialog);
16221 return 0;
16222 }
16223
16224 sip_pvt_unlock(dialog);
16225
16226
16227 dialog_unlink_all(dialog, TRUE, FALSE);
16228 return 0;
16229 }
16230
16231 sip_pvt_unlock(dialog);
16232
16233 return 0;
16234 }
16235
16236
16237
16238 static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16239 {
16240 struct sip_peer *peer, *pi;
16241 int prunepeer = FALSE;
16242 int multi = FALSE;
16243 const char *name = NULL;
16244 regex_t regexbuf;
16245 struct ao2_iterator i;
16246 static const char * const choices[] = { "all", "like", NULL };
16247 char *cmplt;
16248
16249 if (cmd == CLI_INIT) {
16250 e->command = "sip prune realtime [peer|all]";
16251 e->usage =
16252 "Usage: sip prune realtime [peer [<name>|all|like <pattern>]|all]\n"
16253 " Prunes object(s) from the cache.\n"
16254 " Optional regular expression pattern is used to filter the objects.\n";
16255 return NULL;
16256 } else if (cmd == CLI_GENERATE) {
16257 if (a->pos == 4 && !strcasecmp(a->argv[3], "peer")) {
16258 cmplt = ast_cli_complete(a->word, choices, a->n);
16259 if (!cmplt)
16260 cmplt = complete_sip_peer(a->word, a->n - sizeof(choices), SIP_PAGE2_RTCACHEFRIENDS);
16261 return cmplt;
16262 }
16263 if (a->pos == 5 && !strcasecmp(a->argv[4], "like"))
16264 return complete_sip_peer(a->word, a->n, SIP_PAGE2_RTCACHEFRIENDS);
16265 return NULL;
16266 }
16267 switch (a->argc) {
16268 case 4:
16269 name = a->argv[3];
16270
16271 if (!strcasecmp(name, "peer") || !strcasecmp(name, "like"))
16272 return CLI_SHOWUSAGE;
16273 prunepeer = TRUE;
16274 if (!strcasecmp(name, "all")) {
16275 multi = TRUE;
16276 name = NULL;
16277 }
16278
16279 break;
16280 case 5:
16281
16282 name = a->argv[4];
16283 if (!strcasecmp(a->argv[3], "peer"))
16284 prunepeer = TRUE;
16285 else if (!strcasecmp(a->argv[3], "like")) {
16286 prunepeer = TRUE;
16287 multi = TRUE;
16288 } else
16289 return CLI_SHOWUSAGE;
16290 if (!strcasecmp(name, "like"))
16291 return CLI_SHOWUSAGE;
16292 if (!multi && !strcasecmp(name, "all")) {
16293 multi = TRUE;
16294 name = NULL;
16295 }
16296 break;
16297 case 6:
16298 name = a->argv[5];
16299 multi = TRUE;
16300
16301 if (strcasecmp(a->argv[4], "like"))
16302 return CLI_SHOWUSAGE;
16303 if (!strcasecmp(a->argv[3], "peer")) {
16304 prunepeer = TRUE;
16305 } else
16306 return CLI_SHOWUSAGE;
16307 break;
16308 default:
16309 return CLI_SHOWUSAGE;
16310 }
16311
16312 if (multi && name) {
16313 if (regcomp(®exbuf, name, REG_EXTENDED | REG_NOSUB))
16314 return CLI_SHOWUSAGE;
16315 }
16316
16317 if (multi) {
16318 if (prunepeer) {
16319 int pruned = 0;
16320
16321 i = ao2_iterator_init(peers, 0);
16322 while ((pi = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
16323 ao2_lock(pi);
16324 if (name && regexec(®exbuf, pi->name, 0, NULL, 0)) {
16325 unref_peer(pi, "toss iterator peer ptr before continue");
16326 ao2_unlock(pi);
16327 continue;
16328 };
16329 if (ast_test_flag(&pi->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
16330 pi->the_mark = 1;
16331 pruned++;
16332 }
16333 ao2_unlock(pi);
16334 unref_peer(pi, "toss iterator peer ptr");
16335 }
16336 ao2_iterator_destroy(&i);
16337 if (pruned) {
16338 unlink_marked_peers_from_tables();
16339 ast_cli(a->fd, "%d peers pruned.\n", pruned);
16340 } else
16341 ast_cli(a->fd, "No peers found to prune.\n");
16342 }
16343 } else {
16344 if (prunepeer) {
16345 struct sip_peer tmp;
16346 ast_copy_string(tmp.name, name, sizeof(tmp.name));
16347 if ((peer = ao2_t_find(peers, &tmp, OBJ_POINTER | OBJ_UNLINK, "finding to unlink from peers"))) {
16348 if (!ast_sockaddr_isnull(&peer->addr)) {
16349 ao2_t_unlink(peers_by_ip, peer, "unlinking peer from peers_by_ip also");
16350 }
16351 if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
16352 ast_cli(a->fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
16353
16354 ao2_t_link(peers, peer, "link peer into peer table");
16355 if (!ast_sockaddr_isnull(&peer->addr)) {
16356 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
16357 }
16358 } else
16359 ast_cli(a->fd, "Peer '%s' pruned.\n", name);
16360 unref_peer(peer, "sip_prune_realtime: unref_peer: tossing temp peer ptr");
16361 } else
16362 ast_cli(a->fd, "Peer '%s' not found.\n", name);
16363 }
16364 }
16365
16366 return CLI_SUCCESS;
16367 }
16368
16369
16370 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
16371 {
16372 int x;
16373 format_t codec;
16374
16375 for(x = 0; x < 64 ; x++) {
16376 codec = ast_codec_pref_index(pref, x);
16377 if (!codec)
16378 break;
16379 ast_cli(fd, "%s", ast_getformatname(codec));
16380 ast_cli(fd, ":%d", pref->framing[x]);
16381 if (x < 31 && ast_codec_pref_index(pref, x + 1))
16382 ast_cli(fd, ",");
16383 }
16384 if (!x)
16385 ast_cli(fd, "none");
16386 }
16387
16388
16389 static const char *domain_mode_to_text(const enum domain_mode mode)
16390 {
16391 switch (mode) {
16392 case SIP_DOMAIN_AUTO:
16393 return "[Automatic]";
16394 case SIP_DOMAIN_CONFIG:
16395 return "[Configured]";
16396 }
16397
16398 return "";
16399 }
16400
16401
16402 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16403 {
16404 struct domain *d;
16405 #define FORMAT "%-40.40s %-20.20s %-16.16s\n"
16406
16407 switch (cmd) {
16408 case CLI_INIT:
16409 e->command = "sip show domains";
16410 e->usage =
16411 "Usage: sip show domains\n"
16412 " Lists all configured SIP local domains.\n"
16413 " Asterisk only responds to SIP messages to local domains.\n";
16414 return NULL;
16415 case CLI_GENERATE:
16416 return NULL;
16417 }
16418
16419 if (AST_LIST_EMPTY(&domain_list)) {
16420 ast_cli(a->fd, "SIP Domain support not enabled.\n\n");
16421 return CLI_SUCCESS;
16422 } else {
16423 ast_cli(a->fd, FORMAT, "Our local SIP domains:", "Context", "Set by");
16424 AST_LIST_LOCK(&domain_list);
16425 AST_LIST_TRAVERSE(&domain_list, d, list)
16426 ast_cli(a->fd, FORMAT, d->domain, S_OR(d->context, "(default)"),
16427 domain_mode_to_text(d->mode));
16428 AST_LIST_UNLOCK(&domain_list);
16429 ast_cli(a->fd, "\n");
16430 return CLI_SUCCESS;
16431 }
16432 }
16433 #undef FORMAT
16434
16435
16436 static int manager_sip_show_peer(struct mansession *s, const struct message *m)
16437 {
16438 const char *a[4];
16439 const char *peer;
16440
16441 peer = astman_get_header(m, "Peer");
16442 if (ast_strlen_zero(peer)) {
16443 astman_send_error(s, m, "Peer: <name> missing.");
16444 return 0;
16445 }
16446 a[0] = "sip";
16447 a[1] = "show";
16448 a[2] = "peer";
16449 a[3] = peer;
16450
16451 _sip_show_peer(1, -1, s, m, 4, a);
16452 astman_append(s, "\r\n\r\n" );
16453 return 0;
16454 }
16455
16456
16457 static char *sip_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16458 {
16459 switch (cmd) {
16460 case CLI_INIT:
16461 e->command = "sip show peer";
16462 e->usage =
16463 "Usage: sip show peer <name> [load]\n"
16464 " Shows all details on one SIP peer and the current status.\n"
16465 " Option \"load\" forces lookup of peer in realtime storage.\n";
16466 return NULL;
16467 case CLI_GENERATE:
16468 return complete_sip_show_peer(a->line, a->word, a->pos, a->n);
16469 }
16470 return _sip_show_peer(0, a->fd, NULL, NULL, a->argc, (const char **) a->argv);
16471 }
16472
16473
16474 static char *_sip_qualify_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
16475 {
16476 struct sip_peer *peer;
16477 int load_realtime;
16478
16479 if (argc < 4)
16480 return CLI_SHOWUSAGE;
16481
16482 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
16483 if ((peer = find_peer(argv[3], NULL, load_realtime, FINDPEERS, FALSE, 0))) {
16484 sip_poke_peer(peer, 1);
16485 unref_peer(peer, "qualify: done with peer");
16486 } else if (type == 0) {
16487 ast_cli(fd, "Peer '%s' not found\n", argv[3]);
16488 } else {
16489 astman_send_error(s, m, "Peer not found");
16490 }
16491 return CLI_SUCCESS;
16492 }
16493
16494
16495 static int manager_sip_qualify_peer(struct mansession *s, const struct message *m)
16496 {
16497 const char *a[4];
16498 const char *peer;
16499
16500 peer = astman_get_header(m, "Peer");
16501 if (ast_strlen_zero(peer)) {
16502 astman_send_error(s, m, "Peer: <name> missing.");
16503 return 0;
16504 }
16505 a[0] = "sip";
16506 a[1] = "qualify";
16507 a[2] = "peer";
16508 a[3] = peer;
16509
16510 _sip_qualify_peer(1, -1, s, m, 4, a);
16511 astman_append(s, "\r\n\r\n" );
16512 return 0;
16513 }
16514
16515
16516 static char *sip_qualify_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16517 {
16518 switch (cmd) {
16519 case CLI_INIT:
16520 e->command = "sip qualify peer";
16521 e->usage =
16522 "Usage: sip qualify peer <name> [load]\n"
16523 " Requests a response from one SIP peer and the current status.\n"
16524 " Option \"load\" forces lookup of peer in realtime storage.\n";
16525 return NULL;
16526 case CLI_GENERATE:
16527 return complete_sip_show_peer(a->line, a->word, a->pos, a->n);
16528 }
16529 return _sip_qualify_peer(0, a->fd, NULL, NULL, a->argc, (const char **) a->argv);
16530 }
16531
16532
16533 static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer)
16534 {
16535 struct sip_mailbox *mailbox;
16536
16537 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
16538 ast_str_append(mailbox_str, 0, "%s%s%s%s",
16539 mailbox->mailbox,
16540 ast_strlen_zero(mailbox->context) ? "" : "@",
16541 S_OR(mailbox->context, ""),
16542 AST_LIST_NEXT(mailbox, entry) ? "," : "");
16543 }
16544 }
16545
16546 static struct _map_x_s faxecmodes[] = {
16547 { SIP_PAGE2_T38SUPPORT_UDPTL, "None"},
16548 { SIP_PAGE2_T38SUPPORT_UDPTL_FEC, "FEC"},
16549 { SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY, "Redundancy"},
16550 { -1, NULL},
16551 };
16552
16553 static const char *faxec2str(int faxec)
16554 {
16555 return map_x_s(faxecmodes, faxec, "Unknown");
16556 }
16557
16558
16559 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
16560 {
16561 char status[30] = "";
16562 char cbuf[256];
16563 struct sip_peer *peer;
16564 char codec_buf[512];
16565 struct ast_codec_pref *pref;
16566 struct ast_variable *v;
16567 struct sip_auth *auth;
16568 int x = 0, load_realtime;
16569 format_t codec = 0;
16570 int realtimepeers;
16571
16572 realtimepeers = ast_check_realtime("sippeers");
16573
16574 if (argc < 4)
16575 return CLI_SHOWUSAGE;
16576
16577 load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
16578 peer = find_peer(argv[3], NULL, load_realtime, FINDPEERS, FALSE, 0);
16579
16580 if (s) {
16581 if (peer) {
16582 const char *id = astman_get_header(m, "ActionID");
16583
16584 astman_append(s, "Response: Success\r\n");
16585 if (!ast_strlen_zero(id))
16586 astman_append(s, "ActionID: %s\r\n", id);
16587 } else {
16588 snprintf (cbuf, sizeof(cbuf), "Peer %s not found.", argv[3]);
16589 astman_send_error(s, m, cbuf);
16590 return CLI_SUCCESS;
16591 }
16592 }
16593 if (peer && type==0 ) {
16594 struct ast_str *mailbox_str = ast_str_alloca(512);
16595 ast_cli(fd, "\n\n");
16596 ast_cli(fd, " * Name : %s\n", peer->name);
16597 if (realtimepeers) {
16598 ast_cli(fd, " Realtime peer: %s\n", peer->is_realtime ? "Yes, cached" : "No");
16599 }
16600 ast_cli(fd, " Secret : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
16601 ast_cli(fd, " MD5Secret : %s\n", ast_strlen_zero(peer->md5secret)?"<Not set>":"<Set>");
16602 ast_cli(fd, " Remote Secret: %s\n", ast_strlen_zero(peer->remotesecret)?"<Not set>":"<Set>");
16603 for (auth = peer->auth; auth; auth = auth->next) {
16604 ast_cli(fd, " Realm-auth : Realm %-15.15s User %-10.20s ", auth->realm, auth->username);
16605 ast_cli(fd, "%s\n", !ast_strlen_zero(auth->secret)?"<Secret set>":(!ast_strlen_zero(auth->md5secret)?"<MD5secret set>" : "<Not set>"));
16606 }
16607 ast_cli(fd, " Context : %s\n", peer->context);
16608 ast_cli(fd, " Subscr.Cont. : %s\n", S_OR(peer->subscribecontext, "<Not set>") );
16609 ast_cli(fd, " Language : %s\n", peer->language);
16610 if (!ast_strlen_zero(peer->accountcode))
16611 ast_cli(fd, " Accountcode : %s\n", peer->accountcode);
16612 ast_cli(fd, " AMA flags : %s\n", ast_cdr_flags2str(peer->amaflags));
16613 ast_cli(fd, " Transfer mode: %s\n", transfermode2str(peer->allowtransfer));
16614 ast_cli(fd, " CallingPres : %s\n", ast_describe_caller_presentation(peer->callingpres));
16615 if (!ast_strlen_zero(peer->fromuser))
16616 ast_cli(fd, " FromUser : %s\n", peer->fromuser);
16617 if (!ast_strlen_zero(peer->fromdomain))
16618 ast_cli(fd, " FromDomain : %s Port %d\n", peer->fromdomain, (peer->fromdomainport) ? peer->fromdomainport : STANDARD_SIP_PORT);
16619 ast_cli(fd, " Callgroup : ");
16620 print_group(fd, peer->callgroup, 0);
16621 ast_cli(fd, " Pickupgroup : ");
16622 print_group(fd, peer->pickupgroup, 0);
16623 peer_mailboxes_to_str(&mailbox_str, peer);
16624 ast_cli(fd, " MOH Suggest : %s\n", peer->mohsuggest);
16625 ast_cli(fd, " Mailbox : %s\n", mailbox_str->str);
16626 ast_cli(fd, " VM Extension : %s\n", peer->vmexten);
16627 ast_cli(fd, " LastMsgsSent : %d/%d\n", (peer->lastmsgssent & 0x7fff0000) >> 16, peer->lastmsgssent & 0xffff);
16628 ast_cli(fd, " Call limit : %d\n", peer->call_limit);
16629 ast_cli(fd, " Max forwards : %d\n", peer->maxforwards);
16630 if (peer->busy_level)
16631 ast_cli(fd, " Busy level : %d\n", peer->busy_level);
16632 ast_cli(fd, " Dynamic : %s\n", AST_CLI_YESNO(peer->host_dynamic));
16633 ast_cli(fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
16634 ast_cli(fd, " MaxCallBR : %d kbps\n", peer->maxcallbitrate);
16635 ast_cli(fd, " Expire : %ld\n", ast_sched_when(sched, peer->expire));
16636 ast_cli(fd, " Insecure : %s\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
16637 ast_cli(fd, " Force rport : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT)));
16638 ast_cli(fd, " ACL : %s\n", AST_CLI_YESNO(peer->ha != NULL));
16639 ast_cli(fd, " DirectMedACL : %s\n", AST_CLI_YESNO(peer->directmediaha != NULL));
16640 ast_cli(fd, " T.38 support : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
16641 ast_cli(fd, " T.38 EC mode : %s\n", faxec2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
16642 ast_cli(fd, " T.38 MaxDtgrm: %d\n", peer->t38_maxdatagram);
16643 ast_cli(fd, " DirectMedia : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_DIRECT_MEDIA)));
16644 ast_cli(fd, " PromiscRedir : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)));
16645 ast_cli(fd, " User=Phone : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)));
16646 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)));
16647 ast_cli(fd, " Text Support : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)));
16648 ast_cli(fd, " Ign SDP ver : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_IGNORESDPVERSION)));
16649 ast_cli(fd, " Trust RPID : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_TRUSTRPID)));
16650 ast_cli(fd, " Send RPID : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[0], SIP_SENDRPID)));
16651 ast_cli(fd, " Subscriptions: %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
16652 ast_cli(fd, " Overlap dial : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWOVERLAP)));
16653 if (peer->outboundproxy)
16654 ast_cli(fd, " Outb. proxy : %s %s\n", ast_strlen_zero(peer->outboundproxy->name) ? "<not set>" : peer->outboundproxy->name,
16655 peer->outboundproxy->force ? "(forced)" : "");
16656
16657
16658 ast_cli(fd, " DTMFmode : %s\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
16659 ast_cli(fd, " Timer T1 : %d\n", peer->timer_t1);
16660 ast_cli(fd, " Timer B : %d\n", peer->timer_b);
16661 ast_cli(fd, " ToHost : %s\n", peer->tohost);
16662 ast_cli(fd, " Addr->IP : %s\n", ast_sockaddr_stringify(&peer->addr));
16663 ast_cli(fd, " Defaddr->IP : %s\n", ast_sockaddr_stringify(&peer->defaddr));
16664 ast_cli(fd, " Prim.Transp. : %s\n", get_transport(peer->socket.type));
16665 ast_cli(fd, " Allowed.Trsp : %s\n", get_transport_list(peer->transports));
16666 if (!ast_strlen_zero(sip_cfg.regcontext))
16667 ast_cli(fd, " Reg. exten : %s\n", peer->regexten);
16668 ast_cli(fd, " Def. Username: %s\n", peer->username);
16669 ast_cli(fd, " SIP Options : ");
16670 if (peer->sipoptions) {
16671 int lastoption = -1;
16672 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
16673 if (sip_options[x].id != lastoption) {
16674 if (peer->sipoptions & sip_options[x].id)
16675 ast_cli(fd, "%s ", sip_options[x].text);
16676 lastoption = x;
16677 }
16678 }
16679 } else
16680 ast_cli(fd, "(none)");
16681
16682 ast_cli(fd, "\n");
16683 ast_cli(fd, " Codecs : ");
16684 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
16685 ast_cli(fd, "%s\n", codec_buf);
16686 ast_cli(fd, " Codec Order : (");
16687 print_codec_to_cli(fd, &peer->prefs);
16688 ast_cli(fd, ")\n");
16689
16690 ast_cli(fd, " Auto-Framing : %s \n", AST_CLI_YESNO(peer->autoframing));
16691 ast_cli(fd, " 100 on REG : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_REGISTERTRYING)));
16692 ast_cli(fd, " Status : ");
16693 peer_status(peer, status, sizeof(status));
16694 ast_cli(fd, "%s\n", status);
16695 ast_cli(fd, " Useragent : %s\n", peer->useragent);
16696 ast_cli(fd, " Reg. Contact : %s\n", peer->fullcontact);
16697 ast_cli(fd, " Qualify Freq : %d ms\n", peer->qualifyfreq);
16698 if (peer->chanvars) {
16699 ast_cli(fd, " Variables :\n");
16700 for (v = peer->chanvars ; v ; v = v->next)
16701 ast_cli(fd, " %s = %s\n", v->name, v->value);
16702 }
16703
16704 ast_cli(fd, " Sess-Timers : %s\n", stmode2str(peer->stimer.st_mode_oper));
16705 ast_cli(fd, " Sess-Refresh : %s\n", strefresher2str(peer->stimer.st_ref));
16706 ast_cli(fd, " Sess-Expires : %d secs\n", peer->stimer.st_max_se);
16707 ast_cli(fd, " Min-Sess : %d secs\n", peer->stimer.st_min_se);
16708 ast_cli(fd, " RTP Engine : %s\n", peer->engine);
16709 ast_cli(fd, " Parkinglot : %s\n", peer->parkinglot);
16710 ast_cli(fd, " Use Reason : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_Q850_REASON)));
16711 ast_cli(fd, " Encryption : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP)));
16712 ast_cli(fd, "\n");
16713 peer = unref_peer(peer, "sip_show_peer: unref_peer: done with peer ptr");
16714 } else if (peer && type == 1) {
16715 char buffer[256];
16716 struct ast_str *mailbox_str = ast_str_alloca(512);
16717 astman_append(s, "Channeltype: SIP\r\n");
16718 astman_append(s, "ObjectName: %s\r\n", peer->name);
16719 astman_append(s, "ChanObjectType: peer\r\n");
16720 astman_append(s, "SecretExist: %s\r\n", ast_strlen_zero(peer->secret)?"N":"Y");
16721 astman_append(s, "RemoteSecretExist: %s\r\n", ast_strlen_zero(peer->remotesecret)?"N":"Y");
16722 astman_append(s, "MD5SecretExist: %s\r\n", ast_strlen_zero(peer->md5secret)?"N":"Y");
16723 astman_append(s, "Context: %s\r\n", peer->context);
16724 astman_append(s, "Language: %s\r\n", peer->language);
16725 if (!ast_strlen_zero(peer->accountcode))
16726 astman_append(s, "Accountcode: %s\r\n", peer->accountcode);
16727 astman_append(s, "AMAflags: %s\r\n", ast_cdr_flags2str(peer->amaflags));
16728 astman_append(s, "CID-CallingPres: %s\r\n", ast_describe_caller_presentation(peer->callingpres));
16729 if (!ast_strlen_zero(peer->fromuser))
16730 astman_append(s, "SIP-FromUser: %s\r\n", peer->fromuser);
16731 if (!ast_strlen_zero(peer->fromdomain))
16732 astman_append(s, "SIP-FromDomain: %s\r\nSip-FromDomain-Port: %d\r\n", peer->fromdomain, (peer->fromdomainport) ? peer->fromdomainport : STANDARD_SIP_PORT);
16733 astman_append(s, "Callgroup: ");
16734 astman_append(s, "%s\r\n", ast_print_group(buffer, sizeof(buffer), peer->callgroup));
16735 astman_append(s, "Pickupgroup: ");
16736 astman_append(s, "%s\r\n", ast_print_group(buffer, sizeof(buffer), peer->pickupgroup));
16737 astman_append(s, "MOHSuggest: %s\r\n", peer->mohsuggest);
16738 peer_mailboxes_to_str(&mailbox_str, peer);
16739 astman_append(s, "VoiceMailbox: %s\r\n", mailbox_str->str);
16740 astman_append(s, "TransferMode: %s\r\n", transfermode2str(peer->allowtransfer));
16741 astman_append(s, "Maxforwards: %d\r\n", peer->maxforwards);
16742 astman_append(s, "LastMsgsSent: %d\r\n", peer->lastmsgssent);
16743 astman_append(s, "Maxforwards: %d\r\n", peer->maxforwards);
16744 astman_append(s, "Call-limit: %d\r\n", peer->call_limit);
16745 astman_append(s, "Busy-level: %d\r\n", peer->busy_level);
16746 astman_append(s, "MaxCallBR: %d kbps\r\n", peer->maxcallbitrate);
16747 astman_append(s, "Dynamic: %s\r\n", peer->host_dynamic?"Y":"N");
16748 astman_append(s, "Callerid: %s\r\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, ""));
16749 astman_append(s, "RegExpire: %ld seconds\r\n", ast_sched_when(sched, peer->expire));
16750 astman_append(s, "SIP-AuthInsecure: %s\r\n", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
16751 astman_append(s, "SIP-Forcerport: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_NAT_FORCE_RPORT)?"Y":"N"));
16752 astman_append(s, "ACL: %s\r\n", (peer->ha?"Y":"N"));
16753 astman_append(s, "SIP-CanReinvite: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_DIRECT_MEDIA)?"Y":"N"));
16754 astman_append(s, "SIP-DirectMedia: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_DIRECT_MEDIA)?"Y":"N"));
16755 astman_append(s, "SIP-PromiscRedir: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_PROMISCREDIR)?"Y":"N"));
16756 astman_append(s, "SIP-UserPhone: %s\r\n", (ast_test_flag(&peer->flags[0], SIP_USEREQPHONE)?"Y":"N"));
16757 astman_append(s, "SIP-VideoSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_VIDEOSUPPORT)?"Y":"N"));
16758 astman_append(s, "SIP-TextSupport: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_TEXTSUPPORT)?"Y":"N"));
16759 astman_append(s, "SIP-T.38Support: %s\r\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)?"Y":"N"));
16760 astman_append(s, "SIP-T.38EC: %s\r\n", faxec2str(ast_test_flag(&peer->flags[1], SIP_PAGE2_T38SUPPORT)));
16761 astman_append(s, "SIP-T.38MaxDtgrm: %d\r\n", peer->t38_maxdatagram);
16762 astman_append(s, "SIP-Sess-Timers: %s\r\n", stmode2str(peer->stimer.st_mode_oper));
16763 astman_append(s, "SIP-Sess-Refresh: %s\r\n", strefresher2str(peer->stimer.st_ref));
16764 astman_append(s, "SIP-Sess-Expires: %d\r\n", peer->stimer.st_max_se);
16765 astman_append(s, "SIP-Sess-Min: %d\r\n", peer->stimer.st_min_se);
16766 astman_append(s, "SIP-RTP-Engine: %s\r\n", peer->engine);
16767 astman_append(s, "SIP-Encryption: %s\r\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP) ? "Y" : "N");
16768
16769
16770 astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
16771 astman_append(s, "ToHost: %s\r\n", peer->tohost);
16772 astman_append(s, "Address-IP: %s\r\nAddress-Port: %d\r\n", ast_sockaddr_stringify_addr(&peer->addr), ast_sockaddr_port(&peer->addr));
16773 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));
16774 astman_append(s, "Default-Username: %s\r\n", peer->username);
16775 if (!ast_strlen_zero(sip_cfg.regcontext))
16776 astman_append(s, "RegExtension: %s\r\n", peer->regexten);
16777 astman_append(s, "Codecs: ");
16778 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
16779 astman_append(s, "%s\r\n", codec_buf);
16780 astman_append(s, "CodecOrder: ");
16781 pref = &peer->prefs;
16782 for(x = 0; x < 64 ; x++) {
16783 codec = ast_codec_pref_index(pref, x);
16784 if (!codec)
16785 break;
16786 astman_append(s, "%s", ast_getformatname(codec));
16787 if (x < 63 && ast_codec_pref_index(pref, x+1))
16788 astman_append(s, ",");
16789 }
16790
16791 astman_append(s, "\r\n");
16792 astman_append(s, "Status: ");
16793 peer_status(peer, status, sizeof(status));
16794 astman_append(s, "%s\r\n", status);
16795 astman_append(s, "SIP-Useragent: %s\r\n", peer->useragent);
16796 astman_append(s, "Reg-Contact: %s\r\n", peer->fullcontact);
16797 astman_append(s, "QualifyFreq: %d ms\r\n", peer->qualifyfreq);
16798 astman_append(s, "Parkinglot: %s\r\n", peer->parkinglot);
16799 if (peer->chanvars) {
16800 for (v = peer->chanvars ; v ; v = v->next) {
16801 astman_append(s, "ChanVariable: %s=%s\r\n", v->name, v->value);
16802 }
16803 }
16804 astman_append(s, "SIP-Use-Reason-Header : %s\n", (ast_test_flag(&peer->flags[1], SIP_PAGE2_Q850_REASON)) ? "Y" : "N");
16805
16806 peer = unref_peer(peer, "sip_show_peer: unref_peer: done with peer");
16807
16808 } else {
16809 ast_cli(fd, "Peer %s not found.\n", argv[3]);
16810 ast_cli(fd, "\n");
16811 }
16812
16813 return CLI_SUCCESS;
16814 }
16815
16816
16817 static char *complete_sip_user(const char *word, int state)
16818 {
16819 char *result = NULL;
16820 int wordlen = strlen(word);
16821 int which = 0;
16822 struct ao2_iterator user_iter;
16823 struct sip_peer *user;
16824
16825 user_iter = ao2_iterator_init(peers, 0);
16826 while ((user = ao2_iterator_next(&user_iter))) {
16827 ao2_lock(user);
16828 if (!(user->type & SIP_TYPE_USER)) {
16829 ao2_unlock(user);
16830 unref_peer(user, "complete sip user");
16831 continue;
16832 }
16833
16834 if (!strncasecmp(word, user->name, wordlen) && ++which > state) {
16835 result = ast_strdup(user->name);
16836 }
16837 ao2_unlock(user);
16838 unref_peer(user, "complete sip user");
16839 if (result) {
16840 break;
16841 }
16842 }
16843 ao2_iterator_destroy(&user_iter);
16844 return result;
16845 }
16846
16847 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state)
16848 {
16849 if (pos == 3)
16850 return complete_sip_user(word, state);
16851
16852 return NULL;
16853 }
16854
16855
16856 static char *sip_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16857 {
16858 char cbuf[256];
16859 struct sip_peer *user;
16860 struct ast_variable *v;
16861 int load_realtime;
16862
16863 switch (cmd) {
16864 case CLI_INIT:
16865 e->command = "sip show user";
16866 e->usage =
16867 "Usage: sip show user <name> [load]\n"
16868 " Shows all details on one SIP user and the current status.\n"
16869 " Option \"load\" forces lookup of peer in realtime storage.\n";
16870 return NULL;
16871 case CLI_GENERATE:
16872 return complete_sip_show_user(a->line, a->word, a->pos, a->n);
16873 }
16874
16875 if (a->argc < 4)
16876 return CLI_SHOWUSAGE;
16877
16878
16879 load_realtime = (a->argc == 5 && !strcmp(a->argv[4], "load")) ? TRUE : FALSE;
16880
16881 if ((user = find_peer(a->argv[3], NULL, load_realtime, FINDUSERS, FALSE, 0))) {
16882 ao2_lock(user);
16883 ast_cli(a->fd, "\n\n");
16884 ast_cli(a->fd, " * Name : %s\n", user->name);
16885 ast_cli(a->fd, " Secret : %s\n", ast_strlen_zero(user->secret)?"<Not set>":"<Set>");
16886 ast_cli(a->fd, " MD5Secret : %s\n", ast_strlen_zero(user->md5secret)?"<Not set>":"<Set>");
16887 ast_cli(a->fd, " Context : %s\n", user->context);
16888 ast_cli(a->fd, " Language : %s\n", user->language);
16889 if (!ast_strlen_zero(user->accountcode))
16890 ast_cli(a->fd, " Accountcode : %s\n", user->accountcode);
16891 ast_cli(a->fd, " AMA flags : %s\n", ast_cdr_flags2str(user->amaflags));
16892 ast_cli(a->fd, " Transfer mode: %s\n", transfermode2str(user->allowtransfer));
16893 ast_cli(a->fd, " MaxCallBR : %d kbps\n", user->maxcallbitrate);
16894 ast_cli(a->fd, " CallingPres : %s\n", ast_describe_caller_presentation(user->callingpres));
16895 ast_cli(a->fd, " Call limit : %d\n", user->call_limit);
16896 ast_cli(a->fd, " Callgroup : ");
16897 print_group(a->fd, user->callgroup, 0);
16898 ast_cli(a->fd, " Pickupgroup : ");
16899 print_group(a->fd, user->pickupgroup, 0);
16900 ast_cli(a->fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), user->cid_name, user->cid_num, "<unspecified>"));
16901 ast_cli(a->fd, " ACL : %s\n", AST_CLI_YESNO(user->ha != NULL));
16902 ast_cli(a->fd, " Sess-Timers : %s\n", stmode2str(user->stimer.st_mode_oper));
16903 ast_cli(a->fd, " Sess-Refresh : %s\n", strefresher2str(user->stimer.st_ref));
16904 ast_cli(a->fd, " Sess-Expires : %d secs\n", user->stimer.st_max_se);
16905 ast_cli(a->fd, " Sess-Min-SE : %d secs\n", user->stimer.st_min_se);
16906 ast_cli(a->fd, " RTP Engine : %s\n", user->engine);
16907
16908 ast_cli(a->fd, " Codec Order : (");
16909 print_codec_to_cli(a->fd, &user->prefs);
16910 ast_cli(a->fd, ")\n");
16911
16912 ast_cli(a->fd, " Auto-Framing: %s \n", AST_CLI_YESNO(user->autoframing));
16913 if (user->chanvars) {
16914 ast_cli(a->fd, " Variables :\n");
16915 for (v = user->chanvars ; v ; v = v->next)
16916 ast_cli(a->fd, " %s = %s\n", v->name, v->value);
16917 }
16918
16919 ast_cli(a->fd, "\n");
16920
16921 ao2_unlock(user);
16922 unref_peer(user, "sip show user");
16923 } else {
16924 ast_cli(a->fd, "User %s not found.\n", a->argv[3]);
16925 ast_cli(a->fd, "\n");
16926 }
16927
16928 return CLI_SUCCESS;
16929 }
16930
16931
16932 static char *sip_show_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16933 {
16934 struct ast_str *cbuf;
16935 struct ast_cb_names cbnames = {9, { "retrans_pkt",
16936 "__sip_autodestruct",
16937 "expire_register",
16938 "auto_congest",
16939 "sip_reg_timeout",
16940 "sip_poke_peer_s",
16941 "sip_poke_noanswer",
16942 "sip_reregister",
16943 "sip_reinvite_retry"},
16944 { retrans_pkt,
16945 __sip_autodestruct,
16946 expire_register,
16947 auto_congest,
16948 sip_reg_timeout,
16949 sip_poke_peer_s,
16950 sip_poke_noanswer,
16951 sip_reregister,
16952 sip_reinvite_retry}};
16953
16954 switch (cmd) {
16955 case CLI_INIT:
16956 e->command = "sip show sched";
16957 e->usage =
16958 "Usage: sip show sched\n"
16959 " Shows stats on what's in the sched queue at the moment\n";
16960 return NULL;
16961 case CLI_GENERATE:
16962 return NULL;
16963 }
16964
16965 cbuf = ast_str_alloca(2048);
16966
16967 ast_cli(a->fd, "\n");
16968 ast_sched_report(sched, &cbuf, &cbnames);
16969 ast_cli(a->fd, "%s", cbuf->str);
16970
16971 return CLI_SUCCESS;
16972 }
16973
16974
16975 static char *sip_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16976 {
16977 #define FORMAT2 "%-39.39s %-6.6s %-12.12s %8.8s %-20.20s %-25.25s\n"
16978 #define FORMAT "%-39.39s %-6.6s %-12.12s %8d %-20.20s %-25.25s\n"
16979 char host[80];
16980 char user[80];
16981 char tmpdat[256];
16982 struct ast_tm tm;
16983 int counter = 0;
16984
16985 switch (cmd) {
16986 case CLI_INIT:
16987 e->command = "sip show registry";
16988 e->usage =
16989 "Usage: sip show registry\n"
16990 " Lists all registration requests and status.\n";
16991 return NULL;
16992 case CLI_GENERATE:
16993 return NULL;
16994 }
16995
16996 if (a->argc != 3)
16997 return CLI_SHOWUSAGE;
16998 ast_cli(a->fd, FORMAT2, "Host", "dnsmgr", "Username", "Refresh", "State", "Reg.Time");
16999
17000 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
17001 ASTOBJ_RDLOCK(iterator);
17002 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
17003 snprintf(user, sizeof(user), "%s", iterator->username);
17004 if (!ast_strlen_zero(iterator->regdomain)) {
17005 snprintf(tmpdat, sizeof(tmpdat), "%s", user);
17006 snprintf(user, sizeof(user), "%s@%s", tmpdat, iterator->regdomain);}
17007 if (iterator->regdomainport) {
17008 snprintf(tmpdat, sizeof(tmpdat), "%s", user);
17009 snprintf(user, sizeof(user), "%s:%d", tmpdat, iterator->regdomainport);}
17010 if (iterator->regtime.tv_sec) {
17011 ast_localtime(&iterator->regtime, &tm, NULL);
17012 ast_strftime(tmpdat, sizeof(tmpdat), "%a, %d %b %Y %T", &tm);
17013 } else
17014 tmpdat[0] = '\0';
17015 ast_cli(a->fd, FORMAT, host, (iterator->dnsmgr) ? "Y" : "N", user, iterator->refresh, regstate2str(iterator->regstate), tmpdat);
17016 ASTOBJ_UNLOCK(iterator);
17017 counter++;
17018 } while(0));
17019 ast_cli(a->fd, "%d SIP registrations.\n", counter);
17020 return CLI_SUCCESS;
17021 #undef FORMAT
17022 #undef FORMAT2
17023 }
17024
17025
17026
17027
17028
17029 static char *sip_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17030 {
17031 struct sip_peer *peer;
17032 int load_realtime = 0;
17033
17034 switch (cmd) {
17035 case CLI_INIT:
17036 e->command = "sip unregister";
17037 e->usage =
17038 "Usage: sip unregister <peer>\n"
17039 " Unregister (force expiration) a SIP peer from the registry\n";
17040 return NULL;
17041 case CLI_GENERATE:
17042 return complete_sip_unregister(a->line, a->word, a->pos, a->n);
17043 }
17044
17045 if (a->argc != 3)
17046 return CLI_SHOWUSAGE;
17047
17048 if ((peer = find_peer(a->argv[2], NULL, load_realtime, FINDPEERS, TRUE, 0))) {
17049 if (peer->expire > 0) {
17050 AST_SCHED_DEL_UNREF(sched, peer->expire,
17051 unref_peer(peer, "remove register expire ref"));
17052 expire_register(ref_peer(peer, "ref for expire_register"));
17053 ast_cli(a->fd, "Unregistered peer \'%s\'\n\n", a->argv[2]);
17054 } else {
17055 ast_cli(a->fd, "Peer %s not registered\n", a->argv[2]);
17056 }
17057 unref_peer(peer, "sip_unregister: unref_peer via sip_unregister: done with peer from find_peer call");
17058 } else {
17059 ast_cli(a->fd, "Peer unknown: \'%s\'. Not unregistered.\n", a->argv[2]);
17060 }
17061
17062 return CLI_SUCCESS;
17063 }
17064
17065
17066 static int show_chanstats_cb(void *__cur, void *__arg, int flags)
17067 {
17068 #define FORMAT2 "%-15.15s %-11.11s %-8.8s %-10.10s %-10.10s ( %%) %-6.6s %-10.10s %-10.10s ( %%) %-6.6s\n"
17069 #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"
17070 struct sip_pvt *cur = __cur;
17071 struct ast_rtp_instance_stats stats;
17072 char durbuf[10];
17073 int duration;
17074 int durh, durm, durs;
17075 struct ast_channel *c = cur->owner;
17076 struct __show_chan_arg *arg = __arg;
17077 int fd = arg->fd;
17078
17079
17080 if (cur->subscribed != NONE)
17081 return 0;
17082
17083 if (!cur->rtp) {
17084 if (sipdebug) {
17085 ast_cli(fd, "%-15.15s %-11.11s (inv state: %s) -- %s\n",
17086 ast_sockaddr_stringify_addr(&cur->sa), cur->callid,
17087 invitestate2string[cur->invitestate].desc,
17088 "-- No RTP active");
17089 }
17090 return 0;
17091 }
17092
17093 ast_rtp_instance_get_stats(cur->rtp, &stats, AST_RTP_INSTANCE_STAT_ALL);
17094
17095 if (c && c->cdr && !ast_tvzero(c->cdr->start)) {
17096 duration = (int)(ast_tvdiff_ms(ast_tvnow(), c->cdr->start) / 1000);
17097 durh = duration / 3600;
17098 durm = (duration % 3600) / 60;
17099 durs = duration % 60;
17100 snprintf(durbuf, sizeof(durbuf), "%02d:%02d:%02d", durh, durm, durs);
17101 } else {
17102 durbuf[0] = '\0';
17103 }
17104
17105 ast_cli(fd, FORMAT,
17106 ast_sockaddr_stringify_addr(&cur->sa),
17107 cur->callid,
17108 durbuf,
17109 stats.rxcount > (unsigned int) 100000 ? (unsigned int) (stats.rxcount)/(unsigned int) 1000 : stats.rxcount,
17110 stats.rxcount > (unsigned int) 100000 ? "K":" ",
17111 stats.rxploss,
17112 (stats.rxcount + stats.rxploss) > 0 ? (double) stats.rxploss / (stats.rxcount + stats.rxploss) * 100 : 0,
17113 stats.rxjitter,
17114 stats.txcount > (unsigned int) 100000 ? (unsigned int) (stats.txcount)/(unsigned int) 1000 : stats.txcount,
17115 stats.txcount > (unsigned int) 100000 ? "K":" ",
17116 stats.txploss,
17117 stats.txcount > 0 ? (double) stats.txploss / stats.txcount * 100 : 0,
17118 stats.txjitter
17119 );
17120 arg->numchans++;
17121
17122 return 0;
17123 }
17124
17125
17126 static char *sip_show_channelstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17127 {
17128 struct __show_chan_arg arg = { .fd = a->fd, .numchans = 0 };
17129
17130 switch (cmd) {
17131 case CLI_INIT:
17132 e->command = "sip show channelstats";
17133 e->usage =
17134 "Usage: sip show channelstats\n"
17135 " Lists all currently active SIP channel's RTCP statistics.\n"
17136 " Note that calls in the much optimized RTP P2P bridge mode will not show any packets here.";
17137 return NULL;
17138 case CLI_GENERATE:
17139 return NULL;
17140 }
17141
17142 if (a->argc != 3)
17143 return CLI_SHOWUSAGE;
17144
17145 ast_cli(a->fd, FORMAT2, "Peer", "Call ID", "Duration", "Recv: Pack", "Lost", "Jitter", "Send: Pack", "Lost", "Jitter");
17146
17147 ao2_t_callback(dialogs, OBJ_NODATA, show_chanstats_cb, &arg, "callback to sip show chanstats");
17148 ast_cli(a->fd, "%d active SIP channel%s\n", arg.numchans, (arg.numchans != 1) ? "s" : "");
17149 return CLI_SUCCESS;
17150 }
17151 #undef FORMAT
17152 #undef FORMAT2
17153
17154
17155 static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17156 {
17157 int realtimepeers;
17158 int realtimeregs;
17159 char codec_buf[SIPBUFSIZE];
17160 const char *msg;
17161
17162 switch (cmd) {
17163 case CLI_INIT:
17164 e->command = "sip show settings";
17165 e->usage =
17166 "Usage: sip show settings\n"
17167 " Provides detailed list of the configuration of the SIP channel.\n";
17168 return NULL;
17169 case CLI_GENERATE:
17170 return NULL;
17171 }
17172
17173
17174 realtimepeers = ast_check_realtime("sippeers");
17175 realtimeregs = ast_check_realtime("sipregs");
17176
17177 if (a->argc != 3)
17178 return CLI_SHOWUSAGE;
17179 ast_cli(a->fd, "\n\nGlobal Settings:\n");
17180 ast_cli(a->fd, "----------------\n");
17181 ast_cli(a->fd, " UDP Bindaddress: %s\n", ast_sockaddr_stringify(&bindaddr));
17182 if (ast_sockaddr_is_ipv6(&bindaddr) && ast_sockaddr_is_any(&bindaddr)) {
17183 ast_cli(a->fd, " ** Additional Info:\n");
17184 ast_cli(a->fd, " [::] may include IPv4 in addition to IPv6, if such a feature is enabled in the OS.\n");
17185 }
17186 ast_cli(a->fd, " TCP SIP Bindaddress: %s\n",
17187 sip_cfg.tcp_enabled != FALSE ?
17188 ast_sockaddr_stringify(&sip_tcp_desc.local_address) :
17189 "Disabled");
17190 ast_cli(a->fd, " TLS SIP Bindaddress: %s\n",
17191 default_tls_cfg.enabled != FALSE ?
17192 ast_sockaddr_stringify(&sip_tls_desc.local_address) :
17193 "Disabled");
17194 ast_cli(a->fd, " Videosupport: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT)));
17195 ast_cli(a->fd, " Textsupport: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT)));
17196 ast_cli(a->fd, " Ignore SDP sess. ver.: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION)));
17197 ast_cli(a->fd, " AutoCreate Peer: %s\n", AST_CLI_YESNO(sip_cfg.autocreatepeer));
17198 ast_cli(a->fd, " Match Auth Username: %s\n", AST_CLI_YESNO(global_match_auth_username));
17199 ast_cli(a->fd, " Allow unknown access: %s\n", AST_CLI_YESNO(sip_cfg.allowguest));
17200 ast_cli(a->fd, " Allow subscriptions: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)));
17201 ast_cli(a->fd, " Allow overlap dialing: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP)));
17202 ast_cli(a->fd, " Allow promsic. redir: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_PROMISCREDIR)));
17203 ast_cli(a->fd, " Enable call counters: %s\n", AST_CLI_YESNO(global_callcounter));
17204 ast_cli(a->fd, " SIP domain support: %s\n", AST_CLI_YESNO(!AST_LIST_EMPTY(&domain_list)));
17205 ast_cli(a->fd, " Realm. auth: %s\n", AST_CLI_YESNO(authl != NULL));
17206 ast_cli(a->fd, " Our auth realm %s\n", sip_cfg.realm);
17207 ast_cli(a->fd, " Use domains as realms: %s\n", AST_CLI_YESNO(sip_cfg.domainsasrealm));
17208 ast_cli(a->fd, " Call to non-local dom.: %s\n", AST_CLI_YESNO(sip_cfg.allow_external_domains));
17209 ast_cli(a->fd, " URI user is phone no: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_USEREQPHONE)));
17210 ast_cli(a->fd, " Always auth rejects: %s\n", AST_CLI_YESNO(sip_cfg.alwaysauthreject));
17211 ast_cli(a->fd, " Direct RTP setup: %s\n", AST_CLI_YESNO(sip_cfg.directrtpsetup));
17212 ast_cli(a->fd, " User Agent: %s\n", global_useragent);
17213 ast_cli(a->fd, " SDP Session Name: %s\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
17214 ast_cli(a->fd, " SDP Owner Name: %s\n", ast_strlen_zero(global_sdpowner) ? "-" : global_sdpowner);
17215 ast_cli(a->fd, " Reg. context: %s\n", S_OR(sip_cfg.regcontext, "(not set)"));
17216 ast_cli(a->fd, " Regexten on Qualify: %s\n", AST_CLI_YESNO(sip_cfg.regextenonqualify));
17217 ast_cli(a->fd, " Caller ID: %s\n", default_callerid);
17218 if ((default_fromdomainport) && (default_fromdomainport != STANDARD_SIP_PORT)) {
17219 ast_cli(a->fd, " From: Domain: %s:%d\n", default_fromdomain, default_fromdomainport);
17220 } else {
17221 ast_cli(a->fd, " From: Domain: %s\n", default_fromdomain);
17222 }
17223 ast_cli(a->fd, " Record SIP history: %s\n", AST_CLI_ONOFF(recordhistory));
17224 ast_cli(a->fd, " Call Events: %s\n", AST_CLI_ONOFF(sip_cfg.callevents));
17225 ast_cli(a->fd, " Auth. Failure Events: %s\n", AST_CLI_ONOFF(global_authfailureevents));
17226
17227 ast_cli(a->fd, " T.38 support: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT)));
17228 ast_cli(a->fd, " T.38 EC mode: %s\n", faxec2str(ast_test_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT)));
17229 ast_cli(a->fd, " T.38 MaxDtgrm: %d\n", global_t38_maxdatagram);
17230 if (!realtimepeers && !realtimeregs)
17231 ast_cli(a->fd, " SIP realtime: Disabled\n" );
17232 else
17233 ast_cli(a->fd, " SIP realtime: Enabled\n" );
17234 ast_cli(a->fd, " Qualify Freq : %d ms\n", global_qualifyfreq);
17235 ast_cli(a->fd, " Q.850 Reason header: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_Q850_REASON)));
17236 ast_cli(a->fd, "\nNetwork QoS Settings:\n");
17237 ast_cli(a->fd, "---------------------------\n");
17238 ast_cli(a->fd, " IP ToS SIP: %s\n", ast_tos2str(global_tos_sip));
17239 ast_cli(a->fd, " IP ToS RTP audio: %s\n", ast_tos2str(global_tos_audio));
17240 ast_cli(a->fd, " IP ToS RTP video: %s\n", ast_tos2str(global_tos_video));
17241 ast_cli(a->fd, " IP ToS RTP text: %s\n", ast_tos2str(global_tos_text));
17242 ast_cli(a->fd, " 802.1p CoS SIP: %d\n", global_cos_sip);
17243 ast_cli(a->fd, " 802.1p CoS RTP audio: %d\n", global_cos_audio);
17244 ast_cli(a->fd, " 802.1p CoS RTP video: %d\n", global_cos_video);
17245 ast_cli(a->fd, " 802.1p CoS RTP text: %d\n", global_cos_text);
17246 ast_cli(a->fd, " Jitterbuffer enabled: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_ENABLED)));
17247 ast_cli(a->fd, " Jitterbuffer forced: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_FORCED)));
17248 ast_cli(a->fd, " Jitterbuffer max size: %ld\n", global_jbconf.max_size);
17249 ast_cli(a->fd, " Jitterbuffer resync: %ld\n", global_jbconf.resync_threshold);
17250 ast_cli(a->fd, " Jitterbuffer impl: %s\n", global_jbconf.impl);
17251 ast_cli(a->fd, " Jitterbuffer log: %s\n", AST_CLI_YESNO(ast_test_flag(&global_jbconf, AST_JB_LOG)));
17252
17253 ast_cli(a->fd, "\nNetwork Settings:\n");
17254 ast_cli(a->fd, "---------------------------\n");
17255
17256 if (localaddr == NULL)
17257 msg = "Disabled, no localnet list";
17258 else if (ast_sockaddr_isnull(&externaddr))
17259 msg = "Disabled";
17260 else if (!ast_strlen_zero(externhost))
17261 msg = "Enabled using externhost";
17262 else
17263 msg = "Enabled using externaddr";
17264 ast_cli(a->fd, " SIP address remapping: %s\n", msg);
17265 ast_cli(a->fd, " Externhost: %s\n", S_OR(externhost, "<none>"));
17266 ast_cli(a->fd, " externaddr: %s\n", ast_sockaddr_stringify(&externaddr));
17267 ast_cli(a->fd, " Externrefresh: %d\n", externrefresh);
17268 {
17269 struct ast_ha *d;
17270 const char *prefix = "Localnet:";
17271
17272 for (d = localaddr; d ; prefix = "", d = d->next) {
17273 const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&d->addr));
17274 const char *mask = ast_strdupa(ast_sockaddr_stringify_addr(&d->netmask));
17275 ast_cli(a->fd, " %-24s%s/%s\n", prefix, addr, mask);
17276 }
17277 }
17278 ast_cli(a->fd, "\nGlobal Signalling Settings:\n");
17279 ast_cli(a->fd, "---------------------------\n");
17280 ast_cli(a->fd, " Codecs: ");
17281 ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, sip_cfg.capability);
17282 ast_cli(a->fd, "%s\n", codec_buf);
17283 ast_cli(a->fd, " Codec Order: ");
17284 print_codec_to_cli(a->fd, &default_prefs);
17285 ast_cli(a->fd, "\n");
17286 ast_cli(a->fd, " Relax DTMF: %s\n", AST_CLI_YESNO(global_relaxdtmf));
17287 ast_cli(a->fd, " RFC2833 Compensation: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE)));
17288 ast_cli(a->fd, " Symmetric RTP: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_SYMMETRICRTP)));
17289 ast_cli(a->fd, " Compact SIP headers: %s\n", AST_CLI_YESNO(sip_cfg.compactheaders));
17290 ast_cli(a->fd, " RTP Keepalive: %d %s\n", global_rtpkeepalive, global_rtpkeepalive ? "" : "(Disabled)" );
17291 ast_cli(a->fd, " RTP Timeout: %d %s\n", global_rtptimeout, global_rtptimeout ? "" : "(Disabled)" );
17292 ast_cli(a->fd, " RTP Hold Timeout: %d %s\n", global_rtpholdtimeout, global_rtpholdtimeout ? "" : "(Disabled)");
17293 ast_cli(a->fd, " MWI NOTIFY mime type: %s\n", default_notifymime);
17294 ast_cli(a->fd, " DNS SRV lookup: %s\n", AST_CLI_YESNO(sip_cfg.srvlookup));
17295 ast_cli(a->fd, " Pedantic SIP support: %s\n", AST_CLI_YESNO(sip_cfg.pedanticsipchecking));
17296 ast_cli(a->fd, " Reg. min duration %d secs\n", min_expiry);
17297 ast_cli(a->fd, " Reg. max duration: %d secs\n", max_expiry);
17298 ast_cli(a->fd, " Reg. default duration: %d secs\n", default_expiry);
17299 ast_cli(a->fd, " Outbound reg. timeout: %d secs\n", global_reg_timeout);
17300 ast_cli(a->fd, " Outbound reg. attempts: %d\n", global_regattempts_max);
17301 ast_cli(a->fd, " Notify ringing state: %s\n", AST_CLI_YESNO(sip_cfg.notifyringing));
17302 if (sip_cfg.notifyringing) {
17303 ast_cli(a->fd, " Include CID: %s%s\n",
17304 AST_CLI_YESNO(sip_cfg.notifycid),
17305 sip_cfg.notifycid == IGNORE_CONTEXT ? " (Ignoring context)" : "");
17306 }
17307 ast_cli(a->fd, " Notify hold state: %s\n", AST_CLI_YESNO(sip_cfg.notifyhold));
17308 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(sip_cfg.allowtransfer));
17309 ast_cli(a->fd, " Max Call Bitrate: %d kbps\n", default_maxcallbitrate);
17310 ast_cli(a->fd, " Auto-Framing: %s\n", AST_CLI_YESNO(global_autoframing));
17311 ast_cli(a->fd, " Outb. proxy: %s %s\n", ast_strlen_zero(sip_cfg.outboundproxy.name) ? "<not set>" : sip_cfg.outboundproxy.name,
17312 sip_cfg.outboundproxy.force ? "(forced)" : "");
17313 ast_cli(a->fd, " Session Timers: %s\n", stmode2str(global_st_mode));
17314 ast_cli(a->fd, " Session Refresher: %s\n", strefresher2str (global_st_refresher));
17315 ast_cli(a->fd, " Session Expires: %d secs\n", global_max_se);
17316 ast_cli(a->fd, " Session Min-SE: %d secs\n", global_min_se);
17317 ast_cli(a->fd, " Timer T1: %d\n", global_t1);
17318 ast_cli(a->fd, " Timer T1 minimum: %d\n", global_t1min);
17319 ast_cli(a->fd, " Timer B: %d\n", global_timer_b);
17320 ast_cli(a->fd, " No premature media: %s\n", AST_CLI_YESNO(global_prematuremediafilter));
17321 ast_cli(a->fd, " Max forwards: %d\n", sip_cfg.default_max_forwards);
17322
17323 ast_cli(a->fd, "\nDefault Settings:\n");
17324 ast_cli(a->fd, "-----------------\n");
17325 ast_cli(a->fd, " Allowed transports: %s\n", get_transport_list(default_transports));
17326 ast_cli(a->fd, " Outbound transport: %s\n", get_transport(default_primary_transport));
17327 ast_cli(a->fd, " Context: %s\n", sip_cfg.default_context);
17328 ast_cli(a->fd, " Force rport: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_NAT_FORCE_RPORT)));
17329 ast_cli(a->fd, " DTMF: %s\n", dtmfmode2str(ast_test_flag(&global_flags[0], SIP_DTMF)));
17330 ast_cli(a->fd, " Qualify: %d\n", default_qualify);
17331 ast_cli(a->fd, " Use ClientCode: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[0], SIP_USECLIENTCODE)));
17332 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)));
17333 ast_cli(a->fd, " Language: %s\n", default_language);
17334 ast_cli(a->fd, " MOH Interpret: %s\n", default_mohinterpret);
17335 ast_cli(a->fd, " MOH Suggest: %s\n", default_mohsuggest);
17336 ast_cli(a->fd, " Voice Mail Extension: %s\n", default_vmexten);
17337
17338
17339 if (realtimepeers || realtimeregs) {
17340 ast_cli(a->fd, "\nRealtime SIP Settings:\n");
17341 ast_cli(a->fd, "----------------------\n");
17342 ast_cli(a->fd, " Realtime Peers: %s\n", AST_CLI_YESNO(realtimepeers));
17343 ast_cli(a->fd, " Realtime Regs: %s\n", AST_CLI_YESNO(realtimeregs));
17344 ast_cli(a->fd, " Cache Friends: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)));
17345 ast_cli(a->fd, " Update: %s\n", AST_CLI_YESNO(sip_cfg.peer_rtupdate));
17346 ast_cli(a->fd, " Ignore Reg. Expire: %s\n", AST_CLI_YESNO(sip_cfg.ignore_regexpire));
17347 ast_cli(a->fd, " Save sys. name: %s\n", AST_CLI_YESNO(sip_cfg.rtsave_sysname));
17348 ast_cli(a->fd, " Auto Clear: %d (%s)\n", sip_cfg.rtautoclear, ast_test_flag(&global_flags[1], SIP_PAGE2_RTAUTOCLEAR) ? "Enabled" : "Disabled");
17349 }
17350 ast_cli(a->fd, "\n----\n");
17351 return CLI_SUCCESS;
17352 }
17353
17354 static char *sip_show_mwi(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17355 {
17356 #define FORMAT "%-30.30s %-12.12s %-10.10s %-10.10s\n"
17357 char host[80];
17358
17359 switch (cmd) {
17360 case CLI_INIT:
17361 e->command = "sip show mwi";
17362 e->usage =
17363 "Usage: sip show mwi\n"
17364 " Provides a list of MWI subscriptions and status.\n";
17365 return NULL;
17366 case CLI_GENERATE:
17367 return NULL;
17368 }
17369
17370 ast_cli(a->fd, FORMAT, "Host", "Username", "Mailbox", "Subscribed");
17371
17372 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
17373 ASTOBJ_RDLOCK(iterator);
17374 snprintf(host, sizeof(host), "%s:%d", iterator->hostname, iterator->portno ? iterator->portno : STANDARD_SIP_PORT);
17375 ast_cli(a->fd, FORMAT, host, iterator->username, iterator->mailbox, AST_CLI_YESNO(iterator->subscribed));
17376 ASTOBJ_UNLOCK(iterator);
17377 } while(0));
17378
17379 return CLI_SUCCESS;
17380 #undef FORMAT
17381 }
17382
17383
17384
17385 static const char *subscription_type2str(enum subscriptiontype subtype)
17386 {
17387 int i;
17388
17389 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
17390 if (subscription_types[i].type == subtype) {
17391 return subscription_types[i].text;
17392 }
17393 }
17394 return subscription_types[0].text;
17395 }
17396
17397
17398 static const struct cfsubscription_types *find_subscription_type(enum subscriptiontype subtype)
17399 {
17400 int i;
17401
17402 for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
17403 if (subscription_types[i].type == subtype) {
17404 return &subscription_types[i];
17405 }
17406 }
17407 return &subscription_types[0];
17408 }
17409
17410
17411
17412
17413
17414
17415
17416
17417 #define FORMAT4 "%-15.15s %-15.15s %-15.15s %-15.15s %-13.13s %-15.15s %-10.10s %-6.6d\n"
17418 #define FORMAT3 "%-15.15s %-15.15s %-15.15s %-15.15s %-13.13s %-15.15s %-10.10s %-6.6s\n"
17419 #define FORMAT2 "%-15.15s %-15.15s %-15.15s %-15.15s %-7.7s %-15.15s %-10.10s %-10.10s\n"
17420 #define FORMAT "%-15.15s %-15.15s %-15.15s %-15.15s %-3.3s %-3.3s %-15.15s %-10.10s %-10.10s\n"
17421
17422
17423 static int show_channels_cb(void *__cur, void *__arg, int flags)
17424 {
17425 struct sip_pvt *cur = __cur;
17426 struct __show_chan_arg *arg = __arg;
17427 const struct ast_sockaddr *dst = sip_real_dst(cur);
17428
17429
17430 if (cur->subscribed == NONE && !arg->subscriptions) {
17431
17432 const char *referstatus = cur->refer ? referstatus2str(cur->refer->status) : "";
17433 char formatbuf[SIPBUFSIZE/2];
17434
17435 ast_cli(arg->fd, FORMAT, ast_sockaddr_stringify_addr(dst),
17436 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
17437 cur->callid,
17438 ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
17439 AST_CLI_YESNO(ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD)),
17440 cur->needdestroy ? "(d)" : "",
17441 cur->lastmsg ,
17442 referstatus,
17443 cur->relatedpeer ? cur->relatedpeer->name : "<guest>"
17444 );
17445 arg->numchans++;
17446 }
17447 if (cur->subscribed != NONE && arg->subscriptions) {
17448 struct ast_str *mailbox_str = ast_str_alloca(512);
17449 if (cur->subscribed == MWI_NOTIFICATION && cur->relatedpeer)
17450 peer_mailboxes_to_str(&mailbox_str, cur->relatedpeer);
17451 ast_cli(arg->fd, FORMAT4, ast_sockaddr_stringify_addr(dst),
17452 S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
17453 cur->callid,
17454
17455 cur->subscribed == MWI_NOTIFICATION ? "--" : cur->subscribeuri,
17456 cur->subscribed == MWI_NOTIFICATION ? "<none>" : ast_extension_state2str(cur->laststate),
17457 subscription_type2str(cur->subscribed),
17458 cur->subscribed == MWI_NOTIFICATION ? S_OR(mailbox_str->str, "<none>") : "<none>",
17459 cur->expiry
17460 );
17461 arg->numchans++;
17462 }
17463 return 0;
17464 }
17465
17466
17467
17468
17469
17470
17471 static char *sip_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17472 {
17473 struct __show_chan_arg arg = { .fd = a->fd, .numchans = 0 };
17474
17475
17476 if (cmd == CLI_INIT) {
17477 e->command = "sip show {channels|subscriptions}";
17478 e->usage =
17479 "Usage: sip show channels\n"
17480 " Lists all currently active SIP calls (dialogs).\n"
17481 "Usage: sip show subscriptions\n"
17482 " Lists active SIP subscriptions.\n";
17483 return NULL;
17484 } else if (cmd == CLI_GENERATE)
17485 return NULL;
17486
17487 if (a->argc != e->args)
17488 return CLI_SHOWUSAGE;
17489 arg.subscriptions = !strcasecmp(a->argv[e->args - 1], "subscriptions");
17490 if (!arg.subscriptions)
17491 ast_cli(arg.fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Format", "Hold", "Last Message", "Expiry", "Peer");
17492 else
17493 ast_cli(arg.fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox", "Expiry");
17494
17495
17496 ao2_t_callback(dialogs, OBJ_NODATA, show_channels_cb, &arg, "callback to show channels");
17497
17498
17499 ast_cli(arg.fd, "%d active SIP %s%s\n", arg.numchans,
17500 (arg.subscriptions ? "subscription" : "dialog"),
17501 ESS(arg.numchans));
17502 return CLI_SUCCESS;
17503 #undef FORMAT
17504 #undef FORMAT2
17505 #undef FORMAT3
17506 }
17507
17508
17509
17510
17511
17512
17513 static char *complete_sipch(const char *line, const char *word, int pos, int state)
17514 {
17515 int which=0;
17516 struct sip_pvt *cur;
17517 char *c = NULL;
17518 int wordlen = strlen(word);
17519 struct ao2_iterator i;
17520
17521 if (pos != 3) {
17522 return NULL;
17523 }
17524
17525 i = ao2_iterator_init(dialogs, 0);
17526 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
17527 sip_pvt_lock(cur);
17528 if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
17529 c = ast_strdup(cur->callid);
17530 sip_pvt_unlock(cur);
17531 dialog_unref(cur, "drop ref in iterator loop break");
17532 break;
17533 }
17534 sip_pvt_unlock(cur);
17535 dialog_unref(cur, "drop ref in iterator loop");
17536 }
17537 ao2_iterator_destroy(&i);
17538 return c;
17539 }
17540
17541
17542
17543 static char *complete_sip_peer(const char *word, int state, int flags2)
17544 {
17545 char *result = NULL;
17546 int wordlen = strlen(word);
17547 int which = 0;
17548 struct ao2_iterator i = ao2_iterator_init(peers, 0);
17549 struct sip_peer *peer;
17550
17551 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
17552
17553 if (!strncasecmp(word, peer->name, wordlen) &&
17554 (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
17555 ++which > state)
17556 result = ast_strdup(peer->name);
17557 unref_peer(peer, "toss iterator peer ptr before break");
17558 if (result) {
17559 break;
17560 }
17561 }
17562 ao2_iterator_destroy(&i);
17563 return result;
17564 }
17565
17566
17567 static char *complete_sip_registered_peer(const char *word, int state, int flags2)
17568 {
17569 char *result = NULL;
17570 int wordlen = strlen(word);
17571 int which = 0;
17572 struct ao2_iterator i;
17573 struct sip_peer *peer;
17574
17575 i = ao2_iterator_init(peers, 0);
17576 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
17577 if (!strncasecmp(word, peer->name, wordlen) &&
17578 (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
17579 ++which > state && peer->expire > 0)
17580 result = ast_strdup(peer->name);
17581 if (result) {
17582 unref_peer(peer, "toss iterator peer ptr before break");
17583 break;
17584 }
17585 unref_peer(peer, "toss iterator peer ptr");
17586 }
17587 ao2_iterator_destroy(&i);
17588 return result;
17589 }
17590
17591
17592 static char *complete_sip_show_history(const char *line, const char *word, int pos, int state)
17593 {
17594 if (pos == 3)
17595 return complete_sipch(line, word, pos, state);
17596
17597 return NULL;
17598 }
17599
17600
17601 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state)
17602 {
17603 if (pos == 3) {
17604 return complete_sip_peer(word, state, 0);
17605 }
17606
17607 return NULL;
17608 }
17609
17610
17611 static char *complete_sip_unregister(const char *line, const char *word, int pos, int state)
17612 {
17613 if (pos == 2)
17614 return complete_sip_registered_peer(word, state, 0);
17615
17616 return NULL;
17617 }
17618
17619
17620 static char *complete_sipnotify(const char *line, const char *word, int pos, int state)
17621 {
17622 char *c = NULL;
17623
17624 if (pos == 2) {
17625 int which = 0;
17626 char *cat = NULL;
17627 int wordlen = strlen(word);
17628
17629
17630
17631 if (!notify_types)
17632 return NULL;
17633
17634 while ( (cat = ast_category_browse(notify_types, cat)) ) {
17635 if (!strncasecmp(word, cat, wordlen) && ++which > state) {
17636 c = ast_strdup(cat);
17637 break;
17638 }
17639 }
17640 return c;
17641 }
17642
17643 if (pos > 2)
17644 return complete_sip_peer(word, state, 0);
17645
17646 return NULL;
17647 }
17648
17649
17650 static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17651 {
17652 struct sip_pvt *cur;
17653 size_t len;
17654 int found = 0;
17655 struct ao2_iterator i;
17656
17657 switch (cmd) {
17658 case CLI_INIT:
17659 e->command = "sip show channel";
17660 e->usage =
17661 "Usage: sip show channel <call-id>\n"
17662 " Provides detailed status on a given SIP dialog (identified by SIP call-id).\n";
17663 return NULL;
17664 case CLI_GENERATE:
17665 return complete_sipch(a->line, a->word, a->pos, a->n);
17666 }
17667
17668 if (a->argc != 4)
17669 return CLI_SHOWUSAGE;
17670 len = strlen(a->argv[3]);
17671
17672 i = ao2_iterator_init(dialogs, 0);
17673 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
17674 sip_pvt_lock(cur);
17675
17676 if (!strncasecmp(cur->callid, a->argv[3], len)) {
17677 char formatbuf[SIPBUFSIZE/2];
17678 ast_cli(a->fd, "\n");
17679 if (cur->subscribed != NONE)
17680 ast_cli(a->fd, " * Subscription (type: %s)\n", subscription_type2str(cur->subscribed));
17681 else
17682 ast_cli(a->fd, " * SIP Call\n");
17683 ast_cli(a->fd, " Curr. trans. direction: %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
17684 ast_cli(a->fd, " Call-ID: %s\n", cur->callid);
17685 ast_cli(a->fd, " Owner channel ID: %s\n", cur->owner ? cur->owner->name : "<none>");
17686 ast_cli(a->fd, " Our Codec Capability: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->capability));
17687 ast_cli(a->fd, " Non-Codec Capability (DTMF): %d\n", cur->noncodeccapability);
17688 ast_cli(a->fd, " Their Codec Capability: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->peercapability));
17689 ast_cli(a->fd, " Joint Codec Capability: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->jointcapability));
17690 ast_cli(a->fd, " Format: %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
17691 ast_cli(a->fd, " T.38 support %s\n", AST_CLI_YESNO(cur->udptl != NULL));
17692 ast_cli(a->fd, " Video support %s\n", AST_CLI_YESNO(cur->vrtp != NULL));
17693 ast_cli(a->fd, " MaxCallBR: %d kbps\n", cur->maxcallbitrate);
17694 ast_cli(a->fd, " Theoretical Address: %s\n", ast_sockaddr_stringify(&cur->sa));
17695 ast_cli(a->fd, " Received Address: %s\n", ast_sockaddr_stringify(&cur->recv));
17696 ast_cli(a->fd, " SIP Transfer mode: %s\n", transfermode2str(cur->allowtransfer));
17697 ast_cli(a->fd, " Force rport: %s\n", AST_CLI_YESNO(ast_test_flag(&cur->flags[0], SIP_NAT_FORCE_RPORT)));
17698 if (ast_sockaddr_isnull(&cur->redirip)) {
17699 ast_cli(a->fd,
17700 " Audio IP: %s (local)\n",
17701 ast_sockaddr_stringify_addr(&cur->ourip));
17702 } else {
17703 ast_cli(a->fd,
17704 " Audio IP: %s (Outside bridge)\n",
17705 ast_sockaddr_stringify_addr(&cur->redirip));
17706 }
17707 ast_cli(a->fd, " Our Tag: %s\n", cur->tag);
17708 ast_cli(a->fd, " Their Tag: %s\n", cur->theirtag);
17709 ast_cli(a->fd, " SIP User agent: %s\n", cur->useragent);
17710 if (!ast_strlen_zero(cur->username))
17711 ast_cli(a->fd, " Username: %s\n", cur->username);
17712 if (!ast_strlen_zero(cur->peername))
17713 ast_cli(a->fd, " Peername: %s\n", cur->peername);
17714 if (!ast_strlen_zero(cur->uri))
17715 ast_cli(a->fd, " Original uri: %s\n", cur->uri);
17716 if (!ast_strlen_zero(cur->cid_num))
17717 ast_cli(a->fd, " Caller-ID: %s\n", cur->cid_num);
17718 ast_cli(a->fd, " Need Destroy: %s\n", AST_CLI_YESNO(cur->needdestroy));
17719 ast_cli(a->fd, " Last Message: %s\n", cur->lastmsg);
17720 ast_cli(a->fd, " Promiscuous Redir: %s\n", AST_CLI_YESNO(ast_test_flag(&cur->flags[0], SIP_PROMISCREDIR)));
17721 ast_cli(a->fd, " Route: %s\n", cur->route ? cur->route->hop : "N/A");
17722 ast_cli(a->fd, " DTMF Mode: %s\n", dtmfmode2str(ast_test_flag(&cur->flags[0], SIP_DTMF)));
17723 ast_cli(a->fd, " SIP Options: ");
17724 if (cur->sipoptions) {
17725 int x;
17726 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
17727 if (cur->sipoptions & sip_options[x].id)
17728 ast_cli(a->fd, "%s ", sip_options[x].text);
17729 }
17730 ast_cli(a->fd, "\n");
17731 } else
17732 ast_cli(a->fd, "(none)\n");
17733
17734 if (!cur->stimer)
17735 ast_cli(a->fd, " Session-Timer: Uninitiallized\n");
17736 else {
17737 ast_cli(a->fd, " Session-Timer: %s\n", cur->stimer->st_active ? "Active" : "Inactive");
17738 if (cur->stimer->st_active == TRUE) {
17739 ast_cli(a->fd, " S-Timer Interval: %d\n", cur->stimer->st_interval);
17740 ast_cli(a->fd, " S-Timer Refresher: %s\n", strefresher2str(cur->stimer->st_ref));
17741 ast_cli(a->fd, " S-Timer Expirys: %d\n", cur->stimer->st_expirys);
17742 ast_cli(a->fd, " S-Timer Sched Id: %d\n", cur->stimer->st_schedid);
17743 ast_cli(a->fd, " S-Timer Peer Sts: %s\n", cur->stimer->st_active_peer_ua ? "Active" : "Inactive");
17744 ast_cli(a->fd, " S-Timer Cached Min-SE: %d\n", cur->stimer->st_cached_min_se);
17745 ast_cli(a->fd, " S-Timer Cached SE: %d\n", cur->stimer->st_cached_max_se);
17746 ast_cli(a->fd, " S-Timer Cached Ref: %s\n", strefresher2str(cur->stimer->st_cached_ref));
17747 ast_cli(a->fd, " S-Timer Cached Mode: %s\n", stmode2str(cur->stimer->st_cached_mode));
17748 }
17749 }
17750
17751 ast_cli(a->fd, "\n\n");
17752
17753 found++;
17754 }
17755
17756 sip_pvt_unlock(cur);
17757
17758 ao2_t_ref(cur, -1, "toss dialog ptr set by iterator_next");
17759 }
17760 ao2_iterator_destroy(&i);
17761
17762 if (!found)
17763 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
17764
17765 return CLI_SUCCESS;
17766 }
17767
17768
17769 static char *sip_show_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17770 {
17771 struct sip_pvt *cur;
17772 size_t len;
17773 int found = 0;
17774 struct ao2_iterator i;
17775
17776 switch (cmd) {
17777 case CLI_INIT:
17778 e->command = "sip show history";
17779 e->usage =
17780 "Usage: sip show history <call-id>\n"
17781 " Provides detailed dialog history on a given SIP call (specified by call-id).\n";
17782 return NULL;
17783 case CLI_GENERATE:
17784 return complete_sip_show_history(a->line, a->word, a->pos, a->n);
17785 }
17786
17787 if (a->argc != 4)
17788 return CLI_SHOWUSAGE;
17789
17790 if (!recordhistory)
17791 ast_cli(a->fd, "\n***Note: History recording is currently DISABLED. Use 'sip set history on' to ENABLE.\n");
17792
17793 len = strlen(a->argv[3]);
17794
17795 i = ao2_iterator_init(dialogs, 0);
17796 while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
17797 sip_pvt_lock(cur);
17798 if (!strncasecmp(cur->callid, a->argv[3], len)) {
17799 struct sip_history *hist;
17800 int x = 0;
17801
17802 ast_cli(a->fd, "\n");
17803 if (cur->subscribed != NONE)
17804 ast_cli(a->fd, " * Subscription\n");
17805 else
17806 ast_cli(a->fd, " * SIP Call\n");
17807 if (cur->history)
17808 AST_LIST_TRAVERSE(cur->history, hist, list)
17809 ast_cli(a->fd, "%d. %s\n", ++x, hist->event);
17810 if (x == 0)
17811 ast_cli(a->fd, "Call '%s' has no history\n", cur->callid);
17812 found++;
17813 }
17814 sip_pvt_unlock(cur);
17815 ao2_t_ref(cur, -1, "toss dialog ptr from iterator_next");
17816 }
17817 ao2_iterator_destroy(&i);
17818
17819 if (!found)
17820 ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
17821
17822 return CLI_SUCCESS;
17823 }
17824
17825
17826 static void sip_dump_history(struct sip_pvt *dialog)
17827 {
17828 int x = 0;
17829 struct sip_history *hist;
17830 static int errmsg = 0;
17831
17832 if (!dialog)
17833 return;
17834
17835 if (!option_debug && !sipdebug) {
17836 if (!errmsg) {
17837 ast_log(LOG_NOTICE, "You must have debugging enabled (SIP or Asterisk) in order to dump SIP history.\n");
17838 errmsg = 1;
17839 }
17840 return;
17841 }
17842
17843 ast_debug(1, "\n---------- SIP HISTORY for '%s' \n", dialog->callid);
17844 if (dialog->subscribed)
17845 ast_debug(1, " * Subscription\n");
17846 else
17847 ast_debug(1, " * SIP Call\n");
17848 if (dialog->history)
17849 AST_LIST_TRAVERSE(dialog->history, hist, list)
17850 ast_debug(1, " %-3.3d. %s\n", ++x, hist->event);
17851 if (!x)
17852 ast_debug(1, "Call '%s' has no history\n", dialog->callid);
17853 ast_debug(1, "\n---------- END SIP HISTORY for '%s' \n", dialog->callid);
17854 }
17855
17856
17857
17858 static void handle_request_info(struct sip_pvt *p, struct sip_request *req)
17859 {
17860 char buf[1024];
17861 unsigned int event;
17862 const char *c = get_header(req, "Content-Type");
17863
17864
17865 if (!strcasecmp(c, "application/dtmf-relay") ||
17866 !strcasecmp(c, "application/vnd.nortelnetworks.digits")) {
17867 unsigned int duration = 0;
17868
17869 if (!p->owner) {
17870 transmit_response(p, "481 Call leg/transaction does not exist", req);
17871 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17872 return;
17873 }
17874
17875
17876 if (ast_strlen_zero(c = get_body(req, "Signal", '=')) && ast_strlen_zero(c = get_body(req, "d", '='))) {
17877 ast_log(LOG_WARNING, "Unable to retrieve DTMF signal from INFO message from %s\n", p->callid);
17878 transmit_response(p, "200 OK", req);
17879 return;
17880 } else {
17881 ast_copy_string(buf, c, sizeof(buf));
17882 }
17883
17884 if (!ast_strlen_zero((c = get_body(req, "Duration", '='))))
17885 duration = atoi(c);
17886 if (!duration)
17887 duration = 100;
17888
17889
17890 if (ast_strlen_zero(buf)) {
17891 transmit_response(p, "200 OK", req);
17892 return;
17893 }
17894
17895 if (buf[0] == '*')
17896 event = 10;
17897 else if (buf[0] == '#')
17898 event = 11;
17899 else if ((buf[0] >= 'A') && (buf[0] <= 'D'))
17900 event = 12 + buf[0] - 'A';
17901 else if (buf[0] == '!')
17902 event = 16;
17903 else
17904 event = atoi(buf);
17905 if (event == 16) {
17906
17907 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
17908 ast_queue_frame(p->owner, &f);
17909 if (sipdebug)
17910 ast_verbose("* DTMF-relay event received: FLASH\n");
17911 } else {
17912
17913 struct ast_frame f = { AST_FRAME_DTMF, };
17914 if (event < 10) {
17915 f.subclass.integer = '0' + event;
17916 } else if (event == 10) {
17917 f.subclass.integer = '*';
17918 } else if (event == 11) {
17919 f.subclass.integer = '#';
17920 } else if (event < 16) {
17921 f.subclass.integer = 'A' + (event - 12);
17922 }
17923 f.len = duration;
17924 ast_queue_frame(p->owner, &f);
17925 if (sipdebug)
17926 ast_verbose("* DTMF-relay event received: %c\n", (int) f.subclass.integer);
17927 }
17928 transmit_response(p, "200 OK", req);
17929 return;
17930 } else if (!strcasecmp(c, "application/dtmf")) {
17931
17932 unsigned int duration = 0;
17933
17934 if (!p->owner) {
17935 transmit_response(p, "481 Call leg/transaction does not exist", req);
17936 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
17937 return;
17938 }
17939
17940 get_msg_text(buf, sizeof(buf), req, TRUE);
17941 duration = 100;
17942
17943 if (ast_strlen_zero(buf)) {
17944 transmit_response(p, "200 OK", req);
17945 return;
17946 }
17947 event = atoi(buf);
17948 if (event == 16) {
17949
17950 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH }, };
17951 ast_queue_frame(p->owner, &f);
17952 if (sipdebug)
17953 ast_verbose("* DTMF-relay event received: FLASH\n");
17954 } else {
17955
17956 struct ast_frame f = { AST_FRAME_DTMF, };
17957 if (event < 10) {
17958 f.subclass.integer = '0' + event;
17959 } else if (event == 10) {
17960 f.subclass.integer = '*';
17961 } else if (event == 11) {
17962 f.subclass.integer = '#';
17963 } else if (event < 16) {
17964 f.subclass.integer = 'A' + (event - 12);
17965 }
17966 f.len = duration;
17967 ast_queue_frame(p->owner, &f);
17968 if (sipdebug)
17969 ast_verbose("* DTMF-relay event received: %c\n", (int) f.subclass.integer);
17970 }
17971 transmit_response(p, "200 OK", req);
17972 return;
17973
17974 } else if (!strcasecmp(c, "application/media_control+xml")) {
17975
17976 if (p->owner)
17977 ast_queue_control(p->owner, AST_CONTROL_VIDUPDATE);
17978 transmit_response(p, "200 OK", req);
17979 return;
17980 } else if (!ast_strlen_zero(c = get_header(req, "X-ClientCode"))) {
17981
17982 if (ast_test_flag(&p->flags[0], SIP_USECLIENTCODE)) {
17983 if (p->owner && p->owner->cdr)
17984 ast_cdr_setuserfield(p->owner, c);
17985 if (p->owner && ast_bridged_channel(p->owner) && ast_bridged_channel(p->owner)->cdr)
17986 ast_cdr_setuserfield(ast_bridged_channel(p->owner), c);
17987 transmit_response(p, "200 OK", req);
17988 } else {
17989 transmit_response(p, "403 Forbidden", req);
17990 }
17991 return;
17992 } else if (!ast_strlen_zero(c = get_header(req, "Record"))) {
17993
17994
17995
17996
17997
17998
17999
18000 struct ast_call_feature *feat;
18001 int j;
18002 struct ast_frame f = { AST_FRAME_DTMF, };
18003
18004 ast_rdlock_call_features();
18005 feat = ast_find_call_feature("automon");
18006 if (!feat || ast_strlen_zero(feat->exten)) {
18007 ast_log(LOG_WARNING, "Recording requested, but no One Touch Monitor registered. (See features.conf)\n");
18008
18009 transmit_response(p, "403 Forbidden", req);
18010 ast_unlock_call_features();
18011 return;
18012 }
18013
18014 f.len = 100;
18015 for (j=0; j < strlen(feat->exten); j++) {
18016 f.subclass.integer = feat->exten[j];
18017 ast_queue_frame(p->owner, &f);
18018 if (sipdebug)
18019 ast_verbose("* DTMF-relay event faked: %c\n", f.subclass.integer);
18020 }
18021 ast_unlock_call_features();
18022
18023 ast_debug(1, "Got a Request to Record the channel, state %s\n", c);
18024 transmit_response(p, "200 OK", req);
18025 return;
18026 } else if (ast_strlen_zero(c = get_header(req, "Content-Length")) || !strcasecmp(c, "0")) {
18027
18028 transmit_response(p, "200 OK", req);
18029 return;
18030 }
18031
18032
18033
18034
18035 ast_log(LOG_WARNING, "Unable to parse INFO message from %s. Content %s\n", p->callid, buf);
18036 transmit_response(p, "415 Unsupported media type", req);
18037 return;
18038 }
18039
18040
18041 static char *sip_do_debug_ip(int fd, const char *arg)
18042 {
18043 if (ast_sockaddr_resolve_first(&debugaddr, arg, 0)) {
18044 return CLI_SHOWUSAGE;
18045 }
18046
18047 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_sockaddr_stringify_addr(&debugaddr));
18048 sipdebug |= sip_debug_console;
18049
18050 return CLI_SUCCESS;
18051 }
18052
18053
18054 static char *sip_do_debug_peer(int fd, const char *arg)
18055 {
18056 struct sip_peer *peer = find_peer(arg, NULL, TRUE, FINDPEERS, FALSE, 0);
18057 if (!peer)
18058 ast_cli(fd, "No such peer '%s'\n", arg);
18059 else if (ast_sockaddr_isnull(&peer->addr))
18060 ast_cli(fd, "Unable to get IP address of peer '%s'\n", arg);
18061 else {
18062 ast_sockaddr_copy(&debugaddr, &peer->addr);
18063 ast_cli(fd, "SIP Debugging Enabled for IP: %s\n", ast_sockaddr_stringify_addr(&debugaddr));
18064 sipdebug |= sip_debug_console;
18065 }
18066 if (peer)
18067 unref_peer(peer, "sip_do_debug_peer: unref_peer, from find_peer call");
18068 return CLI_SUCCESS;
18069 }
18070
18071
18072 static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18073 {
18074 int oldsipdebug = sipdebug & sip_debug_console;
18075 const char *what;
18076
18077 if (cmd == CLI_INIT) {
18078 e->command = "sip set debug {on|off|ip|peer}";
18079 e->usage =
18080 "Usage: sip set debug {off|on|ip addr[:port]|peer peername}\n"
18081 " Globally disables dumping of SIP packets,\n"
18082 " or enables it either globally or for a (single)\n"
18083 " IP address or registered peer.\n";
18084 return NULL;
18085 } else if (cmd == CLI_GENERATE) {
18086 if (a->pos == 4 && !strcasecmp(a->argv[3], "peer"))
18087 return complete_sip_peer(a->word, a->n, 0);
18088 return NULL;
18089 }
18090
18091 what = a->argv[e->args-1];
18092 if (a->argc == e->args) {
18093 if (!strcasecmp(what, "on")) {
18094 sipdebug |= sip_debug_console;
18095 sipdebug_text = 1;
18096 memset(&debugaddr, 0, sizeof(debugaddr));
18097 ast_cli(a->fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
18098 return CLI_SUCCESS;
18099 } else if (!strcasecmp(what, "off")) {
18100 sipdebug &= ~sip_debug_console;
18101 sipdebug_text = 0;
18102 ast_cli(a->fd, "SIP Debugging Disabled\n");
18103 return CLI_SUCCESS;
18104 }
18105 } else if (a->argc == e->args +1) {
18106 if (!strcasecmp(what, "ip"))
18107 return sip_do_debug_ip(a->fd, a->argv[e->args]);
18108 else if (!strcasecmp(what, "peer"))
18109 return sip_do_debug_peer(a->fd, a->argv[e->args]);
18110 }
18111 return CLI_SHOWUSAGE;
18112 }
18113
18114
18115 static char *sip_cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18116 {
18117 struct ast_variable *varlist;
18118 int i;
18119
18120 switch (cmd) {
18121 case CLI_INIT:
18122 e->command = "sip notify";
18123 e->usage =
18124 "Usage: sip notify <type> <peer> [<peer>...]\n"
18125 " Send a NOTIFY message to a SIP peer or peers\n"
18126 " Message types are defined in sip_notify.conf\n";
18127 return NULL;
18128 case CLI_GENERATE:
18129 return complete_sipnotify(a->line, a->word, a->pos, a->n);
18130 }
18131
18132 if (a->argc < 4)
18133 return CLI_SHOWUSAGE;
18134
18135 if (!notify_types) {
18136 ast_cli(a->fd, "No %s file found, or no types listed there\n", notify_config);
18137 return CLI_FAILURE;
18138 }
18139
18140 varlist = ast_variable_browse(notify_types, a->argv[2]);
18141
18142 if (!varlist) {
18143 ast_cli(a->fd, "Unable to find notify type '%s'\n", a->argv[2]);
18144 return CLI_FAILURE;
18145 }
18146
18147 for (i = 3; i < a->argc; i++) {
18148 struct sip_pvt *p;
18149 char buf[512];
18150 struct ast_variable *header, *var;
18151
18152 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
18153 ast_log(LOG_WARNING, "Unable to build sip pvt data for notify (memory/socket error)\n");
18154 return CLI_FAILURE;
18155 }
18156
18157 if (create_addr(p, a->argv[i], NULL, 1, NULL)) {
18158
18159 dialog_unlink_all(p, TRUE, TRUE);
18160 dialog_unref(p, "unref dialog inside for loop" );
18161
18162 ast_cli(a->fd, "Could not create address for '%s'\n", a->argv[i]);
18163 continue;
18164 }
18165
18166
18167 ast_set_flag(&p->flags[0], SIP_OUTGOING);
18168 sip_notify_allocate(p);
18169 p->notify->headers = header = ast_variable_new("Subscription-State", "terminated", "");
18170
18171 for (var = varlist; var; var = var->next) {
18172 ast_copy_string(buf, var->value, sizeof(buf));
18173 ast_unescape_semicolon(buf);
18174
18175 if (!strcasecmp(var->name, "Content")) {
18176 if (ast_str_strlen(p->notify->content))
18177 ast_str_append(&p->notify->content, 0, "\r\n");
18178 ast_str_append(&p->notify->content, 0, "%s", buf);
18179 } else if (!strcasecmp(var->name, "Content-Length")) {
18180 ast_log(LOG_WARNING, "it is not necessary to specify Content-Length in sip_notify.conf, ignoring");
18181 } else {
18182 header->next = ast_variable_new(var->name, buf, "");
18183 header = header->next;
18184 }
18185 }
18186
18187
18188 ast_cli(a->fd, "Sending NOTIFY of type '%s' to '%s'\n", a->argv[2], a->argv[i]);
18189 dialog_ref(p, "bump the count of p, which transmit_sip_request will decrement.");
18190 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
18191 transmit_invite(p, SIP_NOTIFY, 0, 2, NULL);
18192 }
18193
18194 return CLI_SUCCESS;
18195 }
18196
18197
18198 static char *sip_set_history(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
18199 {
18200 switch (cmd) {
18201 case CLI_INIT:
18202 e->command = "sip set history {on|off}";
18203 e->usage =
18204 "Usage: sip set history {on|off}\n"
18205 " Enables/Disables recording of SIP dialog history for debugging purposes.\n"
18206 " Use 'sip show history' to view the history of a call number.\n";
18207 return NULL;
18208 case CLI_GENERATE:
18209 return NULL;
18210 }
18211
18212 if (a->argc != e->args)
18213 return CLI_SHOWUSAGE;
18214
18215 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
18216 recordhistory = TRUE;
18217 ast_cli(a->fd, "SIP History Recording Enabled (use 'sip show history')\n");
18218 } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
18219 recordhistory = FALSE;
18220 ast_cli(a->fd, "SIP History Recording Disabled\n");
18221 } else {
18222 return CLI_SHOWUSAGE;
18223 }
18224 return CLI_SUCCESS;
18225 }
18226
18227
18228 static int do_register_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code)
18229 {
18230 char *header, *respheader;
18231 char digest[1024];
18232
18233 p->authtries++;
18234 auth_headers(code, &header, &respheader);
18235 memset(digest, 0, sizeof(digest));
18236 if (reply_digest(p, req, header, SIP_REGISTER, digest, sizeof(digest))) {
18237
18238
18239 if (sip_debug_test_pvt(p) && p->registry)
18240 ast_verbose("No authentication challenge, sending blank registration to domain/host name %s\n", p->registry->hostname);
18241
18242 return -1;
18243 }
18244 if (p->do_history)
18245 append_history(p, "RegistryAuth", "Try: %d", p->authtries);
18246 if (sip_debug_test_pvt(p) && p->registry)
18247 ast_verbose("Responding to challenge, registration to domain/host name %s\n", p->registry->hostname);
18248 return transmit_register(p->registry, SIP_REGISTER, digest, respheader);
18249 }
18250
18251
18252 static int do_proxy_auth(struct sip_pvt *p, struct sip_request *req, enum sip_auth_type code, int sipmethod, int init)
18253 {
18254 char *header, *respheader;
18255 char digest[1024];
18256
18257 if (!p->options && !(p->options = ast_calloc(1, sizeof(*p->options))))
18258 return -2;
18259
18260 p->authtries++;
18261 auth_headers(code, &header, &respheader);
18262 ast_debug(2, "Auth attempt %d on %s\n", p->authtries, sip_methods[sipmethod].text);
18263 memset(digest, 0, sizeof(digest));
18264 if (reply_digest(p, req, header, sipmethod, digest, sizeof(digest) )) {
18265
18266 return -1;
18267 }
18268
18269 p->options->auth = digest;
18270 p->options->authheader = respheader;
18271 return transmit_invite(p, sipmethod, sipmethod == SIP_INVITE, init, NULL);
18272 }
18273
18274
18275
18276
18277
18278 static int reply_digest(struct sip_pvt *p, struct sip_request *req, char *header, int sipmethod, char *digest, int digest_len)
18279 {
18280 char tmp[512];
18281 char *c;
18282 char oldnonce[256];
18283
18284
18285 const struct x {
18286 const char *key;
18287 const ast_string_field *field;
18288 } *i, keys[] = {
18289 { "realm=", &p->realm },
18290 { "nonce=", &p->nonce },
18291 { "opaque=", &p->opaque },
18292 { "qop=", &p->qop },
18293 { "domain=", &p->domain },
18294 { NULL, 0 },
18295 };
18296
18297 ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
18298 if (ast_strlen_zero(tmp))
18299 return -1;
18300 if (strncasecmp(tmp, "Digest ", strlen("Digest "))) {
18301 ast_log(LOG_WARNING, "missing Digest.\n");
18302 return -1;
18303 }
18304 c = tmp + strlen("Digest ");
18305 ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
18306 while (c && *(c = ast_skip_blanks(c))) {
18307 for (i = keys; i->key != NULL; i++) {
18308 char *src, *separator;
18309 if (strncasecmp(c, i->key, strlen(i->key)) != 0)
18310 continue;
18311
18312 c += strlen(i->key);
18313 if (*c == '"') {
18314 src = ++c;
18315 separator = "\"";
18316 } else {
18317 src = c;
18318 separator = ",";
18319 }
18320 strsep(&c, separator);
18321 ast_string_field_ptr_set(p, i->field, src);
18322 break;
18323 }
18324 if (i->key == NULL)
18325 strsep(&c, ",");
18326 }
18327
18328 if (strcmp(p->nonce, oldnonce))
18329 p->noncecount = 0;
18330
18331
18332 if (p->registry) {
18333 struct sip_registry *r = p->registry;
18334
18335 if (strcmp(r->nonce, p->nonce)) {
18336 ast_string_field_set(r, realm, p->realm);
18337 ast_string_field_set(r, nonce, p->nonce);
18338 ast_string_field_set(r, authdomain, p->domain);
18339 ast_string_field_set(r, opaque, p->opaque);
18340 ast_string_field_set(r, qop, p->qop);
18341 r->noncecount = 0;
18342 }
18343 }
18344 return build_reply_digest(p, sipmethod, digest, digest_len);
18345 }
18346
18347
18348
18349
18350
18351
18352 static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
18353 {
18354 char a1[256];
18355 char a2[256];
18356 char a1_hash[256];
18357 char a2_hash[256];
18358 char resp[256];
18359 char resp_hash[256];
18360 char uri[256];
18361 char opaque[256] = "";
18362 char cnonce[80];
18363 const char *username;
18364 const char *secret;
18365 const char *md5secret;
18366 struct sip_auth *auth = NULL;
18367
18368 if (!ast_strlen_zero(p->domain))
18369 ast_copy_string(uri, p->domain, sizeof(uri));
18370 else if (!ast_strlen_zero(p->uri))
18371 ast_copy_string(uri, p->uri, sizeof(uri));
18372 else
18373 snprintf(uri, sizeof(uri), "sip:%s@%s", p->username, ast_sockaddr_stringify_host(&p->sa));
18374
18375 snprintf(cnonce, sizeof(cnonce), "%08lx", ast_random());
18376
18377
18378 if(!(auth = find_realm_authentication(p->peerauth, p->realm)))
18379 auth = find_realm_authentication(authl, p->realm);
18380
18381 if (auth) {
18382 ast_debug(3, "use realm [%s] from peer [%s][%s]\n", auth->username, p->peername, p->username);
18383 username = auth->username;
18384 secret = auth->secret;
18385 md5secret = auth->md5secret;
18386 if (sipdebug)
18387 ast_debug(1, "Using realm %s authentication for call %s\n", p->realm, p->callid);
18388 } else {
18389
18390 username = p->authname;
18391 secret = p->peersecret;
18392 md5secret = p->peermd5secret;
18393 }
18394 if (ast_strlen_zero(username))
18395 return -1;
18396
18397
18398 snprintf(a1, sizeof(a1), "%s:%s:%s", username, p->realm, secret);
18399 snprintf(a2, sizeof(a2), "%s:%s", sip_methods[method].text, uri);
18400 if (!ast_strlen_zero(md5secret))
18401 ast_copy_string(a1_hash, md5secret, sizeof(a1_hash));
18402 else
18403 ast_md5_hash(a1_hash, a1);
18404 ast_md5_hash(a2_hash, a2);
18405
18406 p->noncecount++;
18407 if (!ast_strlen_zero(p->qop))
18408 snprintf(resp, sizeof(resp), "%s:%s:%08x:%s:%s:%s", a1_hash, p->nonce, p->noncecount, cnonce, "auth", a2_hash);
18409 else
18410 snprintf(resp, sizeof(resp), "%s:%s:%s", a1_hash, p->nonce, a2_hash);
18411 ast_md5_hash(resp_hash, resp);
18412
18413
18414 if (!ast_strlen_zero(p->opaque)) {
18415 snprintf(opaque, sizeof(opaque), ", opaque=\"%s\"", p->opaque);
18416 }
18417
18418
18419 if (!ast_strlen_zero(p->qop))
18420 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, p->noncecount);
18421 else
18422 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);
18423
18424 append_history(p, "AuthResp", "Auth response sent for %s in realm %s - nc %d", username, p->realm, p->noncecount);
18425
18426 return 0;
18427 }
18428
18429
18430 static int func_header_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
18431 {
18432 struct sip_pvt *p;
18433 const char *content = NULL;
18434 AST_DECLARE_APP_ARGS(args,
18435 AST_APP_ARG(header);
18436 AST_APP_ARG(number);
18437 );
18438 int i, number, start = 0;
18439
18440 if (ast_strlen_zero(data)) {
18441 ast_log(LOG_WARNING, "This function requires a header name.\n");
18442 return -1;
18443 }
18444
18445 ast_channel_lock(chan);
18446 if (!IS_SIP_TECH(chan->tech)) {
18447 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
18448 ast_channel_unlock(chan);
18449 return -1;
18450 }
18451
18452 AST_STANDARD_APP_ARGS(args, data);
18453 if (!args.number) {
18454 number = 1;
18455 } else {
18456 sscanf(args.number, "%30d", &number);
18457 if (number < 1)
18458 number = 1;
18459 }
18460
18461 p = chan->tech_pvt;
18462
18463
18464 if (!p) {
18465 ast_channel_unlock(chan);
18466 return -1;
18467 }
18468
18469 for (i = 0; i < number; i++)
18470 content = __get_header(&p->initreq, args.header, &start);
18471
18472 if (ast_strlen_zero(content)) {
18473 ast_channel_unlock(chan);
18474 return -1;
18475 }
18476
18477 ast_copy_string(buf, content, len);
18478 ast_channel_unlock(chan);
18479
18480 return 0;
18481 }
18482
18483 static struct ast_custom_function sip_header_function = {
18484 .name = "SIP_HEADER",
18485 .read = func_header_read,
18486 };
18487
18488
18489 static int func_check_sipdomain(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
18490 {
18491 if (ast_strlen_zero(data)) {
18492 ast_log(LOG_WARNING, "CHECKSIPDOMAIN requires an argument - A domain name\n");
18493 return -1;
18494 }
18495 if (check_sip_domain(data, NULL, 0))
18496 ast_copy_string(buf, data, len);
18497 else
18498 buf[0] = '\0';
18499 return 0;
18500 }
18501
18502 static struct ast_custom_function checksipdomain_function = {
18503 .name = "CHECKSIPDOMAIN",
18504 .read = func_check_sipdomain,
18505 };
18506
18507
18508 static int function_sippeer(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
18509 {
18510 struct sip_peer *peer;
18511 char *colname;
18512
18513 if ((colname = strchr(data, ':'))) {
18514 static int deprecation_warning = 0;
18515 *colname++ = '\0';
18516 if (deprecation_warning++ % 10 == 0)
18517 ast_log(LOG_WARNING, "SIPPEER(): usage of ':' to separate arguments is deprecated. Please use ',' instead.\n");
18518 } else if ((colname = strchr(data, ',')))
18519 *colname++ = '\0';
18520 else
18521 colname = "ip";
18522
18523 if (!(peer = find_peer(data, NULL, TRUE, FINDPEERS, FALSE, 0)))
18524 return -1;
18525
18526 if (!strcasecmp(colname, "ip")) {
18527 ast_copy_string(buf, ast_sockaddr_stringify_addr(&peer->addr), len);
18528 } else if (!strcasecmp(colname, "port")) {
18529 snprintf(buf, len, "%d", ast_sockaddr_port(&peer->addr));
18530 } else if (!strcasecmp(colname, "status")) {
18531 peer_status(peer, buf, len);
18532 } else if (!strcasecmp(colname, "language")) {
18533 ast_copy_string(buf, peer->language, len);
18534 } else if (!strcasecmp(colname, "regexten")) {
18535 ast_copy_string(buf, peer->regexten, len);
18536 } else if (!strcasecmp(colname, "limit")) {
18537 snprintf(buf, len, "%d", peer->call_limit);
18538 } else if (!strcasecmp(colname, "busylevel")) {
18539 snprintf(buf, len, "%d", peer->busy_level);
18540 } else if (!strcasecmp(colname, "curcalls")) {
18541 snprintf(buf, len, "%d", peer->inUse);
18542 } else if (!strcasecmp(colname, "maxforwards")) {
18543 snprintf(buf, len, "%d", peer->maxforwards);
18544 } else if (!strcasecmp(colname, "accountcode")) {
18545 ast_copy_string(buf, peer->accountcode, len);
18546 } else if (!strcasecmp(colname, "callgroup")) {
18547 ast_print_group(buf, len, peer->callgroup);
18548 } else if (!strcasecmp(colname, "pickupgroup")) {
18549 ast_print_group(buf, len, peer->pickupgroup);
18550 } else if (!strcasecmp(colname, "useragent")) {
18551 ast_copy_string(buf, peer->useragent, len);
18552 } else if (!strcasecmp(colname, "mailbox")) {
18553 struct ast_str *mailbox_str = ast_str_alloca(512);
18554 peer_mailboxes_to_str(&mailbox_str, peer);
18555 ast_copy_string(buf, mailbox_str->str, len);
18556 } else if (!strcasecmp(colname, "context")) {
18557 ast_copy_string(buf, peer->context, len);
18558 } else if (!strcasecmp(colname, "expire")) {
18559 snprintf(buf, len, "%d", peer->expire);
18560 } else if (!strcasecmp(colname, "dynamic")) {
18561 ast_copy_string(buf, peer->host_dynamic ? "yes" : "no", len);
18562 } else if (!strcasecmp(colname, "callerid_name")) {
18563 ast_copy_string(buf, peer->cid_name, len);
18564 } else if (!strcasecmp(colname, "callerid_num")) {
18565 ast_copy_string(buf, peer->cid_num, len);
18566 } else if (!strcasecmp(colname, "codecs")) {
18567 ast_getformatname_multiple(buf, len -1, peer->capability);
18568 } else if (!strcasecmp(colname, "encryption")) {
18569 snprintf(buf, len, "%d", ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP));
18570 } else if (!strncasecmp(colname, "chanvar[", 8)) {
18571 char *chanvar=colname + 8;
18572 struct ast_variable *v;
18573
18574 chanvar = strsep(&chanvar, "]");
18575 for (v = peer->chanvars ; v ; v = v->next) {
18576 if (!strcasecmp(v->name, chanvar)) {
18577 ast_copy_string(buf, v->value, len);
18578 }
18579 }
18580 } else if (!strncasecmp(colname, "codec[", 6)) {
18581 char *codecnum;
18582 format_t codec = 0;
18583
18584 codecnum = colname + 6;
18585 codecnum = strsep(&codecnum, "]");
18586 if((codec = ast_codec_pref_index(&peer->prefs, atoi(codecnum)))) {
18587 ast_copy_string(buf, ast_getformatname(codec), len);
18588 } else {
18589 buf[0] = '\0';
18590 }
18591 } else {
18592 buf[0] = '\0';
18593 }
18594
18595 unref_peer(peer, "unref_peer from function_sippeer, just before return");
18596
18597 return 0;
18598 }
18599
18600
18601 static struct ast_custom_function sippeer_function = {
18602 .name = "SIPPEER",
18603 .read = function_sippeer,
18604 };
18605
18606
18607 static int function_sipchaninfo_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
18608 {
18609 struct sip_pvt *p;
18610 static int deprecated = 0;
18611
18612 *buf = 0;
18613
18614 if (!data) {
18615 ast_log(LOG_WARNING, "This function requires a parameter name.\n");
18616 return -1;
18617 }
18618
18619 ast_channel_lock(chan);
18620 if (!IS_SIP_TECH(chan->tech)) {
18621 ast_log(LOG_WARNING, "This function can only be used on SIP channels.\n");
18622 ast_channel_unlock(chan);
18623 return -1;
18624 }
18625
18626 if (deprecated++ % 20 == 0) {
18627
18628 ast_log(LOG_WARNING, "SIPCHANINFO() is deprecated. Please transition to using CHANNEL().\n");
18629 }
18630
18631 p = chan->tech_pvt;
18632
18633
18634 if (!p) {
18635 ast_channel_unlock(chan);
18636 return -1;
18637 }
18638
18639 if (!strcasecmp(data, "peerip")) {
18640 ast_copy_string(buf, ast_sockaddr_stringify_addr(&p->sa), len);
18641 } else if (!strcasecmp(data, "recvip")) {
18642 ast_copy_string(buf, ast_sockaddr_stringify_addr(&p->recv), len);
18643 } else if (!strcasecmp(data, "from")) {
18644 ast_copy_string(buf, p->from, len);
18645 } else if (!strcasecmp(data, "uri")) {
18646 ast_copy_string(buf, p->uri, len);
18647 } else if (!strcasecmp(data, "useragent")) {
18648 ast_copy_string(buf, p->useragent, len);
18649 } else if (!strcasecmp(data, "peername")) {
18650 ast_copy_string(buf, p->peername, len);
18651 } else if (!strcasecmp(data, "t38passthrough")) {
18652 if (p->t38.state == T38_DISABLED) {
18653 ast_copy_string(buf, "0", len);
18654 } else {
18655 ast_copy_string(buf, "1", len);
18656 }
18657 } else {
18658 ast_channel_unlock(chan);
18659 return -1;
18660 }
18661 ast_channel_unlock(chan);
18662
18663 return 0;
18664 }
18665
18666
18667 static struct ast_custom_function sipchaninfo_function = {
18668 .name = "SIPCHANINFO",
18669 .read = function_sipchaninfo_read,
18670 };
18671
18672
18673
18674
18675 static void change_redirecting_information(struct sip_pvt *p, struct sip_request *req,
18676 struct ast_party_redirecting *redirecting,
18677 struct ast_set_party_redirecting *update_redirecting, int set_call_forward)
18678 {
18679 char *redirecting_from_name = NULL;
18680 char *redirecting_from_number = NULL;
18681 char *redirecting_to_name = NULL;
18682 char *redirecting_to_number = NULL;
18683 int reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
18684 int is_response = req->method == SIP_RESPONSE;
18685 int res = 0;
18686
18687 res = get_rdnis(p, req, &redirecting_from_name, &redirecting_from_number, &reason);
18688 if (res == -1) {
18689 if (is_response) {
18690 get_name_and_number(get_header(req, "TO"), &redirecting_from_name, &redirecting_from_number);
18691 } else {
18692 return;
18693 }
18694 }
18695
18696
18697
18698
18699
18700 if (is_response) {
18701 parse_moved_contact(p, req, &redirecting_to_name, &redirecting_to_number, set_call_forward);
18702 } else {
18703 get_name_and_number(get_header(req, "TO"), &redirecting_to_name, &redirecting_to_number);
18704 }
18705
18706 if (!ast_strlen_zero(redirecting_from_number)) {
18707 ast_debug(3, "Got redirecting from number %s\n", redirecting_from_number);
18708 update_redirecting->from.number = 1;
18709 redirecting->from.number.valid = 1;
18710 ast_free(redirecting->from.number.str);
18711 redirecting->from.number.str = redirecting_from_number;
18712 }
18713 if (!ast_strlen_zero(redirecting_from_name)) {
18714 ast_debug(3, "Got redirecting from name %s\n", redirecting_from_name);
18715 update_redirecting->from.name = 1;
18716 redirecting->from.name.valid = 1;
18717 ast_free(redirecting->from.name.str);
18718 redirecting->from.name.str = redirecting_from_name;
18719 }
18720 if (!ast_strlen_zero(p->cid_tag)) {
18721 ast_free(redirecting->from.tag);
18722 redirecting->from.tag = ast_strdup(p->cid_tag);
18723 ast_free(redirecting->to.tag);
18724 redirecting->to.tag = ast_strdup(p->cid_tag);
18725 }
18726 if (!ast_strlen_zero(redirecting_to_number)) {
18727 ast_debug(3, "Got redirecting to number %s\n", redirecting_to_number);
18728 update_redirecting->to.number = 1;
18729 redirecting->to.number.valid = 1;
18730 ast_free(redirecting->to.number.str);
18731 redirecting->to.number.str = redirecting_to_number;
18732 }
18733 if (!ast_strlen_zero(redirecting_to_name)) {
18734 ast_debug(3, "Got redirecting to name %s\n", redirecting_from_number);
18735 update_redirecting->to.name = 1;
18736 redirecting->to.name.valid = 1;
18737 ast_free(redirecting->to.name.str);
18738 redirecting->to.name.str = redirecting_to_name;
18739 }
18740 redirecting->reason = reason;
18741 }
18742
18743
18744
18745
18746
18747
18748
18749 static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req, char **name, char **number, int set_call_forward)
18750 {
18751 char contact[SIPBUFSIZE];
18752 char *contact_name = NULL;
18753 char *contact_number = NULL;
18754 char *separator, *trans;
18755 char *domain;
18756 enum sip_transport transport = SIP_TRANSPORT_UDP;
18757
18758 ast_copy_string(contact, get_header(req, "Contact"), sizeof(contact));
18759 if ((separator = strchr(contact, ',')))
18760 *separator = '\0';
18761
18762 contact_number = get_in_brackets(contact);
18763 if ((trans = strcasestr(contact_number, ";transport="))) {
18764 trans += 11;
18765
18766 if ((separator = strchr(trans, ';')))
18767 *separator = '\0';
18768
18769 if (!strncasecmp(trans, "tcp", 3))
18770 transport = SIP_TRANSPORT_TCP;
18771 else if (!strncasecmp(trans, "tls", 3))
18772 transport = SIP_TRANSPORT_TLS;
18773 else {
18774 if (strncasecmp(trans, "udp", 3))
18775 ast_debug(1, "received contact with an invalid transport, '%s'\n", contact_number);
18776
18777 transport = SIP_TRANSPORT_UDP;
18778 }
18779 }
18780 contact_number = remove_uri_parameters(contact_number);
18781
18782 if (p->socket.tcptls_session) {
18783 ao2_ref(p->socket.tcptls_session, -1);
18784 p->socket.tcptls_session = NULL;
18785 }
18786
18787 set_socket_transport(&p->socket, transport);
18788
18789 if (set_call_forward && ast_test_flag(&p->flags[0], SIP_PROMISCREDIR)) {
18790 char *host = NULL;
18791 if (!strncasecmp(contact_number, "sip:", 4))
18792 contact_number += 4;
18793 else if (!strncasecmp(contact_number, "sips:", 5))
18794 contact_number += 5;
18795 separator = strchr(contact_number, '/');
18796 if (separator)
18797 *separator = '\0';
18798 if ((host = strchr(contact_number, '@'))) {
18799 *host++ = '\0';
18800 ast_debug(2, "Found promiscuous redirection to 'SIP/%s::::%s@%s'\n", contact_number, get_transport(transport), host);
18801 if (p->owner)
18802 ast_string_field_build(p->owner, call_forward, "SIP/%s::::%s@%s", contact_number, get_transport(transport), host);
18803 } else {
18804 ast_debug(2, "Found promiscuous redirection to 'SIP/::::%s@%s'\n", get_transport(transport), contact_number);
18805 if (p->owner)
18806 ast_string_field_build(p->owner, call_forward, "SIP/::::%s@%s", get_transport(transport), contact_number);
18807 }
18808 } else {
18809 separator = strchr(contact, '@');
18810 if (separator) {
18811 *separator++ = '\0';
18812 domain = separator;
18813 } else {
18814
18815 domain = contact;
18816 }
18817 separator = strchr(contact, '/');
18818 if (separator)
18819 *separator = '\0';
18820
18821 if (!strncasecmp(contact_number, "sip:", 4))
18822 contact_number += 4;
18823 else if (!strncasecmp(contact_number, "sips:", 5))
18824 contact_number += 5;
18825 separator = strchr(contact_number, ';');
18826 if (separator)
18827 *separator = '\0';
18828 ast_uri_decode(contact_number);
18829 if (set_call_forward) {
18830 ast_debug(2, "Received 302 Redirect to extension '%s' (domain %s)\n", contact_number, domain);
18831 if (p->owner) {
18832 pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
18833 ast_string_field_set(p->owner, call_forward, contact_number);
18834 }
18835 }
18836 }
18837
18838
18839
18840 if (*contact == '\"') {
18841 contact_name = contact + 1;
18842 if (!(separator = (char *)find_closing_quote(contact_name, NULL))) {
18843 ast_log(LOG_NOTICE, "No closing quote on name in Contact header? %s\n", contact);
18844 }
18845 *separator = '\0';
18846 }
18847
18848 if (name && !ast_strlen_zero(contact_name)) {
18849 *name = ast_strdup(contact_name);
18850 }
18851 if (number) {
18852 *number = ast_strdup(contact_number);
18853 }
18854 }
18855
18856
18857
18858
18859
18860
18861 static void check_pendings(struct sip_pvt *p)
18862 {
18863 if (ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
18864
18865 if (p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA) {
18866 p->invitestate = INV_CANCELLED;
18867 transmit_request(p, SIP_CANCEL, p->lastinvite, XMIT_RELIABLE, FALSE);
18868
18869
18870 } else {
18871
18872
18873 if (p->pendinginvite)
18874 return;
18875
18876 if (p->owner) {
18877 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
18878 }
18879
18880 transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
18881 }
18882 ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
18883 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18884 } else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
18885
18886 if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
18887 ast_debug(2, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
18888 } else {
18889 ast_debug(2, "Sending pending reinvite on '%s'\n", p->callid);
18890
18891 transmit_reinvite_with_sdp(p, (p->t38.state == T38_LOCAL_REINVITE ? TRUE : FALSE), FALSE);
18892 ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
18893 }
18894 }
18895 }
18896
18897
18898
18899
18900
18901 static int sip_reinvite_retry(const void *data)
18902 {
18903 struct sip_pvt *p = (struct sip_pvt *) data;
18904 struct ast_channel *owner;
18905
18906 sip_pvt_lock(p);
18907 while ((owner = p->owner) && ast_channel_trylock(owner)) {
18908 sip_pvt_unlock(p);
18909 usleep(1);
18910 sip_pvt_lock(p);
18911 }
18912 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
18913 p->waitid = -1;
18914 check_pendings(p);
18915 sip_pvt_unlock(p);
18916 if (owner) {
18917 ast_channel_unlock(owner);
18918 }
18919 dialog_unref(p, "unref the dialog ptr from sip_reinvite_retry, because it held a dialog ptr");
18920 return 0;
18921 }
18922
18923
18924
18925
18926
18927
18928 static void handle_response_update(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
18929 {
18930 if (p->options) {
18931 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
18932 }
18933 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, SIP_UPDATE, 1)) {
18934 ast_log(LOG_NOTICE, "Failed to authenticate on UPDATE to '%s'\n", get_header(&p->initreq, "From"));
18935 }
18936 }
18937
18938 static void cc_handle_publish_error(struct sip_pvt *pvt, const int resp, struct sip_request *req, struct sip_epa_entry *epa_entry)
18939 {
18940 struct cc_epa_entry *cc_entry = epa_entry->instance_data;
18941 struct sip_monitor_instance *monitor_instance = ao2_callback(sip_monitor_instances, 0,
18942 find_sip_monitor_instance_by_suspension_entry, epa_entry);
18943 const char *min_expires;
18944
18945 if (!monitor_instance) {
18946 ast_log(LOG_WARNING, "Can't find monitor_instance corresponding to epa_entry %p.\n", epa_entry);
18947 return;
18948 }
18949
18950 if (resp != 423) {
18951 ast_cc_monitor_failed(cc_entry->core_id, monitor_instance->device_name,
18952 "Received error response to our PUBLISH");
18953 ao2_ref(monitor_instance, -1);
18954 return;
18955 }
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965 min_expires = get_header(req, "Min-Expires");
18966 if (ast_strlen_zero(min_expires)) {
18967 pvt->expiry *= 2;
18968 if (pvt->expiry < 0) {
18969
18970 ast_cc_monitor_failed(cc_entry->core_id, monitor_instance->device_name,
18971 "PUBLISH expiry overflowed");
18972 ao2_ref(monitor_instance, -1);
18973 return;
18974 }
18975 } else if (sscanf(min_expires, "%d", &pvt->expiry) != 1) {
18976 ast_cc_monitor_failed(cc_entry->core_id, monitor_instance->device_name,
18977 "Min-Expires has non-numeric value");
18978 ao2_ref(monitor_instance, -1);
18979 return;
18980 }
18981
18982
18983
18984 transmit_invite(pvt, SIP_PUBLISH, FALSE, 0, NULL);
18985 ao2_ref(monitor_instance, -1);
18986 }
18987
18988 static void handle_response_publish(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
18989 {
18990 struct sip_epa_entry *epa_entry = p->epa_entry;
18991 const char *etag = get_header(req, "Sip-ETag");
18992
18993 ast_assert(epa_entry != NULL);
18994
18995 if (resp == 401 || resp == 407) {
18996 ast_string_field_set(p, theirtag, NULL);
18997 if (p->options) {
18998 p->options->auth_type = (resp == 401 ? WWW_AUTH : PROXY_AUTH);
18999 }
19000 if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, SIP_PUBLISH, 0)) {
19001 ast_log(LOG_NOTICE, "Failed to authenticate on PUBLISH to '%s'\n", get_header(&p->initreq, "From"));
19002 pvt_set_needdestroy(p, "Failed to authenticate on PUBLISH");
19003 sip_alreadygone(p);
19004 }
19005 return;
19006 }
19007
19008 if (resp == 501 || resp == 405) {
19009 mark_method_unallowed(&p->allowed_methods, SIP_PUBLISH);
19010 }
19011
19012 if (resp == 200) {
19013 p->authtries = 0;
19014
19015
19016
19017 if (!ast_strlen_zero(etag)) {
19018 ast_copy_string(epa_entry->entity_tag, etag, sizeof(epa_entry->entity_tag));
19019 }
19020
19021
19022
19023
19024 if (epa_entry->static_data->handle_ok) {
19025 epa_entry->static_data->handle_ok(p, req, epa_entry);
19026 }
19027 } else {
19028
19029
19030
19031
19032 if (epa_entry->static_data->handle_error) {
19033 epa_entry->static_data->handle_error(p, resp, req, epa_entry);
19034 }
19035 }
19036 }
19037
19038
19039 static void handle_response_invite(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19040 {
19041 int outgoing = ast_test_flag(&p->flags[0], SIP_OUTGOING);
19042 int res = 0;
19043 int xmitres = 0;
19044 int reinvite = (p->owner && p->owner->_state == AST_STATE_UP);
19045 char *p_hdrval;
19046 int rtn;
19047 struct ast_party_connected_line connected;
19048 struct ast_set_party_connected_line update_connected;
19049
19050 if (reinvite)
19051 ast_debug(4, "SIP response %d to RE-invite on %s call %s\n", resp, outgoing ? "outgoing" : "incoming", p->callid);
19052 else
19053 ast_debug(4, "SIP response %d to standard invite\n", resp);
19054
19055 if (p->alreadygone) {
19056 ast_debug(1, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
19057 return;
19058 }
19059
19060
19061
19062 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"));
19063
19064
19065
19066
19067 if (resp > 100 && resp < 200 && resp!=101 && resp != 180 && resp != 181 && resp != 182 && resp != 183)
19068 resp = 183;
19069
19070
19071 if (resp >= 100 && resp < 200 && p->invitestate == INV_CALLING)
19072 p->invitestate = INV_PROCEEDING;
19073
19074
19075 if (resp >= 300 && (p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA ))
19076 p->invitestate = INV_COMPLETED;
19077
19078
19079 if ((resp == 200 || resp >= 300) && p->pendinginvite && seqno == p->pendinginvite)
19080 p->pendinginvite = 0;
19081
19082
19083
19084
19085 if (!reinvite) {
19086 set_pvt_allowed_methods(p, req);
19087 }
19088
19089 switch (resp) {
19090 case 100:
19091 case 101:
19092 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
19093 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
19094 check_pendings(p);
19095 break;
19096
19097 case 180:
19098 case 182:
19099 if (!req->ignore && p->invitestate != INV_CANCELLED && sip_cancel_destroy(p))
19100 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
19101 if (!req->ignore && p->owner) {
19102 if (get_rpid(p, req)) {
19103 ast_party_connected_line_init(&connected);
19104 memset(&update_connected, 0, sizeof(update_connected));
19105 if (p->cid_num) {
19106 update_connected.id.number = 1;
19107 connected.id.number.valid = 1;
19108 connected.id.number.str = (char *) p->cid_num;
19109 connected.id.number.presentation = p->callingpres;
19110 }
19111 if (p->cid_name) {
19112 update_connected.id.name = 1;
19113 connected.id.name.valid = 1;
19114 connected.id.name.str = (char *) p->cid_name;
19115 connected.id.name.presentation = p->callingpres;
19116 }
19117 connected.id.tag = (char *) p->cid_tag;
19118 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
19119 ast_channel_queue_connected_line_update(p->owner, &connected,
19120 &update_connected);
19121 }
19122 sip_handle_cc(p, req, AST_CC_CCNR);
19123 ast_queue_control(p->owner, AST_CONTROL_RINGING);
19124 if (p->owner->_state != AST_STATE_UP) {
19125 ast_setstate(p->owner, AST_STATE_RINGING);
19126 }
19127 }
19128 if (find_sdp(req)) {
19129 if (p->invitestate != INV_CANCELLED)
19130 p->invitestate = INV_EARLY_MEDIA;
19131 res = process_sdp(p, req, SDP_T38_NONE);
19132 if (!req->ignore && p->owner) {
19133
19134 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
19135 }
19136 ast_rtp_instance_activate(p->rtp);
19137 }
19138 check_pendings(p);
19139 break;
19140
19141 case 181:
19142 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
19143 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
19144 if (!req->ignore && p->owner) {
19145 struct ast_party_redirecting redirecting;
19146 struct ast_set_party_redirecting update_redirecting;
19147
19148 ast_party_redirecting_init(&redirecting);
19149 memset(&update_redirecting, 0, sizeof(update_redirecting));
19150 change_redirecting_information(p, req, &redirecting, &update_redirecting,
19151 FALSE);
19152 ast_channel_queue_redirecting_update(p->owner, &redirecting,
19153 &update_redirecting);
19154 ast_party_redirecting_free(&redirecting);
19155 sip_handle_cc(p, req, AST_CC_CCNR);
19156 }
19157 check_pendings(p);
19158 break;
19159
19160 case 183:
19161 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
19162 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
19163 if (!req->ignore && p->owner) {
19164 if (get_rpid(p, req)) {
19165
19166 ast_party_connected_line_init(&connected);
19167 memset(&update_connected, 0, sizeof(update_connected));
19168 if (p->cid_num) {
19169 update_connected.id.number = 1;
19170 connected.id.number.valid = 1;
19171 connected.id.number.str = (char *) p->cid_num;
19172 connected.id.number.presentation = p->callingpres;
19173 }
19174 if (p->cid_name) {
19175 update_connected.id.name = 1;
19176 connected.id.name.valid = 1;
19177 connected.id.name.str = (char *) p->cid_name;
19178 connected.id.name.presentation = p->callingpres;
19179 }
19180 connected.id.tag = (char *) p->cid_tag;
19181 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
19182 ast_channel_queue_connected_line_update(p->owner, &connected,
19183 &update_connected);
19184 }
19185 sip_handle_cc(p, req, AST_CC_CCNR);
19186 }
19187 if (find_sdp(req)) {
19188 if (p->invitestate != INV_CANCELLED)
19189 p->invitestate = INV_EARLY_MEDIA;
19190 res = process_sdp(p, req, SDP_T38_NONE);
19191 if (!req->ignore && p->owner) {
19192
19193 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
19194 }
19195 ast_rtp_instance_activate(p->rtp);
19196 } else {
19197
19198
19199
19200
19201 if (!req->ignore && p->owner) {
19202 ast_queue_control(p->owner, AST_CONTROL_RINGING);
19203 }
19204 }
19205 check_pendings(p);
19206 break;
19207
19208 case 200:
19209 if (!req->ignore && (p->invitestate != INV_CANCELLED) && sip_cancel_destroy(p))
19210 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
19211 p->authtries = 0;
19212 if (find_sdp(req)) {
19213 if ((res = process_sdp(p, req, SDP_T38_ACCEPT)) && !req->ignore)
19214 if (!reinvite)
19215
19216
19217 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
19218 ast_rtp_instance_activate(p->rtp);
19219 }
19220
19221 if (!req->ignore && p->owner && (get_rpid(p, req) || !reinvite)) {
19222
19223 ast_party_connected_line_init(&connected);
19224 memset(&update_connected, 0, sizeof(update_connected));
19225 if (p->cid_num) {
19226 update_connected.id.number = 1;
19227 connected.id.number.valid = 1;
19228 connected.id.number.str = (char *) p->cid_num;
19229 connected.id.number.presentation = p->callingpres;
19230 }
19231 if (p->cid_name) {
19232 update_connected.id.name = 1;
19233 connected.id.name.valid = 1;
19234 connected.id.name.str = (char *) p->cid_name;
19235 connected.id.name.presentation = p->callingpres;
19236 }
19237 connected.id.tag = (char *) p->cid_tag;
19238 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
19239 ast_channel_queue_connected_line_update(p->owner, &connected,
19240 &update_connected);
19241 }
19242
19243
19244
19245
19246 if (outgoing) {
19247 update_call_counter(p, DEC_CALL_RINGING);
19248 parse_ok_contact(p, req);
19249
19250 if (!reinvite)
19251 build_route(p, req, 1);
19252
19253 if(set_address_from_contact(p)) {
19254
19255
19256 if (!p->route && !req->ignore)
19257 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
19258 }
19259
19260 }
19261
19262 if (!req->ignore && p->owner) {
19263 if (!reinvite) {
19264 ast_queue_control(p->owner, AST_CONTROL_ANSWER);
19265 if (sip_cfg.callevents)
19266 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
19267 "Channel: %s\r\nChanneltype: %s\r\nUniqueid: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
19268 p->owner->name, "SIP", p->owner->uniqueid, p->callid, p->fullcontact, p->peername);
19269 } else {
19270 ast_queue_frame(p->owner, &ast_null_frame);
19271 }
19272 } else {
19273
19274
19275
19276 if (!req->ignore)
19277 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
19278 }
19279
19280
19281 if (st_get_mode(p) != SESSION_TIMER_MODE_REFUSE && p->outgoing_call == TRUE && !reinvite) {
19282 p_hdrval = (char*)get_header(req, "Session-Expires");
19283 if (!ast_strlen_zero(p_hdrval)) {
19284
19285 enum st_refresher tmp_st_ref = SESSION_TIMER_REFRESHER_AUTO;
19286 int tmp_st_interval = 0;
19287 rtn = parse_session_expires(p_hdrval, &tmp_st_interval, &tmp_st_ref);
19288 if (rtn != 0) {
19289 ast_set_flag(&p->flags[0], SIP_PENDINGBYE);
19290 }
19291 if (tmp_st_ref == SESSION_TIMER_REFRESHER_UAC ||
19292 tmp_st_ref == SESSION_TIMER_REFRESHER_UAS) {
19293 p->stimer->st_ref = tmp_st_ref;
19294 }
19295 if (tmp_st_interval) {
19296 p->stimer->st_interval = tmp_st_interval;
19297 }
19298 p->stimer->st_active = TRUE;
19299 p->stimer->st_active_peer_ua = TRUE;
19300 start_session_timer(p);
19301 } else {
19302
19303 if (st_get_mode(p) == SESSION_TIMER_MODE_ORIGINATE) {
19304 p->stimer->st_ref = SESSION_TIMER_REFRESHER_UAC;
19305 p->stimer->st_active_peer_ua = FALSE;
19306 start_session_timer(p);
19307 }
19308 }
19309 }
19310
19311
19312
19313 p->invitestate = INV_TERMINATED;
19314 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
19315 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, TRUE);
19316 check_pendings(p);
19317 break;
19318
19319 case 407:
19320 case 401:
19321
19322 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19323 if (p->options)
19324 p->options->auth_type = resp;
19325
19326
19327 ast_string_field_set(p, theirtag, NULL);
19328 if (!req->ignore) {
19329 if (p->authtries < MAX_AUTHTRIES)
19330 p->invitestate = INV_CALLING;
19331 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, SIP_INVITE, 1)) {
19332 ast_log(LOG_NOTICE, "Failed to authenticate on INVITE to '%s'\n", get_header(&p->initreq, "From"));
19333 pvt_set_needdestroy(p, "failed to authenticate on INVITE");
19334 sip_alreadygone(p);
19335 if (p->owner)
19336 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19337 }
19338 }
19339 break;
19340
19341 case 403:
19342
19343 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19344 ast_log(LOG_WARNING, "Received response: \"Forbidden\" from '%s'\n", get_header(&p->initreq, "From"));
19345 if (!req->ignore && p->owner) {
19346 ast_set_hangupsource(p->owner, p->owner->name, 0);
19347 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19348 }
19349 pvt_set_needdestroy(p, "received 403 response");
19350 sip_alreadygone(p);
19351 break;
19352
19353 case 404:
19354 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19355 if (p->owner && !req->ignore) {
19356 ast_set_hangupsource(p->owner, p->owner->name, 0);
19357 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19358 }
19359 sip_alreadygone(p);
19360 break;
19361
19362 case 408:
19363 case 481:
19364
19365 ast_log(LOG_WARNING, "Re-invite to non-existing call leg on other UA. SIP dialog '%s'. Giving up.\n", p->callid);
19366 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19367 if (p->owner)
19368 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19369 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19370 break;
19371
19372 case 422:
19373 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19374 ast_string_field_set(p, theirtag, NULL);
19375 proc_422_rsp(p, req);
19376 break;
19377
19378 case 428:
19379 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19380 append_history(p, "Identity", "SIP identity is required. Not supported by Asterisk.");
19381 ast_log(LOG_WARNING, "SIP identity required by proxy. SIP dialog '%s'. Giving up.\n", p->callid);
19382 if (p->owner)
19383 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19384 break;
19385
19386
19387
19388 case 487:
19389
19390
19391
19392 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19393 if (p->owner && !req->ignore) {
19394 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_NORMAL_CLEARING);
19395 append_history(p, "Hangup", "Got 487 on CANCEL request from us. Queued AST hangup request");
19396 } else if (!req->ignore) {
19397 update_call_counter(p, DEC_CALL_LIMIT);
19398 append_history(p, "Hangup", "Got 487 on CANCEL request from us on call without owner. Killing this dialog.");
19399 pvt_set_needdestroy(p, "received 487 response");
19400 sip_alreadygone(p);
19401 }
19402 break;
19403 case 415:
19404 case 488:
19405 case 606:
19406 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19407 if (p->udptl && p->t38.state == T38_LOCAL_REINVITE) {
19408 change_t38_state(p, T38_DISABLED);
19409
19410
19411
19412
19413 transmit_reinvite_with_sdp(p, FALSE, FALSE);
19414 } else {
19415
19416 if (p->owner && !req->ignore)
19417 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19418 pvt_set_needdestroy(p, "received 488 response");
19419
19420 if (!reinvite)
19421 sip_alreadygone(p);
19422 }
19423 break;
19424 case 491:
19425 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19426 if (p->owner && !req->ignore) {
19427 if (p->owner->_state != AST_STATE_UP) {
19428 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19429 pvt_set_needdestroy(p, "received 491 response");
19430 } else {
19431
19432
19433
19434 int wait;
19435
19436
19437 if (p->outgoing_call) {
19438 wait = 2100 + ast_random() % 2000;
19439 } else {
19440 wait = ast_random() % 2000;
19441 }
19442 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."));
19443 ast_log(LOG_WARNING, "just did sched_add waitid(%d) for sip_reinvite_retry for dialog %s in handle_response_invite\n", p->waitid, p->callid);
19444 ast_debug(2, "Reinvite race. Waiting %d secs before retry\n", wait);
19445 }
19446 }
19447 break;
19448
19449 case 405:
19450 case 501:
19451 xmitres = transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
19452 if (p->owner)
19453 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19454 break;
19455 }
19456 if (xmitres == XMIT_ERROR)
19457 ast_log(LOG_WARNING, "Could not transmit message in dialog %s\n", p->callid);
19458 }
19459
19460
19461
19462
19463 static void handle_response_notify(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19464 {
19465 switch (resp) {
19466 case 200:
19467
19468 if (p->owner) {
19469 if (!p->refer) {
19470 ast_log(LOG_WARNING, "Notify answer on an owned channel? - %s\n", p->owner->name);
19471 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_NORMAL_UNSPECIFIED);
19472 } else {
19473 ast_debug(4, "Got OK on REFER Notify message\n");
19474 }
19475 } else {
19476 if (p->subscribed == NONE) {
19477 ast_debug(4, "Got 200 accepted on NOTIFY\n");
19478 pvt_set_needdestroy(p, "received 200 response");
19479 }
19480 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
19481
19482 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
19483 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
19484 }
19485 }
19486 break;
19487 case 401:
19488 case 407:
19489 if (!p->notify) {
19490 break;
19491 }
19492 ast_string_field_set(p, theirtag, NULL);
19493 if (ast_strlen_zero(p->authname)) {
19494 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));
19495 pvt_set_needdestroy(p, "unable to authenticate NOTIFY");
19496 }
19497 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_NOTIFY, 0)) {
19498 ast_log(LOG_NOTICE, "Failed to authenticate on NOTIFY to '%s'\n", get_header(&p->initreq, "From"));
19499 pvt_set_needdestroy(p, "failed to authenticate NOTIFY");
19500 }
19501 break;
19502 }
19503 }
19504
19505
19506 static void handle_response_subscribe(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19507 {
19508 if (!p->mwi) {
19509 return;
19510 }
19511
19512 switch (resp) {
19513 case 200:
19514 ast_debug(3, "Got 200 OK on subscription for MWI\n");
19515 set_pvt_allowed_methods(p, req);
19516 if (p->options) {
19517 ast_free(p->options);
19518 p->options = NULL;
19519 }
19520 p->mwi->subscribed = 1;
19521 if ((p->mwi->resub = ast_sched_add(sched, mwi_expiry * 1000, sip_subscribe_mwi_do, ASTOBJ_REF(p->mwi))) < 0) {
19522 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
19523 }
19524 break;
19525 case 401:
19526 case 407:
19527 ast_string_field_set(p, theirtag, NULL);
19528 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_SUBSCRIBE, 0)) {
19529 ast_log(LOG_NOTICE, "Failed to authenticate on SUBSCRIBE to '%s'\n", get_header(&p->initreq, "From"));
19530 p->mwi->call = NULL;
19531 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
19532 pvt_set_needdestroy(p, "failed to authenticate SUBSCRIBE");
19533 }
19534 break;
19535 case 403:
19536 transmit_response_with_date(p, "200 OK", req);
19537 ast_log(LOG_WARNING, "Authentication failed while trying to subscribe for MWI.\n");
19538 p->mwi->call = NULL;
19539 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
19540 pvt_set_needdestroy(p, "received 403 response");
19541 sip_alreadygone(p);
19542 break;
19543 case 404:
19544 ast_log(LOG_WARNING, "Subscription failed for MWI. The remote side said that a mailbox may not have been configured.\n");
19545 p->mwi->call = NULL;
19546 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
19547 pvt_set_needdestroy(p, "received 404 response");
19548 break;
19549 case 481:
19550 ast_log(LOG_WARNING, "Subscription failed for MWI. The remote side said that our dialog did not exist.\n");
19551 p->mwi->call = NULL;
19552 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
19553 pvt_set_needdestroy(p, "received 481 response");
19554 break;
19555 case 500:
19556 case 501:
19557 ast_log(LOG_WARNING, "Subscription failed for MWI. The remote side may have suffered a heart attack.\n");
19558 p->mwi->call = NULL;
19559 ASTOBJ_UNREF(p->mwi, sip_subscribe_mwi_destroy);
19560 pvt_set_needdestroy(p, "received 500/501 response");
19561 break;
19562 }
19563 }
19564
19565
19566
19567
19568 static void handle_response_refer(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19569 {
19570 enum ast_control_transfer message = AST_TRANSFER_FAILED;
19571
19572
19573 if (!p->refer)
19574 return;
19575
19576 switch (resp) {
19577 case 202:
19578
19579
19580 p->refer->status = REFER_ACCEPTED;
19581
19582 ast_debug(3, "Got 202 accepted on transfer\n");
19583
19584 break;
19585
19586 case 401:
19587 case 407:
19588 if (ast_strlen_zero(p->authname)) {
19589 ast_log(LOG_WARNING, "Asked to authenticate REFER to %s but we have no matching peer or realm auth!\n",
19590 ast_sockaddr_stringify(&p->recv));
19591 if (p->owner) {
19592 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
19593 }
19594 pvt_set_needdestroy(p, "unable to authenticate REFER");
19595 }
19596 if (p->authtries > 1 || do_proxy_auth(p, req, resp, SIP_REFER, 0)) {
19597 ast_log(LOG_NOTICE, "Failed to authenticate on REFER to '%s'\n", get_header(&p->initreq, "From"));
19598 p->refer->status = REFER_NOAUTH;
19599 if (p->owner) {
19600 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
19601 }
19602 pvt_set_needdestroy(p, "failed to authenticate REFER");
19603 }
19604 break;
19605
19606 case 405:
19607
19608
19609 ast_log(LOG_NOTICE, "SIP transfer to %s failed, REFER not allowed. \n", p->refer->refer_to);
19610 pvt_set_needdestroy(p, "received 405 response");
19611 p->refer->status = REFER_FAILED;
19612 if (p->owner) {
19613 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
19614 }
19615 break;
19616
19617 case 481:
19618
19619
19620
19621
19622 ast_log(LOG_WARNING, "Remote host can't match REFER request to call '%s'. Giving up.\n", p->callid);
19623 if (p->owner)
19624 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
19625 pvt_set_needdestroy(p, "received 481 response");
19626 break;
19627
19628 case 500:
19629 case 501:
19630
19631
19632 ast_log(LOG_NOTICE, "SIP transfer to %s failed, call miserably fails. \n", p->refer->refer_to);
19633 pvt_set_needdestroy(p, "received 500/501 response");
19634 p->refer->status = REFER_FAILED;
19635 if (p->owner) {
19636 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
19637 }
19638 break;
19639 case 603:
19640 ast_log(LOG_NOTICE, "SIP transfer to %s declined, call miserably fails. \n", p->refer->refer_to);
19641 p->refer->status = REFER_FAILED;
19642 pvt_set_needdestroy(p, "received 603 response");
19643 if (p->owner) {
19644 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
19645 }
19646 break;
19647 default:
19648
19649
19650
19651
19652 if (resp < 299) {
19653 ast_log(LOG_WARNING, "SIP transfer to %s had unxpected 2xx response (%d), confusion is possible. \n", p->refer->refer_to, resp);
19654 } else {
19655 ast_log(LOG_WARNING, "SIP transfer to %s with response (%d). \n", p->refer->refer_to, resp);
19656 }
19657
19658 p->refer->status = REFER_FAILED;
19659 pvt_set_needdestroy(p, "received failure response");
19660 if (p->owner) {
19661 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
19662 }
19663 break;
19664 }
19665 }
19666
19667
19668 static int handle_response_register(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19669 {
19670 int expires, expires_ms;
19671 struct sip_registry *r;
19672 r=p->registry;
19673
19674 switch (resp) {
19675 case 401:
19676 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
19677 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s@%s' (Tries %d)\n", p->registry->username, p->registry->hostname, p->authtries);
19678 pvt_set_needdestroy(p, "failed to authenticate REGISTER");
19679 }
19680 break;
19681 case 403:
19682 ast_log(LOG_WARNING, "Forbidden - wrong password on authentication for REGISTER for '%s' to '%s'\n", p->registry->username, p->registry->hostname);
19683 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 403"));
19684 r->regstate = REG_STATE_NOAUTH;
19685 pvt_set_needdestroy(p, "received 403 response");
19686 break;
19687 case 404:
19688 ast_log(LOG_WARNING, "Got 404 Not found on SIP register to service %s@%s, giving up\n", p->registry->username, p->registry->hostname);
19689 pvt_set_needdestroy(p, "received 404 response");
19690 if (r->call)
19691 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 404");
19692 r->regstate = REG_STATE_REJECTED;
19693 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 404"));
19694 break;
19695 case 407:
19696 if (p->authtries == MAX_AUTHTRIES || do_register_auth(p, req, resp)) {
19697 ast_log(LOG_NOTICE, "Failed to authenticate on REGISTER to '%s' (tries '%d')\n", get_header(&p->initreq, "From"), p->authtries);
19698 pvt_set_needdestroy(p, "failed to authenticate REGISTER");
19699 }
19700 break;
19701 case 408:
19702
19703 if (r) {
19704 r->regattempts = 0;
19705 } else {
19706 ast_log(LOG_WARNING, "Got a 408 response to our REGISTER on call %s after we had destroyed the registry object\n", p->callid);
19707 }
19708 break;
19709 case 423:
19710 r->expiry = atoi(get_header(req, "Min-Expires"));
19711 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);
19712 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 423"));
19713 if (r->call) {
19714 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 423");
19715 pvt_set_needdestroy(p, "received 423 response");
19716 }
19717 if (r->expiry > max_expiry) {
19718 ast_log(LOG_WARNING, "Required expiration time from %s@%s is too high, giving up\n", p->registry->username, p->registry->hostname);
19719 r->expiry = r->configured_expiry;
19720 r->regstate = REG_STATE_REJECTED;
19721 } else {
19722 r->regstate = REG_STATE_UNREGISTERED;
19723 transmit_register(r, SIP_REGISTER, NULL, NULL);
19724 }
19725 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));
19726 break;
19727 case 479:
19728 ast_log(LOG_WARNING, "Got error 479 on register to %s@%s, giving up (check config)\n", p->registry->username, p->registry->hostname);
19729 pvt_set_needdestroy(p, "received 479 response");
19730 if (r->call)
19731 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 479");
19732 r->regstate = REG_STATE_REJECTED;
19733 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 479"));
19734 break;
19735 case 200:
19736 if (!r) {
19737 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));
19738 pvt_set_needdestroy(p, "received erroneous 200 response");
19739 return 0;
19740 }
19741
19742 r->regstate = REG_STATE_REGISTERED;
19743 r->regtime = ast_tvnow();
19744 manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
19745 r->regattempts = 0;
19746 ast_debug(1, "Registration successful\n");
19747 if (r->timeout > -1) {
19748 ast_debug(1, "Cancelling timeout %d\n", r->timeout);
19749 }
19750 AST_SCHED_DEL_UNREF(sched, r->timeout, registry_unref(r, "reg ptr unref from handle_response_register 200"));
19751 if (r->call)
19752 r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 200");
19753 p->registry = registry_unref(p->registry, "unref registry entry p->registry");
19754
19755 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19756
19757
19758
19759
19760
19761
19762 expires = 0;
19763
19764
19765 if (!ast_strlen_zero(get_header(req, "Contact"))) {
19766 const char *contact = NULL;
19767 const char *tmptmp = NULL;
19768 int start = 0;
19769 for(;;) {
19770 contact = __get_header(req, "Contact", &start);
19771
19772 if(!ast_strlen_zero(contact)) {
19773 if( (tmptmp=strstr(contact, p->our_contact))) {
19774 contact=tmptmp;
19775 break;
19776 }
19777 } else
19778 break;
19779 }
19780 tmptmp = strcasestr(contact, "expires=");
19781 if (tmptmp) {
19782 if (sscanf(tmptmp + 8, "%30d;", &expires) != 1)
19783 expires = 0;
19784 }
19785
19786 }
19787 if (!expires)
19788 expires=atoi(get_header(req, "expires"));
19789 if (!expires)
19790 expires=default_expiry;
19791
19792 expires_ms = expires * 1000;
19793 if (expires <= EXPIRY_GUARD_LIMIT)
19794 expires_ms -= MAX((expires_ms * EXPIRY_GUARD_PCT), EXPIRY_GUARD_MIN);
19795 else
19796 expires_ms -= EXPIRY_GUARD_SECS * 1000;
19797 if (sipdebug)
19798 ast_log(LOG_NOTICE, "Outbound Registration: Expiry for %s is %d sec (Scheduling reregistration in %d s)\n", r->hostname, expires, expires_ms/1000);
19799
19800 r->refresh= (int) expires_ms / 1000;
19801
19802
19803 AST_SCHED_REPLACE_UNREF(r->expire, sched, expires_ms, sip_reregister, r,
19804 registry_unref(_data,"unref in REPLACE del fail"),
19805 registry_unref(r,"unref in REPLACE add fail"),
19806 registry_addref(r,"The Addition side of REPLACE"));
19807 }
19808 return 1;
19809 }
19810
19811
19812 static void handle_response_peerpoke(struct sip_pvt *p, int resp, struct sip_request *req)
19813 {
19814 struct sip_peer *peer = p->relatedpeer ;
19815 int statechanged, is_reachable, was_reachable;
19816 int pingtime = ast_tvdiff_ms(ast_tvnow(), peer->ps);
19817
19818
19819
19820
19821
19822
19823 if (pingtime < 1)
19824 pingtime = 1;
19825
19826
19827
19828
19829
19830 was_reachable = peer->lastms > 0 && peer->lastms <= peer->maxms;
19831 is_reachable = pingtime <= peer->maxms;
19832 statechanged = peer->lastms == 0
19833 || was_reachable != is_reachable;
19834
19835 peer->lastms = pingtime;
19836 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
19837 if (statechanged) {
19838 const char *s = is_reachable ? "Reachable" : "Lagged";
19839 char str_lastms[20];
19840 snprintf(str_lastms, sizeof(str_lastms), "%d", pingtime);
19841
19842 ast_log(LOG_NOTICE, "Peer '%s' is now %s. (%dms / %dms)\n",
19843 peer->name, s, pingtime, peer->maxms);
19844 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
19845 if (sip_cfg.peer_rtupdate) {
19846 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", str_lastms, SENTINEL);
19847 }
19848 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus",
19849 "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: %s\r\nTime: %d\r\n",
19850 peer->name, s, pingtime);
19851 if (is_reachable && sip_cfg.regextenonqualify)
19852 register_peer_exten(peer, TRUE);
19853 }
19854
19855 pvt_set_needdestroy(p, "got OPTIONS response");
19856
19857
19858 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched,
19859 is_reachable ? peer->qualifyfreq : DEFAULT_FREQ_NOTOK,
19860 sip_poke_peer_s, peer,
19861 unref_peer(_data, "removing poke peer ref"),
19862 unref_peer(peer, "removing poke peer ref"),
19863 ref_peer(peer, "adding poke peer ref"));
19864 }
19865
19866
19867 static void stop_media_flows(struct sip_pvt *p)
19868 {
19869
19870 if (p->rtp)
19871 ast_rtp_instance_stop(p->rtp);
19872 if (p->vrtp)
19873 ast_rtp_instance_stop(p->vrtp);
19874 if (p->trtp)
19875 ast_rtp_instance_stop(p->trtp);
19876 if (p->udptl)
19877 ast_udptl_stop(p->udptl);
19878 }
19879
19880
19881
19882 static void handle_response(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19883 {
19884 struct ast_channel *owner;
19885 int sipmethod;
19886 int res = 1;
19887 const char *c = get_header(req, "Cseq");
19888
19889 char *c_copy = ast_strdupa(c);
19890
19891 const char *msg = ast_skip_blanks(ast_skip_nonblanks(c_copy));
19892
19893 if (!msg)
19894 msg = "";
19895
19896 sipmethod = find_sip_method(msg);
19897
19898 owner = p->owner;
19899 if (owner) {
19900 const char *rp = NULL, *rh = NULL;
19901
19902 owner->hangupcause = 0;
19903 if (ast_test_flag(&p->flags[1], SIP_PAGE2_Q850_REASON) && (rh = get_header(req, "Reason"))) {
19904 rh = ast_skip_blanks(rh);
19905 if (!strncasecmp(rh, "Q.850", 5)) {
19906 rp = strstr(rh, "cause=");
19907 if (rp && sscanf(rp + 6, "%30d", &owner->hangupcause) == 1) {
19908 owner->hangupcause &= 0x7f;
19909 if (req->debug)
19910 ast_verbose("Using Reason header for cause code: %d\n", owner->hangupcause);
19911 }
19912 }
19913 }
19914
19915 if (!owner->hangupcause)
19916 owner->hangupcause = hangup_sip2cause(resp);
19917 }
19918
19919 if (p->socket.type == SIP_TRANSPORT_UDP) {
19920 int ack_res = FALSE;
19921
19922
19923 if ((resp >= 100) && (resp <= 199)) {
19924
19925 if (sipmethod == SIP_INVITE) {
19926 ack_res = __sip_semi_ack(p, seqno, 0, sipmethod);
19927 }
19928 } else {
19929 ack_res = __sip_ack(p, seqno, 0, sipmethod);
19930 }
19931
19932 if (ack_res == FALSE) {
19933
19934 if (sipmethod == SIP_INVITE && resp >= 200) {
19935 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, resp < 300 ? TRUE: FALSE);
19936 }
19937
19938 append_history(p, "Ignore", "Ignoring this retransmit\n");
19939 return;
19940 }
19941 }
19942
19943
19944 if (!p->owner && sipmethod == SIP_NOTIFY && p->pendinginvite)
19945 p->pendinginvite = 0;
19946
19947
19948 if (ast_strlen_zero(p->theirtag) || (resp >= 200)) {
19949 char tag[128];
19950
19951 gettag(req, "To", tag, sizeof(tag));
19952 ast_string_field_set(p, theirtag, tag);
19953 }
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969 if ((resp == 404 || resp == 408 || resp == 481) && sipmethod == SIP_BYE) {
19970 pvt_set_needdestroy(p, "received 4XX response to a BYE");
19971 return;
19972 }
19973
19974 if (p->relatedpeer && p->method == SIP_OPTIONS) {
19975
19976
19977
19978 if (resp != 100)
19979 handle_response_peerpoke(p, resp, req);
19980 } else if (sipmethod == SIP_REFER && resp >= 200) {
19981 handle_response_refer(p, resp, rest, req, seqno);
19982 } else if (sipmethod == SIP_PUBLISH) {
19983
19984
19985
19986 handle_response_publish(p, resp, rest, req, seqno);
19987 } else if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
19988 switch(resp) {
19989 case 100:
19990 case 101:
19991 case 183:
19992 case 180:
19993 case 182:
19994 case 181:
19995 if (sipmethod == SIP_INVITE)
19996 handle_response_invite(p, resp, rest, req, seqno);
19997 break;
19998 case 200:
19999 p->authtries = 0;
20000 if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
20001
20002
20003
20004 } else if (sipmethod == SIP_INVITE) {
20005 handle_response_invite(p, resp, rest, req, seqno);
20006 } else if (sipmethod == SIP_NOTIFY) {
20007 handle_response_notify(p, resp, rest, req, seqno);
20008 } else if (sipmethod == SIP_REGISTER) {
20009 res = handle_response_register(p, resp, rest, req, seqno);
20010 } else if (sipmethod == SIP_SUBSCRIBE) {
20011 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
20012 handle_response_subscribe(p, resp, rest, req, seqno);
20013 } else if (sipmethod == SIP_BYE) {
20014 pvt_set_needdestroy(p, "received 200 response");
20015 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
20016 }
20017 break;
20018 case 401:
20019 case 407:
20020 if (sipmethod == SIP_INVITE)
20021 handle_response_invite(p, resp, rest, req, seqno);
20022 else if (sipmethod == SIP_NOTIFY)
20023 handle_response_notify(p, resp, rest, req, seqno);
20024 else if (sipmethod == SIP_SUBSCRIBE)
20025 handle_response_subscribe(p, resp, rest, req, seqno);
20026 else if (p->registry && sipmethod == SIP_REGISTER)
20027 res = handle_response_register(p, resp, rest, req, seqno);
20028 else if (sipmethod == SIP_UPDATE) {
20029 handle_response_update(p, resp, rest, req, seqno);
20030 } else if (sipmethod == SIP_BYE) {
20031 if (p->options)
20032 p->options->auth_type = resp;
20033 if (ast_strlen_zero(p->authname)) {
20034 ast_log(LOG_WARNING, "Asked to authenticate %s, to %s but we have no matching peer!\n",
20035 msg, ast_sockaddr_stringify(&p->recv));
20036 pvt_set_needdestroy(p, "unable to authenticate BYE");
20037 } else if ((p->authtries == MAX_AUTHTRIES) || do_proxy_auth(p, req, resp, sipmethod, 0)) {
20038 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
20039 pvt_set_needdestroy(p, "failed to authenticate BYE");
20040 }
20041 } else {
20042 ast_log(LOG_WARNING, "Got authentication request (%d) on %s to '%s'\n", resp, sip_methods[sipmethod].text, get_header(req, "To"));
20043 pvt_set_needdestroy(p, "received 407 response");
20044 }
20045 break;
20046 case 403:
20047 if (sipmethod == SIP_INVITE)
20048 handle_response_invite(p, resp, rest, req, seqno);
20049 else if (sipmethod == SIP_SUBSCRIBE)
20050 handle_response_subscribe(p, resp, rest, req, seqno);
20051 else if (p->registry && sipmethod == SIP_REGISTER)
20052 res = handle_response_register(p, resp, rest, req, seqno);
20053 else {
20054 ast_log(LOG_WARNING, "Forbidden - maybe wrong password on authentication for %s\n", msg);
20055 pvt_set_needdestroy(p, "received 403 response");
20056 }
20057 break;
20058 case 404:
20059 if (p->registry && sipmethod == SIP_REGISTER)
20060 res = handle_response_register(p, resp, rest, req, seqno);
20061 else if (sipmethod == SIP_INVITE)
20062 handle_response_invite(p, resp, rest, req, seqno);
20063 else if (sipmethod == SIP_SUBSCRIBE)
20064 handle_response_subscribe(p, resp, rest, req, seqno);
20065 else if (owner)
20066 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
20067 break;
20068 case 423:
20069 if (sipmethod == SIP_REGISTER)
20070 res = handle_response_register(p, resp, rest, req, seqno);
20071 break;
20072 case 408:
20073 if (sipmethod == SIP_INVITE)
20074 handle_response_invite(p, resp, rest, req, seqno);
20075 else if (sipmethod == SIP_REGISTER)
20076 res = handle_response_register(p, resp, rest, req, seqno);
20077 else if (sipmethod == SIP_BYE) {
20078 pvt_set_needdestroy(p, "received 408 response");
20079 ast_debug(4, "Got timeout on bye. Thanks for the answer. Now, kill this call\n");
20080 } else {
20081 if (owner)
20082 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
20083 pvt_set_needdestroy(p, "received 408 response");
20084 }
20085 break;
20086
20087 case 422:
20088 if (sipmethod == SIP_INVITE) {
20089 handle_response_invite(p, resp, rest, req, seqno);
20090 }
20091 break;
20092
20093 case 481:
20094 if (sipmethod == SIP_INVITE) {
20095 handle_response_invite(p, resp, rest, req, seqno);
20096 } else if (sipmethod == SIP_SUBSCRIBE) {
20097 handle_response_subscribe(p, resp, rest, req, seqno);
20098 } else if (sipmethod == SIP_NOTIFY) {
20099 pvt_set_needdestroy(p, "received 481 response");
20100 } else if (sipmethod == SIP_BYE) {
20101
20102
20103 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
20104 } else if (sipmethod == SIP_CANCEL) {
20105
20106
20107 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
20108 } else {
20109 ast_log(LOG_WARNING, "Remote host can't match request %s to call '%s'. Giving up.\n", sip_methods[sipmethod].text, p->callid);
20110
20111 }
20112 break;
20113 case 487:
20114 if (sipmethod == SIP_INVITE)
20115 handle_response_invite(p, resp, rest, req, seqno);
20116 break;
20117 case 415:
20118 case 488:
20119 case 606:
20120 if (sipmethod == SIP_INVITE)
20121 handle_response_invite(p, resp, rest, req, seqno);
20122 break;
20123 case 491:
20124 if (sipmethod == SIP_INVITE)
20125 handle_response_invite(p, resp, rest, req, seqno);
20126 else {
20127 ast_debug(1, "Got 491 on %s, unsupported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
20128 pvt_set_needdestroy(p, "received 491 response");
20129 }
20130 break;
20131 case 405:
20132 case 501:
20133 mark_method_unallowed(&p->allowed_methods, sipmethod);
20134 if (p->relatedpeer) {
20135 mark_method_allowed(&p->relatedpeer->disallowed_methods, sipmethod);
20136 }
20137 if (sipmethod == SIP_INVITE)
20138 handle_response_invite(p, resp, rest, req, seqno);
20139 else
20140 ast_log(LOG_WARNING, "Host '%s' does not implement '%s'\n", ast_sockaddr_stringify(&p->sa), msg);
20141 break;
20142
20143 default:
20144 if ((resp >= 300) && (resp < 700)) {
20145
20146 if ((resp != 487))
20147 ast_verb(3, "Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_sockaddr_stringify(&p->sa));
20148
20149 if (sipmethod == SIP_INVITE)
20150 stop_media_flows(p);
20151
20152
20153 switch(resp) {
20154 case 300:
20155 case 301:
20156 case 302:
20157 case 305:
20158 if (p->owner) {
20159 struct ast_party_redirecting redirecting;
20160 struct ast_set_party_redirecting update_redirecting;
20161
20162 ast_party_redirecting_init(&redirecting);
20163 change_redirecting_information(p, req, &redirecting,
20164 &update_redirecting, TRUE);
20165 ast_channel_set_redirecting(p->owner, &redirecting,
20166 &update_redirecting);
20167 ast_party_redirecting_free(&redirecting);
20168 }
20169
20170 case 486:
20171 case 600:
20172 case 603:
20173 if (p->owner) {
20174 sip_handle_cc(p, req, AST_CC_CCBS);
20175 ast_queue_control(p->owner, AST_CONTROL_BUSY);
20176 }
20177 break;
20178 case 482:
20179 case 480:
20180 case 404:
20181 case 410:
20182 case 400:
20183 case 500:
20184 if (sipmethod == SIP_SUBSCRIBE) {
20185 handle_response_subscribe(p, resp, rest, req, seqno);
20186 break;
20187 }
20188
20189 case 502:
20190 case 503:
20191 case 504:
20192 if (owner)
20193 ast_queue_control(p->owner, AST_CONTROL_CONGESTION);
20194 break;
20195 default:
20196
20197 if (owner && sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO && sipmethod != SIP_BYE)
20198 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
20199 break;
20200 }
20201
20202 if (sipmethod == SIP_INVITE)
20203 transmit_request(p, SIP_ACK, seqno, XMIT_UNRELIABLE, FALSE);
20204 if (sipmethod != SIP_MESSAGE && sipmethod != SIP_INFO)
20205 sip_alreadygone(p);
20206 if (!p->owner) {
20207 pvt_set_needdestroy(p, "transaction completed");
20208 }
20209 } else if ((resp >= 100) && (resp < 200)) {
20210 if (sipmethod == SIP_INVITE) {
20211 if (!req->ignore && sip_cancel_destroy(p))
20212 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20213 if (find_sdp(req))
20214 process_sdp(p, req, SDP_T38_NONE);
20215 if (p->owner) {
20216
20217 ast_queue_control(p->owner, AST_CONTROL_PROGRESS);
20218 }
20219 }
20220 } else
20221 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));
20222 }
20223 } else {
20224
20225
20226 if (req->debug)
20227 ast_verbose("SIP Response message for INCOMING dialog %s arrived\n", msg);
20228
20229 if (sipmethod == SIP_INVITE && resp == 200) {
20230
20231
20232 char tag[128];
20233
20234 gettag(req, "To", tag, sizeof(tag));
20235 ast_string_field_set(p, theirtag, tag);
20236 }
20237
20238 if (sipmethod == SIP_SUBSCRIBE && resp >= 400) {
20239 struct sip_monitor_instance *monitor_instance = ao2_callback(sip_monitor_instances,
20240 0, find_sip_monitor_instance_by_subscription_pvt, p);
20241 if (monitor_instance) {
20242 ast_cc_monitor_failed(monitor_instance->core_id, monitor_instance->device_name,
20243 "Received error response to our SUBSCRIBE");
20244 return;
20245 }
20246 }
20247
20248 switch(resp) {
20249 case 200:
20250 if (sipmethod == SIP_INVITE) {
20251 handle_response_invite(p, resp, rest, req, seqno);
20252 } else if (sipmethod == SIP_CANCEL) {
20253 ast_debug(1, "Got 200 OK on CANCEL\n");
20254
20255
20256 } else if (sipmethod == SIP_NOTIFY) {
20257
20258 if (p->owner) {
20259 if (p->refer) {
20260 ast_debug(1, "Got 200 OK on NOTIFY for transfer\n");
20261 } else
20262 ast_log(LOG_WARNING, "Notify answer on an owned channel?\n");
20263
20264 } else {
20265 if (!p->subscribed && !p->refer) {
20266 pvt_set_needdestroy(p, "transaction completed");
20267 }
20268 if (ast_test_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE)) {
20269
20270 ast_clear_flag(&p->flags[1], SIP_PAGE2_STATECHANGEQUEUE);
20271 cb_extensionstate((char *)p->context, (char *)p->exten, p->laststate, (void *) p);
20272 }
20273 }
20274 } else if (sipmethod == SIP_BYE) {
20275 pvt_set_needdestroy(p, "transaction completed");
20276 } else if (sipmethod == SIP_MESSAGE || sipmethod == SIP_INFO) {
20277
20278
20279 ;
20280 }
20281 break;
20282 case 401:
20283 case 407:
20284 if (sipmethod == SIP_INVITE)
20285 handle_response_invite(p, resp, rest, req, seqno);
20286 else if (sipmethod == SIP_BYE) {
20287 if (p->authtries == MAX_AUTHTRIES || do_proxy_auth(p, req, resp, sipmethod, 0)) {
20288 ast_log(LOG_NOTICE, "Failed to authenticate on %s to '%s'\n", msg, get_header(&p->initreq, "From"));
20289 pvt_set_needdestroy(p, "failed to authenticate BYE");
20290 }
20291 }
20292 break;
20293 case 481:
20294 if (sipmethod == SIP_INVITE) {
20295
20296 handle_response_invite(p, resp, rest, req, seqno);
20297 } else if (sipmethod == SIP_BYE) {
20298 pvt_set_needdestroy(p, "received 481 response");
20299 } else if (sipmethod == SIP_NOTIFY) {
20300 pvt_set_needdestroy(p, "received 481 response");
20301 } else if (sipdebug) {
20302 ast_debug(1, "Remote host can't match request %s to call '%s'. Giving up\n", sip_methods[sipmethod].text, p->callid);
20303 }
20304 break;
20305 case 501:
20306 if (sipmethod == SIP_INVITE)
20307 handle_response_invite(p, resp, rest, req, seqno);
20308 break;
20309 default:
20310 if ((resp >= 100) && (resp < 200)) {
20311 if (sipmethod == SIP_INVITE) {
20312 if (!req->ignore && sip_cancel_destroy(p))
20313 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20314 }
20315 }
20316 if ((resp >= 300) && (resp < 700)) {
20317 if ((resp != 487))
20318 ast_verb(3, "Incoming call: Got SIP response %d \"%s\" back from %s\n", resp, rest, ast_sockaddr_stringify(&p->sa));
20319 switch(resp) {
20320 case 415:
20321 case 488:
20322 case 603:
20323 case 500:
20324 case 502:
20325 case 503:
20326 case 504:
20327
20328
20329 if (sipmethod == SIP_INVITE && sip_cancel_destroy(p))
20330 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
20331 break;
20332 }
20333 }
20334 break;
20335 }
20336 }
20337 }
20338
20339
20340
20341
20342
20343
20344
20345 static void *sip_park_thread(void *stuff)
20346 {
20347 struct ast_channel *transferee, *transferer;
20348 struct sip_dual *d;
20349 struct sip_request req = {0,};
20350 int ext;
20351 int res;
20352
20353 d = stuff;
20354 transferee = d->chan1;
20355 transferer = d->chan2;
20356 copy_request(&req, &d->req);
20357
20358 if (!transferee || !transferer) {
20359 ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
20360 deinit_req(&d->req);
20361 ast_free(d);
20362 return NULL;
20363 }
20364 ast_debug(4, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
20365
20366 if (ast_do_masquerade(transferee)) {
20367 ast_log(LOG_WARNING, "Masquerade failed.\n");
20368 transmit_response(transferer->tech_pvt, "503 Internal error", &req);
20369 deinit_req(&d->req);
20370 ast_free(d);
20371 return NULL;
20372 }
20373
20374 res = ast_park_call(transferee, transferer, 0, d->parkexten, &ext);
20375
20376
20377 #ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
20378 if (!res) {
20379 transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n");
20380 } else {
20381
20382 sprintf(buf, "Call parked on extension '%d'", ext);
20383 transmit_message_with_text(transferer->tech_pvt, buf);
20384 }
20385 #endif
20386
20387
20388
20389 if (!res) {
20390
20391 append_history(transferer->tech_pvt, "SIPpark", "Parked call on %d", ext);
20392 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
20393 transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
20394 ast_hangup(transferer);
20395 ast_debug(1, "SIP Call parked on extension '%d'\n", ext);
20396 } else {
20397 transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "503 Service Unavailable", TRUE);
20398 append_history(transferer->tech_pvt, "SIPpark", "Parking failed\n");
20399 ast_debug(1, "SIP Call parked failed \n");
20400
20401 }
20402 deinit_req(&d->req);
20403 ast_free(d);
20404 return NULL;
20405 }
20406
20407
20408
20409
20410 static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno, char *parkexten)
20411 {
20412 struct sip_dual *d;
20413 struct ast_channel *transferee, *transferer;
20414
20415 pthread_t th;
20416
20417 transferee = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan1->accountcode, chan1->exten, chan1->context, chan1->linkedid, chan1->amaflags, "Parking/%s", chan1->name);
20418 transferer = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->linkedid, chan2->amaflags, "SIPPeer/%s", chan2->name);
20419 if ((!transferer) || (!transferee)) {
20420 if (transferee) {
20421 transferee->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
20422 ast_hangup(transferee);
20423 }
20424 if (transferer) {
20425 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
20426 ast_hangup(transferer);
20427 }
20428 return -1;
20429 }
20430
20431
20432 transferee->readformat = chan1->readformat;
20433 transferee->writeformat = chan1->writeformat;
20434
20435
20436 ast_channel_masquerade(transferee, chan1);
20437
20438
20439 ast_copy_string(transferee->context, chan1->context, sizeof(transferee->context));
20440 ast_copy_string(transferee->exten, chan1->exten, sizeof(transferee->exten));
20441 transferee->priority = chan1->priority;
20442
20443
20444
20445
20446
20447 transferer->readformat = chan2->readformat;
20448 transferer->writeformat = chan2->writeformat;
20449 if (!ast_strlen_zero(chan2->parkinglot))
20450 ast_string_field_set(transferer, parkinglot, chan2->parkinglot);
20451
20452
20453
20454
20455 while (ast_channel_trylock(chan2)) {
20456 struct sip_pvt *pvt = chan2->tech_pvt;
20457 sip_pvt_unlock(pvt);
20458 usleep(1);
20459 sip_pvt_lock(pvt);
20460 }
20461 ast_channel_masquerade(transferer, chan2);
20462 ast_channel_unlock(chan2);
20463
20464
20465 ast_copy_string(transferer->context, chan2->context, sizeof(transferer->context));
20466 ast_copy_string(transferer->exten, chan2->exten, sizeof(transferer->exten));
20467 transferer->priority = chan2->priority;
20468
20469 if (ast_do_masquerade(transferer)) {
20470 ast_log(LOG_WARNING, "Masquerade failed :(\n");
20471 transferer->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
20472 ast_hangup(transferer);
20473 return -1;
20474 }
20475 if (!transferer || !transferee) {
20476 if (!transferer) {
20477 ast_debug(1, "No transferer channel, giving up parking\n");
20478 }
20479 if (!transferee) {
20480 ast_debug(1, "No transferee channel, giving up parking\n");
20481 }
20482 return -1;
20483 }
20484 if (!(d = ast_calloc(1, sizeof(*d)))) {
20485 return -1;
20486 }
20487
20488
20489 copy_request(&d->req, req);
20490 d->chan1 = transferee;
20491 d->chan2 = transferer;
20492 d->seqno = seqno;
20493 d->parkexten = parkexten;
20494 if (ast_pthread_create_detached_background(&th, NULL, sip_park_thread, d) < 0) {
20495
20496 deinit_req(&d->req);
20497 ast_free(d);
20498
20499 return -1;
20500 }
20501 return 0;
20502 }
20503
20504
20505
20506
20507 static void ast_quiet_chan(struct ast_channel *chan)
20508 {
20509 if (chan && chan->_state == AST_STATE_UP) {
20510 if (ast_test_flag(chan, AST_FLAG_MOH))
20511 ast_moh_stop(chan);
20512 else if (chan->generatordata)
20513 ast_deactivate_generator(chan);
20514 }
20515 }
20516
20517
20518
20519 static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
20520 {
20521 int res = 0;
20522 struct ast_channel *peera = NULL,
20523 *peerb = NULL,
20524 *peerc = NULL,
20525 *peerd = NULL;
20526
20527
20528
20529
20530 ast_debug(4, "Sip transfer:--------------------\n");
20531 if (transferer->chan1)
20532 ast_debug(4, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
20533 else
20534 ast_debug(4, "-- No transferer first channel - odd??? \n");
20535 if (target->chan1)
20536 ast_debug(4, "-- Transferer to PBX second channel (target): %s State %s\n", target->chan1->name, ast_state2str(target->chan1->_state));
20537 else
20538 ast_debug(4, "-- No target first channel ---\n");
20539 if (transferer->chan2)
20540 ast_debug(4, "-- Bridged call to transferee: %s State %s\n", transferer->chan2->name, ast_state2str(transferer->chan2->_state));
20541 else
20542 ast_debug(4, "-- No bridged call to transferee\n");
20543 if (target->chan2)
20544 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)");
20545 else
20546 ast_debug(4, "-- No target second channel ---\n");
20547 ast_debug(4, "-- END Sip transfer:--------------------\n");
20548 if (transferer->chan2) {
20549 peera = transferer->chan1;
20550 peerb = target->chan1;
20551 peerc = transferer->chan2;
20552 peerd = target->chan2;
20553 ast_debug(3, "SIP transfer: Four channels to handle\n");
20554 } else if (target->chan2) {
20555 peera = target->chan1;
20556 peerb = transferer->chan1;
20557 peerc = target->chan2;
20558 peerd = transferer->chan2;
20559 ast_debug(3, "SIP transfer: Three channels to handle\n");
20560 }
20561
20562 if (peera && peerb && peerc && (peerb != peerc)) {
20563 ast_quiet_chan(peera);
20564 ast_quiet_chan(peerb);
20565 ast_quiet_chan(peerc);
20566 if (peerd)
20567 ast_quiet_chan(peerd);
20568
20569 ast_debug(4, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
20570 if (ast_channel_masquerade(peerb, peerc)) {
20571 ast_log(LOG_WARNING, "Failed to masquerade %s into %s\n", peerb->name, peerc->name);
20572 res = -1;
20573 } else
20574 ast_debug(4, "SIP transfer: Succeeded to masquerade channels.\n");
20575 return res;
20576 } else {
20577 ast_log(LOG_NOTICE, "SIP Transfer attempted with no appropriate bridged calls to transfer\n");
20578 if (transferer->chan1)
20579 ast_softhangup_nolock(transferer->chan1, AST_SOFTHANGUP_DEV);
20580 if (target->chan1)
20581 ast_softhangup_nolock(target->chan1, AST_SOFTHANGUP_DEV);
20582 return -1;
20583 }
20584 return 0;
20585 }
20586
20587
20588
20589
20590
20591
20592 static const char *gettag(const struct sip_request *req, const char *header, char *tagbuf, int tagbufsize)
20593 {
20594 const char *thetag;
20595
20596 if (!tagbuf)
20597 return NULL;
20598 tagbuf[0] = '\0';
20599 thetag = get_header(req, header);
20600 thetag = strcasestr(thetag, ";tag=");
20601 if (thetag) {
20602 thetag += 5;
20603 ast_copy_string(tagbuf, thetag, tagbufsize);
20604 return strsep(&tagbuf, ";");
20605 }
20606 return NULL;
20607 }
20608
20609 static int handle_cc_notify(struct sip_pvt *pvt, struct sip_request *req)
20610 {
20611 struct sip_monitor_instance *monitor_instance = ao2_callback(sip_monitor_instances, 0,
20612 find_sip_monitor_instance_by_subscription_pvt, pvt);
20613 const char *status = get_body(req, "cc-state", ':');
20614 struct cc_epa_entry *cc_entry;
20615 char *uri;
20616
20617 if (!monitor_instance) {
20618 transmit_response(pvt, "400 Bad Request", req);
20619 return -1;
20620 }
20621
20622 if (ast_strlen_zero(status)) {
20623 ao2_ref(monitor_instance, -1);
20624 transmit_response(pvt, "400 Bad Request", req);
20625 return -1;
20626 }
20627
20628 if (!strcmp(status, "queued")) {
20629
20630
20631
20632
20633 ast_cc_monitor_request_acked(monitor_instance->core_id, "SIP endpoint %s accepted request", monitor_instance->device_name);
20634 transmit_response(pvt, "200 OK", req);
20635 ao2_ref(monitor_instance, -1);
20636 return 0;
20637 }
20638
20639
20640 uri = get_body(req, "cc-URI", ':');
20641 if (ast_strlen_zero(uri)) {
20642 uri = get_in_brackets((char *)get_header(req, "From"));
20643 }
20644
20645 ast_string_field_set(monitor_instance, notify_uri, uri);
20646 if (monitor_instance->suspension_entry) {
20647 cc_entry = monitor_instance->suspension_entry->instance_data;
20648 if (cc_entry->current_state == CC_CLOSED) {
20649
20650
20651
20652
20653
20654 construct_pidf_body(CC_CLOSED, monitor_instance->suspension_entry->body,
20655 sizeof(monitor_instance->suspension_entry->body), monitor_instance->peername);
20656 transmit_publish(monitor_instance->suspension_entry, SIP_PUBLISH_INITIAL, monitor_instance->notify_uri);
20657 } else {
20658 ast_cc_monitor_callee_available(monitor_instance->core_id, "SIP monitored callee has become available");
20659 }
20660 } else {
20661 ast_cc_monitor_callee_available(monitor_instance->core_id, "SIP monitored callee has become available");
20662 }
20663 ao2_ref(monitor_instance, -1);
20664 transmit_response(pvt, "200 OK", req);
20665
20666 return 0;
20667 }
20668
20669
20670 static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e)
20671 {
20672
20673
20674 int res = 0;
20675 const char *event = get_header(req, "Event");
20676 char *eventid = NULL;
20677 char *sep;
20678
20679 if( (sep = strchr(event, ';')) ) {
20680 *sep++ = '\0';
20681 eventid = sep;
20682 }
20683
20684 if (sipdebug)
20685 ast_debug(2, "Got NOTIFY Event: %s\n", event);
20686
20687 if (!strcmp(event, "refer")) {
20688
20689
20690
20691
20692
20693 char buf[1024];
20694 char *cmd, *code;
20695 int respcode;
20696 int success = TRUE;
20697
20698
20699
20700
20701
20702
20703
20704
20705 if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
20706
20707 transmit_response(p, "400 Bad request", req);
20708 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20709 return -1;
20710 }
20711
20712
20713 if (get_msg_text(buf, sizeof(buf), req, TRUE)) {
20714 ast_log(LOG_WARNING, "Unable to retrieve attachment from NOTIFY %s\n", p->callid);
20715 transmit_response(p, "400 Bad request", req);
20716 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20717 return -1;
20718 }
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740 ast_debug(3, "* SIP Transfer NOTIFY Attachment: \n---%s\n---\n", buf);
20741 cmd = ast_skip_blanks(buf);
20742 code = cmd;
20743
20744 while(*code && (*code > 32)) {
20745 code++;
20746 }
20747 *code++ = '\0';
20748 code = ast_skip_blanks(code);
20749 sep = code;
20750 sep++;
20751 while(*sep && (*sep > 32)) {
20752 sep++;
20753 }
20754 *sep++ = '\0';
20755 respcode = atoi(code);
20756 switch (respcode) {
20757 case 200:
20758
20759 break;
20760 case 301:
20761 case 302:
20762
20763 success = FALSE;
20764 break;
20765 case 503:
20766 case 603:
20767
20768 success = FALSE;
20769 break;
20770 case 0:
20771
20772 ast_log(LOG_NOTICE, "Error parsing sipfrag in NOTIFY in response to REFER.\n");
20773 success = FALSE;
20774 break;
20775 default:
20776 if (respcode < 200) {
20777
20778 success = -1;
20779 } else {
20780 ast_log(LOG_NOTICE, "Got unknown code '%d' in NOTIFY in response to REFER.\n", respcode);
20781 success = FALSE;
20782 }
20783 break;
20784 }
20785 if (success == FALSE) {
20786 ast_log(LOG_NOTICE, "Transfer failed. Sorry. Nothing further to do with this call\n");
20787 }
20788
20789 if (p->owner && success != -1) {
20790 enum ast_control_transfer message = success ? AST_TRANSFER_SUCCESS : AST_TRANSFER_FAILED;
20791 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
20792 }
20793
20794 transmit_response(p, "200 OK", req);
20795 } else if (!strcmp(event, "message-summary")) {
20796 const char *mailbox = NULL;
20797 char *c = ast_strdupa(get_body(req, "Voice-Message", ':'));
20798
20799 if (!p->mwi) {
20800 struct sip_peer *peer = find_peer(NULL, &p->recv, TRUE, FINDPEERS, FALSE, p->socket.type);
20801
20802 if (peer) {
20803 mailbox = ast_strdupa(peer->unsolicited_mailbox);
20804 unref_peer(peer, "removing unsolicited mwi ref");
20805 }
20806 } else {
20807 mailbox = p->mwi->mailbox;
20808 }
20809
20810 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(c)) {
20811 char *old = strsep(&c, " ");
20812 char *new = strsep(&old, "/");
20813 struct ast_event *event;
20814
20815 if ((event = ast_event_new(AST_EVENT_MWI,
20816 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
20817 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, "SIP_Remote",
20818 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, atoi(new),
20819 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, atoi(old),
20820 AST_EVENT_IE_END))) {
20821 ast_event_queue_and_cache(event);
20822 }
20823 transmit_response(p, "200 OK", req);
20824 } else {
20825 transmit_response(p, "489 Bad event", req);
20826 res = -1;
20827 }
20828 } else if (!strcmp(event, "keep-alive")) {
20829
20830
20831 transmit_response(p, "200 OK", req);
20832 } else if (!strcmp(event, "call-completion")) {
20833 res = handle_cc_notify(p, req);
20834 } else {
20835
20836 transmit_response(p, "489 Bad event", req);
20837 res = -1;
20838 }
20839
20840 if (!p->lastinvite)
20841 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20842
20843 return res;
20844 }
20845
20846
20847
20848
20849 static int handle_request_options(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e)
20850 {
20851 int res;
20852
20853 if (p->lastinvite) {
20854
20855 transmit_response_with_allow(p, "200 OK", req, 0);
20856 return 0;
20857 }
20858
20859 if (sip_cfg.auth_options_requests) {
20860
20861 copy_request(&p->initreq, req);
20862 set_pvt_allowed_methods(p, req);
20863 res = check_user(p, req, SIP_OPTIONS, e, XMIT_UNRELIABLE, addr);
20864 if (res == AUTH_CHALLENGE_SENT) {
20865 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20866 return 0;
20867 }
20868 if (res < 0) {
20869 if (res == AUTH_FAKE_AUTH) {
20870 ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
20871 transmit_fake_auth_response(p, SIP_OPTIONS, req, XMIT_UNRELIABLE);
20872 } else {
20873 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
20874 transmit_response(p, "403 Forbidden", req);
20875 }
20876 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20877 return 0;
20878 }
20879 }
20880
20881
20882 res = (get_destination(p, req, NULL) == SIP_GET_DEST_EXTEN_FOUND ? 0 : -1);
20883 build_contact(p);
20884
20885 if (ast_strlen_zero(p->context))
20886 ast_string_field_set(p, context, sip_cfg.default_context);
20887
20888 if (ast_shutting_down())
20889 transmit_response_with_allow(p, "503 Unavailable", req, 0);
20890 else if (res < 0)
20891 transmit_response_with_allow(p, "404 Not Found", req, 0);
20892 else
20893 transmit_response_with_allow(p, "200 OK", req, 0);
20894
20895
20896
20897 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20898
20899 return res;
20900 }
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914 static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *nounlock)
20915 {
20916 int earlyreplace = 0;
20917 int oneleggedreplace = 0;
20918 struct ast_channel *c = p->owner;
20919 struct ast_channel *replacecall = p->refer->refer_call->owner;
20920 struct ast_channel *targetcall;
20921
20922
20923 if (replacecall->_state == AST_STATE_RING)
20924 earlyreplace = 1;
20925
20926
20927 if (!(targetcall = ast_bridged_channel(replacecall))) {
20928
20929 if (!earlyreplace) {
20930 ast_debug(2, " Attended transfer attempted to replace call with no bridge (maybe ringing). Channel %s!\n", replacecall->name);
20931 oneleggedreplace = 1;
20932 }
20933 }
20934 if (targetcall && targetcall->_state == AST_STATE_RINGING)
20935 ast_debug(4, "SIP transfer: Target channel is in ringing state\n");
20936
20937 if (targetcall)
20938 ast_debug(4, "SIP transfer: Invite Replace incoming channel should bridge to channel %s while hanging up channel %s\n", targetcall->name, replacecall->name);
20939 else
20940 ast_debug(4, "SIP transfer: Invite Replace incoming channel should replace and hang up channel %s (one call leg)\n", replacecall->name);
20941
20942 if (req->ignore) {
20943 ast_log(LOG_NOTICE, "Ignoring this INVITE with replaces in a stupid way.\n");
20944
20945
20946
20947 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE, FALSE);
20948
20949 if (c) {
20950 *nounlock = 1;
20951 ast_channel_unlock(c);
20952 }
20953 ast_channel_unlock(replacecall);
20954 sip_pvt_unlock(p->refer->refer_call);
20955 return 1;
20956 }
20957 if (!c) {
20958
20959 ast_log(LOG_ERROR, "Unable to create new channel. Invite/replace failed.\n");
20960 transmit_response_reliable(p, "503 Service Unavailable", req);
20961 append_history(p, "Xfer", "INVITE/Replace Failed. No new channel.");
20962 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20963 ast_channel_unlock(replacecall);
20964 sip_pvt_unlock(p->refer->refer_call);
20965 return 1;
20966 }
20967 append_history(p, "Xfer", "INVITE/Replace received");
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979 transmit_response(p, "100 Trying", req);
20980 ast_setstate(c, AST_STATE_RING);
20981
20982
20983
20984
20985
20986 transmit_response_with_sdp(p, "200 OK", req, XMIT_RELIABLE, FALSE, FALSE);
20987
20988 ast_setstate(c, AST_STATE_UP);
20989
20990
20991 ast_quiet_chan(replacecall);
20992 ast_quiet_chan(targetcall);
20993 ast_debug(4, "Invite/Replaces: preparing to masquerade %s into %s\n", c->name, replacecall->name);
20994
20995
20996 if (! earlyreplace && ! oneleggedreplace )
20997 ast_set_flag(&p->refer->refer_call->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
20998
20999
21000 if(ast_channel_masquerade(replacecall, c))
21001 ast_log(LOG_ERROR, "Failed to masquerade C into Replacecall\n");
21002 else
21003 ast_debug(4, "Invite/Replaces: Going to masquerade %s into %s\n", c->name, replacecall->name);
21004
21005
21006
21007
21008
21009 ast_channel_ref(c);
21010 ast_channel_unlock(replacecall);
21011 ast_channel_unlock(c);
21012 sip_pvt_unlock(p->refer->refer_call);
21013 sip_pvt_unlock(p);
21014 if (ast_do_masquerade(replacecall)) {
21015 ast_log(LOG_WARNING, "Failed to perform masquerade with INVITE replaces\n");
21016 }
21017 ast_channel_lock(c);
21018 if (earlyreplace || oneleggedreplace ) {
21019 c->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
21020 }
21021 ast_setstate(c, AST_STATE_DOWN);
21022 ast_channel_unlock(c);
21023
21024
21025 c->tech_pvt = dialog_unref(c->tech_pvt, "unref dialog c->tech_pvt");
21026
21027
21028 ast_hangup(c);
21029
21030 *nounlock = 1;
21031
21032 sip_pvt_lock(p);
21033 ast_channel_unref(c);
21034 return 0;
21035 }
21036
21037
21038 static int do_magic_pickup(struct ast_channel *channel, const char *extension, const char *context)
21039 {
21040 struct ast_str *str = ast_str_alloca(AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2);
21041 struct ast_app *pickup = pbx_findapp("Pickup");
21042
21043 if (!pickup) {
21044 ast_log(LOG_ERROR, "Unable to perform pickup: Application 'Pickup' not loaded (app_directed_pickup.so).\n");
21045 return -1;
21046 }
21047
21048 ast_str_set(&str, 0, "%s@%s", extension, sip_cfg.notifycid == IGNORE_CONTEXT ? "PICKUPMARK" : context);
21049
21050 ast_debug(2, "About to call Pickup(%s)\n", str->str);
21051
21052
21053
21054
21055 pbx_exec(channel, pickup, str->str);
21056
21057 return 0;
21058 }
21059
21060
21061 static int sip_t38_abort(const void *data)
21062 {
21063 struct sip_pvt *p = (struct sip_pvt *) data;
21064
21065 sip_pvt_lock(p);
21066
21067
21068
21069
21070
21071 if (p->t38id != -1) {
21072 change_t38_state(p, T38_DISABLED);
21073 transmit_response_reliable(p, "488 Not acceptable here", &p->initreq);
21074 p->t38id = -1;
21075 dialog_unref(p, "unref the dialog ptr from sip_t38_abort, because it held a dialog ptr");
21076 }
21077 sip_pvt_unlock(p);
21078 return 0;
21079 }
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092 static int handle_request_update(struct sip_pvt *p, struct sip_request *req)
21093 {
21094 if (ast_strlen_zero(get_header(req, "X-Asterisk-rpid-update"))) {
21095 transmit_response(p, "501 Method Not Implemented", req);
21096 return 0;
21097 }
21098 if (get_rpid(p, req)) {
21099 struct ast_party_connected_line connected;
21100 struct ast_set_party_connected_line update_connected;
21101 ast_party_connected_line_init(&connected);
21102 memset(&update_connected, 0, sizeof(update_connected));
21103 if (p->cid_num) {
21104 update_connected.id.number = 1;
21105 connected.id.number.valid = 1;
21106 connected.id.number.str = (char *) p->cid_num;
21107 connected.id.number.presentation = p->callingpres;
21108 }
21109 if (p->cid_name) {
21110 update_connected.id.name = 1;
21111 connected.id.name.valid = 1;
21112 connected.id.name.str = (char *) p->cid_name;
21113 connected.id.name.presentation = p->callingpres;
21114 }
21115 connected.id.tag = (char *) p->cid_tag;
21116 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
21117 ast_channel_queue_connected_line_update(p->owner, &connected, &update_connected);
21118 }
21119 transmit_response(p, "200 OK", req);
21120 return 0;
21121 }
21122
21123
21124
21125
21126
21127
21128
21129
21130 static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock)
21131 {
21132 int res = 1;
21133 int gotdest;
21134 const char *p_replaces;
21135 char *replace_id = NULL;
21136 int refer_locked = 0;
21137 const char *required;
21138 unsigned int required_profile = 0;
21139 struct ast_channel *c = NULL;
21140 struct sip_peer *authpeer = NULL;
21141 int reinvite = 0;
21142 int rtn;
21143 struct ast_party_redirecting redirecting;
21144 struct ast_set_party_redirecting update_redirecting;
21145
21146 const char *p_uac_se_hdr;
21147 const char *p_uac_min_se;
21148 int uac_max_se = -1;
21149 int uac_min_se = -1;
21150 int st_active = FALSE;
21151 int st_interval = 0;
21152 enum st_refresher st_ref;
21153 int dlg_min_se = -1;
21154 struct {
21155 char exten[AST_MAX_EXTENSION];
21156 char context[AST_MAX_CONTEXT];
21157 } pickup = {
21158 .exten = "",
21159 };
21160 st_ref = SESSION_TIMER_REFRESHER_AUTO;
21161
21162
21163 if (!p->sipoptions) {
21164 const char *supported = get_header(req, "Supported");
21165 if (!ast_strlen_zero(supported)) {
21166 p->sipoptions = parse_sip_options(supported, NULL, 0);
21167 }
21168 }
21169
21170
21171 required = get_header(req, "Require");
21172 if (!ast_strlen_zero(required)) {
21173 char unsupported[256] = { 0, };
21174 required_profile = parse_sip_options(required, unsupported, ARRAY_LEN(unsupported));
21175
21176
21177
21178 if (!ast_strlen_zero(unsupported)) {
21179 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, unsupported);
21180 ast_log(LOG_WARNING, "Received SIP INVITE with unsupported required extension: required:%s unsupported:%s\n", required, unsupported);
21181 p->invitestate = INV_COMPLETED;
21182 if (!p->lastinvite)
21183 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21184 res = -1;
21185 goto request_invite_cleanup;
21186 }
21187 }
21188
21189
21190
21191 p->sipoptions |= required_profile;
21192 p->reqsipoptions = required_profile;
21193
21194
21195 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) {
21196
21197
21198
21199
21200
21201 int different;
21202 const char *initial_rlPart2 = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);
21203 const char *this_rlPart2 = REQ_OFFSET_TO_STR(req, rlPart2);
21204 if (sip_cfg.pedanticsipchecking)
21205 different = sip_uri_cmp(initial_rlPart2, this_rlPart2);
21206 else
21207 different = strcmp(initial_rlPart2, this_rlPart2);
21208 if (!different) {
21209 transmit_response(p, "482 Loop Detected", req);
21210 p->invitestate = INV_COMPLETED;
21211 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21212 res = 0;
21213 goto request_invite_cleanup;
21214 } else {
21215
21216
21217
21218
21219
21220
21221
21222 char *uri = ast_strdupa(this_rlPart2);
21223 char *at = strchr(uri, '@');
21224 char *peerorhost;
21225 ast_debug(2, "Potential spiral detected. Original RURI was %s, new RURI is %s\n", initial_rlPart2, this_rlPart2);
21226 transmit_response(p, "100 Trying", req);
21227 if (at) {
21228 *at = '\0';
21229 }
21230
21231 if ((peerorhost = strchr(uri, ':'))) {
21232 *peerorhost++ = '\0';
21233 }
21234 ast_string_field_set(p, theirtag, NULL);
21235
21236
21237 ast_string_field_set(p->owner, call_forward, peerorhost);
21238 ast_queue_control(p->owner, AST_CONTROL_BUSY);
21239 res = 0;
21240 goto request_invite_cleanup;
21241 }
21242 }
21243
21244 if (!req->ignore && p->pendinginvite) {
21245 if (!ast_test_flag(&p->flags[0], SIP_OUTGOING) && (p->invitestate == INV_COMPLETED || p->invitestate == INV_TERMINATED)) {
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256 __sip_ack(p, p->pendinginvite, 1, 0);
21257 } else {
21258
21259 p->glareinvite = seqno;
21260 if (p->rtp && find_sdp(req)) {
21261 struct ast_sockaddr addr;
21262 if (get_ip_and_port_from_sdp(req, SDP_AUDIO, &addr)) {
21263 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Audio may not work properly on this call.\n");
21264 } else {
21265 ast_rtp_instance_set_alt_remote_address(p->rtp, &addr);
21266 }
21267 if (p->vrtp) {
21268 if (get_ip_and_port_from_sdp(req, SDP_VIDEO, &addr)) {
21269 ast_log(LOG_WARNING, "Failed to set an alternate media source on glared reinvite. Video may not work properly on this call.\n");
21270 } else {
21271 ast_rtp_instance_set_alt_remote_address(p->vrtp, &addr);
21272 }
21273 }
21274 }
21275 transmit_response_reliable(p, "491 Request Pending", req);
21276 ast_debug(1, "Got INVITE on call where we already have pending INVITE, deferring that - %s\n", p->callid);
21277
21278 res = 0;
21279 goto request_invite_cleanup;
21280 }
21281 }
21282
21283 p_replaces = get_header(req, "Replaces");
21284 if (!ast_strlen_zero(p_replaces)) {
21285
21286 char *ptr;
21287 char *fromtag = NULL;
21288 char *totag = NULL;
21289 char *start, *to;
21290 int error = 0;
21291
21292 if (p->owner) {
21293 ast_debug(3, "INVITE w Replaces on existing call? Refusing action. [%s]\n", p->callid);
21294 transmit_response_reliable(p, "400 Bad request", req);
21295
21296 res = -1;
21297 goto request_invite_cleanup;
21298 }
21299
21300 if (sipdebug)
21301 ast_debug(3, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
21302
21303 replace_id = ast_strdupa(p_replaces);
21304 ast_uri_decode(replace_id);
21305
21306 if (!p->refer && !sip_refer_allocate(p)) {
21307 transmit_response_reliable(p, "500 Server Internal Error", req);
21308 append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
21309 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21310 p->invitestate = INV_COMPLETED;
21311 res = -1;
21312 goto request_invite_cleanup;
21313 }
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324 replace_id = ast_skip_blanks(replace_id);
21325
21326 start = replace_id;
21327 while ( (ptr = strsep(&start, ";")) ) {
21328 ptr = ast_skip_blanks(ptr);
21329 if ( (to = strcasestr(ptr, "to-tag=") ) )
21330 totag = to + 7;
21331 else if ( (to = strcasestr(ptr, "from-tag=") ) ) {
21332 fromtag = to + 9;
21333 fromtag = strsep(&fromtag, "&");
21334 }
21335 }
21336
21337 if (sipdebug)
21338 ast_debug(4, "Invite/replaces: Will use Replace-Call-ID : %s Fromtag: %s Totag: %s\n",
21339 replace_id,
21340 fromtag ? fromtag : "<no from tag>",
21341 totag ? totag : "<no to tag>");
21342
21343
21344
21345
21346
21347 if (strncmp(replace_id, "pickup-", 7) == 0) {
21348 struct sip_pvt *subscription = NULL;
21349 replace_id += 7;
21350
21351 if ((subscription = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
21352 ast_log(LOG_NOTICE, "Unable to find subscription with call-id: %s\n", replace_id);
21353 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
21354 error = 1;
21355 } else {
21356 ast_log(LOG_NOTICE, "Trying to pick up %s@%s\n", subscription->exten, subscription->context);
21357 ast_copy_string(pickup.exten, subscription->exten, sizeof(pickup.exten));
21358 ast_copy_string(pickup.context, subscription->context, sizeof(pickup.context));
21359 sip_pvt_unlock(subscription);
21360 if (subscription->owner) {
21361 ast_channel_unlock(subscription->owner);
21362 }
21363 }
21364 }
21365
21366
21367 if (!error && ast_strlen_zero(pickup.exten) && (p->refer->refer_call = get_sip_pvt_byid_locked(replace_id, totag, fromtag)) == NULL) {
21368 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existent call id (%s)!\n", replace_id);
21369 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replaces)", req);
21370 error = 1;
21371 } else {
21372 refer_locked = 1;
21373 }
21374
21375
21376
21377
21378
21379 if (p->refer->refer_call == p) {
21380 ast_log(LOG_NOTICE, "INVITE with replaces into it's own call id (%s == %s)!\n", replace_id, p->callid);
21381 p->refer->refer_call = dialog_unref(p->refer->refer_call, "unref dialog p->refer->refer_call");
21382 transmit_response_reliable(p, "400 Bad request", req);
21383 error = 1;
21384 }
21385
21386 if (!error && ast_strlen_zero(pickup.exten) && !p->refer->refer_call->owner) {
21387
21388 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-existing call id (%s)!\n", replace_id);
21389
21390 transmit_response_reliable(p, "481 Call Leg Does Not Exist (Replace)", req);
21391 error = 1;
21392 }
21393
21394 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) {
21395 ast_log(LOG_NOTICE, "Supervised transfer attempted to replace non-ringing or active call id (%s)!\n", replace_id);
21396 transmit_response_reliable(p, "603 Declined (Replaces)", req);
21397 error = 1;
21398 }
21399
21400 if (error) {
21401 append_history(p, "Xfer", "INVITE/Replace Failed.");
21402 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21403 sip_pvt_unlock(p);
21404 if (p->refer->refer_call) {
21405 sip_pvt_unlock(p->refer->refer_call);
21406 if (p->refer->refer_call->owner) {
21407 ast_channel_unlock(p->refer->refer_call->owner);
21408 }
21409 }
21410 refer_locked = 0;
21411 p->invitestate = INV_COMPLETED;
21412 res = -1;
21413 goto request_invite_cleanup;
21414 }
21415 }
21416
21417
21418
21419
21420 if (!req->ignore) {
21421 int newcall = (p->initreq.headers ? TRUE : FALSE);
21422
21423 if (sip_cancel_destroy(p))
21424 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
21425
21426 p->pendinginvite = seqno;
21427 check_via(p, req);
21428
21429 copy_request(&p->initreq, req);
21430 if (sipdebug)
21431 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
21432 if (!p->owner) {
21433 if (debug)
21434 ast_verbose("Using INVITE request as basis request - %s\n", p->callid);
21435 if (newcall)
21436 append_history(p, "Invite", "New call: %s", p->callid);
21437 parse_ok_contact(p, req);
21438 } else {
21439 ast_clear_flag(&p->flags[0], SIP_OUTGOING);
21440 if (get_rpid(p, req)) {
21441 struct ast_party_connected_line connected;
21442 struct ast_set_party_connected_line update_connected;
21443
21444 ast_party_connected_line_init(&connected);
21445 memset(&update_connected, 0, sizeof(update_connected));
21446 if (p->cid_num) {
21447 update_connected.id.number = 1;
21448 connected.id.number.valid = 1;
21449 connected.id.number.str = (char *) p->cid_num;
21450 connected.id.number.presentation = p->callingpres;
21451 }
21452 if (p->cid_name) {
21453 update_connected.id.name = 1;
21454 connected.id.name.valid = 1;
21455 connected.id.name.str = (char *) p->cid_name;
21456 connected.id.name.presentation = p->callingpres;
21457 }
21458 connected.id.tag = (char *) p->cid_tag;
21459 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
21460 ast_channel_queue_connected_line_update(p->owner, &connected,
21461 &update_connected);
21462 }
21463
21464 if (find_sdp(req)) {
21465 if (process_sdp(p, req, SDP_T38_INITIATE)) {
21466 if (!ast_strlen_zero(get_header(req, "Content-Encoding"))) {
21467
21468
21469
21470 transmit_response_reliable(p, "415 Unsupported Media type", req);
21471 } else {
21472 transmit_response_reliable(p, "488 Not acceptable here", req);
21473 }
21474 if (!p->lastinvite)
21475 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21476 res = -1;
21477 goto request_invite_cleanup;
21478 }
21479 ast_queue_control(p->owner, AST_CONTROL_SRCUPDATE);
21480 } else {
21481 p->jointcapability = p->capability;
21482 ast_debug(1, "Hm.... No sdp for the moment\n");
21483
21484
21485
21486 if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
21487 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
21488
21489 ast_queue_frame(p->owner, &ast_null_frame);
21490 change_hold_state(p, req, FALSE, 0);
21491 }
21492 }
21493 if (p->do_history)
21494 append_history(p, "ReInv", "Re-invite received");
21495 }
21496 } else if (debug)
21497 ast_verbose("Ignoring this INVITE request\n");
21498
21499 if (!p->lastinvite && !req->ignore && !p->owner) {
21500
21501
21502 int cc_recall_core_id = -1;
21503 set_pvt_allowed_methods(p, req);
21504 res = check_user_full(p, req, SIP_INVITE, e, XMIT_RELIABLE, addr, &authpeer);
21505 if (res == AUTH_CHALLENGE_SENT) {
21506 p->invitestate = INV_COMPLETED;
21507 res = 0;
21508 goto request_invite_cleanup;
21509 }
21510 if (res < 0) {
21511 if (res == AUTH_FAKE_AUTH) {
21512 ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
21513 transmit_fake_auth_response(p, SIP_INVITE, req, XMIT_RELIABLE);
21514 } else {
21515 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
21516 transmit_response_reliable(p, "403 Forbidden", req);
21517 }
21518 p->invitestate = INV_COMPLETED;
21519 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21520 ast_string_field_set(p, theirtag, NULL);
21521 res = 0;
21522 goto request_invite_cleanup;
21523 }
21524
21525
21526 if (p->relatedpeer) {
21527 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting the relatedpeer field in the dialog, before it is set to something else.");
21528 }
21529 if (authpeer) {
21530 p->relatedpeer = ref_peer(authpeer, "setting dialog's relatedpeer pointer");
21531 }
21532
21533 if (ast_test_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT) && !p->udptl) {
21534 if ((p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &bindaddr))) {
21535 p->t38_maxdatagram = global_t38_maxdatagram;
21536 set_t38_capabilities(p);
21537 } else {
21538
21539 ast_debug(1, "UDPTL creation failed on dialog.\n");
21540 ast_clear_flag(&p->flags[1], SIP_PAGE2_T38SUPPORT);
21541 }
21542 }
21543
21544 req->authenticated = 1;
21545
21546
21547 if (find_sdp(req)) {
21548 if (process_sdp(p, req, SDP_T38_INITIATE)) {
21549
21550
21551
21552 if (!ast_strlen_zero(get_header(req, "Content-Encoding"))) {
21553 transmit_response_reliable(p, "415 Unsupported Media type", req);
21554 } else {
21555
21556 transmit_response_reliable(p, "488 Not acceptable here", req);
21557 }
21558 p->invitestate = INV_COMPLETED;
21559 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21560 ast_debug(1, "No compatible codecs for this SIP call.\n");
21561 res = -1;
21562 goto request_invite_cleanup;
21563 }
21564 } else {
21565 p->jointcapability = p->capability;
21566 ast_debug(2, "No SDP in Invite, third party call control\n");
21567 }
21568
21569
21570
21571 if (p->owner)
21572 ast_queue_frame(p->owner, &ast_null_frame);
21573
21574
21575
21576 if (ast_strlen_zero(p->context))
21577 ast_string_field_set(p, context, sip_cfg.default_context);
21578
21579
21580
21581 ast_debug(1, "Checking SIP call limits for device %s\n", p->username);
21582 if ((res = update_call_counter(p, INC_CALL_LIMIT))) {
21583 if (res < 0) {
21584 ast_log(LOG_NOTICE, "Failed to place call for device %s, too many calls\n", p->username);
21585 transmit_response_reliable(p, "480 Temporarily Unavailable (Call limit) ", req);
21586 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21587 p->invitestate = INV_COMPLETED;
21588 }
21589 res = 0;
21590 goto request_invite_cleanup;
21591 }
21592 gotdest = get_destination(p, NULL, &cc_recall_core_id);
21593 extract_uri(p, req);
21594 build_contact(p);
21595
21596 if (p->rtp) {
21597 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
21598 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&p->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
21599 }
21600
21601 if (!replace_id && (gotdest != SIP_GET_DEST_EXTEN_FOUND)) {
21602 switch(gotdest) {
21603 case SIP_GET_DEST_INVALID_URI:
21604 transmit_response_reliable(p, "416 Unsupported URI scheme", req);
21605 break;
21606 case SIP_GET_DEST_PICKUP_EXTEN_FOUND:
21607 if (ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWOVERLAP)) {
21608 transmit_response_reliable(p, "484 Address Incomplete", req);
21609 break;
21610 }
21611
21612 case SIP_GET_DEST_EXTEN_NOT_FOUND:
21613 case SIP_GET_DEST_REFUSED:
21614 default:
21615 {
21616 char *decoded_exten = ast_strdupa(p->exten);
21617 transmit_response_reliable(p, "404 Not Found", req);
21618 ast_uri_decode(decoded_exten);
21619 ast_log(LOG_NOTICE, "Call from '%s' to extension"
21620 " '%s' rejected because extension not found in context '%s'.\n",
21621 S_OR(p->username, p->peername), decoded_exten, p->context);
21622 }
21623 }
21624
21625 p->invitestate = INV_COMPLETED;
21626 update_call_counter(p, DEC_CALL_LIMIT);
21627 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21628 res = 0;
21629 goto request_invite_cleanup;
21630 } else {
21631
21632
21633
21634 if (ast_strlen_zero(p->exten))
21635 ast_string_field_set(p, exten, "s");
21636
21637
21638 make_our_tag(p->tag, sizeof(p->tag));
21639
21640
21641 c = sip_new(p, AST_STATE_DOWN, S_OR(p->peername, NULL), NULL);
21642 if (cc_recall_core_id != -1) {
21643 ast_setup_cc_recall_datastore(c, cc_recall_core_id);
21644 ast_cc_agent_set_interfaces_chanvar(c);
21645 }
21646 *recount = 1;
21647
21648
21649 build_route(p, req, 0);
21650
21651 if (c) {
21652 ast_party_redirecting_init(&redirecting);
21653 memset(&update_redirecting, 0, sizeof(update_redirecting));
21654
21655 ast_channel_lock(c);
21656 change_redirecting_information(p, req, &redirecting, &update_redirecting,
21657 FALSE);
21658 ast_channel_set_redirecting(c, &redirecting, &update_redirecting);
21659 ast_party_redirecting_free(&redirecting);
21660 }
21661 }
21662 } else {
21663 ast_party_redirecting_init(&redirecting);
21664 memset(&update_redirecting, 0, sizeof(update_redirecting));
21665 if (sipdebug) {
21666 if (!req->ignore)
21667 ast_debug(2, "Got a SIP re-invite for call %s\n", p->callid);
21668 else
21669 ast_debug(2, "Got a SIP re-transmit of INVITE for call %s\n", p->callid);
21670 }
21671 if (!req->ignore)
21672 reinvite = 1;
21673 c = p->owner;
21674 change_redirecting_information(p, req, &redirecting, &update_redirecting, FALSE);
21675 if (c) {
21676 ast_channel_set_redirecting(c, &redirecting, &update_redirecting);
21677 }
21678 ast_party_redirecting_free(&redirecting);
21679 }
21680
21681
21682 if ((p->sipoptions & SIP_OPT_TIMER) && !ast_strlen_zero(get_header(req, "Session-Expires"))) {
21683
21684
21685 ast_debug(2, "Incoming INVITE with 'timer' option supported and \"Session-Expires\" header.\n");
21686
21687
21688 if (!p->stimer)
21689 sip_st_alloc(p);
21690
21691
21692 p_uac_se_hdr = get_header(req, "Session-Expires");
21693 rtn = parse_session_expires(p_uac_se_hdr, &uac_max_se, &st_ref);
21694 if (rtn != 0) {
21695 transmit_response_reliable(p, "400 Session-Expires Invalid Syntax", req);
21696 p->invitestate = INV_COMPLETED;
21697 if (!p->lastinvite) {
21698 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21699 }
21700 res = -1;
21701 goto request_invite_cleanup;
21702 }
21703
21704
21705 p_uac_min_se = get_header(req, "Min-SE");
21706 if (!ast_strlen_zero(p_uac_min_se)) {
21707 rtn = parse_minse(p_uac_min_se, &uac_min_se);
21708 if (rtn != 0) {
21709 transmit_response_reliable(p, "400 Min-SE Invalid Syntax", req);
21710 p->invitestate = INV_COMPLETED;
21711 if (!p->lastinvite) {
21712 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21713 }
21714 res = -1;
21715 goto request_invite_cleanup;
21716 }
21717 }
21718
21719 dlg_min_se = st_get_se(p, FALSE);
21720 switch (st_get_mode(p)) {
21721 case SESSION_TIMER_MODE_ACCEPT:
21722 case SESSION_TIMER_MODE_ORIGINATE:
21723 if (uac_max_se > 0 && uac_max_se < dlg_min_se) {
21724 transmit_response_with_minse(p, "422 Session Interval Too Small", req, dlg_min_se);
21725 p->invitestate = INV_COMPLETED;
21726 if (!p->lastinvite) {
21727 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21728 }
21729 res = -1;
21730 goto request_invite_cleanup;
21731 }
21732
21733 p->stimer->st_active_peer_ua = TRUE;
21734 st_active = TRUE;
21735 if (st_ref == SESSION_TIMER_REFRESHER_AUTO) {
21736 st_ref = st_get_refresher(p);
21737 }
21738
21739 if (uac_max_se > 0) {
21740 int dlg_max_se = st_get_se(p, TRUE);
21741 if (dlg_max_se >= uac_min_se) {
21742 st_interval = (uac_max_se < dlg_max_se) ? uac_max_se : dlg_max_se;
21743 } else {
21744 st_interval = uac_max_se;
21745 }
21746 } else {
21747
21748 st_interval = global_max_se;
21749 }
21750 break;
21751
21752 case SESSION_TIMER_MODE_REFUSE:
21753 if (p->reqsipoptions & SIP_OPT_TIMER) {
21754 transmit_response_with_unsupported(p, "420 Option Disabled", req, required);
21755 ast_log(LOG_WARNING, "Received SIP INVITE with supported but disabled option: %s\n", required);
21756 p->invitestate = INV_COMPLETED;
21757 if (!p->lastinvite) {
21758 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21759 }
21760 res = -1;
21761 goto request_invite_cleanup;
21762 }
21763 break;
21764
21765 default:
21766 ast_log(LOG_ERROR, "Internal Error %d at %s:%d\n", st_get_mode(p), __FILE__, __LINE__);
21767 break;
21768 }
21769 } else {
21770
21771
21772
21773 switch (st_get_mode(p)) {
21774 case SESSION_TIMER_MODE_ORIGINATE:
21775 st_active = TRUE;
21776 st_interval = st_get_se(p, TRUE);
21777 st_ref = SESSION_TIMER_REFRESHER_UAS;
21778 p->stimer->st_active_peer_ua = FALSE;
21779 break;
21780
21781 default:
21782 break;
21783 }
21784 }
21785
21786 if (reinvite == 0) {
21787
21788 if (st_active == TRUE) {
21789 p->stimer->st_active = TRUE;
21790 p->stimer->st_interval = st_interval;
21791 p->stimer->st_ref = st_ref;
21792 start_session_timer(p);
21793 }
21794 } else {
21795 if (p->stimer->st_active == TRUE) {
21796
21797
21798
21799 ast_debug (2, "Restarting session-timers on a refresh - %s\n", p->callid);
21800
21801
21802 if (st_interval > 0) {
21803 p->stimer->st_interval = st_interval;
21804 p->stimer->st_ref = st_ref;
21805 }
21806
21807 restart_session_timer(p);
21808 if (p->stimer->st_expirys > 0) {
21809 p->stimer->st_expirys--;
21810 }
21811 }
21812 }
21813
21814 if (!req->ignore && p)
21815 p->lastinvite = seqno;
21816
21817 if (c && replace_id) {
21818 if (!ast_strlen_zero(pickup.exten)) {
21819 append_history(p, "Xfer", "INVITE/Replace received");
21820
21821
21822 transmit_response(p, "100 Trying", req);
21823 p->invitestate = INV_PROCEEDING;
21824 ast_setstate(c, AST_STATE_RING);
21825
21826
21827 ast_channel_unlock(c);
21828 *nounlock = 1;
21829
21830
21831
21832
21833
21834
21835 sip_pvt_unlock(p);
21836 do_magic_pickup(c, pickup.exten, pickup.context);
21837
21838 ast_hangup(c);
21839 sip_pvt_lock(p);
21840
21841 res = 0;
21842 goto request_invite_cleanup;
21843 } else {
21844
21845 if (sipdebug)
21846 ast_debug(4, "Sending this call to the invite/replcaes handler %s\n", p->callid);
21847 res = handle_invite_replaces(p, req, debug, seqno, addr, nounlock);
21848 refer_locked = 0;
21849 goto request_invite_cleanup;
21850 }
21851 }
21852
21853
21854 if (c) {
21855 enum ast_channel_state c_state = c->_state;
21856
21857 if (c_state != AST_STATE_UP && reinvite &&
21858 (p->invitestate == INV_TERMINATED || p->invitestate == INV_CONFIRMED)) {
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868 c_state = AST_STATE_UP;
21869 }
21870
21871 switch(c_state) {
21872 case AST_STATE_DOWN:
21873 ast_debug(2, "%s: New call is still down.... Trying... \n", c->name);
21874 transmit_provisional_response(p, "100 Trying", req, 0);
21875 p->invitestate = INV_PROCEEDING;
21876 ast_setstate(c, AST_STATE_RING);
21877 if (strcmp(p->exten, ast_pickup_ext())) {
21878 enum ast_pbx_result result;
21879
21880 result = ast_pbx_start(c);
21881
21882 switch(result) {
21883 case AST_PBX_FAILED:
21884 ast_log(LOG_WARNING, "Failed to start PBX :(\n");
21885 p->invitestate = INV_COMPLETED;
21886 transmit_response_reliable(p, "503 Unavailable", req);
21887 break;
21888 case AST_PBX_CALL_LIMIT:
21889 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
21890 p->invitestate = INV_COMPLETED;
21891 transmit_response_reliable(p, "480 Temporarily Unavailable", req);
21892 break;
21893 case AST_PBX_SUCCESS:
21894
21895 break;
21896 }
21897
21898 if (result) {
21899
21900
21901 ast_channel_unlock(c);
21902 sip_pvt_unlock(p);
21903 ast_hangup(c);
21904 sip_pvt_lock(p);
21905 c = NULL;
21906 }
21907 } else {
21908 ast_channel_unlock(c);
21909 *nounlock = 1;
21910 if (ast_pickup_call(c)) {
21911 ast_log(LOG_NOTICE, "Nothing to pick up for %s\n", p->callid);
21912 transmit_response_reliable(p, "503 Unavailable", req);
21913 sip_alreadygone(p);
21914
21915 sip_pvt_unlock(p);
21916 c->hangupcause = AST_CAUSE_CALL_REJECTED;
21917 } else {
21918 sip_pvt_unlock(p);
21919 c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
21920 }
21921 p->invitestate = INV_COMPLETED;
21922 ast_hangup(c);
21923 sip_pvt_lock(p);
21924 c = NULL;
21925 }
21926 break;
21927 case AST_STATE_RING:
21928 transmit_provisional_response(p, "100 Trying", req, 0);
21929 p->invitestate = INV_PROCEEDING;
21930 break;
21931 case AST_STATE_RINGING:
21932 transmit_provisional_response(p, "180 Ringing", req, 0);
21933 p->invitestate = INV_PROCEEDING;
21934 break;
21935 case AST_STATE_UP:
21936 ast_debug(2, "%s: This call is UP.... \n", c->name);
21937
21938 transmit_response(p, "100 Trying", req);
21939
21940 if (p->t38.state == T38_PEER_REINVITE) {
21941 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."));
21942 } else if (p->t38.state == T38_ENABLED) {
21943 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
21944 transmit_response_with_t38_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)));
21945 } else if (p->t38.state == T38_DISABLED) {
21946
21947 if (p->srtp && !ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)) {
21948 ast_log(LOG_WARNING, "Target does not support required crypto\n");
21949 transmit_response_reliable(p, "488 Not Acceptable Here (crypto)", req);
21950 } else {
21951 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
21952 transmit_response_with_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ? XMIT_UNRELIABLE : XMIT_CRITICAL)), p->session_modify == TRUE ? FALSE : TRUE, FALSE);
21953 }
21954 }
21955
21956 p->invitestate = INV_TERMINATED;
21957 break;
21958 default:
21959 ast_log(LOG_WARNING, "Don't know how to handle INVITE in state %d\n", c->_state);
21960 transmit_response(p, "100 Trying", req);
21961 break;
21962 }
21963 } else {
21964 if (p && (p->autokillid == -1)) {
21965 const char *msg;
21966
21967 if (!p->jointcapability)
21968 msg = "488 Not Acceptable Here (codec error)";
21969 else {
21970 ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
21971 msg = "503 Unavailable";
21972 }
21973 transmit_response_reliable(p, msg, req);
21974 p->invitestate = INV_COMPLETED;
21975 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21976 }
21977 }
21978
21979 request_invite_cleanup:
21980
21981 if (refer_locked && p->refer && p->refer->refer_call) {
21982 sip_pvt_unlock(p->refer->refer_call);
21983 if (p->refer->refer_call->owner) {
21984 ast_channel_unlock(p->refer->refer_call->owner);
21985 }
21986 }
21987 if (authpeer) {
21988 authpeer = unref_peer(authpeer, "unref_peer, from handle_request_invite authpeer");
21989 }
21990
21991 return res;
21992 }
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007 static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno, int *nounlock)
22008 {
22009 struct sip_dual target;
22010
22011 int res = 0;
22012 struct sip_pvt *targetcall_pvt;
22013 struct ast_party_connected_line connected_to_transferee;
22014 struct ast_party_connected_line connected_to_target;
22015 char transferer_linkedid[32];
22016 struct ast_channel *chans[2];
22017
22018
22019 if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
22020 transferer->refer->replaces_callid_fromtag))) {
22021 if (transferer->refer->localtransfer) {
22022
22023
22024
22025 transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
22026 append_history(transferer, "Xfer", "Refer failed");
22027 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
22028 transferer->refer->status = REFER_FAILED;
22029 return -1;
22030 }
22031
22032 ast_debug(3, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
22033 return 0;
22034 }
22035
22036
22037 append_history(transferer, "Xfer", "Refer accepted");
22038 if (!targetcall_pvt->owner) {
22039 ast_debug(4, "SIP attended transfer: Error: No owner of target call\n");
22040
22041 transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
22042 append_history(transferer, "Xfer", "Refer failed");
22043 ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
22044 transferer->refer->status = REFER_FAILED;
22045 sip_pvt_unlock(targetcall_pvt);
22046 if (targetcall_pvt)
22047 ao2_t_ref(targetcall_pvt, -1, "Drop targetcall_pvt pointer");
22048 return -1;
22049 }
22050
22051
22052 target.chan1 = targetcall_pvt->owner;
22053 target.chan2 = ast_bridged_channel(targetcall_pvt->owner);
22054
22055 if (!target.chan2 || !(target.chan2->_state == AST_STATE_UP || target.chan2->_state == AST_STATE_RINGING) ) {
22056
22057 if (target.chan2)
22058 ast_debug(4, "SIP attended transfer: Error: Wrong state of target call: %s\n", ast_state2str(target.chan2->_state));
22059 else if (target.chan1->_state != AST_STATE_RING)
22060 ast_debug(4, "SIP attended transfer: Error: No target channel\n");
22061 else
22062 ast_debug(4, "SIP attended transfer: Attempting transfer in ringing state\n");
22063 }
22064
22065
22066 if (sipdebug) {
22067 if (current->chan2)
22068 ast_debug(4, "SIP attended transfer: trying to bridge %s and %s\n", target.chan1->name, current->chan2->name);
22069 else
22070 ast_debug(4, "SIP attended transfer: trying to make %s take over (masq) %s\n", target.chan1->name, current->chan1->name);
22071 }
22072
22073 ast_set_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
22074
22075 ast_copy_string(transferer_linkedid, transferer->owner->linkedid, sizeof(transferer_linkedid));
22076
22077
22078 chans[0] = transferer->owner;
22079 chans[1] = target.chan1;
22080 ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
22081 "TransferMethod: SIP\r\n"
22082 "TransferType: Attended\r\n"
22083 "Channel: %s\r\n"
22084 "Uniqueid: %s\r\n"
22085 "SIP-Callid: %s\r\n"
22086 "TargetChannel: %s\r\n"
22087 "TargetUniqueid: %s\r\n",
22088 transferer->owner->name,
22089 transferer->owner->uniqueid,
22090 transferer->callid,
22091 target.chan1->name,
22092 target.chan1->uniqueid);
22093 ast_party_connected_line_init(&connected_to_transferee);
22094 ast_party_connected_line_init(&connected_to_target);
22095
22096 ast_party_connected_line_copy(&connected_to_transferee, ¤t->chan1->connected);
22097
22098 ast_party_connected_line_copy(&connected_to_target, &target.chan1->connected);
22099 connected_to_target.source = connected_to_transferee.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
22100 res = attempt_transfer(current, &target);
22101 if (res) {
22102
22103 transmit_notify_with_sipfrag(transferer, seqno, "486 Busy Here", TRUE);
22104 append_history(transferer, "Xfer", "Refer failed");
22105 ast_clear_flag(&transferer->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
22106
22107 sip_pvt_unlock(targetcall_pvt);
22108 ast_channel_unlock(target.chan1);
22109 } else {
22110
22111 const char *xfersound = pbx_builtin_getvar_helper(target.chan1, "ATTENDED_TRANSFER_COMPLETE_SOUND");
22112
22113
22114 ast_cel_report_event(target.chan1, AST_CEL_ATTENDEDTRANSFER, NULL, transferer_linkedid, target.chan2);
22115
22116
22117 transmit_notify_with_sipfrag(transferer, seqno, "200 OK", TRUE);
22118 append_history(transferer, "Xfer", "Refer succeeded");
22119 transferer->refer->status = REFER_200OK;
22120 if (target.chan2 && !ast_strlen_zero(xfersound) && ast_streamfile(target.chan2, xfersound, target.chan2->language) >= 0) {
22121 ast_waitstream(target.chan2, "");
22122 }
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134 ast_channel_ref(current->chan1);
22135 ast_channel_unlock(current->chan1);
22136 ast_channel_unlock(target.chan1);
22137 *nounlock = 1;
22138 sip_pvt_unlock(targetcall_pvt);
22139 sip_pvt_unlock(transferer);
22140
22141 ast_do_masquerade(target.chan1);
22142
22143 sip_pvt_lock(transferer);
22144
22145 ast_indicate(target.chan1, AST_CONTROL_UNHOLD);
22146
22147 if (current->chan2 && current->chan2->_state == AST_STATE_RING) {
22148 ast_indicate(target.chan1, AST_CONTROL_RINGING);
22149 }
22150
22151 if (target.chan2) {
22152 ast_channel_queue_connected_line_update(target.chan1, &connected_to_transferee, NULL);
22153 ast_channel_queue_connected_line_update(target.chan2, &connected_to_target, NULL);
22154 } else {
22155
22156
22157
22158
22159
22160
22161
22162 struct ast_control_read_action_payload *frame_payload;
22163 int payload_size;
22164 int frame_size;
22165 unsigned char connected_line_data[1024];
22166 payload_size = ast_connected_line_build_data(connected_line_data,
22167 sizeof(connected_line_data), &connected_to_target, NULL);
22168 frame_size = payload_size + sizeof(*frame_payload);
22169 if (payload_size != -1 && (frame_payload = alloca(frame_size))) {
22170 frame_payload->payload_size = payload_size;
22171 memcpy(frame_payload->payload, connected_line_data, payload_size);
22172 frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
22173 ast_queue_control_data(target.chan1, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
22174 }
22175
22176
22177
22178
22179 ast_channel_queue_connected_line_update(target.chan1, &connected_to_transferee, NULL);
22180
22181 }
22182 ast_channel_unref(current->chan1);
22183 }
22184
22185
22186 ast_party_connected_line_free(&connected_to_target);
22187 ast_party_connected_line_free(&connected_to_transferee);
22188 if (targetcall_pvt)
22189 ao2_t_ref(targetcall_pvt, -1, "drop targetcall_pvt");
22190 return 1;
22191 }
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257 static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
22258 {
22259 struct sip_dual current;
22260
22261
22262 int res = 0;
22263 struct ast_channel *chans[2];
22264 current.req.data = NULL;
22265
22266 if (req->debug)
22267 ast_verbose("Call %s got a SIP call transfer from %s: (REFER)!\n", p->callid, ast_test_flag(&p->flags[0], SIP_OUTGOING) ? "callee" : "caller");
22268
22269 if (!p->owner) {
22270
22271
22272 ast_debug(3, "Call %s: Declined REFER, outside of dialog...\n", p->callid);
22273 transmit_response(p, "603 Declined (No dialog)", req);
22274 if (!req->ignore) {
22275 append_history(p, "Xfer", "Refer failed. Outside of dialog.");
22276 sip_alreadygone(p);
22277 pvt_set_needdestroy(p, "outside of dialog");
22278 }
22279 return 0;
22280 }
22281
22282
22283
22284 if (p->allowtransfer == TRANSFER_CLOSED ) {
22285
22286 transmit_response(p, "603 Declined (policy)", req);
22287 append_history(p, "Xfer", "Refer failed. Allowtransfer == closed.");
22288
22289 return 0;
22290 }
22291
22292 if (!req->ignore && ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
22293
22294 transmit_response(p, "491 Request pending", req);
22295 append_history(p, "Xfer", "Refer failed. Request pending.");
22296 return 0;
22297 }
22298
22299
22300 if (!p->refer && !sip_refer_allocate(p)) {
22301 transmit_response(p, "500 Internal Server Error", req);
22302 append_history(p, "Xfer", "Refer failed. Memory allocation error.");
22303 return -3;
22304 }
22305
22306 res = get_refer_info(p, req);
22307
22308 p->refer->status = REFER_SENT;
22309
22310 if (res != 0) {
22311 switch (res) {
22312 case -2:
22313 transmit_response(p, "400 Bad Request (Refer-to missing)", req);
22314 append_history(p, "Xfer", "Refer failed. Refer-to missing.");
22315 if (req->debug)
22316 ast_debug(1, "SIP transfer to black hole can't be handled (no refer-to: )\n");
22317 break;
22318 case -3:
22319 transmit_response(p, "603 Declined (Non sip: uri)", req);
22320 append_history(p, "Xfer", "Refer failed. Non SIP uri");
22321 if (req->debug)
22322 ast_debug(1, "SIP transfer to non-SIP uri denied\n");
22323 break;
22324 default:
22325
22326 transmit_response(p, "202 Accepted", req);
22327 append_history(p, "Xfer", "Refer failed. Bad extension.");
22328 transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
22329 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22330 if (req->debug)
22331 ast_debug(1, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
22332 break;
22333 }
22334 return 0;
22335 }
22336 if (ast_strlen_zero(p->context))
22337 ast_string_field_set(p, context, sip_cfg.default_context);
22338
22339
22340 if (sip_cfg.allow_external_domains && check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
22341 p->refer->localtransfer = 1;
22342 if (sipdebug)
22343 ast_debug(3, "This SIP transfer is local : %s\n", p->refer->refer_to_domain);
22344 } else if (AST_LIST_EMPTY(&domain_list) || check_sip_domain(p->refer->refer_to_domain, NULL, 0)) {
22345
22346 p->refer->localtransfer = 1;
22347 } else if (sipdebug)
22348 ast_debug(3, "This SIP transfer is to a remote SIP extension (remote domain %s)\n", p->refer->refer_to_domain);
22349
22350
22351
22352 if (req->ignore)
22353 return res;
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381 chans[0] = current.chan1 = p->owner;
22382
22383
22384 chans[1] = current.chan2 = ast_bridged_channel(current.chan1);
22385
22386 if (sipdebug)
22387 ast_debug(3, "SIP %s transfer: Transferer channel %s, transferee channel %s\n", p->refer->attendedtransfer ? "attended" : "blind", current.chan1->name, current.chan2 ? current.chan2->name : "<none>");
22388
22389 if (!current.chan2 && !p->refer->attendedtransfer) {
22390
22391
22392
22393 if (sipdebug)
22394 ast_debug(3, "Refused SIP transfer on non-bridged channel.\n");
22395 p->refer->status = REFER_FAILED;
22396 append_history(p, "Xfer", "Refer failed. Non-bridged channel.");
22397 transmit_response(p, "603 Declined", req);
22398 return -1;
22399 }
22400
22401 if (current.chan2) {
22402 if (sipdebug)
22403 ast_debug(4, "Got SIP transfer, applying to bridged peer '%s'\n", current.chan2->name);
22404
22405 ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
22406 }
22407
22408 ast_set_flag(&p->flags[0], SIP_GOTREFER);
22409
22410
22411 transmit_response(p, "202 Accepted", req);
22412
22413
22414 if (p->refer->attendedtransfer) {
22415 if ((res = local_attended_transfer(p, ¤t, req, seqno, nounlock)))
22416 return res;
22417
22418 if (sipdebug)
22419 ast_debug(4, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
22420
22421 }
22422
22423
22424
22425 *nounlock = 1;
22426
22427
22428
22429
22430 ast_channel_ref(current.chan1);
22431 sip_pvt_unlock(p);
22432 ast_channel_unlock(current.chan1);
22433
22434
22435 if (p->refer->localtransfer && ast_parking_ext_valid(p->refer->refer_to, p->owner, p->owner->context)) {
22436 sip_pvt_lock(p);
22437 copy_request(¤t.req, req);
22438 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22439 p->refer->status = REFER_200OK;
22440 append_history(p, "Xfer", "REFER to call parking.");
22441 ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
22442 "TransferMethod: SIP\r\n"
22443 "TransferType: Blind\r\n"
22444 "Channel: %s\r\n"
22445 "Uniqueid: %s\r\n"
22446 "SIP-Callid: %s\r\n"
22447 "TargetChannel: %s\r\n"
22448 "TargetUniqueid: %s\r\n"
22449 "TransferExten: %s\r\n"
22450 "Transfer2Parking: Yes\r\n",
22451 current.chan1->name,
22452 current.chan1->uniqueid,
22453 p->callid,
22454 current.chan2->name,
22455 current.chan2->uniqueid,
22456 p->refer->refer_to);
22457 if (sipdebug)
22458 ast_debug(4, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
22459 if (sip_park(current.chan2, current.chan1, req, seqno, p->refer->refer_to)) {
22460 transmit_notify_with_sipfrag(p, seqno, "500 Internal Server Error", TRUE);
22461 }
22462 ast_channel_unref(current.chan1);
22463 return res;
22464 }
22465
22466
22467 if (current.chan1 && current.chan2) {
22468 ast_debug(3, "chan1->name: %s\n", current.chan1->name);
22469 pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
22470 }
22471
22472 sip_pvt_lock(p);
22473
22474 if (current.chan2) {
22475 pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
22476 pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
22477 pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
22478
22479 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER", "yes");
22480
22481 if (p->refer->referred_by)
22482 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REFERER", p->refer->referred_by);
22483 }
22484
22485 if (!ast_strlen_zero(p->refer->replaces_callid)) {
22486 char tempheader[SIPBUFSIZE];
22487 snprintf(tempheader, sizeof(tempheader), "%s%s%s%s%s", p->refer->replaces_callid,
22488 p->refer->replaces_callid_totag ? ";to-tag=" : "",
22489 p->refer->replaces_callid_totag,
22490 p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
22491 p->refer->replaces_callid_fromtag);
22492 if (current.chan2)
22493 pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
22494 }
22495
22496
22497
22498
22499 if (!p->refer->attendedtransfer)
22500 transmit_notify_with_sipfrag(p, seqno, "183 Ringing", FALSE);
22501
22502
22503
22504
22505
22506 if (!current.chan2) {
22507
22508
22509
22510
22511
22512
22513
22514 p->refer->status = REFER_FAILED;
22515 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable (can't handle one-legged xfers)", TRUE);
22516 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22517 append_history(p, "Xfer", "Refer failed (only bridged calls).");
22518 ast_channel_unref(current.chan1);
22519 return -1;
22520 }
22521 ast_set_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
22522
22523
22524
22525
22526
22527
22528 ast_indicate(current.chan2, AST_CONTROL_UNHOLD);
22529 res = ast_async_goto(current.chan2, p->refer->refer_to_context, p->refer->refer_to, 1);
22530
22531 if (!res) {
22532 ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
22533 "TransferMethod: SIP\r\n"
22534 "TransferType: Blind\r\n"
22535 "Channel: %s\r\n"
22536 "Uniqueid: %s\r\n"
22537 "SIP-Callid: %s\r\n"
22538 "TargetChannel: %s\r\n"
22539 "TargetUniqueid: %s\r\n"
22540 "TransferExten: %s\r\n"
22541 "TransferContext: %s\r\n",
22542 current.chan1->name,
22543 current.chan1->uniqueid,
22544 p->callid,
22545 current.chan2->name,
22546 current.chan2->uniqueid,
22547 p->refer->refer_to, p->refer->refer_to_context);
22548
22549 ast_debug(3, "%s transfer succeeded. Telling transferer.\n", p->refer->attendedtransfer? "Attended" : "Blind");
22550
22551 while (ast_channel_trylock(current.chan1)) {
22552 sip_pvt_unlock(p);
22553 sched_yield();
22554 sip_pvt_lock(p);
22555 }
22556
22557
22558 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);
22559 ast_channel_unlock(current.chan1);
22560
22561 transmit_notify_with_sipfrag(p, seqno, "200 Ok", TRUE);
22562 if (p->refer->localtransfer)
22563 p->refer->status = REFER_200OK;
22564 if (p->owner)
22565 p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
22566 append_history(p, "Xfer", "Refer succeeded.");
22567 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22568
22569
22570 res = 0;
22571 } else {
22572 ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);
22573 ast_debug(3, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
22574 append_history(p, "Xfer", "Refer failed.");
22575
22576 p->refer->status = REFER_FAILED;
22577 transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
22578 ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22579 res = -1;
22580 }
22581
22582 ast_channel_unref(current.chan1);
22583
22584 return res;
22585 }
22586
22587
22588 static int handle_request_cancel(struct sip_pvt *p, struct sip_request *req)
22589 {
22590
22591 check_via(p, req);
22592 sip_alreadygone(p);
22593
22594 if (p->owner && p->owner->_state == AST_STATE_UP) {
22595
22596 transmit_response(p, "200 OK", req);
22597 ast_debug(1, "Got CANCEL on an answered call. Ignoring... \n");
22598 return 0;
22599 }
22600
22601
22602
22603
22604
22605
22606 if (p->invitestate == INV_TERMINATED || p->invitestate == INV_COMPLETED) {
22607 __sip_pretend_ack(p);
22608 }
22609 if (p->invitestate != INV_TERMINATED)
22610 p->invitestate = INV_CANCELLED;
22611
22612 if (ast_test_flag(&p->flags[0], SIP_INC_COUNT) || ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD))
22613 update_call_counter(p, DEC_CALL_LIMIT);
22614
22615 stop_media_flows(p);
22616 if (p->owner) {
22617 ast_set_hangupsource(p->owner, p->owner->name, 0);
22618 ast_queue_hangup(p->owner);
22619 }
22620 else
22621 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
22622 if (p->initreq.len > 0) {
22623 struct sip_pkt *pkt, *prev_pkt;
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635 for (pkt = p->packets, prev_pkt = NULL; pkt; prev_pkt = pkt, pkt = pkt->next) {
22636 if (pkt->seqno == p->lastinvite && pkt->response_code == 487) {
22637 AST_SCHED_DEL(sched, pkt->retransid);
22638 UNLINK(pkt, p->packets, prev_pkt);
22639 ast_free(pkt);
22640 break;
22641 }
22642 }
22643 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
22644 transmit_response(p, "200 OK", req);
22645 return 1;
22646 } else {
22647 transmit_response(p, "481 Call Leg Does Not Exist", req);
22648 return 0;
22649 }
22650 }
22651
22652
22653 static int handle_request_bye(struct sip_pvt *p, struct sip_request *req)
22654 {
22655 struct ast_channel *c=NULL;
22656 int res;
22657 struct ast_channel *bridged_to;
22658 const char *required;
22659
22660
22661 if (p->pendinginvite && !ast_test_flag(&p->flags[0], SIP_OUTGOING) && !req->ignore) {
22662 transmit_response_reliable(p, "487 Request Terminated", &p->initreq);
22663 }
22664
22665 __sip_pretend_ack(p);
22666
22667 p->invitestate = INV_TERMINATED;
22668
22669 copy_request(&p->initreq, req);
22670 if (sipdebug)
22671 ast_debug(1, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
22672 check_via(p, req);
22673 sip_alreadygone(p);
22674
22675
22676 if (p->do_history || p->owner) {
22677 char quality_buf[AST_MAX_USER_FIELD], *quality;
22678 struct ast_channel *bridge = p->owner ? ast_bridged_channel(p->owner) : NULL;
22679
22680
22681
22682
22683 while (bridge && ast_channel_trylock(bridge)) {
22684 ast_channel_unlock(p->owner);
22685 do {
22686
22687 sip_pvt_unlock(p);
22688 usleep(1);
22689 sip_pvt_lock(p);
22690 } while (p->owner && ast_channel_trylock(p->owner));
22691 bridge = p->owner ? ast_bridged_channel(p->owner) : NULL;
22692 }
22693
22694
22695 if (p->rtp && (quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
22696 if (p->do_history) {
22697 append_history(p, "RTCPaudio", "Quality:%s", quality);
22698
22699 if ((quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
22700 append_history(p, "RTCPaudioJitter", "Quality:%s", quality);
22701 }
22702 if ((quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
22703 append_history(p, "RTCPaudioLoss", "Quality:%s", quality);
22704 }
22705 if ((quality = ast_rtp_instance_get_quality(p->rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
22706 append_history(p, "RTCPaudioRTT", "Quality:%s", quality);
22707 }
22708 }
22709
22710 if (p->owner) {
22711 ast_rtp_instance_set_stats_vars(p->owner, p->rtp);
22712 }
22713
22714 }
22715
22716 if (bridge) {
22717 struct sip_pvt *q = bridge->tech_pvt;
22718
22719 if (IS_SIP_TECH(bridge->tech) && q && q->rtp) {
22720 ast_rtp_instance_set_stats_vars(bridge, q->rtp);
22721 }
22722 ast_channel_unlock(bridge);
22723 }
22724
22725 if (p->vrtp && (quality = ast_rtp_instance_get_quality(p->vrtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
22726 if (p->do_history) {
22727 append_history(p, "RTCPvideo", "Quality:%s", quality);
22728 }
22729 if (p->owner) {
22730 pbx_builtin_setvar_helper(p->owner, "RTPVIDEOQOS", quality);
22731 }
22732 }
22733 if (p->trtp && (quality = ast_rtp_instance_get_quality(p->trtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
22734 if (p->do_history) {
22735 append_history(p, "RTCPtext", "Quality:%s", quality);
22736 }
22737 if (p->owner) {
22738 pbx_builtin_setvar_helper(p->owner, "RTPTEXTQOS", quality);
22739 }
22740 }
22741 }
22742
22743 stop_media_flows(p);
22744 stop_session_timer(p);
22745
22746 if (!ast_strlen_zero(get_header(req, "Also"))) {
22747 ast_log(LOG_NOTICE, "Client '%s' using deprecated BYE/Also transfer method. Ask vendor to support REFER instead\n",
22748 ast_sockaddr_stringify(&p->recv));
22749 if (ast_strlen_zero(p->context))
22750 ast_string_field_set(p, context, sip_cfg.default_context);
22751 res = get_also_info(p, req);
22752 if (!res) {
22753 c = p->owner;
22754 if (c) {
22755 bridged_to = ast_bridged_channel(c);
22756 if (bridged_to) {
22757
22758 ast_queue_control(c, AST_CONTROL_UNHOLD);
22759 ast_channel_unlock(c);
22760 ast_async_goto(bridged_to, p->context, p->refer->refer_to, 1);
22761 ast_channel_lock(c);
22762 } else
22763 ast_queue_hangup(p->owner);
22764 }
22765 } else {
22766 ast_log(LOG_WARNING, "Invalid transfer information from '%s'\n", ast_sockaddr_stringify(&p->recv));
22767 if (p->owner)
22768 ast_queue_hangup_with_cause(p->owner, AST_CAUSE_PROTOCOL_ERROR);
22769 }
22770 } else if (p->owner) {
22771 ast_set_hangupsource(p->owner, p->owner->name, 0);
22772 ast_queue_hangup(p->owner);
22773 sip_scheddestroy_final(p, DEFAULT_TRANS_TIMEOUT);
22774 ast_debug(3, "Received bye, issuing owner hangup\n");
22775 } else {
22776 sip_scheddestroy_final(p, DEFAULT_TRANS_TIMEOUT);
22777 ast_debug(3, "Received bye, no owner, selfdestruct soon.\n");
22778 }
22779 ast_clear_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
22780
22781
22782 required = get_header(req, "Require");
22783 if (!ast_strlen_zero(required)) {
22784 char unsupported[256] = { 0, };
22785 parse_sip_options(required, unsupported, ARRAY_LEN(unsupported));
22786
22787
22788 if (!ast_strlen_zero(unsupported)) {
22789 transmit_response_with_unsupported(p, "420 Bad extension (unsupported)", req, unsupported);
22790 ast_log(LOG_WARNING, "Received SIP BYE with unsupported required extension: required:%s unsupported:%s\n", required, unsupported);
22791 } else {
22792 transmit_response(p, "200 OK", req);
22793 }
22794 } else {
22795 transmit_response(p, "200 OK", req);
22796 }
22797
22798 return 1;
22799 }
22800
22801
22802 static int handle_request_message(struct sip_pvt *p, struct sip_request *req)
22803 {
22804 if (!req->ignore) {
22805 if (req->debug)
22806 ast_verbose("Receiving message!\n");
22807 receive_message(p, req);
22808 } else
22809 transmit_response(p, "202 Accepted", req);
22810 return 1;
22811 }
22812
22813 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)
22814 {
22815 int etag_present = !ast_strlen_zero(etag);
22816 int body_present = req->lines > 0;
22817
22818 ast_assert(expires_int != NULL);
22819
22820 if (ast_strlen_zero(expires)) {
22821
22822
22823
22824
22825 *expires_int = DEFAULT_PUBLISH_EXPIRES;
22826 } else if (sscanf(expires, "%30d", expires_int) != 1) {
22827 return SIP_PUBLISH_UNKNOWN;
22828 }
22829
22830 if (*expires_int == 0) {
22831 return SIP_PUBLISH_REMOVE;
22832 } else if (!etag_present && body_present) {
22833 return SIP_PUBLISH_INITIAL;
22834 } else if (etag_present && !body_present) {
22835 return SIP_PUBLISH_REFRESH;
22836 } else if (etag_present && body_present) {
22837 return SIP_PUBLISH_MODIFY;
22838 }
22839
22840 return SIP_PUBLISH_UNKNOWN;
22841 }
22842
22843 #ifdef HAVE_LIBXML2
22844 static void get_pidf_body(struct sip_request *req, char *pidf_body, size_t size)
22845 {
22846 int i;
22847 struct ast_str *str = ast_str_alloca(size);
22848 for (i = 0; i < req->lines; ++i) {
22849 ast_str_append(&str, 0, "%s", REQ_OFFSET_TO_STR(req, line[i]));
22850 }
22851 ast_copy_string(pidf_body, ast_str_buffer(str), size);
22852 }
22853
22854 static int pidf_validate_tuple(struct ast_xml_node *tuple_node)
22855 {
22856 const char *id;
22857 int status_found = FALSE;
22858 struct ast_xml_node *tuple_children;
22859 struct ast_xml_node *tuple_children_iterator;
22860
22861 if (!(id = ast_xml_get_attribute(tuple_node, "id"))) {
22862 ast_log(LOG_WARNING, "Tuple XML element has no attribute 'id'\n");
22863 return FALSE;
22864 }
22865
22866 ast_xml_free_attr(id);
22867
22868 if (!(tuple_children = ast_xml_node_get_children(tuple_node))) {
22869
22870 ast_log(LOG_WARNING, "Tuple XML element has no child elements\n");
22871 return FALSE;
22872 }
22873 for (tuple_children_iterator = tuple_children; tuple_children_iterator;
22874 tuple_children_iterator = ast_xml_node_get_next(tuple_children_iterator)) {
22875
22876
22877
22878
22879 if (strcmp(ast_xml_node_get_name(tuple_children_iterator), "status")) {
22880
22881 continue;
22882 }
22883 if (status_found == TRUE) {
22884
22885 ast_log(LOG_WARNING, "Multiple status elements found in tuple. Only one allowed\n");
22886 return FALSE;
22887 }
22888 status_found = TRUE;
22889 }
22890 return status_found;
22891 }
22892
22893
22894 static int pidf_validate_presence(struct ast_xml_doc *doc)
22895 {
22896 struct ast_xml_node *presence_node = ast_xml_get_root(doc);
22897 struct ast_xml_node *child_nodes;
22898 struct ast_xml_node *node_iterator;
22899 struct ast_xml_ns *ns;
22900 const char *entity;
22901 const char *namespace;
22902 const char presence_namespace[] = "urn:ietf:params:xml:ns:pidf";
22903
22904 if (!presence_node) {
22905 ast_log(LOG_WARNING, "Unable to retrieve root node of the XML document\n");
22906 return FALSE;
22907 }
22908
22909
22910
22911 if (strcmp(ast_xml_node_get_name(presence_node), "presence")) {
22912 ast_log(LOG_WARNING, "Root node of PIDF document is not 'presence'. Invalid\n");
22913 return FALSE;
22914 }
22915
22916
22917
22918
22919 if (!(entity = ast_xml_get_attribute(presence_node, "entity"))) {
22920 ast_log(LOG_WARNING, "Presence element of PIDF document has no 'entity' attribute\n");
22921 return FALSE;
22922 }
22923
22924 ast_xml_free_attr(entity);
22925
22926 if (!(ns = ast_xml_find_namespace(doc, presence_node, NULL))) {
22927 ast_log(LOG_WARNING, "Couldn't find default namespace...\n");
22928 return FALSE;
22929 }
22930
22931 namespace = ast_xml_get_ns_href(ns);
22932 if (ast_strlen_zero(namespace) || strcmp(namespace, presence_namespace)) {
22933 ast_log(LOG_WARNING, "PIDF document has invalid namespace value %s\n", namespace);
22934 return FALSE;
22935 }
22936
22937 if (!(child_nodes = ast_xml_node_get_children(presence_node))) {
22938 ast_log(LOG_WARNING, "PIDF document has no elements as children of 'presence'. Invalid\n");
22939 return FALSE;
22940 }
22941
22942
22943
22944
22945
22946
22947
22948
22949
22950 for (node_iterator = child_nodes; node_iterator;
22951 node_iterator = ast_xml_node_get_next(node_iterator)) {
22952 if (strcmp(ast_xml_node_get_name(node_iterator), "tuple")) {
22953
22954 continue;
22955 }
22956 if (pidf_validate_tuple(node_iterator) == FALSE) {
22957 ast_log(LOG_WARNING, "Unable to validate tuple\n");
22958 return FALSE;
22959 }
22960 }
22961
22962 return TRUE;
22963 }
22964
22965
22966
22967
22968
22969
22970
22971
22972
22973
22974
22975
22976
22977
22978
22979
22980 static int sip_pidf_validate(struct sip_request *req, struct ast_xml_doc **pidf_doc)
22981 {
22982 struct ast_xml_doc *doc;
22983 int content_length;
22984 const char *content_length_str = get_header(req, "Content-Length");
22985 const char *content_type = get_header(req, "Content-Type");
22986 char pidf_body[SIPBUFSIZE];
22987 int res;
22988
22989 if (ast_strlen_zero(content_type) || strcmp(content_type, "application/pidf+xml")) {
22990 ast_log(LOG_WARNING, "Content type is not PIDF\n");
22991 return FALSE;
22992 }
22993
22994 if (ast_strlen_zero(content_length_str)) {
22995 ast_log(LOG_WARNING, "No content length. Can't determine bounds of PIDF document\n");
22996 return FALSE;
22997 }
22998
22999 if (sscanf(content_length_str, "%30d", &content_length) != 1) {
23000 ast_log(LOG_WARNING, "Invalid content length provided\n");
23001 return FALSE;
23002 }
23003
23004 if (content_length > sizeof(pidf_body)) {
23005 ast_log(LOG_WARNING, "Content length of PIDF document truncated to %d bytes\n", (int) sizeof(pidf_body));
23006 content_length = sizeof(pidf_body);
23007 }
23008
23009 get_pidf_body(req, pidf_body, content_length);
23010
23011 if (!(doc = ast_xml_read_memory(pidf_body, content_length))) {
23012 ast_log(LOG_WARNING, "Unable to open XML PIDF document. Is it malformed?\n");
23013 return FALSE;
23014 }
23015
23016 res = pidf_validate_presence(doc);
23017 if (res == TRUE) {
23018 *pidf_doc = doc;
23019 } else {
23020 ast_xml_close(doc);
23021 }
23022 return res;
23023 }
23024
23025 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)
23026 {
23027 const char *uri = REQ_OFFSET_TO_STR(req, rlPart2);
23028 struct ast_cc_agent *agent = find_sip_cc_agent_by_notify_uri(uri);
23029 struct sip_cc_agent_pvt *agent_pvt;
23030 struct ast_xml_doc *pidf_doc = NULL;
23031 const char *basic_status = NULL;
23032 struct ast_xml_node *presence_node;
23033 struct ast_xml_node *presence_children;
23034 struct ast_xml_node *tuple_node;
23035 struct ast_xml_node *tuple_children;
23036 struct ast_xml_node *status_node;
23037 struct ast_xml_node *status_children;
23038 struct ast_xml_node *basic_node;
23039 int res = 0;
23040
23041 if (!agent) {
23042 ast_log(LOG_WARNING, "Could not find agent using uri '%s'\n", uri);
23043 transmit_response(pvt, "412 Conditional Request Failed", req);
23044 return -1;
23045 }
23046
23047 agent_pvt = agent->private_data;
23048
23049 if (sip_pidf_validate(req, &pidf_doc) == FALSE) {
23050 res = -1;
23051 goto cc_publish_cleanup;
23052 }
23053
23054
23055
23056
23057
23058
23059
23060 presence_node = ast_xml_get_root(pidf_doc);
23061 if (!(presence_children = ast_xml_node_get_children(presence_node))) {
23062 ast_log(LOG_WARNING, "No tuples within presence element.\n");
23063 res = -1;
23064 goto cc_publish_cleanup;
23065 }
23066
23067 if (!(tuple_node = ast_xml_find_element(presence_children, "tuple", NULL, NULL))) {
23068 ast_log(LOG_NOTICE, "Couldn't find tuple node?\n");
23069 res = -1;
23070 goto cc_publish_cleanup;
23071 }
23072
23073
23074
23075
23076 tuple_children = ast_xml_node_get_children(tuple_node);
23077 status_node = ast_xml_find_element(tuple_children, "status", NULL, NULL);
23078
23079 if (!(status_children = ast_xml_node_get_children(status_node))) {
23080 ast_log(LOG_WARNING, "No basic elements within status element.\n");
23081 res = -1;
23082 goto cc_publish_cleanup;
23083 }
23084
23085 if (!(basic_node = ast_xml_find_element(status_children, "basic", NULL, NULL))) {
23086 ast_log(LOG_WARNING, "Couldn't find basic node?\n");
23087 res = -1;
23088 goto cc_publish_cleanup;
23089 }
23090
23091 basic_status = ast_xml_get_text(basic_node);
23092
23093 if (ast_strlen_zero(basic_status)) {
23094 ast_log(LOG_NOTICE, "NOthing in basic node?\n");
23095 res = -1;
23096 goto cc_publish_cleanup;
23097 }
23098
23099 if (!strcmp(basic_status, "open")) {
23100 agent_pvt->is_available = TRUE;
23101 ast_cc_agent_caller_available(agent->core_id, "Received PUBLISH stating SIP caller %s is available",
23102 agent->device_name);
23103 } else if (!strcmp(basic_status, "closed")) {
23104 agent_pvt->is_available = FALSE;
23105 ast_cc_agent_caller_busy(agent->core_id, "Received PUBLISH stating SIP caller %s is busy",
23106 agent->device_name);
23107 } else {
23108 ast_log(LOG_NOTICE, "Invalid content in basic element: %s\n", basic_status);
23109 }
23110
23111 cc_publish_cleanup:
23112 if (basic_status) {
23113 ast_xml_free_text(basic_status);
23114 }
23115 if (pidf_doc) {
23116 ast_xml_close(pidf_doc);
23117 }
23118 ao2_ref(agent, -1);
23119 if (res) {
23120 transmit_response(pvt, "400 Bad Request", req);
23121 }
23122 return res;
23123 }
23124
23125 #endif
23126
23127 static int handle_sip_publish_initial(struct sip_pvt *p, struct sip_request *req, struct event_state_compositor *esc, const int expires)
23128 {
23129 struct sip_esc_entry *esc_entry = create_esc_entry(esc, req, expires);
23130 int res = 0;
23131
23132 if (!esc_entry) {
23133 transmit_response(p, "503 Internal Server Failure", req);
23134 return -1;
23135 }
23136
23137 if (esc->callbacks->initial_handler) {
23138 res = esc->callbacks->initial_handler(p, req, esc, esc_entry);
23139 }
23140
23141 if (!res) {
23142 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 0);
23143 }
23144
23145 ao2_ref(esc_entry, -1);
23146 return res;
23147 }
23148
23149 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)
23150 {
23151 struct sip_esc_entry *esc_entry = get_esc_entry(etag, esc);
23152 int expires_ms = expires * 1000;
23153 int res = 0;
23154
23155 if (!esc_entry) {
23156 transmit_response(p, "412 Conditional Request Failed", req);
23157 return -1;
23158 }
23159
23160 AST_SCHED_REPLACE_UNREF(esc_entry->sched_id, sched, expires_ms, publish_expire, esc_entry,
23161 ao2_ref(_data, -1),
23162 ao2_ref(esc_entry, -1),
23163 ao2_ref(esc_entry, +1));
23164
23165 if (esc->callbacks->refresh_handler) {
23166 res = esc->callbacks->refresh_handler(p, req, esc, esc_entry);
23167 }
23168
23169 if (!res) {
23170 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 1);
23171 }
23172
23173 ao2_ref(esc_entry, -1);
23174 return res;
23175 }
23176
23177 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)
23178 {
23179 struct sip_esc_entry *esc_entry = get_esc_entry(etag, esc);
23180 int expires_ms = expires * 1000;
23181 int res = 0;
23182
23183 if (!esc_entry) {
23184 transmit_response(p, "412 Conditional Request Failed", req);
23185 return -1;
23186 }
23187
23188 AST_SCHED_REPLACE_UNREF(esc_entry->sched_id, sched, expires_ms, publish_expire, esc_entry,
23189 ao2_ref(_data, -1),
23190 ao2_ref(esc_entry, -1),
23191 ao2_ref(esc_entry, +1));
23192
23193 if (esc->callbacks->modify_handler) {
23194 res = esc->callbacks->modify_handler(p, req, esc, esc_entry);
23195 }
23196
23197 if (!res) {
23198 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 1);
23199 }
23200
23201 ao2_ref(esc_entry, -1);
23202 return res;
23203 }
23204
23205 static int handle_sip_publish_remove(struct sip_pvt *p, struct sip_request *req, struct event_state_compositor *esc, const char * const etag)
23206 {
23207 struct sip_esc_entry *esc_entry = get_esc_entry(etag, esc);
23208 int res = 0;
23209
23210 if (!esc_entry) {
23211 transmit_response(p, "412 Conditional Request Failed", req);
23212 return -1;
23213 }
23214
23215 AST_SCHED_DEL(sched, esc_entry->sched_id);
23216
23217 ao2_ref(esc_entry, -1);
23218
23219 if (esc->callbacks->remove_handler) {
23220 res = esc->callbacks->remove_handler(p, req, esc, esc_entry);
23221 }
23222
23223 if (!res) {
23224 transmit_response_with_sip_etag(p, "200 OK", req, esc_entry, 1);
23225 }
23226
23227
23228 ao2_unlink(esc->compositor, esc_entry);
23229 ao2_ref(esc_entry, -1);
23230 return res;
23231 }
23232
23233 static int handle_request_publish(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const int seqno, const char *uri)
23234 {
23235 const char *etag = get_header(req, "SIP-If-Match");
23236 const char *event = get_header(req, "Event");
23237 struct event_state_compositor *esc;
23238 enum sip_publish_type publish_type;
23239 const char *expires_str = get_header(req, "Expires");
23240 int expires_int;
23241 int auth_result;
23242 int handler_result = -1;
23243
23244 if (ast_strlen_zero(event)) {
23245 transmit_response(p, "489 Bad Event", req);
23246 return -1;
23247 }
23248
23249 if (!(esc = get_esc(event))) {
23250 transmit_response(p, "489 Bad Event", req);
23251 return -1;
23252 }
23253
23254 auth_result = check_user(p, req, SIP_PUBLISH, uri, XMIT_RELIABLE, addr);
23255 if (auth_result == AUTH_CHALLENGE_SENT) {
23256 p->lastinvite = seqno;
23257 return 0;
23258 } else if (auth_result < 0) {
23259 if (auth_result == AUTH_FAKE_AUTH) {
23260 ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
23261 transmit_fake_auth_response(p, SIP_INVITE, req, XMIT_RELIABLE);
23262 } else {
23263 ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
23264 transmit_response_reliable(p, "403 Forbidden", req);
23265 }
23266 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23267 ast_string_field_set(p, theirtag, NULL);
23268 return 0;
23269 } else if (auth_result == AUTH_SUCCESSFUL && p->lastinvite) {
23270
23271 __sip_ack(p, p->lastinvite, 1, 0);
23272 }
23273
23274 publish_type = determine_sip_publish_type(req, event, etag, expires_str, &expires_int);
23275
23276
23277
23278
23279 switch (publish_type) {
23280 case SIP_PUBLISH_UNKNOWN:
23281 transmit_response(p, "400 Bad Request", req);
23282 break;
23283 case SIP_PUBLISH_INITIAL:
23284 handler_result = handle_sip_publish_initial(p, req, esc, expires_int);
23285 break;
23286 case SIP_PUBLISH_REFRESH:
23287 handler_result = handle_sip_publish_refresh(p, req, esc, etag, expires_int);
23288 break;
23289 case SIP_PUBLISH_MODIFY:
23290 handler_result = handle_sip_publish_modify(p, req, esc, etag, expires_int);
23291 break;
23292 case SIP_PUBLISH_REMOVE:
23293 handler_result = handle_sip_publish_remove(p, req, esc, etag);
23294 break;
23295 default:
23296 transmit_response(p, "400 Impossible Condition", req);
23297 break;
23298 }
23299
23300 return handler_result;
23301 }
23302
23303 static void add_peer_mwi_subs(struct sip_peer *peer)
23304 {
23305 struct sip_mailbox *mailbox;
23306
23307 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
23308 mailbox->event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, "SIP mbox event", peer,
23309 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
23310 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
23311 AST_EVENT_IE_END);
23312 }
23313 }
23314
23315 static int handle_cc_subscribe(struct sip_pvt *p, struct sip_request *req)
23316 {
23317 const char *uri = REQ_OFFSET_TO_STR(req, rlPart2);
23318 char *param_separator;
23319 struct ast_cc_agent *agent;
23320 struct sip_cc_agent_pvt *agent_pvt;
23321 const char *expires_str = get_header(req, "Expires");
23322 int expires = -1;
23323
23324 if (!ast_strlen_zero(expires_str)) {
23325 sscanf(expires_str, "%d", &expires);
23326 }
23327
23328 if ((param_separator = strchr(uri, ';'))) {
23329 *param_separator = '\0';
23330 }
23331
23332 if (!(agent = find_sip_cc_agent_by_subscribe_uri(uri))) {
23333 if (!expires) {
23334
23335
23336
23337
23338
23339
23340 transmit_response(p, "200 OK", req);
23341 return 0;
23342 }
23343 ast_log(LOG_WARNING, "Invalid URI '%s' in CC subscribe\n", uri);
23344 transmit_response(p, "404 Not Found", req);
23345 return -1;
23346 }
23347
23348 agent_pvt = agent->private_data;
23349
23350 if (!expires) {
23351
23352
23353
23354 ast_cc_failed(agent->core_id, "CC is being canceled by %s", agent->device_name);
23355 transmit_response(p, "200 OK", req);
23356 ao2_ref(agent, -1);
23357 return 0;
23358 }
23359
23360 agent_pvt->subscribe_pvt = dialog_ref(p, "SIP CC agent gains reference to subscription dialog");
23361 ast_cc_agent_accept_request(agent->core_id, "SIP caller %s has requested CC via SUBSCRIBE",
23362 agent->device_name);
23363 p->subscribed = CALL_COMPLETION;
23364
23365
23366
23367
23368 ao2_ref(agent, -1);
23369 return 0;
23370 }
23371
23372
23373 static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e)
23374 {
23375 int gotdest = 0;
23376 int res = 0;
23377 int firststate = AST_EXTENSION_REMOVED;
23378 struct sip_peer *authpeer = NULL;
23379 const char *eventheader = get_header(req, "Event");
23380 int resubscribe = (p->subscribed != NONE) && !req->ignore;
23381 char *temp, *event;
23382
23383 if (p->initreq.headers) {
23384
23385 if (p->initreq.method != SIP_SUBSCRIBE) {
23386
23387
23388 transmit_response(p, "403 Forbidden (within dialog)", req);
23389
23390 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);
23391 return 0;
23392 } else if (req->debug) {
23393 if (resubscribe)
23394 ast_debug(1, "Got a re-subscribe on existing subscription %s\n", p->callid);
23395 else
23396 ast_debug(1, "Got a new subscription %s (possibly with auth) or retransmission\n", p->callid);
23397 }
23398 }
23399
23400
23401
23402
23403 if (!sip_cfg.allowsubscribe) {
23404 transmit_response(p, "403 Forbidden (policy)", req);
23405 pvt_set_needdestroy(p, "forbidden");
23406 return 0;
23407 }
23408
23409 if (!req->ignore && !resubscribe) {
23410 const char *to = get_header(req, "To");
23411 char totag[128];
23412 set_pvt_allowed_methods(p, req);
23413
23414
23415 if (!ast_strlen_zero(to) && gettag(req, "To", totag, sizeof(totag))) {
23416 if (req->debug)
23417 ast_verbose("Received resubscription for a dialog we no longer know about. Telling remote side to subscribe again.\n");
23418 transmit_response(p, "481 Subscription does not exist", req);
23419 pvt_set_needdestroy(p, "subscription does not exist");
23420 return 0;
23421 }
23422
23423
23424 if (req->debug)
23425 ast_verbose("Creating new subscription\n");
23426
23427 copy_request(&p->initreq, req);
23428 if (sipdebug)
23429 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
23430 check_via(p, req);
23431 build_route(p, req, 0);
23432 } else if (req->debug && req->ignore)
23433 ast_verbose("Ignoring this SUBSCRIBE request\n");
23434
23435
23436 if (ast_strlen_zero(eventheader)) {
23437 transmit_response(p, "489 Bad Event", req);
23438 ast_debug(2, "Received SIP subscribe for unknown event package: <none>\n");
23439 pvt_set_needdestroy(p, "unknown event package in subscribe");
23440 return 0;
23441 }
23442
23443 if ( (strchr(eventheader, ';'))) {
23444 event = ast_strdupa(eventheader);
23445 temp = strchr(event, ';');
23446 *temp = '\0';
23447
23448 } else
23449 event = (char *) eventheader;
23450
23451
23452
23453
23454 if (p->subscribed == NONE || resubscribe) {
23455 res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, addr, &authpeer);
23456
23457
23458 if (res == AUTH_CHALLENGE_SENT)
23459 return 0;
23460 if (res < 0) {
23461 if (res == AUTH_FAKE_AUTH) {
23462 ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
23463 transmit_fake_auth_response(p, SIP_SUBSCRIBE, req, XMIT_UNRELIABLE);
23464 } else {
23465 ast_log(LOG_NOTICE, "Failed to authenticate device %s for SUBSCRIBE\n", get_header(req, "From"));
23466 transmit_response_reliable(p, "403 Forbidden", req);
23467 }
23468
23469 pvt_set_needdestroy(p, "authentication failed");
23470 return 0;
23471 }
23472 }
23473
23474
23475
23476
23477
23478
23479
23480 if (!ast_test_flag(&p->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
23481 transmit_response(p, "403 Forbidden (policy)", req);
23482 pvt_set_needdestroy(p, "subscription not allowed");
23483 if (authpeer)
23484 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 1)");
23485 return 0;
23486 }
23487
23488 if (strcmp(event, "message-summary") && strcmp(event, "call-completion")) {
23489
23490 gotdest = get_destination(p, NULL, NULL);
23491 }
23492
23493
23494 parse_ok_contact(p, req);
23495
23496 build_contact(p);
23497 if (gotdest != SIP_GET_DEST_EXTEN_FOUND) {
23498 if (gotdest == SIP_GET_DEST_INVALID_URI) {
23499 transmit_response(p, "416 Unsupported URI scheme", req);
23500 } else {
23501 transmit_response(p, "404 Not Found", req);
23502 }
23503 pvt_set_needdestroy(p, "subscription target not found");
23504 if (authpeer)
23505 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 2)");
23506 return 0;
23507 }
23508
23509
23510 if (ast_strlen_zero(p->tag))
23511 make_our_tag(p->tag, sizeof(p->tag));
23512
23513 if (!strcmp(event, "presence") || !strcmp(event, "dialog")) {
23514 unsigned int pidf_xml;
23515 const char *accept;
23516 int start = 0;
23517 enum subscriptiontype subscribed = NONE;
23518 const char *unknown_acceptheader = NULL;
23519
23520 if (authpeer)
23521 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 2)");
23522
23523
23524 accept = __get_header(req, "Accept", &start);
23525 while ((subscribed == NONE) && !ast_strlen_zero(accept)) {
23526 pidf_xml = strstr(accept, "application/pidf+xml") ? 1 : 0;
23527
23528
23529
23530 if (pidf_xml && strstr(p->useragent, "Polycom")) {
23531 subscribed = XPIDF_XML;
23532 } else if (pidf_xml) {
23533 subscribed = PIDF_XML;
23534 } else if (strstr(accept, "application/dialog-info+xml")) {
23535 subscribed = DIALOG_INFO_XML;
23536
23537 } else if (strstr(accept, "application/cpim-pidf+xml")) {
23538 subscribed = CPIM_PIDF_XML;
23539 } else if (strstr(accept, "application/xpidf+xml")) {
23540 subscribed = XPIDF_XML;
23541 } else {
23542 unknown_acceptheader = accept;
23543 }
23544
23545 accept = __get_header(req, "Accept", &start);
23546 }
23547
23548 if (!start) {
23549 if (p->subscribed == NONE) {
23550 transmit_response(p, "489 Bad Event", req);
23551 ast_log(LOG_WARNING,"SUBSCRIBE failure: no Accept header: pvt: "
23552 "stateid: %d, laststate: %d, dialogver: %d, subscribecont: "
23553 "'%s', subscribeuri: '%s'\n",
23554 p->stateid,
23555 p->laststate,
23556 p->dialogver,
23557 p->subscribecontext,
23558 p->subscribeuri);
23559 pvt_set_needdestroy(p, "no Accept header");
23560 return 0;
23561 }
23562
23563
23564 } else if (subscribed == NONE) {
23565
23566 char mybuf[200];
23567 if (!ast_strlen_zero(unknown_acceptheader)) {
23568 snprintf(mybuf, sizeof(mybuf), "489 Bad Event (format %s)", unknown_acceptheader);
23569 } else {
23570 snprintf(mybuf, sizeof(mybuf), "489 Bad Event");
23571 }
23572 transmit_response(p, mybuf, req);
23573 ast_log(LOG_WARNING,"SUBSCRIBE failure: unrecognized format:"
23574 "'%s' pvt: subscribed: %d, stateid: %d, laststate: %d,"
23575 "dialogver: %d, subscribecont: '%s', subscribeuri: '%s'\n",
23576 unknown_acceptheader,
23577 (int)p->subscribed,
23578 p->stateid,
23579 p->laststate,
23580 p->dialogver,
23581 p->subscribecontext,
23582 p->subscribeuri);
23583 pvt_set_needdestroy(p, "unrecognized format");
23584 return 0;
23585 } else {
23586 p->subscribed = subscribed;
23587 }
23588 } else if (!strcmp(event, "message-summary")) {
23589 int start = 0;
23590 int found_supported = 0;
23591 const char *acceptheader;
23592
23593 acceptheader = __get_header(req, "Accept", &start);
23594 while (!found_supported && !ast_strlen_zero(acceptheader)) {
23595 found_supported = strcmp(acceptheader, "application/simple-message-summary") ? 0 : 1;
23596 if (!found_supported && (option_debug > 2)) {
23597 ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
23598 }
23599 acceptheader = __get_header(req, "Accept", &start);
23600 }
23601 if (start && !found_supported) {
23602
23603 transmit_response(p, "406 Not Acceptable", req);
23604 ast_debug(2, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
23605 pvt_set_needdestroy(p, "unknown format");
23606 if (authpeer)
23607 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 3)");
23608 return 0;
23609 }
23610
23611
23612
23613
23614
23615 if (!authpeer || AST_LIST_EMPTY(&authpeer->mailboxes)) {
23616 transmit_response(p, "404 Not found (no mailbox)", req);
23617 pvt_set_needdestroy(p, "received 404 response");
23618 ast_log(LOG_NOTICE, "Received SIP subscribe for peer without mailbox: %s\n", authpeer->name);
23619 if (authpeer)
23620 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 4)");
23621 return 0;
23622 }
23623
23624 p->subscribed = MWI_NOTIFICATION;
23625 if (ast_test_flag(&authpeer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY)) {
23626 add_peer_mwi_subs(authpeer);
23627 }
23628 if (authpeer->mwipvt && authpeer->mwipvt != p) {
23629
23630 dialog_unlink_all(authpeer->mwipvt, TRUE, TRUE);
23631 authpeer->mwipvt = dialog_unref(authpeer->mwipvt, "unref dialog authpeer->mwipvt");
23632
23633 }
23634 if (authpeer->mwipvt)
23635 dialog_unref(authpeer->mwipvt, "Unref previously stored mwipvt dialog pointer");
23636 authpeer->mwipvt = dialog_ref(p, "setting peers' mwipvt to p");
23637 if (p->relatedpeer)
23638 unref_peer(p->relatedpeer, "Unref previously stored relatedpeer ptr");
23639 p->relatedpeer = ref_peer(authpeer, "setting dialog's relatedpeer pointer");
23640
23641 } else if (!strcmp(event, "call-completion")) {
23642 handle_cc_subscribe(p, req);
23643 } else {
23644 transmit_response(p, "489 Bad Event", req);
23645 ast_debug(2, "Received SIP subscribe for unknown event package: %s\n", event);
23646 pvt_set_needdestroy(p, "unknown event package");
23647 if (authpeer)
23648 unref_peer(authpeer, "unref_peer, from handle_request_subscribe (authpeer 5)");
23649 return 0;
23650 }
23651
23652
23653 if (p->subscribed != MWI_NOTIFICATION && p->subscribed != CALL_COMPLETION && !resubscribe) {
23654 if (p->stateid > -1) {
23655 ast_extension_state_del(p->stateid, cb_extensionstate);
23656
23657 dialog_unref(p, "the extensionstate containing this dialog ptr was deleted");
23658 }
23659 p->stateid = ast_extension_state_add(p->context, p->exten, cb_extensionstate, dialog_ref(p,"copying dialog ptr into extension state struct"));
23660 }
23661
23662 if (!req->ignore && p)
23663 p->lastinvite = seqno;
23664 if (p && !p->needdestroy) {
23665 p->expiry = atoi(get_header(req, "Expires"));
23666
23667
23668 if (p->expiry > max_expiry) {
23669 p->expiry = max_expiry;
23670 } else if (p->expiry < min_expiry && p->expiry > 0) {
23671 transmit_response_with_minexpires(p, "423 Interval too small", req);
23672 ast_log(LOG_WARNING, "Received subscription for extension \"%s\" context \"%s\" "
23673 "with Expire header less that 'minexpire' limit. Received \"Expire: %d\" min is %d\n",
23674 p->exten, p->context, p->expiry, min_expiry);
23675 p->expiry = min_expiry;
23676 pvt_set_needdestroy(p, "Expires is less that the min expires allowed. ");
23677 return 0;
23678 }
23679
23680 if (sipdebug) {
23681 if (p->subscribed == MWI_NOTIFICATION && p->relatedpeer) {
23682 ast_debug(2, "Adding subscription for mailbox notification - peer %s\n", p->relatedpeer->name);
23683 } else if (p->subscribed == CALL_COMPLETION) {
23684 ast_debug(2, "Adding CC subscription for peer %s\n", p->username);
23685 } else {
23686 ast_debug(2, "Adding subscription for extension %s context %s for peer %s\n", p->exten, p->context, p->username);
23687 }
23688 }
23689 if (p->autokillid > -1 && sip_cancel_destroy(p))
23690 ast_log(LOG_WARNING, "Unable to cancel SIP destruction. Expect bad things.\n");
23691 if (p->expiry > 0)
23692 sip_scheddestroy(p, (p->expiry + 10) * 1000);
23693
23694 if (p->subscribed == MWI_NOTIFICATION) {
23695 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
23696 transmit_response(p, "200 OK", req);
23697 if (p->relatedpeer) {
23698 ao2_lock(p->relatedpeer);
23699 sip_send_mwi_to_peer(p->relatedpeer, NULL, 0);
23700 ao2_unlock(p->relatedpeer);
23701 }
23702 } else if (p->subscribed != CALL_COMPLETION) {
23703 if ((firststate = ast_extension_state(NULL, p->context, p->exten)) < 0) {
23704
23705 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));
23706 transmit_response(p, "404 Not found", req);
23707 pvt_set_needdestroy(p, "no extension for SUBSCRIBE");
23708 return 0;
23709 }
23710 ast_set_flag(&p->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
23711 transmit_response(p, "200 OK", req);
23712 transmit_state_notify(p, firststate, 1, FALSE);
23713 append_history(p, "Subscribestatus", "%s", ast_extension_state2str(firststate));
23714
23715 ast_string_field_build(p, subscribeuri, "%s@%s", p->exten, p->context);
23716
23717
23718 }
23719 if (!p->expiry) {
23720 pvt_set_needdestroy(p, "forcing expiration");
23721 }
23722 }
23723 return 1;
23724 }
23725
23726
23727 static int handle_request_register(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e)
23728 {
23729 enum check_auth_result res;
23730
23731
23732
23733 if (p->initreq.headers && p->initreq.method != SIP_REGISTER) {
23734 ast_log(LOG_WARNING, "Ignoring spurious REGISTER with Call-ID: %s\n", p->callid);
23735 return -1;
23736 }
23737
23738
23739 copy_request(&p->initreq, req);
23740 if (sipdebug)
23741 ast_debug(4, "Initializing initreq for method %s - callid %s\n", sip_methods[req->method].text, p->callid);
23742 check_via(p, req);
23743 if ((res = register_verify(p, addr, req, e)) < 0) {
23744 const char *reason;
23745
23746 switch (res) {
23747 case AUTH_SECRET_FAILED:
23748 reason = "Wrong password";
23749 break;
23750 case AUTH_USERNAME_MISMATCH:
23751 reason = "Username/auth name mismatch";
23752 break;
23753 case AUTH_NOT_FOUND:
23754 reason = "No matching peer found";
23755 break;
23756 case AUTH_UNKNOWN_DOMAIN:
23757 reason = "Not a local domain";
23758 break;
23759 case AUTH_PEER_NOT_DYNAMIC:
23760 reason = "Peer is not supposed to register";
23761 break;
23762 case AUTH_ACL_FAILED:
23763 reason = "Device does not match ACL";
23764 break;
23765 case AUTH_BAD_TRANSPORT:
23766 reason = "Device not configured to use this transport type";
23767 break;
23768 default:
23769 reason = "Unknown failure";
23770 break;
23771 }
23772 ast_log(LOG_NOTICE, "Registration from '%s' failed for '%s' - %s\n",
23773 get_header(req, "To"), ast_sockaddr_stringify(addr),
23774 reason);
23775 append_history(p, "RegRequest", "Failed : Account %s : %s", get_header(req, "To"), reason);
23776 } else {
23777 req->authenticated = 1;
23778 append_history(p, "RegRequest", "Succeeded : Account %s", get_header(req, "To"));
23779 }
23780
23781 if (res < 1) {
23782
23783
23784 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23785 }
23786 return res;
23787 }
23788
23789
23790
23791
23792
23793
23794
23795
23796 static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int *recount, int *nounlock)
23797 {
23798
23799
23800 const char *cmd;
23801 const char *cseq;
23802 const char *useragent;
23803 const char *via;
23804 const char *callid;
23805 int via_pos = 0;
23806 int seqno;
23807 int len;
23808 int respid;
23809 int res = 0;
23810 int debug = sip_debug_test_pvt(p);
23811 const char *e;
23812 int error = 0;
23813 int oldmethod = p->method;
23814 int acked = 0;
23815
23816
23817
23818
23819 cseq = get_header(req, "Cseq");
23820 cmd = REQ_OFFSET_TO_STR(req, header[0]);
23821
23822 via = __get_header(req, "Via", &via_pos);
23823
23824 callid = get_header(req, "Call-ID");
23825
23826
23827 if (ast_strlen_zero(cmd) || ast_strlen_zero(cseq) || ast_strlen_zero(via)) {
23828 ast_log(LOG_ERROR, "Dropping this SIP message with Call-ID '%s', it's incomplete.\n", callid);
23829 error = 1;
23830 }
23831 if (!error && sscanf(cseq, "%30d%n", &seqno, &len) != 1) {
23832 ast_log(LOG_ERROR, "No seqno in '%s'. Dropping incomplete message.\n", cmd);
23833 error = 1;
23834 }
23835 if (error) {
23836 if (!p->initreq.headers) {
23837 pvt_set_needdestroy(p, "no headers");
23838 }
23839 return -1;
23840 }
23841
23842
23843 cmd = REQ_OFFSET_TO_STR(req, rlPart1);
23844 e = ast_skip_blanks(REQ_OFFSET_TO_STR(req, rlPart2));
23845
23846
23847 useragent = get_header(req, "User-Agent");
23848 if (!ast_strlen_zero(useragent))
23849 ast_string_field_set(p, useragent, useragent);
23850
23851
23852 if (req->method == SIP_RESPONSE) {
23853
23854
23855
23856
23857
23858
23859 if (ast_strlen_zero(e)) {
23860 return 0;
23861 }
23862 if (sscanf(e, "%30d %n", &respid, &len) != 1) {
23863 ast_log(LOG_WARNING, "Invalid response: '%s'\n", e);
23864 return 0;
23865 }
23866 if (respid <= 0) {
23867 ast_log(LOG_WARNING, "Invalid SIP response code: '%d'\n", respid);
23868 return 0;
23869 }
23870
23871
23872
23873 if (!ast_strlen_zero(__get_header(req, "via", &via_pos))) {
23874 ast_log(LOG_WARNING, "Misrouted SIP response '%s' with Call-ID '%s', too many vias\n", e, callid);
23875 return 0;
23876 }
23877 if (p->ocseq && (p->ocseq < seqno)) {
23878 ast_debug(1, "Ignoring out of order response %d (expecting %d)\n", seqno, p->ocseq);
23879 return -1;
23880 } else {
23881 char causevar[256], causeval[256];
23882
23883 if ((respid == 200) || ((respid >= 300) && (respid <= 399))) {
23884 extract_uri(p, req);
23885 }
23886
23887 handle_response(p, respid, e + len, req, seqno);
23888
23889 if (p->owner) {
23890 struct ast_channel *owner = p->owner;
23891
23892 snprintf(causevar, sizeof(causevar), "MASTER_CHANNEL(HASH(SIP_CAUSE,%s))", owner->name);
23893 snprintf(causeval, sizeof(causeval), "SIP %s", REQ_OFFSET_TO_STR(req, rlPart2));
23894
23895 ast_channel_ref(owner);
23896 sip_pvt_unlock(p);
23897 ast_channel_unlock(owner);
23898 *nounlock = 1;
23899 pbx_builtin_setvar_helper(owner, causevar, causeval);
23900 ast_channel_unref(owner);
23901 sip_pvt_lock(p);
23902 }
23903 }
23904 return 0;
23905 }
23906
23907
23908
23909
23910
23911 p->method = req->method;
23912 ast_debug(4, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
23913
23914 if (p->icseq && (p->icseq > seqno) ) {
23915 if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
23916 ast_debug(2, "Got CANCEL or ACK on INVITE with transactions in between.\n");
23917 } else {
23918 ast_debug(1, "Ignoring too old SIP packet packet %d (expecting >= %d)\n", seqno, p->icseq);
23919 if (req->method == SIP_INVITE) {
23920 unsigned int ran = (ast_random() % 10) + 1;
23921 char seconds[4];
23922 snprintf(seconds, sizeof(seconds), "%u", ran);
23923 transmit_response_with_retry_after(p, "500 Server error", req, seconds);
23924 } else if (req->method != SIP_ACK) {
23925 transmit_response(p, "500 Server error", req);
23926 }
23927 return -1;
23928 }
23929 } else if (p->icseq &&
23930 p->icseq == seqno &&
23931 req->method != SIP_ACK &&
23932 (p->method != SIP_CANCEL || p->alreadygone)) {
23933
23934
23935
23936 req->ignore = 1;
23937 ast_debug(3, "Ignoring SIP message because of retransmit (%s Seqno %d, ours %d)\n", sip_methods[p->method].text, p->icseq, seqno);
23938 }
23939
23940
23941
23942 if (!p->pendinginvite && (req->method == SIP_CANCEL)) {
23943 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
23944 return res;
23945 }
23946
23947 if (seqno >= p->icseq)
23948
23949
23950
23951 p->icseq = seqno;
23952
23953
23954 if (ast_strlen_zero(p->theirtag)) {
23955 char tag[128];
23956
23957 gettag(req, "From", tag, sizeof(tag));
23958 ast_string_field_set(p, theirtag, tag);
23959 }
23960 snprintf(p->lastmsg, sizeof(p->lastmsg), "Rx: %s", cmd);
23961
23962 if (sip_cfg.pedanticsipchecking) {
23963
23964
23965
23966
23967 if (!p->initreq.headers && req->has_to_tag) {
23968
23969 if (!req->ignore && req->method == SIP_INVITE) {
23970 transmit_response_reliable(p, "481 Call/Transaction Does Not Exist", req);
23971
23972 } else if (req->method != SIP_ACK) {
23973 transmit_response(p, "481 Call/Transaction Does Not Exist", req);
23974 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23975 } else {
23976 ast_debug(1, "Got ACK for unknown dialog... strange.\n");
23977 }
23978 return res;
23979 }
23980 }
23981
23982 if (!e && (p->method == SIP_INVITE || p->method == SIP_SUBSCRIBE || p->method == SIP_REGISTER || p->method == SIP_NOTIFY || p->method == SIP_PUBLISH)) {
23983 transmit_response(p, "400 Bad request", req);
23984 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
23985 return -1;
23986 }
23987
23988
23989 switch (p->method) {
23990 case SIP_OPTIONS:
23991 res = handle_request_options(p, req, addr, e);
23992 break;
23993 case SIP_INVITE:
23994 res = handle_request_invite(p, req, debug, seqno, addr, recount, e, nounlock);
23995 break;
23996 case SIP_REFER:
23997 res = handle_request_refer(p, req, debug, seqno, nounlock);
23998 break;
23999 case SIP_CANCEL:
24000 res = handle_request_cancel(p, req);
24001 break;
24002 case SIP_BYE:
24003 res = handle_request_bye(p, req);
24004 break;
24005 case SIP_MESSAGE:
24006 res = handle_request_message(p, req);
24007 break;
24008 case SIP_PUBLISH:
24009 res = handle_request_publish(p, req, addr, seqno, e);
24010 break;
24011 case SIP_SUBSCRIBE:
24012 res = handle_request_subscribe(p, req, addr, seqno, e);
24013 break;
24014 case SIP_REGISTER:
24015 res = handle_request_register(p, req, addr, e);
24016 break;
24017 case SIP_INFO:
24018 if (req->debug)
24019 ast_verbose("Receiving INFO!\n");
24020 if (!req->ignore)
24021 handle_request_info(p, req);
24022 else
24023 transmit_response(p, "200 OK", req);
24024 break;
24025 case SIP_NOTIFY:
24026 res = handle_request_notify(p, req, addr, seqno, e);
24027 break;
24028 case SIP_UPDATE:
24029 res = handle_request_update(p, req);
24030 break;
24031 case SIP_ACK:
24032
24033 if (seqno == p->pendinginvite) {
24034 p->invitestate = INV_TERMINATED;
24035 p->pendinginvite = 0;
24036 acked = __sip_ack(p, seqno, 1 , 0);
24037 if (find_sdp(req)) {
24038 if (process_sdp(p, req, SDP_T38_NONE))
24039 return -1;
24040 }
24041 check_pendings(p);
24042 } else if (p->glareinvite == seqno) {
24043
24044 p->glareinvite = 0;
24045 acked = __sip_ack(p, seqno, 1, 0);
24046 }
24047 if (!acked) {
24048
24049
24050 p->method = oldmethod;
24051 }
24052 if (!p->lastinvite && ast_strlen_zero(p->randdata)) {
24053 pvt_set_needdestroy(p, "unmatched ACK");
24054 }
24055 break;
24056 default:
24057 transmit_response_with_allow(p, "501 Method Not Implemented", req, 0);
24058 ast_log(LOG_NOTICE, "Unknown SIP command '%s' from '%s'\n",
24059 cmd, ast_sockaddr_stringify(&p->sa));
24060
24061 if (!p->initreq.headers) {
24062 pvt_set_needdestroy(p, "unimplemented method");
24063 }
24064 break;
24065 }
24066 return res;
24067 }
24068
24069 static void process_request_queue(struct sip_pvt *p, int *recount, int *nounlock)
24070 {
24071 struct sip_request *req;
24072
24073 while ((req = AST_LIST_REMOVE_HEAD(&p->request_queue, next))) {
24074 if (handle_incoming(p, req, &p->recv, recount, nounlock) == -1) {
24075
24076 ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
24077 }
24078 ast_free(req);
24079 }
24080 }
24081
24082 static int scheduler_process_request_queue(const void *data)
24083 {
24084 struct sip_pvt *p = (struct sip_pvt *) data;
24085 int recount = 0;
24086 int nounlock = 0;
24087 int lockretry;
24088
24089 for (lockretry = 10; lockretry > 0; lockretry--) {
24090 sip_pvt_lock(p);
24091
24092
24093
24094 if (!p->owner || !ast_channel_trylock(p->owner)) {
24095 break;
24096 }
24097
24098 if (lockretry != 1) {
24099 sip_pvt_unlock(p);
24100
24101 usleep(1);
24102 }
24103 }
24104
24105 if (!lockretry) {
24106 int retry = !AST_LIST_EMPTY(&p->request_queue);
24107
24108
24109
24110
24111
24112
24113 sip_pvt_unlock(p);
24114 if (!retry) {
24115 dialog_unref(p, "The ref to a dialog passed to this sched callback is going out of scope; unref it.");
24116 }
24117 return retry;
24118 };
24119
24120 process_request_queue(p, &recount, &nounlock);
24121 p->request_queue_sched_id = -1;
24122
24123 if (p->owner && !nounlock) {
24124 ast_channel_unlock(p->owner);
24125 }
24126 sip_pvt_unlock(p);
24127
24128 if (recount) {
24129 ast_update_use_count();
24130 }
24131
24132 dialog_unref(p, "The ref to a dialog passed to this sched callback is going out of scope; unref it.");
24133
24134 return 0;
24135 }
24136
24137 static int queue_request(struct sip_pvt *p, const struct sip_request *req)
24138 {
24139 struct sip_request *newreq;
24140
24141 if (!(newreq = ast_calloc(1, sizeof(*newreq)))) {
24142 return -1;
24143 }
24144
24145 copy_request(newreq, req);
24146 AST_LIST_INSERT_TAIL(&p->request_queue, newreq, next);
24147 if (p->request_queue_sched_id == -1) {
24148 if ((p->request_queue_sched_id = ast_sched_add(sched, 10, scheduler_process_request_queue, dialog_ref(p, "Increment refcount to pass dialog pointer to sched callback"))) == -1) {
24149 dialog_unref(p, "Decrement refcount due to sched_add failure");
24150 }
24151 }
24152
24153 return 0;
24154 }
24155
24156
24157
24158
24159
24160
24161 static int sipsock_read(int *id, int fd, short events, void *ignore)
24162 {
24163 struct sip_request req;
24164 struct ast_sockaddr addr;
24165 int res;
24166 static char readbuf[65535];
24167
24168 memset(&req, 0, sizeof(req));
24169 res = ast_recvfrom(fd, readbuf, sizeof(readbuf) - 1, 0, &addr);
24170 if (res < 0) {
24171 #if !defined(__FreeBSD__)
24172 if (errno == EAGAIN)
24173 ast_log(LOG_NOTICE, "SIP: Received packet with bad UDP checksum\n");
24174 else
24175 #endif
24176 if (errno != ECONNREFUSED)
24177 ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
24178 return 1;
24179 }
24180
24181 readbuf[res] = '\0';
24182
24183 if (!(req.data = ast_str_create(SIP_MIN_PACKET))) {
24184 return 1;
24185 }
24186
24187 if (ast_str_set(&req.data, 0, "%s", readbuf) == AST_DYNSTR_BUILD_FAILED) {
24188 return -1;
24189 }
24190
24191
24192 req.len = ast_str_strlen(req.data);
24193 req.socket.fd = sipsock;
24194 set_socket_transport(&req.socket, SIP_TRANSPORT_UDP);
24195 req.socket.tcptls_session = NULL;
24196 req.socket.port = htons(ast_sockaddr_port(&bindaddr));
24197
24198 handle_request_do(&req, &addr);
24199 deinit_req(&req);
24200
24201 return 1;
24202 }
24203
24204
24205
24206
24207
24208 static int handle_request_do(struct sip_request *req, struct ast_sockaddr *addr)
24209 {
24210 struct sip_pvt *p;
24211 int recount = 0;
24212 int nounlock = 0;
24213 int lockretry;
24214
24215 if (sip_debug_test_addr(addr))
24216 req->debug = 1;
24217 if (sip_cfg.pedanticsipchecking)
24218 req->len = lws2sws(req->data->str, req->len);
24219 if (req->debug) {
24220 ast_verbose("\n<--- SIP read from %s:%s --->\n%s\n<------------->\n",
24221 get_transport(req->socket.type), ast_sockaddr_stringify(addr), req->data->str);
24222 }
24223
24224 if (parse_request(req) == -1) {
24225 ast_str_reset(req->data);
24226 return 1;
24227 }
24228 req->method = find_sip_method(REQ_OFFSET_TO_STR(req, rlPart1));
24229
24230 if (req->debug)
24231 ast_verbose("--- (%d headers %d lines)%s ---\n", req->headers, req->lines, (req->headers + req->lines == 0) ? " Nat keepalive" : "");
24232
24233 if (req->headers < 2) {
24234 ast_str_reset(req->data);
24235 return 1;
24236 }
24237
24238
24239 for (lockretry = 10; lockretry > 0; lockretry--) {
24240 ast_mutex_lock(&netlock);
24241
24242
24243 p = find_call(req, addr, req->method);
24244 if (p == NULL) {
24245 ast_debug(1, "Invalid SIP message - rejected , no callid, len %d\n", req->len);
24246 ast_mutex_unlock(&netlock);
24247 return 1;
24248 }
24249
24250 copy_socket_data(&p->socket, &req->socket);
24251
24252
24253
24254 if (!p->owner || !ast_channel_trylock(p->owner))
24255 break;
24256
24257 if (lockretry != 1) {
24258 sip_pvt_unlock(p);
24259 ao2_t_ref(p, -1, "release p (from find_call) inside lockretry loop");
24260 ast_mutex_unlock(&netlock);
24261
24262 usleep(1);
24263 }
24264 }
24265 ast_sockaddr_copy(&p->recv, addr);
24266
24267
24268 if (p->owner) {
24269 req->authenticated = 1;
24270 }
24271
24272 if (p->do_history)
24273 append_history(p, "Rx", "%s / %s / %s", req->data->str, get_header(req, "CSeq"), REQ_OFFSET_TO_STR(req, rlPart2));
24274
24275 if (!lockretry) {
24276 if (!queue_request(p, req)) {
24277
24278 sip_pvt_unlock(p);
24279 ao2_t_ref(p, -1, "release p (from find_call) after queueing request");
24280 ast_mutex_unlock(&netlock);
24281 return 1;
24282 }
24283
24284 if (p->owner)
24285 ast_log(LOG_ERROR, "Channel lock for %s could not be obtained, and request was unable to be queued.\n", S_OR(p->owner->name, "- no channel name ??? - "));
24286 ast_log(LOG_ERROR, "SIP transaction failed: %s \n", p->callid);
24287 if (req->method != SIP_ACK)
24288 transmit_response(p, "503 Server error", req);
24289
24290 append_history(p, "LockFail", "Owner lock failed, transaction failed.");
24291 sip_pvt_unlock(p);
24292 ao2_t_ref(p, -1, "release p (from find_call) at end of lockretry");
24293 ast_mutex_unlock(&netlock);
24294 return 1;
24295 }
24296
24297
24298
24299
24300 if (!AST_LIST_EMPTY(&p->request_queue)) {
24301 AST_SCHED_DEL_UNREF(sched, p->request_queue_sched_id, dialog_unref(p, "when you delete the request_queue_sched_id sched, you should dec the refcount for the stored dialog ptr"));
24302 process_request_queue(p, &recount, &nounlock);
24303 }
24304
24305 if (handle_incoming(p, req, addr, &recount, &nounlock) == -1) {
24306
24307 ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
24308 }
24309
24310 if (recount)
24311 ast_update_use_count();
24312
24313 if (p->owner && !nounlock)
24314 ast_channel_unlock(p->owner);
24315 sip_pvt_unlock(p);
24316 ao2_t_ref(p, -1, "throw away dialog ptr from find_call at end of routine");
24317 ast_mutex_unlock(&netlock);
24318 return 1;
24319 }
24320
24321
24322
24323
24324
24325
24326
24327 static int sip_standard_port(enum sip_transport type, int port)
24328 {
24329 if (type & SIP_TRANSPORT_TLS)
24330 return port == STANDARD_TLS_PORT;
24331 else
24332 return port == STANDARD_SIP_PORT;
24333 }
24334
24335 static int threadinfo_locate_cb(void *obj, void *arg, int flags)
24336 {
24337 struct sip_threadinfo *th = obj;
24338 struct ast_sockaddr *s = arg;
24339
24340 if (!ast_sockaddr_cmp(s, &th->tcptls_session->remote_address)) {
24341 return CMP_MATCH | CMP_STOP;
24342 }
24343
24344 return 0;
24345 }
24346
24347
24348
24349
24350
24351
24352 static struct ast_tcptls_session_instance *sip_tcp_locate(struct ast_sockaddr *s)
24353 {
24354 struct sip_threadinfo *th;
24355 struct ast_tcptls_session_instance *tcptls_instance = NULL;
24356
24357 if ((th = ao2_callback(threadt, 0, threadinfo_locate_cb, s))) {
24358 tcptls_instance = (ao2_ref(th->tcptls_session, +1), th->tcptls_session);
24359 ao2_t_ref(th, -1, "decrement ref from callback");
24360 }
24361
24362 return tcptls_instance;
24363 }
24364
24365
24366
24367
24368
24369
24370 int get_address_family_filter(const struct ast_sockaddr *addr)
24371 {
24372 if (ast_sockaddr_is_ipv6(addr) && ast_sockaddr_is_any(addr)) {
24373 return 0;
24374 }
24375
24376 return addr->ss.ss_family;
24377 }
24378
24379
24380 static int sip_prepare_socket(struct sip_pvt *p)
24381 {
24382 struct sip_socket *s = &p->socket;
24383 static const char name[] = "SIP socket";
24384 struct sip_threadinfo *th = NULL;
24385 struct ast_tcptls_session_instance *tcptls_session;
24386 struct ast_tcptls_session_args *ca;
24387 struct ast_sockaddr sa_tmp;
24388
24389
24390 if ((s->fd != -1) && (s->type == SIP_TRANSPORT_UDP)) {
24391 return s->fd;
24392 }
24393 if ((s->type & (SIP_TRANSPORT_TCP | SIP_TRANSPORT_TLS)) &&
24394 (s->tcptls_session) &&
24395 (s->tcptls_session->fd != -1)) {
24396 return s->tcptls_session->fd;
24397 }
24398
24399
24400
24401
24402 if (p->outboundproxy && p->outboundproxy->transport) {
24403 s->type = p->outboundproxy->transport;
24404 }
24405
24406 if (s->type == SIP_TRANSPORT_UDP) {
24407 s->fd = sipsock;
24408 return s->fd;
24409 }
24410
24411
24412
24413
24414
24415
24416
24417
24418
24419
24420
24421 ast_sockaddr_copy(&sa_tmp, sip_real_dst(p));
24422 if ((tcptls_session = sip_tcp_locate(&sa_tmp))) {
24423 s->fd = tcptls_session->fd;
24424 if (s->tcptls_session) {
24425 ao2_ref(s->tcptls_session, -1);
24426 s->tcptls_session = NULL;
24427 }
24428 s->tcptls_session = tcptls_session;
24429 return s->fd;
24430
24431 } else if (s->tcptls_session) {
24432 return s->fd;
24433 }
24434
24435
24436
24437 if (!(ca = ao2_alloc(sizeof(*ca), sip_tcptls_client_args_destructor)) ||
24438 !(ca->name = ast_strdup(name))) {
24439 goto create_tcptls_session_fail;
24440 }
24441 ca->accept_fd = -1;
24442 ast_sockaddr_copy(&ca->remote_address,sip_real_dst(p));
24443
24444 if (s->type == SIP_TRANSPORT_TLS) {
24445 if (!(ca->tls_cfg = ast_calloc(1, sizeof(*ca->tls_cfg)))) {
24446 goto create_tcptls_session_fail;
24447 }
24448 memcpy(ca->tls_cfg, &default_tls_cfg, sizeof(*ca->tls_cfg));
24449
24450 if (!(ca->tls_cfg->certfile = ast_strdup(default_tls_cfg.certfile)) ||
24451 !(ca->tls_cfg->pvtfile = ast_strdup(default_tls_cfg.pvtfile)) ||
24452 !(ca->tls_cfg->cipher = ast_strdup(default_tls_cfg.cipher)) ||
24453 !(ca->tls_cfg->cafile = ast_strdup(default_tls_cfg.cafile)) ||
24454 !(ca->tls_cfg->capath = ast_strdup(default_tls_cfg.capath))) {
24455
24456 goto create_tcptls_session_fail;
24457 }
24458
24459
24460 if (!ast_strlen_zero(p->tohost)) {
24461 ast_copy_string(ca->hostname, p->tohost, sizeof(ca->hostname));
24462 }
24463 }
24464
24465
24466 if (!(s->tcptls_session = ast_tcptls_client_create(ca))) {
24467 goto create_tcptls_session_fail;
24468 }
24469
24470 s->fd = s->tcptls_session->fd;
24471
24472
24473
24474
24475
24476 if (!(th = sip_threadinfo_create(s->tcptls_session, s->type))) {
24477 goto create_tcptls_session_fail;
24478 }
24479
24480
24481 ao2_ref(s->tcptls_session, +1);
24482
24483 if (ast_pthread_create_background(&ca->master, NULL, sip_tcp_worker_fn, s->tcptls_session)) {
24484 ast_debug(1, "Unable to launch '%s'.", ca->name);
24485 ao2_ref(s->tcptls_session, -1);
24486 goto create_tcptls_session_fail;
24487 }
24488
24489 return s->fd;
24490
24491 create_tcptls_session_fail:
24492 if (ca) {
24493 ao2_t_ref(ca, -1, "failed to create client, getting rid of client tcptls_session arguments");
24494 }
24495 if (s->tcptls_session) {
24496 close(tcptls_session->fd);
24497 s->fd = tcptls_session->fd = -1;
24498 ao2_ref(s->tcptls_session, -1);
24499 s->tcptls_session = NULL;
24500 }
24501 if (th) {
24502 ao2_t_unlink(threadt, th, "Removing tcptls thread info object, thread failed to open");
24503 }
24504
24505 return -1;
24506 }
24507
24508
24509
24510
24511
24512
24513 static int get_cached_mwi(struct sip_peer *peer, int *new, int *old)
24514 {
24515 struct sip_mailbox *mailbox;
24516
24517 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
24518 struct ast_event *event;
24519 event = ast_event_get_cached(AST_EVENT_MWI,
24520 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox->mailbox,
24521 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, S_OR(mailbox->context, "default"),
24522 AST_EVENT_IE_END);
24523 if (!event)
24524 continue;
24525 *new += ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
24526 *old += ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
24527 ast_event_destroy(event);
24528 }
24529
24530 return (*new || *old) ? 0 : 1;
24531 }
24532
24533
24534 static int sip_send_mwi_to_peer(struct sip_peer *peer, const struct ast_event *event, int cache_only)
24535 {
24536
24537 struct sip_pvt *p;
24538 int newmsgs = 0, oldmsgs = 0;
24539
24540 if (ast_test_flag((&peer->flags[1]), SIP_PAGE2_SUBSCRIBEMWIONLY) && !peer->mwipvt)
24541 return 0;
24542
24543
24544 if (ast_sockaddr_isnull(&peer->addr) && ast_sockaddr_isnull(&peer->defaddr))
24545 return 0;
24546
24547 if (event) {
24548 newmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
24549 oldmsgs = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
24550 } else if (!cache_only) {
24551 struct ast_str *mailbox_str = ast_str_alloca(512);
24552 peer_mailboxes_to_str(&mailbox_str, peer);
24553 ast_app_inboxcount(mailbox_str->str, &newmsgs, &oldmsgs);
24554 } else {
24555 get_cached_mwi(peer, &newmsgs, &oldmsgs);
24556 }
24557
24558 if (peer->mwipvt) {
24559
24560 p = dialog_ref(peer->mwipvt, "sip_send_mwi_to_peer: Setting dialog ptr p from peer->mwipvt-- should this be done?");
24561 } else {
24562
24563 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL)))
24564 return -1;
24565
24566
24567
24568
24569 set_socket_transport(&p->socket, 0);
24570 if (create_addr_from_peer(p, peer)) {
24571
24572 dialog_unlink_all(p, TRUE, TRUE);
24573 dialog_unref(p, "unref dialog p just created via sip_alloc");
24574
24575 return 0;
24576 }
24577
24578 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
24579 build_via(p);
24580 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
24581 build_callid_pvt(p);
24582 if (!ast_strlen_zero(peer->mwi_from)) {
24583 ast_string_field_set(p, mwi_from, peer->mwi_from);
24584 } else if (!ast_strlen_zero(default_mwi_from)) {
24585 ast_string_field_set(p, mwi_from, default_mwi_from);
24586 }
24587 ao2_t_link(dialogs, p, "Linking in under new name");
24588
24589 sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
24590 }
24591
24592
24593 ast_set_flag(&p->flags[0], SIP_OUTGOING);
24594
24595 transmit_notify_with_mwi(p, newmsgs, oldmsgs, peer->vmexten);
24596 dialog_unref(p, "unref dialog ptr p just before it goes out of scope at the end of sip_send_mwi_to_peer.");
24597 return 0;
24598 }
24599
24600
24601 static void check_rtp_timeout(struct sip_pvt *dialog, time_t t)
24602 {
24603
24604 if (!dialog->rtp || !dialog->owner)
24605 return;
24606
24607
24608 if (dialog->owner->_state != AST_STATE_UP || !ast_sockaddr_isnull(&dialog->redirip))
24609 return;
24610
24611
24612 if (dialog->t38.state == T38_ENABLED)
24613 return;
24614
24615
24616 if (!ast_rtp_instance_get_timeout(dialog->rtp) && !ast_rtp_instance_get_hold_timeout(dialog->rtp)) {
24617 return;
24618 }
24619
24620
24621
24622
24623
24624
24625
24626
24627 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))) {
24628 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)))) {
24629
24630 if (ast_rtp_instance_get_timeout(dialog->rtp)) {
24631 while (dialog->owner && ast_channel_trylock(dialog->owner)) {
24632 sip_pvt_unlock(dialog);
24633 usleep(1);
24634 sip_pvt_lock(dialog);
24635 }
24636 if (!dialog->owner) {
24637 return;
24638 }
24639 ast_log(LOG_NOTICE, "Disconnecting call '%s' for lack of RTP activity in %ld seconds\n",
24640 dialog->owner->name, (long) (t - dialog->lastrtprx));
24641
24642 ast_softhangup_nolock(dialog->owner, AST_SOFTHANGUP_DEV);
24643 ast_channel_unlock(dialog->owner);
24644
24645
24646
24647
24648 ast_rtp_instance_set_timeout(dialog->rtp, 0);
24649 ast_rtp_instance_set_hold_timeout(dialog->rtp, 0);
24650 if (dialog->vrtp) {
24651 ast_rtp_instance_set_timeout(dialog->vrtp, 0);
24652 ast_rtp_instance_set_hold_timeout(dialog->vrtp, 0);
24653 }
24654 }
24655 }
24656 }
24657 }
24658
24659
24660
24661
24662
24663 static void *do_monitor(void *data)
24664 {
24665 int res;
24666 time_t t;
24667 int reloading;
24668
24669
24670 if (sipsock > -1)
24671 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
24672
24673
24674 for(;;) {
24675
24676 ast_mutex_lock(&sip_reload_lock);
24677 reloading = sip_reloading;
24678 sip_reloading = FALSE;
24679 ast_mutex_unlock(&sip_reload_lock);
24680 if (reloading) {
24681 ast_verb(1, "Reloading SIP\n");
24682 sip_do_reload(sip_reloadreason);
24683
24684
24685 if (sipsock > -1) {
24686 if (sipsock_read_id)
24687 sipsock_read_id = ast_io_change(io, sipsock_read_id, sipsock, NULL, 0, NULL);
24688 else
24689 sipsock_read_id = ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
24690 } else if (sipsock_read_id) {
24691 ast_io_remove(io, sipsock_read_id);
24692 sipsock_read_id = NULL;
24693 }
24694 }
24695
24696
24697 t = time(NULL);
24698
24699
24700
24701
24702 ao2_t_callback(dialogs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, dialog_needdestroy, &t,
24703 "callback to remove dialogs w/needdestroy");
24704
24705
24706
24707
24708
24709
24710
24711
24712
24713
24714
24715 pthread_testcancel();
24716
24717 res = ast_sched_wait(sched);
24718 if ((res < 0) || (res > 1000))
24719 res = 1000;
24720 res = ast_io_wait(io, res);
24721 if (res > 20)
24722 ast_debug(1, "chan_sip: ast_io_wait ran %d all at once\n", res);
24723 ast_mutex_lock(&monlock);
24724 res = ast_sched_runq(sched);
24725 if (res >= 20)
24726 ast_debug(1, "chan_sip: ast_sched_runq ran %d all at once\n", res);
24727 ast_mutex_unlock(&monlock);
24728 }
24729
24730
24731 return NULL;
24732 }
24733
24734
24735 static int restart_monitor(void)
24736 {
24737
24738 if (monitor_thread == AST_PTHREADT_STOP)
24739 return 0;
24740 ast_mutex_lock(&monlock);
24741 if (monitor_thread == pthread_self()) {
24742 ast_mutex_unlock(&monlock);
24743 ast_log(LOG_WARNING, "Cannot kill myself\n");
24744 return -1;
24745 }
24746 if (monitor_thread != AST_PTHREADT_NULL) {
24747
24748 pthread_kill(monitor_thread, SIGURG);
24749 } else {
24750
24751 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
24752 ast_mutex_unlock(&monlock);
24753 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
24754 return -1;
24755 }
24756 }
24757 ast_mutex_unlock(&monlock);
24758 return 0;
24759 }
24760
24761
24762
24763 static void restart_session_timer(struct sip_pvt *p)
24764 {
24765 if (!p->stimer) {
24766 ast_log(LOG_WARNING, "Null stimer in restart_session_timer - %s\n", p->callid);
24767 return;
24768 }
24769
24770 if (p->stimer->st_active == TRUE) {
24771 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
24772 dialog_unref(p, "Removing session timer ref"));
24773 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
24774 start_session_timer(p);
24775 }
24776 }
24777
24778
24779
24780 static void stop_session_timer(struct sip_pvt *p)
24781 {
24782 if (!p->stimer) {
24783 ast_log(LOG_WARNING, "Null stimer in stop_session_timer - %s\n", p->callid);
24784 return;
24785 }
24786
24787 if (p->stimer->st_active == TRUE) {
24788 p->stimer->st_active = FALSE;
24789 AST_SCHED_DEL_UNREF(sched, p->stimer->st_schedid,
24790 dialog_unref(p, "removing session timer ref"));
24791 ast_debug(2, "Session timer stopped: %d - %s\n", p->stimer->st_schedid, p->callid);
24792 }
24793 }
24794
24795
24796
24797 static void start_session_timer(struct sip_pvt *p)
24798 {
24799 if (!p->stimer) {
24800 ast_log(LOG_WARNING, "Null stimer in start_session_timer - %s\n", p->callid);
24801 return;
24802 }
24803
24804 p->stimer->st_schedid = ast_sched_add(sched, p->stimer->st_interval * 1000 / 2, proc_session_timer,
24805 dialog_ref(p, "adding session timer ref"));
24806 if (p->stimer->st_schedid < 0) {
24807 dialog_unref(p, "removing session timer ref");
24808 ast_log(LOG_ERROR, "ast_sched_add failed.\n");
24809 }
24810 ast_debug(2, "Session timer started: %d - %s\n", p->stimer->st_schedid, p->callid);
24811 }
24812
24813
24814
24815 static int proc_session_timer(const void *vp)
24816 {
24817 struct sip_pvt *p = (struct sip_pvt *) vp;
24818 int sendreinv = FALSE;
24819 int res = 0;
24820
24821 if (!p->stimer) {
24822 ast_log(LOG_WARNING, "Null stimer in proc_session_timer - %s\n", p->callid);
24823 goto return_unref;
24824 }
24825
24826 ast_debug(2, "Session timer expired: %d - %s\n", p->stimer->st_schedid, p->callid);
24827
24828 if (!p->owner) {
24829 goto return_unref;
24830 }
24831
24832 if ((p->stimer->st_active != TRUE) || (p->owner->_state != AST_STATE_UP)) {
24833 goto return_unref;
24834 }
24835
24836 switch (p->stimer->st_ref) {
24837 case SESSION_TIMER_REFRESHER_UAC:
24838 if (p->outgoing_call == TRUE) {
24839 sendreinv = TRUE;
24840 }
24841 break;
24842 case SESSION_TIMER_REFRESHER_UAS:
24843 if (p->outgoing_call != TRUE) {
24844 sendreinv = TRUE;
24845 }
24846 break;
24847 default:
24848 ast_log(LOG_ERROR, "Unknown session refresher %d\n", p->stimer->st_ref);
24849 goto return_unref;
24850 }
24851
24852 if (sendreinv == TRUE) {
24853 res = 1;
24854 transmit_reinvite_with_sdp(p, FALSE, TRUE);
24855 } else {
24856 p->stimer->st_expirys++;
24857 if (p->stimer->st_expirys >= 2) {
24858 if (p->stimer->quit_flag) {
24859 goto return_unref;
24860 }
24861 ast_log(LOG_WARNING, "Session-Timer expired - %s\n", p->callid);
24862 sip_pvt_lock(p);
24863 while (p->owner && ast_channel_trylock(p->owner)) {
24864 sip_pvt_unlock(p);
24865 usleep(1);
24866 if (p->stimer && p->stimer->quit_flag) {
24867 goto return_unref;
24868 }
24869 sip_pvt_lock(p);
24870 }
24871
24872 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
24873 ast_channel_unlock(p->owner);
24874 sip_pvt_unlock(p);
24875 }
24876 }
24877
24878 return_unref:
24879 if (!res) {
24880
24881 if (p->stimer) {
24882 p->stimer->st_schedid = -1;
24883 stop_session_timer(p);
24884 }
24885
24886
24887
24888 dialog_unref(p, "removing session timer ref");
24889 }
24890
24891 return res;
24892 }
24893
24894
24895
24896 int parse_minse (const char *p_hdrval, int *const p_interval)
24897 {
24898 if (ast_strlen_zero(p_hdrval)) {
24899 ast_log(LOG_WARNING, "Null Min-SE header\n");
24900 return -1;
24901 }
24902
24903 *p_interval = 0;
24904 p_hdrval = ast_skip_blanks(p_hdrval);
24905 if (!sscanf(p_hdrval, "%30d", p_interval)) {
24906 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
24907 return -1;
24908 }
24909
24910 ast_debug(2, "Received Min-SE: %d\n", *p_interval);
24911 return 0;
24912 }
24913
24914
24915
24916 int parse_session_expires(const char *p_hdrval, int *const p_interval, enum st_refresher *const p_ref)
24917 {
24918 char *p_token;
24919 int ref_idx;
24920 char *p_se_hdr;
24921
24922 if (ast_strlen_zero(p_hdrval)) {
24923 ast_log(LOG_WARNING, "Null Session-Expires header\n");
24924 return -1;
24925 }
24926
24927 *p_ref = SESSION_TIMER_REFRESHER_AUTO;
24928 *p_interval = 0;
24929
24930 p_se_hdr = ast_strdupa(p_hdrval);
24931 p_se_hdr = ast_skip_blanks(p_se_hdr);
24932
24933 while ((p_token = strsep(&p_se_hdr, ";"))) {
24934 p_token = ast_skip_blanks(p_token);
24935 if (!sscanf(p_token, "%30d", p_interval)) {
24936 ast_log(LOG_WARNING, "Parsing of Session-Expires failed\n");
24937 return -1;
24938 }
24939
24940 ast_debug(2, "Session-Expires: %d\n", *p_interval);
24941
24942 if (!p_se_hdr)
24943 continue;
24944
24945 p_se_hdr = ast_skip_blanks(p_se_hdr);
24946 ref_idx = strlen("refresher=");
24947 if (!strncasecmp(p_se_hdr, "refresher=", ref_idx)) {
24948 p_se_hdr += ref_idx;
24949 p_se_hdr = ast_skip_blanks(p_se_hdr);
24950
24951 if (!strncasecmp(p_se_hdr, "uac", strlen("uac"))) {
24952 *p_ref = SESSION_TIMER_REFRESHER_UAC;
24953 ast_debug(2, "Refresher: UAC\n");
24954 } else if (!strncasecmp(p_se_hdr, "uas", strlen("uas"))) {
24955 *p_ref = SESSION_TIMER_REFRESHER_UAS;
24956 ast_debug(2, "Refresher: UAS\n");
24957 } else {
24958 ast_log(LOG_WARNING, "Invalid refresher value %s\n", p_se_hdr);
24959 return -1;
24960 }
24961 break;
24962 }
24963 }
24964 return 0;
24965 }
24966
24967
24968
24969
24970
24971
24972
24973
24974
24975 static void proc_422_rsp(struct sip_pvt *p, struct sip_request *rsp)
24976 {
24977 int rtn;
24978 const char *p_hdrval;
24979 int minse;
24980
24981 p_hdrval = get_header(rsp, "Min-SE");
24982 if (ast_strlen_zero(p_hdrval)) {
24983 ast_log(LOG_WARNING, "422 response without a Min-SE header %s\n", p_hdrval);
24984 return;
24985 }
24986 rtn = parse_minse(p_hdrval, &minse);
24987 if (rtn != 0) {
24988 ast_log(LOG_WARNING, "Parsing of Min-SE header failed %s\n", p_hdrval);
24989 return;
24990 }
24991 p->stimer->st_interval = minse;
24992 transmit_invite(p, SIP_INVITE, 1, 2, NULL);
24993 }
24994
24995
24996
24997
24998
24999
25000 int st_get_se(struct sip_pvt *p, int max)
25001 {
25002 if (max == TRUE) {
25003 if (p->stimer->st_cached_max_se) {
25004 return p->stimer->st_cached_max_se;
25005 }
25006 if (p->relatedpeer) {
25007 p->stimer->st_cached_max_se = p->relatedpeer->stimer.st_max_se;
25008 return (p->stimer->st_cached_max_se);
25009 }
25010 p->stimer->st_cached_max_se = global_max_se;
25011 return (p->stimer->st_cached_max_se);
25012 }
25013
25014 if (p->stimer->st_cached_min_se) {
25015 return p->stimer->st_cached_min_se;
25016 }
25017 if (p->relatedpeer) {
25018 p->stimer->st_cached_min_se = p->relatedpeer->stimer.st_min_se;
25019 return (p->stimer->st_cached_min_se);
25020 }
25021 p->stimer->st_cached_min_se = global_min_se;
25022 return (p->stimer->st_cached_min_se);
25023 }
25024
25025
25026
25027
25028
25029 enum st_refresher st_get_refresher(struct sip_pvt *p)
25030 {
25031 if (p->stimer->st_cached_ref != SESSION_TIMER_REFRESHER_AUTO)
25032 return p->stimer->st_cached_ref;
25033
25034 if (p->relatedpeer) {
25035 p->stimer->st_cached_ref = p->relatedpeer->stimer.st_ref;
25036 return p->stimer->st_cached_ref;
25037 }
25038
25039 p->stimer->st_cached_ref = global_st_refresher;
25040 return global_st_refresher;
25041 }
25042
25043
25044
25045
25046
25047 enum st_mode st_get_mode(struct sip_pvt *p)
25048 {
25049 if (!p->stimer)
25050 sip_st_alloc(p);
25051
25052 if (p->stimer->st_cached_mode != SESSION_TIMER_MODE_INVALID)
25053 return p->stimer->st_cached_mode;
25054
25055 if (p->relatedpeer) {
25056 p->stimer->st_cached_mode = p->relatedpeer->stimer.st_mode_oper;
25057 return p->stimer->st_cached_mode;
25058 }
25059
25060 p->stimer->st_cached_mode = global_st_mode;
25061 return global_st_mode;
25062 }
25063
25064
25065
25066 static int sip_poke_noanswer(const void *data)
25067 {
25068 struct sip_peer *peer = (struct sip_peer *)data;
25069
25070 peer->pokeexpire = -1;
25071
25072 if (peer->lastms > -1) {
25073 ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Last qualify: %d\n", peer->name, peer->lastms);
25074 if (sip_cfg.peer_rtupdate) {
25075 ast_update_realtime(ast_check_realtime("sipregs") ? "sipregs" : "sippeers", "name", peer->name, "lastms", "-1", SENTINEL);
25076 }
25077 manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: SIP\r\nPeer: SIP/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, -1);
25078 if (sip_cfg.regextenonqualify) {
25079 register_peer_exten(peer, FALSE);
25080 }
25081 }
25082
25083 if (peer->call) {
25084 dialog_unlink_all(peer->call, TRUE, TRUE);
25085 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
25086
25087 }
25088
25089
25090 if (peer->lastms > -1) {
25091 peer->lastms = -1;
25092 ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", peer->name);
25093 }
25094
25095
25096 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched,
25097 DEFAULT_FREQ_NOTOK, sip_poke_peer_s, peer,
25098 unref_peer(_data, "removing poke peer ref"),
25099 unref_peer(peer, "removing poke peer ref"),
25100 ref_peer(peer, "adding poke peer ref"));
25101
25102
25103 unref_peer(peer, "release peer poke noanswer ref");
25104
25105 return 0;
25106 }
25107
25108
25109
25110
25111
25112
25113 static int sip_poke_peer(struct sip_peer *peer, int force)
25114 {
25115 struct sip_pvt *p;
25116 int xmitres = 0;
25117
25118 if ((!peer->maxms && !force) || ast_sockaddr_isnull(&peer->addr)) {
25119
25120
25121 AST_SCHED_DEL_UNREF(sched, peer->pokeexpire,
25122 unref_peer(peer, "removing poke peer ref"));
25123
25124 peer->lastms = 0;
25125 if (peer->call) {
25126 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
25127 }
25128 return 0;
25129 }
25130 if (peer->call) {
25131 if (sipdebug) {
25132 ast_log(LOG_NOTICE, "Still have a QUALIFY dialog active, deleting\n");
25133 }
25134 dialog_unlink_all(peer->call, TRUE, TRUE);
25135 peer->call = dialog_unref(peer->call, "unref dialog peer->call");
25136
25137 }
25138 if (!(p = sip_alloc(NULL, NULL, 0, SIP_OPTIONS, NULL))) {
25139 return -1;
25140 }
25141 peer->call = dialog_ref(p, "copy sip alloc from p to peer->call");
25142
25143 p->sa = peer->addr;
25144 p->recv = peer->addr;
25145 copy_socket_data(&p->socket, &peer->socket);
25146 ast_copy_flags(&p->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
25147 ast_copy_flags(&p->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
25148 ast_copy_flags(&p->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
25149
25150
25151 if (!ast_strlen_zero(peer->fullcontact))
25152 ast_string_field_set(p, fullcontact, peer->fullcontact);
25153
25154 if (!ast_strlen_zero(peer->tohost))
25155 ast_string_field_set(p, tohost, peer->tohost);
25156 else
25157 ast_string_field_set(p, tohost, ast_sockaddr_stringify_host(&peer->addr));
25158
25159
25160 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
25161 build_via(p);
25162 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
25163 build_callid_pvt(p);
25164 ao2_t_link(dialogs, p, "Linking in under new name");
25165
25166 AST_SCHED_DEL_UNREF(sched, peer->pokeexpire,
25167 unref_peer(peer, "removing poke peer ref"));
25168
25169 if (p->relatedpeer)
25170 p->relatedpeer = unref_peer(p->relatedpeer,"unsetting the relatedpeer field in the dialog, before it is set to something else.");
25171 p->relatedpeer = ref_peer(peer, "setting the relatedpeer field in the dialog");
25172 ast_set_flag(&p->flags[0], SIP_OUTGOING);
25173 #ifdef VOCAL_DATA_HACK
25174 ast_copy_string(p->username, "__VOCAL_DATA_SHOULD_READ_THE_SIP_SPEC__", sizeof(p->username));
25175 xmitres = transmit_invite(p, SIP_INVITE, 0, 2, NULL);
25176 #else
25177 xmitres = transmit_invite(p, SIP_OPTIONS, 0, 2, NULL);
25178 #endif
25179 peer->ps = ast_tvnow();
25180 if (xmitres == XMIT_ERROR) {
25181 sip_poke_noanswer(peer);
25182 } else if (!force) {
25183 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, peer->maxms * 2, sip_poke_noanswer, peer,
25184 unref_peer(_data, "removing poke peer ref"),
25185 unref_peer(peer, "removing poke peer ref"),
25186 ref_peer(peer, "adding poke peer ref"));
25187 }
25188 dialog_unref(p, "unref dialog at end of sip_poke_peer, obtained from sip_alloc, just before it goes out of scope");
25189 return 0;
25190 }
25191
25192
25193
25194
25195
25196
25197
25198
25199
25200
25201
25202
25203
25204
25205
25206
25207
25208
25209
25210
25211
25212
25213
25214
25215
25216
25217
25218
25219
25220
25221
25222
25223
25224
25225 static int sip_devicestate(void *data)
25226 {
25227 char *host;
25228 char *tmp;
25229 struct sip_peer *p;
25230
25231 int res = AST_DEVICE_INVALID;
25232
25233
25234 host = ast_strdupa(data ? data : "");
25235 if ((tmp = strchr(host, '@')))
25236 host = tmp + 1;
25237
25238 ast_debug(3, "Checking device state for peer %s\n", host);
25239
25240
25241
25242
25243
25244
25245
25246
25247 if ((p = find_peer(host, NULL, FALSE, FINDALLDEVICES, TRUE, 0))) {
25248 if (!(ast_sockaddr_isnull(&p->addr) && ast_sockaddr_isnull(&p->defaddr))) {
25249
25250
25251
25252
25253
25254
25255
25256
25257
25258
25259
25260 if (p->onHold)
25261
25262 res = AST_DEVICE_ONHOLD;
25263 else if (p->inRinging) {
25264 if (p->inRinging == p->inUse)
25265 res = AST_DEVICE_RINGING;
25266 else
25267 res = AST_DEVICE_RINGINUSE;
25268 } else if (p->call_limit && (p->inUse == p->call_limit))
25269
25270 res = AST_DEVICE_BUSY;
25271 else if (p->call_limit && p->busy_level && p->inUse >= p->busy_level)
25272
25273 res = AST_DEVICE_BUSY;
25274 else if (p->call_limit && p->inUse)
25275
25276 res = AST_DEVICE_INUSE;
25277 else if (p->maxms && ((p->lastms > p->maxms) || (p->lastms < 0)))
25278
25279 res = AST_DEVICE_UNAVAILABLE;
25280 else
25281 res = AST_DEVICE_NOT_INUSE;
25282 } else {
25283
25284 res = AST_DEVICE_UNAVAILABLE;
25285 }
25286 unref_peer(p, "unref_peer, from sip_devicestate, release ref from find_peer");
25287 } else {
25288 res = AST_DEVICE_UNKNOWN;
25289 }
25290
25291 return res;
25292 }
25293
25294
25295
25296
25297
25298
25299
25300
25301
25302
25303
25304 static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
25305 {
25306 struct sip_pvt *p;
25307 struct ast_channel *tmpc = NULL;
25308 char *ext = NULL, *host;
25309 char tmp[256];
25310 char *dest = data;
25311 char *dnid;
25312 char *secret = NULL;
25313 char *md5secret = NULL;
25314 char *authname = NULL;
25315 char *trans = NULL;
25316 char dialstring[256];
25317 char *remote_address;
25318 enum sip_transport transport = 0;
25319 struct ast_sockaddr remote_address_sa = { {0,} };
25320 format_t oldformat = format;
25321 AST_DECLARE_APP_ARGS(args,
25322 AST_APP_ARG(peerorhost);
25323 AST_APP_ARG(exten);
25324 AST_APP_ARG(remote_address);
25325 );
25326
25327
25328
25329
25330
25331
25332
25333
25334 format &= AST_FORMAT_AUDIO_MASK;
25335 if (!format) {
25336 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));
25337 *cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
25338 return NULL;
25339 }
25340 ast_debug(1, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
25341
25342 if (ast_strlen_zero(dest)) {
25343 ast_log(LOG_ERROR, "Unable to create channel with empty destination.\n");
25344 *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
25345 return NULL;
25346 }
25347
25348 if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE, NULL))) {
25349 ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", dest);
25350 *cause = AST_CAUSE_SWITCH_CONGESTION;
25351 return NULL;
25352 }
25353
25354 p->outgoing_call = TRUE;
25355
25356 snprintf(dialstring, sizeof(dialstring), "%s/%s", type, dest);
25357 ast_string_field_set(p, dialstring, dialstring);
25358
25359 if (!(p->options = ast_calloc(1, sizeof(*p->options)))) {
25360 dialog_unlink_all(p, TRUE, TRUE);
25361 dialog_unref(p, "unref dialog p from mem fail");
25362
25363 ast_log(LOG_ERROR, "Unable to build option SIP data structure - Out of memory\n");
25364 *cause = AST_CAUSE_SWITCH_CONGESTION;
25365 return NULL;
25366 }
25367
25368
25369 ast_copy_string(tmp, dest, sizeof(tmp));
25370
25371
25372 dnid = strchr(tmp, '!');
25373 if (dnid != NULL) {
25374 *dnid++ = '\0';
25375 ast_string_field_set(p, todnid, dnid);
25376 }
25377
25378
25379 AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
25380
25381
25382 host = strchr(args.peerorhost, '@');
25383 if (host) {
25384 *host++ = '\0';
25385 ext = args.peerorhost;
25386 secret = strchr(ext, ':');
25387 }
25388 if (secret) {
25389 *secret++ = '\0';
25390 md5secret = strchr(secret, ':');
25391 }
25392 if (md5secret) {
25393 *md5secret++ = '\0';
25394 authname = strchr(md5secret, ':');
25395 }
25396 if (authname) {
25397 *authname++ = '\0';
25398 trans = strchr(authname, ':');
25399 }
25400 if (trans) {
25401 *trans++ = '\0';
25402 if (!strcasecmp(trans, "tcp"))
25403 transport = SIP_TRANSPORT_TCP;
25404 else if (!strcasecmp(trans, "tls"))
25405 transport = SIP_TRANSPORT_TLS;
25406 else {
25407 if (strcasecmp(trans, "udp"))
25408 ast_log(LOG_WARNING, "'%s' is not a valid transport option to Dial() for SIP calls, using udp by default.\n", trans);
25409 transport = SIP_TRANSPORT_UDP;
25410 }
25411 } else {
25412 transport = SIP_TRANSPORT_UDP;
25413 }
25414
25415 if (!host) {
25416 ext = args.exten;
25417 host = args.peerorhost;
25418 remote_address = args.remote_address;
25419 } else {
25420 remote_address = args.remote_address;
25421 if (!ast_strlen_zero(args.exten)) {
25422 ast_log(LOG_NOTICE, "Conflicting extension values given. Using '%s' and not '%s'\n", ext, args.exten);
25423 }
25424 }
25425
25426 if (!ast_strlen_zero(remote_address)) {
25427 if (ast_sockaddr_resolve_first(&remote_address_sa, remote_address, 0)) {
25428 ast_log(LOG_WARNING, "Unable to find IP address for host %s. We will not use this remote IP address\n", remote_address);
25429 } else {
25430 if (!ast_sockaddr_port(&remote_address_sa)) {
25431 ast_sockaddr_set_port(&remote_address_sa,
25432 transport & SIP_TRANSPORT_TLS ?
25433 STANDARD_TLS_PORT :
25434 STANDARD_SIP_PORT);
25435 }
25436 }
25437 }
25438
25439 set_socket_transport(&p->socket, transport);
25440
25441
25442
25443
25444
25445
25446 if (create_addr(p, host, NULL, 1, &remote_address_sa)) {
25447 *cause = AST_CAUSE_UNREGISTERED;
25448 ast_debug(3, "Cant create SIP call - target device not registered\n");
25449 dialog_unlink_all(p, TRUE, TRUE);
25450 dialog_unref(p, "unref dialog p UNREGISTERED");
25451
25452 return NULL;
25453 }
25454 if (ast_strlen_zero(p->peername) && ext)
25455 ast_string_field_set(p, peername, ext);
25456
25457 ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
25458 build_via(p);
25459 ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
25460 build_callid_pvt(p);
25461 ao2_t_link(dialogs, p, "Linking in under new name");
25462
25463
25464
25465
25466
25467
25468 if (ext) {
25469 ast_string_field_set(p, username, ext);
25470 ast_string_field_set(p, fullcontact, NULL);
25471 }
25472 if (secret && !ast_strlen_zero(secret))
25473 ast_string_field_set(p, peersecret, secret);
25474
25475 if (md5secret && !ast_strlen_zero(md5secret))
25476 ast_string_field_set(p, peermd5secret, md5secret);
25477
25478 if (authname && !ast_strlen_zero(authname))
25479 ast_string_field_set(p, authname, authname);
25480 #if 0
25481 printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
25482 #endif
25483 p->prefcodec = oldformat;
25484 p->jointcapability = oldformat & p->capability;
25485 sip_pvt_lock(p);
25486 tmpc = sip_new(p, AST_STATE_DOWN, host, requestor ? requestor->linkedid : NULL);
25487 if (sip_cfg.callevents)
25488 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
25489 "Channel: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
25490 p->owner? p->owner->name : "", "SIP", p->callid, p->fullcontact, p->peername);
25491 sip_pvt_unlock(p);
25492 if (!tmpc) {
25493 dialog_unlink_all(p, TRUE, TRUE);
25494
25495 }
25496 dialog_unref(p, "toss pvt ptr at end of sip_request_call");
25497 ast_update_use_count();
25498 restart_monitor();
25499 return tmpc;
25500 }
25501
25502
25503 static void set_insecure_flags (struct ast_flags *flags, const char *value, int lineno)
25504 {
25505 if (ast_strlen_zero(value))
25506 return;
25507
25508 if (!ast_false(value)) {
25509 char buf[64];
25510 char *word, *next;
25511
25512 ast_copy_string(buf, value, sizeof(buf));
25513 next = buf;
25514 while ((word = strsep(&next, ","))) {
25515 if (!strcasecmp(word, "port"))
25516 ast_set_flag(&flags[0], SIP_INSECURE_PORT);
25517 else if (!strcasecmp(word, "invite"))
25518 ast_set_flag(&flags[0], SIP_INSECURE_INVITE);
25519 else
25520 ast_log(LOG_WARNING, "Unknown insecure mode '%s' on line %d\n", value, lineno);
25521 }
25522 }
25523 }
25524
25525
25526
25527
25528
25529 static int handle_t38_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v,
25530 int *maxdatagram)
25531 {
25532 int res = 1;
25533
25534 if (!strcasecmp(v->name, "t38pt_udptl")) {
25535 char *buf = ast_strdupa(v->value);
25536 char *word, *next = buf;
25537
25538 ast_set_flag(&mask[1], SIP_PAGE2_T38SUPPORT);
25539
25540 while ((word = strsep(&next, ","))) {
25541 if (ast_true(word) || !strcasecmp(word, "fec")) {
25542 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
25543 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL_FEC);
25544 } else if (!strcasecmp(word, "redundancy")) {
25545 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
25546 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL_REDUNDANCY);
25547 } else if (!strcasecmp(word, "none")) {
25548 ast_clear_flag(&flags[1], SIP_PAGE2_T38SUPPORT);
25549 ast_set_flag(&flags[1], SIP_PAGE2_T38SUPPORT_UDPTL);
25550 } else if (!strncasecmp(word, "maxdatagram=", 12)) {
25551 if (sscanf(&word[12], "%30u", maxdatagram) != 1) {
25552 ast_log(LOG_WARNING, "Invalid maxdatagram '%s' at line %d of %s\n", v->value, v->lineno, config);
25553 *maxdatagram = global_t38_maxdatagram;
25554 }
25555 }
25556 }
25557 } else if (!strcasecmp(v->name, "t38pt_usertpsource")) {
25558 ast_set_flag(&mask[1], SIP_PAGE2_UDPTL_DESTINATION);
25559 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_UDPTL_DESTINATION);
25560 } else {
25561 res = 0;
25562 }
25563
25564 return res;
25565 }
25566
25567
25568
25569
25570
25571
25572
25573
25574 static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask, struct ast_variable *v)
25575 {
25576 int res = 1;
25577
25578 if (!strcasecmp(v->name, "trustrpid")) {
25579 ast_set_flag(&mask[0], SIP_TRUSTRPID);
25580 ast_set2_flag(&flags[0], ast_true(v->value), SIP_TRUSTRPID);
25581 } else if (!strcasecmp(v->name, "sendrpid")) {
25582 ast_set_flag(&mask[0], SIP_SENDRPID);
25583 if (!strcasecmp(v->value, "pai")) {
25584 ast_set_flag(&flags[0], SIP_SENDRPID_PAI);
25585 } else if (!strcasecmp(v->value, "rpid")) {
25586 ast_set_flag(&flags[0], SIP_SENDRPID_RPID);
25587 } else if (ast_true(v->value)) {
25588 ast_set_flag(&flags[0], SIP_SENDRPID_RPID);
25589 }
25590 } else if (!strcasecmp(v->name, "rpid_update")) {
25591 ast_set_flag(&mask[1], SIP_PAGE2_RPID_UPDATE);
25592 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RPID_UPDATE);
25593 } else if (!strcasecmp(v->name, "rpid_immediate")) {
25594 ast_set_flag(&mask[1], SIP_PAGE2_RPID_IMMEDIATE);
25595 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RPID_IMMEDIATE);
25596 } else if (!strcasecmp(v->name, "g726nonstandard")) {
25597 ast_set_flag(&mask[0], SIP_G726_NONSTANDARD);
25598 ast_set2_flag(&flags[0], ast_true(v->value), SIP_G726_NONSTANDARD);
25599 } else if (!strcasecmp(v->name, "useclientcode")) {
25600 ast_set_flag(&mask[0], SIP_USECLIENTCODE);
25601 ast_set2_flag(&flags[0], ast_true(v->value), SIP_USECLIENTCODE);
25602 } else if (!strcasecmp(v->name, "dtmfmode")) {
25603 ast_set_flag(&mask[0], SIP_DTMF);
25604 ast_clear_flag(&flags[0], SIP_DTMF);
25605 if (!strcasecmp(v->value, "inband"))
25606 ast_set_flag(&flags[0], SIP_DTMF_INBAND);
25607 else if (!strcasecmp(v->value, "rfc2833"))
25608 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
25609 else if (!strcasecmp(v->value, "info"))
25610 ast_set_flag(&flags[0], SIP_DTMF_INFO);
25611 else if (!strcasecmp(v->value, "shortinfo"))
25612 ast_set_flag(&flags[0], SIP_DTMF_SHORTINFO);
25613 else if (!strcasecmp(v->value, "auto"))
25614 ast_set_flag(&flags[0], SIP_DTMF_AUTO);
25615 else {
25616 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' on line %d, using rfc2833\n", v->value, v->lineno);
25617 ast_set_flag(&flags[0], SIP_DTMF_RFC2833);
25618 }
25619 } else if (!strcasecmp(v->name, "nat")) {
25620 ast_set_flag(&mask[0], SIP_NAT_FORCE_RPORT);
25621 if (!strcasecmp(v->value, "no")) {
25622 ast_clear_flag(&flags[0], SIP_NAT_FORCE_RPORT);
25623 } else if (!strcasecmp(v->value, "force_rport")) {
25624 ast_set_flag(&flags[0], SIP_NAT_FORCE_RPORT);
25625 } else if (!strcasecmp(v->value, "yes")) {
25626 ast_set_flag(&flags[0], SIP_NAT_FORCE_RPORT);
25627 ast_set_flag(&mask[1], SIP_PAGE2_SYMMETRICRTP);
25628 ast_set_flag(&flags[1], SIP_PAGE2_SYMMETRICRTP);
25629 } else if (!strcasecmp(v->value, "comedia")) {
25630 ast_clear_flag(&flags[0], SIP_NAT_FORCE_RPORT);
25631 ast_set_flag(&mask[1], SIP_PAGE2_SYMMETRICRTP);
25632 ast_set_flag(&flags[1], SIP_PAGE2_SYMMETRICRTP);
25633 }
25634 } else if (!strcasecmp(v->name, "directmedia") || !strcasecmp(v->name, "canreinvite")) {
25635 ast_set_flag(&mask[0], SIP_REINVITE);
25636 ast_clear_flag(&flags[0], SIP_REINVITE);
25637 if (ast_true(v->value)) {
25638 ast_set_flag(&flags[0], SIP_DIRECT_MEDIA | SIP_DIRECT_MEDIA_NAT);
25639 } else if (!ast_false(v->value)) {
25640 char buf[64];
25641 char *word, *next = buf;
25642
25643 ast_copy_string(buf, v->value, sizeof(buf));
25644 while ((word = strsep(&next, ","))) {
25645 if (!strcasecmp(word, "update")) {
25646 ast_set_flag(&flags[0], SIP_REINVITE_UPDATE | SIP_DIRECT_MEDIA);
25647 } else if (!strcasecmp(word, "nonat")) {
25648 ast_set_flag(&flags[0], SIP_DIRECT_MEDIA);
25649 ast_clear_flag(&flags[0], SIP_DIRECT_MEDIA_NAT);
25650 } else {
25651 ast_log(LOG_WARNING, "Unknown directmedia mode '%s' on line %d\n", v->value, v->lineno);
25652 }
25653 }
25654 }
25655 } else if (!strcasecmp(v->name, "insecure")) {
25656 ast_set_flag(&mask[0], SIP_INSECURE);
25657 ast_clear_flag(&flags[0], SIP_INSECURE);
25658 set_insecure_flags(&flags[0], v->value, v->lineno);
25659 } else if (!strcasecmp(v->name, "progressinband")) {
25660 ast_set_flag(&mask[0], SIP_PROG_INBAND);
25661 ast_clear_flag(&flags[0], SIP_PROG_INBAND);
25662 if (ast_true(v->value))
25663 ast_set_flag(&flags[0], SIP_PROG_INBAND_YES);
25664 else if (strcasecmp(v->value, "never"))
25665 ast_set_flag(&flags[0], SIP_PROG_INBAND_NO);
25666 } else if (!strcasecmp(v->name, "promiscredir")) {
25667 ast_set_flag(&mask[0], SIP_PROMISCREDIR);
25668 ast_set2_flag(&flags[0], ast_true(v->value), SIP_PROMISCREDIR);
25669 } else if (!strcasecmp(v->name, "videosupport")) {
25670 if (!strcasecmp(v->value, "always")) {
25671 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
25672 ast_set_flag(&flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
25673 } else {
25674 ast_set_flag(&mask[1], SIP_PAGE2_VIDEOSUPPORT);
25675 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_VIDEOSUPPORT);
25676 }
25677 } else if (!strcasecmp(v->name, "textsupport")) {
25678 ast_set_flag(&mask[1], SIP_PAGE2_TEXTSUPPORT);
25679 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_TEXTSUPPORT);
25680 res = 1;
25681 } else if (!strcasecmp(v->name, "allowoverlap")) {
25682 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWOVERLAP);
25683 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWOVERLAP);
25684 } else if (!strcasecmp(v->name, "allowsubscribe")) {
25685 ast_set_flag(&mask[1], SIP_PAGE2_ALLOWSUBSCRIBE);
25686 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_ALLOWSUBSCRIBE);
25687 } else if (!strcasecmp(v->name, "ignoresdpversion")) {
25688 ast_set_flag(&mask[1], SIP_PAGE2_IGNORESDPVERSION);
25689 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_IGNORESDPVERSION);
25690 } else if (!strcasecmp(v->name, "faxdetect")) {
25691 ast_set_flag(&mask[1], SIP_PAGE2_FAX_DETECT);
25692 if (ast_true(v->value)) {
25693 ast_set_flag(&flags[1], SIP_PAGE2_FAX_DETECT_BOTH);
25694 } else if (ast_false(v->value)) {
25695 ast_clear_flag(&flags[1], SIP_PAGE2_FAX_DETECT_BOTH);
25696 } else {
25697 char *buf = ast_strdupa(v->value);
25698 char *word, *next = buf;
25699
25700 while ((word = strsep(&next, ","))) {
25701 if (!strcasecmp(word, "cng")) {
25702 ast_set_flag(&flags[1], SIP_PAGE2_FAX_DETECT_CNG);
25703 } else if (!strcasecmp(word, "t38")) {
25704 ast_set_flag(&flags[1], SIP_PAGE2_FAX_DETECT_T38);
25705 } else {
25706 ast_log(LOG_WARNING, "Unknown faxdetect mode '%s' on line %d.\n", word, v->lineno);
25707 }
25708 }
25709 }
25710 } else if (!strcasecmp(v->name, "rfc2833compensate")) {
25711 ast_set_flag(&mask[1], SIP_PAGE2_RFC2833_COMPENSATE);
25712 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_RFC2833_COMPENSATE);
25713 } else if (!strcasecmp(v->name, "buggymwi")) {
25714 ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI);
25715 ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI);
25716 } else
25717 res = 0;
25718
25719 return res;
25720 }
25721
25722
25723 static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
25724 {
25725 struct domain *d;
25726
25727 if (ast_strlen_zero(domain)) {
25728 ast_log(LOG_WARNING, "Zero length domain.\n");
25729 return 1;
25730 }
25731
25732 if (!(d = ast_calloc(1, sizeof(*d))))
25733 return 0;
25734
25735 ast_copy_string(d->domain, domain, sizeof(d->domain));
25736
25737 if (!ast_strlen_zero(context))
25738 ast_copy_string(d->context, context, sizeof(d->context));
25739
25740 d->mode = mode;
25741
25742 AST_LIST_LOCK(&domain_list);
25743 AST_LIST_INSERT_TAIL(&domain_list, d, list);
25744 AST_LIST_UNLOCK(&domain_list);
25745
25746 if (sipdebug)
25747 ast_debug(1, "Added local SIP domain '%s'\n", domain);
25748
25749 return 1;
25750 }
25751
25752
25753 static int check_sip_domain(const char *domain, char *context, size_t len)
25754 {
25755 struct domain *d;
25756 int result = 0;
25757
25758 AST_LIST_LOCK(&domain_list);
25759 AST_LIST_TRAVERSE(&domain_list, d, list) {
25760 if (strcasecmp(d->domain, domain))
25761 continue;
25762
25763 if (len && !ast_strlen_zero(d->context))
25764 ast_copy_string(context, d->context, len);
25765
25766 result = 1;
25767 break;
25768 }
25769 AST_LIST_UNLOCK(&domain_list);
25770
25771 return result;
25772 }
25773
25774
25775 static void clear_sip_domains(void)
25776 {
25777 struct domain *d;
25778
25779 AST_LIST_LOCK(&domain_list);
25780 while ((d = AST_LIST_REMOVE_HEAD(&domain_list, list)))
25781 ast_free(d);
25782 AST_LIST_UNLOCK(&domain_list);
25783 }
25784
25785
25786
25787 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, const char *configuration, int lineno)
25788 {
25789 char authcopy[256];
25790 char *username=NULL, *realm=NULL, *secret=NULL, *md5secret=NULL;
25791 struct sip_auth *a, *b, *auth;
25792
25793 if (ast_strlen_zero(configuration))
25794 return authlist;
25795
25796 ast_debug(1, "Auth config :: %s\n", configuration);
25797
25798 ast_copy_string(authcopy, configuration, sizeof(authcopy));
25799 username = authcopy;
25800
25801
25802 realm = strrchr(username, '@');
25803 if (realm)
25804 *realm++ = '\0';
25805 if (ast_strlen_zero(username) || ast_strlen_zero(realm)) {
25806 ast_log(LOG_WARNING, "Format for authentication entry is user[:secret]@realm at line %d\n", lineno);
25807 return authlist;
25808 }
25809
25810
25811 if ((secret = strchr(username, ':'))) {
25812 *secret++ = '\0';
25813 } else if ((md5secret = strchr(username, '#'))) {
25814 *md5secret++ = '\0';
25815 }
25816
25817 if (!(auth = ast_calloc(1, sizeof(*auth))))
25818 return authlist;
25819
25820 ast_copy_string(auth->realm, realm, sizeof(auth->realm));
25821 ast_copy_string(auth->username, username, sizeof(auth->username));
25822 if (secret)
25823 ast_copy_string(auth->secret, secret, sizeof(auth->secret));
25824 if (md5secret)
25825 ast_copy_string(auth->md5secret, md5secret, sizeof(auth->md5secret));
25826
25827
25828 for (b = NULL, a = authlist; a ; b = a, a = a->next)
25829 ;
25830 if (b)
25831 b->next = auth;
25832 else
25833 authlist = auth;
25834
25835 ast_verb(3, "Added authentication for realm %s\n", realm);
25836
25837 return authlist;
25838
25839 }
25840
25841
25842 static int clear_realm_authentication(struct sip_auth *authlist)
25843 {
25844 struct sip_auth *a = authlist;
25845 struct sip_auth *b;
25846
25847 while (a) {
25848 b = a;
25849 a = a->next;
25850 ast_free(b);
25851 }
25852
25853 return 1;
25854 }
25855
25856
25857 static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
25858 {
25859 struct sip_auth *a;
25860
25861 for (a = authlist; a; a = a->next) {
25862 if (!strcasecmp(a->realm, realm))
25863 break;
25864 }
25865
25866 return a;
25867 }
25868
25869
25870
25871
25872 static struct ast_variable *add_var(const char *buf, struct ast_variable *list)
25873 {
25874 struct ast_variable *tmpvar = NULL;
25875 char *varname = ast_strdupa(buf), *varval = NULL;
25876
25877 if ((varval = strchr(varname, '='))) {
25878 *varval++ = '\0';
25879 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
25880 tmpvar->next = list;
25881 list = tmpvar;
25882 }
25883 }
25884 return list;
25885 }
25886
25887
25888 static void set_peer_defaults(struct sip_peer *peer)
25889 {
25890 if (peer->expire == 0) {
25891
25892
25893
25894 peer->expire = -1;
25895 peer->pokeexpire = -1;
25896 set_socket_transport(&peer->socket, SIP_TRANSPORT_UDP);
25897 }
25898 peer->type = SIP_TYPE_PEER;
25899 ast_copy_flags(&peer->flags[0], &global_flags[0], SIP_FLAGS_TO_COPY);
25900 ast_copy_flags(&peer->flags[1], &global_flags[1], SIP_PAGE2_FLAGS_TO_COPY);
25901 ast_copy_flags(&peer->flags[2], &global_flags[2], SIP_PAGE3_FLAGS_TO_COPY);
25902 ast_string_field_set(peer, context, sip_cfg.default_context);
25903 ast_string_field_set(peer, subscribecontext, sip_cfg.default_subscribecontext);
25904 ast_string_field_set(peer, language, default_language);
25905 ast_string_field_set(peer, mohinterpret, default_mohinterpret);
25906 ast_string_field_set(peer, mohsuggest, default_mohsuggest);
25907 ast_string_field_set(peer, engine, default_engine);
25908 ast_sockaddr_setnull(&peer->addr);
25909 ast_sockaddr_setnull(&peer->defaddr);
25910 peer->capability = sip_cfg.capability;
25911 peer->maxcallbitrate = default_maxcallbitrate;
25912 peer->rtptimeout = global_rtptimeout;
25913 peer->rtpholdtimeout = global_rtpholdtimeout;
25914 peer->rtpkeepalive = global_rtpkeepalive;
25915 peer->allowtransfer = sip_cfg.allowtransfer;
25916 peer->autoframing = global_autoframing;
25917 peer->t38_maxdatagram = global_t38_maxdatagram;
25918 peer->qualifyfreq = global_qualifyfreq;
25919 if (global_callcounter)
25920 peer->call_limit=INT_MAX;
25921 ast_string_field_set(peer, vmexten, default_vmexten);
25922 ast_string_field_set(peer, secret, "");
25923 ast_string_field_set(peer, remotesecret, "");
25924 ast_string_field_set(peer, md5secret, "");
25925 ast_string_field_set(peer, cid_num, "");
25926 ast_string_field_set(peer, cid_name, "");
25927 ast_string_field_set(peer, cid_tag, "");
25928 ast_string_field_set(peer, fromdomain, "");
25929 ast_string_field_set(peer, fromuser, "");
25930 ast_string_field_set(peer, regexten, "");
25931 peer->callgroup = 0;
25932 peer->pickupgroup = 0;
25933 peer->maxms = default_qualify;
25934 peer->prefs = default_prefs;
25935 peer->stimer.st_mode_oper = global_st_mode;
25936 peer->stimer.st_ref = global_st_refresher;
25937 peer->stimer.st_min_se = global_min_se;
25938 peer->stimer.st_max_se = global_max_se;
25939 peer->timer_t1 = global_t1;
25940 peer->timer_b = global_timer_b;
25941 clear_peer_mailboxes(peer);
25942 peer->disallowed_methods = sip_cfg.disallowed_methods;
25943 peer->transports = default_transports;
25944 peer->default_outbound_transport = default_primary_transport;
25945 }
25946
25947
25948 static struct sip_peer *temp_peer(const char *name)
25949 {
25950 struct sip_peer *peer;
25951
25952 if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct")))
25953 return NULL;
25954
25955 if (ast_string_field_init(peer, 512)) {
25956 ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
25957 return NULL;
25958 }
25959
25960 if (!(peer->cc_params = ast_cc_config_params_init())) {
25961 ao2_t_ref(peer, -1, "failed to allocate cc_params for peer");
25962 return NULL;
25963 }
25964
25965 ast_atomic_fetchadd_int(&apeerobjs, 1);
25966 set_peer_defaults(peer);
25967
25968 ast_copy_string(peer->name, name, sizeof(peer->name));
25969
25970 peer->selfdestruct = TRUE;
25971 peer->host_dynamic = TRUE;
25972 peer->prefs = default_prefs;
25973 reg_source_db(peer);
25974
25975 return peer;
25976 }
25977
25978
25979 static void add_peer_mailboxes(struct sip_peer *peer, const char *value)
25980 {
25981 char *next, *mbox, *context;
25982
25983 next = ast_strdupa(value);
25984
25985 while ((mbox = context = strsep(&next, ","))) {
25986 struct sip_mailbox *mailbox;
25987 int duplicate = 0;
25988
25989 strsep(&context, "@");
25990
25991 if (ast_strlen_zero(mbox)) {
25992 continue;
25993 }
25994
25995
25996 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
25997 if (!strcmp(mailbox->mailbox, mbox) && !strcmp(S_OR(mailbox->context, ""), S_OR(context, ""))) {
25998 duplicate = 1;
25999 mailbox->delme = 0;
26000 break;
26001 }
26002 }
26003 if (duplicate) {
26004 continue;
26005 }
26006
26007 if (!(mailbox = ast_calloc(1, sizeof(*mailbox) + strlen(mbox) + strlen(S_OR(context, ""))))) {
26008 continue;
26009 }
26010
26011 if (!ast_strlen_zero(context)) {
26012 mailbox->context = mailbox->mailbox + strlen(mbox) + 1;
26013 strcpy(mailbox->context, context);
26014 }
26015 strcpy(mailbox->mailbox, mbox);
26016
26017 AST_LIST_INSERT_TAIL(&peer->mailboxes, mailbox, entry);
26018 }
26019 }
26020
26021
26022 static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only)
26023 {
26024 struct sip_peer *peer = NULL;
26025 struct ast_ha *oldha = NULL;
26026 struct ast_ha *olddirectmediaha = NULL;
26027 int found = 0;
26028 int firstpass = 1;
26029 uint16_t port = 0;
26030 int format = 0;
26031 int timerb_set = 0, timert1_set = 0;
26032 time_t regseconds = 0;
26033 struct ast_flags peerflags[3] = {{(0)}};
26034 struct ast_flags mask[3] = {{(0)}};
26035 char callback[256] = "";
26036 struct sip_peer tmp_peer;
26037 const char *srvlookup = NULL;
26038 static int deprecation_warning = 1;
26039 int alt_fullcontact = alt ? 1 : 0, headercount = 0;
26040 struct ast_str *fullcontact = ast_str_alloca(512);
26041
26042 if (!realtime || ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
26043
26044
26045
26046
26047
26048 ast_copy_string(tmp_peer.name, name, sizeof(tmp_peer.name));
26049 peer = ao2_t_find(peers, &tmp_peer, OBJ_POINTER | OBJ_UNLINK, "find and unlink peer from peers table");
26050 }
26051
26052 if (peer) {
26053
26054 found++;
26055 if (!(peer->the_mark))
26056 firstpass = 0;
26057 } else {
26058 if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct")))
26059 return NULL;
26060
26061 if (ast_string_field_init(peer, 512)) {
26062 ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
26063 return NULL;
26064 }
26065
26066 if (!(peer->cc_params = ast_cc_config_params_init())) {
26067 ao2_t_ref(peer, -1, "failed to allocate cc_params for peer");
26068 return NULL;
26069 }
26070
26071 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
26072 ast_atomic_fetchadd_int(&rpeerobjs, 1);
26073 ast_debug(3, "-REALTIME- peer built. Name: %s. Peer objects: %d\n", name, rpeerobjs);
26074 } else
26075 ast_atomic_fetchadd_int(&speerobjs, 1);
26076 }
26077
26078
26079 if (firstpass) {
26080 peer->lastmsgssent = -1;
26081 oldha = peer->ha;
26082 peer->ha = NULL;
26083 olddirectmediaha = peer->directmediaha;
26084 peer->directmediaha = NULL;
26085 set_peer_defaults(peer);
26086 peer->type = 0;
26087 }
26088 if (!found && name)
26089 ast_copy_string(peer->name, name, sizeof(peer->name));
26090
26091
26092 if (peer->chanvars) {
26093 ast_variables_destroy(peer->chanvars);
26094 peer->chanvars = NULL;
26095
26096 }
26097
26098 if (found)
26099 peer->portinuri = 0;
26100
26101
26102 clear_realm_authentication(peer->auth);
26103 peer->auth = NULL;
26104
26105 peer->default_outbound_transport = 0;
26106 peer->transports = 0;
26107
26108 if (!devstate_only) {
26109 struct sip_mailbox *mailbox;
26110 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
26111 mailbox->delme = 1;
26112 }
26113 }
26114
26115 for (; v || ((v = alt) && !(alt=NULL)); v = v->next) {
26116 if (!devstate_only) {
26117 if (handle_common_options(&peerflags[0], &mask[0], v)) {
26118 continue;
26119 }
26120 if (handle_t38_options(&peerflags[0], &mask[0], v, &peer->t38_maxdatagram)) {
26121 continue;
26122 }
26123 if (!strcasecmp(v->name, "transport") && !ast_strlen_zero(v->value)) {
26124 char *val = ast_strdupa(v->value);
26125 char *trans;
26126
26127 while ((trans = strsep(&val, ","))) {
26128 trans = ast_skip_blanks(trans);
26129
26130 if (!strncasecmp(trans, "udp", 3)) {
26131 peer->transports |= SIP_TRANSPORT_UDP;
26132 } else if (!strncasecmp(trans, "tcp", 3)) {
26133 peer->transports |= SIP_TRANSPORT_TCP;
26134 } else if (!strncasecmp(trans, "tls", 3)) {
26135 peer->transports |= SIP_TRANSPORT_TLS;
26136 } else {
26137 ast_log(LOG_NOTICE, "'%s' is not a valid transport type. if no other is specified, udp will be used.\n", trans);
26138 }
26139
26140 if (!peer->default_outbound_transport) {
26141 peer->default_outbound_transport = peer->transports;
26142 }
26143 }
26144 } else if (realtime && !strcasecmp(v->name, "regseconds")) {
26145 ast_get_time_t(v->value, ®seconds, 0, NULL);
26146 } else if (realtime && !strcasecmp(v->name, "name")) {
26147 ast_copy_string(peer->name, v->value, sizeof(peer->name));
26148 } else if (realtime && !strcasecmp(v->name, "useragent")) {
26149 ast_string_field_set(peer, useragent, v->value);
26150 } else if (!strcasecmp(v->name, "type")) {
26151 if (!strcasecmp(v->value, "peer")) {
26152 peer->type |= SIP_TYPE_PEER;
26153 } else if (!strcasecmp(v->value, "user")) {
26154 peer->type |= SIP_TYPE_USER;
26155 } else if (!strcasecmp(v->value, "friend")) {
26156 peer->type = SIP_TYPE_USER | SIP_TYPE_PEER;
26157 }
26158 } else if (!strcasecmp(v->name, "remotesecret")) {
26159 ast_string_field_set(peer, remotesecret, v->value);
26160 } else if (!strcasecmp(v->name, "secret")) {
26161 ast_string_field_set(peer, secret, v->value);
26162 } else if (!strcasecmp(v->name, "md5secret")) {
26163 ast_string_field_set(peer, md5secret, v->value);
26164 } else if (!strcasecmp(v->name, "auth")) {
26165 peer->auth = add_realm_authentication(peer->auth, v->value, v->lineno);
26166 } else if (!strcasecmp(v->name, "callerid")) {
26167 char cid_name[80] = { '\0' }, cid_num[80] = { '\0' };
26168
26169 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
26170 ast_string_field_set(peer, cid_name, cid_name);
26171 ast_string_field_set(peer, cid_num, cid_num);
26172 } else if (!strcasecmp(v->name, "mwi_from")) {
26173 ast_string_field_set(peer, mwi_from, v->value);
26174 } else if (!strcasecmp(v->name, "fullname")) {
26175 ast_string_field_set(peer, cid_name, v->value);
26176 } else if (!strcasecmp(v->name, "trunkname")) {
26177
26178 ast_string_field_set(peer, cid_name, "");
26179 } else if (!strcasecmp(v->name, "cid_number")) {
26180 ast_string_field_set(peer, cid_num, v->value);
26181 } else if (!strcasecmp(v->name, "cid_tag")) {
26182 ast_string_field_set(peer, cid_tag, v->value);
26183 } else if (!strcasecmp(v->name, "context")) {
26184 ast_string_field_set(peer, context, v->value);
26185 ast_set_flag(&peer->flags[1], SIP_PAGE2_HAVEPEERCONTEXT);
26186 } else if (!strcasecmp(v->name, "subscribecontext")) {
26187 ast_string_field_set(peer, subscribecontext, v->value);
26188 } else if (!strcasecmp(v->name, "fromdomain")) {
26189 char *fromdomainport;
26190 ast_string_field_set(peer, fromdomain, v->value);
26191 if ((fromdomainport = strchr(peer->fromdomain, ':'))) {
26192 *fromdomainport++ = '\0';
26193 if (!(peer->fromdomainport = port_str2int(fromdomainport, 0))) {
26194 ast_log(LOG_NOTICE, "'%s' is not a valid port number for fromdomain.\n",fromdomainport);
26195 }
26196 } else {
26197 peer->fromdomainport = STANDARD_SIP_PORT;
26198 }
26199 } else if (!strcasecmp(v->name, "usereqphone")) {
26200 ast_set2_flag(&peer->flags[0], ast_true(v->value), SIP_USEREQPHONE);
26201 } else if (!strcasecmp(v->name, "fromuser")) {
26202 ast_string_field_set(peer, fromuser, v->value);
26203 } else if (!strcasecmp(v->name, "outboundproxy")) {
26204 char *tok, *proxyname;
26205
26206 if (ast_strlen_zero(v->value)) {
26207 ast_log(LOG_WARNING, "no value given for outbound proxy on line %d of sip.conf.", v->lineno);
26208 continue;
26209 }
26210
26211 peer->outboundproxy =
26212 ao2_alloc(sizeof(*peer->outboundproxy), NULL);
26213
26214 tok = ast_skip_blanks(strtok(ast_strdupa(v->value), ","));
26215
26216 sip_parse_host(tok, v->lineno, &proxyname,
26217 &peer->outboundproxy->port,
26218 &peer->outboundproxy->transport);
26219
26220 tok = ast_skip_blanks(strtok(ast_strdupa(v->value), ","));
26221
26222 if ((tok = strtok(NULL, ","))) {
26223 peer->outboundproxy->force = !strncasecmp(ast_skip_blanks(tok), "force", 5);
26224 } else {
26225 peer->outboundproxy->force = FALSE;
26226 }
26227
26228 if (ast_strlen_zero(proxyname)) {
26229 ast_log(LOG_WARNING, "you must specify a name for the outboundproxy on line %d of sip.conf.", v->lineno);
26230 sip_cfg.outboundproxy.name[0] = '\0';
26231 continue;
26232 }
26233
26234 ast_copy_string(peer->outboundproxy->name, proxyname, sizeof(peer->outboundproxy->name));
26235
26236 proxy_update(peer->outboundproxy);
26237 } else if (!strcasecmp(v->name, "host")) {
26238 if (!strcasecmp(v->value, "dynamic")) {
26239
26240 if (!found || !peer->host_dynamic) {
26241
26242
26243 ast_sockaddr_setnull(&peer->addr);
26244 }
26245 peer->host_dynamic = TRUE;
26246 } else {
26247
26248 AST_SCHED_DEL_UNREF(sched, peer->expire,
26249 unref_peer(peer, "removing register expire ref"));
26250 peer->host_dynamic = FALSE;
26251 srvlookup = v->value;
26252 }
26253 } else if (!strcasecmp(v->name, "defaultip")) {
26254 if (!ast_strlen_zero(v->value) && ast_get_ip(&peer->defaddr, v->value)) {
26255 unref_peer(peer, "unref_peer: from build_peer defaultip");
26256 return NULL;
26257 }
26258 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
26259 int ha_error = 0;
26260 if (!ast_strlen_zero(v->value)) {
26261 peer->ha = ast_append_ha(v->name, v->value, peer->ha, &ha_error);
26262 }
26263 if (ha_error) {
26264 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
26265 }
26266 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
26267 int ha_error = 0;
26268 if (!ast_strlen_zero(v->value)) {
26269 peer->contactha = ast_append_ha(v->name + 7, v->value, peer->contactha, &ha_error);
26270 }
26271 if (ha_error) {
26272 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
26273 }
26274 } else if (!strcasecmp(v->name, "directmediapermit") || !strcasecmp(v->name, "directmediadeny")) {
26275 int ha_error = 0;
26276 peer->directmediaha = ast_append_ha(v->name + 11, v->value, peer->directmediaha, &ha_error);
26277 if (ha_error) {
26278 ast_log(LOG_ERROR, "Bad directmedia ACL entry in configuration line %d : %s\n", v->lineno, v->value);
26279 }
26280 } else if (!strcasecmp(v->name, "port")) {
26281 peer->portinuri = 1;
26282 if (!(port = port_str2int(v->value, 0))) {
26283 if (realtime) {
26284
26285 peer->portinuri = 0;
26286 } else {
26287 ast_log(LOG_WARNING, "Invalid peer port configuration at line %d : %s\n", v->lineno, v->value);
26288 }
26289 }
26290 } else if (!strcasecmp(v->name, "callingpres")) {
26291 peer->callingpres = ast_parse_caller_presentation(v->value);
26292 if (peer->callingpres == -1) {
26293 peer->callingpres = atoi(v->value);
26294 }
26295 } else if (!strcasecmp(v->name, "username") || !strcmp(v->name, "defaultuser")) {
26296 ast_string_field_set(peer, username, v->value);
26297 if (!strcasecmp(v->name, "username")) {
26298 if (deprecation_warning) {
26299 ast_log(LOG_NOTICE, "The 'username' field for sip peers has been deprecated in favor of the term 'defaultuser'\n");
26300 deprecation_warning = 0;
26301 }
26302 peer->deprecated_username = 1;
26303 }
26304 } else if (!strcasecmp(v->name, "language")) {
26305 ast_string_field_set(peer, language, v->value);
26306 } else if (!strcasecmp(v->name, "regexten")) {
26307 ast_string_field_set(peer, regexten, v->value);
26308 } else if (!strcasecmp(v->name, "callbackextension")) {
26309 ast_copy_string(callback, v->value, sizeof(callback));
26310 } else if (!strcasecmp(v->name, "amaflags")) {
26311 format = ast_cdr_amaflags2int(v->value);
26312 if (format < 0) {
26313 ast_log(LOG_WARNING, "Invalid AMA Flags for peer: %s at line %d\n", v->value, v->lineno);
26314 } else {
26315 peer->amaflags = format;
26316 }
26317 } else if (!strcasecmp(v->name, "maxforwards")) {
26318 if ((sscanf(v->value, "%30d", &peer->maxforwards) != 1) || (peer->maxforwards < 1)) {
26319 ast_log(LOG_WARNING, "'%s' is not a valid maxforwards value at line %d. Using default.\n", v->value, v->lineno);
26320 peer->maxforwards = sip_cfg.default_max_forwards;
26321 }
26322 } else if (!strcasecmp(v->name, "accountcode")) {
26323 ast_string_field_set(peer, accountcode, v->value);
26324 } else if (!strcasecmp(v->name, "mohinterpret")) {
26325 ast_string_field_set(peer, mohinterpret, v->value);
26326 } else if (!strcasecmp(v->name, "mohsuggest")) {
26327 ast_string_field_set(peer, mohsuggest, v->value);
26328 } else if (!strcasecmp(v->name, "parkinglot")) {
26329 ast_string_field_set(peer, parkinglot, v->value);
26330 } else if (!strcasecmp(v->name, "rtp_engine")) {
26331 ast_string_field_set(peer, engine, v->value);
26332 } else if (!strcasecmp(v->name, "mailbox")) {
26333 add_peer_mailboxes(peer, v->value);
26334 } else if (!strcasecmp(v->name, "hasvoicemail")) {
26335
26336
26337 if (ast_true(v->value) && AST_LIST_EMPTY(&peer->mailboxes)) {
26338 add_peer_mailboxes(peer, name);
26339 }
26340 } else if (!strcasecmp(v->name, "subscribemwi")) {
26341 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_SUBSCRIBEMWIONLY);
26342 } else if (!strcasecmp(v->name, "vmexten")) {
26343 ast_string_field_set(peer, vmexten, v->value);
26344 } else if (!strcasecmp(v->name, "callgroup")) {
26345 peer->callgroup = ast_get_group(v->value);
26346 } else if (!strcasecmp(v->name, "allowtransfer")) {
26347 peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
26348 } else if (!strcasecmp(v->name, "pickupgroup")) {
26349 peer->pickupgroup = ast_get_group(v->value);
26350 } else if (!strcasecmp(v->name, "allow")) {
26351 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, TRUE);
26352 if (error) {
26353 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
26354 }
26355 } else if (!strcasecmp(v->name, "disallow")) {
26356 int error = ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, FALSE);
26357 if (error) {
26358 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
26359 }
26360 } else if (!strcasecmp(v->name, "preferred_codec_only")) {
26361 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
26362 } else if (!strcasecmp(v->name, "registertrying")) {
26363 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_REGISTERTRYING);
26364 } else if (!strcasecmp(v->name, "autoframing")) {
26365 peer->autoframing = ast_true(v->value);
26366 } else if (!strcasecmp(v->name, "rtptimeout")) {
26367 if ((sscanf(v->value, "%30d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
26368 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
26369 peer->rtptimeout = global_rtptimeout;
26370 }
26371 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
26372 if ((sscanf(v->value, "%30d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
26373 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
26374 peer->rtpholdtimeout = global_rtpholdtimeout;
26375 }
26376 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
26377 if ((sscanf(v->value, "%30d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
26378 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
26379 peer->rtpkeepalive = global_rtpkeepalive;
26380 }
26381 } else if (!strcasecmp(v->name, "timert1")) {
26382 if ((sscanf(v->value, "%30d", &peer->timer_t1) != 1) || (peer->timer_t1 < 200) || (peer->timer_t1 < global_t1min)) {
26383 ast_log(LOG_WARNING, "'%s' is not a valid T1 time at line %d. Using default.\n", v->value, v->lineno);
26384 peer->timer_t1 = global_t1min;
26385 }
26386 timert1_set = 1;
26387 } else if (!strcasecmp(v->name, "timerb")) {
26388 if ((sscanf(v->value, "%30d", &peer->timer_b) != 1) || (peer->timer_b < 200)) {
26389 ast_log(LOG_WARNING, "'%s' is not a valid Timer B time at line %d. Using default.\n", v->value, v->lineno);
26390 peer->timer_b = global_timer_b;
26391 }
26392 timerb_set = 1;
26393 } else if (!strcasecmp(v->name, "setvar")) {
26394 peer->chanvars = add_var(v->value, peer->chanvars);
26395 } else if (!strcasecmp(v->name, "header")) {
26396 char tmp[4096];
26397 snprintf(tmp, sizeof(tmp), "__SIPADDHEADERpre%2d=%s", ++headercount, v->value);
26398 peer->chanvars = add_var(tmp, peer->chanvars);
26399 } else if (!strcasecmp(v->name, "qualifyfreq")) {
26400 int i;
26401 if (sscanf(v->value, "%30d", &i) == 1) {
26402 peer->qualifyfreq = i * 1000;
26403 } else {
26404 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
26405 peer->qualifyfreq = global_qualifyfreq;
26406 }
26407 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
26408 peer->maxcallbitrate = atoi(v->value);
26409 if (peer->maxcallbitrate < 0) {
26410 peer->maxcallbitrate = default_maxcallbitrate;
26411 }
26412 } else if (!strcasecmp(v->name, "session-timers")) {
26413 int i = (int) str2stmode(v->value);
26414 if (i < 0) {
26415 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
26416 peer->stimer.st_mode_oper = global_st_mode;
26417 } else {
26418 peer->stimer.st_mode_oper = i;
26419 }
26420 } else if (!strcasecmp(v->name, "session-expires")) {
26421 if (sscanf(v->value, "%30d", &peer->stimer.st_max_se) != 1) {
26422 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
26423 peer->stimer.st_max_se = global_max_se;
26424 }
26425 } else if (!strcasecmp(v->name, "session-minse")) {
26426 if (sscanf(v->value, "%30d", &peer->stimer.st_min_se) != 1) {
26427 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
26428 peer->stimer.st_min_se = global_min_se;
26429 }
26430 if (peer->stimer.st_min_se < 90) {
26431 ast_log(LOG_WARNING, "session-minse '%s' at line %d of %s is not allowed to be < 90 secs\n", v->value, v->lineno, config);
26432 peer->stimer.st_min_se = global_min_se;
26433 }
26434 } else if (!strcasecmp(v->name, "session-refresher")) {
26435 int i = (int) str2strefresher(v->value);
26436 if (i < 0) {
26437 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
26438 peer->stimer.st_ref = global_st_refresher;
26439 } else {
26440 peer->stimer.st_ref = i;
26441 }
26442 } else if (!strcasecmp(v->name, "disallowed_methods")) {
26443 char *disallow = ast_strdupa(v->value);
26444 mark_parsed_methods(&peer->disallowed_methods, disallow);
26445 } else if (!strcasecmp(v->name, "unsolicited_mailbox")) {
26446 ast_string_field_set(peer, unsolicited_mailbox, v->value);
26447 } else if (!strcasecmp(v->name, "use_q850_reason")) {
26448 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_Q850_REASON);
26449 } else if (!strcasecmp(v->name, "encryption")) {
26450 ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_USE_SRTP);
26451 } else if (!strcasecmp(v->name, "snom_aoc_enabled")) {
26452 ast_set2_flag(&peer->flags[2], ast_true(v->value), SIP_PAGE3_SNOM_AOC);
26453 }
26454 }
26455
26456
26457 if (realtime && !strcasecmp(v->name, "lastms")) {
26458 sscanf(v->value, "%30d", &peer->lastms);
26459 } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) {
26460 ast_sockaddr_parse(&peer->addr, v->value, PARSE_PORT_FORBID);
26461 } else if (realtime && !strcasecmp(v->name, "fullcontact")) {
26462 if (alt_fullcontact && !alt) {
26463
26464
26465
26466
26467
26468 alt_fullcontact = 0;
26469 ast_str_reset(fullcontact);
26470 }
26471
26472 if (fullcontact->used > 0) {
26473 ast_str_append(&fullcontact, 0, ";%s", v->value);
26474 } else {
26475 ast_str_set(&fullcontact, 0, "%s", v->value);
26476 }
26477 } else if (!strcasecmp(v->name, "qualify")) {
26478 if (!strcasecmp(v->value, "no")) {
26479 peer->maxms = 0;
26480 } else if (!strcasecmp(v->value, "yes")) {
26481 peer->maxms = default_qualify ? default_qualify : DEFAULT_MAXMS;
26482 } else if (sscanf(v->value, "%30d", &peer->maxms) != 1) {
26483 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);
26484 peer->maxms = 0;
26485 }
26486 if (realtime && !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) && peer->maxms > 0) {
26487
26488
26489
26490
26491 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);
26492 peer->maxms = 0;
26493 }
26494 } else if (!strcasecmp(v->name, "callcounter")) {
26495 peer->call_limit = ast_true(v->value) ? INT_MAX : 0;
26496 } else if (!strcasecmp(v->name, "call-limit")) {
26497 peer->call_limit = atoi(v->value);
26498 if (peer->call_limit < 0) {
26499 peer->call_limit = 0;
26500 }
26501 } else if (!strcasecmp(v->name, "busylevel")) {
26502 peer->busy_level = atoi(v->value);
26503 if (peer->busy_level < 0) {
26504 peer->busy_level = 0;
26505 }
26506 } else if (ast_cc_is_config_param(v->name)) {
26507 ast_cc_set_param(peer->cc_params, v->name, v->value);
26508 }
26509 }
26510
26511 if (!devstate_only) {
26512 struct sip_mailbox *mailbox;
26513 AST_LIST_TRAVERSE_SAFE_BEGIN(&peer->mailboxes, mailbox, entry) {
26514 if (mailbox->delme) {
26515 AST_LIST_REMOVE_CURRENT(entry);
26516 destroy_mailbox(mailbox);
26517 }
26518 }
26519 AST_LIST_TRAVERSE_SAFE_END;
26520 }
26521
26522 if (!can_parse_xml && (ast_get_cc_agent_policy(peer->cc_params) == AST_CC_AGENT_NATIVE)) {
26523 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);
26524 ast_set_cc_agent_policy(peer->cc_params, AST_CC_AGENT_NEVER);
26525 }
26526
26527
26528
26529 if (peer->timer_b < peer->timer_t1 * 64) {
26530 if (timerb_set && timert1_set) {
26531 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);
26532 } else if (timerb_set) {
26533 if ((peer->timer_t1 = peer->timer_b / 64) < global_t1min) {
26534 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);
26535 peer->timer_t1 = global_t1min;
26536 peer->timer_b = peer->timer_t1 * 64;
26537 }
26538 peer->timer_t1 = peer->timer_b / 64;
26539 } else {
26540 peer->timer_b = peer->timer_t1 * 64;
26541 }
26542 }
26543
26544 if (!peer->default_outbound_transport) {
26545
26546 peer->transports = default_transports;
26547
26548 peer->default_outbound_transport = default_primary_transport;
26549 }
26550
26551
26552
26553
26554
26555 if (((peer->socket.type != peer->default_outbound_transport) && (peer->expire == -1)) ||
26556 !(peer->socket.type & peer->transports) || !(peer->socket.type)) {
26557
26558 set_socket_transport(&peer->socket, peer->default_outbound_transport);
26559 }
26560
26561 if (ast_str_strlen(fullcontact)) {
26562 ast_string_field_set(peer, fullcontact, ast_str_buffer(fullcontact));
26563 peer->rt_fromcontact = TRUE;
26564
26565
26566
26567
26568
26569
26570
26571
26572 if (!ast_test_flag(&peer->flags[0], SIP_NAT_RPORT_PRESENT) || ast_sockaddr_isnull(&peer->addr)) {
26573 __set_address_from_contact(fullcontact->str, &peer->addr, 0);
26574 }
26575 }
26576
26577 if (srvlookup && peer->dnsmgr == NULL) {
26578 char transport[MAXHOSTNAMELEN];
26579 char _srvlookup[MAXHOSTNAMELEN];
26580 char *params;
26581
26582 ast_copy_string(_srvlookup, srvlookup, sizeof(_srvlookup));
26583 if ((params = strchr(_srvlookup, ';'))) {
26584 *params++ = '\0';
26585 }
26586
26587 snprintf(transport, sizeof(transport), "_%s._%s", get_srv_service(peer->socket.type), get_srv_protocol(peer->socket.type));
26588
26589 peer->addr.ss.ss_family = get_address_family_filter(&bindaddr);
26590 if (ast_dnsmgr_lookup(_srvlookup, &peer->addr, &peer->dnsmgr, sip_cfg.srvlookup && !peer->portinuri ? transport : NULL)) {
26591 ast_log(LOG_ERROR, "srvlookup failed for host: %s, on peer %s, removing peer\n", _srvlookup, peer->name);
26592 unref_peer(peer, "getting rid of a peer pointer");
26593 return NULL;
26594 }
26595
26596 ast_string_field_set(peer, tohost, srvlookup);
26597
26598 if (global_dynamic_exclude_static) {
26599 int err = 0;
26600 sip_cfg.contact_ha = ast_append_ha("deny", ast_sockaddr_stringify_addr(&peer->addr),
26601 sip_cfg.contact_ha, &err);
26602 if (err) {
26603 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
26604 }
26605 }
26606 } else if (peer->dnsmgr && !peer->host_dynamic) {
26607
26608 ast_dnsmgr_refresh(peer->dnsmgr);
26609 }
26610
26611 if (port && !realtime && peer->host_dynamic) {
26612 ast_sockaddr_set_port(&peer->defaddr, port);
26613 } else if (port) {
26614 ast_sockaddr_set_port(&peer->addr, port);
26615 }
26616
26617 if (ast_sockaddr_port(&peer->addr) == 0) {
26618 ast_sockaddr_set_port(&peer->addr,
26619 (peer->socket.type & SIP_TRANSPORT_TLS) ?
26620 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
26621 }
26622 if (ast_sockaddr_port(&peer->defaddr) == 0) {
26623 ast_sockaddr_set_port(&peer->defaddr,
26624 (peer->socket.type & SIP_TRANSPORT_TLS) ?
26625 STANDARD_TLS_PORT : STANDARD_SIP_PORT);
26626 }
26627 if (!peer->socket.port) {
26628 peer->socket.port = htons(((peer->socket.type & SIP_TRANSPORT_TLS) ? STANDARD_TLS_PORT : STANDARD_SIP_PORT));
26629 }
26630
26631 if (!sip_cfg.ignore_regexpire && peer->host_dynamic && realtime) {
26632 time_t nowtime = time(NULL);
26633
26634 if ((nowtime - regseconds) > 0) {
26635 destroy_association(peer);
26636 memset(&peer->addr, 0, sizeof(peer->addr));
26637 peer->lastms = -1;
26638 ast_debug(1, "Bah, we're expired (%d/%d/%d)!\n", (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
26639 }
26640 }
26641
26642
26643 if (!devstate_only && realtime && peer->lastms > 0) {
26644 ref_peer(peer, "schedule qualify");
26645 sip_poke_peer(peer, 0);
26646 }
26647
26648 ast_copy_flags(&peer->flags[0], &peerflags[0], mask[0].flags);
26649 ast_copy_flags(&peer->flags[1], &peerflags[1], mask[1].flags);
26650 ast_copy_flags(&peer->flags[2], &peerflags[2], mask[2].flags);
26651 if (ast_test_flag(&peer->flags[1], SIP_PAGE2_ALLOWSUBSCRIBE)) {
26652 sip_cfg.allowsubscribe = TRUE;
26653 }
26654 if (peer->host_dynamic && !peer->is_realtime) {
26655 reg_source_db(peer);
26656 }
26657
26658
26659
26660 if (!devstate_only && !ast_test_flag(&peer->flags[1], SIP_PAGE2_SUBSCRIBEMWIONLY) &&
26661 !AST_LIST_EMPTY(&peer->mailboxes)) {
26662 add_peer_mwi_subs(peer);
26663
26664
26665
26666 sip_send_mwi_to_peer(peer, NULL, 1);
26667 }
26668
26669 peer->the_mark = 0;
26670
26671 ast_free_ha(oldha);
26672 ast_free_ha(olddirectmediaha);
26673 if (!ast_strlen_zero(callback)) {
26674 char *reg_string;
26675 if (asprintf(®_string, "%s?%s:%s@%s/%s", peer->name, peer->username, !ast_strlen_zero(peer->remotesecret) ? peer->remotesecret : peer->secret, peer->tohost, callback) < 0) {
26676 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
26677 } else if (reg_string) {
26678 sip_register(reg_string, 0);
26679 ast_free(reg_string);
26680 }
26681 }
26682 return peer;
26683 }
26684
26685 static int peer_markall_func(void *device, void *arg, int flags)
26686 {
26687 struct sip_peer *peer = device;
26688 peer->the_mark = 1;
26689 return 0;
26690 }
26691
26692
26693
26694
26695
26696
26697
26698 static int reload_config(enum channelreloadreason reason)
26699 {
26700 struct ast_config *cfg, *ucfg;
26701 struct ast_variable *v;
26702 struct sip_peer *peer;
26703 char *cat, *stringp, *context, *oldregcontext;
26704 char newcontexts[AST_MAX_CONTEXT], oldcontexts[AST_MAX_CONTEXT];
26705 struct ast_flags dummy[2];
26706 struct ast_flags config_flags = { reason == CHANNEL_MODULE_LOAD ? 0 : ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS) ? 0 : CONFIG_FLAG_FILEUNCHANGED };
26707 int auto_sip_domains = FALSE;
26708 struct ast_sockaddr old_bindaddr = bindaddr;
26709 int registry_count = 0, peer_count = 0, timerb_set = 0, timert1_set = 0;
26710 int subscribe_network_change = 1;
26711 time_t run_start, run_end;
26712 int bindport = 0;
26713
26714 run_start = time(0);
26715 ast_unload_realtime("sipregs");
26716 ast_unload_realtime("sippeers");
26717 cfg = ast_config_load(config, config_flags);
26718
26719
26720 if (!cfg) {
26721 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
26722 return -1;
26723 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
26724 ucfg = ast_config_load("users.conf", config_flags);
26725 if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
26726 return 1;
26727 } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
26728 ast_log(LOG_ERROR, "Contents of users.conf are invalid and cannot be parsed\n");
26729 return 1;
26730 }
26731
26732 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
26733 if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
26734 ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed\n", config);
26735 ast_config_destroy(ucfg);
26736 return 1;
26737 }
26738 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
26739 ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed\n", config);
26740 return 1;
26741 } else {
26742 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
26743 if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
26744 ast_log(LOG_ERROR, "Contents of users.conf are invalid and cannot be parsed\n");
26745 ast_config_destroy(cfg);
26746 return 1;
26747 }
26748 }
26749
26750 ast_free_ha(sip_cfg.contact_ha);
26751 sip_cfg.contact_ha = NULL;
26752
26753 default_tls_cfg.enabled = FALSE;
26754
26755 if (reason != CHANNEL_MODULE_LOAD) {
26756 ast_debug(4, "--------------- SIP reload started\n");
26757
26758 clear_realm_authentication(authl);
26759 clear_sip_domains();
26760 authl = NULL;
26761
26762
26763
26764 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
26765
26766 ASTOBJ_RDLOCK(iterator);
26767 if (iterator->call) {
26768 ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", iterator->username, iterator->hostname);
26769
26770 dialog_unlink_all(iterator->call, TRUE, TRUE);
26771 iterator->call = dialog_unref(iterator->call, "remove iterator->call from registry traversal");
26772 }
26773 if (iterator->expire > -1) {
26774 AST_SCHED_DEL_UNREF(sched, iterator->expire, registry_unref(iterator, "reg ptr unref from reload config"));
26775 }
26776 if (iterator->timeout > -1) {
26777 AST_SCHED_DEL_UNREF(sched, iterator->timeout, registry_unref(iterator, "reg ptr unref from reload config"));
26778 }
26779 ASTOBJ_UNLOCK(iterator);
26780 } while(0));
26781
26782
26783 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
26784 ast_debug(4, "--------------- Done destroying registry list\n");
26785 ao2_t_callback(peers, OBJ_NODATA, peer_markall_func, NULL, "callback to mark all peers");
26786 }
26787
26788
26789 if (reason != CHANNEL_MODULE_LOAD) {
26790 ast_free(default_tls_cfg.certfile);
26791 ast_free(default_tls_cfg.pvtfile);
26792 ast_free(default_tls_cfg.cipher);
26793 ast_free(default_tls_cfg.cafile);
26794 ast_free(default_tls_cfg.capath);
26795 }
26796 default_tls_cfg.certfile = ast_strdup(AST_CERTFILE);
26797 default_tls_cfg.pvtfile = ast_strdup("");
26798 default_tls_cfg.cipher = ast_strdup("");
26799 default_tls_cfg.cafile = ast_strdup("");
26800 default_tls_cfg.capath = ast_strdup("");
26801
26802
26803 ast_copy_string(oldcontexts, sip_cfg.regcontext, sizeof(oldcontexts));
26804 oldregcontext = oldcontexts;
26805
26806
26807
26808 sipdebug &= sip_debug_console;
26809 ast_clear_flag(&global_flags[0], AST_FLAGS_ALL);
26810 ast_clear_flag(&global_flags[1], AST_FLAGS_ALL);
26811
26812
26813 ast_sockaddr_parse(&bindaddr, "0.0.0.0:0", 0);
26814 memset(&internip, 0, sizeof(internip));
26815
26816
26817 ast_free_ha(localaddr);
26818 memset(&localaddr, 0, sizeof(localaddr));
26819 memset(&externaddr, 0, sizeof(externaddr));
26820 memset(&media_address, 0, sizeof(media_address));
26821 memset(&default_prefs, 0 , sizeof(default_prefs));
26822 memset(&sip_cfg.outboundproxy, 0, sizeof(struct sip_proxy));
26823 sip_cfg.outboundproxy.force = FALSE;
26824 default_transports = 0;
26825 default_primary_transport = 0;
26826 ourport_tcp = STANDARD_SIP_PORT;
26827 ourport_tls = STANDARD_TLS_PORT;
26828 externtcpport = STANDARD_SIP_PORT;
26829 externtlsport = STANDARD_TLS_PORT;
26830 sip_cfg.srvlookup = DEFAULT_SRVLOOKUP;
26831 global_tos_sip = DEFAULT_TOS_SIP;
26832 global_tos_audio = DEFAULT_TOS_AUDIO;
26833 global_tos_video = DEFAULT_TOS_VIDEO;
26834 global_tos_text = DEFAULT_TOS_TEXT;
26835 global_cos_sip = DEFAULT_COS_SIP;
26836 global_cos_audio = DEFAULT_COS_AUDIO;
26837 global_cos_video = DEFAULT_COS_VIDEO;
26838 global_cos_text = DEFAULT_COS_TEXT;
26839
26840 externhost[0] = '\0';
26841 externexpire = 0;
26842 externrefresh = 10;
26843
26844
26845 sip_cfg.allow_external_domains = DEFAULT_ALLOW_EXT_DOM;
26846 sip_cfg.regcontext[0] = '\0';
26847 sip_cfg.capability = DEFAULT_CAPABILITY;
26848 sip_cfg.regextenonqualify = DEFAULT_REGEXTENONQUALIFY;
26849 sip_cfg.notifyringing = DEFAULT_NOTIFYRINGING;
26850 sip_cfg.notifycid = DEFAULT_NOTIFYCID;
26851 sip_cfg.notifyhold = FALSE;
26852 sip_cfg.directrtpsetup = FALSE;
26853 sip_cfg.alwaysauthreject = DEFAULT_ALWAYSAUTHREJECT;
26854 sip_cfg.auth_options_requests = DEFAULT_AUTH_OPTIONS;
26855 sip_cfg.allowsubscribe = FALSE;
26856 sip_cfg.disallowed_methods = SIP_UNKNOWN;
26857 sip_cfg.contact_ha = NULL;
26858 snprintf(global_useragent, sizeof(global_useragent), "%s %s", DEFAULT_USERAGENT, ast_get_version());
26859 snprintf(global_sdpsession, sizeof(global_sdpsession), "%s %s", DEFAULT_SDPSESSION, ast_get_version());
26860 snprintf(global_sdpowner, sizeof(global_sdpowner), "%s", DEFAULT_SDPOWNER);
26861 global_prematuremediafilter = TRUE;
26862 ast_copy_string(default_notifymime, DEFAULT_NOTIFYMIME, sizeof(default_notifymime));
26863 ast_copy_string(sip_cfg.realm, S_OR(ast_config_AST_SYSTEM_NAME, DEFAULT_REALM), sizeof(sip_cfg.realm));
26864 sip_cfg.domainsasrealm = DEFAULT_DOMAINSASREALM;
26865 ast_copy_string(default_callerid, DEFAULT_CALLERID, sizeof(default_callerid));
26866 ast_copy_string(default_mwi_from, DEFAULT_MWI_FROM, sizeof(default_mwi_from));
26867 sip_cfg.compactheaders = DEFAULT_COMPACTHEADERS;
26868 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
26869 global_regattempts_max = 0;
26870 sip_cfg.pedanticsipchecking = DEFAULT_PEDANTIC;
26871 sip_cfg.autocreatepeer = DEFAULT_AUTOCREATEPEER;
26872 global_autoframing = 0;
26873 sip_cfg.allowguest = DEFAULT_ALLOWGUEST;
26874 global_callcounter = DEFAULT_CALLCOUNTER;
26875 global_match_auth_username = FALSE;
26876 global_rtptimeout = 0;
26877 global_rtpholdtimeout = 0;
26878 global_rtpkeepalive = DEFAULT_RTPKEEPALIVE;
26879 sip_cfg.allowtransfer = TRANSFER_OPENFORALL;
26880 sip_cfg.rtautoclear = 120;
26881 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWSUBSCRIBE);
26882 ast_set_flag(&global_flags[1], SIP_PAGE2_ALLOWOVERLAP);
26883 sip_cfg.peer_rtupdate = TRUE;
26884 global_dynamic_exclude_static = 0;
26885 sip_cfg.tcp_enabled = FALSE;
26886
26887
26888 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
26889 global_st_refresher = SESSION_TIMER_REFRESHER_UAS;
26890 global_min_se = DEFAULT_MIN_SE;
26891 global_max_se = DEFAULT_MAX_SE;
26892
26893
26894 global_qualify_gap = DEFAULT_QUALIFY_GAP;
26895 global_qualify_peers = DEFAULT_QUALIFY_PEERS;
26896
26897
26898 ast_copy_string(sip_cfg.default_context, DEFAULT_CONTEXT, sizeof(sip_cfg.default_context));
26899 sip_cfg.default_subscribecontext[0] = '\0';
26900 sip_cfg.default_max_forwards = DEFAULT_MAX_FORWARDS;
26901 default_language[0] = '\0';
26902 default_fromdomain[0] = '\0';
26903 default_fromdomainport = 0;
26904 default_qualify = DEFAULT_QUALIFY;
26905 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
26906 ast_copy_string(default_mohinterpret, DEFAULT_MOHINTERPRET, sizeof(default_mohinterpret));
26907 ast_copy_string(default_mohsuggest, DEFAULT_MOHSUGGEST, sizeof(default_mohsuggest));
26908 ast_copy_string(default_vmexten, DEFAULT_VMEXTEN, sizeof(default_vmexten));
26909 ast_set_flag(&global_flags[0], SIP_DTMF_RFC2833);
26910 ast_set_flag(&global_flags[0], SIP_DIRECT_MEDIA);
26911 ast_copy_string(default_engine, DEFAULT_ENGINE, sizeof(default_engine));
26912 ast_copy_string(default_parkinglot, DEFAULT_PARKINGLOT, sizeof(default_parkinglot));
26913
26914
26915 dumphistory = FALSE;
26916 recordhistory = FALSE;
26917 sipdebug &= ~sip_debug_config;
26918
26919
26920 global_relaxdtmf = FALSE;
26921 sip_cfg.callevents = DEFAULT_CALLEVENTS;
26922 global_authfailureevents = FALSE;
26923 global_t1 = DEFAULT_TIMER_T1;
26924 global_timer_b = 64 * DEFAULT_TIMER_T1;
26925 global_t1min = DEFAULT_T1MIN;
26926 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
26927 global_t38_maxdatagram = -1;
26928 global_shrinkcallerid = 1;
26929 authlimit = DEFAULT_AUTHLIMIT;
26930 authtimeout = DEFAULT_AUTHTIMEOUT;
26931
26932 sip_cfg.matchexternaddrlocally = DEFAULT_MATCHEXTERNADDRLOCALLY;
26933
26934
26935 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
26936
26937 ast_clear_flag(&global_flags[1], SIP_PAGE2_FAX_DETECT);
26938 ast_clear_flag(&global_flags[1], SIP_PAGE2_VIDEOSUPPORT | SIP_PAGE2_VIDEOSUPPORT_ALWAYS);
26939 ast_clear_flag(&global_flags[1], SIP_PAGE2_TEXTSUPPORT);
26940 ast_clear_flag(&global_flags[1], SIP_PAGE2_IGNORESDPVERSION);
26941
26942
26943
26944 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
26945 if (handle_common_options(&global_flags[0], &dummy[0], v)) {
26946 continue;
26947 }
26948 if (handle_t38_options(&global_flags[0], &dummy[0], v, &global_t38_maxdatagram)) {
26949 continue;
26950 }
26951
26952 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
26953 continue;
26954
26955
26956 if (!ast_tls_read_conf(&default_tls_cfg, &sip_tls_desc, v->name, v->value)) {
26957 continue;
26958 }
26959
26960 if (!strcasecmp(v->name, "context")) {
26961 ast_copy_string(sip_cfg.default_context, v->value, sizeof(sip_cfg.default_context));
26962 } else if (!strcasecmp(v->name, "subscribecontext")) {
26963 ast_copy_string(sip_cfg.default_subscribecontext, v->value, sizeof(sip_cfg.default_subscribecontext));
26964 } else if (!strcasecmp(v->name, "callcounter")) {
26965 global_callcounter = ast_true(v->value) ? 1 : 0;
26966 } else if (!strcasecmp(v->name, "allowguest")) {
26967 sip_cfg.allowguest = ast_true(v->value) ? 1 : 0;
26968 } else if (!strcasecmp(v->name, "realm")) {
26969 ast_copy_string(sip_cfg.realm, v->value, sizeof(sip_cfg.realm));
26970 } else if (!strcasecmp(v->name, "domainsasrealm")) {
26971 sip_cfg.domainsasrealm = ast_true(v->value);
26972 } else if (!strcasecmp(v->name, "useragent")) {
26973 ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
26974 ast_debug(1, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
26975 } else if (!strcasecmp(v->name, "sdpsession")) {
26976 ast_copy_string(global_sdpsession, v->value, sizeof(global_sdpsession));
26977 } else if (!strcasecmp(v->name, "sdpowner")) {
26978
26979 if (!strstr(v->value, " ")) {
26980 ast_copy_string(global_sdpowner, v->value, sizeof(global_sdpowner));
26981 } else {
26982 ast_log(LOG_WARNING, "'%s' must not contain spaces at line %d. Using default.\n", v->value, v->lineno);
26983 }
26984 } else if (!strcasecmp(v->name, "allowtransfer")) {
26985 sip_cfg.allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
26986 } else if (!strcasecmp(v->name, "rtcachefriends")) {
26987 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);
26988 } else if (!strcasecmp(v->name, "rtsavesysname")) {
26989 sip_cfg.rtsave_sysname = ast_true(v->value);
26990 } else if (!strcasecmp(v->name, "rtupdate")) {
26991 sip_cfg.peer_rtupdate = ast_true(v->value);
26992 } else if (!strcasecmp(v->name, "ignoreregexpire")) {
26993 sip_cfg.ignore_regexpire = ast_true(v->value);
26994 } else if (!strcasecmp(v->name, "timert1")) {
26995
26996
26997
26998 global_t1 = atoi(v->value);
26999 } else if (!strcasecmp(v->name, "timerb")) {
27000 int tmp = atoi(v->value);
27001 if (tmp < 500) {
27002 global_timer_b = global_t1 * 64;
27003 ast_log(LOG_WARNING, "Invalid value for timerb ('%s'). Setting to default ('%d').\n", v->value, global_timer_b);
27004 }
27005 timerb_set = 1;
27006 } else if (!strcasecmp(v->name, "t1min")) {
27007 global_t1min = atoi(v->value);
27008 } else if (!strcasecmp(v->name, "transport") && !ast_strlen_zero(v->value)) {
27009 char *val = ast_strdupa(v->value);
27010 char *trans;
27011
27012 while ((trans = strsep(&val, ","))) {
27013 trans = ast_skip_blanks(trans);
27014
27015 if (!strncasecmp(trans, "udp", 3)) {
27016 default_transports |= SIP_TRANSPORT_UDP;
27017 } else if (!strncasecmp(trans, "tcp", 3)) {
27018 default_transports |= SIP_TRANSPORT_TCP;
27019 } else if (!strncasecmp(trans, "tls", 3)) {
27020 default_transports |= SIP_TRANSPORT_TLS;
27021 } else {
27022 ast_log(LOG_NOTICE, "'%s' is not a valid transport type. if no other is specified, udp will be used.\n", trans);
27023 }
27024 if (default_primary_transport == 0) {
27025 default_primary_transport = default_transports;
27026 }
27027 }
27028 } else if (!strcasecmp(v->name, "tcpenable")) {
27029 if (!ast_false(v->value)) {
27030 ast_debug(2, "Enabling TCP socket for listening\n");
27031 sip_cfg.tcp_enabled = TRUE;
27032 }
27033 } else if (!strcasecmp(v->name, "tcpbindaddr")) {
27034 if (ast_parse_arg(v->value, PARSE_ADDR,
27035 &sip_tcp_desc.local_address)) {
27036 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n",
27037 v->name, v->value, v->lineno, config);
27038 }
27039 ast_debug(2, "Setting TCP socket address to %s\n",
27040 ast_sockaddr_stringify(&sip_tcp_desc.local_address));
27041 } else if (!strcasecmp(v->name, "dynamic_exclude_static") || !strcasecmp(v->name, "dynamic_excludes_static")) {
27042 global_dynamic_exclude_static = ast_true(v->value);
27043 } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny")) {
27044 int ha_error = 0;
27045 sip_cfg.contact_ha = ast_append_ha(v->name + 7, v->value, sip_cfg.contact_ha, &ha_error);
27046 if (ha_error) {
27047 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
27048 }
27049 } else if (!strcasecmp(v->name, "rtautoclear")) {
27050 int i = atoi(v->value);
27051 if (i > 0) {
27052 sip_cfg.rtautoclear = i;
27053 } else {
27054 i = 0;
27055 }
27056 ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
27057 } else if (!strcasecmp(v->name, "usereqphone")) {
27058 ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);
27059 } else if (!strcasecmp(v->name, "prematuremedia")) {
27060 global_prematuremediafilter = ast_true(v->value);
27061 } else if (!strcasecmp(v->name, "relaxdtmf")) {
27062 global_relaxdtmf = ast_true(v->value);
27063 } else if (!strcasecmp(v->name, "vmexten")) {
27064 ast_copy_string(default_vmexten, v->value, sizeof(default_vmexten));
27065 } else if (!strcasecmp(v->name, "rtptimeout")) {
27066 if ((sscanf(v->value, "%30d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
27067 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
27068 global_rtptimeout = 0;
27069 }
27070 } else if (!strcasecmp(v->name, "rtpholdtimeout")) {
27071 if ((sscanf(v->value, "%30d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
27072 ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d. Using default.\n", v->value, v->lineno);
27073 global_rtpholdtimeout = 0;
27074 }
27075 } else if (!strcasecmp(v->name, "rtpkeepalive")) {
27076 if ((sscanf(v->value, "%30d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
27077 ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d. Using default.\n", v->value, v->lineno);
27078 global_rtpkeepalive = DEFAULT_RTPKEEPALIVE;
27079 }
27080 } else if (!strcasecmp(v->name, "compactheaders")) {
27081 sip_cfg.compactheaders = ast_true(v->value);
27082 } else if (!strcasecmp(v->name, "notifymimetype")) {
27083 ast_copy_string(default_notifymime, v->value, sizeof(default_notifymime));
27084 } else if (!strcasecmp(v->name, "directrtpsetup")) {
27085 sip_cfg.directrtpsetup = ast_true(v->value);
27086 } else if (!strcasecmp(v->name, "notifyringing")) {
27087 sip_cfg.notifyringing = ast_true(v->value);
27088 } else if (!strcasecmp(v->name, "notifyhold")) {
27089 sip_cfg.notifyhold = ast_true(v->value);
27090 } else if (!strcasecmp(v->name, "notifycid")) {
27091 if (!strcasecmp(v->value, "ignore-context")) {
27092 sip_cfg.notifycid = IGNORE_CONTEXT;
27093 } else {
27094 sip_cfg.notifycid = ast_true(v->value);
27095 }
27096 } else if (!strcasecmp(v->name, "alwaysauthreject")) {
27097 sip_cfg.alwaysauthreject = ast_true(v->value);
27098 } else if (!strcasecmp(v->name, "auth_options_requests")) {
27099 if (ast_true(v->value)) {
27100 sip_cfg.auth_options_requests = 1;
27101 }
27102 } else if (!strcasecmp(v->name, "mohinterpret")) {
27103 ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
27104 } else if (!strcasecmp(v->name, "mohsuggest")) {
27105 ast_copy_string(default_mohsuggest, v->value, sizeof(default_mohsuggest));
27106 } else if (!strcasecmp(v->name, "language")) {
27107 ast_copy_string(default_language, v->value, sizeof(default_language));
27108 } else if (!strcasecmp(v->name, "regcontext")) {
27109 ast_copy_string(newcontexts, v->value, sizeof(newcontexts));
27110 stringp = newcontexts;
27111
27112 cleanup_stale_contexts(stringp, oldregcontext);
27113
27114 while ((context = strsep(&stringp, "&"))) {
27115 ast_copy_string(used_context, context, sizeof(used_context));
27116 ast_context_find_or_create(NULL, NULL, context, "SIP");
27117 }
27118 ast_copy_string(sip_cfg.regcontext, v->value, sizeof(sip_cfg.regcontext));
27119 } else if (!strcasecmp(v->name, "regextenonqualify")) {
27120 sip_cfg.regextenonqualify = ast_true(v->value);
27121 } else if (!strcasecmp(v->name, "callerid")) {
27122 ast_copy_string(default_callerid, v->value, sizeof(default_callerid));
27123 } else if (!strcasecmp(v->name, "mwi_from")) {
27124 ast_copy_string(default_mwi_from, v->value, sizeof(default_mwi_from));
27125 } else if (!strcasecmp(v->name, "fromdomain")) {
27126 char *fromdomainport;
27127 ast_copy_string(default_fromdomain, v->value, sizeof(default_fromdomain));
27128 if ((fromdomainport = strchr(default_fromdomain, ':'))) {
27129 *fromdomainport++ = '\0';
27130 if (!(default_fromdomainport = port_str2int(fromdomainport, 0))) {
27131 ast_log(LOG_NOTICE, "'%s' is not a valid port number for fromdomain.\n",fromdomainport);
27132 }
27133 } else {
27134 default_fromdomainport = STANDARD_SIP_PORT;
27135 }
27136 } else if (!strcasecmp(v->name, "outboundproxy")) {
27137 char *tok, *proxyname;
27138
27139 if (ast_strlen_zero(v->value)) {
27140 ast_log(LOG_WARNING, "no value given for outbound proxy on line %d of sip.conf.", v->lineno);
27141 continue;
27142 }
27143
27144 tok = ast_skip_blanks(strtok(ast_strdupa(v->value), ","));
27145
27146 sip_parse_host(tok, v->lineno, &proxyname,
27147 &sip_cfg.outboundproxy.port,
27148 &sip_cfg.outboundproxy.transport);
27149
27150 if ((tok = strtok(NULL, ","))) {
27151 sip_cfg.outboundproxy.force = !strncasecmp(ast_skip_blanks(tok), "force", 5);
27152 } else {
27153 sip_cfg.outboundproxy.force = FALSE;
27154 }
27155
27156 if (ast_strlen_zero(proxyname)) {
27157 ast_log(LOG_WARNING, "you must specify a name for the outboundproxy on line %d of sip.conf.", v->lineno);
27158 sip_cfg.outboundproxy.name[0] = '\0';
27159 continue;
27160 }
27161
27162 ast_copy_string(sip_cfg.outboundproxy.name, proxyname, sizeof(sip_cfg.outboundproxy.name));
27163
27164 proxy_update(&sip_cfg.outboundproxy);
27165 } else if (!strcasecmp(v->name, "autocreatepeer")) {
27166 sip_cfg.autocreatepeer = ast_true(v->value);
27167 } else if (!strcasecmp(v->name, "match_auth_username")) {
27168 global_match_auth_username = ast_true(v->value);
27169 } else if (!strcasecmp(v->name, "srvlookup")) {
27170 sip_cfg.srvlookup = ast_true(v->value);
27171 } else if (!strcasecmp(v->name, "pedantic")) {
27172 sip_cfg.pedanticsipchecking = ast_true(v->value);
27173 } else if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
27174 max_expiry = atoi(v->value);
27175 if (max_expiry < 1) {
27176 max_expiry = DEFAULT_MAX_EXPIRY;
27177 }
27178 } else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
27179 min_expiry = atoi(v->value);
27180 if (min_expiry < 1) {
27181 min_expiry = DEFAULT_MIN_EXPIRY;
27182 }
27183 } else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
27184 default_expiry = atoi(v->value);
27185 if (default_expiry < 1) {
27186 default_expiry = DEFAULT_DEFAULT_EXPIRY;
27187 }
27188 } else if (!strcasecmp(v->name, "mwiexpiry") || !strcasecmp(v->name, "mwiexpirey")) {
27189 mwi_expiry = atoi(v->value);
27190 if (mwi_expiry < 1) {
27191 mwi_expiry = DEFAULT_MWI_EXPIRY;
27192 }
27193 } else if (!strcasecmp(v->name, "tcpauthtimeout")) {
27194 if (ast_parse_arg(v->value, PARSE_INT32|PARSE_DEFAULT|PARSE_IN_RANGE,
27195 &authtimeout, DEFAULT_AUTHTIMEOUT, 1, INT_MAX)) {
27196 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n",
27197 v->name, v->value, v->lineno, config);
27198 }
27199 } else if (!strcasecmp(v->name, "tcpauthlimit")) {
27200 if (ast_parse_arg(v->value, PARSE_INT32|PARSE_DEFAULT|PARSE_IN_RANGE,
27201 &authlimit, DEFAULT_AUTHLIMIT, 1, INT_MAX)) {
27202 ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of %s\n",
27203 v->name, v->value, v->lineno, config);
27204 }
27205 } else if (!strcasecmp(v->name, "sipdebug")) {
27206 if (ast_true(v->value))
27207 sipdebug |= sip_debug_config;
27208 } else if (!strcasecmp(v->name, "dumphistory")) {
27209 dumphistory = ast_true(v->value);
27210 } else if (!strcasecmp(v->name, "recordhistory")) {
27211 recordhistory = ast_true(v->value);
27212 } else if (!strcasecmp(v->name, "registertimeout")) {
27213 global_reg_timeout = atoi(v->value);
27214 if (global_reg_timeout < 1) {
27215 global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT;
27216 }
27217 } else if (!strcasecmp(v->name, "registerattempts")) {
27218 global_regattempts_max = atoi(v->value);
27219 } else if (!strcasecmp(v->name, "bindaddr") || !strcasecmp(v->name, "udpbindaddr")) {
27220 if (ast_parse_arg(v->value, PARSE_ADDR, &bindaddr)) {
27221 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
27222 }
27223 } else if (!strcasecmp(v->name, "localnet")) {
27224 struct ast_ha *na;
27225 int ha_error = 0;
27226
27227 if (!(na = ast_append_ha("d", v->value, localaddr, &ha_error))) {
27228 ast_log(LOG_WARNING, "Invalid localnet value: %s\n", v->value);
27229 } else {
27230 localaddr = na;
27231 }
27232 if (ha_error) {
27233 ast_log(LOG_ERROR, "Bad localnet configuration value line %d : %s\n", v->lineno, v->value);
27234 }
27235 } else if (!strcasecmp(v->name, "media_address")) {
27236 if (ast_parse_arg(v->value, PARSE_ADDR, &media_address))
27237 ast_log(LOG_WARNING, "Invalid address for media_address keyword: %s\n", v->value);
27238 } else if (!strcasecmp(v->name, "externaddr") || !strcasecmp(v->name, "externip")) {
27239 if (ast_parse_arg(v->value, PARSE_ADDR, &externaddr)) {
27240 ast_log(LOG_WARNING,
27241 "Invalid address for externaddr keyword: %s\n",
27242 v->value);
27243 }
27244 externexpire = 0;
27245 } else if (!strcasecmp(v->name, "externhost")) {
27246 ast_copy_string(externhost, v->value, sizeof(externhost));
27247 if (ast_sockaddr_resolve_first(&externaddr, externhost, 0)) {
27248 ast_log(LOG_WARNING, "Invalid address for externhost keyword: %s\n", externhost);
27249 }
27250 externexpire = time(NULL);
27251 } else if (!strcasecmp(v->name, "externrefresh")) {
27252 if (sscanf(v->value, "%30d", &externrefresh) != 1) {
27253 ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
27254 externrefresh = 10;
27255 }
27256 } else if (!strcasecmp(v->name, "externtcpport")) {
27257 if (!(externtcpport = port_str2int(v->value, 0))) {
27258 ast_log(LOG_WARNING, "Invalid externtcpport value, must be a positive integer between 1 and 65535 at line %d\n", v->lineno);
27259 externtcpport = 0;
27260 }
27261 } else if (!strcasecmp(v->name, "externtlsport")) {
27262 if (!(externtlsport = port_str2int(v->value, STANDARD_TLS_PORT))) {
27263 ast_log(LOG_WARNING, "Invalid externtlsport value, must be a positive integer between 1 and 65535 at line %d\n", v->lineno);
27264 }
27265 } else if (!strcasecmp(v->name, "allow")) {
27266 int error = ast_parse_allow_disallow(&default_prefs, &sip_cfg.capability, v->value, TRUE);
27267 if (error) {
27268 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
27269 }
27270 } else if (!strcasecmp(v->name, "disallow")) {
27271 int error = ast_parse_allow_disallow(&default_prefs, &sip_cfg.capability, v->value, FALSE);
27272 if (error) {
27273 ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
27274 }
27275 } else if (!strcasecmp(v->name, "preferred_codec_only")) {
27276 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
27277 } else if (!strcasecmp(v->name, "autoframing")) {
27278 global_autoframing = ast_true(v->value);
27279 } else if (!strcasecmp(v->name, "allowexternaldomains")) {
27280 sip_cfg.allow_external_domains = ast_true(v->value);
27281 } else if (!strcasecmp(v->name, "autodomain")) {
27282 auto_sip_domains = ast_true(v->value);
27283 } else if (!strcasecmp(v->name, "domain")) {
27284 char *domain = ast_strdupa(v->value);
27285 char *cntx = strchr(domain, ',');
27286
27287 if (cntx) {
27288 *cntx++ = '\0';
27289 }
27290
27291 if (ast_strlen_zero(cntx)) {
27292 ast_debug(1, "No context specified at line %d for domain '%s'\n", v->lineno, domain);
27293 }
27294 if (ast_strlen_zero(domain)) {
27295 ast_log(LOG_WARNING, "Empty domain specified at line %d\n", v->lineno);
27296 } else {
27297 add_sip_domain(ast_strip(domain), SIP_DOMAIN_CONFIG, cntx ? ast_strip(cntx) : "");
27298 }
27299 } else if (!strcasecmp(v->name, "register")) {
27300 if (sip_register(v->value, v->lineno) == 0) {
27301 registry_count++;
27302 }
27303 } else if (!strcasecmp(v->name, "mwi")) {
27304 sip_subscribe_mwi(v->value, v->lineno);
27305 } else if (!strcasecmp(v->name, "tos_sip")) {
27306 if (ast_str2tos(v->value, &global_tos_sip)) {
27307 ast_log(LOG_WARNING, "Invalid tos_sip value at line %d, refer to QoS documentation\n", v->lineno);
27308 }
27309 } else if (!strcasecmp(v->name, "tos_audio")) {
27310 if (ast_str2tos(v->value, &global_tos_audio)) {
27311 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
27312 }
27313 } else if (!strcasecmp(v->name, "tos_video")) {
27314 if (ast_str2tos(v->value, &global_tos_video)) {
27315 ast_log(LOG_WARNING, "Invalid tos_video value at line %d, refer to QoS documentation\n", v->lineno);
27316 }
27317 } else if (!strcasecmp(v->name, "tos_text")) {
27318 if (ast_str2tos(v->value, &global_tos_text)) {
27319 ast_log(LOG_WARNING, "Invalid tos_text value at line %d, refer to QoS documentation\n", v->lineno);
27320 }
27321 } else if (!strcasecmp(v->name, "cos_sip")) {
27322 if (ast_str2cos(v->value, &global_cos_sip)) {
27323 ast_log(LOG_WARNING, "Invalid cos_sip value at line %d, refer to QoS documentation\n", v->lineno);
27324 }
27325 } else if (!strcasecmp(v->name, "cos_audio")) {
27326 if (ast_str2cos(v->value, &global_cos_audio)) {
27327 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
27328 }
27329 } else if (!strcasecmp(v->name, "cos_video")) {
27330 if (ast_str2cos(v->value, &global_cos_video)) {
27331 ast_log(LOG_WARNING, "Invalid cos_video value at line %d, refer to QoS documentation\n", v->lineno);
27332 }
27333 } else if (!strcasecmp(v->name, "cos_text")) {
27334 if (ast_str2cos(v->value, &global_cos_text)) {
27335 ast_log(LOG_WARNING, "Invalid cos_text value at line %d, refer to QoS documentation\n", v->lineno);
27336 }
27337 } else if (!strcasecmp(v->name, "bindport")) {
27338 if (sscanf(v->value, "%5d", &bindport) != 1) {
27339 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
27340 }
27341 } else if (!strcasecmp(v->name, "qualify")) {
27342 if (!strcasecmp(v->value, "no")) {
27343 default_qualify = 0;
27344 } else if (!strcasecmp(v->value, "yes")) {
27345 default_qualify = DEFAULT_MAXMS;
27346 } else if (sscanf(v->value, "%30d", &default_qualify) != 1) {
27347 ast_log(LOG_WARNING, "Qualification default should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", v->lineno);
27348 default_qualify = 0;
27349 }
27350 } else if (!strcasecmp(v->name, "qualifyfreq")) {
27351 int i;
27352 if (sscanf(v->value, "%30d", &i) == 1) {
27353 global_qualifyfreq = i * 1000;
27354 } else {
27355 ast_log(LOG_WARNING, "Invalid qualifyfreq number '%s' at line %d of %s\n", v->value, v->lineno, config);
27356 global_qualifyfreq = DEFAULT_QUALIFYFREQ;
27357 }
27358 } else if (!strcasecmp(v->name, "callevents")) {
27359 sip_cfg.callevents = ast_true(v->value);
27360 } else if (!strcasecmp(v->name, "authfailureevents")) {
27361 global_authfailureevents = ast_true(v->value);
27362 } else if (!strcasecmp(v->name, "maxcallbitrate")) {
27363 default_maxcallbitrate = atoi(v->value);
27364 if (default_maxcallbitrate < 0)
27365 default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE;
27366 } else if (!strcasecmp(v->name, "matchexternaddrlocally") || !strcasecmp(v->name, "matchexterniplocally")) {
27367 sip_cfg.matchexternaddrlocally = ast_true(v->value);
27368 } else if (!strcasecmp(v->name, "session-timers")) {
27369 int i = (int) str2stmode(v->value);
27370 if (i < 0) {
27371 ast_log(LOG_WARNING, "Invalid session-timers '%s' at line %d of %s\n", v->value, v->lineno, config);
27372 global_st_mode = SESSION_TIMER_MODE_ACCEPT;
27373 } else {
27374 global_st_mode = i;
27375 }
27376 } else if (!strcasecmp(v->name, "session-expires")) {
27377 if (sscanf(v->value, "%30d", &global_max_se) != 1) {
27378 ast_log(LOG_WARNING, "Invalid session-expires '%s' at line %d of %s\n", v->value, v->lineno, config);
27379 global_max_se = DEFAULT_MAX_SE;
27380 }
27381 } else if (!strcasecmp(v->name, "session-minse")) {
27382 if (sscanf(v->value, "%30d", &global_min_se) != 1) {
27383 ast_log(LOG_WARNING, "Invalid session-minse '%s' at line %d of %s\n", v->value, v->lineno, config);
27384 global_min_se = DEFAULT_MIN_SE;
27385 }
27386 if (global_min_se < 90) {
27387 ast_log(LOG_WARNING, "session-minse '%s' at line %d of %s is not allowed to be < 90 secs\n", v->value, v->lineno, config);
27388 global_min_se = DEFAULT_MIN_SE;
27389 }
27390 } else if (!strcasecmp(v->name, "session-refresher")) {
27391 int i = (int) str2strefresher(v->value);
27392 if (i < 0) {
27393 ast_log(LOG_WARNING, "Invalid session-refresher '%s' at line %d of %s\n", v->value, v->lineno, config);
27394 global_st_refresher = SESSION_TIMER_REFRESHER_UAS;
27395 } else {
27396 global_st_refresher = i;
27397 }
27398 } else if (!strcasecmp(v->name, "qualifygap")) {
27399 if (sscanf(v->value, "%30d", &global_qualify_gap) != 1) {
27400 ast_log(LOG_WARNING, "Invalid qualifygap '%s' at line %d of %s\n", v->value, v->lineno, config);
27401 global_qualify_gap = DEFAULT_QUALIFY_GAP;
27402 }
27403 } else if (!strcasecmp(v->name, "qualifypeers")) {
27404 if (sscanf(v->value, "%30d", &global_qualify_peers) != 1) {
27405 ast_log(LOG_WARNING, "Invalid pokepeers '%s' at line %d of %s\n", v->value, v->lineno, config);
27406 global_qualify_peers = DEFAULT_QUALIFY_PEERS;
27407 }
27408 } else if (!strcasecmp(v->name, "disallowed_methods")) {
27409 char *disallow = ast_strdupa(v->value);
27410 mark_parsed_methods(&sip_cfg.disallowed_methods, disallow);
27411 } else if (!strcasecmp(v->name, "shrinkcallerid")) {
27412 if (ast_true(v->value)) {
27413 global_shrinkcallerid = 1;
27414 } else if (ast_false(v->value)) {
27415 global_shrinkcallerid = 0;
27416 } else {
27417 ast_log(LOG_WARNING, "shrinkcallerid value %s is not valid at line %d.\n", v->value, v->lineno);
27418 }
27419 } else if (!strcasecmp(v->name, "use_q850_reason")) {
27420 ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_Q850_REASON);
27421 } else if (!strcasecmp(v->name, "maxforwards")) {
27422 if ((sscanf(v->value, "%30d", &sip_cfg.default_max_forwards) != 1) || (sip_cfg.default_max_forwards < 1)) {
27423 ast_log(LOG_WARNING, "'%s' is not a valid maxforwards value at line %d. Using default.\n", v->value, v->lineno);
27424 sip_cfg.default_max_forwards = DEFAULT_MAX_FORWARDS;
27425 }
27426 } else if (!strcasecmp(v->name, "subscribe_network_change_event")) {
27427 if (ast_true(v->value)) {
27428 subscribe_network_change = 1;
27429 } else if (ast_false(v->value)) {
27430 subscribe_network_change = 0;
27431 } else {
27432 ast_log(LOG_WARNING, "subscribe_network_change_event value %s is not valid at line %d.\n", v->value, v->lineno);
27433 }
27434 } else if (!strcasecmp(v->name, "snom_aoc_enabled")) {
27435 ast_set2_flag(&global_flags[2], ast_true(v->value), SIP_PAGE3_SNOM_AOC);
27436 } else if (!strcasecmp(v->name, "parkinglot")) {
27437 ast_copy_string(default_parkinglot, v->value, sizeof(default_parkinglot));
27438 }
27439 }
27440
27441 if (subscribe_network_change) {
27442 network_change_event_subscribe();
27443 } else {
27444 network_change_event_unsubscribe();
27445 }
27446
27447 if (global_t1 < global_t1min) {
27448 ast_log(LOG_WARNING, "'t1min' (%d) cannot be greater than 't1timer' (%d). Resetting 't1timer' to the value of 't1min'\n", global_t1min, global_t1);
27449 global_t1 = global_t1min;
27450 }
27451
27452 if (global_timer_b < global_t1 * 64) {
27453 if (timerb_set && timert1_set) {
27454 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);
27455 } else if (timerb_set) {
27456 if ((global_t1 = global_timer_b / 64) < global_t1min) {
27457 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);
27458 global_t1 = global_t1min;
27459 global_timer_b = global_t1 * 64;
27460 }
27461 } else {
27462 global_timer_b = global_t1 * 64;
27463 }
27464 }
27465 if (!sip_cfg.allow_external_domains && AST_LIST_EMPTY(&domain_list)) {
27466 ast_log(LOG_WARNING, "To disallow external domains, you need to configure local SIP domains.\n");
27467 sip_cfg.allow_external_domains = 1;
27468 }
27469
27470 if (default_transports == 0) {
27471 default_transports = default_primary_transport = SIP_TRANSPORT_UDP;
27472 }
27473
27474
27475 for (v = ast_variable_browse(cfg, "authentication"); v ; v = v->next) {
27476
27477 if (!strcasecmp(v->name, "auth")) {
27478 authl = add_realm_authentication(authl, v->value, v->lineno);
27479 }
27480 }
27481
27482 if (bindport) {
27483 if (ast_sockaddr_port(&bindaddr)) {
27484 ast_log(LOG_WARNING, "bindport is also specified in bindaddr. "
27485 "Using %d.\n", bindport);
27486 }
27487 ast_sockaddr_set_port(&bindaddr, bindport);
27488 }
27489
27490 if (!ast_sockaddr_port(&bindaddr)) {
27491 ast_sockaddr_set_port(&bindaddr, STANDARD_SIP_PORT);
27492 }
27493
27494
27495 ast_sockaddr_copy(&internip, &bindaddr);
27496 if (ast_find_ourip(&internip, &bindaddr, 0)) {
27497 ast_log(LOG_WARNING, "Unable to get own IP address, SIP disabled\n");
27498 ast_config_destroy(cfg);
27499 return 0;
27500 }
27501
27502 ast_mutex_lock(&netlock);
27503 if ((sipsock > -1) && (ast_sockaddr_cmp(&old_bindaddr, &bindaddr))) {
27504 close(sipsock);
27505 sipsock = -1;
27506 }
27507 if (sipsock < 0) {
27508 sipsock = socket(ast_sockaddr_is_ipv6(&bindaddr) ?
27509 AF_INET6 : AF_INET, SOCK_DGRAM, 0);
27510 if (sipsock < 0) {
27511 ast_log(LOG_WARNING, "Unable to create SIP socket: %s\n", strerror(errno));
27512 ast_config_destroy(cfg);
27513 ast_mutex_unlock(&netlock);
27514 return -1;
27515 } else {
27516
27517 const int reuseFlag = 1;
27518
27519 setsockopt(sipsock, SOL_SOCKET, SO_REUSEADDR,
27520 (const char*)&reuseFlag,
27521 sizeof reuseFlag);
27522
27523 ast_enable_packet_fragmentation(sipsock);
27524
27525 if (ast_bind(sipsock, &bindaddr) < 0) {
27526 ast_log(LOG_WARNING, "Failed to bind to %s: %s\n",
27527 ast_sockaddr_stringify(&bindaddr), strerror(errno));
27528 close(sipsock);
27529 sipsock = -1;
27530 } else {
27531 ast_verb(2, "SIP Listening on %s\n", ast_sockaddr_stringify(&bindaddr));
27532 ast_set_qos(sipsock, global_tos_sip, global_cos_sip, "SIP");
27533 }
27534 }
27535 } else {
27536 ast_set_qos(sipsock, global_tos_sip, global_cos_sip, "SIP");
27537 }
27538 ast_mutex_unlock(&netlock);
27539
27540
27541 if (sip_cfg.tcp_enabled) {
27542 if (ast_sockaddr_isnull(&sip_tcp_desc.local_address)) {
27543 ast_sockaddr_copy(&sip_tcp_desc.local_address, &bindaddr);
27544 }
27545 if (!ast_sockaddr_port(&sip_tcp_desc.local_address)) {
27546 ast_sockaddr_set_port(&sip_tcp_desc.local_address, STANDARD_SIP_PORT);
27547 }
27548 } else {
27549 ast_sockaddr_setnull(&sip_tcp_desc.local_address);
27550 }
27551 ast_tcptls_server_start(&sip_tcp_desc);
27552 if (sip_cfg.tcp_enabled && sip_tcp_desc.accept_fd == -1) {
27553
27554 ast_log(LOG_ERROR, "SIP TCP Server start failed. Not listening on TCP socket.\n");
27555 } else {
27556 ast_debug(2, "SIP TCP server started\n");
27557 }
27558
27559
27560 memcpy(sip_tls_desc.tls_cfg, &default_tls_cfg, sizeof(default_tls_cfg));
27561
27562 if (ast_ssl_setup(sip_tls_desc.tls_cfg)) {
27563 if (ast_sockaddr_isnull(&sip_tls_desc.local_address)) {
27564 ast_sockaddr_copy(&sip_tls_desc.local_address, &bindaddr);
27565 ast_sockaddr_set_port(&sip_tls_desc.local_address,
27566 STANDARD_TLS_PORT);
27567 }
27568 if (!ast_sockaddr_port(&sip_tls_desc.local_address)) {
27569 ast_sockaddr_set_port(&sip_tls_desc.local_address,
27570 STANDARD_TLS_PORT);
27571 }
27572 ast_tcptls_server_start(&sip_tls_desc);
27573 if (default_tls_cfg.enabled && sip_tls_desc.accept_fd == -1) {
27574 ast_log(LOG_ERROR, "TLS Server start failed. Not listening on TLS socket.\n");
27575 sip_tls_desc.tls_cfg = NULL;
27576 }
27577 } else if (sip_tls_desc.tls_cfg->enabled) {
27578 sip_tls_desc.tls_cfg = NULL;
27579 ast_log(LOG_WARNING, "SIP TLS server did not load because of errors.\n");
27580 }
27581
27582 if (ucfg) {
27583 struct ast_variable *gen;
27584 int genhassip, genregistersip;
27585 const char *hassip, *registersip;
27586
27587 genhassip = ast_true(ast_variable_retrieve(ucfg, "general", "hassip"));
27588 genregistersip = ast_true(ast_variable_retrieve(ucfg, "general", "registersip"));
27589 gen = ast_variable_browse(ucfg, "general");
27590 cat = ast_category_browse(ucfg, NULL);
27591 while (cat) {
27592 if (strcasecmp(cat, "general")) {
27593 hassip = ast_variable_retrieve(ucfg, cat, "hassip");
27594 registersip = ast_variable_retrieve(ucfg, cat, "registersip");
27595 if (ast_true(hassip) || (!hassip && genhassip)) {
27596 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0, 0);
27597 if (peer) {
27598
27599 peer->type = SIP_TYPE_USER | SIP_TYPE_PEER;
27600 ao2_t_link(peers, peer, "link peer into peer table");
27601 if ((peer->type & SIP_TYPE_PEER) && !ast_sockaddr_isnull(&peer->addr)) {
27602 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
27603 }
27604
27605 unref_peer(peer, "unref_peer: from reload_config");
27606 peer_count++;
27607 }
27608 }
27609 if (ast_true(registersip) || (!registersip && genregistersip)) {
27610 char tmp[256];
27611 const char *host = ast_variable_retrieve(ucfg, cat, "host");
27612 const char *username = ast_variable_retrieve(ucfg, cat, "username");
27613 const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
27614 const char *contact = ast_variable_retrieve(ucfg, cat, "contact");
27615 const char *authuser = ast_variable_retrieve(ucfg, cat, "authuser");
27616 if (!host) {
27617 host = ast_variable_retrieve(ucfg, "general", "host");
27618 }
27619 if (!username) {
27620 username = ast_variable_retrieve(ucfg, "general", "username");
27621 }
27622 if (!secret) {
27623 secret = ast_variable_retrieve(ucfg, "general", "secret");
27624 }
27625 if (!contact) {
27626 contact = "s";
27627 }
27628 if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
27629 if (!ast_strlen_zero(secret)) {
27630 if (!ast_strlen_zero(authuser)) {
27631 snprintf(tmp, sizeof(tmp), "%s?%s:%s:%s@%s/%s", cat, username, secret, authuser, host, contact);
27632 } else {
27633 snprintf(tmp, sizeof(tmp), "%s?%s:%s@%s/%s", cat, username, secret, host, contact);
27634 }
27635 } else if (!ast_strlen_zero(authuser)) {
27636 snprintf(tmp, sizeof(tmp), "%s?%s::%s@%s/%s", cat, username, authuser, host, contact);
27637 } else {
27638 snprintf(tmp, sizeof(tmp), "%s?%s@%s/%s", cat, username, host, contact);
27639 }
27640 if (sip_register(tmp, 0) == 0) {
27641 registry_count++;
27642 }
27643 }
27644 }
27645 }
27646 cat = ast_category_browse(ucfg, cat);
27647 }
27648 ast_config_destroy(ucfg);
27649 }
27650
27651
27652 cat = NULL;
27653 while ( (cat = ast_category_browse(cfg, cat)) ) {
27654 const char *utype;
27655 if (!strcasecmp(cat, "general") || !strcasecmp(cat, "authentication"))
27656 continue;
27657 utype = ast_variable_retrieve(cfg, cat, "type");
27658 if (!utype) {
27659 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
27660 continue;
27661 } else {
27662 if (!strcasecmp(utype, "user")) {
27663 ;
27664 } else if (!strcasecmp(utype, "friend")) {
27665 ;
27666 } else if (!strcasecmp(utype, "peer")) {
27667 ;
27668 } else {
27669 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
27670 continue;
27671 }
27672 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0, 0);
27673 if (peer) {
27674 ao2_t_link(peers, peer, "link peer into peers table");
27675 if ((peer->type & SIP_TYPE_PEER) && !ast_sockaddr_isnull(&peer->addr)) {
27676 ao2_t_link(peers_by_ip, peer, "link peer into peers_by_ip table");
27677 }
27678 unref_peer(peer, "unref the result of the build_peer call. Now, the links from the tables are the only ones left.");
27679 peer_count++;
27680 }
27681 }
27682 }
27683
27684
27685
27686
27687
27688
27689 if (auto_sip_domains) {
27690 char temp[MAXHOSTNAMELEN];
27691
27692
27693 if (!ast_sockaddr_isnull(&bindaddr) && !ast_sockaddr_is_any(&bindaddr)) {
27694 add_sip_domain(ast_sockaddr_stringify_addr(&bindaddr),
27695 SIP_DOMAIN_AUTO, NULL);
27696 } else if (!ast_sockaddr_isnull(&internip) && !ast_sockaddr_is_any(&internip)) {
27697
27698 add_sip_domain(ast_sockaddr_stringify_addr(&internip),
27699 SIP_DOMAIN_AUTO, NULL);
27700 } else {
27701 ast_log(LOG_NOTICE, "Can't add wildcard IP address to domain list, please add IP address to domain manually.\n");
27702 }
27703
27704
27705 if (!ast_sockaddr_isnull(&sip_tcp_desc.local_address) &&
27706 !ast_sockaddr_cmp(&bindaddr, &sip_tcp_desc.local_address)) {
27707 add_sip_domain(ast_sockaddr_stringify_addr(&sip_tcp_desc.local_address),
27708 SIP_DOMAIN_AUTO, NULL);
27709 }
27710
27711
27712 if (!ast_sockaddr_isnull(&sip_tls_desc.local_address) &&
27713 !ast_sockaddr_cmp(&bindaddr, &sip_tls_desc.local_address) &&
27714 !ast_sockaddr_cmp(&sip_tcp_desc.local_address,
27715 &sip_tls_desc.local_address)) {
27716 add_sip_domain(ast_sockaddr_stringify_addr(&sip_tcp_desc.local_address),
27717 SIP_DOMAIN_AUTO, NULL);
27718 }
27719
27720
27721 if (!ast_sockaddr_isnull(&externaddr)) {
27722 add_sip_domain(ast_sockaddr_stringify_addr(&externaddr),
27723 SIP_DOMAIN_AUTO, NULL);
27724 }
27725
27726
27727 if (!ast_strlen_zero(externhost)) {
27728 add_sip_domain(externhost, SIP_DOMAIN_AUTO, NULL);
27729 }
27730
27731
27732 if (!gethostname(temp, sizeof(temp))) {
27733 add_sip_domain(temp, SIP_DOMAIN_AUTO, NULL);
27734 }
27735 }
27736
27737
27738 ast_config_destroy(cfg);
27739
27740
27741 if (notify_types)
27742 ast_config_destroy(notify_types);
27743 if ((notify_types = ast_config_load(notify_config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
27744 ast_log(LOG_ERROR, "Contents of %s are invalid and cannot be parsed.\n", notify_config);
27745 notify_types = NULL;
27746 }
27747
27748
27749 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);
27750 run_end = time(0);
27751 ast_debug(4, "SIP reload_config done...Runtime= %d sec\n", (int)(run_end-run_start));
27752
27753 return 0;
27754 }
27755
27756 static int apply_directmedia_ha(struct sip_pvt *p, const char *op)
27757 {
27758 struct ast_sockaddr us = { { 0, }, }, them = { { 0, }, };
27759 int res = AST_SENSE_ALLOW;
27760
27761 ast_rtp_instance_get_remote_address(p->rtp, &them);
27762 ast_rtp_instance_get_local_address(p->rtp, &us);
27763
27764 if ((res = ast_apply_ha(p->directmediaha, &them)) == AST_SENSE_DENY) {
27765 const char *us_addr = ast_strdupa(ast_sockaddr_stringify(&us));
27766 const char *them_addr = ast_strdupa(ast_sockaddr_stringify(&them));
27767
27768 ast_debug(3, "Reinvite %s to %s denied by directmedia ACL on %s\n",
27769 op, them_addr, us_addr);
27770 }
27771
27772 return res;
27773 }
27774
27775 static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
27776 {
27777 struct sip_pvt *p;
27778 struct ast_udptl *udptl = NULL;
27779
27780 p = chan->tech_pvt;
27781 if (!p) {
27782 return NULL;
27783 }
27784
27785 sip_pvt_lock(p);
27786 if (p->udptl && ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
27787 if (apply_directmedia_ha(p, "UDPTL T.38 data")) {
27788 udptl = p->udptl;
27789 }
27790 }
27791 sip_pvt_unlock(p);
27792 return udptl;
27793 }
27794
27795 static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
27796 {
27797 struct sip_pvt *p;
27798
27799 p = chan->tech_pvt;
27800 if (!p) {
27801 return -1;
27802 }
27803
27804
27805
27806 sip_pvt_lock(p);
27807 while (p->owner && ast_channel_trylock(p->owner)) {
27808 sip_pvt_unlock(p);
27809 usleep(1);
27810 sip_pvt_lock(p);
27811 }
27812
27813 if (!p->owner) {
27814 sip_pvt_unlock(p);
27815 return 0;
27816 }
27817 if (udptl) {
27818 ast_udptl_get_peer(udptl, &p->udptlredirip);
27819 } else {
27820 memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
27821 }
27822 if (!ast_test_flag(&p->flags[0], SIP_GOTREFER)) {
27823 if (!p->pendinginvite) {
27824 ast_debug(3, "Sending reinvite on SIP '%s' - It's UDPTL soon redirected to IP %s\n",
27825 p->callid, ast_sockaddr_stringify(udptl ? &p->udptlredirip : &p->ourip));
27826 transmit_reinvite_with_sdp(p, TRUE, FALSE);
27827 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
27828 ast_debug(3, "Deferring reinvite on SIP '%s' - It's UDPTL will be redirected to IP %s\n",
27829 p->callid, ast_sockaddr_stringify(udptl ? &p->udptlredirip : &p->ourip));
27830 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
27831 }
27832 }
27833
27834 p->lastrtprx = p->lastrtptx = time(NULL);
27835 ast_channel_unlock(p->owner);
27836 sip_pvt_unlock(p);
27837 return 0;
27838 }
27839
27840 static enum ast_rtp_glue_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
27841 {
27842 struct sip_pvt *p = NULL;
27843 enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
27844
27845 if (!(p = chan->tech_pvt)) {
27846 return AST_RTP_GLUE_RESULT_FORBID;
27847 }
27848
27849 sip_pvt_lock(p);
27850 if (!(p->rtp)) {
27851 sip_pvt_unlock(p);
27852 return AST_RTP_GLUE_RESULT_FORBID;
27853 }
27854
27855 ao2_ref(p->rtp, +1);
27856 *instance = p->rtp;
27857
27858 if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
27859 res = AST_RTP_GLUE_RESULT_REMOTE;
27860 if (!apply_directmedia_ha(p, "audio")) {
27861 res = AST_RTP_GLUE_RESULT_FORBID;
27862 }
27863 } else if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA_NAT)) {
27864 res = AST_RTP_GLUE_RESULT_REMOTE;
27865 } else if (ast_test_flag(&global_jbconf, AST_JB_FORCED)) {
27866 res = AST_RTP_GLUE_RESULT_FORBID;
27867 }
27868
27869 if (p->srtp) {
27870 res = AST_RTP_GLUE_RESULT_FORBID;
27871 }
27872
27873 sip_pvt_unlock(p);
27874
27875 return res;
27876 }
27877
27878 static enum ast_rtp_glue_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
27879 {
27880 struct sip_pvt *p = NULL;
27881 enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
27882
27883 if (!(p = chan->tech_pvt)) {
27884 return AST_RTP_GLUE_RESULT_FORBID;
27885 }
27886
27887 sip_pvt_lock(p);
27888 if (!(p->vrtp)) {
27889 sip_pvt_unlock(p);
27890 return AST_RTP_GLUE_RESULT_FORBID;
27891 }
27892
27893 ao2_ref(p->vrtp, +1);
27894 *instance = p->vrtp;
27895
27896 if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
27897 res = AST_RTP_GLUE_RESULT_REMOTE;
27898 if (!apply_directmedia_ha(p, "video")) {
27899 res = AST_RTP_GLUE_RESULT_FORBID;
27900 }
27901 }
27902
27903 sip_pvt_unlock(p);
27904
27905 return res;
27906 }
27907
27908 static enum ast_rtp_glue_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
27909 {
27910 struct sip_pvt *p = NULL;
27911 enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
27912
27913 if (!(p = chan->tech_pvt)) {
27914 return AST_RTP_GLUE_RESULT_FORBID;
27915 }
27916
27917 sip_pvt_lock(p);
27918 if (!(p->trtp)) {
27919 sip_pvt_unlock(p);
27920 return AST_RTP_GLUE_RESULT_FORBID;
27921 }
27922
27923 ao2_ref(p->trtp, +1);
27924 *instance = p->trtp;
27925
27926 if (ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA)) {
27927 res = AST_RTP_GLUE_RESULT_REMOTE;
27928 if (!apply_directmedia_ha(p, "text")) {
27929 res = AST_RTP_GLUE_RESULT_FORBID;
27930 }
27931 }
27932
27933 sip_pvt_unlock(p);
27934
27935 return res;
27936 }
27937
27938 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)
27939 {
27940 struct sip_pvt *p;
27941 int changed = 0;
27942
27943 p = chan->tech_pvt;
27944 if (!p) {
27945 return -1;
27946 }
27947
27948
27949 if (!ast_bridged_channel(chan) && !sip_cfg.directrtpsetup)
27950 return 0;
27951
27952
27953
27954
27955 sip_pvt_lock(p);
27956 while (p->owner && ast_channel_trylock(p->owner)) {
27957 sip_pvt_unlock(p);
27958 usleep(1);
27959 sip_pvt_lock(p);
27960 }
27961
27962 if (!p->owner) {
27963 sip_pvt_unlock(p);
27964 return 0;
27965 }
27966
27967 if (p->alreadygone) {
27968
27969 ast_channel_unlock(p->owner);
27970 sip_pvt_unlock(p);
27971 return 0;
27972 }
27973
27974
27975
27976
27977 if (nat_active && !ast_test_flag(&p->flags[0], SIP_DIRECT_MEDIA_NAT)) {
27978 ast_channel_unlock(p->owner);
27979 sip_pvt_unlock(p);
27980 return 0;
27981 }
27982
27983 if (instance) {
27984 changed |= ast_rtp_instance_get_and_cmp_remote_address(instance, &p->redirip);
27985 } else if (!ast_sockaddr_isnull(&p->redirip)) {
27986 memset(&p->redirip, 0, sizeof(p->redirip));
27987 changed = 1;
27988 }
27989 if (vinstance) {
27990 changed |= ast_rtp_instance_get_and_cmp_remote_address(vinstance, &p->vredirip);
27991 } else if (!ast_sockaddr_isnull(&p->vredirip)) {
27992 memset(&p->vredirip, 0, sizeof(p->vredirip));
27993 changed = 1;
27994 }
27995 if (tinstance) {
27996 changed |= ast_rtp_instance_get_and_cmp_remote_address(tinstance, &p->tredirip);
27997 } else if (!ast_sockaddr_isnull(&p->tredirip)) {
27998 memset(&p->tredirip, 0, sizeof(p->tredirip));
27999 changed = 1;
28000 }
28001 if (codecs && (p->redircodecs != codecs)) {
28002 p->redircodecs = codecs;
28003 changed = 1;
28004 }
28005 if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
28006 if (chan->_state != AST_STATE_UP) {
28007 if (p->do_history)
28008 append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
28009 ast_debug(1, "Early remote bridge setting SIP '%s' - Sending media to %s\n", p->callid, ast_sockaddr_stringify(instance ? &p->redirip : &p->ourip));
28010 } else if (!p->pendinginvite) {
28011 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));
28012 transmit_reinvite_with_sdp(p, FALSE, FALSE);
28013 } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
28014 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));
28015
28016 ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
28017 }
28018 }
28019
28020 p->lastrtprx = p->lastrtptx = time(NULL);
28021 ast_channel_unlock(p->owner);
28022 sip_pvt_unlock(p);
28023 return 0;
28024 }
28025
28026 static format_t sip_get_codec(struct ast_channel *chan)
28027 {
28028 struct sip_pvt *p = chan->tech_pvt;
28029 return p->peercapability ? p->peercapability : p->capability;
28030 }
28031
28032 static struct ast_rtp_glue sip_rtp_glue = {
28033 .type = "SIP",
28034 .get_rtp_info = sip_get_rtp_peer,
28035 .get_vrtp_info = sip_get_vrtp_peer,
28036 .get_trtp_info = sip_get_trtp_peer,
28037 .update_peer = sip_set_rtp_peer,
28038 .get_codec = sip_get_codec,
28039 };
28040
28041 static char *app_dtmfmode = "SIPDtmfMode";
28042 static char *app_sipaddheader = "SIPAddHeader";
28043 static char *app_sipremoveheader = "SIPRemoveHeader";
28044
28045
28046 static int sip_dtmfmode(struct ast_channel *chan, const char *data)
28047 {
28048 struct sip_pvt *p;
28049 const char *mode = data;
28050
28051 if (!data) {
28052 ast_log(LOG_WARNING, "This application requires the argument: info, inband, rfc2833\n");
28053 return 0;
28054 }
28055 ast_channel_lock(chan);
28056 if (!IS_SIP_TECH(chan->tech)) {
28057 ast_log(LOG_WARNING, "Call this application only on SIP incoming calls\n");
28058 ast_channel_unlock(chan);
28059 return 0;
28060 }
28061 p = chan->tech_pvt;
28062 if (!p) {
28063 ast_channel_unlock(chan);
28064 return 0;
28065 }
28066 sip_pvt_lock(p);
28067 if (!strcasecmp(mode, "info")) {
28068 ast_clear_flag(&p->flags[0], SIP_DTMF);
28069 ast_set_flag(&p->flags[0], SIP_DTMF_INFO);
28070 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
28071 } else if (!strcasecmp(mode, "shortinfo")) {
28072 ast_clear_flag(&p->flags[0], SIP_DTMF);
28073 ast_set_flag(&p->flags[0], SIP_DTMF_SHORTINFO);
28074 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
28075 } else if (!strcasecmp(mode, "rfc2833")) {
28076 ast_clear_flag(&p->flags[0], SIP_DTMF);
28077 ast_set_flag(&p->flags[0], SIP_DTMF_RFC2833);
28078 p->jointnoncodeccapability |= AST_RTP_DTMF;
28079 } else if (!strcasecmp(mode, "inband")) {
28080 ast_clear_flag(&p->flags[0], SIP_DTMF);
28081 ast_set_flag(&p->flags[0], SIP_DTMF_INBAND);
28082 p->jointnoncodeccapability &= ~AST_RTP_DTMF;
28083 } else {
28084 ast_log(LOG_WARNING, "I don't know about this dtmf mode: %s\n", mode);
28085 }
28086 if (p->rtp)
28087 ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
28088 if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_INBAND) ||
28089 (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
28090 enable_dsp_detect(p);
28091 } else {
28092 disable_dsp_detect(p);
28093 }
28094 sip_pvt_unlock(p);
28095 ast_channel_unlock(chan);
28096 return 0;
28097 }
28098
28099
28100 static int sip_addheader(struct ast_channel *chan, const char *data)
28101 {
28102 int no = 0;
28103 int ok = FALSE;
28104 char varbuf[30];
28105 const char *inbuf = data;
28106 char *subbuf;
28107
28108 if (ast_strlen_zero(inbuf)) {
28109 ast_log(LOG_WARNING, "This application requires the argument: Header\n");
28110 return 0;
28111 }
28112 ast_channel_lock(chan);
28113
28114
28115 while (!ok && no <= 50) {
28116 no++;
28117 snprintf(varbuf, sizeof(varbuf), "__SIPADDHEADER%.2d", no);
28118
28119
28120 if ((pbx_builtin_getvar_helper(chan, (const char *) varbuf + 2) == (const char *) NULL)) {
28121 ok = TRUE;
28122 }
28123 }
28124 if (ok) {
28125 size_t len = strlen(inbuf);
28126 subbuf = alloca(len + 1);
28127 ast_get_encoded_str(inbuf, subbuf, len + 1);
28128 pbx_builtin_setvar_helper(chan, varbuf, subbuf);
28129 if (sipdebug) {
28130 ast_debug(1, "SIP Header added \"%s\" as %s\n", inbuf, varbuf);
28131 }
28132 } else {
28133 ast_log(LOG_WARNING, "Too many SIP headers added, max 50\n");
28134 }
28135 ast_channel_unlock(chan);
28136 return 0;
28137 }
28138
28139
28140 static int sip_removeheader(struct ast_channel *chan, const char *data)
28141 {
28142 struct ast_var_t *newvariable;
28143 struct varshead *headp;
28144 int removeall = 0;
28145 char *inbuf = (char *) data;
28146
28147 if (ast_strlen_zero(inbuf)) {
28148 removeall = 1;
28149 }
28150 ast_channel_lock(chan);
28151
28152 headp=&chan->varshead;
28153 AST_LIST_TRAVERSE_SAFE_BEGIN (headp, newvariable, entries) {
28154 if (strncasecmp(ast_var_name(newvariable), "SIPADDHEADER", strlen("SIPADDHEADER")) == 0) {
28155 if (removeall || (!strncasecmp(ast_var_value(newvariable),inbuf,strlen(inbuf)))) {
28156 if (sipdebug)
28157 ast_debug(1,"removing SIP Header \"%s\" as %s\n",
28158 ast_var_value(newvariable),
28159 ast_var_name(newvariable));
28160 AST_LIST_REMOVE_CURRENT(entries);
28161 ast_var_delete(newvariable);
28162 }
28163 }
28164 }
28165 AST_LIST_TRAVERSE_SAFE_END;
28166
28167 ast_channel_unlock(chan);
28168 return 0;
28169 }
28170
28171
28172
28173
28174
28175
28176
28177 static int sip_sipredirect(struct sip_pvt *p, const char *dest)
28178 {
28179 char *cdest;
28180 char *extension, *domain;
28181
28182 cdest = ast_strdupa(dest);
28183
28184 extension = strsep(&cdest, "@");
28185 domain = strsep(&cdest, ":");
28186 if (ast_strlen_zero(extension)) {
28187 ast_log(LOG_ERROR, "Missing mandatory argument: extension\n");
28188 return 0;
28189 }
28190
28191
28192 if (!domain) {
28193 char *local_to_header;
28194 char to_header[256];
28195
28196 ast_copy_string(to_header, get_header(&p->initreq, "To"), sizeof(to_header));
28197 if (ast_strlen_zero(to_header)) {
28198 ast_log(LOG_ERROR, "Cannot retrieve the 'To' header from the original SIP request!\n");
28199 return 0;
28200 }
28201 if (((local_to_header = strcasestr(to_header, "sip:")) || (local_to_header = strcasestr(to_header, "sips:")))
28202 && (local_to_header = strchr(local_to_header, '@'))) {
28203 char ldomain[256];
28204
28205 memset(ldomain, 0, sizeof(ldomain));
28206 local_to_header++;
28207
28208 sscanf(local_to_header, "%256[^<>; ]", ldomain);
28209 if (ast_strlen_zero(ldomain)) {
28210 ast_log(LOG_ERROR, "Can't find the host address\n");
28211 return 0;
28212 }
28213 domain = ast_strdupa(ldomain);
28214 }
28215 }
28216
28217 ast_string_field_build(p, our_contact, "Transfer <sip:%s@%s>", extension, domain);
28218 transmit_response_reliable(p, "302 Moved Temporarily", &p->initreq);
28219
28220 sip_scheddestroy(p, SIP_TRANS_TIMEOUT);
28221 sip_alreadygone(p);
28222
28223 if (p->owner) {
28224 enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
28225 ast_queue_control_data(p->owner, AST_CONTROL_TRANSFER, &message, sizeof(message));
28226 }
28227
28228 return 0;
28229 }
28230
28231 static int sip_is_xml_parsable(void)
28232 {
28233 #ifdef HAVE_LIBXML2
28234 return TRUE;
28235 #else
28236 return FALSE;
28237 #endif
28238 }
28239
28240
28241 static void sip_poke_all_peers(void)
28242 {
28243 int ms = 0, num = 0;
28244 struct ao2_iterator i;
28245 struct sip_peer *peer;
28246
28247 if (!speerobjs) {
28248 return;
28249 }
28250
28251 i = ao2_iterator_init(peers, 0);
28252 while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
28253 ao2_lock(peer);
28254 if (num == global_qualify_peers) {
28255 ms += global_qualify_gap;
28256 num = 0;
28257 } else {
28258 num++;
28259 }
28260 AST_SCHED_REPLACE_UNREF(peer->pokeexpire, sched, ms, sip_poke_peer_s, peer,
28261 unref_peer(_data, "removing poke peer ref"),
28262 unref_peer(peer, "removing poke peer ref"),
28263 ref_peer(peer, "adding poke peer ref"));
28264 ao2_unlock(peer);
28265 unref_peer(peer, "toss iterator peer ptr");
28266 }
28267 ao2_iterator_destroy(&i);
28268 }
28269
28270
28271 static void sip_send_all_registers(void)
28272 {
28273 int ms;
28274 int regspacing;
28275 if (!regobjs)
28276 return;
28277 regspacing = default_expiry * 1000/regobjs;
28278 if (regspacing > 100) {
28279 regspacing = 100;
28280 }
28281 ms = regspacing;
28282 ASTOBJ_CONTAINER_TRAVERSE(®l, 1, do {
28283 ASTOBJ_WRLOCK(iterator);
28284 ms += regspacing;
28285 AST_SCHED_REPLACE_UNREF(iterator->expire, sched, ms, sip_reregister, iterator,
28286 registry_unref(_data, "REPLACE sched del decs the refcount"),
28287 registry_unref(iterator, "REPLACE sched add failure decs the refcount"),
28288 registry_addref(iterator, "REPLACE sched add incs the refcount"));
28289 ASTOBJ_UNLOCK(iterator);
28290 } while (0)
28291 );
28292 }
28293
28294
28295 static void sip_send_all_mwi_subscriptions(void)
28296 {
28297 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
28298 ASTOBJ_WRLOCK(iterator);
28299 AST_SCHED_DEL(sched, iterator->resub);
28300 if ((iterator->resub = ast_sched_add(sched, 1, sip_subscribe_mwi_do, ASTOBJ_REF(iterator))) < 0) {
28301 ASTOBJ_UNREF(iterator, sip_subscribe_mwi_destroy);
28302 }
28303 ASTOBJ_UNLOCK(iterator);
28304 } while (0));
28305 }
28306
28307
28308 static int setup_srtp(struct sip_srtp **srtp)
28309 {
28310 if (!ast_rtp_engine_srtp_is_registered()) {
28311 ast_log(LOG_ERROR, "No SRTP module loaded, can't setup SRTP session.\n");
28312 return -1;
28313 }
28314
28315 if (!(*srtp = sip_srtp_alloc())) {
28316 return -1;
28317 }
28318
28319 return 0;
28320 }
28321
28322 static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct sip_srtp **srtp, const char *a)
28323 {
28324 if (strncasecmp(a, "crypto:", 7)) {
28325 return FALSE;
28326 }
28327 if (!*srtp) {
28328 if (ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
28329 ast_log(LOG_WARNING, "Ignoring unexpected crypto attribute in SDP answer\n");
28330 return FALSE;
28331 }
28332
28333 if (setup_srtp(srtp) < 0) {
28334 return FALSE;
28335 }
28336 }
28337
28338
28339 if ((*srtp)->crypto && !ast_test_flag(&p->flags[0], SIP_OUTGOING)) {
28340 ast_debug(3, "We've already processed a crypto attribute, skipping '%s'\n", a);
28341 return FALSE;
28342 }
28343
28344 if (!(*srtp)->crypto && !((*srtp)->crypto = sdp_crypto_setup())) {
28345 return FALSE;
28346 }
28347
28348 if (sdp_crypto_process((*srtp)->crypto, a, rtp) < 0) {
28349 return FALSE;
28350 }
28351
28352 ast_set_flag(*srtp, SRTP_CRYPTO_OFFER_OK);
28353
28354 return TRUE;
28355 }
28356
28357
28358 static int sip_do_reload(enum channelreloadreason reason)
28359 {
28360 time_t start_poke, end_poke;
28361
28362 reload_config(reason);
28363 ast_sched_dump(sched);
28364
28365 start_poke = time(0);
28366
28367 unlink_marked_peers_from_tables();
28368
28369 ast_debug(4, "--------------- Done destroying pruned peers\n");
28370
28371
28372 sip_poke_all_peers();
28373
28374
28375 sip_send_all_registers();
28376
28377 sip_send_all_mwi_subscriptions();
28378
28379 end_poke = time(0);
28380
28381 ast_debug(4, "do_reload finished. peer poke/prune reg contact time = %d sec.\n", (int)(end_poke-start_poke));
28382
28383 ast_debug(4, "--------------- SIP reload done\n");
28384
28385 return 0;
28386 }
28387
28388
28389 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
28390 {
28391
28392 switch (cmd) {
28393 case CLI_INIT:
28394 e->command = "sip reload";
28395 e->usage =
28396 "Usage: sip reload\n"
28397 " Reloads SIP configuration from sip.conf\n";
28398 return NULL;
28399 case CLI_GENERATE:
28400 return NULL;
28401 }
28402
28403 ast_mutex_lock(&sip_reload_lock);
28404 if (sip_reloading) {
28405 ast_verbose("Previous SIP reload not yet done\n");
28406 } else {
28407 sip_reloading = TRUE;
28408 sip_reloadreason = (a && a->fd) ? CHANNEL_CLI_RELOAD : CHANNEL_MODULE_RELOAD;
28409 }
28410 ast_mutex_unlock(&sip_reload_lock);
28411 restart_monitor();
28412
28413 return CLI_SUCCESS;
28414 }
28415
28416
28417 static int reload(void)
28418 {
28419 if (sip_reload(0, 0, NULL))
28420 return 0;
28421 return 1;
28422 }
28423
28424
28425
28426
28427
28428 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
28429 const char* name, int flag, int family)
28430 {
28431 struct ast_sockaddr *addrs;
28432 int addrs_cnt;
28433
28434 addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
28435 if (addrs_cnt <= 0) {
28436 return 1;
28437 }
28438 if (addrs_cnt > 1) {
28439 ast_debug(1, "Multiple addresses, using the first one only\n");
28440 }
28441
28442 ast_sockaddr_copy(addr, &addrs[0]);
28443
28444 ast_free(addrs);
28445 return 0;
28446 }
28447
28448
28449
28450
28451
28452 static int ast_sockaddr_resolve_first(struct ast_sockaddr *addr,
28453 const char* name, int flag)
28454 {
28455 return ast_sockaddr_resolve_first_af(addr, name, flag, get_address_family_filter(&bindaddr));
28456 }
28457
28458
28459
28460
28461 static int peer_hash_cb(const void *obj, const int flags)
28462 {
28463 const struct sip_peer *peer = obj;
28464
28465 return ast_str_case_hash(peer->name);
28466 }
28467
28468
28469
28470
28471 static int peer_cmp_cb(void *obj, void *arg, int flags)
28472 {
28473 struct sip_peer *peer = obj, *peer2 = arg;
28474
28475 return !strcasecmp(peer->name, peer2->name) ? CMP_MATCH | CMP_STOP : 0;
28476 }
28477
28478
28479
28480
28481
28482
28483 static int peer_iphash_cb(const void *obj, const int flags)
28484 {
28485 const struct sip_peer *peer = obj;
28486 int ret = 0;
28487
28488 if (ast_sockaddr_isnull(&peer->addr)) {
28489 ast_log(LOG_ERROR, "Empty address\n");
28490 }
28491
28492 ret = ast_sockaddr_hash(&peer->addr);
28493
28494 if (ret < 0) {
28495 ret = -ret;
28496 }
28497
28498 return ret;
28499 }
28500
28501
28502
28503
28504
28505
28506
28507
28508
28509
28510
28511
28512
28513
28514
28515
28516
28517
28518
28519 static int peer_ipcmp_cb(void *obj, void *arg, int flags)
28520 {
28521 struct sip_peer *peer = obj, *peer2 = arg;
28522
28523 if (ast_sockaddr_cmp_addr(&peer->addr, &peer2->addr)) {
28524
28525 return 0;
28526 }
28527
28528
28529 if ((peer->transports & peer2->transports) & (SIP_TRANSPORT_TLS | SIP_TRANSPORT_TCP)) {
28530
28531 return CMP_MATCH | CMP_STOP;
28532 } else if (ast_test_flag(&peer2->flags[0], SIP_INSECURE_PORT)) {
28533
28534
28535 return ast_test_flag(&peer->flags[0], SIP_INSECURE_PORT) ?
28536 (CMP_MATCH | CMP_STOP) : 0;
28537 }
28538
28539
28540 return ast_sockaddr_port(&peer->addr) == ast_sockaddr_port(&peer2->addr) ?
28541 (CMP_MATCH | CMP_STOP) : 0;
28542 }
28543
28544
28545 static int threadt_hash_cb(const void *obj, const int flags)
28546 {
28547 const struct sip_threadinfo *th = obj;
28548
28549 return ast_sockaddr_hash(&th->tcptls_session->remote_address);
28550 }
28551
28552 static int threadt_cmp_cb(void *obj, void *arg, int flags)
28553 {
28554 struct sip_threadinfo *th = obj, *th2 = arg;
28555
28556 return (th->tcptls_session == th2->tcptls_session) ? CMP_MATCH | CMP_STOP : 0;
28557 }
28558
28559
28560
28561
28562 static int dialog_hash_cb(const void *obj, const int flags)
28563 {
28564 const struct sip_pvt *pvt = obj;
28565
28566 return ast_str_case_hash(pvt->callid);
28567 }
28568
28569
28570
28571
28572 static int dialog_find_multiple(void *obj, void *arg, int flags)
28573 {
28574 struct sip_pvt *pvt = obj, *pvt2 = arg;
28575
28576 return !strcasecmp(pvt->callid, pvt2->callid) ? CMP_MATCH : 0;
28577 }
28578
28579
28580
28581
28582 static int dialog_cmp_cb(void *obj, void *arg, int flags)
28583 {
28584 struct sip_pvt *pvt = obj, *pvt2 = arg;
28585
28586 return !strcasecmp(pvt->callid, pvt2->callid) ? CMP_MATCH | CMP_STOP : 0;
28587 }
28588
28589
28590 static struct ast_cli_entry cli_sip[] = {
28591 AST_CLI_DEFINE(sip_show_channels, "List active SIP channels or subscriptions"),
28592 AST_CLI_DEFINE(sip_show_channelstats, "List statistics for active SIP channels"),
28593 AST_CLI_DEFINE(sip_show_domains, "List our local SIP domains"),
28594 AST_CLI_DEFINE(sip_show_inuse, "List all inuse/limits"),
28595 AST_CLI_DEFINE(sip_show_objects, "List all SIP object allocations"),
28596 AST_CLI_DEFINE(sip_show_peers, "List defined SIP peers"),
28597 AST_CLI_DEFINE(sip_show_registry, "List SIP registration status"),
28598 AST_CLI_DEFINE(sip_unregister, "Unregister (force expiration) a SIP peer from the registry"),
28599 AST_CLI_DEFINE(sip_show_settings, "Show SIP global settings"),
28600 AST_CLI_DEFINE(sip_show_mwi, "Show MWI subscriptions"),
28601 AST_CLI_DEFINE(sip_cli_notify, "Send a notify packet to a SIP peer"),
28602 AST_CLI_DEFINE(sip_show_channel, "Show detailed SIP channel info"),
28603 AST_CLI_DEFINE(sip_show_history, "Show SIP dialog history"),
28604 AST_CLI_DEFINE(sip_show_peer, "Show details on specific SIP peer"),
28605 AST_CLI_DEFINE(sip_show_users, "List defined SIP users"),
28606 AST_CLI_DEFINE(sip_show_user, "Show details on specific SIP user"),
28607 AST_CLI_DEFINE(sip_qualify_peer, "Send an OPTIONS packet to a peer"),
28608 AST_CLI_DEFINE(sip_show_sched, "Present a report on the status of the scheduler queue"),
28609 AST_CLI_DEFINE(sip_prune_realtime, "Prune cached Realtime users/peers"),
28610 AST_CLI_DEFINE(sip_do_debug, "Enable/Disable SIP debugging"),
28611 AST_CLI_DEFINE(sip_set_history, "Enable/Disable SIP history"),
28612 AST_CLI_DEFINE(sip_reload, "Reload SIP configuration"),
28613 AST_CLI_DEFINE(sip_show_tcp, "List TCP Connections")
28614 };
28615
28616
28617 static void sip_register_tests(void)
28618 {
28619 sip_config_parser_register_tests();
28620 sip_request_parser_register_tests();
28621 sip_dialplan_function_register_tests();
28622 }
28623
28624
28625 static void sip_unregister_tests(void)
28626 {
28627 sip_config_parser_unregister_tests();
28628 sip_request_parser_unregister_tests();
28629 sip_dialplan_function_unregister_tests();
28630 }
28631
28632 #ifdef TEST_FRAMEWORK
28633 AST_TEST_DEFINE(test_sip_mwi_subscribe_parse)
28634 {
28635 int found = 0;
28636 enum ast_test_result_state res = AST_TEST_PASS;
28637 const char *mwi1 = "1234@mysipprovider.com/1234";
28638 const char *mwi2 = "1234:password@mysipprovider.com/1234";
28639 const char *mwi3 = "1234:password@mysipprovider.com:5061/1234";
28640 const char *mwi4 = "1234:password:authuser@mysipprovider.com/1234";
28641 const char *mwi5 = "1234:password:authuser@mysipprovider.com:5061/1234";
28642 const char *mwi6 = "1234:password";
28643
28644 switch (cmd) {
28645 case TEST_INIT:
28646 info->name = "sip_mwi_subscribe_parse_test";
28647 info->category = "/channels/chan_sip/";
28648 info->summary = "SIP MWI subscribe line parse unit test";
28649 info->description =
28650 "Tests the parsing of mwi subscription lines (e.g., mwi => from sip.conf)";
28651 return AST_TEST_NOT_RUN;
28652 case TEST_EXECUTE:
28653 break;
28654 }
28655
28656 if (sip_subscribe_mwi(mwi1, 1)) {
28657 res = AST_TEST_FAIL;
28658 } else {
28659 found = 0;
28660 res = AST_TEST_FAIL;
28661 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
28662 ASTOBJ_WRLOCK(iterator);
28663 if (
28664 !strcmp(iterator->hostname, "mysipprovider.com") &&
28665 !strcmp(iterator->username, "1234") &&
28666 !strcmp(iterator->secret, "") &&
28667 !strcmp(iterator->authuser, "") &&
28668 !strcmp(iterator->mailbox, "1234") &&
28669 iterator->portno == 0) {
28670 found = 1;
28671 res = AST_TEST_PASS;
28672 }
28673 ASTOBJ_UNLOCK(iterator);
28674 } while(0));
28675 if (!found) {
28676 ast_test_status_update(test, "sip_subscribe_mwi test 1 failed\n");
28677 }
28678 }
28679
28680 if (sip_subscribe_mwi(mwi2, 1)) {
28681 res = AST_TEST_FAIL;
28682 } else {
28683 found = 0;
28684 res = AST_TEST_FAIL;
28685 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
28686 ASTOBJ_WRLOCK(iterator);
28687 if (
28688 !strcmp(iterator->hostname, "mysipprovider.com") &&
28689 !strcmp(iterator->username, "1234") &&
28690 !strcmp(iterator->secret, "password") &&
28691 !strcmp(iterator->authuser, "") &&
28692 !strcmp(iterator->mailbox, "1234") &&
28693 iterator->portno == 0) {
28694 found = 1;
28695 res = AST_TEST_PASS;
28696 }
28697 ASTOBJ_UNLOCK(iterator);
28698 } while(0));
28699 if (!found) {
28700 ast_test_status_update(test, "sip_subscribe_mwi test 2 failed\n");
28701 }
28702 }
28703
28704 if (sip_subscribe_mwi(mwi3, 1)) {
28705 res = AST_TEST_FAIL;
28706 } else {
28707 found = 0;
28708 res = AST_TEST_FAIL;
28709 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
28710 ASTOBJ_WRLOCK(iterator);
28711 if (
28712 !strcmp(iterator->hostname, "mysipprovider.com") &&
28713 !strcmp(iterator->username, "1234") &&
28714 !strcmp(iterator->secret, "password") &&
28715 !strcmp(iterator->authuser, "") &&
28716 !strcmp(iterator->mailbox, "1234") &&
28717 iterator->portno == 5061) {
28718 found = 1;
28719 res = AST_TEST_PASS;
28720 }
28721 ASTOBJ_UNLOCK(iterator);
28722 } while(0));
28723 if (!found) {
28724 ast_test_status_update(test, "sip_subscribe_mwi test 3 failed\n");
28725 }
28726 }
28727
28728 if (sip_subscribe_mwi(mwi4, 1)) {
28729 res = AST_TEST_FAIL;
28730 } else {
28731 found = 0;
28732 res = AST_TEST_FAIL;
28733 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
28734 ASTOBJ_WRLOCK(iterator);
28735 if (
28736 !strcmp(iterator->hostname, "mysipprovider.com") &&
28737 !strcmp(iterator->username, "1234") &&
28738 !strcmp(iterator->secret, "password") &&
28739 !strcmp(iterator->authuser, "authuser") &&
28740 !strcmp(iterator->mailbox, "1234") &&
28741 iterator->portno == 0) {
28742 found = 1;
28743 res = AST_TEST_PASS;
28744 }
28745 ASTOBJ_UNLOCK(iterator);
28746 } while(0));
28747 if (!found) {
28748 ast_test_status_update(test, "sip_subscribe_mwi test 4 failed\n");
28749 }
28750 }
28751
28752 if (sip_subscribe_mwi(mwi5, 1)) {
28753 res = AST_TEST_FAIL;
28754 } else {
28755 found = 0;
28756 res = AST_TEST_FAIL;
28757 ASTOBJ_CONTAINER_TRAVERSE(&submwil, 1, do {
28758 ASTOBJ_WRLOCK(iterator);
28759 if (
28760 !strcmp(iterator->hostname, "mysipprovider.com") &&
28761 !strcmp(iterator->username, "1234") &&
28762 !strcmp(iterator->secret, "password") &&
28763 !strcmp(iterator->authuser, "authuser") &&
28764 !strcmp(iterator->mailbox, "1234") &&
28765 iterator->portno == 5061) {
28766 found = 1;
28767 res = AST_TEST_PASS;
28768 }
28769 ASTOBJ_UNLOCK(iterator);
28770 } while(0));
28771 if (!found) {
28772 ast_test_status_update(test, "sip_subscribe_mwi test 5 failed\n");
28773 }
28774 }
28775
28776 if (sip_subscribe_mwi(mwi6, 1)) {
28777 res = AST_TEST_PASS;
28778 } else {
28779 res = AST_TEST_FAIL;
28780 }
28781 return res;
28782 }
28783
28784 AST_TEST_DEFINE(test_sip_peers_get)
28785 {
28786 struct sip_peer *peer;
28787 struct ast_data *node;
28788 struct ast_data_query query = {
28789 .path = "/asterisk/channel/sip/peers",
28790 .search = "peers/peer/name=test_peer_data_provider"
28791 };
28792
28793 switch (cmd) {
28794 case TEST_INIT:
28795 info->name = "sip_peers_get_data_test";
28796 info->category = "/main/data/sip/peers/";
28797 info->summary = "SIP peers data providers unit test";
28798 info->description =
28799 "Tests whether the SIP peers data provider implementation works as expected.";
28800 return AST_TEST_NOT_RUN;
28801 case TEST_EXECUTE:
28802 break;
28803 }
28804
28805
28806 peer = build_peer("test_peer_data_provider", NULL, NULL, 0, 0);
28807 if (!peer) {
28808 return AST_TEST_FAIL;
28809 }
28810 peer->type = SIP_TYPE_USER;
28811 peer->call_limit = 10;
28812 ao2_link(peers, peer);
28813
28814
28815 node = ast_data_get(&query);
28816 if (!node) {
28817 ao2_unlink(peers, peer);
28818 ao2_ref(peer, -1);
28819 return AST_TEST_FAIL;
28820 }
28821
28822
28823 if (strcmp(ast_data_retrieve_string(node, "peer/name"), "test_peer_data_provider")) {
28824 ao2_unlink(peers, peer);
28825 ao2_ref(peer, -1);
28826 ast_data_free(node);
28827 return AST_TEST_FAIL;
28828 }
28829
28830 if (strcmp(ast_data_retrieve_string(node, "peer/type"), "user")) {
28831 ao2_unlink(peers, peer);
28832 ao2_ref(peer, -1);
28833 ast_data_free(node);
28834 return AST_TEST_FAIL;
28835 }
28836
28837 if (ast_data_retrieve_int(node, "peer/call_limit") != 10) {
28838 ao2_unlink(peers, peer);
28839 ao2_ref(peer, -1);
28840 ast_data_free(node);
28841 return AST_TEST_FAIL;
28842 }
28843
28844
28845 ast_data_free(node);
28846
28847 ao2_unlink(peers, peer);
28848 ao2_ref(peer, -1);
28849
28850 return AST_TEST_PASS;
28851 }
28852
28853 #endif
28854
28855 #define DATA_EXPORT_SIP_PEER(MEMBER) \
28856 MEMBER(sip_peer, name, AST_DATA_STRING) \
28857 MEMBER(sip_peer, secret, AST_DATA_PASSWORD) \
28858 MEMBER(sip_peer, md5secret, AST_DATA_PASSWORD) \
28859 MEMBER(sip_peer, remotesecret, AST_DATA_PASSWORD) \
28860 MEMBER(sip_peer, context, AST_DATA_STRING) \
28861 MEMBER(sip_peer, subscribecontext, AST_DATA_STRING) \
28862 MEMBER(sip_peer, username, AST_DATA_STRING) \
28863 MEMBER(sip_peer, accountcode, AST_DATA_STRING) \
28864 MEMBER(sip_peer, tohost, AST_DATA_STRING) \
28865 MEMBER(sip_peer, regexten, AST_DATA_STRING) \
28866 MEMBER(sip_peer, fromuser, AST_DATA_STRING) \
28867 MEMBER(sip_peer, fromdomain, AST_DATA_STRING) \
28868 MEMBER(sip_peer, fullcontact, AST_DATA_STRING) \
28869 MEMBER(sip_peer, cid_num, AST_DATA_STRING) \
28870 MEMBER(sip_peer, cid_name, AST_DATA_STRING) \
28871 MEMBER(sip_peer, vmexten, AST_DATA_STRING) \
28872 MEMBER(sip_peer, language, AST_DATA_STRING) \
28873 MEMBER(sip_peer, mohinterpret, AST_DATA_STRING) \
28874 MEMBER(sip_peer, mohsuggest, AST_DATA_STRING) \
28875 MEMBER(sip_peer, parkinglot, AST_DATA_STRING) \
28876 MEMBER(sip_peer, useragent, AST_DATA_STRING) \
28877 MEMBER(sip_peer, mwi_from, AST_DATA_STRING) \
28878 MEMBER(sip_peer, engine, AST_DATA_STRING) \
28879 MEMBER(sip_peer, unsolicited_mailbox, AST_DATA_STRING) \
28880 MEMBER(sip_peer, is_realtime, AST_DATA_BOOLEAN) \
28881 MEMBER(sip_peer, host_dynamic, AST_DATA_BOOLEAN) \
28882 MEMBER(sip_peer, autoframing, AST_DATA_BOOLEAN) \
28883 MEMBER(sip_peer, inUse, AST_DATA_INTEGER) \
28884 MEMBER(sip_peer, inRinging, AST_DATA_INTEGER) \
28885 MEMBER(sip_peer, onHold, AST_DATA_INTEGER) \
28886 MEMBER(sip_peer, call_limit, AST_DATA_INTEGER) \
28887 MEMBER(sip_peer, t38_maxdatagram, AST_DATA_INTEGER) \
28888 MEMBER(sip_peer, maxcallbitrate, AST_DATA_INTEGER) \
28889 MEMBER(sip_peer, rtptimeout, AST_DATA_SECONDS) \
28890 MEMBER(sip_peer, rtpholdtimeout, AST_DATA_SECONDS) \
28891 MEMBER(sip_peer, rtpkeepalive, AST_DATA_SECONDS) \
28892 MEMBER(sip_peer, lastms, AST_DATA_MILLISECONDS) \
28893 MEMBER(sip_peer, maxms, AST_DATA_MILLISECONDS) \
28894 MEMBER(sip_peer, qualifyfreq, AST_DATA_MILLISECONDS) \
28895 MEMBER(sip_peer, timer_t1, AST_DATA_MILLISECONDS) \
28896 MEMBER(sip_peer, timer_b, AST_DATA_MILLISECONDS)
28897
28898 AST_DATA_STRUCTURE(sip_peer, DATA_EXPORT_SIP_PEER);
28899
28900 static int peers_data_provider_get(const struct ast_data_search *search,
28901 struct ast_data *data_root)
28902 {
28903 struct sip_peer *peer;
28904 struct ao2_iterator i;
28905 struct ast_data *data_peer, *data_peer_mailboxes = NULL, *data_peer_mailbox, *enum_node;
28906 struct ast_data *data_sip_options;
28907 int total_mailboxes, x;
28908 struct sip_mailbox *mailbox;
28909
28910 i = ao2_iterator_init(peers, 0);
28911 while ((peer = ao2_iterator_next(&i))) {
28912 ao2_lock(peer);
28913
28914 data_peer = ast_data_add_node(data_root, "peer");
28915 if (!data_peer) {
28916 ao2_unlock(peer);
28917 ao2_ref(peer, -1);
28918 continue;
28919 }
28920
28921 ast_data_add_structure(sip_peer, data_peer, peer);
28922
28923
28924 enum_node = ast_data_add_node(data_peer, "allowtransfer");
28925 if (!enum_node) {
28926 continue;
28927 }
28928 ast_data_add_str(enum_node, "text", transfermode2str(peer->allowtransfer));
28929 ast_data_add_int(enum_node, "value", peer->allowtransfer);
28930
28931
28932 ast_data_add_str(data_peer, "transports", get_transport_list(peer->transports));
28933
28934
28935 if ((peer->type & SIP_TYPE_USER) && (peer->type & SIP_TYPE_PEER)) {
28936 ast_data_add_str(data_peer, "type", "friend");
28937 } else if (peer->type & SIP_TYPE_PEER) {
28938 ast_data_add_str(data_peer, "type", "peer");
28939 } else if (peer->type & SIP_TYPE_USER) {
28940 ast_data_add_str(data_peer, "type", "user");
28941 }
28942
28943
28944 total_mailboxes = 0;
28945 AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
28946 if (!total_mailboxes) {
28947 data_peer_mailboxes = ast_data_add_node(data_peer, "mailboxes");
28948 if (!data_peer_mailboxes) {
28949 break;
28950 }
28951 total_mailboxes++;
28952 }
28953
28954 data_peer_mailbox = ast_data_add_node(data_peer_mailboxes, "mailbox");
28955 if (!data_peer_mailbox) {
28956 continue;
28957 }
28958 ast_data_add_str(data_peer_mailbox, "mailbox", mailbox->mailbox);
28959 ast_data_add_str(data_peer_mailbox, "context", mailbox->context);
28960 }
28961
28962
28963 enum_node = ast_data_add_node(data_peer, "amaflags");
28964 if (!enum_node) {
28965 continue;
28966 }
28967 ast_data_add_int(enum_node, "value", peer->amaflags);
28968 ast_data_add_str(enum_node, "text", ast_cdr_flags2str(peer->amaflags));
28969
28970
28971 data_sip_options = ast_data_add_node(data_peer, "sipoptions");
28972 if (!data_sip_options) {
28973 continue;
28974 }
28975 for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
28976 ast_data_add_bool(data_sip_options, sip_options[x].text, peer->sipoptions & sip_options[x].id);
28977 }
28978
28979
28980 enum_node = ast_data_add_node(data_peer, "callingpres");
28981 if (!enum_node) {
28982 continue;
28983 }
28984 ast_data_add_int(enum_node, "value", peer->callingpres);
28985 ast_data_add_str(enum_node, "text", ast_describe_caller_presentation(peer->callingpres));
28986
28987
28988 ast_data_add_codecs(data_peer, "codecs", peer->capability);
28989
28990 if (!ast_data_search_match(search, data_peer)) {
28991 ast_data_remove_node(data_root, data_peer);
28992 }
28993
28994 ao2_unlock(peer);
28995 ao2_ref(peer, -1);
28996 }
28997 ao2_iterator_destroy(&i);
28998
28999 return 0;
29000 }
29001
29002 static const struct ast_data_handler peers_data_provider = {
29003 .version = AST_DATA_HANDLER_VERSION,
29004 .get = peers_data_provider_get
29005 };
29006
29007 static const struct ast_data_entry sip_data_providers[] = {
29008 AST_DATA_ENTRY("asterisk/channel/sip/peers", &peers_data_provider),
29009 };
29010
29011
29012 static int load_module(void)
29013 {
29014 ast_verbose("SIP channel loading...\n");
29015
29016
29017 peers = ao2_t_container_alloc(HASH_PEER_SIZE, peer_hash_cb, peer_cmp_cb, "allocate peers");
29018 peers_by_ip = ao2_t_container_alloc(HASH_PEER_SIZE, peer_iphash_cb, peer_ipcmp_cb, "allocate peers_by_ip");
29019 dialogs = ao2_t_container_alloc(HASH_DIALOG_SIZE, dialog_hash_cb, dialog_cmp_cb, "allocate dialogs");
29020 threadt = ao2_t_container_alloc(HASH_DIALOG_SIZE, threadt_hash_cb, threadt_cmp_cb, "allocate threadt table");
29021
29022 ASTOBJ_CONTAINER_INIT(®l);
29023 ASTOBJ_CONTAINER_INIT(&submwil);
29024
29025 if (!(sched = sched_context_create())) {
29026 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
29027 return AST_MODULE_LOAD_FAILURE;
29028 }
29029
29030 if (!(io = io_context_create())) {
29031 ast_log(LOG_ERROR, "Unable to create I/O context\n");
29032 sched_context_destroy(sched);
29033 return AST_MODULE_LOAD_FAILURE;
29034 }
29035
29036 sip_reloadreason = CHANNEL_MODULE_LOAD;
29037
29038 can_parse_xml = sip_is_xml_parsable();
29039 if(reload_config(sip_reloadreason)) {
29040 return AST_MODULE_LOAD_DECLINE;
29041 }
29042
29043
29044
29045
29046
29047 memcpy(&sip_tech_info, &sip_tech, sizeof(sip_tech));
29048 memset((void *) &sip_tech_info.send_digit_begin, 0, sizeof(sip_tech_info.send_digit_begin));
29049
29050
29051 if (ast_channel_register(&sip_tech)) {
29052 ast_log(LOG_ERROR, "Unable to register channel type 'SIP'\n");
29053 io_context_destroy(io);
29054 sched_context_destroy(sched);
29055 return AST_MODULE_LOAD_FAILURE;
29056 }
29057
29058 #ifdef TEST_FRAMEWORK
29059 AST_TEST_REGISTER(test_sip_peers_get);
29060 AST_TEST_REGISTER(test_sip_mwi_subscribe_parse);
29061 #endif
29062
29063
29064 ast_data_register_multiple(sip_data_providers, ARRAY_LEN(sip_data_providers));
29065
29066
29067 ast_cli_register_multiple(cli_sip, ARRAY_LEN(cli_sip));
29068
29069
29070 ast_udptl_proto_register(&sip_udptl);
29071
29072
29073 ast_rtp_glue_register(&sip_rtp_glue);
29074
29075
29076 ast_register_application_xml(app_dtmfmode, sip_dtmfmode);
29077 ast_register_application_xml(app_sipaddheader, sip_addheader);
29078 ast_register_application_xml(app_sipremoveheader, sip_removeheader);
29079
29080
29081 ast_custom_function_register(&sip_header_function);
29082 ast_custom_function_register(&sippeer_function);
29083 ast_custom_function_register(&sipchaninfo_function);
29084 ast_custom_function_register(&checksipdomain_function);
29085
29086
29087 ast_manager_register_xml("SIPpeers", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peers);
29088 ast_manager_register_xml("SIPshowpeer", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_show_peer);
29089 ast_manager_register_xml("SIPqualifypeer", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_sip_qualify_peer);
29090 ast_manager_register_xml("SIPshowregistry", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_show_registry);
29091 ast_manager_register_xml("SIPnotify", EVENT_FLAG_SYSTEM, manager_sipnotify);
29092 sip_poke_all_peers();
29093 sip_send_all_registers();
29094 sip_send_all_mwi_subscriptions();
29095 initialize_escs();
29096
29097 if (sip_epa_register(&cc_epa_static_data)) {
29098 return AST_MODULE_LOAD_DECLINE;
29099 }
29100
29101 if (sip_reqresp_parser_init() == -1) {
29102 ast_log(LOG_ERROR, "Unable to initialize the SIP request and response parser\n");
29103 return AST_MODULE_LOAD_DECLINE;
29104 }
29105
29106 if (can_parse_xml) {
29107
29108
29109
29110 if (ast_cc_agent_register(&sip_cc_agent_callbacks)) {
29111 return AST_MODULE_LOAD_DECLINE;
29112 }
29113 }
29114 if (ast_cc_monitor_register(&sip_cc_monitor_callbacks)) {
29115 return AST_MODULE_LOAD_DECLINE;
29116 }
29117 if (!(sip_monitor_instances = ao2_container_alloc(37, sip_monitor_instance_hash_fn, sip_monitor_instance_cmp_fn))) {
29118 return AST_MODULE_LOAD_DECLINE;
29119 }
29120
29121
29122 restart_monitor();
29123
29124 ast_realtime_require_field(ast_check_realtime("sipregs") ? "sipregs" : "sippeers",
29125 "name", RQ_CHAR, 10,
29126 "ipaddr", RQ_CHAR, INET6_ADDRSTRLEN - 1,
29127 "port", RQ_UINTEGER2, 5,
29128 "regseconds", RQ_INTEGER4, 11,
29129 "defaultuser", RQ_CHAR, 10,
29130 "fullcontact", RQ_CHAR, 35,
29131 "regserver", RQ_CHAR, 20,
29132 "useragent", RQ_CHAR, 20,
29133 "lastms", RQ_INTEGER4, 11,
29134 SENTINEL);
29135
29136
29137 sip_register_tests();
29138 network_change_event_subscribe();
29139
29140 return AST_MODULE_LOAD_SUCCESS;
29141 }
29142
29143
29144 static int unload_module(void)
29145 {
29146 struct sip_pvt *p;
29147 struct sip_threadinfo *th;
29148 struct ast_context *con;
29149 struct ao2_iterator i;
29150
29151 network_change_event_unsubscribe();
29152
29153 ast_sched_dump(sched);
29154
29155
29156 ast_channel_unregister(&sip_tech);
29157
29158
29159 ast_custom_function_unregister(&sipchaninfo_function);
29160 ast_custom_function_unregister(&sippeer_function);
29161 ast_custom_function_unregister(&sip_header_function);
29162 ast_custom_function_unregister(&checksipdomain_function);
29163
29164
29165 ast_unregister_application(app_dtmfmode);
29166 ast_unregister_application(app_sipaddheader);
29167 ast_unregister_application(app_sipremoveheader);
29168
29169 #ifdef TEST_FRAMEWORK
29170 AST_TEST_UNREGISTER(test_sip_peers_get);
29171 AST_TEST_UNREGISTER(test_sip_mwi_subscribe_parse);
29172 #endif
29173
29174 ast_data_unregister(NULL);
29175
29176
29177 ast_cli_unregister_multiple(cli_sip, ARRAY_LEN(cli_sip));
29178
29179
29180 ast_udptl_proto_unregister(&sip_udptl);
29181
29182
29183 ast_rtp_glue_unregister(&sip_rtp_glue);
29184
29185
29186 ast_manager_unregister("SIPpeers");
29187 ast_manager_unregister("SIPshowpeer");
29188 ast_manager_unregister("SIPqualifypeer");
29189 ast_manager_unregister("SIPshowregistry");
29190 ast_manager_unregister("SIPnotify");
29191
29192
29193 if (sip_tcp_desc.master) {
29194 ast_tcptls_server_stop(&sip_tcp_desc);
29195 }
29196 if (sip_tls_desc.master) {
29197 ast_tcptls_server_stop(&sip_tls_desc);
29198 }
29199
29200
29201 i = ao2_iterator_init(threadt, 0);
29202 while ((th = ao2_t_iterator_next(&i, "iterate through tcp threads for 'sip show tcp'"))) {
29203 pthread_t thread = th->threadid;
29204 th->stop = 1;
29205 pthread_kill(thread, SIGURG);
29206 pthread_join(thread, NULL);
29207 ao2_t_ref(th, -1, "decrement ref from iterator");
29208 }
29209 ao2_iterator_destroy(&i);
29210
29211
29212 i = ao2_iterator_init(dialogs, 0);
29213 while ((p = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
29214 if (p->owner)
29215 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
29216 ao2_t_ref(p, -1, "toss dialog ptr from iterator_next");
29217 }
29218 ao2_iterator_destroy(&i);
29219
29220 ast_mutex_lock(&monlock);
29221 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
29222 pthread_cancel(monitor_thread);
29223 pthread_kill(monitor_thread, SIGURG);
29224 pthread_join(monitor_thread, NULL);
29225 }
29226 monitor_thread = AST_PTHREADT_STOP;
29227 ast_mutex_unlock(&monlock);
29228
29229
29230 i = ao2_iterator_init(dialogs, 0);
29231 while ((p = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
29232 dialog_unlink_all(p, TRUE, TRUE);
29233 ao2_t_ref(p, -1, "throw away iterator result");
29234 }
29235 ao2_iterator_destroy(&i);
29236
29237
29238 ast_free_ha(localaddr);
29239
29240 clear_realm_authentication(authl);
29241
29242 destroy_escs();
29243
29244 if (default_tls_cfg.certfile) {
29245 ast_free(default_tls_cfg.certfile);
29246 }
29247 if (default_tls_cfg.pvtfile) {
29248 ast_free(default_tls_cfg.pvtfile);
29249 }
29250 if (default_tls_cfg.cipher) {
29251 ast_free(default_tls_cfg.cipher);
29252 }
29253 if (default_tls_cfg.cafile) {
29254 ast_free(default_tls_cfg.cafile);
29255 }
29256 if (default_tls_cfg.capath) {
29257 ast_free(default_tls_cfg.capath);
29258 }
29259
29260 ASTOBJ_CONTAINER_DESTROYALL(®l, sip_registry_destroy);
29261 ASTOBJ_CONTAINER_DESTROY(®l);
29262 ASTOBJ_CONTAINER_DESTROYALL(&submwil, sip_subscribe_mwi_destroy);
29263 ASTOBJ_CONTAINER_DESTROY(&submwil);
29264
29265 ao2_t_ref(peers, -1, "unref the peers table");
29266 ao2_t_ref(peers_by_ip, -1, "unref the peers_by_ip table");
29267 ao2_t_ref(dialogs, -1, "unref the dialogs table");
29268 ao2_t_ref(threadt, -1, "unref the thread table");
29269
29270 clear_sip_domains();
29271 ast_free_ha(sip_cfg.contact_ha);
29272 close(sipsock);
29273 sched_context_destroy(sched);
29274 con = ast_context_find(used_context);
29275 if (con) {
29276 ast_context_destroy(con, "SIP");
29277 }
29278 ast_unload_realtime("sipregs");
29279 ast_unload_realtime("sippeers");
29280 ast_cc_monitor_unregister(&sip_cc_monitor_callbacks);
29281 ast_cc_agent_unregister(&sip_cc_agent_callbacks);
29282
29283 sip_reqresp_parser_exit();
29284 sip_unregister_tests();
29285
29286 return 0;
29287 }
29288
29289 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Session Initiation Protocol (SIP)",
29290 .load = load_module,
29291 .unload = unload_module,
29292 .reload = reload,
29293 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
29294 .nonoptreq = "res_crypto,chan_local",
29295 );